diff --git a/sdk/objc/components/audio/RTCAudioSession.h b/sdk/objc/components/audio/RTCAudioSession.h index f6a9ba5f0b..1eb0ee2b64 100644 --- a/sdk/objc/components/audio/RTCAudioSession.h +++ b/sdk/objc/components/audio/RTCAudioSession.h @@ -100,7 +100,7 @@ RTC_OBJC_EXPORT error:(NSError *)error; /** Called when audio session changed from output-only to input & output */ -- (void)audioSessionWillRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession; +- (void)audioSessionDidChangeRecordingEnabled:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession; @end diff --git a/sdk/objc/components/audio/RTCAudioSession.mm b/sdk/objc/components/audio/RTCAudioSession.mm index 22ed9afd64..b639d6f4af 100644 --- a/sdk/objc/components/audio/RTCAudioSession.mm +++ b/sdk/objc/components/audio/RTCAudioSession.mm @@ -103,7 +103,7 @@ - (instancetype)initWithAudioSession:(id)audioSession { options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class]; - self.isRecordingEnabled = [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]; + _isRecordingEnabled = [self sessionCategoryIsRecordingEnabled]; RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self); } @@ -492,14 +492,13 @@ - (void)handleRouteChangeNotification:(NSNotification *)notification { RTCLog(@"Audio route changed: OldDeviceUnavailable"); break; case AVAudioSessionRouteChangeReasonCategoryChange: - RTCLog(@"Audio route changed: CategoryChange to :%@", - self.session.category); - if (!self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) { - self.isRecordingEnabled = true; - [self notifyWillRecord]; - } - if (self.isRecordingEnabled && [self.session.category isEqualToString:AVAudioSessionCategoryPlayback]) { - self.isRecordingEnabled = false; + RTCLog(@"Audio route changed: CategoryChange to :%@", self.session.category); + { + BOOL newValue = [self sessionCategoryIsRecordingEnabled]; + if (_isRecordingEnabled != newValue) { + _isRecordingEnabled = newValue; + [self notifyDidChangeAudioSessionRecordingEnabled]; + } } break; case AVAudioSessionRouteChangeReasonOverride: @@ -713,7 +712,7 @@ - (BOOL)unconfigureWebRTCSession:(NSError **)outError { } RTCLog(@"Unconfiguring audio session for WebRTC."); [self setActive:NO error:outError]; - self.isRecordingEnabled = NO; + _isRecordingEnabled = NO; return YES; } @@ -926,14 +925,18 @@ - (void)notifyFailedToSetActive:(BOOL)active error:(NSError *)error { } } -- (void)notifyWillRecord { +- (void)notifyDidChangeAudioSessionRecordingEnabled { for (auto delegate : self.delegates) { - SEL sel = @selector(audioSessionWillRecord:); + SEL sel = @selector(audioSessionDidChangeRecordingEnabled:); if ([delegate respondsToSelector:sel]) { - [delegate audioSessionWillRecord:self]; + [delegate audioSessionDidChangeRecordingEnabled:self]; } } } +-(BOOL)sessionCategoryIsRecordingEnabled { + return [_session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord] || + [_session.category isEqualToString:AVAudioSessionCategoryRecord]; +} @end diff --git a/sdk/objc/components/audio/RTCNativeAudioSessionDelegateAdapter.mm b/sdk/objc/components/audio/RTCNativeAudioSessionDelegateAdapter.mm index 2f0f094262..6f8978b066 100644 --- a/sdk/objc/components/audio/RTCNativeAudioSessionDelegateAdapter.mm +++ b/sdk/objc/components/audio/RTCNativeAudioSessionDelegateAdapter.mm @@ -86,9 +86,9 @@ - (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession _observer->OnChangedOutputVolume(); } -- (void)audioSessionWillRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)session { +- (void)audioSessionDidChangeRecordingEnabled:(RTC_OBJC_TYPE(RTCAudioSession) *)session { // re-trigger audio unit init, by using interrupt ended callback - _observer->OnAudioWillRecord(); + _observer->OnChangedRecordingEnabled(); } @end diff --git a/sdk/objc/native/src/audio/audio_device_ios.h b/sdk/objc/native/src/audio/audio_device_ios.h index 53920859f7..8d31ea282a 100644 --- a/sdk/objc/native/src/audio/audio_device_ios.h +++ b/sdk/objc/native/src/audio/audio_device_ios.h @@ -145,7 +145,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric, void OnValidRouteChange() override; void OnCanPlayOrRecordChange(bool can_play_or_record) override; void OnChangedOutputVolume() override; - void OnAudioWillRecord() override; + void OnChangedRecordingEnabled() override; // VoiceProcessingAudioUnitObserver methods. OSStatus OnDeliverRecordedData(AudioUnitRenderActionFlags* flags, @@ -173,7 +173,7 @@ class AudioDeviceIOS : public AudioDeviceGeneric, void HandleSampleRateChange(float sample_rate); void HandlePlayoutGlitchDetected(); void HandleOutputVolumeChange(); - void HandleAudioWillRecord(); + void HandleAudioSessionRecordingEnabledChange(); // Uses current |playout_parameters_| and |record_parameters_| to inform // the audio device buffer (ADB) about our internal audio parameters. diff --git a/sdk/objc/native/src/audio/audio_device_ios.mm b/sdk/objc/native/src/audio/audio_device_ios.mm index 93d173a21f..af67019d9a 100644 --- a/sdk/objc/native/src/audio/audio_device_ios.mm +++ b/sdk/objc/native/src/audio/audio_device_ios.mm @@ -68,7 +68,7 @@ kMessageTypeCanPlayOrRecordChange, kMessageTypePlayoutGlitchDetected, kMessageOutputVolumeChange, - kMessageTypeAudioWillRecord, + kMessageTypeRecordingEnabledChange, }; using ios::CheckAndLogError; @@ -368,9 +368,9 @@ static void LogDeviceInfo() { thread_->Post(RTC_FROM_HERE, this, kMessageOutputVolumeChange); } -void AudioDeviceIOS::OnAudioWillRecord() { +void AudioDeviceIOS::OnChangedRecordingEnabled() { RTC_DCHECK(thread_); - thread_->Post(RTC_FROM_HERE, this, kMessageTypeAudioWillRecord); + thread_->Post(RTC_FROM_HERE, this, kMessageTypeRecordingEnabledChange); } OSStatus AudioDeviceIOS::OnDeliverRecordedData(AudioUnitRenderActionFlags* flags, @@ -503,8 +503,9 @@ static void LogDeviceInfo() { case kMessageOutputVolumeChange: HandleOutputVolumeChange(); break; - case kMessageTypeAudioWillRecord: - HandleAudioWillRecord(); + case kMessageTypeRecordingEnabledChange: + HandleAudioSessionRecordingEnabledChange(); + break; } } @@ -668,10 +669,10 @@ static void LogDeviceInfo() { last_output_volume_change_time_ = rtc::TimeMillis(); } -void AudioDeviceIOS::HandleAudioWillRecord() { +void AudioDeviceIOS::HandleAudioSessionRecordingEnabledChange() { RTC_DCHECK_RUN_ON(&thread_checker_); - LOGI() << "HandleAudioWillRecord"; + LOGI() << "HandleAudioSessionRecordingEnabledChange"; // If we don't have an audio unit yet, or the audio unit is uninitialized, // there is no work to do. diff --git a/sdk/objc/native/src/audio/audio_session_observer.h b/sdk/objc/native/src/audio/audio_session_observer.h index c050fb7f5f..978d8e2cdd 100644 --- a/sdk/objc/native/src/audio/audio_session_observer.h +++ b/sdk/objc/native/src/audio/audio_session_observer.h @@ -32,7 +32,7 @@ class AudioSessionObserver { virtual void OnChangedOutputVolume() = 0; - virtual void OnAudioWillRecord() = 0; + virtual void OnChangedRecordingEnabled() = 0; protected: virtual ~AudioSessionObserver() {}