Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix video viewer presentation state

Anand Baburajan9c54e3c0282f
diff --git a/web/apps/space/src/components/InlinePostVideo.tsx b/web/apps/space/src/components/InlinePostVideo.tsx
index acda39a533..b6074ff6af 100644
--- a/web/apps/space/src/components/InlinePostVideo.tsx
+++ b/web/apps/space/src/components/InlinePostVideo.tsx
@@ -72,9 +72,7 @@ export const SpaceInlinePostVideo: React.FC<{
}, [shouldPreload, onLoadVideo, video]);
useEffect(() => {
- const element = player.current!.video;
- if (imageUrl) element.poster = imageUrl;
- else element.removeAttribute("poster");
+ player.current!.setPoster(imageUrl);
}, [imageUrl, onLoadVideo, video]);
useEffect(() => {
diff --git a/web/apps/space/src/components/PostVideoContent.ts b/web/apps/space/src/components/PostVideoContent.ts
index 1061510766..6e714ddf6d 100644
--- a/web/apps/space/src/components/PostVideoContent.ts
+++ b/web/apps/space/src/components/PostVideoContent.ts
@@ -44,6 +44,7 @@ export const createSpaceVideoContent = (
height: "100%",
objectFit: inline ? "cover" : "contain",
cursor: "pointer",
+ opacity: "0",
});
const button = document.createElement("button");
button.type = "button";
@@ -135,21 +136,29 @@ export const createSpaceVideoContent = (
let active = false;
let autoplayBlocked = false;
let failure: "error" | "rate-limit" | undefined;
- let posterReady = false;
+ let posterState: "pending" | "ready" | "unavailable" = poster
+ ? "pending"
+ : "unavailable";
+ let hasPresentedFrame = false;
let ready = false;
let retryTimer: ReturnType<typeof setTimeout> | undefined;
let loadingTimer: ReturnType<typeof setTimeout> | undefined;
let playbackTimeout: ReturnType<typeof setTimeout> | undefined;
const updatePreview = () => {
if (disposed) return;
- const frameReady = video.readyState >= video.HAVE_CURRENT_DATA;
- if (poster) poster.hidden = !posterReady || frameReady;
+ const frameVisible =
+ hasPresentedFrame ||
+ (!video.poster && video.readyState >= video.HAVE_CURRENT_DATA);
+ video.style.opacity = frameVisible ? "1" : "0";
+ video.dataset.spaceVideoFrameVisible = String(frameVisible);
+ if (poster) poster.hidden = posterState != "ready" || frameVisible;
if (
!ready &&
- (posterReady ||
- frameReady ||
- failure ||
- (active && autoplayBlocked))
+ (posterState == "ready" ||
+ frameVisible ||
+ !errorMessage.hidden ||
+ (posterState == "unavailable" &&
+ button.getAttribute("aria-hidden") == "false"))
) {
ready = true;
onReady?.();
@@ -157,10 +166,14 @@ export const createSpaceVideoContent = (
};
void poster?.decode().then(
() => {
- posterReady = true;
+ posterState = "ready";
+ updatePreview();
+ },
+ () => {
+ posterState = "unavailable";
+ video.removeAttribute("poster");
updatePreview();
},
- () => undefined,
);
const hideLoading = () => {
clearTimeout(loadingTimer);
@@ -204,6 +217,8 @@ export const createSpaceVideoContent = (
updatePreview();
};
const showPlaying = () => {
+ if (video.readyState >= video.HAVE_CURRENT_DATA)
+ hasPresentedFrame = true;
hideLoading();
failure = undefined;
errorMessage.hidden = true;
@@ -222,6 +237,7 @@ export const createSpaceVideoContent = (
sourceGeneration++;
pendingLoad = undefined;
pause();
+ hasPresentedFrame = false;
video.removeAttribute("src");
video.load();
updatePreview();
@@ -362,6 +378,11 @@ export const createSpaceVideoContent = (
};
video.onplaying = showPlaying;
video.onloadeddata = updatePreview;
+ video.onseeked = () => {
+ if (video.readyState >= video.HAVE_CURRENT_DATA)
+ hasPresentedFrame = true;
+ updatePreview();
+ };
video.onerror = () => {
if (active) failPlayback("media");
else reset();
@@ -382,6 +403,11 @@ export const createSpaceVideoContent = (
return {
element,
video,
+ setPoster: (url: string | undefined) => {
+ if (url) video.poster = url;
+ else video.removeAttribute("poster");
+ updatePreview();
+ },
preload,
play,
pause,
diff --git a/web/apps/space/src/components/viewer-transition.ts b/web/apps/space/src/components/viewer-transition.ts
index a04fa44a80..87ba587943 100644
--- a/web/apps/space/src/components/viewer-transition.ts
+++ b/web/apps/space/src/components/viewer-transition.ts
@@ -35,7 +35,12 @@ interface ViewerFlightTarget {
export const viewerVideoFrame = (
video: HTMLVideoElement | null | undefined,
) => {
- if (!video || video.readyState < video.HAVE_CURRENT_DATA) return undefined;
+ if (
+ !video ||
+ video.readyState < video.HAVE_CURRENT_DATA ||
+ video.dataset.spaceVideoFrameVisible != "true"
+ )
+ return undefined;
const frame = document.createElement("canvas");
frame.width = video.videoWidth;
frame.height = video.videoHeight;
diff --git a/web/apps/space/tests/post-video-content.test.ts b/web/apps/space/tests/post-video-content.test.ts
index 62e47204a6..17eef835c4 100644
--- a/web/apps/space/tests/post-video-content.test.ts
+++ b/web/apps/space/tests/post-video-content.test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { createSpaceVideoContent } from "../src/components/PostVideoContent";
+import { viewerVideoFrame } from "../src/components/viewer-transition";
import { SpaceMediaRateLimitError } from "../src/services/media-load";
import type {
SpacePostAsset,
@@ -9,7 +10,8 @@ import type {
vi.mock("ente-base/log-web", () => ({ logToDisk: vi.fn() }));
class Element extends EventTarget {
- style = {};
+ style: Record<string, string> = {};
+ dataset: Record<string, string> = {};
attributes = new Map<string, string>();
children: Element[] = [];
disabled = false;
@@ -39,6 +41,14 @@ class Video extends Element {
onpause?: () => void;
onplaying?: () => void;
onloadeddata?: () => void;
+ onseeked?: () => void;
+ onended?: () => void;
+ get poster() {
+ return this.getAttribute("poster") ?? "";
+ }
+ set poster(value: string) {
+ this.setAttribute("poster", value);
+ }
set src(value: string) {
this.setAttribute("src", value);
}
@@ -62,6 +72,10 @@ class Poster extends Element {
decode = () => decodePoster();
}
+class Canvas extends Element {
+ getContext = () => ({ drawImage: vi.fn() });
+}
+
let contents: ReturnType<typeof createSpaceVideoContent>[];
let load: ReturnType<typeof vi.fn<SpacePostAssetURLLoader>>;
let revokeURL: ReturnType<typeof vi.spyOn>;
@@ -84,7 +98,9 @@ beforeEach(() => {
? new Video()
: tag == "img"
? new Poster()
- : new Element(),
+ : tag == "canvas"
+ ? new Canvas()
+ : new Element(),
}),
);
});
@@ -167,13 +183,20 @@ test("viewer content waits for its cover to decode before handing over the flyer
expect(content.video.play).not.toHaveBeenCalled();
});
-test("a decoded video frame can hand over before its cover finishes loading", async () => {
+test("playback can hand over before its cover finishes loading", async () => {
const decoded = Promise.withResolvers<undefined>();
decodePoster.mockReturnValue(decoded.promise);
const onReady = vi.fn();
const content = create(false, "blob:poster", { onReady });
content.video.readyState = content.video.HAVE_CURRENT_DATA;
content.video.onloadeddata?.();
+ expect(onReady).not.toHaveBeenCalled();
+ expect(content.video.style.opacity).toBe("0");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeUndefined();
+ content.play();
+ await vi.advanceTimersByTimeAsync(0);
expect(onReady).toHaveBeenCalledOnce();
expect(content.poster?.hidden).toBe(true);
@@ -183,6 +206,49 @@ test("a decoded video frame can hand over before its cover finishes loading", as
expect(content.poster?.hidden).toBe(true);
});
+test.each(["ready", "failed"])(
+ "blocked autoplay waits for a pending cover to become %s",
+ async (result) => {
+ const decoded = Promise.withResolvers<undefined>();
+ decodePoster.mockReturnValue(decoded.promise);
+ const onReady = vi.fn();
+ const content = create(false, "blob:poster", { onReady });
+ content.video.play.mockRejectedValue(
+ new DOMException("Blocked", "NotAllowedError"),
+ );
+ content.play();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(content.button.getAttribute("aria-hidden")).toBe("false");
+ expect(onReady).not.toHaveBeenCalled();
+
+ if (result == "ready") decoded.resolve(undefined);
+ else decoded.reject(new Error("decode failed"));
+ await vi.advanceTimersByTimeAsync(0);
+ expect(onReady).toHaveBeenCalledOnce();
+ expect(content.poster?.hidden).toBe(result != "ready");
+ expect(content.video.style.opacity).toBe("0");
+ },
+);
+
+test.each(["", "blob:failed-poster"])(
+ "blocked autoplay without a usable cover (%s) hands over to Play",
+ async (url) => {
+ decodePoster.mockRejectedValue(new Error("decode failed"));
+ const onReady = vi.fn();
+ const content = create(false, url, { onReady });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(onReady).not.toHaveBeenCalled();
+ content.video.play.mockRejectedValue(
+ new DOMException("Blocked", "NotAllowedError"),
+ );
+ content.play();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(onReady).toHaveBeenCalledOnce();
+ expect(content.button.getAttribute("aria-hidden")).toBe("false");
+ expect(content.error.hidden).toBe(true);
+ },
+);
+
test("the viewer cover yields to playback and returns when the video resets", async () => {
const onReady = vi.fn();
const content = create(false, "blob:poster", { onReady });
@@ -192,11 +258,71 @@ test("the viewer cover yields to playback and returns when the video resets", as
await vi.advanceTimersByTimeAsync(0);
expect(content.poster?.hidden).toBe(true);
+ content.pause();
+ expect(content.poster?.hidden).toBe(true);
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeInstanceOf(Canvas);
+
content.deactivate();
expect(content.poster?.hidden).toBe(false);
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeUndefined();
expect(onReady).toHaveBeenCalledOnce();
});
+test("feed snapshots follow the displayed cover, playing frame, and paused frame", async () => {
+ const content = create();
+ await content.preload();
+ content.video.readyState = content.video.HAVE_CURRENT_DATA;
+ content.video.onloadeddata?.();
+ expect(content.video.style.opacity).toBe("0");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeUndefined();
+
+ content.play();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(content.video.style.opacity).toBe("1");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeInstanceOf(Canvas);
+ content.deactivate();
+ expect(content.video.style.opacity).toBe("1");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeInstanceOf(Canvas);
+
+ content.video.onended?.();
+ content.video.readyState = content.video.HAVE_CURRENT_DATA;
+ content.video.onloadeddata?.();
+ expect(content.video.style.opacity).toBe("0");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeUndefined();
+});
+
+test("a late feed cover replaces only a frame that has not played or been sought", () => {
+ const content = create(true, "");
+ content.video.readyState = content.video.HAVE_CURRENT_DATA;
+ content.video.onloadeddata?.();
+ expect(content.video.style.opacity).toBe("1");
+ content.setPoster("blob:late-poster");
+ expect(content.video.style.opacity).toBe("0");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeUndefined();
+
+ content.video.onseeked?.();
+ expect(content.video.style.opacity).toBe("1");
+ expect(
+ viewerVideoFrame(content.video as unknown as HTMLVideoElement),
+ ).toBeInstanceOf(Canvas);
+ content.setPoster("blob:new-poster");
+ expect(content.video.style.opacity).toBe("1");
+});
+
test("destroyed viewer content cannot hand over after late cover or video readiness", async () => {
const decoded = Promise.withResolvers<undefined>();
decodePoster.mockReturnValue(decoded.promise);
diff --git a/web/apps/space/tests/viewer-transition.test.ts b/web/apps/space/tests/viewer-transition.test.ts
index 61e20df8cf..e9619b3b2c 100644
--- a/web/apps/space/tests/viewer-transition.test.ts
+++ b/web/apps/space/tests/viewer-transition.test.ts
@@ -142,11 +142,12 @@ test("a failed flyer leaves the tile visible and allows viewer handover", async
test("a playing video opens with its current frame without decoding the poster", async () => {
const video = {
+ dataset: { spaceVideoFrameVisible: "true" },
readyState: 2,
HAVE_CURRENT_DATA: 2,
videoWidth: 1920,
videoHeight: 1080,
- } as HTMLVideoElement;
+ } as unknown as HTMLVideoElement;
const { flight, flyer, frame, drawFrame, origin, requestFrame } =
pendingFlight(video);
@@ -161,20 +162,25 @@ test("a playing video opens with its current frame without decoding the poster",
flight.stop();
});
-test("a video without a decoded frame opens using its poster", async () => {
- const { flight, drawFrame, image, origin, resolveDecode } = pendingFlight({
- readyState: 1,
- HAVE_CURRENT_DATA: 2,
- } as HTMLVideoElement);
+test.each([1, 2])(
+ "a video displaying its poster opens using that poster at readyState %i",
+ async (readyState) => {
+ const { flight, drawFrame, image, origin, resolveDecode } =
+ pendingFlight({
+ readyState,
+ HAVE_CURRENT_DATA: 2,
+ dataset: { spaceVideoFrameVisible: "false" },
+ } as unknown as HTMLVideoElement);
- expect(drawFrame).not.toHaveBeenCalled();
- expect(image.src).toBe("data:thumbnail");
- expect(origin.style.opacity).toBe("");
- resolveDecode();
- await Promise.resolve();
- expect(origin.style.opacity).toBe("0");
- flight.stop();
-});
+ expect(drawFrame).not.toHaveBeenCalled();
+ expect(image.src).toBe("data:thumbnail");
+ expect(origin.style.opacity).toBe("");
+ resolveDecode();
+ await Promise.resolve();
+ expect(origin.style.opacity).toBe("0");
+ flight.stop();
+ },
+);
const closingFlight = (src: string | HTMLCanvasElement = "full-photo") => {
const { flight, origin, requestFrame } = pendingFlight();
@@ -280,11 +286,12 @@ test("closing a video keeps its captured frame until it fades into the tile", ()
const { flight, frame } = pendingFlight();
flight.stop();
const snapshot = viewerVideoFrame({
+ dataset: { spaceVideoFrameVisible: "true" },
readyState: 2,
HAVE_CURRENT_DATA: 2,
videoWidth: 1920,
videoHeight: 1080,
- } as HTMLVideoElement)!;
+ } as unknown as HTMLVideoElement)!;
expect(snapshot).toBe(frame);
const { root, origin, fade, finishSpring } = closingFlight(snapshot);