Skip to content

🎥 Make videos programmatically with React

stream when chunk is complete

Jonny Burger95e882f5baec
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index a065dd9435..292e818e87 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -10,7 +10,6 @@ import {
renderMetadataKey,
rendersPrefix,
} from '../../shared/constants';
-import {parseLambdaChunkKey} from '../../shared/parse-chunk-key';
import {truthy} from '../../shared/truthy';
import {calculateChunkTimes} from './calculate-chunk-times';
import {estimatePriceFromBucket} from './calculate-price-from-bucket';
@@ -243,20 +242,14 @@ export const getProgress = async ({
renderMetadata,
});
- const chunkCount = outputFile
- ? renderMetadata?.totalChunks ?? 0
- : chunks.length / chunkMultiplier;
-
- const availableChunks = chunks.map((c) =>
- parseLambdaChunkKey(c.Key as string),
- );
+ const chunkCount = overallProgress?.chunks.length ?? 0;
const missingChunks = renderMetadata
? new Array(renderMetadata.totalChunks)
.fill(true)
.map((_, i) => i)
.filter((index) => {
- return !availableChunks.find((c) => c.chunk === index);
+ return !(overallProgress?.chunks ?? []).find((c) => c === index);
})
: null;
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index bbe8fe6e8b..9d0cb8fda5 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -363,6 +363,7 @@ const innerLaunchHandler = async ({
mkdirSync(outdir);
const files: string[] = [];
+ const chunksCompleted: number[] = [];
// TODO: Now this will wait for the render to complete
await Promise.all(
@@ -407,6 +408,11 @@ const innerLaunchHandler = async ({
);
writeFileSync(filename, message.payload);
files.push(filename);
+ } else if (message.type === 'chunk-complete') {
+ chunksCompleted.push(payload.chunk);
+ overallProgress.setChunks(chunksCompleted);
+ } else {
+ throw new Error(`Unknown message type ${message.type}`);
}
},
});
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 5e13983675..8337266547 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -314,11 +314,16 @@ const renderHandler = async ({
});
}
+ onStream({
+ type: 'chunk-complete',
+ payload: {},
+ });
+
const writeStart = Date.now();
RenderInternals.Log.verbose(
{indent: false, logLevel: params.logLevel},
- `Wrote chunk to S3 (${Date.now() - writeStart}ms)`,
+ `Streamed chunk to main function (${Date.now() - writeStart}ms)`,
);
RenderInternals.Log.verbose(
{indent: false, logLevel: params.logLevel},
@@ -415,6 +420,7 @@ export const rendererHandler = async (
renderId: params.renderId,
expectedBucketOwner: options.expectedBucketOwner,
});
+ // TODO: This does not stream anymore
if (willRetry) {
const retryPayload: LambdaPayloads[LambdaRoutines.renderer] = {
...params,
diff --git a/packages/lambda/src/functions/streaming/streaming.ts b/packages/lambda/src/functions/streaming/streaming.ts
index 45f533b145..3b1bef9ad8 100644
--- a/packages/lambda/src/functions/streaming/streaming.ts
+++ b/packages/lambda/src/functions/streaming/streaming.ts
@@ -6,6 +6,7 @@ const errorOccurred = 'error-occurred' as const;
const renderIdDetermined = 'render-id-determined' as const;
const videoChunkRendered = 'video-chunk-rendered' as const;
const audioChunkRendered = 'audio-chunk-rendered' as const;
+const chunkComplete = 'chunk-complete' as const;
const stillRendered = 'still-rendered' as const;
const messageTypes = {
@@ -15,6 +16,7 @@ const messageTypes = {
'4': {type: videoChunkRendered},
'5': {type: audioChunkRendered},
'6': {type: stillRendered},
+ '7': {type: chunkComplete},
} as const;
export type MessageTypeId = keyof typeof messageTypes;
@@ -27,6 +29,7 @@ export const formatMap: {[key in MessageType]: 'json' | 'binary'} = {
[videoChunkRendered]: 'binary',
[audioChunkRendered]: 'binary',
[stillRendered]: 'json',
+ [chunkComplete]: 'json',
};
export type StreamingPayload =
@@ -60,6 +63,10 @@ export type StreamingPayload =
| {
type: typeof stillRendered;
payload: RenderStillLambdaResponsePayload;
+ }
+ | {
+ type: typeof chunkComplete;
+ payload: {};
};
export const messageTypeIdToMessageType = (
diff --git a/packages/lambda/src/shared/parse-chunk-key.ts b/packages/lambda/src/shared/parse-chunk-key.ts
deleted file mode 100644
index fe248ffeb6..0000000000
--- a/packages/lambda/src/shared/parse-chunk-key.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-export const parseLambdaChunkKey = (key: string) => {
- const match = key.match(/chunk:([0-9]+):(video|audio)$/);
- if (!match) {
- throw new Error(
- `Cannot parse filename ${key} into timing information. Malformed data.`,
- );
- }
-
- return {
- chunk: Number(match[1]),
- type: match[2],
- };
-};
diff --git a/packages/lambda/src/test/unit/lambda-chunk-key.test.ts b/packages/lambda/src/test/unit/lambda-chunk-key.test.ts
deleted file mode 100644
index 91e6c7eb70..0000000000
--- a/packages/lambda/src/test/unit/lambda-chunk-key.test.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import {expect, test} from 'vitest';
-import {chunkKeyForIndex} from '../../defaults';
-import {parseLambdaChunkKey} from '../../shared/parse-chunk-key';
-
-test('Should be able to parse Lambda key', () => {
- expect(
- parseLambdaChunkKey(
- chunkKeyForIndex({
- index: 1111,
- type: 'video',
- }),
- ),
- ).toEqual({
- chunk: 1111,
- type: 'video',
- });
-});