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: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
};
};

Expand Down
25 changes: 25 additions & 0 deletions js/publish/src/broadcast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 12 additions & 5 deletions js/publish/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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);
});
}
});
Expand Down
33 changes: 23 additions & 10 deletions js/watch/src/audio/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 7 additions & 15 deletions js/watch/src/audio/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand All @@ -724,7 +728,7 @@ export class Decoder {
reads: FrameRead[],
): Promise<boolean> {
for (const read of reads) {
if (this.#onRead(read)) {
if (this.#terminal.update(read)) {
this.#reprime(decoder, config, order);
}
if (!order.admit(read)) continue;
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion js/watch/src/audio/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion quest/m0/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 0 additions & 47 deletions quest/m0/test-flakes-2/browser-media-flakes.md

This file was deleted.

1 change: 1 addition & 0 deletions test/interop/clients/js/buffering/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="data:," />
<title>Paused buffering controls</title>
</head>
<body>
Expand Down
27 changes: 19 additions & 8 deletions test/interop/clients/js/harness.browser.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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`,
Expand All @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions test/interop/clients/js/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down