From 92816ed56bde07affff0f6e0a5bce3dd96a96808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 14 Nov 2026 02:04:23 +0000 Subject: [PATCH] perf(core): a reloaded preview video resyncs only itself (#7684) A paused preview's loadedmetadata handler put a reloaded video back at the playhead by resyncing every clip and re-deciding every timed element in the film. It now syncs and decides only the video that reloaded. --- .../init.reloadedVideoKeepsPlayhead.test.ts | 78 +++++++++++++++++++ packages/core/src/runtime/init.ts | 37 +++++---- 2 files changed, 98 insertions(+), 17 deletions(-) diff --git a/packages/core/src/runtime/init.reloadedVideoKeepsPlayhead.test.ts b/packages/core/src/runtime/init.reloadedVideoKeepsPlayhead.test.ts index 5d88e12d58..9cb3f1f359 100644 --- a/packages/core/src/runtime/init.reloadedVideoKeepsPlayhead.test.ts +++ b/packages/core/src/runtime/init.reloadedVideoKeepsPlayhead.test.ts @@ -4,6 +4,32 @@ import { initSandboxRuntimeModular } from "./init"; import { evictMediaSyncState } from "./media"; import { createMockTimeline, resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers"; +const asked = vi.hoisted(() => ({ about: null as Set | null })); + +vi.mock("./startResolver", async (importOriginal) => { + const actual = await importOriginal(); + const noting = + (resolve: (el: Element, ...rest: A) => R) => + (el: Element, ...rest: A): R => { + asked.about?.add(el); + return resolve(el, ...rest); + }; + return { + ...actual, + createRuntimeStartTimeResolver: ( + params: Parameters[0], + ) => { + const resolver = actual.createRuntimeStartTimeResolver(params); + return { + ...resolver, + resolveStartForElement: noting(resolver.resolveStartForElement), + resolveDurationForElement: noting(resolver.resolveDurationForElement), + resolveMediaStartForElement: noting(resolver.resolveMediaStartForElement), + }; + }, + }; +}); + /** A loaded, paused video whose `load()` starts it over at 0, as a browser's does. */ function loadedVideo(el: HTMLVideoElement) { Object.defineProperty(el, "duration", { value: 6.27, configurable: true }); @@ -54,7 +80,59 @@ it("leaves a render's videos to the render", () => { expect(clip.currentTime).toBe(0); }); +/** The film's shape: 10 scenes of 20 timed lines, 14 videos; scene 3's first video reloads `into` s in. */ +function reloadInLongFilm(into: number) { + resetRuntimeFixtureDom(); + const videos = (s: number, count: number) => + Array.from( + { length: count }, + (_, v) => + ``, + ).join(""); + const scenes = Array.from({ length: 10 }, (_, s) => { + const lines = Array.from( + { length: 20 }, + (_, k) => `
${k}
`, + ).join(""); + return ( + `
` + + `${lines}${videos(s, s < 4 ? 2 : 1)}
` + ); + }).join(""); + document.body.innerHTML = `
${scenes}
`; + document.querySelectorAll("video").forEach(loadedVideo); + window.__timelines = { main: createMockTimeline(1410) }; + initSandboxRuntimeModular(); + window.__player!.seek(3 * 141 + 10 + into); + const reloaded = document.getElementById("v3-0") as HTMLVideoElement; + expect(reloaded.currentTime).toBeCloseTo(mediaElementTime(into), 6); + + evictMediaSyncState(reloaded); + reloaded.load(); + asked.about = new Set(); + reloaded.dispatchEvent(new Event("loadedmetadata")); + const otherClips = [...asked.about].filter( + (el) => !el.hasAttribute("data-composition-id") && el !== reloaded, + ); + asked.about = null; + return { reloaded, otherClips }; +} + +// A long film reloads its clips one by one as the playhead nears them; each reload re-decided every +// timed element in the film, which froze a 23-minute, 10-scene film for seconds at a time. +it("a reloaded video is put back at the playhead without re-deciding the rest of the film", () => { + const { reloaded, otherClips } = reloadInLongFilm(5); + expect(reloaded.currentTime).toBe(mediaElementTime(5)); + expect(otherClips).toEqual([]); +}); + +it("a reloaded video a frame off the playhead is still put back on it", () => { + const { reloaded } = reloadInLongFilm(0.1); + expect(reloaded.currentTime).toBeCloseTo(mediaElementTime(0.1), 6); +}); + afterEach(() => { + asked.about = null; window.__hfRuntimeTeardown?.(); delete window.__HF_EXPORT_RENDER_SEEK_CONFIG; vi.restoreAllMocks(); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 010cf5225f..67a3e1ca92 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -2731,9 +2731,7 @@ export function initSandboxRuntimeModular(): void { isRenderMode(el) ) return; - lastSyncedMediaTimeSeconds = null; - state.mediaForceSyncNextTick = true; - syncMediaForCurrentState(); + syncMediaForCurrentState(undefined, el); }; // Chromium's software decoders cost threads per loaded video; WebKit's reload of a shared file stalls audio. const freesBufferedVideo = (el: unknown): el is HTMLVideoElement => @@ -3858,7 +3856,7 @@ export function initSandboxRuntimeModular(): void { runtimeCleanupCallbacks.push(watchMediaFetches(document)); let lastVisibilityPassSeconds: number | null = null; - const syncMediaForCurrentState = (timingRevision?: number) => { + const syncMediaForCurrentState = (timingRevision?: number, onlyReloaded?: HTMLMediaElement) => { if (previewSeek?.time !== undefined && !previewSeek.committing) return; // Scope 1 of 3 (see `withTimingResolver`). Closes before `syncRuntimeMedia`, // which may call `el.load()` and invalidate every cached duration. @@ -3876,6 +3874,7 @@ export function initSandboxRuntimeModular(): void { ) : 0; const mediaClips = withTimingResolver(() => { + if (onlyReloaded) return buildRuntimeMediaCache([onlyReloaded]).mediaClips; if (!indexed) return buildRuntimeMediaCache().mediaClips; const index = resolveMediaClipIndex(timingRevision); // Windows come from the index, but every field handed to `syncRuntimeMedia` @@ -3897,8 +3896,8 @@ export function initSandboxRuntimeModular(): void { // below still records a leased clip and the pass after its release visits it. const syncedClips = transportMovableClips(mediaClips); - const forceSync = state.mediaForceSyncNextTick; - if (forceSync) state.mediaForceSyncNextTick = false; + const forceSync = onlyReloaded !== undefined || state.mediaForceSyncNextTick; + if (!onlyReloaded) state.mediaForceSyncNextTick = false; if (syncedClips) { syncRuntimeMedia({ clips: syncedClips, @@ -3929,17 +3928,21 @@ export function initSandboxRuntimeModular(): void { postRuntimeMessage({ source: "hf-preview", type: "media-autoplay-blocked" }); }, }); - if (indexed) { - lastSyncedMediaTimeSeconds = state.currentTime; - // Every clip not visited was out of window at both ends of this seek, so - // the visited ones are the only possible members. - mediaClipsInWindow = mediaClips.filter((clip) => - isInClipWindow(state.currentTime, clip.start - MEDIA_SYNC_TOLERANCE_SECONDS, clip.end), - ); - } else { - lastSyncedMediaTimeSeconds = null; - mediaClipsInWindow = []; - } + } + // A reload moves no clip window, so the sweep's cursor and in-window set still hold. + if (onlyReloaded) { + syncTimedElementVisibility(state.currentTime, [onlyReloaded]); + return; + } + if (syncedClips && indexed) { + lastSyncedMediaTimeSeconds = state.currentTime; + // Unvisited clips were out of window at both ends of this seek: only visited ones can be in. + mediaClipsInWindow = mediaClips.filter((clip) => + isInClipWindow(state.currentTime, clip.start - MEDIA_SYNC_TOLERANCE_SECONDS, clip.end), + ); + } else if (syncedClips) { + lastSyncedMediaTimeSeconds = null; + mediaClipsInWindow = []; } const playedFrom = indexed && state.isPlaying && !forceSync && lastVisibilityPassSeconds !== null