Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Fix export codec deadlocks

Bittia225a243d83d
diff --git a/docs/architecture.md b/docs/architecture.md
index da9c60b..1ad025a 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -67,6 +67,8 @@ Timeline and Export share `ProgramMonitor`. It keeps the current picture and the
With WebGPU, `ProgramPicture` presents all visible layers through one persistent canvas using the export compositor. Each update captures the required `VideoFrame` handles before clearing or drawing; a loading or seeking source leaves the last complete picture intact. Actual timeline gaps clear to the project background. This avoids the black frame caused by handing off between browser video surfaces at a cut. Frames remain in GPU memory, and effect pipelines are prepared when the compositor starts. Machines without WebGPU retain the native-media preview.
+Export consumes and closes decoded frames while waiting for decoder backpressure or `flush()`. Waiting for those operations without releasing output can exhaust hardware frame pools and deadlock a run. Video and audio codec waits check cancellation and asynchronous errors; 30 seconds without queue/output progress stops the run with a stage-specific error. Codec and frame resources are released on cancellation and failure. Starting an export pauses preview playback.
+
## Independently judged slices
1. Foundation: desktop shell, library, portable project persistence, schema, and always-visible agent prompt.
diff --git a/src/components/workspace/ExportView.tsx b/src/components/workspace/ExportView.tsx
index 3fd8027..d10982a 100644
--- a/src/components/workspace/ExportView.tsx
+++ b/src/components/workspace/ExportView.tsx
@@ -226,6 +226,7 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
return;
}
if (!destination) return; // The user closed the save dialog. Nothing to say.
+ setPlaying(false);
cancelRef.current = false;
setProgress({ phase: "preparing", framesDone: 0, frameCount: plan.frameCount, detail: "Starting…" });
try {
@@ -374,7 +375,7 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
type="button"
className="export-transport__play"
onClick={() => setPlaying((value) => !value)}
- disabled={plan.frameCount === 0}
+ disabled={running || plan.frameCount === 0}
aria-label={playing ? "Pause" : "Play"}
title={plan.frameCount === 0 ? "There is nothing on the timeline to play" : playing ? "Pause" : "Play"}
>{playing ? <Pause size={20} fill="currentColor" aria-hidden="true" /> : <Play size={20} fill="currentColor" aria-hidden="true" />}</button>
diff --git a/src/lib/codecWait.test.ts b/src/lib/codecWait.test.ts
new file mode 100644
index 0000000..0d9f933
--- /dev/null
+++ b/src/lib/codecWait.test.ts
@@ -0,0 +1,56 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { finishCodec, waitForCodec } from "./codecWait";
+
+afterEach(() => { vi.useRealTimers(); });
+
+it("consumes output while waiting for decoder flush to release its surfaces", async () => {
+ let finish!: () => void;
+ const flushing = new Promise<void>((resolve) => { finish = resolve; });
+ const pump = vi.fn(() => finish());
+ await finishCodec(flushing, { stage: "Decoding", check: () => {}, progress: () => 0, pump });
+ expect(pump).toHaveBeenCalled();
+});
+
+it("cancels a flush that never settles", async () => {
+ vi.useFakeTimers();
+ let cancelled = false;
+ const run = finishCodec(new Promise(() => {}), { stage: "Encoding", check: () => { if (cancelled) throw new Error("Cancelled"); }, progress: () => 0 });
+ const result = expect(run).rejects.toThrow("Cancelled");
+ cancelled = true;
+ await vi.advanceTimersByTimeAsync(4);
+ await result;
+ expect(vi.getTimerCount()).toBe(0);
+});
+
+it("reports a codec error even when its queue size never changes", async () => {
+ vi.useFakeTimers();
+ let failure: Error | null = null;
+ const run = waitForCodec(() => false, { stage: "Encoding", check: () => { if (failure) throw failure; }, progress: () => 9 });
+ const result = expect(run).rejects.toThrow("Hardware encoder failed");
+ failure = new Error("Hardware encoder failed");
+ await vi.advanceTimersByTimeAsync(4);
+ await result;
+});
+
+it("stops a codec after thirty seconds without progress", async () => {
+ vi.useFakeTimers();
+ const run = waitForCodec(() => false, { stage: "Decoding scene.mp4", check: () => {}, progress: () => 9 });
+ const result = expect(run).rejects.toThrow("Decoding scene.mp4 stopped making progress for 30 seconds");
+ await vi.advanceTimersByTimeAsync(30_004);
+ await result;
+ expect(vi.getTimerCount()).toBe(0);
+});
+
+it("allows long operations that continue producing output", async () => {
+ vi.useFakeTimers();
+ let packets = 0, ready = false;
+ const run = waitForCodec(() => ready, { stage: "Encoding", check: () => {}, progress: () => packets });
+ const result = expect(run).resolves.toBeUndefined();
+ for (let interval = 0; interval < 3; interval++) {
+ await vi.advanceTimersByTimeAsync(20_000);
+ packets++;
+ }
+ ready = true;
+ await vi.advanceTimersByTimeAsync(4);
+ await result;
+});
diff --git a/src/lib/codecWait.ts b/src/lib/codecWait.ts
new file mode 100644
index 0000000..4379f21
--- /dev/null
+++ b/src/lib/codecWait.ts
@@ -0,0 +1,41 @@
+const CODEC_IDLE_TIMEOUT_MS = 30_000;
+
+export interface CodecWaitOptions {
+ stage: string;
+ check: () => void;
+ /** Changes when the codec consumes input, produces output or releases frames. */
+ progress: () => string | number;
+ /** Consume decoder output while waiting, so its hardware surfaces can recycle. */
+ pump?: () => Promise<void> | void;
+}
+
+/** Queue size can stay nonzero forever if output frames are retained. Always
+ * give the consumer a turn, and observe cancellation/errors inside the wait. */
+export async function waitForCodec(ready: () => boolean, options: CodecWaitOptions): Promise<void> {
+ let progress = options.progress();
+ let changedAt = performance.now();
+ while (true) {
+ options.check();
+ if (ready()) return;
+ await options.pump?.();
+ options.check();
+ if (ready()) return;
+ const next = options.progress();
+ if (next !== progress) { progress = next; changedAt = performance.now(); }
+ if (performance.now() - changedAt >= CODEC_IDLE_TIMEOUT_MS) {
+ throw new Error(`${options.stage} stopped making progress for 30 seconds. The export was stopped; try again or use a lower resolution.`);
+ }
+ await new Promise<void>((resolve) => setTimeout(resolve, 4));
+ }
+}
+
+/** flush() still delivers output. Awaiting it without consuming those frames
+ * can exhaust the decoder's surface pool before the promise can resolve. */
+export async function finishCodec(flushing: Promise<void>, options: CodecWaitOptions): Promise<void> {
+ let settled = false;
+ let failure: unknown;
+ let rejected = false;
+ void flushing.then(() => { settled = true; }, (reason) => { failure = reason; rejected = true; settled = true; });
+ await waitForCodec(() => settled, options);
+ if (rejected) throw failure;
+}
diff --git a/src/lib/exportPipeline.test.ts b/src/lib/exportPipeline.test.ts
index 3989905..d26dcf9 100644
--- a/src/lib/exportPipeline.test.ts
+++ b/src/lib/exportPipeline.test.ts
@@ -298,6 +298,8 @@ interface HarnessRecord {
audioConfig: AudioEncoderConfig | null;
audioChunks: number;
videoChunks: number;
+ decoderLiveFrames: number;
+ decoderPeakFrames: number;
}
class HarnessAudioBuffer {
@@ -328,6 +330,8 @@ interface HarnessOptions {
/** False: no AudioEncoder/AudioData/OfflineAudioContext at all, which is the
* webview `detectExportSupport().audio` has to answer false for. */
audio?: boolean;
+ decoderPool?: number;
+ decodeOnFlush?: boolean;
}
function installWebCodecs(options: HarnessOptions = {}): { restore: () => void; recorded: HarnessRecord } {
@@ -346,7 +350,7 @@ function installWebCodecs(options: HarnessOptions = {}): { restore: () => void;
] as const;
const saved: Record<string, unknown> = {};
for (const name of names) saved[name] = scope[name];
- const recorded: HarnessRecord = { audioData: [], audioConfig: null, audioChunks: 0, videoChunks: 0 };
+ const recorded: HarnessRecord = { audioData: [], audioConfig: null, audioChunks: 0, videoChunks: 0, decoderLiveFrames: 0, decoderPeakFrames: 0 };
class HarnessChunk {
readonly type: "key" | "delta";
@@ -405,7 +409,12 @@ function installWebCodecs(options: HarnessOptions = {}): { restore: () => void;
scope.VideoDecoder = class {
state = "configured";
- decodeQueueSize = 0;
+ private inputs: { timestamp: number; duration: number }[] = [];
+ private liveFrames = 0;
+ private scheduled = false;
+ private flushing = false;
+ private flushed: (() => void) | null = null;
+ get decodeQueueSize() { return options.decodeOnFlush && !this.flushing ? 0 : this.inputs.length; }
private readonly emit: (frame: unknown) => void;
constructor(init: { output: (frame: unknown) => void; error: (reason: unknown) => void }) {
this.emit = init.output;
@@ -413,12 +422,44 @@ function installWebCodecs(options: HarnessOptions = {}): { restore: () => void;
configure() {}
decode(chunk: { timestamp: number; duration: number }) {
if (!decodes) return;
+ if (options.decoderPool) { this.inputs.push(chunk); this.schedule(); return; }
const Frame = scope.VideoFrame as new (source: unknown, init: { timestamp: number; duration?: number }) => unknown;
this.emit(new Frame(null, { timestamp: chunk.timestamp, duration: chunk.duration }));
}
- async flush() {}
+ private schedule() {
+ if (this.scheduled || this.state === "closed" || (options.decodeOnFlush && !this.flushing)) return;
+ this.scheduled = true;
+ setTimeout(() => {
+ this.scheduled = false;
+ if (this.state === "closed") return;
+ while (this.inputs.length && this.liveFrames < options.decoderPool!) {
+ const chunk = this.inputs.shift()!;
+ const Frame = scope.VideoFrame as typeof VideoFrame;
+ const frame = new Frame(null as unknown as CanvasImageSource, { timestamp: chunk.timestamp, duration: chunk.duration });
+ this.liveFrames++; recorded.decoderLiveFrames++;
+ recorded.decoderPeakFrames = Math.max(recorded.decoderPeakFrames, recorded.decoderLiveFrames);
+ const close = frame.close.bind(frame);
+ let closed = false;
+ frame.close = () => {
+ if (closed) return;
+ closed = true; close();
+ this.liveFrames--; recorded.decoderLiveFrames--;
+ this.schedule();
+ };
+ this.emit(frame);
+ }
+ if (!this.inputs.length) { this.flushed?.(); this.flushed = null; }
+ }, 0);
+ }
+ async flush() {
+ if (!options.decoderPool) return;
+ this.flushing = true;
+ await new Promise<void>((resolve) => { this.flushed = resolve; this.schedule(); });
+ }
close() {
this.state = "closed";
+ this.inputs.length = 0;
+ this.flushed?.();
}
};
@@ -611,6 +652,81 @@ function installWebCodecs(options: HarnessOptions = {}): { restore: () => void;
}
describe("running an export of external media", () => {
+ it.each(["video-queue", "video-flush", "decoder-queue", "decoder-flush", "audio-queue", "audio-flush"])("cancels and closes codecs stuck in %s", async (stuck) => {
+ const harness = installWebCodecs({ decodes: true });
+ let cancelled = false;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const closed = vi.fn();
+ const stall = () => { timer ??= setTimeout(() => { cancelled = true; }, 0); };
+ try {
+ const scope = globalThis as unknown as Record<string, any>;
+ const name = stuck.startsWith("video") ? "VideoEncoder" : stuck.startsWith("audio") ? "AudioEncoder" : "VideoDecoder";
+ const Base = scope[name];
+ scope[name] = class extends Base {
+ encode(...args: unknown[]) {
+ super.encode(...args);
+ if (stuck.endsWith("queue")) { Object.defineProperty(this, "encodeQueueSize", { value: 20 }); stall(); }
+ }
+ decode(...args: unknown[]) {
+ if (stuck === "decoder-flush") return; // all output is deferred until flush
+ super.decode(...args);
+ if (stuck.endsWith("queue")) { Object.defineProperty(this, "decodeQueueSize", { value: 20 }); stall(); }
+ }
+ flush() {
+ if (stuck.endsWith("flush")) { stall(); return new Promise<void>(() => {}); }
+ return super.flush();
+ }
+ close() { closed(); super.close(); }
+ };
+ serveFiles({ video: await sampleFile(120, 30), score: scoreFile() });
+ const config = audioProject();
+ const settings = defaultExportSettings(config);
+ await expect(runExport({ folderPath: AUDIO_FOLDER, config, settings, plan: buildExportPlan(config, settings),
+ bitrate: 5_000_000, onProgress: () => {}, cancelled: () => cancelled }).then(() => undefined)).rejects.toBeInstanceOf(ExportCancelled);
+ expect(closed).toHaveBeenCalledTimes(1);
+ expect(callLog().some((call) => call.cmd.includes("write"))).toBe(false);
+ } finally { clearTimeout(timer); harness.restore(); }
+ });
+
+ it.each([
+ { name: "decoder backpressure", frames: 80, decodeOnFlush: false, layered: false },
+ { name: "decoder flush", frames: 9, decodeOnFlush: true, layered: false },
+ { name: "layered decoder backpressure", frames: 80, decodeOnFlush: false, layered: true },
+ ])("recycles a three-frame hardware pool during $name", async ({ frames, decodeOnFlush, layered }) => {
+ const harness = installWebCodecs({ decodes: true, audio: false, decoderPool: 3, decodeOnFlush });
+ const instances: VideoDecoder[] = [];
+ const Decoder = VideoDecoder;
+ vi.stubGlobal("VideoDecoder", class extends Decoder {
+ constructor(init: VideoDecoderInit) { super(init); instances.push(this); }
+ });
+ let cancelled = false;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ try {
+ invoked.mockResolvedValue(await sampleFile(frames, 30));
+ const config = externalProject();
+ const track = config.timeline.tracks.find((track) => track.kind === "video" && track.clips.length)!;
+ track.clips[0].durationMs = Math.floor(frames * 1000 / 30);
+ if (layered) {
+ track.clips[0].look = { opacity: 50, temperature: 0 };
+ config.timeline.tracks.push({ ...track, id: "lower", clips: [{ ...track.clips[0], id: "lower-clip", trackId: "lower", look: { opacity: 100, temperature: 0 } }] });
+ }
+ const settings = defaultExportSettings(config);
+ const plan = buildExportPlan(config, settings);
+ const run = runExport({ folderPath: FOLDER, config, settings, plan, bitrate: 5_000_000,
+ onProgress: () => {}, cancelled: () => cancelled });
+ // A hardware pool cannot output again until an earlier frame is closed.
+ // Stop the mock on timeout so a regression fails without leaking a run.
+ timer = setTimeout(() => { cancelled = true; instances.forEach((decoder) => decoder.close()); }, 5000);
+ await expect(run).resolves.toMatchObject({ audio: false });
+ expect(harness.recorded.videoChunks).toBe(plan.frameCount);
+ expect(harness.recorded.decoderLiveFrames).toBe(0);
+ expect(harness.recorded.decoderPeakFrames).toBeLessThanOrEqual(layered ? 6 : 3);
+ } finally {
+ clearTimeout(timer);
+ harness.restore();
+ }
+ }, 10_000);
+
it("refuses to silently omit GPU effects when only canvas compositing is available", async () => {
const { restore, recorded } = installWebCodecs({ decodes: true, audio: false });
try {
diff --git a/src/lib/exportPipeline.ts b/src/lib/exportPipeline.ts
index 7ec2872..118febc 100644
--- a/src/lib/exportPipeline.ts
+++ b/src/lib/exportPipeline.ts
@@ -17,6 +17,7 @@
========================================================================== */
import { invoke } from "@tauri-apps/api/core";
+import { finishCodec, waitForCodec } from "./codecWait";
import { isTauri, readMediaFileBytes } from "./persistence";
import {
AUDIO_CHANNELS,
@@ -763,23 +764,28 @@ async function probeAudioCodec(): Promise<{ supported: boolean; detail: string }
async function encodeAudio(
mix: AudioMix,
add: (chunk: EncodedAudioChunk, meta?: EncodedAudioChunkMetadata) => void,
+ stopIfCancelled: () => void,
): Promise<void> {
const failure: { reason: Error | null } = { reason: null };
+ let packets = 0;
const encoder = new AudioEncoder({
- output: (chunk, meta) => add(chunk, meta),
+ output: (chunk, meta) => {
+ try { add(chunk, meta); packets++; }
+ catch (reason) { failure.reason = reason instanceof Error ? reason : new Error(String(reason)); }
+ },
error: (reason) => {
failure.reason = reason instanceof Error ? reason : new Error(String(reason));
},
});
- encoder.configure({
- codec: AUDIO_CODEC,
- sampleRate: AUDIO_SAMPLE_RATE,
- numberOfChannels: AUDIO_CHANNELS,
- bitrate: AUDIO_BITRATE,
- });
+ const waiting = {
+ stage: "Audio encoding",
+ check: () => { stopIfCancelled(); if (failure.reason) throw failure.reason; },
+ progress: () => `${encoder.encodeQueueSize}:${packets}`,
+ };
try {
+ encoder.configure({ codec: AUDIO_CODEC, sampleRate: AUDIO_SAMPLE_RATE, numberOfChannels: AUDIO_CHANNELS, bitrate: AUDIO_BITRATE });
for (let offset = 0; offset < mix.frames; offset += AUDIO_BLOCK) {
- if (failure.reason) throw failure.reason;
+ waiting.check();
const count = Math.min(AUDIO_BLOCK, mix.frames - offset);
// f32-planar: channel 0 then channel 1, exactly as the mix holds them.
const planar = new Float32Array(count * AUDIO_CHANNELS);
@@ -794,12 +800,11 @@ async function encodeAudio(
timestamp: Math.round((offset * 1_000_000) / AUDIO_SAMPLE_RATE),
data: planar,
});
- encoder.encode(data);
- data.close();
- while (encoder.encodeQueueSize > 16) await tick();
+ try { encoder.encode(data); }
+ finally { data.close(); }
+ await waitForCodec(() => encoder.encodeQueueSize <= 16, waiting);
}
- await encoder.flush();
- if (failure.reason) throw failure.reason;
+ await finishCodec(encoder.flush(), waiting);
} finally {
if (encoder.state !== "closed") encoder.close();
}
@@ -852,7 +857,7 @@ export interface ExportRunOptions {
plan: ExportPlan;
bitrate: number;
onProgress: (progress: ExportProgress) => void;
- /** Polled once per frame. Returning true aborts with `ExportCancelled`. */
+ /** Polled per frame and during codec waits. True aborts with `ExportCancelled`. */
cancelled: () => boolean;
}
@@ -890,12 +895,16 @@ async function openLayerFrameReader(
const pending: VideoFrame[] = [];
let screen: VideoFrame | null = null;
let flushed = false;
+ let flushing = false;
+ let disposed = false;
+ let decodedFrames = 0;
const failure: { reason: Error | null } = { reason: null };
const decoder = new VideoDecoder({
- output: (frame) => pending.push(frame),
+ output: (frame) => { if (disposed) frame.close(); else { pending.push(frame); decodedFrames++; } },
error: (reason) => { failure.reason = reason; },
});
const dispose = () => {
+ disposed = true;
if (decoder.state !== "closed") decoder.close();
screen?.close();
pending.forEach((frame) => frame.close());
@@ -904,6 +913,11 @@ async function openLayerFrameReader(
};
try { decoder.configure(source.config); }
catch (reason) { dispose(); throw reason; }
+ const waiting = {
+ stage: `Decoding ${asset.name}`,
+ check: () => { stopIfCancelled(); if (failure.reason) throw failure.reason; },
+ progress: () => `${decoder.decodeQueueSize}:${decodedFrames}`,
+ };
return {
async frameAt(wantUs) {
while (true) {
@@ -917,8 +931,14 @@ async function openLayerFrameReader(
if (screen && (pending.length || flushed)) return screen;
if (flushed) throw new Error(`${asset.name} decoded no frames.`);
if (nextSample >= source.samples.length) {
- await decoder.flush();
- flushed = true;
+ if (!flushing) {
+ flushing = true;
+ void decoder.flush().then(() => { flushed = true; }, (reason) => {
+ failure.reason = reason instanceof Error ? reason : new Error(String(reason));
+ });
+ }
+ // Return to the consumer as each frame arrives, even during flush.
+ await waitForCodec(() => pending.length > 0 || flushed, waiting);
} else {
// Yield between small batches so output can arrive before feeding
// more samples. This bounds decoded frames even for long sources.
@@ -929,7 +949,7 @@ async function openLayerFrameReader(
duration: sample.durationUs, data: sample.data,
}));
}
- while (decoder.decodeQueueSize > 8) { stopIfCancelled(); if (failure.reason) throw failure.reason; await tick(); }
+ await waitForCodec(() => decoder.decodeQueueSize <= 8 || pending.length > 0, waiting);
await tick();
}
}
@@ -1032,13 +1052,21 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
/* On an object because the encoder writes it from its own callback, where
TypeScript's flow analysis cannot follow the assignment. */
const encoderFailure: { reason: Error | null } = { reason: null };
+ let encodedPackets = 0;
const encoder = new VideoEncoder({
- output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
+ output: (chunk, meta) => {
+ try { muxer.addVideoChunk(chunk, meta); encodedPackets++; }
+ catch (reason) { encoderFailure.reason = reason instanceof Error ? reason : new Error(String(reason)); }
+ },
error: (reason) => {
encoderFailure.reason = reason instanceof Error ? reason : new Error(String(reason));
},
});
- encoder.configure(encoderConfig(probe.codecString, plan, bitrate));
+ const encoderWait = {
+ stage: "Video encoding",
+ check: () => { stopIfCancelled(); if (encoderFailure.reason) throw encoderFailure.reason; },
+ progress: () => `${encoder.encodeQueueSize}:${encodedPackets}`,
+ };
/* WebGPU first, as the pipeline is meant to run — the decoded frame is
sampled where it already lives. A machine without it, or a GPU that gives
@@ -1103,14 +1131,14 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
timestampUs(outputIndex),
timestampUs(outputIndex + 1) - timestampUs(outputIndex),
);
- encoder.encode(composited, { keyFrame: outputIndex % keyFrameInterval === 0 });
- composited.close();
+ try { encoder.encode(composited, { keyFrame: outputIndex % keyFrameInterval === 0 }); }
+ finally { composited.close(); }
framesDone += 1;
if (framesDone % 5 === 0 || framesDone === plan.frameCount) {
report("rendering", framesDone, `Rendering frame ${framesDone} of ${plan.frameCount}.`);
}
// Yield so the encoder's own queue drains and the window stays alive.
- while (encoder.encodeQueueSize > 8) await tick();
+ await waitForCodec(() => encoder.encodeQueueSize <= 8, encoderWait);
if (framesDone % 12 === 0) await tick();
};
@@ -1201,13 +1229,14 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
const wantUs = (index: number) => sourceTimeMsForFrame(segment, index, plan.frameRate) * 1000;
const pending: VideoFrame[] = [];
+ let decodedFrames = 0;
+ let disposed = false;
const decoder = new VideoDecoder({
- output: (frame) => pending.push(frame),
+ output: (frame) => { if (disposed) frame.close(); else { pending.push(frame); decodedFrames++; } },
error: (reason) => {
failure.reason = reason instanceof Error ? reason : new Error(String(reason));
},
});
- decoder.configure(source.config);
/* Frames arrive in presentation order, so the frame showing at output time
W is the last one whose timestamp is at or before W. Each decoded frame
@@ -1216,14 +1245,19 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
const drain = async (final: boolean) => {
while (pending.length > 0) {
const frame = pending.shift() as VideoFrame;
- if (on.screen) {
- while (nextOutput < segment.endFrame && wantUs(nextOutput) < frame.timestamp) {
- await emit(on.screen, nextOutput, segment);
- nextOutput += 1;
+ try {
+ if (on.screen) {
+ while (nextOutput < segment.endFrame && wantUs(nextOutput) < frame.timestamp) {
+ await emit(on.screen, nextOutput, segment);
+ nextOutput += 1;
+ }
+ on.screen.close();
}
- on.screen.close();
+ on.screen = frame;
+ } catch (reason) {
+ frame.close();
+ throw reason;
}
- on.screen = frame;
if (nextOutput >= segment.endFrame) break;
}
if (final) {
@@ -1237,9 +1271,16 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
}
};
+ const decoderWait = {
+ stage: `Decoding ${asset.name}`,
+ check: () => { encoderWait.check(); if (failure.reason) throw failure.reason; },
+ progress: () => `${decoder.decodeQueueSize}:${decodedFrames}:${nextOutput}`,
+ pump: () => drain(false),
+ };
try {
+ decoder.configure(source.config);
for (let index = first; index <= last && nextOutput < segment.endFrame; index += 1) {
- if (failure.reason) throw failure.reason;
+ decoderWait.check();
const sample = source.samples[index];
decoder.decode(
new EncodedVideoChunk({
@@ -1249,16 +1290,17 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
data: sample.data,
}),
);
- while (decoder.decodeQueueSize > 12) await tick();
+ await waitForCodec(() => decoder.decodeQueueSize <= 12, decoderWait);
await drain(false);
}
if (nextOutput < segment.endFrame) {
- await decoder.flush();
+ await finishCodec(decoder.flush(), decoderWait);
await drain(false);
}
if (failure.reason) throw failure.reason;
await drain(true);
} finally {
+ disposed = true;
on.screen?.close();
for (const frame of pending) frame.close();
if (decoder.state !== "closed") decoder.close();
@@ -1293,6 +1335,7 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
};
try {
+ encoder.configure(encoderConfig(probe.codecString, plan, bitrate));
for (const segment of plan.segments) {
stopIfCancelled();
if (segment.kind === "gap") {
@@ -1306,11 +1349,10 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
if (soundtrack) {
report("finishing", framesDone, `Encoding the mixed soundtrack as AAC…`);
- await encodeAudio(soundtrack, (chunk, meta) => muxer.addAudioChunk(chunk, meta));
+ await encodeAudio(soundtrack, (chunk, meta) => muxer.addAudioChunk(chunk, meta), encoderWait.check);
}
report("finishing", framesDone, "Flushing the encoder and writing the MP4 index…");
- await encoder.flush();
- if (encoderFailure.reason) throw encoderFailure.reason;
+ await finishCodec(encoder.flush(), encoderWait);
muxer.finalize();
report("done", framesDone, "Encoded.");
return {