UNPKG

m3u8stream

Version:

Reads segments from a m3u8 or dash playlist into a consumable stream.

345 lines • 13.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const stream_1 = require("stream"); const miniget_1 = __importDefault(require("miniget")); const m3u8_parser_1 = __importDefault(require("./m3u8-parser")); const dash_mpd_parser_1 = __importDefault(require("./dash-mpd-parser")); const queue_1 = require("./queue"); const parse_time_1 = require("./parse-time"); const key_manager_1 = __importDefault(require("./key-manager")); const parser_1 = require("./parser"); const formatByteRange = (range) => { var _a; return `${range.start}-${(_a = range.end) !== null && _a !== void 0 ? _a : ''}`; }; const itemKey = (url, range) => (range ? `${url}@${formatByteRange(range)}` : url); const supportedParsers = { m3u8: m3u8_parser_1.default, 'dash-mpd': dash_mpd_parser_1.default, }; let m3u8stream = ((playlistURL, options = {}) => { const stream = new stream_1.PassThrough({ highWaterMark: options.highWaterMark, }); const chunkReadahead = options.chunkReadahead || 3; // 20 seconds. const liveBuffer = options.liveBuffer || 20000; const requestOptions = options.requestOptions; const parserType = options.parser || (/\.mpd$/.test(playlistURL) ? 'dash-mpd' : 'm3u8'); const Parser = supportedParsers[parserType]; if (!Parser) { throw TypeError(`parser '${options.parser}' not supported`); } const isDash = parserType === 'dash-mpd'; if ((0, parser_1.isSet)(options.end) && (0, parser_1.isSet)(options.duration)) { throw TypeError('Options `end` and `duration` cannot be used together'); } const isLive = options.begin === 'live'; const applyLiveBuffer = (time) => isLive ? Math.max(time - liveBuffer, 0) : time; const isRelativeTime = (time) => typeof time === 'string' && time !== 'live'; const isAbsoluteTime = (time) => typeof time === 'number'; let begin = isLive ? Infinity : 0; if ((0, parser_1.isSet)(options.begin) && !isLive) { begin = isRelativeTime(options.begin) ? (0, parse_time_1.humanStr)(options.begin) : applyLiveBuffer(options.begin); } const durationMs = (0, parser_1.isSet)(options.duration) ? isRelativeTime(options.duration) ? (0, parse_time_1.humanStr)(options.duration) : options.duration : undefined; let end = Infinity; if ((0, parser_1.isSet)(options.end)) { end = isRelativeTime(options.end) ? (0, parse_time_1.humanStr)(options.end) : applyLiveBuffer(options.end); } else if ((0, parser_1.isSet)(durationMs) && !isLive) { end = begin + durationMs; } const sameTimeBasis = !isLive && ((0, parser_1.isSet)(options.duration) || (isRelativeTime(options.end) && !(0, parser_1.isSet)(options.begin)) || (isRelativeTime(options.begin) && isRelativeTime(options.end)) || (isAbsoluteTime(options.begin) && isAbsoluteTime(options.end))); if (sameTimeBasis && begin >= end) { throw RangeError('Option `begin` must be before `end`'); } const forwardEvents = (req) => { for (let event of [ 'abort', 'request', 'response', 'redirect', 'retry', 'reconnect', ]) { req.on(event, stream.emit.bind(stream, event)); } }; let currSegment; const streamQueue = new queue_1.Queue((req, callback) => { currSegment = req; // Count the size manually, since the `content-length` header is not // always there. let size = 0; req.on('data', (chunk) => (size += chunk.length)); req.pipe(stream, { end: false }); req.on('end', () => callback(null, size)); }, { concurrency: 1 }); const keyManager = new key_manager_1.default(requestOptions); let segmentNumber = 0; let downloaded = 0; const requestQueue = new queue_1.Queue((segment, callback) => { let reqOptions = Object.assign({}, requestOptions); if (segment.range) { reqOptions.headers = Object.assign({}, reqOptions.headers, { Range: `bytes=${formatByteRange(segment.range)}`, }); } const req = (0, miniget_1.default)(new URL(segment.url, playlistURL).toString(), reqOptions); forwardEvents(req); const segmentStream = new stream_1.PassThrough(); segmentStream.on('error', () => undefined); segmentStream.on('close', () => req.destroy()); const destroyWithError = (err) => { req.destroy(); segmentStream.destroy(err); callback(err); }; req.on('error', destroyWithError); streamQueue.push(segmentStream, (_, size) => { downloaded += +size; stream.emit('progress', { num: ++segmentNumber, size: size, duration: segment.duration, url: segment.url, }, requestQueue.total, downloaded); callback(null); }); keyManager .getDecipher(segment.key, playlistURL, segment.seq) .then((decipher) => { if (ended) { req.destroy(); segmentStream.destroy(); return; } if (decipher) { decipher.on('error', destroyWithError); req.pipe(decipher).pipe(segmentStream); } else { req.pipe(segmentStream); } }) .catch((err) => { if (ended) { return; } destroyWithError(err); }); }, { concurrency: chunkReadahead }); const onError = (err) => { stream.emit('error', err); // Stop on any error. stream.end(); }; // When to look for items again. let refreshThreshold; let minRefreshTime; let refreshTimeout; let fetchingPlaylist = true; let ended = false; let noMoreItems = false; let lastRefresh; const onQueuedEnd = (err) => { currSegment = null; if (err) { onError(err); } else if (!fetchingPlaylist && !ended && !noMoreItems && requestQueue.tasks.length + requestQueue.active <= refreshThreshold) { let ms = Math.max(0, minRefreshTime - (Date.now() - lastRefresh)); fetchingPlaylist = true; refreshTimeout = setTimeout(refreshPlaylist, ms); } else if ((ended || noMoreItems) && !requestQueue.tasks.length && !requestQueue.active) { stream.end(); } }; let currPlaylist; let lastSeq; let lastInit; const seenDashItems = new Set(); const maxSeenDashItems = 1000; const evictOldestSeen = () => { const oldest = seenDashItems.values().next().value; seenDashItems.delete(oldest); }; let starttime = 0; let targetDuration = 0; let lastDuration = 0; const refreshPlaylist = () => { lastRefresh = Date.now(); currPlaylist = (0, miniget_1.default)(playlistURL, requestOptions); currPlaylist.on('error', onError); currPlaylist.on('redirect', (redirectURL) => { playlistURL = new URL(redirectURL, playlistURL).toString(); }); forwardEvents(currPlaylist); const parser = currPlaylist.pipe(new Parser(options.id)); parser.on('error', onError); parser.on('starttime', (a) => { if (starttime) { return; } starttime = a; if ((0, parser_1.isSet)(durationMs) && !(0, parser_1.isSet)(options.begin)) { begin += starttime; end += starttime; } if (isRelativeTime(options.begin) && begin >= 0) { begin += starttime; if ((0, parser_1.isSet)(options.duration)) { end += starttime; } } if (isRelativeTime(options.end)) { end += starttime; } if (!isLive && begin >= end) { onError(new RangeError('Option `begin` must be before `end`')); } }); parser.on('endlist', () => { noMoreItems = true; }); parser.on('targetduration', (ms) => { targetDuration = ms; }); const variants = []; parser.on('variant', (variant) => { variants.push(variant); }); let addedItems = []; const addItem = (item) => { if (item.init) { const initKey = itemKey(item.url, item.range); if (initKey === lastInit) { return; } lastInit = initKey; } else if (isDash) { const key = `${itemKey(item.url, item.range)}@${item.seq}`; if (seenDashItems.has(key)) { return; } seenDashItems.add(key); if (seenDashItems.size > maxSeenDashItems) { evictOldestSeen(); } } else { if (item.seq <= lastSeq) { return; } lastSeq = item.seq; } if (isLive && (0, parser_1.isSet)(durationMs) && end === Infinity) { end = item.time + durationMs; } if (item.time >= end) { noMoreItems = true; return; } begin = item.time; requestQueue.push(item, onQueuedEnd); addedItems.push(item); }; let tailedItems = [], tailedItemsDuration = 0; parser.on('item', (item) => { let timedItem = Object.assign({ time: starttime }, item); if (timedItem.time >= end) { noMoreItems = true; return; } if (begin <= timedItem.time) { addItem(timedItem); } else { tailedItems.push(timedItem); tailedItemsDuration += timedItem.duration; // Only keep the last `liveBuffer` of items. while (tailedItems.length > 1 && tailedItemsDuration - tailedItems[0].duration > liveBuffer) { const lastItem = tailedItems.shift(); tailedItemsDuration -= lastItem.duration; } } starttime += timedItem.duration; lastDuration = timedItem.duration || lastDuration; }); parser.on('end', () => { currPlaylist = null; // This is a master playlist, follow the chosen variant stream. if (variants.length) { const variant = (0, parser_1.selectVariant)(variants, options.id); if (!variant) { onError(new Error(`variant stream '${options.id}' not found`)); return; } playlistURL = new URL(variant.url, playlistURL).toString(); refreshPlaylist(); return; } // If we are too ahead of the stream, make sure to get the // latest available items with a small buffer. if (!addedItems.length && tailedItems.length) { tailedItems.forEach((item) => { addItem(item); }); } // Refresh the playlist when remaining segments get low. refreshThreshold = Math.max(1, Math.ceil(addedItems.length * 0.01)); // Reload after the target duration, or half of it if nothing new came // in (RFC 8216 6.3.4). DASH has no target duration, so the last // segment's stands in. minRefreshTime = Math.max(250, (targetDuration || lastDuration) / (addedItems.length ? 1 : 2)); fetchingPlaylist = false; onQueuedEnd(null); }); }; refreshPlaylist(); const cleanup = () => { ended = true; for (const task of streamQueue.tasks) { task.item.destroy(); } streamQueue.die(); requestQueue.die(); clearTimeout(refreshTimeout); currPlaylist === null || currPlaylist === void 0 ? void 0 : currPlaylist.destroy(); currSegment === null || currSegment === void 0 ? void 0 : currSegment.destroy(); keyManager.clear(); }; stream._destroy = (err, callback) => { cleanup(); callback(err); }; stream.end = () => { cleanup(); stream_1.PassThrough.prototype.end.apply(stream); return stream; }; return stream; }); m3u8stream.parseTimestamp = parse_time_1.humanStr; module.exports = m3u8stream; //# sourceMappingURL=index.js.map