Skip to content
Open
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
23 changes: 22 additions & 1 deletion js/watch/src/audio/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { describe, expect, it } from "bun:test";
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { Duration } from "@moq/clock";
import { Manual } from "@moq/clock/test";
import { Order } from "./order";

type Read = { group: bigint; frame: { timestamp: Duration } };
const read = (group: bigint, timestamp: number): Read => ({ group, frame: { timestamp: Duration.micros(timestamp) } });
const timestamps = (reads: Read[]) => reads.map(({ frame }) => frame.timestamp.asMicros());

describe("Order", () => {
let clock: Manual;
let installed: { close(): void };
beforeEach(() => {
clock = new Manual();
installed = clock.install();
});
afterEach(() => installed.close());

it("releases a held head sorted once a newer group arrives", () => {
const order = new Order<Read>(() => ({ floor: 100, maxDelay: 1000 }));
for (const t of [40, 20, 0]) expect(order.push(read(BigInt(t / 20), t))).toEqual([]);
Expand Down Expand Up @@ -80,6 +89,18 @@ describe("Order", () => {
expect(timestamps(order.push(read(5n, 140)))).toEqual([0, 20, 60, 140]);
});

// 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<Read>(() => ({ floor: 100, maxDelay: 1000 }));
for (let i = 0; i < 100; i++) expect(order.push(read(5n, 20))).toEqual([]);
clock.jump(Duration.micros(999));
expect(order.push(read(5n, 20))).toEqual([]);
clock.jump(Duration.micros(1));
expect(order.push(read(5n, 20))).toHaveLength(102);
expect(timestamps(order.push(read(5n, 20)))).toEqual([20]);
});

it("holds nothing live", () => {
const order = new Order<Read>(undefined);
expect(timestamps(order.push(read(1n, 20)))).toEqual([20]);
Expand Down
14 changes: 8 additions & 6 deletions js/watch/src/audio/order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Duration } from "@moq/clock";
import { type Duration, type Instant, now } from "@moq/clock";

/**
* Puts one subscription's frames in timestamp order for the audio decoder.
Expand All @@ -13,16 +13,17 @@ import type { Duration } from "@moq/clock";
* 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<T extends { group: bigint; frame: { timestamp: Duration } }> {
// The current bounds in microseconds, while the head is still arriving.
#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: bigint; timestamp: number };
// The group the head started with, its own first frame, and when that arrived.
#first?: { group: bigint; timestamp: number; arrived: Instant };
// The oldest and newest held timestamps.
#span?: { oldest: number; newest: number };
// The newest frame admitted since the decoder was last reset.
Expand All @@ -40,7 +41,7 @@ export class Order<T extends { group: bigint; frame: { timestamp: Duration } }>

const { group, frame } = read;
const timestamp = frame.timestamp.asMicros();
this.#first ??= { group, timestamp };
this.#first ??= { group, timestamp, arrived: now() };
if (group > this.#first.group) return [...this.flush(), read];

this.#held.push(read);
Expand All @@ -49,7 +50,8 @@ export class Order<T extends { group: bigint; frame: { timestamp: Duration } }>
this.#span = { oldest, newest };

const ran = group === this.#first.group && timestamp - this.#first.timestamp > hold.floor;
if (ran || newest - oldest >= hold.maxDelay) return this.flush();
const waited = now().since(this.#first.arrived).asMicros();
if (ran || newest - oldest >= hold.maxDelay || waited >= hold.maxDelay) return this.flush();
return [];
}

Expand Down