mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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.
This commit is contained in:
@@ -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<Element> | null }));
|
||||
|
||||
vi.mock("./startResolver", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("./startResolver")>();
|
||||
const noting =
|
||||
<A extends unknown[], R>(resolve: (el: Element, ...rest: A) => R) =>
|
||||
(el: Element, ...rest: A): R => {
|
||||
asked.about?.add(el);
|
||||
return resolve(el, ...rest);
|
||||
};
|
||||
return {
|
||||
...actual,
|
||||
createRuntimeStartTimeResolver: (
|
||||
params: Parameters<typeof actual.createRuntimeStartTimeResolver>[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) =>
|
||||
`<video id="v${s}-${v}" data-start="${10 + v * 60}" data-duration="6.27" data-track-index="0" src="/v${s}-${v}.mp4"></video>`,
|
||||
).join("");
|
||||
const scenes = Array.from({ length: 10 }, (_, s) => {
|
||||
const lines = Array.from(
|
||||
{ length: 20 },
|
||||
(_, k) => `<div data-start="${k * 7}" data-duration="4" data-track-index="1">${k}</div>`,
|
||||
).join("");
|
||||
return (
|
||||
`<div id="s${s}" data-composition-id="s${s}" data-start="${s * 141}" data-duration="141">` +
|
||||
`${lines}${videos(s, s < 4 ? 2 : 1)}</div>`
|
||||
);
|
||||
}).join("");
|
||||
document.body.innerHTML = `<div data-composition-id="main" data-root="true" data-duration="1410">${scenes}</div>`;
|
||||
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();
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user