Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ within each round, with the starting variant rotated, after one warmup round
- `transport`: a publisher page writes 160 B through 1 MiB frames, a consumer page
subscribes and reads them all. Connection setup and fixture generation stay
outside the timed interval, which starts at SUBSCRIBE.
- `cmaf`: CMAF fragment encode and decode of fixed audio (50 x 160 B) and video
(a 64 KiB keyframe and 29 x 4 KiB) fixtures, with no network.
- `cmaf`: CMAF fragment decode of fixed audio (50 x 160 B) and video
(a 64 KiB keyframe and 29 x 4 KiB) fixtures. Fixture loading stays outside
the timed interval; no encoding or network runs in that interval.
- `media`: `@moq/publish` encodes a scrolling 1280x720 canvas at 30 fps (H.264,
2.5 Mb/s, 1 s keyframes) and `@moq/watch` plays it on a 2D canvas. After a
warmup, a 10 s window reports published, received, decoded and presented frame
Expand Down
30 changes: 1 addition & 29 deletions bench/browser/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* `:copy` injects one extra copy of every received stream chunk into @moq/net, the control
* that proves the suite can see a copy-heavy change.
*/
import { readdir } from "node:fs/promises";
import { basename, join, resolve } from "node:path";
import type { BunPlugin } from "bun";
import { workspace } from "./workspace.ts";

const PAGES = ["transport", "cmaf", "media"].map((page) => join(import.meta.dir, "page", `${page}.ts`));

Expand All @@ -37,34 +37,6 @@ for (const spec of specs) {
console.log(`bundled ${name} from ${root}${copy ? " with an injected chunk copy" : ""}`);
}

