Files
frontend-helper/src/lib/video-playback.ts
T

22 lines
802 B
TypeScript

// Chooses how a feed video plays. Pure, so it is unit-testable without a browser.
export type Playback = "none" | "native" | "hlsjs";
export interface PlaybackEnv {
/** True when the browser can play .m3u8 directly (Safari). */
nativeHls: boolean;
/** True when hls.js (Media Source Extensions) is supported. */
hlsJsSupported: boolean;
}
/** No hlsUrl: thumbnail only. Native HLS wins; hls.js is the fallback. */
export function choosePlayback(hlsUrl: string | null | undefined, env: PlaybackEnv): Playback {
if (!hlsUrl) return "none";
if (env.nativeHls) return "native";
return env.hlsJsSupported ? "hlsjs" : "none";
}
export function supportsNativeHls(video: Pick<HTMLVideoElement, "canPlayType">): boolean {
return video.canPlayType("application/vnd.apple.mpegurl") !== "";
}