Skip to content
Open
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
74 changes: 74 additions & 0 deletions packages/studio-server/src/helpers/proxyTranscoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ afterEach(() => {
vi.resetModules();
vi.doUnmock("node:child_process");
vi.doUnmock("@hyperframes/parsers/ff-binaries");
vi.doUnmock("./safePath.js");
delete process.env.HYPERFRAMES_PROXY_MAX_CONCURRENCY;
delete process.env.HYPERFRAMES_PROXY_MAX_QUEUE;
});
Expand Down Expand Up @@ -830,6 +831,79 @@ describe("resolveProxy", () => {
expect(boxedPath).not.toBe(await getProxyCachePath(projectDir, sourcePath, "h264"));
});

it("serves a smaller preview ask from the copy a larger ask is making or made, and copies again only for a larger one", async () => {
const { spawn, calls } = createSpawnSpy();
const { resolveProxy } = await loadModule(spawn, FFMPEG_PATH);
const projectDir = tmpProject();
const sourcePath = join(projectDir, "clip.mov");
writeFileSync(sourcePath, "source-bytes");

const large = resolveProxy(projectDir, sourcePath, "h264", { width: 724, height: 430 });
await flush();
const small = resolveProxy(projectDir, sourcePath, "h264", { width: 608, height: 362 });
await flush();
expect(calls).toHaveLength(1);
succeed(calls[0]!);
const made = await large;
expect(await small).toBe(made);
expect(await resolveProxy(projectDir, sourcePath, "h264", { width: 608, height: 362 })).toBe(
made,
);
expect(calls).toHaveLength(1);

const larger = resolveProxy(projectDir, sourcePath, "h264", { width: 862, height: 512 });
await flush();
expect(calls).toHaveLength(2);
succeed(calls[1]!);
expect(await larger).not.toBe(made);
});

it("makes a borrowed copy at its own size when the larger job it borrowed is dropped before the smaller ask joins it", async () => {
let dropOnCheck: { path: string; drop: () => void } | null = null;
vi.doMock("./safePath.js", async (importOriginal) => {
const real = await importOriginal<typeof import("./safePath.js")>();
return {
...real,
isProjectCacheFileAsync: async (root: string, file: string) => {
if (dropOnCheck?.path === file) {
const armed = dropOnCheck;
dropOnCheck = null;
armed.drop();
}
return real.isProjectCacheFileAsync(root, file);
},
};
});
const { spawn, calls } = createSpawnSpy();
const { resolveProxy, getProxyCachePath } = await loadModule(spawn, FFMPEG_PATH);
const projectDir = tmpProject();
const sourcePath = join(projectDir, "clip.mov");
writeFileSync(sourcePath, "source-bytes");
const large = { width: 724, height: 430 };
const leaving = new AbortController();

const left = resolveProxy(projectDir, sourcePath, "h264", large, { signal: leaving.signal });
void left.catch(() => undefined);
await flush();
expect(calls).toHaveLength(1);
dropOnCheck = {
path: await getProxyCachePath(projectDir, sourcePath, "h264", large),
drop: () => {
leaving.abort();
calls[0]!.proc.emit("close", null, "SIGTERM");
},
};
const small = resolveProxy(projectDir, sourcePath, "h264", { width: 608, height: 362 });
await flush();

expect(dropOnCheck).toBeNull();
await vi.waitFor(() => expect(calls).toHaveLength(2));
expect(calls[1]!.args.join(" ")).toContain("724/iw");
expect(calls[1]!.args.join(" ")).not.toContain("608/iw");
succeed(calls[1]!);
await small;
});

