avframes/main_test.go

266 lines
4.3 KiB
Go

package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
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 TestLayerIndex(t *testing.T) {
bytes := []byte{0xff, 0xe0, 0, 0}
h := header(bytes)
assert.Equal(t, LayerIndexReserved, h.LayerIndex())
bytes = []byte{0xff, 0xe2, 0, 0}
h = header(bytes)
assert.Equal(t, LayerIndexIII, h.LayerIndex())
bytes = []byte{0xff, 0xe4, 0, 0}
h = header(bytes)
assert.Equal(t, LayerIndexII, h.LayerIndex())
bytes = []byte{0xff, 0xe6, 0, 0}
h = header(bytes)
assert.Equal(t, LayerIndexI, h.LayerIndex())
}
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 TestBitRate(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expBitRate uint32
}{
{
"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 uint32
}{
{
"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()))
})
}
}