@interlacejs/hls-adapter
v0.3.0
Published
HLS VideoAdapter for Interlace: hls.js playback with selectable quality levels, native HLS and MP4 fallback.
Maintainers
Readme
@interlacejs/hls-adapter
An HLS implementation of the @interlacejs/core VideoAdapter contract, built on hls.js. It extends NativeVideoAdapter from @interlacejs/native-adapter (play/pause/seek, programmaticSeek, overlay mounting, fullscreen and destroy() behave the same) and adds the optional quality capability, so @interlacejs/player shows its built-in quality menu.
Install
pnpm add @interlacejs/hls-adapter @interlacejs/core hls.jshls.js (^1.5) is a peer dependency.
Usage
import { HlsVideoAdapter } from "@interlacejs/hls-adapter";
import { InteractiveVideoPlayer } from "@interlacejs/player";
function Watch({ doc, hlsUrl, mp4Url }) {
const videoRef = useRef<HTMLVideoElement>(null);
const [adapter, setAdapter] = useState<HlsVideoAdapter>();
useEffect(() => {
if (!videoRef.current) return;
const a = new HlsVideoAdapter(videoRef.current, {
src: hlsUrl, // .m3u8 manifest
fallbackSrc: mp4Url, // optional progressive fallback
rememberQualityKey: "my-app:video-quality", // optional
});
setAdapter(a);
return () => a.destroy();
}, [hlsUrl, mp4Url]);
return (
<div style={{ position: "relative" }}>
{/* No src: the adapter sets the source. */}
<video ref={videoRef} controls />
{adapter && (
<div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
<InteractiveVideoPlayer
adapter={adapter}
document={doc}
registry={registry}
/>
</div>
)}
</div>
);
}Don't set src on the <video> yourself; the adapter picks the source.
Options
| Option | Description |
| -------------------- | -------------------------------------------------------------------------------------------------------------------- |
| src | Video URL (required): an HLS manifest (.m3u8) or a plain video file such as an MP4. |
| type | "auto" (default: HLS when the URL path ends in .m3u8), "hls" or "progressive". |
| fallbackSrc | Progressive source (e.g. MP4) for browsers without HLS support and for unrecoverable errors. |
| hlsConfig | Partial<HlsConfig> passed to new Hls(). |
| refreshSource | () => Promise<{ src, fallbackSrc? }>: fetches fresh URLs when the current ones are rejected as expired. See below. |
| rememberQualityKey | localStorage key for the viewer's quality choice (stored as a height or "auto"). Not remembered when omitted. |
Playback paths
- Plain video files (
srcisn't HLS, e.g. an MP4): played directly in the<video>element, likeNativeVideoAdapter. There is one quality, sogetQualityLevels()returns[]and the player shows no quality menu. You can use this adapter for every video and let it pick the path. - hls.js (
Hls.isSupported(), most desktop and Android browsers): attaches hls.js and loadssrc. Once the manifest is parsed, levels are exposed one per height (highest bitrate wins), sorted high to low and labelled720p, etc. A remembered choice is re-applied (the same height, or the nearest lower one), andqualitychangefires. - Native HLS (Safari, iOS): sets
video.src = src. The browser picks the quality, sogetQualityLevels()returns[]and the player hides its quality menu. - Neither: plays
fallbackSrc, if given.
Quality API
HlsVideoAdapter implements QualityCapableAdapter from @interlacejs/core:
getQualityLevels(): QualityLevel[]; // [] until the manifest loads, and on the native/fallback paths
getQualityLevel(): number; // selected id, -1 = Auto
getActiveQualityLevel(): number | null; // the level playing right now (useful in Auto)
setQualityLevel(id: number): void; // -1 = Auto; persisted under rememberQualityKeyqualitychange fires when the levels load or are cleared, when the selection changes, and when hls.js switches the level that is playing. getHls() returns the hls.js instance (or null) if you need lower-level access.
Error handling
- Fatal network error after the manifest loaded:
hls.startLoad()is retried once. - Fatal media error:
hls.recoverMediaError()is tried once. - Anything else, or a repeated failure: hls.js is destroyed and the adapter switches to
fallbackSrc(if given), keeping the current time and paused state. The position is restored with the adapter's ownseek(), so the player receivesprogrammaticSeek, not a userseek. Levels become[]andqualitychangefires. - On the native HLS path, a
<video>errorevent switches tofallbackSrcin the same way.
Expiring links
Signed URLs expire. Pass refreshSource and the adapter asks for fresh URLs, then reloads from where playback is (same position and play state); through hls.js this is a brief stall, not a restart. It refreshes when a stream request is rejected with 401/403, when hls.js gives up on a network error (object storage often rejects expired segment URLs without CORS headers, which the browser reports as a failed request rather than a 403), and when a plain or fallback video fails to load. It refreshes at most once every 30 seconds (REFRESH_COOLDOWN_MS), so links that still fail when fresh go through the usual handling above, as does a refresh that rejects.
new HlsVideoAdapter(video, {
src: hlsUrl,
fallbackSrc: mp4Url,
refreshSource: async () => {
const lesson = await api.getLesson(id); // re-signed URLs
return { src: lesson.hlsUrl, fallbackSrc: lesson.mp4Url };
},
});License
ISC
