Files
Shade/packages/shade-media/src/splitter.ts
Sterister 75dbf530ce feat(media): live fragmented MP4 (H.264/HEVC/AV1 + AAC/Opus), shade live video
Fmp4Splitter cuts ftyp+moov into the sticky init and moof+mdat into frames,
reading sync samples from trun/tfhd/trex flags so fragments shorter than a
GOP still mark where late joiners may start. Codec strings come from the
sample entries. MediaSplitter sniffs WebM vs fMP4; the CLI's new
`live video` uses it (`live webm` stays as the pinned form).

Verified: 9 new tests on real ffmpeg output; H.264/AAC live in headless
Chromium (49/49 fragments appended, late join, 3.2 s latency) and a sealed
H.264 film seeking to 24 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-18 21:27:50 +02:00

66 lines
2.5 KiB
TypeScript

/**
* Live container → live frames, whichever container it is.
*
* `MediaSplitter` sniffs the first bytes — an EBML header is WebM, an
* `ftyp`/`moov`/`styp` box is fragmented MP4 — and hands the rest to the
* matching splitter. Both give the same two pieces: one sticky `init` with
* the MSE type string, then media pieces that a SourceBuffer appends on
* their own, flagged where a late joiner may start.
*/
import { concat } from './bytes.js';
import { MediaFormatError } from './errors.js';
import { Fmp4Splitter } from './fmp4.js';
import { WebmSplitter } from './webm.js';
export type MediaPiece =
| { type: 'init'; mime: string; bytes: Uint8Array }
| { type: 'media'; keyframe: boolean; bytes: Uint8Array };
export type LiveContainer = 'webm' | 'fmp4';
/** The container of a live stream from its first bytes, or null if more are needed. */
export function sniffContainer(head: Uint8Array): LiveContainer | null {
if (head.length < 8) return null;
if (head[0] === 0x1a && head[1] === 0x45 && head[2] === 0xdf && head[3] === 0xa3) return 'webm';
const box = String.fromCharCode(head[4]!, head[5]!, head[6]!, head[7]!);
if (box === 'ftyp' || box === 'moov' || box === 'styp') return 'fmp4';
throw new MediaFormatError('not a live WebM or fragmented MP4 stream');
}
export class MediaSplitter {
private inner: WebmSplitter | Fmp4Splitter | null = null;
private head: Uint8Array = new Uint8Array(0);
container: LiveContainer | null = null;
/** `container` pins the format; without it the first bytes decide. */
constructor(container?: LiveContainer) {
if (container !== undefined) this.start(container);
}
push(chunk: Uint8Array): MediaPiece[] {
if (this.inner === null) {
this.head = this.head.length === 0 ? chunk.slice() : concat([this.head, chunk]);
const kind = sniffContainer(this.head);
if (kind === null) return [];
this.start(kind);
chunk = this.head;
this.head = new Uint8Array(0);
}
return this.inner!.push(chunk).map(normalise);
}
finish(): MediaPiece[] {
return this.inner === null ? [] : this.inner.finish().map(normalise);
}
private start(kind: LiveContainer): void {
this.container = kind;
this.inner = kind === 'webm' ? new WebmSplitter() : new Fmp4Splitter();
}
}
function normalise(p: ReturnType<WebmSplitter['push']>[number] | ReturnType<Fmp4Splitter['push']>[number]): MediaPiece {
return p.type === 'init' ? p : { type: 'media', keyframe: p.keyframe, bytes: p.bytes };
}