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
This commit is contained in:
Miguel Ángel
2026-11-22 14:58:03 +00:00
committed by GitHub
parent f03233ab4d
commit 4a47c33ea3
2 changed files with 55 additions and 18 deletions
+32
View File
@@ -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();
+23 -18
View File
@@ -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<HTMLMediaElement>();
// The src each clip loaded its length for; the document's timing listener (sourceLength.ts) keeps it, and runs first.
const learned = new WeakMap<HTMLMediaElement, string>();
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,