1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package h264reader
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func CreateReader(h264 []byte, require *require.Assertions) *H264Reader {
reader, err := NewReader(bytes.NewReader(h264))
require.Nil(err)
require.NotNil(reader)
return reader
}
func TestDataDoesNotStartWithH264Header(t *testing.T) {
require := require.New(t)
testFunction := func(input []byte, expectedErr error) {
reader := CreateReader(input, require)
nal, err := reader.NextNAL()
require.ErrorIs(err, expectedErr)
require.Nil(nal)
}
h264Bytes1 := []byte{2}
testFunction(h264Bytes1, io.EOF)
h264Bytes2 := []byte{0, 2}
testFunction(h264Bytes2, io.EOF)
h264Bytes3 := []byte{0, 0, 2}
testFunction(h264Bytes3, io.EOF)
h264Bytes4 := []byte{0, 0, 2, 0}
testFunction(h264Bytes4, errDataIsNotH264Stream)
h264Bytes5 := []byte{0, 0, 0, 2}
testFunction(h264Bytes5, errDataIsNotH264Stream)
}
func TestParseHeader(t *testing.T) {
require := require.New(t)
h264Bytes := []byte{0x0, 0x0, 0x1, 0xAB}
reader := CreateReader(h264Bytes, require)
nal, err := reader.NextNAL()
require.Nil(err)
require.Equal(1, len(nal.Data))
require.True(nal.ForbiddenZeroBit)
require.Equal(uint32(0), nal.PictureOrderCount)
require.Equal(uint8(1), nal.RefIdc)
require.Equal(NalUnitTypeEndOfStream, nal.UnitType)
}
func TestEOF(t *testing.T) {
require := require.New(t)
testFunction := func(input []byte) {
reader := CreateReader(input, require)
nal, err := reader.NextNAL()
require.Equal(io.EOF, err)
require.Nil(nal)
}
h264Bytes1 := []byte{0, 0, 0, 1}
testFunction(h264Bytes1)
h264Bytes2 := []byte{0, 0, 1}
testFunction(h264Bytes2)
h264Bytes3 := []byte{}
testFunction(h264Bytes3)
}
func TestSkipSEI(t *testing.T) {
require := require.New(t)
h264Bytes := []byte{
0x0, 0x0, 0x0, 0x1, 0xAA,
0x0, 0x0, 0x0, 0x1, 0x6, // SEI
0x0, 0x0, 0x0, 0x1, 0xAB,
}
reader := CreateReader(h264Bytes, require)
nal, err := reader.NextNAL()
require.Nil(err)
require.Equal(byte(0xAA), nal.Data[0])
nal, err = reader.NextNAL()
require.Nil(err)
require.Equal(byte(0xAB), nal.Data[0])
}
func TestIssue1734_NextNal(t *testing.T) {
tt := [...][]byte{
[]byte("\x00\x00\x010\x00\x00\x01\x00\x00\x01"),
[]byte("\x00\x00\x00\x01\x00\x00\x01"),
}
for _, cur := range tt {
r, err := NewReader(bytes.NewReader(cur))
require.NoError(t, err)
// Just make sure it doesn't crash
for {
nal, err := r.NextNAL()
if err != nil || nal == nil {
break
}
}
}
}
func TestTrailing01AfterStartCode(t *testing.T) {
r, err := NewReader(bytes.NewReader([]byte{
0x0, 0x0, 0x0, 0x1, 0x01,
0x0, 0x0, 0x0, 0x1, 0x01,
}))
require.NoError(t, err)
for i := 0; i <= 1; i++ {
nal, err := r.NextNAL()
require.NoError(t, err)
require.NotNil(t, nal)
}
}
|