Skip to content

🎥 Make videos programmatically with React

onstrean

Jonny Burger82da3b4c23c9
diff --git a/packages/lambda/src/functions/helpers/streaming-payloads.ts b/packages/lambda/src/functions/helpers/streaming-payloads.ts
deleted file mode 100644
index 2161ccb064..0000000000
--- a/packages/lambda/src/functions/helpers/streaming-payloads.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import {z} from 'zod';
-import type {ResponseStream} from './streamify-response';
-
-const streamingPayloadSchema = z.discriminatedUnion('type', [
- z.object({
- type: z.literal('render-id-determined'),
- renderId: z.string(),
- }),
-]);
-
-export type StreamingPayloads = z.infer<typeof streamingPayloadSchema>;
-
-export const isStreamingPayload = (str: string) => {
- try {
- const parsed = JSON.parse(str) as unknown;
- return streamingPayloadSchema.parse(parsed);
- } catch {
- return false;
- }
-};
-
-export const sendProgressEvent = (
- responseStream: ResponseStream,
- payload: StreamingPayloads,
-) => {
- const stringified = JSON.stringify(payload);
- responseStream.write(stringified);
-};
diff --git a/packages/lambda/src/functions/index.ts b/packages/lambda/src/functions/index.ts
index 94320577eb..268dadaa45 100644
--- a/packages/lambda/src/functions/index.ts
+++ b/packages/lambda/src/functions/index.ts
@@ -14,14 +14,13 @@ import {printCloudwatchHelper} from './helpers/print-cloudwatch-helper';
import type {RequestContext} from './helpers/request-context';
import type {ResponseStream} from './helpers/streamify-response';
import {streamifyResponse} from './helpers/streamify-response';
-import type {StreamingPayloads} from './helpers/streaming-payloads';
-import {sendProgressEvent} from './helpers/streaming-payloads';
import {infoHandler} from './info';
import {launchHandler} from './launch';
import {progressHandler} from './progress';
import {rendererHandler} from './renderer';
import {startHandler} from './start';
import {stillHandler} from './still';
+import type {OnStream} from './streaming/streaming';
import {makePayloadMessage} from './streaming/streaming';
const sendResponse = ({
@@ -44,9 +43,11 @@ const innerHandler = async ({
params,
responseStream,
context,
+ onStream,
}: {
params: LambdaPayload;
responseStream: ResponseStream;
+ onStream: OnStream;
context: RequestContext;
}): Promise<void> => {
setCurrentRequestId(context.awsRequestId);
@@ -83,20 +84,16 @@ const innerHandler = async ({
params.logLevel,
);
- const renderIdDetermined: StreamingPayloads = {
+ await onStream({
type: 'render-id-determined',
- renderId,
- };
- sendProgressEvent(responseStream, renderIdDetermined);
+ payload: {renderId},
+ });
- const response = await stillHandler({
+ await stillHandler({
expectedBucketOwner: currentUserId,
params,
renderId,
- });
- sendResponse({
- responseStream,
- response,
+ onStream,
});
return;
@@ -246,9 +243,10 @@ const routine = async (
params: LambdaPayload,
responseStream: ResponseStream,
context: RequestContext,
+ onStream: OnStream,
): Promise<void> => {
try {
- await innerHandler({params, responseStream, context});
+ await innerHandler({params, responseStream, context, onStream});
} catch (err) {
const res: OrError<0> = {
type: 'error',
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index d3afa445c8..655301ebc5 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -189,7 +189,7 @@ const renderHandler = async ({
imageFormat: params.imageFormat,
serializedInputPropsWithCustomSchema,
frameRange: params.frameRange,
- onProgress: ({renderedFrames, encodedFrames, stitchStage}) => {
+ onProgress: async ({renderedFrames, encodedFrames, stitchStage}) => {
if (renderedFrames % 5 === 0) {
RenderInternals.Log.info(
{indent: false, logLevel: params.logLevel},
diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.ts
index 4fce665eec..6d4e0d2ccc 100644
--- a/packages/lambda/src/functions/still.ts
+++ b/packages/lambda/src/functions/still.ts
@@ -44,17 +44,20 @@ import {
getTmpDirStateIfENoSp,
writeLambdaError,
} from './helpers/write-lambda-error';
+import type {OnStream} from './streaming/streaming';
type Options = {
params: LambdaPayload;
renderId: string;
expectedBucketOwner: string;
+ onStream: OnStream;
};
const innerStillHandler = async ({
params: lambdaParams,
expectedBucketOwner,
renderId,
+ onStream,
}: Options) => {
if (lambdaParams.type !== LambdaRoutines.still) {
throw new TypeError('Expected still type');
@@ -271,19 +274,22 @@ const innerStillHandler = async ({
customCredentials,
);
- return {
- type: 'success' as const,
- output: url,
- size,
- sizeInBytes: size,
- bucketName,
- estimatedPrice: formatCostsInfo(estimatedPrice),
- renderId,
- outKey,
- };
+ await onStream({
+ type: 'still-rendered',
+ payload: {
+ type: 'success' as const,
+ output: url,
+ size,
+ sizeInBytes: size,
+ bucketName,
+ estimatedPrice: formatCostsInfo(estimatedPrice),
+ renderId,
+ outKey,
+ },
+ });
};
-type RenderStillLambdaResponsePayload = {
+export type RenderStillLambdaResponsePayload = {
type: 'success';
output: string;
outKey: string;
@@ -294,9 +300,7 @@ type RenderStillLambdaResponsePayload = {
renderId: string;
};
-export const stillHandler = async (
- options: Options,
-): Promise<RenderStillLambdaResponsePayload> => {
+export const stillHandler = async (options: Options): Promise<void> => {
const {params} = options;
if (params.type !== LambdaRoutines.still) {
diff --git a/packages/lambda/src/functions/streaming/streaming.ts b/packages/lambda/src/functions/streaming/streaming.ts
index 59f60b1a50..6ac251da52 100644
--- a/packages/lambda/src/functions/streaming/streaming.ts
+++ b/packages/lambda/src/functions/streaming/streaming.ts
@@ -1,9 +1,12 @@
+import type {RenderStillLambdaResponsePayload} from '../still';
+
const framesRendered = 'frames-rendered' as const;
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 responseJson = 'response-json' as const;
+const stillRendered = 'still-rendered' as const;
const messageTypes = {
'1': {type: framesRendered},
@@ -12,6 +15,7 @@ const messageTypes = {
'4': {type: videoChunkRendered},
'5': {type: audioChunkRendered},
'6': {type: responseJson},
+ '7': {type: stillRendered},
} as const;
type MessageTypeId = keyof typeof messageTypes;
@@ -24,6 +28,7 @@ export const formatMap: {[key in MessageType]: 'json' | 'binary'} = {
[responseJson]: 'json',
[videoChunkRendered]: 'binary',
[audioChunkRendered]: 'binary',
+ [stillRendered]: 'json',
};
export type StreamingPayload =
@@ -56,6 +61,10 @@ export type StreamingPayload =
| {
type: typeof responseJson;
payload: Record<string, unknown>;
+ }
+ | {
+ type: typeof stillRendered;
+ payload: RenderStillLambdaResponsePayload;
};
export const messageTypeIdToMessage = (