Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(media-server): render process previews while the output uploads
Richie McIlroy7b5c08ef2f8f
diff --git a/apps/media-server/src/__tests__/routes/lifecycle.test.ts b/apps/media-server/src/__tests__/routes/lifecycle.test.tsindex ae93d131a..37a271c8a 100644--- a/apps/media-server/src/__tests__/routes/lifecycle.test.ts+++ b/apps/media-server/src/__tests__/routes/lifecycle.test.ts@@ -313,6 +313,113 @@ describe("process jobs", () => {expect(files.every((handle) => !existsSync(handle.path))).toBe(true);});+ function previewJob(videoId: string) {+ return post("/video/process", {+ videoId,+ userId: "user-id",+ videoUrl: "https://example.com/input.mp4",+ outputPresignedUrl: "https://example.com/output.mp4",+ thumbnailPresignedUrl: "https://example.com/screenshot.jpg",+ previewGifPresignedUrl: "https://example.com/preview.gif",+ });+ }++ function mockPreviewJob() {+ const events: string[] = [];+ const signals: AbortSignal[] = [];+ const gifs: TempFileHandle[] = [];+ spyOn(mediaVideo, "downloadVideoToTemp").mockImplementation(() =>+ tempFileWith(".mp4", "input"),+ );+ spyOn(mediaProbe, "probeVideoFile").mockResolvedValue({+ ...metadata,+ duration: 10,+ });+ spyOn(mediaVideo, "processVideo").mockImplementation(() =>+ tempFileWith(".mp4", "output"),+ );+ spyOn(mediaVideo, "generateThumbnail").mockImplementation(+ async (_path, _duration, _options, signal) => {+ events.push("thumbnail rendered");+ if (signal) signals.push(signal);+ return new Uint8Array([1]);+ },+ );+ spyOn(mediaVideo, "generatePreviewGif").mockImplementation(+ async (_path, _duration, _options, signal) => {+ events.push("gif rendered");+ if (signal) signals.push(signal);+ const gif = await tempFileWith(".gif", "gif");+ gifs.push(gif);+ return gif;+ },+ );+ spyOn(mediaVideo, "uploadToS3").mockImplementation(async () => {+ events.push("thumbnail uploaded");+ });+ const output = deferred<mediaVideo.StorageUploadReceipt>();+ const outputStarted = deferred();+ const outputFailure = deferred<Error | null>();+ spyOn(mediaVideo, "uploadFileToS3").mockImplementation(+ async (_path, _url, contentType) => {+ if (contentType === "image/gif") {+ events.push("gif uploaded");+ return {};+ }+ events.push("output upload started");+ outputStarted.resolve();+ const failure = await Promise.race([+ output.promise.then(() => null),+ outputFailure.promise,+ ]);+ if (failure) throw failure;+ events.push("output uploaded");+ return {};+ },+ );+ return { events, signals, gifs, output, outputStarted, outputFailure };+ }++ test("renders previews while the output uploads and uploads them after it", async () => {+ const preview = mockPreviewJob();+ const response = await app.fetch(previewJob("video-preview-overlap"));+ const { jobId } = await response.json();+ createdJobs.push(jobId);+ await preview.outputStarted.promise;+ await waitFor(() => preview.events.includes("gif rendered"));+ expect(preview.events).toContain("thumbnail rendered");+ expect(preview.events).not.toContain("thumbnail uploaded");+ expect(preview.events).not.toContain("gif uploaded");++ preview.output.resolve({});+ await waitFor(() => getJob(jobId)?.phase === "complete");+ const uploaded = preview.events.indexOf("output uploaded");+ expect(preview.events.indexOf("thumbnail uploaded")).toBeGreaterThan(+ uploaded,+ );+ expect(preview.events.indexOf("gif uploaded")).toBeGreaterThan(uploaded);+ await waitForDrain(2_000);+ expect(preview.gifs.every((gif) => !existsSync(gif.path))).toBe(true);+ });++ test("uploads no previews and stops rendering when the output upload fails", async () => {+ const preview = mockPreviewJob();+ const response = await app.fetch(previewJob("video-preview-upload-fails"));+ const { jobId } = await response.json();+ createdJobs.push(jobId);+ await preview.outputStarted.promise;+ await waitFor(() => preview.events.includes("gif rendered"));++ preview.outputFailure.resolve(new Error("Storage upload failed: 403"));+ expect(await waitForDrain(2_000)).toBe(true);+ expect(getJob(jobId)?.phase).toBe("error");+ expect(preview.events).not.toContain("thumbnail uploaded");+ expect(preview.events).not.toContain("gif uploaded");+ expect(preview.signals.length).toBe(2);+ expect(preview.signals.every((signal) => signal.aborted)).toBe(true);+ expect(preview.gifs.every((gif) => !existsSync(gif.path))).toBe(true);+ });+test("releases the job slot and temp files when processing fails", async () => {const before = getActiveVideoProcessCount();let input: TempFileHandle | undefined;diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.tsindex b0163bcb1..34478db9b 100644--- a/apps/media-server/src/routes/video.ts+++ b/apps/media-server/src/routes/video.ts@@ -63,6 +63,7 @@ import {} from "../lib/media-transfer";import type {ResilientInputFlags,+ StorageUploadReceipt,StorageUploadTarget,} from "../lib/media-video";import {@@ -1197,15 +1198,30 @@ async function generateAndUploadPreviewGif(): Promise<void> {if (!previewGifPresignedUrl) return;- let previewGifFile: TempFileHandle | null = null;-+ const render = generatePreviewGif(inputPath, duration, {}, abortSignal);try {- previewGifFile = await generatePreviewGif(- inputPath,- duration,- {},+ await uploadRenderedPreviewGif(+ render,+ previewGifPresignedUrl,abortSignal,+ logPrefix,+ );+ } finally {+ await render.then(+ (gif) => gif.cleanup(),+ () => {},);+ }+}++async function uploadRenderedPreviewGif(+ render: Promise<TempFileHandle>,+ previewGifPresignedUrl: string,+ abortSignal: AbortSignal | undefined,+ logPrefix: string,+): Promise<void> {+ try {+ const previewGifFile = await render;await uploadFileToS3(previewGifFile.path,previewGifPresignedUrl,@@ -1219,8 +1235,6 @@ async function generateAndUploadPreviewGif(: new Error("Preview GIF generation aborted");}console.warn(`[${logPrefix}] Preview GIF generation failed:`, previewErr);- } finally {- await previewGifFile?.cleanup();}}@@ -1639,71 +1653,92 @@ async function processVideoAsync(});await sendWebhook(job);- const uploadReceipt = await withJobHeartbeat(jobId, async () =>- options.existingOutput- ? { objectIdentity: options.existingOutput.objectIdentity }- : options.outputUpload- ? uploadFileToStorage(- outputPath,- options.outputUpload,- "video/mp4",- signal,- )- : uploadFileToS3(outputPath, outputPresignedUrl, "video/mp4", signal),- );- outputUploaded = true;-- if (thumbnailPresignedUrl || previewGifPresignedUrl) {- updateJob(jobId, {- phase: "generating_thumbnail",- progress: 90,- message: "Generating preview assets...",- });- await sendWebhook(job);- }+ // Previews only read the finished output, so render them while it uploads; they are still+ // uploaded after the output, so a failed upload never leaves previews behind.+ let uploadReceipt: StorageUploadReceipt;+ const previewAbort = new AbortController();+ const previewSignal = AbortSignal.any([signal, previewAbort.signal]);+ const thumbnailRender = thumbnailPresignedUrl+ ? generateThumbnail(outputPath, metadata.duration, {}, previewSignal)+ : undefined;+ const previewGifRender = previewGifPresignedUrl+ ? generatePreviewGif(outputPath, metadata.duration, {}, previewSignal)+ : undefined;+ thumbnailRender?.catch(() => {});+ previewGifRender?.catch(() => {});+ try {+ uploadReceipt = await withJobHeartbeat(jobId, async () =>+ options.existingOutput+ ? { objectIdentity: options.existingOutput.objectIdentity }+ : options.outputUpload+ ? uploadFileToStorage(+ outputPath,+ options.outputUpload,+ "video/mp4",+ signal,+ )+ : uploadFileToS3(+ outputPath,+ outputPresignedUrl,+ "video/mp4",+ signal,+ ),+ );+ outputUploaded = true;- await withJobHeartbeat(jobId, async () => {- if (thumbnailPresignedUrl) {- // Only the fully verified reuse path can finish without a decoded preview.- const thumbnailData = await generateThumbnail(- outputPath,- metadata.duration,- {},- signal,- ).catch((error) => {- signal.throwIfAborted();- if (!options.existingOutput) throw error;- console.warn(- "Verified Loom output preview could not be generated",- error,- );- return undefined;+ if (thumbnailPresignedUrl || previewGifPresignedUrl) {+ updateJob(jobId, {+ phase: "generating_thumbnail",+ progress: 90,+ message: "Generating preview assets...",});- try {- if (thumbnailData)- await uploadToS3(- thumbnailData,- thumbnailPresignedUrl,- "image/jpeg",- signal,+ await sendWebhook(job);+ }++ await withJobHeartbeat(jobId, async () => {+ if (thumbnailRender && thumbnailPresignedUrl) {+ // Only the fully verified reuse path can finish without a decoded preview.+ const thumbnailData = await thumbnailRender.catch((error) => {+ signal.throwIfAborted();+ if (!options.existingOutput) throw error;+ console.warn(+ "Verified Loom output preview could not be generated",+ error,);- } catch (error) {- signal.throwIfAborted();- console.warn(- `[video/process] Thumbnail upload failed for ${jobId}:`,- error,- );+ return undefined;+ });+ try {+ if (thumbnailData)+ await uploadToS3(+ thumbnailData,+ thumbnailPresignedUrl,+ "image/jpeg",+ signal,+ );+ } catch (error) {+ signal.throwIfAborted();+ console.warn(+ `[video/process] Thumbnail upload failed for ${jobId}:`,+ error,+ );+ }}- }- await generateAndUploadPreviewGif(- outputPath,- metadata.duration,- previewGifPresignedUrl,- signal,- "video/process",+ if (previewGifRender && previewGifPresignedUrl)+ await uploadRenderedPreviewGif(+ previewGifRender,+ previewGifPresignedUrl,+ signal,+ "video/process",+ );+ });+ } finally {+ previewAbort.abort();+ await previewGifRender?.then(+ (gif) => gif.cleanup(),+ () => {},);- });+ }signal.throwIfAborted();updateJob(jobId, {