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();
|
||
|
|
}
|
||
|
|
}
|