Skip to content

Open source CapCut alternative.

TypeScript

fix(site): cloud media reads at the width it can afford

David Le1c5555486ebf
diff --git a/site/src/cut/lib/mediaRead.test.ts b/site/src/cut/lib/mediaRead.test.ts
new file mode 100644
index 00000000..e177509e
--- /dev/null
+++ b/site/src/cut/lib/mediaRead.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, test } from "bun:test";
+import { CHUNK_SIZE } from "./chunkCache";
+import { urlParallelism } from "./mediaRead";
+
+/** The cache one reader is given by the memory budget on an ordinary machine. */
+const READER_CACHE = 8 * 2 ** 20;
+
+describe("read width", () => {
+ test("cloud media reads several chunks at once", () => {
+ // The width is the point of the branch: a source over a real network
+ // overlaps its reads instead of waiting a round trip per chunk. Sized off
+ // what a worker holds — its chunk — rather than the whole cache, which is
+ // what left every source on the floor of two.
+ expect(urlParallelism("https://media.donkeycut.com/cut/u1/a.mp4", READER_CACHE)).toBe(
+ READER_CACHE / CHUNK_SIZE
+ );
+ expect(urlParallelism("https://media.donkeycut.com/cut/u1/a.mp4", READER_CACHE)).toBeGreaterThan(2);
+ });
+
+ test("a cache with room for one chunk still overlaps a pair", () => {
+ expect(urlParallelism("https://media.donkeycut.com/cut/u1/a.mp4", CHUNK_SIZE)).toBe(2);
+ expect(urlParallelism("https://media.donkeycut.com/cut/u1/a.mp4", 1024)).toBe(2);
+ });
+
+ test("the width never passes what the library will run", () => {
+ expect(urlParallelism("https://media.donkeycut.com/cut/u1/a.mp4", 999 * 2 ** 20)).toBe(8);
+ });
+
+ test("this Mac's engine and any plain-http origin keep the pair", () => {
+ expect(urlParallelism("http://127.0.0.1:41417/file?p=a.mp4", READER_CACHE)).toBe(2);
+ });
+});
diff --git a/site/src/cut/lib/mediaRead.ts b/site/src/cut/lib/mediaRead.ts
index 27ea2c53..9b163578 100644
--- a/site/src/cut/lib/mediaRead.ts
+++ b/site/src/cut/lib/mediaRead.ts
@@ -43,7 +43,7 @@ import {
} from "mediabunny";
import { confirmEngine, engineConnected, isEngineUrl } from "./api";
import { resolveRegisteredBlob } from "./backend/browser/registry";
-import { chunkSourceOptions } from "./chunkCache";
+import { CHUNK_SIZE, chunkSourceOptions } from "./chunkCache";
import { allowance, holdMemory } from "./memoryBudget";
/** What a file turns out to be, read from its container. */
@@ -87,13 +87,14 @@ export class UnreadableMediaError extends Error {
* looking at. So the local engine and any other plain-HTTP origin keep the
* library's pair, and so does a link that says it is slow or metered. The
* width is for multiplexed cloud media over a real network. */
-function urlParallelism(url: string, cache: number): number {
- // Room decides first. A ranged reader grows each worker's region ahead of
- // itself up to PREFETCH_REGION, so a source running more workers than its
- // cache has room for spends the width refetching what the other workers
- // just evicted. The pair is the floor: below that a walk stops overlapping
- // its own reads at all.
- const afforded = Math.max(2, Math.floor(cache / PREFETCH_REGION));
+export function urlParallelism(url: string, cache: number): number {
+ // Room decides first. Each worker holds the chunk it is reading in the
+ // source's one cache, so the width a source can afford is how many of those
+ // fit: past that the workers evict each other's reads and the width buys
+ // refetches. The pair is the floor — below that a walk stops overlapping its
+ // own reads at all — and the cache is sized by the memory budget, so a
+ // machine with less room reads narrower rather than thrashing.
+ const afforded = Math.max(2, Math.floor(cache / CHUNK_SIZE));
if (!/^https:/i.test(url)) return 2;
const conn =
typeof navigator === "undefined"
@@ -103,10 +104,6 @@ function urlParallelism(url: string, cache: number): number {
return Math.min(8, afforded);
}
-/** How far ahead of itself one ranged worker reads before it stops growing
- * its region (mediabunny's network prefetch profile). */
-const PREFETCH_REGION = 8 * 2 ** 20;
-
/** Backoff, in seconds, for a URL read whose request failed outright. The
* list ends, so a URL that is genuinely gone still fails — the preview's
* outage recovery (frameSource.ts) re-opens on its own cadence, and a read