1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084 |
- /**
- * m3u8-parser
- * @version 2.1.0
- * @copyright 2017 Brightcove, Inc
- * @license Apache-2.0
- */
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.m3u8Parser = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _stream = require('./stream');
- var _stream2 = _interopRequireDefault(_stream);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
- * @file m3u8/line-stream.js
- */
- /**
- * A stream that buffers string input and generates a `data` event for each
- * line.
- *
- * @class LineStream
- * @extends Stream
- */
- var LineStream = function (_Stream) {
- _inherits(LineStream, _Stream);
- function LineStream() {
- _classCallCheck(this, LineStream);
- var _this = _possibleConstructorReturn(this, (LineStream.__proto__ || Object.getPrototypeOf(LineStream)).call(this));
- _this.buffer = '';
- return _this;
- }
- /**
- * Add new data to be parsed.
- *
- * @param {String} data the text to process
- */
- _createClass(LineStream, [{
- key: 'push',
- value: function push(data) {
- var nextNewline = void 0;
- this.buffer += data;
- nextNewline = this.buffer.indexOf('\n');
- for (; nextNewline > -1; nextNewline = this.buffer.indexOf('\n')) {
- this.trigger('data', this.buffer.substring(0, nextNewline));
- this.buffer = this.buffer.substring(nextNewline + 1);
- }
- }
- }]);
- return LineStream;
- }(_stream2['default']);
- exports['default'] = LineStream;
- },{"./stream":4}],2:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _stream = require('./stream');
- var _stream2 = _interopRequireDefault(_stream);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
- * @file m3u8/parse-stream.js
- */
- /**
- * "forgiving" attribute list psuedo-grammar:
- * attributes -> keyvalue (',' keyvalue)*
- * keyvalue -> key '=' value
- * key -> [^=]*
- * value -> '"' [^"]* '"' | [^,]*
- */
- var attributeSeparator = function attributeSeparator() {
- var key = '[^=]*';
- var value = '"[^"]*"|[^,]*';
- var keyvalue = '(?:' + key + ')=(?:' + value + ')';
- return new RegExp('(?:^|,)(' + keyvalue + ')');
- };
- /**
- * Parse attributes from a line given the seperator
- *
- * @param {String} attributes the attibute line to parse
- */
- var parseAttributes = function parseAttributes(attributes) {
- // split the string using attributes as the separator
- var attrs = attributes.split(attributeSeparator());
- var result = {};
- var i = attrs.length;
- var attr = void 0;
- while (i--) {
- // filter out unmatched portions of the string
- if (attrs[i] === '') {
- continue;
- }
- // split the key and value
- attr = /([^=]*)=(.*)/.exec(attrs[i]).slice(1);
- // trim whitespace and remove optional quotes around the value
- attr[0] = attr[0].replace(/^\s+|\s+$/g, '');
- attr[1] = attr[1].replace(/^\s+|\s+$/g, '');
- attr[1] = attr[1].replace(/^['"](.*)['"]$/g, '$1');
- result[attr[0]] = attr[1];
- }
- return result;
- };
- /**
- * A line-level M3U8 parser event stream. It expects to receive input one
- * line at a time and performs a context-free parse of its contents. A stream
- * interpretation of a manifest can be useful if the manifest is expected to
- * be too large to fit comfortably into memory or the entirety of the input
- * is not immediately available. Otherwise, it's probably much easier to work
- * with a regular `Parser` object.
- *
- * Produces `data` events with an object that captures the parser's
- * interpretation of the input. That object has a property `tag` that is one
- * of `uri`, `comment`, or `tag`. URIs only have a single additional
- * property, `line`, which captures the entirety of the input without
- * interpretation. Comments similarly have a single additional property
- * `text` which is the input without the leading `#`.
- *
- * Tags always have a property `tagType` which is the lower-cased version of
- * the M3U8 directive without the `#EXT` or `#EXT-X-` prefix. For instance,
- * `#EXT-X-MEDIA-SEQUENCE` becomes `media-sequence` when parsed. Unrecognized
- * tags are given the tag type `unknown` and a single additional property
- * `data` with the remainder of the input.
- *
- * @class ParseStream
- * @extends Stream
- */
- var ParseStream = function (_Stream) {
- _inherits(ParseStream, _Stream);
- function ParseStream() {
- _classCallCheck(this, ParseStream);
- return _possibleConstructorReturn(this, (ParseStream.__proto__ || Object.getPrototypeOf(ParseStream)).call(this));
- }
- /**
- * Parses an additional line of input.
- *
- * @param {String} line a single line of an M3U8 file to parse
- */
- _createClass(ParseStream, [{
- key: 'push',
- value: function push(line) {
- var match = void 0;
- var event = void 0;
- // strip whitespace
- line = line.replace(/^[\u0000\s]+|[\u0000\s]+$/g, '');
- if (line.length === 0) {
- // ignore empty lines
- return;
- }
- // URIs
- if (line[0] !== '#') {
- this.trigger('data', {
- type: 'uri',
- uri: line
- });
- return;
- }
- // Comments
- if (line.indexOf('#EXT') !== 0) {
- this.trigger('data', {
- type: 'comment',
- text: line.slice(1)
- });
- return;
- }
- // strip off any carriage returns here so the regex matching
- // doesn't have to account for them.
- line = line.replace('\r', '');
- // Tags
- match = /^#EXTM3U/.exec(line);
- if (match) {
- this.trigger('data', {
- type: 'tag',
- tagType: 'm3u'
- });
- return;
- }
- match = /^#EXTINF:?([0-9\.]*)?,?(.*)?$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'inf'
- };
- if (match[1]) {
- event.duration = parseFloat(match[1]);
- }
- if (match[2]) {
- event.title = match[2];
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-TARGETDURATION:?([0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'targetduration'
- };
- if (match[1]) {
- event.duration = parseInt(match[1], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#ZEN-TOTAL-DURATION:?([0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'totalduration'
- };
- if (match[1]) {
- event.duration = parseInt(match[1], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-VERSION:?([0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'version'
- };
- if (match[1]) {
- event.version = parseInt(match[1], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-MEDIA-SEQUENCE:?(\-?[0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'media-sequence'
- };
- if (match[1]) {
- event.number = parseInt(match[1], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-DISCONTINUITY-SEQUENCE:?(\-?[0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'discontinuity-sequence'
- };
- if (match[1]) {
- event.number = parseInt(match[1], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-PLAYLIST-TYPE:?(.*)?$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'playlist-type'
- };
- if (match[1]) {
- event.playlistType = match[1];
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-BYTERANGE:?([0-9.]*)?@?([0-9.]*)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'byterange'
- };
- if (match[1]) {
- event.length = parseInt(match[1], 10);
- }
- if (match[2]) {
- event.offset = parseInt(match[2], 10);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-ALLOW-CACHE:?(YES|NO)?/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'allow-cache'
- };
- if (match[1]) {
- event.allowed = !/NO/.test(match[1]);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-MAP:?(.*)$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'map'
- };
- if (match[1]) {
- var attributes = parseAttributes(match[1]);
- if (attributes.URI) {
- event.uri = attributes.URI;
- }
- if (attributes.BYTERANGE) {
- var _attributes$BYTERANGE = attributes.BYTERANGE.split('@'),
- _attributes$BYTERANGE2 = _slicedToArray(_attributes$BYTERANGE, 2),
- length = _attributes$BYTERANGE2[0],
- offset = _attributes$BYTERANGE2[1];
- event.byterange = {};
- if (length) {
- event.byterange.length = parseInt(length, 10);
- }
- if (offset) {
- event.byterange.offset = parseInt(offset, 10);
- }
- }
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-STREAM-INF:?(.*)$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'stream-inf'
- };
- if (match[1]) {
- event.attributes = parseAttributes(match[1]);
- if (event.attributes.RESOLUTION) {
- var split = event.attributes.RESOLUTION.split('x');
- var resolution = {};
- if (split[0]) {
- resolution.width = parseInt(split[0], 10);
- }
- if (split[1]) {
- resolution.height = parseInt(split[1], 10);
- }
- event.attributes.RESOLUTION = resolution;
- }
- if (event.attributes.BANDWIDTH) {
- event.attributes.BANDWIDTH = parseInt(event.attributes.BANDWIDTH, 10);
- }
- if (event.attributes['PROGRAM-ID']) {
- event.attributes['PROGRAM-ID'] = parseInt(event.attributes['PROGRAM-ID'], 10);
- }
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-MEDIA:?(.*)$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'media'
- };
- if (match[1]) {
- event.attributes = parseAttributes(match[1]);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-ENDLIST/.exec(line);
- if (match) {
- this.trigger('data', {
- type: 'tag',
- tagType: 'endlist'
- });
- return;
- }
- match = /^#EXT-X-DISCONTINUITY/.exec(line);
- if (match) {
- this.trigger('data', {
- type: 'tag',
- tagType: 'discontinuity'
- });
- return;
- }
- match = /^#EXT-X-PROGRAM-DATE-TIME:?(.*)$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'program-date-time'
- };
- if (match[1]) {
- event.dateTimeString = match[1];
- event.dateTimeObject = new Date(match[1]);
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-KEY:?(.*)$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'key'
- };
- if (match[1]) {
- event.attributes = parseAttributes(match[1]);
- // parse the IV string into a Uint32Array
- if (event.attributes.IV) {
- if (event.attributes.IV.substring(0, 2).toLowerCase() === '0x') {
- event.attributes.IV = event.attributes.IV.substring(2);
- }
- event.attributes.IV = event.attributes.IV.match(/.{8}/g);
- event.attributes.IV[0] = parseInt(event.attributes.IV[0], 16);
- event.attributes.IV[1] = parseInt(event.attributes.IV[1], 16);
- event.attributes.IV[2] = parseInt(event.attributes.IV[2], 16);
- event.attributes.IV[3] = parseInt(event.attributes.IV[3], 16);
- event.attributes.IV = new Uint32Array(event.attributes.IV);
- }
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-CUE-OUT-CONT:?(.*)?$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'cue-out-cont'
- };
- if (match[1]) {
- event.data = match[1];
- } else {
- event.data = '';
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-CUE-OUT:?(.*)?$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'cue-out'
- };
- if (match[1]) {
- event.data = match[1];
- } else {
- event.data = '';
- }
- this.trigger('data', event);
- return;
- }
- match = /^#EXT-X-CUE-IN:?(.*)?$/.exec(line);
- if (match) {
- event = {
- type: 'tag',
- tagType: 'cue-in'
- };
- if (match[1]) {
- event.data = match[1];
- } else {
- event.data = '';
- }
- this.trigger('data', event);
- return;
- }
- // unknown tag type
- this.trigger('data', {
- type: 'tag',
- data: line.slice(4)
- });
- }
- }]);
- return ParseStream;
- }(_stream2['default']);
- exports['default'] = ParseStream;
- },{"./stream":4}],3:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _stream = require('./stream');
- var _stream2 = _interopRequireDefault(_stream);
- var _lineStream = require('./line-stream');
- var _lineStream2 = _interopRequireDefault(_lineStream);
- var _parseStream = require('./parse-stream');
- var _parseStream2 = _interopRequireDefault(_parseStream);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
- * @file m3u8/parser.js
- */
- /**
- * A parser for M3U8 files. The current interpretation of the input is
- * exposed as a property `manifest` on parser objects. It's just two lines to
- * create and parse a manifest once you have the contents available as a string:
- *
- * ```js
- * var parser = new m3u8.Parser();
- * parser.push(xhr.responseText);
- * ```
- *
- * New input can later be applied to update the manifest object by calling
- * `push` again.
- *
- * The parser attempts to create a usable manifest object even if the
- * underlying input is somewhat nonsensical. It emits `info` and `warning`
- * events during the parse if it encounters input that seems invalid or
- * requires some property of the manifest object to be defaulted.
- *
- * @class Parser
- * @extends Stream
- */
- var Parser = function (_Stream) {
- _inherits(Parser, _Stream);
- function Parser() {
- _classCallCheck(this, Parser);
- var _this = _possibleConstructorReturn(this, (Parser.__proto__ || Object.getPrototypeOf(Parser)).call(this));
- _this.lineStream = new _lineStream2['default']();
- _this.parseStream = new _parseStream2['default']();
- _this.lineStream.pipe(_this.parseStream);
- /* eslint-disable consistent-this */
- var self = _this;
- /* eslint-enable consistent-this */
- var uris = [];
- var currentUri = {};
- // if specified, the active EXT-X-MAP definition
- var currentMap = void 0;
- // if specified, the active decryption key
- var _key = void 0;
- var noop = function noop() {};
- var defaultMediaGroups = {
- 'AUDIO': {},
- 'VIDEO': {},
- 'CLOSED-CAPTIONS': {},
- 'SUBTITLES': {}
- };
- // group segments into numbered timelines delineated by discontinuities
- var currentTimeline = 0;
- // the manifest is empty until the parse stream begins delivering data
- _this.manifest = {
- allowCache: true,
- discontinuityStarts: [],
- segments: []
- };
- // update the manifest with the m3u8 entry from the parse stream
- _this.parseStream.on('data', function (entry) {
- var mediaGroup = void 0;
- var rendition = void 0;
- ({
- tag: function tag() {
- // switch based on the tag type
- (({
- 'allow-cache': function allowCache() {
- this.manifest.allowCache = entry.allowed;
- if (!('allowed' in entry)) {
- this.trigger('info', {
- message: 'defaulting allowCache to YES'
- });
- this.manifest.allowCache = true;
- }
- },
- byterange: function byterange() {
- var byterange = {};
- if ('length' in entry) {
- currentUri.byterange = byterange;
- byterange.length = entry.length;
- if (!('offset' in entry)) {
- this.trigger('info', {
- message: 'defaulting offset to zero'
- });
- entry.offset = 0;
- }
- }
- if ('offset' in entry) {
- currentUri.byterange = byterange;
- byterange.offset = entry.offset;
- }
- },
- endlist: function endlist() {
- this.manifest.endList = true;
- },
- inf: function inf() {
- if (!('mediaSequence' in this.manifest)) {
- this.manifest.mediaSequence = 0;
- this.trigger('info', {
- message: 'defaulting media sequence to zero'
- });
- }
- if (!('discontinuitySequence' in this.manifest)) {
- this.manifest.discontinuitySequence = 0;
- this.trigger('info', {
- message: 'defaulting discontinuity sequence to zero'
- });
- }
- if (entry.duration > 0) {
- currentUri.duration = entry.duration;
- }
- if (entry.duration === 0) {
- currentUri.duration = 0.01;
- this.trigger('info', {
- message: 'updating zero segment duration to a small value'
- });
- }
- this.manifest.segments = uris;
- },
- key: function key() {
- if (!entry.attributes) {
- this.trigger('warn', {
- message: 'ignoring key declaration without attribute list'
- });
- return;
- }
- // clear the active encryption key
- if (entry.attributes.METHOD === 'NONE') {
- _key = null;
- return;
- }
- if (!entry.attributes.URI) {
- this.trigger('warn', {
- message: 'ignoring key declaration without URI'
- });
- return;
- }
- if (!entry.attributes.METHOD) {
- this.trigger('warn', {
- message: 'defaulting key method to AES-128'
- });
- }
- // setup an encryption key for upcoming segments
- _key = {
- method: entry.attributes.METHOD || 'AES-128',
- uri: entry.attributes.URI
- };
- if (typeof entry.attributes.IV !== 'undefined') {
- _key.iv = entry.attributes.IV;
- }
- },
- 'media-sequence': function mediaSequence() {
- if (!isFinite(entry.number)) {
- this.trigger('warn', {
- message: 'ignoring invalid media sequence: ' + entry.number
- });
- return;
- }
- this.manifest.mediaSequence = entry.number;
- },
- 'discontinuity-sequence': function discontinuitySequence() {
- if (!isFinite(entry.number)) {
- this.trigger('warn', {
- message: 'ignoring invalid discontinuity sequence: ' + entry.number
- });
- return;
- }
- this.manifest.discontinuitySequence = entry.number;
- currentTimeline = entry.number;
- },
- 'playlist-type': function playlistType() {
- if (!/VOD|EVENT/.test(entry.playlistType)) {
- this.trigger('warn', {
- message: 'ignoring unknown playlist type: ' + entry.playlist
- });
- return;
- }
- this.manifest.playlistType = entry.playlistType;
- },
- map: function map() {
- currentMap = {};
- if (entry.uri) {
- currentMap.uri = entry.uri;
- }
- if (entry.byterange) {
- currentMap.byterange = entry.byterange;
- }
- },
- 'stream-inf': function streamInf() {
- this.manifest.playlists = uris;
- this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
- if (!entry.attributes) {
- this.trigger('warn', {
- message: 'ignoring empty stream-inf attributes'
- });
- return;
- }
- if (!currentUri.attributes) {
- currentUri.attributes = {};
- }
- _extends(currentUri.attributes, entry.attributes);
- },
- media: function media() {
- this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
- if (!(entry.attributes && entry.attributes.TYPE && entry.attributes['GROUP-ID'] && entry.attributes.NAME)) {
- this.trigger('warn', {
- message: 'ignoring incomplete or missing media group'
- });
- return;
- }
- // find the media group, creating defaults as necessary
- var mediaGroupType = this.manifest.mediaGroups[entry.attributes.TYPE];
- mediaGroupType[entry.attributes['GROUP-ID']] = mediaGroupType[entry.attributes['GROUP-ID']] || {};
- mediaGroup = mediaGroupType[entry.attributes['GROUP-ID']];
- // collect the rendition metadata
- rendition = {
- 'default': /yes/i.test(entry.attributes.DEFAULT)
- };
- if (rendition['default']) {
- rendition.autoselect = true;
- } else {
- rendition.autoselect = /yes/i.test(entry.attributes.AUTOSELECT);
- }
- if (entry.attributes.LANGUAGE) {
- rendition.language = entry.attributes.LANGUAGE;
- }
- if (entry.attributes.URI) {
- rendition.uri = entry.attributes.URI;
- }
- if (entry.attributes['INSTREAM-ID']) {
- rendition.instreamId = entry.attributes['INSTREAM-ID'];
- }
- if (entry.attributes.CHARACTERISTICS) {
- rendition.characteristics = entry.attributes.CHARACTERISTICS;
- }
- if (entry.attributes.FORCED) {
- rendition.forced = /yes/i.test(entry.attributes.FORCED);
- }
- // insert the new rendition
- mediaGroup[entry.attributes.NAME] = rendition;
- },
- discontinuity: function discontinuity() {
- currentTimeline += 1;
- currentUri.discontinuity = true;
- this.manifest.discontinuityStarts.push(uris.length);
- },
- 'program-date-time': function programDateTime() {
- this.manifest.dateTimeString = entry.dateTimeString;
- this.manifest.dateTimeObject = entry.dateTimeObject;
- },
- targetduration: function targetduration() {
- if (!isFinite(entry.duration) || entry.duration < 0) {
- this.trigger('warn', {
- message: 'ignoring invalid target duration: ' + entry.duration
- });
- return;
- }
- this.manifest.targetDuration = entry.duration;
- },
- totalduration: function totalduration() {
- if (!isFinite(entry.duration) || entry.duration < 0) {
- this.trigger('warn', {
- message: 'ignoring invalid total duration: ' + entry.duration
- });
- return;
- }
- this.manifest.totalDuration = entry.duration;
- },
- 'cue-out': function cueOut() {
- currentUri.cueOut = entry.data;
- },
- 'cue-out-cont': function cueOutCont() {
- currentUri.cueOutCont = entry.data;
- },
- 'cue-in': function cueIn() {
- currentUri.cueIn = entry.data;
- }
- })[entry.tagType] || noop).call(self);
- },
- uri: function uri() {
- currentUri.uri = entry.uri;
- uris.push(currentUri);
- // if no explicit duration was declared, use the target duration
- if (this.manifest.targetDuration && !('duration' in currentUri)) {
- this.trigger('warn', {
- message: 'defaulting segment duration to the target duration'
- });
- currentUri.duration = this.manifest.targetDuration;
- }
- // annotate with encryption information, if necessary
- if (_key) {
- currentUri.key = _key;
- }
- currentUri.timeline = currentTimeline;
- // annotate with initialization segment information, if necessary
- if (currentMap) {
- currentUri.map = currentMap;
- }
- // prepare for the next URI
- currentUri = {};
- },
- comment: function comment() {
- // comments are not important for playback
- }
- })[entry.type].call(self);
- });
- return _this;
- }
- /**
- * Parse the input string and update the manifest object.
- *
- * @param {String} chunk a potentially incomplete portion of the manifest
- */
- _createClass(Parser, [{
- key: 'push',
- value: function push(chunk) {
- this.lineStream.push(chunk);
- }
- /**
- * Flush any remaining input. This can be handy if the last line of an M3U8
- * manifest did not contain a trailing newline but the file has been
- * completely received.
- */
- }, {
- key: 'end',
- value: function end() {
- // flush any buffered input
- this.lineStream.push('\n');
- }
- }]);
- return Parser;
- }(_stream2['default']);
- exports['default'] = Parser;
- },{"./line-stream":1,"./parse-stream":2,"./stream":4}],4:[function(require,module,exports){
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * @file stream.js
- */
- /**
- * A lightweight readable stream implemention that handles event dispatching.
- *
- * @class Stream
- */
- var Stream = function () {
- function Stream() {
- _classCallCheck(this, Stream);
- this.listeners = {};
- }
- /**
- * Add a listener for a specified event type.
- *
- * @param {String} type the event name
- * @param {Function} listener the callback to be invoked when an event of
- * the specified type occurs
- */
- _createClass(Stream, [{
- key: 'on',
- value: function on(type, listener) {
- if (!this.listeners[type]) {
- this.listeners[type] = [];
- }
- this.listeners[type].push(listener);
- }
- /**
- * Remove a listener for a specified event type.
- *
- * @param {String} type the event name
- * @param {Function} listener a function previously registered for this
- * type of event through `on`
- * @return {Boolean} if we could turn it off or not
- */
- }, {
- key: 'off',
- value: function off(type, listener) {
- if (!this.listeners[type]) {
- return false;
- }
- var index = this.listeners[type].indexOf(listener);
- this.listeners[type].splice(index, 1);
- return index > -1;
- }
- /**
- * Trigger an event of the specified type on this stream. Any additional
- * arguments to this function are passed as parameters to event listeners.
- *
- * @param {String} type the event name
- */
- }, {
- key: 'trigger',
- value: function trigger(type) {
- var callbacks = this.listeners[type];
- var i = void 0;
- var length = void 0;
- var args = void 0;
- if (!callbacks) {
- return;
- }
- // Slicing the arguments on every invocation of this method
- // can add a significant amount of overhead. Avoid the
- // intermediate object creation for the common case of a
- // single callback argument
- if (arguments.length === 2) {
- length = callbacks.length;
- for (i = 0; i < length; ++i) {
- callbacks[i].call(this, arguments[1]);
- }
- } else {
- args = Array.prototype.slice.call(arguments, 1);
- length = callbacks.length;
- for (i = 0; i < length; ++i) {
- callbacks[i].apply(this, args);
- }
- }
- }
- /**
- * Destroys the stream and cleans up.
- */
- }, {
- key: 'dispose',
- value: function dispose() {
- this.listeners = {};
- }
- /**
- * Forwards all `data` events on this stream to the destination stream. The
- * destination stream should provide a method `push` to receive the data
- * events as they arrive.
- *
- * @param {Stream} destination the stream that will receive all `data` events
- * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
- */
- }, {
- key: 'pipe',
- value: function pipe(destination) {
- this.on('data', function (data) {
- destination.push(data);
- });
- }
- }]);
- return Stream;
- }();
- exports['default'] = Stream;
- },{}],5:[function(require,module,exports){
- 'use strict';
- var _lineStream = require('./line-stream');
- var _lineStream2 = _interopRequireDefault(_lineStream);
- var _parseStream = require('./parse-stream');
- var _parseStream2 = _interopRequireDefault(_parseStream);
- var _parser = require('./parser');
- var _parser2 = _interopRequireDefault(_parser);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- module.exports = {
- LineStream: _lineStream2['default'],
- ParseStream: _parseStream2['default'],
- Parser: _parser2['default']
- }; /**
- * @file m3u8/index.js
- *
- * Utilities for parsing M3U8 files. If the entire manifest is available,
- * `Parser` will create an object representation with enough detail for managing
- * playback. `ParseStream` and `LineStream` are lower-level parsing primitives
- * that do not assume the entirety of the manifest is ready and expose a
- * ReadableStream-like interface.
- */
- },{"./line-stream":1,"./parse-stream":2,"./parser":3}]},{},[5])(5)
- });
|