diff --git a/packages/core/src/runtime/entry.test.ts b/packages/core/src/runtime/entry.test.ts index 78f1b31fc8..bc027bdb3c 100644 --- a/packages/core/src/runtime/entry.test.ts +++ b/packages/core/src/runtime/entry.test.ts @@ -533,6 +533,39 @@ describe("runtime entry", () => { expect(loadsOf(now!)).toEqual([1]); }); + // fails without the fix: a preview loaded every clip with no authored length in full, so a film of hundreds ran + // the page out of memory. + it("in a preview a far clip with no authored length loads only its length, then gives its decoder back and keeps the length", async () => { + spyLoad(); + servePreview(); + const clips = videos(mountRoot(), "0", "1.5", "6"); + for (const clip of clips) clip.removeAttribute("data-duration"); + const [now, soon, later] = clips as [HTMLVideoElement, HTMLVideoElement, HTMLVideoElement]; + later.setAttribute("src", "later.mp4"); + + await evaluateRuntime(); + expect(armed(now, soon)).toEqual([true, true]); + // A clip the window arms loads whole ("auto"); this one only its length. + expect(later.preload).toBe("metadata"); + expect(loadsOf(later)).toEqual([1]); + + Object.defineProperty(later, "readyState", { value: 1, configurable: true }); + Object.defineProperty(later, "duration", { value: 2, configurable: true }); + later.dispatchEvent(new Event("loadedmetadata")); + // Freed by a reload with its src dropped and put back, as a clip the playhead left is. + expect(later.preload).toBe("none"); + expect(loadsOf(later)).toEqual([2]); + expect(later.getAttribute("src")).toBe("later.mp4"); + + // The released clip reports no length again; the timeline keeps the one it reported, for that src only. + Object.defineProperty(later, "readyState", { value: 0, configurable: true }); + Object.defineProperty(later, "duration", { value: Number.NaN, configurable: true }); + const { resolveMediaElementDurationSeconds } = await import("./playbackRate"); + expect(resolveMediaElementDurationSeconds(later)).toBe(2); + later.setAttribute("src", "other.mp4"); + expect(resolveMediaElementDurationSeconds(later)).toBeNull(); + }); + // Three takes of one file, each a picture and a sound, as an edit cut from a single source is. const takesOfOneFile = () => { const root = mountRoot(); @@ -639,7 +672,7 @@ describe("runtime entry", () => { expect(armed(now, soon)).toEqual([true, false]); }); - it("loads a clip with no authored length as a render does, so no reload drops its duration", async () => { + it("loads a due clip with no authored length whole and a far one's length only, so no reload drops a duration", async () => { spyLoad(); servePreview(); const [untrimmed, later] = videos(mountRoot(), "0", "8"); @@ -652,7 +685,8 @@ describe("runtime entry", () => { window.__player?.seek(3.6); window.__player?.seek(0.5); window.__player?.seek(7.5); - expect(armed(untrimmed, later)).toEqual([true, true]); + expect(untrimmed.preload).toBe("auto"); + expect(later.preload).toBe("metadata"); expect(loadsOf(untrimmed, later)).toEqual([1, 1]); }); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index f6e13592f1..4762558005 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -2754,9 +2754,23 @@ export function initSandboxRuntimeModular(): void { // 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 => isVideoElement(el) && !isWebKit(); + // A preview's video with no set length loads its length first; once known, the window decides it. + const learnsLength = (el: unknown): el is HTMLVideoElement => + mediaDeferred && + loadsAhead && + freesBufferedVideo(el) && + el.hasAttribute("data-start") && + !lengthIsAuthored(el); + // Clips that loaded their length; the document's timing listener (sourceLength.ts) keeps it, and runs first. + const learned = new WeakSet(); // A far clip can finish a load the parser started before its window was decided. const onMediaLoadedWhileFar = (event: Event) => { const el = event.currentTarget; + if (learnsLength(el) && !learned.has(el)) { + learned.add(el); + syncTimedElementVisibility(state.currentTime, [el]); + return; + } if (!freesBufferedVideo(el) || mediaNearPlayhead.get(el) !== false || el.readyState === 0) return; el.preload = "none"; @@ -2861,7 +2875,13 @@ export function initSandboxRuntimeModular(): void { // Studio's preview loads a timed clip only near the playhead; the visibility pass decides. if (inPreloadWindow(mediaEl)) nearPlayheadPending.push(mediaEl); - else preloadMedia(mediaEl); + else if ( + learnsLength(mediaEl) && + resolveAbsoluteMediaStartSeconds(mediaEl) > state.currentTime + LOOKAHEAD_SECONDS + ) { + mediaEl.preload = "metadata"; + mediaEl.load(); + } else preloadMedia(mediaEl); // Probe volume automation from the GSAP timeline — same approach as the // renderer (see discoverAudioVolumeAutomationFromTimeline / audioMixer). @@ -3134,7 +3154,9 @@ export function initSandboxRuntimeModular(): void { const inPreloadWindow = (el: HTMLMediaElement) => mediaDeferred && el.hasAttribute("data-start") && - (lengthIsAuthored(el) || (!loadsAhead && el.readyState === HTMLMediaElement.HAVE_NOTHING)); + (lengthIsAuthored(el) || + (learnsLength(el) && learned.has(el) && Number.isFinite(sourceLengthOf(el))) || + (!loadsAhead && el.readyState === HTMLMediaElement.HAVE_NOTHING)); // Media on screen or due within the look-ahead loads; the rest stops holding a connection to // the origin that also serves Studio's thumbnails.