Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Cover encoder fallback and cross-platform renderer selection

webadderallb7ea069d02b1
diff --git a/src/lib/exporter/annexBEncoderFallback.test.ts b/src/lib/exporter/annexBEncoderFallback.test.ts
new file mode 100644
index 00000000..5b3c369f
--- /dev/null
+++ b/src/lib/exporter/annexBEncoderFallback.test.ts
@@ -0,0 +1,104 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { ModernVideoExporter } from "./modernVideoExporter";
+import type { VideoExporterConfig } from "./types";
+import { VideoExporter } from "./videoExporter";
+
+interface NativeExportStartup {
+ tryStartNativeVideoExport: () => Promise<boolean>;
+ shouldUseExperimentalNativeExport: () => boolean;
+}
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
+
+for (const Exporter of [ModernVideoExporter, VideoExporter]) {
+ describe(`${Exporter.name} Annex B encoder fallback`, () => {
+ it.each([
+ "hardware",
+ "software",
+ "unsupported",
+ "probe-error",
+ "configure-error",
+ "odd-width",
+ "odd-height",
+ ] as const)("handles %s without starting an unusable native session", async (scenario) => {
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ const configure = vi.fn((_config: VideoEncoderConfig) => {
+ if (scenario === "configure-error") throw new Error("Encoder configure failed");
+ });
+ const close = vi.fn();
+ const probe = vi.fn(async (config: VideoEncoderConfig) => {
+ if (scenario === "probe-error") throw new Error("Encoder probe failed");
+ return {
+ supported:
+ scenario === "hardware" ||
+ scenario === "configure-error" ||
+ (scenario === "software" &&
+ config.hardwareAcceleration === "prefer-software"),
+ };
+ });
+ class Encoder {
+ static isConfigSupported = probe;
+ configure = configure;
+ close = close;
+ }
+ vi.stubGlobal("VideoEncoder", Encoder);
+ const start = vi.fn(async () => ({ success: true, sessionId: "test-session" }));
+ const cancel = vi.fn(async () => ({ success: true }));
+ vi.stubGlobal("window", {
+ electronAPI: { nativeVideoExportStart: start, nativeVideoExportCancel: cancel },
+ });
+ const config = {
+ videoUrl: "file:///recording.mp4",
+ width: scenario === "odd-width" ? 1919 : 1920,
+ height: scenario === "odd-height" ? 1079 : 1080,
+ frameRate: 30,
+ bitrate: 8_000_000,
+ wallpaper: "#000000",
+ } as VideoExporterConfig;
+ const exporter = new Exporter(config) as unknown as NativeExportStartup;
+ if (Exporter === VideoExporter) {
+ vi.spyOn(exporter, "shouldUseExperimentalNativeExport").mockReturnValue(true);
+ }
+
+ const success = await exporter.tryStartNativeVideoExport();
+
+ expect(success).toBe(scenario === "hardware" || scenario === "software");
+ if (scenario === "hardware" || scenario === "software") {
+ expect(configure).toHaveBeenCalledWith(
+ expect.objectContaining({
+ codec: "avc1.640034",
+ width: 1920,
+ height: 1080,
+ hardwareAcceleration:
+ scenario === "hardware" ? "prefer-hardware" : "prefer-software",
+ avc: { format: "annexb" },
+ }),
+ );
+ expect(start).toHaveBeenCalledWith(
+ expect.objectContaining({ inputMode: "h264-stream" }),
+ );
+ } else if (scenario !== "configure-error") {
+ expect(start).not.toHaveBeenCalled();
+ expect(configure).not.toHaveBeenCalled();
+ }
+ if (scenario === "software" || scenario === "unsupported") {
+ expect(probe).toHaveBeenCalledTimes(2);
+ expect(probe.mock.calls.map(([config]) => config.hardwareAcceleration)).toEqual([
+ "prefer-hardware",
+ "prefer-software",
+ ]);
+ } else if (scenario === "odd-width" || scenario === "odd-height") {
+ expect(probe).not.toHaveBeenCalled();
+ } else {
+ expect(probe).toHaveBeenCalledTimes(1);
+ }
+ if (scenario === "configure-error") {
+ expect(close).toHaveBeenCalledOnce();
+ expect(cancel).toHaveBeenCalledWith("test-session");
+ }
+ });
+ });
+}
diff --git a/src/lib/exporter/modernVideoExporter.fallback.test.ts b/src/lib/exporter/modernVideoExporter.fallback.test.ts
index f4d291ab..58b36a71 100644
--- a/src/lib/exporter/modernVideoExporter.fallback.test.ts
+++ b/src/lib/exporter/modernVideoExporter.fallback.test.ts
@@ -80,6 +80,52 @@ describe("ModernVideoExporter native fallback routing", () => {
vi.unstubAllGlobals();
});
+ it.each([
+ ...["MacIntel", "Win32", "Linux x86_64"].flatMap((platform) => [
+ { platform, configured: undefined, expected: "webgl" },
+ { platform, configured: "webgpu" as const, expected: "webgpu" },
+ { platform, configured: "webgl" as const, expected: "webgl" },
+ ]),
+ ])("uses $expected as the Lightning render backend on $platform when configured backend is $configured", async ({
+ platform,
+ configured,
+ expected,
+ }) => {
+ vi.stubGlobal("navigator", { platform });
+ const exporter = new ModernVideoExporter({
+ videoUrl: "file:///recording.mp4",
+ width: 1920,
+ height: 1080,
+ frameRate: 30,
+ bitrate: 8_000_000,
+ wallpaper: "#101010",
+ padding: 0,
+ borderRadius: 0,
+ backgroundBlur: 0,
+ shadowIntensity: 0,
+ showShadow: false,
+ cropRegion: { x: 0, y: 0, width: 1, height: 1 },
+ backendPreference: "webcodecs",
+ preferredRenderBackend: configured,
+ } as never) as unknown as {
+ export: () => Promise<{ success: boolean }>;
+ initializeEncoder: () => Promise<unknown>;
+ };
+ vi.spyOn(exporter, "initializeEncoder").mockResolvedValue({
+ codec: "avc1.640034",
+ hardwareAcceleration: "prefer-hardware",
+ });
+
+ const result = await exporter.export();
+
+ expect(result.success).toBe(true);
+ const { FrameRenderer } = await import("./modernFrameRenderer");
+ expect(FrameRenderer).toHaveBeenCalledOnce();
+ expect(FrameRenderer).toHaveBeenCalledWith(
+ expect.objectContaining({ preferredRenderBackend: expected }),
+ );
+ }, 15_000);
+
it("removes failed native writes without creating an unhandled rejection", async () => {
const exporter = new ModernVideoExporter({} as never) as unknown as {
trackNativeWritePromise: (write: Promise<void>) => void;