/** Resolves `@moq/<name>[/<sub>]` through the variant checkout's package exports. */
function workspace(root: string): BunPlugin {
return {
name: "workspace",
async setup(build) {
const packages = new Map<string, { dir: string; exports: Record<string, string> }>();
for (const entry of await readdir(join(root, "js"), { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const manifest = Bun.file(join(root, "js", entry.name, "package.json"));
if (!(await manifest.exists())) continue;
const { name, exports } = await manifest.json();
if (typeof exports !== "object" || exports === null) continue;
packages.set(name, { dir: join(root, "js", entry.name), exports });
}
build.onResolve({ filter: /^@moq\// }, (args) => {
const [scope, name, ...rest] = args.path.split("/");
const found = packages.get(`${scope}/${name}`);
// Published dependencies resolve normally.
if (!found) return undefined;
const key = rest.length ? `./${rest.join("/")}` : ".";
const target = found.exports[key];
if (typeof target !== "string") throw new Error(`${args.path} is not a source export of ${found.dir}`);
return { path: join(found.dir, target) };
});
},
};
}

/** `?worklet` imports become a blob URL of the separately bundled worklet, as vite-plugin-worklet does. */
function worklet(): BunPlugin {
return {
Expand Down
12 changes: 7 additions & 5 deletions bench/browser/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const server = Bun.serve({
port: 0,
async fetch(request) {
const path = new URL(request.url).pathname;
if (path === "/cmaf-fixtures.json")
return new Response(Bun.file(new URL("../../js/hang/src/cmaf/fixtures.json", import.meta.url)));
const page = /^\/([a-z0-9-]+)\/(transport|cmaf|media)$/.exec(path);
if (page)
return new Response(
Expand Down Expand Up @@ -465,11 +467,11 @@ async function cmaf(variant: string, shape: Cmaf, round: number, pass: Pass) {
const opened = await open(variant, "cmaf");
try {
await opened.page.evaluate((shape) => window.cmaf.prepare(shape), shape);
const expected = shape.sizes.reduce(
(sum, size, frame) => (sum + check(payload(size, frame), size, frame, false)) >>> 0,
0,
);
for (const phase of ["encode", "decode"] as const) {
const expected = shape.sizes.reduce((sum, size, frame) => {
const pattern = shape.kind === "video" && frame > 0 ? 1 : 0;
return (sum + check(payload(size, pattern), size, pattern, false)) >>> 0;
}, 0);
for (const phase of ["decode"] as const) {
const before = await metrics(opened);
let result: Phase | undefined;
const run = async () => {
Expand Down
115 changes: 29 additions & 86 deletions bench/browser/page/cmaf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/** CMAF fragment encode and decode over fixed audio and video fixtures, with no network. */
import * as Catalog from "@moq/hang/catalog";
import * as Container from "@moq/hang/container";
import { check, payload } from "../fixture.ts";
/** CMAF fragment decode over fixed audio and video input, with no network in the timed phase. */
import * as CmafDecode from "@moq/hang/cmaf";
Comment thread
kixelated marked this conversation as resolved.
Resolved
import { check } from "../fixture.ts";
import { longTasks, nextTask } from "./observe.ts";

/** One CMAF workload: a track kind, its frame sizes, and how often the whole set repeats. */
Expand All @@ -20,90 +19,41 @@ export interface Phase {
long_tasks: { count: number; total_ms: number };
}

const VIDEO = Catalog.VideoConfigSchema.parse({
codec: "avc1.64001f",
container: { kind: "legacy" },
codedWidth: 1280,
codedHeight: 720,
description: "0164001fffe100046764001f01000268ee",
});
const AUDIO = Catalog.AudioConfigSchema.parse({
codec: "opus",
container: { kind: "legacy" },
sampleRate: 48_000,
numberOfChannels: 2,
});

let frames: Uint8Array[] = [];
let fragments: Uint8Array[] = [];
// The last decode's output, or undefined after an encode.
let decoded: Container.Cmaf.Sample[][] | undefined;
let init: Container.Cmaf.InitSegment | undefined;
let decoded: CmafDecode.Sample[][] | undefined;
let init: CmafDecode.InitSegment | undefined;
let config: Cmaf | undefined;

/** Builds the fixture frames and init segment, outside any timed interval. */
function prepare(next: Cmaf): void {
/** Load fixed fragment input outside the timed interval. */
async function prepare(next: Cmaf): Promise<void> {
const response = await fetch("/cmaf-fixtures.json");
if (!response.ok) throw new Error(`CMAF fixture: ${response.status}`);
const fixtures = await response.json();
const bytes = (value: string) => Uint8Array.from(atob(value), (char) => char.charCodeAt(0));
config = next;
frames = next.sizes.map((size, frame) => payload(size, frame));
fragments = [];
init = Container.Cmaf.decodeInitSegment(
next.kind === "video"
? Container.Cmaf.createVideoInitSegment(VIDEO)
: Container.Cmaf.createAudioInitSegment(AUDIO),
init = CmafDecode.decodeInitSegment(bytes(next.kind === "video" ? fixtures.videoInit : fixtures.audioInit));
fragments = next.sizes.map((_, frame) =>
bytes(
next.kind === "audio"
? fixtures.bench.audio
: frame === 0
? fixtures.bench.videoKey
: fixtures.bench.videoDelta,
),
);
}

// 30 fps video and 20 ms audio, in the init segment's timescale.
function duration(): number {
if (!init || !config) throw new Error("cmaf before prepare");
return config.kind === "video" ? init.timescale / 30 : init.timescale / 50;
}

async function encode(): Promise<Phase> {
if (!config) throw new Error("encode before prepare");
const step = duration();
// The phase runs alone in its own task, so its long task is neither shared nor missed.
await nextTask();
const start = performance.now();
fragments = [];
let bytes = 0;
for (let iteration = 0; iteration < config.iterations; iteration++) {
for (const [index, data] of frames.entries()) {
const sequence = iteration * frames.length + index;
const fragment = Container.Cmaf.encodeDataSegment({
kind: config.kind,
data,
timestamp: sequence * step,
duration: step,
keyframe: index === 0,
sequence: sequence + 1,
});
bytes += fragment.byteLength;
// Only the last iteration's fragments feed decode; earlier ones exist to be timed.
if (iteration === config.iterations - 1) fragments.push(fragment);
}
}
const end = performance.now();
await nextTask();
decoded = undefined;
return {
fragments: config.iterations * frames.length,
bytes,
elapsed_ms: end - start,
long_tasks: longTasks(start, end),
};
}

async function decode(): Promise<Phase> {
const segment = init;
if (!config || !segment || fragments.length !== frames.length) throw new Error("decode before encode");
if (!config || !segment || fragments.length !== config.sizes.length) throw new Error("decode before prepare");
await nextTask();
const start = performance.now();
let bytes = 0;
for (let iteration = 0; iteration < config.iterations; iteration++) {
decoded = fragments.map((fragment) => {
bytes += fragment.byteLength;
return Container.Cmaf.decodeDataSegment(fragment, segment, undefined);
return CmafDecode.decodeDataSegment(fragment, segment, undefined);
});
}
const end = performance.now();
Expand All @@ -116,30 +66,23 @@ async function decode(): Promise<Phase> {
};
}

/**
* Digest of the last phase's output, checked byte for byte.
*
* A separate call, so the driver's CPU and trace snapshots for a phase exclude it. After
* encode it decodes the retained fragments, since the encoder must carry every frame.
*/
/** Check the last decode's output byte for byte outside its measured phase. */
function verify(): number {
const segment = init;
if (!config || !segment) throw new Error("verify before prepare");
const samples =
decoded ?? fragments.map((fragment) => Container.Cmaf.decodeDataSegment(fragment, segment, undefined));
if (!config || !decoded) throw new Error("verify before decode");
let digest = 0;
for (const [frame, sample] of samples.entries()) {
for (const [frame, sample] of decoded.entries()) {
if (sample.length !== 1) throw new Error(`fragment ${frame}: ${sample.length} samples`);
if (config.kind === "video" && sample[0].keyframe !== (frame === 0))
throw new Error(`fragment ${frame}: keyframe flag lost`);
digest = (digest + check(sample[0].data, config.sizes[frame], frame, true)) >>> 0;
const pattern = config.kind === "video" && frame > 0 ? 1 : 0;
digest = (digest + check(sample[0].data, config.sizes[frame], pattern, true)) >>> 0;
}
return digest;
}

declare global {
interface Window {
cmaf: { prepare: typeof prepare; encode: typeof encode; decode: typeof decode; verify: typeof verify };
cmaf: { prepare: typeof prepare; decode: typeof decode; verify: typeof verify };
}
}
window.cmaf = { prepare, encode, decode, verify };
window.cmaf = { prepare, decode, verify };
43 changes: 43 additions & 0 deletions bench/browser/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, test } from "bun:test";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { workspace } from "./workspace.ts";

for (const current of [false, true]) {
test(`CMAF benchmark resolves the ${current ? "optional" : "baseline container"} export`, async () => {
const root = await mkdtemp(join(tmpdir(), "moq-bench-workspace-"));
try {
const dir = join(root, "js/hang");
await mkdir(dir, { recursive: true });
await writeFile(
join(dir, "package.json"),
JSON.stringify({
name: "@moq/hang",
exports: { "./container": "./container.ts", ...(current ? { "./cmaf": "./cmaf.ts" } : {}) },
}),
);
await writeFile(
join(dir, "container.ts"),
'export const Cmaf = { decodeInitSegment: () => "legacy-init", decodeDataSegment: () => "legacy" };',
);
await writeFile(
join(dir, "cmaf.ts"),
'export const decodeInitSegment = () => "current-init"; export const decodeDataSegment = () => "current";',
);
const entry = join(root, "entry.ts");
await writeFile(entry, 'export { decodeInitSegment, decodeDataSegment } from "@moq/hang/cmaf";');
const result = await Bun.build({
entrypoints: [entry],
outdir: join(root, "dist"),
plugins: [workspace(root)],
});
expect(result.success).toBe(true);
const decoder = await import(result.outputs[0].path);
expect(decoder.decodeInitSegment()).toBe(current ? "current-init" : "legacy-init");
expect(decoder.decodeDataSegment()).toBe(current ? "current" : "legacy");
} finally {
await rm(root, { recursive: true, force: true });
}
});
}
47 changes: 47 additions & 0 deletions bench/browser/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import type { BunPlugin } from "bun";

/** Resolves `@moq/<name>[/<sub>]` through the variant checkout's package exports. */
export function workspace(root: string): BunPlugin {
return {
name: "workspace",
async setup(build) {
const packages = new Map<string, { dir: string; exports: Record<string, string> }>();
for (const entry of await readdir(join(root, "js"), { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const manifest = Bun.file(join(root, "js", entry.name, "package.json"));
if (!(await manifest.exists())) continue;
const { name, exports } = await manifest.json();
if (typeof exports !== "object" || exports === null) continue;
packages.set(name, { dir: join(root, "js", entry.name), exports });
}
build.onResolve({ filter: /^@moq\// }, (args) => {
const [scope, name, ...rest] = args.path.split("/");
const found = packages.get(`${scope}/${name}`);
// Published dependencies resolve normally.
if (!found) return undefined;
const key = rest.length ? `./${rest.join("/")}` : ".";
const target = found.exports[key];
// Older baselines expose the same decoder through Container.Cmaf. Adapt the
// benchmark import at build time, outside every measured phase.
if (args.path === "@moq/hang/cmaf" && target === undefined) {
const container = found.exports["./container"];
if (typeof container !== "string") throw new Error(`missing CMAF decoder in ${found.dir}`);
return { path: `${join(found.dir, container)}?bench-cmaf`, namespace: "legacy-cmaf" };
}
if (typeof target !== "string") throw new Error(`${args.path} is not a source export of ${found.dir}`);
return { path: join(found.dir, target) };
});
build.onResolve({ filter: /.*/, namespace: "legacy-cmaf" }, (args) => ({
path: args.path,
namespace: "file",
}));
build.onLoad({ filter: /.*/, namespace: "legacy-cmaf" }, (args) => ({
loader: "js",
contents: `import { Cmaf } from ${JSON.stringify(args.path.slice(0, -"?bench-cmaf".length))};
export const { decodeInitSegment, decodeDataSegment } = Cmaf;`,
}));
},
};
}
1 change: 0 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion doc/lib/js/hang.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The [hang media format](/concept/hang) as TypeScript types and codecs, shared
by [`@moq/watch`](/lib/js/watch) and [`@moq/publish`](/lib/js/publish).

- **Catalog**: zod schemas for the root, renditions, data tracks, and `archive`. The root is a loose object, so `z.extend(Catalog.RootSchema, { yourSection })` adds your own. `Catalog.watch(broadcast)` iterates validated catalog updates, and parsing throws `Catalog.UnsupportedVersion` on a catalog that needs a newer moq-hang draft. `Catalog.Audio.ranked(renditions)` and `Catalog.Video.ranked(renditions)` order renditions best first, with enabled renditions ahead; `<moq-watch>` uses the video order.
- **Containers**: `Container.Legacy` producer/consumer and `Container.Cmaf` init and data segment helpers.
- **Containers**: `Container.Legacy` producer/consumer and separate `@moq/hang/cmaf` decoding. Import the CMAF module only when reading fMP4.
- **Utilities**: priority and latency math, an Opus polyfill for browsers without a native decoder, and the browser quirks the media packages work around.

```ts
Expand Down
6 changes: 6 additions & 0 deletions js/hang/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ How to update your code for each breaking change, newest first.

## Unreleased

### CMAF decoding is a separate module

Replace `Container.Cmaf` with `import * as Cmaf from "@moq/hang/cmaf"`.
Its `Format`, `decodeInitSegment`, and `decodeDataSegment` remain synchronous.
The CMAF encoder (`createVideoInitSegment`, `createAudioInitSegment`, and `encodeDataSegment`) is removed; Hang publishers write Legacy or LOC instead.

### A container decode error rejects `Consumer.next()` (#5942)

A payload the format fails to decode, or a data track's marker sharing its group, now rejects `Container.Consumer.next()` with that error and ends the reader.
Expand Down
9 changes: 5 additions & 4 deletions js/hang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ import * as Hang from "@moq/hang";
// Catalog — describes tracks and their codec properties
import * as Catalog from "@moq/hang/catalog";

// Container — media framing (CMAF and Legacy formats)
// Container — Legacy and LOC framing
import * as Container from "@moq/hang/container";

// CMAF (fMP4) and Legacy (varint-timestamp + raw bitstream) are both available:
// Container.Cmaf — createVideoInitSegment, createAudioInitSegment, encodeDataSegment, decodeDataSegment, etc.
// Container.Legacy — Producer / Consumer classes
// CMAF decoding is a separate synchronous module, loaded only when needed.
import * as Cmaf from "@moq/hang/cmaf";
// Cmaf.decodeInitSegment, Cmaf.decodeDataSegment, Cmaf.Format
// Container.Legacy.Producer writes frames; Container.Consumer reads a Format.
```

For watching and publishing, use the dedicated packages:
Expand Down
2 changes: 1 addition & 1 deletion js/hang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"sideEffects": false,
"exports": {
".": "./src/index.ts",
"./cmaf": "./src/cmaf/index.ts",
"./catalog": "./src/catalog/index.ts",
"./container": "./src/container/index.ts",
"./util": "./src/util/index.ts"
Expand All @@ -31,7 +32,6 @@
"@moq/net": "workspace:^",
"@moq/signals": "workspace:^",
"@svta/cml-iso-bmff": "^1.0.7",
"@svta/cml-utils": "^1.6.2",
"@zod/mini": "^4.6.7",
"zod": "^4.6.7"
},
Expand Down
Loading
Loading