Skip to content

Commit 3eead54

Browse files
fix(core): audio no longer plays fast for a moment after Play (#7067)
* fix(core): audio no longer plays fast for a moment after Play * refactor(core): split the audio start wait out of the clock read
1 parent 3baf81f commit 3eead54

4 files changed

Lines changed: 172 additions & 1 deletion

File tree

‎packages/core/src/runtime/clock.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,24 @@ describe("TransportClock", () => {
426426
expect(clock.now()).toBeCloseTo(5.016, 5);
427427
});
428428

429+
it("a Play waits for audio that has not started where it began, but never pulls back to audio far behind", () => {
430+
const { clock, advance } = createClock({ duration: 30 });
431+
clock.seek(5);
432+
clock.play();
433+
advance(16);
434+
expect(clock.now()).toBeCloseTo(5.016, 5);
435+
const voice = createMockAudioEl(5, false);
436+
clock.attachAudioSource({ el: voice, compositionStart: 0, mediaStart: 0 });
437+
advance(100);
438+
expect(clock.now()).toBe(5);
439+
440+
clock.pause();
441+
clock.play();
442+
advance(16);
443+
voice.currentTime = 1;
444+
expect(clock.now()).toBeCloseTo(5.016, 5);
445+
});
446+
429447
it("does not freeze on a short native loop that wraps", () => {
430448
const { clock, advance } = createClock({ duration: 30 });
431449
clock.play();

‎packages/core/src/runtime/clock.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export type AudioClockSource =
2626
const STALL_THRESHOLD_MS = 500;
2727
const STALL_ADJUSTED_LAG_MS = 33;
2828
const HAVE_FUTURE_DATA = 3;
29+
const AUDIO_START_WAIT_MS = 1500;
30+
const MEDIA_SYNC_STEER_TOLERANCE_SECONDS = 0.04;
31+
32+
type PendingAudioStart = { sinceMs: number; at: number; from: number | null };
2933

3034
export class TransportClock {
3135
private _baseTime = 0;
@@ -40,6 +44,7 @@ export class TransportClock {
4044
/** Wall-clock time of the last `now()` read while playing; null while paused. */
4145
private _lastReadMs: number | null = null;
4246
private _lastNow = 0;
47+
private _audioStart: PendingAudioStart | null = null;
4348

4449
constructor(opts?: {
4550
initialTime?: number;
@@ -89,14 +94,32 @@ export class TransportClock {
8994
): number | null {
9095
const { el, compositionStart, mediaStart, rate } = source;
9196
if (el.paused || !Number.isFinite(el.currentTime)) return null;
92-
if (el.seeking) return null;
9397
const spec = rate ?? 1;
9498
const time = timeAtSourceTime(spec, el.currentTime - mediaStart) + compositionStart;
99+
const held = this._heldUntilAudioOutputStarts(el.seeking, time);
100+
if (held !== undefined) return held;
95101
if (time >= this._lastNow) return time;
96102
const wrapped = el.loop && this._lastNow - time > timeAtSourceTime(spec, el.duration) / 2;
97103
return !wrapped && el.readyState < HAVE_FUTURE_DATA ? this._lastNow : null;
98104
}
99105

106+
private _heldUntilAudioOutputStarts(seeking: boolean, time: number): number | null | undefined {
107+
const pending = this._pendingAudioStart();
108+
if (seeking) return pending ? this._lastNow : null;
109+
if (!pending) return undefined;
110+
pending.from ??= time;
111+
const startsHere = Math.abs(pending.from - pending.at) <= MEDIA_SYNC_STEER_TOLERANCE_SECONDS;
112+
if (startsHere && time === pending.from) return time;
113+
this._audioStart = null;
114+
return undefined;
115+
}
116+
117+
private _pendingAudioStart(): PendingAudioStart | null {
118+
const pending = this._audioStart;
119+
if (pending && this._nowMs() - pending.sinceMs > AUDIO_START_WAIT_MS) this._audioStart = null;
120+
return this._audioStart;
121+
}
122+
100123
/** Folds a >500ms gap since the last read into `_playStartMs` so it's never reported as played time. */
101124
private _applyStallCorrection(): void {
102125
if (this._playStartMs === null) return;
@@ -114,6 +137,7 @@ export class TransportClock {
114137
if (this._playStartMs !== null) return false;
115138
if (this._baseTime >= this.getEnd()) return false;
116139
this._playStartMs = this._nowMs();
140+
this._audioStart = { sinceMs: this._playStartMs, at: this._baseTime, from: null };
117141
this._lastNow = this._baseTime;
118142
// Not a stall: the gap since the clock was last read (possibly a long
119143
// paused idle) says nothing about lost playback time, since none was
@@ -138,6 +162,7 @@ export class TransportClock {
138162
this._lastNow = clamped;
139163
if (this._playStartMs !== null) {
140164
this._playStartMs = this._nowMs();
165+
this._audioStart = { sinceMs: this._playStartMs, at: clamped, from: null };
141166
// Same reasoning as `play()`: the seek itself, not any elapsed gap
142167
// since the last read, is why the reported time is moving now.
143168
this._lastReadMs = null;
@@ -208,6 +233,7 @@ export class TransportClock {
208233
}
209234

210235
/** The element the playhead currently follows, if the source is one. */
236+
// fallow-ignore-next-line unused-class-member
211237
audioElement(): HTMLMediaElement | null {
212238
return this._audioSource && "el" in this._audioSource ? this._audioSource.el : null;
213239
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { afterEach, expect, it, vi } from "vitest";
2+
import { initSandboxRuntimeModular } from "./init";
3+
import { createMockTimeline, resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers";
4+
5+
let now = 0;
6+
7+
/** An element that reports playing at once but only moves `startLagMs` later, as real output does; a seek renews it. */
8+
function lateStartingAudio(el: HTMLAudioElement, startLagMs: number) {
9+
let paused = true;
10+
let position = 0;
11+
let movesFromMs = 0;
12+
let rate = 1;
13+
const rates: number[] = [];
14+
const read = () =>
15+
paused || now < movesFromMs ? position : position + ((now - movesFromMs) / 1000) * rate;
16+
const rebase = () => {
17+
position = read();
18+
movesFromMs = Math.max(now, movesFromMs);
19+
};
20+
Object.defineProperties(el, {
21+
duration: { value: 10, configurable: true },
22+
readyState: { value: 4, configurable: true },
23+
seeking: { value: false, configurable: true },
24+
buffered: { value: { length: 1, start: () => 0, end: () => 10 }, configurable: true },
25+
paused: { get: () => paused, configurable: true },
26+
currentTime: {
27+
get: read,
28+
set: (t: number) => {
29+
position = t;
30+
movesFromMs = now + startLagMs;
31+
},
32+
configurable: true,
33+
},
34+
playbackRate: {
35+
get: () => rate,
36+
set: (r: number) => {
37+
rebase();
38+
rate = r;
39+
rates.push(r);
40+
},
41+
configurable: true,
42+
},
43+
});
44+
el.play = vi.fn(() => {
45+
if (paused) {
46+
paused = false;
47+
movesFromMs = now + startLagMs;
48+
}
49+
return Promise.resolve();
50+
});
51+
el.pause = vi.fn(() => {
52+
if (paused) return;
53+
position = read();
54+
paused = true;
55+
el.dispatchEvent(new Event("pause"));
56+
});
57+
el.load = vi.fn();
58+
return rates;
59+
}
60+
61+
function playNarration(startLagMs: number) {
62+
resetRuntimeFixtureDom();
63+
document.body.innerHTML =
64+
'<div data-composition-id="main" data-root="true" data-duration="10">' +
65+
'<audio id="voice" data-start="0" data-duration="10" src="/voice.m4a"></audio>' +
66+
"</div>";
67+
const voice = document.getElementById("voice") as HTMLAudioElement;
68+
const rates = lateStartingAudio(voice, startLagMs);
69+
window.__timelines = { main: createMockTimeline(10) };
70+
now = 0;
71+
const frames = new Map<number, FrameRequestCallback>();
72+
let nextFrame = 0;
73+
vi.spyOn(performance, "now").mockImplementation(() => now);
74+
window.requestAnimationFrame = (cb) => (frames.set(++nextFrame, cb), nextFrame);
75+
window.cancelAnimationFrame = (id) => void frames.delete(id);
76+
vi.spyOn(window.parent, "postMessage").mockImplementation(() => {});
77+
initSandboxRuntimeModular();
78+
const playFor = (seconds: number) => {
79+
for (let ms = 0; ms < seconds * 1000; ms += 1000 / 60) {
80+
now += 1000 / 60;
81+
const due = [...frames.values()];
82+
frames.clear();
83+
for (const cb of due) cb(now);
84+
}
85+
};
86+
return { voice, rates, playFor };
87+
}
88+
89+
afterEach(() => {
90+
vi.restoreAllMocks();
91+
delete window.__player;
92+
});
93+
94+
it.each([60, 200, 700])(
95+
"audio that starts %i ms after Play is not sped up to catch the film, on the first Play or after a Pause",
96+
(startLagMs) => {
97+
const { voice, rates, playFor } = playNarration(startLagMs);
98+
99+
window.__player!.play();
100+
playFor(2);
101+
window.__player!.pause();
102+
playFor(0.5);
103+
window.__player!.play();
104+
playFor(2);
105+
106+
expect(rates.filter((r) => r !== 1)).toEqual([]);
107+
expect(Math.abs(window.__player!.getTime() - voice.currentTime)).toBeLessThan(0.05);
108+
},
109+
);

‎packages/core/src/runtime/init.buffering.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@ import { createMockTimeline, resetRuntimeFixtureDom } from "./runtimeSeekFixture
77
function audio(id: string, duration: number, readyState: { value: number; bufferedTo?: number }) {
88
const el = document.getElementById(id) as HTMLAudioElement;
99
let paused = true;
10+
let position = 0;
11+
let movedAtMs = 0;
12+
// A playing element moves only while it has data, as a real one does.
13+
const read = () => {
14+
if (!paused && readyState.value >= 3) position += (performance.now() - movedAtMs) / 1000;
15+
movedAtMs = performance.now();
16+
return position;
17+
};
1018
Object.defineProperties(el, {
1119
duration: { value: duration, configurable: true },
20+
currentTime: {
21+
get: read,
22+
set: (t: number) => {
23+
read();
24+
position = t;
25+
},
26+
configurable: true,
27+
},
1228
readyState: { get: () => readyState.value, configurable: true },
1329
buffered: {
1430
get: () => ({ length: 1, start: () => 0, end: () => readyState.bufferedTo ?? 0 }),
@@ -17,10 +33,12 @@ function audio(id: string, duration: number, readyState: { value: number; buffer
1733
paused: { get: () => paused, configurable: true },
1834
});
1935
el.play = vi.fn(() => {
36+
read();
2037
paused = false;
2138
return Promise.resolve();
2239
});
2340
el.pause = vi.fn(() => {
41+
read();
2442
if (!paused) el.dispatchEvent(new Event("pause"));
2543
paused = true;
2644
});

0 commit comments

Comments
 (0)