Skip to content

armgit / ente

Public Imported from GitHub
0

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

Web side

Manav Rathi6614e4468dbb
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index b595517f5c..760e7408c2 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -1,12 +1,9 @@
import { decryptBlob } from "ente-base/crypto";
import type { EncryptedBlob } from "ente-base/crypto/types";
-import {
- retryEnsuringHTTPOkOr4xx,
- type PublicAlbumsCredentials,
-} from "ente-base/http";
+import { type PublicAlbumsCredentials } from "ente-base/http";
import log from "ente-base/log";
import type { Electron } from "ente-base/types/ipc";
-import type { EnteFile } from "ente-media/file";
+import { fileLogID, type EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { settingsSnapshot } from "ente-new/photos/services/settings";
import { gunzip, gzip } from "ente-new/photos/utils/gzip";
@@ -406,46 +403,52 @@ const processQueueItem = async (
const { objectID, url: objectUploadURL } =
await getFilePreviewDataUploadURL(file);
- const res = await writeVideoStream(electron, "generate-hls", fileBlob!);
- const { playlistToken, videoToken, dimensions, videoSize } =
- GenerateHLSResult.parse(await res.json());
+ log.info(`HLS gen ${fileLogID(file)} | start`);
+
+ // [Note: Upload HLS video segment from node side]
+ //
+ // The generated video can be huge (multi-GB), too large to read it into
+ // memory as an arrayBuffer.
+ //
+ // One option was to chain the video stream response (from the node side)
+ // directly into a fetch request to `objectUploadURL`, however that requires
+ // HTTP/2 (our servers support it, but self hosters' might not). Also that
+ // approach won't work with retries on transient failures unless we
+ // duplicate the stream beforehand, which invalidates the point of
+ // streaming.
+ //
+ // So instead we provide the presigned upload URL to the node side so that
+ // it can directly upload the generated video segments.
+ const queryParams = new URLSearchParams(objectUploadURL);
+
+ const res = await writeVideoStream(
+ electron,
+ "generate-hls",
+ fileBlob!,
+ queryParams,
+ );
+
+ const { playlistToken, dimensions, videoSize } = GenerateHLSResult.parse(
+ await res.json(),
+ );
try {
- const playlistBlob = await readVideoStream(
- electron,
- playlistToken,
- ).then((res) => res.blob());
-
- const videoRes = await readVideoStream(electron, videoToken);
-
- // Video conversion is non-trivial effort, and the video segment file
- // we're uploading is potentially very large, so add retries to avoid
- // the need to redo everything in case of transient errors.
- await retryEnsuringHTTPOkOr4xx(() =>
- fetch(objectUploadURL, {
- method: "PUT",
- // Need to specify the duplex option since body is a stream.
- //
- // @ts-expect-error TypeScript's libdom.d.ts does not include
- // the duplex option.
- duplex: "half",
- body: videoRes.clone().body
- }),
+ const playlist = await readVideoStream(electron, playlistToken).then(
+ (res) => res.text(),
);
const playlistData = await encodePlaylistJSON({
type: "hls_video",
- playlist: await playlistBlob.text(),
+ playlist,
...dimensions,
size: videoSize,
});
await putVideoData(file, playlistData, objectID, videoSize);
+
+ log.info(`HLS gen ${fileLogID(file)} | done`);
} finally {
- await Promise.all([
- videoStreamDone(electron, playlistToken),
- videoStreamDone(electron, videoToken),
- ]);
+ await Promise.all([videoStreamDone(electron, playlistToken)]);
}
};
diff --git a/web/packages/gallery/utils/native-stream.ts b/web/packages/gallery/utils/native-stream.ts
index 450e34e7e6..59a7ac997a 100644
--- a/web/packages/gallery/utils/native-stream.ts
+++ b/web/packages/gallery/utils/native-stream.ts
@@ -138,11 +138,6 @@ export const GenerateHLSResult = z.object({
* the generated HLS playlist.
*/
playlistToken: z.string(),
- /**
- * A token that can be used to passed to {@link readVideoStream} to retrieve
- * the video segments.
- */
- videoToken: z.string(),
/**
* The dimensions (width and height in pixels) of the generated video stream.
*/
@@ -163,6 +158,9 @@ export const GenerateHLSResult = z.object({
* @param video The video to convert, as a {@link Blob} or a
* {@link ReadableStream}.
*
+ * @param queryParams Optional additional {@link URLSearchParams} params to pass
+ * along with the request.
+ *
* @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:
@@ -170,17 +168,20 @@ export const GenerateHLSResult = z.object({
* - "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.
*
- * - "generate-hls" returns two tokens, first one that can be used to retrieve
- * the generated HLS playlist, and the second one that can be used to retrieve
- * the video (segments). It also returns the dimensions of the generated
- * video. See {@link GenerateHLSResult}.
+ * - "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}.
*/
export const writeVideoStream = async (
_: Electron,
op: VideoStreamOp,
video: Blob | ReadableStream,
+ queryParams?: URLSearchParams,
): Promise<Response> => {
- const url = `stream://video?op=${op}`;
+ let url = `stream://video?op=${op}`;
+ if (queryParams) {
+ url += `&${queryParams.toString()}`;
+ }
const req = new Request(url, {
method: "POST",