Skip to content

armgit / ente

Public Imported from GitHub
0

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

web side 1

Manav Rathi71d6aed1aa86
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index 25f5ce2126..b1c5ff57e8 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -334,43 +334,7 @@ export interface Electron {
maxSize: number,
) => Promise<Uint8Array>;
- /**
- * Execute a FFmpeg {@link command} on the given
- * {@link dataOrPathOrZipItem}.
- *
- * This executes the command using a FFmpeg executable we bundle with our
- * desktop app. We also have a Wasm FFmpeg implementation that we use when
- * running on the web, which has a sibling function with the same
- * 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
- * and ffmpeg's own path are replaced before executing the command
- * (respectively {@link inputPathPlaceholder},
- * {@link outputPathPlaceholder}, {@link ffmpegPathPlaceholder}).
- *
- * @param dataOrPathOrZipItem The bytes of the input file, or the path to
- * the input file on the user's local disk, or the path to a zip file on the
- * user's disk and the name of an entry in it. In all three cases, the data
- * gets serialized to a temporary file, and then that path gets substituted
- * in the FFmpeg {@link command} in lieu of {@link inputPathPlaceholder}.
- *
- * @param outputFileExtension The extension (without the dot, e.g. "jpeg")
- * to use for the output file that we ask FFmpeg to create in
- * {@param command}. While this file will eventually get deleted, and we'll
- * just return its contents, for some FFmpeg command the extension matters
- * (e.g. conversion to a JPEG fails if the extension is arbitrary).
- *
- * @returns The contents of the output file produced by the ffmpeg command
- * (specified as {@link outputPathPlaceholder} in {@link command}).
- */
- ffmpegExec: (
- command: FFmpegCommand,
- dataOrPathOrZipItem: Uint8Array | string | ZipItem,
- outputFileExtension: string,
- ) => Promise<Uint8Array>;
-
- // - ML
+ // - Utility process
/**
* Trigger the creation of a new utility process of the given {@link type},
@@ -385,6 +349,8 @@ export interface Electron {
* value of {@link type}. Thus, att the other end of that port will be an
* object that conforms to:
*
+ * - {@link ElectronFFmpegWorker} interface, when type is "ffmpeg".
+ *
* - {@link ElectronMLWorker} interface, when type is "ml".
*
* For more details about the IPC flow, see: [Note: ML IPC].
@@ -596,7 +562,50 @@ export interface Electron {
clearPendingUploads: () => Promise<void>;
}
-export type UtilityProcessType = "ml";
+export type UtilityProcessType = "ffmpeg" | "ml";
+
+/**
+ * The shape of the object exposed by the Node.js utility process listening on
+ * the other side message port that the web layer obtains by doing
+ * {@link triggerCreateUtilityProcess} with type "ffmpeg".
+ */
+export interface ElectronFFmpegWorker {
+ /**
+ * Execute a FFmpeg {@link command} on the given
+ * {@link dataOrPathOrZipItem}.
+ *
+ * This executes the command using a FFmpeg executable we bundle with our
+ * desktop app. We also have a Wasm FFmpeg implementation that we use when
+ * running on the web, which has a sibling function with the same
+ * 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
+ * and ffmpeg's own path are replaced before executing the command
+ * (respectively {@link inputPathPlaceholder},
+ * {@link outputPathPlaceholder}, {@link ffmpegPathPlaceholder}).
+ *
+ * @param dataOrPathOrZipItem The bytes of the input file, or the path to
+ * the input file on the user's local disk, or the path to a zip file on the
+ * user's disk and the name of an entry in it. In all three cases, the data
+ * gets serialized to a temporary file, and then that path gets substituted
+ * in the FFmpeg {@link command} in lieu of {@link inputPathPlaceholder}.
+ *
+ * @param outputFileExtension The extension (without the dot, e.g. "jpeg")
+ * to use for the output file that we ask FFmpeg to create in
+ * {@param command}. While this file will eventually get deleted, and we'll
+ * just return its contents, for some FFmpeg command the extension matters
+ * (e.g. conversion to a JPEG fails if the extension is arbitrary).
+ *
+ * @returns The contents of the output file produced by the ffmpeg command
+ * (specified as {@link outputPathPlaceholder} in {@link command}).
+ */
+ ffmpegExec: (
+ command: FFmpegCommand,
+ dataOrPathOrZipItem: Uint8Array | string | ZipItem,
+ outputFileExtension: string,
+ ) => Promise<Uint8Array>;
+}
/**
* The shape of the object exposed by the Node.js utility process listening on
diff --git a/web/packages/gallery/services/ffmpeg/index.ts b/web/packages/gallery/services/ffmpeg/index.ts
index 1dd4f7f4b7..d2f6af4756 100644
--- a/web/packages/gallery/services/ffmpeg/index.ts
+++ b/web/packages/gallery/services/ffmpeg/index.ts
@@ -1,6 +1,6 @@
import { ensureElectron } from "ente-base/electron";
import log from "ente-base/log";
-import type { Electron } from "ente-base/types/ipc";
+import type { Electron, ElectronFFmpegWorker } from "ente-base/types/ipc";
import {
toDataOrPathOrZipEntry,
type FileSystemUploadItem,
@@ -16,6 +16,7 @@ import {
type ParsedMetadata,
} from "ente-media/file-metadata";
import { settingsSnapshot } from "ente-new/photos/services/settings";
+import { createUtilityProcess } from "../../utils/native-worker";
import {
ffmpegPathPlaceholder,
inputPathPlaceholder,
@@ -23,6 +24,21 @@ import {
} from "./constants";
import { ffmpegExecWeb } from "./web";
+let _electronFFmpegWorker: Promise<ElectronFFmpegWorker> | undefined;
+
+/**
+ * Handle to the on-demand lazily created utility process in the Node.js layer
+ * that exposes an {@link ElectronFFmpegWorker} interface.
+ */
+const electronFFmpegWorker = () =>
+ (_electronFFmpegWorker ??= createElectronFFmpegWorker());
+
+const createElectronFFmpegWorker = () =>
+ createUtilityProcess(
+ ensureElectron(),
+ "ffmpeg",
+ ) as unknown as Promise<ElectronFFmpegWorker>;
+
/**
* Generate a thumbnail for the given video using a Wasm FFmpeg running in a web
* worker.
@@ -76,14 +92,15 @@ const _generateVideoThumbnail = async (
* See also {@link generateVideoThumbnailNative}.
*/
export const generateVideoThumbnailNative = async (
- electron: Electron,
fsUploadItem: FileSystemUploadItem,
) =>
- _generateVideoThumbnail((seekTime: number) =>
- electron.ffmpegExec(
- makeGenThumbnailCommand(seekTime),
- toDataOrPathOrZipEntry(fsUploadItem),
- "jpeg",
+ electronFFmpegWorker().then((electronFW) =>
+ _generateVideoThumbnail((seekTime: number) =>
+ electronFW.ffmpegExec(
+ makeGenThumbnailCommand(seekTime),
+ toDataOrPathOrZipEntry(fsUploadItem),
+ "jpeg",
+ ),
),
);
@@ -144,11 +161,9 @@ export const extractVideoMetadata = async (
return parseFFmpegExtractedMetadata(
uploadItem instanceof File
? await ffmpegExecWeb(command, uploadItem, "txt")
- : await ensureElectron().ffmpegExec(
- command,
- toDataOrPathOrZipEntry(uploadItem),
- "txt",
- ),
+ : await (
+ await electronFFmpegWorker()
+ ).ffmpegExec(command, toDataOrPathOrZipEntry(uploadItem), "txt"),
);
};
diff --git a/web/packages/gallery/services/upload/thumbnail.ts b/web/packages/gallery/services/upload/thumbnail.ts
index 49c8b78135..6cb9696de4 100644
--- a/web/packages/gallery/services/upload/thumbnail.ts
+++ b/web/packages/gallery/services/upload/thumbnail.ts
@@ -200,7 +200,7 @@ export const generateThumbnailNative = async (
maxThumbnailDimension,
maxThumbnailSize,
)
- : ffmpeg.generateVideoThumbnailNative(electron, fsUploadItem);
+ : ffmpeg.generateVideoThumbnailNative(fsUploadItem);
/**
* A fallback, black, thumbnail for use in cases where thumbnail generation