forked from grafov/m3u8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
935 lines (869 loc) · 26.2 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/*
Package m3u8. Playlist generation tests.
Copyright 2013-2017 The Project Developers.
See the AUTHORS and LICENSE files at the top-level directory of this distribution
and at https://github.com/grafov/m3u8/
ॐ तारे तुत्तारे तुरे स्व
*/
package m3u8
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"sync"
"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")
}
}
// Tests the last method on media playlist
func TestLastSegmentMediaPlaylist(t *testing.T) {
p, _ := NewMediaPlaylist(5, 5)
if p.last() != 4 {
t.Errorf("last is %v, expected: 4", p.last())
}
for i := uint(0); i < 5; i++ {
_ = p.Append("uri.ts", 4, "")
if p.last() != i {
t.Errorf("last is: %v, expected: %v", p.last(), i)
}
}
}
// 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", 10.0, "title")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if p.Segments[0].URI != "test01.ts" {
t.Errorf("Expected: test01.ts, got: %v", p.Segments[0].URI)
}
if p.Segments[0].Duration != 10 {
t.Errorf("Expected: 10, got: %v", p.Segments[0].Duration)
}
if p.Segments[0].Title != "title" {
t.Errorf("Expected: title, got: %v", p.Segments[0].Title)
}
}
func TestAppendSegmentToMediaPlaylist(t *testing.T) {
p, _ := NewMediaPlaylist(2, 2)
e := p.AppendSegment(&MediaSegment{Duration: 10})
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if p.TargetDuration != 10 {
t.Errorf("Failed to increase TargetDuration, expected: 10, got: %v", p.TargetDuration)
}
e = p.AppendSegment(&MediaSegment{Duration: 10})
if e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
e = p.AppendSegment(&MediaSegment{Duration: 10})
if e != ErrPlaylistFull {
t.Errorf("Add 3rd expected full error, got: %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)
}
}
}
func TestSetSCTE35(t *testing.T) {
p, _ := NewMediaPlaylist(1, 2)
scte := &SCTE{Cue: "some cue"}
if err := p.SetSCTE35(scte); err == nil {
t.Error("SetSCTE35 expected empty playlist error")
}
_ = p.Append("test01.ts", 10.0, "title")
if err := p.SetSCTE35(scte); err != nil {
t.Errorf("SetSCTE35 did not expect error: %v", err)
}
if !reflect.DeepEqual(p.Segments[0].SCTE, scte) {
t.Errorf("SetSCTE35\nexp: %#v\ngot: %#v", scte, p.Segments[0].SCTE)
}
}
// Create new media playlist
// Add segment to media playlist
// Set SCTE
func TestSetSCTEForMediaPlaylist(t *testing.T) {
tests := []struct {
Cue string
ID string
Time float64
Expected string
}{
{"CueData1", "", 0, `#EXT-SCTE35:CUE="CueData1"` + "\n"},
{"CueData2", "ID2", 0, `#EXT-SCTE35:CUE="CueData2",ID="ID2"` + "\n"},
{"CueData3", "ID3", 3.141, `#EXT-SCTE35:CUE="CueData3",ID="ID3",TIME=3.141` + "\n"},
{"CueData4", "", 3.1, `#EXT-SCTE35:CUE="CueData4",TIME=3.1` + "\n"},
{"CueData5", "", 3.0, `#EXT-SCTE35:CUE="CueData5",TIME=3` + "\n"},
}
for _, test := range tests {
p, e := NewMediaPlaylist(1, 1)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
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.SetSCTE(test.Cue, test.ID, test.Time); e != nil {
t.Errorf("SetSCTE to a media playlist failed: %s", e)
}
if !strings.Contains(p.String(), test.Expected) {
t.Errorf("Test %+v did not contain: %q, playlist: %v", test, test.Expected, p.String())
}
}
}
// Create new media playlist
// Add segment to media playlist
// Set encryption key
func TestSetKeyForMediaPlaylist(t *testing.T) {
tests := []struct {
KeyFormat string
KeyFormatVersions string
ExpectVersion uint8
}{
{"", "", 3},
{"Format", "", 5},
{"", "Version", 5},
{"Format", "Version", 5},
}
for _, test := range tests {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
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.SetKey("AES-128", "https://example.com", "iv", test.KeyFormat, test.KeyFormatVersions); e != nil {
t.Errorf("Set key to a media playlist failed: %s", e)
}
if p.ver != test.ExpectVersion {
t.Errorf("Set key playlist version: %v, expected: %v", p.ver, test.ExpectVersion)
}
}
}
// Create new media playlist
// Add segment to media playlist
// Set encryption key
func TestSetDefaultKeyForMediaPlaylist(t *testing.T) {
tests := []struct {
KeyFormat string
KeyFormatVersions string
ExpectVersion uint8
}{
{"", "", 3},
{"Format", "", 5},
{"", "Version", 5},
{"Format", "Version", 5},
}
for _, test := range tests {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
if e := p.SetDefaultKey("AES-128", "https://example.com", "iv", test.KeyFormat, test.KeyFormatVersions); e != nil {
t.Errorf("Set key to a media playlist failed: %s", e)
}
if p.ver != test.ExpectVersion {
t.Errorf("Set key playlist version: %v, expected: %v", p.ver, test.ExpectVersion)
}
}
}
// Create new media playlist
// Set default map
func TestSetDefaultMapForMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.SetDefaultMap("https://example.com", 1000*1024, 1024*1024)
expected := `EXT-X-MAP:URI="https://example.com",BYTERANGE=1024000@1048576`
if !strings.Contains(p.String(), expected) {
t.Fatalf("Media playlist did not contain: %s\nMedia Playlist:\n%v", expected, p.String())
}
}
// Create new media playlist
// Add segment to media playlist
// Set map on segment
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)
}
expected := `EXT-X-MAP:URI="https://example.com",BYTERANGE=1024000@1048576
#EXTINF:5.000,
test01.ts`
if !strings.Contains(p.String(), expected) {
t.Fatalf("Media playlist did not contain: %s\nMedia Playlist:\n%v", expected, p.String())
}
}
// Create new media playlist
// Set default map
// Add segment to media playlist
// Set map on segment (should be ignored when encoding)
func TestEncodeMediaPlaylistWithDefaultMap(t *testing.T) {
p, e := NewMediaPlaylist(3, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.SetDefaultMap("https://example.com", 1000*1024, 1024*1024)
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://notencoded.com", 1000*1024, 1024*1024)
if e != nil {
t.Errorf("Set map to segment failed: %s", e)
}
encoded := p.String()
expected := `EXT-X-MAP:URI="https://example.com",BYTERANGE=1024000@1048576`
if !strings.Contains(encoded, expected) {
t.Fatalf("Media playlist did not contain: %s\nMedia Playlist:\n%v", expected, encoded)
}
ignored := `EXT-X-MAP:URI="https://notencoded.com"`
if strings.Contains(encoded, ignored) {
t.Fatalf("Media playlist contains non default map: %s\nMedia Playlist:\n%v", ignored, encoded)
}
}
// 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 5
// Add 5 segments and 5 unique keys
// Test correct keys set on correct segments
func TestEncryptionKeysInMediaPlaylist(t *testing.T) {
p, _ := NewMediaPlaylist(5, 5)
// Add 5 segments and set custom encryption key
for i := uint(0); i < 5; i++ {
uri := fmt.Sprintf("uri-%d", i)
expected := &Key{
Method: "AES-128",
URI: uri,
IV: fmt.Sprintf("%d", i),
Keyformat: "identity",
Keyformatversions: "1",
}
_ = p.Append(uri+".ts", 4, "")
_ = p.SetKey(expected.Method, expected.URI, expected.IV, expected.Keyformat, expected.Keyformatversions)
if p.Segments[i].Key == nil {
t.Fatalf("Key was not set on segment %v", i)
}
if *p.Segments[i].Key != *expected {
t.Errorf("Key %+v does not match expected %+v", p.Segments[i].Key, expected)
}
}
}
func TestEncryptionKeyMethodNoneInMediaPlaylist(t *testing.T) {
p, e := NewMediaPlaylist(5, 5)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.Append("segment-1.ts", 4, "")
p.SetKey("AES-128", "key-uri", "iv", "identity", "1")
p.Append("segment-2.ts", 4, "")
p.SetKey("NONE", "", "", "", "")
expected := `#EXT-X-KEY:METHOD=NONE
#EXTINF:4.000,
segment-2.ts`
if !strings.Contains(p.String(), expected) {
t.Errorf("Manifest %+v did not contain expected %+v", p, expected)
}
}
// 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 media playlist as sliding playlist.
func TestLargeMediaPlaylistWithParallel(t *testing.T) {
testCount := 10
expect, err := ioutil.ReadFile("sample-playlists/media-playlist-large.m3u8")
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < testCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
f, err := os.Open("sample-playlists/media-playlist-large.m3u8")
if err != nil {
t.Fatal(err)
}
p, err := NewMediaPlaylist(50000, 50000)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
if err = p.DecodeFrom(bufio.NewReader(f), true); err != nil {
t.Fatal(err)
}
actual := p.Encode().Bytes() // disregard output
if bytes.Compare(expect, actual) != 0 {
t.Fatal("not matched")
}
}()
wg.Wait()
}
}
func TestMediaVersion(t *testing.T) {
m, _ := NewMediaPlaylist(3, 3)
m.ver = 5
if m.Version() != m.ver {
t.Errorf("Expected version: %v, got: %v", m.ver, m.Version())
}
}
func TestMediaSetVersion(t *testing.T) {
m, _ := NewMediaPlaylist(3, 3)
m.ver = 3
m.SetVersion(5)
if m.ver != 5 {
t.Errorf("Expected version: %v, got: %v", 5, m.ver)
}
}
func TestMediaWinSize(t *testing.T) {
m, _ := NewMediaPlaylist(3, 3)
if m.WinSize() != m.winsize {
t.Errorf("Expected winsize: %v, got: %v", m.winsize, m.WinSize())
}
}
func TestMediaSetWinSize(t *testing.T) {
m, _ := NewMediaPlaylist(3, 5)
err := m.SetWinSize(5)
if err != nil {
t.Fatal(err)
}
if m.winsize != 5 {
t.Errorf("Expected winsize: %v, got: %v", 5, m.winsize)
}
// Check winsize cannot exceed capacity
err = m.SetWinSize(99999)
if err == nil {
t.Error("Expected error, received: ", err)
}
// Ensure winsize didn't change
if m.winsize != 5 {
t.Errorf("Expected winsize: %v, got: %v", 5, m.winsize)
}
}
// 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)
}
expected := `#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="main",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="english",URI="800/rendition.m3u8"`
if !strings.Contains(m.String(), expected) {
t.Fatalf("Master playlist did not contain: %s\nMaster Playlist:\n%v", expected, m.String())
}
}
// Create new master playlist supporting CLOSED-CAPTIONS=NONE
func TestNewMasterPlaylistWithClosedCaptionEqNone(t *testing.T) {
m := NewMasterPlaylist()
vp := &VariantParams{
ProgramId: 0,
Bandwidth: 8000,
Codecs: "avc1",
Resolution: "1280x720",
Audio: "audio0",
Captions: "NONE",
}
p, err := NewMediaPlaylist(1, 1)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
m.Append(fmt.Sprintf("eng_rendition_rendition.m3u8"), p, *vp)
expected := "CLOSED-CAPTIONS=NONE"
if !strings.Contains(m.String(), expected) {
t.Fatalf("Master playlist did not contain: %s\nMaster Playlist:\n%v", expected, m.String())
}
// quotes need to be include if not eq NONE
vp.Captions = "CC1"
m2 := NewMasterPlaylist()
m2.Append(fmt.Sprintf("eng_rendition_rendition.m3u8"), p, *vp)
expected = `CLOSED-CAPTIONS="CC1"`
if !strings.Contains(m2.String(), expected) {
t.Fatalf("Master playlist did not contain: %s\nMaster Playlist:\n%v", expected, m2.String())
}
}
// 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`) {
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")
}
}
func TestMasterVersion(t *testing.T) {
m := NewMasterPlaylist()
m.ver = 5
if m.Version() != m.ver {
t.Errorf("Expected version: %v, got: %v", m.ver, m.Version())
}
}
func TestMasterSetVersion(t *testing.T) {
m := NewMasterPlaylist()
m.ver = 3
m.SetVersion(5)
if m.ver != 5 {
t.Errorf("Expected version: %v, got: %v", 5, m.ver)
}
}
/******************************
* 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:0
// #EXT-X-TARGETDURATION:6
// #EXTINF:5.000,
// test01.ts
}
// Create new media playlist
// Add two segments to media playlist
// Print it
func ExampleMediaPlaylist_String_Winsize0() {
p, _ := NewMediaPlaylist(0, 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:0
// #EXT-X-TARGETDURATION:6
// #EXTINF:5.000,
// test01.ts
// #EXTINF:6.000,
// test02.ts
}
// Create new media playlist
// Add two segments to media playlist
// Print it
func ExampleMediaPlaylist_String_Winsize0_VOD() {
p, _ := NewMediaPlaylist(0, 2)
p.Append("test01.ts", 5.0, "")
p.Append("test02.ts", 6.0, "")
p.Close()
fmt.Printf("%s\n", p)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:0
// #EXT-X-TARGETDURATION:6
// #EXTINF:5.000,
// test01.ts
// #EXTINF:6.000,
// test02.ts
// #EXT-X-ENDLIST
}
// 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, AverageBandwidth: 1500000, Resolution: "576x480", FrameRate: 25.000})
m.Append("chunklist2.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, AverageBandwidth: 1500000, Resolution: "576x480", FrameRate: 25.000})
fmt.Printf("%s", m)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1500000,RESOLUTION=576x480,FRAME-RATE=25.000
// chunklist1.m3u8
// #EXT-X-STREAM-INF:PROGRAM-ID=123,BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1500000,RESOLUTION=576x480,FRAME-RATE=25.000
// chunklist2.m3u8
}
func ExampleMediaPlaylist_Segments_scte35_oatcls() {
f, _ := os.Open("sample-playlists/media-playlist-with-oatcls-scte35.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
fmt.Print(pp)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:0
// #EXT-X-TARGETDURATION:10
// #EXT-OATCLS-SCTE35:/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA==
// #EXT-X-CUE-OUT:15
// #EXTINF:8.844,
// media0.ts
// #EXT-X-CUE-OUT-CONT:ElapsedTime=8.844,Duration=15,SCTE35=/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA==
// #EXTINF:6.156,
// media1.ts
// #EXT-X-CUE-IN
// #EXTINF:3.844,
// media2.ts
}
func ExampleMediaPlaylist_Segments_scte35_67_2014() {
f, _ := os.Open("sample-playlists/media-playlist-with-scte35.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
fmt.Print(pp)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:0
// #EXT-X-TARGETDURATION:10
// #EXTINF:10.000,
// media0.ts
// #EXTINF:10.000,
// media1.ts
// #EXT-SCTE35:CUE="/DAIAAAAAAAAAAAQAAZ/I0VniQAQAgBDVUVJQAAAAH+cAAAAAA==",ID="123",TIME=123.12
// #EXTINF:10.000,
// media2.ts
}
/****************
* Benchmarks *
****************/
func BenchmarkEncodeMasterPlaylist(b *testing.B) {
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
b.Fatal(err)
}
p := NewMasterPlaylist()
if err := p.DecodeFrom(bufio.NewReader(f), true); err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
p.ResetCache()
_ = p.Encode() // disregard output
}
}
func BenchmarkEncodeMediaPlaylist(b *testing.B) {
f, err := os.Open("sample-playlists/media-playlist-large.m3u8")
if err != nil {
b.Fatal(err)
}
p, err := NewMediaPlaylist(50000, 50000)
if err != nil {
b.Fatalf("Create media playlist failed: %s", err)
}
if err = p.DecodeFrom(bufio.NewReader(f), true); err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
p.ResetCache()
_ = p.Encode() // disregard output
}
}