2019-05-01 07:15:08 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2019-05-01 16:50:25 +03:00
|
|
|
lex_test.go
|
2019-05-01 07:15:08 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
2019-05-01 16:50:25 +03:00
|
|
|
lex_test.go provides tests to check validity of the Lexer found in lex.go.
|
2019-05-01 07:15:08 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package h265
|
2019-05-01 10:15:22 +03:00
|
|
|
|
|
|
|
import (
|
2020-01-17 09:11:12 +03:00
|
|
|
"errors"
|
2019-05-01 10:15:22 +03:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-05-01 18:18:57 +03:00
|
|
|
// rtpReader provides the RTP stream.
|
2019-05-01 10:15:22 +03:00
|
|
|
type rtpReader struct {
|
2019-05-01 17:45:24 +03:00
|
|
|
packets [][]byte
|
|
|
|
idx int
|
2019-05-01 10:15:22 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 18:18:57 +03:00
|
|
|
// Read implements io.Reader.
|
2019-05-01 10:15:22 +03:00
|
|
|
func (r *rtpReader) Read(p []byte) (int, error) {
|
2019-05-01 17:45:24 +03:00
|
|
|
if r.idx == len(r.packets) {
|
2019-05-01 10:15:22 +03:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2019-05-01 17:45:24 +03:00
|
|
|
b := r.packets[r.idx]
|
2019-05-09 05:39:54 +03:00
|
|
|
n := copy(p, b)
|
2019-05-09 08:05:21 +03:00
|
|
|
if n < len(r.packets[r.idx]) {
|
|
|
|
r.packets[r.idx] = r.packets[r.idx][n:]
|
|
|
|
} else {
|
|
|
|
r.idx++
|
|
|
|
}
|
2019-05-09 05:39:54 +03:00
|
|
|
return n, nil
|
2019-05-01 10:15:22 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 18:18:57 +03:00
|
|
|
// destination holds the access units extracted during the lexing process.
|
2019-05-01 10:15:22 +03:00
|
|
|
type destination [][]byte
|
|
|
|
|
2019-05-01 18:18:57 +03:00
|
|
|
// Write implements io.Writer.
|
2019-05-01 10:15:22 +03:00
|
|
|
func (d *destination) Write(p []byte) (int, error) {
|
|
|
|
t := make([]byte, len(p))
|
|
|
|
copy(t, p)
|
|
|
|
*d = append([][]byte(*d), t)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 05:40:46 +03:00
|
|
|
// TestLex checks that the Lexer can correctly extract H265 access units from
|
2019-05-01 18:18:57 +03:00
|
|
|
// HEVC RTP stream in RTP payload format.
|
2019-05-01 17:45:24 +03:00
|
|
|
func TestLex(t *testing.T) {
|
2019-05-01 10:15:22 +03:00
|
|
|
const rtpVer = 2
|
|
|
|
|
2019-05-01 17:45:24 +03:00
|
|
|
tests := []struct {
|
2019-05-01 18:18:57 +03:00
|
|
|
donl bool
|
2019-05-01 17:45:24 +03:00
|
|
|
packets [][]byte
|
|
|
|
expect [][]byte
|
|
|
|
}{
|
|
|
|
{
|
2019-05-01 18:18:57 +03:00
|
|
|
donl: false,
|
2019-05-01 17:45:24 +03:00
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // 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.
|
|
|
|
},
|
2019-05-01 10:15:22 +03:00
|
|
|
},
|
2019-05-01 17:45:24 +03:00
|
|
|
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.
|
2019-05-19 14:15:26 +03:00
|
|
|
0x00, 0x00, 0x01, 0x02, 0x03, // FU payload.
|
2019-05-01 17:45:24 +03:00
|
|
|
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.
|
|
|
|
},
|
2019-05-01 10:15:22 +03:00
|
|
|
},
|
|
|
|
},
|
2019-05-01 17:45:24 +03:00
|
|
|
{
|
2019-05-01 18:18:57 +03:00
|
|
|
donl: true,
|
2019-05-01 17:45:24 +03:00
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // 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.
|
|
|
|
},
|
2019-05-01 10:15:22 +03:00
|
|
|
},
|
2019-05-01 17:45:24 +03:00
|
|
|
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.
|
2019-05-19 14:15:26 +03:00
|
|
|
0x00, 0x00, 0x01, 0x02, 0x03, // FU payload.
|
2019-05-01 17:45:24 +03:00
|
|
|
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.
|
|
|
|
},
|
2019-05-01 10:15:22 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:45:24 +03:00
|
|
|
for testNum, test := range tests {
|
|
|
|
r := &rtpReader{packets: test.packets}
|
|
|
|
d := &destination{}
|
2019-05-01 18:18:57 +03:00
|
|
|
err := NewLexer(test.donl).Lex(d, r, 0)
|
2020-01-17 09:11:12 +03:00
|
|
|
switch {
|
|
|
|
case err == nil || errors.Is(err, io.EOF): // Do nothing
|
|
|
|
default:
|
2019-05-01 17:45:24 +03:00
|
|
|
t.Fatalf("error lexing: %v\n", err)
|
|
|
|
}
|
2019-05-01 10:15:22 +03:00
|
|
|
|
2019-05-01 17:45:24 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-05-01 10:15:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|