Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unmarshal header #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit
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 @@ -124,9 +124,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit
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 {
if expected := n + 4; len(buf) < expected {
Expand Down Expand Up @@ -159,7 +157,7 @@ func (h *Header) Unmarshal(buf []byte) (n int, err error) { //nolint:gocognit
}

extid := buf[n] >> 4
payloadLen := int(buf[n]&^0xF0 + 1)
payloadLen := int(buf[n]&0x0F + 1)
n++

if extid == extensionIDReserved {
Expand Down
44 changes: 40 additions & 4 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestBasic(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
CSRC: nil,
},
Payload: rawPkt[20:],
PaddingSize: 0,
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestBasic(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
CSRC: nil,
},
Payload: rawPkt[20:21],
PaddingSize: 4,
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestBasic(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
CSRC: nil,
},
Payload: []byte{},
PaddingSize: 5,
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestBasic(t *testing.T) {
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
CSRC: nil,
},
Payload: []byte{},
PaddingSize: 0,
Expand Down Expand Up @@ -1485,3 +1485,39 @@ func BenchmarkUnmarshal(b *testing.B) {
}
})
}

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: 1 addition & 1 deletion packetizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ 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{},
CSRC: nil,
},
Payload: pp,
}
Expand Down
2 changes: 1 addition & 1 deletion packetizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestPacketizer_AbsSendTime(t *testing.T) {
SequenceNumber: 1234,
Timestamp: 45678,
SSRC: 0x1234ABCD,
CSRC: []uint32{},
CSRC: nil,
ExtensionProfile: 0xBEDE,
Extensions: []Extension{
{
Expand Down