Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests in most packages #386

Merged
merged 17 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- CreatePrftBox now takes flags parameter
- PrftBox Info output
- Removed ReplaceChild method of StsdBox
- CreateHdlr name for timed metadata

### Added

- NTP64 struct with methods to convert to time.Time
- Constants for PrftBox flags
- Unittest to all commands and examples


### Fixed

- Allow missing optional DecoderSpecificInfo
Expand Down
16 changes: 6 additions & 10 deletions aac/aac.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aac

import (
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -90,28 +89,25 @@ func DecodeAudioSpecificConfig(r io.Reader) (*AudioSpecificConfig, error) {
asc.SBRPresentFlag = true
asc.PSPresentFlag = true
default:
return asc, errors.New("Only LC, HE-AACv1, and HE-AACv2 supported")
return asc, fmt.Errorf("unsupported object type: %d", audioObjectType)
}
frequency, ok := getFrequency(br)
if !ok {
return asc, fmt.Errorf("Strange frequency index")
return asc, fmt.Errorf("strange frequency index")
}
asc.SamplingFrequency = frequency
asc.ChannelConfiguration = byte(br.Read(4))
switch audioObjectType {
case HEAACv1, HEAACv2:
frequency, ok := getFrequency(br)
extFrequency, ok := getFrequency(br)
if !ok {
return asc, errors.New("Strange frequency index")
return asc, fmt.Errorf("strange frequency index")
}
asc.ExtensionFrequency = frequency
asc.ExtensionFrequency = extFrequency
audioObjectType = byte(br.Read(5)) // Shall be set to AAC-LC here again
if audioObjectType == 22 {
return asc, errors.New("ExtensionChannelConfiguration not supported")
}
}
if audioObjectType != AAClc {
return nil, fmt.Errorf("Base audioObjectType is %d instead of AAC-LC (2)", audioObjectType)
return nil, fmt.Errorf("base audioObjectType is %d instead of AAC-LC (2)", audioObjectType)
}
//GASpecificConfig()
_ = br.Read(3) //GASpecificConfig
Expand Down
46 changes: 46 additions & 0 deletions aac/aac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,51 @@ func TestAudioSpecificConfigEncodeDecode(t *testing.T) {
t.Errorf("Diff %v for %+v", diff, asc)
}
}
}

