forked from kuu/hls-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.ts
1041 lines (1005 loc) · 37.5 KB
/
parse.ts
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
import * as utils from './utils';
import {
AllowedCpc,
ExtInfo,
Rendition,
Resolution,
TagParam,
UserAttribute,
Variant,
SessionData,
Key,
MediaInitializationSection,
Byterange,
DateRange,
SpliceInfo,
MasterPlaylist,
MediaPlaylist,
Segment,
PartialSegment,
PrefetchSegment,
RenditionReport
} from './types';
function unquote(str: string | undefined) {
return utils.trim(str, '"');
}
type TagCategory = 'Basic' | 'Segment' | 'MasterPlaylist' | 'MediaPlaylist' | 'MediaorMasterPlaylist' | 'Unknown';
function getTagCategory(tagName: string): TagCategory {
switch (tagName) {
case 'EXTM3U':
case 'EXT-X-VERSION':
return 'Basic';
case 'EXTINF':
case 'EXT-X-BYTERANGE':
case 'EXT-X-DISCONTINUITY':
case 'EXT-X-PREFETCH-DISCONTINUITY':
case 'EXT-X-KEY':
case 'EXT-X-MAP':
case 'EXT-X-PROGRAM-DATE-TIME':
case 'EXT-X-DATERANGE':
case 'EXT-X-CUE-OUT':
case 'EXT-X-CUE-IN':
case 'EXT-X-CUE-OUT-CONT':
case 'EXT-X-CUE':
case 'EXT-OATCLS-SCTE35':
case 'EXT-X-ASSET':
case 'EXT-X-SCTE35':
case 'EXT-X-PART':
case 'EXT-X-PRELOAD-HINT':
case 'EXT-X-GAP':
return 'Segment';
case 'EXT-X-TARGETDURATION':
case 'EXT-X-MEDIA-SEQUENCE':
case 'EXT-X-DISCONTINUITY-SEQUENCE':
case 'EXT-X-ENDLIST':
case 'EXT-X-PLAYLIST-TYPE':
case 'EXT-X-I-FRAMES-ONLY':
case 'EXT-X-SERVER-CONTROL':
case 'EXT-X-PART-INF':
case 'EXT-X-PREFETCH':
case 'EXT-X-RENDITION-REPORT':
case 'EXT-X-SKIP':
return 'MediaPlaylist';
case 'EXT-X-MEDIA':
case 'EXT-X-STREAM-INF':
case 'EXT-X-I-FRAME-STREAM-INF':
case 'EXT-X-SESSION-DATA':
case 'EXT-X-SESSION-KEY':
return 'MasterPlaylist';
case 'EXT-X-INDEPENDENT-SEGMENTS':
case 'EXT-X-START':
return 'MediaorMasterPlaylist';
default:
return 'Unknown';
}
}
function parseEXTINF(param: string): ExtInfo {
const pair = utils.splitAt(param, ',') as [string, string];
return {duration: utils.toNumber(pair[0]), title: decodeURIComponent(escape(pair[1]))};
}
function parseBYTERANGE(param: string): Byterange {
const pair = utils.splitAt(param, '@');
return {length: utils.toNumber(pair[0]), offset: pair[1] ? utils.toNumber(pair[1]) : -1};
}
function parseResolution(str: string): Resolution {
const pair = utils.splitAt(str, 'x') as [string, string];
return {width: utils.toNumber(pair[0]), height: utils.toNumber(pair[1])};
}
function parseAllowedCpc(str: string): AllowedCpc[] {
const message = 'ALLOWED-CPC: Each entry must consit of KEYFORMAT and Content Protection Configuration';
const list = str.split(',');
if (list.length === 0) {
utils.INVALIDPLAYLIST(message);
}
const allowedCpcList: AllowedCpc[] = [];
for (const item of list) {
const [format, cpcText] = utils.splitAt(item, ':');
if (!format || !cpcText) {
utils.INVALIDPLAYLIST(message);
continue;
}
allowedCpcList.push({format, cpcList: cpcText.split('/')});
}
return allowedCpcList;
}
function parseIV(str: string): Uint8Array {
const iv = utils.hexToByteSequence(str);
if (iv.length !== 16) {
utils.INVALIDPLAYLIST('IV must be a 128-bit unsigned integer');
}
return iv;
}
function parseUserAttribute(str: string): UserAttribute {
if (str.startsWith('"')) {
return unquote(str)!;
}
if (str.startsWith('0x') || str.startsWith('0X')) {
return utils.hexToByteSequence(str);
}
return utils.toNumber(str);
}
function setCompatibleVersionOfKey(params: Record<string, any>, attributes: Record<string, any>) {
if (attributes['IV'] && params.compatibleVersion < 2) {
params.compatibleVersion = 2;
}
if ((attributes['KEYFORMAT'] || attributes['KEYFORMATVERSIONS']) && params.compatibleVersion < 5) {
params.compatibleVersion = 5;
}
}
function parseAttributeList(param): Record<string, any> {
const attributes = {};
for (const item of utils.splitByCommaWithPreservingQuotes(param)) {
const [key, value] = utils.splitAt(item, '=');
const val = unquote(value)!;
switch (key) {
case 'URI':
attributes[key] = val;
break;
case 'START-DATE':
case 'END-DATE':
attributes[key] = new Date(val);
break;
case 'IV':
attributes[key] = parseIV(val);
break;
case 'BYTERANGE':
attributes[key] = parseBYTERANGE(val);
break;
case 'RESOLUTION':
attributes[key] = parseResolution(val);
break;
case 'ALLOWED-CPC':
attributes[key] = parseAllowedCpc(val);
break;
case 'END-ON-NEXT':
case 'DEFAULT':
case 'AUTOSELECT':
case 'FORCED':
case 'PRECISE':
case 'CAN-BLOCK-RELOAD':
case 'INDEPENDENT':
case 'GAP':
attributes[key] = val === 'YES';
break;
case 'DURATION':
case 'PLANNED-DURATION':
case 'BANDWIDTH':
case 'AVERAGE-BANDWIDTH':
case 'FRAME-RATE':
case 'TIME-OFFSET':
case 'CAN-SKIP-UNTIL':
case 'HOLD-BACK':
case 'PART-HOLD-BACK':
case 'PART-TARGET':
case 'BYTERANGE-START':
case 'BYTERANGE-LENGTH':
case 'LAST-MSN':
case 'LAST-PART':
case 'SKIPPED-SEGMENTS':
case 'SCORE':
case 'PROGRAM-ID':
attributes[key] = utils.toNumber(val);
break;
default:
if (key.startsWith('SCTE35-')) {
attributes[key] = utils.hexToByteSequence(val);
} else if (key.startsWith('X-')) {
attributes[key] = parseUserAttribute(value!);
} else {
if (key === 'VIDEO-RANGE' && val !== 'SDR' && val !== 'HLG' && val !== 'PQ') {
utils.INVALIDPLAYLIST(`VIDEO-RANGE: unknown value "${val}"`);
}
attributes[key] = val;
}
}
}
return attributes;
}
function parseTagParam(name: string, param): TagParam {
switch (name) {
case 'EXTM3U':
case 'EXT-X-DISCONTINUITY':
case 'EXT-X-ENDLIST':
case 'EXT-X-I-FRAMES-ONLY':
case 'EXT-X-INDEPENDENT-SEGMENTS':
case 'EXT-X-CUE-IN':
case 'EXT-X-GAP':
return [null, null];
case 'EXT-X-VERSION':
case 'EXT-X-TARGETDURATION':
case 'EXT-X-MEDIA-SEQUENCE':
case 'EXT-X-DISCONTINUITY-SEQUENCE':
return [utils.toNumber(param), null];
case 'EXT-X-CUE-OUT':
// For backwards compatibility: attributes list is optional,
// if only a number is found, use it as the duration
if (!Number.isNaN(Number(param))) {
return [utils.toNumber(param), null];
}
// If attributes are found, parse them out (i.e. DURATION)
return [null, parseAttributeList(param)];
case 'EXT-X-KEY':
case 'EXT-X-MAP':
case 'EXT-X-DATERANGE':
case 'EXT-X-MEDIA':
case 'EXT-X-STREAM-INF':
case 'EXT-X-I-FRAME-STREAM-INF':
case 'EXT-X-SESSION-DATA':
case 'EXT-X-SESSION-KEY':
case 'EXT-X-START':
case 'EXT-X-SERVER-CONTROL':
case 'EXT-X-PART-INF':
case 'EXT-X-PART':
case 'EXT-X-PRELOAD-HINT':
case 'EXT-X-RENDITION-REPORT':
case 'EXT-X-SKIP':
return [null, parseAttributeList(param)];
case 'EXTINF':
return [parseEXTINF(param), null];
case 'EXT-X-BYTERANGE':
return [parseBYTERANGE(param), null];
case 'EXT-X-PROGRAM-DATE-TIME':
return [new Date(param), null];
case 'EXT-X-PLAYLIST-TYPE':
return [param, null]; // <EVENT|VOD>
default:
return [param, null]; // Unknown tag
}
}
function MIXEDTAGS() {
utils.INVALIDPLAYLIST(`The file contains both media and master playlist tags.`);
}
function splitTag(line: string): [string, string | null] {
const index = line.indexOf(':');
if (index === -1) {
return [line.slice(1).trim(), null];
}
return [line.slice(1, index).trim(), line.slice(index + 1).trim()];
}
function parseRendition({attributes}: Tag): Rendition {
const rendition = new Rendition({
type: attributes['TYPE'],
uri: attributes['URI'],
groupId: attributes['GROUP-ID'],
language: attributes['LANGUAGE'],
assocLanguage: attributes['ASSOC-LANGUAGE'],
name: attributes['NAME'],
isDefault: attributes['DEFAULT'],
autoselect: attributes['AUTOSELECT'],
forced: attributes['FORCED'],
instreamId: attributes['INSTREAM-ID'],
characteristics: attributes['CHARACTERISTICS'],
channels: attributes['CHANNELS']
});
return rendition;
}
function checkRedundantRendition(renditions, rendition): string {
let defaultFound = false;
for (const item of renditions) {
if (item.name === rendition.name) {
return 'All EXT-X-MEDIA tags in the same Group MUST have different NAME attributes.';
}
if (item.isDefault) {
defaultFound = true;
}
}
if (defaultFound && rendition.isDefault) {
return 'EXT-X-MEDIA A Group MUST NOT have more than one member with a DEFAULT attribute of YES.';
}
return '';
}
function addRendition(variant, line, type) {
const rendition = parseRendition(line);
const renditions = variant[utils.camelify(type)];
const errorMessage = checkRedundantRendition(renditions, rendition);
if (errorMessage) {
utils.INVALIDPLAYLIST(errorMessage);
}
renditions.push(rendition);
if (rendition.isDefault) {
variant.currentRenditions[utils.camelify(type)] = renditions.length - 1;
}
}
function matchTypes(attrs, variant, params) {
for (const type of ['AUDIO', 'VIDEO', 'SUBTITLES', 'CLOSED-CAPTIONS']) {
if (type === 'CLOSED-CAPTIONS' && attrs[type] === 'NONE') {
params.isClosedCaptionsNone = true;
variant.closedCaptions = [];
} else if (attrs[type] && !variant[utils.camelify(type)].some(item => item.groupId === attrs[type])) {
utils.INVALIDPLAYLIST(`${type} attribute MUST match the value of the GROUP-ID attribute of an EXT-X-MEDIA tag whose TYPE attribute is ${type}.`);
}
}
}
function parseVariant(lines, variantAttrs, uri: string, iFrameOnly: boolean, params: Record<string, any>): Variant {
const variant = new Variant({
uri,
bandwidth: variantAttrs['BANDWIDTH'],
averageBandwidth: variantAttrs['AVERAGE-BANDWIDTH'],
score: variantAttrs['SCORE'],
codecs: variantAttrs['CODECS'],
resolution: variantAttrs['RESOLUTION'],
frameRate: variantAttrs['FRAME-RATE'],
hdcpLevel: variantAttrs['HDCP-LEVEL'],
allowedCpc: variantAttrs['ALLOWED-CPC'],
videoRange: variantAttrs['VIDEO-RANGE'],
stableVariantId: variantAttrs['STABLE-VARIANT-ID'],
programId: variantAttrs['PROGRAM-ID']
});
for (const line of lines) {
if (line.name === 'EXT-X-MEDIA') {
const renditionAttrs = line.attributes;
const renditionType = renditionAttrs['TYPE'];
if (!renditionType || !renditionAttrs['GROUP-ID']) {
utils.INVALIDPLAYLIST('EXT-X-MEDIA TYPE attribute is REQUIRED.');
}
if (variantAttrs[renditionType] === renditionAttrs['GROUP-ID']) {
addRendition(variant, line, renditionType);
if (renditionType === 'CLOSED-CAPTIONS') {
for (const {instreamId} of variant.closedCaptions) {
if (instreamId && instreamId.startsWith('SERVICE') && params.compatibleVersion < 7) {
params.compatibleVersion = 7;
break;
}
}
}
}
}
}
matchTypes(variantAttrs, variant, params);
variant.isIFrameOnly = iFrameOnly;
return variant;
}
function sameKey(key1: Key, key2: Key): boolean {
if (key1.method !== key2.method) {
return false;
}
if (key1.uri !== key2.uri) {
return false;
}
if (key1.iv) {
if (!key2.iv) {
return false;
}
if (key1.iv.byteLength !== key2.iv.byteLength) {
return false;
}
for (let i = 0; i < key1.iv.byteLength; i++) {
if (key1.iv[i] !== key2.iv[i]) {
return false;
}
}
} else if (key2.iv) {
return false;
}
if (key1.format !== key2.format) {
return false;
}
if (key1.formatVersion !== key2.formatVersion) {
return false;
}
return true;
}
function parseMasterPlaylist(lines: Line[], params: Record<string, any>): MasterPlaylist {
const playlist = new MasterPlaylist();
let variantIsScored = false;
for (const [index, {name, value, attributes}] of (lines as Tag[]).entries()) {
if (name === 'EXT-X-VERSION') {
playlist.version = value;
} else if (name === 'EXT-X-STREAM-INF') {
const uri = lines[index + 1];
if (typeof uri !== 'string' || uri.startsWith('#EXT')) {
utils.INVALIDPLAYLIST('EXT-X-STREAM-INF must be followed by a URI line');
}
const variant = parseVariant(lines, attributes, uri as string, false, params);
if (variant) {
if (typeof variant.score === 'number') {
variantIsScored = true;
if (variant.score < 0) {
utils.INVALIDPLAYLIST('SCORE attribute on EXT-X-STREAM-INF must be positive decimal-floating-point number.');
}
}
playlist.variants.push(variant);
}
} else if (name === 'EXT-X-I-FRAME-STREAM-INF') {
const variant = parseVariant(lines, attributes, attributes.URI, true, params);
if (variant) {
playlist.variants.push(variant);
}
} else if (name === 'EXT-X-SESSION-DATA') {
const sessionData = new SessionData({
id: attributes['DATA-ID'],
value: attributes['VALUE'],
uri: attributes['URI'],
language: attributes['LANGUAGE']
});
if (playlist.sessionDataList.some(item => item.id === sessionData.id && item.language === sessionData.language)) {
utils.INVALIDPLAYLIST('A Playlist MUST NOT contain more than one EXT-X-SESSION-DATA tag with the same DATA-ID attribute and the same LANGUAGE attribute.');
}
playlist.sessionDataList.push(sessionData);
} else if (name === 'EXT-X-SESSION-KEY') {
if (attributes['METHOD'] === 'NONE') {
utils.INVALIDPLAYLIST('EXT-X-SESSION-KEY: The value of the METHOD attribute MUST NOT be NONE');
}
const sessionKey = new Key({
method: attributes['METHOD'],
uri: attributes['URI'],
iv: attributes['IV'],
format: attributes['KEYFORMAT'],
formatVersion: attributes['KEYFORMATVERSIONS']
});
if (playlist.sessionKeyList.some(item => sameKey(item, sessionKey))) {
utils.INVALIDPLAYLIST('A Master Playlist MUST NOT contain more than one EXT-X-SESSION-KEY tag with the same METHOD, URI, IV, KEYFORMAT, and KEYFORMATVERSIONS attribute values.');
}
setCompatibleVersionOfKey(params, attributes);
playlist.sessionKeyList.push(sessionKey);
} else if (name === 'EXT-X-INDEPENDENT-SEGMENTS') {
if (playlist.independentSegments) {
utils.INVALIDPLAYLIST('EXT-X-INDEPENDENT-SEGMENTS tag MUST NOT appear more than once in a Playlist');
}
playlist.independentSegments = true;
} else if (name === 'EXT-X-START') {
if (playlist.start) {
utils.INVALIDPLAYLIST('EXT-X-START tag MUST NOT appear more than once in a Playlist');
}
if (typeof attributes['TIME-OFFSET'] !== 'number') {
utils.INVALIDPLAYLIST('EXT-X-START: TIME-OFFSET attribute is REQUIRED');
}
playlist.start = {offset: attributes['TIME-OFFSET'], precise: attributes['PRECISE'] || false};
}
}
if (variantIsScored) {
for (const variant of playlist.variants) {
if (typeof variant.score !== 'number') {
utils.INVALIDPLAYLIST('If any Variant Stream contains the SCORE attribute, then all Variant Streams in the Master Playlist SHOULD have a SCORE attribute');
}
}
}
if (params.isClosedCaptionsNone) {
for (const variant of playlist.variants) {
if (variant.closedCaptions.length > 0) {
utils.INVALIDPLAYLIST('If there is a variant with CLOSED-CAPTIONS attribute of NONE, all EXT-X-STREAM-INF tags MUST have this attribute with a value of NONE');
}
}
}
return playlist;
}
function parseSegment(lines: Line[], uri: string, start: number, end: number, mediaSequenceNumber: number, discontinuitySequence: number, params: Record<string, any>): Segment {
const segment = new Segment({uri, mediaSequenceNumber, discontinuitySequence});
let mapHint = false;
let partHint = false;
for (let i = start; i <= end; i++) {
const {name, value, attributes} = lines[i] as Tag;
if (name === 'EXTINF') {
if (!Number.isInteger(value.duration) && params.compatibleVersion < 3) {
params.compatibleVersion = 3;
}
if (Math.round(value.duration) > params.targetDuration) {
utils.INVALIDPLAYLIST('EXTINF duration, when rounded to the nearest integer, MUST be less than or equal to the target duration');
}
segment.duration = value.duration;
segment.title = value.title;
} else if (name === 'EXT-X-BYTERANGE') {
if (params.compatibleVersion < 4) {
params.compatibleVersion = 4;
}
segment.byterange = value;
} else if (name === 'EXT-X-DISCONTINUITY') {
if (segment.parts.length > 0) {
utils.INVALIDPLAYLIST('EXT-X-DISCONTINUITY must appear before the first EXT-X-PART tag of the Parent Segment.');
}
segment.discontinuity = true;
} else if (name === 'EXT-X-GAP') {
if (params.compatibleVersion < 8) {
params.compatibleVersion = 8;
}
segment.gap = true;
} else if (name === 'EXT-X-KEY') {
if (segment.parts.length > 0) {
utils.INVALIDPLAYLIST('EXT-X-KEY must appear before the first EXT-X-PART tag of the Parent Segment.');
}
setCompatibleVersionOfKey(params, attributes);
segment.key = new Key({
method: attributes['METHOD'],
uri: attributes['URI'],
iv: attributes['IV'],
format: attributes['KEYFORMAT'],
formatVersion: attributes['KEYFORMATVERSIONS']
});
} else if (name === 'EXT-X-MAP') {
if (segment.parts.length > 0) {
utils.INVALIDPLAYLIST('EXT-X-MAP must appear before the first EXT-X-PART tag of the Parent Segment.');
}
if (params.compatibleVersion < 5) {
params.compatibleVersion = 5;
}
params.hasMap = true;
segment.map = new MediaInitializationSection({
uri: attributes['URI'],
byterange: attributes['BYTERANGE']
});
} else if (name === 'EXT-X-PROGRAM-DATE-TIME') {
segment.programDateTime = value;
} else if (name === 'EXT-X-DATERANGE') {
const attrs: Record<string, any> = {};
for (const key of Object.keys(attributes)) {
if (key.startsWith('SCTE35-') || key.startsWith('X-')) {
attrs[key] = attributes[key];
}
}
segment.dateRange = new DateRange({
id: attributes['ID'],
classId: attributes['CLASS'],
start: attributes['START-DATE'],
end: attributes['END-DATE'],
duration: attributes['DURATION'],
plannedDuration: attributes['PLANNED-DURATION'],
endOnNext: attributes['END-ON-NEXT'],
attributes: attrs
});
} else if (name === 'EXT-X-CUE-OUT') {
segment.markers.push(new SpliceInfo({
type: 'OUT',
duration: (attributes && attributes.DURATION) || value
}));
} else if (name === 'EXT-X-CUE-IN') {
segment.markers.push(new SpliceInfo({
type: 'IN'
}));
} else if (
name === 'EXT-X-CUE-OUT-CONT' ||
name === 'EXT-X-CUE' ||
name === 'EXT-OATCLS-SCTE35' ||
name === 'EXT-X-ASSET' ||
name === 'EXT-X-SCTE35'
) {
segment.markers.push(new SpliceInfo({
type: 'RAW',
tagName: name,
value
}));
} else if (name === 'EXT-X-PRELOAD-HINT' && !attributes['TYPE']) {
utils.INVALIDPLAYLIST('EXT-X-PRELOAD-HINT: TYPE attribute is mandatory');
} else if (name === 'EXT-X-PRELOAD-HINT' && attributes['TYPE'] === 'PART' && partHint) {
utils.INVALIDPLAYLIST('Servers should not add more than one EXT-X-PRELOAD-HINT tag with the same TYPE attribute to a Playlist.');
} else if ((name === 'EXT-X-PART' || name === 'EXT-X-PRELOAD-HINT') && !attributes['URI']) {
utils.INVALIDPLAYLIST('EXT-X-PART / EXT-X-PRELOAD-HINT: URI attribute is mandatory');
} else if (name === 'EXT-X-PRELOAD-HINT' && attributes['TYPE'] === 'MAP') {
if (mapHint) {
utils.INVALIDPLAYLIST('Servers should not add more than one EXT-X-PRELOAD-HINT tag with the same TYPE attribute to a Playlist.');
}
mapHint = true;
params.hasMap = true;
segment.map = new MediaInitializationSection({
hint: true,
uri: attributes['URI'],
byterange: {length: attributes['BYTERANGE-LENGTH'], offset: attributes['BYTERANGE-START'] || 0}
});
} else if (name === 'EXT-X-PART' || (name === 'EXT-X-PRELOAD-HINT' && attributes['TYPE'] === 'PART')) {
if (name === 'EXT-X-PART' && !attributes['DURATION']) {
utils.INVALIDPLAYLIST('EXT-X-PART: DURATION attribute is mandatory');
}
if (name === 'EXT-X-PRELOAD-HINT') {
partHint = true;
}
const partialSegment = new PartialSegment({
hint: (name === 'EXT-X-PRELOAD-HINT'),
uri: attributes['URI'],
byterange: (name === 'EXT-X-PART' ? attributes['BYTERANGE'] : {length: attributes['BYTERANGE-LENGTH'], offset: attributes['BYTERANGE-START'] || 0}),
duration: attributes['DURATION'],
independent: attributes['INDEPENDENT'],
gap: attributes['GAP']
});
if (segment.gap && !partialSegment.gap) {
// https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis#section-6.2.1
utils.INVALIDPLAYLIST('Partial segments must have GAP=YES if they are in a gap (EXT-X-GAP)');
}
segment.parts.push(partialSegment);
}
}
return segment;
}
function parsePrefetchSegment(lines: Line[], uri: any, start: number, end: number, mediaSequenceNumber: number, discontinuitySequence: number, params: Record<string, any>): PrefetchSegment {
const segment = new PrefetchSegment({uri, mediaSequenceNumber, discontinuitySequence});
for (let i = start; i <= end; i++) {
const {name, attributes} = lines[i] as Tag;
if (name === 'EXTINF') {
utils.INVALIDPLAYLIST('A prefetch segment must not be advertised with an EXTINF tag.');
} else if (name === 'EXT-X-DISCONTINUITY') {
utils.INVALIDPLAYLIST('A prefetch segment must not be advertised with an EXT-X-DISCONTINUITY tag.');
} else if (name === 'EXT-X-PREFETCH-DISCONTINUITY') {
segment.discontinuity = true;
} else if (name === 'EXT-X-KEY') {
setCompatibleVersionOfKey(params, attributes);
segment.key = new Key({
method: attributes['METHOD'],
uri: attributes['URI'],
iv: attributes['IV'],
format: attributes['KEYFORMAT'],
formatVersion: attributes['KEYFORMATVERSIONS']
});
} else if (name === 'EXT-X-MAP') {
utils.INVALIDPLAYLIST('Prefetch segments must not be advertised with an EXT-X-MAP tag.');
}
}
return segment;
}
function parseMediaPlaylist(lines: Line[], params: Record<string, any>): MediaPlaylist {
const playlist = new MediaPlaylist();
let segmentStart = -1;
let mediaSequence = 0;
let discontinuityFound = false;
let prefetchFound = false;
let discontinuitySequence = 0;
let currentKey: Key | null = null;
let currentMap: MediaInitializationSection | null = null;
let containsParts = false;
for (const [index, line] of lines.entries()) {
const {name, value, attributes, category} = line as Tag;
if (category === 'Segment') {
if (segmentStart === -1) {
segmentStart = index;
}
if (name === 'EXT-X-DISCONTINUITY') {
discontinuityFound = true;
}
continue;
}
if (name === 'EXT-X-VERSION') {
if (playlist.version === undefined) {
playlist.version = value;
} else {
utils.INVALIDPLAYLIST('A Playlist file MUST NOT contain more than one EXT-X-VERSION tag.');
}
} else if (name === 'EXT-X-TARGETDURATION') {
playlist.targetDuration = params.targetDuration = value;
} else if (name === 'EXT-X-MEDIA-SEQUENCE') {
if (playlist.segments.length > 0) {
utils.INVALIDPLAYLIST('The EXT-X-MEDIA-SEQUENCE tag MUST appear before the first Media Segment in the Playlist.');
}
playlist.mediaSequenceBase = mediaSequence = value;
} else if (name === 'EXT-X-DISCONTINUITY-SEQUENCE') {
if (playlist.segments.length > 0) {
utils.INVALIDPLAYLIST('The EXT-X-DISCONTINUITY-SEQUENCE tag MUST appear before the first Media Segment in the Playlist.');
}
if (discontinuityFound) {
utils.INVALIDPLAYLIST('The EXT-X-DISCONTINUITY-SEQUENCE tag MUST appear before any EXT-X-DISCONTINUITY tag.');
}
playlist.discontinuitySequenceBase = discontinuitySequence = value;
} else if (name === 'EXT-X-ENDLIST') {
playlist.endlist = true;
} else if (name === 'EXT-X-PLAYLIST-TYPE') {
playlist.playlistType = value;
} else if (name === 'EXT-X-I-FRAMES-ONLY') {
if (params.compatibleVersion < 4) {
params.compatibleVersion = 4;
}
playlist.isIFrame = true;
} else if (name === 'EXT-X-INDEPENDENT-SEGMENTS') {
if (playlist.independentSegments) {
utils.INVALIDPLAYLIST('EXT-X-INDEPENDENT-SEGMENTS tag MUST NOT appear more than once in a Playlist');
}
playlist.independentSegments = true;
} else if (name === 'EXT-X-START') {
if (playlist.start) {
utils.INVALIDPLAYLIST('EXT-X-START tag MUST NOT appear more than once in a Playlist');
}
if (typeof attributes['TIME-OFFSET'] !== 'number') {
utils.INVALIDPLAYLIST('EXT-X-START: TIME-OFFSET attribute is REQUIRED');
}
playlist.start = {offset: attributes['TIME-OFFSET'], precise: attributes['PRECISE'] || false};
} else if (name === 'EXT-X-SERVER-CONTROL') {
if (!attributes['CAN-BLOCK-RELOAD']) {
utils.INVALIDPLAYLIST('EXT-X-SERVER-CONTROL: CAN-BLOCK-RELOAD=YES is mandatory for Low-Latency HLS');
}
playlist.lowLatencyCompatibility = {
canBlockReload: attributes['CAN-BLOCK-RELOAD'],
canSkipUntil: attributes['CAN-SKIP-UNTIL'],
holdBack: attributes['HOLD-BACK'],
partHoldBack: attributes['PART-HOLD-BACK']
};
} else if (name === 'EXT-X-PART-INF') {
if (!attributes['PART-TARGET']) {
utils.INVALIDPLAYLIST('EXT-X-PART-INF: PART-TARGET attribute is mandatory');
}
playlist.partTargetDuration = attributes['PART-TARGET'];
} else if (name === 'EXT-X-RENDITION-REPORT') {
if (!attributes['URI']) {
utils.INVALIDPLAYLIST('EXT-X-RENDITION-REPORT: URI attribute is mandatory');
}
if (attributes['URI'].search(/^[a-z]+:/) === 0) {
utils.INVALIDPLAYLIST('EXT-X-RENDITION-REPORT: URI must be relative to the playlist uri');
}
playlist.renditionReports.push(new RenditionReport({
uri: attributes['URI'],
lastMSN: attributes['LAST-MSN'],
lastPart: attributes['LAST-PART']
}));
} else if (name === 'EXT-X-SKIP') {
if (!attributes['SKIPPED-SEGMENTS']) {
utils.INVALIDPLAYLIST('EXT-X-SKIP: SKIPPED-SEGMENTS attribute is mandatory');
}
if (params.compatibleVersion < 9) {
params.compatibleVersion = 9;
}
playlist.skip = attributes['SKIPPED-SEGMENTS'];
mediaSequence += playlist.skip;
} else if (name === 'EXT-X-PREFETCH') {
const segment = parsePrefetchSegment(lines, value, segmentStart === -1 ? index : segmentStart, index - 1, mediaSequence++, discontinuitySequence, params);
if (segment) {
if (segment.discontinuity) {
segment.discontinuitySequence++;
discontinuitySequence = segment.discontinuitySequence;
}
if (segment.key) {
currentKey = segment.key;
} else {
segment.key = currentKey;
}
playlist.prefetchSegments.push(segment);
}
prefetchFound = true;
segmentStart = -1;
} else if (typeof line === 'string') {
// uri
if (segmentStart === -1) {
utils.INVALIDPLAYLIST('A URI line is not preceded by any segment tags');
}
if (!playlist.targetDuration) {
utils.INVALIDPLAYLIST('The EXT-X-TARGETDURATION tag is REQUIRED');
}
if (prefetchFound) {
utils.INVALIDPLAYLIST('These segments must appear after all complete segments.');
}
const segment = parseSegment(lines, line, segmentStart, index - 1, mediaSequence++, discontinuitySequence, params);
if (segment) {
[discontinuitySequence, currentKey, currentMap] = addSegment(playlist, segment, discontinuitySequence, currentKey!, currentMap!);
if (!containsParts && segment.parts.length > 0) {
containsParts = true;
}
}
segmentStart = -1;
}
}
if (segmentStart !== -1) {
const segment = parseSegment(lines, '', segmentStart, lines.length - 1, mediaSequence++, discontinuitySequence, params);
if (segment) {
const {parts} = segment;
if (parts.length > 0 && !playlist.endlist && !parts.at(-1)?.hint) {
utils.INVALIDPLAYLIST('If the Playlist contains EXT-X-PART tags and does not contain an EXT-X-ENDLIST tag, the Playlist must contain an EXT-X-PRELOAD-HINT tag with a TYPE=PART attribute');
}
// @ts-expect-error TODO check if this is not a bug the third argument should be a discontinuitySequence
addSegment(playlist, segment, currentKey, currentMap);
if (!containsParts && segment.parts.length > 0) {
containsParts = true;
}
}
}
checkDateRange(playlist.segments);
if (playlist.lowLatencyCompatibility) {
checkLowLatencyCompatibility(playlist, containsParts);
}
return playlist;
}
function addSegment(playlist: MediaPlaylist, segment: Segment, discontinuitySequence: number, currentKey?: Key, currentMap?: MediaInitializationSection): [number, Key, MediaInitializationSection] {
const {discontinuity, key, map, byterange, uri} = segment;
if (discontinuity) {
segment.discontinuitySequence = discontinuitySequence + 1;
}
if (!key) {
segment.key = currentKey;
}
if (!map) {
segment.map = currentMap!;
}
if (byterange && byterange.offset === -1) {
const {segments} = playlist;
if (segments.length > 0) {
const prevSegment = segments.at(-1)!;
if (prevSegment.byterange && prevSegment.uri === uri) {
byterange.offset = prevSegment.byterange.offset + prevSegment.byterange.length;
} else {
utils.INVALIDPLAYLIST('If offset of EXT-X-BYTERANGE is not present, a previous Media Segment MUST be a sub-range of the same media resource');
}
} else {
utils.INVALIDPLAYLIST('If offset of EXT-X-BYTERANGE is not present, a previous Media Segment MUST appear in the Playlist file');
}
}
playlist.segments.push(segment);
return [segment.discontinuitySequence, segment.key!, segment.map];
}
function checkDateRange(segments: Segment[]) {
const earliestDates = new Map();
const rangeList = new Map();
let hasDateRange = false;
let hasProgramDateTime = false;
for (let i = segments.length - 1; i >= 0; i--) {
const {programDateTime, dateRange} = segments[i];
if (programDateTime) {
hasProgramDateTime = true;
}
if (dateRange && dateRange.start) {
hasDateRange = true;
if (dateRange.endOnNext && (dateRange.end || dateRange.duration)) {
utils.INVALIDPLAYLIST('An EXT-X-DATERANGE tag with an END-ON-NEXT=YES attribute MUST NOT contain DURATION or END-DATE attributes.');
}
const start = dateRange.start.getTime();
const duration = dateRange.duration || 0;
if (dateRange.end && dateRange.duration) {
if ((start + duration * 1000) !== dateRange.end.getTime()) {
utils.INVALIDPLAYLIST('END-DATE MUST be equal to the value of the START-DATE attribute plus the value of the DURATION');
}
}
if (dateRange.endOnNext) {
dateRange.end = earliestDates.get(dateRange.classId);
}
earliestDates.set(dateRange.classId, dateRange.start);
const end = dateRange.end ? dateRange.end.getTime() : dateRange.start.getTime() + (dateRange.duration || 0) * 1000;
const range = rangeList.get(dateRange.classId);
if (range) {
for (const entry of range) {
if ((entry.start <= start && entry.end > start) || (entry.start >= start && entry.start < end)) {
utils.INVALIDPLAYLIST('DATERANGE tags with the same CLASS should not overlap');
}
}
range.push({start, end});
} else if (dateRange.classId) {
rangeList.set(dateRange.classId, [{start, end}]);
}
}
}
if (hasDateRange && !hasProgramDateTime) {
utils.INVALIDPLAYLIST('If a Playlist contains an EXT-X-DATERANGE tag, it MUST also contain at least one EXT-X-PROGRAM-DATE-TIME tag.');
}
}
function checkLowLatencyCompatibility({lowLatencyCompatibility, targetDuration, partTargetDuration, segments, renditionReports}: any, containsParts) {
const {canSkipUntil, holdBack, partHoldBack} = lowLatencyCompatibility;
if (canSkipUntil < targetDuration * 6) {
utils.INVALIDPLAYLIST('The Skip Boundary must be at least six times the EXT-X-TARGETDURATION.');
}
// Its value is a floating-point number of seconds and .
if (holdBack < targetDuration * 3) {
utils.INVALIDPLAYLIST('HOLD-BACK must be at least three times the EXT-X-TARGETDURATION.');
}
if (containsParts) {
if (partTargetDuration === undefined) {
utils.INVALIDPLAYLIST('EXT-X-PART-INF is required if a Playlist contains one or more EXT-X-PART tags');
}
if (partHoldBack === undefined) {
utils.INVALIDPLAYLIST('EXT-X-PART: PART-HOLD-BACK attribute is mandatory');
}
if (partHoldBack < partTargetDuration) {
utils.INVALIDPLAYLIST('PART-HOLD-BACK must be at least PART-TARGET');
}
for (const [segmentIndex, {parts}] of segments.entries()) {
if (parts.length > 0 && segmentIndex < segments.length - 3) {
utils.INVALIDPLAYLIST('Remove EXT-X-PART tags from the Playlist after they are greater than three target durations from the end of the Playlist.');
}
for (const [partIndex, {duration}] of parts.entries()) {
if (duration === undefined) {
continue;
}
if (duration > partTargetDuration) {
utils.INVALIDPLAYLIST('PART-TARGET is the maximum duration of any Partial Segment');
}
if (partIndex < parts.length - 1 && duration < partTargetDuration * 0.85) {
utils.INVALIDPLAYLIST('All Partial Segments except the last part of a segment must have a duration of at least 85% of PART-TARGET');
}
}
}
}
for (const report of renditionReports) {
const lastSegment = segments.at(-1);
report.lastMSN ??= lastSegment.mediaSequenceNumber;
if ((report.lastPart === null || report.lastPart === undefined) && lastSegment.parts.length > 0) {
report.lastPart = lastSegment.parts.length - 1;
}
}
}
function CHECKTAGCATEGORY(category: TagCategory, params: Record<string, any>) {
if (category === 'Segment' || category === 'MediaPlaylist') {
if (params.isMasterPlaylist === undefined) {
params.isMasterPlaylist = false;
return;
}
if (params.isMasterPlaylist) {
MIXEDTAGS();
}
return;
}
if (category === 'MasterPlaylist') {
if (params.isMasterPlaylist === undefined) {
params.isMasterPlaylist = true;
return;
}
if (params.isMasterPlaylist === false) {
MIXEDTAGS();
}
}
// category === 'Basic' or 'MediaorMasterPlaylist' or 'Unknown'
}
type Tag = {
name: string;
category: TagCategory;
value: any;
attributes: any;
};
function parseTag(line: string, params: Record<string, any>): Tag | null {
const [name, param] = splitTag(line);
const category = getTagCategory(name);
CHECKTAGCATEGORY(category, params);
if (category === 'Unknown') {
return null;
}
if (category === 'MediaPlaylist' && name !== 'EXT-X-RENDITION-REPORT' && name !== 'EXT-X-PREFETCH') {
if (params.hash[name]) {
utils.INVALIDPLAYLIST('There MUST NOT be more than one Media Playlist tag of each type in any Media Playlist');
}
params.hash[name] = true;
}
const [value, attributes] = parseTagParam(name, param);
return {name, category, value, attributes};
}
type Line = string | Tag;
function lexicalParse(text: string, params: Record<string, any>): Line[] {
const lines: Line[] = [];
for (const l of text.split('\n')) {
// V8 has garbage collection issues when cleaning up substrings split from strings greater
// than 13 characters so before we continue we need to safely copy over each line so that it
// doesn't hold any reference to the containing string.
const line = l.trim();
if (!line) {
// empty line
continue;
}
if (line.startsWith('#')) {
if (line.startsWith('#EXT')) {
// tag
const tag = parseTag(line, params);
if (tag) {
lines.push(tag);
}
}
// comment
continue;
}
// uri
lines.push(line);
}
if (lines.length === 0 || (lines[0] as Tag).name !== 'EXTM3U') {