From ca399d3e104afc390515ffb4d2944bdcbfb2341f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 09:00:11 -0700 Subject: [PATCH 1/8] chore(quest): claim quest/m1/push-to-talk/audio-support-cache.md Quest-Claim: c5f3cb43fbccce56 From bb00013ebedb887c37ef5cd920985cb23204d3c0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 09:09:12 -0700 Subject: [PATCH 2/8] fix(watch): cache audio rendition support across unmute Co-Authored-By: GPT-6.1 Sol --- js/justfile | 5 + js/watch/bench/support.ts | 74 +++++++ js/watch/src/audio/config.ts | 5 + js/watch/src/audio/decoder.test.ts | 12 +- js/watch/src/audio/decoder.ts | 12 +- js/watch/src/audio/source.test.ts | 198 +++++++++++++++++++ js/watch/src/audio/source.ts | 45 +++-- js/watch/src/audio/supported.test.ts | 34 +++- js/watch/src/support-cache.ts | 7 + js/watch/src/video/config.ts | 3 - js/watch/src/video/decoder.ts | 9 +- js/watch/src/video/source.test.ts | 3 +- js/watch/src/video/source.ts | 8 +- quest/m1/push-to-talk/README.md | 1 - quest/m1/push-to-talk/audio-support-cache.md | 17 -- sh/gh/nightly.sh | 1 + 16 files changed, 383 insertions(+), 51 deletions(-) create mode 100644 js/watch/bench/support.ts create mode 100644 js/watch/src/support-cache.ts delete mode 100644 quest/m1/push-to-talk/audio-support-cache.md diff --git a/js/justfile b/js/justfile index 00e472c7231..c47c26d7a19 100644 --- a/js/justfile +++ b/js/justfile @@ -155,6 +155,11 @@ ring-bench: bun install --frozen-lockfile bun watch/bench/ring.ts +# Sweep audio rendition count against repeated mute/unmute catalog updates. +audio-support-bench: + bun install --frozen-lockfile + bun watch/bench/support.ts + # Sweep decoder count against PCM callback size during audio warmup. audio-warmup-bench: bun install --frozen-lockfile diff --git a/js/watch/bench/support.ts b/js/watch/bench/support.ts new file mode 100644 index 00000000000..b46a8147676 --- /dev/null +++ b/js/watch/bench/support.ts @@ -0,0 +1,74 @@ +// Catalog-update costs with a warmed audio support verdict. Native probes run in browsers; +// Bun's promise probe isolates selection overhead and probe counts for the nightly series. +import * as Catalog from "@moq/hang/catalog"; +import { Signal } from "@moq/signals"; +import { decoderConfigKey } from "../src/audio/config"; +import { Source } from "../src/audio/source"; +import type { Broadcast } from "../src/broadcast"; +import { supportCacheKey } from "../src/support-cache"; + +/** Measure metadata updates over rendition count, checking every published config. */ +export async function run(): Promise { + const native = typeof AudioDecoder !== "undefined"; + const probe = native + ? async (config: Catalog.AudioConfig) => (await AudioDecoder.isConfigSupported(config)).supported ?? false + : async () => true; + console.log("probe,renditions,updates,probes,us_per_update"); + for (const count of [1, 8, 32]) { + let probes = 0; + const supported = Object.assign( + async (config: Catalog.AudioConfig) => { + probes++; + return probe(config); + }, + { [supportCacheKey]: decoderConfigKey }, + ); + const renditions: Record = Object.fromEntries( + Array.from({ length: count }, (_, i) => [ + `audio${i}`, + Catalog.AudioConfigSchema.parse({ + codec: "opus", + container: { kind: "legacy" }, + sampleRate: 48_000, + numberOfChannels: 2, + }), + ]), + ); + const catalog = new Signal({ audio: { renditions } }); + const source = new Source({ broadcast: { out: { catalog } } as unknown as Broadcast, supported }); + try { + await source.out.available.changed(); + if (Object.keys(source.out.available.peek()).length !== count) throw new Error("unsupported fixture"); + for (const [measured, updates] of [ + [false, 100], + [true, 20], + [true, 200], + ] as const) { + const before = probes; + const start = performance.now(); + for (let update = 0; update < updates; update++) { + const enabled = update % 2 === 0; + const changed = source.out.available.changed(); + catalog.set({ + audio: { + renditions: Object.fromEntries( + Object.entries(renditions).map(([name, c]) => [name, { ...c, enabled }]), + ), + }, + }); + await changed; + const available = Object.values(source.out.available.peek()); + if (available.length !== count || available.some((c) => c.enabled !== enabled)) + throw new Error("stale rendition metadata"); + } + const us = ((performance.now() - start) * 1000) / updates; + if (!measured) continue; + console.log(`${native ? "native" : "promise"},${count},${updates},${probes - before},${us.toFixed(3)}`); + } + } finally { + source.close(); + } + } +} + +await run(); diff --git a/js/watch/src/audio/config.ts b/js/watch/src/audio/config.ts index ba4d7e02e47..103a4839f61 100644 --- a/js/watch/src/audio/config.ts +++ b/js/watch/src/audio/config.ts @@ -36,6 +36,11 @@ export function decoderConfig(config: Catalog.AudioConfig): DecoderConfig { }; } +/** Return a stable cache key for the fields used by the decoder support probe. */ +export function decoderConfigKey(config: Catalog.AudioConfig): string { + return JSON.stringify(decoderConfig(config)); +} + /** Reduce a rendition config to the fields that require a replacement subscription. */ export function playbackIdentity(config: Catalog.AudioConfig): PlaybackIdentity { return { diff --git a/js/watch/src/audio/decoder.test.ts b/js/watch/src/audio/decoder.test.ts index f1450bbd4e3..7165af81dae 100644 --- a/js/watch/src/audio/decoder.test.ts +++ b/js/watch/src/audio/decoder.test.ts @@ -6,9 +6,11 @@ import * as Container from "@moq/hang/container"; import * as Moq from "@moq/net"; import { Effect, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; +import { supportCacheKey } from "../support-cache"; import { type Delay, Sync } from "../sync"; import { Decoder as VideoDecoder } from "../video/decoder"; import { Source as VideoSource } from "../video/source"; +import { decoderConfigKey } from "./config"; import type { Message, State } from "./render"; import { AudioRingBuffer } from "./ring-buffer"; import { Source } from "./source"; @@ -316,7 +318,9 @@ async function play(initial: Delay, buffered = false, cmaf = false) { } as unknown as Broadcast); const delay = new Signal(initial); - const source = new Source({ broadcast, supported: async () => true }); + const supportProbes = mock(async () => true); + Object.assign(supportProbes, { [supportCacheKey]: decoderConfigKey }); + const source = new Source({ broadcast, supported: supportProbes }); const buffer = new Signal(Milli(buffered ? 1000 : 0)); const sync = new Sync({ delay, buffer }); // The decoder's own switch, which a mute or pause turns off. @@ -337,6 +341,7 @@ async function play(initial: Delay, buffered = false, cmaf = false) { }; return { + supportProbes, delay, buffer, sync, @@ -688,10 +693,15 @@ describe("Decoder across a rendition's absence", () => { await microtasks(); expect(playback.track()).toBeUndefined(); expect(contexts).toHaveLength(1); + drain(playback.rings()[0]); + await microtasks(); + expect(contexts[0].state).toBe("suspended"); playback.restore(); await microtasks(); expect(playback.track()).toBe("audio"); + expect(contexts[0].state).toBe("running"); + expect(playback.supportProbes).toHaveBeenCalledTimes(1); await playback.play(6); expect(contexts).toHaveLength(1); diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index 60c1f4f34ff..5ce7679d00e 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -17,9 +17,17 @@ import { Producer as ArrivalProducer, type Arrivals, gap } from "../arrivals"; import { hostedAssets } from "../assets"; import { base64ToBytes } from "../base64"; import { type Failure, failureMoved, nextMedia, subscribeMedia } from "../media"; +import { supportCacheKey } from "../support-cache"; import type { Sync } from "../sync"; import { AudioBuffer } from "./buffer"; -import { type DecoderConfig, frameDuration, type PlaybackIdentity, packetDuration, playbackIdentity } from "./config"; +import { + type DecoderConfig, + decoderConfigKey, + frameDuration, + type PlaybackIdentity, + packetDuration, + playbackIdentity, +} from "./config"; import { Handover } from "./handover"; import { AUTO_MAX_DELAY, ringHold, target } from "./latency"; import { Order } from "./order"; @@ -968,6 +976,8 @@ async function supported(config: Catalog.AudioConfig): Promise { return res.supported ?? false; } +Object.assign(supported, { [supportCacheKey]: decoderConfigKey }); + // Run `task` for one decode attempt. A setup, read, or decode failure that is not the attempt's // cancellation fails its source through `failure`, which latches it and tears the attempt down. function spawnFatal(effect: Effect, failure: Signal, task: () => Promise): void { diff --git a/js/watch/src/audio/source.test.ts b/js/watch/src/audio/source.test.ts index d1709b2e2a2..4245aa01b25 100644 --- a/js/watch/src/audio/source.test.ts +++ b/js/watch/src/audio/source.test.ts @@ -2,6 +2,8 @@ import { expect, test } from "bun:test"; import * as Catalog from "@moq/hang/catalog"; import { Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; +import { supportCacheKey } from "../support-cache"; +import { decoderConfigKey } from "./config"; import { Source } from "./source"; function config(codec: string): Catalog.AudioConfig { @@ -66,3 +68,199 @@ test("a rejected replacement catalog clears previously available audio", async ( source.close(); } }); + +test("unmute reuses the decoder support verdict and publishes the new metadata", async () => { + const initial = config("opus"); + const state = broadcast({ audio: initial }); + let probes = 0; + const supported = Object.assign( + async () => { + probes++; + return true; + }, + { [supportCacheKey]: decoderConfigKey }, + ); + const source = new Source({ broadcast: state.broadcast, supported }); + try { + await settled(source); + for (const enabled of [false, true, false, true]) { + const selected = source.out.track.changed(); + const changed = source.out.available.changed(); + state.catalog.set({ + audio: { + renditions: { + audio: Catalog.AudioConfigSchema.parse({ ...initial, enabled, jitter: 20, bitrate: 96_000 }), + }, + }, + }); + await changed; + await selected; + expect(source.out.available.peek().audio.enabled).toBe(enabled); + expect(source.out.track.peek()).toBe(enabled ? "audio" : undefined); + expect(probes).toBe(1); + } + } finally { + source.close(); + } +}); + +for (const [field, patch] of Object.entries({ + codec: { codec: "mp4a.40.2" }, + description: { description: "1190" }, + sampleRate: { sampleRate: 44100 }, + numberOfChannels: { numberOfChannels: 1 }, + container: { container: { kind: "loc" as const } }, + init: { container: { kind: "cmaf" as const, init: "AA==" } }, + warmup: { warmup: 80 }, +})) { + test(`a ${field} change invalidates the decoder support verdict`, async () => { + const initial = config("opus"); + const state = broadcast({ audio: initial }); + let probes = 0; + const supported = Object.assign( + async () => { + probes++; + return true; + }, + { [supportCacheKey]: decoderConfigKey }, + ); + const source = new Source({ broadcast: state.broadcast, supported }); + try { + await settled(source); + const updated = Catalog.AudioConfigSchema.parse({ ...initial, ...patch }); + const changed = source.out.available.changed(); + state.catalog.set({ audio: { renditions: { audio: updated } } }); + await changed; + expect(probes).toBe(2); + expect(source.out.available.peek().audio).toEqual(updated); + } finally { + source.close(); + } + }); +} + +test("custom support probes include metadata in their cache key", async () => { + const initial = config("opus"); + const state = broadcast({ audio: initial }); + let probes = 0; + const source = new Source({ + broadcast: state.broadcast, + supported: async (c) => { + probes++; + return c.enabled !== false; + }, + }); + try { + await settled(source); + state.catalog.set({ audio: { renditions: { audio: { ...initial, enabled: false } } } }); + await settled(source); + expect(probes).toBe(2); + expect(source.out.available.peek()).toEqual({}); + } finally { + source.close(); + } +}); + +test("unsupported verdicts are cached, rejected probes are not, and removed renditions are pruned", async () => { + const initial = { good: config("opus"), unsupported: config("flac"), rejected: config("mp4a.40.2") }; + const state = broadcast(initial); + const probes: Record = {}; + const supported = Object.assign( + async (c: Catalog.AudioConfig) => { + probes[c.codec] = (probes[c.codec] ?? 0) + 1; + if (c.codec === "mp4a.40.2" && probes[c.codec] === 1) throw new Error("probe failed"); + return c.codec !== "flac"; + }, + { [supportCacheKey]: decoderConfigKey }, + ); + const source = new Source({ broadcast: state.broadcast, supported }); + try { + await settled(source); + const changed = source.out.available.changed(); + state.catalog.set({ + audio: { + renditions: Object.fromEntries( + Object.entries(initial).map(([name, c]) => [name, { ...c, label: "updated" }]), + ), + }, + }); + await changed; + expect(probes).toEqual({ opus: 1, flac: 1, "mp4a.40.2": 2 }); + expect(Object.keys(source.out.available.peek())).toEqual(["good", "rejected"]); + + const removed = source.out.available.changed(); + state.catalog.set({ audio: { renditions: { good: initial.good } } }); + await removed; + const restored = source.out.available.changed(); + state.catalog.set({ audio: { renditions: initial } }); + await restored; + expect(probes).toEqual({ opus: 1, flac: 2, "mp4a.40.2": 3 }); + } finally { + source.close(); + } +}); + +test("a replacement support function cannot reuse another function's verdict", async () => { + const state = broadcast({ audio: config("opus") }); + const supported = new Signal<((c: Catalog.AudioConfig) => Promise) | undefined>(async () => true); + const source = new Source({ broadcast: state.broadcast, supported }); + try { + await settled(source); + supported.set(async () => false); + await settled(source); + expect(source.out.available.peek()).toEqual({}); + } finally { + source.close(); + } +}); + +test("catalog replacement and close release hanging probes without publishing stale verdicts", async () => { + const initial = config("opus"); + const state = broadcast({ audio: initial }); + const started = Promise.withResolvers(); + const pending = Promise.withResolvers(); + let probes = 0; + const supported = Object.assign( + async () => { + probes++; + if (probes === 1) { + started.resolve(); + return pending.promise; + } + return true; + }, + { [supportCacheKey]: decoderConfigKey }, + ); + const source = new Source({ broadcast: state.broadcast, supported }); + try { + await started.promise; + const changed = source.out.available.changed(); + state.catalog.set({ audio: { renditions: { audio: { ...initial, enabled: false } } } }); + await changed; + expect(probes).toBe(2); + pending.resolve(false); + const unmuted = source.out.available.changed(); + state.catalog.set({ audio: { renditions: { audio: { ...initial, enabled: true } } } }); + await unmuted; + expect(probes).toBe(2); + expect(source.out.available.peek().audio.enabled).toBe(true); + } finally { + source.close(); + } + + const closing = Promise.withResolvers(); + const never = Promise.withResolvers(); + const closed = new Source({ + broadcast: state.broadcast, + supported: async () => { + closing.resolve(); + return never.promise; + }, + }); + await closing.promise; + closed.close(); + while (closed.pending.peek()) await closed.pending.changed(); + never.resolve(true); + expect(closed.out.available.peek()).toEqual({}); + expect(closed.out.track.peek()).toBeUndefined(); +}); diff --git a/js/watch/src/audio/source.ts b/js/watch/src/audio/source.ts index 004573ac431..e7e10adcae5 100644 --- a/js/watch/src/audio/source.ts +++ b/js/watch/src/audio/source.ts @@ -2,6 +2,7 @@ import { Milli } from "@moq/clock"; import type * as Catalog from "@moq/hang/catalog"; import { Effect, type Getter, getter, type Inputs, type Readonlys, readonlys, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; +import { type CacheableSupported, supportCacheKey } from "../support-cache"; /** What the viewer wants from the audio rendition. */ export type Target = { @@ -61,6 +62,7 @@ export class Source { readonly pending: Getter = this.#pending; #signals = new Effect(); + #supportCache = new WeakMap>(); constructor(props?: Inputs) { this.in = { @@ -89,28 +91,44 @@ export class Source { const supported = effect.get(this.in.supported); if (!supported) return; + let cache = this.#supportCache.get(supported); + if (!cache) { + cache = new Map(); + this.#supportCache.set(supported, cache); + } + // Retain only the latest config per current rendition, not a catalog's history. + const names = new Set(Object.keys(renditions)); + for (const name of cache.keys()) { + if (!names.has(name)) cache.delete(name); + } + const cacheKey = (supported as CacheableSupported)[supportCacheKey]; + effect.set(this.#pending, true, false); effect.spawn(async () => { try { const available: Record = {}; for (const [name, config] of Object.entries(renditions)) { - // `supported` comes from the consumer, so we cannot assume it ever settles. A rerun - // waits for the tasks it spawned, so an unraced probe would hold the next run shut. - let isSupported = false; - try { - isSupported = !!(await effect.race(supported(config))); - } catch (err) { + const key = cacheKey ? cacheKey(config) : JSON.stringify(config); + const cached = cache.get(name); + let isSupported = cached?.key === key ? cached.supported : undefined; + if (isSupported === undefined) { + try { + // A consumer's probe may never settle; teardown must still release this run. + isSupported = await effect.race(supported(config)); + } catch (err) { + if (effect.abort.aborted) return; + console.warn( + `[Source] audio rendition ${name} (${config.codec}) support probe failed; treating as unsupported`, + err, + ); + } + + // A canceled or rejected probe supplies no verdict for the next run. if (effect.abort.aborted) return; - console.warn( - `[Source] audio rendition ${name} (${config.codec}) support probe failed; treating as unsupported`, - err, - ); + if (isSupported !== undefined) cache.set(name, { key, supported: isSupported }); } - // Torn down: stop probing and publish nothing, since the rerun redoes this. - if (effect.abort.aborted) return; - if (isSupported) available[name] = config; } @@ -185,5 +203,6 @@ export class Source { /** Stop and release everything this holds. */ close(): void { this.#signals.close(); + this.#supportCache = new WeakMap(); } } diff --git a/js/watch/src/audio/supported.test.ts b/js/watch/src/audio/supported.test.ts index 0505ae3fe27..5022d0448e4 100644 --- a/js/watch/src/audio/supported.test.ts +++ b/js/watch/src/audio/supported.test.ts @@ -1,5 +1,8 @@ -import { expect, mock, test } from "bun:test"; +import { expect, mock, spyOn, test } from "bun:test"; import * as Catalog from "@moq/hang/catalog"; +import { Signal } from "@moq/signals"; +import type { Broadcast } from "../broadcast"; +import { Source } from "./source"; // Bun cannot load the blob-URL worklet import, so mock it before the decoder imports it. mock.module("./render-worklet.ts?worklet", () => ({ default: async () => "blob:fake-render" })); @@ -22,3 +25,32 @@ test("renditions are probed through the libav fallback without a native AudioDec expect(await Decoder.supported(config({ codec: "opus" }))).toBe(true); expect(await Decoder.supported(config({ codec: "flac" }))).toBe(false); }); + +test("the decoder support probe is reused through mute and unmute", async () => { + const config = Catalog.AudioConfigSchema.parse({ + codec: "opus", + container: { kind: "legacy" }, + sampleRate: 48000, + numberOfChannels: 2, + }); + // Load the real libav decoder before counting its WebCodecs support queries. + await Decoder.supported(config); + using queries = spyOn(AudioDecoder, "isConfigSupported"); + const catalog = new Signal({ audio: { renditions: { audio: config } } }); + const source = new Source({ + broadcast: { out: { catalog } } as unknown as Broadcast, + supported: Decoder.supported, + }); + try { + await source.out.available.changed(); + for (const enabled of [false, true]) { + const changed = source.out.available.changed(); + catalog.set({ audio: { renditions: { audio: { ...config, enabled } } } }); + await changed; + expect(source.out.available.peek().audio.enabled).toBe(enabled); + } + expect(queries).toHaveBeenCalledTimes(1); + } finally { + source.close(); + } +}); diff --git a/js/watch/src/support-cache.ts b/js/watch/src/support-cache.ts new file mode 100644 index 00000000000..238f9d5dd8c --- /dev/null +++ b/js/watch/src/support-cache.ts @@ -0,0 +1,7 @@ +/** Internal key describing which config fields can change a support verdict. */ +export const supportCacheKey = Symbol("supportCacheKey"); + +/** A support probe that can identify the fields its verdict depends on. */ +export type CacheableSupported = ((config: Config) => Promise) & { + [supportCacheKey]?: (config: Config) => string; +}; diff --git a/js/watch/src/video/config.ts b/js/watch/src/video/config.ts index 0c284366e48..79d20ce83f0 100644 --- a/js/watch/src/video/config.ts +++ b/js/watch/src/video/config.ts @@ -1,8 +1,5 @@ import type * as Catalog from "@moq/hang/catalog"; -/** Internal key used to describe which fields can change a support probe's result. */ -export const supportCacheKey = Symbol("supportCacheKey"); - /** The catalog fields that determine the demuxer and WebCodecs decoder instance. */ export type DecoderConfig = Pick< Catalog.VideoConfig, diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 4d0454eea9f..3b03566e408 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -17,14 +17,9 @@ import { import { rotateVideoDimensions } from "@moq/video"; import { base64ToBytes } from "../base64"; import { type Failure, failureMoved, nextMedia, subscribeMedia } from "../media"; +import { supportCacheKey } from "../support-cache"; import type { Sync } from "../sync"; -import { - type DecoderConfig, - decoderConfigKey, - type PlaybackIdentity, - playbackIdentity, - supportCacheKey, -} from "./config"; +import { type DecoderConfig, decoderConfigKey, type PlaybackIdentity, playbackIdentity } from "./config"; import { caughtUp, renditionJitter, switchJitter } from "./playhead"; import { Recovery } from "./recovery"; import type { Source } from "./source"; diff --git a/js/watch/src/video/source.test.ts b/js/watch/src/video/source.test.ts index 23a81562bb3..00227463dc1 100644 --- a/js/watch/src/video/source.test.ts +++ b/js/watch/src/video/source.test.ts @@ -3,7 +3,8 @@ import * as Catalog from "@moq/hang/catalog"; import { Path } from "@moq/net"; import { Signal } from "@moq/signals"; import { Broadcast } from "../broadcast"; -import { decoderConfigKey, supportCacheKey } from "./config"; +import { supportCacheKey } from "../support-cache"; +import { decoderConfigKey } from "./config"; import { Source } from "./source"; const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); diff --git a/js/watch/src/video/source.ts b/js/watch/src/video/source.ts index 5552eefd4d1..b3f58771c49 100644 --- a/js/watch/src/video/source.ts +++ b/js/watch/src/video/source.ts @@ -2,7 +2,7 @@ import * as Catalog from "@moq/hang/catalog"; import type * as Moq from "@moq/net"; import { Effect, type Getter, getter, type Inputs, type Readonlys, readonlys, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; -import { supportCacheKey } from "./config"; +import { type CacheableSupported, supportCacheKey } from "../support-cache"; /** * A function that checks if a video configuration can be played. @@ -11,10 +11,6 @@ import { supportCacheKey } from "./config"; */ export type Supported = (config: Catalog.VideoConfig) => Promise; -type CacheableSupported = Supported & { - [supportCacheKey]?: (config: Catalog.VideoConfig) => string; -}; - /** What the viewer wants from the video rendition; the source picks the best one within it. */ export type Target = { // Optional manual override for the selected rendition name. @@ -270,7 +266,7 @@ export class Source { const available: Record = {}; for (const [name, config] of Object.entries(renditions)) { - const cacheKey = (supported as CacheableSupported)[supportCacheKey]; + const cacheKey = (supported as CacheableSupported)[supportCacheKey]; const key = cacheKey ? cacheKey(config) : JSON.stringify(config); const cached = cache.get(name); let isSupported: boolean | undefined = false; diff --git a/quest/m1/push-to-talk/README.md b/quest/m1/push-to-talk/README.md index 1d13350a0e8..7d95effd828 100644 --- a/quest/m1/push-to-talk/README.md +++ b/quest/m1/push-to-talk/README.md @@ -57,7 +57,6 @@ encoder, and record why. - [JS consumers hold a disabled rendition](/quest/m1/push-to-talk/hold-js.md) - watch keeps a disabled audio or video rendition subscribed, and the hang spec says "should not select" - [moqsrc holds a disabled rendition](/quest/m1/push-to-talk/hold-rs.md) - moqsrc keeps its reader running under the selection rule moq play and moq-mux already apply - [`muted` and `invisible` keep capture open](/quest/m1/push-to-talk/mute.md) - the element disables only the encoder, so unmute skips getUserMedia and AudioContext setup -- [Watch caches audio codec support](/quest/m1/push-to-talk/audio-support-cache.md) - an unmute resumes audio without waiting on a support check ## Closes diff --git a/quest/m1/push-to-talk/audio-support-cache.md b/quest/m1/push-to-talk/audio-support-cache.md deleted file mode 100644 index 7f19ada9777..00000000000 --- a/quest/m1/push-to-talk/audio-support-cache.md +++ /dev/null @@ -1,17 +0,0 @@ -# [XS] Watch caches audio codec support - -## Goal - -`js/watch`'s audio source checks codec support once per rendition config, as -video does, so an unmute resumes the AudioContext without waiting on a support -check. - -## Plan - -Found 2026-10-28 by [moq-dev/moq#7155](https://github.com/moq-dev/moq/pull/7155): -the audio source re-checks codec support on every catalog change, while video -caches the result. So on unmute the AudioContext resumes only after a support -check of a few milliseconds, even though frames are already arriving. Share -the cache with video if that keeps it simpler. - -Public API: none. Wire: none. diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 89403170435..d6b56f98400 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -151,6 +151,7 @@ case "$1" in javascript slow-group just js slow-group-bench javascript jitter just js jitter-bench javascript audio-warmup just js audio-warmup-bench + javascript audio-support just js audio-support-bench javascript clock just js clock-bench ;; # The binding benchmark's runners at a token budget, so they keep working at this From 35332260c87bac888e718ddf5656f3be32361cf7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 09:15:26 -0700 Subject: [PATCH 3/8] test(watch): keep the support benchmark fixture browser-safe Co-Authored-By: GPT-6.1 Sol --- js/watch/bench/support.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/js/watch/bench/support.ts b/js/watch/bench/support.ts index b46a8147676..4ed994e639d 100644 --- a/js/watch/bench/support.ts +++ b/js/watch/bench/support.ts @@ -1,6 +1,6 @@ // Catalog-update costs with a warmed audio support verdict. Native probes run in browsers; // Bun's promise probe isolates selection overhead and probe counts for the nightly series. -import * as Catalog from "@moq/hang/catalog"; +import type * as Catalog from "@moq/hang/catalog"; import { Signal } from "@moq/signals"; import { decoderConfigKey } from "../src/audio/config"; import { Source } from "../src/audio/source"; @@ -11,7 +11,14 @@ import { supportCacheKey } from "../src/support-cache"; export async function run(): Promise { const native = typeof AudioDecoder !== "undefined"; const probe = native - ? async (config: Catalog.AudioConfig) => (await AudioDecoder.isConfigSupported(config)).supported ?? false + ? async (config: Catalog.AudioConfig) => + ( + await AudioDecoder.isConfigSupported({ + codec: config.codec, + sampleRate: config.sampleRate, + numberOfChannels: config.numberOfChannels, + }) + ).supported ?? false : async () => true; console.log("probe,renditions,updates,probes,us_per_update"); for (const count of [1, 8, 32]) { @@ -26,12 +33,12 @@ export async function run(): Promise { const renditions: Record = Object.fromEntries( Array.from({ length: count }, (_, i) => [ `audio${i}`, - Catalog.AudioConfigSchema.parse({ + { codec: "opus", container: { kind: "legacy" }, sampleRate: 48_000, numberOfChannels: 2, - }), + } as Catalog.AudioConfig, ]), ); const catalog = new Signal({ audio: { renditions } }); From 5ec4253563608b1c62c7d102d1d69d95a421591e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 09:18:34 -0700 Subject: [PATCH 4/8] chore(quest): preserve the audio cache validation blocker Co-Authored-By: GPT-6.1 Sol --- quest/m1/push-to-talk/README.md | 1 + quest/m1/push-to-talk/audio-support-cache.md | 34 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 quest/m1/push-to-talk/audio-support-cache.md diff --git a/quest/m1/push-to-talk/README.md b/quest/m1/push-to-talk/README.md index 7d95effd828..1d13350a0e8 100644 --- a/quest/m1/push-to-talk/README.md +++ b/quest/m1/push-to-talk/README.md @@ -57,6 +57,7 @@ encoder, and record why. - [JS consumers hold a disabled rendition](/quest/m1/push-to-talk/hold-js.md) - watch keeps a disabled audio or video rendition subscribed, and the hang spec says "should not select" - [moqsrc holds a disabled rendition](/quest/m1/push-to-talk/hold-rs.md) - moqsrc keeps its reader running under the selection rule moq play and moq-mux already apply - [`muted` and `invisible` keep capture open](/quest/m1/push-to-talk/mute.md) - the element disables only the encoder, so unmute skips getUserMedia and AudioContext setup +- [Watch caches audio codec support](/quest/m1/push-to-talk/audio-support-cache.md) - an unmute resumes audio without waiting on a support check ## Closes diff --git a/quest/m1/push-to-talk/audio-support-cache.md b/quest/m1/push-to-talk/audio-support-cache.md new file mode 100644 index 00000000000..b47abcb5586 --- /dev/null +++ b/quest/m1/push-to-talk/audio-support-cache.md @@ -0,0 +1,34 @@ +# [XS] Watch caches audio codec support + +## Goal + +`js/watch`'s audio source checks codec support once per rendition config, as +video does, so an unmute resumes the AudioContext without waiting on a support +check. + +## Plan + +The implementation and focused validation are present in +[moq-dev/moq#7248](https://github.com/moq-dev/moq/pull/7248). Audio follows the +video cache pattern and shares its internal key protocol. Built-in probes reuse +the decoder configuration; custom probes include all config fields. Removed +renditions are pruned, rejected or canceled probes are not cached, and close +releases the cache. + +Remaining: pass the full local gate before marking the PR ready. Two +`quest gate just ci` attempts failed in mbx with `Argument list too long` +during the WASM build and the Rust lint-tooling test. Removing unused Qt and +build-host flags from the environment did not resolve it. Keep this external +tooling failure outside the audio implementation's scope. + +Verified: 429 watch tests, TypeScript checking, configuration invalidation, +probe replacement/removal/cancellation/close, the real libav support probe, and +AudioContext suspension/resumption. A Chromium 156 benchmark with baseline and +identical A/A controls confirms no additional native support queries on warmed +mute/unmute updates. The benchmark runs in the nightly JS series. + +After the gate passes, resolve relevant findings, remove this quest and its +links, and mark the PR ready. Do not merge or enqueue it under the current +PR-only instruction. + +Public API: none. Wire: none. From 061b7f04ef6f2ba27ca7b59083401901025dea07 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:15:49 -0700 Subject: [PATCH 5/8] chore(quest): claim quest/m2/watch-support-probe.md Quest-Claim: 7e8400ce34632c2b From 36e02deb466c24e8d9ed1a6a9cf96c623f145424 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:20:30 -0700 Subject: [PATCH 6/8] feat(watch): one support probe for audio and video Audio and video Sources now probe rendition support through a shared SupportProbe that owns the probe race, the per-rendition config-keyed cache, pending, and the NotSupportedError. Audio gains video's cache and an out.error, and the unsupported indicator reports an audio-only broadcast it cannot play. Co-Authored-By: Claude Opus 5.5 --- js/watch/api/index.api.md | 1 + js/watch/src/audio/source.test.ts | 127 +++++++++++++++++- js/watch/src/audio/source.ts | 53 ++------ js/watch/src/support-probe.ts | 100 ++++++++++++++ .../components/unsupported-indicator.test.ts | 57 ++++++++ .../ui/components/unsupported-indicator.ts | 14 +- js/watch/src/video/config.ts | 3 - js/watch/src/video/decoder.ts | 9 +- js/watch/src/video/source.test.ts | 3 +- js/watch/src/video/source.ts | 76 +---------- 10 files changed, 313 insertions(+), 130 deletions(-) create mode 100644 js/watch/src/support-probe.ts create mode 100644 js/watch/src/ui/components/unsupported-indicator.test.ts diff --git a/js/watch/api/index.api.md b/js/watch/api/index.api.md index 1683a82b913..dca2227ed56 100644 --- a/js/watch/api/index.api.md +++ b/js/watch/api/index.api.md @@ -80,6 +80,7 @@ export namespace Audio { available: Signals.Signal>; catalog: Signals.Signal; config: Signals.Signal; + error: Signals.Signal; jitter: Signals.Signal; track: Signals.Signal; }; diff --git a/js/watch/src/audio/source.test.ts b/js/watch/src/audio/source.test.ts index d67b979bc60..31fb587ff07 100644 --- a/js/watch/src/audio/source.test.ts +++ b/js/watch/src/audio/source.test.ts @@ -19,10 +19,17 @@ function broadcast(renditions: Record) { return { catalog, broadcast: { out: { catalog } } as unknown as Broadcast }; } -// Wait on the probe's causal boundary, rather than a timer or a guessed number of ticks. +const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); + +// Wait out the probe, then a tick for the effects its result wakes. A probe served wholly from +// the cache sets and clears `pending` in one run, which notifies nothing, so start with a tick +// that lets the rerun happen rather than waiting on a change that may never come. async function settled(source: Source): Promise { - if (!source.pending.peek()) await source.pending.changed(); - while (source.pending.peek()) await source.pending.changed(); + await tick(); + while (source.pending.peek()) { + await source.pending.changed(); + await tick(); + } } test("a rejected rendition probe preserves its supported siblings", async () => { @@ -151,7 +158,7 @@ test("does not carry a hold to another broadcast", async () => { // The same catalog on another broadcast probes nothing new, so wait on a timer, not the probe. current.set(broadcast({ mic: config("opus", { enabled: false }) }).broadcast); - await new Promise((resolve) => setTimeout(resolve, 0)); + await tick(); expect(source.out.track.peek()).toBeUndefined(); } finally { source.close(); @@ -166,7 +173,7 @@ test("does not hold an enabled selection into another broadcast's disabled rendi expect(source.out.track.peek()).toBe("mic"); current.set(broadcast({ mic: config("opus", { enabled: false }) }).broadcast); - for (let i = 0; i < 5; i++) await new Promise((resolve) => setTimeout(resolve, 0)); + for (let i = 0; i < 5; i++) await tick(); expect(source.out.track.peek()).toBeUndefined(); expect(source.out.config.peek()).toBeUndefined(); } finally { @@ -194,3 +201,113 @@ test("holds on a broadcast whose renditions match the previous one", async () => source.close(); } }); + +async function quietly(fn: () => Promise): Promise { + const warn = console.warn; + console.warn = () => {}; + try { + await fn(); + } finally { + console.warn = warn; + } +} + +test("reports NotSupportedError when no rendition is supported", () => + quietly(async () => { + const state = broadcast({ mic: config("opus") }); + const source = new Source({ broadcast: state.broadcast, supported: async () => false }); + try { + await settled(source); + expect(source.out.error.peek()?.name).toBe("NotSupportedError"); + expect(source.out.available.peek()).toEqual({}); + + state.catalog.set({ audio: { renditions: {} } }); + await settled(source); + expect(source.out.error.peek()).toBeUndefined(); + } finally { + source.close(); + } + })); + +test("a rejecting probe alone is unsupported", () => + quietly(async () => { + const source = new Source({ + broadcast: broadcast({ mic: config("opus") }).broadcast, + supported: async () => { + throw new TypeError("probe failed"); + }, + }); + try { + await settled(source); + expect(source.out.error.peek()?.name).toBe("NotSupportedError"); + expect(source.out.track.peek()).toBeUndefined(); + } finally { + source.close(); + } + })); + +test("does not wedge the effect when a support probe never settles", async () => { + const supported = new Signal<(config: Catalog.AudioConfig) => Promise>(() => new Promise(() => {})); + const source = new Source({ broadcast: broadcast({ mic: config("opus") }).broadcast, supported }); + try { + if (!source.pending.peek()) await source.pending.changed(); + expect(source.out.available.peek()).toEqual({}); + expect(source.out.error.peek()).toBeUndefined(); + + // Swapping the probe reruns the effect, which has to tear the parked one down first. + supported.set(async () => true); + await settled(source); + expect(source.out.track.peek()).toBe("mic"); + } finally { + source.close(); + } +}); + +test("a catalog replaced while its probe is pending publishes only the replacement", async () => { + const probes = new Map void>(); + const state = broadcast({ old: config("opus") }); + const source = new Source({ + broadcast: state.broadcast, + supported: (c) => new Promise((resolve) => probes.set(c.codec, resolve)), + }); + try { + if (!source.pending.peek()) await source.pending.changed(); + const old = probes.get("opus"); + expect(old).toBeDefined(); + + state.catalog.set({ audio: { renditions: { next: config("mp4a.40.2") } } }); + while (!probes.has("mp4a.40.2")) await tick(); + + // The torn-down probe settles late; its result must not land. + old?.(true); + probes.get("mp4a.40.2")?.(true); + await settled(source); + expect(Object.keys(source.out.available.peek())).toEqual(["next"]); + expect(source.out.track.peek()).toBe("next"); + } finally { + source.close(); + } +}); + +test("serves unchanged configs from the cache when the catalog changes", async () => { + const state = broadcast({ mic: config("opus") }); + const probed: string[] = []; + const source = new Source({ + broadcast: state.broadcast, + supported: async (c) => { + probed.push(c.codec); + return true; + }, + }); + try { + await settled(source); + expect(probed).toEqual(["opus"]); + + state.catalog.set({ audio: { renditions: { mic: config("opus"), backup: config("mp4a.40.2") } } }); + await settled(source); + expect(probed).toEqual(["opus", "mp4a.40.2"]); + expect(Object.keys(source.out.available.peek())).toEqual(["mic", "backup"]); + } finally { + source.close(); + } +}); diff --git a/js/watch/src/audio/source.ts b/js/watch/src/audio/source.ts index 901476fedd0..0641d075d45 100644 --- a/js/watch/src/audio/source.ts +++ b/js/watch/src/audio/source.ts @@ -2,6 +2,7 @@ import { Milli } from "@moq/clock"; import type * as Catalog from "@moq/hang/catalog"; import { Effect, type Getter, getter, type Inputs, type Readonlys, readonlys, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; +import { SupportProbe } from "../support-probe"; /** What the viewer wants from the audio rendition. */ export type Target = { @@ -32,6 +33,11 @@ export type SourceInput = { export type SourceOutput = { catalog: Signal; available: Signal>; + + // Why no rendition can be chosen, or undefined while healthy or still probing. A + // `NotSupportedError` means the browser can decode none of the catalog's renditions. + error: Signal; + track: Signal; config: Signal; @@ -47,18 +53,20 @@ export class Source { /** The wired inputs, read-only. */ readonly in: Readonlys; + #probe = new SupportProbe("audio"); + readonly #out: SourceOutput = { catalog: new Signal(undefined), available: new Signal>({}), + error: this.#probe.error, track: new Signal(undefined), config: new Signal(undefined), jitter: new Signal(undefined), }; /** The derived outputs, read-only. */ readonly out = readonlys(this.#out); - #pending = new Signal(false); /** Whether rendition support is still being resolved. @internal */ - readonly pending: Getter = this.#pending; + readonly pending: Getter = this.#probe.pending; // The last selection, which a disable holds while no enabled rendition remains. #held?: { broadcast: Broadcast; track: string }; @@ -92,46 +100,13 @@ export class Source { } #runSupported(effect: Effect): void { + const supported = effect.get(this.in.supported); // Read the catalog from the broadcast itself, so the probe result is tagged with the right one. const broadcast = effect.get(this.in.broadcast); const renditions = (broadcast && effect.get(broadcast.out.catalog)?.audio?.renditions) ?? {}; - const supported = effect.get(this.in.supported); - if (!supported) return; - - effect.set(this.#pending, true, false); - effect.spawn(async () => { - try { - const available: Record = {}; - - for (const [name, config] of Object.entries(renditions)) { - // `supported` comes from the consumer, so we cannot assume it ever settles. A rerun - // waits for the tasks it spawned, so an unraced probe would hold the next run shut. - let isSupported = false; - try { - isSupported = !!(await effect.race(supported(config))); - } catch (err) { - if (effect.abort.aborted) return; - console.warn( - `[Source] audio rendition ${name} (${config.codec}) support probe failed; treating as unsupported`, - err, - ); - } - - // Torn down: stop probing and publish nothing, since the rerun redoes this. - if (effect.abort.aborted) return; - - if (isSupported) available[name] = config; - } - - if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) { - console.warn("no supported audio renditions found:", renditions); - } - - this.#probed.set(broadcast); - this.#out.available.set(available); - } finally { - this.#pending.set(false); - } + this.#probe.run(effect, supported, renditions, (available) => { + this.#probed.set(broadcast); + this.#out.available.set(available); }); } diff --git a/js/watch/src/support-probe.ts b/js/watch/src/support-probe.ts new file mode 100644 index 00000000000..0af9ffe5b6d --- /dev/null +++ b/js/watch/src/support-probe.ts @@ -0,0 +1,100 @@ +import { type Effect, type Getter, Signal } from "@moq/signals"; + +/** Internal key used to describe which fields can change a support probe's result. */ +export const supportCacheKey = Symbol("supportCacheKey"); + +/** A function that checks if a rendition configuration can be played. */ +type Supported = ((config: Config) => Promise) & { + [supportCacheKey]?: (config: Config) => string; +}; + +/** + * Probes which renditions a `supported` function accepts, shared by the audio and video sources. + * + * A result is cached per rendition until the rendition's config changes, keyed by the probe's + * `supportCacheKey` or else the whole config. + */ +export class SupportProbe { + readonly #kind: string; + + readonly #pending = new Signal(false); + /** Whether rendition support is still being resolved. */ + readonly pending: Getter = this.#pending; + + /** A `NotSupportedError` once no rendition of a non-empty catalog is supported. */ + readonly error = new Signal(undefined); + + #cache = new WeakMap, Map>(); + + constructor(kind: "audio" | "video") { + this.#kind = kind; + } + + /** + * Probe `renditions` within `effect`, then `publish` the supported ones. + * Nothing is published when the effect is torn down first, since its rerun redoes this. + */ + run( + effect: Effect, + supported: Supported | undefined, + renditions: Record, + publish: (available: Record) => void, + ): void { + this.error.set(undefined); + if (!supported) return; + + let cache = this.#cache.get(supported); + if (!cache) { + cache = new Map(); + this.#cache.set(supported, cache); + } + const names = new Set(Object.keys(renditions)); + for (const name of cache.keys()) { + if (!names.has(name)) cache.delete(name); + } + + const cacheKey = supported[supportCacheKey] ?? ((config: Config) => JSON.stringify(config)); + + effect.set(this.#pending, true, false); + effect.spawn(async () => { + const available: Record = {}; + + for (const [name, config] of Object.entries(renditions)) { + const key = cacheKey(config); + const cached = cache.get(name); + let isSupported = cached?.key === key ? cached.supported : undefined; + if (isSupported === undefined) { + try { + // `supported` comes from the consumer, so we cannot assume it ever settles. A + // rerun waits for the tasks it spawned, so an unraced probe would hold it shut. + const result = await effect.race(supported(config)); + // Undefined when torn down; the abort check below handles it. + if (result !== undefined) { + isSupported = !!result; + // Recompute the key: a probe may rewrite the config it accepted. + cache.set(name, { key: cacheKey(config), supported: isSupported }); + } + } catch (err) { + if (effect.abort.aborted) return; + console.warn( + `[Source] ${this.#kind} rendition ${name} (${config.codec}) support probe failed; treating as unsupported`, + err, + ); + } + } + + // Torn down: stop probing and publish nothing, since the rerun redoes this. + if (effect.abort.aborted) return; + + if (isSupported) available[name] = config; + } + + if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) { + console.warn(`[Source] no supported ${this.#kind} renditions found:`, renditions); + this.error.set(new DOMException(`no supported ${this.#kind} renditions`, "NotSupportedError")); + } + publish(available); + this.#pending.set(false); + }); + } +} diff --git a/js/watch/src/ui/components/unsupported-indicator.test.ts b/js/watch/src/ui/components/unsupported-indicator.test.ts new file mode 100644 index 00000000000..20cfb3f123f --- /dev/null +++ b/js/watch/src/ui/components/unsupported-indicator.test.ts @@ -0,0 +1,57 @@ +import { expect, test } from "bun:test"; +import { Effect, Signal } from "@moq/signals"; +import type MoqWatch from "../../element"; +import { unsupportedIndicator } from "./unsupported-indicator"; + +const unsupported = () => new DOMException("no supported renditions", "NotSupportedError"); + +test("reports unsupported audio only when the broadcast has no video", async () => { + // The only DOM the component touches: a container holding a text span. + const container = { style: { display: "" }, setAttribute: () => {}, appendChild: () => {} }; + const text = { textContent: "" }; + const original = globalThis.document; + let created = 0; + globalThis.document = { + createElement: () => (created++ === 0 ? container : text), + } as unknown as Document; + + const video = { + catalog: new Signal<{ renditions: Record } | undefined>(undefined), + error: new Signal(undefined), + }; + const audio = { + catalog: new Signal({ renditions: { mic: { codec: "opus" } } }), + error: new Signal(undefined), + }; + const watch = { + broadcast: { out: { status: new Signal("live") } }, + video: { source: { out: video } }, + audio: { source: { out: audio } }, + } as unknown as MoqWatch; + + const effect = new Effect(); + const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); + try { + unsupportedIndicator(effect, watch); + await tick(); + expect(container.style.display).toBe("none"); + + audio.error.set(unsupported()); + await tick(); + expect(container.style.display).toBe(""); + expect(text.textContent).toBe("This audio codec is not supported by your browser: opus"); + + // Playable video carries the broadcast, so unsupported audio alone is not reported. + video.catalog.set({ renditions: { hd: { codec: "avc1.640028" } } }); + await tick(); + expect(container.style.display).toBe("none"); + + video.error.set(unsupported()); + await tick(); + expect(container.style.display).toBe(""); + expect(text.textContent).toBe("This video codec is not supported by your browser: avc1.640028"); + } finally { + effect.close(); + globalThis.document = original; + } +}); diff --git a/js/watch/src/ui/components/unsupported-indicator.ts b/js/watch/src/ui/components/unsupported-indicator.ts index db02f72bc63..a6aabca0ed8 100644 --- a/js/watch/src/ui/components/unsupported-indicator.ts +++ b/js/watch/src/ui/components/unsupported-indicator.ts @@ -1,7 +1,7 @@ import type { Effect } from "@moq/signals"; import type MoqWatch from "../../element"; -/** Shows when the player needs an update or cannot decode any video rendition. */ +/** Shows when the player needs an update, or cannot decode the video or, lacking video, the audio. */ export function unsupportedIndicator(parent: Effect, watch: MoqWatch): HTMLElement { const container = document.createElement("div"); container.className = "watch-ui__notice watch-ui__unsupported-indicator"; @@ -20,16 +20,20 @@ export function unsupportedIndicator(parent: Effect, watch: MoqWatch): HTMLEleme return; } - const unsupported = effect.get(watch.video.source.out.error)?.name === "NotSupportedError"; + // Audio speaks for the broadcast only when there is no video to report on. + const video = effect.get(watch.video.source.out.catalog); + const kind = video ? "video" : "audio"; + const source = video ? watch.video.source : watch.audio.source; + const unsupported = effect.get(source.out.error)?.name === "NotSupportedError"; const show = unsupported && (status === "loading" || status === "live"); container.style.display = show ? "" : "none"; if (!show) return; - const renditions = effect.get(watch.video.source.out.catalog)?.renditions ?? {}; + const renditions = (video ?? effect.get(watch.audio.source.out.catalog))?.renditions ?? {}; const codecs = [...new Set(Object.values(renditions).map((rendition) => rendition.codec))].join(", "); text.textContent = codecs - ? `This video codec is not supported by your browser: ${codecs}` - : "This video codec is not supported by your browser"; + ? `This ${kind} codec is not supported by your browser: ${codecs}` + : `This ${kind} codec is not supported by your browser`; }); return container; diff --git a/js/watch/src/video/config.ts b/js/watch/src/video/config.ts index 0c284366e48..79d20ce83f0 100644 --- a/js/watch/src/video/config.ts +++ b/js/watch/src/video/config.ts @@ -1,8 +1,5 @@ import type * as Catalog from "@moq/hang/catalog"; -/** Internal key used to describe which fields can change a support probe's result. */ -export const supportCacheKey = Symbol("supportCacheKey"); - /** The catalog fields that determine the demuxer and WebCodecs decoder instance. */ export type DecoderConfig = Pick< Catalog.VideoConfig, diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 78994dabcb5..010f86bbe4c 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -17,14 +17,9 @@ import { import { rotateVideoDimensions } from "@moq/video"; import { base64ToBytes } from "../base64"; import { type Failure, failureMoved, nextMedia, subscribeMedia } from "../media"; +import { supportCacheKey } from "../support-probe"; import type { Sync } from "../sync"; -import { - type DecoderConfig, - decoderConfigKey, - type PlaybackIdentity, - playbackIdentity, - supportCacheKey, -} from "./config"; +import { type DecoderConfig, decoderConfigKey, type PlaybackIdentity, playbackIdentity } from "./config"; import { caughtUp, renditionJitter, switchJitter } from "./playhead"; import { Recovery } from "./recovery"; import type { Source } from "./source"; diff --git a/js/watch/src/video/source.test.ts b/js/watch/src/video/source.test.ts index 960a0598abc..d5a13d59f3b 100644 --- a/js/watch/src/video/source.test.ts +++ b/js/watch/src/video/source.test.ts @@ -3,7 +3,8 @@ import * as Catalog from "@moq/hang/catalog"; import { Path } from "@moq/net"; import { Signal } from "@moq/signals"; import { Broadcast } from "../broadcast"; -import { decoderConfigKey, supportCacheKey } from "./config"; +import { supportCacheKey } from "../support-probe"; +import { decoderConfigKey } from "./config"; import { Source } from "./source"; const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); diff --git a/js/watch/src/video/source.ts b/js/watch/src/video/source.ts index 6ddbf1922c3..31162082e73 100644 --- a/js/watch/src/video/source.ts +++ b/js/watch/src/video/source.ts @@ -2,7 +2,7 @@ import * as Catalog from "@moq/hang/catalog"; import type * as Moq from "@moq/net"; import { Effect, type Getter, getter, type Inputs, type Readonlys, readonlys, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; -import { supportCacheKey } from "./config"; +import { SupportProbe } from "../support-probe"; /** * A function that checks if a video configuration can be played. @@ -11,10 +11,6 @@ import { supportCacheKey } from "./config"; */ export type Supported = (config: Catalog.VideoConfig) => Promise; -type CacheableSupported = Supported & { - [supportCacheKey]?: (config: Catalog.VideoConfig) => string; -}; - /** What the viewer wants from the video rendition; the source picks the best one within it. */ export type Target = { // Optional manual override for the selected rendition name. @@ -207,18 +203,19 @@ export class Source { /** The wired inputs, read-only. */ readonly in: Readonlys; + #probe = new SupportProbe("video"); + readonly #out: SourceOutput = { catalog: new Signal(undefined), available: new Signal>({}), - error: new Signal(undefined), + error: this.#probe.error, track: new Signal(undefined), config: new Signal(undefined), }; /** The derived outputs, read-only. */ readonly out = readonlys(this.#out); - #pending = new Signal(false); /** Whether rendition support is still being resolved. @internal */ - readonly pending: Getter = this.#pending; + readonly pending: Getter = this.#probe.pending; // The last selection, which a disable holds while no enabled rendition remains. #held?: { broadcast: Broadcast; track: string }; @@ -228,7 +225,6 @@ export class Source { #probed = new Signal(undefined); #signals = new Effect(); - #supportCache = new WeakMap>(); constructor(props?: Inputs) { this.in = { @@ -255,72 +251,12 @@ export class Source { #runSupported(effect: Effect): void { const supported = effect.get(this.in.supported); - if (!supported) { - this.#out.error.set(undefined); - return; - } - // Read the catalog from the broadcast itself, so the probe result is tagged with the right one. const broadcast = effect.get(this.in.broadcast); const renditions = (broadcast && effect.get(broadcast.out.catalog)?.video?.renditions) ?? {}; - this.#out.error.set(undefined); - let cache = this.#supportCache.get(supported); - if (!cache) { - cache = new Map(); - this.#supportCache.set(supported, cache); - } - const names = new Set(Object.keys(renditions)); - for (const name of cache.keys()) { - if (!names.has(name)) cache.delete(name); - } - - effect.set(this.#pending, true, false); - effect.spawn(async () => { - const available: Record = {}; - - for (const [name, config] of Object.entries(renditions)) { - const cacheKey = (supported as CacheableSupported)[supportCacheKey]; - const key = cacheKey ? cacheKey(config) : JSON.stringify(config); - const cached = cache.get(name); - let isSupported: boolean | undefined = false; - if (cached?.key === key) { - isSupported = cached.supported; - } else { - let failed = false; - try { - // `supported` comes from the consumer, so we cannot assume it ever settles. A - // rerun waits for the tasks it spawned, so an unraced probe would hold it shut. - isSupported = await effect.race(supported(config)); - } catch (err) { - failed = true; - console.warn( - `[Source] video rendition ${name} (${config.codec}) support probe failed; treating as unsupported`, - err, - ); - } - if (!failed && isSupported !== undefined) { - cache.set(name, { - key: cacheKey ? cacheKey(config) : JSON.stringify(config), - supported: isSupported, - }); - } - } - - // Torn down: stop probing and publish nothing, since the rerun redoes this. - if (effect.abort.aborted) return; - - if (isSupported) available[name] = config; - } - - if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) { - console.warn("[Source] No supported video renditions found:", renditions); - this.#out.error.set(new DOMException("no supported video renditions", "NotSupportedError")); - } else { - this.#out.error.set(undefined); - } + this.#probe.run(effect, supported, renditions, (available) => { this.#probed.set(broadcast); this.#out.available.set(available); - this.#pending.set(false); }); } From 6782f76c845f835edaa64356e6677cb0150c2a95 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 05:24:40 -0700 Subject: [PATCH 7/8] chore(quest): finish quest/m2/watch-support-probe.md Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 - quest/m2/watch-support-probe.md | 26 -------------------------- 2 files changed, 27 deletions(-) delete mode 100644 quest/m2/watch-support-probe.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 4f4f2452b0a..ff27dae1f9d 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -148,7 +148,6 @@ lite-07; bitrate enforcement is not part of this release. - [Hang contribution references](/quest/m2/contribution-ref.md) - a catalog names a whole contribution broadcast that a player opens only when selected, backing moq.pro's composed rooms - [Synced data playback](/quest/m2/watch-data-sync.md) - js/watch releases JSON and binary payloads on the media playhead, and a slow data track holds media back - [Watch decode gate](/quest/m2/watch-decode-gate.md) - video lookahead stays encoded until it is near presentation, like audio -- [One support probe for watch audio and video](/quest/m2/watch-support-probe.md) - one probe helper with video's cache, and audio reports unsupported renditions - [Shared fullscreen, formatters, and stats cards in @moq/ui](/quest/m2/ui-shared-controls.md) - watch and publish stop keeping copies of their shared controls - [moqsrc A/V sync](/quest/m2/moqsrc-av-sync.md) - a nightly test measures A/V skew from `moqsrc` through clock-synced sinks - [MP4 export](/quest/m2/mp4-export.md) - `moq export mp4 --output` records crash-safe fragments, then finishes a regular MP4 with moov at the end diff --git a/quest/m2/watch-support-probe.md b/quest/m2/watch-support-probe.md deleted file mode 100644 index d41718ea7b5..00000000000 --- a/quest/m2/watch-support-probe.md +++ /dev/null @@ -1,26 +0,0 @@ -# [S] One support probe for watch audio and video - -## Goal - -The watch audio and video `Source`s probe rendition support through one -private helper that owns the race against an unsettled probe, the config-keyed -cache, `pending`, and abort. Audio gains video's cache and an `out.error` that -is `NotSupportedError` when no audio rendition is supported, and the bundled -UI's unsupported indicator reports an audio-only broadcast it cannot play. -Rendition selection stays separate per kind. Additive `@moq/watch` API; no -wire impact. - -## Plan - -Decided 2026-10-27 (maintainer interview, from the 2026-10-27 refactor audit): - -- Audio exposes `out.error` like video, and the unsupported indicator is its - consumer: it fires when video is unsupported, or when the catalog has no - video and audio is unsupported. -- Video's `#runSupported` (`js/watch/src/video/source.ts`) caches per - rendition by `supportCacheKey` or the JSON config and sets the error; audio's - (`js/watch/src/audio/source.ts`) re-probes every run and only logs. The - helper takes video's behavior for both. -- Test a catalog replaced while a probe is pending, a probe that never - settles, a rejecting probe, and a catalog change that leaves configs - unchanged (served from cache). From 3689fc104b012b6eddfd8e607985cea402b9dde1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 12:27:53 -0700 Subject: [PATCH 8/8] style(watch): format the mute/unmute support test Co-Authored-By: Claude Opus 5.5 --- js/watch/src/audio/source.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/watch/src/audio/source.test.ts b/js/watch/src/audio/source.test.ts index 1bf32c7cfe8..e1541623a53 100644 --- a/js/watch/src/audio/source.test.ts +++ b/js/watch/src/audio/source.test.ts @@ -329,7 +329,9 @@ test("mute and unmute reuse the decoder support verdict and publish the new meta try { await settled(source); for (const enabled of [false, true, false, true]) { - state.catalog.set({ audio: { renditions: { mic: config("opus", { enabled, jitter: 20, bitrate: 96_000 }) } } }); + state.catalog.set({ + audio: { renditions: { mic: config("opus", { enabled, jitter: 20, bitrate: 96_000 }) } }, + }); await settled(source); expect(source.out.available.peek().mic.enabled).toBe(enabled); expect(source.out.track.peek()).toBe("mic");