Skip to content

Commit 5eae3f0

Browse files
committed
fix(aws-lambda): the audio task keeps a worker free and runs only when the plan left audio out
1 parent 9bc8cf4 commit 5eae3f0

6 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎examples/aws-lambda/template.yaml‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ Resources:
324324
MaxChunkConcurrency.$: "$.Payload.MaxChunkConcurrency"
325325
Format.$: "$.Payload.Format"
326326
HasAudio.$: "$.Payload.HasAudio"
327+
SeparateAudio.$: "$.Payload.SeparateAudio"
327328
ResultPath: $.Plan
328329
Retry:
329330
- ErrorEquals:
@@ -586,8 +587,22 @@ Resources:
586587
MaxDelaySeconds: 60
587588
End: true
588589
End: true
589-
- StartAt: Audio
590+
- StartAt: AudioNeeded
590591
States:
592+
AudioNeeded:
593+
Type: Choice
594+
Choices:
595+
- Variable: $.Plan.SeparateAudio
596+
BooleanEquals: true
597+
Next: Audio
598+
Default: NoAudio
599+
NoAudio:
600+
Type: Pass
601+
Result:
602+
HasAudio: false
603+
AudioS3Uri: null
604+
ResultPath: $.Audio
605+
End: true
591606
Audio:
592607
# The plan left its audio out: mix it here while the chunks render.
593608
Type: Task

‎packages/aws-lambda/src/cdk/HyperframesRenderStack.snapshot.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ describe("HyperframesRenderStack — snapshot", () => {
270270
"Audio.$": "$[1].Audio",
271271
});
272272
expect(sentPayload(renderV2Branch(definition, "Audio"))).toMatchObject({ Action: "audio" });
273+
// Only a plan that left its audio out runs the task; any other skips its Lambda.
274+
expect(renderV2Branch(definition, "AudioNeeded")).toMatchObject({
275+
Type: "Choice",
276+
Choices: [{ Variable: "$.Plan.SeparateAudio", BooleanEquals: true, Next: "Audio" }],
277+
Default: "NoAudio",
278+
});
279+
expect(renderV2Branch(definition, "NoAudio")).toMatchObject({
280+
Type: "Pass",
281+
Result: { HasAudio: false, AudioS3Uri: null },
282+
ResultPath: "$.Audio",
283+
End: true,
284+
});
285+
expect(requireRecord(definition.States.PlanV2, "PlanV2").ResultSelector).toMatchObject({
286+
"SeparateAudio.$": "$.Payload.SeparateAudio",
287+
});
273288
expect(sentPayload(definition.States.AssembleV2)).toMatchObject({
274289
"ChunkS3Uris.$": "$.Render.Chunks[*].ChunkS3Uri",
275290
});

‎packages/aws-lambda/src/cdk/HyperframesRenderStack.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export class HyperframesRenderStack extends Construct {
318318
"MaxChunkConcurrency.$": "$.Payload.MaxChunkConcurrency",
319319
"Format.$": "$.Payload.Format",
320320
"HasAudio.$": "$.Payload.HasAudio",
321+
"SeparateAudio.$": "$.Payload.SeparateAudio",
321322
},
322323
resultPath: "$.Plan",
323324
});
@@ -525,12 +526,20 @@ export class HyperframesRenderStack extends Construct {
525526
backoffRate: 2,
526527
maxDelay: Duration.seconds(60),
527528
});
529+
const audioNeeded = new sfn.Choice(this, "AudioNeeded")
530+
.when(sfn.Condition.booleanEquals("$.Plan.SeparateAudio", true), audio)
531+
.otherwise(
532+
new sfn.Pass(this, "NoAudio", {
533+
result: sfn.Result.fromObject({ HasAudio: false, AudioS3Uri: null }),
534+
resultPath: "$.Audio",
535+
}),
536+
);
528537
const renderV2 = new sfn.Parallel(this, "RenderV2", {
529538
resultSelector: { "Chunks.$": "$[0].Chunks", "Audio.$": "$[1].Audio" },
530539
resultPath: "$.Render",
531540
})
532541
.branch(renderChunksV2)
533-
.branch(audio);
542+
.branch(audioNeeded);
534543

535544
const selectWorkerProtocol = new sfn.Choice(this, "SelectWorkerProtocol")
536545
.when(sfn.Condition.stringEquals("$.Plan.PlanProtocol", "v2"), renderV2.next(assembleV2))

‎packages/aws-lambda/src/events.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ export interface PlanV2LambdaResult extends PlanLambdaResultBase {
196196
PlanProtocol: "v2";
197197
PlanV2ManifestS3Uri: string;
198198
PlanV2ArtifactS3Prefix: string;
199+
/** The plan left its audio to the audio task, which has a worker kept free for it. */
200+
SeparateAudio: boolean;
199201
}
200202

