Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Read exported media where it actually lives

Bitti26b295082849
diff --git a/src/components/workspace/ExportView.test.tsx b/src/components/workspace/ExportView.test.tsx
index c0dd4d5..8515d8c 100644
--- a/src/components/workspace/ExportView.test.tsx
+++ b/src/components/workspace/ExportView.test.tsx
@@ -2,7 +2,8 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
-import { createProjectConfig, type ProjectConfig, type TimelineClip } from "../../lib/project";
+import externalFixture from "../../../fixtures/project-v1-external-media.json";
+import { createProjectConfig, parseProjectConfig, type ProjectConfig, type TimelineClip } from "../../lib/project";
import { ExportView } from "./ExportView";
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
@@ -67,3 +68,31 @@ describe("the export view where nothing can encode", () => {
expect(screen.getByText(/nothing on the timeline yet/i)).toBeTruthy();
});
});
+
+/* The other half of the media policy: video is NOT copied into the project, so
+ a real timeline's clips carry an absolute source path and no relative one.
+ The page has to plan such a project exactly as it plans any other — the
+ previous build did, and then the run died on the first read. */
+describe("the export view on media the project does not contain", () => {
+ const external = () => parseProjectConfig(externalFixture);
+
+ it("plans the external clip instead of calling it missing media", () => {
+ render(<ExportView config={external()} folderPath="D:\\tmp\\news\\News broadcast" />);
+ const reasons = screen.getByRole("alert").textContent ?? "";
+ expect(reasons).not.toMatch(/not in this project/i);
+ expect(reasons).not.toMatch(/no file recorded/i);
+ expect(screen.getByText("00:12.000")).toBeTruthy();
+ expect(screen.getByText("360 frames at 30 fps")).toBeTruthy();
+ });
+});
+
+describe("what the page claims about the compositor", () => {
+ it("does not promise WebGPU before anything has asked for an adapter", async () => {
+ render(<ExportView config={project([clip("a", 0, 2_000)])} folderPath="/tmp/project" />);
+ // jsdom has no navigator.gpu, so the settled answer is the 2D canvas with a
+ // stated reason. What must never appear is a bare WebGPU promise.
+ expect(await screen.findByText(/no navigator\.gpu/i)).toBeTruthy();
+ expect(screen.queryByText("WebGPU")).toBeNull();
+ expect(document.body.textContent).toContain("on a 2D canvas");
+ });
+});
diff --git a/src/components/workspace/ExportView.tsx b/src/components/workspace/ExportView.tsx
index 5fc1f0c..72b4713 100644
--- a/src/components/workspace/ExportView.tsx
+++ b/src/components/workspace/ExportView.tsx
@@ -25,10 +25,12 @@ import {
ExportCancelled,
PreviewSources,
probeAllCodecs,
+ probeCompositor,
runExport,
writeExportFile,
type CodecProbe,
type CompositorKind,
+ type CompositorProbe,
type ExportProgress,
} from "../../lib/exportPipeline";
import type { ProjectConfig, Resolution } from "../../lib/project";
@@ -45,6 +47,7 @@ interface FinishedExport {
path: string;
bytes: number;
compositor: CompositorKind;
+ compositorDetail: string;
codecString: string;
}
@@ -59,6 +62,10 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
is gated on the answer rather than on what the code hopes is there. */
const support = useMemo(detectExportSupport, []);
const [probes, setProbes] = useState<CodecProbe[] | null>(null);
+ /* Which compositor will ACTUALLY run, asked by requesting an adapter rather
+ than by looking for `navigator.gpu`. Null until the answer comes back — the
+ page says it is asking rather than guessing WebGPU and being wrong. */
+ const [compositor, setCompositor] = useState<CompositorProbe | null>(null);
const [previewTimeMs, setPreviewTimeMs] = useState(0);
const [previewBusy, setPreviewBusy] = useState(false);
@@ -89,6 +96,16 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
cancelRef.current = true;
}, []);
+ useEffect(() => {
+ let live = true;
+ void probeCompositor().then((found) => {
+ if (live) setCompositor(found);
+ });
+ return () => {
+ live = false;
+ };
+ }, []);
+
useEffect(() => {
const sources = new PreviewSources(folderPath);
sourcesRef.current = sources;
@@ -210,7 +227,13 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
detail: `Writing ${formatBytes(outcome.bytes.byteLength)} to ${destination}…`,
});
const written = await writeExportFile(destination, outcome.bytes);
- setResult({ path: destination, bytes: written, compositor: outcome.compositor, codecString: outcome.codecString });
+ setResult({
+ path: destination,
+ bytes: written,
+ compositor: outcome.compositor,
+ compositorDetail: outcome.compositorDetail,
+ codecString: outcome.codecString,
+ });
} catch (reason) {
if (reason instanceof ExportCancelled) setCancelled(true);
else setError(describe(reason));
@@ -237,8 +260,12 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
["Sound", "None", "video only — see below"],
[
"Compositor",
- support.webgpu ? "WebGPU" : "2D canvas",
- support.webgpu ? "frames scaled without leaving the GPU" : "no WebGPU in this webview",
+ compositor ? (compositor.kind === "webgpu" ? "WebGPU" : "2D canvas") : "asking this computer…",
+ compositor
+ ? compositor.kind === "webgpu"
+ ? "frames scaled without leaving the GPU"
+ : compositor.detail
+ : "requesting a GPU adapter",
],
];
@@ -250,9 +277,13 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
<p>
PolStudio renders the video tracks in order — trims, gaps and all — taking each source file apart with
mp4box, decoding it through this computer’s own video decoder, compositing{" "}
- {support.webgpu ? "on the GPU with WebGPU" : "on a 2D canvas, because this webview has no WebGPU"} and
- muxing a real .mp4. Nothing below is a mock-up: where a control cannot do its job here, it says why instead
- of pretending.
+ {compositor === null
+ ? "on the GPU if this computer gives PolStudio one and on a 2D canvas otherwise — it is being asked right now"
+ : compositor.kind === "webgpu"
+ ? "on the GPU with WebGPU, which this computer offered an adapter for"
+ : "on a 2D canvas, because this computer offers no working WebGPU adapter"}{" "}
+ and muxing a real .mp4. Nothing below is a mock-up: where a control cannot do its job here, it says why
+ instead of pretending.
</p>
</div>
</header>
@@ -425,7 +456,8 @@ export function ExportView({ config, folderPath }: { config: ProjectConfig; fold
</p>
<p>
{formatBytes(result.bytes)} written · {result.codecString} · composited with{" "}
- {result.compositor === "webgpu" ? "WebGPU" : "a 2D canvas (WebGPU was unavailable)"} · no audio track.
+ {result.compositor === "webgpu" ? "WebGPU" : "a 2D canvas"}
+ {result.compositorDetail ? <> — {result.compositorDetail}</> : null} · no audio track.
</p>
</div>}
</section>
diff --git a/src/lib/export.ts b/src/lib/export.ts
index 64ba372..2cc0e50 100644
--- a/src/lib/export.ts
+++ b/src/lib/export.ts
@@ -283,6 +283,7 @@ export function buildExportPlan(config: ProjectConfig, settings: ExportSettings)
blockers.push("There are no video clips on the timeline, so there is nothing to render.");
}
const missing: string[] = [];
+ const locationless: string[] = [];
const undecodable: string[] = [];
for (const segment of segments) {
if (segment.kind !== "clip") continue;
@@ -291,12 +292,22 @@ export function buildExportPlan(config: ProjectConfig, settings: ExportSettings)
missing.push(segment.label);
continue;
}
+ /* An asset carries EITHER a path inside the project or an absolute one
+ outside it — imported video and audio are never copied in, so they only
+ ever have the second. Neither means there is no file to read, and the run
+ would die on the first frame; say so here, where the button still is. */
+ if (!asset.relativePath && !asset.sourcePath) {
+ locationless.push(`${segment.label} (${asset.name})`);
+ }
const drawable = DEMUXABLE_VIDEO.has(asset.mimeType) || DRAWABLE_IMAGE.has(asset.mimeType);
if (!drawable) undecodable.push(`${segment.label} (${asset.name}, ${asset.mimeType})`);
}
if (missing.length > 0) {
blockers.push(`These clips point at media that is not in this project: ${missing.join(", ")}.`);
}
+ if (locationless.length > 0) {
+ blockers.push(`These clips point at media with no file recorded for it at all: ${locationless.join(", ")}.`);
+ }
if (undecodable.length > 0) {
blockers.push(
`PolStudio can only take apart .mp4 and .mov video and still images so far. These clips use something else: ${undecodable.join(", ")}.`,
diff --git a/src/lib/exportPipeline.test.ts b/src/lib/exportPipeline.test.ts
index 43fe867..f035eb9 100644
--- a/src/lib/exportPipeline.test.ts
+++ b/src/lib/exportPipeline.test.ts
@@ -1,7 +1,14 @@
-import { describe, expect, it, vi } from "vitest";
-import { demux } from "./exportPipeline";
+// @vitest-environment jsdom
-// The module reaches for Tauri's IPC at import time; nothing here invokes it.
+import { invoke } from "@tauri-apps/api/core";
+import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
+import externalFixture from "../../fixtures/project-v1-external-media.json";
+import { buildExportPlan, defaultExportSettings } from "./export";
+import { parseProjectConfig, type ProjectAsset } from "./project";
+import { demux, ExportCancelled, PreviewSources, probeCompositor, runExport } from "./exportPipeline";
+
+// Every IPC call is captured rather than performed: what the export DISPATCHES
+// is the thing under test.
vi.mock("@tauri-apps/api/core", () => ({ invoke: vi.fn() }));
/* Stage 1 of the pipeline, checked against a real MP4 rather than a mock.
@@ -72,3 +79,238 @@ describe("demuxing a real MP4", () => {
await expect(demux(rubbish, "broken.mp4")).rejects.toThrow(/broken\.mp4/);
});
});
+
+/* ---------------------------------------------------------------------------
+ The export against media the project does NOT contain.
+
+ Every earlier export test built its assets as `relativePath: media/<id>.mp4`,
+ which is a shape imported video has not had since media stopped being copied
+ into the project folder. That is why a broken read shipped green: the export
+ asked `read_project_file` for a null relative path, the call died inside IPC
+ argument deserialisation, and no test had an external asset to notice with.
+
+ These run on `fixtures/project-v1-external-media.json` — the same file the
+ schema tests and six Rust tests use — so the shape here is the shape on disk.
+ --------------------------------------------------------------------------- */
+
+const invoked = invoke as unknown as Mock;
+const FOLDER = "D:\\tmp\\news\\News broadcast";
+const EXTERNAL_VIDEO = "D:\\tmp\\news\\News broadcast\\rush-01.mp4";
+
+const externalProject = () => parseProjectConfig(externalFixture);
+const assetNamed = (id: string): ProjectAsset => {
+ const found = externalProject().assets.find((asset) => asset.id === id);
+ if (!found) throw new Error(`fixture has no asset ${id}`);
+ return found;
+};
+
+/** Every call the export made, in the shape a reader of the failing IPC log
+ * would see it. */
+const callLog = () => invoked.mock.calls.map(([cmd, args]) => ({ cmd, args }));
+
+beforeEach(() => {
+ invoked.mockReset();
+ (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__ = {};
+ URL.createObjectURL = vi.fn(() => "blob:preview");
+ URL.revokeObjectURL = vi.fn();
+});
+
+afterEach(() => {
+ delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__;
+});
+
+describe("reading the media an export needs", () => {
+ it("fetches an imported .mp4 from where it actually lives", async () => {
+ invoked.mockResolvedValue(await sampleFile(5, 30));
+ const sources = new PreviewSources(FOLDER);
+ await sources.url(assetNamed("asset-external-video"));
+
+ expect(callLog()).toEqual([
+ { cmd: "read_external_media_file", args: { folderPath: FOLDER, sourcePath: EXTERNAL_VIDEO } },
+ ]);
+ });
+
+ it("never asks read_project_file for a path the asset does not have", async () => {
+ invoked.mockResolvedValue(await sampleFile(5, 30));
+ const sources = new PreviewSources(FOLDER);
+ await sources.url(assetNamed("asset-external-video"));
+ await sources.url(assetNamed("asset-external-audio"));
+
+ // The exact failure this class of bug produces: a String parameter handed a
+ // null, rejected by IPC argument deserialisation before Rust runs at all.
+ for (const call of callLog()) {
+ expect(call.cmd).not.toBe("read_project_file");
+ expect(call.args).not.toHaveProperty("relativePath");
+ }
+ });
+
+ it("still fetches a copied still from inside the project folder", async () => {
+ invoked.mockResolvedValue(new Uint8Array([1, 2, 3]).buffer);
+ const sources = new PreviewSources(FOLDER);
+ await sources.url(assetNamed("asset-copied-image"));
+
+ expect(callLog()).toEqual([
+ { cmd: "read_project_file", args: { folderPath: FOLDER, relativePath: "media/title-card.png" } },
+ ]);
+ });
+
+ it("reads each asset once, however many frames want it", async () => {
+ invoked.mockResolvedValue(await sampleFile(5, 30));
+ const sources = new PreviewSources(FOLDER);
+ const asset = assetNamed("asset-external-video");
+ await Promise.all([sources.url(asset), sources.url(asset), sources.url(asset)]);
+ expect(invoked).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe("planning an export of external media", () => {
+ it("raises no blocker: the file is reachable, it just is not in the folder", () => {
+ const config = externalProject();
+ const plan = buildExportPlan(config, defaultExportSettings(config));
+ expect(plan.blockers).toEqual([]);
+ expect(plan.clipCount).toBe(1);
+ expect(plan.frameCount).toBe(360);
+ expect(plan.segments).toEqual([
+ expect.objectContaining({ kind: "clip", assetId: "asset-external-video", startFrame: 0, endFrame: 360 }),
+ ]);
+ });
+
+ it("blocks when an asset records no file at all, instead of offering the run", () => {
+ const config = externalProject();
+ const stripped = {
+ ...config,
+ assets: config.assets.map((asset) =>
+ asset.id === "asset-external-video" ? { ...asset, sourcePath: null, relativePath: null } : asset,
+ ),
+ };
+ const plan = buildExportPlan(stripped, defaultExportSettings(config));
+ expect(plan.blockers.join(" ")).toMatch(/no file recorded/i);
+ });
+});
+
+/* WebCodecs and OffscreenCanvas do not exist in jsdom, so the run below stands
+ them up. They do only what the pipeline asks of them and nothing more: the
+ decoder deliberately emits no frames, which makes the run fail at a known
+ point AFTER the source file has been read. What is asserted is the read. */
+function installWebCodecs(): () => void {
+ const scope = globalThis as unknown as Record<string, unknown>;
+ const saved = {
+ VideoEncoder: scope.VideoEncoder,
+ VideoDecoder: scope.VideoDecoder,
+ VideoFrame: scope.VideoFrame,
+ EncodedVideoChunk: scope.EncodedVideoChunk,
+ OffscreenCanvas: scope.OffscreenCanvas,
+ };
+ scope.VideoEncoder = class {
+ static async isConfigSupported(config: unknown) {
+ return { supported: true, config };
+ }
+ state = "configured";
+ encodeQueueSize = 0;
+ configure() {}
+ encode() {}
+ async flush() {}
+ close() {
+ this.state = "closed";
+ }
+ };
+ scope.VideoDecoder = class {
+ state = "configured";
+ decodeQueueSize = 0;
+ configure() {}
+ decode() {}
+ async flush() {}
+ close() {
+ this.state = "closed";
+ }
+ };
+ scope.VideoFrame = class {
+ timestamp: number;
+ duration: number;
+ displayWidth = 320;
+ displayHeight = 180;
+ constructor(_source: unknown, init: { timestamp: number; duration?: number }) {
+ this.timestamp = init.timestamp;
+ this.duration = init.duration ?? 0;
+ }
+ close() {}
+ };
+ scope.EncodedVideoChunk = class {
+ constructor(init: Record<string, unknown>) {
+ Object.assign(this, init);
+ }
+ };
+ scope.OffscreenCanvas = class {
+ constructor(
+ public width: number,
+ public height: number,
+ ) {}
+ getContext(kind: string) {
+ return kind === "2d" ? { fillStyle: "", fillRect() {}, drawImage() {} } : null;
+ }
+ };
+ return () => Object.assign(scope, saved);
+}
+
+describe("running an export of external media", () => {
+ it("dispatches the external read, not a project-relative one", async () => {
+ const restore = installWebCodecs();
+ try {
+ invoked.mockResolvedValue(await sampleFile(5, 30));
+ const config = externalProject();
+ const settings = defaultExportSettings(config);
+ await expect(
+ runExport({
+ folderPath: FOLDER,
+ config,
+ settings,
+ plan: buildExportPlan(config, settings),
+ bitrate: 5_000_000,
+ onProgress: () => {},
+ cancelled: () => false,
+ }),
+ // The stub decoder produces nothing, so the run ends here — well past
+ // the read, which is the step this test exists for.
+ ).rejects.toThrow(/no picture/i);
+
+ expect(callLog()).toEqual([
+ { cmd: "read_external_media_file", args: { folderPath: FOLDER, sourcePath: EXTERNAL_VIDEO } },
+ ]);
+ } finally {
+ restore();
+ }
+ });
+
+ it("stops on cancel without ever touching the file", async () => {
+ const restore = installWebCodecs();
+ try {
+ const config = externalProject();
+ const settings = defaultExportSettings(config);
+ await expect(
+ runExport({
+ folderPath: FOLDER,
+ config,
+ settings,
+ plan: buildExportPlan(config, settings),
+ bitrate: 5_000_000,
+ onProgress: () => {},
+ cancelled: () => true,
+ }),
+ ).rejects.toBeInstanceOf(ExportCancelled);
+ expect(invoked).not.toHaveBeenCalled();
+ } finally {
+ restore();
+ }
+ });
+});
+
+describe("which compositor will really run", () => {
+ it("answers from the adapter, and says why when there is none", async () => {
+ // jsdom has no navigator.gpu at all — the honest answer is canvas2d with
+ // that as the stated reason, not a WebGPU claim from a present object.
+ const probe = await probeCompositor();
+ expect(probe.kind).toBe("canvas2d");
+ expect(probe.detail).toMatch(/navigator\.gpu/);
+ });
+
+});
diff --git a/src/lib/exportPipeline.ts b/src/lib/exportPipeline.ts
index fd5c84d..5271365 100644
--- a/src/lib/exportPipeline.ts
+++ b/src/lib/exportPipeline.ts
@@ -17,7 +17,7 @@
========================================================================== */
import { invoke } from "@tauri-apps/api/core";
-import { isTauri } from "./persistence";
+import { isTauri, readMediaFileBytes } from "./persistence";
import {
fitRect,
outputCodec,
@@ -37,8 +37,12 @@ export interface ExportSupport {
/** WebCodecs encode. Present in WebView2/Chromium, absent in older engines. */
encoder: boolean;
decoder: boolean;
- /** The GPU compositor. Absent is not fatal — there is a 2D canvas fallback. */
- webgpu: boolean;
+ /** `navigator.gpu` EXISTS. That is all this says, and it is not enough to
+ * promise a GPU compositor: on a VM, over RDP, on a blocklisted driver, the
+ * object is there and `requestAdapter()` still returns null. Ask
+ * `probeCompositor()` — which does request an adapter — before telling a
+ * user which compositor will run. */
+ webgpuApi: boolean;
/** Only the desktop shell has files to read and a place to write one. */
desktop: boolean;
}
@@ -48,7 +52,7 @@ export function detectExportSupport(): ExportSupport {
return {
encoder: typeof scope.VideoEncoder === "function",
decoder: typeof scope.VideoDecoder === "function",
- webgpu: typeof navigator !== "undefined" && "gpu" in navigator && Boolean(navigator.gpu),
+ webgpuApi: typeof navigator !== "undefined" && "gpu" in navigator && Boolean(navigator.gpu),
desktop: isTauri(),
};
}
@@ -163,14 +167,28 @@ fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {
}
`;
-async function createWebGpuCompositor(width: number, height: number, background: string): Promise<Compositor | null> {
- if (typeof navigator === "undefined" || !("gpu" in navigator) || !navigator.gpu) return null;
+/** Either a working GPU compositor, or the reason there is none. Never a bare
+ * null: "WebGPU was unavailable" with no cause is what the last round shipped,
+ * and it is unactionable. */
+type GpuAttempt = { compositor: Compositor; reason: null } | { compositor: null; reason: string };
+
+const NO_GPU_API = "This webview exposes no navigator.gpu at all.";
+const NO_ADAPTER =
+ "navigator.gpu exists but requestAdapter() returned no adapter — the usual causes are a virtual machine, an RDP session, or a driver on Chromium's blocklist.";
+
+async function createWebGpuCompositor(width: number, height: number, background: string): Promise<GpuAttempt> {
+ if (typeof navigator === "undefined" || !("gpu" in navigator) || !navigator.gpu) {
+ return { compositor: null, reason: NO_GPU_API };
+ }
const adapter = await navigator.gpu.requestAdapter();
- if (!adapter) return null;
+ if (!adapter) return { compositor: null, reason: NO_ADAPTER };
const device = await adapter.requestDevice();
const canvas = new OffscreenCanvas(width, height);
const context = canvas.getContext("webgpu");
- if (!context) return null;
+ if (!context) {
+ device.destroy();
+ return { compositor: null, reason: "This computer gave no WebGPU context for an OffscreenCanvas." };
+ }
const format = navigator.gpu.getPreferredCanvasFormat();
context.configure({ device, format, alphaMode: "opaque" });
const shader = device.createShaderModule({ code: COMPOSITOR_SHADER });
@@ -213,7 +231,7 @@ async function createWebGpuCompositor(width: number, height: number, background:
device.queue.submit([encoder.finish()]);
};
- return {
+ const compositor: Compositor = {
kind: "webgpu",
clear() {
pass(context.getCurrentTexture().createView(), null);
@@ -244,6 +262,7 @@ async function createWebGpuCompositor(width: number, height: number, background:
device.destroy();
},
};
+ return { compositor, reason: null };
}
function createCanvasCompositor(width: number, height: number, background: string): Compositor {
@@ -269,6 +288,44 @@ function createCanvasCompositor(width: number, height: number, background: strin
};
}
+export interface CompositorProbe {
+ /** Which compositor an export started right now would actually use. */
+ kind: CompositorKind;
+ /** Why, in this machine's own terms. For canvas2d that is the reason the GPU
+ * path is out; for webgpu it is what the adapter calls itself. */
+ detail: string;
+}
+
+/** Asks the GPU for an adapter, exactly as the export will.
+ *
+ * `navigator.gpu` being present is not the same question, and it was the wrong
+ * one to put in front of a user: measured on one ordinary machine, the object
+ * was there, `requestAdapter()` returned null, and the run composited on a 2D
+ * canvas while the page had already promised WebGPU. This asks what the export
+ * asks, and the answer is what the page is allowed to claim. */
+export async function probeCompositor(): Promise<CompositorProbe> {
+ if (typeof navigator === "undefined" || !("gpu" in navigator) || !navigator.gpu) {
+ return { kind: "canvas2d", detail: NO_GPU_API };
+ }
+ try {
+ const adapter = await navigator.gpu.requestAdapter();
+ if (!adapter) return { kind: "canvas2d", detail: NO_ADAPTER };
+ /* Nothing is kept: this adapter is dropped here and the export requests its
+ own. The probe is a question, not a reservation. */
+ const info: GPUAdapterInfo | undefined = adapter.info;
+ const named = [info?.description, info?.device, info?.vendor, info?.architecture]
+ .filter((part) => Boolean(part))
+ .join(" ")
+ .trim();
+ return { kind: "webgpu", detail: named ? `GPU adapter: ${named}.` : "This computer offered a GPU adapter." };
+ } catch (reason) {
+ return {
+ kind: "canvas2d",
+ detail: `Asking this computer for a GPU adapter failed: ${reason instanceof Error ? reason.message : String(reason)}`,
+ };
+ }
+}
+
/* ---------------------------------------------------------------------------
Stage 1 — demux
--------------------------------------------------------------------------- */
@@ -278,11 +335,20 @@ export interface DemuxedSource {
samples: Array<{ data: Uint8Array; timestampUs: number; durationUs: number; key: boolean }>;
}
+/** The asset's file, wherever the project actually keeps it.
+ *
+ * This MUST stay routed through `readMediaFileBytes`. Reading
+ * `asset.relativePath` here directly is what broke every real export: since
+ * media stopped being copied into the project folder, imported video and audio
+ * carry an absolute `sourcePath` and a null `relativePath`, so the old call
+ * handed `read_project_file` a null where Rust declares a String and died at
+ * IPC argument deserialisation — after the page had shown a clean plan and a
+ * live Export button. */
async function readAssetBytes(folderPath: string, asset: ProjectAsset): Promise<ArrayBuffer> {
- if (!isTauri()) {
- throw new Error("Reading project media needs the desktop app; the browser preview has no project folder.");
+ if (!asset.sourcePath && !asset.relativePath) {
+ throw new Error(`${asset.name} records no file at all, so there is nothing to read for this clip.`);
}
- return invoke<ArrayBuffer>("read_project_file", { folderPath, relativePath: asset.relativePath });
+ return readMediaFileBytes(folderPath, asset);
}
/** Any of the codec configuration boxes. They all serialise themselves the
@@ -390,6 +456,10 @@ export class ExportCancelled extends Error {
export interface ExportResult {
bytes: Uint8Array;
compositor: CompositorKind;
+ /** Why that one. When the GPU compositor did not run, this is the reason it
+ * did not — the previous version reported only WHICH compositor ran, which
+ * told nobody what to fix. */
+ compositorDetail: string;
codecString: string;
}
@@ -452,12 +522,21 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
/* 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
up part way through, drops to the 2D canvas, which composites the same
- picture on the same geometry. Which one ran is reported, never assumed. */
- const stage: { compositor: Compositor } = {
- compositor:
- (await createWebGpuCompositor(plan.width, plan.height, plan.backgroundColor).catch(() => null)) ??
- createCanvasCompositor(plan.width, plan.height, plan.backgroundColor),
- };
+ picture on the same geometry. Which one ran is reported, never assumed —
+ and so is WHY, because "WebGPU was unavailable" on its own is a sentence
+ nobody can act on. */
+ const attempt = await createWebGpuCompositor(plan.width, plan.height, plan.backgroundColor).catch(
+ (reason: unknown) => ({
+ compositor: null,
+ reason: `Starting the GPU compositor failed: ${reason instanceof Error ? reason.message : String(reason)}`,
+ }),
+ );
+ const stage: { compositor: Compositor; detail: string } = attempt.compositor
+ ? { compositor: attempt.compositor, detail: attempt.reason ?? "" }
+ : {
+ compositor: createCanvasCompositor(plan.width, plan.height, plan.backgroundColor),
+ detail: attempt.reason,
+ };
const paint = (frame: VideoFrame | null) => {
try {
if (frame) stage.compositor.draw(frame);
@@ -467,6 +546,7 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
if (stage.compositor.kind === "canvas2d") throw reason;
stage.compositor.dispose();
stage.compositor = createCanvasCompositor(plan.width, plan.height, plan.backgroundColor);
+ stage.detail = `The GPU compositor gave out ${framesDone} frames in and the rest was composited on a 2D canvas: ${reason instanceof Error ? reason.message : String(reason)}`;
}
if (frame) stage.compositor.draw(frame);
else stage.compositor.clear();
@@ -669,6 +749,7 @@ export async function runExport(options: ExportRunOptions): Promise<ExportResult
return {
bytes: new Uint8Array(target.buffer),
compositor: stage.compositor.kind,
+ compositorDetail: stage.detail,
codecString: probe.codecString,
};
} finally {
diff --git a/src/lib/persistence.ts b/src/lib/persistence.ts
index d700059..7f5acd3 100644
--- a/src/lib/persistence.ts
+++ b/src/lib/persistence.ts
@@ -197,13 +197,33 @@ export async function readProjectFileUrl(folderPath: string, relativePath: strin
* project itself names — see `read_external_media_file` in
* src-tauri/src/lib.rs. Same revoke rule as `readProjectFileUrl`. */
export async function readMediaFileUrl(folderPath: string, item: StoredLocation, mimeType: string): Promise<string | null> {
- if (!isTauri()) return null;
- const bytes = item.sourcePath
- ? await invoke<ArrayBuffer>("read_external_media_file", { folderPath, sourcePath: item.sourcePath })
- : item.relativePath
- ? await invoke<ArrayBuffer>("read_project_file", { folderPath, relativePath: item.relativePath })
- : null;
- return bytes ? URL.createObjectURL(new Blob([bytes], { type: mimeType })) : null;
+ if (!isTauri() || (!item.sourcePath && !item.relativePath)) return null;
+ return URL.createObjectURL(new Blob([await readMediaFileBytes(folderPath, item)], { type: mimeType }));
+}
+
+/** The same read as `readMediaFileUrl`, stopping at the bytes.
+ *
+ * The export pipeline wants the file itself — mp4box takes an ArrayBuffer
+ * apart, and wrapping it in a blob URL first only to fetch it back would copy
+ * a multi-gigabyte rush for nothing. It exists so that pipeline cannot go and
+ * read `relativePath` on its own: video and audio have none, and calling
+ * `read_project_file` with a null relative path dies at IPC argument
+ * deserialisation before Rust ever sees it — which is exactly the bug this
+ * sibling was added to end.
+ *
+ * Throws rather than returning null: every caller needs the bytes, so an
+ * absent file is a failure with a reason, not an empty result. */
+export async function readMediaFileBytes(folderPath: string, item: StoredLocation): Promise<ArrayBuffer> {
+ if (!isTauri()) {
+ throw new Error("Reading project media needs the desktop app; the browser preview has no project folder.");
+ }
+ if (item.sourcePath) {
+ return invoke<ArrayBuffer>("read_external_media_file", { folderPath, sourcePath: item.sourcePath });
+ }
+ if (item.relativePath) {
+ return invoke<ArrayBuffer>("read_project_file", { folderPath, relativePath: item.relativePath });
+ }
+ throw new Error("This item records no file at all — neither a path inside the project nor one outside it.");
}
export async function createProject(input: CreateProjectInput): Promise<ProjectRecord | null> {