Skip to content

Fix AirPods audio when macOS switches A2DP to HFP - #423

Merged
devopvoid merged 3 commits into
devopvoid:mainfrom
Stasyanych:fixes/Fix_AirPods_switches_A2DP_to_HFP
Nov 14, 2026
Merged

devopvoid merged 3 commits into
devopvoid:mainfrom
Stasyanych:fixes/Fix_AirPods_switches_A2DP_to_HFP

Conversation

@Stasyanych

Copy link
Copy Markdown
Contributor

On macOS, activating the microphone of a Bluetooth headset (AirPods) switches the device from A2DP (48 kHz) to HFP (24 kHz in the log). The WebRTC macOS ADM rejected the format change in HandleStreamFormatChange when streamFormat.mChannelsPerFrame != _recChannels. That check is wrong for playout: _recChannels is the capture channel count (1), while the output has 2 channels. As a result, _outStreamFormat and the render converter stayed at 48 kHz while the device was already running at 24 kHz. Audio played back slowed down; reopening the sound settings or reinitializing playout re-read the actual device format and fixed it only temporarily.

On the capture side, _captureConverter was recreated directly in the CoreAudio listener thread while the capture worker was using it. That is a race: capture could break or be corrupted on a format change, and the sample count could be computed incorrectly if _inStreamFormat had already been updated but the data arrived in the new format.

What the patch does:

  • playout: removed the incorrect channel check based on _recChannels; the new output format is accepted, _outStreamFormat is updated, mono fallback is applied when needed, and the render converter and device buffer parameters are rebuilt via SetDesiredPlayoutFormat();
  • capture: converter recreation is deferred to the capture worker. The listener only stores the pending format and sets a flag; the worker applies it, drops frames during the transition, and disposes the old converter after creating the new one. This removes the listener/worker race;
  • fixed the sample count calculation in the input IOProc: use the actual buffer size (interleaved Float32) instead of potentially stale _inStreamFormat;
  • added logging for applied formats and capture/playout changes.

As a result, the application-level workaround that reinitialized audio devices after connection is no longer needed.

Tested on macOS:

  1. AirPods selected as the playback device before the call starts.
  2. AirPods connected/selected during a call, with audio initially going through the built-in speakers. In both cases audio plays correctly after the HFP switch, with no slowdown and no silence.

Affected files:

  • modules/audio_device/mac/audio_device_mac.cc
  • modules/audio_device/mac/audio_device_mac.h

On macOS, activating the microphone of a Bluetooth headset (AirPods)
switches the device from A2DP (48 kHz) to HFP (24 kHz in the log). The
WebRTC macOS ADM rejected the format change in HandleStreamFormatChange
when streamFormat.mChannelsPerFrame != _recChannels. That check is wrong
for playout: _recChannels is the capture channel count (1), while the
output has 2 channels. As a result, _outStreamFormat and the render
converter stayed at 48 kHz while the device was already running at
24 kHz. Audio played back slowed down; reopening the sound settings or
reinitializing playout re-read the actual device format and fixed it
only temporarily.

On the capture side, _captureConverter was recreated directly in the
CoreAudio listener thread while the capture worker was using it. That
is a race: capture could break or be corrupted on a format change, and
the sample count could be computed incorrectly if _inStreamFormat had
already been updated but the data arrived in the new format.

What the patch does:
- playout: removed the incorrect channel check based on _recChannels;
  the new output format is accepted, _outStreamFormat is updated, mono
  fallback is applied when needed, and the render converter and device
  buffer parameters are rebuilt via SetDesiredPlayoutFormat();
- capture: converter recreation is deferred to the capture worker. The
  listener only stores the pending format and sets a flag; the worker
  applies it, drops frames during the transition, and disposes the old
  converter after creating the new one. This removes the listener/worker
  race;
- fixed the sample count calculation in the input IOProc: use the actual
  buffer size (interleaved Float32) instead of potentially stale
  _inStreamFormat;
- added logging for applied formats and capture/playout changes.

As a result, the application-level workaround that reinitialized audio
devices after connection is no longer needed.

Tested on macOS:
1. AirPods selected as the playback device before the call starts.
2. AirPods connected/selected during a call, with audio initially going
   through the built-in speakers.
