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
4 changes: 2 additions & 2 deletions js/watch/src/audio/replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
8 changes: 5 additions & 3 deletions js/watch/src/audio/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export async function target(input: Target): Promise<Time.Milli> {
const jitter = playbackJitter(input.config);
const unregister = sync.register(new Signal<Time.Milli | undefined>(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();
Expand Down
54 changes: 50 additions & 4 deletions js/watch/src/sync.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Delay>(Time.Milli(fixed));
const rendition = new Signal<Time.Milli | undefined>(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();
Expand Down Expand Up @@ -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);
Expand All @@ -68,7 +114,7 @@ describe("delay and buffer", () => {

it("includes registered decoder jitter until the decoder unregisters", async () => {
const media = new Signal<Time.Milli | undefined>(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);
Expand All @@ -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();
Expand All @@ -102,7 +148,7 @@ describe("delay and buffer", () => {

it("unregisters duplicate jitter inputs independently", async () => {
const media = new Signal<Time.Milli | undefined>(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);

Expand Down
18 changes: 12 additions & 6 deletions js/watch/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Time.Milli | undefined>;

// 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<Time.Milli>;

// The jitter component of `delay` (always numeric).
Expand Down Expand Up @@ -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<Time.Milli | undefined>): Dispose {
const registered = { jitter };
this.#media.update((media) => [...media, registered]);
Expand Down Expand Up @@ -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);
}
Expand Down
Loading