Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/packages/player.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ console.log(player.duration, player.paused, player.ready);
console.log(player.compositionWidth, player.compositionHeight);
```

In Studio previews, scrubbing keeps the last complete picture visible while video
layers decode at the requested time. Rapid seeks keep only the latest queued
destination, and the captions and video layers appear together once it is ready.
Complete intermediate pictures can appear during a drag. Jumping to a different
clip supersedes an earlier pending seek; failed sources do not hold up the preview.

## Events

```js
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/runtime/colorGrading.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// fallow-ignore-file code-duplication
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mediaElementTime } from "./mediaClock";
import { PREVIEW_SEEK_HOLD_ATTR } from "../studioPreviewMark";
import {
HF_COLOR_GRADING_ATTR,
HF_COLOR_GRADING_EFFECT_KEYS,
Expand Down Expand Up @@ -352,6 +353,26 @@ describe("createColorGradingRuntime", () => {
expect(texImage2DCalls.length).toBeGreaterThan(drawsBefore);
});

it("keeps graded layers on the committed picture until a preview seek lands", () => {
const { video } = startRuntimeWithVideo();
const drawsBefore = texImage2DCalls.length;
const canvas = document.querySelector("[data-hf-color-grading-canvas]");
document.documentElement.setAttribute(PREVIEW_SEEK_HOLD_ATTR, "");
try {
video.dispatchEvent(new Event("seeked"));
video.style.setProperty("--hf-color-grading-exposure", "1");
runtime?.refresh();
expect(runtime?.redraw()).toBe(0);
expect(texImage2DCalls).toHaveLength(drawsBefore);
expect(document.querySelector("[data-hf-color-grading-canvas]")).toBe(canvas);
expect(canvas?.isConnected).toBe(true);
} finally {
document.documentElement.removeAttribute(PREVIEW_SEEK_HOLD_ATTR);
}
expect(runtime?.redraw()).toBe(1);
expect(texImage2DCalls.length).toBeGreaterThan(drawsBefore);
});

it("does not redraw-loop on its own hide writes (opacity/visibility only)", async () => {
const { video } = startRuntimeWithVideo();
await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/runtime/colorGrading.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { wallClearTimeout, wallSetTimeout } from "./wallClock";
import { PREVIEW_SEEK_HOLD_ATTR } from "../studioPreviewMark";
import {
HF_COLOR_GRADING_ADJUST_KEYS,
HF_COLOR_GRADING_ANIMATABLE_PROPERTIES,
Expand Down Expand Up @@ -3419,6 +3420,7 @@ type DrawableFrame = {
};

function drawableFrame(entry: ColorGradingEntry): DrawableFrame | null {
if (document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)) return null;
const source = getDrawableSource(entry.element);
const pixels = source && readablePixels(source, entry);
if (!source || !pixels) {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/runtime/entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ describe("runtime entry", () => {
await window.__hfWaitForSeekCompletion?.();
});

it("resolves a mixed image/video seek only after both have decoded", async () => {
servePreview();
const root = mountRoot();
const current = timed(root, "div", "0");
const later = timed(root, "div", "5");
const image = later.appendChild(document.createElement("img"));
let decodeImage = () => {};
image.decode = () => new Promise<void>((resolve) => (decodeImage = resolve));
const video = timed(later, "video", "5");
video.src = "/movie.mp4";
let ready = 1;
Object.defineProperty(video, "readyState", { configurable: true, get: () => ready });
Object.defineProperty(video, "networkState", { configurable: true, value: 2 });
await evaluateRuntime();
let settled = false;
const landed = Promise.resolve(window.__player!.seek(5.5)).then(() => {
settled = true;
});
decodeImage();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(settled).toBe(false);
expect(visibility(current, later)).toEqual(["visible", "hidden"]);
ready = 4;
video.dispatchEvent(new Event("seeked"));
await landed;
expect(visibility(current, later)).toEqual(["hidden", "visible"]);
});

it("applies a held jump after the cap when an image never decodes", async () => {
servePreview();
const root = mountRoot();
Expand Down Expand Up @@ -495,6 +523,9 @@ describe("runtime entry", () => {
]);

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],
Expand Down
283 changes: 283 additions & 0 deletions packages/core/src/runtime/init.previewSeek.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
import { afterEach, expect, it, vi } from "vitest";
import { initSandboxRuntimeModular } from "./init";
import { hideTimedClipsUntilFirstPass } from "./timedClipHide";
import { createMockTimeline, resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers";
import { PREVIEW_SEEK_HOLD_ATTR, STUDIO_PREVIEW_MARK_META } from "../studioPreviewMark";

function decoder(video: HTMLVideoElement, initiallyReady: boolean) {
let time = 0;
let ready = initiallyReady ? 4 : 0;
let seeking = false;
const seeks: number[] = [];
Object.defineProperties(video, {
duration: { value: 10, configurable: true },
videoWidth: { value: 64, configurable: true },
videoHeight: { value: 36, configurable: true },
readyState: { get: () => ready, configurable: true },
networkState: { value: 2, configurable: true },
paused: { value: true, configurable: true },
seeking: { get: () => seeking, configurable: true },
currentTime: {
get: () => time,
set: (next: number) => {
time = next;
seeks.push(next);
seeking = true;
ready = 1;
},
configurable: true,
},
});
video.pause = vi.fn();
video.play = vi.fn(() => Promise.resolve());
video.load = vi.fn();
return {
video,
seeks,
land: () => {
ready = 4;
seeking = false;
video.dispatchEvent(new Event("seeked"));
},
reload: () => {
time = 0;
ready = 1;
seeking = false;
video.dispatchEvent(new Event("loadedmetadata"));
},
};
}

function film(withImage = false) {
resetRuntimeFixtureDom();
const meta = document.createElement("meta");
meta.name = STUDIO_PREVIEW_MARK_META;
document.head.append(meta);
vi.spyOn(window, "requestAnimationFrame").mockReturnValue(0);
const drawImage = vi.fn();
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({
drawImage,
} as unknown as CanvasRenderingContext2D);
document.body.innerHTML =
'<div data-composition-id="main" data-root="true" data-duration="10">' +
[0, 5]
.flatMap((start) =>
["plate", "matte"].map(
(name) =>
`<video id="${name}-${start}" style="width:64px;height:36px" data-start="${start}" data-duration="5" src="/${name}.mp4" muted></video>`,
),
)
.join("") +
(withImage
? '<div data-start="8" data-duration="2"><img id="later-image" src="/later.png"></div>'
: "") +
"</div>";
const videos = [...document.querySelectorAll("video")].map((v) =>
decoder(v, v.dataset.start === "0"),
);
const main = createMockTimeline(10);
window.__timelines = { main };
if (withImage) hideTimedClipsUntilFirstPass();
initSandboxRuntimeModular();
for (const video of videos.slice(0, 2)) video.land();
return { main, old: videos.slice(0, 2), next: videos.slice(2), drawImage };
}

it("keeps the old picture and page until both destination videos have decoded", async () => {
const { main, old, next, drawImage } = film();
const landed = window.__player!.seek(5.5);
expect(window.__player!.getTime()).toBe(5.5);
expect(main.time?.()).toBe(0);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(true);
expect(drawImage).toHaveBeenCalledTimes(2);
expect(document.querySelectorAll("canvas")).toHaveLength(2);
expect(old.every(({ video }) => getComputedStyle(video).display === "none")).toBe(true);
next[0]!.land();
expect(main.time?.()).toBe(0);
next[1]!.land();
await landed;
expect(main.time?.()).toBe(5.5);
expect(document.querySelectorAll("canvas")).toHaveLength(0);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(false);
expect(old.every(({ video }) => video.style.visibility === "hidden")).toBe(true);
expect(next.every(({ video }) => video.style.visibility === "visible")).toBe(true);
});

it("coalesces same-clip requests and publishes complete intermediate pictures", async () => {
const { main, next } = film();
const landed = window.__player!.seek(5.5);
const initialSeeks = next.map((v) => v.seeks.length);
window.__player!.seek(6);
window.__player!.seek(7.5);
expect(next.map((v) => v.seeks.length)).toEqual(initialSeeks);
for (const video of next) video.land();
expect(main.time?.()).toBe(5.5);
expect(next.every((v) => Math.abs(v.video.currentTime - 2.5) < 0.001)).toBe(true);
for (const video of next) video.land();
await landed;
expect(main.time?.()).toBe(7.5);
});

it("supersedes a stuck run immediately when the destination uses different clips", async () => {
const { main, old } = film();
const landed = window.__player!.seek(6);
window.__player!.seek(1);
expect(old.every(({ video }) => Math.abs(video.currentTime - 1) < 0.001)).toBe(true);
for (const video of old) video.land();
await landed;
expect(main.time?.()).toBe(1);
expect(document.querySelectorAll("canvas, [data-hf-seek-video]")).toHaveLength(0);
});

it("settles a failed source without waiting for the decoder cap", async () => {
const { main, next } = film();
const landed = window.__player!.seek(6);
for (const { video } of next) {
Object.defineProperties(video, {
error: { value: null, configurable: true },
networkState: { value: 3, configurable: true },
});
const source = document.createElement("source");
video.append(source);
source.dispatchEvent(new Event("error"));
}
await landed;
expect(main.time?.()).toBe(6);
expect(document.querySelectorAll("canvas, [data-hf-seek-video]")).toHaveLength(0);
});

it("keeps a pending video's complete-picture hold while waiting for a later image", async () => {
const { main, old, next } = film(true);
const image = document.querySelector<HTMLImageElement>("#later-image")!;
let decodeImage = () => {};
const decode = vi.fn(() => new Promise<void>((resolve) => (decodeImage = resolve)));
image.decode = decode;
window.__player!.seek(6);
const landed = window.__player!.seek(8.5);
expect(decode).toHaveBeenCalledOnce();
expect(window.__player!.getTime()).toBe(8.5);
expect(main.time?.()).toBe(0);
expect(old.every(({ video }) => getComputedStyle(video).display === "none")).toBe(true);
expect(document.querySelectorAll("canvas")).toHaveLength(2);
Object.defineProperty(image, "complete", { value: true, configurable: true });
decodeImage();
await new Promise((resolve) => setTimeout(resolve, 0));
for (const video of next) video.land();
for (const video of next) video.land();
await landed;
expect(main.time?.()).toBe(8.5);
});

it("holds overlapping same-clip seeks too, and repositions a reloaded source at the requested time", async () => {
const { main, old } = film();
const landed = window.__player!.seek(2);
old[0]!.reload();
expect(old[0]!.video.currentTime).toBeCloseTo(2, 3);
old[0]!.land();
expect(main.time?.()).toBe(0);
old[1]!.land();
await landed;
expect(main.time?.()).toBe(2);
});

it("holds a paused keepPlaying seek and normal seeks after a non-export renderSeek", async () => {
const { main, old, next } = film();
window.__player!.renderSeek(0.2);
for (const video of old) video.land();
const landed = window.__player!.seek(6, { keepPlaying: true });
expect(main.time?.()).toBe(0.2);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(true);
for (const video of next) video.land();
await landed;
expect(main.time?.()).toBe(6);
});

it("does not pause, seek or cover a video leased by another preview control", async () => {
const { old, next } = film();
const leased = old[0]!;
window.__hf!.leasePausedMedia!(leased.video);
Object.defineProperty(leased.video, "paused", { value: false, configurable: true });
const seeks = leased.seeks.length;
const landed = window.__player!.seek(6);
expect(leased.video.pause).not.toHaveBeenCalled();
expect(leased.seeks).toHaveLength(seeks);
expect(leased.video.hasAttribute("data-hf-seek-video")).toBe(false);
for (const video of next) video.land();
await landed;
expect(leased.video.pause).not.toHaveBeenCalled();
});

it("leaves host-controlled native media to the host", () => {
const { old } = film();
window.dispatchEvent(
new MessageEvent("message", {
source: window,
data: {
source: "hf-parent",
type: "control",
action: "set-native-media-sync-disabled",
disabled: true,
},
}),
);
Object.defineProperty(old[0]!.video, "paused", { value: false, configurable: true });
const seeks = old[0]!.seeks.length;
window.__player!.seek(2);
expect(old[0]!.seeks).toHaveLength(seeks);
expect(old[0]!.video.pause).not.toHaveBeenCalled();
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(false);
});

it("leaves a grading canvas's hidden source in its layout during the hold", async () => {
const { old, next, drawImage } = film();
old[0]!.video.setAttribute("data-hf-color-grading-source-hidden", "true");
old[0]!.video.style.setProperty("opacity", "0", "important");
const landed = window.__player!.seek(6);
expect(old[0]!.video.hasAttribute("data-hf-seek-video")).toBe(false);
expect(drawImage).toHaveBeenCalledTimes(1);
for (const video of next) video.land();
await landed;
});

it.each(["play", "render", "teardown"])(
"cleans up a pending seek on %s and ignores late completions",
async (action) => {
const { main, next } = film();
const player = window.__player!;
const landed = player.seek(6);
if (action === "play") player.play();
else if (action === "render") player.renderSeek(2);
else window.__hfRuntimeTeardown?.();
const time = main.time?.();
for (const video of next) video.land();
await landed;
expect(main.time?.()).toBe(time);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(false);
expect(document.querySelectorAll("canvas, [data-hf-seek-video]")).toHaveLength(0);
if (action === "play") expect(player.getTime()).toBeGreaterThanOrEqual(6);
if (action === "render") expect(time).toBe(2);
},
);

it("caps a stalled decoder without leaving the picture hidden", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const { main } = film();
const landed = window.__player!.seek(6);
await vi.advanceTimersByTimeAsync(5000);
await landed;
expect(main.time?.()).toBe(6);
expect(document.documentElement.hasAttribute(PREVIEW_SEEK_HOLD_ATTR)).toBe(false);
expect(document.querySelectorAll("canvas, [data-hf-seek-video]")).toHaveLength(0);
});

afterEach(() => {
window.__hfRuntimeTeardown?.();
document.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`)?.remove();
for (const style of document.querySelectorAll(
"style[data-hf-skip-hidden-images], style[data-hf-hidden-clip-contents]",
))
style.remove();
vi.restoreAllMocks();
vi.useRealTimers();
resetRuntimeFixtureDom();
});
Loading
Loading