In both cases audio plays correctly after the HFP switch, with no
slowdown and no silence.

Affected files:
- modules/audio_device/mac/audio_device_mac.cc
- modules/audio_device/mac/audio_device_mac.h

@devopvoid devopvoid left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down. The diagnosis is right: the old streamFormat.mChannelsPerFrame != _recChannels check compared the output format against the capture channel count, so every playout format change on a stereo-out/mono-in device was rejected. Deferring the capture converter rebuild to the capture worker is a clean fix for the listener/worker race.

I checked that the patch applies on top of patch_for_mac_deviceId.patch against branch-heads/8059, and the macOS CI logs confirm it was applied and compiled (no cache hit). A few things I'd like to see addressed before merging:

1. Playout: render converter leak and the same race the PR fixes for capture

SetDesiredPlayoutFormat() calls AudioConverterNew(&_outDesiredFormat, &_outStreamFormat, &_renderConverter) without disposing the previous converter, so every playout format change now leaks one AudioConverter. More importantly, it swaps _renderConverter on the CoreAudio listener thread while implDeviceIOProc may be inside AudioConverterFillComplexBuffer(_renderConverter, ...) on the IO thread. Before this PR the path was effectively dead because of the bogus channel check; now it runs on every A2DP -> HFP switch. It only avoids a crash today because the old converter is never freed. Adding the "obvious" dispose later would turn it into a use-after-free.

Options: keep the replaced converter and dispose it in StopPlayout() once the IOProc has stopped, or mirror the capture approach (pending flag, rebuild the converter on the IO thread before the next AudioConverterFillComplexBuffer).

2. ConfigureCaptureConverter() mutates state before the converter is created

_inStreamFormat, _inDesiredFormat and _recChannels are overwritten before AudioConverterNew. If that call fails, the old converter keeps running with the new _inStreamFormat channel count in implInConverterProc, which contradicts the "keep capturing with the previous converter" comment. Since _inStreamFormat already equals the new format, the equality check in ApplyPendingCaptureFormat() also prevents any retry. Building into locals and committing only on success would fix it.

3. Smaller points

  • _captureBuffer isn't flushed when the new capture format is applied, so leftover samples in the old format are fed to the new converter. Usually that's a short glitch, but on a 1 -> 2 channel change an odd leftover count can leave L/R interleaving permanently shifted. Draining the buffer in ApplyPendingCaptureFormat() (the worker is the consumer) avoids that.
  • _captureLatencyUs is derived from _inStreamFormat.mSampleRate in InitRecording() and isn't recomputed after a sample-rate change.
  • The patch isn't in the required list in dependencies/webrtc/CMakeLists.txt, so after a future WebRTC branch bump it could silently stop applying (only a warning) and the bug would come back unnoticed. Please consider making it required like pulseaudio_reinitialization.patch.
  • Nit: other patches use snake_case names (e.g. wasapi_mix_format.patch), so something like mac_stream_format_change.patch would fit better.
  • Nit: the commit headline is just "Problem:"; we'll use the PR title when squashing.

Follow-up to the stream format change patch, addressing review feedback:

- Playout: the render converter was replaced on the CoreAudio listener
  thread while the IO proc may still use it, and the old one was never
  disposed. Publish it through an atomic, keep replaced converters until
  StopPlayout() has stopped the IO proc and removed the listener, then
  dispose them all.
- Create the capture and render converters before touching any state, so
  a failed AudioConverterNew() leaves formats and converter consistent.
  On a failed playout change the previous format is restored.
- Drain the capture ring buffer before applying a new capture format, so
  the new converter never reads samples in the old format, and reset the
  converter when the format turns out to be unchanged.
- Recompute the capture latency after a capture format change.
- Rename the patch to stream_format_change.patch, in line with the other
  patch names.
@devopvoid

Copy link
Copy Markdown
Owner

Could you please test the improved patch?

@Stasyanych

Copy link
Copy Markdown
Contributor Author

Could you please test the improved patch?

Sure. I will try to do it soon.

@devopvoid
devopvoid merged commit 50440d2 into devopvoid:main Nov 14, 2026
18 checks passed
@Stasyanych

Copy link
Copy Markdown
Contributor Author

I checked, everything is working fine.

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.

2 participants