-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmediasegment_test.go
264 lines (230 loc) · 5.62 KB
/
mediasegment_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package mp4
import (
"bytes"
"io"
"os"
"testing"
"github.com/go-test/deep"
)
func TestMediaSegmentFragmentation(t *testing.T) {
trex := &TrexBox{
TrackID: 2,
}
inFile := "testdata/1.m4s"
inFileGoldenDumpPath := "testdata/golden_1_m4s_dump.txt"
goldenFragPath := "testdata/golden_1_frag.m4s"
goldenFragDumpPath := "testdata/golden_1_frag_m4s_dump.txt"
fd, err := os.Open(inFile)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
f, err := DecodeFile(fd)
if err != io.EOF && err != nil {
t.Error(err)
}
if len(f.Segments) != 1 {
t.Errorf("Not exactly one mediasegment")
}
var bufInSeg bytes.Buffer
f.EncOptimize = OptimizeNone // Avoid trun optimization
f.FragEncMode = EncModeBoxTree
err = f.Encode(&bufInSeg)
if err != nil {
t.Error(err)
}
inSeg, err := os.ReadFile(inFile)
if err != nil {
t.Fatal(err)
}
diff := deep.Equal(inSeg, bufInSeg.Bytes())
if diff != nil {
t.Errorf("Written segment differs from %s", inFile)
}
err = compareOrUpdateInfo(t, f, inFileGoldenDumpPath)
if err != nil {
t.Error(err)
}
mediaSegment := f.Segments[0]
var timeScale uint64 = 90000
var duration uint32 = 45000
fragments, err := mediaSegment.Fragmentify(timeScale, trex, duration)
if err != nil {
t.Errorf("Fragmentation went wrong")
}
if len(fragments) != 4 {
t.Errorf("%d fragments instead of 4", len(fragments))
}
var bufFrag bytes.Buffer
fragmentedSegment := NewMediaSegment()
fragmentedSegment.EncOptimize = OptimizeTrun
fragmentedSegment.Styp = f.Segments[0].Styp
fragmentedSegment.Fragments = fragments
err = fragmentedSegment.Encode(&bufFrag)
if err != nil {
t.Error(err)
}
err = compareOrUpdateInfo(t, fragmentedSegment, goldenFragDumpPath)
if err != nil {
t.Error(err)
}
if *update {
err = writeGolden(t, goldenFragPath, bufFrag.Bytes())
if err != nil {
t.Error(err)
}
} else {
goldenFrag, err := os.ReadFile(goldenFragPath)
if err != nil {
t.Error(err)
}
diff := deep.Equal(goldenFrag, bufFrag.Bytes())
if diff != nil {
t.Errorf("Generated dump different from %s", goldenFragPath)
}
}
}
func TestDoubleDecodeEncodeOptimize(t *testing.T) {
inFile := "testdata/1.m4s"
fd, err := os.Open(inFile)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
enc1 := decodeEncode(t, fd, OptimizeTrun)
buf1 := bytes.NewBuffer(enc1)
enc2 := decodeEncode(t, buf1, OptimizeTrun)
diff := deep.Equal(enc2, enc1)
if diff != nil {
t.Errorf("Second write gives diff %s", diff)
}
}
func TestDecodeEncodeNoOptimize(t *testing.T) {
inFile := "testdata/1.m4s"
data, err := os.ReadFile(inFile)
if err != nil {
t.Fatal(err)
}
buf0 := bytes.NewBuffer(data)
enc := decodeEncode(t, buf0, OptimizeNone)
diff := deep.Equal(enc, data)
if diff != nil {
t.Errorf("First encode gives diff %s", diff)
}
}
func decodeEncode(t *testing.T, r io.Reader, optimize EncOptimize) []byte {
f, err := DecodeFile(r)
if err != nil {
t.Fatal(err)
}
buf := bytes.Buffer{}
f.EncOptimize = optimize
err = f.Encode(&buf)
if err != nil {
t.Error(err)
}
return buf.Bytes()
}
func TestMoofEncrypted(t *testing.T) {
inFile := "testdata/moof_enc.m4s"
inFileGoldenDumpPath := "testdata/golden_moof_enc_m4s_dump.txt"
fd, err := os.Open(inFile)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
f, err := DecodeFile(fd)
if err != io.EOF && err != nil {
t.Error(err)
}
var bufOut bytes.Buffer
f.FragEncMode = EncModeBoxTree
err = f.Encode(&bufOut)
if err != nil {
t.Error(err)
}
inSeg, err := os.ReadFile(inFile)
if err != nil {
t.Fatal(err)
}
diff := deep.Equal(inSeg, bufOut.Bytes())
if diff != nil {
tmpOutput := "testdata/moof_enc_tmp.mp4"
err := writeGolden(t, tmpOutput, bufOut.Bytes())
if err == nil {
t.Errorf("Encoded output not same as input for %s. Wrote %s", inFile, tmpOutput)
} else {
t.Errorf("Encoded output not same as input for %s, but error %s when writing %s", inFile, err, tmpOutput)
}
}
err = compareOrUpdateInfo(t, f, inFileGoldenDumpPath)
if err != nil {
t.Error(err)
}
}
func TestDecodeEncodeCencFragmentedFile(t *testing.T) {
inData, err := os.ReadFile("testdata/prog_8s_enc_dashinit.mp4")
if err != nil {
t.Fatal(err)
}
inBuf := bytes.NewBuffer(inData)
decFile, err := DecodeFile(inBuf)
if err != nil {
t.Error(err)
}
outSlice := make([]byte, 0, len(inData))
outBuf := bytes.NewBuffer(outSlice)
decFile.FragEncMode = EncFragFileMode(EncModeBoxTree)
err = decFile.Encode(outBuf)
if err != nil {
t.Error(err)
}
outData := outBuf.Bytes()
if !bytes.Equal(inData, outData) {
t.Errorf("generated bytes differ from input")
}
}
func TestCommonSampleDuration(t *testing.T) {
cases := []struct {
inFile string
trackID uint32
wantedCommonDur uint32
wantedError string
}{
{"testdata/1.m4s", 2, 3000, ""},
{"testdata/golden_1_frag.m4s", 2, 3000, ""},
{"testdata/1.m4s", 1, 0, "fragment.CommonSampleDuration: no track with trex trackID=1"},
}
for _, c := range cases {
fd, err := os.Open(c.inFile)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
f, err := DecodeFile(fd)
if err != nil {
t.Fatal(err)
}
trex := &TrexBox{
TrackID: c.trackID,
}
for _, s := range f.Segments {
gotCommonDur, err := s.CommonSampleDuration(trex)
if c.wantedError != "" {
if err == nil {
t.Errorf("case %s: wanted error %s, got nil", c.inFile, c.wantedError)
}
if err.Error() != c.wantedError {
t.Errorf("case %q: wanted error %q, got %q", c.inFile, c.wantedError, err.Error())
}
continue
}
if err != nil {
t.Fatal(err)
}
if gotCommonDur != c.wantedCommonDur {
t.Errorf("case %s: got commonDur %d, wanted %d", c.inFile, gotCommonDur, c.wantedCommonDur)
}
}
}
}