Skip to content

armgit / ente

Public Imported from GitHub
0

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

Split

Manav Rathi34233875bdec
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index a0fc763b32..277f06ee65 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -10,10 +10,9 @@ import { gunzip, gzip } from "ente-new/photos/utils/gzip";
import { ensurePrecondition } from "ente-utils/ensure";
import { z } from "zod";
import {
- GenerateHLSResult,
+ initiateGenerateHLS,
readVideoStream,
videoStreamDone,
- writeVideoStream,
} from "../utils/native-stream";
import { downloadManager } from "./download";
import {
@@ -31,9 +30,12 @@ interface VideoProcessingQueueItem {
*/
file: EnteFile;
/**
- * The contents of the {@link file} as the newly uploaded {@link UploadItem}.
+ * The {@link UploadItem} if available for the newly uploaded {@link file}.
+ *
+ * If present, this serves as an optimization allowing us to directly read
+ * the file off the user's filesystem.
*/
- uploadItem: UploadItem;
+ uploadItem: UploadItem | undefined;
}
/**
@@ -398,7 +400,8 @@ const processQueueItem = async (
log.debug(() => ["gen-hls", { file, uploadItem }]);
// TODO(HLS):
- const fileBlob = await fetchOriginalVideoBlob(file, uploadItem);
+ // const sourceVideo = uploadItem ?? (await downloadManager.fileStream(file));
+ const sourceVideo = await downloadManager.fileStream(file);
const { objectID, url: objectUploadURL } =
await getFilePreviewDataUploadURL(file);
@@ -421,17 +424,12 @@ const processQueueItem = async (
// it can directly upload the generated video segments.
const queryParams = new URLSearchParams({ objectUploadURL });
- const res = await writeVideoStream(
+ const { playlistToken, dimensions, videoSize } = await initiateGenerateHLS(
electron,
- "generate-hls",
- fileBlob!,
+ sourceVideo!,
queryParams,
);
- const { playlistToken, dimensions, videoSize } = GenerateHLSResult.parse(
- await res.json(),
- );
-
try {
const playlist = await readVideoStream(electron, playlistToken).then(
(res) => res.text(),
@@ -452,59 +450,6 @@ const processQueueItem = async (
}
};
-/**
- * Return a blob containing the contents of the given video file.
- *
- * The blob is either constructed using the given {@link uploadItem} if present,
- * otherwise it is downloaded from remote.
- *
- * @param file An {@link EnteFile} of type {@link FileType.video}.
- *
- * @param uploadItem If we're called during the upload process, then this will
- * be set to the {@link UploadItem} that was uploaded. This way, we can directly
- * use the on-disk file instead of needing to download the original from remote.
- */
-const fetchOriginalVideoBlob = async (
- file: EnteFile,
- uploadItem: UploadItem | undefined,
-): Promise<Blob | ReadableStream | null> =>
- uploadItem
- ? fetchOriginalVideoUploadItemBlob(file, uploadItem)
- : await downloadManager.fileStream(file);
-
-const fetchOriginalVideoUploadItemBlob = (
- _: EnteFile,
- uploadItem: UploadItem,
-) => {
- // TODO(HLS): Commented below is the implementation that the eventual
- // desktop only conversion would need to handle - the conversion logic would
- // need to move to the desktop side to allow it to handle large videos.
- //
- // Meanwhile during development, we assume we're on the happy web-only cases
- // (dragging and dropping a file). All this code is behind a development
- // feature flag, so it is not going to impact end users.
-
- if (typeof uploadItem == "string" || Array.isArray(uploadItem)) {
- throw new Error("Not implemented");
- // const { response, lastModifiedMs } = await readStream(
- // ensureElectron(),
- // uploadItem,
- // );
- // const path = typeof uploadItem == "string" ? uploadItem : uploadItem[1];
- // // This function will not be called for videos, and for images
- // // it is reasonable to read the entire stream into memory here.
- // return new File([await response.arrayBuffer()], basename(path), {
- // lastModified: lastModifiedMs,
- // });
- } else {
- if (uploadItem instanceof File) {
- return uploadItem;
- } else {
- return uploadItem.file;
- }
- }
-};
-
/**
* A semi-sibling of {@link decryptPlaylistJSON}, which does the gzip but leaves
* the encryption up to the next layer.
diff --git a/web/packages/gallery/utils/native-stream.ts b/web/packages/gallery/utils/native-stream.ts
index b0438b125b..7a8b3a5392 100644
--- a/web/packages/gallery/utils/native-stream.ts
+++ b/web/packages/gallery/utils/native-stream.ts
@@ -8,6 +8,7 @@
import type { Electron, ElectronMLWorker, ZipItem } from "ente-base/types/ipc";
import { z } from "zod";
+import type { UploadItem } from "../services/upload";
/**
* Stream the given file or zip entry from the user's local file system.
@@ -128,9 +129,11 @@ export const writeStream = async (
*
* @param video A {@link Blob} containing the video to convert.
*
- * @returns return a token that can then be passed to {@link readVideoStream} to
+ * @returns a token that can then be passed to {@link readVideoStream} to
* retrieve the converted MP4 file. This three step sequence (write/read/done)
* can then be ended by using {@link videoStreamDone}).
+ *
+ * See: [Note: Convert to MP4].
*/
export const initiateConvertToMP4 = async (
_: Electron,
@@ -143,21 +146,7 @@ export const initiateConvertToMP4 = async (
return res.text();
};
-/**
- * One of the predefined operations to perform when invoking
- * {@link writeVideoStream} or {@link readVideoStream}.
- *
- * - "convert-to-mp4" (See: [Note: Convert to MP4])
- *
- * - "generate-hls" (See: [Note: Preview variant of videos])
- */
-type VideoStreamOp = "convert-to-mp4" | "generate-hls";
-
-/**
- * The contents of the {@link Response} body returned by
- * {@link writeVideoStream} for op "generate-hls".
- */
-export const GenerateHLSResult = z.object({
+const GenerateHLSResult = z.object({
/**
* A token that can be used to passed to {@link readVideoStream} to retrieve
* the generated HLS playlist.
@@ -173,40 +162,46 @@ export const GenerateHLSResult = z.object({
videoSize: z.number(),
});
+export type GenerateHLSResult = z.infer<typeof GenerateHLSResult>;
+
/**
- * Variant of {@link writeStream} tailored for video processing operations.
+ * Initate the generation of a HLS stream, streaming the source video contents
+ * to the node side.
*
- * @param op The operation to perform on this video (the result can then be
- * later read back in via {@link readVideoStream}, and the sequence ended by
- * using {@link videoStreamDone}).
+ * This is a variant of {@link writeStream} tailored for the HLS generation. It
+ * is similar to {@link initiateConvertToMP4}, but also supports streaming
+ * {@link UploadItem}s and {@link ReadableStream}s.
*
- * @param video The video to convert, as a {@link Blob} or a
- * {@link ReadableStream}.
+ * @param _ An {@link Electron} instance, witness to the fact that we're running
+ * in the context of the desktop app. It is otherwise not used.
+ *
+ * @param video The video to convert.
*
- * @param queryParams Optional additional {@link URLSearchParams} params to pass
- * along with the request.
+ * - If we're called during the upload process, then this will be set to the
+ * {@link UploadItem} that was uploaded. This way, we can directly use the
+ * on-disk file instead of needing to download the original from remote.
*
- * @returns the successful (2xx) HTTP response received from the node side (An
- * exception is thrown otherwise). The contents of the response body are
- * dependent on the operation:
+ * - Otherwise it should be a {@link ReadableStream} of the video contents.
*
- * - "convert-to-mp4" returns a plain string which is a token that can then be
- * passed to {@link readVideoStream} to retrieve the converted MP4 file.
+ * @param queryParams Additional {@link URLSearchParams} to pass along with the
+ * request.
*
- * - "generate-hls" returns a token that can be used to retrieve the generated
- * HLS playlist, and metadata about the generated video (its byte size and
- * dimensions). See {@link GenerateHLSResult}.
+ * @returns a token that can be used to retrieve the generated HLS playlist, and
+ * metadata about the generated video (its byte size and dimensions). See {@link
+ * GenerateHLSResult.
+ *
+ * See: [Note: Preview variant of videos].
*/
-export const writeVideoStream = async (
+export const initiateGenerateHLS = async (
_: Electron,
- op: VideoStreamOp,
- video: Blob | ReadableStream,
- queryParams?: URLSearchParams,
-): Promise<Response> => {
- let url = `stream://video?op=${op}`;
- if (queryParams) url += `&${queryParams.toString()}`;
+ video: UploadItem | ReadableStream,
+ queryParams: URLSearchParams,
+): Promise<GenerateHLSResult> => {
+ // TODO(HLS):
+ if (!(video instanceof ReadableStream)) throw new Error("TODO(HLS)");
- const req = new Request(url, {
+ const url = `stream://video?op=generate-hls&${queryParams.toString()}`;
+ const res = await fetch(url, {
method: "POST",
// The duplex option is required when body is a stream.
//
@@ -216,12 +211,10 @@ export const writeVideoStream = async (
duplex: "half",
body: video,
});
-
- const res = await fetch(req);
if (!res.ok)
throw new Error(`Failed to write stream to ${url}: HTTP ${res.status}`);
- return res;
+ return GenerateHLSResult.parse(await res.json());
};
/**