UNPKG

videojs-contrib-hls

Version:

Play back HLS with video.js, even where it's not natively supported.

589 lines (557 loc) 18.3 kB
/** * 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. */ (function(videojs, parseInt, isFinite, mergeOptions, undefined) { var noop = function() {}, // "forgiving" attribute list psuedo-grammar: // attributes -> keyvalue (',' keyvalue)* // keyvalue -> key '=' value // key -> [^=]* // value -> '"' [^"]* '"' | [^,]* attributeSeparator = (function() { var key = '[^=]*', value = '"[^"]*"|[^,]*', keyvalue = '(?:' + key + ')=(?:' + value + ')'; return new RegExp('(?:^|,)(' + keyvalue + ')'); })(), parseAttributes = function(attributes) { var // split the string using attributes as the separator attrs = attributes.split(attributeSeparator), i = attrs.length, result = {}, 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; }, Stream = videojs.Hls.Stream, LineStream, ParseStream, Parser; /** * A stream that buffers string input and generates a `data` event for each * line. */ LineStream = function() { var buffer = ''; LineStream.prototype.init.call(this); /** * Add new data to be parsed. * @param data {string} the text to process */ this.push = function(data) { var nextNewline; buffer += data; nextNewline = buffer.indexOf('\n'); for (; nextNewline > -1; nextNewline = buffer.indexOf('\n')) { this.trigger('data', buffer.substring(0, nextNewline)); buffer = buffer.substring(nextNewline + 1); } }; }; LineStream.prototype = new Stream(); /** * 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. */ ParseStream = function() { ParseStream.prototype.init.call(this); }; ParseStream.prototype = new Stream(); /** * Parses an additional line of input. * @param line {string} a single line of an M3U8 file to parse */ ParseStream.prototype.push = function(line) { var match, event; //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-STREAM-INF:?(.*)$/).exec(line); if (match) { event = { type: 'tag', tagType: 'stream-inf' }; if (match[1]) { event.attributes = parseAttributes(match[1]); if (event.attributes.RESOLUTION) { (function() { var split = event.attributes.RESOLUTION.split('x'), 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-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-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) === '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; } // unknown tag type this.trigger('data', { type: 'tag', data: line.slice(4, line.length) }); }; /** * 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 videojs.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. */ Parser = function() { var self = this, uris = [], currentUri = {}, key; Parser.prototype.init.call(this); this.lineStream = new LineStream(); this.parseStream = new ParseStream(); this.lineStream.pipe(this.parseStream); // the manifest is empty until the parse stream begins delivering data this.manifest = { allowCache: true, discontinuityStarts: [] }; // update the manifest with the m3u8 entry from the parse stream this.parseStream.on('data', function(entry) { ({ tag: function() { // switch based on the tag type (({ 'allow-cache': function() { this.manifest.allowCache = entry.allowed; if (!('allowed' in entry)) { this.trigger('info', { message: 'defaulting allowCache to YES' }); this.manifest.allowCache = true; } }, 'byterange': function() { 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() { this.manifest.endList = true; }, 'inf': function() { 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; } this.manifest.segments = uris; }, 'key': function() { 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 (entry.attributes.IV !== undefined) { key.iv = entry.attributes.IV; } }, 'media-sequence': function() { if (!isFinite(entry.number)) { this.trigger('warn', { message: 'ignoring invalid media sequence: ' + entry.number }); return; } this.manifest.mediaSequence = entry.number; }, 'discontinuity-sequence': function() { if (!isFinite(entry.number)) { this.trigger('warn', { message: 'ignoring invalid discontinuity sequence: ' + entry.number }); return; } this.manifest.discontinuitySequence = entry.number; }, 'playlist-type': function() { if (!(/VOD|EVENT/).test(entry.playlistType)) { this.trigger('warn', { message: 'ignoring unknown playlist type: ' + entry.playlist }); return; } this.manifest.playlistType = entry.playlistType; }, 'stream-inf': function() { this.manifest.playlists = uris; if (!entry.attributes) { this.trigger('warn', { message: 'ignoring empty stream-inf attributes' }); return; } if (!currentUri.attributes) { currentUri.attributes = {}; } currentUri.attributes = mergeOptions(currentUri.attributes, entry.attributes); }, 'discontinuity': function() { currentUri.discontinuity = true; this.manifest.discontinuityStarts.push(uris.length); }, 'targetduration': function() { 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() { if (!isFinite(entry.duration) || entry.duration < 0) { this.trigger('warn', { message: 'ignoring invalid total duration: ' + entry.duration }); return; } this.manifest.totalDuration = entry.duration; } })[entry.tagType] || noop).call(self); }, uri: function() { 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; } // prepare for the next URI currentUri = {}; }, comment: function() { // comments are not important for playback } })[entry.type].call(self); }); }; Parser.prototype = new Stream(); /** * Parse the input string and update the manifest object. * @param chunk {string} a potentially incomplete portion of the manifest */ Parser.prototype.push = function(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. */ Parser.prototype.end = function() { // flush any buffered input this.lineStream.push('\n'); }; window.videojs.m3u8 = { LineStream: LineStream, ParseStream: ParseStream, Parser: Parser }; })(window.videojs, window.parseInt, window.isFinite, window.videojs.mergeOptions);