forked from zencoder/m3u8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
1070 lines (994 loc) · 32.6 KB
/
reader_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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Playlist parsing tests.
Copyright 2013-2019 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"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
)
func TestDecodeMasterPlaylist(t *testing.T) {
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if len(p.Variants) != 5 {
t.Error("Not all variants in master playlist parsed.")
}
// TODO check other values
// fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithMultipleCodecs(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-multiple-codecs.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if len(p.Variants) != 5 {
t.Error("Not all variants in master playlist parsed.")
}
for _, v := range p.Variants {
if v.Codecs != "avc1.42c015,mp4a.40.2" {
t.Error("Codec string is wrong")
}
}
// TODO check other values
// fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithAlternatives(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-alternatives.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if len(p.Variants) != 4 {
t.Fatal("not all variants in master playlist parsed")
}
// TODO check other values
for i, v := range p.Variants {
if i == 0 && len(v.Alternatives) != 3 {
t.Fatalf("not all alternatives from #EXT-X-MEDIA parsed (has %d but should be 3", len(v.Alternatives))
}
if i == 1 && len(v.Alternatives) != 3 {
t.Fatalf("not all alternatives from #EXT-X-MEDIA parsed (has %d but should be 3", len(v.Alternatives))
}
if i == 2 && len(v.Alternatives) != 3 {
t.Fatalf("not all alternatives from #EXT-X-MEDIA parsed (has %d but should be 3", len(v.Alternatives))
}
if i == 3 && len(v.Alternatives) > 0 {
t.Fatal("should not be alternatives for this variant")
}
}
// fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithClosedCaptionEqNone(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-closed-captions-eq-none.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
if len(p.Variants) != 3 {
t.Fatal("not all variants in master playlist parsed")
}
for _, v := range p.Variants {
if v.Captions != "NONE" {
t.Fatal("variant field for CLOSED-CAPTIONS should be equal to NONE but it equals", v.Captions)
}
}
}
// Decode a master playlist with Name tag in EXT-X-STREAM-INF
func TestDecodeMasterPlaylistWithStreamInfName(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-stream-inf-name.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
for _, variant := range p.Variants {
if variant.Name == "" {
t.Errorf("Empty name tag on variant URI: %s", variant.URI)
}
}
}
func TestDecodeMediaPlaylistByteRange(t *testing.T) {
f, _ := os.Open("sample-playlists/media-playlist-with-byterange.m3u8")
p, _ := NewMediaPlaylist(3, 3)
_ = p.DecodeFrom(bufio.NewReader(f), true)
expected := []*MediaSegment{
{URI: "video.ts", Duration: 10, Limit: 75232, SeqId: 0},
{URI: "video.ts", Duration: 10, Limit: 82112, Offset: 752321, SeqId: 1},
{URI: "video.ts", Duration: 10, Limit: 69864, SeqId: 2},
}
for i, seg := range p.Segments {
if !reflect.DeepEqual(*seg, *expected[i]) {
t.Errorf("exp: %+v\ngot: %+v", expected[i], seg)
}
}
}
// Decode a master playlist with i-frame-stream-inf
func TestDecodeMasterPlaylistWithIFrameStreamInf(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-i-frame-stream-inf.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
expected := map[int]*Variant{
86000: {URI: "low/iframe.m3u8", VariantParams: VariantParams{Bandwidth: 86000, ProgramId: 1, Codecs: "c1", Resolution: "1x1", Video: "1", Iframe: true}},
150000: {URI: "mid/iframe.m3u8", VariantParams: VariantParams{Bandwidth: 150000, ProgramId: 1, Codecs: "c2", Resolution: "2x2", Video: "2", Iframe: true}},
550000: {URI: "hi/iframe.m3u8", VariantParams: VariantParams{Bandwidth: 550000, ProgramId: 1, Codecs: "c2", Resolution: "2x2", Video: "2", Iframe: true}},
}
for _, variant := range p.Variants {
for k, expect := range expected {
if reflect.DeepEqual(variant, expect) {
delete(expected, k)
}
}
}
for _, expect := range expected {
t.Errorf("not found:%+v", expect)
}
}
func TestDecodeMasterPlaylistWithStreamInfAverageBandwidth(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-stream-inf-1.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
for _, variant := range p.Variants {
if variant.AverageBandwidth == 0 {
t.Errorf("Empty average bandwidth tag on variant URI: %s", variant.URI)
}
}
}
func TestDecodeMasterPlaylistWithStreamInfFrameRate(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-stream-inf-1.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
for _, variant := range p.Variants {
if variant.FrameRate == 0 {
t.Errorf("Empty frame rate tag on variant URI: %s", variant.URI)
}
}
}
func TestDecodeMasterPlaylistWithIndependentSegments(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-independent-segments.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
if !p.IndependentSegments() {
t.Error("Expected independent segments to be true")
}
}
func TestDecodeMasterWithHLSV7(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-hlsv7.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
var unexpected []*Variant
expected := map[string]VariantParams{
"sdr_720/prog_index.m3u8": {Bandwidth: 3971374, AverageBandwidth: 2778321, Codecs: "hvc1.2.4.L123.B0", Resolution: "1280x720", Captions: "NONE", VideoRange: "SDR", HDCPLevel: "NONE", FrameRate: 23.976},
"sdr_1080/prog_index.m3u8": {Bandwidth: 10022043, AverageBandwidth: 6759875, Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", Captions: "NONE", VideoRange: "SDR", HDCPLevel: "TYPE-0", FrameRate: 23.976},
"sdr_2160/prog_index.m3u8": {Bandwidth: 28058971, AverageBandwidth: 20985770, Codecs: "hvc1.2.4.L150.B0", Resolution: "3840x2160", Captions: "NONE", VideoRange: "SDR", HDCPLevel: "TYPE-1", FrameRate: 23.976},
"dolby_720/prog_index.m3u8": {Bandwidth: 5327059, AverageBandwidth: 3385450, Codecs: "dvh1.05.01", Resolution: "1280x720", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "NONE", FrameRate: 23.976},
"dolby_1080/prog_index.m3u8": {Bandwidth: 12876596, AverageBandwidth: 7999361, Codecs: "dvh1.05.03", Resolution: "1920x1080", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "TYPE-0", FrameRate: 23.976},
"dolby_2160/prog_index.m3u8": {Bandwidth: 30041698, AverageBandwidth: 24975091, Codecs: "dvh1.05.06", Resolution: "3840x2160", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "TYPE-1", FrameRate: 23.976},
"hdr10_720/prog_index.m3u8": {Bandwidth: 5280654, AverageBandwidth: 3320040, Codecs: "hvc1.2.4.L123.B0", Resolution: "1280x720", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "NONE", FrameRate: 23.976},
"hdr10_1080/prog_index.m3u8": {Bandwidth: 12886714, AverageBandwidth: 7964551, Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "TYPE-0", FrameRate: 23.976},
"hdr10_2160/prog_index.m3u8": {Bandwidth: 29983769, AverageBandwidth: 24833402, Codecs: "hvc1.2.4.L150.B0", Resolution: "3840x2160", Captions: "NONE", VideoRange: "PQ", HDCPLevel: "TYPE-1", FrameRate: 23.976},
"sdr_720/iframe_index.m3u8": {Bandwidth: 593626, AverageBandwidth: 248586, Codecs: "hvc1.2.4.L123.B0", Resolution: "1280x720", Iframe: true, VideoRange: "SDR", HDCPLevel: "NONE"},
"sdr_1080/iframe_index.m3u8": {Bandwidth: 956552, AverageBandwidth: 399790, Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", Iframe: true, VideoRange: "SDR", HDCPLevel: "TYPE-0"},
"sdr_2160/iframe_index.m3u8": {Bandwidth: 1941397, AverageBandwidth: 826971, Codecs: "hvc1.2.4.L150.B0", Resolution: "3840x2160", Iframe: true, VideoRange: "SDR", HDCPLevel: "TYPE-1"},
"dolby_720/iframe_index.m3u8": {Bandwidth: 573073, AverageBandwidth: 232253, Codecs: "dvh1.05.01", Resolution: "1280x720", Iframe: true, VideoRange: "PQ", HDCPLevel: "NONE"},
"dolby_1080/iframe_index.m3u8": {Bandwidth: 905037, AverageBandwidth: 365337, Codecs: "dvh1.05.03", Resolution: "1920x1080", Iframe: true, VideoRange: "PQ", HDCPLevel: "TYPE-0"},
"dolby_2160/iframe_index.m3u8": {Bandwidth: 1893236, AverageBandwidth: 739114, Codecs: "dvh1.05.06", Resolution: "3840x2160", Iframe: true, VideoRange: "PQ", HDCPLevel: "TYPE-1"},
"hdr10_720/iframe_index.m3u8": {Bandwidth: 572673, AverageBandwidth: 232511, Codecs: "hvc1.2.4.L123.B0", Resolution: "1280x720", Iframe: true, VideoRange: "PQ", HDCPLevel: "NONE"},
"hdr10_1080/iframe_index.m3u8": {Bandwidth: 905053, AverageBandwidth: 364552, Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", Iframe: true, VideoRange: "PQ", HDCPLevel: "TYPE-0"},
"hdr10_2160/iframe_index.m3u8": {Bandwidth: 1895477, AverageBandwidth: 739757, Codecs: "hvc1.2.4.L150.B0", Resolution: "3840x2160", Iframe: true, VideoRange: "PQ", HDCPLevel: "TYPE-1"},
}
for _, variant := range p.Variants {
var found bool
for uri, vp := range expected {
if variant == nil || variant.URI != uri {
continue
}
if reflect.DeepEqual(variant.VariantParams, vp) {
delete(expected, uri)
found = true
}
}
if !found {
unexpected = append(unexpected, variant)
}
}
for uri, expect := range expected {
t.Errorf("not found: uri=%q %+v", uri, expect)
}
for _, unexpect := range unexpected {
t.Errorf("found but not expecting:%+v", unexpect)
}
}
func TestDecodeMasterPlaylistWithChapters(t *testing.T) {
f, err := os.Open("sample-playlists/master-adv-example-hevc.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
var audio []*Alternative
for _, v := range p.Variants {
for _, a := range v.Alternatives {
if a.Type == "AUDIO" {
audio = append(audio, a)
}
}
}
if len(audio) != 3 {
t.Fatalf("expected 6 audio renditions got=%d", len(audio))
}
for _, a := range audio {
var expectedChannels string
switch a.GroupId {
case "a1":
expectedChannels = "2"
case "a2", "a3":
expectedChannels = "6"
default:
t.Fatalf("unexpected audio GROUP-ID=%q", a.GroupId)
}
if a.Channels != expectedChannels {
t.Fatalf("incorrect channels attriute expected=%q got=%q GROUP-ID=%q", expectedChannels, a.Channels, a.GroupId)
}
}
}
/****************************
* Begin Test MediaPlaylist *
****************************/
func TestDecodeMediaPlaylist(t *testing.T) {
f, err := os.Open("sample-playlists/wowza-vod-chunklist.m3u8")
if err != nil {
t.Fatal(err)
}
p, err := NewMediaPlaylist(5, 798)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
err = p.DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
//fmt.Printf("Playlist object: %+v\n", p)
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if p.TargetDuration != 12 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 12.0)", p.TargetDuration)
}
if !p.Closed {
t.Error("This is a closed (VOD) playlist but Close field = false")
}
titles := []string{"Title 1", "Title 2", ""}
for i, s := range p.Segments {
if i > len(titles)-1 {
break
}
if s.Title != titles[i] {
t.Errorf("Segment %v's title = %v (must = %q)", i, s.Title, titles[i])
}
}
if p.Count() != 522 {
t.Errorf("Excepted segments quantity: 522, got: %v", p.Count())
}
var seqId, idx uint
for seqId, idx = 1, 0; idx < p.Count(); seqId, idx = seqId+1, idx+1 {
if p.Segments[idx].SeqId != uint64(seqId) {
t.Errorf("Excepted SeqId for %vth segment: %v, got: %v", idx+1, seqId, p.Segments[idx].SeqId)
}
}
// TODO check other values…
//fmt.Println(p.Encode().String()), stream.Name}
}
func TestDecodeMediaPlaylistExtInfNonStrict2(t *testing.T) {
header := `#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
%s
`
tests := []struct {
strict bool
extInf string
wantError bool
wantSegment *MediaSegment
}{
// strict mode on
{true, "#EXTINF:10.000,", false, &MediaSegment{Duration: 10.0, Title: ""}},
{true, "#EXTINF:10.000,Title", false, &MediaSegment{Duration: 10.0, Title: "Title"}},
{true, "#EXTINF:10.000,Title,Track", false, &MediaSegment{Duration: 10.0, Title: "Title,Track"}},
{true, "#EXTINF:invalid,", true, nil},
{true, "#EXTINF:10.000", true, nil},
// strict mode off
{false, "#EXTINF:10.000,", false, &MediaSegment{Duration: 10.0, Title: ""}},
{false, "#EXTINF:10.000,Title", false, &MediaSegment{Duration: 10.0, Title: "Title"}},
{false, "#EXTINF:10.000,Title,Track", false, &MediaSegment{Duration: 10.0, Title: "Title,Track"}},
{false, "#EXTINF:invalid,", false, &MediaSegment{Duration: 0.0, Title: ""}},
{false, "#EXTINF:10.000", false, &MediaSegment{Duration: 10.0, Title: ""}},
}
for _, test := range tests {
p, err := NewMediaPlaylist(1, 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
reader := bytes.NewBufferString(fmt.Sprintf(header, test.extInf))
err = p.DecodeFrom(reader, test.strict)
if test.wantError {
if err == nil {
t.Errorf("expected error but have: %v", err)
}
continue
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(p.Segments[0], test.wantSegment) {
t.Errorf("\nhave: %+v\nwant: %+v", p.Segments[0], test.wantSegment)
}
}
}
func TestDecodeMediaPlaylistWithWidevine(t *testing.T) {
f, err := os.Open("sample-playlists/widevine-bitrate.m3u8")
if err != nil {
t.Fatal(err)
}
p, err := NewMediaPlaylist(5, 798)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
err = p.DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
//fmt.Printf("Playlist object: %+v\n", p)
// check parsed values
if p.ver != 2 {
t.Errorf("Version of parsed playlist = %d (must = 2)", p.ver)
}
if p.TargetDuration != 9 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 9.0)", p.TargetDuration)
}
// TODO check other values…
//fmt.Printf("%+v\n", p.Key)
//fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithAutodetection(t *testing.T) {
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
t.Fatal(err)
}
m, listType, err := DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
if listType != MASTER {
t.Error("Sample not recognized as master playlist.")
}
mp := m.(*MasterPlaylist)
// fmt.Printf(">%+v\n", mp)
// for _, v := range mp.Variants {
// fmt.Printf(">>%+v +v\n", v)
// }
//fmt.Println("Type below must be MasterPlaylist:")
CheckType(t, mp)
}
func TestDecodeMediaPlaylistWithAutodetection(t *testing.T) {
f, err := os.Open("sample-playlists/wowza-vod-chunklist.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as media playlist.")
}
// check parsed values
if pp.TargetDuration != 12 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 12.0)", pp.TargetDuration)
}
if !pp.Closed {
t.Error("This is a closed (VOD) playlist but Close field = false")
}
if pp.winsize != 0 {
t.Errorf("Media window size %v != 0", pp.winsize)
}
// TODO check other values…
// fmt.Println(pp.Encode().String())
}
// TestDecodeMediaPlaylistAutoDetectExtend tests a very large playlist auto
// extends to the appropriate size.
func TestDecodeMediaPlaylistAutoDetectExtend(t *testing.T) {
f, err := os.Open("sample-playlists/media-playlist-large.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as media playlist.")
}
var exp uint = 40001
if pp.Count() != exp {
t.Errorf("Media segment count %v != %v", pp.Count(), exp)
}
}
// Test for FullTimeParse of EXT-X-PROGRAM-DATE-TIME
// We testing ISO/IEC 8601:2004 where we can get time in UTC, UTC with Nanoseconds
// timeZone in formats '±00:00', '±0000', '±00'
// m3u8.FullTimeParse()
func TestFullTimeParse(t *testing.T) {
var timestamps = []struct {
name string
value string
}{
{"time_in_utc", "2006-01-02T15:04:05Z"},
{"time_in_utc_nano", "2006-01-02T15:04:05.123456789Z"},
{"time_with_positive_zone_and_colon", "2006-01-02T15:04:05+01:00"},
{"time_with_positive_zone_no_colon", "2006-01-02T15:04:05+0100"},
{"time_with_positive_zone_2digits", "2006-01-02T15:04:05+01"},
{"time_with_negative_zone_and_colon", "2006-01-02T15:04:05-01:00"},
{"time_with_negative_zone_no_colon", "2006-01-02T15:04:05-0100"},
{"time_with_negative_zone_2digits", "2006-01-02T15:04:05-01"},
}
var err error
for _, tstamp := range timestamps {
_, err = FullTimeParse(tstamp.value)
if err != nil {
t.Errorf("FullTimeParse Error at %s [%s]: %s", tstamp.name, tstamp.value, err)
}
}
}
// Test for StrictTimeParse of EXT-X-PROGRAM-DATE-TIME
// We testing Strict format of RFC3339 where we can get time in UTC, UTC with Nanoseconds
// timeZone in formats '±00:00', '±0000', '±00'
// m3u8.StrictTimeParse()
func TestStrictTimeParse(t *testing.T) {
var timestamps = []struct {
name string
value string
}{
{"time_in_utc", "2006-01-02T15:04:05Z"},
{"time_in_utc_nano", "2006-01-02T15:04:05.123456789Z"},
{"time_with_positive_zone_and_colon", "2006-01-02T15:04:05+01:00"},
{"time_with_negative_zone_and_colon", "2006-01-02T15:04:05-01:00"},
}
var err error
for _, tstamp := range timestamps {
_, err = StrictTimeParse(tstamp.value)
if err != nil {
t.Errorf("StrictTimeParse Error at %s [%s]: %s", tstamp.name, tstamp.value, err)
}
}
}
func TestMediaPlaylistWithOATCLSSCTE35Tag(t *testing.T) {
f, err := os.Open("sample-playlists/media-playlist-with-oatcls-scte35.m3u8")
if err != nil {
t.Fatal(err)
}
p, _, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
expect := map[int]*SCTE{
0: {Syntax: SCTE35_OATCLS, CueType: SCTE35Cue_Start, Cue: "/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA==", Time: 15},
1: {Syntax: SCTE35_OATCLS, CueType: SCTE35Cue_Mid, Cue: "/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA==", Time: 15, Elapsed: 8.844},
2: {Syntax: SCTE35_OATCLS, CueType: SCTE35Cue_End},
}
for i := 0; i < int(pp.Count()); i++ {
if !reflect.DeepEqual(pp.Segments[i].SCTE, expect[i]) {
t.Errorf("OATCLS SCTE35 segment %v (uri: %v)\ngot: %#v\nexp: %#v",
i, pp.Segments[i].URI, pp.Segments[i].SCTE, expect[i],
)
}
}
}
func TestMediaPlaylistWithCueSCTE35Tag(t *testing.T) {
f, err := ioutil.ReadFile("sample-playlists/media-playlist-with-cue-scte35.m3u8")
if err != nil {
t.Fatal(err)
}
p, _, err := DecodeFrom(bytes.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
expect := map[int]*SCTE{
2: {Syntax: SCTE35_CUE, CueType: SCTE35Cue_Start, Time: 18},
3: {Syntax: SCTE35_CUE, CueType: SCTE35Cue_Mid, Time: 6.0, Elapsed: 6.0},
4: {Syntax: SCTE35_CUE, CueType: SCTE35Cue_Mid, Time: 6.0, Elapsed: 12.0},
5: {Syntax: SCTE35_CUE, CueType: SCTE35Cue_End},
}
for i := 0; i < int(pp.Count()); i++ {
if !reflect.DeepEqual(pp.Segments[i].SCTE, expect[i]) {
t.Errorf("CUE SCTE35 segment %v (uri: %v)\ngot: %#v\nexp: %#v",
i, pp.Segments[i].URI, pp.Segments[i].SCTE, expect[i],
)
}
}
}
func TestDecodeMediaPlaylistWithDiscontinuitySeq(t *testing.T) {
f, err := os.Open("sample-playlists/media-playlist-with-discontinuity-seq.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as media playlist.")
}
if pp.DiscontinuitySeq == 0 {
t.Error("Empty discontinuity sequenece tag")
}
if pp.Count() != 4 {
t.Errorf("Excepted segments quantity: 4, got: %v", pp.Count())
}
if pp.SeqNo != 0 {
t.Errorf("Excepted SeqNo: 0, got: %v", pp.SeqNo)
}
var seqId, idx uint
for seqId, idx = 0, 0; idx < pp.Count(); seqId, idx = seqId+1, idx+1 {
if pp.Segments[idx].SeqId != uint64(seqId) {
t.Errorf("Excepted SeqId for %vth segment: %v, got: %v", idx+1, seqId, pp.Segments[idx].SeqId)
}
}
}
func TestDecodeMasterPlaylistWithCustomTags(t *testing.T) {
cases := []struct {
src string
customDecoders []CustomDecoder
expectedError error
expectedPlaylistTags []string
}{
{
src: "sample-playlists/master-playlist-with-custom-tags.m3u8",
customDecoders: nil,
expectedError: nil,
expectedPlaylistTags: nil,
},
{
src: "sample-playlists/master-playlist-with-custom-tags.m3u8",
customDecoders: []CustomDecoder{
&MockCustomTag{
name: "#CUSTOM-PLAYLIST-TAG:",
err: errors.New("Error decoding tag"),
segment: false,
encodedString: "#CUSTOM-PLAYLIST-TAG:42",
},
},
expectedError: errors.New("Error decoding tag"),
expectedPlaylistTags: nil,
},
{
src: "sample-playlists/master-playlist-with-custom-tags.m3u8",
customDecoders: []CustomDecoder{
&MockCustomTag{
name: "#CUSTOM-PLAYLIST-TAG:",
err: nil,
segment: false,
encodedString: "#CUSTOM-PLAYLIST-TAG:42",
},
},
expectedError: nil,
expectedPlaylistTags: []string{
"#CUSTOM-PLAYLIST-TAG:",
},
},
}
for _, testCase := range cases {
f, err := os.Open(testCase.src)
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeWith(bufio.NewReader(f), true, testCase.customDecoders)
if !reflect.DeepEqual(err, testCase.expectedError) {
t.Fatal(err)
}
if testCase.expectedError != nil {
// No need to make other assertions if we were expecting an error
continue
}
pp := p.(*MasterPlaylist)
CheckType(t, pp)
if listType != MASTER {
t.Error("Sample not recognized as master playlist.")
}
if len(pp.Custom) != len(testCase.expectedPlaylistTags) {
t.Errorf("Did not parse expected number of custom tags. Got: %d Expected: %d", len(pp.Custom), len(testCase.expectedPlaylistTags))
} else {
// we have the same count, lets confirm its the right tags
for _, expectedTag := range testCase.expectedPlaylistTags {
if _, ok := pp.Custom[expectedTag]; !ok {
t.Errorf("Did not parse custom tag %s", expectedTag)
}
}
}
}
}
func TestDecodeMediaPlaylistWithCustomTags(t *testing.T) {
cases := []struct {
src string
customDecoders []CustomDecoder
expectedError error
expectedPlaylistTags []string
expectedSegmentTags []*struct {
index int
names []string
}
}{
{
src: "sample-playlists/media-playlist-with-custom-tags.m3u8",
customDecoders: nil,
expectedError: nil,
expectedPlaylistTags: nil,
expectedSegmentTags: nil,
},
{
src: "sample-playlists/media-playlist-with-custom-tags.m3u8",
customDecoders: []CustomDecoder{
&MockCustomTag{
name: "#CUSTOM-PLAYLIST-TAG:",
err: errors.New("Error decoding tag"),
segment: false,
encodedString: "#CUSTOM-PLAYLIST-TAG:42",
},
},
expectedError: errors.New("Error decoding tag"),
expectedPlaylistTags: nil,
expectedSegmentTags: nil,
},
{
src: "sample-playlists/media-playlist-with-custom-tags.m3u8",
customDecoders: []CustomDecoder{
&MockCustomTag{
name: "#CUSTOM-PLAYLIST-TAG:",
err: nil,
segment: false,
encodedString: "#CUSTOM-PLAYLIST-TAG:42",
},
&MockCustomTag{
name: "#CUSTOM-SEGMENT-TAG:",
err: nil,
segment: true,
encodedString: "#CUSTOM-SEGMENT-TAG:NAME=\"Yoda\",JEDI=YES",
},
&MockCustomTag{
name: "#CUSTOM-SEGMENT-TAG-B",
err: nil,
segment: true,
encodedString: "#CUSTOM-SEGMENT-TAG-B",
},
},
expectedError: nil,
expectedPlaylistTags: []string{
"#CUSTOM-PLAYLIST-TAG:",
},
expectedSegmentTags: []*struct {
index int
names []string
}{
{1, []string{"#CUSTOM-SEGMENT-TAG:"}},
{2, []string{"#CUSTOM-SEGMENT-TAG:", "#CUSTOM-SEGMENT-TAG-B"}},
},
},
}
for _, testCase := range cases {
f, err := os.Open(testCase.src)
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeWith(bufio.NewReader(f), true, testCase.customDecoders)
if !reflect.DeepEqual(err, testCase.expectedError) {
t.Fatal(err)
}
if testCase.expectedError != nil {
// No need to make other assertions if we were expecting an error
continue
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as master playlist.")
}
if len(pp.Custom) != len(testCase.expectedPlaylistTags) {
t.Errorf("Did not parse expected number of custom tags. Got: %d Expected: %d", len(pp.Custom), len(testCase.expectedPlaylistTags))
} else {
// we have the same count, lets confirm its the right tags
for _, expectedTag := range testCase.expectedPlaylistTags {
if _, ok := pp.Custom[expectedTag]; !ok {
t.Errorf("Did not parse custom tag %s", expectedTag)
}
}
}
var expectedSegmentTag *struct {
index int
names []string
}
expectedIndex := 0
for i := 0; i < int(pp.Count()); i++ {
seg := pp.Segments[i]
if expectedIndex != len(testCase.expectedSegmentTags) {
expectedSegmentTag = testCase.expectedSegmentTags[expectedIndex]
} else {
// we are at the end of the expectedSegmentTags list, the rest of the segments
// should have no custom tags
expectedSegmentTag = nil
}
if expectedSegmentTag == nil || expectedSegmentTag.index != i {
if len(seg.Custom) != 0 {
t.Errorf("Did not parse expected number of custom tags on Segment %d. Got: %d Expected: %d", i, len(seg.Custom), 0)
}
continue
}
// We are now checking the segment corresponding to exepectedSegmentTag
// increase our expectedIndex for next iteration
expectedIndex++
if len(expectedSegmentTag.names) != len(seg.Custom) {
t.Errorf("Did not parse expected number of custom tags on Segment %d. Got: %d Expected: %d", i, len(seg.Custom), len(expectedSegmentTag.names))
} else {
// we have the same count, lets confirm its the right tags
for _, expectedTag := range expectedSegmentTag.names {
if _, ok := seg.Custom[expectedTag]; !ok {
t.Errorf("Did not parse customTag %s on Segment %d", expectedTag, i)
}
}
}
}
if expectedIndex != len(testCase.expectedSegmentTags) {
t.Errorf("Did not parse custom tags on all expected segments. Parsed Segments: %d Expected: %d", expectedIndex, len(testCase.expectedSegmentTags))
}
}
}
/***************************
* Code parsing examples *
***************************/
// Example of parsing a playlist with EXT-X-DISCONTINIUTY tag
// and output it with integer segment durations.
func ExampleMediaPlaylist_DurationAsInt() {
f, _ := os.Open("sample-playlists/media-playlist-with-discontinuity.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
pp.DurationAsInt(true)
fmt.Printf("%s", pp)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:0
// #EXT-X-TARGETDURATION:10
// #EXTINF:10,
// ad0.ts
// #EXTINF:8,
// ad1.ts
// #EXT-X-DISCONTINUITY
// #EXTINF:10,
// movieA.ts
// #EXTINF:10,
// movieB.ts
}
func TestMediaPlaylistWithSCTE35Tag(t *testing.T) {
cases := []struct {
playlistLocation string
expectedSCTEIndex int
expectedSCTECue string
expectedSCTEID string
expectedSCTETime float64
}{
{
"sample-playlists/media-playlist-with-scte35.m3u8",
2,
"/DAIAAAAAAAAAAAQAAZ/I0VniQAQAgBDVUVJQAAAAH+cAAAAAA==",
"123",
123.12,
},
{
"sample-playlists/media-playlist-with-scte35-1.m3u8",
1,
"/DAIAAAAAAAAAAAQAAZ/I0VniQAQAgBDVUVJQAA",
"",
0,
},
}
for _, c := range cases {
f, _ := os.Open(c.playlistLocation)
playlist, _, _ := DecodeFrom(bufio.NewReader(f), true)
mediaPlaylist := playlist.(*MediaPlaylist)
for index, item := range mediaPlaylist.Segments {
if item == nil {
break
}
if index != c.expectedSCTEIndex && item.SCTE != nil {
t.Error("Not expecting SCTE information on this segment")
} else if index == c.expectedSCTEIndex && item.SCTE == nil {
t.Error("Expecting SCTE information on this segment")
} else if index == c.expectedSCTEIndex && item.SCTE != nil {
if (*item.SCTE).Cue != c.expectedSCTECue {
t.Error("Expected ", c.expectedSCTECue, " got ", (*item.SCTE).Cue)
} else if (*item.SCTE).ID != c.expectedSCTEID {
t.Error("Expected ", c.expectedSCTEID, " got ", (*item.SCTE).ID)
} else if (*item.SCTE).Time != c.expectedSCTETime {
t.Error("Expected ", c.expectedSCTETime, " got ", (*item.SCTE).Time)
}
}
}
}
}
func TestDecodeMediaPlaylistWithProgramDateTime(t *testing.T) {
f, err := os.Open("sample-playlists/media-playlist-with-program-date-time.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as media playlist.")
}
// check parsed values
if pp.TargetDuration != 15 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 15.0)", pp.TargetDuration)
}
if !pp.Closed {
t.Error("VOD sample media playlist, closed should be true.")
}
if pp.SeqNo != 0 {
t.Error("Media sequence defined in sample playlist is 0")
}
segNames := []string{"20181231/0555e0c371ea801726b92512c331399d_00000000.ts",
"20181231/0555e0c371ea801726b92512c331399d_00000001.ts",
"20181231/0555e0c371ea801726b92512c331399d_00000002.ts",