471 lines
7.7 KiB
Go
471 lines
7.7 KiB
Go
package mpeg_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.netflux.io/rob/avframes/pkg/mpeg"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestHasSyncWord(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
bytes []byte
|
|
expResult bool
|
|
}{
|
|
{
|
|
"empty mpeg.Header",
|
|
[]byte{0, 0, 0, 0},
|
|
false,
|
|
},
|
|
{
|
|
"almost",
|
|
[]byte{0xff, 0xc0, 0, 0},
|
|
false,
|
|
},
|
|
{
|
|
"present",
|
|
[]byte{0xff, 0xe0, 0, 0},
|
|
true,
|
|
},
|
|
{
|
|
"full mpeg.Header",
|
|
[]byte{0xff, 0xff, 0xff, 0xff},
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h := mpeg.Header(tc.bytes)
|
|
assert.Equal(t, tc.expResult, h.HasSyncWord())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAudioVersionId(t *testing.T) {
|
|
bytes := []byte{0xff, 0xe8, 0, 0}
|
|
h := mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.AudioVersionReserved, h.AudioVersionId())
|
|
|
|
bytes = []byte{0xff, 0xf0, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.AudioVersionMPEG2, h.AudioVersionId())
|
|
|
|
bytes = []byte{0xff, 0xff, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.AudioVersionMPEG1, h.AudioVersionId())
|
|
|
|
bytes = []byte{0xff, 0xe0, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.AudioVersionMPEG25, h.AudioVersionId())
|
|
}
|
|
|
|
func TestLayer(t *testing.T) {
|
|
bytes := []byte{0xff, 0xe0, 0, 0}
|
|
h := mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.LayerReserved, h.Layer())
|
|
|
|
bytes = []byte{0xff, 0xe2, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.LayerIII, h.Layer())
|
|
|
|
bytes = []byte{0xff, 0xe4, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.LayerII, h.Layer())
|
|
|
|
bytes = []byte{0xff, 0xe6, 0, 0}
|
|
h = mpeg.Header(bytes)
|
|
assert.Equal(t, mpeg.LayerI, h.Layer())
|
|
}
|
|
|
|
func TestPaddingBytes(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
bytes []byte
|
|
expPadding int
|
|
}{
|
|
{
|
|
"MPEG1 Layer3 128kbps 44.1k, no padding",
|
|
[]byte{0xff, 0xfa, 0x90, 0},
|
|
0,
|
|
},
|
|
{
|
|
"MPEG1 Layer3 128kbps 44.1k, padding",
|
|
[]byte{0xff, 0xfa, 0x92, 0},
|
|
1,
|
|
},
|
|
{
|
|
"MPEG2 Layer1 144kbps 24k, no padding",
|
|
[]byte{0xff, 0xf6, 0x94, 0},
|
|
0,
|
|
},
|
|
{
|
|
"MPEG2 Layer1 144kbps 24k, padding",
|
|
[]byte{0xff, 0xf6, 0x96, 0},
|
|
4,
|
|
},
|
|
{
|
|
"MPEG25 Layer2 56kbps 8k, no padding",
|
|
[]byte{0xff, 0xe4, 0x78, 0},
|
|
0,
|
|
},
|
|
{
|
|
"MPEG25 Layer2 56kbps 8k, padding",
|
|
[]byte{0xff, 0xe4, 0x7a, 0},
|
|
1,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h := mpeg.Header(tc.bytes)
|
|
assert.Equal(t, tc.expPadding, h.PaddingBytes())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsProtected(t *testing.T) {
|
|
bytes := []byte{0xff, 0xe1, 0, 0}
|
|
h := mpeg.Header(bytes)
|
|
assert.True(t, h.IsProtected())
|
|
|
|
bytes = []byte{0xff, 0xe0, 0, 0}
|
|
h = mpeg.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 := mpeg.Header(tc.bytes)
|
|
assert.Equal(t, tc.expSamplesPerFrame, h.SamplesPerFrame())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBitRate(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
bytes []byte
|
|
expBitRate int
|
|
}{
|
|
{
|
|
"empty mpeg.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 mpeg.Header",
|
|
[]byte{1, 1, 1, 1},
|
|
0,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h := mpeg.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 := mpeg.Header(tc.bytes)
|
|
assert.Equal(t, int(tc.expSampleRate), int(h.SampleRate()))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLen(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
bytes []byte
|
|
expLen int
|
|
}{
|
|
{
|
|
"MPEG1 Layer3 128kbps 44.1k, no padding",
|
|
[]byte{0xff, 0xfa, 0x90, 0},
|
|
417,
|
|
},
|
|
{
|
|
"MPEG1 Layer3 128kbps 44.1k, padding",
|
|
[]byte{0xff, 0xfa, 0x92, 0},
|
|
418,
|
|
},
|
|
{
|
|
"MPEG2 Layer1 144kbps 24k, no padding",
|
|
[]byte{0xff, 0xf6, 0x94, 0},
|
|
288,
|
|
},
|
|
{
|
|
"MPEG2 Layer1 144kbps 24k, padding",
|
|
[]byte{0xff, 0xf6, 0x96, 0},
|
|
292,
|
|
},
|
|
{
|
|
"MPEG25 Layer2 56kbps 8k, no padding",
|
|
[]byte{0xff, 0xe4, 0x78, 0},
|
|
1008,
|
|
},
|
|
{
|
|
"MPEG25 Layer2 56kbps 8k, padding",
|
|
[]byte{0xff, 0xe4, 0x7a, 0},
|
|
1009,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h := mpeg.Header(tc.bytes)
|
|
assert.Equal(t, tc.expLen, h.Len())
|
|
})
|
|
}
|
|
}
|