Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

Add regression test for encoder stall timeout

もりこ22db7faebdf5
diff --git a/src/lib/exporter/videoExporter.test.ts b/src/lib/exporter/videoExporter.test.ts
index 1b64255a2..a5c330f70 100644
--- a/src/lib/exporter/videoExporter.test.ts
+++ b/src/lib/exporter/videoExporter.test.ts
@@ -1,8 +1,9 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import {
getSourceCopyFastPathBlockers,
isSourceCopyFastPathEligible,
type VideoExporterConfig,
+ waitForEncoderQueueSpace,
} from "./videoExporter";
function createConfig(overrides: Partial<VideoExporterConfig> = {}): VideoExporterConfig {
@@ -119,3 +120,129 @@ describe("getSourceCopyFastPathBlockers", () => {
).toContain("output-size 1920x1080 differs from source 1920x1032");
});
});
+
+describe("waitForEncoderQueueSpace", () => {
+ function fakeClock(start = 0) {
+ let elapsedMs = start;
+ return {
+ now: () => elapsedMs,
+ sleep: async (ms: number) => {
+ elapsedMs += ms;
+ },
+ };
+ }
+
+ it("resolves immediately when the queue already has space", async () => {
+ const clock = fakeClock();
+ const sleep = vi.fn(clock.sleep);
+
+ await waitForEncoderQueueSpace({
+ getQueueSize: () => 0,
+ maxEncodeQueue: 8,
+ isCancelled: () => false,
+ encoderPreference: "prefer-hardware",
+ now: clock.now,
+ sleep,
+ });
+
+ expect(sleep).not.toHaveBeenCalled();
+ });
+
+ it("waits for the queue to drain and then resolves", async () => {
+ const clock = fakeClock();
+ let queueSize = 8;
+ // Queue drains well within the timeout.
+ const sleep = vi.fn(async (ms: number) => {
+ await clock.sleep(ms);
+ queueSize = 0;
+ });
+
+ await waitForEncoderQueueSpace({
+ getQueueSize: () => queueSize,
+ maxEncodeQueue: 8,
+ isCancelled: () => false,
+ encoderPreference: "prefer-hardware",
+ now: clock.now,
+ sleep,
+ });
+
+ expect(sleep).toHaveBeenCalledTimes(1);
+ });
+
+ it("throws a hardware-specific error once the queue stays full past the timeout", async () => {
+ const clock = fakeClock();
+
+ await expect(
+ waitForEncoderQueueSpace({
+ getQueueSize: () => 8,
+ maxEncodeQueue: 8,
+ isCancelled: () => false,
+ encoderPreference: "prefer-hardware",
+ now: clock.now,
+ sleep: clock.sleep,
+ }),
+ ).rejects.toThrow(
+ "The hardware video encoder stopped responding. Retrying with a safer encoder.",
+ );
+ });
+
+ it("throws a generic error for the software encoder once the queue stays full past the timeout", async () => {
+ const clock = fakeClock();
+
+ await expect(
+ waitForEncoderQueueSpace({
+ getQueueSize: () => 8,
+ maxEncodeQueue: 8,
+ isCancelled: () => false,
+ encoderPreference: "prefer-software",
+ now: clock.now,
+ sleep: clock.sleep,
+ }),
+ ).rejects.toThrow("The video encoder stopped responding during export.");
+ });
+
+ // Regression test for the false-positive stall: a long gap *before* this call
+ // (e.g. the decoder discarding frames inside a trim region) must not count
+ // against the timeout. Only time spent waiting *inside* this call should.
+ it("does not throw merely because a long time has already passed before this call", async () => {
+ // Simulate this call starting two minutes after some unrelated earlier event,
+ // then having the queue drain almost immediately once we're inside the wait.
+ const clock = fakeClock(2 * 60 * 1000);
+ let queueSize = 8;
+ const sleep = vi.fn(async (ms: number) => {
+ await clock.sleep(ms);
+ queueSize = 0;
+ });
+
+ await expect(
+ waitForEncoderQueueSpace({
+ getQueueSize: () => queueSize,
+ maxEncodeQueue: 8,
+ isCancelled: () => false,
+ encoderPreference: "prefer-hardware",
+ now: clock.now,
+ sleep,
+ }),
+ ).resolves.toBeUndefined();
+ });
+
+ it("stops waiting without throwing once cancelled", async () => {
+ const clock = fakeClock();
+ let cancelled = false;
+ const sleep = vi.fn(async (ms: number) => {
+ await clock.sleep(ms);
+ cancelled = true;
+ });
+
+ await expect(
+ waitForEncoderQueueSpace({
+ getQueueSize: () => 8,
+ maxEncodeQueue: 8,
+ isCancelled: () => cancelled,
+ encoderPreference: "prefer-hardware",
+ now: clock.now,
+ sleep,
+ }),
+ ).resolves.toBeUndefined();
+ });
+});
diff --git a/src/lib/exporter/videoExporter.ts b/src/lib/exporter/videoExporter.ts
index 3a7894098..8f239656c 100644
--- a/src/lib/exporter/videoExporter.ts
+++ b/src/lib/exporter/videoExporter.ts
@@ -20,6 +20,37 @@ import type { ExportConfig, ExportProgress, ExportResult } from "./types";
const ENCODER_STALL_TIMEOUT_MS = 15_000;
const ENCODER_FLUSH_TIMEOUT_MS = 20_000;
+/**
+ * Waits for the encoder's queue to drain below maxEncodeQueue before returning.
+ *
+ * The stall timer starts fresh on each call (not from the encoder's last output), so a
+ * long gap before this call — e.g. the decoder discarding frames inside a trim region —
+ * doesn't get blamed on the encoder once real frames resume.
+ */
+export async function waitForEncoderQueueSpace(params: {
+ getQueueSize: () => number;
+ maxEncodeQueue: number;
+ isCancelled: () => boolean;
+ encoderPreference: HardwareAcceleration;
+ now?: () => number;
+ sleep?: (ms: number) => Promise<void>;
+}): Promise<void> {
+ const now = params.now ?? Date.now;
+ const sleep = params.sleep ?? ((ms: number) => new Promise((resolve) => setTimeout(resolve, ms)));
+
+ const stallWaitStartAt = now();
+ while (params.getQueueSize() >= params.maxEncodeQueue && !params.isCancelled()) {
+ if (now() - stallWaitStartAt > ENCODER_STALL_TIMEOUT_MS) {
+ throw new Error(
+ params.encoderPreference === "prefer-hardware"
+ ? "The hardware video encoder stopped responding. Retrying with a safer encoder."
+ : "The video encoder stopped responding during export.",
+ );
+ }
+ await sleep(5);
+ }
+}
+
export interface VideoExporterConfig extends ExportConfig {
videoUrl: string;
webcamVideoUrl?: string;
@@ -383,24 +414,16 @@ export class VideoExporter {
exportFrame = new VideoFrame(canvas, { timestamp, duration: frameDuration });
}
- // Timer resets each time we enter the queue-full wait, so a long
- // trimmed-region decode (decoder busy discarding frames) doesn't
- // make the stall check fire spuriously.
- const stallWaitStartAt = Date.now();
- while (
- this.encoder &&
- this.encoder.encodeQueueSize >= maxEncodeQueue &&
- !this.cancelled
- ) {
- if (Date.now() - stallWaitStartAt > ENCODER_STALL_TIMEOUT_MS) {
- exportFrame.close();
- throw new Error(
- encoderPreference === "prefer-hardware"
- ? "The hardware video encoder stopped responding. Retrying with a safer encoder."
- : "The video encoder stopped responding during export.",
- );
- }
- await new Promise((resolve) => setTimeout(resolve, 5));
+ try {
+ await waitForEncoderQueueSpace({
+ getQueueSize: () => this.encoder?.encodeQueueSize ?? 0,
+ maxEncodeQueue,
+ isCancelled: () => this.cancelled,
+ encoderPreference,
+ });
+ } catch (error) {
+ exportFrame.close();
+ throw error;
}
if (this.encoder && this.encoder.state === "configured") {