From cafa84872e218c29ed28c6bfaf6bfe7856d0779b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 21 Oct 2026 15:40:24 -0700 Subject: [PATCH 1/2] chore: claim release fixed-delay backport Co-Authored-By: GPT-6 From 3b3e669153ba46651911712f39b3360248cc39b6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 21 Oct 2026 15:48:58 -0700 Subject: [PATCH 2/2] fix(watch): honor fixed playback delay on release Keep a numeric delay independent of rendition catalog delay and jitter, while preserving auto and instant behavior. Update replay target expectations and cover fixed playhead and max-age behavior with mocked time. Co-Authored-By: GPT-6 --- js/watch/src/audio/replay.test.ts | 4 +-- js/watch/src/audio/replay.ts | 8 +++-- js/watch/src/sync.test.ts | 54 ++++++++++++++++++++++++++++--- js/watch/src/sync.ts | 18 +++++++---- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/js/watch/src/audio/replay.test.ts b/js/watch/src/audio/replay.test.ts index 3abcc742080..f7065ab857f 100644 --- a/js/watch/src/audio/replay.test.ts +++ b/js/watch/src/audio/replay.test.ts @@ -94,7 +94,7 @@ describe("target", () => { expect(await target({ delay: "auto", config, rtt: 40 })).toBe(Time.Milli(50 + 20 + 3)); }); - it("adds the rendition's jitter to a fixed delay", async () => { - expect(await target({ delay: Time.Milli(250), config })).toBe(Time.Milli(250 + 20 + 3)); + it.each([0, 100, 250])("uses a fixed delay of %d ms without adding rendition jitter", async (delay) => { + expect(await target({ delay: Time.Milli(delay), config })).toBe(Time.Milli(delay)); }); }); diff --git a/js/watch/src/audio/replay.ts b/js/watch/src/audio/replay.ts index 6102d61c100..8cc84aec36e 100644 --- a/js/watch/src/audio/replay.ts +++ b/js/watch/src/audio/replay.ts @@ -66,9 +66,11 @@ export async function target(input: Target): Promise { const jitter = playbackJitter(input.config); const unregister = sync.register(new Signal(jitter)); try { - // Sync's effects run on microtasks. The delay has settled once it holds this rendition's jitter - // on top of the resolved jitter, the only rendition registered. - while (sync.out.delay.peek() !== Time.Milli.add(sync.out.jitter.peek(), jitter)) { + // Sync's effects settle on the configured number, or the network plus rendition jitter in auto. + while ( + sync.out.delay.peek() !== + (typeof input.delay === "number" ? input.delay : Time.Milli.add(sync.out.jitter.peek(), jitter)) + ) { await sync.out.delay.changed(); } return sync.out.delay.peek(); diff --git a/js/watch/src/sync.test.ts b/js/watch/src/sync.test.ts index f0c068ebddf..bb3743814ab 100644 --- a/js/watch/src/sync.test.ts +++ b/js/watch/src/sync.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "bun:test"; +import { describe, expect, it, spyOn } from "bun:test"; import type * as Catalog from "@moq/hang/catalog"; import { Time } from "@moq/net"; import { Signal } from "@moq/signals"; @@ -10,6 +10,51 @@ import { renditionJitter } from "./video/playhead"; const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); describe("delay and buffer", () => { + it("keeps a fixed delay independent of rendition jitter", () => { + const jobs: (() => void)[] = []; + const microtasks = spyOn(globalThis, "queueMicrotask").mockImplementation((job) => { + jobs.push(job); + }); + const clock = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1_000)); + const flushJobs = () => { + for (let job = jobs.shift(); job; job = jobs.shift()) job(); + }; + let sync: Sync | undefined; + try { + for (const fixed of [0, 100, 250]) { + const delay = new Signal(Time.Milli(fixed)); + const rendition = new Signal(Time.Milli(234)); + sync = new Sync({ delay, buffer: Time.Milli(500) }); + const unregister = sync.register(rendition); + flushJobs(); + sync.received(Time.Milli(500)); + expect(sync.out.delay.peek()).toBe(Time.Milli(fixed)); + expect(sync.out.maxAge.peek()).toBe(Time.Milli(fixed + 500)); + expect(sync.now()).toBe(Time.Milli(500 - fixed)); + + // A growing publisher estimate must not move the fixed-delay playhead back. + rendition.set(Time.Milli(42_000)); + flushJobs(); + expect(sync.out.delay.peek()).toBe(Time.Milli(fixed)); + expect(sync.now()).toBe(Time.Milli(500 - fixed)); + + delay.set(Time.Milli(fixed + 50)); + flushJobs(); + expect(sync.out.delay.peek()).toBe(Time.Milli(fixed + 50)); + expect(sync.out.maxAge.peek()).toBe(Time.Milli(fixed + 550)); + + unregister(); + flushJobs(); + expect(sync.out.delay.peek()).toBe(Time.Milli(fixed + 50)); + sync.close(); + } + } finally { + sync?.close(); + clock.mockRestore(); + microtasks.mockRestore(); + } + }); + it("holds no lookahead by default", async () => { const sync = new Sync(); await flush(); @@ -59,6 +104,7 @@ describe("delay and buffer", () => { it("holds nothing when instant, whatever the buffer says", async () => { const sync = new Sync({ delay: "instant", buffer: 30_000 as Time.Milli }); + sync.register(new Signal(Time.Milli(42_000))); await flush(); expect(sync.out.buffered.peek()).toBe(false); expect(sync.out.delay.peek()).toBe(0 as Time.Milli); @@ -68,7 +114,7 @@ describe("delay and buffer", () => { it("includes registered decoder jitter until the decoder unregisters", async () => { const media = new Signal(20 as Time.Milli); - const sync = new Sync({ delay: 100 as Time.Milli }); + const sync = new Sync({ delay: "auto" }); const unregister = sync.register(media); await flush(); expect(sync.out.delay.peek()).toBe(120 as Time.Milli); @@ -86,7 +132,7 @@ describe("delay and buffer", () => { it("holds the largest delay plus jitter among subscribed renditions", async () => { const audio = { codec: "opus", container: { kind: "legacy" }, sampleRate: 48000, numberOfChannels: 2 }; const video = { codec: "avc1.640028", container: { kind: "legacy" }, jitter: 34, delay: 200 }; - const sync = new Sync({ delay: 100 as Time.Milli }); + const sync = new Sync({ delay: "auto" }); sync.register(new Signal(playbackJitter(audio as Catalog.AudioConfig))); const unsubscribe = sync.register(new Signal(renditionJitter(video as Catalog.VideoConfig))); await flush(); @@ -102,7 +148,7 @@ describe("delay and buffer", () => { it("unregisters duplicate jitter inputs independently", async () => { const media = new Signal(20 as Time.Milli); - const sync = new Sync({ delay: 100 as Time.Milli }); + const sync = new Sync({ delay: "auto" }); const unregisterFirst = sync.register(media); const unregisterSecond = sync.register(media); diff --git a/js/watch/src/sync.ts b/js/watch/src/sync.ts index c6d3404be73..7aaa1837d5c 100644 --- a/js/watch/src/sync.ts +++ b/js/watch/src/sync.ts @@ -14,7 +14,8 @@ import { /** * How far playback trails the live edge. * - * `"auto"` (the default) sizes the jitter buffer from the connection RTT; a `Time.Milli` fixes it. + * `"auto"` (the default) sizes the jitter buffer from the connection RTT and rendition jitter. + * A `Time.Milli` fixes the total delay at that number, ignoring rendition delay and jitter. * * `"instant"` drops the buffer and the pacing together: nothing is held, and {@link Sync.wait} * returns without sleeping, so a frame presents as soon as it exists. It also overrides @@ -53,7 +54,7 @@ type SyncOutput = { // This will keep being updated as we catch up to the live playhead then will be relatively static. reference: Signal; - // The resolved delay from the live edge to the playhead: jitter + max(audio, video). + // The resolved delay: the configured number, or in "auto" jitter + max(audio, video). delay: Signal; // The jitter component of `delay` (always numeric). @@ -109,7 +110,7 @@ export class Sync { this.#signals.run(this.#runMaxAge.bind(this)); } - /** Include a decoder's rendition delay in the shared playback clock until disposed. */ + /** Include a decoder's rendition delay in the "auto" playback clock until disposed. */ register(jitter: Getter): Dispose { const registered = { jitter }; this.#media.update((media) => [...media, registered]); @@ -161,15 +162,20 @@ export class Sync { } #runDelay(effect: Effect): void { + const configured = effect.get(this.in.delay); + if (typeof configured === "number") { + this.#out.delay.set(configured); + return; + } + const jitter = effect.get(this.#out.jitter); let media = Time.Milli.zero; for (const registered of effect.get(this.#media)) { media = Time.Milli.max(media, effect.get(registered.jitter) ?? Time.Milli.zero); } - // A zero delay still holds the rendition's own delay, which is a frame interval at 60fps. - // "instant" holds nothing at all. - const instant = effect.get(this.in.delay) === "instant"; + // "auto" includes the rendition's own delay; "instant" holds nothing at all. + const instant = configured === "instant"; const delay = instant ? Time.Milli.zero : Time.Milli.add(media, jitter); this.#out.delay.set(delay); }