forked from grafov/m3u8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
535 lines (499 loc) · 15.7 KB
/
writer_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
Package m3u8. Playlist generation tests.
Copyleft 2013-2015 Alexander I.Grafov aka Axel <grafov@gmail.com>
Copyleft 2013-2015 library authors (see AUTHORS file).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
ॐ तारे तुत्तारे तुरे स्व
*/
package m3u8
import (
"fmt"
"strings"
"testing"
"time"
)
// Check how master and media playlists implement common Playlist interface
func TestInterfaceImplemented(t *testing.T) {
m := NewMasterPlaylist()
CheckType(t, m)
p, e := NewMediaPlaylist(1, 2)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
CheckType(t, p)
}
// Create new media playlist with wrong size (must be failed)
func TestCreateMediaPlaylistWithWrongSize(t *testing.T) {
_, e := NewMediaPlaylist(2, 1) //wrong winsize
if e == nil {
t.Fatal("Create new media playlist must be failed, but it's don't")
}
}
// Create new media playlist
// Add two segments to media playlist
func TestAddSegmentToMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(1, 2)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 5.0, "")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
e = p.Append("test02.ts", 6.0, "")
if e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
}
// Create new media playlist
// Add three segments to media playlist
// Set discontinuity tag for the 2nd segment.
func TestDiscontinuityForMediaPlaylist(t *testing.T) {
var e error
p, e := NewMediaPlaylist(3, 4)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.Close()
if e = p.Append("test01.ts", 5.0, ""); e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if e = p.Append("test02.ts", 6.0, ""); e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
if e = p.SetDiscontinuity(); e != nil {
t.Error("Can't set discontinuity tag")
}
if e = p.Append("test03.ts", 6.0, ""); e != nil {
t.Errorf("Add 3nd segment to a media playlist failed: %s", e)
}
//fmt.Println(p.Encode().String())
}
// Create new media playlist
// Add three segments to media playlist
// Set program date and time for 2nd segment.
// Set discontinuity tag for the 2nd segment.
func TestProgramDateTimeForMediaPlaylist(t *testing.T) {
var e error
p, e := NewMediaPlaylist(3, 4)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.Close()
if e = p.Append("test01.ts", 5.0, ""); e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if e = p.Append("test02.ts", 6.0, ""); e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
loc, _ := time.LoadLocation("Europe/Moscow")
if e = p.SetProgramDateTime(time.Date(2010, time.November, 30, 16, 25, 0, 125*1e6, loc)); e != nil {
t.Error("Can't set program date and time")
}
if e = p.SetDiscontinuity(); e != nil {
t.Error("Can't set discontinuity tag")
}
if e = p.Append("test03.ts", 6.0, ""); e != nil {
t.Errorf("Add 3nd segment to a media playlist failed: %s", e)
}
//fmt.Println(p.Encode().String())
}
// Create new media playlist
// Add two segments to media playlist with duration 9.0 and 9.1.
// Target duration must be set to nearest greater integer (= 10).
func TestTargetDurationForMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(1, 2)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 9.0, "")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
e = p.Append("test02.ts", 9.1, "")
if e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
if p.TargetDuration < 10.0 {
t.Errorf("Target duration must = 10 (nearest greater integer to durations 9.0 and 9.1)")
}
}
// Create new media playlist with capacity 10 elements
// Try to add 11 segments to media playlist (oversize error)
func TestOverAddSegmentsToMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(1, 10)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 11; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Logf("As expected new segment #%d not assigned to a media playlist: %s due oversize\n", i, e)
}
}
}
// Create new media playlist
// Add segment to media playlist
// Set encryption key
func TestSetKeyForMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 5.0, "")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
e = p.SetKey("AES-128", "https://example.com", "iv", "format", "vers")
if e != nil {
t.Errorf("Set key to a media playlist failed: %s", e)
}
}
// Create new media playlist
// Add segment to media playlist
// Set encryption key
func TestSetDefaultKeyForMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.SetDefaultKey("AES-128", "https://example.com", "iv", "", "")
if e != nil {
t.Errorf("Set default key to a media playlist failed: %s", e)
}
if p.ver != 3 {
t.Errorf("SetDefaultKey to a media playlist changed version unnecessarily")
}
// Test that using V5 features updates EXT-X-VERSION
e = p.SetDefaultKey("AES-128", "https://example.com", "iv", "format", "vers")
if e != nil {
t.Errorf("Set key to a media playlist failed: %s", e)
}
if p.ver != 5 {
t.Errorf("SetDefaultKey did not update version")
}
}
// Create new media playlist
// Add segment to media playlist
// Set map
func TestSetMapForMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 5.0, "")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
e = p.SetMap("https://example.com", 1000*1024, 1024*1024)
if e != nil {
t.Errorf("Set map to a media playlist failed: %s", e)
}
}
// Create new media playlist
// Add two segments to media playlist
// Encode structures to HLS
func TestEncodeMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 5.0, "")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
p.DurationAsInt(true)
//fmt.Println(p.Encode().String())
}
// Create new media playlist
// Add 10 segments to media playlist
// Test iterating over segments
func TestLoopSegmentsOfMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
p.DurationAsInt(true)
//fmt.Println(p.Encode().String())
}
// Create new media playlist with capacity 30
// Add 10 segments to media playlist
// Add encryption key
// Add another 10 segments to media playlist
// Add new encryption key
// Add another 10 segments to media playlist
// Iterate over segments
func TestEncryptionKeysInMediaPlaylist(t *testing.T) {
// Create new media playlist with capacity 30
p, e := NewMediaPlaylist(5, 15)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
// Add 10 segments to media playlist
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test0-%d.ts", i), 6.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
// Add encryption key
p.SetKey("AES-128", "https://example.com/", "0X00000000000000000000000000000000", "key-format1", "version x.x")
// Add 10 segments to media playlist
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test1-%d.ts", i), 6.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
// Add encryption key
p.SetKey("AES-128", "https://example.com/", "0X00000000000000000000000000000001", "key-format2", "version x.x")
// Add 10 segments to media playlist
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test2-%d.ts", i), 6.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
for i := 0; i < 3; i++ {
//fmt.Printf("Iteration %d:\n%s\n", i, p.Encode().String())
p.Remove()
}
}
// Create new media playlist
// Add 10 segments to media playlist
// Encode structure to HLS with integer target durations
func TestMediaPlaylistWithIntegerDurations(t *testing.T) {
p, e := NewMediaPlaylist(3, 10)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 9; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.6, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
p.DurationAsInt(false)
// fmt.Println(p.Encode().String())
}
// Create new media playlist
// Add 9 segments to media playlist
// 11 times encode structure to HLS with integer target durations
// Last playlist must be empty
func TestMediaPlaylistWithEmptyMedia(t *testing.T) {
p, e := NewMediaPlaylist(3, 10)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 1; i < 10; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.6, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
for i := 1; i < 11; i++ {
//fmt.Println(p.Encode().String())
p.Remove()
} // TODO add check for buffers equality
}
// Create new media playlist with winsize == capacity
func TestMediaPlaylistWinsize(t *testing.T) {
p, e := NewMediaPlaylist(6, 6)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 1; i < 10; i++ {
p.Slide(fmt.Sprintf("test%d.ts", i), 5.6, "")
//fmt.Println(p.Encode().String()) // TODO check playlist sizes and mediasequence values
}
}
// Create new media playlist as sliding playlist.
// Close it.
func TestClosedMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(1, 10)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 10; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Due oversize new segment #%d not assigned to a media playlist: %s\n", i, e)
}
}
p.Close()
}
// Create new master playlist without params
// Add media playlist
func TestNewMasterPlaylist(t *testing.T) {
m := NewMasterPlaylist()
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8", p, VariantParams{})
}
// Create new master playlist without params
// Add media playlist with Alternatives
func TestNewMasterPlaylistWithAlternatives(t *testing.T) {
m := NewMasterPlaylist()
audioUri := fmt.Sprintf("%s/rendition.m3u8", "800")
audioAlt := &Alternative{
GroupId: "audio",
URI: audioUri,
Type: "AUDIO",
Name: "main",
Default: true,
Autoselect: "YES",
Language: "english",
}
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8", p, VariantParams{Alternatives: []*Alternative{audioAlt}})
if m.ver != 4 {
t.Fatalf("Expected version 4, actual, %d", m.ver)
}
}
// Create new master playlist with params
// Add media playlist
func TestNewMasterPlaylistWithParams(t *testing.T) {
m := NewMasterPlaylist()
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
}
// Create new master playlist
// Add media playlist with existing query params in URI
// Append more query params and ensure it encodes correctly
func TestEncodeMasterPlaylistWithExistingQuery(t *testing.T) {
m := NewMasterPlaylist()
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8?k1=v1&k2=v2", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
m.Args = "k3=v3"
if !strings.Contains(m.String(), "chunklist1.m3u8?k1=v1&k2=v2&k3=v3\n") {
t.Errorf("Encode master with existing args failed")
}
}
// Create new master playlist
// Add media playlist
// Encode structures to HLS
func TestEncodeMasterPlaylist(t *testing.T) {
m := NewMasterPlaylist()
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
m.Append("chunklist2.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
}
// Create new master playlist with Name tag in EXT-X-STREAM-INF
func TestEncodeMasterPlaylistWithStreamInfName(t *testing.T) {
m := NewMasterPlaylist()
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
for i := 0; i < 5; i++ {
e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
if e != nil {
t.Errorf("Add segment #%d to a media playlist failed: %s", i, e)
}
}
m.Append("chunklist1.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 3000000, Resolution: "1152x960", Name: "HD 960p"})
if m.Variants[0].Name != "HD 960p" {
t.Fatalf("Create master with Name in EXT-X-STREAM-INF failed")
}
if !strings.Contains(m.String(), "NAME=\"HD 960p\"") {
t.Fatalf("Encode master with Name in EXT-X-STREAM-INF failed")
}
}
/******************************
* Code generation examples *
******************************/
// Create new media playlist
// Add two segments to media playlist
// Print it
func ExampleMediaPlaylist_String() {
p, _ := NewMediaPlaylist(1, 2)
p.Append("test01.ts", 5.0, "")
p.Append("test02.ts", 6.0, "")
fmt.Printf("%s\n", p)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:1
// #EXT-X-TARGETDURATION:6
// #EXTINF:5.000,
// test01.ts
}
// Create new master playlist
// Add media playlist
// Encode structures to HLS
func ExampleMasterPlaylist_String() {
m := NewMasterPlaylist()
p, _ := NewMediaPlaylist(3, 5)
for i := 0; i < 5; i++ {
p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
}
m.Append("chunklist1.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
m.Append("chunklist2.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"})
fmt.Printf("%s", m)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,RESOLUTION=576x480
// chunklist1.m3u8
// #EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,RESOLUTION=576x480
// chunklist2.m3u8
}