Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/hooks/useScreenRecorder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import {
createProcessedMicrophoneConstraints,
normalizeBrowserMicrophoneProfile,
resolveBrowserCaptureCursorPolicy,
} from "./useScreenRecorder";

type RecordingState = "inactive" | "recording" | "paused";
Expand Down Expand Up @@ -108,6 +109,26 @@ describe("createProcessedMicrophoneConstraints", () => {
});
});

describe("resolveBrowserCaptureCursorPolicy", () => {
it("preserves the existing hidden-cursor browser policy by default", () => {
expect(resolveBrowserCaptureCursorPolicy()).toEqual({
streamCursor: "never",
hideOsCursorBeforeRecording: true,
hideEditorOverlayCursorByDefault: true,
});
});

it("uses the browser captured cursor after native Windows capture fails to start", () => {
expect(
resolveBrowserCaptureCursorPolicy({ nativeWindowsCaptureStartFailed: true }),
).toEqual({
streamCursor: "always",
hideOsCursorBeforeRecording: false,
hideEditorOverlayCursorByDefault: true,
});
});
});

function stopRecording(
recorder: ReturnType<typeof createMockMediaRecorder>,
isNativeRecording: boolean,
Expand Down
58 changes: 48 additions & 10 deletions src/hooks/useScreenRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export type BrowserMicrophoneProfile =
| "no-echo"
| "no-noise-suppression"
| "raw";
type BrowserCaptureCursorMode = "always" | "never";
export type BrowserCaptureCursorPolicy = {
streamCursor: BrowserCaptureCursorMode;
hideOsCursorBeforeRecording: boolean;
hideEditorOverlayCursorByDefault: boolean;
};
const DEFAULT_BROWSER_MICROPHONE_PROFILE: BrowserMicrophoneProfile = "no-agc";
const BROWSER_MICROPHONE_PROFILES = new Set<BrowserMicrophoneProfile>([
"processed",
Expand Down Expand Up @@ -183,6 +189,28 @@ export function normalizeBrowserMicrophoneProfile(value?: string | null): Browse
: DEFAULT_BROWSER_MICROPHONE_PROFILE;
}

export function resolveBrowserCaptureCursorPolicy({
nativeWindowsCaptureStartFailed = false,
}: {
nativeWindowsCaptureStartFailed?: boolean;
} = {}): BrowserCaptureCursorPolicy {
if (nativeWindowsCaptureStartFailed) {
// If WGC already failed, avoid the telemetry overlay path that can lag on
// constrained Windows systems; keep the browser-captured cursor instead.
return {
streamCursor: "always",
hideOsCursorBeforeRecording: false,
hideEditorOverlayCursorByDefault: true,
};
}

return {
streamCursor: "never",
hideOsCursorBeforeRecording: true,
hideEditorOverlayCursorByDefault: true,
};
}

export function createProcessedMicrophoneConstraints(
microphoneDeviceId?: string,
profile: BrowserMicrophoneProfile = DEFAULT_BROWSER_MICROPHONE_PROFILE,
Expand Down Expand Up @@ -1331,6 +1359,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
typeof window.electronAPI.startNativeScreenRecording === "function";

let useNativeWindowsCapture = false;
let nativeWindowsCaptureStartFailed = false;
if (
platform === "win32" &&
(selectedSource.id?.startsWith("screen:") ||
Expand Down Expand Up @@ -1385,7 +1414,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
);
if (!nativeResult.success) {
if (useNativeWindowsCapture) {
hideEditorOverlayCursorByDefault.current = true;
nativeWindowsCaptureStartFailed = true;
console.warn(
"Native Windows capture failed, falling back to browser capture:",
nativeResult.error ?? nativeResult.message,
Expand Down Expand Up @@ -1495,7 +1524,11 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
}
}

hideEditorOverlayCursorByDefault.current = true;
const browserCursorPolicy = resolveBrowserCaptureCursorPolicy({
nativeWindowsCaptureStartFailed,
});
hideEditorOverlayCursorByDefault.current =
browserCursorPolicy.hideEditorOverlayCursorByDefault;

const wantsAudioCapture = microphoneEnabled || systemAudioEnabled;
const browserCaptureSource = await resolveBrowserCaptureSource(selectedSource);
Expand All @@ -1509,10 +1542,15 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
);
}

try {
await window.electronAPI.hideOsCursor?.();
} catch {
console.warn("Could not hide OS cursor before recording.");
if (browserCursorPolicy.hideOsCursorBeforeRecording) {
try {
const hideCursorResult = await window.electronAPI.hideOsCursor?.();
if (hideCursorResult && !hideCursorResult.success) {
console.warn("Could not hide OS cursor before recording.", hideCursorResult);
}
} catch {
console.warn("Could not hide OS cursor before recording.");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

let videoTrack: MediaStreamTrack | undefined;
Expand All @@ -1527,9 +1565,9 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
maxHeight: TARGET_HEIGHT,
maxFrameRate: TARGET_FRAME_RATE,
minFrameRate: MIN_FRAME_RATE,
googCaptureCursor: false,
googCaptureCursor: browserCursorPolicy.streamCursor === "always",
},
cursor: "never" as const,
cursor: browserCursorPolicy.streamCursor,
};

if (wantsAudioCapture) {
Expand All @@ -1542,7 +1580,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
cursor: "never",
cursor: browserCursorPolicy.streamCursor,
},
selfBrowserSurface: "exclude",
surfaceSwitching: "exclude",
Expand Down Expand Up @@ -1653,7 +1691,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
cursor: "never",
cursor: browserCursorPolicy.streamCursor,
},
selfBrowserSurface: "exclude",
surfaceSwitching: "exclude",
Expand Down