From 1a15889522b1b0a378a29ec64f8c5211cb872ed2 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 17 Nov 2018 17:17:54 +1030 Subject: [PATCH] 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 --- stream/rtp/rtp.go | 6 +++++- stream/rtp/rtp_test.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/stream/rtp/rtp.go b/stream/rtp/rtp.go index 164f9951..d7fca6b2 100644 --- a/stream/rtp/rtp.go +++ b/stream/rtp/rtp.go @@ -73,6 +73,7 @@ func (p *Pkt) Bytes() []byte { } buf := make([]byte, defaultHeadSize, defaultHeadSize+len(p.Payload)+p.Padding) + // First 4 bytes buf[0] |= p.V<<6 | p.P<<5 | p.CC buf[1] |= p.M<<7 | p.PT @@ -88,8 +89,11 @@ func (p *Pkt) Bytes() []byte { buf[9] |= byte(p.SSRC >> 16) buf[10] |= byte(p.SSRC >> 8) buf[11] |= byte(p.SSRC) + // Add payload and padding 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 } diff --git a/stream/rtp/rtp_test.go b/stream/rtp/rtp_test.go index 3844865e..1fe199f2 100644 --- a/stream/rtp/rtp_test.go +++ b/stream/rtp/rtp_test.go @@ -64,9 +64,9 @@ var rtpTests = []struct { func TestRtpPktToByteSlice(t *testing.T) { for _, test := range rtpTests { - got := test.pkt + got := test.pkt.Bytes() 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) } } }