From 4a47c33ea36970ac288662ac40ec7790590ce9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 14:58:03 +0000 Subject: [PATCH] fix(core): a far clip whose source is swapped learns its new length (#8509) * fix(core): a far clip whose source is swapped learns its new length * test(core): the swapped-source case names a freed clip, not the proxy fallback --- packages/core/src/runtime/entry.test.ts | 32 +++++++++++++++++++ packages/core/src/runtime/init.ts | 41 ++++++++++++++----------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/packages/core/src/runtime/entry.test.ts b/packages/core/src/runtime/entry.test.ts index bc027bdb3c..5627792cfd 100644 --- a/packages/core/src/runtime/entry.test.ts +++ b/packages/core/src/runtime/entry.test.ts @@ -672,6 +672,38 @@ describe("runtime entry", () => { expect(armed(now, soon)).toEqual([true, false]); }); + // A src set on a clip already freed left it at preload="none" with no length for the new src: out of its window, + // the timeline lost its length until the playhead reached it. + it("a far clip whose src is swapped after its length was learned learns the new length, then is freed again", async () => { + spyLoad(); + servePreview(); + const clips = videos(mountRoot(), "0", "6"); + for (const clip of clips) clip.removeAttribute("data-duration"); + const later = clips[1] as HTMLVideoElement; + later.setAttribute("src", "later.mp4"); + await evaluateRuntime(); + const metadata = (seconds: number) => { + Object.defineProperty(later, "readyState", { value: 1, configurable: true }); + Object.defineProperty(later, "duration", { value: seconds, configurable: true }); + later.dispatchEvent(new Event("loadedmetadata")); + }; + metadata(2); + expect([later.preload, loadsOf(later)[0]]).toEqual(["none", 2]); + + Object.defineProperty(later, "readyState", { value: 0, configurable: true }); + Object.defineProperty(later, "duration", { value: Number.NaN, configurable: true }); + later.setAttribute("src", "other.mp4"); + window.__player?.seek(0.5); + expect([later.preload, loadsOf(later)[0]]).toEqual(["metadata", 3]); + + metadata(3); + expect([later.preload, loadsOf(later)[0]]).toEqual(["none", 4]); + expect(later.getAttribute("src")).toBe("other.mp4"); + const { resolveMediaElementDurationSeconds } = await import("./playbackRate"); + Object.defineProperty(later, "duration", { value: Number.NaN, configurable: true }); + expect(resolveMediaElementDurationSeconds(later)).toBe(3); + }); + 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(); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 4762558005..d56d008752 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -2761,13 +2761,25 @@ export function initSandboxRuntimeModular(): void { 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(); + // The src each clip loaded its length for; the document's timing listener (sourceLength.ts) keeps it, and runs first. + const learned = new WeakMap(); + const knowsLength = (el: HTMLMediaElement) => learned.get(el) === el.src; + const armLength = (el: HTMLMediaElement) => { + if ( + learnsLength(el) && + resolveAbsoluteMediaStartSeconds(el) > state.currentTime + LOOKAHEAD_SECONDS + ) { + el.preload = "metadata"; + el.load(); + } else preloadMedia(el); + }; + const swappedAfterLearning = (el: HTMLMediaElement) => + learnsLength(el) && learned.has(el) && !knowsLength(el) && el.preload === "none"; // 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); + if (learnsLength(el) && !knowsLength(el)) { + learned.set(el, el.src); syncTimedElementVisibility(state.currentTime, [el]); return; } @@ -2875,13 +2887,7 @@ 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 if ( - learnsLength(mediaEl) && - resolveAbsoluteMediaStartSeconds(mediaEl) > state.currentTime + LOOKAHEAD_SECONDS - ) { - mediaEl.preload = "metadata"; - mediaEl.load(); - } else preloadMedia(mediaEl); + else armLength(mediaEl); // Probe volume automation from the GSAP timeline — same approach as the // renderer (see discoverAudioVolumeAutomationFromTimeline / audioMixer). @@ -3155,7 +3161,7 @@ export function initSandboxRuntimeModular(): void { mediaDeferred && el.hasAttribute("data-start") && (lengthIsAuthored(el) || - (learnsLength(el) && learned.has(el) && Number.isFinite(sourceLengthOf(el))) || + (learnsLength(el) && knowsLength(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 @@ -3547,12 +3553,11 @@ export function initSandboxRuntimeModular(): void { if (upcoming && !rawNode.hasAttribute(STUDIO_PREVIEW_UPCOMING_ATTR)) loadPreviewImagesIn(rawNode); rawNode.toggleAttribute(STUDIO_PREVIEW_UPCOMING_ATTR, upcoming); - if ( - !seekOwnsMedia && - isMediaElement(rawNode) && - metadataBoundMedia.has(rawNode) && - inPreloadWindow(rawNode) - ) + const managed = !seekOwnsMedia && isMediaElement(rawNode) && metadataBoundMedia.has(rawNode); + if (managed && swappedAfterLearning(rawNode)) { + mediaNearPlayhead.delete(rawNode); + armLength(rawNode); + } else if (managed && inPreloadWindow(rawNode)) preloadNearPlayhead( rawNode, isVisibleNow,