From 632c5b5acf19bf8a6676f0a9cd83c869e45eea2f Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 21 Nov 2024 15:06:58 +0530 Subject: [PATCH] Restore queue Observed on Safari > Failed to generate video thumbnail using the wasm FFmpeg web worker, will fallback to canvas: RuntimeError: Out of bounds memory access (evaluating 'Module["_malloc"](len)') --- web/packages/new/photos/services/ffmpeg/web.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/web/packages/new/photos/services/ffmpeg/web.ts b/web/packages/new/photos/services/ffmpeg/web.ts index cec054a78c..7678c93269 100644 --- a/web/packages/new/photos/services/ffmpeg/web.ts +++ b/web/packages/new/photos/services/ffmpeg/web.ts @@ -6,10 +6,14 @@ import { inputPathPlaceholder, outputPathPlaceholder, } from "./constants"; +import QueueProcessor from "@ente/shared/utils/queueProcessor"; /** Lazily initialized and loaded FFmpeg instance. */ let _ffmpeg: Promise | undefined; +/** Queue of in-flight requests. */ +const _ffmpegTaskQueue = new QueueProcessor(); + /** * Return the shared {@link FFmpeg} instance, lazily creating and loading it if * needed. @@ -46,8 +50,18 @@ export const ffmpegExecWeb = async ( command: string[], blob: Blob, outputFileExtension: string, -): Promise => - ffmpegExec(await ffmpegLazy(), command, outputFileExtension, blob); +): Promise => { + const ffmpeg = await ffmpegLazy(); + // Interleaving multiple ffmpeg.execs causes errors like + // + // > "Out of bounds memory access (evaluating 'Module["_malloc"](len)')" + // + // So serialize them using a promise queue. + const request = _ffmpegTaskQueue.queueUpRequest(() => + ffmpegExec(ffmpeg, command, outputFileExtension, blob), + ); + return await request.promise; +}; const ffmpegExec = async ( ffmpeg: FFmpeg,