Skip to content

Commit 3470a34

Browse files
committed
fix(producer): a cancelled span render stops, and its real-render test runs in the integration lane
renderSpan checks the abort signal before each chunk part and before the stitch. The real-render tests move to their own file listed in the integration lane.
1 parent 2d0449d commit 3470a34

5 files changed

Lines changed: 147 additions & 93 deletions

File tree

‎packages/cli/src/commands/render.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ async function executeLocalRender(
11771177
resumeSegments: options.resumeSegments,
11781178
keepSegments: options.keepSegments,
11791179
strictness: options.bestEffort === false ? "strict" : "best-effort",
1180+
// plan() takes the film's own size; these only satisfy the request shape.
11801181
distributed: options.range ? { width: 1920, height: 1080 } : undefined,
11811182
},
11821183
});

‎packages/producer/scripts/integration-tests.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"src/services/distributed/planSizeCap.test.ts",
1717
"src/services/distributed/planV2RemoteFiles.test.ts",
1818
"src/services/distributed/renderChunk.test.ts",
19+
"src/services/distributed/renderSpan.integration.test.ts",
1920
"src/services/fileServer.test.ts",
2021
"src/services/fileServer.file-race.test.ts",
2122
"src/services/fileServer.listenError.test.ts",
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
2+
import { spawnSync } from "node:child_process";
3+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { createRenderJob, executeRenderJob, RenderCancelledError } from "../renderOrchestrator.js";
7+
import { renderSpan } from "./renderSpan.js";
8+
9+
const ROOT_HTML = `<!doctype html>
10+
<html><head><meta charset="utf-8">
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
12+
<style>html,body{margin:0;width:160px;height:90px;overflow:hidden;background:#0f172a}
13+
#main{position:relative;width:160px;height:90px}
14+
@keyframes slide{from{transform:translateX(0)}to{transform:translateX(120px)}}
15+
@keyframes ramp{from{background:#000}to{background:#fff}}
16+
#bg{position:absolute;inset:0;animation:ramp 3s linear forwards}
17+
#box{position:absolute;left:0;top:10px;width:30px;height:30px;background:#f97316;animation:slide 3s linear forwards}
18+
#sub{position:absolute;inset:0}</style></head>
19+
<body><div id="main" data-composition-id="main" data-width="160" data-height="90" data-start="0" data-duration="3">
20+
<div id="bg"></div>
21+
<div id="box"></div>
22+
<div id="sub" data-composition-id="sub" data-composition-src="compositions/sub.html" data-start="1" data-duration="1" data-track-index="1"></div>
23+
</div>
24+
<script>const tl=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["main"]=tl;tl.to({},{duration:3},0);</script>
25+
</body></html>`;
26+
27+
const SUB_HTML = `<template id="sub-template">
28+
<div id="sub-root" data-composition-id="sub" data-start="0" data-duration="1" data-width="160" data-height="90">
29+
<div id="chip"></div></div>
30+
<style>[data-composition-id="sub"]{position:absolute;inset:0;width:160px;height:90px}
31+
@keyframes grow{from{width:10px}to{width:150px}}
32+
[data-composition-id="sub"] #chip{position:absolute;left:5px;top:60px;height:20px;width:10px;background:#22c55e;animation:grow 1s linear forwards}</style>
33+
<script>const t=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["sub"]=t;t.to({},{duration:1},0);</script>
34+
</template>`;
35+
36+
function frames(path: string): Buffer[] {
37+
const out = spawnSync(
38+
"ffmpeg",
39+
["-v", "error", "-i", path, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"],
40+
{ maxBuffer: 1 << 28 },
41+
);
42+
const size = 160 * 90 * 3;
43+
const all = out.stdout;
44+
return Array.from({ length: Math.floor(all.length / size) }, (_, i) =>
45+
all.subarray(i * size, (i + 1) * size),
46+
);
47+
}
48+
49+
function meanDiff(a: Buffer, b: Buffer): number {
50+
let sum = 0;
51+
for (let i = 0; i < a.length; i++) sum += Math.abs(a[i]! - b[i]!);
52+
return sum / a.length;
53+
}
54+
55+
describe("renderSpan", () => {
56+
let root: string;
57+
let projectDir: string;
58+
59+
beforeAll(() => {
60+
root = mkdtempSync(join(tmpdir(), "hf-render-span-"));
61+
projectDir = join(root, "project");
62+
mkdirSync(join(projectDir, "compositions"), { recursive: true });
63+
writeFileSync(join(projectDir, "index.html"), ROOT_HTML);
64+
writeFileSync(join(projectDir, "compositions", "sub.html"), SUB_HTML);
65+
});
66+
67+
afterAll(() => rmSync(root, { recursive: true, force: true }));
68+
69+
it("renders the frames of the span the way a full render renders them, sub-composition included", async () => {
70+
const fullPath = join(root, "full.mp4");
71+
const job = createRenderJob({ fps: { num: 30, den: 1 }, quality: "draft", format: "mp4" });
72+
await executeRenderJob(job, projectDir, fullPath);
73+
74+
const spanPath = join(root, "span.mp4");
75+
const { framesEncoded } = await renderSpan(
76+
projectDir,
77+
{ fps: 30, width: 160, height: 90, format: "mp4", quality: "draft", hdrMode: "force-sdr" },
78+
{ startSeconds: 0.5, endSeconds: 2 },
79+
spanPath,
80+
);
81+
expect(framesEncoded).toBe(45);
82+
83+
const full = frames(fullPath);
84+
const span = frames(spanPath);
85+
expect(span.length).toBe(45);
86+
const worstMatch = Math.max(...span.map((frame, i) => meanDiff(frame, full[15 + i]!)));
87+
// The backdrop brightens about 2.8 levels a frame, so a span one frame off its place misses by more than this.
88+
expect(worstMatch).toBeLessThan(1.5);
89+
const oneFrameLate = span.map((frame, i) => meanDiff(frame, full[16 + i]!));
90+
expect(Math.min(...oneFrameLate)).toBeGreaterThan(worstMatch);
91+
}, 240_000);
92+
93+
it("stops when it is cancelled after planning, with no output written", async () => {
94+
const scratch = join(root, "scratch");
95+
mkdirSync(scratch, { recursive: true });
96+
const previousTmp = process.env.TMPDIR;
97+
process.env.TMPDIR = scratch;
98+
const controller = new AbortController();
99+
const outputPath = join(root, "cancelled.mp4");
100+
try {
101+
const running = renderSpan(
102+
projectDir,
103+
{
104+
fps: 30,
105+
width: 160,
106+
height: 90,
107+
format: "mp4",
108+
quality: "draft",
109+
hdrMode: "force-sdr",
110+
chunkSize: 15,
111+
abortSignal: controller.signal,
112+
},
113+
{ startSeconds: 0 },
114+
outputPath,
115+
);
116+
const firstPart = () =>
117+
readdirSync(scratch).some((dir) =>
118+
existsSync(join(scratch, dir, "parts", "part-0000.mp4")),
119+
);
120+
while (!firstPart()) await new Promise((done) => setTimeout(done, 20));
121+
controller.abort();
122+
await expect(running).rejects.toBeInstanceOf(RenderCancelledError);
123+
expect(existsSync(outputPath)).toBe(false);
124+
} finally {
125+
if (previousTmp === undefined) delete process.env.TMPDIR;
126+
else process.env.TMPDIR = previousTmp;
127+
}
128+
}, 240_000);
129+
});
Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
2-
import { spawnSync } from "node:child_process";
3-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4-
import { tmpdir } from "node:os";
5-
import { join } from "node:path";
6-
import { createRenderJob, executeRenderJob } from "../renderOrchestrator.js";
7-
import { renderSpan, RenderSpanUnsupportedError, spanFrameRange } from "./renderSpan.js";
1+
import { describe, expect, it } from "bun:test";
2+
import { RenderSpanUnsupportedError, spanFrameRange } from "./renderSpan.js";
83

94
describe("spanFrameRange", () => {
105
it("is the frames the seconds cover, to the end when --end is absent", () => {
@@ -21,88 +16,3 @@ describe("spanFrameRange", () => {
2116
expect(() => spanFrameRange(3, 4, 30, 90)).toThrow("90 frames at 30 fps (3s)");
2217
});
2318
});
24-
25-
const ROOT_HTML = `<!doctype html>
26-
<html><head><meta charset="utf-8">
27-
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
28-
<style>html,body{margin:0;width:160px;height:90px;overflow:hidden;background:#0f172a}
29-
#main{position:relative;width:160px;height:90px}
30-
@keyframes slide{from{transform:translateX(0)}to{transform:translateX(120px)}}
31-
@keyframes ramp{from{background:#000}to{background:#fff}}
32-
#bg{position:absolute;inset:0;animation:ramp 3s linear forwards}
33-
#box{position:absolute;left:0;top:10px;width:30px;height:30px;background:#f97316;animation:slide 3s linear forwards}
34-
#sub{position:absolute;inset:0}</style></head>
35-
<body><div id="main" data-composition-id="main" data-width="160" data-height="90" data-start="0" data-duration="3">
36-
<div id="bg"></div>
37-
<div id="box"></div>
38-
<div id="sub" data-composition-id="sub" data-composition-src="compositions/sub.html" data-start="1" data-duration="1" data-track-index="1"></div>
39-
</div>
40-
<script>const tl=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["main"]=tl;tl.to({},{duration:3},0);</script>
41-
</body></html>`;
42-
43-
const SUB_HTML = `<template id="sub-template">
44-
<div id="sub-root" data-composition-id="sub" data-start="0" data-duration="1" data-width="160" data-height="90">
45-
<div id="chip"></div></div>
46-
<style>[data-composition-id="sub"]{position:absolute;inset:0;width:160px;height:90px}
47-
@keyframes grow{from{width:10px}to{width:150px}}
48-
[data-composition-id="sub"] #chip{position:absolute;left:5px;top:60px;height:20px;width:10px;background:#22c55e;animation:grow 1s linear forwards}</style>
49-
<script>const t=gsap.timeline({paused:true});window.__timelines=window.__timelines||{};window.__timelines["sub"]=t;t.to({},{duration:1},0);</script>
50-
</template>`;
51-
52-
function frames(path: string): Buffer[] {
53-
const out = spawnSync(
54-
"ffmpeg",
55-
["-v", "error", "-i", path, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"],
56-
{ maxBuffer: 1 << 28 },
57-
);
58-
const size = 160 * 90 * 3;
59-
const all = out.stdout;
60-
return Array.from({ length: Math.floor(all.length / size) }, (_, i) =>
61-
all.subarray(i * size, (i + 1) * size),
62-
);
63-
}
64-
65-
function meanDiff(a: Buffer, b: Buffer): number {
66-
let sum = 0;
67-
for (let i = 0; i < a.length; i++) sum += Math.abs(a[i]! - b[i]!);
68-
return sum / a.length;
69-
}
70-
71-
describe("renderSpan", () => {
72-
let root: string;
73-
let projectDir: string;
74-
75-
beforeAll(() => {
76-
root = mkdtempSync(join(tmpdir(), "hf-render-span-"));
77-
projectDir = join(root, "project");
78-
mkdirSync(join(projectDir, "compositions"), { recursive: true });
79-
writeFileSync(join(projectDir, "index.html"), ROOT_HTML);
80-
writeFileSync(join(projectDir, "compositions", "sub.html"), SUB_HTML);
81-
});
82-
83-
afterAll(() => rmSync(root, { recursive: true, force: true }));
84-
85-
it("renders the frames of the span the way a full render renders them, sub-composition included", async () => {
86-
const fullPath = join(root, "full.mp4");
87-
const job = createRenderJob({ fps: { num: 30, den: 1 }, quality: "draft", format: "mp4" });
88-
await executeRenderJob(job, projectDir, fullPath);
89-
90-
const spanPath = join(root, "span.mp4");
91-
const { framesEncoded } = await renderSpan(
92-
projectDir,
93-
{ fps: 30, width: 160, height: 90, format: "mp4", quality: "draft", hdrMode: "force-sdr" },
94-
{ startSeconds: 0.5, endSeconds: 2 },
95-
spanPath,
96-
);
97-
expect(framesEncoded).toBe(45);
98-
99-
const full = frames(fullPath);
100-
const span = frames(spanPath);
101-
expect(span.length).toBe(45);
102-
const worstMatch = Math.max(...span.map((frame, i) => meanDiff(frame, full[15 + i]!)));
103-
// The backdrop brightens about 2.8 levels a frame, so a span one frame off its place misses by more than this.
104-
expect(worstMatch).toBeLessThan(1.5);
105-
const oneFrameLate = span.map((frame, i) => meanDiff(frame, full[16 + i]!));
106-
expect(Math.min(...oneFrameLate)).toBeGreaterThan(worstMatch);
107-
}, 240_000);
108-
});

‎packages/producer/src/services/distributed/renderSpan.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ChunkSliceJson } from "../render/stages/freezePlan.js";
55
import { writeStitchedMp4 } from "./mp4Stitch.js";
66
import { plan, type DistributedRenderConfig } from "./plan.js";
77
import { renderChunk } from "./renderChunk.js";
8+
import { RenderCancelledError } from "../renderOrchestrator.js";
89

910
export interface RenderSpanResult {
1011
startFrame: number;
@@ -40,6 +41,10 @@ export function spanFrameRange(
4041
}
4142

4243
/** Renders `[startSeconds, endSeconds)` as a standalone mp4 from 0, with that span's audio, by chunk sub-ranges. */
44+
function throwIfAborted(signal: AbortSignal | undefined): void {
45+
if (signal?.aborted) throw new RenderCancelledError("render_cancelled", "user_cancelled");
46+
}
47+
4348
export async function renderSpan(
4449
projectDir: string,
4550
config: DistributedRenderConfig,
@@ -59,7 +64,13 @@ export async function renderSpan(
5964
const planDir = join(workDir, "plan");
6065
const partsDir = join(workDir, "parts");
6166
mkdirSync(partsDir, { recursive: true });
62-
const planned = await plan(projectDir, config, planDir, { chunksCarryAudio: true });
67+
// The span captures here in the browser a full render uses, so the container-only font guards do not apply.
68+
const planned = await plan(
69+
projectDir,
70+
{ ...config, rejectOnSystemFonts: false, failClosedFontFetch: false },
71+
planDir,
72+
{ chunksCarryAudio: true },
73+
);
6374
const { startFrame, endFrame } = spanFrameRange(
6475
span.startSeconds,
6576
span.endSeconds,
@@ -76,10 +87,12 @@ export async function renderSpan(
7687
endFrame: Math.min(slice.endFrame, endFrame),
7788
};
7889
if (range.endFrame <= range.startFrame) continue;
90+
throwIfAborted(config.abortSignal);
7991
const part = join(partsDir, `part-${String(parts.length).padStart(4, "0")}.mp4`);
8092
await renderChunk(planDir, slice.index, part, { frameRange: range });
8193
parts.push(part);
8294
}
95+
throwIfAborted(config.abortSignal);
8396
await writeStitchedMp4(parts, outputPath);
8497
return { startFrame, endFrame, framesEncoded: endFrame - startFrame };
8598
} finally {

0 commit comments

Comments
 (0)