| 1 |
import { PassThrough } from 'stream'; |
| 2 |
import miniget from 'miniget'; |
| 3 |
import m3u8Parser from './m3u8-parser'; |
| 4 |
import DashMPDParser from './dash-mpd-parser'; |
| 5 |
import { Callback, Queue } from './queue'; |
| 6 |
import { humanStr } from './parse-time'; |
| 7 |
import { Item } from './parser'; |
| 8 |
|
| 9 |
|
| 10 |
namespace m3u8stream { |
| 11 |
export interface Options { |
| 12 |
begin?: number | string; |
| 13 |
liveBuffer?: number; |
| 14 |
chunkReadahead?: number; |
| 15 |
highWaterMark?: number; |
| 16 |
requestOptions?: miniget.Options; |
| 17 |
parser?: 'm3u8' | 'dash-mpd'; |
| 18 |
id?: string; |
| 19 |
} |
| 20 |
|
| 21 |
export interface Progress { |
| 22 |
num: number; |
| 23 |
size: number; |
| 24 |
duration: number; |
| 25 |
url: string; |
| 26 |
} |
| 27 |
export interface Stream extends PassThrough { |
| 28 |
end: () => this; |
| 29 |
on(event: 'progress', listener: (progress: Progress, totalSegments: number, downloadedBytes: number) => void): this; |
| 30 |
on(event: string | symbol, listener: (...args: any) => void): this; |
| 31 |
} |
| 32 |
|
| 33 |
export interface m3u8streamFunc { |
| 34 |
(playlistURL: string, options?: m3u8stream.Options): Stream; |
| 35 |
parseTimestamp(time: number | string): number; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
interface TimedItem extends Item { |
| 40 |
time: number; |
| 41 |
} |
| 42 |
|
| 43 |
const supportedParsers = { |
| 44 |
m3u8: m3u8Parser, |
| 45 |
'dash-mpd': DashMPDParser, |
| 46 |
}; |
| 47 |
|
| 48 |
let m3u8stream = ((playlistURL: string, options: m3u8stream.Options = {}): m3u8stream.Stream => { |
| 49 |
const stream = new PassThrough({ highWaterMark: options.highWaterMark }) as m3u8stream.Stream; |
| 50 |
const chunkReadahead = options.chunkReadahead || 3; |
| 51 |
// 20 seconds. |
| 52 |
const liveBuffer = options.liveBuffer || 20000; |
| 53 |
const requestOptions = options.requestOptions; |
| 54 |
const Parser = supportedParsers[options.parser || (/\.mpd$/.test(playlistURL) ? 'dash-mpd' : 'm3u8')]; |
| 55 |
if (!Parser) { |
| 56 |
throw TypeError(`parser '${options.parser}' not supported`); |
| 57 |
} |
| 58 |
let begin = 0; |
| 59 |
if (typeof options.begin !== 'undefined') { |
| 60 |
begin = typeof options.begin === 'string' ? |
| 61 |
humanStr(options.begin) : |
| 62 |
Math.max(options.begin - liveBuffer, 0); |
| 63 |
} |
| 64 |
|
| 65 |
const forwardEvents = (req: miniget.Stream) => { |
| 66 |
for (let event of ['abort', 'request', 'response', 'redirect', 'retry', 'reconnect']) { |
| 67 |
req.on(event, stream.emit.bind(stream, event)); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
let currSegment: miniget.Stream | null; |
| 72 |
const streamQueue = new Queue((req: miniget.Stream, callback): void => { |
| 73 |
currSegment = req; |
| 74 |
// Count the size manually, since the `content-length` header is not |
| 75 |
// always there. |
| 76 |
let size = 0; |
| 77 |
req.on('data', (chunk: Buffer) => size += chunk.length); |
| 78 |
req.pipe(stream, { end: false }); |
| 79 |
req.on('end', () => callback(null, size)); |
| 80 |
}, { concurrency: 1 }); |
| 81 |
|
| 82 |
let segmentNumber = 0; |
| 83 |
let downloaded = 0; |
| 84 |
const requestQueue = new Queue((segment: Item, callback: Callback): void => { |
| 85 |
let reqOptions = Object.assign({}, requestOptions); |
| 86 |
if (segment.range) { |
| 87 |
reqOptions.headers = Object.assign({}, reqOptions.headers, { |
| 88 |
Range: `bytes=${segment.range.start}-${segment.range.end}`, |
| 89 |
}); |
| 90 |
} |
| 91 |
let req = miniget(new URL(segment.url, playlistURL).toString(), reqOptions); |
| 92 |
req.on('error', callback); |
| 93 |
forwardEvents(req); |
| 94 |
streamQueue.push(req, (_, size) => { |
| 95 |
downloaded += +size; |
| 96 |
stream.emit('progress', { |
| 97 |
num: ++segmentNumber, |
| 98 |
size: size, |
| 99 |
duration: segment.duration, |
| 100 |
url: segment.url, |
| 101 |
}, requestQueue.total, downloaded); |
| 102 |
callback(null); |
| 103 |
}); |
| 104 |
}, { concurrency: chunkReadahead }); |
| 105 |
|
| 106 |
const onError = (err: Error): void => { |
| 107 |
stream.emit('error', err); |
| 108 |
// Stop on any error. |
| 109 |
stream.end(); |
| 110 |
}; |
| 111 |
|
| 112 |
// When to look for items again. |
| 113 |
let refreshThreshold: number; |
| 114 |
let minRefreshTime: number; |
| 115 |
let refreshTimeout: NodeJS.Timer; |
| 116 |
let fetchingPlaylist = true; |
| 117 |
let ended = false; |
| 118 |
let isStatic = false; |
| 119 |
let lastRefresh: number; |
| 120 |
|
| 121 |
const onQueuedEnd = (err: Error | null): void => { |
| 122 |
currSegment = null; |
| 123 |
if (err) { |
| 124 |
onError(err); |
| 125 |
} else if (!fetchingPlaylist && !ended && !isStatic && |
| 126 |
requestQueue.tasks.length + requestQueue.active <= refreshThreshold) { |
| 127 |
let ms = Math.max(0, minRefreshTime - (Date.now() - lastRefresh)); |
| 128 |
fetchingPlaylist = true; |
| 129 |
refreshTimeout = setTimeout(refreshPlaylist, ms); |
| 130 |
} else if ((ended || isStatic) && |
| 131 |
!requestQueue.tasks.length && !requestQueue.active) { |
| 132 |
stream.end(); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
let currPlaylist: miniget.Stream | null; |
| 137 |
let lastSeq: number; |
| 138 |
let starttime = 0; |
| 139 |
|
| 140 |
const refreshPlaylist = (): void => { |
| 141 |
lastRefresh = Date.now(); |
| 142 |
currPlaylist = miniget(playlistURL, requestOptions); |
| 143 |
currPlaylist.on('error', onError); |
| 144 |
forwardEvents(currPlaylist); |
| 145 |
const parser = currPlaylist.pipe(new Parser(options.id)); |
| 146 |
parser.on('starttime', (a: number) => { |
| 147 |
if (starttime) { return; } |
| 148 |
starttime = a; |
| 149 |
if (typeof options.begin === 'string' && begin >= 0) { |
| 150 |
begin += starttime; |
| 151 |
} |
| 152 |
}); |
| 153 |
parser.on('endlist', () => { isStatic = true; }); |
| 154 |
parser.on('endearly', currPlaylist.unpipe.bind(currPlaylist, parser)); |
| 155 |
|
| 156 |
let addedItems: any[] = []; |
| 157 |
const addItem = (item: TimedItem): void => { |
| 158 |
if (!item.init) { |
| 159 |
if (item.seq <= lastSeq) { return; } |
| 160 |
lastSeq = item.seq; |
| 161 |
} |
| 162 |
begin = item.time; |
| 163 |
requestQueue.push(item, onQueuedEnd); |
| 164 |
addedItems.push(item); |
| 165 |
}; |
| 166 |
|
| 167 |
let tailedItems: TimedItem[] = [], tailedItemsDuration = 0; |
| 168 |
parser.on('item', (item: Item) => { |
| 169 |
let timedItem = { time: starttime, ...item }; |
| 170 |
if (begin <= timedItem.time) { |
| 171 |
addItem(timedItem); |
| 172 |
} else { |
| 173 |
tailedItems.push(timedItem); |
| 174 |
tailedItemsDuration += timedItem.duration; |
| 175 |
// Only keep the last `liveBuffer` of items. |
| 176 |
while (tailedItems.length > 1 && |
| 177 |
tailedItemsDuration - tailedItems[0].duration > liveBuffer) { |
| 178 |
const lastItem = tailedItems.shift() as TimedItem; |
| 179 |
tailedItemsDuration -= lastItem.duration; |
| 180 |
} |
| 181 |
} |
| 182 |
starttime += timedItem.duration; |
| 183 |
}); |
| 184 |
|
| 185 |
parser.on('end', () => { |
| 186 |
currPlaylist = null; |
| 187 |
// If we are too ahead of the stream, make sure to get the |
| 188 |
// latest available items with a small buffer. |
| 189 |
if (!addedItems.length && tailedItems.length) { |
| 190 |
tailedItems.forEach(item => { addItem(item); }); |
| 191 |
} |
| 192 |
|
| 193 |
// Refresh the playlist when remaining segments get low. |
| 194 |
refreshThreshold = Math.max(1, Math.ceil(addedItems.length * 0.01)); |
| 195 |
|
| 196 |
// Throttle refreshing the playlist by looking at the duration |
| 197 |
// of live items added on this refresh. |
| 198 |
minRefreshTime = |
| 199 |
addedItems.reduce((total, item) => item.duration + total, 0); |
| 200 |
|
| 201 |
fetchingPlaylist = false; |
| 202 |
onQueuedEnd(null); |
| 203 |
}); |
| 204 |
}; |
| 205 |
refreshPlaylist(); |
| 206 |
|
| 207 |
stream.end = () => { |
| 208 |
ended = true; |
| 209 |
streamQueue.die(); |
| 210 |
requestQueue.die(); |
| 211 |
clearTimeout(refreshTimeout); |
| 212 |
currPlaylist?.destroy(); |
| 213 |
currSegment?.destroy(); |
| 214 |
PassThrough.prototype.end.call(stream, null); |
| 215 |
return stream; |
| 216 |
}; |
| 217 |
|
| 218 |
return stream; |
| 219 |
}) as m3u8stream.m3u8streamFunc; |
| 220 |
m3u8stream.parseTimestamp = humanStr; |
| 221 |
|
| 222 |
export = m3u8stream; |