-
Notifications
You must be signed in to change notification settings - Fork 1
/
goffmpeg_test.go
122 lines (84 loc) · 2.06 KB
/
goffmpeg_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package goffmpeg
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
const (
tmpDirPrefix = "gotestout"
tmpFileName = "tmpfile"
G729InFilePath = "testfiles/G729.raw"
G729OutFilePath = "testfiles/G729.wav"
G723InFilePath = "testfiles/G723.raw"
G723OutFilePath = "testfiles/G723.wav"
)
func TestDecodeG729(t *testing.T) {
assertDecode(t, "G729", G729InFilePath, G729OutFilePath)
}
func TestDecodeG723(t *testing.T) {
assertDecode(t, "G723", G723InFilePath, G723OutFilePath)
}
func assertDecode(t *testing.T, codec string, inputFile, expectedFilePath string) {
t.Helper()
tmpDir := createTmpDir(t, tmpDirPrefix)
defer os.RemoveAll(tmpDir)
tmpFilePath := filepath.Join(tmpDir, tmpFileName)
decoder := getDecoder(t, codec)
defer decoder.Destroy()
byteStream := readFile(t, inputFile)
data := decodeData(t, decoder, byteStream)
writeFile(t, tmpFilePath, data)
writtenFile := readFile(t, tmpFilePath)
expectedOutput := readFile(t, expectedFilePath)
assertFilesEqual(t, writtenFile, expectedOutput)
}
func readFile(t *testing.T, path string) []byte {
t.Helper()
f, err := ioutil.ReadFile(path)
if err != nil {
t.Error(err)
}
return f
}
func createTmpDir(t *testing.T, prefix string) string {
t.Helper()
dir, err := ioutil.TempDir("", prefix)
if err != nil {
t.Error(err)
}
return dir
}
func getDecoder(t *testing.T, codec string) Decoder {
t.Helper()
d, err := NewFFMPEGDecoder(codec)
if err != nil {
t.Error(err)
}
decoder, ok := d.(Decoder)
if !ok {
t.Errorf("interface type not of Decoder")
}
return decoder
}
func decodeData(t *testing.T, decoder Decoder, in []byte) []byte {
t.Helper()
data, err := decoder.Decode(in)
if err != nil {
t.Error(err)
}
return data
}
func writeFile(t *testing.T, path string, data []byte) {
t.Helper()
if err := ioutil.WriteFile(path, data, 0755); err != nil {
t.Error(err)
}
}
func assertFilesEqual(t *testing.T, b1, b2 []byte) {
t.Helper()
if !bytes.Equal(b1, b2) {
t.Errorf("bytestream of files are different. Lengths: %d vs %d", len(b1), len(b2))
}
}