From f5e3b14b5b72acb2a9be6e2acf8696eb0ca8a7df Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 17:14:32 -0800 Subject: [PATCH] fix(watch): a buffered audio head waits at most the max delay (backport #9267) The hold released only as timestamps advanced, so a publisher repeating one timestamp, or sending older groups within the span, grew it without bound. Release it once it has waited the max delay too. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/audio/order.test.ts | 21 ++++++++++++++++++++- js/watch/src/audio/order.ts | 14 +++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/js/watch/src/audio/order.test.ts b/js/watch/src/audio/order.test.ts index d8269b7e03e..54e59f88058 100644 --- a/js/watch/src/audio/order.test.ts +++ b/js/watch/src/audio/order.test.ts @@ -1,4 +1,5 @@ -import { describe, expect, it } from "bun:test"; +import { afterEach, beforeEach, describe, expect, it, type Mock, spyOn } from "bun:test"; +import { Time } from "@moq/net"; import { Order } from "./order"; type Read = { group: number; frame: { timestamp: number } }; @@ -6,6 +7,12 @@ const read = (group: number, timestamp: number): Read => ({ group, frame: { time const timestamps = (reads: Read[]) => reads.map(({ frame }) => frame.timestamp); describe("Order", () => { + let clock: Mock; + beforeEach(() => { + clock = spyOn(Time.Micro, "now").mockReturnValue(Time.Micro(0)); + }); + afterEach(() => clock.mockRestore()); + it("releases a held head sorted once a newer group arrives", () => { const order = new Order(() => ({ floor: 100, maxDelay: 1000 })); for (const t of [40, 20, 0]) expect(order.push(read(t / 20, t))).toEqual([]); @@ -69,6 +76,18 @@ describe("Order", () => { expect(timestamps(order.push(read(5, 120)))).toEqual([-20, 0, 40, 120]); }); + // A publisher repeating one timestamp never advances the span or runs past the floor, so only the + // time the head has waited releases it, however many frames arrived meanwhile. + it("releases a head once it has waited the max delay", () => { + const order = new Order(() => ({ floor: 100, maxDelay: 1000 })); + for (let i = 0; i < 100; i++) expect(order.push(read(5, 20))).toEqual([]); + clock.mockReturnValue(Time.Micro(999)); + expect(order.push(read(5, 20))).toEqual([]); + clock.mockReturnValue(Time.Micro(1000)); + expect(order.push(read(5, 20))).toHaveLength(102); + expect(timestamps(order.push(read(5, 20)))).toEqual([20]); + }); + it("holds nothing live", () => { const order = new Order(undefined); expect(timestamps(order.push(read(1, 20)))).toEqual([20]); diff --git a/js/watch/src/audio/order.ts b/js/watch/src/audio/order.ts index 098ba25e402..542c6cc28b5 100644 --- a/js/watch/src/audio/order.ts +++ b/js/watch/src/audio/order.ts @@ -1,3 +1,5 @@ +import { Time } from "@moq/net"; + /** * Puts one subscription's frames in timestamp order for the audio decoder. * @@ -9,7 +11,8 @@ * A buffered subscription holds its head and releases it sorted. The head is complete once a newer * group than the first arrives, or once the first group alone runs past the floor: the ring could not * start playing before holding that much, so waiting for the head costs no startup latency. It is - * also released once it spans the subscription's max delay, since nothing older is served. After + * also released once it spans the subscription's max delay, since nothing older is served, or once it + * has waited that long, which bounds what it keeps by arrival time whatever the timestamps say. After * that, and throughout when not holding, a frame older than one already decoded is not admitted. */ export class Order { @@ -17,8 +20,8 @@ export class Order { #hold: (() => { floor: number; maxDelay: number }) | undefined; // The head, in arrival order. #held: T[] = []; - // The group the head started with, and its own first frame. - #first?: { group: number; timestamp: number }; + // The group the head started with, its own first frame, and when that arrived. + #first?: { group: number; timestamp: number; arrived: number }; // The oldest and newest held timestamps. #span?: { oldest: number; newest: number }; // The newest frame admitted since the decoder was last reset. @@ -35,7 +38,7 @@ export class Order { if (!hold) return [read]; const { group, frame } = read; - this.#first ??= { group, timestamp: frame.timestamp }; + this.#first ??= { group, timestamp: frame.timestamp, arrived: Time.Micro.now() }; if (group > this.#first.group) return [...this.flush(), read]; this.#held.push(read); @@ -44,7 +47,8 @@ export class Order { this.#span = { oldest, newest }; const ran = group === this.#first.group && frame.timestamp - this.#first.timestamp > hold.floor; - if (ran || newest - oldest >= hold.maxDelay) return this.flush(); + const waited = Time.Micro.now() - this.#first.arrived; + if (ran || newest - oldest >= hold.maxDelay || waited >= hold.maxDelay) return this.flush(); return []; }