feat(web): add hls.js and reusable HlsPlayer component
Adds hls.js as a web dependency and a small HlsPlayer wrapper that attaches hls.js when MSE is available (config mirrors the legacy _Host.cshtml previewChannel: liveDurationInfinity + unbounded manifest time-to-first-byte, since the troubleshooting playback.m3u8 endpoint blocks until segments exist) and falls back to native HLS on Safari. Tears down the instance on src change and unmount. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Generated
+7
@@ -9,6 +9,7 @@
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"@vitejs/plugin-react": "6.0.3",
|
||||
"hls.js": "^1.6.16",
|
||||
"lucide-react": "1.23.0",
|
||||
"react": "19.2.7",
|
||||
"react-dom": "19.2.7",
|
||||
@@ -2359,6 +2360,12 @@
|
||||
"hermes-estree": "0.25.1"
|
||||
}
|
||||
},
|
||||
"node_modules/hls.js": {
|
||||
"version": "1.6.16",
|
||||
"resolved": "https://registry.npmjs.org/hls.js/-/hls.js-1.6.16.tgz",
|
||||
"integrity": "sha512-VSIRpLfRwlAAdGL4wiTucx2ScRipo0ed1FBatWkyt832jC4CReKstga6yIhYVwGu9LOBjuX9wzmRMeQdBJtzEA==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/html-encoding-sniffer": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-6.0.0.tgz",
|
||||
|
||||
+4
-3
@@ -14,11 +14,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@vitejs/plugin-react": "6.0.3",
|
||||
"vite": "8.1.3",
|
||||
"typescript": "6.0.3",
|
||||
"hls.js": "^1.6.16",
|
||||
"lucide-react": "1.23.0",
|
||||
"react": "19.2.7",
|
||||
"react-dom": "19.2.7",
|
||||
"lucide-react": "1.23.0"
|
||||
"typescript": "6.0.3",
|
||||
"vite": "8.1.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "10.0.1",
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import { useEffect, useRef, type CSSProperties } from 'react';
|
||||
import Hls from 'hls.js';
|
||||
|
||||
export interface HlsPlayerProps {
|
||||
// The HLS manifest (.m3u8) URL to play, or null to render an idle player. Setting a new src
|
||||
// tears down any previous hls.js instance and re-attaches.
|
||||
src: string | null;
|
||||
className?: string;
|
||||
style?: CSSProperties;
|
||||
}
|
||||
|
||||
// A small HLS video player. Uses hls.js when Media Source Extensions are available (config mirrors
|
||||
// _Host.cshtml's legacy `previewChannel`: the troubleshooting playback.m3u8 endpoint blocks until
|
||||
// segments exist, so the manifest request must tolerate an unbounded time-to-first-byte), and falls
|
||||
// back to the browser's native HLS support (Safari) otherwise. Reusable across any screen that needs
|
||||
// to preview an ErsatzTV HLS stream.
|
||||
export function HlsPlayer({ src, className, style }: HlsPlayerProps) {
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null);
|
||||
const hlsRef = useRef<Hls | null>(null);
|
||||
|
||||
const destroyHls = () => {
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy();
|
||||
hlsRef.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const video = videoRef.current;
|
||||
if (!video || !src) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Tear down any previous instance before re-attaching for the new source.
|
||||
destroyHls();
|
||||
|
||||
if (Hls.isSupported()) {
|
||||
const hls = new Hls({
|
||||
liveDurationInfinity: true,
|
||||
manifestLoadPolicy: {
|
||||
default: {
|
||||
maxTimeToFirstByteMs: Infinity,
|
||||
maxLoadTimeMs: 60_000,
|
||||
timeoutRetry: null,
|
||||
errorRetry: null
|
||||
}
|
||||
}
|
||||
});
|
||||
hlsRef.current = hls;
|
||||
hls.loadSource(src);
|
||||
hls.attachMedia(video);
|
||||
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||
void video.play().catch(() => {
|
||||
// Autoplay may be blocked; the user can press play manually.
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
destroyHls();
|
||||
};
|
||||
}
|
||||
|
||||
if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
||||
video.src = src;
|
||||
const onCanPlay = () => {
|
||||
void video.play().catch(() => {
|
||||
// Autoplay may be blocked; the user can press play manually.
|
||||
});
|
||||
};
|
||||
video.addEventListener('canplay', onCanPlay);
|
||||
return () => {
|
||||
video.removeEventListener('canplay', onCanPlay);
|
||||
video.removeAttribute('src');
|
||||
video.load();
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}, [src]);
|
||||
|
||||
// Final unmount safety net (covers the hls.js instance in every path).
|
||||
useEffect(
|
||||
() => () => {
|
||||
destroyHls();
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return <video className={className} controls ref={videoRef} style={style} />;
|
||||
}
|
||||
Reference in New Issue
Block a user