From 841e90ffe201823bb011ad568efe474787674b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 7 Nov 2026 22:09:06 +0000 Subject: [PATCH] fix(core): Safari video picture lands on the cut again, undoing the hidden pre-roll (#7383) * fix(core): Safari video picture lands on the cut again, undoing the hidden pre-roll Reverts the one-second hidden video pre-roll (#7265): on Safari the pre-rolled video is still behind at the cut and trails it by 30 to 150 ms in every measured run, and some cut flashes never show. * fix(core): shorten a restored comment line to the 120-column limit --- .../src/runtime/init.videoPreseek.test.ts | 3 +- .../core/src/runtime/media.preseek.test.ts | 10 +- packages/core/src/runtime/media.test.ts | 192 ++++++++++-------- packages/core/src/runtime/media.ts | 65 +++--- packages/core/src/runtime/mediaSteer.test.ts | 14 -- packages/core/src/runtime/mediaSteer.ts | 11 - scripts/package-size-budgets.json | 2 +- 7 files changed, 146 insertions(+), 151 deletions(-) diff --git a/packages/core/src/runtime/init.videoPreseek.test.ts b/packages/core/src/runtime/init.videoPreseek.test.ts index b5071ac3f1..61b77efbbf 100644 --- a/packages/core/src/runtime/init.videoPreseek.test.ts +++ b/packages/core/src/runtime/init.videoPreseek.test.ts @@ -51,8 +51,7 @@ it.each(["video", "audio"])( expect(play).not.toHaveBeenCalled(); for (let i = 0; i < 11; i++) step(); if (tag === "video") expect(video.style.visibility).toBe("visible"); - // A video is landed once more where its pre-roll plays from. - expect(seek).toHaveBeenCalledTimes(tag === "video" ? 2 : 1); + expect(seek).toHaveBeenCalledTimes(1); expect(play).toHaveBeenCalledTimes(1); }, ); diff --git a/packages/core/src/runtime/media.preseek.test.ts b/packages/core/src/runtime/media.preseek.test.ts index c6c77ee9c5..16bd0e10d9 100644 --- a/packages/core/src/runtime/media.preseek.test.ts +++ b/packages/core/src/runtime/media.preseek.test.ts @@ -49,14 +49,8 @@ describe("incoming media preparation", () => { expect(el.currentTime).toBe(mediaElementTime(8)); expect(el.play).not.toHaveBeenCalled(); tick(3); - if (tag === "video") { - // A video plays from one second before its in-point, so it is landed there once more as it starts. - expect(el.currentTime).toBe(mediaElementTime(7)); - expect(seek).toHaveBeenCalledTimes(2); - } else { - tick(4); - expect(seek).toHaveBeenCalledTimes(1); - } + tick(4); + expect(seek).toHaveBeenCalledTimes(1); expect(el.play).toHaveBeenCalledTimes(1); }, ); diff --git a/packages/core/src/runtime/media.test.ts b/packages/core/src/runtime/media.test.ts index c79eee21cc..7965a921e0 100644 --- a/packages/core/src/runtime/media.test.ts +++ b/packages/core/src/runtime/media.test.ts @@ -9,6 +9,7 @@ import { readElementPlaybackRate, readElementPlaybackStart, refreshRuntimeMediaCache, + resetVideoStartLatency, resolveRuntimeMediaClipDuration, syncRuntimeMedia as syncRuntimeMediaWithDuration, } from "./media"; @@ -1014,7 +1015,7 @@ describe("syncRuntimeMedia", () => { expect(clip.el.play).not.toHaveBeenCalled(); }); - it("starts no audio early while paused", () => { + it("starts nothing early while paused, and never a video", () => { const audio = createMockClip({ start: 5, end: 6 }, "audio"); syncRuntimeMedia({ clips: [audio], @@ -1023,17 +1024,15 @@ describe("syncRuntimeMedia", () => { playbackRate: 1, cueAheadSeconds: 0.017, }); - expect(audio.el.play).not.toHaveBeenCalled(); - }); - - it("starts no video early while paused", () => { const video = createMockClip({ start: 5, end: 6 }, "video"); syncRuntimeMedia({ clips: [video], timeSeconds: 4.99, - playing: false, + playing: true, playbackRate: 1, + cueAheadSeconds: 0.017, }); + expect(audio.el.play).not.toHaveBeenCalled(); expect(video.el.play).not.toHaveBeenCalled(); }); @@ -2304,106 +2303,121 @@ describe("syncRuntimeMedia", () => { expect(el.pause).not.toHaveBeenCalled(); }); - describe("video pre-roll", () => { - const playing = (clip: RuntimeMediaClip, currentTime: number) => { + describe("start latency", () => { + afterEach(() => resetVideoStartLatency()); + + // Plays `clip` at `playAt`, then has its element `lag` s behind the playhead at `sampleAt`. + const startWithLag = ( + clip: RuntimeMediaClip, + playAt: number, + sampleAt: number, + lag: number, + playheadRunning = true, + ) => { + if (playheadRunning) + syncRuntimeMedia({ + clips: [], + timeSeconds: playAt, + playing: true, + playbackRate: 1, + getCompositionDuration: () => 30, + }); + tick(clip, playAt); + expect(clip.el.play).toHaveBeenCalledTimes(1); Object.defineProperty(clip.el, "paused", { value: false, writable: true, configurable: true, }); - clip.el.currentTime = currentTime; + clip.el.currentTime = clip.mediaStart + (sampleAt - clip.start) - lag; + tick(clip, sampleAt); }; - it("starts an upcoming video one second before its clip, from one second before its first frame", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 5 }); - tick(clip, 8.9); - expect(clip.el.play).not.toHaveBeenCalled(); - tick(clip, 9); - expect(clip.el.play).toHaveBeenCalledTimes(1); - expect(clip.el.currentTime).toBe(mediaElementTime(4)); - }); - - it("starts a video with less than a second of source before its first frame from the start of the file", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 0.3 }); - tick(clip, 9.5); - expect(clip.el.play).not.toHaveBeenCalled(); - tick(clip, 9.75); - expect(clip.el.play).toHaveBeenCalledTimes(1); - }); - - it("starts a video that plays from the very start of its file 50 ms early, on its first frame", () => { + it("does not start an upcoming video early before any start is measured", () => { const clip = videoClip({ start: 10, end: 20, mediaStart: 0 }); - tick(clip, 9.9); + tick(clip, 9.99); expect(clip.el.play).not.toHaveBeenCalled(); - tick(clip, 9.96); - expect(clip.el.play).toHaveBeenCalledTimes(1); }); - it.each([0, 0.04])( - "plays a from-file-start clip that starts in %d s through its cut without pausing it", - (startLatency) => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 0 }); - clip.el.pause = vi.fn(); - let playedAt: number | null = null; - clip.el.play = vi.fn(() => { - playedAt = lastTick; - Object.defineProperty(clip.el, "paused", { - value: false, - writable: true, - configurable: true, - }); - return Promise.resolve(); - }); - let lastTick = 9.9; - for (let t = 9.9; t < 10.3; t += 1 / 60) { - if (playedAt !== null && t - playedAt >= startLatency) - clip.el.currentTime += (1 / 60) * clip.el.playbackRate; - tick(clip, t); - lastTick = t; - } - expect(clip.el.play).toHaveBeenCalledTimes(1); - expect(clip.el.pause).not.toHaveBeenCalled(); - }, - ); - - it("slows a video that started ahead on its first frame, hidden, instead of holding it", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 0 }); - tick(clip, 9.96); - playing(clip, 0.1); - clip.el.pause = vi.fn(); - tick(clip, 9.97); - expect(clip.el.pause).not.toHaveBeenCalled(); - expect(clip.el.playbackRate).toBeLessThan(1); + it("starts an upcoming video early by the start latency measured on earlier ones", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06); + const due50ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due50ms, 9.95); + expect(due50ms.el.play).toHaveBeenCalledTimes(1); + const due70ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due70ms, 9.93); + expect(due70ms.el.play).not.toHaveBeenCalled(); }); - it("runs a pre-rolled video that is late up to twice as fast, hidden", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 5 }); - tick(clip, 9.5); - playing(clip, 4.4); // its target is 4.5 - tick(clip, 9.5); - expect(clip.el.playbackRate).toBeCloseTo(1.5, 9); - playing(clip, 4); - tick(clip, 9.5); - expect(clip.el.playbackRate).toBeCloseTo(2, 9); + it("starts an upcoming video on the tick nearest its lead", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06); + const due75ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + // Now is 15 ms earlier than the 60 ms lead; the next tick, 40 ms on, would be 25 ms late. + syncRuntimeMedia({ + clips: [due75ms], + timeSeconds: 9.925, + playing: true, + playbackRate: 1, + cueAheadSeconds: 0.04, + getCompositionDuration: () => 30, + }); + expect(due75ms.el.play).toHaveBeenCalledTimes(1); }); - it("runs a pre-rolled video that is ahead at half speed instead of holding it", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 5 }); - tick(clip, 9.5); - playing(clip, 4.9); - clip.el.pause = vi.fn(); - tick(clip, 9.5); - expect(clip.el.pause).not.toHaveBeenCalled(); - expect(clip.el.playbackRate).toBeCloseTo(0.5, 9); + it("learns half the start delay of a video started by Play", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.074, false); + const due45ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due45ms, 9.955); + expect(due45ms.el.play).not.toHaveBeenCalled(); + const due35ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due35ms, 9.965); + expect(due35ms.el.play).toHaveBeenCalledTimes(1); }); - it("steers a video gently once its clip is visible", () => { - const clip = videoClip({ start: 10, end: 20, mediaStart: 5 }); - tick(clip, 9.5); - playing(clip, 4.9); - tick(clip, 10); - tick(clip, 10); - expect(clip.el.playbackRate).toBeCloseTo(1.03, 9); + it("learns from a video seeked and started in the same tick", () => { + const seeked = videoClip({ start: 0, end: 10, mediaStart: 0 }); + seeked.el.currentTime = 0; + startWithLag(seeked, 5, 5.3, 0.06); + const due25ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due25ms, 9.975); + expect(due25ms.el.play).toHaveBeenCalledTimes(1); + }); + + it("keeps its lead when an early start lands on time", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06); + startWithLag(videoClip({ start: 10, end: 20, mediaStart: 0 }), 9.945, 10.3, 0.005); + const due58ms = videoClip({ start: 20, end: 30, mediaStart: 0 }); + tick(due58ms, 19.942); + expect(due58ms.el.play).toHaveBeenCalledTimes(1); + }); + + it("caps the learned start delay at 100 ms", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.3); + const due150ms = videoClip({ start: 10, end: 20, mediaStart: 0 }); + tick(due150ms, 9.85); + expect(due150ms.el.play).not.toHaveBeenCalled(); + }); + + it("does not learn from a looping video sampled across a lap wrap", () => { + startWithLag(videoClip({ start: 0, end: 10, mediaStart: 0 }), 0, 0.3, 0.06); + const loop = videoClip({ + start: 10, + end: 20, + mediaStart: 0, + loop: true, + sourceDuration: 0.2, + }); + tick(loop, 10); + Object.defineProperty(loop.el, "paused", { + value: false, + writable: true, + configurable: true, + }); + loop.el.currentTime = 0.19; // the playhead has wrapped to 0.06, the element not yet + tick(loop, 10.26); + const due50ms = videoClip({ start: 30, end: 40, mediaStart: 0 }); + tick(due50ms, 29.95); + expect(due50ms.el.play).toHaveBeenCalledTimes(1); }); }); }); diff --git a/packages/core/src/runtime/media.ts b/packages/core/src/runtime/media.ts index 2a10dd1cf2..1e2721cf97 100644 --- a/packages/core/src/runtime/media.ts +++ b/packages/core/src/runtime/media.ts @@ -317,6 +317,7 @@ export function evictMediaSyncState(el: HTMLMediaElement): void { forgetSteering(el); heldAhead.delete(el); forgetOutPoint(el); + videoStarts.delete(el); } /** Whether the clip is inside its window at the playhead right now: the sync tick keeps its time while it is. */ @@ -448,12 +449,28 @@ function outPointHoldTime(el: HTMLMediaElement, outPoint: number, watched: boole return last?.outPoint === outPoint ? last.at : outPoint; } -const VIDEO_PREROLL_SECONDS = 1; -const VIDEO_MIN_LEAD_SECONDS = VIDEO_LEAD_HOLD_SECONDS / 2; +// A muted video runs on its audio track, which starts after play(): an upcoming one starts early by the learned delay. +let videoStartLatency: number | null = null; +const videoStarts = new WeakMap< + HTMLMediaElement, + { offset: number; lead: number; at: number; weight: number } +>(); +let playheadWasRunning = false; -const isRenderDriven = (el: HTMLMediaElement): boolean => - !!findInjectedRenderFrame(el) || - ("__HF_EXPORT_RENDER_SEEK_CONFIG" in window && !!window.__HF_EXPORT_RENDER_SEEK_CONFIG); +export function resetVideoStartLatency(): void { + videoStartLatency = null; + playheadWasRunning = false; +} + +/** A quarter second in: its lag plus how early it started, capped at 0.1 s. */ +function learnVideoStartLatency(el: HTMLMediaElement, time: number, offset: number, rate: number) { + const start = videoStarts.get(el); + if (!start || time < start.at) return; + videoStarts.delete(el); + const sample = ((offset - start.offset) / rate + start.lead) * start.weight; + const averaged = videoStartLatency === null ? sample : (videoStartLatency + sample) / 2; + videoStartLatency = Math.min(0.1, Math.max(0, averaged)); +} // fallow-ignore-next-line complexity export function syncRuntimeMedia(params: { @@ -510,6 +527,8 @@ export function syncRuntimeMedia(params: { getCompositionDuration: () => number; }): void { const forceMuteAll = !!(params.outputMuted || params.userMuted); + const startWeight = playheadWasRunning ? 1 : 0.5; + playheadWasRunning = params.playing && params.clockAdvancing !== false; for (const clip of params.clips) { const { el } = clip; if (!el.isConnected) continue; @@ -521,11 +540,8 @@ export function syncRuntimeMedia(params: { const cueAhead = el.tagName === "AUDIO" ? (params.cueAheadSeconds ?? 0) - : params.playing && (el.paused || startedEarly.has(el)) && !isRenderDriven(el) - ? Math.min( - VIDEO_PREROLL_SECONDS, - Math.max(VIDEO_MIN_LEAD_SECONDS, clip.mediaStart / rateAt(clipRate, 0)), - ) * params.playbackRate + : params.playing && videoStartLatency !== null + ? videoStartLatency * params.playbackRate + (params.cueAheadSeconds ?? 0) / 2 : 0; const startsEarly = !inWindow && @@ -541,10 +557,7 @@ export function syncRuntimeMedia(params: { !inWindow && (params.timeSeconds >= clip.end || sameInstant(params.timeSeconds, clip.end)) && isClipVisibleAt(params.timeSeconds, clip.start, clip.end, params.getCompositionDuration()); - const relTime = - startsEarly && isNonLoopVideo - ? Math.max(0, clip.mediaStart - dueIn * rateAt(clipRate, 0)) - : mediaSourceTimeAt(clip, params.timeSeconds); + const relTime = mediaSourceTimeAt(clip, params.timeSeconds); const isHeldVideoTail = isTerminalVideo || (isNonLoopVideo && clip.sourceDuration != null && relTime >= clip.sourceDuration && inWindow); @@ -736,7 +749,7 @@ export function syncRuntimeMedia(params: { // A loop's playhead wraps before its element, so its offset jumps by a lap: steering owns loops. const ahead = !clip.loop && - ((offset < -VIDEO_LEAD_HOLD_SECONDS && !startedEarly.has(el)) || + (offset < -VIDEO_LEAD_HOLD_SECONDS || atOutPoint || currentElTime >= outPointHoldTime(el, outPoint, watched)); if (!playing || (!ahead && offset >= -MEDIA_SYNC_TOLERANCE_SECONDS)) heldAhead.delete(el); @@ -747,6 +760,7 @@ export function syncRuntimeMedia(params: { } const holdingAhead = heldAhead.has(el); const isPlayingMedia = !el.paused && playing; + if (isPlayingMedia) learnVideoStartLatency(el, params.timeSeconds, offset, baseRate); // Only apply strict sync when offset has stabilized (not growing). // During initial buffering, offset grows ~16ms/tick as the timeline // advances while media stays at 0. Accumulated drift from pause/play @@ -774,11 +788,8 @@ export function syncRuntimeMedia(params: { strictDriftSamples.set(el, 0); } const forceSync = !isPlayingMedia && !holdingAhead && params.forceSync && drift > 0.02; - const isHiddenPreRoll = el.tagName === "VIDEO" && startedEarly.has(el); const steered = - isPlayingMedia && !hardSync - ? steerMedia(el, offset, baseRate, offsetStabilized, undefined, isHiddenPreRoll) - : null; + isPlayingMedia && !hardSync ? steerMedia(el, offset, baseRate, offsetStabilized) : null; const steerSeeks = steered?.seek === true; const dipped = steerSeeks && params.seekUnderDip?.(el, relTime) === true; try { @@ -791,13 +802,7 @@ export function syncRuntimeMedia(params: { // ignore unsupported playbackRate swallow("runtime.media.site1", err); } - const prerollSeek = - startsEarly && - isNonLoopVideo && - el.paused && - !playRequested.has(el) && - drift > MEDIA_SYNC_TOLERANCE_SECONDS; - if (hardSync || strictSync || forceSync || prerollSeek || (steerSeeks && !dipped)) { + if (hardSync || strictSync || forceSync || (steerSeeks && !dipped)) { // Skip the per-tick seek (and the `el.load()` drift-recovery retry // below) for `