it("uses Chromium-compatible VP8 alpha args and a distinct WebM cache path", async () => {
const { spawn, calls } = createSpawnSpy();
const { resolveProxy, getProxyCachePath } = await loadModule(spawn, FFMPEG_PATH);
Expand Down
64 changes: 58 additions & 6 deletions packages/studio-server/src/helpers/proxyTranscoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,22 @@ async function canonicalizeProxySource(
return { projectDir: real, sourcePath };
}

/** The copy for this source: the one at its fast name, the one its fast index names, or where a new one goes. */
async function getCanonicalProxyCachePath(
source: CanonicalProxySource,
variant: ProxyVariant,
box?: PreviewProxyBox,
limitSeconds?: number,
): Promise<string> {
return (await findCopy(source, variant, box, limitSeconds)).path;
}

/** The copy for this source and its box: at its fast name, its fast index, a larger copy, or where a new one goes. */
async function findCopy(
source: CanonicalProxySource,
variant: ProxyVariant,
box: PreviewProxyBox | undefined,
limitSeconds: number | undefined,
): Promise<{ path: string; box?: PreviewProxyBox }> {
const key = await fastCopyKey(
source.sourcePath,
PROXY_PARAMS_VERSION,
Expand All @@ -242,9 +251,47 @@ async function getCanonicalProxyCachePath(
CACHE_DIR_NAME,
`${key}${PROXY_VARIANT_CONFIG[variant].extension}`,
);
return (await isProjectCacheFileAsync(source.projectDir, made))
const exact = (await isProjectCacheFileAsync(source.projectDir, made))
? made
: ((await readCopyIndex(source.projectDir, fastIndexPath(made))) ?? made);
if (!box) return { path: exact };
const family = await fastCopyKey(
source.sourcePath,
PROXY_PARAMS_VERSION,
keyVariant(variant, limitSeconds),
undefined,
);
const sized = boxedCopiesAsked.get(family) ?? new Map<string, PreviewProxyBox>();
boxedCopiesAsked.set(family, sized.set(exact, box));
if (
exact !== made ||
inFlight.has(exact) ||
(await isProjectCacheFileAsync(source.projectDir, made))
)
return { path: exact, box };
return (
(await largerCopyMadeOrMaking(source.projectDir, sized, box, exact)) ?? { path: exact, box }
);
}

/** Boxed copies this process was asked for, per source version: a smaller ask is served by one made or being made. */
const boxedCopiesAsked = new Map<string, Map<string, PreviewProxyBox>>();

async function largerCopyMadeOrMaking(
projectDir: string,
sized: Map<string, PreviewProxyBox>,
box: PreviewProxyBox,
exact: string,
): Promise<{ path: string; box: PreviewProxyBox } | undefined> {
const larger = [...sized]
.filter(
([path, other]) => path !== exact && other.width >= box.width && other.height >= box.height,
)
.sort(([, a], [, b]) => a.width * a.height - b.width * b.height);
for (const [path, other] of larger)
if (inFlight.has(path) || (await isProjectCacheFileAsync(projectDir, path)))
return { path, box: other };
return undefined;
}

/**
Expand Down Expand Up @@ -1229,8 +1276,13 @@ async function lookUpCopy(
limitSeconds: number | undefined,
) {
const source = await canonicalizeProxySource(projectDir, absoluteSourcePath);
const cachePath = await getCanonicalProxyCachePath(source, variant, box, limitSeconds);
return { source, cachePath, cached: await isProjectCacheFileAsync(source.projectDir, cachePath) };
const { path: cachePath, box: copyBox } = await findCopy(source, variant, box, limitSeconds);
return {
source,
cachePath,
copyBox,
cached: await isProjectCacheFileAsync(source.projectDir, cachePath),
};
}

const askOrder = new Map<string, Promise<unknown>>();
Expand Down Expand Up @@ -1270,7 +1322,7 @@ export async function resolveProxy(
options: ResolveProxyOptions = {},
): Promise<string> {
options.signal?.throwIfAborted();
const { source, cachePath, cached } = await reachQueueInAskOrder(
const { source, cachePath, copyBox, cached } = await reachQueueInAskOrder(
projectDir,
lookUpCopy(projectDir, absoluteSourcePath, variant, box, options.limitSeconds),
);
Expand Down Expand Up @@ -1305,7 +1357,7 @@ export async function resolveProxy(
source.sourcePath,
cachePath,
variant,
box,
copyBox,
options.limitSeconds,
)
.catch((err: unknown) => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/package-size-budgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
},
"@hyperframes/studio-server": {
"packed": 203563,
"unpacked": 779727
"unpacked": 787666
}
}
Loading