avframes/main_test.go

388 lines
6.1 KiB
Go

package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestHasSyncWord(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expResult bool
}{
{
"empty header",
[]byte{0, 0, 0, 0},
false,
},
{
"almost",
[]byte{0xff, 0xc0, 0, 0},
false,
},
{
"present",
[]byte{0xff, 0xe0, 0, 0},
true,
},
{
"full header",
[]byte{0xff, 0xff, 0xff, 0xff},
true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, tc.expResult, h.HasSyncWord())
})
}
}
func TestAudioVersionId(t *testing.T) {
bytes := []byte{0xff, 0xe8, 0, 0}
h := header(bytes)
assert.Equal(t, AudioVersionReserved, h.AudioVersionId())
bytes = []byte{0xff, 0xf0, 0, 0}
h = header(bytes)
assert.Equal(t, AudioVersionMPEG2, h.AudioVersionId())
bytes = []byte{0xff, 0xff, 0, 0}
h = header(bytes)
assert.Equal(t, AudioVersionMPEG1, h.AudioVersionId())
bytes = []byte{0xff, 0xe0, 0, 0}
h = header(bytes)
assert.Equal(t, AudioVersionMPEG25, h.AudioVersionId())
}
func TestLayer(t *testing.T) {
bytes := []byte{0xff, 0xe0, 0, 0}
h := header(bytes)
assert.Equal(t, LayerReserved, h.Layer())
bytes = []byte{0xff, 0xe2, 0, 0}
h = header(bytes)
assert.Equal(t, LayerIII, h.Layer())
bytes = []byte{0xff, 0xe4, 0, 0}
h = header(bytes)
assert.Equal(t, LayerII, h.Layer())
bytes = []byte{0xff, 0xe6, 0, 0}
h = header(bytes)
assert.Equal(t, LayerI, h.Layer())
}
func TestPaddingBytes(t *testing.T) {
bytes := []byte{0xff, 0xe0, 0x2, 0}
h := header(bytes)
assert.Equal(t, 1, h.PaddingBytes())
bytes = []byte{0xff, 0xe0, 0x0, 0}
h = header(bytes)
assert.Equal(t, 0, h.PaddingBytes())
}
func TestIsProtected(t *testing.T) {
bytes := []byte{0xff, 0xe1, 0, 0}
h := header(bytes)
assert.True(t, h.IsProtected())
bytes = []byte{0xff, 0xe0, 0, 0}
h = header(bytes)
assert.False(t, h.IsProtected())
}
func TestSamplesPerFrame(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expSamplesPerFrame int
}{
{
"MPEG1 LayerI",
[]byte{0xff, 0xfe, 0, 0},
384,
},
{
"MPEG1 LayerII",
[]byte{0xff, 0xfc, 0, 0},
1152,
},
{
"MPEG1 LayerIII",
[]byte{0xff, 0xfa, 0, 0},
1152,
},
{
"MPEG1 LayerReserved",
[]byte{0xff, 0xf8, 0, 0},
0,
},
{
"MPEG2 LayerI",
[]byte{0xff, 0xf6, 0, 0},
384,
},
{
"MPEG2 LayerII",
[]byte{0xff, 0xf4, 0, 0},
1152,
},
{
"MPEG2 LayerIII",
[]byte{0xff, 0xf2, 0, 0},
576,
},
{
"MPEG2 LayerReserved",
[]byte{0xff, 0xf0, 0, 0},
0,
},
{
"MPEG25 LayerI",
[]byte{0xff, 0xe6, 0, 0},
384,
},
{
"MPEG25 LayerII",
[]byte{0xff, 0xe4, 0, 0},
1152,
},
{
"MPEG25 LayerIII",
[]byte{0xff, 0xe2, 0, 0},
576,
},
{
"MPEG25 LayerReserved",
[]byte{0xff, 0xe0, 0, 0},
0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, tc.expSamplesPerFrame, h.SamplesPerFrame())
})
}
}
func TestBitRate(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expBitRate int
}{
{
"empty header",
[]byte{0, 0, 0, 0},
0,
},
{
"MPEG1 LayerI 32kbps",
[]byte{0xff, 0xfe, 0x10, 0},
32,
},
{
"MPEG1 LayerI 448kbps",
[]byte{0xff, 0xfe, 0xe0, 0},
448,
},
{
"MPEG1 LayerII 32kbps",
[]byte{0xff, 0xfc, 0x10, 0},
32,
},
{
"MPEG1 LayerII 448kbps",
[]byte{0xff, 0xfc, 0xe0, 0},
384,
},
{
"MPEG1 LayerIII 32kbps",
[]byte{0xff, 0xfa, 0x10, 0},
32,
},
{
"MPEG1 LayerIII 128kbps",
[]byte{0xff, 0xfa, 0x90, 0},
128,
},
{
"MPEG1 LayerIII 160kbps",
[]byte{0xff, 0xfa, 0xa0, 0},
160,
},
{
"MPEG1 LayerIII 192kbps",
[]byte{0xff, 0xfb, 0xb0, 0},
192,
},
{
"MPEG1 LayerIII 320kbps",
[]byte{0xff, 0xfa, 0xe0, 0},
320,
},
{
"MPEG1 LayerIII reserved",
[]byte{0xff, 0xfa, 0xf0, 0},
0,
},
{
"MPEG2 LayerI 32kbps",
[]byte{0xff, 0xf6, 0x10, 0},
32,
},
{
"MPEG2 LayerI 256",
[]byte{0xff, 0xf6, 0xe0, 0},
256,
},
{
"MPEG2 LayerII 32kbps",
[]byte{0xff, 0xf4, 0x10, 0},
8,
},
{
"MPEG2 LayerII 160kbps",
[]byte{0xff, 0xf4, 0xe0, 0},
160,
},
{
"MPEG2 LayerIII 32kbps",
[]byte{0xff, 0xf2, 0x10, 0},
8,
},
{
"MPEG2 LayerIII 160kbps",
[]byte{0xff, 0xf2, 0xe0, 0},
160,
},
{
"MPEG25 LayerI 32kbps",
[]byte{0xff, 0xe6, 0x10, 0},
32,
},
{
"MPEG25 LayerI 256",
[]byte{0xff, 0xe6, 0xe0, 0},
256,
},
{
"MPEG25 LayerII 32kbps",
[]byte{0xff, 0xe4, 0x10, 0},
8,
},
{
"MPEG25 LayerII 160kbps",
[]byte{0xff, 0xe4, 0xe0, 0},
160,
},
{
"MPEG25 LayerIII 32kbps",
[]byte{0xff, 0xe2, 0x10, 0},
8,
},
{
"MPEG25 LayerIII 160kbps",
[]byte{0xff, 0xe2, 0xe0, 0},
160,
},
{
"full header",
[]byte{1, 1, 1, 1},
0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, tc.expBitRate, h.BitRate())
})
}
}
func TestSampleRate(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expSampleRate int
}{
{
"MPEG1 44.1k",
[]byte{0xff, 0xfa, 0x90, 0},
44100,
},
{
"MPEG1 48k",
[]byte{0xff, 0xfa, 0x94, 0},
48000,
},
{
"MPEG1 32k",
[]byte{0xff, 0xfa, 0x98, 0},
32000,
},
{
"MPEG1 Reserved",
[]byte{0xff, 0xfa, 0x9c, 0},
0,
},
{
"MPEG2 22.05k",
[]byte{0xff, 0xf6, 0x90, 0},
22050,
},
{
"MPEG2 24k",
[]byte{0xff, 0xf6, 0x94, 0},
24000,
},
{
"MPEG2 16k",
[]byte{0xff, 0xf6, 0x98, 0},
16000,
},
{
"MPEG2 Reserved",
[]byte{0xff, 0xf6, 0x9c, 0},
0,
},
{
"MPEG25 11.025k",
[]byte{0xff, 0xe6, 0x90, 0},
11025,
},
{
"MPEG25 12k",
[]byte{0xff, 0xe6, 0x94, 0},
12000,
},
{
"MPEG25 8k",
[]byte{0xff, 0xe6, 0x98, 0},
8000,
},
{
"MPEG25 Reserved",
[]byte{0xff, 0xe6, 0x9c, 0},
0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, int(tc.expSampleRate), int(h.SampleRate()))
})
}
}