fix(player): stop the preview freezing when many clips share one long recording (#7795)

* fix(core): a clip the preview lets go of stays unloaded in Firefox

Releasing a clip dropped its src, called load() and set the src again.
Firefox treats preload="none" as "metadata" while an explicit load() is
pending, so every release fetched the clip's metadata and first frame. The
runtime then saw a far clip loading and released it again, in a loop: about
20,700 media requests over one 2:16 film, with 2-4 s stalls at cuts.

Setting src already runs the media load algorithm, which ends the fetch, so
the release now sets it again without load(). Chrome and Safari behave as
before: Blink clears its ignore-preload-none flag when src is set, and
WebKit's src set cancels the prepareToPlay that load() queued.

To reproduce: in Firefox, give a <video> with a src preload="none", then call
removeAttribute("src"), load() and setAttribute("src", src) in one task. It
loads to readyState 4; with the src set in a later task it stays at 0.

* fix(player): a film cut from one long recording plays through in Chrome and Firefox

A film whose clips are cut from one or two long recordings froze a few
seconds in, in Chrome and Firefox. Both keep a paused clip's request open
with its unread bytes, and every request to one host shares one HTTP/2 or
HTTP/3 connection window. With every clip of the file loading at parse, those
idle streams fill the window, and the request for the clip coming up is never
answered.

The player's frame now holds clips back on every engine, as it already did on
WebKit: each file keeps the clip on screen and the next one loading, and
frees the rest. The player also names its frame, because a composition in an
opaque sandbox cannot read frameElement to find the player's mark.

To reproduce: play a film of 37 clips cut from a 363 MB and a 180 MB .mov
served over HTTP/3 in <hyperframes-player>. On main it stops at 0:17 in
Chrome and at 0:09 in Firefox and never recovers.

* chore(player): raise the packed size budget for the frame name

Naming the player's frame adds 44 bytes to the packed player (158,706 to
158,750 bytes with pnpm 10.17.1). main was already 10 bytes under the 1%
tolerance of its 157,145-byte budget, so any growth fails the check. The
budget moves to 5% above the new measured size, as the check suggests.

* fix(core): seeking in the player stays as it was in Chrome and Firefox

Holding clips back in every player frame also moved Chrome and Firefox
player frames onto Studio's paused-seek path, which keeps the old picture
until every clip on screen has its new frame. In the player's scrub
benchmark the inline p95 rose from about 1.0 s on main to 1.9 s in CI,
and a seek could miss the benchmark's 1 s limit. In Firefox a paused seek
to a clip the player had let go showed no frame for about 5 s, because the
clip was released again each time it loaded.

