codec/h264: using table based testing, and now also testing for when DON is being used.

This commit is contained in:
Saxon 2019-05-02 00:15:24 +09:30
parent a11b5b4857
commit 659be05a1f
1 changed files with 184 additions and 120 deletions

View File

@ -30,21 +30,19 @@ package h265
import (
"io"
"testing"
"bitbucket.org/ausocean/av/protocol/rtp"
)
type rtpReader struct {
pkts []rtp.Pkt
idx int
packets [][]byte
idx int
}
func (r *rtpReader) Read(p []byte) (int, error) {
if r.idx == len(r.pkts) {
if r.idx == len(r.packets) {
return 0, io.EOF
}
b := r.pkts[r.idx].Bytes(nil)
copy(p, r.pkts[r.idx].Bytes(nil))
b := r.packets[r.idx]
copy(p, b)
r.idx++
return len(b), nil
}
@ -58,130 +56,196 @@ func (d *destination) Write(p []byte) (int, error) {
return len(p), nil
}
func TestLexNo(t *testing.T) {
func TestLex(t *testing.T) {
const rtpVer = 2
packets := []rtp.Pkt{
// Singla NAL
rtp.Pkt{
V: rtpVer,
Payload: []byte{
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL Data.
},
},
// Fragmentation (start packet).
rtp.Pkt{
V: rtpVer,
Payload: []byte{
0x62, 0x00, // NAL header (type49).
0x80, // FU header.
0x01, 0x02, 0x03, // FU payload.
},
},
// Fragmentation (middle packet)
rtp.Pkt{
V: rtpVer,
Payload: []byte{
0x62, 0x00, // NAL header (type 49).
0x00, // FU header.
0x04, 0x05, 0x06, // FU payload.
},
},
// Fragmentation (end packet)
rtp.Pkt{
V: rtpVer,
Payload: []byte{
0x62, 0x00, // NAL header (type 49).
0x40, // FU header.
0x07, 0x08, 0x09, // FU payload
},
},
// Aggregation.
// Consider last packet of access unit - M true
rtp.Pkt{
V: rtpVer,
M: true,
Payload: []byte{
0x60, 0x00, // NAL header (type 49).
0x00, 0x04, // NAL 1 size.
0x01, 0x02, 0x03, 0x04, // NAL 1 data.
0x00, 0x04, // NAL 2 size.
0x01, 0x02, 0x03, 0x04, // NAL 2 data.
},
},
// Singla NAL
rtp.Pkt{
V: rtpVer,
Payload: []byte{
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
},
},
// Singla NAL
rtp.Pkt{
V: rtpVer,
M: true,
Payload: []byte{
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
},
},
}
expect := [][]byte{
// First access unit.
tests := []struct {
don bool
packets [][]byte
expect [][]byte
}{
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
don: false,
packets: [][]byte{
{ // Single NAL unit.
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL Data.
},
{ // Fragmentation (start packet).
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type49).
0x80, // FU header.
0x01, 0x02, 0x03, // FU payload.
},
{ // Fragmentation (middle packet)
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type 49).
0x00, // FU header.
0x04, 0x05, 0x06, // FU payload.
},
{ // Fragmentation (end packet)
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type 49).
0x40, // FU header.
0x07, 0x08, 0x09, // FU payload
},
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, // FU payload.
0x04, 0x05, 0x06, // FU payload.
0x07, 0x08, 0x09, // FU payload.
// NAL 3
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL data.
// NAL 4
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL 2 data
{ // Aggregation. Make last packet of access unit => marker bit true.
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x60, 0x00, // NAL header (type 49).
0x00, 0x04, // NAL 1 size.
0x01, 0x02, 0x03, 0x04, // NAL 1 data.
0x00, 0x04, // NAL 2 size.
0x01, 0x02, 0x03, 0x04, // NAL 2 data.
},
{ // Singla NAL
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
},
{ // Singla NAL. Make last packet of access unit => marker bit true.
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
},
},
expect: [][]byte{
// First access unit.
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // NAL data.
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, // FU payload.
0x04, 0x05, 0x06, // FU payload.
0x07, 0x08, 0x09, // FU payload.
// NAL 3
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL data.
// NAL 4
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL 2 data
},
// Second access unit.
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // Data.
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // Data.
},
},
},
// Second access unit.
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // Data.
don: true,
packets: [][]byte{
{ // Single NAL unit.
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS).
0x00, 0x00, // DONL
0x01, 0x02, 0x03, 0x04, // NAL Data.
},
{ // Fragmentation (start packet).
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type49).
0x80, // FU header.
0x00, 0x00, // DONL
0x01, 0x02, 0x03, // FU payload.
},
{ // Fragmentation (middle packet)
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type 49).
0x00, // FU header.
0x00, 0x00, // DONL
0x04, 0x05, 0x06, // FU payload.
},
{ // Fragmentation (end packet)
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x62, 0x00, // NAL header (type 49).
0x40, // FU header.
0x00, 0x00, // DONL
0x07, 0x08, 0x09, // FU payload
},
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x01, 0x02, 0x03, 0x04, // Data.
{ // Aggregation. Make last packet of access unit => marker bit true.
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x60, 0x00, // NAL header (type 49).
0x00, 0x00, // DONL
0x00, 0x04, // NAL 1 size.
0x01, 0x02, 0x03, 0x04, // NAL 1 data.
0x00, // DOND
0x00, 0x04, // NAL 2 size.
0x01, 0x02, 0x03, 0x04, // NAL 2 data.
},
{ // Singla NAL
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS)
0x00, 0x00, // DONL.
0x01, 0x02, 0x03, 0x04, // NAL data.
},
{ // Singla NAL. Make last packet of access unit => marker bit true.
0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RTP header.
0x40, 0x00, // NAL header (type=32 VPS).
0x00, 0x00, // DONL
0x01, 0x02, 0x03, 0x04, // NAL data.
},
},
expect: [][]byte{
// First access unit.
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x00, 0x00, // DONL
0x01, 0x02, 0x03, 0x04, // NAL data.
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, // FU payload.
0x04, 0x05, 0x06, // FU payload.
0x07, 0x08, 0x09, // FU payload.
// NAL 3
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL data.
// NAL 4
0x00, 0x00, 0x00, 0x01, // Start code.
0x01, 0x02, 0x03, 0x04, // NAL 2 data
},
// Second access unit.
{
// NAL 1
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x00, 0x00, // DONL
0x01, 0x02, 0x03, 0x04, // Data.
// NAL 2
0x00, 0x00, 0x00, 0x01, // Start code.
0x40, 0x00, // NAL header (type=32 VPS).
0x00, 0x00, // DONL
0x01, 0x02, 0x03, 0x04, // Data.
},
},
},
}
r := &rtpReader{pkts: packets}
d := &destination{}
l := Lexer{UsingDON: false}
err := l.Lex(d, r, 0)
if err != nil {
t.Fatalf("error lexing: %v\n", err)
}
for testNum, test := range tests {
r := &rtpReader{packets: test.packets}
d := &destination{}
err := (&Lexer{UsingDON: test.don}).Lex(d, r, 0)
if err != nil {
t.Fatalf("error lexing: %v\n", err)
}
for i, au := range expect {
for j, b := range au {
if b != [][]byte(*d)[i][j] {
t.Fatalf("did not get expected data.\nGot: %v\nWant: %v\n", d, expect)
for i, accessUnit := range test.expect {
for j, part := range accessUnit {
if part != [][]byte(*d)[i][j] {
t.Fatalf("did not get expected data for test: %v.\nGot: %v\nWant: %v\n", testNum, d, test.expect)
}
}
}
}