Remove ffmpeg timeout

This commit is contained in:
Manav Rathi 2024-05-13 19:59:50 +05:30
parent 0b797cebed
commit bf7c97c006
No known key found for this signature in database
8 changed files with 21 additions and 77 deletions

View file

@ -174,14 +174,7 @@ export const attachIPCHandlers = () => {
command: string[],
dataOrPathOrZipItem: Uint8Array | string | ZipItem,
outputFileExtension: string,
timeoutMS: number,
) =>
ffmpegExec(
command,
dataOrPathOrZipItem,
outputFileExtension,
timeoutMS,
),
) => ffmpegExec(command, dataOrPathOrZipItem, outputFileExtension),
);
// - ML

View file

@ -140,17 +140,17 @@ const checkForUpdatesAndNotify = async (mainWindow: BrowserWindow) => {
log.debug(() => "Attempting auto update");
await autoUpdater.downloadUpdate();
let timeoutId: ReturnType<typeof setTimeout>;
let timeout: ReturnType<typeof setTimeout>;
const fiveMinutes = 5 * 60 * 1000;
autoUpdater.on("update-downloaded", () => {
timeoutId = setTimeout(
timeout = setTimeout(
() => showUpdateDialog({ autoUpdatable: true, version }),
fiveMinutes,
);
});
autoUpdater.on("error", (error) => {
clearTimeout(timeoutId);
clearTimeout(timeout);
log.error("Auto update failed", error);
showUpdateDialog({ autoUpdatable: false, version });
});

View file

@ -1,7 +1,7 @@
import pathToFfmpeg from "ffmpeg-static";
import fs from "node:fs/promises";
import type { ZipItem } from "../../types/ipc";
import { ensure, withTimeout } from "../utils/common";
import { ensure } from "../utils/common";
import { execAsync } from "../utils/electron";
import {
deleteTempFileIgnoringErrors,
@ -45,7 +45,6 @@ export const ffmpegExec = async (
command: string[],
dataOrPathOrZipItem: Uint8Array | string | ZipItem,
outputFileExtension: string,
timeoutMS: number,
): Promise<Uint8Array> => {
// TODO (MR): This currently copies files for both input (when
// dataOrPathOrZipItem is data) and output. This needs to be tested
@ -68,8 +67,7 @@ export const ffmpegExec = async (
outputFilePath,
);
if (timeoutMS) await withTimeout(execAsync(cmd), timeoutMS);
else await execAsync(cmd);
await execAsync(cmd);
return fs.readFile(outputFilePath);
} finally {
@ -135,5 +133,5 @@ export const ffmpegConvertToMP4 = async (
const cmd = substitutePlaceholders(command, inputFilePath, outputFilePath);
await withTimeout(execAsync(cmd), 30 * 1000);
await execAsync(cmd);
};

View file

@ -13,32 +13,3 @@ export const ensure = <T>(v: T | null | undefined): T => {
if (v === undefined) throw new Error("Required value was not found");
return v;
};
/**
* Wait for {@link ms} milliseconds
*
* This function is a promisified `setTimeout`. It returns a promise that
* resolves after {@link ms} milliseconds.
*/
export const wait = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
/**
* Await the given {@link promise} for {@link timeoutMS} milliseconds. If it
* does not resolve within {@link timeoutMS}, then reject with a timeout error.
*/
export const withTimeout = async <T>(promise: Promise<T>, ms: number) => {
let timeoutId: ReturnType<typeof setTimeout>;
const rejectOnTimeout = new Promise<T>((_, reject) => {
timeoutId = setTimeout(
() => reject(new Error("Operation timed out")),
ms,
);
});
const promiseAndCancelTimeout = async () => {
const result = await promise;
clearTimeout(timeoutId);
return result;
};
return Promise.race([promiseAndCancelTimeout(), rejectOnTimeout]);
};

View file

@ -143,14 +143,12 @@ const ffmpegExec = (
command: string[],
dataOrPathOrZipItem: Uint8Array | string | ZipItem,
outputFileExtension: string,
timeoutMS: number,
) =>
ipcRenderer.invoke(
"ffmpegExec",
command,
dataOrPathOrZipItem,
outputFileExtension,
timeoutMS,
);
// - ML

View file

@ -36,7 +36,7 @@ import {
*/
export const generateVideoThumbnailWeb = async (blob: Blob) =>
_generateVideoThumbnail((seekTime: number) =>
ffmpegExecWeb(makeGenThumbnailCommand(seekTime), blob, "jpeg", 0),
ffmpegExecWeb(makeGenThumbnailCommand(seekTime), blob, "jpeg"),
);
const _generateVideoThumbnail = async (
@ -75,7 +75,6 @@ export const generateVideoThumbnailNative = async (
makeGenThumbnailCommand(seekTime),
toDataOrPathOrZipEntry(desktopUploadItem),
"jpeg",
0,
),
);
@ -112,12 +111,11 @@ export const extractVideoMetadata = async (
const command = extractVideoMetadataCommand;
const outputData =
uploadItem instanceof File
? await ffmpegExecWeb(command, uploadItem, "txt", 0)
? await ffmpegExecWeb(command, uploadItem, "txt")
: await electron.ffmpegExec(
command,
toDataOrPathOrZipEntry(uploadItem),
"txt",
0,
);
return parseFFmpegExtractedMetadata(outputData);
@ -224,10 +222,9 @@ const ffmpegExecWeb = async (
command: string[],
blob: Blob,
outputFileExtension: string,
timeoutMs: number,
) => {
const worker = await workerFactory.lazy();
return await worker.exec(command, blob, outputFileExtension, timeoutMs);
return await worker.exec(command, blob, outputFileExtension);
};
/**
@ -246,19 +243,15 @@ export const convertToMP4 = async (blob: Blob): Promise<Blob | Uint8Array> => {
if (electron) {
return convertToMP4Native(electron, blob);
} else {
return ffmpegExecWeb(
[
const command = [
ffmpegPathPlaceholder,
"-i",
inputPathPlaceholder,
"-preset",
"ultrafast",
outputPathPlaceholder,
],
blob,
"mp4",
30 * 1000,
);
];
return ffmpegExecWeb(command, blob, "mp4");
}
};

View file

@ -1,5 +1,4 @@
import log from "@/next/log";
import { withTimeout } from "@/utils/promise";
import QueueProcessor from "@ente/shared/utils/queueProcessor";
import { expose } from "comlink";
import {
@ -48,15 +47,11 @@ export class DedicatedFFmpegWorker {
command: string[],
blob: Blob,
outputFileExtension: string,
timeoutMs,
): Promise<Uint8Array> {
if (!this.ffmpeg.isLoaded()) await this.ffmpeg.load();
const go = () =>
ffmpegExec(this.ffmpeg, command, outputFileExtension, blob);
const request = this.ffmpegTaskQueue.queueUpRequest(() =>
timeoutMs ? withTimeout(go(), timeoutMs) : go(),
ffmpegExec(this.ffmpeg, command, outputFileExtension, blob),
);
return await request.promise;

View file

@ -267,7 +267,7 @@ export interface Electron {
* This executes the command using a FFmpeg executable we bundle with our
* desktop app. We also have a wasm FFmpeg wasm implementation that we use
* when running on the web, which has a sibling function with the same
* parameters. See [Note: ffmpeg in Electron].
* parameters. See [Note:FFmpeg in Electron].
*
* @param command An array of strings, each representing one positional
* parameter in the command to execute. Placeholders for the input, output
@ -287,9 +287,6 @@ export interface Electron {
* just return its contents, for some FFmpeg command the extension matters
* (e.g. conversion to a JPEG fails if the extension is arbitrary).
*
* @param timeoutMS If non-zero, then abort and throw a timeout error if the
* ffmpeg command takes more than the given number of milliseconds.
*
* @returns The contents of the output file produced by the ffmpeg command
* (specified as {@link outputPathPlaceholder} in {@link command}).
*/
@ -297,7 +294,6 @@ export interface Electron {
command: string[],
dataOrPathOrZipItem: Uint8Array | string | ZipItem,
outputFileExtension: string,
timeoutMS: number,
) => Promise<Uint8Array>;
// - ML