m3u8-parser.js 58 KB

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