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
This commit is contained in:
Miguel Ángel
2026-11-24 01:58:38 +00:00
committed by GitHub
parent c9f9a52077
commit e2fe0e6f53
3 changed files with 117 additions and 0 deletions
+2
View File
@@ -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();
@@ -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);
+40
View File
@@ -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<string, string> = {
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;