diff options
Diffstat (limited to 'src/hls-player.js')
| -rw-r--r-- | src/hls-player.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/hls-player.js b/src/hls-player.js new file mode 100644 index 0000000..18292dd --- /dev/null +++ b/src/hls-player.js @@ -0,0 +1,20 @@ +// HLS where the browser has none of its own (Firefox): hls.js feeds the +// <video> through Media Source. Chrome and Safari play the playlist as a +// plain src, so this file is fetched only when needed. The single-file +// build replaces it with a stub (attachHls → null): no stream there. +// A stream that's on (`live`) is held near its live edge: started 1.5 s +// back, played up to 1.1× when it drifts past 5 s. +export async function attachHls(video, src, { live = false } = {}) { + const { default: Hls } = await import('hls.js'); + if (!Hls.isSupported()) return null; + const hls = new Hls({ lowLatencyMode: true, backBufferLength: 30, + ...(live ? { liveSyncDuration: 1.5, liveMaxLatencyDuration: 5, maxLiveSyncPlaybackRate: 1.1 } : {}) }); + hls.loadSource(src); + hls.attachMedia(video); + hls.on(Hls.Events.ERROR, (_, d) => { + if (!d.fatal) return; + if (d.type === Hls.ErrorTypes.MEDIA_ERROR) { hls.recoverMediaError(); return; } + video.dispatchEvent(new Event('hlsfatal')); + }); + return hls; +} |
