From 47bdcc1e246ee8efaab3d1eddff5ef26d21f2679 Mon Sep 17 00:00:00 2001 From: Abby Yeh Date: Sun, 11 Jun 2023 09:35:07 +0200 Subject: [PATCH] When updating audio session, update category, mode, options at once. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:15233 Change-Id: I5f1014dc93a780b05af1fbda198b2c5af25de077 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/308400 Reviewed-by: Kári Helgason Commit-Queue: Abby Yeh Cr-Commit-Position: refs/heads/main@{#40256} --- .../audio/RTCAudioSession+Configuration.mm | 42 ++++--------------- sdk/objc/components/audio/RTCAudioSession.h | 4 ++ sdk/objc/components/audio/RTCAudioSession.mm | 10 +++++ 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/sdk/objc/components/audio/RTCAudioSession+Configuration.mm b/sdk/objc/components/audio/RTCAudioSession+Configuration.mm index 449f31e9dd..4bf0a542e1 100644 --- a/sdk/objc/components/audio/RTCAudioSession+Configuration.mm +++ b/sdk/objc/components/audio/RTCAudioSession+Configuration.mm @@ -49,43 +49,17 @@ // everything we can. NSError *error = nil; - if (self.category != configuration.category || + if (self.category != configuration.category || self.mode != configuration.mode || self.categoryOptions != configuration.categoryOptions) { - NSError *categoryError = nil; + NSError *configuringError = nil; if (![self setCategory:configuration.category - withOptions:configuration.categoryOptions - error:&categoryError]) { - RTCLogError(@"Failed to set category: %@", - categoryError.localizedDescription); - error = categoryError; + mode:configuration.mode + options:configuration.categoryOptions + error:&configuringError]) { + RTCLogError(@"Failed to set category and mode: %@", configuringError.localizedDescription); + error = configuringError; } else { - RTCLog(@"Set category to: %@", configuration.category); - } - } - - if (self.mode != configuration.mode) { - NSError *modeError = nil; - if (![self setMode:configuration.mode error:&modeError]) { - RTCLogError(@"Failed to set mode: %@", - modeError.localizedDescription); - error = modeError; - } else { - RTCLog(@"Set mode to: %@", configuration.mode); - } - } - - // Sometimes category options don't stick after setting mode. - if (self.categoryOptions != configuration.categoryOptions) { - NSError *categoryError = nil; - if (![self setCategory:configuration.category - withOptions:configuration.categoryOptions - error:&categoryError]) { - RTCLogError(@"Failed to set category options: %@", - categoryError.localizedDescription); - error = categoryError; - } else { - RTCLog(@"Set category options to: %ld", - (long)configuration.categoryOptions); + RTCLog(@"Set category to: %@, mode: %@", configuration.category, configuration.mode); } } diff --git a/sdk/objc/components/audio/RTCAudioSession.h b/sdk/objc/components/audio/RTCAudioSession.h index 3b83b27ba5..729b5a2126 100644 --- a/sdk/objc/components/audio/RTCAudioSession.h +++ b/sdk/objc/components/audio/RTCAudioSession.h @@ -225,6 +225,10 @@ RTC_OBJC_EXPORT // AVAudioSession. `lockForConfiguration` must be called before using them // otherwise they will fail with kRTCAudioSessionErrorLockRequired. +- (BOOL)setCategory:(NSString *)category + mode:(NSString *)mode + options:(AVAudioSessionCategoryOptions)options + error:(NSError **)outError; - (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError; diff --git a/sdk/objc/components/audio/RTCAudioSession.mm b/sdk/objc/components/audio/RTCAudioSession.mm index 550a426d36..2a6a2022ea 100644 --- a/sdk/objc/components/audio/RTCAudioSession.mm +++ b/sdk/objc/components/audio/RTCAudioSession.mm @@ -407,6 +407,16 @@ ABSL_CONST_INIT thread_local bool mutex_locked = false; return success; } +- (BOOL)setCategory:(NSString *)category + mode:(NSString *)mode + options:(AVAudioSessionCategoryOptions)options + error:(NSError **)outError { + if (![self checkLock:outError]) { + return NO; + } + return [self.session setCategory:category mode:mode options:options error:outError]; +} + - (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError {