diff --git a/packages/cli/src/utils/checkBrowser.test.ts b/packages/cli/src/utils/checkBrowser.test.ts index 17467a3f6a..7b38b1ef22 100644 --- a/packages/cli/src/utils/checkBrowser.test.ts +++ b/packages/cli/src/utils/checkBrowser.test.ts @@ -30,6 +30,7 @@ const mocks = vi.hoisted(() => ({ resolveProxy: vi.fn<(projectDir: string, absoluteSourcePath: string, variant?: string) => Promise>(), FfmpegUnavailableError: class FfmpegUnavailableError extends Error {}, + getProxyCachePath: vi.fn((projectDir: string, sourcePath: string) => `${sourcePath}.proxy.mp4`), scanProjectMediaCodecMap: vi.fn< (...args: unknown[]) => Promise< Record< @@ -84,9 +85,16 @@ vi.mock("@hyperframes/studio-server/media-codec-map", async (importOriginal) => })); vi.mock("@hyperframes/studio-server/proxy-transcoder", () => ({ FfmpegUnavailableError: mocks.FfmpegUnavailableError, - resolveProxies: (projectDir: string, sources: Array<{ sourcePath: string; variant: string }>) => + getProxyCachePath: mocks.getProxyCachePath, + resolveProxies: ( + projectDir: string, + sources: Array<{ sourcePath: string; variant: string }>, + onSettled: () => void = () => {}, + ) => Promise.allSettled( - sources.map(({ sourcePath, variant }) => mocks.resolveProxy(projectDir, sourcePath, variant)), + sources.map(({ sourcePath, variant }) => + mocks.resolveProxy(projectDir, sourcePath, variant).finally(onSettled), + ), ), })); @@ -1172,17 +1180,47 @@ describe("preResolveHostileMediaProxies", () => { await preResolveHostileMediaProxies(projectDir, ""); - expect(errorSpy).toHaveBeenCalledTimes(1); - expect(errorSpy).toHaveBeenCalledWith( + expect(errorSpy.mock.calls.map(([line]) => line)).toEqual([ + expect.stringMatching(/^\[hyperframes\] Making browser-playable copies of 1 video /), + "[hyperframes] browser-playable copies: 1/1", expect.stringMatching( /^\[hyperframes\] media proxy pre-resolve: 1\/1 ready, 0 failed \(\d+ms\)$/, ), - ); + ]); expect(infoSpy).not.toHaveBeenCalled(); expect(logSpy).not.toHaveBeenCalled(); expect(stdoutSpy).not.toHaveBeenCalled(); }); + it("says it is making copies, and counts them, only when some are not made yet", async () => { + const projectDir = mkProjectDir(); + const codec = { + codecName: "hevc", + browserHostile: true, + representativeMime: null, + hasAlpha: false, + }; + mocks.scanProjectMediaCodecMap.mockResolvedValue({ "/a.mov": codec, "/b.mov": codec }); + mocks.resolveProxy.mockResolvedValue("/cache/copy.mp4"); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); + + writeFileSync(join(projectDir, "a.mov.proxy.mp4"), ""); + await preResolveHostileMediaProxies(projectDir, ""); + expect(errorSpy.mock.calls.map(([line]) => String(line).slice(0, 48))).toEqual([ + "[hyperframes] Making browser-playable copies of ", + "[hyperframes] browser-playable copies: 1/2", + "[hyperframes] browser-playable copies: 2/2", + "[hyperframes] media proxy pre-resolve: 2/2 ready", + ]); + expect(errorSpy.mock.calls[0]![0]).toContain("copies of 1 video Chrome"); + + errorSpy.mockClear(); + writeFileSync(join(projectDir, "b.mov.proxy.mp4"), ""); + await preResolveHostileMediaProxies(projectDir, ""); + expect(errorSpy).toHaveBeenCalledTimes(1); + expect(errorSpy.mock.calls[0]![0]).toMatch(/^\[hyperframes\] media proxy pre-resolve: 2\/2/); + }); + it("writes a scan failure to stderr and leaves stdout untouched", async () => { const projectDir = mkProjectDir(); mocks.scanProjectMediaCodecMap.mockRejectedValue(new Error("ffprobe not found")); diff --git a/packages/cli/src/utils/checkBrowser.ts b/packages/cli/src/utils/checkBrowser.ts index 6dc38db92f..c4c42ce8b2 100644 --- a/packages/cli/src/utils/checkBrowser.ts +++ b/packages/cli/src/utils/checkBrowser.ts @@ -1,7 +1,7 @@ import type { CheckDeadline } from "./checkDeadline.js"; import { BrowserCleanupError, closeManagedBrowser } from "../browser/launch.js"; import { checkSnapshotDirectory } from "./checkSnapshotDirectory.js"; -import { mkdirSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import os from "node:os"; import { join, resolve } from "node:path"; import type { Browser, Page } from "puppeteer-core"; @@ -39,6 +39,7 @@ import { } from "@hyperframes/studio-server/media-codec-map"; import { FfmpegUnavailableError, + getProxyCachePath, resolveProxies, } from "@hyperframes/studio-server/proxy-transcoder"; import { rectToBbox } from "./checkTypes.js"; @@ -165,12 +166,22 @@ export async function preResolveHostileMediaProxies( variant: proxyVariantFor(facts), }; }); - const results = await resolveProxies(projectDir, sources); + const total = hostileEntries.length; + const toMake = sources.filter((source) => !proxyCached(projectDir, source)).length; + if (toMake > 0) { + console.error( + `[hyperframes] Making browser-playable copies of ${toMake} video${toMake === 1 ? "" : "s"} Chrome cannot decode. Only the first check of these files waits for this; later checks reuse the copies.`, + ); + } + let settled = 0; + const results = await resolveProxies(projectDir, sources, () => { + settled += 1; + if (toMake > 0) console.error(`[hyperframes] browser-playable copies: ${settled}/${total}`); + }); const failures = sources.flatMap((source, index) => { const result = results[index]!; return result.status === "rejected" ? [{ ...source, reason: result.reason }] : []; }); - const total = hostileEntries.length; console.error( `[hyperframes] media proxy pre-resolve: ${total - failures.length}/${total} ready, ${failures.length} failed (${Date.now() - startedAt}ms)`, ); @@ -192,6 +203,17 @@ export async function preResolveHostileMediaProxies( return { findings, failedPaths: new Set(failures.map(({ pathname }) => pathname)) }; } +function proxyCached( + projectDir: string, + source: { sourcePath: string; variant: ReturnType }, +): boolean { + try { + return existsSync(getProxyCachePath(projectDir, source.sourcePath, source.variant)); + } catch { + return false; + } +} + interface ProxyPreResolution { findings: RuntimeDraft[]; /** Root-relative paths of the videos whose copy failed. */ diff --git a/packages/studio-server/src/helpers/proxyTranscoder.test.ts b/packages/studio-server/src/helpers/proxyTranscoder.test.ts index 06fa94c158..74e7785354 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.test.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.test.ts @@ -578,11 +578,13 @@ describe("resolveProxy", () => { const projectDir = tmpProject(); const sources = batchSources(projectDir); - const batch = resolveProxies(projectDir, sources); + let settled = 0; + const batch = resolveProxies(projectDir, sources, () => settled++); await answerUntilIdle(calls, (call) => succeed(call)); const results = await batch; expect(results.map((result) => result.status)).toEqual(sources.map(() => "fulfilled")); + expect(settled).toBe(sources.length); }); it("settles each request of a batch on its own when one of them fails", async () => { diff --git a/packages/studio-server/src/helpers/proxyTranscoder.ts b/packages/studio-server/src/helpers/proxyTranscoder.ts index 001534af3b..79c2a94235 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.ts @@ -658,6 +658,7 @@ export async function resolveProxy( export async function resolveProxies( projectDir: string, sources: ReadonlyArray<{ sourcePath: string; variant: ProxyVariant }>, + onSettled: () => void = () => {}, ): Promise[]> { const results: PromiseSettledResult[] = []; let next = 0; @@ -671,6 +672,7 @@ export async function resolveProxies( (value) => ({ status: "fulfilled", value }), (reason: unknown) => ({ status: "rejected", reason }), ); + onSettled(); } }; const workers = Math.min(MAX_CONCURRENT_TRANSCODES, sources.length);