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
29 changes: 27 additions & 2 deletions js/watch/src/audio/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ describe("Backpressure", () => {
it("counts a frame still decoding where its output lands", () => {
const gate = new Backpressure(1000, true, Duration.millis(2000));
gate.observe(ms(0), false);
gate.decoded(ms(520), ms(500));
gate.decoded(ms(520), ms(20));
gate.inserted(ms(480), ms(500));
const signal = new AbortController().signal;
expect(Bun.peek.status(gate.wait(ms(520), signal))).toBe("fulfilled");
Expand Down Expand Up @@ -580,7 +580,7 @@ describe("AudioBuffer steering", () => {
const { buffer, sent } = fake(100);
try {
buffer.received(Duration.millis(1000));
buffer.decoded(ms(1020), ms(1014));
buffer.decoded(ms(1020), ms(6));
buffer.received(Duration.millis(1040));
expect(sent.slice(1)).toEqual([
{ type: "received", end: 1_000_000 },
Expand Down Expand Up @@ -831,4 +831,29 @@ describe("AudioBuffer steering", () => {
clock.close();
}
});

// Opus output lands on the ring a pre-skip before its frame timestamp, at its presentation time, so
// the clock maps onto the ring unshifted and the playhead reports the ring's position: only a frame
// timestamp, as `received` takes, moves back by the pre-skip.
it("plays and reports the ring at presentation time under a pre-skip", () => {
const clock = started();
const { buffer, sent, state, context } = fake(100, {
clock: { position: () => 5000, out: { reference: new Signal<number | undefined>(0) } },
});
try {
buffer.decoded(ms(5020), ms(6.5));
buffer.received(Duration.millis(5020));
context.currentTime = 1;
context.sounding = 1;
state(ms(5000), false, 0, 1);
expect(sent.filter((msg) => msg.type === "clock")).toEqual([
{ type: "clock", timestamp: 5_000_000, time: 1 },
]);
expect(sent).toContainEqual({ type: "received", end: 5_013_500 });
expect(buffer.timestamp.peek()).toBe(ms(5000));
} finally {
buffer.close();
clock.close();
}
});
});
22 changes: 12 additions & 10 deletions js/watch/src/audio/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ export class Backpressure {
}

/**
* Note that the decoder produced output ending at `end`, which lands in the ring up to `landed`
* unless it was discarded. It answers the oldest frame still decoding and every one it reaches
* past: a decoder may emit nothing for a frame.
* Note that the decoder produced output ending at `end`, which lands in the ring `offset` behind it
* (the codec's pre-skip, not what a trim removed) unless it was discarded. It answers the oldest
* frame still decoding and every one it reaches past: a decoder may emit nothing for a frame.
*/
decoded(end: number, landed?: number): void {
if (landed !== undefined) this.#offset = end - landed;
decoded(end: number, offset?: number): void {
if (offset !== undefined) this.#offset = offset;
let answered = 0;
while (answered < this.#decoding.length && (answered === 0 || this.#decoding[answered] < end)) answered++;
this.#decoding.splice(0, answered);
Expand Down Expand Up @@ -295,7 +295,7 @@ export class AudioBuffer {
#context: AudioContext;

readonly #timestamp = new Signal<number>(0);
/** The playback timestamp sounding now, as of the worklet's last report. */
/** The presentation timestamp sounding now, as of the worklet's last report. */
readonly timestamp: Getter<number> = this.#timestamp;

readonly #output = new Signal<Duration>(Duration.ZERO);
Expand Down Expand Up @@ -520,7 +520,9 @@ export class AudioBuffer {
}
// A reference move is a step of the clock, not where a reading landed, so the offset takes it whole.
const moved = last !== undefined && reference !== undefined ? (last - reference) * 1000 : 0;
const offset = position * 1000 - this.#backpressure.offset - heard * 1_000_000;
// The ring already holds presentation time (Opus output lands a pre-skip before its frame
// timestamp), so the clock maps onto it unshifted.
const offset = position * 1000 - heard * 1_000_000;
const previous = this.#offset === undefined ? undefined : this.#offset + moved;
this.#offset =
previous !== undefined && Math.abs(offset - previous) < JUMP ? Math.max(offset, previous - FALL) : offset;
Expand All @@ -538,10 +540,10 @@ export class AudioBuffer {

/**
* Note that the decoder produced output ending at `end` on its own timeline, which lands in the ring
* up to `landed` unless it was discarded.
* `offset` behind it unless it was discarded.
*/
decoded(end: number, landed?: number): void {
this.#backpressure.decoded(end, landed);
decoded(end: number, offset?: number): void {
this.#backpressure.decoded(end, offset);
}

/** Note that the decoder closed or reset, dropping every frame still decoding. */
Expand Down
2 changes: 1 addition & 1 deletion js/watch/src/audio/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ export class Decoder {
try {
landed = this.#place(sample);
} finally {
if (!preroll) this.#ring.peek()?.decoded(decoded, landed);
if (!preroll) this.#ring.peek()?.decoded(decoded, landed === undefined ? undefined : this.#terminal.delay);
}
return landed;
}
Expand Down
2 changes: 1 addition & 1 deletion js/watch/src/audio/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export async function* replay(trace: Arrival[], options: Options): AsyncGenerato
const end = timestamp + micros((pcm.length / rate) * 1000);
gate.inserted(timestamp, end);
decoded.update((span) => ({ start: span?.start ?? media, end: Duration.micros(end) }));
gate.decoded(end, end);
gate.decoded(end, 0);
}
})().catch((error: unknown) => {
failure = { error };
Expand Down
17 changes: 0 additions & 17 deletions quest/m0/nightly/audio-quality-p50.md

This file was deleted.

20 changes: 20 additions & 0 deletions quest/m0/test-flakes-3/announce-consumer-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# [S] Closing a JS announce consumer ends the peer's stream under load

## Goal

`moq-lite-07-wip: closing an announce consumer ends the peer's announce stream`
in `js/net/src/integration.test.ts` passes under a loaded `just check`.

## Plan

Seen once on 2026-11-19 while validating
[moq-dev/moq#9139](https://github.com/moq-dev/moq/pull/9139), which touches no
`js/net` code: `until(() => ended === i)` hit its 1 s wall-clock deadline
during the full `test js` run, and the test passed alone right after.

Find whether the peer's announce stream really ends late under load (the
close reaching the server only on a later turn) or whether the 1 s `until`
deadline is what fails. Then assert on the stream's end as an event instead of
polling against wall time.

Public API: none. Wire: none.
30 changes: 15 additions & 15 deletions test/audio-quality/budgets.json

Large diffs are not rendered by default.