rtp: fixed bug by actually checking to see if there is padding before adding padding size to end buf - which would mean there's actually padding

This commit is contained in:
saxon 2018-11-17 17:17:54 +10:30
parent a6cbfee22b
commit 1a15889522
2 changed files with 7 additions and 3 deletions

View File

@ -73,6 +73,7 @@ func (p *Pkt) Bytes() []byte {
} }
buf := make([]byte, defaultHeadSize, defaultHeadSize+len(p.Payload)+p.Padding) buf := make([]byte, defaultHeadSize, defaultHeadSize+len(p.Payload)+p.Padding)
// First 4 bytes // First 4 bytes
buf[0] |= p.V<<6 | p.P<<5 | p.CC buf[0] |= p.V<<6 | p.P<<5 | p.CC
buf[1] |= p.M<<7 | p.PT buf[1] |= p.M<<7 | p.PT
@ -88,8 +89,11 @@ func (p *Pkt) Bytes() []byte {
buf[9] |= byte(p.SSRC >> 16) buf[9] |= byte(p.SSRC >> 16)
buf[10] |= byte(p.SSRC >> 8) buf[10] |= byte(p.SSRC >> 8)
buf[11] |= byte(p.SSRC) buf[11] |= byte(p.SSRC)
// Add payload and padding // Add payload and padding
buf = append(buf, p.Payload...) buf = append(buf, p.Payload...)
buf = append(buf, append(make([]byte, p.Padding-1, p.Padding), byte(p.Padding))...) if p.Padding != 0 {
buf = append(buf, append(make([]byte, p.Padding-1, p.Padding), byte(p.Padding))...)
}
return buf return buf
} }

View File

@ -64,9 +64,9 @@ var rtpTests = []struct {
func TestRtpPktToByteSlice(t *testing.T) { func TestRtpPktToByteSlice(t *testing.T) {
for _, test := range rtpTests { for _, test := range rtpTests {
got := test.pkt got := test.pkt.Bytes()
if !reflect.DeepEqual(got, test.want) { if !reflect.DeepEqual(got, test.want) {
t.Errorf("unexpected error for %q: got:%v want:%v", test.num, got, test.want) t.Errorf("unexpected error for test %v: got:%v want:%v", test.num, got, test.want)
} }
} }
} }