h264reader_test.go 2.8 KB

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