@transmitlive/m3u8-parser 4.7.2-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/CONTRIBUTING.md +30 -0
  2. package/LICENSE +13 -0
  3. package/README.md +388 -0
  4. package/dist/m3u8-parser.cjs.js +1563 -0
  5. package/dist/m3u8-parser.es.js +1549 -0
  6. package/dist/m3u8-parser.js +1748 -0
  7. package/dist/m3u8-parser.min.js +2 -0
  8. package/index.html +15 -0
  9. package/package.json +100 -0
  10. package/scripts/karma.conf.js +12 -0
  11. package/scripts/rollup.config.js +47 -0
  12. package/src/index.js +19 -0
  13. package/src/line-stream.js +35 -0
  14. package/src/parse-stream.js +619 -0
  15. package/src/parser.js +748 -0
  16. package/test/fixtures/integration/absoluteUris.js +31 -0
  17. package/test/fixtures/integration/absoluteUris.m3u8 +12 -0
  18. package/test/fixtures/integration/allowCache.js +165 -0
  19. package/test/fixtures/integration/allowCache.m3u8 +58 -0
  20. package/test/fixtures/integration/allowCacheInvalid.js +21 -0
  21. package/test/fixtures/integration/allowCacheInvalid.m3u8 +10 -0
  22. package/test/fixtures/integration/alternateAudio.js +56 -0
  23. package/test/fixtures/integration/alternateAudio.m3u8 +9 -0
  24. package/test/fixtures/integration/alternateVideo.js +48 -0
  25. package/test/fixtures/integration/alternateVideo.m3u8 +8 -0
  26. package/test/fixtures/integration/brightcove.js +57 -0
  27. package/test/fixtures/integration/brightcove.m3u8 +9 -0
  28. package/test/fixtures/integration/byteRange.js +161 -0
  29. package/test/fixtures/integration/byteRange.m3u8 +56 -0
  30. package/test/fixtures/integration/dateTime.js +27 -0
  31. package/test/fixtures/integration/dateTime.m3u8 +12 -0
  32. package/test/fixtures/integration/diff-init-key.js +164 -0
  33. package/test/fixtures/integration/diff-init-key.m3u8 +57 -0
  34. package/test/fixtures/integration/disallowCache.js +21 -0
  35. package/test/fixtures/integration/disallowCache.m3u8 +10 -0
  36. package/test/fixtures/integration/disc-sequence.js +32 -0
  37. package/test/fixtures/integration/disc-sequence.m3u8 +15 -0
  38. package/test/fixtures/integration/discontinuity.js +59 -0
  39. package/test/fixtures/integration/discontinuity.m3u8 +26 -0
  40. package/test/fixtures/integration/domainUris.js +31 -0
  41. package/test/fixtures/integration/domainUris.m3u8 +12 -0
  42. package/test/fixtures/integration/empty.js +5 -0
  43. package/test/fixtures/integration/empty.m3u8 +0 -0
  44. package/test/fixtures/integration/emptyAllowCache.js +21 -0
  45. package/test/fixtures/integration/emptyAllowCache.m3u8 +10 -0
  46. package/test/fixtures/integration/emptyMediaSequence.js +31 -0
  47. package/test/fixtures/integration/emptyMediaSequence.m3u8 +14 -0
  48. package/test/fixtures/integration/emptyPlaylistType.js +40 -0
  49. package/test/fixtures/integration/emptyPlaylistType.m3u8 +16 -0
  50. package/test/fixtures/integration/emptyTargetDuration.js +57 -0
  51. package/test/fixtures/integration/emptyTargetDuration.m3u8 +10 -0
  52. package/test/fixtures/integration/encrypted.js +61 -0
  53. package/test/fixtures/integration/encrypted.m3u8 +28 -0
  54. package/test/fixtures/integration/event.js +41 -0
  55. package/test/fixtures/integration/event.m3u8 +16 -0
  56. package/test/fixtures/integration/extXPlaylistTypeInvalidPlaylist.js +15 -0
  57. package/test/fixtures/integration/extXPlaylistTypeInvalidPlaylist.m3u8 +8 -0
  58. package/test/fixtures/integration/extinf.js +165 -0
  59. package/test/fixtures/integration/extinf.m3u8 +57 -0
  60. package/test/fixtures/integration/fmp4.js +44 -0
  61. package/test/fixtures/integration/fmp4.m3u8 +14 -0
  62. package/test/fixtures/integration/headerOnly.js +5 -0
  63. package/test/fixtures/integration/headerOnly.m3u8 +1 -0
  64. package/test/fixtures/integration/invalidAllowCache.js +21 -0
  65. package/test/fixtures/integration/invalidAllowCache.m3u8 +10 -0
  66. package/test/fixtures/integration/invalidMediaSequence.js +31 -0
  67. package/test/fixtures/integration/invalidMediaSequence.m3u8 +14 -0
  68. package/test/fixtures/integration/invalidPlaylistType.js +40 -0
  69. package/test/fixtures/integration/invalidPlaylistType.m3u8 +16 -0
  70. package/test/fixtures/integration/invalidTargetDuration.js +164 -0
  71. package/test/fixtures/integration/invalidTargetDuration.m3u8 +57 -0
  72. package/test/fixtures/integration/liveMissingSegmentDuration.js +25 -0
  73. package/test/fixtures/integration/liveMissingSegmentDuration.m3u8 +9 -0
  74. package/test/fixtures/integration/liveStart30sBefore.js +54 -0
  75. package/test/fixtures/integration/liveStart30sBefore.m3u8 +22 -0
  76. package/test/fixtures/integration/llhls-byte-range.js +253 -0
  77. package/test/fixtures/integration/llhls-byte-range.m3u8 +66 -0
  78. package/test/fixtures/integration/llhls-delta-byte-range.js +149 -0
  79. package/test/fixtures/integration/llhls-delta-byte-range.m3u8 +30 -0
  80. package/test/fixtures/integration/llhls.js +214 -0
  81. package/test/fixtures/integration/llhls.m3u8 +56 -0
  82. package/test/fixtures/integration/llhlsDelta.js +186 -0
  83. package/test/fixtures/integration/llhlsDelta.m3u8 +50 -0
  84. package/test/fixtures/integration/manifestExtTTargetdurationNegative.js +14 -0
  85. package/test/fixtures/integration/manifestExtTTargetdurationNegative.m3u8 +5 -0
  86. package/test/fixtures/integration/manifestExtXEndlistEarly.js +35 -0
  87. package/test/fixtures/integration/manifestExtXEndlistEarly.m3u8 +14 -0
  88. package/test/fixtures/integration/manifestNoExtM3u.js +15 -0
  89. package/test/fixtures/integration/manifestNoExtM3u.m3u8 +4 -0
  90. package/test/fixtures/integration/master-fmp4.js +465 -0
  91. package/test/fixtures/integration/master-fmp4.m3u8 +76 -0
  92. package/test/fixtures/integration/master.js +57 -0
  93. package/test/fixtures/integration/master.m3u8 +10 -0
  94. package/test/fixtures/integration/media.js +31 -0
  95. package/test/fixtures/integration/media.m3u8 +12 -0
  96. package/test/fixtures/integration/mediaSequence.js +31 -0
  97. package/test/fixtures/integration/mediaSequence.m3u8 +14 -0
  98. package/test/fixtures/integration/missingEndlist.js +19 -0
  99. package/test/fixtures/integration/missingEndlist.m3u8 +6 -0
  100. package/test/fixtures/integration/missingExtinf.js +27 -0
  101. package/test/fixtures/integration/missingExtinf.m3u8 +11 -0
  102. package/test/fixtures/integration/missingMediaSequence.js +31 -0
  103. package/test/fixtures/integration/missingMediaSequence.m3u8 +13 -0
  104. package/test/fixtures/integration/missingSegmentDuration.js +31 -0
  105. package/test/fixtures/integration/missingSegmentDuration.m3u8 +11 -0
  106. package/test/fixtures/integration/multipleAudioGroups.js +89 -0
  107. package/test/fixtures/integration/multipleAudioGroups.m3u8 +17 -0
  108. package/test/fixtures/integration/multipleAudioGroupsCombinedMain.js +88 -0
  109. package/test/fixtures/integration/multipleAudioGroupsCombinedMain.m3u8 +17 -0
  110. package/test/fixtures/integration/multipleTargetDurations.js +28 -0
  111. package/test/fixtures/integration/multipleTargetDurations.m3u8 +8 -0
  112. package/test/fixtures/integration/multipleVideo.js +74 -0
  113. package/test/fixtures/integration/multipleVideo.m3u8 +16 -0
  114. package/test/fixtures/integration/negativeMediaSequence.js +31 -0
  115. package/test/fixtures/integration/negativeMediaSequence.m3u8 +14 -0
  116. package/test/fixtures/integration/playlist.js +165 -0
  117. package/test/fixtures/integration/playlist.m3u8 +57 -0
  118. package/test/fixtures/integration/playlistMediaSequenceHigher.js +16 -0
  119. package/test/fixtures/integration/playlistMediaSequenceHigher.m3u8 +8 -0
  120. package/test/fixtures/integration/start.js +36 -0
  121. package/test/fixtures/integration/start.m3u8 +13 -0
  122. package/test/fixtures/integration/streamInfInvalid.js +24 -0
  123. package/test/fixtures/integration/streamInfInvalid.m3u8 +6 -0
  124. package/test/fixtures/integration/twoMediaSequences.js +31 -0
  125. package/test/fixtures/integration/twoMediaSequences.m3u8 +15 -0
  126. package/test/fixtures/integration/versionInvalid.js +16 -0
  127. package/test/fixtures/integration/versionInvalid.m3u8 +8 -0
  128. package/test/fixtures/integration/whiteSpace.js +31 -0
  129. package/test/fixtures/integration/whiteSpace.m3u8 +13 -0
  130. package/test/fixtures/integration/zeroDuration.js +16 -0
  131. package/test/fixtures/integration/zeroDuration.m3u8 +7 -0
  132. package/test/line-stream.test.js +80 -0
  133. package/test/parse-stream.test.js +903 -0
  134. package/test/parser.test.js +884 -0
