π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Preload feed videos earlier and prioritize nearby downloads
Anand Baburajan4d734382f181
diff --git a/web/apps/space/src/components/InlinePostVideo.tsx b/web/apps/space/src/components/InlinePostVideo.tsxindex e472d2fb8a..6b944b4d3e 100644--- a/web/apps/space/src/components/InlinePostVideo.tsx+++ b/web/apps/space/src/components/InlinePostVideo.tsx@@ -1,12 +1,13 @@import { Box } from "@mui/material";import { createSpaceVideoContent } from "components/PostVideoContent";import { registerFeedVideo } from "components/feed-video-playback";+import { queueFeedVideoLoad } from "components/feed-video-preloading";import log from "ente-base/log";import React, { useEffect, useRef } from "react";import type { SpacePostAssetURLLoader, SpacePostVideo } from "services/space";export const SpaceInlinePostVideo: React.FC<{- imageUrl: string;+ imageUrl?: string;video: SpacePostVideo;isActive: boolean;muted: boolean;@@ -20,9 +21,18 @@ export const SpaceInlinePostVideo: React.FC<{useEffect(() => {const container = root.current!;+ let queuedLoad: ReturnType<typeof queueFeedVideoLoad> | undefined;const content = createSpaceVideoContent(- { imageUrl, video },- onLoadVideo,+ { video },+ onLoadVideo+ ? (asset) => {+ queuedLoad?.cancel();+ queuedLoad = queueFeedVideoLoad(container, () =>+ onLoadVideo(asset),+ );+ return queuedLoad.promise;+ }+ : undefined,{inline: true,onTogglePlayback: (play) => {@@ -36,15 +46,23 @@ export const SpaceInlinePostVideo: React.FC<{player.current = content;container.append(content.element);void content.preload().catch((error: unknown) => {- log.warn("Failed to preload feed video", error);+ if (!(error instanceof DOMException && error.name == "AbortError"))+ log.warn("Failed to preload feed video", error);});return () => {registration.unregister();content.destroy();+ queuedLoad?.cancel();content.element.remove();player.current = undefined;playback.current = undefined;};+ }, [onLoadVideo, video]);++ useEffect(() => {+ const element = player.current!.video;+ if (imageUrl) element.poster = imageUrl;+ else element.removeAttribute("poster");}, [imageUrl, onLoadVideo, video]);useEffect(() => {@@ -52,11 +70,11 @@ export const SpaceInlinePostVideo: React.FC<{content.video.tabIndex = isActive ? 0 : -1;content.element.querySelector("button")!.tabIndex = isActive ? 0 : -1;playback.current!.setActive(isActive);- }, [imageUrl, isActive, onLoadVideo, video]);+ }, [isActive, onLoadVideo, video]);useEffect(() => {player.current!.video.muted = muted;- }, [imageUrl, muted, onLoadVideo, video]);+ }, [muted, onLoadVideo, video]);return (<Box ref={root} sx={{ position: "absolute", inset: 0, zIndex: 1 }} />diff --git a/web/apps/space/src/components/PostVideoContent.ts b/web/apps/space/src/components/PostVideoContent.tsindex 610985ba7e..a28f19effb 100644--- a/web/apps/space/src/components/PostVideoContent.ts+++ b/web/apps/space/src/components/PostVideoContent.ts@@ -3,7 +3,7 @@ import { logToDisk } from "ente-base/log-web";import type { SpacePostAssetURLLoader } from "services/space";export const createSpaceVideoContent = (- item: Pick<SpaceViewerPhoto, "imageUrl" | "video">,+ item: Pick<SpaceViewerPhoto, "video"> & { imageUrl?: string },load?: SpacePostAssetURLLoader,{inline = false,@@ -23,7 +23,7 @@ export const createSpaceVideoContent = (const video = document.createElement("video");video.playsInline = true;video.preload = "none";- video.poster = item.imageUrl;+ if (item.imageUrl) video.poster = item.imageUrl;video.muted = inline || (media.muted ?? false);video.loop = inline;video.tabIndex = 0;diff --git a/web/apps/space/src/components/feed-video-preloading.ts b/web/apps/space/src/components/feed-video-preloading.tsnew file mode 100644index 0000000000..68a5917475--- /dev/null+++ b/web/apps/space/src/components/feed-video-preloading.ts@@ -0,0 +1,67 @@+interface VideoLoad {+ element: HTMLElement;+ load: () => Promise<string>;+ resolve: (url: string) => void;+ reject: (error: unknown) => void;+}++const queued = new Set<VideoLoad>();+let inFlight = 0;+let frame: number | undefined;++const distance = ({ element }: VideoLoad) => {+ const rect = element.getBoundingClientRect();+ const post = element.closest("article")!.getBoundingClientRect();+ return (+ Math.max(0, rect.top - window.innerHeight, -rect.bottom) ++ Math.abs(rect.left - post.left)+ );+};++const schedule = () => {+ if (!queued.size || inFlight >= 2) return;+ frame ??= requestAnimationFrame(() => {+ frame = undefined;+ const next = [...queued].sort((a, b) => distance(a) - distance(b));+ for (const request of next) {+ if (inFlight >= 2) break;+ queued.delete(request);+ inFlight++;+ void (async () => {+ try {+ request.resolve(await request.load());+ } catch (error) {+ request.reject(error);+ } finally {+ inFlight--;+ schedule();+ }+ })();+ }+ });+};++export const queueFeedVideoLoad = (+ element: HTMLElement,+ load: VideoLoad["load"],+) => {+ let request: VideoLoad;+ const promise = new Promise<string>((resolve, reject) => {+ request = { element, load, resolve, reject };+ queued.add(request);+ schedule();+ });+ return {+ promise,+ cancel: () => {+ if (!queued.delete(request)) return;+ request.reject(+ new DOMException("Video preload canceled", "AbortError"),+ );+ if (!queued.size && frame != undefined) {+ cancelAnimationFrame(frame);+ frame = undefined;+ }+ },+ };+};diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsxindex c2aacb60ad..ef19c349dc 100644--- a/web/apps/space/src/screens/HomeScreen.tsx+++ b/web/apps/space/src/screens/HomeScreen.tsx@@ -824,18 +824,15 @@ const FeedPhoto: React.FC<{}}/>)}- {!isPostUnavailable &&- isPhotoReady &&- video &&- shouldPreloadVideo && (- <SpaceInlinePostVideo- imageUrl={displayImageUrl!}- video={video}- isActive={isActive}- muted={muted}- onLoadVideo={onLoadVideo}- />- )}+ {!isPostUnavailable && video && shouldPreloadVideo && (+ <SpaceInlinePostVideo+ imageUrl={displayImageUrl}+ video={video}+ isActive={isActive}+ muted={muted}+ onLoadVideo={onLoadVideo}+ />+ )}{isPostUnavailable && (<Boxsx={{@@ -897,6 +894,7 @@ const FeedItem: React.FC<FeedItemProps> = ({);const [shouldPreloadVideo, setShouldPreloadVideo] = useState(false);const feedPhotos = photos ?? [{ imageUrl, thumbHash }];+ const hasVideo = feedPhotos.some((photo) => Boolean(photo.video));const activePhoto = feedPhotos[photoIndex]!;const carouselRef = React.useRef<HTMLDivElement | null>(null);React.useEffect(() => registerFeedPost(carouselRef.current!), []);@@ -1132,6 +1130,7 @@ const FeedItem: React.FC<FeedItemProps> = ({React.useEffect(() => {if (isPostUnavailable) return;+ if (shouldLoadMedia) return;const element = rootRef.current;if (!element) return;if (@@ -1139,21 +1138,45 @@ const FeedItem: React.FC<FeedItemProps> = ({!("IntersectionObserver" in window)) {setShouldLoadMedia(true);- setShouldPreloadVideo(true);return;}const observer = new IntersectionObserver((entries) => {- const isNearby = entries.some((entry) => entry.isIntersecting);- setShouldPreloadVideo(isNearby);- if (isNearby) setShouldLoadMedia(true);+ if (entries.some((entry) => entry.isIntersecting)) {+ setShouldLoadMedia(true);+ observer.disconnect();+ }},{ rootMargin: feedMediaLoadRootMargin },);observer.observe(element);return () => observer.disconnect();- }, [isPostUnavailable]);+ }, [isPostUnavailable, shouldLoadMedia]);++ React.useEffect(() => {+ if (!hasVideo || isPostUnavailable) return;+ const element = rootRef.current!;+ let observer: IntersectionObserver | undefined;+ const observe = () => {+ observer?.disconnect();+ observer = new IntersectionObserver(+ (entries) => {+ setShouldPreloadVideo(+ entries.some((entry) => entry.isIntersecting),+ );+ },+ { rootMargin: `640px 0px ${window.innerHeight * 3}px 0px` },+ );+ observer.observe(element);+ };+ observe();+ window.addEventListener("resize", observe);+ return () => {+ observer?.disconnect();+ window.removeEventListener("resize", observe);+ };+ }, [hasVideo, isPostUnavailable]);React.useEffect(() => {if (isPostUnavailable) return;@@ -1419,8 +1442,8 @@ const FeedItem: React.FC<FeedItemProps> = ({: undefined}shouldLoad={- shouldLoadMedia &&- ((shouldPreloadVideo && Boolean(photo.video)) ||+ (shouldPreloadVideo && Boolean(photo.video)) ||+ (shouldLoadMedia &&Math.abs(index - photoIndex) <= 1)}shouldPreloadVideo={shouldPreloadVideo}diff --git a/web/apps/space/tests/feed-video-preloading.test.ts b/web/apps/space/tests/feed-video-preloading.test.tsnew file mode 100644index 0000000000..00776de071--- /dev/null+++ b/web/apps/space/tests/feed-video-preloading.test.ts@@ -0,0 +1,144 @@+import { afterEach, beforeEach, expect, test, vi } from "vitest";+import { queueFeedVideoLoad } from "../src/components/feed-video-preloading";++let viewport: { innerHeight: number; scrollY: number };+let frames: Map<number, FrameRequestCallback>;+let requests: (ReturnType<typeof queueFeedVideoLoad> & {+ download: PromiseWithResolvers<string>;+})[];+let started: string[];++beforeEach(() => {+ viewport = { innerHeight: 800, scrollY: 0 };+ frames = new Map();+ requests = [];+ started = [];+ let nextFrame = 0;+ vi.stubGlobal("window", viewport);+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {+ frames.set(++nextFrame, callback);+ return nextFrame;+ });+ vi.stubGlobal("cancelAnimationFrame", (id: number) => frames.delete(id));+});++afterEach(async () => {+ for (const request of requests) {+ request.cancel();+ request.download.resolve("blob:cleanup");+ }+ await Promise.allSettled(requests.map((request) => request.promise));+ vi.unstubAllGlobals();+});++const flush = () => {+ const callbacks = [...frames.values()];+ frames.clear();+ callbacks.forEach((callback) => callback(0));+};++const enqueue = (name: string, top: number, slideOffset = 0) => {+ const position = { top, slideOffset };+ const element = {+ getBoundingClientRect: () => ({+ top: position.top - viewport.scrollY,+ bottom: position.top + 300 - viewport.scrollY,+ left: 100 + position.slideOffset,+ }),+ closest: () => ({ getBoundingClientRect: () => ({ left: 100 }) }),+ } as unknown as HTMLElement;+ const download = Promise.withResolvers<string>();+ const load = vi.fn(() => {+ started.push(name);+ return download.promise;+ });+ const control = queueFeedVideoLoad(element, load);+ void control.promise.catch(() => undefined);+ const request = { ...control, download, load, position };+ requests.push(request);+ return request;+};++test("downloads the nearest videos first and limits simultaneous loads to two", async () => {+ const far = enqueue("far", 2400);+ const next = enqueue("next", 1000);+ const visible = enqueue("visible", 100);+ flush();+ expect(started).toEqual(["visible", "next"]);+ expect(far.load).not.toHaveBeenCalled();+ visible.download.resolve("blob:visible");+ await visible.promise;+ flush();+ expect(started).toEqual(["visible", "next", "far"]);+ expect(next.load).toHaveBeenCalledTimes(1);+});++test("updates the priority of queued videos after scrolling", async () => {+ const first = enqueue("first", 100);+ enqueue("second", 500);+ enqueue("earlier", 1000);+ enqueue("later", 2500);+ flush();+ viewport.scrollY = 2300;+ first.download.resolve("blob:first");+ await first.promise;+ flush();+ expect(started).toEqual(["first", "second", "later"]);+});++test("prioritizes a visible slide and the next post over distant carousel slides", () => {+ enqueue("fifth slide", 100, 1200);+ enqueue("next post", 900);+ enqueue("visible slide", 100);+ flush();+ expect(started).toEqual(["visible slide", "next post"]);+});++test("swiping a queued carousel video into view moves it ahead in the queue", async () => {+ const first = enqueue("first", 100);+ enqueue("second", 400);+ const carousel = enqueue("carousel", 500, 1500);+ enqueue("lower post", 1500);+ flush();+ carousel.position.slideOffset = 0;+ first.download.resolve("blob:first");+ await first.promise;+ flush();+ expect(started).toEqual(["first", "second", "carousel"]);+});++test("canceling a queued preload prevents its network request", async () => {+ const canceled = enqueue("canceled", 100);+ canceled.cancel();+ await expect(canceled.promise).rejects.toMatchObject({+ name: "AbortError",+ });+ flush();+ expect(started).toEqual([]);+ expect(frames.size).toBe(0);+});++test("an in-flight download retains its slot when its post is unmounted", async () => {+ const first = enqueue("first", 100);+ enqueue("second", 500);+ enqueue("third", 900);+ flush();+ first.cancel();+ flush();+ expect(started).toEqual(["first", "second"]);+ first.download.resolve("blob:first");+ await expect(first.promise).resolves.toBe("blob:first");+ flush();+ expect(started).toEqual(["first", "second", "third"]);+});++test("a failed download releases its slot for the next video", async () => {+ const first = enqueue("first", 100);+ enqueue("second", 500);+ enqueue("third", 900);+ flush();+ first.download.reject(new Error("offline"));+ await expect(first.promise).rejects.toThrow("offline");+ flush();+ expect(started).toEqual(["first", "second", "third"]);+});diff --git a/web/apps/space/tests/post-video-content.test.ts b/web/apps/space/tests/post-video-content.test.tsindex 0def6d0566..35389af613 100644--- a/web/apps/space/tests/post-video-content.test.ts+++ b/web/apps/space/tests/post-video-content.test.ts@@ -78,10 +78,13 @@ afterEach(() => {vi.useRealTimers();});-const create = (inline = true) => {+const create = (+ inline = true,+ imageUrl: string | undefined = "blob:poster",+) => {const content = createSpaceVideoContent({- imageUrl: "blob:poster",+ imageUrl,video: {asset: { objectKey: "video" } as SpacePostAsset,durationMs: 1000,@@ -115,6 +118,15 @@ test("background preload keeps the poster controls hidden and does not play", asexpect(content.element.getAttribute("aria-busy")).toBeNull();});+test("a video can download and play before its cover image is available", async () => {+ const content = create(true, "");+ content.play();+ await vi.advanceTimersByTimeAsync(0);+ expect(load).toHaveBeenCalledTimes(1);+ expect(content.video.paused).toBe(false);+ expect(content.video.getAttribute("poster")).toBeNull();+});+test("autoplay joins an in-flight preload without downloading the video again", async () => {const download = Promise.withResolvers<string>();load.mockReturnValue(download.promise);