201203
export type PlanLambdaResult = PlanV1LambdaResult | PlanV2LambdaResult;
202204

203-
/** Result of a `renderChunk` invocation. Sized ≤200 bytes per §2.4. */
204205
export interface AudioLambdaResult {
205206
Action: "audio";
206207
HasAudio: boolean;
@@ -209,6 +210,7 @@ export interface AudioLambdaResult {
209210
DurationMs: number;
210211
}
211212

213+
/** Result of a `renderChunk` invocation. Sized ≤200 bytes per §2.4. */
212214
export interface RenderChunkLambdaResult {
213215
Action: "renderChunk";
214216
ChunkS3Uri: string;

‎packages/aws-lambda/src/handler.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ describe("handler dispatch", () => {
10121012
expect(url.searchParams.get("X-Amz-Signature")).toMatch(/^[a-f0-9]{64}$/);
10131013
});
10141014

1015-
async function planStitched(chunkCount: number, separateAudio = false) {
1015+
async function planStitched(chunkCount: number, separateAudio = false, workerConcurrency = 16) {
10161016
const tmpRoot = makeTmpRoot();
10171017
const s3 = new FakeS3Client();
10181018
s3.objects.set("s3://bucket/project.tar.gz", await makeMinimalProjectTar());
@@ -1049,7 +1049,7 @@ describe("handler dispatch", () => {
10491049
Action: "plan",
10501050
ProjectS3Uri: "s3://bucket/project.tar.gz",
10511051
PlanOutputS3Prefix: "s3://bucket/renders/f/",
1052-
Config: { fps: 30, width: 640, height: 360, format: "mp4" },
1052+
Config: { fps: 30, width: 640, height: 360, format: "mp4", workerConcurrency },
10531053
},
10541054
deps,
10551055
);
@@ -1069,9 +1069,20 @@ describe("handler dispatch", () => {
10691069
},
10701070
deps,
10711071
);
1072-
return { s3, assembleMock, assemble, planV2WithPublisherMock };
1072+
return { s3, assembleMock, assemble, planV2WithPublisherMock, planned };
10731073
}
10741074

1075+
it("keeps a worker free for the audio task, and keeps the audio in a plan with one worker", async () => {
1076+
const planOptions = (run: Awaited<ReturnType<typeof planStitched>>) =>
1077+
(run.planV2WithPublisherMock.mock.calls[0] as unknown[])[3];
1078+
const four = await planStitched(8, true, 4);
1079+
expect(planOptions(four)).toMatchObject({ deferAudio: true });
1080+
expect([four.planned.SeparateAudio, four.planned.MaxChunkConcurrency]).toEqual([true, 3]);
1081+
const one = await planStitched(8, false, 1);
1082+
expect(planOptions(one)).toMatchObject({ deferAudio: false });
1083+
expect([one.planned.SeparateAudio, one.planned.MaxChunkConcurrency]).toEqual([false, 1]);
1084+
});
1085+
10751086
it.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)(
10761087
"stitches mp4 chunks inside S3 instead of joining them locally",
10771088
async () => {

‎packages/aws-lambda/src/handler.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,14 @@ async function handlePlanV2(
520520
planOutputS3Prefix: event.PlanOutputS3Prefix,
521521
temporaryRoot: work,
522522
});
523+
// The audio task shares the chunks' workers: it runs beside them only with one kept free.
524+
const separateAudio =
525+
config.workerConcurrency > 1 && plansSeparateAudio(config, { deferAudio: true });
523526
const manifest: PlanV2Manifest = await primitive(projectDir, config, publisher, {
524527
stagingParentDir: work,
525528
remoteFiles,
526529
chunksCarryAudio: true,
527-
deferAudio: true,
530+
deferAudio: separateAudio,
528531
});
529532

530533
return {
@@ -534,14 +537,18 @@ async function handlePlanV2(
534537
PlanV2ArtifactS3Prefix: publisher.artifactPrefix,
535538
PlanHash: manifest.planHash,
536539
ChunkCount: manifest.chunkCount,
537-
MaxChunkConcurrency: Math.min(config.workerConcurrency, manifest.chunkCount),
540+
MaxChunkConcurrency: Math.min(
541+
config.workerConcurrency - (separateAudio ? 1 : 0),
542+
manifest.chunkCount,
543+
),
538544
TotalFrames: manifest.totalFrames,
539545
Fps: manifest.fps,
540546
Width: manifest.width,
541547
Height: manifest.height,
542548
Format: manifest.format,
543549
HasAudio: manifest.artifacts.some((artifact) => isPlanAudioArtifactPath(artifact.path)),
544550
AudioS3Uri: null,
551+
SeparateAudio: separateAudio,
545552
FfmpegVersion: manifest.ffmpegVersion,
546553
ProducerVersion: manifest.producerVersion,
547554
DurationMs: Date.now() - started,

0 commit comments

Comments
 (0)