Skip to content

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

docs(native-core): price the Rust/wgpu compositor against sandbox:false, honestly

Etienne Lescoteee92f7b6090
diff --git a/docs/architecture/export-pipeline-v2-spec.md b/docs/architecture/export-pipeline-v2-spec.md
index 23f5c7c4c..f3f6a9843 100644
--- a/docs/architecture/export-pipeline-v2-spec.md
+++ b/docs/architecture/export-pipeline-v2-spec.md
@@ -15,7 +15,7 @@ Scope: the `.axcut` (AI‑edition) export path only. The legacy `components/vide
### Constraints
- **The app is MIT and must stay MIT.** No GPL component may end up in what we ship (§7.2).
-- **Windows + macOS + Linux**, x64 and arm64 (Apple Silicon). No platform gets a degraded export.
+- **Windows + macOS + Linux**, x64 and arm64 (Apple Silicon). **No platform gets a degraded export.** Native encode ships on all three. *(Etienne, 2026‑07‑16 — see the rejected option below.)*
- Must degrade gracefully where no hardware encoder exists (→ WebCodecs fallback).
- **Bundle size is not a constraint** *(Etienne, 2026‑07‑16)*: ~200 MB per platform is acceptable. Do not trade features, codec coverage or simplicity for bytes. Precedent: the app already ships native binaries (`wgc-capture.exe`, `cursor-sampler.exe`, `whisper.dll`, `ggml-*.dll`).
@@ -24,6 +24,7 @@ Scope: the `.axcut` (AI‑edition) export path only. The legacy `components/vide
- Compositing multiple assets **simultaneously** (picture‑in‑picture of two screen recordings). The webcam overlay is the only simultaneous second source and stays as‑is.
- ~~Replacing WebCodecs with a native ffmpeg binary as the primary encoder (WebCodecs already *is* the hardware path; native ffmpeg is only a fallback).~~ **Inverted — this is now Goal 2.** WebCodecs reaches the same silicon but at **8 fps vs 165 fps** for native ffmpeg (§3.1). It becomes the *fallback*; native ffmpeg is the primary encoder (§7).
- Shipping a **GPL** ffmpeg build (x264/x265). We build LGPL only, so the app stays MIT (§7.2).
+- **Keeping WebCodecs as the primary encoder on macOS** because the gap is smaller there. **Rejected — do not reintroduce this.** It is a tempting argument and it will come back, so here it is with its rebuttal: WebCodecs *is* closer to native on VideoToolbox (~3.5× off, per [w3c/webcodecs#492](https://github.com/w3c/webcodecs/issues/492)) than on our Windows/AMD reference (~20×), and skipping the macOS ffmpeg build would dodge real work — BtbN publishes prebuilt LGPL binaries for Windows and Linux but **not macOS**, so macOS is the one platform we must build (and notarise) ourselves. We are still not doing it: "3.5× slower, but only on Macs" is a degraded export on a platform where our users edit, sold as an engineering convenience. Native encode ships on all three, or the plan is wrong.
- Rewriting the compositor. The Canvas2D composite stays; only the encoder changes (§4.4).
- GIF export changes beyond what falls out of the shared render plan.
@@ -289,7 +290,19 @@ Each phase is independently shippable and independently verifiable.
### Phase 3 — **Native ffmpeg encode (L) — the actual win** ← *next*
Replace the WebCodecs encoder + JS muxer with a bundled LGPL ffmpeg subprocess (§7).
-1. **Build & bundle.** ffmpeg per platform, built **without** `--enable-gpl` and without `--enable-nonfree` — that is what makes it LGPL, and it is the *only* control that matters (those flags are what pull x264/x265; absent them ffmpeg simply won't build a GPL component, whatever else is enabled). **Binary size is not a constraint** (~200 MB/platform is fine), so do **not** strip for size — enable the codecs we may want next (HEVC, AV1, VP9, ProRes) so a future format doesn't need a rebuild + re‑qualification. Ship next to the existing native binaries in `electron/native/bin/<platform>/`. Add the LGPL notice + source offer.
+1. **Build & bundle.** ffmpeg per platform, built **without** `--enable-gpl` and without `--enable-nonfree` — that is what makes it LGPL, and it is the *only* control that matters (those flags are what pull x264/x265; absent them ffmpeg simply won't build a GPL component, whatever else is enabled). **Binary size is not a constraint** (~200 MB/platform is fine), so do **not** strip for size — enable the codecs we may want next (HEVC, AV1, VP9, ProRes) so a future format doesn't need a rebuild + re‑qualification. Ship next to the existing native binaries in `electron/native/bin/<platform>-<arch>/`. Add the LGPL notice + source offer.
+
+ **Where each platform's binary comes from:**
+
+ | platform | source | effort |
+ |---|---|---|
+ | Windows x64 + arm64 | **[BtbN prebuilt LGPL](https://github.com/BtbN/FFmpeg-Builds)** — daily builds, `h264_amf`/`h264_nvenc`/`h264_qsv` compiled in | just verify + vendor |
+ | Linux x64 + arm64 | **BtbN prebuilt LGPL** | just verify + vendor |
+ | **macOS x64 + arm64** | **we build it** — BtbN has no macOS target | **real work: build + sign + notarise** |
+
+ macOS is the one we own end‑to‑end, and skipping it is not on the table (§1 non‑goals). A VideoToolbox‑only macOS build is inherently x264‑free, which makes the LGPL gate easy there — the cost is the build/notarisation pipeline, not the licence.
+
+ **Verify every binary before vendoring it**, whatever the source: `ffmpeg -version` must print `License: LGPL version 2.1 or later`, its `configuration:` line must contain neither `--enable-gpl` nor `--enable-nonfree`, and `ffmpeg -encoders` must not list `libx264`/`libx265`. `isLgplBuild()` in `electron/media/ffmpegCapabilities.ts` encodes exactly this check — wire it into CI so a "just add x264" patch cannot silently relicense the app.
2. **Capability probe** (§7.1), cached per machine.
3. **Encode service** (main process): spawn ffmpeg, stream **BGRA** frames to stdin **honouring `drain`** (the measured 489–589 MB/s depends on it), parse progress from stderr, handle cancel (kill the tree) and non‑zero exit.
4. **Renderer side:** after `renderFrame`, extract with `new VideoFrame(canvas).copyTo(buf)` (**not** `getImageData` — 1.43 ms vs 7.00, §4.4.1) and ship the frame to the service over a **credit window of 8**. Keep the WebCodecs path behind the capability probe as the fallback.
diff --git a/docs/architecture/native-core-tauri-spec.md b/docs/architecture/native-core-tauri-spec.md
new file mode 100644
index 000000000..9af38b01d
--- /dev/null
+++ b/docs/architecture/native-core-tauri-spec.md
@@ -0,0 +1,149 @@
+# Native Core — moving the compositor out of the browser renderer
+
+Status: **Exploratory / for decision.** This is a product-architecture proposal, not a perf patch.
+Scope: the render/composite/encode core of the `.axcut` editor — preview *and* export.
+Prerequisite reading: [`export-pipeline-v2-spec.md`](./export-pipeline-v2-spec.md) §3.1 and §4.4.1 — every number below comes from there.
+
+---
+
+## 1. Why this exists
+
+The export pipeline was measured end‑to‑end (2026‑07‑16). After replacing the WebCodecs encoder with native ffmpeg, the bottleneck is **not** the encoder any more — it is the **renderer→main process crossing**:
+
+| architecture | fps @1080p | vs today |
+|---|---:|---:|
+| today (WebCodecs) | 8 | — |
+| Phase 3 — native ffmpeg, BGRA over IPC | 34 | 4.3× |
+| Phase 4 — + GPU NV12 packing (halves the bytes) | 81 | 10× |
+| **native compositor (no crossing)** | **~165** (encoder‑bound) | **20×** |
+| native compositor + parallel encode sessions | >165 | >20× |
+
+The crossing exists for one reason: **the compositor runs inside a sandboxed Chromium renderer**, so every finished frame must be copied across a process boundary to reach a native encoder. Electron offers no zero‑copy path out ([electron#34905](https://github.com/electron/electron/issues/34905): the transfer list accepts `MessagePortMain[]` only — an ArrayBuffer transfer silently drops the message). Every frame is structured‑cloned: ~390–420 MB/s, and attaching ffmpeg to the receiving process costs another ~40 % because one thread both receives IPC and writes stdin.
+
+**Phase 4 works around the wall (10×). This document is about removing it (20×+).**
+
+## 2. The trap this must avoid
+
+> Moving compositing to Rust **for export only** would give us two implementations of the entire visual identity — zoom spring, auto‑focus follow, motion blur, three‑layer drop shadow, webcam masks, cursor smoothing/click‑bounce/motion‑blur, annotations, 3D rotation, gradients, border‑radius, padding, crop — that must agree **pixel‑for‑pixel, forever**, because the preview would stay in the browser.
+
+We spent a full session validating parity for *one* path. Two paths means paying that validation on every visual change, indefinitely. **A native compositor is only worth building if it serves the preview too.** That is the whole argument of this document, and the reason it is an architecture decision rather than an optimisation.
+
+## 3. Correcting the framing: "Electron for UI + Tauri for backend"
+
+That combination does not exist. **Electron and Tauri are both application shells** — each provides the window, the webview and the native backend. Electron = web UI + Node backend; Tauri = web UI + Rust backend. You pick one.
+
+The real question is not *Electron vs Tauri*. It is:
+
+> **Where does the compositor live, and how does the preview display its output?**
+
+| option | compositor | preview display | crossing? | parity | other cost |
+|---|---|---|---|---|---|
+| **A. status quo + Phase 4** | Pixi/WebGL in renderer | canvas in the webview | yes (3.0 MB/frame) | ✅ one compositor | — |
+| **A′. Electron, export window with `sandbox:false`** | Pixi/WebGL in renderer | canvas in the webview | **no** | ✅ one compositor | **the sandbox** |
+| **B. Rust compositor, export only** | Rust/wgpu | Pixi (unchanged) | export: no | ❌ **two compositors** | months |
+| **C. Rust compositor + native preview surface** | Rust/wgpu | native surface in the window | no | ⚠️ *promised*, not given (see below) | months + shell migration |
+| **D. Rust compositor, preview via webview** | Rust/wgpu | frames pushed to the webview | **yes, reversed** | ✅ one compositor | months, for nothing |
+
+**A′ is the option this document originally missed, and it matters.** The crossing is not purely architectural — it is partly *configuration*. With `sandbox:false` + `nodeIntegration:true`, the renderer **is** a Node process: it can `spawn('ffmpeg')` and write frames straight to `stdin`. V8 heap → pipe, no Mojo, no structured clone. (That is literally what our 165 fps `pipebench.cjs` measured.) **A′ reaches the same ~165 fps as C, for a boolean.**
+
+We are **not** taking it. The app deliberately runs `nodeIntegration:false` + `contextIsolation:true` with the sandbox on by default, and the editor already spends its security budget on `webSecurity:false`. Turning the sandbox off on the window that handles user-supplied media trades a layer of defence-in-depth for 2×. (The threat model is narrower than it first looks — demuxing is WASM, decoding runs in the GPU process, and ffmpeg only ever sees rawvideo we produced — but an exploit in the WASM demuxer or the WebGL path would land on full Node instead of having to escape a sandbox first. That is a real loss.)
+
+**What A′ does to the case for C:** they buy the *same thing* — removing the crossing, ~165 fps. A′ costs the sandbox; C costs months plus a wgpu compositor. So C is not "the only way to 20×" — it is "the way to 20× that lets us *design* the isolation instead of deleting it" (§3.2 — and only if we actually budget for that design).
+
+**On C's parity column — read it honestly.** It is marked ⚠️, not ✅, on purpose. One compositor gives parity *by construction* only **after** the shadow, webcam mask, cursor and zoom easing are re‑implemented identically in wgpu — which is precisely where parity breaks in practice. Until then the ✅ is a promise, not a state. B is worse still: two compositors permanently, on a product whose entire value is that the export looks like the preview.
+
+D is a trap: pushing preview frames back to the webview reintroduces the exact bottleneck, reversed.
+
+### 3.1 A′′ — sandboxed demux, unsandboxed composite (unverified)
+
+[electron#34905](https://github.com/electron/electron/issues/34905) notes the zero‑copy transfer that fails renderer→main **works renderer→renderer**. That suggests a narrower A′: keep demux/decode in the sandboxed editor renderer, transfer the *decoded* frames zero‑copy to a second, `sandbox:false` window that only ever sees **our own pixels**, and let that window spawn ffmpeg. The lock stays where the hostile input actually arrives.
+
+**This is speculation.** Not measured, not validated. Two things would have to hold: that renderer→renderer transfer really is zero‑copy in our Electron version, and that a renderer holding no untrusted input is meaningfully safer — which is arguable, since it still runs Blink/V8. Worth an hour before anyone spends a month on C; not worth more than that until someone measures it.
+
+### 3.2 Security: "going native" relocates the problem, it does not solve it
+
+An earlier draft justified C partly as *"the way to 20× that keeps the sandbox"*. **That is wrong, and it is the most important correction in this document.**
+
+| | who parses hostile input | is one bug enough? |
+|---|---|---|
+| **A + Phase 4** | `web-demuxer` in **WASM** (confined linear memory) + decode in the **GPU process** (separately sandboxed) | no — needs a parser bug *chained with* a sandbox escape |
+| **A′** | unchanged — still WASM + GPU process | **yes** — *any* renderer bug reaches `require('child_process')` |
+| **C** | **native ffmpeg (C), in‑process, no sandbox at all** | **yes** — any ffmpeg demuxer CVE lands on the machine |
+
+Read the media column again: today the riskiest parser runs in **WASM**. A native core would replace it with **unsandboxed C parsing untrusted containers** — on the media path specifically, **C is a regression against both A and A′**. Media demuxers are among the most CVE‑dense code in existence, which is exactly why browsers confine them.
+
+So the security objection to A′ does **not** convert into an argument for C. The honest distinction is narrower:
+
+- **A′ removes a layer wholesale, with no recourse.** `nodeIntegration` is all‑or‑nothing for that renderer.
+- **C hands us the isolation design.** We *can* run demux/decode in a sandboxed child process (what Chromium does), keep our own logic in memory‑safe Rust, and ship a webview with **no Node** at all (Tauri's IPC is a command allowlist, far narrower than `nodeIntegration`).
+
+That is a **capability, not a property**. If C is built without that isolation, we will have spent months to end up **less safe than today**. Therefore:
+
+> **Requirement (blocking) for C:** demux and decode of user‑supplied media MUST run in a sandboxed child process, not in the core. Any C proposal that does not budget for this is not a C proposal — it is a regression with better fps.
+
+**For the record — this app is a screen recorder.** It legitimately holds screen contents, microphone, camera and the filesystem. An RCE here is worth more to an attacker than in almost any other desktop app. That asymmetry, not the raw CVE odds, is why the 2× does not buy the sandbox.
+
+C is achievable in **either** shell — Electron with a napi‑rs addon plus a native child window, or Tauri natively. Tauri is the natural home (the backend already *is* Rust, and `wry` + a wgpu surface is a known pattern); Electron would be swimming against the process model. **This is what Cap (cap.so) does** — and why its native encoder crates make sense for it: Tauri/Rust, capture + composite + preview all native, no sandboxed renderer in the frame path.
+
+## 4. Target architecture (option C)
+
+```
+┌─ Web UI (React) ─ unchanged ────────────────────────────────┐
+│ timeline, inspector, dialogs, state (Zustand), i18n │
+│ │ commands (seek, set zoom, play…) ▲ events │
+└─────────┼────────────────────────────────────┼──────────────┘
+ ▼ │
+┌─ Rust core ─────────────────────────────────────────────────┐
+│ compositor (wgpu) ← ONE implementation, used by both │
+│ ├── preview → native surface layered in the window │
+│ └── export → frame → encoder, in‑process, no crossing │
+│ decode (ffmpeg/VideoToolbox/MF) │
+│ encode (h264_nvenc / h264_qsv / h264_amf / videotoolbox / │
+│ vaapi — via ff-encode or vendor crates) │
+│ muxing, audio (WSOLA + AAC) │
+└─────────────────────────────────────────────────────────────┘
+```
+
+**Key property:** the compositor is a pure function of `(RenderPlan, time) → frame`. The preview draws it to a surface; the export hands it to an encoder. Same code, same pixels, by construction — parity stops being a thing we validate and becomes a thing we get for free.
+
+### 4.1 What ports, and what does not
+
+| layer | today | native core | effort |
+|---|---|---|---|
+| Web UI, timeline, state | React | **unchanged** | none |
+| `RenderPlan` model | TS (`renderPlan.ts`) | port to Rust (pure data + logic, already unit‑tested) | S |
+| geometry/timing SSOT (`compositeLayout`, `zoomTransform`, zoom spring, auto‑focus, cursor smoothing) | TS, pure | port to Rust — these are pure maths with tests, the cheapest part to move | M |
+| compositor (Pixi/WebGL + Canvas2D layers) | TS | **rewrite in wgpu** — video sprite, wallpaper, 3‑layer shadow, webcam mask, cursor, annotations, 3D rotation, blur/motion‑blur | **L — the real cost** |
+| preview display | `<canvas>` in webview | native wgpu surface in the window | M |
+| decode | WebCodecs + web‑demuxer (WASM) | ffmpeg/native | M |
+| encode | WebCodecs | native (this is the 20×) | S — mostly done in Phase 3 |
+| audio (WSOLA, AAC, concat) | TS, pure + tested | port to Rust | M |
+| capture (WGC, cursor sampler) | already native `.exe` | reuse as‑is or fold into the core | S |
+| STT (whisper) | already native | unchanged | none |
+| shell (IPC, updater, packaging, single‑instance, global shortcuts) | Electron | **rewrite for Tauri** | **L — the hidden cost** |
+
+**The two big items are the wgpu compositor and the shell migration.** Everything the app already ships natively (`wgc-capture.exe`, `cursor-sampler.exe`, whisper) survives.
+
+### 4.2 Why wgpu
+
+One Rust codebase targeting Vulkan/Metal/D3D12 natively — and WebGPU in a browser, which keeps a web build possible. Effects map to shaders; the current 2D‑canvas layers (shadow, masks, cursor) become quads + fragment shaders. Alternative: per‑platform native APIs (Cap's `enc-avfoundation` / `enc-mediafoundation` model) — more code, no portability upside for compositing.
+
+## 5. Honest cost / risk
+
+- **This touches the product's entire visual identity.** Every effect must be re‑implemented and must look *right*, not merely *similar*. The frame‑diff harness from the export work is the gate, but the surface is large.
+- **Shell migration is not free**: Electron APIs in use (`desktopCapturer`, `globalShortcut`, single‑instance lock, auto‑update, notarisation/packaging for 3 OSes, the whole `electron/ipc` surface) all need Tauri equivalents. Some are better in Tauri, some worse.
+- **The team's language mix changes.** Today: TS + a little C++/native. After: a substantial Rust core that the whole team must be able to touch.
+- **It is a months‑scale project**, and it competes with product work for the same 2× that Phase 4 does not deliver (10× → 20×).
+- **Risk of a half‑migration**: option B (export‑only Rust compositor) is the tempting shortcut and it is the worst outcome — two compositors, permanent parity tax. If we start, we must reach C.
+
+## 6. Recommendation & sequencing
+
+1. **Do Phase 4 first, now** (§4.4.1 of the export spec): NV12 packing + move the ffmpeg stdin write off the IPC‑receiving thread. Target ~81–100 fps (**10×**), no architecture change, one compositor, no security trade. It is *not* wasted if we later go native — the encoder selection, the LGPL ffmpeg bundling and the capability probe all survive into the Rust core.
+2. **Exhaust the free levers before paying for anything.** They can close the question outright: **N parallel MessagePorts** (untested — if the crossing goes 387 → ~600 MB/s, Phase 4 becomes encoder‑bound and C loses its entire reason to exist), and moving the stdin write to a `worker_thread` so the IPC‑receiving thread stops serialising with it (that serialisation costs a measured 40 %). A bigger credit window is **already ruled out** — measured 131 fps at window=32 vs 130 at window=8, saturated.
+3. **Then decide C on evidence**, judged against A′ rather than against A: C's actual offer is *"the 2× that A′ gives for free, but without giving up the sandbox"*. That is worth something — it is not worth what this document originally implied.
+4. **Spend an hour on A′′ (§3.1) before spending a month on C.** If renderer→renderer zero‑copy holds up, the crossing may be removable without touching the lock that guards the hostile input.
+5. If we go C: **spike first** — a wgpu compositor rendering *one* clip with zoom + shadow + webcam, frame‑diffed against the current export. That subset is where the ⚠️ in §3's parity column lives. If parity is achievable there at reasonable cost, the rest is grind. If not, we learn cheaply. **Budget the sandboxed demux/decode child process from day one** (§3.2) — it is blocking, not a nice‑to‑have.
+
+**Bottom line:** Phase 4 buys **10×** for weeks, safely. C buys the last **2×** (81 → 165 fps: 6.7 s → 3.3 s on os_parity, ~7.4 → ~3.6 min on a 10‑minute recording) for months, a Rust compositor, a shell migration, and a parity risk that is real. A′ buys that same 2× for a boolean — and we are refusing it on security. Consistency does not *force* refusing C, but not for the reason first written here: C does **not** "keep the sandbox" (§3.2). It relocates the exposure into native code and hands us the isolation design — which is a capability we then have to pay for. Same 2× prize either way; price it as such.
+
+**Retracted:** an earlier draft argued *"measure 4K, the crossing scales with pixels while the encoder degrades more gently, so 4K makes this case much stronger."* That was itself an unmeasured extrapolation — the encoder slows at 4K too (h264_amf drops to roughly 40–50 fps), so the gap narrows rather than widens. 4K does not rescue this case.