func TestVariousInputs(t *testing.T) {

testCases := []struct {
desc string
data []byte
expectedError string
}{
{
desc: "unsupported object type",
data: []byte{0x0f, 0x00},
expectedError: "unsupported object type: 1",
},
{
desc: "bad frequency index",
data: []byte{0x17, 0x30},
expectedError: "strange frequency index",
},
{
desc: "too short extended frequency",
data: []byte{0x17, 0x80, 0x40},
expectedError: "strange frequency index",
},
}

for _, c := range testCases {
t.Run(c.desc, func(t *testing.T) {
readBuf := bytes.NewBuffer(c.data)
_, err := DecodeAudioSpecificConfig(readBuf)
if err == nil || err.Error() != c.expectedError {
t.Errorf("Expected error: %s", c.expectedError)
}
})
}

t.Run("32768Hz", func(t *testing.T) {
data := []byte{0x17, 0x80, 0x40, 0x00, 0x00}
readBuf := bytes.NewBuffer(data)
gotAsc, err := DecodeAudioSpecificConfig(readBuf)
if err != nil {
t.Error(err)
}
if gotAsc.SamplingFrequency != 32768 {
t.Errorf("Expected 32768Hz, got %d", gotAsc.SamplingFrequency)
}
})
}
28 changes: 16 additions & 12 deletions aac/adts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ func TestADTS(t *testing.T) {
adtsBytes := adtsHdrStart.Encode()

testCases := []struct {
adtsBytes []byte
wantedHdr *ADTSHeader
wantedOffset int
wantedError error
adtsBytes []byte
wantedHdr *ADTSHeader
wantedOffset int
wantedFrequency uint16
wantedError error
}{
{
adtsBytes: adtsBytes,
wantedHdr: adtsHdrStart,
wantedOffset: 0,
wantedError: nil,
adtsBytes: adtsBytes,
wantedHdr: adtsHdrStart,
wantedOffset: 0,
wantedFrequency: 48000,
},
{
adtsBytes: append([]byte{0xfe}, adtsBytes...),
wantedHdr: adtsHdrStart,
wantedOffset: 1,
wantedError: nil,
adtsBytes: append([]byte{0xfe}, adtsBytes...),
wantedHdr: adtsHdrStart,
wantedOffset: 1,
wantedFrequency: 48000,
},
}

Expand All @@ -45,6 +46,9 @@ func TestADTS(t *testing.T) {
if gotOffset != tc.wantedOffset {
t.Errorf("Got offset %d instead of %d", gotOffset, tc.wantedOffset)
}
if tc.wantedFrequency != gotHdr.Frequency() {
t.Errorf("Got frequency %d instead of %d", gotHdr.Frequency(), tc.wantedFrequency)
}
if diff := deep.Equal(gotHdr, tc.wantedHdr); diff != nil {
t.Error(diff)
}
Expand Down
21 changes: 21 additions & 0 deletions avc/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ func TestSliceTypeParser(t *testing.T) {
}
}

func TestSliceTypeStrings(t *testing.T) {
cases := []struct {
sliceType SliceType
want string
}{
{SLICE_P, "P"},
{SLICE_B, "B"},
{SLICE_I, "I"},
{SLICE_SP, "SP"},
{SLICE_SI, "SI"},
{SliceType(12), ""},
}
for _, c := range cases {
got := c.sliceType.String()
if got != c.want {
t.Errorf("got %s want %s", got, c.want)
}
}

}

func TestParseSliceHeader_BlackFrame(t *testing.T) {
wantedHdr := SliceHeader{
SliceType: 7,
Expand Down
3 changes: 3 additions & 0 deletions hevc/hevc.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func FindNaluTypesUpToFirstVideoNalu(sample []byte) []NaluType {
func ContainsNaluType(sample []byte, specificNaluType NaluType) bool {
var pos uint32 = 0
length := len(sample)
if length < 4 {
return false
}
for pos < uint32(length-4) {
naluLength := binary.BigEndian.Uint32(sample[pos : pos+4])
pos += 4
Expand Down
97 changes: 77 additions & 20 deletions hevc/hevc_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package hevc

import (
"strings"
"testing"

"github.com/go-test/deep"
)

func TestGetNaluTypes(t *testing.T) {
testCases := []struct {
name string
input []byte
wanted []NaluType
name string
input []byte
wanted []NaluType
nalusUpToFirstVideo []NaluType
containsVPS bool
isRapSample bool
isIDRSample bool
}{
{
"AUD",
[]byte{0, 0, 0, 2, 70, 0},
[]NaluType{NALU_AUD},
[]NaluType{NALU_AUD},
false,
false,
false,
},
{
"AUD, VPS, SPS, PPS, and IDR ",
Expand All @@ -26,14 +35,45 @@ func TestGetNaluTypes(t *testing.T) {
0, 0, 0, 3, 68, 3, 3,
0, 0, 0, 3, 40, 4, 4},
[]NaluType{NALU_AUD, NALU_VPS, NALU_SPS, NALU_PPS, NALU_IDR_N_LP},
[]NaluType{NALU_AUD, NALU_VPS, NALU_SPS, NALU_PPS, NALU_IDR_N_LP},
true,
true,
true,
},
{
"too short",
[]byte{0, 0, 0},
[]NaluType{},
[]NaluType{},
false,
false,
false,
},
}

for _, tc := range testCases {
got := FindNaluTypes(tc.input)
if diff := deep.Equal(got, tc.wanted); diff != nil {
t.Errorf("%s: %v", tc.name, diff)
}
t.Run(tc.name, func(t *testing.T) {
got := FindNaluTypes(tc.input)
if diff := deep.Equal(got, tc.wanted); diff != nil {
t.Errorf("nalulist diff: %v", diff)
}
got = FindNaluTypesUpToFirstVideoNalu(tc.input)
if diff := deep.Equal(got, tc.nalusUpToFirstVideo); diff != nil {
t.Errorf("nalus before first video diff: %v", diff)
}
hasVPS := ContainsNaluType(tc.input, NALU_VPS)
if hasVPS != tc.containsVPS {
t.Errorf("got %t instead of %t", hasVPS, tc.containsVPS)
}
isRAP := IsRAPSample(tc.input)
if isRAP != tc.isRapSample {
t.Errorf("got %t instead of %t", isRAP, tc.isRapSample)
}
isIDR := IsIDRSample(tc.input)
if isIDR != tc.isIDRSample {
t.Errorf("got %t instead of %t", isIDR, tc.isIDRSample)
}
})
}
}

Expand Down Expand Up @@ -61,10 +101,12 @@ func TestHasParameterSets(t *testing.T) {
}

for _, tc := range testCases {
got := HasParameterSets(tc.input)
if got != tc.wanted {
t.Errorf("%s: got %t instead of %t", tc.name, got, tc.wanted)
}
t.Run(tc.name, func(t *testing.T) {
got := HasParameterSets(tc.input)
if got != tc.wanted {
t.Errorf("got %t instead of %t", got, tc.wanted)
}
})
}
}

Expand Down Expand Up @@ -96,15 +138,30 @@ func TestGetParameterSets(t *testing.T) {
}

for _, tc := range testCases {
gotVPS, gotSPS, gotPPS := GetParameterSets(tc.input)
if diff := deep.Equal(gotVPS, tc.wantedVPS); diff != nil {
t.Errorf("%s VPS: %v", tc.name, diff)
}
if diff := deep.Equal(gotSPS, tc.wantedSPS); diff != nil {
t.Errorf("%s SPS: %v", tc.name, diff)
}
if diff := deep.Equal(gotPPS, tc.wantedPPS); diff != nil {
t.Errorf("%s PPS: %v", tc.name, diff)
t.Run(tc.name, func(t *testing.T) {
gotVPS, gotSPS, gotPPS := GetParameterSets(tc.input)
if diff := deep.Equal(gotVPS, tc.wantedVPS); diff != nil {
t.Errorf("VPS diff: %v", diff)
}
if diff := deep.Equal(gotSPS, tc.wantedSPS); diff != nil {
t.Errorf("SPS diff: %v", diff)
}
if diff := deep.Equal(gotPPS, tc.wantedPPS); diff != nil {
t.Errorf("PPS diff: %v", diff)
}
})
}
}

func TestNaluTypeStrings(t *testing.T) {
named := 0
for n := NaluType(0); n < NaluType(64); n++ {
desc := n.String()
if !strings.HasPrefix(desc, "Other") {
named++
}
}
if named != 22 {
t.Errorf("got %d named instead of 22", named)
}
}
8 changes: 7 additions & 1 deletion hevc/hevcdecoderconfigurationrecord_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hevc

import (
"bytes"
"encoding/hex"
"strings"
"testing"
Expand Down Expand Up @@ -96,7 +97,6 @@ func TestDecodeConfRec(t *testing.T) {
t.Error(err)
}
for _, na := range hdcr.NaluArrays {

switch na.NaluType() {
case NALU_VPS, NALU_PPS:
case NALU_SPS:
Expand Down Expand Up @@ -127,4 +127,10 @@ func TestDecodeConfRec(t *testing.T) {
t.Errorf("strange nalu type %s", na.NaluType())
}
}

out := bytes.Buffer{}
err = hdcr.Encode(&out)
if err != nil {
t.Error(err)
}
}
Loading
Loading