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(linux): compact cursor telemetry across pauses

Steffen Butzer3957984ee25d
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 1724d695d..ee36e105f 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -48,7 +48,6 @@ import {
import {
AI_EDITION_OPEN_PROJECT_CHANNEL,
type CursorRecordingData,
- type CursorRecordingSample,
type ProjectFileResult,
type ProjectPathResult,
} from "../../src/native/contracts";
@@ -102,6 +101,7 @@ import {
type LinuxCaptureSourceKind,
LinuxNativeCaptureSession,
} from "../native-bridge/capture/linuxNativeCaptureSession";
+import { compactCursorTelemetryPauseRanges } from "../native-bridge/cursor/recording/cursorPauseRanges";
import { createCursorRecordingSession } from "../native-bridge/cursor/recording/factory";
import {
isMacCursorHelperUnavailable,
@@ -1428,45 +1428,11 @@ function shiftPendingCursorTelemetry(offsetMs: number) {
function compactPendingCursorTelemetryPauseRanges(
ranges: Array<{ startMs: number; endMs: number }>,
) {
- if (!pendingCursorRecordingData || ranges.length === 0) {
- return;
- }
-
- const normalizedRanges = ranges
- .map((range) => ({
- startMs: Math.max(0, Math.min(range.startMs, range.endMs)),
- endMs: Math.max(0, Math.max(range.startMs, range.endMs)),
- }))
- .filter((range) => Number.isFinite(range.startMs) && Number.isFinite(range.endMs))
- .filter((range) => range.endMs > range.startMs)
- .sort((a, b) => a.startMs - b.startMs);
-
- if (normalizedRanges.length === 0) {
- return;
- }
-
- pendingCursorRecordingData = {
- ...pendingCursorRecordingData,
- samples: pendingCursorRecordingData.samples
- .map((sample) => {
- let pausedBeforeSampleMs = 0;
- for (const range of normalizedRanges) {
- if (sample.timeMs >= range.startMs && sample.timeMs <= range.endMs) {
- return null;
- }
- if (sample.timeMs > range.endMs) {
- pausedBeforeSampleMs += range.endMs - range.startMs;
- }
- }
-
- return {
- ...sample,
- timeMs: Math.max(0, sample.timeMs - pausedBeforeSampleMs),
- };
- })
- .filter((sample): sample is CursorRecordingSample => Boolean(sample))
- .sort((a, b) => a.timeMs - b.timeMs),
- };
+ if (!pendingCursorRecordingData) return;
+ pendingCursorRecordingData = compactCursorTelemetryPauseRanges(
+ pendingCursorRecordingData,
+ ranges,
+ );
}
function completeNativeMacCursorPauseRange(endMs = Date.now()) {
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
index 7af0409be..96bdfe32b 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
@@ -85,6 +85,45 @@ function flushStdout() {
return new Promise((resolve) => setTimeout(resolve, 0));
}
+function emitCaptureStarted(timestampMs: number) {
+ helper.emitEvent({
+ event: "capture-started",
+ timestampMs,
+ path: "/tmp/recording.mp4",
+ width: 800,
+ height: 600,
+ fps: 30,
+ });
+}
+
+function emitCursorSample(timestampMs: number) {
+ helper.emitEvent({
+ event: "cursor-sample",
+ timestampMs,
+ x: 400,
+ y: 300,
+ width: 800,
+ height: 600,
+ visible: true,
+ });
+}
+
+async function finishCapture(session: LinuxNativeCaptureSession, timestampMs: number) {
+ const stopping = session.stop();
+ await flushStdout();
+ helper.emitEvent({
+ event: "capture-stopped",
+ timestampMs,
+ path: "/tmp/recording.mp4",
+ durationMs: timestampMs - 1_000,
+ frames: 27,
+ dropped: 0,
+ });
+ await flushStdout();
+ helper.emit("exit", 0, null);
+ return stopping;
+}
+
/** Drives a session to a finished recording, optionally granting a source kind. */
async function record(sourceKind?: string) {
const session = newSession();
@@ -435,6 +474,59 @@ describe("LinuxNativeCaptureSession", () => {
await expect(session.waitUntilCapturing()).resolves.toBeUndefined();
});
+ it("removes cursor samples from repeated pauses and compacts later timestamps", async () => {
+ const now = vi.spyOn(Date, "now").mockReturnValue(900);
+ const session = newSession();
+ await startReady(session);
+
+ emitCaptureStarted(1_000);
+ emitCursorSample(1_100);
+ await flushStdout();
+
+ now.mockReturnValue(1_200);
+ session.pause();
+ emitCursorSample(1_250);
+ now.mockReturnValue(1_400);
+ session.resume();
+ emitCursorSample(1_500);
+
+ now.mockReturnValue(1_600);
+ session.pause();
+ emitCursorSample(1_650);
+ now.mockReturnValue(1_800);
+ session.resume();
+ emitCursorSample(1_900);
+ await flushStdout();
+
+ now.mockReturnValue(2_000);
+ const result = await finishCapture(session, 2_000);
+
+ expect(result.cursor.samples.map((sample) => sample.timeMs)).toEqual([100, 300, 500]);
+ expect(helper.stdinWrites.filter((line) => line === "pause\n")).toHaveLength(2);
+ expect(helper.stdinWrites.filter((line) => line === "resume\n")).toHaveLength(2);
+ });
+
+ it("closes the cursor pause range when the recording stops while paused", async () => {
+ const now = vi.spyOn(Date, "now").mockReturnValue(900);
+ const session = newSession();
+ await startReady(session);
+
+ emitCaptureStarted(1_000);
+ emitCursorSample(1_100);
+ await flushStdout();
+
+ now.mockReturnValue(1_200);
+ session.pause();
+ emitCursorSample(1_300);
+ await flushStdout();
+
+ now.mockReturnValue(1_500);
+ const result = await finishCapture(session, 1_500);
+
+ expect(result.cursor.samples.map((sample) => sample.timeMs)).toEqual([100]);
+ expect(helper.stdinWrites).not.toContain("resume\n");
+ });
+
/**
* A non-fatal `error` event after the first frame does not end the capture;
* a later wait is still satisfied.
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
index 0c65c13d5..64f47e25e 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
@@ -1,6 +1,10 @@
import { type ChildProcessByStdio, spawn } from "node:child_process";
import type { Readable, Writable } from "node:stream";
import type { CursorRecordingData } from "../../../src/native/contracts";
+import {
+ type CursorPauseRange,
+ compactCursorTelemetryPauseRanges,
+} from "../cursor/recording/cursorPauseRanges";
import {
NdjsonLineReader,
PipeWireCursorAccumulator,
@@ -114,6 +118,9 @@ export class LinuxNativeCaptureSession {
private lastError: string | null = null;
private paused = false;
private helperOutputText = "";
+ private cursorRecordingStartMs = 0;
+ private cursorPauseStartedAtMs: number | null = null;
+ private cursorPauseRanges: CursorPauseRange[] = [];
constructor(private readonly config: LinuxCaptureConfig) {
this.cursor = new PipeWireCursorAccumulator(config.maxCursorSamples);
@@ -134,6 +141,10 @@ export class LinuxNativeCaptureSession {
this.cursor.reset(Date.now());
this.lines.reset();
+ this.cursorRecordingStartMs = 0;
+ this.cursorPauseStartedAtMs = null;
+ this.cursorPauseRanges = [];
+ this.paused = false;
const request = {
outputPath: this.config.outputPath,
@@ -305,16 +316,27 @@ export class LinuxNativeCaptureSession {
if (this.paused) {
return;
}
- this.paused = true;
this.write("pause");
+ this.paused = true;
+ this.cursorPauseStartedAtMs = Date.now();
}
resume() {
if (!this.paused) {
return;
}
- this.paused = false;
this.write("resume");
+ this.completeCursorPauseRange();
+ this.paused = false;
+ }
+
+ private completeCursorPauseRange(endMs = Date.now()) {
+ if (this.cursorPauseStartedAtMs === null || this.cursorRecordingStartMs <= 0) return;
+ this.cursorPauseRanges.push({
+ startMs: Math.max(0, this.cursorPauseStartedAtMs - this.cursorRecordingStartMs),
+ endMs: Math.max(0, endMs - this.cursorRecordingStartMs),
+ });
+ this.cursorPauseStartedAtMs = null;
}
get isPaused() {
@@ -375,6 +397,7 @@ export class LinuxNativeCaptureSession {
throw new Error("The Linux capture helper is not running.");
}
+ this.completeCursorPauseRange();
const exited = new Promise<void>((resolve, reject) => {
this.stoppedResolve = resolve;
this.stoppedReject = reject;
@@ -518,6 +541,7 @@ export class LinuxNativeCaptureSession {
// telemetry is re-based onto it and anything from during the
// picker is dropped rather than left pinned to the start.
this.cursor.rebase(payload.timestampMs);
+ this.cursorRecordingStartMs = payload.timestampMs;
this.capturing = true;
console.info(
"[capture-linux] capture started",
@@ -538,7 +562,10 @@ export class LinuxNativeCaptureSession {
durationMs: payload.durationMs,
frames: payload.frames,
droppedFrames: payload.dropped,
- cursor: this.cursor.toRecordingData(),
+ cursor: compactCursorTelemetryPauseRanges(
+ this.cursor.toRecordingData(),
+ this.cursorPauseRanges,
+ ),
...(this.sourceKind ? { sourceKind: this.sourceKind } : {}),
...(this.videoEncoder ? { videoEncoder: this.videoEncoder } : {}),
};
diff --git a/electron/native-bridge/cursor/recording/cursorPauseRanges.test.ts b/electron/native-bridge/cursor/recording/cursorPauseRanges.test.ts
new file mode 100644
index 000000000..858438531
--- /dev/null
+++ b/electron/native-bridge/cursor/recording/cursorPauseRanges.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from "vitest";
+import type { CursorRecordingData } from "../../../../src/native/contracts";
+import { compactCursorTelemetryPauseRanges } from "./cursorPauseRanges";
+
+function recording(...times: number[]): CursorRecordingData {
+ return {
+ version: 2,
+ provider: "none",
+ samples: times.map((timeMs) => ({ timeMs, cx: 0.5, cy: 0.5 })),
+ assets: [],
+ };
+}
+
+describe("compactCursorTelemetryPauseRanges", () => {
+ it("removes paused samples and shifts later samples across repeated pauses", () => {
+ const compacted = compactCursorTelemetryPauseRanges(recording(100, 250, 500, 650, 900), [
+ { startMs: 200, endMs: 400 },
+ { startMs: 600, endMs: 800 },
+ ]);
+
+ expect(compacted.samples.map((sample) => sample.timeMs)).toEqual([100, 300, 500]);
+ });
+
+ it("ignores invalid ranges without replacing the recording", () => {
+ const input = recording(100, 200);
+
+ expect(
+ compactCursorTelemetryPauseRanges(input, [
+ { startMs: 100, endMs: 100 },
+ { startMs: Number.NaN, endMs: 200 },
+ ]),
+ ).toBe(input);
+ });
+});
diff --git a/electron/native-bridge/cursor/recording/cursorPauseRanges.ts b/electron/native-bridge/cursor/recording/cursorPauseRanges.ts
new file mode 100644
index 000000000..1cd73e127
--- /dev/null
+++ b/electron/native-bridge/cursor/recording/cursorPauseRanges.ts
@@ -0,0 +1,44 @@
+import type { CursorRecordingData, CursorRecordingSample } from "../../../../src/native/contracts";
+
+export interface CursorPauseRange {
+ startMs: number;
+ endMs: number;
+}
+
+export function compactCursorTelemetryPauseRanges(
+ recording: CursorRecordingData,
+ ranges: CursorPauseRange[],
+): CursorRecordingData {
+ if (ranges.length === 0) return recording;
+
+ const normalizedRanges = ranges
+ .map((range) => ({
+ startMs: Math.max(0, Math.min(range.startMs, range.endMs)),
+ endMs: Math.max(0, Math.max(range.startMs, range.endMs)),
+ }))
+ .filter((range) => Number.isFinite(range.startMs) && Number.isFinite(range.endMs))
+ .filter((range) => range.endMs > range.startMs)
+ .sort((a, b) => a.startMs - b.startMs);
+
+ if (normalizedRanges.length === 0) return recording;
+
+ return {
+ ...recording,
+ samples: recording.samples
+ .map((sample) => {
+ let pausedBeforeSampleMs = 0;
+ for (const range of normalizedRanges) {
+ if (sample.timeMs >= range.startMs && sample.timeMs <= range.endMs) {
+ return null;
+ }
+ if (sample.timeMs > range.endMs) {
+ pausedBeforeSampleMs += range.endMs - range.startMs;
+ }
+ }
+
+ return { ...sample, timeMs: Math.max(0, sample.timeMs - pausedBeforeSampleMs) };
+ })
+ .filter((sample): sample is CursorRecordingSample => Boolean(sample))
+ .sort((a, b) => a.timeMs - b.timeMs),
+ };
+}