Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

rendezvous

Manav Rathif47a6f7b423e
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.ts
index e3f99f230d..1bf3069add 100644
--- a/desktop/src/main/services/ffmpeg-worker.ts
+++ b/desktop/src/main/services/ffmpeg-worker.ts
@@ -56,8 +56,15 @@ process.parentPort.once("message", (e) => {
} satisfies FFmpegUtilityProcess,
messagePortMainEndpoint(e.ports[0]!),
);
+ mainProcess("ack", undefined);
});
+/**
+ * Send a message to the main process using a barebones RPC protocol.
+ */
+const mainProcess = (method: string, param: unknown) =>
+ process.parentPort.postMessage({ method, p: param });
+
/**
* Run a FFmpeg command
*
diff --git a/desktop/src/main/services/ffmpeg.ts b/desktop/src/main/services/ffmpeg.ts
index 4567d7b704..49f13a5a53 100644
--- a/desktop/src/main/services/ffmpeg.ts
+++ b/desktop/src/main/services/ffmpeg.ts
@@ -17,7 +17,7 @@ import { ffmpegUtilityProcessPort } from "./workers";
* Return a handle to the ffmpeg utility process, starting it if needed.
*/
export const ffmpegUtilityProcess = () => {
- return ffmpegUtilityProcessPort() as unknown as FFmpegUtilityProcess;
+ return ffmpegUtilityProcessPort() as unknown as Promise<FFmpegUtilityProcess>;
};
export const ffmpegExec = async (
@@ -25,6 +25,8 @@ export const ffmpegExec = async (
dataOrPathOrZipItem: Uint8Array | string | ZipItem,
outputFileExtension: string,
): Promise<Uint8Array> => {
+ const worker = await ffmpegUtilityProcess();
+
const {
path: inputFilePath,
isFileTemporary: isInputFileTemporary,
@@ -35,11 +37,7 @@ export const ffmpegExec = async (
try {
await writeToTemporaryInputFile();
- await ffmpegUtilityProcess().ffmpegExec(
- command,
- inputFilePath,
- outputFilePath,
- );
+ await worker.ffmpegExec(command, inputFilePath, outputFilePath);
return await fs.readFile(outputFilePath);
} finally {
diff --git a/desktop/src/main/services/workers.ts b/desktop/src/main/services/workers.ts
index d10f5527b5..6c109ca401 100644
--- a/desktop/src/main/services/workers.ts
+++ b/desktop/src/main/services/workers.ts
@@ -17,10 +17,10 @@ import log, { processUtilityProcessLogMessage } from "../log";
let _utilityProcessML: UtilityProcess | undefined;
/**
- * A {@link MessagePort} that can be used to communicate with
- * the active ffmpeg utility process (if any).
+ * A promise to a {@link MessagePort} that can be used to communicate with the
+ * active ffmpeg utility process (if any).
*/
-let _utilityProcessFFmpegPort: MessagePortMain | undefined;
+let _utilityProcessFFmpegPort: Promise<MessagePortMain> | undefined;
/**
* Create a new utility process of the given {@link type}, terminating the older
@@ -175,18 +175,25 @@ export const ffmpegUtilityProcessPort = () => {
// Send a handle to the port
child.postMessage({}, [port1]);
- handleMessagesFromFFmpegUtilityProcess(child);
-
- _utilityProcessFFmpegPort = port2;
-
- return _utilityProcessFFmpegPort;
-};
-
-const handleMessagesFromFFmpegUtilityProcess = (child: UtilityProcess) => {
child.on("message", (m: unknown) => {
+ if (m && typeof m == "object" && "method" in m) {
+ switch (m.method) {
+ case "ack":
+ resolvePortPromise!(port2);
+ return;
+ }
+ }
+
if (processUtilityProcessLogMessage("[ffmpeg-worker]", m)) {
return;
}
+
log.info("Ignoring unknown message from ffmpeg utility process", m);
});
+
+ let resolvePortPromise: ((port: MessagePortMain) => void) | undefined;
+ _utilityProcessFFmpegPort = new Promise((r) => (resolvePortPromise = r));
+
+ // Resolve with the other end of the port we sent to the utility process.
+ return _utilityProcessFFmpegPort;
};
diff --git a/desktop/src/main/stream.ts b/desktop/src/main/stream.ts
index 0f0a37bd93..f66946bc7c 100644
--- a/desktop/src/main/stream.ts
+++ b/desktop/src/main/stream.ts
@@ -231,11 +231,12 @@ export const clearPendingVideoResults = () => pendingVideoResults.clear();
* See also: [Note: IPC streams]
*/
const handleConvertToMP4Write = async (request: Request) => {
+ const worker = await ffmpegUtilityProcess();
+
const inputTempFilePath = await makeTempFilePath();
await writeStream(inputTempFilePath, request.body!);
const outputTempFilePath = await makeTempFilePath("mp4");
- const worker = ffmpegUtilityProcess();
try {
await worker.ffmpegConvertToMP4(inputTempFilePath, outputTempFilePath);
} catch (e) {
@@ -309,7 +310,7 @@ const handleGenerateHLSWrite = async (
}
}
- const worker = ffmpegUtilityProcess();
+ const worker = await ffmpegUtilityProcess();
const {
path: inputFilePath,