Skip to content

Commit 29736d4

Browse files
committed
fix(macos): record without the microphone when macOS lists no audio input
The capture helper set ScreenCaptureKit's captureMicrophone whenever the request asked for the microphone, input or not. On a Mac with no audio input, that take starts, and the next one in the same picker session stalls in startCapture() after `ready`: the take after a Restart is lost. The helper now records without the microphone when AVFoundation lists no audio input and reports `microphone-not-found`. The main process passes it on as `microphoneNotFound`, and the renderer shows the existing "No microphone found" toast, also when its own pre-check drops a stored "on". macOS 13 and 14 keep `microphone-unavailable`. Part of #995
1 parent bfdf1c0 commit 29736d4

11 files changed

Lines changed: 128 additions & 25 deletions

File tree

‎electron/ipc/handlers.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import {
127127
isSalvageableFragmentedCapture,
128128
NATIVE_WINDOWS_SALVAGEABLE_OUTPUT_BYTES,
129129
readMicrophoneDefaulted,
130+
readMicrophoneNotFound,
130131
readMicrophoneUnavailable,
131132
readSecondaryWindowsApplied,
132133
readWebcamFormat,
@@ -3286,6 +3287,11 @@ export function registerIpcHandlers(
32863287
{ macOS: process.getSystemVersion() },
32873288
);
32883289
}
3290+
const microphoneNotFound =
3291+
request.audio.microphone.enabled && readMicrophoneNotFound(nativeMacCaptureOutput);
3292+
if (microphoneNotFound) {
3293+
console.warn("[native-sck] recording without the microphone; macOS lists no audio input");
3294+
}
32893295
nativeMacCursorOffsetMs =
32903296
cursorCaptureMode === "editable-overlay"
32913297
? Math.max(0, captureStartedAtMs - cursorStartTimeMs)
@@ -3304,6 +3310,7 @@ export function registerIpcHandlers(
33043310
startedAtMs: captureStartedAtMs,
33053311
microphoneDefaulted,
33063312
microphoneUnavailable,
3313+
microphoneNotFound,
33073314
};
33083315
} catch (error) {
33093316
console.error("Failed to start native macOS recording:", error);

‎electron/native/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ npm run build:native:mac
2727

2828
On non-macOS hosts this command exits successfully and does not affect Windows/Linux development. On macOS it builds the Swift package at `electron/native/screencapturekit`, writes the development binaries to `electron/native/screencapturekit/build`, and copies redistributable binaries to `electron/native/bin/darwin-${arch}`.
2929

30-
The current helper implementation supports display/window ScreenCaptureKit video capture, control-window exclusion through the request's optional `excludedWindowIds` and `SCContentFilter(display:excludingWindows:)`, cursor exclusion through `SCStreamConfiguration.showsCursor`, H.264 encoding, MP4 muxing (with `AVAssetWriter.movieFragmentInterval` at 1s, so a helper that dies before `finishWriting()` still leaves a readable file — same reasoning as the Windows fragmented sink below), and ScreenCaptureKit system audio. Electron supplies only the HUD and an open Notes window as exclusions for a direct full-display capture, so ordinary OpenScreen windows remain recordable there. The helper adds Notification Center's windows on its own, and Finder's desktop-icon windows when the request sets `hideDesktopIcons`; a banner appears as a new window, so a direct capture re-reads the exclusion every 250 ms, while the system picker excludes Notification Center by bundle id. It excludes OpenScreen itself the same way, so a screen picked in the editor's Record mode still leaves out the HUD that only appears at Start: a picker capture records none of OpenScreen's windows, and window mode does not offer them. The helper reports this as `excludesApp` on `picker-selected`. The helper also attempts native ScreenCaptureKit microphone capture when the running macOS version exposes that capability, which is macOS 15 and later. Below that it records the take without the microphone and emits a `microphone-unavailable` warning; the app does not offer the microphone there (`canRecordMicrophone` in `src/utils/platformUtils.ts`). Webcam recording currently stays as an Electron sidecar and is attached to the same recording session after the native screen capture stops.
30+
The current helper implementation supports display/window ScreenCaptureKit video capture, control-window exclusion through the request's optional `excludedWindowIds` and `SCContentFilter(display:excludingWindows:)`, cursor exclusion through `SCStreamConfiguration.showsCursor`, H.264 encoding, MP4 muxing (with `AVAssetWriter.movieFragmentInterval` at 1s, so a helper that dies before `finishWriting()` still leaves a readable file — same reasoning as the Windows fragmented sink below), and ScreenCaptureKit system audio. Electron supplies only the HUD and an open Notes window as exclusions for a direct full-display capture, so ordinary OpenScreen windows remain recordable there. The helper adds Notification Center's windows on its own, and Finder's desktop-icon windows when the request sets `hideDesktopIcons`; a banner appears as a new window, so a direct capture re-reads the exclusion every 250 ms, while the system picker excludes Notification Center by bundle id. It excludes OpenScreen itself the same way, so a screen picked in the editor's Record mode still leaves out the HUD that only appears at Start: a picker capture records none of OpenScreen's windows, and window mode does not offer them. The helper reports this as `excludesApp` on `picker-selected`. The helper also attempts native ScreenCaptureKit microphone capture when the running macOS version exposes that capability, which is macOS 15 and later. Below that it records the take without the microphone and emits a `microphone-unavailable` warning; the app does not offer the microphone there (`canRecordMicrophone` in `src/utils/platformUtils.ts`). With no audio input listed it does the same and emits `microphone-not-found`: asked for a microphone anyway, ScreenCaptureKit starts that take and stalls the next one in the picker session (#995). Webcam recording currently stays as an Electron sidecar and is attached to the same recording session after the native screen capture stops.
3131

3232
Electron exposes `is-native-mac-capture-available` for capability probing. It resolves the same helper locations listed above and reports `missing-helper` until a Swift helper binary is present. When available, macOS recording routes screen/window capture through the native helper so editable cursor recordings do not bake the system cursor into the video. Cursor positions are sampled in Electron; when the cursor helper is available and Accessibility is granted, samples are also tagged with link/text cursor hints such as `pointer`.
3333

‎electron/native/screencapturekit/Sources/OpenScreenCaptureCore/MicrophoneDeviceSelection.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,25 @@ public func resolveMicrophoneDeviceID(
2525
let matches = devices.filter { $0.name == normalized }
2626
return matches.count == 1 ? matches[0].id : nil
2727
}
28+
29+
/// What a take that asked for the microphone records.
30+
public enum MicrophoneCapture: Equatable {
31+
/// This macOS has no ScreenCaptureKit microphone (before 15).
32+
case unsupported
33+
/// No audio input at all. ScreenCaptureKit, asked for a microphone anyway, starts that
34+
/// take and then never starts the next one in the same process (#995).
35+
case noInput
36+
/// The input to record; nil is the system default.
37+
case device(String?)
38+
}
39+
40+
public func resolveMicrophoneCapture(
41+
supported: Bool,
42+
deviceID: String?,
43+
deviceName: String?,
44+
devices: [(id: String, name: String)]
45+
) -> MicrophoneCapture {
46+
guard supported else { return .unsupported }
47+
guard !devices.isEmpty else { return .noInput }
48+
return .device(resolveMicrophoneDeviceID(deviceID: deviceID, deviceName: deviceName, devices: devices))
49+
}

‎electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,36 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
721721
configuration.excludesCurrentProcessAudio = true
722722
configuration.capturesAudio = request.audio.system.enabled && !tapsSystemAudio
723723

724-
if request.audio.microphone.enabled {
725-
guard supportsNativeMicrophoneCapture(streamConfig: configuration) else {
726-
nativeMicrophoneEnabled = false
727-
emit([
728-
"event": "warning",
729-
"code": "microphone-unavailable",
730-
"message": "Native microphone capture requires ScreenCaptureKit microphone support on this macOS version.",
731-
])
732-
return configuration
724+
nativeMicrophoneEnabled = false
725+
guard request.audio.microphone.enabled else {
726+
return configuration
727+
}
728+
let microphone = resolveMicrophoneCapture(
729+
supported: supportsNativeMicrophoneCapture(streamConfig: configuration),
730+
deviceID: request.audio.microphone.deviceId,
731+
deviceName: request.audio.microphone.deviceName,
732+
devices: AVCaptureDevice.devices(for: .audio).map {
733+
(id: $0.uniqueID, name: $0.localizedName)
733734
}
735+
)
736+
switch microphone {
737+
case .unsupported:
738+
emit([
739+
"event": "warning",
740+
"code": "microphone-unavailable",
741+
"message": "Native microphone capture requires ScreenCaptureKit microphone support on this macOS version.",
742+
])
743+
case .noInput:
744+
emit([
745+
"event": "warning",
746+
"code": "microphone-not-found",
747+
"message": "No audio input is connected; recording without the microphone.",
748+
])
749+
case .device(let deviceId):
734750
nativeMicrophoneEnabled = true
735751
configuration.capturesAudio = true
736752
configuration.setValue(true, forKey: "captureMicrophone")
737-
if let deviceId = resolveMicrophoneCaptureDeviceID() {
753+
if let deviceId {
738754
configuration.setValue(deviceId, forKey: "microphoneCaptureDeviceID")
739755
} else if requestedASpecificMicrophone {
740756
emit([
@@ -743,8 +759,6 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
743759
"message": "The requested microphone could not be resolved; capturing the default input.",
744760
])
745761
}
746-
} else {
747-
nativeMicrophoneEnabled = false
748762
}
749763

750764
return configuration
@@ -1114,7 +1128,7 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
11141128
///
11151129
/// The same test the Windows helper makes before emitting this warning
11161130
/// (`wantedAParticularMicrophone` in wgc-capture/src/wasapi_loopback_capture.cpp),
1117-
/// and it has to be made here too because `resolveMicrophoneCaptureDeviceID()`
1131+
/// and it has to be made here too because `resolveMicrophoneDeviceID()`
11181132
/// returns nil for two very different situations. One is a chosen microphone that
11191133
/// nothing here could find, which is worth saying out loud. The other is no choice
11201134
/// at all, which is the common case and has to stay silent: the HUD leaves both
@@ -1134,16 +1148,6 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
11341148
if deviceId == "default" { return false }
11351149
return !deviceId.isEmpty || !deviceName.isEmpty
11361150
}
1137-
1138-
private func resolveMicrophoneCaptureDeviceID() -> String? {
1139-
resolveMicrophoneDeviceID(
1140-
deviceID: request.audio.microphone.deviceId,
1141-
deviceName: request.audio.microphone.deviceName,
1142-
devices: AVCaptureDevice.devices(for: .audio).map {
1143-
(id: $0.uniqueID, name: $0.localizedName)
1144-
}
1145-
)
1146-
}
11471151
}
11481152

11491153
@main

‎electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/MicrophoneDeviceSelectionTests.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ final class MicrophoneDeviceSelectionTests: XCTestCase {
3030
XCTAssertEqual(resolveMicrophoneDeviceID(deviceID: "other", deviceName: "Shure MV7", devices: duplicates), "other")
3131
}
3232

33+
func testNoInputRecordsWithoutTheMicrophone() {
34+
XCTAssertEqual(resolveMicrophoneCapture(supported: true, deviceID: nil, deviceName: nil, devices: []), .noInput)
35+
XCTAssertEqual(resolveMicrophoneCapture(supported: true, deviceID: "mv7", deviceName: "Shure MV7", devices: []), .noInput)
36+
}
37+
38+
func testUnsupportedMacOSKeepsItsOwnReason() {
39+
XCTAssertEqual(resolveMicrophoneCapture(supported: false, deviceID: nil, deviceName: nil, devices: []), .unsupported)
40+
XCTAssertEqual(resolveMicrophoneCapture(supported: false, deviceID: "mv7", deviceName: nil, devices: devices), .unsupported)
41+
}
42+
43+
func testListedInputIsCaptured() {
44+
XCTAssertEqual(resolveMicrophoneCapture(supported: true, deviceID: "mv7", deviceName: nil, devices: devices), .device("mv7"))
45+
XCTAssertEqual(resolveMicrophoneCapture(supported: true, deviceID: nil, deviceName: nil, devices: devices), .device(nil))
46+
}
47+
3348
func testExactDecoratedDeviceNameIsPreserved() {
3449
let decorated = devices + [(id: "literal", name: "Shure MV7 (14ed:1012)")]
3550
XCTAssertEqual(resolveMicrophoneDeviceID(deviceID: nil, deviceName: "Shure MV7 (14ed:1012)", devices: decorated), "literal")

‎electron/recording/nativeWindowsCaptureStop.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isSalvageableFragmentedCapture,
77
NATIVE_WINDOWS_SALVAGEABLE_OUTPUT_BYTES,
88
readMicrophoneDefaulted,
9+
readMicrophoneNotFound,
910
readMicrophoneUnavailable,
1011
readSecondaryWindowsApplied,
1112
readStoppedPath,
@@ -164,6 +165,22 @@ describe("readMicrophoneUnavailable", () => {
164165
});
165166
});
166167

168+
describe("readMicrophoneNotFound", () => {
169+
// The line ScreenCaptureRecorder.swift emits when macOS lists no audio input (#995).
170+
it("sees the macOS helper record without a microphone that does not exist", () => {
171+
const output =
172+
'{"code":"microphone-not-found","event":"warning","message":"No audio input is connected; recording without the microphone."}\n';
173+
expect(readMicrophoneNotFound(output)).toBe(true);
174+
expect(readMicrophoneUnavailable(output)).toBe(false);
175+
});
176+
177+
it("is false when this macOS cannot capture the microphone", () => {
178+
const output =
179+
'{"code":"microphone-unavailable","event":"warning","message":"Native microphone capture requires ScreenCaptureKit microphone support on this macOS version."}\n';
180+
expect(readMicrophoneNotFound(output)).toBe(false);
181+
});
182+
});
183+
167184
describe("readWebcamFormat", () => {
168185
it("reads the negotiated camera format", () => {
169186
const output =

‎electron/recording/nativeWindowsCaptureStop.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ export function readMicrophoneUnavailable(output: string) {
147147
return output.includes('"code":"microphone-unavailable"');
148148
}
149149

150+
/**
151+
* Did the macOS helper record without the microphone because no audio input exists?
152+
*
153+
* Asked for one anyway, ScreenCaptureKit starts that take and never starts the next one
154+
* in the same picker session: the take after a Restart was lost (getopenscreen/openscreen#995).
155+
*/
156+
export function readMicrophoneNotFound(output: string) {
157+
return output.includes('"code":"microphone-not-found"');
158+
}
159+
150160
/**
151161
* Index of the `}` that closes the object starting at `start`, or -1.
152162
*

‎src/hooks/useScreenRecorder.nativeMacStartWarning.test.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ describe("useScreenRecorder native macOS start warnings", () => {
137137
expect(toast.error).toHaveBeenCalledWith("recording.microphoneUnavailable");
138138
});
139139

140+
// The helper's own check (#995): macOS lists no audio input, so it records without one.
141+
it("says so when the helper finds no audio input", async () => {
142+
api.startNativeMacRecording.mockResolvedValue({
143+
success: true,
144+
recordingId: 7,
145+
microphoneNotFound: true,
146+
});
147+
const view = renderHook(() => useScreenRecorder());
148+
await settle();
149+
150+
await act(async () => {
151+
view.result.current.toggleRecording();
152+
});
153+
await settle(3_500);
154+
155+
expect(view.result.current.recording).toBe(true);
156+
expect(toast.error).toHaveBeenCalledWith("rec.noMicrophoneFound");
157+
});
158+
140159
it("warns about unavailable system audio while keeping the screen recording active", async () => {
141160
stubElectronAPI(true);
142161
const view = renderHook(() => useScreenRecorder());
@@ -184,6 +203,7 @@ describe("useScreenRecorder native macOS start warnings", () => {
184203
expect(request.audio.microphone.enabled).toBe(false);
185204
}
186205
expect(api.setRecordingPrefs).toHaveBeenCalledWith({ micEnabled: false });
206+
expect(toast.error).toHaveBeenCalledWith("rec.noMicrophoneFound");
187207
expect(toast.error).not.toHaveBeenCalledWith("recording.restartFailed");
188208
} finally {
189209
Reflect.deleteProperty(navigator, "mediaDevices");

‎src/hooks/useScreenRecorder.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,9 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
14461446
if (result.microphoneUnavailable) {
14471447
toast.error(t("recording.microphoneUnavailable"));
14481448
}
1449+
if (result.microphoneNotFound) {
1450+
toast.error(t("rec.noMicrophoneFound"));
1451+
}
14491452

14501453
// The IPC call above only resolves once the helper's stdout confirms its
14511454
// screen capture has truly started (see waitForNativeMacCaptureStart in
@@ -1802,6 +1805,9 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
18021805
}
18031806
// Resolved once, for whichever path records the take (#995).
18041807
const recordMicrophone = await shouldRecordMicrophone(microphoneEnabled);
1808+
if (microphoneEnabled && !recordMicrophone) {
1809+
toast.error(t("rec.noMicrophoneFound"));
1810+
}
18051811

18061812
// BEFORE THE SOURCE GATE, on purpose. On Wayland the portal raises its
18071813
// own picker and is the only thing that can choose a source, so there

‎src/lib/nativeMacRecording.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export type NativeMacRecordingStartResult = {
100100
microphoneDefaulted?: boolean;
101101
/** The microphone was asked for, but this macOS cannot capture it: the take has no voice. */
102102
microphoneUnavailable?: boolean;
103+
/** The microphone was asked for, but macOS lists no audio input: the take has no voice. */
104+
microphoneNotFound?: boolean;
103105
error?: string;
104106
};
105107

0 commit comments

Comments
 (0)