Skip to content

Commit

Permalink
Set CSRC as a empty (not nil) slice by default
Browse files Browse the repository at this point in the history
Allows packetizer marshal / unmarshal to be consistent

Resolves #79
  • Loading branch information
Aaron Boushley authored and Sean-Der committed Jun 14, 2023
1 parent abb1453 commit 6ef1c2c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Header struct {
// NOTE: Raw is populated by Marshal/Unmarshal and should not be modified
type Packet struct {
Header
Raw []byte
Raw []byte
Payload []byte
PaddingSize byte
}
Expand Down
1 change: 1 addition & 0 deletions packetizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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{},
},
Payload: pp,
}
Expand Down
80 changes: 79 additions & 1 deletion packetizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestPacketizer_AbsSendTime(t *testing.T) {
SequenceNumber: 1234,
Timestamp: 45678,
SSRC: 0x1234ABCD,
CSRC: nil,
CSRC: []uint32{},
ExtensionProfile: 0xBEDE,
Extensions: []Extension{
{
Expand All @@ -72,3 +72,81 @@ func TestPacketizer_AbsSendTime(t *testing.T) {
t.Errorf("Packetize failed\nexpected: %v\n got: %v", expected, packets[0])
}
}

func TestPacketizer_Roundtrip(t *testing.T) {
multiplepayload := make([]byte, 128)
packetizer := NewPacketizer(100, 98, 0x1234ABCD, &codecs.G722Payloader{}, NewRandomSequencer(), 90000)
packets := packetizer.Packetize(multiplepayload, 1000)

rawPkts := make([][]byte, 0, 1400)
for _, pkt := range packets {
raw, err := pkt.Marshal()
if err != nil {
t.Errorf("Packet Marshal failed: %v", err)
}

rawPkts = append(rawPkts, raw)
}

for ndx, raw := range rawPkts {
expectedPkt := packets[ndx]
pkt := &Packet{}

err := pkt.Unmarshal(raw)
if err != nil {
t.Errorf("Packet Unmarshal failed: %v", err)
}

if len(raw) != pkt.MarshalSize() {
t.Errorf("Packet sizes don't match, expected %d but got %d", len(raw), pkt.MarshalSize())
}
if expectedPkt.MarshalSize() != pkt.MarshalSize() {
t.Errorf("Packet marshal sizes don't match, expected %d but got %d", expectedPkt.MarshalSize(), pkt.MarshalSize())
}

if expectedPkt.Version != pkt.Version {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.Version, pkt.Version)
}
if expectedPkt.Padding != pkt.Padding {
t.Errorf("Packet versions don't match, expected %t but got %t", expectedPkt.Padding, pkt.Padding)
}
if expectedPkt.Extension != pkt.Extension {
t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.Extension, pkt.Extension)
}
if expectedPkt.Marker != pkt.Marker {
t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.Marker, pkt.Marker)
}
if expectedPkt.PayloadType != pkt.PayloadType {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.PayloadType, pkt.PayloadType)
}
if expectedPkt.SequenceNumber != pkt.SequenceNumber {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.SequenceNumber, pkt.SequenceNumber)
}
if expectedPkt.Timestamp != pkt.Timestamp {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.Timestamp, pkt.Timestamp)
}
if expectedPkt.SSRC != pkt.SSRC {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.SSRC, pkt.SSRC)
}
if !reflect.DeepEqual(expectedPkt.CSRC, pkt.CSRC) {
t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.CSRC, pkt.CSRC)
}
if expectedPkt.ExtensionProfile != pkt.ExtensionProfile {
t.Errorf("Packet versions don't match, expected %d but got %d", expectedPkt.ExtensionProfile, pkt.ExtensionProfile)
}
if !reflect.DeepEqual(expectedPkt.Extensions, pkt.Extensions) {
t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.Extensions, pkt.Extensions)
}
if !reflect.DeepEqual(expectedPkt.Payload, pkt.Payload) {
t.Errorf("Packet versions don't match, expected %v but got %v", expectedPkt.Payload, pkt.Payload)
}

pkt.PayloadOffset = 0
pkt.PaddingSize = 0
pkt.Raw = nil

if !reflect.DeepEqual(expectedPkt, pkt) {
t.Errorf("Packets don't match, expected %v but got %v", expectedPkt, pkt)
}
}
}

0 comments on commit 6ef1c2c

Please sign in to comment.