Skip to content

Commit

Permalink
test: more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbee committed Nov 4, 2024
1 parent 5131098 commit 88dd2df
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 4 deletions.
18 changes: 18 additions & 0 deletions mp4/av1c_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mp4

import (
"testing"

"github.com/Eyevinn/mp4ff/av1"
)

func TestEncodeDecodeAvc1(t *testing.T) {
adc := Av1CBox{
CodecConfRec: av1.CodecConfRec{
Version: 1,
},
}

boxDiffAfterEncodeAndDecode(t, &adc)

}
11 changes: 11 additions & 0 deletions mp4/cdat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mp4

import "testing"

func TestEncodeDecodeCdat(t *testing.T) {

cdat := CdatBox{
Data: []byte{0x01, 0x02, 0x03, 0x04},
}
boxDiffAfterEncodeAndDecode(t, &cdat)
}
9 changes: 7 additions & 2 deletions mp4/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestDecodeFFMpeg(t *testing.T) {
if err != nil {
t.Error(err)
}
sr := bits.NewFixedSliceReader(raw)
box, err := DecodeBoxSR(0, sr)
buf := bytes.NewBuffer(raw)
box, err := DecodeBox(0, buf)
if err != nil {
t.Error(err)
}
Expand All @@ -29,5 +29,10 @@ func TestDecodeFFMpeg(t *testing.T) {
if !bytes.Equal(raw, encBytes) {
t.Errorf("encoded ffmpeg boxes not same as input")
}
}

func TestEncodeData(t *testing.T) {
data := []byte("dummy")
db := &DataBox{Data: data}
boxDiffAfterEncodeAndDecode(t, db)
}
12 changes: 12 additions & 0 deletions mp4/mfhd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mp4

import "testing"

func TestEncodeDecodeMfhd(t *testing.T) {
mfhd := &MfhdBox{
Version: 0,
Flags: 0,
SequenceNumber: 1,
}
boxDiffAfterEncodeAndDecode(t, mfhd)
}
20 changes: 20 additions & 0 deletions mp4/sample_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mp4

import (
"testing"
)

func TestSample(t *testing.T) {
s := NewSample(SyncSampleFlags, 1000, 100, -1001)
fs := FullSample{
Sample: s,
DecodeTime: 1000,
Data: []byte{0x01, 0x02, 0x03, 0x04},
}
if !s.IsSync() {
t.Error("Expected sync sample")
}
if fs.PresentationTime() != 0 {
t.Error("Expected presentation time 0")
}
}
52 changes: 52 additions & 0 deletions mp4/stsd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mp4

import (
"bytes"
"testing"

"github.com/Eyevinn/mp4ff/aac"
)

func TestStsd(t *testing.T) {
stsd := StsdBox{}
samplingFrequency := 48000
asc := &aac.AudioSpecificConfig{
ObjectType: 2,
ChannelConfiguration: 2,
SamplingFrequency: samplingFrequency,
ExtensionFrequency: 0,
SBRPresentFlag: false,
PSPresentFlag: false,
}
buf := &bytes.Buffer{}
err := asc.Encode(buf)
if err != nil {
t.Error(err)
}
ascBytes := buf.Bytes()
esds := CreateEsdsBox(ascBytes)
mp4a := CreateAudioSampleEntryBox("mp4a",
uint16(asc.ChannelConfiguration),
16, uint16(samplingFrequency), esds)
btrt := BtrtBox{
BufferSizeDB: 1536,
MaxBitrate: 96000,
AvgBitrate: 96000,
}
mp4a.AddChild(&btrt)
stsd.AddChild(mp4a)
if len(stsd.Children) != 1 {
t.Error("Expected one child")
}
if stsd.Mp4a == nil {
t.Error("Expected mp4a child")
}
gb := stsd.GetBtrt()
if gb == nil {
t.Error("Expected btrt")
} else {
if *gb != btrt {
t.Errorf("Got btrt %v, expected %v", *gb, btrt)
}
}
}
2 changes: 1 addition & 1 deletion mp4/trak.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (t *TrakBox) GetSampleData(startSampleNr, endSampleNr uint32) ([]Sample, er
if ctts != nil {
cto = ctts.GetCompositionTimeOffset(nr)
}
samples[nr] = Sample{
samples[nr-1] = Sample{
Flags: createSampleFlagsFromProgressiveBoxes(stss, sdtp, nr),
Dur: stts.GetDur(nr),
Size: stbl.Stsz.GetSampleSize(int(nr)),
Expand Down
42 changes: 42 additions & 0 deletions mp4/trak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mp4

import (
"os"
"testing"
)

func TestTrakSampleFunctions(t *testing.T) {
testFile := "testdata/bbb_prog_10s.mp4"
f, err := os.Open(testFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()
mf, err := DecodeFile(f)
if err != nil {
t.Fatal(err)
}
moov := mf.Moov
traks := moov.Traks
if len(traks) != 2 {
t.Fatalf("expected 2 tracks, got %d", len(traks))
}
trak := traks[0]
if trak.Tkhd.TrackID != 1 {
t.Fatalf("expected trackID 1, got %d", trak.Tkhd.TrackID)
}
first2Samples, err := trak.GetSampleData(1, 2)
if err != nil {
t.Fatal(err)
}
if len(first2Samples) != 2 {
t.Fatalf("expected 2 samples, got %d", len(first2Samples))
}
ranges, err := trak.GetRangesForSampleInterval(1, 2)
if err != nil {
t.Fatal(err)
}
if len(ranges) != 1 {
t.Fatalf("expected 1 range, got %d", len(ranges))
}
}
8 changes: 8 additions & 0 deletions mp4/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mp4

import "fmt"

func ExampleGetVersion() {
fmt.Println(GetVersion())
// Output: v0.46, date: 2024-08-08
}
2 changes: 1 addition & 1 deletion sei/sei5.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// UnregisteredSEI is SEI message of type 5.
type UnregisteredSEI struct {
UUID []byte
payload []byte
payload []byte // raw rbsp payload including UUID
}

// Type returns SEI payload type.
Expand Down
31 changes: 31 additions & 0 deletions sei/sei5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sei

import (
"testing"
)

func TestSEI5_UnregisteredType(t *testing.T) {
raw := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x40, 0x40, 0x40, 0x40}

us, err := DecodeUserDataUnregisteredSEI(&SEIData{payloadType: SEIUserDataUnregisteredType,
payload: raw})
if err != nil {
t.Error("Error decoding SEIUserDataUnregisteredType")
}
if us.Type() != SEIUserDataUnregisteredType {
t.Error("Expected SEIUserDataUnregisteredType")
}
if us.Size() != 20 {
t.Errorf("Expected size 20, got %d", us.Size())
}
if string(us.Payload()) != string(raw) {
t.Errorf("Unexpected payload %v, expected %v", us.Payload(), raw)
}
wantedString := "SEI type 5, size=20, uuid=\"000102030405060708090a0b0c0d0e0f\", payload=\"@@@@\""
if wantedString != us.String() {
t.Errorf("Unexpected string %q, expected %q", us.String(), wantedString)
}

}

0 comments on commit 88dd2df

Please sign in to comment.