aboutsummaryrefslogtreecommitdiffstats
path: root/src/hls-player.js
blob: d0878e2a078edfa41f85cfe5a70ba48a6f47a217 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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.
export async function attachHls(video, src) {
  const { default: Hls } = await import('hls.js');
  if (!Hls.isSupported()) return null;
  const hls = new Hls({ lowLatencyMode: true, backBufferLength: 30 });
  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;
}