diff --git a/flake.nix b/flake.nix index 347a3b3d641..22f6bf539ff 100644 --- a/flake.nix +++ b/flake.nix @@ -713,6 +713,11 @@ } // pkgs.lib.optionalAttrs (!pkgs.stdenv.hostPlatform.isDarwin) { ALSA_PLUGIN_DIR = "${alsaPlugins}/lib/alsa-lib"; + # Firefox loads these at runtime; harness.ts exposes them only to its process. + PLAYWRIGHT_FIREFOX_LIBRARY_PATH = pkgs.lib.makeLibraryPath [ + pkgs.ffmpeg + pkgs.libpulseaudio + ]; }; }; diff --git a/js/publish/src/broadcast.test.ts b/js/publish/src/broadcast.test.ts index 9b77f2a5388..71d1ebfb5f4 100644 --- a/js/publish/src/broadcast.test.ts +++ b/js/publish/src/broadcast.test.ts @@ -276,6 +276,31 @@ test("hands the producer to the rendition without a subscriber and clears it whe broadcast.close(); }); +// The audio encoder ends its epoch whenever its track changes, so another rendition's rename must +// not take the track away even for a microtask. +test("renaming one rendition leaves the others' producers in place", async () => { + const broadcast = new Broadcast({ enabled: true, origin: new Origin.Producer(), name: Path.from("test.hang") }); + const video = broadcast.video("video"); + video.config.set(videoConfig); + const audio = broadcast.audio("audio"); + audio.config.set(audioConfig); + await settle(); + + const track = audio.track.peek(); + expect(track).toBeDefined(); + const seen: unknown[] = []; + const dispose = audio.track.subscribe((value) => seen.push(value)); + + video.config.set({ ...videoConfig, codec: "vp8" }); + await settle(); + expect(video.track.peek()?.name).toBe("video/1"); + expect(seen).toEqual([]); + expect(audio.track.peek()).toBe(track); + + dispose(); + broadcast.close(); +}); + test("on demand hands the producer to the rendition only while a subscriber is attached", async () => { const broadcast = new Broadcast({ enabled: true, diff --git a/js/publish/src/broadcast.ts b/js/publish/src/broadcast.ts index 3ab3c9cc9d1..0344e5004cf 100644 --- a/js/publish/src/broadcast.ts +++ b/js/publish/src/broadcast.ts @@ -515,6 +515,9 @@ export class Broadcast { // Static tracks fan out to every subscriber. The encoder-facing handle stays set while the // track is open. Audio keeps it while buffering PCM; on-demand video gates it on a subscriber. + effect.cleanup(() => { + for (const handle of this.#handles.values()) handle.track.set(undefined); + }); effect.run((inner) => { const renditions = inner.get(this.#renditions); inner.get(publication.renames); @@ -539,6 +542,7 @@ export class Broadcast { } else if (entry.track && entry.track.closed.peek() !== undefined) { // A failure leaves this rendition down until it clears. It then recovers under a fresh // track name, so its group sequence can start over. + handle.track.set(undefined); if (!cause) publication.rename(entry); continue; } @@ -551,20 +555,23 @@ export class Broadcast { } entry.owner = rendition; const served = entry.track; + // Set directly rather than undone on cleanup: this effect reruns for every rendition when + // any one registers or renames, and the gate's rerun lands a microtask after its cleanup, + // so an unchanged rendition would see its track vanish and return. Audio ends its epoch on + // any track change. inner.run((gate) => { const closed = gate.get(served.closed); if (closed !== undefined) { // A producer aborting its track directly fails the rendition the same way. if (closed && !entry.slot.error.peek()) entry.slot.error.set(closed); + handle.track.set(undefined); return; } - if ( + const idle = rendition.kind !== "audio" && gate.get(this.in.onDemand) && - gate.get(served.demand().wanted) === undefined - ) - return; - gate.set(handle.track, served); + gate.get(served.demand().wanted) === undefined; + handle.track.set(idle ? undefined : served); }); } }); diff --git a/js/watch/src/audio/decoder.test.ts b/js/watch/src/audio/decoder.test.ts index 1c6060d407f..e6bbc94b16d 100644 --- a/js/watch/src/audio/decoder.test.ts +++ b/js/watch/src/audio/decoder.test.ts @@ -554,19 +554,32 @@ for (const cmaf of [false, true]) { }); } - // A fresh encoder after a declared end may restart its timeline below the old one, and the decoder - // resets for it before anything is judged out of order. - it(`decodes a ${cmaf ? "CMAF" : "legacy"} fresh encoder below the timeline it replaced`, async () => { - const playback = await play(Milli(100), false, cmaf); + // Publishers keep timestamps moving forward across a declared end, so a later group restarts the + // codec on the same timeline: the audio decoded before the end still plays and the clock holds. + it(`keeps ${cmaf ? "CMAF" : "legacy"} PCM and the clock across a declared end`, async () => { + const playback = await play(Milli(500), false, cmaf); + const reset = spyOn(playback.sync, "reset"); + const prime = spyOn(AudioDecoder.prototype, "reset"); try { - frameTimestamp = 480_000; - playback.push({ ...frame(), group: 0n }); - await microtasks(); - playback.push({ kind: "end", group: 0n, end: Micro(520_000) }); - frameTimestamp = 0; + await playback.play(7); + const ring = playback.rings().at(-1); + if (!ring) throw new Error("missing audio ring"); + const held = ring.length; + expect(held).toBe(7 * 960); + + playback.push({ kind: "end", group: 0n, end: Micro(frameTimestamp + 20_000) }); + // The fresh encoder resumes 40 ms past the end, as a capture gap leaves it. + frameTimestamp += 40_000; playback.push({ ...frame(), group: 1n }); await microtasks(); - expect(playback.inserted()).toEqual([500_000, 20_000]); + + expect(prime).toHaveBeenCalledTimes(1); + expect(reset).not.toHaveBeenCalled(); + expect(playback.resets()).toBe(0); + expect(playback.rings()).toEqual([ring]); + // The gap stays silent between what was buffered and the resumed frame. + expect(ring.length).toBe(held + 3 * 960); + expect(playback.inserted().at(-1)).toBe(frameTimestamp); } finally { playback.close(); await microtasks(); diff --git a/js/watch/src/audio/decoder.ts b/js/watch/src/audio/decoder.ts index 4dad4c81cb9..0a5f972efa4 100644 --- a/js/watch/src/audio/decoder.ts +++ b/js/watch/src/audio/decoder.ts @@ -604,7 +604,7 @@ export class Decoder { // The track ending, or a gap or end, cuts a held head short. if (!(await play(order.flush()))) return; if (!next) break; - if (this.#onRead(next)) { + if (this.#terminal.update(next)) { this.#reprime(decoder, decoderConfig, order); } if (next.kind === "gap") this.#arrivals.write(gap(next)); @@ -691,7 +691,7 @@ export class Decoder { // The track ending, or a gap or end, cuts a held head short. if (!(await play(order.flush()))) return; if (!next) break; - if (this.#onRead(next)) { + if (this.#terminal.update(next)) { this.#reprime(decoder, decoderConfig, order); } if (next.kind === "gap") this.#arrivals.write(gap(next)); @@ -716,6 +716,10 @@ export class Decoder { // Decode released frames, resetting the decoder before the first frame of a new codec epoch and // only then dropping one that is out of order. The terminal sees them in decode order, so it // anchors on the oldest. Reports whether to keep going. + // + // A new epoch (a gap, or a later group after a declared end) restarts the codec, never the + // timeline: publishers keep timestamps moving forward across a declared end, so the PCM decoded + // before it still plays and the shared clock stays anchored. async #play( effect: Effect, decoder: AudioDecoder, @@ -724,7 +728,7 @@ export class Decoder { reads: FrameRead[], ): Promise { for (const read of reads) { - if (this.#onRead(read)) { + if (this.#terminal.update(read)) { this.#reprime(decoder, config, order); } if (!order.admit(read)) continue; @@ -980,18 +984,6 @@ export class Decoder { this.#ring.peek()?.reset(); } - // Apply an ordered container read before handling it, reporting whether a codec epoch ended: - // a gap, or a later group after a declared end. - #onRead(read: Container.Read): boolean { - if (!this.#terminal.update(read)) return false; - // A hole restarts codec prediction on the same timeline, keeping decoded PCM and parked video. - if (read.kind === "frame") { - this.sync.reset(); - this.reset(); - } - return true; - } - /** * Observe every frame and gap received from now until the handle or this decoder closes. * diff --git a/js/watch/src/audio/terminal.ts b/js/watch/src/audio/terminal.ts index c349a70a80d..00462d10471 100644 --- a/js/watch/src/audio/terminal.ts +++ b/js/watch/src/audio/terminal.ts @@ -79,7 +79,7 @@ export class Terminal { return reset; } - // Start a fresh timeline after its declared end. + // Start a fresh codec epoch after its declared end, on the same timeline. #restart(): void { this.#started = false; this.#end = undefined; diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index 76f225a7a5d..efd01f6ec95 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -96,7 +96,6 @@ Public API: none. Wire: none. ## Required - [IETF22 fallback](/quest/m0/test-flakes-2/ietf22-fallback.md) - an IETF22 browser viewer follows a fallback within the media lane's bound, keeping its session and its audio -- [Browser buffering and Firefox audio-tone tests pass under load](/quest/m0/test-flakes-2/browser-media-flakes.md) - the two `harness.browser.ts` buffering tests and the Firefox player's audio-tone checks pass reliably in Interop and Nightly, each fixed at its cause under the epic's rules - [moq-gst's interleaved restart test passes under load](/quest/m0/test-flakes-2/gst-reconnect-interleave.md) - `restarts_under_reconnects_interleave_on_one_pad` passes in loaded runs and the merge queue, its missed delivery and its stray `Eos` each fixed at the cause - [reload.test.ts's close-drain tests pass under load](/quest/m0/test-flakes-2/reload-close-drain.md) - the drain cases in `js/net`'s reload tests pass in loaded multi-file runs - [dispatch-query's terminate test survives load](/quest/m0/test-flakes-2/dispatch-query-terminate.md) - `test_terminating_the_run_stops_the_running_phase` passes reliably under host load, re-enabled once fixed diff --git a/quest/m0/test-flakes-2/browser-media-flakes.md b/quest/m0/test-flakes-2/browser-media-flakes.md deleted file mode 100644 index df8a323f2b5..00000000000 --- a/quest/m0/test-flakes-2/browser-media-flakes.md +++ /dev/null @@ -1,47 +0,0 @@ -# [S] Browser buffering and Firefox audio-tone tests pass under load - -## Goal - -The two `harness.browser.ts` buffering tests and the Firefox player's -audio-tone checks pass reliably in Interop and Nightly, each fixed at its -cause under the epic's rules. The Nix dev shell runs local Firefox media -tests without the host's libraries on `LD_LIBRARY_PATH`. - -## Plan - -Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 /quest-complete run), from [moq-dev/moq#7402](https://github.com/moq-dev/moq/pull/7402) and [moq-dev/moq#7255](https://github.com/moq-dev/moq/pull/7255). Milestone: ✅ m0 (user, "all recommended" for quests 1-8). - -- **Buffering tests**: `the paused play button stays clickable during - {audio,video} stalling` (`test/interop/clients/js/harness.browser.ts:30`, - at be39188000) hit their 30 s timeout on main. They passed in scheduled - Interop - [40896908858](https://github.com/moq-dev/moq/actions/runs/40896908858) - (2026-10-29, under 1 s each) and failed in - [41075505055](https://github.com/moq-dev/moq/actions/runs/41075505055) - (2026-10-30), and #7402 saw them time out locally on `origin/main`. They - still time out in - [41317901057](https://github.com/moq-dev/moq/actions/runs/41317901057) - (2026-11-01, "waiting for locator('.buffering') to be hidden"). Find - which wait never resolves (the `.buffering` spinner, the Pause click, or - `.center-play`) and whether a change between those runs caused it; the - fixture is `test/interop/clients/js/buffering/`. If it is a regression in - `@moq/watch`, fix the element, not the test. -- **Firefox audio tone**: `audio tone: after resume` and - `audio tone: coded-size growth` (the `audio tone` check in `measure`, - `test/interop/clients/js/media.ts:604`, reached from the `after resume` - and `coded-size growth` steps at `:1171` and `:1100`) flake under host - load with either media fixture: #7255 measured main's fixture at 2/3 for - `rename,pause`. Run the Firefox lane - (`just test media --media-engines firefox`) under load to find what the - tone check samples too early. -- **Dev shell**: in the Nix dev shell, Firefox needs libavcodec and - libpulse for media; CI installs them, and #7255 had to expose the host's - through `LD_LIBRARY_PATH`. Provide them to the Playwright Firefox from the - flake, scoped to the test launch rather than the whole shell if that is - simpler. - -Public API: none. Wire: none. - -## Related - -- [Interop and Video pass on main](/quest/m0/interop-green/README.md) - the scheduled Interop run these tests fail in diff --git a/test/interop/clients/js/buffering/index.html b/test/interop/clients/js/buffering/index.html index 21419d890b5..19338cd6e53 100644 --- a/test/interop/clients/js/buffering/index.html +++ b/test/interop/clients/js/buffering/index.html @@ -2,6 +2,7 @@ + Paused buffering controls diff --git a/test/interop/clients/js/harness.browser.ts b/test/interop/clients/js/harness.browser.ts index e7256e6d0d6..652eb2f6a9d 100644 --- a/test/interop/clients/js/harness.browser.ts +++ b/test/interop/clients/js/harness.browser.ts @@ -1,8 +1,9 @@ import { expect, test } from "bun:test"; import type MoqWatch from "@moq/watch/element"; +import type { Locator } from "playwright"; import { createServer } from "vite"; import { worklet } from "../../../../js/common/vite-plugin-worklet"; -import { launch, pause } from "./harness"; +import { launch, open, pause, waitFor } from "./harness"; test("pause activates its button when a canvas intercepts pointer input", async () => { const browser = await launch(); @@ -28,6 +29,7 @@ test("pause activates its button when a canvas intercepts pointer input", async for (const stall of ["audio", "video"]) { test(`the paused play button stays clickable during ${stall} stalling`, async () => { + const deadline = Date.now() + 30_000; const server = await createServer({ configFile: false, root: `${import.meta.dir}/buffering`, @@ -36,18 +38,27 @@ for (const stall of ["audio", "video"]) { await server.listen(); const browser = await launch(); try { - const page = await browser.newPage(); - await page.goto(`${server.resolvedUrls?.local[0]}?${stall === "video" ? "video" : ""}`); + const [page, errors] = await open( + browser, + `${server.resolvedUrls?.local[0]}?${stall === "video" ? "video" : ""}`, + "buffering", + ); + const visibility = (locator: Locator, visible: boolean) => + waitFor(page, errors, () => locator.isVisible(), { + deadline, + description: `${locator} to be ${visible ? "visible" : "hidden"}`, + predicate: (shown) => shown === visible, + }); const spinner = page.locator(".buffering"); - await spinner.waitFor({ state: "visible" }); + await visibility(spinner, true); await page.getByRole("button", { name: "Pause", exact: true }).click(); - await page.locator('body[data-paused="true"]').waitFor(); - await spinner.waitFor({ state: "hidden" }); + await visibility(page.locator('body[data-paused="true"]'), true); + await visibility(spinner, false); // Click normally: a forced click would miss the overlay intercepting pointer input. await page.locator(".center-play").click({ timeout: 5000 }); - await page.locator('body[data-paused="false"]').waitFor(); - await spinner.waitFor({ state: "visible" }); + await visibility(page.locator('body[data-paused="false"]'), true); + await visibility(spinner, true); } finally { await browser.close(); await server.close(); diff --git a/test/interop/clients/js/harness.ts b/test/interop/clients/js/harness.ts index 90f019fc331..30b9d24857a 100644 --- a/test/interop/clients/js/harness.ts +++ b/test/interop/clients/js/harness.ts @@ -123,6 +123,14 @@ export function launch(args: string[] = [], engine: Engine = "chromium"): Promis return firefox.launch({ headless: true, handleSIGTERM: false, + // Firefox dlopens its media libraries. Keep the Nix paths on this child, so other + // browsers and native tools retain their own library search paths. + env: { + ...process.env, + LD_LIBRARY_PATH: [process.env.PLAYWRIGHT_FIREFOX_LIBRARY_PATH, process.env.LD_LIBRARY_PATH] + .filter(Boolean) + .join(":"), + }, firefoxUserPrefs: { "media.autoplay.default": 1, "media.autoplay.block-webaudio": true }, }); // WebKit's WebCodecs decode through the system GStreamer. The Nix dev shell points GStreamer at