From e2fe0e6f53430f80efe50883e3d4dab86299689d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 01:58:38 +0000 Subject: [PATCH] fix(core): the 4K clip on screen plays its own file while its preview copy is made (#8602) * fix(core): the 4K clip on screen plays its own file while its preview copy is made * fix(core): a clip that played its own file is held again once it leaves the screen --- packages/core/src/runtime/init.ts | 2 + packages/core/src/runtime/mediaProxy.test.ts | 75 ++++++++++++++++++++ packages/core/src/runtime/mediaProxy.ts | 40 +++++++++++ 3 files changed, 117 insertions(+) diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index b7c9ae29a9..e50a8de72c 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -127,6 +127,7 @@ import { handleMetadataForProxy, isRenderMode, maybeProxyProactively, + decideFileTurn, describeVideoSources, levelsWithLighterCopies, retryPreviewCopies, @@ -3364,6 +3365,7 @@ export function initSandboxRuntimeModular(): void { resolveAbsoluteMediaStartSeconds(el) + (resolveDurationForElement(el) ?? Number.NaN) <= time; const spent = passed && (decided !== true || (el.networkState === 2 && el.readyState < 4)); const near = visible || armed || (upcoming && !unslotted && !spent); + decideFileTurn(el, visible); if (near) giveBackWhenDue(el); if ((visible || !near) && endPrefetch(el)) queueMicrotask(fillPrefetchSlots); const approaching = near && !visible && resolveAbsoluteMediaStartSeconds(el) > clock.now(); diff --git a/packages/core/src/runtime/mediaProxy.test.ts b/packages/core/src/runtime/mediaProxy.test.ts index 7bcfeb6555..df1582870e 100644 --- a/packages/core/src/runtime/mediaProxy.test.ts +++ b/packages/core/src/runtime/mediaProxy.test.ts @@ -1354,6 +1354,81 @@ describe("preview quality: lighter copies of heavy files", () => { } }); + async function twoHeldOnScreen(facts: PreviewSourceFacts = HEAVY) { + vi.useFakeTimers(); + let made = false; + vi.stubGlobal( + "fetch", + vi.fn(async (url: string) => + !String(url).includes("hf-proxy") || made ? new Response("x", { status: 206 }) : making(), + ), + ); + const staged = await stage({ "/a.mp4": facts, "/b.mp4": facts }); + const [a, b] = [staged.video("/a.mp4"), staged.video("/b.mp4")]; + for (const el of [a, b]) staged.maybeProxyProactively(el); + await vi.advanceTimersByTimeAsync(0); + const preload = await import("./preloadMedia"); + const proxySrc = await import("./proxySrc"); + // One media tick, as init.ts runs it: each clip and whether it is on screen. + const tick = (onScreen: HTMLVideoElement | null) => { + for (const el of [a, b]) staged.decideFileTurn(el, el === onScreen); + return [a.getAttribute("src"), b.getAttribute("src")]; + }; + return { ...staged, a, b, tick, preload, proxySrc, made: () => (made = true) }; + } + + // fails without the fix: every clip near the playhead stayed held with no picture until its copy was made, so a + // 4K clip on screen showed a frozen frame for as long as its copy took (6.7 s for a 15 s clip). + it("lets one clip on screen play its own file while its copy is made, and keeps the rest held", async () => { + const { a, b, decideFileTurn } = await twoHeldOnScreen(); + expect([a.hasAttribute("src"), b.hasAttribute("src")]).toEqual([false, false]); + + decideFileTurn(a, true); + decideFileTurn(b, true); + + expect([a.getAttribute("src"), b.getAttribute("src")]).toEqual(["/a.mp4", null]); + }); + + // A scrub or a loop crosses clips whose copies are still being made: a clip that leaves the screen goes back on + // hold, or each one shown once would keep its oversized file for good. + it("holds a clip again once it leaves the screen, so clips crossing the playhead play their own file one at a time", async () => { + const { a, b, tick, preload, proxySrc } = await twoHeldOnScreen(); + + expect(tick(a)).toEqual(["/a.mp4", null]); + expect(tick(b)).toEqual([null, "/b.mp4"]); + expect(proxySrc.heldForCopy.has(a)).toBe(true); + + preload.giveBackWhenDue(a); + expect(a.hasAttribute("src")).toBe(false); + expect(tick(a)).toEqual([null, null]); + expect(tick(a)).toEqual(["/a.mp4", null]); + }); + + it("still swaps the clip that played its own file to its copy once the copy is made", async () => { + const { a, decideFileTurn, made } = await twoHeldOnScreen(); + decideFileTurn(a, true); + + made(); + await vi.advanceTimersByTimeAsync(2_000); + + expect(params(a).get("hf-proxy-box")).toBe("2048x1218"); + }); + + it("keeps a file deeper than 8 bits held where the browser does not name its codec at that depth", async () => { + const { a, decideFileTurn } = await twoHeldOnScreen({ + ...HEAVY, + bitDepth: 10, + codec: "h264", + }); + a.canPlayType = vi.fn((mime: string) => + mime.includes("avc1.6E") ? "" : "probably", + ) as never; + + decideFileTurn(a, true); + + expect(a.hasAttribute("src")).toBe(false); + }); + it("is not given its file back as its clip comes due", async () => { const { el, preload } = await waitingForItsCopy(making); diff --git a/packages/core/src/runtime/mediaProxy.ts b/packages/core/src/runtime/mediaProxy.ts index a6a3fc2d23..5e24ec20c1 100644 --- a/packages/core/src/runtime/mediaProxy.ts +++ b/packages/core/src/runtime/mediaProxy.ts @@ -20,6 +20,7 @@ import { import { naturalVideoSize } from "../mediaDisplaySize"; import { escapeCssString } from "./editingElement"; import { + authoredSrcOf, heldForCopy, heldPosters, inPointOf, @@ -492,6 +493,45 @@ function holdForCopy(el: HTMLMediaElement): boolean { return true; } +// A file deeper than 8 bits plays only where the browser names its codec at that depth; elsewhere it stays held. +const DEEP_CODEC_MIME: Record = { + h264: 'video/mp4; codecs="avc1.6E0028"', + hevc: 'video/mp4; codecs="hvc1.2.4.L120.B0"', + vp9: 'video/webm; codecs="vp09.02.10.10"', + av1: 'video/mp4; codecs="av01.0.08M.10"', +}; + +function decodesItsFile(el: HTMLMediaElement, file: string): boolean { + const facts = sourceFactsOfSrc(file); + if (!facts || facts.bitDepth <= 8) return true; + const mime = facts.codec === undefined ? undefined : DEEP_CODEC_MIME[facts.codec]; + return mime !== undefined && el.canPlayType(mime) !== ""; +} + +let playingItsFile: HTMLMediaElement | null = null; + +const copyStillComing = (el: HTMLMediaElement) => + el.isConnected && !swappedElements.has(el) && proxyRequested.has(el); + +/** Each media tick re-decides a clip whose copy is still being made: the one on screen plays its own file, and every + * other, one that played its file and left the screen included, is held, so at most one oversized file decodes. */ +export function decideFileTurn(el: HTMLMediaElement, onScreen: boolean): void { + if (el === playingItsFile && !(onScreen && copyStillComing(el))) { + playingItsFile = null; + if (copyStillComing(el)) holdForCopy(el); + } + if (playingItsFile && !playingItsFile.isConnected) playingItsFile = null; + if (!onScreen || playingItsFile || !heldForCopy.has(el) || el.getAttribute("src") !== null) + return; + const before = heldForCopy.get(el); + const src = before ? before.written : authoredSrcOf(el); + if (src === null || !decodesItsFile(el, src)) return; + playingItsFile = el; + heldForCopy.delete(el); + recordOpenedAtInPoint(el, before?.authored ?? src, src, before?.holdsWhenIdle); + el.setAttribute("src", src); +} + /** Without a copy (`giveBack` set), a clip held until due stays held; any other gets its src back as the runtime's. */ function endCopyHold(el: HTMLMediaElement, giveBack: string | null): void { if (!heldForCopy.has(el)) return;