Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit,cy
h.Padding = (buf[0] >> paddingShift & paddingMask) > 0
h.Extension = (buf[0] >> extensionShift & extensionMask) > 0
nCSRC := int(buf[0] & ccMask)
if cap(h.CSRC) < nCSRC || h.CSRC == nil {
if cap(h.CSRC) < nCSRC {
h.CSRC = make([]uint32, nCSRC)
} else {
h.CSRC = h.CSRC[:nCSRC]
Expand All @@ -153,9 +153,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit,cy
h.CSRC[i] = binary.BigEndian.Uint32(buf[offset:])
}

if h.Extensions != nil {
h.Extensions = h.Extensions[:0]
}
h.Extensions = h.Extensions[:0]

if h.Extension { // nolint: nestif
if expected := n + 4; len(buf) < expected {
Expand Down
66 changes: 35 additions & 31 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 0,
},
Payload: rawPkt[20:],
Expand Down Expand Up @@ -78,7 +77,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 4,
},
Payload: rawPkt[20:21],
Expand Down Expand Up @@ -108,7 +106,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 0,
},
Payload: rawPkt[20:],
Expand Down Expand Up @@ -137,7 +134,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 5,
},
Payload: []byte{},
Expand Down Expand Up @@ -168,7 +164,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 0,
},
Payload: []byte{},
Expand Down Expand Up @@ -198,7 +193,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 4,
},
Payload: rawPkt[20:21],
Expand Down Expand Up @@ -228,7 +222,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 5,
},
Payload: []byte{},
Expand Down Expand Up @@ -258,7 +251,6 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 5,
},
Payload: []byte{},
Expand Down Expand Up @@ -330,7 +322,6 @@ func TestRFC8285OneByteExtension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
}
Expand Down Expand Up @@ -384,7 +375,6 @@ func TestRFC8285OneByteTwoExtensionOfTwoBytes(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
}
Expand Down Expand Up @@ -493,7 +483,6 @@ func TestRFC8285OneByteMultipleExtensions(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[28:],
}
Expand Down Expand Up @@ -531,7 +520,6 @@ func TestRFC8285TwoByteExtension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[44:],
}
Expand Down Expand Up @@ -645,7 +633,6 @@ func TestRFC8285TwoByteMultipleExtensionsWithLargeExtension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[40:],
}
Expand All @@ -667,7 +654,6 @@ func TestRFC8285GetExtensionReturnsNilWhenExtensionsDisabled(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand All @@ -694,7 +680,6 @@ func TestRFC8285DelExtension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -727,7 +712,6 @@ func TestRFC8285GetExtensionIDs(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -755,7 +739,6 @@ func TestRFC8285GetExtensionIDsReturnsErrorWhenExtensionsDisabled(t *testing.T)
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand All @@ -776,7 +759,6 @@ func TestRFC8285DelExtensionReturnsErrorWhenExtensionsDisabled(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand All @@ -799,7 +781,6 @@ func TestRFC8285OneByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T)
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand All @@ -825,7 +806,6 @@ func TestRFC8285OneByteSetExtensionShouldSetCorrectExtensionProfileFor16ByteExte
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -861,7 +841,6 @@ func TestRFC8285OneByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -893,7 +872,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenInvalidIDProvided(t *testing.T
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -961,7 +939,6 @@ func TestRFC8285OneByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T)
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -989,7 +966,6 @@ func TestRFC8285TwoByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T)
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -1025,7 +1001,6 @@ func TestRFC8285TwoByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -1060,7 +1035,6 @@ func TestRFC8285TwoByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T)
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -1142,7 +1116,6 @@ func TestRFC3550SetExtensionShouldErrorWhenNonZero(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand All @@ -1169,7 +1142,6 @@ func TestRFC3550SetExtensionShouldRaiseErrorWhenSettingNonzeroID(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: payload,
}
Expand Down Expand Up @@ -1335,7 +1307,6 @@ func TestCloneHeader(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
}
clone := header.Clone()
assert.Equal(t, header, clone)
Expand Down Expand Up @@ -1385,7 +1356,6 @@ func TestMarshalRTPPacketFuncs(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
PaddingSize: 5,
},
Payload: []byte{},
Expand Down Expand Up @@ -1426,7 +1396,6 @@ func TestDeprecatedPaddingSizeField(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: []byte{},
PaddingSize: 5,
Expand Down Expand Up @@ -1555,3 +1524,38 @@ func TestMarshalToPanic(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 16, n)
}

func BenchmarkUnmarshalHeader(b *testing.B) {
rawPkt := []byte{
0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda,
0x1c, 0x64, 0x27, 0x82, 0xBE, 0xDE, 0x00, 0x01,
0x50, 0xAA, 0x00, 0x00, 0x98, 0x36, 0xbe, 0x88,
}
b.Run("NewStructWithoutCSRC", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := &Header{}
if _, err := h.Unmarshal(rawPkt); err != nil {
b.Fatal(err)
}
}
})

rawPkt = []byte{
0x92, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda,
0x1c, 0x64, 0x27, 0x82, 0x00, 0x00, 0x11, 0x11,
0x00, 0x00, 0x22, 0x22, 0xBE, 0xDE, 0x00, 0x01,
0x50, 0xAA, 0x00, 0x00, 0x98, 0x36, 0xbe, 0x88,
}
b.Run("NewStructWithCSRC", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := &Header{}
if _, err := h.Unmarshal(rawPkt); err != nil {
b.Fatal(err)
}
}
})
}
2 changes: 0 additions & 2 deletions packetizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func (p *packetizer) Packetize(payload []byte, samples uint32) []*Packet {
SequenceNumber: p.Sequencer.NextSequenceNumber(),
Timestamp: p.Timestamp, // Figure out how to do timestamps
SSRC: p.SSRC,
CSRC: []uint32{},
},
Payload: pp,
}
Expand Down Expand Up @@ -175,7 +174,6 @@ func (p *packetizer) GeneratePadding(samples uint32) []*Packet {
SequenceNumber: p.Sequencer.NextSequenceNumber(),
Timestamp: p.Timestamp, // Use latest timestamp
SSRC: p.SSRC,
CSRC: []uint32{},
PaddingSize: 255,
},
}
Expand Down
1 change: 0 additions & 1 deletion packetizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func TestPacketizer_AbsSendTime(t *testing.T) {
SequenceNumber: 1234,
Timestamp: 45678,
SSRC: 0x1234ABCD,
CSRC: []uint32{},
ExtensionProfile: 0xBEDE,
Extensions: []Extension{
{
Expand Down
Loading