Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reserve video download capacity for active playback

Anand Baburajan72286616fb99
diff --git a/web/apps/space/src/components/InlinePostVideo.tsx b/web/apps/space/src/components/InlinePostVideo.tsx
index 6b944b4d3e..73d8267132 100644
--- a/web/apps/space/src/components/InlinePostVideo.tsx
+++ b/web/apps/space/src/components/InlinePostVideo.tsx
@@ -35,6 +35,8 @@ export const SpaceInlinePostVideo: React.FC<{
: undefined,
{
inline: true,
+ onPlay: () => queuedLoad?.prioritize(),
+ onPause: () => queuedLoad?.deprioritize(),
onTogglePlayback: (play) => {
if (play) registration.play();
else registration.pause();
diff --git a/web/apps/space/src/components/PostVideoContent.ts b/web/apps/space/src/components/PostVideoContent.ts
index a28f19effb..7f1b0737d7 100644
--- a/web/apps/space/src/components/PostVideoContent.ts
+++ b/web/apps/space/src/components/PostVideoContent.ts
@@ -7,8 +7,15 @@ export const createSpaceVideoContent = (
load?: SpacePostAssetURLLoader,
{
inline = false,
+ onPlay,
+ onPause,
onTogglePlayback,
- }: { inline?: boolean; onTogglePlayback?: (play: boolean) => void } = {},
+ }: {
+ inline?: boolean;
+ onPlay?: () => Promise<void> | void;
+ onPause?: () => void;
+ onTogglePlayback?: (play: boolean) => void;
+ } = {},
) => {
const media = item.video!;
const element = document.createElement("div");
@@ -98,8 +105,12 @@ export const createSpaceVideoContent = (
loading.setAttribute("aria-hidden", "true");
element.removeAttribute("aria-busy");
};
- const showLoading = () => {
- playbackTimeout ??= setTimeout(() => failPlayback("timeout"), 30_000);
+ const showLoading = (startTimeout = true) => {
+ if (startTimeout)
+ playbackTimeout ??= setTimeout(
+ () => failPlayback("timeout"),
+ 30_000,
+ );
if (document.activeElement == button)
video.focus({ preventScroll: true });
button.setAttribute("aria-hidden", "true");
@@ -129,6 +140,7 @@ export const createSpaceVideoContent = (
};
const pause = () => {
generation++;
+ onPause?.();
video.pause();
showPlayButton();
};
@@ -184,10 +196,15 @@ export const createSpaceVideoContent = (
active = true;
errorMessage.hidden = true;
const current = ++generation;
- showLoading();
+ showLoading(false);
button.disabled = true;
void (async () => {
- await preload();
+ await Promise.all([
+ preload(),
+ Promise.resolve(onPlay?.()).then(() => {
+ if (!disposed && generation == current) showLoading();
+ }),
+ ]);
if (disposed || generation != current) return;
if (
video.ended ||
diff --git a/web/apps/space/src/components/feed-video-preloading.ts b/web/apps/space/src/components/feed-video-preloading.ts
index 5f8719ffa6..c799ed4073 100644
--- a/web/apps/space/src/components/feed-video-preloading.ts
+++ b/web/apps/space/src/components/feed-video-preloading.ts
@@ -1,6 +1,7 @@
interface VideoLoad {
element: HTMLElement;
load: () => Promise<string>;
+ start: () => void;
resolve: (url: string) => void;
reject: (error: unknown) => void;
}
@@ -8,6 +9,7 @@ interface VideoLoad {
const queued = new Set<VideoLoad>();
let inFlight = 0;
let frame: number | undefined;
+let foreground: VideoLoad | undefined;
const distance = ({ element }: VideoLoad) => {
const rect = element.getBoundingClientRect();
@@ -19,22 +21,28 @@ const distance = ({ element }: VideoLoad) => {
};
const schedule = () => {
- if (!queued.size || inFlight >= 2) return;
+ if (!queued.size || inFlight >= 3) return;
frame ??= requestAnimationFrame(() => {
frame = undefined;
for (const request of queued) {
if (!request.element.isConnected) {
queued.delete(request);
+ if (foreground == request) foreground = undefined;
request.reject(
new DOMException("Video preload canceled", "AbortError"),
);
}
}
- const next = [...queued].sort((a, b) => distance(a) - distance(b));
+ const next = [...queued].sort(
+ (a, b) =>
+ Number(b == foreground) - Number(a == foreground) ||
+ distance(a) - distance(b),
+ );
for (const request of next) {
- if (inFlight >= 2) break;
+ if (inFlight >= (request == foreground ? 3 : 2)) continue;
queued.delete(request);
inFlight++;
+ request.start();
void (async () => {
try {
request.resolve(await request.load());
@@ -42,6 +50,7 @@ const schedule = () => {
request.reject(error);
} finally {
inFlight--;
+ if (foreground == request) foreground = undefined;
schedule();
}
})();
@@ -53,15 +62,30 @@ export const queueFeedVideoLoad = (
element: HTMLElement,
load: VideoLoad["load"],
) => {
+ let start: () => void;
+ const started = new Promise<void>((resolve) => {
+ start = resolve;
+ });
let request: VideoLoad;
const promise = new Promise<string>((resolve, reject) => {
- request = { element, load, resolve, reject };
+ request = { element, load, start, resolve, reject };
queued.add(request);
schedule();
});
return {
promise,
+ prioritize: () => {
+ if (queued.has(request)) {
+ foreground = request;
+ schedule();
+ }
+ return Promise.race([started, promise.then(() => undefined)]);
+ },
+ deprioritize: () => {
+ if (foreground == request) foreground = undefined;
+ },
cancel: () => {
+ if (foreground == request) foreground = undefined;
if (!queued.delete(request)) return;
request.reject(
new DOMException("Video preload canceled", "AbortError"),
diff --git a/web/apps/space/tests/feed-video-preloading.test.ts b/web/apps/space/tests/feed-video-preloading.test.ts
index a335f9f115..a81930b0cc 100644
--- a/web/apps/space/tests/feed-video-preloading.test.ts
+++ b/web/apps/space/tests/feed-video-preloading.test.ts
@@ -92,6 +92,64 @@ test("updates the priority of queued videos after scrolling", async () => {
expect(started).toEqual(["first", "second", "later"]);
});
+test("active playback starts while both background downloads are still busy", async () => {
+ enqueue("first", 100);
+ enqueue("second", 500);
+ const active = enqueue("active", 900);
+ enqueue("background", 1200);
+ flush();
+ const ready = active.prioritize();
+ flush();
+ await ready;
+ expect(started).toEqual(["first", "second", "active"]);
+ expect(active.load).toHaveBeenCalledTimes(1);
+ active.download.resolve("blob:active");
+ await active.promise;
+ flush();
+ expect(started).toEqual(["first", "second", "active"]);
+});
+
+test("rapid scrolling keeps downloads bounded and starts the latest active video next", async () => {
+ const first = enqueue("first", 100);
+ enqueue("second", 500);
+ const previous = enqueue("previous", 900);
+ const skipped = enqueue("skipped", 1200);
+ const active = enqueue("active", 1500);
+ flush();
+ const previousReady = previous.prioritize();
+ flush();
+ await previousReady;
+ const skippedReady = skipped.prioritize();
+ const ready = active.prioritize();
+ flush();
+ expect(started).toEqual(["first", "second", "previous"]);
+ first.download.resolve("blob:first");
+ await first.promise;
+ flush();
+ await ready;
+ expect(started).toEqual(["first", "second", "previous", "active"]);
+ skipped.cancel();
+ await expect(skippedReady).rejects.toMatchObject({ name: "AbortError" });
+});
+
+test("an inactive video no longer uses the reserved playback slot", () => {
+ enqueue("first", 100);
+ enqueue("second", 500);
+ const previous = enqueue("previous", 900);
+ flush();
+ void previous.prioritize().catch(() => undefined);
+ previous.deprioritize();
+ flush();
+ expect(started).toEqual(["first", "second"]);
+});
+
+test("promoting an in-flight preload joins its existing download", async () => {
+ const active = enqueue("active", 100);
+ flush();
+ await active.prioritize();
+ expect(active.load).toHaveBeenCalledTimes(1);
+});
+
test("prioritizes a visible slide and the next post over distant carousel slides", () => {
enqueue("fifth slide", 100, 1200);
enqueue("next post", 900);
diff --git a/web/apps/space/tests/post-video-content.test.ts b/web/apps/space/tests/post-video-content.test.ts
index 35389af613..44f274d247 100644
--- a/web/apps/space/tests/post-video-content.test.ts
+++ b/web/apps/space/tests/post-video-content.test.ts
@@ -81,6 +81,7 @@ afterEach(() => {
const create = (
inline = true,
imageUrl: string | undefined = "blob:poster",
+ options: Parameters<typeof createSpaceVideoContent>[2] = {},
) => {
const content = createSpaceVideoContent(
{
@@ -91,7 +92,7 @@ const create = (
},
},
load,
- { inline },
+ { ...options, inline },
);
contents.push(content);
const [video, button, loading, error] = (
@@ -235,6 +236,64 @@ test("a timed-out download cannot replace the source after retry", async () => {
expect(revokeURL).toHaveBeenCalledWith("blob:late");
});
+test("waiting for a download slot does not time out or prevent eventual autoplay", async () => {
+ const download = Promise.withResolvers<string>();
+ const started = Promise.withResolvers<undefined>();
+ load.mockReturnValue(download.promise);
+ const content = create(true, "", { onPlay: () => started.promise });
+ void content.preload();
+ content.play();
+ await vi.advanceTimersByTimeAsync(60_000);
+ expect(content.error.hidden).toBe(true);
+ expect(content.loading.getAttribute("aria-hidden")).toBe("false");
+ expect(content.video.play).not.toHaveBeenCalled();
+ started.resolve(undefined);
+ await vi.advanceTimersByTimeAsync(29_000);
+ expect(content.error.hidden).toBe(true);
+ download.resolve("blob:video");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(content.video.paused).toBe(false);
+ expect(content.video.play).toHaveBeenCalledTimes(1);
+ expect(load).toHaveBeenCalledTimes(1);
+});
+
+test("the playback timeout still applies once a queued download actually starts", async () => {
+ const download = Promise.withResolvers<string>();
+ const started = Promise.withResolvers<undefined>();
+ load.mockReturnValue(download.promise);
+ const content = create(true, "", { onPlay: () => started.promise });
+ content.play();
+ await vi.advanceTimersByTimeAsync(60_000);
+ started.resolve(undefined);
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(content.error.hidden).toBe(false);
+ download.resolve("blob:late");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(content.video.play).not.toHaveBeenCalled();
+ expect(revokeURL).toHaveBeenCalledWith("blob:late");
+});
+
+test("leaving a queued video clears its priority and does not start a stale timeout", async () => {
+ const download = Promise.withResolvers<string>();
+ const started = Promise.withResolvers<undefined>();
+ const onPause = vi.fn();
+ load.mockReturnValue(download.promise);
+ const content = create(true, "", {
+ onPlay: () => started.promise,
+ onPause,
+ });
+ content.play();
+ content.deactivate();
+ expect(onPause).toHaveBeenCalledTimes(1);
+ started.resolve(undefined);
+ await vi.advanceTimersByTimeAsync(60_000);
+ expect(content.error.hidden).toBe(true);
+ expect(content.loading.getAttribute("aria-hidden")).toBe("true");
+ download.resolve("blob:video");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(content.video.play).not.toHaveBeenCalled();
+});
+
test("fullscreen video still releases its source on deactivation", async () => {
const content = create(false);
content.play();