From 925a64db3906d5751d92f4ce8166222a119ea0bf Mon Sep 17 00:00:00 2001 From: Robin Ebers Date: Fri, 20 Nov 2026 19:48:28 +0000 Subject: [PATCH] fix(player): stop films cut from one long recording freezing in apps that embed the player (#8243) * fix(player): stop the preview freezing when many clips share one long recording Brings back #7795, which #7978 took out because the HyperFrames desktop app stopped re-reading a film after a script edit. The next commit keeps Studio's preview, and every app that embeds it, on main's behaviour. This reverts commit 33e1a36a3973dd587a2921575b0d399fb066d740. * fix(core): the Studio preview, and apps that embed it, keep main's clip handling With #7795 the player's frame held clips back on every engine, by its name, and released a clip by setting its src again. Studio's preview sits in a player frame too, so it took both, and the HyperFrames desktop app, which embeds that preview, stopped re-reading a film after a script edit (#7978). A page carrying Studio's preview mark now takes the same media decisions as on main: the player's frame counts there on WebKit only, as before, and a clip is released by load() with its src dropped. A plain player embed keeps the hold-back and the new release, which is what stops a film cut from one long recording freezing in Chrome and Firefox. --- packages/core/src/runtime/entry.test.ts | 138 +++++++++++------- .../core/src/runtime/init.previewSeek.test.ts | 21 +++ packages/core/src/runtime/init.ts | 2 +- .../core/src/runtime/preloadMedia.test.ts | 33 ++++- packages/core/src/runtime/preloadMedia.ts | 12 +- packages/core/src/studioPreviewMark.ts | 14 +- .../player/src/hyperframes-player.test.ts | 6 + packages/player/src/iframe-dom.ts | 3 +- 8 files changed, 167 insertions(+), 62 deletions(-) diff --git a/packages/core/src/runtime/entry.test.ts b/packages/core/src/runtime/entry.test.ts index 2eb279ed73..a11499bc56 100644 --- a/packages/core/src/runtime/entry.test.ts +++ b/packages/core/src/runtime/entry.test.ts @@ -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 @@ -548,42 +550,87 @@ 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], ]); }); + // An app that embeds Studio's preview re-reads the film after each edit, as the desktop app's script walks do. + it("in Studio's preview the player's named frame changes nothing about which clips load and are freed", async () => { + const decisions = async (frame: "named player frame" | "plain frame") => { + const log: string[] = []; + for (const starts of [ + ["0", "3", "9"], + ["0", "3", "5", "9"], + ]) { + resetRuntimeGlobals(); + vi.restoreAllMocks(); + spyLoad(); + if (frame === "named player frame") { + inPlayerFrame("chrome"); + window.name = PLAYER_FRAME_NAME; + } + servePreview(); + const root = mountRoot(); + const clips = starts.map((start) => timed(root, "audio", start)); + for (const clip of clips) clip.setAttribute("src", "/voice.wav"); + const srcWrites = new MutationObserver(() => {}); + for (const clip of clips) srcWrites.observe(clip, { attributeFilter: ["src"] }); + await evaluateRuntime(); + const step = (label: string) => + log.push( + `${starts.length} clips, ${label}: preload ${clips.map((c) => c.preload)}, loads ${loadsOf(...clips)}, src writes ${srcWrites.takeRecords().map((r) => clips.indexOf(r.target as HTMLMediaElement))}`, + ); + step("parsed"); + for (const time of [3.5, 9.2, 5.1, 0.5]) { + window.__player?.seek(time); + step(`seek ${time}`); + } + } + return log; + }; + const named = await decisions("named player frame"); + expect(named.some((line) => /loads (\d,)*2/.test(line))).toBe(true); + expect(await decisions("plain frame")).toEqual(named); + }); + it("in Studio's preview a clip 5 s ahead stays unloaded, as the look-ahead is 2 s", async () => { servePreview(); const [now, soon] = videos(mountRoot(), "0", "5"); @@ -1115,30 +1162,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")); diff --git a/packages/core/src/runtime/init.previewSeek.test.ts b/packages/core/src/runtime/init.previewSeek.test.ts index f8071342e1..f725df1a5e 100644 --- a/packages/core/src/runtime/init.previewSeek.test.ts +++ b/packages/core/src/runtime/init.previewSeek.test.ts @@ -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); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index e856259836..a231baf61d 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -5183,7 +5183,7 @@ export function initSandboxRuntimeModular(): void { nativePlayTime = undefined; wallClearTimeout(nativeWarmTimer); if ( - !mediaDeferred || + !(studioPreview || (playerFrame && isWebKit())) || state.nativeMediaSyncDisabled || isExportRenderDrivingFrames() || window.__HF_EXPORT_RENDER_SEEK_CONFIG || diff --git a/packages/core/src/runtime/preloadMedia.test.ts b/packages/core/src/runtime/preloadMedia.test.ts index 7511f8894a..47febbab69 100644 --- a/packages/core/src/runtime/preloadMedia.test.ts +++ b/packages/core/src/runtime/preloadMedia.test.ts @@ -1,6 +1,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { mediaElementTime } from "./mediaClock"; import { unproxiedSrc } from "./proxySrc"; +import { STUDIO_PREVIEW_MARK_META } from "../studioPreviewMark"; import { deferMediaUntilDue, giveBackWhenDue, @@ -108,13 +109,35 @@ 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"); + video.load = vi.fn(); + const writes = srcWrites(video); + + releaseMedia(video); + expect(writes()).toBeGreaterThan(0); + expect(video.load).not.toHaveBeenCalled(); + expect(video.getAttribute("src")).toBe("a.mp4"); + }); + + it("in Studio's preview reloads a src clip with its src dropped, then puts the src back, as before", () => { + const mark = document.head.appendChild( + Object.assign(document.createElement("meta"), { name: STUDIO_PREVIEW_MARK_META }), + ); const video = document.createElement("video"); video.setAttribute("src", "a.mp4"); const atLoad: (string | null)[] = []; video.load = () => atLoad.push(video.getAttribute("src")); releaseMedia(video); + mark.remove(); expect(atLoad).toEqual([null]); expect(video.getAttribute("src")).toBe("a.mp4"); }); @@ -127,9 +150,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 +166,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 +184,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 children, their order and its markup untouched", () => { diff --git a/packages/core/src/runtime/preloadMedia.ts b/packages/core/src/runtime/preloadMedia.ts index f9d2193dfc..2890695bd4 100644 --- a/packages/core/src/runtime/preloadMedia.ts +++ b/packages/core/src/runtime/preloadMedia.ts @@ -10,7 +10,7 @@ import { import { createRuntimeStartTimeResolver } from "./startResolver"; import { readMediaRenderId } from "./renderFrameSibling"; import { defersMedia } from "./timedClipHide"; -import { capturePageIn, inPlayerFrame } from "../studioPreviewMark"; +import { capturePageIn, inPlayerFrame, servedToStudio } from "../studioPreviewMark"; import { swallow } from "./diagnostics"; import { isWebKit, mediaElementTime } from "./mediaClock"; @@ -99,8 +99,14 @@ 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 || servedToStudio(document)) { + stopMediaDownload(media); + if (src !== null) media.setAttribute("src", src); + return; + } + cancelPlaybackStart(media); + media.removeAttribute("src"); + media.setAttribute("src", src); } export function lengthIsAuthored(media: Element): boolean { diff --git a/packages/core/src/studioPreviewMark.ts b/packages/core/src/studioPreviewMark.ts index 901615298d..c826576f5e 100644 --- a/packages/core/src/studioPreviewMark.ts +++ b/packages/core/src/studioPreviewMark.ts @@ -30,15 +30,21 @@ 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 servedToStudio(doc: Document): boolean { + return doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null; +} export function inPlayerFrame(doc: Document): boolean { - return isWebKit() && doc.defaultView?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true; + const frame = doc.defaultView; + const marked = frame?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true; + if (servedToStudio(doc)) return isWebKit() && marked; + return marked || frame?.name === PLAYER_FRAME_NAME; } export function defersMediaIn(doc: Document): boolean { - return ( - doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null || inPlayerFrame(doc) - ); + return servedToStudio(doc) || inPlayerFrame(doc); } export function capturePageIn(doc: Document): boolean { return ( diff --git a/packages/player/src/hyperframes-player.test.ts b/packages/player/src/hyperframes-player.test.ts index 109b252579..99bdd3cf58 100644 --- a/packages/player/src/hyperframes-player.test.ts +++ b/packages/player/src/hyperframes-player.test.ts @@ -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 @@ -3892,6 +3893,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); diff --git a/packages/player/src/iframe-dom.ts b/packages/player/src/iframe-dom.ts index 5709c53660..6a14f5ecb1 100644 --- a/packages/player/src/iframe-dom.ts +++ b/packages/player/src/iframe-dom.ts @@ -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 };