-
Notifications
You must be signed in to change notification settings - Fork 0
/
m3u8_test.go
265 lines (230 loc) · 5.25 KB
/
m3u8_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
265
package m3u8_test
import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
"github.com/practigo/m3u8"
)
func TestM3U8(t *testing.T) {
f, err := os.Open("testdata/sample.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
p, err := m3u8.Decode(f)
if err != nil {
t.Fatal("decode m3u8", err)
}
// firstTags := `#EXT-X-CUSTOM:some-key-values
// #EXTINF:10.000000,
// #EXT-X-TAG:VALUE=5954336
// 00001.ts
// `
// first := p.Front()
// if first != nil {
// t.Log(first.Marshal())
// if first.Marshal() != firstTags {
// // t.Log(first.Marshal())
// t.Error("should retains exact tags in order")
// }
// } else {
// t.Fatal("fail to decode")
// }
second := p.Front().Next()
if second != nil {
// t.Log(second.Directives)
} else {
t.Fatal("fail to decode next")
}
p.InsertAfter(second, m3u8.Entry{
URI: "02-b.ts",
Duration: 6.3,
})
p.InsertAfter(second, m3u8.Entry{
URI: "02-a.ts",
Duration: 3.7,
//
Discontinuity: true,
})
p.Remove(second)
out := p.Marshal()
t.Log(out)
_, err = m3u8.Decode(bytes.NewBuffer([]byte(out)))
if err != nil {
t.Error("marshal output should be decode right:", err)
}
}
func TestZeroDuration(t *testing.T) {
f, err := os.Open("testdata/zero.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
_, err = m3u8.Decode(f)
if err != nil {
t.Fatal("decode m3u8", err)
}
}
func TestMarshal(t *testing.T) {
f, err := os.Open("testdata/sample.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
p, err := m3u8.Decode(f)
if err != nil {
t.Fatal("decode m3u8", err)
}
// error test
tmpFile := filepath.Join(os.TempDir(), "test.m3u8")
tf, err := os.OpenFile(tmpFile, os.O_RDONLY|os.O_CREATE|os.O_TRUNC, 0444)
if err != nil {
t.Fatal(err)
}
err = p.MarshalToWithErr(tf)
if err == nil {
t.Error("should have write error")
} else {
t.Log(err)
}
tf.Close()
os.Remove(tmpFile)
tmpFile = filepath.Join(os.TempDir(), "test2.m3u8")
tf, err = os.Create(tmpFile)
if err != nil {
t.Fatal(err)
}
err = p.MarshalToWithErr(tf)
if err != nil {
t.Error("should not have write error")
}
tf.Close()
os.Remove(tmpFile)
}
func TestResolveURL(t *testing.T) {
t.Log(m3u8.ResolveURL("/unix/path/to/index.m3u8", "a.ts"))
t.Log(m3u8.ResolveURL("http://host/path/to/index.m3u8", "a.ts"))
// special cases
t.Log(m3u8.ResolveURL("http://host/path/to/index.m3u8", "/home/a.ts"))
t.Log(m3u8.ResolveURL("/unix/path/to/index.m3u8", "/home/a.ts"))
t.Log(m3u8.ResolveURL("http://host/path/to/index.m3u8", "http://host/path/to/a.ts"))
}
func TestErrors(t *testing.T) {
errCases := []struct {
file string
err error
}{
{
file: "testdata/no-extinf.m3u8",
err: m3u8.ErrMissingInf,
},
{
file: "testdata/illegal.m3u8",
err: m3u8.ErrMissingInf,
},
}
for _, c := range errCases {
f, err := os.Open(c.file)
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
_, err = m3u8.Decode(f)
if errors.Is(err, c.err) {
t.Log("found error:", err)
} else {
t.Error("should return error missing extinf")
}
}
}
func TestIsMediaPlaylistTag(t *testing.T) {
line := "#EXT-X-VERSION:3"
if !m3u8.IsMediaPlaylistTag(line) {
t.Error(line + " is media playlist tag")
}
}
func TestMaster(t *testing.T) {
f, err := os.Open("testdata/alt-videos.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
m, err := m3u8.DecodeMaster(f)
if err != nil {
t.Fatal("should try to decode a master")
}
t.Log(m.Marshal())
f, err = os.Open("testdata/sample.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
_, err = m3u8.DecodeMaster(f)
if err != m3u8.ErrMissingStream {
t.Fatal("should be ErrMissingStream")
}
}
func TestTryDecodeMaster(t *testing.T) {
f, err := os.Open("testdata/master.m3u8")
if err != nil {
t.Fatal("open test file", err)
}
defer f.Close()
m, err := m3u8.TryDecodeMaster(f)
if err != nil {
t.Fatal("should try to decode a master")
}
t.Log(m)
f, err = os.Open("testdata/sample.m3u8")
if err != nil {
t.Fatal("open test file 2", err)
}
defer f.Close()
_, err = m3u8.TryDecodeMaster(f)
if err != m3u8.ErrNotMaster {
t.Fatal("should report no master")
}
}
func TestMoreM3U8s(t *testing.T) {
files := []string{
"testdata/blank-lines.m3u8",
"testdata/cmaf-byterange.m3u8",
}
for _, file := range files {
f, err := os.Open(file)
if err != nil {
t.Fatal("open test file", file, err)
}
defer f.Close()
p, err := m3u8.Decode(f)
if err != nil {
t.Fatal("decode m3u8", file, err)
}
out := p.Marshal()
// t.Log(out)
_, err = m3u8.Decode(bytes.NewBuffer([]byte(out)))
if err != nil {
t.Fatal("marshal output should be decode right:", file, err)
}
}
}
func TestSplitAttributeList(t *testing.T) {
l, err := m3u8.SplitAttributeList(`URI="init.mp4",BYTERANGE="596@0"`)
if err != nil {
t.Fatal(err)
}
if len(l) != 2 || l["URI"] != "init.mp4" || l["BYTERANGE"] != "596@0" {
t.Fatal("wrong split")
}
l, err = m3u8.SplitAttributeList(`TYPE=AUDIO,URI="audio,with-comma.mp4",BANDWIDTH=1280000`)
if err != nil {
t.Fatal(err)
}
if len(l) != 3 || l["TYPE"] != "AUDIO" || l["URI"] != "audio,with-comma.mp4" ||
l["BANDWIDTH"] != "1280000" {
t.Logf("%#+v", l)
t.Fatal("wrong split 2")
}
}