Skip to content

Recover microphone capture when the device disappears mid-call#309

Open
MaxHeimbrock wants to merge 1 commit into
max/mic-fragment-aware-capturefrom
max/mic-switch-device
Open

Recover microphone capture when the device disappears mid-call#309
MaxHeimbrock wants to merge 1 commit into
max/mic-fragment-aware-capturefrom
max/mic-switch-device

Conversation

@MaxHeimbrock

@MaxHeimbrock MaxHeimbrock commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Stacked on #308. Makes the SDK survive the active microphone disappearing mid-call — e.g. a Bluetooth headset disconnecting — instead of going permanently silent.

Behavior

  • Detection (in CaptureLoop): the mic clip's position counter advances continuously while a device is alive — even in silence — so a counter that hasn't moved for 1 s, or Microphone.IsRecording going false, means the device is gone. No false positives from a quiet room.
  • Recovery: end the dead device, then retry once per second until a device is available — preferring the original device if it reappears, otherwise falling back to the system default microphone. The normal start path re-runs the pre-roll, so the new device's rate and fragmentation are re-measured (recovering onto or off the macOS Bluetooth-HFP case works transparently).
  • The published track is unaffected throughout: the native source's format is fixed (48 kHz mono, Fix microphone capture: device-true source format + fragment-aware clip reading #308) and captured audio is resampled to it — no source recreation, no republish, no renegotiation. Subscribers just hear a gap until a device is acquired.

Also included, since it is the same mechanism app-initiated: an internal MicrophoneSource.SwitchDevice(deviceName) (the manual primitive, kept non-public until a device-picker API is designed) and a public DeviceName getter.

Robustness

  • Each CaptureLoop carries a generation token and retires itself when a newer capture (restart, switch, or recovery) supersedes it — rapid transitions can't leave two loops reading different clips.
  • The start sequence pins its device name locally, so a concurrent change can't mix devices mid-start.
  • Recovery suspends while the app is backgrounded, so it can't fight the iOS pause/resume handling; the resume path takes over.

Verification

  • Runtime + Meet sample compile clean.
  • To validate on hardware: publish from the Meet sample with the Bluetooth headset mic, then turn the headset off mid-call. Expect within ~1–2 s:
    MicrophoneSource: device 'MDR-1000X' stopped delivering audio; attempting recovery
    MicrophoneSource device='(default)' clip=...
    MicrophoneSource: recovered capture on device '(default)'
    
    and the receiver hears the built-in mic after a short gap. Turning the headset back on and off again should keep working.

🤖 Generated with Claude Code

@MaxHeimbrock MaxHeimbrock force-pushed the max/mic-switch-device branch from 0283b9c to 77cb74e Compare June 12, 2026 15:02
@MaxHeimbrock MaxHeimbrock changed the title Support switching microphone devices mid-call Recover microphone capture when the device disappears mid-call Jun 12, 2026
@MaxHeimbrock MaxHeimbrock force-pushed the max/mic-switch-device branch from 77cb74e to d7c35bd Compare June 12, 2026 15:08
When the active capture device vanishes (e.g. a Bluetooth headset
disconnects), Unity's Microphone clip silently stops filling and capture
went permanently dead. Detect and recover automatically:

- Detection: the clip's position counter advances continuously while a
  device is alive (even in silence), so CaptureLoop treats a counter that
  hasn't moved for 1s - or IsRecording dropping to false - as device loss.
- Recovery: end the dead device and retry until a device is available,
  preferring the original device if it reappears and falling back to the
  system default microphone otherwise. The normal start path re-measures
  the new device's rate and fragmentation, so recovering onto or off a
  misbehaving device (macOS Bluetooth HFP) works transparently.

The published track is unaffected throughout: the native source's format is
fixed (48kHz mono) and captured audio is resampled to it, so there is no
republish or renegotiation - only a capture gap until a device is acquired.

Also adds MicrophoneSource.SwitchDevice(deviceName) as the manual
counterpart (same mechanism, app-initiated) and a DeviceName getter. Each
CaptureLoop carries a generation token and is retired when a newer capture
(restart, switch, or recovery) supersedes it, so rapid transitions cannot
leave two loops reading different clips; recovery pauses while the app is
backgrounded so it cannot fight the iOS pause/resume handling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@MaxHeimbrock MaxHeimbrock force-pushed the max/mic-switch-device branch from d7c35bd to d8d5383 Compare June 12, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant