Skip to content

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

fix: address review feedback (webcam freeze, O(1) audio accumulator)

my_mac3cb4dd8c68b5
diff --git a/package-lock.json b/package-lock.json
index 02f9cdb1c..b34ad429d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1281,72 +1281,6 @@
"node": ">= 10.0.0"
}
},
- "node_modules/@electron/windows-sign": {
- "version": "1.2.2",
- "resolved": "https://registry.npmjs.org/@electron/windows-sign/-/windows-sign-1.2.2.tgz",
- "integrity": "sha512-dfZeox66AvdPtb2lD8OsIIQh12Tp0GNCRUDfBHIKGpbmopZto2/A8nSpYYLoedPIHpqkeblZ/k8OV0Gy7PYuyQ==",
- "dev": true,
- "license": "BSD-2-Clause",
- "optional": true,
- "peer": true,
- "dependencies": {
- "cross-dirname": "^0.1.0",
- "debug": "^4.3.4",
- "fs-extra": "^11.1.1",
- "minimist": "^1.2.8",
- "postject": "^1.0.0-alpha.6"
- },
- "bin": {
- "electron-windows-sign": "bin/electron-windows-sign.js"
- },
- "engines": {
- "node": ">=14.14"
- }
- },
- "node_modules/@electron/windows-sign/node_modules/fs-extra": {
- "version": "11.3.4",
- "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.3.4.tgz",
- "integrity": "sha512-CTXd6rk/M3/ULNQj8FBqBWHYBVYybQ3VPBw0xGKFe3tuH7ytT6ACnvzpIQ3UZtB8yvUKC2cXn1a+x+5EVQLovA==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "dependencies": {
- "graceful-fs": "^4.2.0",
- "jsonfile": "^6.0.1",
- "universalify": "^2.0.0"
- },
- "engines": {
- "node": ">=14.14"
- }
- },
- "node_modules/@electron/windows-sign/node_modules/jsonfile": {
- "version": "6.2.1",
- "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.2.1.tgz",
- "integrity": "sha512-zwOTdL3rFQ/lRdBnntKVOX6k5cKJwEc1HdilT71BWEu7J41gXIB2MRp+vxduPSwZJPWBxEzv4yH1wYLJGUHX4Q==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "dependencies": {
- "universalify": "^2.0.0"
- },
- "optionalDependencies": {
- "graceful-fs": "^4.1.6"
- }
- },
- "node_modules/@electron/windows-sign/node_modules/universalify": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz",
- "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "engines": {
- "node": ">= 10.0.0"
- }
- },
"node_modules/@esbuild/aix-ppc64": {
"version": "0.27.7",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.7.tgz",
@@ -6307,15 +6241,6 @@
"buffer": "^5.1.0"
}
},
- "node_modules/cross-dirname": {
- "version": "0.1.0",
- "resolved": "https://registry.npmjs.org/cross-dirname/-/cross-dirname-0.1.0.tgz",
- "integrity": "sha512-+R08/oI0nl3vfPcqftZRpytksBXDzOUveBq/NBVx0sUp1axwzPQrKinNx5yd5sxPu8j1wIy8AfnVQ+5eFdha6Q==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true
- },
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
@@ -9756,36 +9681,6 @@
"integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==",
"license": "MIT"
},
- "node_modules/postject": {
- "version": "1.0.0-alpha.6",
- "resolved": "https://registry.npmjs.org/postject/-/postject-1.0.0-alpha.6.tgz",
- "integrity": "sha512-b9Eb8h2eVqNE8edvKdwqkrY6O7kAwmI8kcnBv1NScolYJbo59XUF0noFq+lxbC1yN20bmC0WBEbDC5H/7ASb0A==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "dependencies": {
- "commander": "^9.4.0"
- },
- "bin": {
- "postject": "dist/cli.js"
- },
- "engines": {
- "node": ">=14.0.0"
- }
- },
- "node_modules/postject/node_modules/commander": {
- "version": "9.5.0",
- "resolved": "https://registry.npmjs.org/commander/-/commander-9.5.0.tgz",
- "integrity": "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "engines": {
- "node": "^12.20.0 || >=14"
- }
- },
"node_modules/prebuild-install": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz",
diff --git a/src/components/video-editor/VideoPlayback.tsx b/src/components/video-editor/VideoPlayback.tsx
index 31b8fa89f..8bb6fbe0c 100644
--- a/src/components/video-editor/VideoPlayback.tsx
+++ b/src/components/video-editor/VideoPlayback.tsx
@@ -1847,9 +1847,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
speedRegions.find(
(region) => currentTime * 1000 >= region.startMs && currentTime * 1000 < region.endMs,
) ?? null;
- // Cap the (silent) webcam element at the native rate so playbackRate never
- // throws; the per-frame currentTime resync below keeps it aligned with the
- // stepped playhead in >16× regions.
+ // Cap the (silent) webcam element at the native rate so playbackRate never throws.
webcamVideo.playbackRate = Math.min(
activeSpeedRegion ? activeSpeedRegion.speed : 1,
MAX_NATIVE_PLAYBACK_RATE,
@@ -1863,7 +1861,12 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>(
return;
}
- if (Math.abs(webcamVideo.currentTime - currentTime) > 0.15) {
+ // While the main video is seek-stepping (>16×), the playhead advances far
+ // faster than this 16×-capped element can seek; resyncing every frame would
+ // keep its decoder perpetually mid-seek and freeze it — the same failure the
+ // main video's throttle fixes. Let the muted webcam play best-effort at the
+ // clamped rate instead (a small lag is fine).
+ if (!seekSteppingRef.current && Math.abs(webcamVideo.currentTime - currentTime) > 0.15) {
webcamVideo.currentTime = currentTime;
}
diff --git a/src/lib/exporter/audioEncoder.ts b/src/lib/exporter/audioEncoder.ts
index ec715880c..05bc44aa2 100644
--- a/src/lib/exporter/audioEncoder.ts
+++ b/src/lib/exporter/audioEncoder.ts
@@ -6,6 +6,7 @@ import {
} from "@/components/video-editor/types";
import { WsolaTimeStretcher } from "./audioTimeStretch";
import type { ExportAudioMuxerCodec, VideoMuxer } from "./muxer";
+import { PlanarChunkQueue } from "./planarChunkQueue";
import {
buildSpeedSegments,
maxTimelineSpeed,
@@ -684,29 +685,26 @@ export class AudioProcessor {
});
encoder.configure(encodeConfig);
- // Output-side accumulator: buffered stretched PCM flushed as fixed-size
- // AudioData slices with running (monotonic) output timestamps.
- const outAcc: Float32Array[] = Array.from(
- { length: outputChannels },
- () => new Float32Array(0),
- );
+ // Output-side accumulator: a chunk queue (O(1) append, front-drained) coalesced
+ // into fixed-size AudioData slices with running (monotonic) output timestamps.
+ const outQueue = new PlanarChunkQueue(outputChannels);
let outTimestampSamples = 0;
+ const appendOutput = (planes: Float32Array[], count: number) => {
+ outQueue.push(planes, count);
+ };
+
const flushOutput = (drainAll: boolean) => {
- while (outAcc[0].length >= OFFLINE_OUTPUT_FRAMES || (drainAll && outAcc[0].length > 0)) {
- const take = Math.min(OFFLINE_OUTPUT_FRAMES, outAcc[0].length);
- const data = new Float32Array(take * outputChannels);
- for (let c = 0; c < outputChannels; c++) {
- data.set(outAcc[c].subarray(0, take), c * take);
- outAcc[c] = outAcc[c].slice(take);
- }
+ while (outQueue.length >= OFFLINE_OUTPUT_FRAMES || (drainAll && outQueue.length > 0)) {
+ const take = Math.min(OFFLINE_OUTPUT_FRAMES, outQueue.length);
+ const data = outQueue.take(take);
const audioData = new AudioData({
format: "f32-planar",
sampleRate: outputSampleRate,
numberOfFrames: take,
numberOfChannels: outputChannels,
timestamp: Math.round((outTimestampSamples / outputSampleRate) * 1_000_000),
- data: data.buffer,
+ data: data.buffer as ArrayBuffer,
});
outTimestampSamples += take;
encoder.encode(audioData);
@@ -714,16 +712,6 @@ export class AudioProcessor {
}
};
- const appendOutput = (planes: Float32Array[], count: number) => {
- if (count <= 0) return;
- for (let c = 0; c < outputChannels; c++) {
- const merged = new Float32Array(outAcc[c].length + count);
- merged.set(outAcc[c], 0);
- merged.set(planes[c].subarray(0, count), outAcc[c].length);
- outAcc[c] = merged;
- }
- };
-
// Per-segment state.
let segIdx = 0;
let stretcher: WsolaTimeStretcher | null = null;
@@ -761,6 +749,9 @@ export class AudioProcessor {
const finalizeSegment = () => {
if (!stretcher) return;
emitStretched(stretcher.flush());
+ // Intentional: pad the deficit with silence when WSOLA under-fills a short
+ // high-speed segment, so the segment keeps its exact A/V-locked length. Do
+ // not "fix" this by removing the pad.
if (segEmittedOut < segExpectedOut) {
const pad = segExpectedOut - segEmittedOut;
const silence = Array.from({ length: outputChannels }, () => new Float32Array(pad));
@@ -824,6 +815,10 @@ export class AudioProcessor {
data.copyTo(plane, { format: "f32-planar", planeIndex: c });
srcPlanes.push(plane);
}
+ // Raw source timestamps — the same origin the video decoder uses
+ // (streamingDecoder), so audio and video stay in one coordinate system.
+ // Frames before the first segment start (e.g. negative-timestamp codec
+ // preroll, or a leading trim) are correctly discarded by routeFrame.
const frameStartSample = Math.round((data.timestamp / 1_000_000) * sampleRate);
data.close();
diff --git a/src/lib/exporter/planarChunkQueue.test.ts b/src/lib/exporter/planarChunkQueue.test.ts
new file mode 100644
index 000000000..b1da7a407
--- /dev/null
+++ b/src/lib/exporter/planarChunkQueue.test.ts
@@ -0,0 +1,83 @@
+import { describe, expect, it } from "vitest";
+import { PlanarChunkQueue } from "./planarChunkQueue";
+
+function ramp(from: number, count: number): Float32Array {
+ const a = new Float32Array(count);
+ for (let i = 0; i < count; i++) a[i] = from + i;
+ return a;
+}
+
+describe("PlanarChunkQueue", () => {
+ it("tracks buffered length across pushes and takes", () => {
+ const q = new PlanarChunkQueue(1);
+ expect(q.length).toBe(0);
+ q.push([ramp(0, 10)], 10);
+ q.push([ramp(10, 5)], 5);
+ expect(q.length).toBe(15);
+ q.take(6);
+ expect(q.length).toBe(9);
+ });
+
+ it("drains samples in FIFO order across chunk boundaries (mono)", () => {
+ const q = new PlanarChunkQueue(1);
+ q.push([ramp(0, 4)], 4); // 0..3
+ q.push([ramp(4, 4)], 4); // 4..7
+ q.push([ramp(8, 4)], 4); // 8..11
+ // A take that spans multiple chunks and stops mid-chunk.
+ expect(Array.from(q.take(5))).toEqual([0, 1, 2, 3, 4]);
+ expect(Array.from(q.take(5))).toEqual([5, 6, 7, 8, 9]);
+ expect(Array.from(q.take(2))).toEqual([10, 11]);
+ expect(q.length).toBe(0);
+ });
+
+ it("keeps channels separate and lays them out planar [ch0…, ch1…]", () => {
+ const q = new PlanarChunkQueue(2);
+ q.push([ramp(0, 3), ramp(100, 3)], 3); // L:0,1,2 R:100,101,102
+ q.push([ramp(3, 3), ramp(103, 3)], 3); // L:3,4,5 R:103,104,105
+ const out = q.take(4);
+ // First 4 = left, next 4 = right.
+ expect(Array.from(out.subarray(0, 4))).toEqual([0, 1, 2, 3]);
+ expect(Array.from(out.subarray(4, 8))).toEqual([100, 101, 102, 103]);
+ expect(q.length).toBe(2);
+ const rest = q.take(2);
+ expect(Array.from(rest.subarray(0, 2))).toEqual([4, 5]);
+ expect(Array.from(rest.subarray(2, 4))).toEqual([104, 105]);
+ });
+
+ it("honors a partial chunk consumed by an earlier take before dropping it", () => {
+ const q = new PlanarChunkQueue(1);
+ q.push([ramp(0, 10)], 10);
+ expect(Array.from(q.take(3))).toEqual([0, 1, 2]); // leaves headOffset=3 in chunk 0
+ q.push([ramp(10, 3)], 3);
+ expect(Array.from(q.take(9))).toEqual([3, 4, 5, 6, 7, 8, 9, 10, 11]);
+ expect(q.length).toBe(1);
+ expect(Array.from(q.take(1))).toEqual([12]);
+ });
+
+ it("ignores non-positive pushes/takes", () => {
+ const q = new PlanarChunkQueue(1);
+ q.push([ramp(0, 5)], 0);
+ expect(q.length).toBe(0);
+ expect(Array.from(q.take(0))).toEqual([]);
+ });
+
+ it("reconstructs a long stream fed in odd-sized chunks and drained in fixed slices", () => {
+ const q = new PlanarChunkQueue(1);
+ const total = 5000;
+ let produced = 0;
+ const collected: number[] = [];
+ // Feed odd chunk sizes, drain 512 whenever enough is buffered.
+ for (let chunk = 1; produced < total; chunk++) {
+ const n = Math.min(chunk, total - produced);
+ q.push([ramp(produced, n)], n);
+ produced += n;
+ while (q.length >= 512) collected.push(...q.take(512));
+ }
+ while (q.length > 0) collected.push(...q.take(Math.min(512, q.length)));
+ expect(collected.length).toBe(total);
+ expect(collected[0]).toBe(0);
+ expect(collected[total - 1]).toBe(total - 1);
+ // Strictly increasing ⇒ no reorder/drop/dupe.
+ expect(collected.every((v, i) => v === i)).toBe(true);
+ });
+});
diff --git a/src/lib/exporter/planarChunkQueue.ts b/src/lib/exporter/planarChunkQueue.ts
new file mode 100644
index 000000000..d0d9b4018
--- /dev/null
+++ b/src/lib/exporter/planarChunkQueue.ts
@@ -0,0 +1,76 @@
+// FIFO queue of planar PCM chunks with O(1) amortized append and front-drain.
+//
+// The offline audio time-stretch path produces output in many small chunks and
+// drains fixed-size slices to the encoder. A grow-and-copy accumulator would be
+// O(N^2) over a long run; this keeps each chunk by reference and only copies the
+// samples that are actually pulled out (O(take) per drain, O(total) overall).
+
+export class PlanarChunkQueue {
+ private readonly channels: number;
+ private readonly queue: Float32Array[][];
+ private headOffset = 0; // consumed samples into queue[*][0] (shared across channels)
+ private queuedLen = 0;
+
+ constructor(channels: number) {
+ this.channels = Math.max(1, channels);
+ this.queue = Array.from({ length: this.channels }, () => []);
+ }
+
+ /** Samples currently buffered (per channel). */
+ get length(): number {
+ return this.queuedLen;
+ }
+
+ /** Append the first `count` samples of each channel plane (stored by reference). */
+ push(planes: Float32Array[], count: number): void {
+ if (count <= 0) return;
+ for (let c = 0; c < this.channels; c++) {
+ this.queue[c].push((planes[c] ?? planes[0]).subarray(0, count));
+ }
+ this.queuedLen += count;
+ }
+
+ /**
+ * Remove `take` samples from the front and return them as one planar buffer laid
+ * out [ch0 samples…, ch1 samples…, …]. `take` must be ≤ length.
+ */
+ take(take: number): Float32Array {
+ if (take <= 0) return new Float32Array(0);
+ const data = new Float32Array(take * this.channels);
+ for (let c = 0; c < this.channels; c++) {
+ let need = take;
+ let ci = 0;
+ let off = this.headOffset;
+ let dst = c * take;
+ while (need > 0) {
+ const chunk = this.queue[c][ci];
+ const avail = chunk.length - off;
+ const n = avail < need ? avail : need;
+ data.set(chunk.subarray(off, off + n), dst);
+ dst += n;
+ need -= n;
+ if (n < avail) {
+ off += n;
+ } else {
+ ci += 1;
+ off = 0;
+ }
+ }
+ }
+ // Advance the shared head / drop fully consumed chunks (identical layout per channel).
+ let remaining = take;
+ while (remaining > 0) {
+ const avail = this.queue[0][0].length - this.headOffset;
+ if (avail <= remaining) {
+ for (let c = 0; c < this.channels; c++) this.queue[c].shift();
+ this.headOffset = 0;
+ remaining -= avail;
+ } else {
+ this.headOffset += remaining;
+ remaining = 0;
+ }
+ }
+ this.queuedLen -= take;
+ return data;
+ }
+}
diff --git a/src/lib/exporter/videoExporter.test.ts b/src/lib/exporter/videoExporter.test.ts
index 022766533..176f5f8ed 100644
--- a/src/lib/exporter/videoExporter.test.ts
+++ b/src/lib/exporter/videoExporter.test.ts
@@ -52,8 +52,8 @@ describe("isSourceCopyFastPathEligible", () => {
videoInfo,
),
).toBe(false);
- // A 100× region must also block the copy path so its audio is re-rendered
- // (offline time-stretch) rather than passed through untouched.
+ // A 100× region must also block the copy path (any non-1× speed does), so its
+ // audio is re-rendered (offline time-stretch) rather than passed through untouched.
expect(
isSourceCopyFastPathEligible(
createConfig({