@@ -0,0 +1,1563 @@
1
+ /*! @name @transmitlive/m3u8-parser @version 4.7.2-beta.6 @license Apache-2.0 */
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _inheritsLoose = require('@babel/runtime/helpers/inheritsLoose');
7
+ var Stream = require('@videojs/vhs-utils/cjs/stream.js');
8
+ var _extends = require('@babel/runtime/helpers/extends');
9
+ var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
10
+ var decodeB64ToUint8Array = require('@videojs/vhs-utils/cjs/decode-b64-to-uint8-array.js');
11
+
12
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
+
14
+ var _inheritsLoose__default = /*#__PURE__*/_interopDefaultLegacy(_inheritsLoose);
15
+ var Stream__default = /*#__PURE__*/_interopDefaultLegacy(Stream);
16
+ var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
17
+ var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assertThisInitialized);
18
+ var decodeB64ToUint8Array__default = /*#__PURE__*/_interopDefaultLegacy(decodeB64ToUint8Array);
19
+
20
+ /**
21
+ * A stream that buffers string input and generates a `data` event for each
22
+ * line.
23
+ *
24
+ * @class LineStream
25
+ * @extends Stream
26
+ */
27
+
28
+ var LineStream = /*#__PURE__*/function (_Stream) {
29
+ _inheritsLoose__default['default'](LineStream, _Stream);
30
+
31
+ function LineStream() {
32
+ var _this;
33
+
34
+ _this = _Stream.call(this) || this;
35
+ _this.buffer = '';
36
+ return _this;
37
+ }
38
+ /**
39
+ * Add new data to be parsed.
40
+ *
41
+ * @param {string} data the text to process
42
+ */
43
+
44
+
45
+ var _proto = LineStream.prototype;
46
+
47
+ _proto.push = function push(data) {
48
+ var nextNewline;
49
+ this.buffer += data;
50
+ nextNewline = this.buffer.indexOf('\n');
51
+
52
+ for (; nextNewline > -1; nextNewline = this.buffer.indexOf('\n')) {
53
+ this.trigger('data', this.buffer.substring(0, nextNewline));
54
+ this.buffer = this.buffer.substring(nextNewline + 1);
55
+ }
56
+ };
57
+
58
+ return LineStream;
59
+ }(Stream__default['default']);
60
+
61
+ var TAB = String.fromCharCode(0x09);
62
+
63
+ var parseByterange = function parseByterange(byterangeString) {
64
+ // optionally match and capture 0+ digits before `@`
65
+ // optionally match and capture 0+ digits after `@`
66
+ var match = /([0-9.]*)?@?([0-9.]*)?/.exec(byterangeString || '');
67
+ var result = {};
68
+
69
+ if (match[1]) {
70
+ result.length = parseInt(match[1], 10);
71
+ }
72
+
73
+ if (match[2]) {
74
+ result.offset = parseInt(match[2], 10);
75
+ }
76
+
77
+ return result;
78
+ };
79
+ /**
80
+ * "forgiving" attribute list psuedo-grammar:
81
+ * attributes -> keyvalue (',' keyvalue)*
82
+ * keyvalue -> key '=' value
83
+ * key -> [^=]*
84
+ * value -> '"' [^"]* '"' | [^,]*
85
+ */
86
+
87
+
88
+ var attributeSeparator = function attributeSeparator() {
89
+ var key = '[^=]*';
90
+ var value = '"[^"]*"|[^,]*';
91
+ var keyvalue = '(?:' + key + ')=(?:' + value + ')';
92
+ return new RegExp('(?:^|,)(' + keyvalue + ')');
93
+ };
94
+ /**
95
+ * Parse attributes from a line given the separator
96
+ *
97
+ * @param {string} attributes the attribute line to parse
98
+ */
99
+
100
+
101
+ var parseAttributes = function parseAttributes(attributes) {
102
+ // split the string using attributes as the separator
103
+ var attrs = attributes.split(attributeSeparator());
104
+ var result = {};
105
+ var i = attrs.length;
106
+ var attr;
107
+
108
+ while (i--) {
109
+ // filter out unmatched portions of the string
110
+ if (attrs[i] === '') {
111
+ continue;
112
+ } // split the key and value
113
+
114
+
115
+ attr = /([^=]*)=(.*)/.exec(attrs[i]).slice(1); // trim whitespace and remove optional quotes around the value
116
+
117
+ attr[0] = attr[0].replace(/^\s+|\s+$/g, '');
118
+ attr[1] = attr[1].replace(/^\s+|\s+$/g, '');
119
+ attr[1] = attr[1].replace(/^['"](.*)['"]$/g, '$1');
120
+ result[attr[0]] = attr[1];
121
+ }
122
+
123
+ return result;
124
+ };
125
+ /**
126
+ * A line-level M3U8 parser event stream. It expects to receive input one
127
+ * line at a time and performs a context-free parse of its contents. A stream
128
+ * interpretation of a manifest can be useful if the manifest is expected to
129
+ * be too large to fit comfortably into memory or the entirety of the input
130
+ * is not immediately available. Otherwise, it's probably much easier to work
131
+ * with a regular `Parser` object.
132
+ *
133
+ * Produces `data` events with an object that captures the parser's
134
+ * interpretation of the input. That object has a property `tag` that is one
135
+ * of `uri`, `comment`, or `tag`. URIs only have a single additional
136
+ * property, `line`, which captures the entirety of the input without
137
+ * interpretation. Comments similarly have a single additional property
138
+ * `text` which is the input without the leading `#`.
139
+ *
140
+ * Tags always have a property `tagType` which is the lower-cased version of
141
+ * the M3U8 directive without the `#EXT` or `#EXT-X-` prefix. For instance,
142
+ * `#EXT-X-MEDIA-SEQUENCE` becomes `media-sequence` when parsed. Unrecognized
143
+ * tags are given the tag type `unknown` and a single additional property
144
+ * `data` with the remainder of the input.
145
+ *
146
+ * @class ParseStream
147
+ * @extends Stream
148
+ */
149
+
150
+
151
+ var ParseStream = /*#__PURE__*/function (_Stream) {
152
+ _inheritsLoose__default['default'](ParseStream, _Stream);
153
+
154
+ function ParseStream() {
155
+ var _this;
156
+
157
+ _this = _Stream.call(this) || this;
158
+ _this.customParsers = [];
159
+ _this.tagMappers = [];
160
+ _this.lineNumber = 0;
161
+ return _this;
162
+ }
163
+ /**
164
+ * Parses an additional line of input.
165
+ *
166
+ * @param {string} line a single line of an M3U8 file to parse
167
+ */
168
+
169
+
170
+ var _proto = ParseStream.prototype;
171
+
172
+ _proto.push = function push(line) {
173
+ var _this2 = this;
174
+
175
+ var match;
176
+ var event;
177
+ this.lineNumber = this.lineNumber + 1; // strip whitespace
178
+
179
+ line = line.trim();
180
+
181
+ if (line.length === 0) {
182
+ // ignore empty lines
183
+ return;
184
+ } // URIs
185
+
186
+
187
+ if (line[0] !== '#') {
188
+ this.trigger('data', {
189
+ type: 'uri',
190
+ uri: line
191
+ });
192
+ return;
193
+ } // map tags
194
+
195
+
196
+ var newLines = this.tagMappers.reduce(function (acc, mapper) {
197
+ var mappedLine = mapper(line); // skip if unchanged
198
+
199
+ if (mappedLine === line) {
200
+ return acc;
201
+ }
202
+
203
+ return acc.concat([mappedLine]);
204
+ }, [line]);
205
+ newLines.forEach(function (newLine) {
206
+ for (var i = 0; i < _this2.customParsers.length; i++) {
207
+ if (_this2.customParsers[i].call(_this2, newLine)) {
208
+ return;
209
+ }
210
+ } // Comments
211
+
212
+
213
+ if (newLine.indexOf('#EXT') !== 0) {
214
+ _this2.trigger('data', {
215
+ type: 'comment',
216
+ text: newLine.slice(1)
217
+ });
218
+
219
+ return;
220
+ } // strip off any carriage returns here so the regex matching
221
+ // doesn't have to account for them.
222
+
223
+
224
+ newLine = newLine.replace('\r', ''); // Tags
225
+
226
+ match = /^#EXTM3U/.exec(newLine);
227
+
228
+ if (match) {
229
+ _this2.trigger('data', {
230
+ type: 'tag',
231
+ tagType: 'm3u'
232
+ });
233
+
234
+ return;
235
+ }
236
+
237
+ match = /^#EXTINF:?([0-9\.]*)?,?(.*)?$/.exec(newLine);
238
+
239
+ if (match) {
240
+ event = {
241
+ type: 'tag',
242
+ tagType: 'inf'
243
+ };
244
+
245
+ if (match[1]) {
246
+ event.duration = parseFloat(match[1]);
247
+ }
248
+
249
+ if (match[2]) {
250
+ event.title = match[2];
251
+ }
252
+
253
+ _this2.trigger('data', event);
254
+
255
+ return;
256
+ }
257
+
258
+ match = /^#EXT-X-TARGETDURATION:?([0-9.]*)?/.exec(newLine);
259
+
260
+ if (match) {
261
+ event = {
262
+ type: 'tag',
263
+ tagType: 'targetduration'
264
+ };
265
+
266
+ if (match[1]) {
267
+ event.duration = parseInt(match[1], 10);
268
+ }
269
+
270
+ _this2.trigger('data', event);
271
+
272
+ return;
273
+ }
274
+
275
+ match = /^#EXT-X-VERSION:?([0-9.]*)?/.exec(newLine);
276
+
277
+ if (match) {
278
+ event = {
279
+ type: 'tag',
280
+ tagType: 'version'
281
+ };
282
+
283
+ if (match[1]) {
284
+ event.version = parseInt(match[1], 10);
285
+ }
286
+
287
+ _this2.trigger('data', event);
288
+
289
+ return;
290
+ }
291
+
292
+ match = /^#EXT-X-MEDIA-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine);
293
+
294
+ if (match) {
295
+ event = {
296
+ type: 'tag',
297
+ tagType: 'media-sequence'
298
+ };
299
+
300
+ if (match[1]) {
301
+ event.number = parseInt(match[1], 10);
302
+ }
303
+
304
+ _this2.trigger('data', event);
305
+
306
+ return;
307
+ }
308
+
309
+ match = /^#EXT-X-DISCONTINUITY-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine);
310
+
311
+ if (match) {
312
+ event = {
313
+ type: 'tag',
314
+ tagType: 'discontinuity-sequence'
315
+ };
316
+
317
+ if (match[1]) {
318
+ event.number = parseInt(match[1], 10);
319
+ }
320
+
321
+ _this2.trigger('data', event);
322
+
323
+ return;
324
+ }
325
+
326
+ match = /^#EXT-X-PLAYLIST-TYPE:?(.*)?$/.exec(newLine);
327
+
328
+ if (match) {
329
+ event = {
330
+ type: 'tag',
331
+ tagType: 'playlist-type'
332
+ };
333
+
334
+ if (match[1]) {
335
+ event.playlistType = match[1];
336
+ }
337
+
338
+ _this2.trigger('data', event);
339
+
340
+ return;
341
+ }
342
+
343
+ match = /^#EXT-X-BYTERANGE:?(.*)?$/.exec(newLine);
344
+
345
+ if (match) {
346
+ event = _extends__default['default'](parseByterange(match[1]), {
347
+ type: 'tag',
348
+ tagType: 'byterange'
349
+ });
350
+
351
+ _this2.trigger('data', event);
352
+
353
+ return;
354
+ }
355
+
356
+ match = /^#EXT-X-ALLOW-CACHE:?(YES|NO)?/.exec(newLine);
357
+
358
+ if (match) {
359
+ event = {
360
+ type: 'tag',
361
+ tagType: 'allow-cache'
362
+ };
363
+
364
+ if (match[1]) {
365
+ event.allowed = !/NO/.test(match[1]);
366
+ }
367
+
368
+ _this2.trigger('data', event);
369
+
370
+ return;
371
+ }
372
+
373
+ match = /^#EXT-X-MAP:?(.*)$/.exec(newLine);
374
+
375
+ if (match) {
376
+ event = {
377
+ type: 'tag',
378
+ tagType: 'map'
379
+ };
380
+
381
+ if (match[1]) {
382
+ var attributes = parseAttributes(match[1]);
383
+
384
+ if (attributes.URI) {
385
+ event.uri = attributes.URI;
386
+ }
387
+
388
+ if (attributes.BYTERANGE) {
389
+ event.byterange = parseByterange(attributes.BYTERANGE);
390
+ }
391
+ }
392
+
393
+ _this2.trigger('data', event);
394
+
395
+ return;
396
+ }
397
+
398
+ match = /^#EXT-X-STREAM-INF:?(.*)$/.exec(newLine);
399
+
400
+ if (match) {
401
+ event = {
402
+ type: 'tag',
403
+ tagType: 'stream-inf'
404
+ };
405
+
406
+ if (match[1]) {
407
+ event.attributes = parseAttributes(match[1]);
408
+
409
+ if (event.attributes.RESOLUTION) {
410
+ var split = event.attributes.RESOLUTION.split('x');
411
+ var resolution = {};
412
+
413
+ if (split[0]) {
414
+ resolution.width = parseInt(split[0], 10);
415
+ }
416
+
417
+ if (split[1]) {
418
+ resolution.height = parseInt(split[1], 10);
419
+ }
420
+
421
+ event.attributes.RESOLUTION = resolution;
422
+ }
423
+
424
+ if (event.attributes.BANDWIDTH) {
425
+ event.attributes.BANDWIDTH = parseInt(event.attributes.BANDWIDTH, 10);
426
+ }
427
+
428
+ if (event.attributes['PROGRAM-ID']) {
429
+ event.attributes['PROGRAM-ID'] = parseInt(event.attributes['PROGRAM-ID'], 10);
430
+ }
431
+ }
432
+
433
+ _this2.trigger('data', event);
434
+
435
+ return;
436
+ }
437
+
438
+ match = /^#EXT-X-MEDIA:?(.*)$/.exec(newLine);
439
+
440
+ if (match) {
441
+ event = {
442
+ type: 'tag',
443
+ tagType: 'media'
444
+ };
445
+
446
+ if (match[1]) {
447
+ event.attributes = parseAttributes(match[1]);
448
+ }
449
+
450
+ _this2.trigger('data', event);
451
+
452
+ return;
453
+ }
454
+
455
+ match = /^#EXT-X-ENDLIST/.exec(newLine);
456
+
457
+ if (match) {
458
+ _this2.trigger('data', {
459
+ type: 'tag',
460
+ tagType: 'endlist'
461
+ });
462
+
463
+ return;
464
+ }
465
+
466
+ match = /^#EXT-X-DISCONTINUITY/.exec(newLine);
467
+
468
+ if (match) {
469
+ _this2.trigger('data', {
470
+ type: 'tag',
471
+ tagType: 'discontinuity'
472
+ });
473
+
474
+ return;
475
+ }
476
+
477
+ match = /^#EXT-X-PROGRAM-DATE-TIME:?(.*)$/.exec(newLine);
478
+
479
+ if (match) {
480
+ event = {
481
+ type: 'tag',
482
+ tagType: 'program-date-time'
483
+ };
484
+
485
+ if (match[1]) {
486
+ event.dateTimeString = match[1];
487
+ event.dateTimeObject = new Date(match[1]);
488
+ }
489
+
490
+ _this2.trigger('data', event);
491
+
492
+ return;
493
+ }
494
+
495
+ match = /^#EXT-X-KEY:?(.*)$/.exec(newLine);
496
+
497
+ if (match) {
498
+ event = {
499
+ type: 'tag',
500
+ tagType: 'key'
501
+ };
502
+
503
+ if (match[1]) {
504
+ event.attributes = parseAttributes(match[1]); // parse the IV string into a Uint32Array
505
+
506
+ if (event.attributes.IV) {
507
+ if (event.attributes.IV.substring(0, 2).toLowerCase() === '0x') {
508
+ event.attributes.IV = event.attributes.IV.substring(2);
509
+ }
510
+
511
+ event.attributes.IV = event.attributes.IV.match(/.{8}/g);
512
+ event.attributes.IV[0] = parseInt(event.attributes.IV[0], 16);
513
+ event.attributes.IV[1] = parseInt(event.attributes.IV[1], 16);
514
+ event.attributes.IV[2] = parseInt(event.attributes.IV[2], 16);
515
+ event.attributes.IV[3] = parseInt(event.attributes.IV[3], 16);
516
+ event.attributes.IV = new Uint32Array(event.attributes.IV);
517
+ }
518
+ }
519
+
520
+ _this2.trigger('data', event);
521
+
522
+ return;
523
+ }
524
+
525
+ match = /^#EXT-X-START:?(.*)$/.exec(newLine);
526
+
527
+ if (match) {
528
+ event = {
529
+ type: 'tag',
530
+ tagType: 'start'
531
+ };
532
+
533
+ if (match[1]) {
534
+ event.attributes = parseAttributes(match[1]);
535
+ event.attributes['TIME-OFFSET'] = parseFloat(event.attributes['TIME-OFFSET']);
536
+ event.attributes.PRECISE = /YES/.test(event.attributes.PRECISE);
537
+ }
538
+
539
+ _this2.trigger('data', event);
540
+
541
+ return;
542
+ }
543
+
544
+ match = /^#EXT-X-CUE-OUT-CONT:?(.*)?$/.exec(newLine);
545
+
546
+ if (match) {
547
+ event = {
548
+ type: 'tag',
549
+ tagType: 'cue-out-cont'
550
+ };
551
+
552
+ if (match[1]) {
553
+ event.data = match[1];
554
+ } else {
555
+ event.data = '';
556
+ }
557
+
558
+ _this2.trigger('data', event);
559
+
560
+ return;
561
+ }
562
+
563
+ match = /^#EXT-X-CUE-OUT:?(.*)?$/.exec(newLine);
564
+
565
+ if (match) {
566
+ event = {
567
+ type: 'tag',
568
+ tagType: 'cue-out'
569
+ };
570
+
571
+ if (match[1]) {
572
+ event.data = match[1];
573
+ } else {
574
+ event.data = '';
575
+ }
576
+
577
+ _this2.trigger('data', event);
578
+
579
+ return;
580
+ }
581
+
582
+ match = /^#EXT-X-CUE-IN:?(.*)?$/.exec(newLine);
583
+
584
+ if (match) {
585
+ event = {
586
+ type: 'tag',
587
+ tagType: 'cue-in'
588
+ };
589
+
590
+ if (match[1]) {
591
+ event.data = match[1];
592
+ } else {
593
+ event.data = '';
594
+ }
595
+
596
+ _this2.trigger('data', event);
597
+
598
+ return;
599
+ }
600
+
601
+ match = /^#EXT-X-SKIP:(.*)$/.exec(newLine);
602
+
603
+ if (match && match[1]) {
604
+ event = {
605
+ type: 'tag',
606
+ tagType: 'skip'
607
+ };
608
+ event.attributes = parseAttributes(match[1]);
609
+
610
+ if (event.attributes.hasOwnProperty('SKIPPED-SEGMENTS')) {
611
+ event.attributes['SKIPPED-SEGMENTS'] = parseInt(event.attributes['SKIPPED-SEGMENTS'], 10);
612
+ }
613
+
614
+ if (event.attributes.hasOwnProperty('RECENTLY-REMOVED-DATERANGES')) {
615
+ event.attributes['RECENTLY-REMOVED-DATERANGES'] = event.attributes['RECENTLY-REMOVED-DATERANGES'].split(TAB);
616
+ }
617
+
618
+ _this2.trigger('data', event);
619
+
620
+ return;
621
+ }
622
+
623
+ match = /^#EXT-X-PART:(.*)$/.exec(newLine);
624
+
625
+ if (match && match[1]) {
626
+ event = {
627
+ type: 'tag',
628
+ tagType: 'part'
629
+ };
630
+ event.attributes = parseAttributes(match[1]);
631
+ ['DURATION'].forEach(function (key) {
632
+ if (event.attributes.hasOwnProperty(key)) {
633
+ event.attributes[key] = parseFloat(event.attributes[key]);
634
+ }
635
+ });
636
+ ['INDEPENDENT', 'GAP'].forEach(function (key) {
637
+ if (event.attributes.hasOwnProperty(key)) {
638
+ event.attributes[key] = /YES/.test(event.attributes[key]);
639
+ }
640
+ });
641
+
642
+ if (event.attributes.hasOwnProperty('BYTERANGE')) {
643
+ event.attributes.byterange = parseByterange(event.attributes.BYTERANGE);
644
+ }
645
+
646
+ _this2.trigger('data', event);
647
+
648
+ return;
649
+ }
650
+
651
+ match = /^#EXT-X-SERVER-CONTROL:(.*)$/.exec(newLine);
652
+
653
+ if (match && match[1]) {
654
+ event = {
655
+ type: 'tag',
656
+ tagType: 'server-control'
657
+ };
658
+ event.attributes = parseAttributes(match[1]);
659
+ ['CAN-SKIP-UNTIL', 'PART-HOLD-BACK', 'HOLD-BACK'].forEach(function (key) {
660
+ if (event.attributes.hasOwnProperty(key)) {
661
+ event.attributes[key] = parseFloat(event.attributes[key]);
662
+ }
663
+ });
664
+ ['CAN-SKIP-DATERANGES', 'CAN-BLOCK-RELOAD'].forEach(function (key) {
665
+ if (event.attributes.hasOwnProperty(key)) {
666
+ event.attributes[key] = /YES/.test(event.attributes[key]);
667
+ }
668
+ });
669
+
670
+ _this2.trigger('data', event);
671
+
672
+ return;
673
+ }
674
+
675
+ match = /^#EXT-X-PART-INF:(.*)$/.exec(newLine);
676
+
677
+ if (match && match[1]) {
678
+ event = {
679
+ type: 'tag',
680
+ tagType: 'part-inf'
681
+ };
682
+ event.attributes = parseAttributes(match[1]);
683
+ ['PART-TARGET'].forEach(function (key) {
684
+ if (event.attributes.hasOwnProperty(key)) {
685
+ event.attributes[key] = parseFloat(event.attributes[key]);
686
+ }
687
+ });
688
+
689
+ _this2.trigger('data', event);
690
+
691
+ return;
692
+ }
693
+
694
+ match = /^#EXT-X-PRELOAD-HINT:(.*)$/.exec(newLine);
695
+
696
+ if (match && match[1]) {
697
+ event = {
698
+ type: 'tag',
699
+ tagType: 'preload-hint'
700
+ };
701
+ event.attributes = parseAttributes(match[1]);
702
+ ['BYTERANGE-START', 'BYTERANGE-LENGTH'].forEach(function (key) {
703
+ if (event.attributes.hasOwnProperty(key)) {
704
+ event.attributes[key] = parseInt(event.attributes[key], 10);
705
+ var subkey = key === 'BYTERANGE-LENGTH' ? 'length' : 'offset';
706
+ event.attributes.byterange = event.attributes.byterange || {};
707
+ event.attributes.byterange[subkey] = event.attributes[key]; // only keep the parsed byterange object.
708
+
709
+ delete event.attributes[key];
710
+ }
711
+ });
712
+
713
+ _this2.trigger('data', event);
714
+
715
+ return;
716
+ }
717
+
718
+ match = /^#EXT-X-RENDITION-REPORT:(.*)$/.exec(newLine);
719
+
720
+ if (match && match[1]) {
721
+ event = {
722
+ type: 'tag',
723
+ tagType: 'rendition-report'
724
+ };
725
+ event.attributes = parseAttributes(match[1]);
726
+ ['LAST-MSN', 'LAST-PART'].forEach(function (key) {
727
+ if (event.attributes.hasOwnProperty(key)) {
728
+ event.attributes[key] = parseInt(event.attributes[key], 10);
729
+ }
730
+ });
731
+
732
+ _this2.trigger('data', event);
733
+
734
+ return;
735
+ } // unknown tag type
736
+
737
+
738
+ _this2.trigger('data', {
739
+ type: 'tag',
740
+ data: newLine.slice(4)
741
+ });
742
+ });
743
+ }
744
+ /**
745
+ * Add a parser for custom headers
746
+ *
747
+ * @param {Object} options a map of options for the added parser
748
+ * @param {RegExp} options.expression a regular expression to match the custom header
749
+ * @param {string} options.customType the custom type to register to the output
750
+ * @param {Function} [options.dataParser] function to parse the line into an object
751
+ * @param {boolean} [options.segment] should tag data be attached to the segment object
752
+ */
753
+ ;
754
+
755
+ _proto.addParser = function addParser(_ref) {
756
+ var _this3 = this;
757
+
758
+ var expression = _ref.expression,
759
+ customType = _ref.customType,
760
+ dataParser = _ref.dataParser,
761
+ segment = _ref.segment;
762
+
763
+ if (typeof dataParser !== 'function') {
764
+ dataParser = function dataParser(line) {
765
+ return line;
766
+ };
767
+ }
768
+
769
+ this.customParsers.push(function (line) {
770
+ var match = expression.exec(line);
771
+
772
+ if (match) {
773
+ _this3.trigger('data', {
774
+ type: 'custom',
775
+ data: dataParser(line),
776
+ customType: customType,
777
+ segment: segment
778
+ });
779
+
780
+ return true;
781
+ }
782
+ });
783
+ }
784
+ /**
785
+ * Add a custom header mapper
786
+ *
787
+ * @param {Object} options
788
+ * @param {RegExp} options.expression a regular expression to match the custom header
789
+ * @param {Function} options.map function to translate tag into a different tag
790
+ */
791
+ ;
792
+
793
+ _proto.addTagMapper = function addTagMapper(_ref2) {
794
+ var expression = _ref2.expression,
795
+ map = _ref2.map;
796
+
797
+ var mapFn = function mapFn(line) {
798
+ if (expression.test(line)) {
799
+ return map(line);
800
+ }
801
+
802
+ return line;
803
+ };
804
+
805
+ this.tagMappers.push(mapFn);
806
+ };
807
+
808
+ return ParseStream;
809
+ }(Stream__default['default']);
810
+
811
+ var camelCase = function camelCase(str) {
812
+ return str.toLowerCase().replace(/-(\w)/g, function (a) {
813
+ return a[1].toUpperCase();
814
+ });
815
+ };
816
+
817
+ var camelCaseKeys = function camelCaseKeys(attributes) {
818
+ var result = {};
819
+ Object.keys(attributes).forEach(function (key) {
820
+ result[camelCase(key)] = attributes[key];
821
+ });
822
+ return result;
823
+ }; // set SERVER-CONTROL hold back based upon targetDuration and partTargetDuration
824
+ // we need this helper because defaults are based upon targetDuration and
825
+ // partTargetDuration being set, but they may not be if SERVER-CONTROL appears before
826
+ // target durations are set.
827
+
828
+
829
+ var setHoldBack = function setHoldBack(manifest) {
830
+ var serverControl = manifest.serverControl,
831
+ targetDuration = manifest.targetDuration,
832
+ partTargetDuration = manifest.partTargetDuration;
833
+
834
+ if (!serverControl) {
835
+ return;
836
+ }
837
+
838
+ var tag = '#EXT-X-SERVER-CONTROL';
839
+ var hb = 'holdBack';
840
+ var phb = 'partHoldBack';
841
+ var minTargetDuration = targetDuration && targetDuration * 3;
842
+ var minPartDuration = partTargetDuration && partTargetDuration * 2;
843
+
844
+ if (targetDuration && !serverControl.hasOwnProperty(hb)) {
845
+ serverControl[hb] = minTargetDuration;
846
+ this.trigger('info', {
847
+ message: tag + " defaulting HOLD-BACK to targetDuration * 3 (" + minTargetDuration + ")."
848
+ });
849
+ }
850
+
851
+ if (minTargetDuration && serverControl[hb] < minTargetDuration) {
852
+ this.trigger('warn', {
853
+ message: tag + " clamping HOLD-BACK (" + serverControl[hb] + ") to targetDuration * 3 (" + minTargetDuration + ")"
854
+ });
855
+ serverControl[hb] = minTargetDuration;
856
+ } // default no part hold back to part target duration * 3
857
+
858
+
859
+ if (partTargetDuration && !serverControl.hasOwnProperty(phb)) {
860
+ serverControl[phb] = partTargetDuration * 3;
861
+ this.trigger('info', {
862
+ message: tag + " defaulting PART-HOLD-BACK to partTargetDuration * 3 (" + serverControl[phb] + ")."
863
+ });
864
+ } // if part hold back is too small default it to part target duration * 2
865
+
866
+
867
+ if (partTargetDuration && serverControl[phb] < minPartDuration) {
868
+ this.trigger('warn', {
869
+ message: tag + " clamping PART-HOLD-BACK (" + serverControl[phb] + ") to partTargetDuration * 2 (" + minPartDuration + ")."
870
+ });
871
+ serverControl[phb] = minPartDuration;
872
+ }
873
+ };
874
+ /**
875
+ * A parser for M3U8 files. The current interpretation of the input is
876
+ * exposed as a property `manifest` on parser objects. It's just two lines to
877
+ * create and parse a manifest once you have the contents available as a string:
878
+ *
879
+ * ```js
880
+ * var parser = new m3u8.Parser();
881
+ * parser.push(xhr.responseText);
882
+ * ```
883
+ *
884
+ * New input can later be applied to update the manifest object by calling
885
+ * `push` again.
886
+ *
887
+ * The parser attempts to create a usable manifest object even if the
888
+ * underlying input is somewhat nonsensical. It emits `info` and `warning`
889
+ * events during the parse if it encounters input that seems invalid or
890
+ * requires some property of the manifest object to be defaulted.
891
+ *
892
+ * @class Parser
893
+ * @extends Stream
894
+ */
895
+
896
+
897
+ var Parser = /*#__PURE__*/function (_Stream) {
898
+ _inheritsLoose__default['default'](Parser, _Stream);
899
+
900
+ function Parser() {
901
+ var _this;
902
+
903
+ _this = _Stream.call(this) || this;
904
+ _this.lineStream = new LineStream();
905
+ _this.parseStream = new ParseStream();
906
+
907
+ _this.lineStream.pipe(_this.parseStream);
908
+ /* eslint-disable consistent-this */
909
+
910
+
911
+ var self = _assertThisInitialized__default['default'](_this);
912
+ /* eslint-enable consistent-this */
913
+
914
+
915
+ var uris = [];
916
+ var currentUri = {}; // if specified, the active EXT-X-MAP definition
917
+
918
+ var currentMap; // if specified, the active decryption key
919
+
920
+ var _key;
921
+
922
+ var hasParts = false;
923
+
924
+ var noop = function noop() {};
925
+
926
+ var defaultMediaGroups = {
927
+ 'AUDIO': {},
928
+ 'VIDEO': {},
929
+ 'CLOSED-CAPTIONS': {},
930
+ 'SUBTITLES': {}
931
+ }; // This is the Widevine UUID from DASH IF IOP. The same exact string is
932
+ // used in MPDs with Widevine encrypted streams.
933
+
934
+ var widevineUuid = 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed'; // group segments into numbered timelines delineated by discontinuities
935
+
936
+ var currentTimeline = 0; // the manifest is empty until the parse stream begins delivering data
937
+
938
+ _this.manifest = {
939
+ allowCache: true,
940
+ discontinuityStarts: [],
941
+ segments: []
942
+ }; // keep track of the last seen segment's byte range end, as segments are not required
943
+ // to provide the offset, in which case it defaults to the next byte after the
944
+ // previous segment
945
+
946
+ var lastByterangeEnd = 0; // keep track of the last seen part's byte range end.
947
+
948
+ var lastPartByterangeEnd = 0; // track where next segment starts
949
+
950
+ var nextSegmentLineNumberStart = 0;
951
+
952
+ _this.on('end', function () {
953
+ // only add preloadSegment if we don't yet have a uri for it.
954
+ // and we actually have parts/preloadHints
955
+ if (currentUri.uri || !currentUri.parts && !currentUri.preloadHints) {
956
+ return;
957
+ }
958
+
959
+ if (!currentUri.map && currentMap) {
960
+ currentUri.map = currentMap;
961
+ }
962
+
963
+ if (!currentUri.key && _key) {
964
+ currentUri.key = _key;
965
+ }
966
+
967
+ if (!currentUri.timeline && typeof currentTimeline === 'number') {
968
+ currentUri.timeline = currentTimeline;
969
+ }
970
+
971
+ _this.manifest.preloadSegment = currentUri;
972
+ }); // update the manifest with the m3u8 entry from the parse stream
973
+
974
+
975
+ _this.parseStream.on('data', function (entry) {
976
+ var mediaGroup;
977
+ var rendition; // starting a new segment
978
+
979
+ if (!Object.keys(currentUri).length) {
980
+ nextSegmentLineNumberStart = this.lineNumber;
981
+ }
982
+
983
+ ({
984
+ tag: function tag() {
985
+ // switch based on the tag type
986
+ (({
987
+ version: function version() {
988
+ if (entry.version) {
989
+ this.manifest.version = entry.version;
990
+ }
991
+ },
992
+ 'allow-cache': function allowCache() {
993
+ this.manifest.allowCache = entry.allowed;
994
+
995
+ if (!('allowed' in entry)) {
996
+ this.trigger('info', {
997
+ message: 'defaulting allowCache to YES'
998
+ });
999
+ this.manifest.allowCache = true;
1000
+ }
1001
+ },
1002
+ byterange: function byterange() {
1003
+ var byterange = {};
1004
+
1005
+ if ('length' in entry) {
1006
+ currentUri.byterange = byterange;
1007
+ byterange.length = entry.length;
1008
+
1009
+ if (!('offset' in entry)) {
1010
+ /*
1011
+ * From the latest spec (as of this writing):
1012
+ * https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.2
1013
+ *
1014
+ * Same text since EXT-X-BYTERANGE's introduction in draft 7:
1015
+ * https://tools.ietf.org/html/draft-pantos-http-live-streaming-07#section-3.3.1)
1016
+ *
1017
+ * "If o [offset] is not present, the sub-range begins at the next byte
1018
+ * following the sub-range of the previous media segment."
1019
+ */
1020
+ entry.offset = lastByterangeEnd;
1021
+ }
1022
+ }
1023
+
1024
+ if ('offset' in entry) {
1025
+ currentUri.byterange = byterange;
1026
+ byterange.offset = entry.offset;
1027
+ }
1028
+
1029
+ lastByterangeEnd = byterange.offset + byterange.length;
1030
+ },
1031
+ endlist: function endlist() {
1032
+ this.manifest.endList = true;
1033
+ },
1034
+ inf: function inf() {
1035
+ if (!('mediaSequence' in this.manifest)) {
1036
+ this.manifest.mediaSequence = 0;
1037
+ this.trigger('info', {
1038
+ message: 'defaulting media sequence to zero'
1039
+ });
1040
+ }
1041
+
1042
+ if (!('discontinuitySequence' in this.manifest)) {
1043
+ this.manifest.discontinuitySequence = 0;
1044
+ this.trigger('info', {
1045
+ message: 'defaulting discontinuity sequence to zero'
1046
+ });
1047
+ }
1048
+
1049
+ if (entry.duration > 0) {
1050
+ currentUri.duration = entry.duration;
1051
+ }
1052
+
1053
+ if (entry.duration === 0) {
1054
+ currentUri.duration = 0.01;
1055
+ this.trigger('info', {
1056
+ message: 'updating zero segment duration to a small value'
1057
+ });
1058
+ }
1059
+
1060
+ this.manifest.segments = uris;
1061
+ },
1062
+ key: function key() {
1063
+ if (!entry.attributes) {
1064
+ this.trigger('warn', {
1065
+ message: 'ignoring key declaration without attribute list'
1066
+ });
1067
+ return;
1068
+ } // clear the active encryption key
1069
+
1070
+
1071
+ if (entry.attributes.METHOD === 'NONE') {
1072
+ _key = null;
1073
+ return;
1074
+ }
1075
+
1076
+ if (!entry.attributes.URI) {
1077
+ this.trigger('warn', {
1078
+ message: 'ignoring key declaration without URI'
1079
+ });
1080
+ return;
1081
+ }
1082
+
1083
+ if (entry.attributes.KEYFORMAT === 'com.apple.streamingkeydelivery') {
1084
+ this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this.
1085
+
1086
+ this.manifest.contentProtection['com.apple.fps.1_0'] = {
1087
+ attributes: entry.attributes
1088
+ };
1089
+ return;
1090
+ }
1091
+
1092
+ if (entry.attributes.KEYFORMAT === 'com.microsoft.playready') {
1093
+ this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this.
1094
+
1095
+ this.manifest.contentProtection['com.microsoft.playready'] = {
1096
+ uri: entry.attributes.URI
1097
+ };
1098
+ return;
1099
+ } // check if the content is encrypted for Widevine
1100
+ // Widevine/HLS spec: https://storage.googleapis.com/wvdocs/Widevine_DRM_HLS.pdf
1101
+
1102
+
1103
+ if (entry.attributes.KEYFORMAT === widevineUuid) {
1104
+ var VALID_METHODS = ['SAMPLE-AES', 'SAMPLE-AES-CTR', 'SAMPLE-AES-CENC'];
1105
+
1106
+ if (VALID_METHODS.indexOf(entry.attributes.METHOD) === -1) {
1107
+ this.trigger('warn', {
1108
+ message: 'invalid key method provided for Widevine'
1109
+ });
1110
+ return;
1111
+ }
1112
+
1113
+ if (entry.attributes.METHOD === 'SAMPLE-AES-CENC') {
1114
+ this.trigger('warn', {
1115
+ message: 'SAMPLE-AES-CENC is deprecated, please use SAMPLE-AES-CTR instead'
1116
+ });
1117
+ }
1118
+
1119
+ if (entry.attributes.URI.substring(0, 23) !== 'data:text/plain;base64,') {
1120
+ this.trigger('warn', {
1121
+ message: 'invalid key URI provided for Widevine'
1122
+ });
1123
+ return;
1124
+ }
1125
+
1126
+ if (!(entry.attributes.KEYID && entry.attributes.KEYID.substring(0, 2) === '0x')) {
1127
+ this.trigger('warn', {
1128
+ message: 'invalid key ID provided for Widevine'
1129
+ });
1130
+ return;
1131
+ } // if Widevine key attributes are valid, store them as `contentProtection`
1132
+ // on the manifest to emulate Widevine tag structure in a DASH mpd
1133
+
1134
+
1135
+ this.manifest.contentProtection = this.manifest.contentProtection || {};
1136
+ this.manifest.contentProtection['com.widevine.alpha'] = {
1137
+ attributes: {
1138
+ schemeIdUri: entry.attributes.KEYFORMAT,
1139
+ // remove '0x' from the key id string
1140
+ keyId: entry.attributes.KEYID.substring(2)
1141
+ },
1142
+ // decode the base64-encoded PSSH box
1143
+ pssh: decodeB64ToUint8Array__default['default'](entry.attributes.URI.split(',')[1])
1144
+ };
1145
+ return;
1146
+ }
1147
+
1148
+ if (!entry.attributes.METHOD) {
1149
+ this.trigger('warn', {
1150
+ message: 'defaulting key method to AES-128'
1151
+ });
1152
+ } // setup an encryption key for upcoming segments
1153
+
1154
+
1155
+ _key = {
1156
+ method: entry.attributes.METHOD || 'AES-128',
1157
+ uri: entry.attributes.URI
1158
+ };
1159
+
1160
+ if (typeof entry.attributes.IV !== 'undefined') {
1161
+ _key.iv = entry.attributes.IV;
1162
+ }
1163
+ },
1164
+ 'media-sequence': function mediaSequence() {
1165
+ if (!isFinite(entry.number)) {
1166
+ this.trigger('warn', {
1167
+ message: 'ignoring invalid media sequence: ' + entry.number
1168
+ });
1169
+ return;
1170
+ }
1171
+
1172
+ this.manifest.mediaSequence = entry.number;
1173
+ },
1174
+ 'discontinuity-sequence': function discontinuitySequence() {
1175
+ if (!isFinite(entry.number)) {
1176
+ this.trigger('warn', {
1177
+ message: 'ignoring invalid discontinuity sequence: ' + entry.number
1178
+ });
1179
+ return;
1180
+ }
1181
+
1182
+ this.manifest.discontinuitySequence = entry.number;
1183
+ currentTimeline = entry.number;
1184
+ },
1185
+ 'playlist-type': function playlistType() {
1186
+ if (!/VOD|EVENT/.test(entry.playlistType)) {
1187
+ this.trigger('warn', {
1188
+ message: 'ignoring unknown playlist type: ' + entry.playlist
1189
+ });
1190
+ return;
1191
+ }
1192
+
1193
+ this.manifest.playlistType = entry.playlistType;
1194
+ },
1195
+ map: function map() {
1196
+ currentMap = {};
1197
+
1198
+ if (entry.uri) {
1199
+ currentMap.uri = entry.uri;
1200
+ }
1201
+
1202
+ if (entry.byterange) {
1203
+ currentMap.byterange = entry.byterange;
1204
+ }
1205
+
1206
+ if (_key) {
1207
+ currentMap.key = _key;
1208
+ }
1209
+ },
1210
+ 'stream-inf': function streamInf() {
1211
+ this.manifest.playlists = uris;
1212
+ this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
1213
+
1214
+ if (!entry.attributes) {
1215
+ this.trigger('warn', {
1216
+ message: 'ignoring empty stream-inf attributes'
1217
+ });
1218
+ return;
1219
+ }
1220
+
1221
+ if (!currentUri.attributes) {
1222
+ currentUri.attributes = {};
1223
+ }
1224
+
1225
+ _extends__default['default'](currentUri.attributes, entry.attributes);
1226
+ },
1227
+ media: function media() {
1228
+ this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
1229
+
1230
+ if (!(entry.attributes && entry.attributes.TYPE && entry.attributes['GROUP-ID'] && entry.attributes.NAME)) {
1231
+ this.trigger('warn', {
1232
+ message: 'ignoring incomplete or missing media group'
1233
+ });
1234
+ return;
1235
+ } // find the media group, creating defaults as necessary
1236
+
1237
+
1238
+ var mediaGroupType = this.manifest.mediaGroups[entry.attributes.TYPE];
1239
+ mediaGroupType[entry.attributes['GROUP-ID']] = mediaGroupType[entry.attributes['GROUP-ID']] || {};
1240
+ mediaGroup = mediaGroupType[entry.attributes['GROUP-ID']]; // collect the rendition metadata
1241
+
1242
+ rendition = {
1243
+ default: /yes/i.test(entry.attributes.DEFAULT)
1244
+ };
1245
+
1246
+ if (rendition.default) {
1247
+ rendition.autoselect = true;
1248
+ } else {
1249
+ rendition.autoselect = /yes/i.test(entry.attributes.AUTOSELECT);
1250
+ }
1251
+
1252
+ if (entry.attributes.LANGUAGE) {
1253
+ rendition.language = entry.attributes.LANGUAGE;
1254
+ }
1255
+
1256
+ if (entry.attributes.URI) {
1257
+ rendition.uri = entry.attributes.URI;
1258
+ }
1259
+
1260
+ if (entry.attributes['INSTREAM-ID']) {
1261
+ rendition.instreamId = entry.attributes['INSTREAM-ID'];
1262
+ }
1263
+
1264
+ if (entry.attributes.CHARACTERISTICS) {
1265
+ rendition.characteristics = entry.attributes.CHARACTERISTICS;
1266
+ }
1267
+
1268
+ if (entry.attributes.FORCED) {
1269
+ rendition.forced = /yes/i.test(entry.attributes.FORCED);
1270
+ } // insert the new rendition
1271
+
1272
+
1273
+ mediaGroup[entry.attributes.NAME] = rendition;
1274
+ },
1275
+ discontinuity: function discontinuity() {
1276
+ currentTimeline += 1;
1277
+ currentUri.discontinuity = true;
1278
+ this.manifest.discontinuityStarts.push(uris.length);
1279
+ },
1280
+ 'program-date-time': function programDateTime() {
1281
+ if (typeof this.manifest.dateTimeString === 'undefined') {
1282
+ // PROGRAM-DATE-TIME is a media-segment tag, but for backwards
1283
+ // compatibility, we add the first occurence of the PROGRAM-DATE-TIME tag
1284
+ // to the manifest object
1285
+ // TODO: Consider removing this in future major version
1286
+ this.manifest.dateTimeString = entry.dateTimeString;
1287
+ this.manifest.dateTimeObject = entry.dateTimeObject;
1288
+ }
1289
+
1290
+ currentUri.dateTimeString = entry.dateTimeString;
1291
+ currentUri.dateTimeObject = entry.dateTimeObject;
1292
+ },
1293
+ targetduration: function targetduration() {
1294
+ if (!isFinite(entry.duration) || entry.duration < 0) {
1295
+ this.trigger('warn', {
1296
+ message: 'ignoring invalid target duration: ' + entry.duration
1297
+ });
1298
+ return;
1299
+ }
1300
+
1301
+ this.manifest.targetDuration = entry.duration;
1302
+ setHoldBack.call(this, this.manifest);
1303
+ },
1304
+ start: function start() {
1305
+ if (!entry.attributes || isNaN(entry.attributes['TIME-OFFSET'])) {
1306
+ this.trigger('warn', {
1307
+ message: 'ignoring start declaration without appropriate attribute list'
1308
+ });
1309
+ return;
1310
+ }
1311
+
1312
+ this.manifest.start = {
1313
+ timeOffset: entry.attributes['TIME-OFFSET'],
1314
+ precise: entry.attributes.PRECISE
1315
+ };
1316
+ },
1317
+ 'cue-out': function cueOut() {
1318
+ currentUri.cueOut = entry.data;
1319
+ },
1320
+ 'cue-out-cont': function cueOutCont() {
1321
+ currentUri.cueOutCont = entry.data;
1322
+ },
1323
+ 'cue-in': function cueIn() {
1324
+ currentUri.cueIn = entry.data;
1325
+ },
1326
+ 'skip': function skip() {
1327
+ this.manifest.skip = camelCaseKeys(entry.attributes);
1328
+ this.warnOnMissingAttributes_('#EXT-X-SKIP', entry.attributes, ['SKIPPED-SEGMENTS']);
1329
+ },
1330
+ 'part': function part() {
1331
+ var _this2 = this;
1332
+
1333
+ hasParts = true; // parts are always specifed before a segment
1334
+
1335
+ var segmentIndex = this.manifest.segments.length;
1336
+ var part = camelCaseKeys(entry.attributes);
1337
+ currentUri.parts = currentUri.parts || [];
1338
+ currentUri.parts.push(part);
1339
+
1340
+ if (part.byterange) {
1341
+ if (!part.byterange.hasOwnProperty('offset')) {
1342
+ part.byterange.offset = lastPartByterangeEnd;
1343
+ }
1344
+
1345
+ lastPartByterangeEnd = part.byterange.offset + part.byterange.length;
1346
+ }
1347
+
1348
+ var partIndex = currentUri.parts.length - 1;
1349
+ this.warnOnMissingAttributes_("#EXT-X-PART #" + partIndex + " for segment #" + segmentIndex, entry.attributes, ['URI', 'DURATION']);
1350
+
1351
+ if (this.manifest.renditionReports) {
1352
+ this.manifest.renditionReports.forEach(function (r, i) {
1353
+ if (!r.hasOwnProperty('lastPart')) {
1354
+ _this2.trigger('warn', {
1355
+ message: "#EXT-X-RENDITION-REPORT #" + i + " lacks required attribute(s): LAST-PART"
1356
+ });
1357
+ }
1358
+ });
1359
+ }
1360
+ },
1361
+ 'server-control': function serverControl() {
1362
+ var attrs = this.manifest.serverControl = camelCaseKeys(entry.attributes);
1363
+
1364
+ if (!attrs.hasOwnProperty('canBlockReload')) {
1365
+ attrs.canBlockReload = false;
1366
+ this.trigger('info', {
1367
+ message: '#EXT-X-SERVER-CONTROL defaulting CAN-BLOCK-RELOAD to false'
1368
+ });
1369
+ }
1370
+
1371
+ setHoldBack.call(this, this.manifest);
1372
+
1373
+ if (attrs.canSkipDateranges && !attrs.hasOwnProperty('canSkipUntil')) {
1374
+ this.trigger('warn', {
1375
+ message: '#EXT-X-SERVER-CONTROL lacks required attribute CAN-SKIP-UNTIL which is required when CAN-SKIP-DATERANGES is set'
1376
+ });
1377
+ }
1378
+ },
1379
+ 'preload-hint': function preloadHint() {
1380
+ // parts are always specifed before a segment
1381
+ var segmentIndex = this.manifest.segments.length;
1382
+ var hint = camelCaseKeys(entry.attributes);
1383
+ var isPart = hint.type && hint.type === 'PART';
1384
+ currentUri.preloadHints = currentUri.preloadHints || [];
1385
+ currentUri.preloadHints.push(hint);
1386
+
1387
+ if (hint.byterange) {
1388
+ if (!hint.byterange.hasOwnProperty('offset')) {
1389
+ // use last part byterange end or zero if not a part.
1390
+ hint.byterange.offset = isPart ? lastPartByterangeEnd : 0;
1391
+
1392
+ if (isPart) {
1393
+ lastPartByterangeEnd = hint.byterange.offset + hint.byterange.length;
1394
+ }
1395
+ }
1396
+ }
1397
+
1398
+ var index = currentUri.preloadHints.length - 1;
1399
+ this.warnOnMissingAttributes_("#EXT-X-PRELOAD-HINT #" + index + " for segment #" + segmentIndex, entry.attributes, ['TYPE', 'URI']);
1400
+
1401
+ if (!hint.type) {
1402
+ return;
1403
+ } // search through all preload hints except for the current one for
1404
+ // a duplicate type.
1405
+
1406
+
1407
+ for (var i = 0; i < currentUri.preloadHints.length - 1; i++) {
1408
+ var otherHint = currentUri.preloadHints[i];
1409
+
1410
+ if (!otherHint.type) {
1411
+ continue;
1412
+ }
1413
+
1414
+ if (otherHint.type === hint.type) {
1415
+ this.trigger('warn', {
1416
+ message: "#EXT-X-PRELOAD-HINT #" + index + " for segment #" + segmentIndex + " has the same TYPE " + hint.type + " as preload hint #" + i
1417
+ });
1418
+ }
1419
+ }
1420
+ },
1421
+ 'rendition-report': function renditionReport() {
1422
+ var report = camelCaseKeys(entry.attributes);
1423
+ this.manifest.renditionReports = this.manifest.renditionReports || [];
1424
+ this.manifest.renditionReports.push(report);
1425
+ var index = this.manifest.renditionReports.length - 1;
1426
+ var required = ['LAST-MSN', 'URI'];
1427
+
1428
+ if (hasParts) {
1429
+ required.push('LAST-PART');
1430
+ }
1431
+
1432
+ this.warnOnMissingAttributes_("#EXT-X-RENDITION-REPORT #" + index, entry.attributes, required);
1433
+ },
1434
+ 'part-inf': function partInf() {
1435
+ this.manifest.partInf = camelCaseKeys(entry.attributes);
1436
+ this.warnOnMissingAttributes_('#EXT-X-PART-INF', entry.attributes, ['PART-TARGET']);
1437
+
1438
+ if (this.manifest.partInf.partTarget) {
1439
+ this.manifest.partTargetDuration = this.manifest.partInf.partTarget;
1440
+ }
1441
+
1442
+ setHoldBack.call(this, this.manifest);
1443
+ }
1444
+ })[entry.tagType] || noop).call(self);
1445
+ },
1446
+ uri: function uri() {
1447
+ currentUri.uri = entry.uri;
1448
+ currentUri.lineNumberStart = nextSegmentLineNumberStart;
1449
+ currentUri.lineNumberEnd = this.parseStream.lineNumber;
1450
+ uris.push(currentUri); // if no explicit duration was declared, use the target duration
1451
+
1452
+ if (this.manifest.targetDuration && !('duration' in currentUri)) {
1453
+ this.trigger('warn', {
1454
+ message: 'defaulting segment duration to the target duration'
1455
+ });
1456
+ currentUri.duration = this.manifest.targetDuration;
1457
+ } // annotate with encryption information, if necessary
1458
+
1459
+
1460
+ if (_key) {
1461
+ currentUri.key = _key;
1462
+ }
1463
+
1464
+ currentUri.timeline = currentTimeline; // annotate with initialization segment information, if necessary
1465
+
1466
+ if (currentMap) {
1467
+ currentUri.map = currentMap;
1468
+ } // reset the last byterange end as it needs to be 0 between parts
1469
+
1470
+
1471
+ lastPartByterangeEnd = 0; // prepare for the next URI
1472
+
1473
+ currentUri = {};
1474
+ },
1475
+ comment: function comment() {// comments are not important for playback
1476
+ },
1477
+ custom: function custom() {
1478
+ // if this is segment-level data attach the output to the segment
1479
+ if (entry.segment) {
1480
+ currentUri.custom = currentUri.custom || {};
1481
+ currentUri.custom[entry.customType] = entry.data; // if this is manifest-level data attach to the top level manifest object
1482
+ } else {
1483
+ this.manifest.custom = this.manifest.custom || {};
1484
+ this.manifest.custom[entry.customType] = entry.data;
1485
+ }
1486
+ }
1487
+ })[entry.type].call(self);
1488
+ });
1489
+
1490
+ return _this;
1491
+ }
1492
+
1493
+ var _proto = Parser.prototype;
1494
+
1495
+ _proto.warnOnMissingAttributes_ = function warnOnMissingAttributes_(identifier, attributes, required) {
1496
+ var missing = [];
1497
+ required.forEach(function (key) {
1498
+ if (!attributes.hasOwnProperty(key)) {
1499
+ missing.push(key);
1500
+ }
1501
+ });
1502
+
1503
+ if (missing.length) {
1504
+ this.trigger('warn', {
1505
+ message: identifier + " lacks required attribute(s): " + missing.join(', ')
1506
+ });
1507
+ }
1508
+ }
1509
+ /**
1510
+ * Parse the input string and update the manifest object.
1511
+ *
1512
+ * @param {string} chunk a potentially incomplete portion of the manifest
1513
+ */
1514
+ ;
1515
+
1516
+ _proto.push = function push(chunk) {
1517
+ this.lineStream.push(chunk);
1518
+ }
1519
+ /**
1520
+ * Flush any remaining input. This can be handy if the last line of an M3U8
1521
+ * manifest did not contain a trailing newline but the file has been
1522
+ * completely received.
1523
+ */
1524
+ ;
1525
+
1526
+ _proto.end = function end() {
1527
+ // flush any buffered input
1528
+ this.lineStream.push('\n');
1529
+ this.trigger('end');
1530
+ }
1531
+ /**
1532
+ * Add an additional parser for non-standard tags
1533
+ *
1534
+ * @param {Object} options a map of options for the added parser
1535
+ * @param {RegExp} options.expression a regular expression to match the custom header
1536
+ * @param {string} options.type the type to register to the output
1537
+ * @param {Function} [options.dataParser] function to parse the line into an object
1538
+ * @param {boolean} [options.segment] should tag data be attached to the segment object
1539
+ */
1540
+ ;
1541
+
1542
+ _proto.addParser = function addParser(options) {
1543
+ this.parseStream.addParser(options);
1544
+ }
1545
+ /**
1546
+ * Add a custom header mapper
1547
+ *
1548
+ * @param {Object} options
1549
+ * @param {RegExp} options.expression a regular expression to match the custom header
1550
+ * @param {Function} options.map function to translate tag into a different tag
1551
+ */
1552
+ ;
1553
+
1554
+ _proto.addTagMapper = function addTagMapper(options) {
1555
+ this.parseStream.addTagMapper(options);
1556
+ };
1557
+
1558
+ return Parser;
1559
+ }(Stream__default['default']);
1560
+
1561
+ exports.LineStream = LineStream;
1562
+ exports.ParseStream = ParseStream;
1563
+ exports.Parser = Parser;