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

Add PaddingSize to rtp.Packet #155

Merged
merged 1 commit into from
Nov 22, 2021
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
17 changes: 12 additions & 5 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Header struct {
// Packet represents an RTP Packet
type Packet struct {
Header
Payload []byte
Payload []byte
PaddingSize byte
}

const (
Expand Down Expand Up @@ -212,7 +213,8 @@ func (p *Packet) Unmarshal(buf []byte) error {
}
end := len(buf)
if p.Header.Padding {
end -= int(buf[end-1])
p.PaddingSize = buf[end-1]
end -= int(p.PaddingSize)
}
if end < n {
return errTooSmall
Expand Down Expand Up @@ -468,24 +470,28 @@ func (p *Packet) Marshal() (buf []byte, err error) {

// MarshalTo serializes the packet and writes to the buffer.
func (p *Packet) MarshalTo(buf []byte) (n int, err error) {
p.Header.Padding = p.PaddingSize != 0
n, err = p.Header.MarshalTo(buf)
if err != nil {
return 0, err
}

// Make sure the buffer is large enough to hold the packet.
if n+len(p.Payload) > len(buf) {
if n+len(p.Payload)+int(p.PaddingSize) > len(buf) {
return 0, io.ErrShortBuffer
}

m := copy(buf[n:], p.Payload)
if p.Header.Padding {
buf[n+m+int(p.PaddingSize-1)] = p.PaddingSize
}

return n + m, nil
return n + m + int(p.PaddingSize), nil
}

// MarshalSize returns the size of the packet once marshaled.
func (p *Packet) MarshalSize() int {
return p.Header.MarshalSize() + len(p.Payload)
return p.Header.MarshalSize() + len(p.Payload) + int(p.PaddingSize)
}

// Clone returns a deep copy of p.
Expand All @@ -496,6 +502,7 @@ func (p *Packet) Clone() *Packet {
clone.Payload = make([]byte, len(p.Payload))
copy(clone.Payload, p.Payload)
}
clone.PaddingSize = p.PaddingSize
return clone
}

Expand Down
113 changes: 109 additions & 4 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestBasic(t *testing.T) {
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:],
Payload: rawPkt[20:],
PaddingSize: 0,
}

// Unmarshal to the used Packet should work as well.
Expand Down Expand Up @@ -88,7 +89,8 @@ func TestBasic(t *testing.T) {
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:21],
Payload: rawPkt[20:21],
PaddingSize: 4,
}
if err := p.Unmarshal(rawPkt); err != nil {
t.Error(err)
Expand Down Expand Up @@ -119,7 +121,8 @@ func TestBasic(t *testing.T) {
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: []byte{},
Payload: []byte{},
PaddingSize: 5,
}
if err := p.Unmarshal(rawPkt); err != nil {
t.Error(err)
Expand Down Expand Up @@ -153,7 +156,8 @@ func TestBasic(t *testing.T) {
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: []byte{},
Payload: []byte{},
PaddingSize: 0,
}
err := p.Unmarshal(rawPkt)
if err == nil {
Expand All @@ -162,6 +166,107 @@ func TestBasic(t *testing.T) {
if !errors.Is(err, errTooSmall) {
t.Errorf("Expected error: %v, got: %v", errTooSmall, err)
}

// marshal packet with padding
rawPkt = []byte{
0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x00, 0x00, 0x00, 0x04,
}
parsedPacket = &Packet{
Header: Header{
Padding: true,
Marker: true,
Extension: true,
ExtensionProfile: 1,
Extensions: []Extension{
{0, []byte{
0xFF, 0xFF, 0xFF, 0xFF,
}},
},
Version: 2,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: rawPkt[20:21],
PaddingSize: 4,
}
buf, err := parsedPacket.Marshal()
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(buf, rawPkt) {
t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
}

// marshal packet with padding only
rawPkt = []byte{
0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x05,
}
parsedPacket = &Packet{
Header: Header{
Padding: true,
Marker: true,
Extension: true,
ExtensionProfile: 1,
Extensions: []Extension{
{0, []byte{
0xFF, 0xFF, 0xFF, 0xFF,
}},
},
Version: 2,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: []byte{},
PaddingSize: 5,
}
buf, err = parsedPacket.Marshal()
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(buf, rawPkt) {
t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
}

// marshal packet with padding only without setting Padding explicitly in Header
rawPkt = []byte{
0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x05,
}
parsedPacket = &Packet{
Header: Header{
Marker: true,
Extension: true,
ExtensionProfile: 1,
Extensions: []Extension{
{0, []byte{
0xFF, 0xFF, 0xFF, 0xFF,
}},
},
Version: 2,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
SSRC: 476325762,
CSRC: []uint32{},
},
Payload: []byte{},
PaddingSize: 5,
}
buf, err = parsedPacket.Marshal()
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(buf, rawPkt) {
t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
}
}

func TestExtension(t *testing.T) {
Expand Down