- Service Worker plays sealed files in native <video>/<audio>/<img> with Range seeking; watch page served by the relay itself (#shade= fragment links, grant never sent to a server) - MSE live player with keyframe-aware joining, gap detection, latency control - WebM splitter (init + clusters, keyframe detection) that puts blocks in timecode order — Chrome rejects ffmpeg's interleaved Opus/VP8 otherwise - Link mode for live channels: epoch seeds from a channel secret, so a link survives publisher restarts and scheduled rotations - File store streams byte ranges itself (Bun.file().slice() in a Response sent whole files to Range requests) - shade media serve|put|get|ls|rm, shade live text|webm|watch Verified end to end in headless Chromium: VP9/Opus film plays and seeks, live VP8/Opus at 2.9 s latency, live logs, sealed JPEG and Opus audio. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
65 lines
2.5 KiB
TypeScript
65 lines
2.5 KiB
TypeScript
/**
|
|
* Page-side helpers: hand grants to the Service Worker, and play live
|
|
* channels through Media Source Extensions.
|
|
*/
|
|
|
|
import { encodeGrant } from '../grant.js';
|
|
import type { MediaGrant } from '../grant.js';
|
|
import { toHex } from '../bytes.js';
|
|
|
|
export { LivePlayer } from './player.js';
|
|
export type { LivePlayerOptions, LivePlayerStats } from './player.js';
|
|
|
|
/**
|
|
* Register the media worker and wait until it controls this page. `scope`
|
|
* is the directory the virtual `media/<id>` URLs live under.
|
|
*/
|
|
export async function installMediaWorker(swUrl: string, scope?: string): Promise<MediaKeyring> {
|
|
if (!('serviceWorker' in navigator)) throw new Error('this browser has no Service Workers');
|
|
const reg = await navigator.serviceWorker.register(swUrl, scope !== undefined ? { scope } : {});
|
|
await navigator.serviceWorker.ready;
|
|
if (navigator.serviceWorker.controller === null) {
|
|
// First visit: the worker claims the page on activate.
|
|
await new Promise<void>((resolve) =>
|
|
navigator.serviceWorker.addEventListener('controllerchange', () => resolve(), { once: true }),
|
|
);
|
|
}
|
|
return new MediaKeyring(reg.scope);
|
|
}
|
|
|
|
/**
|
|
* The page's copy of its grants. The worker can be stopped by the browser at
|
|
* any time and loses its memory; it then asks the page, and this answers.
|
|
*/
|
|
export class MediaKeyring {
|
|
private readonly grants = new Map<string, string>();
|
|
|
|
constructor(readonly scope: string) {
|
|
navigator.serviceWorker.addEventListener('message', (e) => {
|
|
const msg = e.data as { type?: string; id?: string } | null;
|
|
if (msg?.type !== 'shade-media-need' || typeof msg.id !== 'string') return;
|
|
e.ports[0]?.postMessage(this.grants.get(msg.id) ?? null);
|
|
});
|
|
}
|
|
|
|
/** Register a grant with the worker; returns the URL to put in `src`. */
|
|
async add(grant: MediaGrant): Promise<string> {
|
|
const id = toHex(grant.mediaId);
|
|
const json = encodeGrant(grant);
|
|
this.grants.set(id, json);
|
|
const sw = navigator.serviceWorker.controller;
|
|
if (sw === null) throw new Error('media worker is not controlling this page');
|
|
const res = await new Promise<{ ok: boolean; error?: string }>((resolve) => {
|
|
const ch = new MessageChannel();
|
|
ch.port1.onmessage = (m) => resolve(m.data);
|
|
sw.postMessage({ type: 'shade-media-grant', grant: json }, [ch.port2]);
|
|
});
|
|
if (!res.ok) throw new Error(res.error ?? 'worker refused the grant');
|
|
return this.urlFor(grant);
|
|
}
|
|
|
|
urlFor(grant: MediaGrant): string {
|
|
return new URL(`media/${toHex(grant.mediaId)}`, this.scope).toString();
|
|
}
|
|
}
|