From a4f104de357d6de7bb778dc1a98d621cb7a1b097 Mon Sep 17 00:00:00 2001 From: Stasonych Date: Fri, 23 Oct 2026 15:22:13 +0500 Subject: [PATCH 1/2] Problem: 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 --- .../webrtc-mac-stream-format-change.patch | 386 ++++++++++++++++++ 1 file changed, 386 insertions(+) create mode 100644 webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch diff --git a/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch b/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch new file mode 100644 index 000000000..daa24ae9f --- /dev/null +++ b/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch @@ -0,0 +1,386 @@ +diff --git a/modules/audio_device/mac/audio_device_mac.cc b/modules/audio_device/mac/audio_device_mac.cc +index 3fdb71b..e4b26f5 100644 +--- a/modules/audio_device/mac/audio_device_mac.cc ++++ b/modules/audio_device/mac/audio_device_mac.cc +@@ -119,6 +119,9 @@ AudioDeviceMac::AudioDeviceMac() + _doStopRec(false), + _macBookPro(false), + _macBookProPanRight(false), ++ _captureConverter(NULL), ++ _renderConverter(NULL), ++ _captureFormatChangePending(false), + _captureLatencyUs(0), + _renderLatencyUs(0), + _captureDelayUs(0), +@@ -135,6 +138,7 @@ AudioDeviceMac::AudioDeviceMac() + memset(&_outDesiredFormat, 0, sizeof(AudioStreamBasicDescription)); + memset(&_inStreamFormat, 0, sizeof(AudioStreamBasicDescription)); + memset(&_inDesiredFormat, 0, sizeof(AudioStreamBasicDescription)); ++ memset(&_pendingInStreamFormat, 0, sizeof(_pendingInStreamFormat)); + } + + AudioDeviceMac::~AudioDeviceMac() { +@@ -1029,6 +1033,11 @@ int32_t AudioDeviceMac::InitRecording() { + return 0; + } + ++ { ++ MutexLock formatLock(&_captureFormatMutex); ++ _captureFormatChangePending.store(false, std::memory_order_release); ++ } ++ + // Initialize the microphone (devices might have been added or removed) + if (InitMicrophoneLocked() == -1) { + RTC_LOG(LS_WARNING) << "InitMicrophone() failed"; +@@ -1099,40 +1108,10 @@ int32_t AudioDeviceMac::InitRecording() { + RTC_LOG(LS_VERBOSE) << "mFormatFlags = " << _inStreamFormat.mFormatFlags; + logCAMsg(LS_VERBOSE, "mFormatID", (const char*)&_inStreamFormat.mFormatID); + +- // Our preferred format to work with +- if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { +- _inDesiredFormat.mChannelsPerFrame = 2; +- } else { +- // Disable stereo recording when we only have one channel on the device. +- _inDesiredFormat.mChannelsPerFrame = 1; +- _recChannels = 1; +- RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; +- } +- +- if (_ptrAudioBuffer) { +- // Update audio buffer with the selected parameters +- _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); +- _ptrAudioBuffer->SetRecordingChannels((uint8_t)_recChannels); ++ if (!ConfigureCaptureConverter(_inStreamFormat)) { ++ return -1; + } + +- _inDesiredFormat.mSampleRate = N_REC_SAMPLES_PER_SEC; +- _inDesiredFormat.mBytesPerPacket = +- _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); +- _inDesiredFormat.mFramesPerPacket = 1; +- _inDesiredFormat.mBytesPerFrame = +- _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); +- _inDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; +- +- _inDesiredFormat.mFormatFlags = +- kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; +-#ifdef WEBRTC_ARCH_BIG_ENDIAN +- _inDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; +-#endif +- _inDesiredFormat.mFormatID = kAudioFormatLinearPCM; +- +- WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew(&_inStreamFormat, &_inDesiredFormat, +- &_captureConverter)); +- + // First try to set buffer size to desired value (10 ms * N_BLOCKS_IO) + // TODO(xians): investigate this block. + UInt32 bufByteCount = +@@ -1211,6 +1190,97 @@ int32_t AudioDeviceMac::InitRecording() { + return 0; + } + ++bool AudioDeviceMac::ConfigureCaptureConverter( ++ const AudioStreamBasicDescription& input_format) { ++ if (input_format.mFormatID != kAudioFormatLinearPCM || ++ input_format.mSampleRate == 0 || ++ input_format.mChannelsPerFrame == 0 || ++ input_format.mChannelsPerFrame > N_DEVICE_CHANNELS) { ++ RTC_LOG(LS_ERROR) << "Unsupported input stream format"; ++ return false; ++ } ++ ++ _inStreamFormat = input_format; ++ ++ // Our preferred format to work with. ++ if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { ++ _inDesiredFormat.mChannelsPerFrame = 2; ++ } else { ++ // Disable stereo recording when we only have one channel on the device. ++ _inDesiredFormat.mChannelsPerFrame = 1; ++ _recChannels = 1; ++ RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; ++ } ++ ++ if (_ptrAudioBuffer) { ++ // Update audio buffer with the selected parameters. ++ _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); ++ _ptrAudioBuffer->SetRecordingChannels((uint8_t)_recChannels); ++ } ++ ++ _inDesiredFormat.mSampleRate = N_REC_SAMPLES_PER_SEC; ++ _inDesiredFormat.mBytesPerPacket = ++ _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); ++ _inDesiredFormat.mFramesPerPacket = 1; ++ _inDesiredFormat.mBytesPerFrame = ++ _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); ++ _inDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; ++ ++ _inDesiredFormat.mFormatFlags = ++ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; ++#ifdef WEBRTC_ARCH_BIG_ENDIAN ++ _inDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; ++#endif ++ _inDesiredFormat.mFormatID = kAudioFormatLinearPCM; ++ ++ AudioConverterRef capture_converter = NULL; ++ OSStatus err = AudioConverterNew(&_inStreamFormat, &_inDesiredFormat, ++ &capture_converter); ++ if (err != noErr) { ++ logCAMsg(LS_ERROR, "AudioConverterNew() failed", (const char*)&err); ++ return false; ++ } ++ ++ if (_captureConverter) { ++ WEBRTC_CA_LOG_WARN(AudioConverterDispose(_captureConverter)); ++ } ++ _captureConverter = capture_converter; ++ ++ return true; ++} ++ ++void AudioDeviceMac::ApplyPendingCaptureFormat() { ++ AudioStreamBasicDescription format; ++ { ++ MutexLock lock(&_captureFormatMutex); ++ if (!_captureFormatChangePending.load(std::memory_order_acquire)) { ++ return; ++ } ++ format = _pendingInStreamFormat; ++ _captureFormatChangePending.store(false, std::memory_order_release); ++ } ++ ++ if (format.mSampleRate == _inStreamFormat.mSampleRate && ++ format.mChannelsPerFrame == _inStreamFormat.mChannelsPerFrame && ++ format.mBytesPerPacket == _inStreamFormat.mBytesPerPacket && ++ format.mBytesPerFrame == _inStreamFormat.mBytesPerFrame && ++ format.mBitsPerChannel == _inStreamFormat.mBitsPerChannel && ++ format.mFormatFlags == _inStreamFormat.mFormatFlags && ++ format.mFormatID == _inStreamFormat.mFormatID) { ++ return; ++ } ++ ++ RTC_LOG(LS_INFO) << "Applying capture stream format: sample_rate=" ++ << format.mSampleRate ++ << ", channels=" << format.mChannelsPerFrame; ++ ++ if (!ConfigureCaptureConverter(format)) { ++ // Keep capturing with the previous converter instead of stopping the ++ // worker. The stream is still better than no audio at all. ++ RTC_LOG(LS_ERROR) << "Failed to apply the new capture stream format"; ++ } ++} ++ + int32_t AudioDeviceMac::StartRecording() { + RTC_LOG(LS_INFO) << "StartRecording"; + MutexLock lock(&mutex_); +@@ -1323,7 +1393,10 @@ int32_t AudioDeviceMac::StopRecording() { + mutex_.Lock(); + } + +- WEBRTC_CA_LOG_WARN(AudioConverterDispose(_captureConverter)); ++ if (_captureConverter) { ++ WEBRTC_CA_LOG_WARN(AudioConverterDispose(_captureConverter)); ++ _captureConverter = NULL; ++ } + + // Remove listeners. + AudioObjectPropertyAddress propertyAddress = { +@@ -1335,6 +1408,11 @@ int32_t AudioDeviceMac::StopRecording() { + WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( + _inputDeviceID, &propertyAddress, &objectListenerProc, this)); + ++ { ++ MutexLock formatLock(&_captureFormatMutex); ++ _captureFormatChangePending.store(false, std::memory_order_release); ++ } ++ + _recIsInitialized = false; + _recording = false; + +@@ -1942,12 +2020,6 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( + return -1; + } + +- if (_ptrAudioBuffer && streamFormat.mChannelsPerFrame != _recChannels) { +- RTC_LOG(LS_ERROR) << "Changing channels not supported (mChannelsPerFrame = " +- << streamFormat.mChannelsPerFrame << ")"; +- return -1; +- } +- + RTC_LOG(LS_VERBOSE) << "Stream format:"; + RTC_LOG(LS_VERBOSE) << "mSampleRate = " << streamFormat.mSampleRate + << ", mChannelsPerFrame = " +@@ -1973,33 +2045,40 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( + return -1; + } + +- memcpy(&_inStreamFormat, &streamFormat, sizeof(streamFormat)); +- +- if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { +- _inDesiredFormat.mChannelsPerFrame = 2; +- } else { +- // Disable stereo recording when we only have one channel on the device. +- _inDesiredFormat.mChannelsPerFrame = 1; +- _recChannels = 1; +- RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; ++ // The capture worker owns `_captureConverter`. Recreating it here, on the ++ // CoreAudio listener thread, races with the worker using the converter. ++ // Remember the new format and let the worker apply it. ++ { ++ MutexLock lock(&_captureFormatMutex); ++ _pendingInStreamFormat = streamFormat; ++ _captureFormatChangePending.store(true, std::memory_order_release); + } ++ semaphore_signal_all(_captureSemaphore); + +- // Recreate the converter with the new format +- // TODO(xians): make this thread safe +- WEBRTC_CA_RETURN_ON_ERR(AudioConverterDispose(_captureConverter)); ++ RTC_LOG(LS_INFO) << "Capture stream format changed: sample_rate=" ++ << streamFormat.mSampleRate ++ << ", channels=" << streamFormat.mChannelsPerFrame; ++ return 0; ++ } + +- WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew(&streamFormat, &_inDesiredFormat, +- &_captureConverter)); +- } else { +- memcpy(&_outStreamFormat, &streamFormat, sizeof(streamFormat)); ++ // A Bluetooth headset can change the playout stream format at runtime, ++ // e.g. A2DP (48 kHz) -> HFP after the microphone is enabled. Apply the ++ // new format instead of rejecting it: SetDesiredPlayoutFormat() rebuilds ++ // the render converter and the device buffer for the new sample rate. A ++ // channel-count downgrade is handled below. ++ RTC_LOG(LS_INFO) << "Applying playout stream format change: sample_rate=" ++ << streamFormat.mSampleRate ++ << ", channels=" << streamFormat.mChannelsPerFrame; + +- // Our preferred format to work with +- if (_outStreamFormat.mChannelsPerFrame < 2) { +- _playChannels = 1; +- RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; +- } +- WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); ++ memcpy(&_outStreamFormat, &streamFormat, sizeof(streamFormat)); ++ ++ // Our preferred format to work with ++ if (_outStreamFormat.mChannelsPerFrame < 2) { ++ _playChannels = 1; ++ RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; + } ++ WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); ++ + return 0; + } + +@@ -2235,6 +2314,12 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, + return 0; + } + ++ if (_captureFormatChangePending.load(std::memory_order_acquire)) { ++ // Drop frames until the capture worker has applied the new format. The ++ // old converter must not consume samples captured with the new format. ++ return 0; ++ } ++ + size_t bufSizeSamples = _captureBuffer.AvailableToRead(); + + int32_t captureDelayUs = +@@ -2247,9 +2332,10 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, + _captureDelayUs = captureDelayUs; + + RTC_DCHECK(inputData->mNumberBuffers == 1); +- size_t numSamples = inputData->mBuffers->mDataByteSize * +- _inStreamFormat.mChannelsPerFrame / +- _inStreamFormat.mBytesPerPacket; ++ // The input data is interleaved 32-bit float. Using the actual buffer size ++ // keeps the sample count correct even if the device format has just changed ++ // and `_inStreamFormat` has not been updated by the capture worker yet. ++ size_t numSamples = inputData->mBuffers->mDataByteSize / sizeof(Float32); + _captureBuffer.Write(std::span( + static_cast(inputData->mBuffers->mData), numSamples)); + +@@ -2264,9 +2350,20 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, + OSStatus AudioDeviceMac::implInConverterProc(UInt32* numberDataPackets, + AudioBufferList* data) { + RTC_DCHECK(data->mNumberBuffers == 1); ++ ++ if (_captureFormatChangePending.load(std::memory_order_acquire)) { ++ *numberDataPackets = 0; ++ return 1; ++ } ++ + size_t numSamples = *numberDataPackets * _inStreamFormat.mChannelsPerFrame; + + while (_captureBuffer.AvailableToRead() < numSamples) { ++ if (_captureFormatChangePending.load(std::memory_order_acquire)) { ++ *numberDataPackets = 0; ++ return 1; ++ } ++ + mach_timespec_t timeout; + timeout.tv_sec = 0; + timeout.tv_nsec = TIMER_PERIOD_MS; +@@ -2361,6 +2458,10 @@ bool AudioDeviceMac::RenderWorkerThread() { + } + + bool AudioDeviceMac::CaptureWorkerThread() { ++ if (_captureFormatChangePending.load(std::memory_order_acquire)) { ++ ApplyPendingCaptureFormat(); ++ } ++ + OSStatus err = noErr; + UInt32 noRecSamples = + ENGINE_REC_BUF_SIZE_IN_SAMPLES * _inDesiredFormat.mChannelsPerFrame; +@@ -2378,8 +2479,10 @@ bool AudioDeviceMac::CaptureWorkerThread() { + this, &size, &engineBuffer, NULL); + if (err != noErr) { + if (err == 1) { +- // This is our own error. +- return false; ++ // Our own error: either the device is gone or a format update was ++ // requested while the converter was running. Keep the worker alive so ++ // it can apply the new format. ++ return _captureFormatChangePending.load(std::memory_order_acquire); + } else { + logCAMsg(LS_ERROR, "Error in AudioConverterFillComplexBuffer()", + (const char*)&err); +diff --git a/modules/audio_device/mac/audio_device_mac.h b/modules/audio_device/mac/audio_device_mac.h +index 07a93e2..83e7625 100644 +--- a/modules/audio_device/mac/audio_device_mac.h ++++ b/modules/audio_device/mac/audio_device_mac.h +@@ -259,12 +259,21 @@ class AudioDeviceMac : public AudioDeviceGeneric { + bool CaptureWorkerThread(); + bool RenderWorkerThread(); + ++ void ApplyPendingCaptureFormat(); ++ bool ConfigureCaptureConverter( ++ const AudioStreamBasicDescription& input_format); ++ + bool KeyPressed(); + + AudioDeviceBuffer* _ptrAudioBuffer; + + Mutex mutex_; + ++ // Protects `_pendingInStreamFormat`. Kept separate from `mutex_` so the ++ // CoreAudio stream-format listener never blocks on the device lock while the ++ // device is being stopped and property listeners are removed. ++ Mutex _captureFormatMutex; ++ + Event _stopEventRec; + Event _stopEvent; + +@@ -311,6 +320,12 @@ class AudioDeviceMac : public AudioDeviceGeneric { + AudioConverterRef _captureConverter; + AudioConverterRef _renderConverter; + ++ // Set by the CoreAudio stream-format listener and applied by the capture ++ // worker, which owns `_captureConverter`. Recreating the converter from the ++ // listener would race with the capture worker using it. ++ std::atomic _captureFormatChangePending; ++ AudioStreamBasicDescription _pendingInStreamFormat; ++ + AudioStreamBasicDescription _outStreamFormat; + AudioStreamBasicDescription _outDesiredFormat; + AudioStreamBasicDescription _inStreamFormat; From 7d26a3160f0df38003cc33174d820ee2e66dc2d0 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Wed, 11 Nov 2026 23:54:37 +0100 Subject: [PATCH 2/2] fix: make macOS stream format changes safe for playout 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. --- ...hange.patch => stream_format_change.patch} | 381 +++++++++++++++--- 1 file changed, 325 insertions(+), 56 deletions(-) rename webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/{webrtc-mac-stream-format-change.patch => stream_format_change.patch} (50%) diff --git a/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch b/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/stream_format_change.patch similarity index 50% rename from webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch rename to webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/stream_format_change.patch index daa24ae9f..4c63574b7 100644 --- a/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/webrtc-mac-stream-format-change.patch +++ b/webrtc-jni/src/main/cpp/dependencies/webrtc/patches/macos/stream_format_change.patch @@ -1,5 +1,5 @@ diff --git a/modules/audio_device/mac/audio_device_mac.cc b/modules/audio_device/mac/audio_device_mac.cc -index 3fdb71b..e4b26f5 100644 +index 641a7fb..1dcf6f8 100644 --- a/modules/audio_device/mac/audio_device_mac.cc +++ b/modules/audio_device/mac/audio_device_mac.cc @@ -119,6 +119,9 @@ AudioDeviceMac::AudioDeviceMac() @@ -20,7 +20,7 @@ index 3fdb71b..e4b26f5 100644 } AudioDeviceMac::~AudioDeviceMac() { -@@ -1029,6 +1033,11 @@ int32_t AudioDeviceMac::InitRecording() { +@@ -1037,6 +1041,11 @@ int32_t AudioDeviceMac::InitRecording() { return 0; } @@ -32,7 +32,7 @@ index 3fdb71b..e4b26f5 100644 // Initialize the microphone (devices might have been added or removed) if (InitMicrophoneLocked() == -1) { RTC_LOG(LS_WARNING) << "InitMicrophone() failed"; -@@ -1099,40 +1108,10 @@ int32_t AudioDeviceMac::InitRecording() { +@@ -1107,40 +1116,10 @@ int32_t AudioDeviceMac::InitRecording() { RTC_LOG(LS_VERBOSE) << "mFormatFlags = " << _inStreamFormat.mFormatFlags; logCAMsg(LS_VERBOSE, "mFormatID", (const char*)&_inStreamFormat.mFormatID); @@ -75,7 +75,38 @@ index 3fdb71b..e4b26f5 100644 // First try to set buffer size to desired value (10 ms * N_BLOCKS_IO) // TODO(xians): investigate this block. UInt32 bufByteCount = -@@ -1211,6 +1190,97 @@ int32_t AudioDeviceMac::InitRecording() { +@@ -1172,27 +1151,9 @@ int32_t AudioDeviceMac::InitRecording() { + WEBRTC_CA_RETURN_ON_ERR(AudioObjectSetPropertyData( + _inputDeviceID, &propertyAddress, 0, NULL, size, &bufByteCount)); + +- // Get capture device latency +- propertyAddress.mSelector = kAudioDevicePropertyLatency; +- UInt32 latency = 0; +- size = sizeof(UInt32); +- WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( +- _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); +- _captureLatencyUs = (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); +- +- // Get capture stream latency +- propertyAddress.mSelector = kAudioDevicePropertyStreams; +- AudioStreamID stream = 0; +- size = sizeof(AudioStreamID); +- WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( +- _inputDeviceID, &propertyAddress, 0, NULL, &size, &stream)); +- propertyAddress.mSelector = kAudioStreamPropertyLatency; +- size = sizeof(UInt32); +- latency = 0; +- WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( +- _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); +- _captureLatencyUs += +- (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); ++ if (UpdateCaptureLatency() == -1) { ++ return -1; ++ } + + // Listen for format changes + // TODO(xians): should we be using kAudioDevicePropertyDeviceHasChanged? +@@ -1219,6 +1180,168 @@ int32_t AudioDeviceMac::InitRecording() { return 0; } @@ -89,47 +120,53 @@ index 3fdb71b..e4b26f5 100644 + return false; + } + -+ _inStreamFormat = input_format; -+ -+ // Our preferred format to work with. -+ if (_inStreamFormat.mChannelsPerFrame >= 2 && (_recChannels == 2)) { -+ _inDesiredFormat.mChannelsPerFrame = 2; -+ } else { -+ // Disable stereo recording when we only have one channel on the device. -+ _inDesiredFormat.mChannelsPerFrame = 1; -+ _recChannels = 1; -+ RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; -+ } -+ -+ if (_ptrAudioBuffer) { -+ // Update audio buffer with the selected parameters. -+ _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); -+ _ptrAudioBuffer->SetRecordingChannels((uint8_t)_recChannels); -+ } ++ // Our preferred format to work with. Stereo recording needs both a stereo ++ // request and at least two channels on the device. ++ const bool stereo = input_format.mChannelsPerFrame >= 2 && _recChannels == 2; + -+ _inDesiredFormat.mSampleRate = N_REC_SAMPLES_PER_SEC; -+ _inDesiredFormat.mBytesPerPacket = -+ _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); -+ _inDesiredFormat.mFramesPerPacket = 1; -+ _inDesiredFormat.mBytesPerFrame = -+ _inDesiredFormat.mChannelsPerFrame * sizeof(SInt16); -+ _inDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; ++ AudioStreamBasicDescription desired_format; ++ memset(&desired_format, 0, sizeof(desired_format)); ++ desired_format.mSampleRate = N_REC_SAMPLES_PER_SEC; ++ desired_format.mChannelsPerFrame = stereo ? 2 : 1; ++ desired_format.mBytesPerPacket = ++ desired_format.mChannelsPerFrame * sizeof(SInt16); ++ desired_format.mFramesPerPacket = 1; ++ desired_format.mBytesPerFrame = ++ desired_format.mChannelsPerFrame * sizeof(SInt16); ++ desired_format.mBitsPerChannel = sizeof(SInt16) * 8; + -+ _inDesiredFormat.mFormatFlags = ++ desired_format.mFormatFlags = + kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; +#ifdef WEBRTC_ARCH_BIG_ENDIAN -+ _inDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; ++ desired_format.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; +#endif -+ _inDesiredFormat.mFormatID = kAudioFormatLinearPCM; ++ desired_format.mFormatID = kAudioFormatLinearPCM; + ++ // Create the converter before changing any state, so a failure leaves the ++ // previous formats and the previous converter consistent with each other. + AudioConverterRef capture_converter = NULL; -+ OSStatus err = AudioConverterNew(&_inStreamFormat, &_inDesiredFormat, -+ &capture_converter); ++ OSStatus err = ++ AudioConverterNew(&input_format, &desired_format, &capture_converter); + if (err != noErr) { + logCAMsg(LS_ERROR, "AudioConverterNew() failed", (const char*)&err); + return false; + } + ++ if (!stereo) { ++ // Disable stereo recording when we only have one channel on the device. ++ _recChannels = 1; ++ RTC_LOG(LS_VERBOSE) << "Stereo recording unavailable on this device"; ++ } ++ ++ _inStreamFormat = input_format; ++ _inDesiredFormat = desired_format; ++ ++ if (_ptrAudioBuffer) { ++ // Update audio buffer with the selected parameters. ++ _ptrAudioBuffer->SetRecordingSampleRate(N_REC_SAMPLES_PER_SEC); ++ _ptrAudioBuffer->SetRecordingChannels((uint8_t)_recChannels); ++ } ++ + if (_captureConverter) { + WEBRTC_CA_LOG_WARN(AudioConverterDispose(_captureConverter)); + } @@ -138,17 +175,72 @@ index 3fdb71b..e4b26f5 100644 + return true; +} + ++int32_t AudioDeviceMac::UpdateCaptureLatency() { ++ OSStatus err = noErr; ++ UInt32 size = sizeof(UInt32); ++ ++ // Get capture device latency ++ AudioObjectPropertyAddress propertyAddress = { ++ kAudioDevicePropertyLatency, kAudioDevicePropertyScopeInput, 0}; ++ UInt32 latency = 0; ++ WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( ++ _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); ++ UInt32 latency_us = ++ (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); ++ ++ // Get capture stream latency ++ propertyAddress.mSelector = kAudioDevicePropertyStreams; ++ AudioStreamID stream = 0; ++ size = sizeof(AudioStreamID); ++ WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( ++ _inputDeviceID, &propertyAddress, 0, NULL, &size, &stream)); ++ propertyAddress.mSelector = kAudioStreamPropertyLatency; ++ size = sizeof(UInt32); ++ latency = 0; ++ WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData( ++ _inputDeviceID, &propertyAddress, 0, NULL, &size, &latency)); ++ latency_us += (UInt32)((1.0e6 * latency) / _inStreamFormat.mSampleRate); ++ ++ _captureLatencyUs = latency_us; ++ return 0; ++} ++ ++void AudioDeviceMac::DrainCaptureBuffer() { ++ // Only the capture worker reads `_captureBuffer`, so reading it empty here ++ // is safe against the IO proc writing to it concurrently. Bound the loop by ++ // what is buffered now, so a writer cannot keep it going. ++ size_t remaining = _captureBuffer.AvailableToRead(); ++ while (remaining > 0) { ++ const size_t read = _captureBuffer.Read(std::span( ++ _captureConvertData, ++ std::min(remaining, REC_BUF_SIZE_IN_SAMPLES))); ++ if (read == 0) { ++ break; ++ } ++ remaining -= read; ++ } ++} ++ +void AudioDeviceMac::ApplyPendingCaptureFormat() { ++ if (!_captureFormatChangePending.load(std::memory_order_acquire)) { ++ return; ++ } ++ ++ // The IO proc drops input while a change is pending, so what is still in ++ // the ring buffer was captured in the previous format. Discard it before ++ // clearing the flag, so the new converter never reads old-format samples ++ // and the channel interleaving cannot shift. ++ DrainCaptureBuffer(); ++ + AudioStreamBasicDescription format; + { + MutexLock lock(&_captureFormatMutex); -+ if (!_captureFormatChangePending.load(std::memory_order_acquire)) { -+ return; -+ } + format = _pendingInStreamFormat; + _captureFormatChangePending.store(false, std::memory_order_release); + } + ++ OSStatus err = noErr; ++ + if (format.mSampleRate == _inStreamFormat.mSampleRate && + format.mChannelsPerFrame == _inStreamFormat.mChannelsPerFrame && + format.mBytesPerPacket == _inStreamFormat.mBytesPerPacket && @@ -156,6 +248,11 @@ index 3fdb71b..e4b26f5 100644 + format.mBitsPerChannel == _inStreamFormat.mBitsPerChannel && + format.mFormatFlags == _inStreamFormat.mFormatFlags && + format.mFormatID == _inStreamFormat.mFormatID) { ++ // The converter may have been interrupted mid-conversion; reset it to ++ // match the drained ring buffer. ++ if (_captureConverter) { ++ WEBRTC_CA_LOG_WARN(AudioConverterReset(_captureConverter)); ++ } + return; + } + @@ -167,13 +264,18 @@ index 3fdb71b..e4b26f5 100644 + // Keep capturing with the previous converter instead of stopping the + // worker. The stream is still better than no audio at all. + RTC_LOG(LS_ERROR) << "Failed to apply the new capture stream format"; ++ return; ++ } ++ ++ if (UpdateCaptureLatency() == -1) { ++ RTC_LOG(LS_WARNING) << "Failed to update the capture latency"; + } +} + int32_t AudioDeviceMac::StartRecording() { RTC_LOG(LS_INFO) << "StartRecording"; MutexLock lock(&mutex_); -@@ -1323,7 +1393,10 @@ int32_t AudioDeviceMac::StopRecording() { +@@ -1331,7 +1454,10 @@ int32_t AudioDeviceMac::StopRecording() { mutex_.Lock(); } @@ -185,7 +287,7 @@ index 3fdb71b..e4b26f5 100644 // Remove listeners. AudioObjectPropertyAddress propertyAddress = { -@@ -1335,6 +1408,11 @@ int32_t AudioDeviceMac::StopRecording() { +@@ -1343,6 +1469,11 @@ int32_t AudioDeviceMac::StopRecording() { WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener( _inputDeviceID, &propertyAddress, &objectListenerProc, this)); @@ -197,7 +299,126 @@ index 3fdb71b..e4b26f5 100644 _recIsInitialized = false; _recording = false; -@@ -1942,12 +2020,6 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( +@@ -1444,8 +1575,6 @@ int32_t AudioDeviceMac::StopPlayout() { + mutex_.Lock(); + } + +- WEBRTC_CA_LOG_WARN(AudioConverterDispose(_renderConverter)); +- + // Remove listeners. + AudioObjectPropertyAddress propertyAddress = { + kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeOutput, 0}; +@@ -1466,6 +1595,10 @@ int32_t AudioDeviceMac::StopPlayout() { + } + } + ++ // The IO proc has stopped, and with the stream-format listener removed no ++ // new converter gets published, so all of them can be disposed now. ++ DisposeRenderConverters(); ++ + _playIsInitialized = false; + _playing = false; + +@@ -1740,9 +1873,44 @@ int32_t AudioDeviceMac::InitDevice(const uint16_t userDeviceIndex, + } + + OSStatus AudioDeviceMac::SetDesiredPlayoutFormat() { ++ OSStatus err = noErr; ++ WEBRTC_CA_RETURN_ON_ERR(ConfigureRenderConverter()); ++ WEBRTC_CA_RETURN_ON_ERR(ConfigurePlayoutBuffer()); ++ return 0; ++} ++ ++OSStatus AudioDeviceMac::ConfigureRenderConverter() { + // Our preferred format to work with. +- _outDesiredFormat.mSampleRate = N_PLAY_SAMPLES_PER_SEC; +- _outDesiredFormat.mChannelsPerFrame = _playChannels; ++ AudioStreamBasicDescription desired_format; ++ memset(&desired_format, 0, sizeof(desired_format)); ++ desired_format.mSampleRate = N_PLAY_SAMPLES_PER_SEC; ++ desired_format.mChannelsPerFrame = _playChannels; ++ desired_format.mBytesPerPacket = ++ desired_format.mChannelsPerFrame * sizeof(SInt16); ++ // In uncompressed audio, a packet is one frame. ++ desired_format.mFramesPerPacket = 1; ++ desired_format.mBytesPerFrame = ++ desired_format.mChannelsPerFrame * sizeof(SInt16); ++ desired_format.mBitsPerChannel = sizeof(SInt16) * 8; ++ ++ desired_format.mFormatFlags = ++ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; ++#ifdef WEBRTC_ARCH_BIG_ENDIAN ++ desired_format.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; ++#endif ++ desired_format.mFormatID = kAudioFormatLinearPCM; ++ ++ // Create the converter before changing any state, so a failure leaves the ++ // current render path intact. ++ AudioConverterRef converter = NULL; ++ OSStatus err = ++ AudioConverterNew(&desired_format, &_outStreamFormat, &converter); ++ if (err != noErr) { ++ logCAMsg(LS_ERROR, "Error in AudioConverterNew()", (const char*)&err); ++ return err; ++ } ++ ++ _outDesiredFormat = desired_format; + + if (_ptrAudioBuffer) { + // Update audio buffer with the selected parameters. +@@ -1756,24 +1924,36 @@ OSStatus AudioDeviceMac::SetDesiredPlayoutFormat() { + N_BUFFERS_OUT * ENGINE_PLAY_BUF_SIZE_IN_SAMPLES * + _outDesiredFormat.mChannelsPerFrame; + +- _outDesiredFormat.mBytesPerPacket = +- _outDesiredFormat.mChannelsPerFrame * sizeof(SInt16); +- // In uncompressed audio, a packet is one frame. +- _outDesiredFormat.mFramesPerPacket = 1; +- _outDesiredFormat.mBytesPerFrame = +- _outDesiredFormat.mChannelsPerFrame * sizeof(SInt16); +- _outDesiredFormat.mBitsPerChannel = sizeof(SInt16) * 8; ++ // On a stream format change this runs on the CoreAudio listener thread, ++ // while the IO proc may be using the current converter on the IO thread. ++ // It must not be disposed here: publish the new converter and keep the old ++ // one until playout has stopped (see DisposeRenderConverters()). ++ MutexLock lock(&_renderConverterMutex); ++ AudioConverterRef previous = ++ _renderConverter.exchange(converter, std::memory_order_acq_rel); ++ if (previous) { ++ _retiredRenderConverters.push_back(previous); ++ } + +- _outDesiredFormat.mFormatFlags = +- kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; +-#ifdef WEBRTC_ARCH_BIG_ENDIAN +- _outDesiredFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; +-#endif +- _outDesiredFormat.mFormatID = kAudioFormatLinearPCM; ++ return noErr; ++} + ++void AudioDeviceMac::DisposeRenderConverters() { ++ OSStatus err = noErr; ++ MutexLock lock(&_renderConverterMutex); ++ AudioConverterRef converter = ++ _renderConverter.exchange(NULL, std::memory_order_acq_rel); ++ if (converter) { ++ WEBRTC_CA_LOG_WARN(AudioConverterDispose(converter)); ++ } ++ for (AudioConverterRef retired : _retiredRenderConverters) { ++ WEBRTC_CA_LOG_WARN(AudioConverterDispose(retired)); ++ } ++ _retiredRenderConverters.clear(); ++} ++ ++OSStatus AudioDeviceMac::ConfigurePlayoutBuffer() { + OSStatus err = noErr; +- WEBRTC_CA_RETURN_ON_ERR(AudioConverterNew( +- &_outDesiredFormat, &_outStreamFormat, &_renderConverter)); + + // Try to set buffer size to desired value set to 20ms. + const uint16_t kPlayBufDelayFixed = 20; +@@ -1950,12 +2130,6 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( return -1; } @@ -210,7 +431,7 @@ index 3fdb71b..e4b26f5 100644 RTC_LOG(LS_VERBOSE) << "Stream format:"; RTC_LOG(LS_VERBOSE) << "mSampleRate = " << streamFormat.mSampleRate << ", mChannelsPerFrame = " -@@ -1973,33 +2045,40 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( +@@ -1981,33 +2155,51 @@ int32_t AudioDeviceMac::HandleStreamFormatChange( return -1; } @@ -248,9 +469,9 @@ index 3fdb71b..e4b26f5 100644 - memcpy(&_outStreamFormat, &streamFormat, sizeof(streamFormat)); + // A Bluetooth headset can change the playout stream format at runtime, + // e.g. A2DP (48 kHz) -> HFP after the microphone is enabled. Apply the -+ // new format instead of rejecting it: SetDesiredPlayoutFormat() rebuilds -+ // the render converter and the device buffer for the new sample rate. A -+ // channel-count downgrade is handled below. ++ // new format instead of rejecting it: rebuild the render converter and the ++ // device buffer for the new sample rate. A channel-count downgrade is ++ // handled below. + RTC_LOG(LS_INFO) << "Applying playout stream format change: sample_rate=" + << streamFormat.mSampleRate + << ", channels=" << streamFormat.mChannelsPerFrame; @@ -261,6 +482,9 @@ index 3fdb71b..e4b26f5 100644 - RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; - } - WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); ++ const AudioStreamBasicDescription previous_format = _outStreamFormat; ++ const uint8_t previous_channels = _playChannels; ++ + memcpy(&_outStreamFormat, &streamFormat, sizeof(streamFormat)); + + // Our preferred format to work with @@ -268,12 +492,33 @@ index 3fdb71b..e4b26f5 100644 + _playChannels = 1; + RTC_LOG(LS_VERBOSE) << "Stereo playout unavailable on this device"; } -+ WEBRTC_CA_RETURN_ON_ERR(SetDesiredPlayoutFormat()); ++ ++ if (ConfigureRenderConverter() != noErr) { ++ // Keep the previous format, which still matches the current converter. ++ _outStreamFormat = previous_format; ++ _playChannels = previous_channels; ++ RTC_LOG(LS_ERROR) << "Failed to apply the new playout stream format"; ++ return -1; ++ } ++ WEBRTC_CA_RETURN_ON_ERR(ConfigurePlayoutBuffer()); + return 0; } -@@ -2235,6 +2314,12 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, +@@ -2162,7 +2354,11 @@ OSStatus AudioDeviceMac::implDeviceIOProc(const AudioBufferList* inputData, + outputData->mBuffers->mDataByteSize / _outStreamFormat.mBytesPerFrame; + + // TODO(xians): signal an error somehow? +- err = AudioConverterFillComplexBuffer(_renderConverter, outConverterProc, ++ // Load the converter once: a stream format change may publish a new one ++ // concurrently, while this one stays valid until playout has stopped. ++ AudioConverterRef renderConverter = ++ _renderConverter.load(std::memory_order_acquire); ++ err = AudioConverterFillComplexBuffer(renderConverter, outConverterProc, + this, &size, outputData, NULL); + if (err != noErr) { + if (err == 1) { +@@ -2243,6 +2439,12 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, return 0; } @@ -286,7 +531,7 @@ index 3fdb71b..e4b26f5 100644 size_t bufSizeSamples = _captureBuffer.AvailableToRead(); int32_t captureDelayUs = -@@ -2247,9 +2332,10 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, +@@ -2255,9 +2457,10 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, _captureDelayUs = captureDelayUs; RTC_DCHECK(inputData->mNumberBuffers == 1); @@ -300,7 +545,7 @@ index 3fdb71b..e4b26f5 100644 _captureBuffer.Write(std::span( static_cast(inputData->mBuffers->mData), numSamples)); -@@ -2264,9 +2350,20 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, +@@ -2272,9 +2475,20 @@ OSStatus AudioDeviceMac::implInDeviceIOProc(const AudioBufferList* inputData, OSStatus AudioDeviceMac::implInConverterProc(UInt32* numberDataPackets, AudioBufferList* data) { RTC_DCHECK(data->mNumberBuffers == 1); @@ -321,7 +566,7 @@ index 3fdb71b..e4b26f5 100644 mach_timespec_t timeout; timeout.tv_sec = 0; timeout.tv_nsec = TIMER_PERIOD_MS; -@@ -2361,6 +2458,10 @@ bool AudioDeviceMac::RenderWorkerThread() { +@@ -2369,6 +2583,10 @@ bool AudioDeviceMac::RenderWorkerThread() { } bool AudioDeviceMac::CaptureWorkerThread() { @@ -332,7 +577,7 @@ index 3fdb71b..e4b26f5 100644 OSStatus err = noErr; UInt32 noRecSamples = ENGINE_REC_BUF_SIZE_IN_SAMPLES * _inDesiredFormat.mChannelsPerFrame; -@@ -2378,8 +2479,10 @@ bool AudioDeviceMac::CaptureWorkerThread() { +@@ -2386,8 +2604,10 @@ bool AudioDeviceMac::CaptureWorkerThread() { this, &size, &engineBuffer, NULL); if (err != noErr) { if (err == 1) { @@ -346,16 +591,30 @@ index 3fdb71b..e4b26f5 100644 logCAMsg(LS_ERROR, "Error in AudioConverterFillComplexBuffer()", (const char*)&err); diff --git a/modules/audio_device/mac/audio_device_mac.h b/modules/audio_device/mac/audio_device_mac.h -index 07a93e2..83e7625 100644 +index 07a93e2..4a559e9 100644 --- a/modules/audio_device/mac/audio_device_mac.h +++ b/modules/audio_device/mac/audio_device_mac.h -@@ -259,12 +259,21 @@ class AudioDeviceMac : public AudioDeviceGeneric { +@@ -18,6 +18,7 @@ + #include + #include + #include ++#include + + #include "absl/strings/string_view.h" + #include "modules/audio_device/audio_device_generic.h" +@@ -259,12 +260,27 @@ class AudioDeviceMac : public AudioDeviceGeneric { bool CaptureWorkerThread(); bool RenderWorkerThread(); + void ApplyPendingCaptureFormat(); + bool ConfigureCaptureConverter( + const AudioStreamBasicDescription& input_format); ++ int32_t UpdateCaptureLatency(); ++ void DrainCaptureBuffer(); ++ ++ OSStatus ConfigureRenderConverter(); ++ OSStatus ConfigurePlayoutBuffer(); ++ void DisposeRenderConverters(); + bool KeyPressed(); @@ -371,16 +630,26 @@ index 07a93e2..83e7625 100644 Event _stopEventRec; Event _stopEvent; -@@ -311,6 +320,12 @@ class AudioDeviceMac : public AudioDeviceGeneric { - AudioConverterRef _captureConverter; - AudioConverterRef _renderConverter; +@@ -309,7 +325,21 @@ class AudioDeviceMac : public AudioDeviceGeneric { + bool _macBookProPanRight; + AudioConverterRef _captureConverter; +- AudioConverterRef _renderConverter; ++ ++ // Used by the IO proc on the CoreAudio IO thread and replaced by the ++ // stream-format listener, so it is published atomically. Replaced ++ // converters may still be in use by the IO proc; they are kept in ++ // `_retiredRenderConverters` and disposed once playout has stopped. ++ std::atomic _renderConverter; ++ Mutex _renderConverterMutex; ++ std::vector _retiredRenderConverters ++ RTC_GUARDED_BY(_renderConverterMutex); ++ + // Set by the CoreAudio stream-format listener and applied by the capture + // worker, which owns `_captureConverter`. Recreating the converter from the + // listener would race with the capture worker using it. + std::atomic _captureFormatChangePending; + AudioStreamBasicDescription _pendingInStreamFormat; -+ + AudioStreamBasicDescription _outStreamFormat; AudioStreamBasicDescription _outDesiredFormat; - AudioStreamBasicDescription _inStreamFormat;