Skip to content

Commit 81a80e0

Browse files
committed
fix(core): the video start delay is learned in less code and capped at 100 ms under test
1 parent 5f89fe5 commit 81a80e0

2 files changed

Lines changed: 29 additions & 50 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,13 @@ describe("syncRuntimeMedia", () => {
21232123
expect(due58ms.el.play).toHaveBeenCalledTimes(1);
21242124
});
21252125

2126+
it("caps the learned start delay at 100 ms", () => {
2127+
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.3);
2128+
const due150ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
2129+
tick(due150ms, 9.85);
2130+
expect(due150ms.el.play).not.toHaveBeenCalled();
2131+
});
2132+
21262133
it("does not learn from a looping video sampled across a lap wrap", () => {
21272134
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06);
21282135
const loop = videoClip({

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

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -303,49 +303,24 @@ const MIN_NATIVE_PLAYBACK_RATE = 1 / 16;
303303
const VIDEO_LEAD_HOLD_SECONDS = 0.1;
304304
const heldAhead = new WeakSet<HTMLMediaElement>();
305305

306-
// A video's clock starts tens of ms after play() (its muted audio track is the clock), so an upcoming
307-
// video is started that much before its cut, hidden on its first frame. Learned from earlier starts.
308-
const MAX_VIDEO_START_LATENCY_SECONDS = 0.1;
309-
const VIDEO_START_SAMPLE_SECONDS = 0.25;
310-
let videoStartLatencySeconds: number | null = null;
311-
const videoStarts = new WeakMap<
312-
HTMLMediaElement,
313-
{ offset: number; leadSeconds: number; sampleAt: number }
314-
>();
306+
// A muted video's clock is its audio track, which starts tens of ms after play(), so an upcoming
307+
// video starts that early, hidden, by the delay learned from earlier starts.
308+
let videoStartLatency: number | null = null;
309+
const videoStarts = new WeakMap<HTMLMediaElement, { offset: number; lead: number; at: number }>();
315310

316-
/** Test-only seam: forget the learned video start latency. */
311+
/** Test-only seam. */
317312
export function resetVideoStartLatency(): void {
318-
videoStartLatencySeconds = null;
313+
videoStartLatency = null;
319314
}
320315

321-
function noteVideoStart(
322-
el: HTMLMediaElement,
323-
clipStart: number,
324-
time: number,
325-
offset: number,
326-
rate: number,
327-
) {
328-
videoStarts.set(el, {
329-
offset,
330-
leadSeconds: Math.max(0, clipStart - time) / rate,
331-
sampleAt: Math.max(time, clipStart) + VIDEO_START_SAMPLE_SECONDS * rate,
332-
});
333-
}
334-
335-
/** Once a started video has run a while, its lag plus how early it started is the latency. */
336-
function learnVideoStartLatency(
337-
el: HTMLMediaElement,
338-
time: number,
339-
offset: number,
340-
baseRate: number,
341-
) {
316+
/** A quarter second in: its lag plus how early it started, capped at 0.1 s. */
317+
function learnVideoStartLatency(el: HTMLMediaElement, time: number, offset: number, rate: number) {
342318
const start = videoStarts.get(el);
343-
if (!start || time < start.sampleAt) return;
319+
if (!start || time < start.at) return;
344320
videoStarts.delete(el);
345-
const sample = (offset - start.offset) / baseRate + start.leadSeconds;
346-
const averaged =
347-
videoStartLatencySeconds === null ? sample : (videoStartLatencySeconds + sample) / 2;
348-
videoStartLatencySeconds = Math.min(MAX_VIDEO_START_LATENCY_SECONDS, Math.max(0, averaged));
321+
const sample = (offset - start.offset) / rate + start.lead;
322+
const averaged = videoStartLatency === null ? sample : (videoStartLatency + sample) / 2;
323+
videoStartLatency = Math.min(0.1, Math.max(0, averaged));
349324
}
350325

351326
// fallow-ignore-next-line complexity
@@ -413,9 +388,9 @@ export function syncRuntimeMedia(params: {
413388
const cueAhead =
414389
el.tagName === "AUDIO"
415390
? (params.cueAheadSeconds ?? 0)
416-
: params.playing && videoStartLatencySeconds !== null
417-
? // Started on the tick nearest its lead: ticks are coarser than the lead.
418-
videoStartLatencySeconds * params.playbackRate + (params.cueAheadSeconds ?? 0) / 2
391+
: params.playing && videoStartLatency !== null
392+
? // Half a tick: it starts on the tick nearest its lead.
393+
videoStartLatency * params.playbackRate + (params.cueAheadSeconds ?? 0) / 2
419394
: 0;
420395
const startsEarly =
421396
!inWindow &&
@@ -717,16 +692,13 @@ export function syncRuntimeMedia(params: {
717692
// fire between `load()` and `addEventListener` attachment, wedging
718693
// the element waiting for a callback that never came.
719694
markPlayRequested(el);
720-
// Read after any seek above: the offset before it says nothing about this start.
721-
// A loop can wrap before the sample, which would be off by a lap.
722-
if (el.tagName === "VIDEO" && !clip.loop)
723-
noteVideoStart(
724-
el,
725-
clip.start,
726-
params.timeSeconds,
727-
relTime - (el.currentTime || 0),
728-
params.playbackRate,
729-
);
695+
// Offset read after any seek above; a loop could wrap a lap before the sample.
696+
if (isNonLoopVideo)
697+
videoStarts.set(el, {
698+
offset: relTime - (el.currentTime || 0),
699+
lead: Math.max(0, clip.start - params.timeSeconds) / params.playbackRate,
700+
at: Math.max(params.timeSeconds, clip.start) + 0.25 * params.playbackRate,
701+
});
730702
void el.play().catch((err: unknown) => {
731703
// If play() rejects — e.g. autoplay blocked, element removed
732704
// mid-flight — drop the in-flight flag so a future sync tick can

0 commit comments

Comments
 (0)