fix(ios): fix stereo mic + A2DP output silence

Three coordinated fixes for the bug where enabling stereo mic capture
caused all audio output (A2DP, speaker, wired) to go silent:

1. audio_engine.cpp — open playback before capture
   On iOS, starting the stereo capture AudioUnit can trigger an audio
   route reconfiguration that drops A2DP before the playback device has
   a chance to claim the route. Opening and starting the playback device
   first commits the output route (A2DP), so iOS is less likely to drop
   it when stereo capture activates afterward.

2. client.cpp — decouple set_capture_channels from engine restart
   Previously vc_set_capture_channels() stopped and restarted the engine
   immediately, which opened capture first (old ordering) and raced
   against the settling AVAudioSession route. Now it only stores the
   channel count; the caller (Swift via vc_audio_restart) controls when
   the engine restarts, after the route has settled.

3. IOSAudioRouter.swift — call audioRestart() after channel config
   selectCaptureChannels() and applyPreset() now call audioRestart()
   after applyConfiguration() + setCaptureChannels(). This is the
   vc_audio_restart() path that was added to the ABI in fdcc84f but
   never wired up in the Swift layer. The restart sees the stored
   channel count and reopens devices in the correct order (playback
   first, capture second).

The doStartMicStream path is unaffected: setCaptureChannels is called
before the server acknowledges the stream (engine not yet running), so
ensure_audio_running() picks up capture_channels=2 directly when the
stream is confirmed and opens with the right count from the start.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 17:39:23 +02:00
parent fdcc84fb42
commit dcb7e6eeca
5 changed files with 62 additions and 60 deletions

View File

@@ -397,14 +397,15 @@ VC_API vc_result vc_test_inject_capture(vc_client* c, uint32_t stream_id, const
size_t samples);
/* Set the capture channel count for a local MIC stream (1 = mono, 2 = stereo interleaved).
* Must be called after vc_stream_start; takes effect on the next AudioEngine restart (e.g. when
* joining voice, or immediately if the engine is already running — it stops and restarts the
* capture device with the new channel count). Defaults to 1 (mono). On iOS the Swift
* AVAudioSession routing layer enables stereo built-in mic capture by switching the built-in
* mic's data source to the .stereo polar pattern (setPreferredDataSource +
* setPreferredPolarPattern(.stereo) + setPreferredInput + setInputDataSource) and then calls
* this to tell the core to open the capture device in stereo. VC_ERR_INVALID_ARG if stream_id
* is unknown, the stream is not a MIC stream, or channels is not 1 or 2. */
* Must be called after vc_stream_start. Stores the value; it takes effect on the next engine
* (re)start. Does NOT restart the engine itself — the caller must follow up with
* vc_audio_restart() after AVAudioSession routing has settled (iOS) or after any platform
* audio-session reconfiguration. On iOS the Swift AVAudioSession routing layer enables stereo
* built-in mic capture by switching the built-in mic's data source to the .stereo polar
* pattern (setPreferredDataSource + setPreferredPolarPattern(.stereo) + setPreferredInput +
* setInputDataSource), calls this to record the desired channel count, and then calls
* vc_audio_restart() so the core reopens capture and playback against the new route.
* VC_ERR_INVALID_ARG if stream_id is unknown or channels is not 1 or 2. */
VC_API vc_result vc_set_capture_channels(vc_client* c, uint32_t stream_id, uint32_t channels);
/* ── Text ─────────────────────────────────────────────────────────────────── */