Skip to content
6 changes: 5 additions & 1 deletion packages/core/src/runtime/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import {
isRenderMode,
maybeProxyProactively,
standInCaptureStill,
showStillWhenDue,
decideFileTurn,
describeVideoSources,
levelsWithLighterCopies,
Expand Down Expand Up @@ -3636,7 +3637,10 @@ export function initSandboxRuntimeModular(): void {
currentTime,
loadsAhead && dueAtLoopStart(rawNode, visibleAt),
);
if (isMediaElement(rawNode)) standInCaptureStill(rawNode, isVisibleNow);
if (isMediaElement(rawNode)) {
standInCaptureStill(rawNode, isVisibleNow);
showStillWhenDue(rawNode, isVisibleNow);
}
if (!isMediaElement(rawNode) && !isImageElement(rawNode)) decidedTimedClip = true;
if (isVideoElement(rawNode) || isImageElement(rawNode)) {
colorGradingRuntime?.setSourceVisibility(rawNode, isVisibleNow);
Expand Down
79 changes: 78 additions & 1 deletion packages/core/src/runtime/mediaProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import {
swapToProxy,
type MediaCodecMapEntry,
} from "./mediaProxy";
import { createMockTimeline } from "./runtimeSeekFixture.test-helpers";
import { deriveCodecMapKey } from "./mediaSourceLookup";
import { escapeCssString } from "./editingElement";
import type { PreviewSourceFacts } from "../previewProxyBox";
import { recordOpenedAtInPoint, unproxiedSrc } from "./proxySrc";

vi.mock("./bridge", () => ({
vi.mock("./bridge", async (importOriginal) => ({
...(await importOriginal<typeof import("./bridge")>()),
postRuntimeMessage: vi.fn(),
}));

Expand Down Expand Up @@ -1917,6 +1919,81 @@ describe("preview quality: lighter copies of heavy files", () => {
]);
});

// fails without the fix: the still went up only at the copy's next poll, 2 s (5 s with the queue full) after the
// clip came on screen, so a held clip showed nothing until then.
it("puts its still up on the tick it comes on screen, not at the copy's next poll", async () => {
const { el, showStillWhenDue } = await waitingForItsCopy(making);
const media = await import("./media");
showStillWhenDue(el, true);
expect(el.hasAttribute("poster")).toBe(false);
const clip = {
el,
start: 0,
mediaStart: 4,
duration: 10,
end: 10,
volume: null,
playbackRate: 1,
};
media.syncRuntimeMedia({
clips: [{ ...clip, loop: false, sourceDuration: null }],
...{ timeSeconds: 1, playing: false, playbackRate: 1, getCompositionDuration: () => 0 },
});
showStillWhenDue(el, false);
expect(el.hasAttribute("poster")).toBe(false);
showStillWhenDue(el, true);
expect(
new URL(el.getAttribute("poster")!, document.baseURI).searchParams.get("hf-proxy-still"),
).toBe("5.0");
});

// fails without the fix: a job a new source or quality had made obsolete still put its old still up.
it("puts up no still for a copy job a new source or quality made obsolete", async () => {
for (const obsolete of [
(el: HTMLVideoElement) => el.setAttribute("src", "/other.mp4"),
(_: HTMLVideoElement, staged: { setPreviewQuality: (level: number) => void }) =>
staged.setPreviewQuality(0.5),
]) {
const staged = await waitingForItsCopy(making);
const media = await import("./media");
const clip = {
el: staged.el,
start: 0,
mediaStart: 4,
duration: 10,
end: 10,
volume: null,
};
media.syncRuntimeMedia({
clips: [{ ...clip, playbackRate: 1, loop: false, sourceDuration: null }],
...{ timeSeconds: 1, playing: false, playbackRate: 1, getCompositionDuration: () => 0 },
});
obsolete(staged.el, staged);
staged.showStillWhenDue(staged.el, true);
expect(staged.el.getAttribute("poster")).toBeNull();
cleanUp();
}
});

// fails without the fix: the runtime's pass never asked, so a paused seek onto a held clip showed nothing until
// the copy's next poll.
it("puts its still up on a paused seek into its window, through the runtime's own pass", async () => {
const { el } = await waitingForItsCopy(making);
const root = document.createElement("div");
Object.assign(root.dataset, { compositionId: "main", root: "true", duration: "10" });
Object.assign(el.dataset, { start: "0", duration: "10", mediaStart: "4" });
document.body.append(root);
root.append(el);
window.__timelines = { main: createMockTimeline(10) };
const { initSandboxRuntimeModular } = await import("./init");
initSandboxRuntimeModular();
expect(el.hasAttribute("poster")).toBe(false);
window.__player!.seek(1);
expect(
new URL(el.getAttribute("poster")!, document.baseURI).searchParams.get("hf-proxy-still"),
).toBe("5.0");
});

async function twoHeldOnScreen(facts: PreviewSourceFacts = HEAVY) {
vi.useFakeTimers();
let made = false;
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/runtime/mediaProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ const stills = new WeakSet<HTMLMediaElement>();
const stillPosters = new WeakMap<HTMLMediaElement, string>();
const stillHolds = new WeakMap<HTMLMediaElement, object>();

// The still each clip's copy job would put up, asked for on the tick the clip comes on screen, not at its next poll.
const stillsWhenDue = new WeakMap<HTMLMediaElement, () => void>();

export function showStillWhenDue(el: HTMLMediaElement, onScreen: boolean): void {
if (onScreen) stillsWhenDue.get(el)?.();
}

/** While a file the browser cannot play is being copied, its own frame at the playhead stands in as the poster. */
function showStill(
el: HTMLMediaElement,
Expand Down Expand Up @@ -630,14 +637,20 @@ async function makeCopy(el: HTMLMediaElement, job: CopyJob, scale: number | null
);
const light = lightFacts !== undefined;
noteCopy(el, "making", null, light);
const still =
light && !held
? undefined
: () => void (live() && showStill(el, originalSrc, variant, box, light));
if (still) stillsWhenDue.set(el, still);
const served = await waitForServedProxy(
proxiedSrc,
live,
(fraction) => noteCopy(el, "making", fraction, light),
() => isActiveAtPlayhead(el),
false,
light && !held ? undefined : () => showStill(el, originalSrc, variant, box, light),
still,
);
if (stillsWhenDue.get(el) === still) stillsWhenDue.delete(el);
const wanted = live();
if (!served || !wanted) sizedBeforeCopy.delete(el);
if (held) endCopyHold(el, served && wanted ? null : originalAttr);
Expand Down
Loading