UNPKG

@videojs/http-streaming

Version:

Play back HLS and DASH with Video.js, even where it's not natively supported

1,634 lines (1,295 loc) 1.11 MB
/*! @name @videojs/http-streaming @version 2.14.3 @license Apache-2.0 */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('video.js'), require('@xmldom/xmldom')) : typeof define === 'function' && define.amd ? define(['exports', 'video.js', '@xmldom/xmldom'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.httpStreaming = {}, global.videojs, global.window)); })(this, (function (exports, videojs, xmldom) { 'use strict'; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var videojs__default = /*#__PURE__*/_interopDefaultLegacy(videojs); function createCommonjsModule(fn, basedir, module) { return module = { path: basedir, exports: {}, require: function (path, base) { return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); } }, fn(module, module.exports), module.exports; } function commonjsRequire () { throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); } var assertThisInitialized = createCommonjsModule(function (module) { function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } module.exports = _assertThisInitialized; module.exports["default"] = module.exports, module.exports.__esModule = true; }); var setPrototypeOf = createCommonjsModule(function (module) { function _setPrototypeOf(o, p) { module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; module.exports["default"] = module.exports, module.exports.__esModule = true; return _setPrototypeOf(o, p); } module.exports = _setPrototypeOf; module.exports["default"] = module.exports, module.exports.__esModule = true; }); var inheritsLoose = createCommonjsModule(function (module) { function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; setPrototypeOf(subClass, superClass); } module.exports = _inheritsLoose; module.exports["default"] = module.exports, module.exports.__esModule = true; }); var urlToolkit = createCommonjsModule(function (module, exports) { // see https://tools.ietf.org/html/rfc1808 (function (root) { var URL_REGEX = /^((?:[a-zA-Z0-9+\-.]+:)?)(\/\/[^\/?#]*)?((?:[^\/?#]*\/)*[^;?#]*)?(;[^?#]*)?(\?[^#]*)?(#[^]*)?$/; var FIRST_SEGMENT_REGEX = /^([^\/?#]*)([^]*)$/; var SLASH_DOT_REGEX = /(?:\/|^)\.(?=\/)/g; var SLASH_DOT_DOT_REGEX = /(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g; var URLToolkit = { // If opts.alwaysNormalize is true then the path will always be normalized even when it starts with / or // // E.g // With opts.alwaysNormalize = false (default, spec compliant) // http://a.com/b/cd + /e/f/../g => http://a.com/e/f/../g // With opts.alwaysNormalize = true (not spec compliant) // http://a.com/b/cd + /e/f/../g => http://a.com/e/g buildAbsoluteURL: function buildAbsoluteURL(baseURL, relativeURL, opts) { opts = opts || {}; // remove any remaining space and CRLF baseURL = baseURL.trim(); relativeURL = relativeURL.trim(); if (!relativeURL) { // 2a) If the embedded URL is entirely empty, it inherits the // entire base URL (i.e., is set equal to the base URL) // and we are done. if (!opts.alwaysNormalize) { return baseURL; } var basePartsForNormalise = URLToolkit.parseURL(baseURL); if (!basePartsForNormalise) { throw new Error('Error trying to parse base URL.'); } basePartsForNormalise.path = URLToolkit.normalizePath(basePartsForNormalise.path); return URLToolkit.buildURLFromParts(basePartsForNormalise); } var relativeParts = URLToolkit.parseURL(relativeURL); if (!relativeParts) { throw new Error('Error trying to parse relative URL.'); } if (relativeParts.scheme) { // 2b) If the embedded URL starts with a scheme name, it is // interpreted as an absolute URL and we are done. if (!opts.alwaysNormalize) { return relativeURL; } relativeParts.path = URLToolkit.normalizePath(relativeParts.path); return URLToolkit.buildURLFromParts(relativeParts); } var baseParts = URLToolkit.parseURL(baseURL); if (!baseParts) { throw new Error('Error trying to parse base URL.'); } if (!baseParts.netLoc && baseParts.path && baseParts.path[0] !== '/') { // If netLoc missing and path doesn't start with '/', assume everthing before the first '/' is the netLoc // This causes 'example.com/a' to be handled as '//example.com/a' instead of '/example.com/a' var pathParts = FIRST_SEGMENT_REGEX.exec(baseParts.path); baseParts.netLoc = pathParts[1]; baseParts.path = pathParts[2]; } if (baseParts.netLoc && !baseParts.path) { baseParts.path = '/'; } var builtParts = { // 2c) Otherwise, the embedded URL inherits the scheme of // the base URL. scheme: baseParts.scheme, netLoc: relativeParts.netLoc, path: null, params: relativeParts.params, query: relativeParts.query, fragment: relativeParts.fragment }; if (!relativeParts.netLoc) { // 3) If the embedded URL's <net_loc> is non-empty, we skip to // Step 7. Otherwise, the embedded URL inherits the <net_loc> // (if any) of the base URL. builtParts.netLoc = baseParts.netLoc; // 4) If the embedded URL path is preceded by a slash "/", the // path is not relative and we skip to Step 7. if (relativeParts.path[0] !== '/') { if (!relativeParts.path) { // 5) If the embedded URL path is empty (and not preceded by a // slash), then the embedded URL inherits the base URL path builtParts.path = baseParts.path; // 5a) if the embedded URL's <params> is non-empty, we skip to // step 7; otherwise, it inherits the <params> of the base // URL (if any) and if (!relativeParts.params) { builtParts.params = baseParts.params; // 5b) if the embedded URL's <query> is non-empty, we skip to // step 7; otherwise, it inherits the <query> of the base // URL (if any) and we skip to step 7. if (!relativeParts.query) { builtParts.query = baseParts.query; } } } else { // 6) The last segment of the base URL's path (anything // following the rightmost slash "/", or the entire path if no // slash is present) is removed and the embedded URL's path is // appended in its place. var baseURLPath = baseParts.path; var newPath = baseURLPath.substring(0, baseURLPath.lastIndexOf('/') + 1) + relativeParts.path; builtParts.path = URLToolkit.normalizePath(newPath); } } } if (builtParts.path === null) { builtParts.path = opts.alwaysNormalize ? URLToolkit.normalizePath(relativeParts.path) : relativeParts.path; } return URLToolkit.buildURLFromParts(builtParts); }, parseURL: function parseURL(url) { var parts = URL_REGEX.exec(url); if (!parts) { return null; } return { scheme: parts[1] || '', netLoc: parts[2] || '', path: parts[3] || '', params: parts[4] || '', query: parts[5] || '', fragment: parts[6] || '' }; }, normalizePath: function normalizePath(path) { // The following operations are // then applied, in order, to the new path: // 6a) All occurrences of "./", where "." is a complete path // segment, are removed. // 6b) If the path ends with "." as a complete path segment, // that "." is removed. path = path.split('').reverse().join('').replace(SLASH_DOT_REGEX, ''); // 6c) All occurrences of "<segment>/../", where <segment> is a // complete path segment not equal to "..", are removed. // Removal of these path segments is performed iteratively, // removing the leftmost matching pattern on each iteration, // until no matching pattern remains. // 6d) If the path ends with "<segment>/..", where <segment> is a // complete path segment not equal to "..", that // "<segment>/.." is removed. while (path.length !== (path = path.replace(SLASH_DOT_DOT_REGEX, '')).length) {} return path.split('').reverse().join(''); }, buildURLFromParts: function buildURLFromParts(parts) { return parts.scheme + parts.netLoc + parts.path + parts.params + parts.query + parts.fragment; } }; module.exports = URLToolkit; })(); }); var DEFAULT_LOCATION = 'http://example.com'; var resolveUrl$1 = function resolveUrl(baseUrl, relativeUrl) { // return early if we don't need to resolve if (/^[a-z]+:/i.test(relativeUrl)) { return relativeUrl; } // if baseUrl is a data URI, ignore it and resolve everything relative to window.location if (/^data:/.test(baseUrl)) { baseUrl = window.location && window.location.href || ''; } // IE11 supports URL but not the URL constructor // feature detect the behavior we want var nativeURL = typeof window.URL === 'function'; var protocolLess = /^\/\//.test(baseUrl); // remove location if window.location isn't available (i.e. we're in node) // and if baseUrl isn't an absolute url var removeLocation = !window.location && !/\/\//i.test(baseUrl); // if the base URL is relative then combine with the current location if (nativeURL) { baseUrl = new window.URL(baseUrl, window.location || DEFAULT_LOCATION); } else if (!/\/\//i.test(baseUrl)) { baseUrl = urlToolkit.buildAbsoluteURL(window.location && window.location.href || '', baseUrl); } if (nativeURL) { var newUrl = new URL(relativeUrl, baseUrl); // if we're a protocol-less url, remove the protocol // and if we're location-less, remove the location // otherwise, return the url unmodified if (removeLocation) { return newUrl.href.slice(DEFAULT_LOCATION.length); } else if (protocolLess) { return newUrl.href.slice(newUrl.protocol.length); } return newUrl.href; } return urlToolkit.buildAbsoluteURL(baseUrl, relativeUrl); }; /** * @file resolve-url.js - Handling how URLs are resolved and manipulated */ var resolveUrl = resolveUrl$1; /** * Checks whether xhr request was redirected and returns correct url depending * on `handleManifestRedirects` option * * @api private * * @param {string} url - an url being requested * @param {XMLHttpRequest} req - xhr request result * * @return {string} */ var resolveManifestRedirect = function resolveManifestRedirect(handleManifestRedirect, url, req) { // To understand how the responseURL below is set and generated: // - https://fetch.spec.whatwg.org/#concept-response-url // - https://fetch.spec.whatwg.org/#atomic-http-redirect-handling if (handleManifestRedirect && req && req.responseURL && url !== req.responseURL) { return req.responseURL; } return url; }; var logger = function logger(source) { if (videojs__default["default"].log.debug) { return videojs__default["default"].log.debug.bind(videojs__default["default"], 'VHS:', source + " >"); } return function () {}; }; var _extends_1 = createCommonjsModule(function (module) { function _extends() { module.exports = _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; }; module.exports["default"] = module.exports, module.exports.__esModule = true; return _extends.apply(this, arguments); } module.exports = _extends; module.exports["default"] = module.exports, module.exports.__esModule = true; }); /** * @file stream.js */ /** * A lightweight readable stream implemention that handles event dispatching. * * @class Stream */ var Stream = /*#__PURE__*/function () { function 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 */ var _proto = Stream.prototype; _proto.on = 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 */ ; _proto.off = function off(type, listener) { if (!this.listeners[type]) { return false; } var index = this.listeners[type].indexOf(listener); // TODO: which is better? // In Video.js we slice listener functions // on trigger so that it does not mess up the order // while we loop through. // // Here we slice on off so that the loop in trigger // can continue using it's old reference to loop without // messing up the order. this.listeners[type] = this.listeners[type].slice(0); 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 */ ; _proto.trigger = function trigger(type) { var callbacks = this.listeners[type]; 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) { var length = callbacks.length; for (var i = 0; i < length; ++i) { callbacks[i].call(this, arguments[1]); } } else { var args = Array.prototype.slice.call(arguments, 1); var _length = callbacks.length; for (var _i = 0; _i < _length; ++_i) { callbacks[_i].apply(this, args); } } } /** * Destroys the stream and cleans up. */ ; _proto.dispose = 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 */ ; _proto.pipe = function pipe(destination) { this.on('data', function (data) { destination.push(data); }); }; return Stream; }(); var atob = function atob(s) { return window.atob ? window.atob(s) : Buffer.from(s, 'base64').toString('binary'); }; function decodeB64ToUint8Array(b64Text) { var decodedString = atob(b64Text); var array = new Uint8Array(decodedString.length); for (var i = 0; i < decodedString.length; i++) { array[i] = decodedString.charCodeAt(i); } return array; } /*! @name m3u8-parser @version 4.7.1 @license Apache-2.0 */ /** * A stream that buffers string input and generates a `data` event for each * line. * * @class LineStream * @extends Stream */ var LineStream = /*#__PURE__*/function (_Stream) { inheritsLoose(LineStream, _Stream); function LineStream() { var _this; _this = _Stream.call(this) || this; _this.buffer = ''; return _this; } /** * Add new data to be parsed. * * @param {string} data the text to process */ var _proto = LineStream.prototype; _proto.push = function push(data) { var nextNewline; 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; }(Stream); var TAB = String.fromCharCode(0x09); var parseByterange = function parseByterange(byterangeString) { // optionally match and capture 0+ digits before `@` // optionally match and capture 0+ digits after `@` var match = /([0-9.]*)?@?([0-9.]*)?/.exec(byterangeString || ''); var result = {}; if (match[1]) { result.length = parseInt(match[1], 10); } if (match[2]) { result.offset = parseInt(match[2], 10); } return result; }; /** * "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 separator * * @param {string} attributes the attribute line to parse */ var parseAttributes$1 = function parseAttributes(attributes) { // split the string using attributes as the separator var attrs = attributes.split(attributeSeparator()); var result = {}; var i = attrs.length; var attr; 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 = /*#__PURE__*/function (_Stream) { inheritsLoose(ParseStream, _Stream); function ParseStream() { var _this; _this = _Stream.call(this) || this; _this.customParsers = []; _this.tagMappers = []; return _this; } /** * Parses an additional line of input. * * @param {string} line a single line of an M3U8 file to parse */ var _proto = ParseStream.prototype; _proto.push = function push(line) { var _this2 = this; var match; var event; // strip whitespace line = line.trim(); if (line.length === 0) { // ignore empty lines return; } // URIs if (line[0] !== '#') { this.trigger('data', { type: 'uri', uri: line }); return; } // map tags var newLines = this.tagMappers.reduce(function (acc, mapper) { var mappedLine = mapper(line); // skip if unchanged if (mappedLine === line) { return acc; } return acc.concat([mappedLine]); }, [line]); newLines.forEach(function (newLine) { for (var i = 0; i < _this2.customParsers.length; i++) { if (_this2.customParsers[i].call(_this2, newLine)) { return; } } // Comments if (newLine.indexOf('#EXT') !== 0) { _this2.trigger('data', { type: 'comment', text: newLine.slice(1) }); return; } // strip off any carriage returns here so the regex matching // doesn't have to account for them. newLine = newLine.replace('\r', ''); // Tags match = /^#EXTM3U/.exec(newLine); if (match) { _this2.trigger('data', { type: 'tag', tagType: 'm3u' }); return; } match = /^#EXTINF:?([0-9\.]*)?,?(.*)?$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'inf' }; if (match[1]) { event.duration = parseFloat(match[1]); } if (match[2]) { event.title = match[2]; } _this2.trigger('data', event); return; } match = /^#EXT-X-TARGETDURATION:?([0-9.]*)?/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'targetduration' }; if (match[1]) { event.duration = parseInt(match[1], 10); } _this2.trigger('data', event); return; } match = /^#EXT-X-VERSION:?([0-9.]*)?/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'version' }; if (match[1]) { event.version = parseInt(match[1], 10); } _this2.trigger('data', event); return; } match = /^#EXT-X-MEDIA-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'media-sequence' }; if (match[1]) { event.number = parseInt(match[1], 10); } _this2.trigger('data', event); return; } match = /^#EXT-X-DISCONTINUITY-SEQUENCE:?(\-?[0-9.]*)?/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'discontinuity-sequence' }; if (match[1]) { event.number = parseInt(match[1], 10); } _this2.trigger('data', event); return; } match = /^#EXT-X-PLAYLIST-TYPE:?(.*)?$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'playlist-type' }; if (match[1]) { event.playlistType = match[1]; } _this2.trigger('data', event); return; } match = /^#EXT-X-BYTERANGE:?(.*)?$/.exec(newLine); if (match) { event = _extends_1(parseByterange(match[1]), { type: 'tag', tagType: 'byterange' }); _this2.trigger('data', event); return; } match = /^#EXT-X-ALLOW-CACHE:?(YES|NO)?/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'allow-cache' }; if (match[1]) { event.allowed = !/NO/.test(match[1]); } _this2.trigger('data', event); return; } match = /^#EXT-X-MAP:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'map' }; if (match[1]) { var attributes = parseAttributes$1(match[1]); if (attributes.URI) { event.uri = attributes.URI; } if (attributes.BYTERANGE) { event.byterange = parseByterange(attributes.BYTERANGE); } } _this2.trigger('data', event); return; } match = /^#EXT-X-STREAM-INF:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'stream-inf' }; if (match[1]) { event.attributes = parseAttributes$1(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); } } _this2.trigger('data', event); return; } match = /^#EXT-X-MEDIA:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'media' }; if (match[1]) { event.attributes = parseAttributes$1(match[1]); } _this2.trigger('data', event); return; } match = /^#EXT-X-ENDLIST/.exec(newLine); if (match) { _this2.trigger('data', { type: 'tag', tagType: 'endlist' }); return; } match = /^#EXT-X-DISCONTINUITY/.exec(newLine); if (match) { _this2.trigger('data', { type: 'tag', tagType: 'discontinuity' }); return; } match = /^#EXT-X-PROGRAM-DATE-TIME:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'program-date-time' }; if (match[1]) { event.dateTimeString = match[1]; event.dateTimeObject = new Date(match[1]); } _this2.trigger('data', event); return; } match = /^#EXT-X-KEY:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'key' }; if (match[1]) { event.attributes = parseAttributes$1(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); } } _this2.trigger('data', event); return; } match = /^#EXT-X-START:?(.*)$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'start' }; if (match[1]) { event.attributes = parseAttributes$1(match[1]); event.attributes['TIME-OFFSET'] = parseFloat(event.attributes['TIME-OFFSET']); event.attributes.PRECISE = /YES/.test(event.attributes.PRECISE); } _this2.trigger('data', event); return; } match = /^#EXT-X-CUE-OUT-CONT:?(.*)?$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'cue-out-cont' }; if (match[1]) { event.data = match[1]; } else { event.data = ''; } _this2.trigger('data', event); return; } match = /^#EXT-X-CUE-OUT:?(.*)?$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'cue-out' }; if (match[1]) { event.data = match[1]; } else { event.data = ''; } _this2.trigger('data', event); return; } match = /^#EXT-X-CUE-IN:?(.*)?$/.exec(newLine); if (match) { event = { type: 'tag', tagType: 'cue-in' }; if (match[1]) { event.data = match[1]; } else { event.data = ''; } _this2.trigger('data', event); return; } match = /^#EXT-X-SKIP:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'skip' }; event.attributes = parseAttributes$1(match[1]); if (event.attributes.hasOwnProperty('SKIPPED-SEGMENTS')) { event.attributes['SKIPPED-SEGMENTS'] = parseInt(event.attributes['SKIPPED-SEGMENTS'], 10); } if (event.attributes.hasOwnProperty('RECENTLY-REMOVED-DATERANGES')) { event.attributes['RECENTLY-REMOVED-DATERANGES'] = event.attributes['RECENTLY-REMOVED-DATERANGES'].split(TAB); } _this2.trigger('data', event); return; } match = /^#EXT-X-PART:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'part' }; event.attributes = parseAttributes$1(match[1]); ['DURATION'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = parseFloat(event.attributes[key]); } }); ['INDEPENDENT', 'GAP'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = /YES/.test(event.attributes[key]); } }); if (event.attributes.hasOwnProperty('BYTERANGE')) { event.attributes.byterange = parseByterange(event.attributes.BYTERANGE); } _this2.trigger('data', event); return; } match = /^#EXT-X-SERVER-CONTROL:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'server-control' }; event.attributes = parseAttributes$1(match[1]); ['CAN-SKIP-UNTIL', 'PART-HOLD-BACK', 'HOLD-BACK'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = parseFloat(event.attributes[key]); } }); ['CAN-SKIP-DATERANGES', 'CAN-BLOCK-RELOAD'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = /YES/.test(event.attributes[key]); } }); _this2.trigger('data', event); return; } match = /^#EXT-X-PART-INF:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'part-inf' }; event.attributes = parseAttributes$1(match[1]); ['PART-TARGET'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = parseFloat(event.attributes[key]); } }); _this2.trigger('data', event); return; } match = /^#EXT-X-PRELOAD-HINT:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'preload-hint' }; event.attributes = parseAttributes$1(match[1]); ['BYTERANGE-START', 'BYTERANGE-LENGTH'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = parseInt(event.attributes[key], 10); var subkey = key === 'BYTERANGE-LENGTH' ? 'length' : 'offset'; event.attributes.byterange = event.attributes.byterange || {}; event.attributes.byterange[subkey] = event.attributes[key]; // only keep the parsed byterange object. delete event.attributes[key]; } }); _this2.trigger('data', event); return; } match = /^#EXT-X-RENDITION-REPORT:(.*)$/.exec(newLine); if (match && match[1]) { event = { type: 'tag', tagType: 'rendition-report' }; event.attributes = parseAttributes$1(match[1]); ['LAST-MSN', 'LAST-PART'].forEach(function (key) { if (event.attributes.hasOwnProperty(key)) { event.attributes[key] = parseInt(event.attributes[key], 10); } }); _this2.trigger('data', event); return; } // unknown tag type _this2.trigger('data', { type: 'tag', data: newLine.slice(4) }); }); } /** * Add a parser for custom headers * * @param {Object} options a map of options for the added parser * @param {RegExp} options.expression a regular expression to match the custom header * @param {string} options.customType the custom type to register to the output * @param {Function} [options.dataParser] function to parse the line into an object * @param {boolean} [options.segment] should tag data be attached to the segment object */ ; _proto.addParser = function addParser(_ref) { var _this3 = this; var expression = _ref.expression, customType = _ref.customType, dataParser = _ref.dataParser, segment = _ref.segment; if (typeof dataParser !== 'function') { dataParser = function dataParser(line) { return line; }; } this.customParsers.push(function (line) { var match = expression.exec(line); if (match) { _this3.trigger('data', { type: 'custom', data: dataParser(line), customType: customType, segment: segment }); return true; } }); } /** * Add a custom header mapper * * @param {Object} options * @param {RegExp} options.expression a regular expression to match the custom header * @param {Function} options.map function to translate tag into a different tag */ ; _proto.addTagMapper = function addTagMapper(_ref2) { var expression = _ref2.expression, map = _ref2.map; var mapFn = function mapFn(line) { if (expression.test(line)) { return map(line); } return line; }; this.tagMappers.push(mapFn); }; return ParseStream; }(Stream); var camelCase = function camelCase(str) { return str.toLowerCase().replace(/-(\w)/g, function (a) { return a[1].toUpperCase(); }); }; var camelCaseKeys = function camelCaseKeys(attributes) { var result = {}; Object.keys(attributes).forEach(function (key) { result[camelCase(key)] = attributes[key]; }); return result; }; // set SERVER-CONTROL hold back based upon targetDuration and partTargetDuration // we need this helper because defaults are based upon targetDuration and // partTargetDuration being set, but they may not be if SERVER-CONTROL appears before // target durations are set. var setHoldBack = function setHoldBack(manifest) { var serverControl = manifest.serverControl, targetDuration = manifest.targetDuration, partTargetDuration = manifest.partTargetDuration; if (!serverControl) { return; } var tag = '#EXT-X-SERVER-CONTROL'; var hb = 'holdBack'; var phb = 'partHoldBack'; var minTargetDuration = targetDuration && targetDuration * 3; var minPartDuration = partTargetDuration && partTargetDuration * 2; if (targetDuration && !serverControl.hasOwnProperty(hb)) { serverControl[hb] = minTargetDuration; this.trigger('info', { message: tag + " defaulting HOLD-BACK to targetDuration * 3 (" + minTargetDuration + ")." }); } if (minTargetDuration && serverControl[hb] < minTargetDuration) { this.trigger('warn', { message: tag + " clamping HOLD-BACK (" + serverControl[hb] + ") to targetDuration * 3 (" + minTargetDuration + ")" }); serverControl[hb] = minTargetDuration; } // default no part hold back to part target duration * 3 if (partTargetDuration && !serverControl.hasOwnProperty(phb)) { serverControl[phb] = partTargetDuration * 3; this.trigger('info', { message: tag + " defaulting PART-HOLD-BACK to partTargetDuration * 3 (" + serverControl[phb] + ")." }); } // if part hold back is too small default it to part target duration * 2 if (partTargetDuration && serverControl[phb] < minPartDuration) { this.trigger('warn', { message: tag + " clamping PART-HOLD-BACK (" + serverControl[phb] + ") to partTargetDuration * 2 (" + minPartDuration + ")." }); serverControl[phb] = minPartDuration; } }; /** * 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 = /*#__PURE__*/function (_Stream) { inheritsLoose(Parser, _Stream); function Parser() { var _this; _this = _Stream.call(this) || this; _this.lineStream = new LineStream(); _this.parseStream = new ParseStream(); _this.lineStream.pipe(_this.parseStream); /* eslint-disable consistent-this */ var self = assertThisInitialized(_this); /* eslint-enable consistent-this */ var uris = []; var currentUri = {}; // if specified, the active EXT-X-MAP definition var currentMap; // if specified, the active decryption key var _key; var hasParts = false; var noop = function noop() {}; var defaultMediaGroups = { 'AUDIO': {}, 'VIDEO': {}, 'CLOSED-CAPTIONS': {}, 'SUBTITLES': {} }; // This is the Widevine UUID from DASH IF IOP. The same exact string is // used in MPDs with Widevine encrypted streams. var widevineUuid = 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed'; // 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: [] }; // keep track of the last seen segment's byte range end, as segments are not required // to provide the offset, in which case it defaults to the next byte after the // previous segment var lastByterangeEnd = 0; // keep track of the last seen part's byte range end. var lastPartByterangeEnd = 0; _this.on('end', function () { // only add preloadSegment if we don't yet have a uri for it. // and we actually have parts/preloadHints if (currentUri.uri || !currentUri.parts && !currentUri.preloadHints) { return; } if (!currentUri.map && currentMap) { currentUri.map = currentMap; } if (!currentUri.key && _key) { currentUri.key = _key; } if (!currentUri.timeline && typeof currentTimeline === 'number') { currentUri.timeline = currentTimeline; } _this.manifest.preloadSegment = currentUri; }); // update the manifest with the m3u8 entry from the parse stream _this.parseStream.on('data', function (entry) { var mediaGroup; var rendition; ({ tag: function tag() { // switch based on the tag type (({ version: function version() { if (entry.version) { this.manifest.version = entry.version; } }, '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)) { /* * From the latest spec (as of this writing): * https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.2 * * Same text since EXT-X-BYTERANGE's introduction in draft 7: * https://tools.ietf.org/html/draft-pantos-http-live-streaming-07#section-3.3.1) * * "If o [offset] is not present, the sub-range begins at the next byte * following the sub-range of the previous media segment." */ entry.offset = lastByterangeEnd; } } if ('offset' in entry) { currentUri.byterange = byterange; byterange.offset = entry.offset; } lastByterangeEnd = byterange.offset + byterange.length; }, 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.KEYFORMAT === 'com.apple.streamingkeydelivery') { this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this. this.manifest.contentProtection['com.apple.fps.1_0'] = { attributes: entry.attributes }; return; } if (entry.attributes.KEYFORMAT === 'com.microsoft.playready') { this.manifest.contentProtection = this.manifest.contentProtection || {}; // TODO: add full support for this. this.manifest.contentProtection['com.microsoft.playready'] = { uri: entry.attributes.URI }; return; } // check if the content is encrypted for Widevine // Widevine/HLS spec: https://storage.googleapis.com/wvdocs/Widevine_DRM_HLS.pdf if (entry.attributes.KEYFORMAT === widevineUuid) { var VALID_METHODS = ['SAMPLE-AES', 'SAMPLE-AES-CTR', 'SAMPLE-AES-CENC']; if (VALID_METHODS.indexOf(entry.attributes.METHOD) === -1) { this.trigger('warn', { message: 'invalid key method provided for Widevine' }); return; } if (entry.attributes.METHOD === 'SAMPLE-AES-CENC') { this.trigger('warn', { message: 'SAMPLE-AES-CENC is deprecated, please use SAMPLE-AES-CTR instead' }); } if (entry.attributes.URI.substring(0, 23) !== 'data:text/plain;base64,') { this.trigger('warn', { message: 'invalid key URI provided for Widevine' }); return; } if (!(entry.attributes.KEYID && entry.attributes.KEYID.substring(0, 2) === '0x')) { this.trigger('warn', { message: 'invalid key ID provided for Widevine' }); return; } // if Widevine key attributes are valid, store them as `contentProtection` // on the manifest to emulate Widevine tag structure in a DASH mpd this.manifest.contentProtection = this.manifest.contentProtection || {}; this.manifest.contentProtection['com.widevine.alpha'] = { attributes: { schemeIdUri: entry.attributes.KEYFORMAT, // remove '0x' from the key id string keyId: entry.attributes.KEYID.substring(2) }, // decode the base64-encoded PSSH box pssh: decodeB64ToUint8Array(entry.attributes.URI.split(',')[1]) }; 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