feat(aws-lambda): a cloud render mixes its audio beside its chunks (#5997)

* feat(aws-lambda): a cloud render mixes its audio beside its chunks

* fix(aws-lambda): the audio task keeps a worker free and runs only when the plan left audio out

* test(aws-lambda): the audio task's free worker holds for a reserved pool of 1 and 2

* refactor(aws-lambda): one helper points every handler at the bundled Chrome
This commit is contained in:
Miguel Ángel
2026-10-21 05:26:58 +00:00
committed by GitHub
parent b16d5a7063
commit dee8380d22
10 changed files with 558 additions and 156 deletions
+149 -8
View File
@@ -1012,7 +1012,11 @@ describe("handler dispatch", () => {
expect(url.searchParams.get("X-Amz-Signature")).toMatch(/^[a-f0-9]{64}$/);
});
async function planStitched(chunkCount: number) {
async function planStitched(
chunkCount: number,
separateAudio = false,
workerConcurrency?: number,
) {
const tmpRoot = makeTmpRoot();
const s3 = new FakeS3Client();
s3.objects.set("s3://bucket/project.tar.gz", await makeMinimalProjectTar());
@@ -1025,7 +1029,11 @@ describe("handler dispatch", () => {
const planV2WithPublisherMock = mock(
async (_dir: string, _config: unknown, publisher: PlanV2ArtifactPublisher) => {
const v1Dir = join(tmpRoot, "v1");
makeMinimalV1PlanDir(v1Dir, true, { chunkCount, chunksCarryAudio: true });
makeMinimalV1PlanDir(v1Dir, !separateAudio, {
chunkCount,
chunksCarryAudio: !separateAudio,
separateAudio,
});
return publishPlanV2FromExecutionPlan(v1Dir, publisher);
},
);
@@ -1045,13 +1053,13 @@ describe("handler dispatch", () => {
Action: "plan",
ProjectS3Uri: "s3://bucket/project.tar.gz",
PlanOutputS3Prefix: "s3://bucket/renders/f/",
Config: { fps: 30, width: 640, height: 360, format: "mp4" },
Config: { fps: 30, width: 640, height: 360, format: "mp4", workerConcurrency },
},
deps,
);
if (!("PlanProtocol" in planned) || planned.PlanProtocol !== "v2")
throw new Error("expected v2");
const assemble = (chunkS3Uris: string[]) =>
const assemble = (chunkS3Uris: string[], audioS3Uri: string | null = null) =>
handler(
{
Action: "assemble",
@@ -1059,15 +1067,42 @@ describe("handler dispatch", () => {
PlanV2ArtifactS3Prefix: planned.PlanV2ArtifactS3Prefix,
PlanHash: planned.PlanHash,
ChunkS3Uris: chunkS3Uris,
AudioS3Uri: null,
AudioS3Uri: audioS3Uri,
OutputS3Uri: "s3://bucket/renders/f/output.mp4",
Format: "mp4",
},
deps,
);
return { s3, assembleMock, assemble };
return { s3, assembleMock, assemble, planV2WithPublisherMock, planned };
}
it("keeps a worker free for the audio task, and keeps the audio in a plan with one worker", async () => {
const planOptions = (run: Awaited<ReturnType<typeof planStitched>>) =>
(run.planV2WithPublisherMock.mock.calls[0] as unknown[])[3];
const four = await planStitched(8, true, 4);
expect(planOptions(four)).toMatchObject({ deferAudio: true });
expect([four.planned.SeparateAudio, four.planned.MaxChunkConcurrency]).toEqual([true, 3]);
const one = await planStitched(8, false, 1);
expect(planOptions(one)).toMatchObject({ deferAudio: false });
expect([one.planned.SeparateAudio, one.planned.MaxChunkConcurrency]).toEqual([false, 1]);
for (const [reserved, separate, chunks] of [
["2", true, 1],
["1", false, 1],
] as const) {
process.env.HYPERFRAMES_LAMBDA_RESERVED_CONCURRENCY = reserved;
try {
const run = await planStitched(8, separate, undefined);
expect(planOptions(run)).toMatchObject({ deferAudio: separate });
expect([run.planned.SeparateAudio, run.planned.MaxChunkConcurrency]).toEqual([
separate,
chunks,
]);
} finally {
delete process.env.HYPERFRAMES_LAMBDA_RESERVED_CONCURRENCY;
}
}
});
it.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)(
"stitches mp4 chunks inside S3 instead of joining them locally",
async () => {
@@ -1096,6 +1131,55 @@ describe("handler dispatch", () => {
},
);
it.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)(
"stitches the audio task's track in when the plan left its audio out",
async () => {
const { s3, assemble, planV2WithPublisherMock } = await planStitched(2, true);
expect((planV2WithPublisherMock.mock.calls[0] as unknown[])[3]).toMatchObject({
chunksCarryAudio: true,
deferAudio: true,
});
const ffmpeg = (args: string[], path: string) => {
const run = spawnSync("ffmpeg", ["-nostdin", "-loglevel", "error", ...args, "-y", path]);
if (run.status !== 0) throw new Error(String(run.stderr));
return readFileSync(path);
};
const chunks = [0, 1].map((index) => {
const uri = `s3://bucket/renders/f/chunks/000${index}.mp4`;
const lavfi = ["-f", "lavfi", "-i", "testsrc2=size=64x48:rate=30:duration=1"];
s3.objects.set(
uri,
ffmpeg([...lavfi, "-c:v", "libx264", "-bf", "0"], join(makeTmpRoot(), "chunk.mp4")),
);
return uri;
});
const audioUri = "s3://bucket/renders/f/audio/audio.m4a";
s3.objects.set(
audioUri,
ffmpeg(
["-f", "lavfi", "-i", "sine=duration=2", "-c:a", "aac"],
join(makeTmpRoot(), "a.m4a"),
),
);
await assemble(chunks, audioUri);
const outputPath = join(makeTmpRoot(), "out.mp4");
writeFileSync(outputPath, s3.objects.get("s3://bucket/renders/f/output.mp4")!);
const probe = spawnSync(
"ffprobe",
["-v", "error", "-show_entries", "stream=codec_type", "-of", "csv=p=0", "--", outputPath],
{ encoding: "utf-8" },
);
expect(probe.stdout.trim().split("\n").sort()).toEqual(["audio", "video"]);
},
);
it("refuses an audio URI for a v2 plan that ships its own audio", async () => {
const { assemble } = await planStitched(1);
await expect(
assemble(["s3://bucket/renders/f/chunks/0000.mp4"], "s3://bucket/renders/f/audio/audio.m4a"),
).rejects.toMatchObject({ name: "PLAN_PROTOCOL_UNSUPPORTED" });
});
it("rejects unknown plan protocol values", async () => {
const tmpRoot = makeTmpRoot();
const s3 = new FakeS3Client();
@@ -1158,6 +1242,55 @@ describe("handler dispatch", () => {
});
});
describe("handler — audio", () => {
async function runAudio(config: Record<string, unknown>) {
const tmpRoot = makeTmpRoot();
const s3 = new FakeS3Client();
s3.objects.set("s3://bucket/project.tar.gz", await makeMinimalProjectTar());
const buildPlanAudio = mock(
async (_project: string, _config: unknown, _work: string, outputPath: string) => {
writeFileSync(outputPath, "AAC");
return { hasAudio: true };
},
);
const result = await handler(
{
Action: "audio",
ProjectS3Uri: "s3://bucket/project.tar.gz",
PlanOutputS3Prefix: "s3://bucket/renders/f/",
Config: { fps: 30, width: 640, height: 360, format: "mp4", ...config },
} as LambdaEvent,
{
s3: s3 as unknown as import("@aws-sdk/client-s3").S3Client,
primitives: { buildPlanAudio } as unknown as NonNullable<
Parameters<typeof handler>[1]
>["primitives"],
tmpRoot,
skipChromeResolution: true,
},
);
return { result, s3, buildPlanAudio };
}
it("mixes a plan's deferred audio and uploads it beside the plan", async () => {
const { result, s3, buildPlanAudio } = await runAudio({});
expect(result).toMatchObject({
Action: "audio",
HasAudio: true,
AudioS3Uri: "s3://bucket/renders/f/audio/audio.m4a",
});
expect(s3.objects.get("s3://bucket/renders/f/audio/audio.m4a")?.toString()).toBe("AAC");
expect(buildPlanAudio).toHaveBeenCalledTimes(1);
});
it("leaves the audio to the plan when the stitch cannot take it (cfr)", async () => {
const { result, s3, buildPlanAudio } = await runAudio({ cfr: true });
expect(result).toMatchObject({ Action: "audio", HasAudio: false, AudioS3Uri: null });
expect(buildPlanAudio).not.toHaveBeenCalled();
expect(s3.ops).toHaveLength(0);
});
});
/** A real client (so it can pre-sign offline) whose requests go to the fake. */
function presigningClient(fake: FakeS3Client): import("@aws-sdk/client-s3").S3Client {
const client = new S3Client({
@@ -1398,7 +1531,12 @@ async function makeMinimalPlanTar(): Promise<Buffer> {
function makeMinimalV1PlanDir(
dir: string,
withAudio: boolean,
options: { chunkCount?: number; chunksCarryAudio?: boolean; withDeferredVideo?: boolean } = {},
options: {
chunkCount?: number;
chunksCarryAudio?: boolean;
separateAudio?: boolean;
withDeferredVideo?: boolean;
} = {},
): void {
const chunkCount = options.chunkCount ?? 1;
mkdirSync(join(dir, "meta"), { recursive: true });
@@ -1426,7 +1564,10 @@ function makeMinimalV1PlanDir(
);
writeFileSync(
join(dir, "meta", "encoder.json"),
options.chunksCarryAudio ? JSON.stringify({ chunksCarryAudio: true }) : "{}",
JSON.stringify({
...(options.chunksCarryAudio ? { chunksCarryAudio: true } : {}),
...(options.separateAudio ? { separateAudio: true } : {}),
}),
);
if (withAudio) writeFileSync(join(dir, "audio.m4a"), "AAC");
if (options.withDeferredVideo) writeDeferredVideo(dir);