Chrome and Firefox player frames now hold clips back and seek as they did
on main. Studio and Safari player frames keep the path they already had.
This commit is contained in:
Robin Ebers
2026-11-16 18:15:46 +00:00
committed by GitHub
parent 5be6c1580f
commit 42508c1ef1
8 changed files with 108 additions and 65 deletions
+51 -52
View File
@@ -4,6 +4,7 @@ import { mediaElementTime } from "./mediaClock";
import { HF_COLOR_GRADING_ATTR, serializeHfColorGrading } from "../colorGrading";
import {
PLAYER_FRAME_ATTR,
PLAYER_FRAME_NAME,
STUDIO_PREVIEW_LAZY_ATTR,
STUDIO_PREVIEW_CAPTURE_MODE,
STUDIO_PREVIEW_MARK_META,
@@ -76,6 +77,7 @@ function resetRuntimeGlobals(): void {
delete win.__hyperframeRuntimeBootstrapped;
delete win.__hfFirstPassHidden;
delete (document as { readyState?: unknown }).readyState;
window.name = "";
}
// The first evaluation transforms the whole runtime graph. Paying it while the file loads keeps
@@ -488,9 +490,9 @@ describe("runtime entry", () => {
srcWrites.observe(now, { attributeFilter: ["src"] });
window.__player?.seek(7.6);
expect(armed(now, soon, later)).toEqual([false, false, true]);
// The two that left are reloaded empty with src dropped for the reload, ending any fetch in flight.
// The two that left are released: now by setting its src again, soon (no src) by reloading empty.
expect([now.preload, soon.preload]).toEqual(["none", "none"]);
expect(loadsOf(now, soon, later)).toEqual([2, 2, 1]);
expect(loadsOf(now, soon, later)).toEqual([1, 2, 1]);
expect(srcWrites.takeRecords()).toHaveLength(2);
expect(now.getAttribute("src")).toBe("now.mp4");
});
@@ -548,39 +550,45 @@ describe("runtime entry", () => {
if (userAgent === "safari") asSafari();
};
it("in the player on Safari a file keeps the take on screen and the next one loading, and frees the rest", async () => {
// Chromium and Firefox keep a paused clip's request open with its unread bytes, and every
// request to one host shares one HTTP/2 or HTTP/3 window: with each take loaded, the window
// fills and the next take's request is never answered.
it.each(["safari", "chrome"] as const)(
"in the player on %s a file keeps the take on screen and the next one loading, and frees the rest",
async (userAgent) => {
spyLoad();
inPlayerFrame(userAgent);
const [first, second, third] = takesOfOneFile();
await evaluateRuntime();
expect([first, second, third].map((take) => armed(...take))).toEqual([
[true, true],
[true, true],
[false, false],
]);
window.__player?.seek(3.5);
const video = second![0]!;
Object.defineProperty(video, "readyState", { value: 4, configurable: true });
video.dispatchEvent(new Event("seeked"));
expect([first, second, third].map((take) => armed(...take))).toEqual([
[false, false],
[true, true],
[true, true],
]);
},
);
it("holds back the takes after the next in a player frame the composition cannot read, as with an opaque sandbox", async () => {
spyLoad();
inPlayerFrame("safari");
const [first, second, third] = takesOfOneFile();
await evaluateRuntime();
expect([first, second, third].map((take) => armed(...take))).toEqual([
[true, true],
[true, true],
[false, false],
]);
window.__player?.seek(3.5);
const video = second![0]!;
Object.defineProperty(video, "readyState", { value: 4, configurable: true });
video.dispatchEvent(new Event("seeked"));
expect([first, second, third].map((take) => armed(...take))).toEqual([
[false, false],
[true, true],
[true, true],
]);
});
it("in the player on Chromium every take of a file loads at parse", async () => {
spyLoad();
inPlayerFrame("chrome");
window.name = PLAYER_FRAME_NAME;
const takes = takesOfOneFile();
await evaluateRuntime();
expect(takes.map((take) => armed(...take))).toEqual([
[true, true],
[true, true],
[true, true],
[false, false],
]);
});
@@ -1115,30 +1123,21 @@ describe("runtime entry", () => {
expect([first.preload, later.preload, untrimmed.preload]).toEqual(["", "none", ""]);
});
it("gives each parsed clip preload none in the player's frame, where no clip fetches ahead of the first take", async () => {
const frame = document.createElement("iframe");
frame.setAttribute(PLAYER_FRAME_ATTR, "");
vi.spyOn(window, "frameElement", "get").mockReturnValue(frame);
asSafari();
const root = mountRoot();
timed(root, "div", "0");
await evaluateRuntime();
const [first, later] = videos(root, "0", "5");
await Promise.resolve();
expect([first.preload, later.preload]).toEqual(["", "none"]);
});
it("leaves Chromium loading every clip at parse in the player's frame", async () => {
const frame = document.createElement("iframe");
frame.setAttribute(PLAYER_FRAME_ATTR, "");
vi.spyOn(window, "frameElement", "get").mockReturnValue(frame);
const root = mountRoot();
timed(root, "div", "0");
await evaluateRuntime();
const [first, later] = videos(root, "0", "5");
await Promise.resolve();
expect([first.preload, later.preload]).toEqual(["", ""]);
});
it.each(["safari", "chrome"] as const)(
"gives each parsed clip preload none in the player's frame on %s, where no clip fetches ahead of the first take",
async (userAgent) => {
const frame = document.createElement("iframe");
frame.setAttribute(PLAYER_FRAME_ATTR, "");
vi.spyOn(window, "frameElement", "get").mockReturnValue(frame);
if (userAgent === "safari") asSafari();
const root = mountRoot();
timed(root, "div", "0");
await evaluateRuntime();
const [first, later] = videos(root, "0", "5");
await Promise.resolve();
expect([first.preload, later.preload]).toEqual(["", "none"]);
},
);
it("leaves a frame the player did not mark loading every clip at parse", async () => {
vi.spyOn(window, "frameElement", "get").mockReturnValue(document.createElement("iframe"));
@@ -994,6 +994,27 @@ it("keeps pending-seek Play synchronous in an unmarked Safari player frame", ()
expect(next.some(({ video }) => vi.mocked(video.play).mock.calls.length > 0)).toBe(true);
});
it.each([
[
"Chrome",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36",
],
["Firefox", "Mozilla/5.0 (X11; Linux x86_64; rv:160.0) Gecko/20100101 Firefox/160.0"],
])(
"moves the playhead at once on a paused seek in a %s player frame, with no picture held",
(_, userAgent) => {
vi.spyOn(navigator, "userAgent", "get").mockReturnValue(userAgent);
const frame = document.createElement("iframe");
frame.setAttribute(PLAYER_FRAME_ATTR, "");
vi.spyOn(window, "frameElement", "get").mockReturnValue(frame);
const { main } = film(false, false);
void window.__player!.seek(6);
expect(main.time?.()).toBe(6);
expect(document.querySelectorAll("canvas")).toHaveLength(0);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(false);
},
);
it("retains an explicit unmarked provider handoff before its first picture", async () => {
const { main, old, next } = safariPlayerFilm();
for (const video of old) video.metadataAt(0);
+1 -1
View File
@@ -5173,7 +5173,7 @@ export function initSandboxRuntimeModular(): void {
nativePlayTime = undefined;
wallClearTimeout(nativeWarmTimer);
if (
!mediaDeferred ||
!(studioPreview || (playerFrame && isWebKit())) ||
state.nativeMediaSyncDisabled ||
isExportRenderDrivingFrames() ||
window.__HF_EXPORT_RENDER_SEEK_CONFIG ||
+17 -6
View File
@@ -108,14 +108,21 @@ describe("initial media preload", () => {
});
describe("releasing a clip's download", () => {
it("reloads a src clip with its src dropped, then puts the src back", () => {
const srcWrites = (el: Element) => {
const observer = new MutationObserver(() => {});
observer.observe(el, { attributeFilter: ["src"] });
return () => observer.takeRecords().length;
};
it("reloads a src clip by setting its src again, not by load(), after which Firefox fetches metadata whatever the preload", () => {
const video = document.createElement("video");
video.setAttribute("src", "a.mp4");
const atLoad: (string | null)[] = [];
video.load = () => atLoad.push(video.getAttribute("src"));
video.load = vi.fn();
const writes = srcWrites(video);
releaseMedia(video);
expect(atLoad).toEqual([null]);
expect(writes()).toBeGreaterThan(0);
expect(video.load).not.toHaveBeenCalled();
expect(video.getAttribute("src")).toBe("a.mp4");
});
@@ -127,9 +134,11 @@ describe("releasing a clip's download", () => {
value: { length: 1, start: () => 0, end: () => 6.67 },
});
video.load = vi.fn();
const writes = srcWrites(video);
releaseMedia(video);
expect(video.load).not.toHaveBeenCalled();
expect(writes()).toBe(0);
expect(video.getAttribute("src")).toBe("a.mp4");
});
@@ -141,9 +150,10 @@ describe("releasing a clip's download", () => {
value: { length: 1, start: () => 0, end: () => 6.67 },
});
video.load = vi.fn();
const writes = srcWrites(video);
releaseMedia(video, true);
expect(video.load).toHaveBeenCalledTimes(1);
expect(writes()).toBeGreaterThan(0);
expect(video.getAttribute("src")).toBe("a.mp4");
});
@@ -158,9 +168,10 @@ describe("releasing a clip's download", () => {
value: { length: 1, start: () => from, end: () => to },
});
video.load = vi.fn();
const writes = srcWrites(video);
releaseMedia(video);
expect(video.load).toHaveBeenCalledTimes(1);
expect(writes()).toBeGreaterThan(0);
});
it("leaves a clip on <source> children, their order and its markup untouched", () => {
+4 -2
View File
@@ -99,8 +99,10 @@ export function releaseMedia(media: HTMLMediaElement, whenBuffered = false): voi
if (media.querySelector("source") || (!whenBuffered && wholeFileBuffered(media))) return;
if (holdsWhenIdle(media)) return holdUntilDue(media);
const src = media.getAttribute("src");
stopMediaDownload(media);
if (src !== null) media.setAttribute("src", src);
if (src === null) return stopMediaDownload(media);
cancelPlaybackStart(media);
media.removeAttribute("src");
media.setAttribute("src", src);
}
export function lengthIsAuthored(media: Element): boolean {
+6 -3
View File
@@ -1,5 +1,3 @@
import { isWebKit } from "./runtime/mediaClock.js";
export const STUDIO_PREVIEW_MARK_META = "hyperframes-studio-preview";
export const PREVIEW_SEEK_HOLD_ATTR = "data-hf-preview-seek-hold";
export const PREVIEW_VIDEO_FRAME_ATTR = "data-hf-preview-video-frame";
@@ -23,9 +21,14 @@ export function getLogicalOpacity(el: Element, computed: CSSStyleDeclaration): s
return computed.opacity;
}
export const PLAYER_FRAME_ATTR = "data-hf-player-frame";
export const PLAYER_FRAME_NAME = "hyperframes-player";
export function inPlayerFrame(doc: Document): boolean {
return isWebKit() && doc.defaultView?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true;
const frame = doc.defaultView;
return (
frame?.name === PLAYER_FRAME_NAME ||
frame?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true
);
}
export function defersMediaIn(doc: Document): boolean {
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest";
import { mediaElementTime } from "@hyperframes/core/runtime/media-clock";
import { PLAYER_FRAME_NAME } from "@hyperframes/core/studio-preview-mark";
import { formatTime, formatSpeed, SPEED_PRESETS } from "./controls.js";
// Install a stubbed contentDocument getter on the given iframe element. The
@@ -3733,6 +3734,11 @@ describe("HyperframesPlayer retained runtime data", () => {
expect(player.iframeElement.sandbox.contains("allow-same-origin")).toBe(true);
});
it("names its frame, the one mark a composition in an opaque sandbox can still read", () => {
player.setAttribute("sandbox-origin", "opaque");
expect(player.iframeElement.name).toBe(PLAYER_FRAME_NAME);
});
it("treats every non-null sandbox-origin value as restrictive", () => {
player.setAttribute("sandbox-origin", "opaqu");
expect(player.iframeElement.sandbox.contains("allow-same-origin")).toBe(false);
+2 -1
View File
@@ -4,7 +4,7 @@
* Keeps constructor boilerplate out of the web component class body.
*/
import { PLAYER_FRAME_ATTR } from "@hyperframes/core/studio-preview-mark";
import { PLAYER_FRAME_ATTR, PLAYER_FRAME_NAME } from "@hyperframes/core/studio-preview-mark";
// Cached Constructable Stylesheet shared across all player instances.
let _sharedSheet: CSSStyleSheet | null = null;
@@ -52,6 +52,7 @@ export function createCompositionIframe(): {
iframe.referrerPolicy = "same-origin";
iframe.title = "HyperFrames Composition";
iframe.setAttribute(PLAYER_FRAME_ATTR, "");
iframe.name = PLAYER_FRAME_NAME;
container.appendChild(iframe);
return { container, iframe };