diff --git a/pkg/rtpaac/rtpaac_test.go b/pkg/rtpaac/rtpaac_test.go index 02e58900..3b3f51e0 100644 --- a/pkg/rtpaac/rtpaac_test.go +++ b/pkg/rtpaac/rtpaac_test.go @@ -29,7 +29,7 @@ var cases = []struct { name string aus [][]byte pts time.Duration - enc [][]byte + pkts []*rtp.Packet }{ { "single", @@ -82,54 +82,63 @@ var cases = []struct { }, }, 20 * time.Millisecond, - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x0a, 0xd8, - 0x21, 0x1a, 0xd4, 0xf5, 0x9e, 0x20, 0xc5, 0x42, - 0x89, 0x40, 0xa2, 0x9b, 0x3c, 0x94, 0xdd, 0x28, - 0x94, 0x48, 0xd5, 0x8b, 0xb0, 0x02, 0xdb, 0x1b, - 0xeb, 0xe0, 0xfa, 0x9f, 0xea, 0x91, 0xa7, 0x03, - 0xe8, 0x6b, 0xe5, 0x05, 0x95, 0x06, 0x62, 0x88, - 0x13, 0x0a, 0x15, 0xa0, 0xeb, 0xef, 0x40, 0x82, - 0xdf, 0x49, 0xf2, 0xe0, 0x26, 0xfc, 0x52, 0x5b, - 0x6c, 0x2a, 0x2d, 0xe8, 0xa5, 0x70, 0xc5, 0xaf, - 0xfc, 0x98, 0x9a, 0x2f, 0x1f, 0xbb, 0xa2, 0xcb, - 0xb8, 0x26, 0xb6, 0x6e, 0x4c, 0x15, 0x6c, 0x21, - 0x3d, 0x35, 0xf6, 0xcf, 0xa4, 0x3b, 0x72, 0x26, - 0xe1, 0x3a, 0x3a, 0x99, 0xd8, 0x2d, 0x6a, 0x22, - 0xcd, 0x97, 0x0a, 0xef, 0x52, 0x9c, 0x5f, 0xcd, - 0x5c, 0xd9, 0xd3, 0x12, 0x7e, 0x45, 0x45, 0xb3, - 0x24, 0xef, 0xd3, 0x4f, 0x2f, 0x96, 0xd9, 0x8b, - 0x9c, 0xc2, 0xcd, 0x54, 0x0b, 0x6e, 0x19, 0x84, - 0x56, 0xeb, 0x85, 0x52, 0x63, 0x64, 0x28, 0xb2, - 0xf2, 0xcf, 0xb8, 0xa8, 0x71, 0x53, 0x06, 0x82, - 0x88, 0xf2, 0xc4, 0xe1, 0x7d, 0x65, 0x54, 0xe0, - 0x5e, 0xc8, 0x38, 0x75, 0x9d, 0xb0, 0x58, 0x65, - 0x41, 0xa2, 0xcd, 0xdb, 0x1b, 0x9e, 0xac, 0xd1, - 0xbe, 0xc9, 0x22, 0xf5, 0xe9, 0xc6, 0x6f, 0xaf, - 0xf8, 0xb1, 0x4c, 0xcb, 0xa2, 0x56, 0x11, 0xa4, - 0xd7, 0xfd, 0xe5, 0xef, 0x8e, 0xbf, 0xce, 0x4b, - 0xef, 0xe1, 0x0d, 0xc0, 0x27, 0x18, 0xe2, 0x64, - 0x63, 0x05, 0x16, 0x06, 0x0c, 0x34, 0x0e, 0xf3, - 0x62, 0xc2, 0xd6, 0x42, 0x5d, 0x66, 0x81, 0x04, - 0x65, 0x76, 0xaa, 0xe7, 0x39, 0xdd, 0x8e, 0xfe, - 0x48, 0x23, 0x3a, 0x01, 0xc4, 0xd3, 0x65, 0x80, - 0x28, 0x6f, 0x9b, 0xc9, 0xb7, 0x4e, 0x44, 0x4c, - 0x98, 0x6a, 0x5f, 0x3b, 0x97, 0x81, 0x9b, 0xa9, - 0xab, 0xfd, 0xcf, 0x8e, 0x78, 0xbd, 0x4d, 0x70, - 0x81, 0x9b, 0x2d, 0x85, 0x94, 0x74, 0x2a, 0x3a, - 0xb4, 0xff, 0x4a, 0x13, 0x70, 0x76, 0x2c, 0x2f, - 0x13, 0x5b, 0x43, 0xf9, 0x17, 0xee, 0x26, 0x37, - 0x01, 0xbc, 0x9f, 0x0b, 0x0e, 0x68, 0xcb, 0x87, - 0x65, 0x86, 0xcc, 0x4c, 0x2f, 0x7a, 0x14, 0x0d, - 0xd1, 0xb9, 0x57, 0xbd, 0x50, 0xb6, 0x95, 0x44, - 0x1a, 0x0d, 0xc0, 0x15, 0x0f, 0xd2, 0xc3, 0x72, - 0x4d, 0x6e, 0x4f, 0x8e, 0x6d, 0x64, 0xdc, 0x64, - 0x1f, 0x33, 0x53, 0x4e, 0xd8, 0xa4, 0x74, 0xf3, - 0x33, 0x04, 0x68, 0xd9, 0x92, 0xf3, 0x6e, 0xb7, - 0x5b, 0xe6, 0xf6, 0xc3, 0x55, 0x14, 0x54, 0x87, - 0x00, 0xaf, 0x07, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x00, 0x10, 0x0a, 0xd8, + 0x21, 0x1a, 0xd4, 0xf5, 0x9e, 0x20, 0xc5, 0x42, + 0x89, 0x40, 0xa2, 0x9b, 0x3c, 0x94, 0xdd, 0x28, + 0x94, 0x48, 0xd5, 0x8b, 0xb0, 0x02, 0xdb, 0x1b, + 0xeb, 0xe0, 0xfa, 0x9f, 0xea, 0x91, 0xa7, 0x03, + 0xe8, 0x6b, 0xe5, 0x05, 0x95, 0x06, 0x62, 0x88, + 0x13, 0x0a, 0x15, 0xa0, 0xeb, 0xef, 0x40, 0x82, + 0xdf, 0x49, 0xf2, 0xe0, 0x26, 0xfc, 0x52, 0x5b, + 0x6c, 0x2a, 0x2d, 0xe8, 0xa5, 0x70, 0xc5, 0xaf, + 0xfc, 0x98, 0x9a, 0x2f, 0x1f, 0xbb, 0xa2, 0xcb, + 0xb8, 0x26, 0xb6, 0x6e, 0x4c, 0x15, 0x6c, 0x21, + 0x3d, 0x35, 0xf6, 0xcf, 0xa4, 0x3b, 0x72, 0x26, + 0xe1, 0x3a, 0x3a, 0x99, 0xd8, 0x2d, 0x6a, 0x22, + 0xcd, 0x97, 0x0a, 0xef, 0x52, 0x9c, 0x5f, 0xcd, + 0x5c, 0xd9, 0xd3, 0x12, 0x7e, 0x45, 0x45, 0xb3, + 0x24, 0xef, 0xd3, 0x4f, 0x2f, 0x96, 0xd9, 0x8b, + 0x9c, 0xc2, 0xcd, 0x54, 0x0b, 0x6e, 0x19, 0x84, + 0x56, 0xeb, 0x85, 0x52, 0x63, 0x64, 0x28, 0xb2, + 0xf2, 0xcf, 0xb8, 0xa8, 0x71, 0x53, 0x06, 0x82, + 0x88, 0xf2, 0xc4, 0xe1, 0x7d, 0x65, 0x54, 0xe0, + 0x5e, 0xc8, 0x38, 0x75, 0x9d, 0xb0, 0x58, 0x65, + 0x41, 0xa2, 0xcd, 0xdb, 0x1b, 0x9e, 0xac, 0xd1, + 0xbe, 0xc9, 0x22, 0xf5, 0xe9, 0xc6, 0x6f, 0xaf, + 0xf8, 0xb1, 0x4c, 0xcb, 0xa2, 0x56, 0x11, 0xa4, + 0xd7, 0xfd, 0xe5, 0xef, 0x8e, 0xbf, 0xce, 0x4b, + 0xef, 0xe1, 0x0d, 0xc0, 0x27, 0x18, 0xe2, 0x64, + 0x63, 0x05, 0x16, 0x06, 0x0c, 0x34, 0x0e, 0xf3, + 0x62, 0xc2, 0xd6, 0x42, 0x5d, 0x66, 0x81, 0x04, + 0x65, 0x76, 0xaa, 0xe7, 0x39, 0xdd, 0x8e, 0xfe, + 0x48, 0x23, 0x3a, 0x01, 0xc4, 0xd3, 0x65, 0x80, + 0x28, 0x6f, 0x9b, 0xc9, 0xb7, 0x4e, 0x44, 0x4c, + 0x98, 0x6a, 0x5f, 0x3b, 0x97, 0x81, 0x9b, 0xa9, + 0xab, 0xfd, 0xcf, 0x8e, 0x78, 0xbd, 0x4d, 0x70, + 0x81, 0x9b, 0x2d, 0x85, 0x94, 0x74, 0x2a, 0x3a, + 0xb4, 0xff, 0x4a, 0x13, 0x70, 0x76, 0x2c, 0x2f, + 0x13, 0x5b, 0x43, 0xf9, 0x17, 0xee, 0x26, 0x37, + 0x01, 0xbc, 0x9f, 0x0b, 0x0e, 0x68, 0xcb, 0x87, + 0x65, 0x86, 0xcc, 0x4c, 0x2f, 0x7a, 0x14, 0x0d, + 0xd1, 0xb9, 0x57, 0xbd, 0x50, 0xb6, 0x95, 0x44, + 0x1a, 0x0d, 0xc0, 0x15, 0x0f, 0xd2, 0xc3, 0x72, + 0x4d, 0x6e, 0x4f, 0x8e, 0x6d, 0x64, 0xdc, 0x64, + 0x1f, 0x33, 0x53, 0x4e, 0xd8, 0xa4, 0x74, 0xf3, + 0x33, 0x04, 0x68, 0xd9, 0x92, 0xf3, 0x6e, 0xb7, + 0x5b, 0xe6, 0xf6, 0xc3, 0x55, 0x14, 0x54, 0x87, + 0x00, 0xaf, 0x07, + }, }, }, }, @@ -141,12 +150,21 @@ var cases = []struct { {0x08, 0x09, 0x0A, 0x0B}, }, 0, - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x30, 0x0, 0x20, - 0x0, 0x20, 0x0, 0x20, 0x0, 0x1, 0x2, 0x3, - 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x0, 0x30, 0x0, 0x20, + 0x0, 0x20, 0x0, 0x20, 0x0, 0x1, 0x2, 0x3, + 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + }, }, }, }, @@ -156,28 +174,49 @@ var cases = []struct { bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 512), }, 0, - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x10, 0x5, 0xb0, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x05, 0xb0, + Payload: mergeBytes( + []byte{0x0, 0x10, 0x5, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xef, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x04, 0xa0, + Payload: mergeBytes( + []byte{0x00, 0x10, 0x05, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17647, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 148), - ), + Payload: mergeBytes( + []byte{0x00, 0x10, 0x04, 0xa0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 148), + ), + }, }, }, { @@ -189,27 +228,50 @@ var cases = []struct { bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 256), }, 0, - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x30, 0x0, 0x20, - 0x0, 0x20, 0x0, 0x20, 0x0, 0x1, 0x2, 0x3, - 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x0, 0x30, 0x0, 0x20, + 0x0, 0x20, 0x0, 0x20, 0x0, 0x1, 0x2, 0x3, + 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + }, }, - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xee, 0x88, 0x77, 0x72, 0x0d, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x10, 0x5, 0xb0, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289529357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xef, 0x88, 0x77, 0x72, 0x0d, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x02, 0x50, + Payload: mergeBytes( + []byte{0x0, 0x10, 0x5, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17647, + Timestamp: 2289529357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 74), - ), + Payload: mergeBytes( + []byte{0x00, 0x10, 0x02, 0x50}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 74), + ), + }, }, }, } @@ -222,26 +284,27 @@ func TestDecode(t *testing.T) { // send an initial packet downstream // in order to compute the right timestamp, // that is relative to the initial packet - var pkt rtp.Packet - err := pkt.Unmarshal([]byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x00, 0x08, 0x0, - }) - require.NoError(t, err) - _, _, err = d.Decode(&pkt) + pkt := rtp.Packet{ + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10, 0x00, 0x08, 0x0}, + } + _, _, err := d.Decode(&pkt) require.NoError(t, err) var aus [][]byte expPTS := ca.pts - for _, byts := range ca.enc { - var pkt rtp.Packet - err := pkt.Unmarshal(byts) - require.NoError(t, err) - + for _, pkt := range ca.pkts { clone := pkt.Clone() - addAUs, pts, err := d.Decode(&pkt) + addAUs, pts, err := d.Decode(pkt) if err == ErrMorePacketsNeeded { continue } @@ -251,8 +314,8 @@ func TestDecode(t *testing.T) { aus = append(aus, addAUs...) expPTS += time.Duration(len(aus)) * 1000 * time.Second / 48000 - // test packet integrity - require.Equal(t, clone, &pkt) + // test input integrity + require.Equal(t, clone, pkt) } require.Equal(t, ca.aus, aus) @@ -263,143 +326,237 @@ func TestDecode(t *testing.T) { func TestDecodeErrors(t *testing.T) { for _, ca := range []struct { name string - pkts [][]byte + pkts []*rtp.Packet err string }{ { "missing payload", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, }, }, "payload is too short", }, { "missing au header", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10}, }, }, "payload is too short", }, { "missing au", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x0a, 0xd8, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10, 0x0a, 0xd8}, }, }, "payload is too short", }, { "invalid au headers length", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x09, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x09}, }, }, "invalid AU-headers-length (9)", }, { "au index not zero", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x0a, 0xd8 | 0x01, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10, 0x0a, 0xd9}, }, }, "AU-index field is not zero", }, { "fragmented with multiple AUs", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0x60, 0x0e, 0xa2, 0x0e, 0x01, 0x9b, 0xb7, - 0x35, 0x6e, 0xcb, 0x3b, 0x00, 0x20, + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 0x60, + SequenceNumber: 0xea2, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x20}, }, }, "a fragmented packet can only contain one AU", }, { "fragmented with AU index not zero", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0x60, 0x0e, 0xa2, 0x0e, 0x01, 0x9b, 0xb7, - 0x35, 0x6e, 0xcb, 0x3b, 0x00, 0x10, 0x0a, 0xd8 | 0x01, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10, 0x0a, 0xd9}, }, }, "AU-index field is not zero", }, { "fragmented with missing au", - [][]byte{ + []*rtp.Packet{ { - 0x80, 0x60, 0x0e, 0xa2, 0x0e, 0x01, 0x9b, 0xb7, - 0x35, 0x6e, 0xcb, 0x3b, 0x00, 0x10, 0x0a, 0xd8, + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x00, 0x10, 0x0a, 0xd8}, }, }, "payload is too short", }, { "fragmented with multiple AUs in 2nd packet", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x10, 0x5, 0xb0, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ + Payload: mergeBytes( + []byte{0x0, 0x10, 0x5, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ee, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x20, }, - ), + }, }, "a fragmented packet can only contain one AU", }, { "fragmented with au index not zero in 2nd packet", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x10, 0x5, 0xb0, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x0a, 0xd8 | 0x01, + Payload: mergeBytes( + []byte{0x0, 0x10, 0x5, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ee, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, }, - ), + Payload: []byte{0x00, 0x10, 0x0a, 0xd8 | 0x01}, + }, }, "AU-index field is not zero", }, { "fragmented without payload in 2nd packet", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x0, 0x10, 0x5, 0xb0, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 0x60, + SequenceNumber: 0x44ed, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x00, 0x10, 0x0a, 0xd8, + Payload: mergeBytes( + []byte{0x0, 0x10, 0x5, 0xb0}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 0x60, + SequenceNumber: 0x44ee, + Timestamp: 0x88776a15, + SSRC: 0x9dbb7812, }, - ), + Payload: []byte{0x00, 0x10, 0x0a, 0xd8}, + }, }, "payload is too short", }, @@ -407,11 +564,8 @@ func TestDecodeErrors(t *testing.T) { t.Run(ca.name, func(t *testing.T) { d := NewDecoder(48000) var lastErr error - for _, byts := range ca.pkts { - var pkt rtp.Packet - err := pkt.Unmarshal(byts) - require.NoError(t, err) - _, _, lastErr = d.Decode(&pkt) + for _, pkt := range ca.pkts { + _, _, lastErr = d.Decode(pkt) } require.EqualError(t, lastErr, ca.err) }) @@ -426,17 +580,9 @@ func TestEncode(t *testing.T) { initialTs := uint32(0x88776655) e := NewEncoder(96, 48000, &sequenceNumber, &ssrc, &initialTs) - enc, err := e.Encode(ca.aus, ca.pts) + pkts, err := e.Encode(ca.aus, ca.pts) require.NoError(t, err) - - var bytss [][]byte - for _, pkt := range enc { - byts, err := pkt.Marshal() - require.NoError(t, err) - bytss = append(bytss, byts) - } - - require.Equal(t, ca.enc, bytss) + require.Equal(t, ca.pkts, pkts) }) } } diff --git a/pkg/rtph264/rtph264_test.go b/pkg/rtph264/rtph264_test.go index 7f908b35..00a2026f 100644 --- a/pkg/rtph264/rtph264_test.go +++ b/pkg/rtph264/rtph264_test.go @@ -30,7 +30,7 @@ var cases = []struct { name string nalus [][]byte pts time.Duration - enc [][]byte + pkts []*rtp.Packet }{ { "single", @@ -41,14 +41,21 @@ var cases = []struct { ), }, 25 * time.Millisecond, - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6f, 0x1f, - 0x9d, 0xbb, 0x78, 0x12, 0x05, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289528607, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), - ), + Payload: mergeBytes( + []byte{0x05}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), + ), + }, }, }, { @@ -60,14 +67,21 @@ var cases = []struct { ), }, -20 * time.Millisecond, - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x5f, 0x4d, - 0x9d, 0xbb, 0x78, 0x12, 0x05, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289524557, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), - ), + Payload: mergeBytes( + []byte{0x05}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), + ), + }, }, }, { @@ -79,32 +93,53 @@ var cases = []struct { ), }, 55 * time.Millisecond, - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289531307, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xee, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x05, + Payload: mergeBytes( + []byte{0x1c, 0x85}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289531307, + SSRC: 0x9dbb7812, }, - []byte{0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 181), - []byte{0x00, 0x01, 0x02, 0x03}, - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xef, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x45, + Payload: mergeBytes( + []byte{0x1c, 0x05}, + []byte{0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 181), + []byte{0x00, 0x01, 0x02, 0x03}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17647, + Timestamp: 2289531307, + SSRC: 0x9dbb7812, }, - []byte{0x04, 0x05, 0x06, 0x07}, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 147), - ), + Payload: mergeBytes( + []byte{0x1c, 0x45}, + []byte{0x04, 0x05, 0x06, 0x07}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 147), + ), + }, }, }, { @@ -124,19 +159,28 @@ var cases = []struct { }, }, 0, - [][]byte{ + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x18, 0x00, 0x02, 0x09, - 0xf0, 0x00, 0x44, 0x41, 0x9a, 0x24, 0x6c, 0x41, - 0x4f, 0xfe, 0xd6, 0x8c, 0xb0, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, - 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, - 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x6d, 0x40, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x18, 0x00, 0x02, 0x09, + 0xf0, 0x00, 0x44, 0x41, 0x9a, 0x24, 0x6c, 0x41, + 0x4f, 0xfe, 0xd6, 0x8c, 0xb0, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x6d, 0x40, + }, }, }, }, @@ -161,27 +205,43 @@ var cases = []struct { ), }, 0, - [][]byte{ + []*rtp.Packet{ { - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x18, 0x00, 0x02, 0x09, - 0xf0, 0x00, 0x44, 0x41, 0x9a, 0x24, 0x6c, 0x41, - 0x4f, 0xfe, 0xd6, 0x8c, 0xb0, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, - 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, - 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, - 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, - 0x00, 0x00, 0x03, 0x00, 0x00, 0x6d, 0x40, - }, - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x08, + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 175), - ), + Payload: []byte{ + 0x18, 0x00, 0x02, 0x09, + 0xf0, 0x00, 0x44, 0x41, 0x9a, 0x24, 0x6c, 0x41, + 0x4f, 0xfe, 0xd6, 0x8c, 0xb0, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x6d, 0x40, + }, + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: mergeBytes( + []byte{0x08}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 175), + ), + }, }, }, { @@ -195,27 +255,50 @@ var cases = []struct { {0x09, 0xF0}, }, 0, - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, - }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xee, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x45, - }, - []byte{0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 73), - ), + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xef, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x18, 0x00, 0x02, 0x09, - 0xf0, 0x00, 0x02, 0x09, 0xf0, + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: mergeBytes( + []byte{0x1c, 0x85}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: mergeBytes( + []byte{0x1c, 0x45}, + []byte{0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 73), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17647, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x18, 0x00, 0x02, 0x09, + 0xf0, 0x00, 0x02, 0x09, 0xf0, + }, }, }, }, @@ -229,24 +312,26 @@ func TestDecode(t *testing.T) { // send an initial packet downstream // in order to compute the right timestamp, // that is relative to the initial packet - var pkt rtp.Packet - err := pkt.Unmarshal([]byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x06, 0x00, - }) - require.NoError(t, err) - _, _, err = d.Decode(&pkt) + pkt := rtp.Packet{ + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x06, 0x00}, + } + _, _, err := d.Decode(&pkt) require.NoError(t, err) var nalus [][]byte - for _, byts := range ca.enc { - err := pkt.Unmarshal(byts) - require.NoError(t, err) - + for _, pkt := range ca.pkts { clone := pkt.Clone() - addNALUs, pts, err := d.Decode(&pkt) + addNALUs, pts, err := d.Decode(pkt) if err == ErrMorePacketsNeeded { continue } @@ -255,8 +340,8 @@ func TestDecode(t *testing.T) { require.Equal(t, ca.pts, pts) nalus = append(nalus, addNALUs...) - // test packet integrity - require.Equal(t, clone, &pkt) + // test input integrity + require.Equal(t, clone, pkt) } require.Equal(t, ca.nalus, nalus) @@ -267,45 +352,60 @@ func TestDecode(t *testing.T) { func TestDecodePartOfFragmentedBeforeSingle(t *testing.T) { d := NewDecoder() - var pkt rtp.Packet - err := pkt.Unmarshal(mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xef, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x45, + pkt := rtp.Packet{ + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17647, + Timestamp: 2289531307, + SSRC: 0x9dbb7812, }, - []byte{0x04, 0x05, 0x06, 0x07}, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 147), - )) - require.NoError(t, err) - _, _, err = d.Decode(&pkt) + Payload: mergeBytes( + []byte{0x1c, 0x45}, + []byte{0x04, 0x05, 0x06, 0x07}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 147), + ), + } + _, _, err := d.Decode(&pkt) require.Equal(t, ErrNonStartingPacketAndNoPrevious, err) - err = pkt.Unmarshal(mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6f, 0x1f, - 0x9d, 0xbb, 0x78, 0x12, 0x05, + pkt = rtp.Packet{ + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289528607, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), - )) - require.NoError(t, err) + Payload: mergeBytes( + []byte{0x05}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), + ), + } _, _, err = d.Decode(&pkt) require.NoError(t, err) } func TestDecodeSTAPAWithPadding(t *testing.T) { - d := NewDecoder() - - var pkt rtp.Packet - err := pkt.Unmarshal([]byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x66, 0x55, - 0x9d, 0xbb, 0x78, 0x12, 0x18, 0x00, 0x02, 0xaa, - 0xbb, 0x00, 0x02, 0xcc, 0xdd, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - }) - require.NoError(t, err) - - nalus, _, err := d.Decode(&pkt) + pkt := rtp.Packet{ + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289526357, + SSRC: 0x9dbb7812, + }, + Payload: []byte{ + 0x18, 0x00, 0x02, 0xaa, + 0xbb, 0x00, 0x02, 0xcc, 0xdd, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }, + } + nalus, _, err := NewDecoder().Decode(&pkt) require.NoError(t, err) require.Equal(t, [][]byte{ {0xaa, 0xbb}, @@ -316,145 +416,248 @@ func TestDecodeSTAPAWithPadding(t *testing.T) { func TestDecodeErrors(t *testing.T) { for _, ca := range []struct { name string - pkts [][]byte + pkts []*rtp.Packet err string }{ { "missing payload", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + }, + }, "payload is too short", }, { "STAP-A without NALUs", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeSTAPA), - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x18}, + }, + }, "STAP-A packet doesn't contain any NALU", }, { "STAP-A without size", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeSTAPA), 0x01, - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x18, 0x01}, + }, + }, "invalid STAP-A packet (invalid size)", }, { "STAP-A with invalid size", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeSTAPA), 0x00, 0x15, - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x18, 0x00, 0x15}, + }, + }, "invalid STAP-A packet (invalid size)", }, { "FU-A without payload", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeFUA), - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x1c}, + }, + }, "invalid FU-A packet (invalid size)", }, { "FU-A without start bit", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6f, 0x1f, - 0x9d, 0xbb, 0x78, 0x12, 0x05, - }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), - ), + []*rtp.Packet{ { - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeFUA), 0x00, + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: mergeBytes( + []byte{0x05}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8), + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: true, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x1c, 0x00}, }, }, "invalid FU-A packet (non-starting)", }, { "FU-A with 2nd packet empty", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, + Payload: mergeBytes( + []byte{0x1c, 0x85}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - ), + }, }, "invalid FU-A packet (invalid size)", }, { "FU-A with 2nd packet invalid", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), - mergeBytes( - []byte{ - 0x80, 0xe0, 0x44, 0xee, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x01, 0x00, + Payload: mergeBytes( + []byte{ + 0x1c, 0x85, + }, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - ), + Payload: []byte{0x01, 0x00}, + }, }, "expected FU-A packet, got another type", }, { "FU-A with two starting packets", - [][]byte{ - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), - mergeBytes( - []byte{ - 0x80, 0x60, 0x44, 0xed, 0x88, 0x77, 0x79, 0xab, - 0x9d, 0xbb, 0x78, 0x12, 0x1c, 0x85, + Payload: mergeBytes( + []byte{0x1c, 0x85}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17646, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, }, - bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), - []byte{0x00, 0x01}, - ), + Payload: mergeBytes( + []byte{0x1c, 0x85}, + bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 182), + []byte{0x00, 0x01}, + ), + }, }, "invalid FU-A packet (decoded two starting packets in a row)", }, { "MTAP", - [][]byte{{ - 0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x6a, 0x15, - 0x9d, 0xbb, 0x78, 0x12, byte(naluTypeMTAP16), - }}, + []*rtp.Packet{ + { + Header: rtp.Header{ + Version: 2, + Marker: false, + PayloadType: 96, + SequenceNumber: 17645, + Timestamp: 2289527317, + SSRC: 0x9dbb7812, + }, + Payload: []byte{0x1a}, + }, + }, "packet type not supported (MTAP-16)", }, } { t.Run(ca.name, func(t *testing.T) { d := NewDecoder() var lastErr error - for _, byts := range ca.pkts { - var pkt rtp.Packet - err := pkt.Unmarshal(byts) - require.NoError(t, err) - _, _, lastErr = d.Decode(&pkt) + for _, pkt := range ca.pkts { + _, _, lastErr = d.Decode(pkt) } require.EqualError(t, lastErr, ca.err) }) @@ -469,17 +672,9 @@ func TestEncode(t *testing.T) { initialTs := uint32(0x88776655) e := NewEncoder(96, &sequenceNumber, &ssrc, &initialTs) - enc, err := e.Encode(ca.nalus, ca.pts) + pkts, err := e.Encode(ca.nalus, ca.pts) require.NoError(t, err) - - var bytss [][]byte - for _, pkt := range enc { - byts, err := pkt.Marshal() - require.NoError(t, err) - bytss = append(bytss, byts) - } - - require.Equal(t, ca.enc, bytss) + require.Equal(t, ca.pkts, pkts) }) } }