Skip to content

Commit 07c714c

Browse files
fix(core): the first cut after Play starts on time instead of running ahead (#6794)
* fix(core): the first cut after Play starts on time instead of running ahead * fix(core): learn half the start delay from Play so the first cut keeps its last frame
1 parent d83f276 commit 07c714c

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,16 @@ describe("syncRuntimeMedia", () => {
21692169
playAt: number,
21702170
sampleAt: number,
21712171
lag: number,
2172+
playheadRunning = true,
21722173
) => {
2174+
if (playheadRunning)
2175+
syncRuntimeMedia({
2176+
clips: [],
2177+
timeSeconds: playAt,
2178+
playing: true,
2179+
playbackRate: 1,
2180+
getCompositionDuration: () => 30,
2181+
});
21732182
tick(clip, playAt);
21742183
expect(clip.el.play).toHaveBeenCalledTimes(1);
21752184
Object.defineProperty(clip.el, "paused", {
@@ -2212,6 +2221,16 @@ describe("syncRuntimeMedia", () => {
22122221
expect(due75ms.el.play).toHaveBeenCalledTimes(1);
22132222
});
22142223

2224+
it("learns half the start delay of a video started by Play", () => {
2225+
startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.074, false);
2226+
const due45ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
2227+
tick(due45ms, 9.955);
2228+
expect(due45ms.el.play).not.toHaveBeenCalled();
2229+
const due35ms = videoClip({ start: 10, end: 20, mediaStart: 0 });
2230+
tick(due35ms, 9.965);
2231+
expect(due35ms.el.play).toHaveBeenCalledTimes(1);
2232+
});
2233+
22152234
it("learns from a video seeked and started in the same tick", () => {
22162235
const seeked = videoClip({ start: 0, end: 10, mediaStart: 0 });
22172236
seeked.el.currentTime = 0;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,24 @@ function outPointHoldTime(el: HTMLMediaElement, outPoint: number, watched: boole
378378
// A muted video's clock is its audio track, which starts tens of ms after play(), so an upcoming
379379
// video starts that early, hidden, by the delay learned from earlier starts.
380380
let videoStartLatency: number | null = null;
381-
const videoStarts = new WeakMap<HTMLMediaElement, { offset: number; lead: number; at: number }>();
381+
const videoStarts = new WeakMap<
382+
HTMLMediaElement,
383+
{ offset: number; lead: number; at: number; weight: number }
384+
>();
385+
let playheadWasRunning = false;
382386

383387
/** Test-only seam. */
384388
export function resetVideoStartLatency(): void {
385389
videoStartLatency = null;
390+
playheadWasRunning = false;
386391
}
387392

388393
/** A quarter second in: its lag plus how early it started, capped at 0.1 s. */
389394
function learnVideoStartLatency(el: HTMLMediaElement, time: number, offset: number, rate: number) {
390395
const start = videoStarts.get(el);
391396
if (!start || time < start.at) return;
392397
videoStarts.delete(el);
393-
const sample = (offset - start.offset) / rate + start.lead;
398+
const sample = ((offset - start.offset) / rate + start.lead) * start.weight;
394399
const averaged = videoStartLatency === null ? sample : (videoStartLatency + sample) / 2;
395400
videoStartLatency = Math.min(0.1, Math.max(0, averaged));
396401
}
@@ -449,6 +454,9 @@ export function syncRuntimeMedia(params: {
449454
getCompositionDuration: () => number;
450455
}): void {
451456
const forceMuteAll = !!(params.outputMuted || params.userMuted);
457+
// A start at Play also waits on the playhead's start, so it counts half.
458+
const startWeight = playheadWasRunning ? 1 : 0.5;
459+
playheadWasRunning = params.playing && params.clockAdvancing !== false;
452460
for (const clip of params.clips) {
453461
const { el } = clip;
454462
if (!el.isConnected) continue;
@@ -780,6 +788,7 @@ export function syncRuntimeMedia(params: {
780788
// Offset read after any seek above; a loop could wrap a lap before the sample.
781789
if (isNonLoopVideo)
782790
videoStarts.set(el, {
791+
weight: startWeight,
783792
offset: relTime - (el.currentTime || 0),
784793
lead: Math.max(0, clip.start - params.timeSeconds) / params.playbackRate,
785794
at: Math.max(params.timeSeconds, clip.start) + 0.25 * params.playbackRate,

0 commit comments

Comments
 (0)