Fix mic going silent after display or input device changes#102
Fix mic going silent after display or input device changes#102modojodo wants to merge 2 commits into
Conversation
Restart the AVCaptureSession after reconfiguration and recover the capture path when hardware routes change, including system default input updates. Co-authored-by: Cursor <cursoragent@cursor.com>
|
@karansinghgit Any way to get this merged or have a fix available for this issue. |
- Re-arm idle auto-stop after restarting a pre-warmed session on a device/route change, so the mic no longer stays hot forever while idle. - Remove the Core Audio default-input listener (and NotificationCenter observers) on deinit. Non-singleton instances (Dashboard/Transcribe views, tests) previously leaked a listener registration per instance.
|
Reviewed and pushed two fixes on top of this branch. Verdict: correct diagnosis and sound approach (restart after reconfigure, recover on route changes when the device ID is unchanged, follow the macOS default input via a Core Audio listener). Builds clean, unit tests pass. Found two real defects and fixed them. 1. Idle mic stays hot after a device change (regresses this PR's own goal). 2. Core Audio listener leaked per instance. Left alone: |
|
@modojodo can you help test this? |
Summary
Fixes a bug where the microphone stops picking up audio after switching monitors/displays, plugging/unplugging input devices, or changing the default input in macOS System Settings — even though the app still appears to be recording.
The root cause is in
AudioRecordingService: theAVCaptureSessionwas being torn down without being restarted, and hardware route changes were not triggering a session recovery when the selected device ID stayed the same.Root cause
Three separate gaps combined to produce the reported behavior:
1.
setupSession()stopped the capture session but never restarted itselectedDeviceId'sdidSetcallssetupSession(), which runscaptureSession?.stopRunning()and rebuilds inputs/outputs — but intentionally does not callstartRunning()(to avoid keeping the mic hot while idle).That is correct for idle state, but broken when a session rebuild happens during an active recording or while a pre-warmed session is running. The session is left configured but stopped, so no audio buffers arrive and the waveform flatlines.
2. Plug/unplug notifications did not recover stale sessions
handleDeviceChangeonly calledfetchAvailableDevices(). If the selected device'suniqueIDdid not change (common when reconnecting a display with HDMI/DisplayPort audio, or when macOS re-enumerates devices without changing the UID),setupSession()was never invoked and the existingAVCaptureSessioncould remain alive-but-silent.3. System Settings input changes were ignored
SpeakType persists a specific
selectedDeviceIdin UserDefaults. Changing the default input in System Settings → Sound → Input does not fireAVCaptureDeviceconnect/disconnect notifications, so the app kept capturing from the old pinned device. Users reasonably expect System Settings changes to take effect.Fix
All changes are in
speaktype/Services/AudioRecordingService.swift:Restart after reconfiguration —
setupSession()now records whether the session was running or a recording was active before teardown, and callsrestartCaptureSession()onaudioQueuewhen either is true.Recover on hardware route changes —
handleDeviceChangenow callsrecoverCaptureSessionIfNeeded(forceReconfigure: true)after refreshing the device list, forcing a rebuild even whenselectedDeviceIdis unchanged.Follow macOS default input changes — Added a Core Audio property listener on
kAudioHardwarePropertyDefaultInputDevice. When the system default changes:Test plan