Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 11 | #import "RTCAudioSession+Private.h" |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 12 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 13 | #import <UIKit/UIKit.h> |
| 14 | |
Joe Chen | 0b3a6e3 | 2019-12-26 23:01:42 -0800 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Niels Möller | 072c008 | 2021-02-15 16:30:44 +0100 | [diff] [blame] | 19 | #include "rtc_base/synchronization/mutex.h" |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame] | 20 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 21 | #import "RTCAudioSessionConfiguration.h" |
| 22 | #import "base/RTCLogging.h" |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame] | 23 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 24 | NSString *const kRTCAudioSessionErrorDomain = @"org.webrtc.RTC_OBJC_TYPE(RTCAudioSession)"; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 25 | NSInteger const kRTCAudioSessionErrorLockRequired = -1; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 26 | NSInteger const kRTCAudioSessionErrorConfiguration = -2; |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 27 | NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume"; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 28 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 29 | @interface RTC_OBJC_TYPE (RTCAudioSession) |
| 30 | () @property(nonatomic, |
| 31 | readonly) std::vector<__weak id<RTC_OBJC_TYPE(RTCAudioSessionDelegate)> > delegates; |
Joe Chen | 0b3a6e3 | 2019-12-26 23:01:42 -0800 | [diff] [blame] | 32 | @end |
| 33 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 34 | // This class needs to be thread-safe because it is accessed from many threads. |
| 35 | // TODO(tkchin): Consider more granular locking. We're not expecting a lot of |
| 36 | // lock contention so coarse locks should be fine for now. |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 37 | @implementation RTC_OBJC_TYPE (RTCAudioSession) { |
Niels Möller | 072c008 | 2021-02-15 16:30:44 +0100 | [diff] [blame] | 38 | webrtc::Mutex _mutex; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 39 | AVAudioSession *_session; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 40 | volatile int _activationCount; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 41 | volatile int _webRTCSessionCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 42 | BOOL _isActive; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 43 | BOOL _useManualAudio; |
| 44 | BOOL _isAudioEnabled; |
| 45 | BOOL _canPlayOrRecord; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 46 | BOOL _isInterrupted; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @synthesize session = _session; |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 50 | @synthesize delegates = _delegates; |
Joe Chen | 0c05b1a | 2019-05-07 10:46:22 -0700 | [diff] [blame] | 51 | @synthesize ignoresPreferredAttributeConfigurationErrors = |
| 52 | _ignoresPreferredAttributeConfigurationErrors; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 53 | |
| 54 | + (instancetype)sharedInstance { |
| 55 | static dispatch_once_t onceToken; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 56 | static RTC_OBJC_TYPE(RTCAudioSession) *sharedInstance = nil; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 57 | dispatch_once(&onceToken, ^{ |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 58 | sharedInstance = [[self alloc] init]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 59 | }); |
| 60 | return sharedInstance; |
| 61 | } |
| 62 | |
| 63 | - (instancetype)init { |
Peter Hanspers | 4721736 | 2017-10-05 11:39:15 +0200 | [diff] [blame] | 64 | return [self initWithAudioSession:[AVAudioSession sharedInstance]]; |
| 65 | } |
| 66 | |
| 67 | /** This initializer provides a way for unit tests to inject a fake/mock audio session. */ |
| 68 | - (instancetype)initWithAudioSession:(id)audioSession { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 69 | if (self = [super init]) { |
Peter Hanspers | 4721736 | 2017-10-05 11:39:15 +0200 | [diff] [blame] | 70 | _session = audioSession; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 71 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 72 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 73 | [center addObserver:self |
| 74 | selector:@selector(handleInterruptionNotification:) |
| 75 | name:AVAudioSessionInterruptionNotification |
| 76 | object:nil]; |
| 77 | [center addObserver:self |
| 78 | selector:@selector(handleRouteChangeNotification:) |
| 79 | name:AVAudioSessionRouteChangeNotification |
| 80 | object:nil]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 81 | [center addObserver:self |
| 82 | selector:@selector(handleMediaServicesWereLost:) |
| 83 | name:AVAudioSessionMediaServicesWereLostNotification |
| 84 | object:nil]; |
| 85 | [center addObserver:self |
| 86 | selector:@selector(handleMediaServicesWereReset:) |
| 87 | name:AVAudioSessionMediaServicesWereResetNotification |
| 88 | object:nil]; |
henrika | f1363fd | 2016-09-27 06:06:44 -0700 | [diff] [blame] | 89 | // Posted on the main thread when the primary audio from other applications |
| 90 | // starts and stops. Foreground applications may use this notification as a |
| 91 | // hint to enable or disable audio that is secondary. |
| 92 | [center addObserver:self |
| 93 | selector:@selector(handleSilenceSecondaryAudioHintNotification:) |
| 94 | name:AVAudioSessionSilenceSecondaryAudioHintNotification |
| 95 | object:nil]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 96 | // Also track foreground event in order to deal with interruption ended situation. |
| 97 | [center addObserver:self |
| 98 | selector:@selector(handleApplicationDidBecomeActive:) |
| 99 | name:UIApplicationDidBecomeActiveNotification |
| 100 | object:nil]; |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 101 | [_session addObserver:self |
| 102 | forKeyPath:kRTCAudioSessionOutputVolumeSelector |
| 103 | options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 104 | context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class]; |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 105 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 106 | RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): init.", self); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 107 | } |
| 108 | return self; |
| 109 | } |
| 110 | |
| 111 | - (void)dealloc { |
| 112 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
Peter Hanspers | 4721736 | 2017-10-05 11:39:15 +0200 | [diff] [blame] | 113 | [_session removeObserver:self |
| 114 | forKeyPath:kRTCAudioSessionOutputVolumeSelector |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 115 | context:(__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class]; |
| 116 | RTCLog(@"RTC_OBJC_TYPE(RTCAudioSession) (%p): dealloc.", self); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 119 | - (NSString *)description { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 120 | NSString *format = @"RTC_OBJC_TYPE(RTCAudioSession): {\n" |
| 121 | " category: %@\n" |
| 122 | " categoryOptions: %ld\n" |
| 123 | " mode: %@\n" |
| 124 | " isActive: %d\n" |
| 125 | " sampleRate: %.2f\n" |
| 126 | " IOBufferDuration: %f\n" |
| 127 | " outputNumberOfChannels: %ld\n" |
| 128 | " inputNumberOfChannels: %ld\n" |
| 129 | " outputLatency: %f\n" |
| 130 | " inputLatency: %f\n" |
| 131 | " outputVolume: %f\n" |
| 132 | "}"; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 133 | NSString *description = [NSString stringWithFormat:format, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 134 | self.category, (long)self.categoryOptions, self.mode, |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 135 | self.isActive, self.sampleRate, self.IOBufferDuration, |
| 136 | self.outputNumberOfChannels, self.inputNumberOfChannels, |
henrika | c5aea65 | 2016-09-21 07:45:55 -0700 | [diff] [blame] | 137 | self.outputLatency, self.inputLatency, self.outputVolume]; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 138 | return description; |
| 139 | } |
| 140 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 141 | - (void)setIsActive:(BOOL)isActive { |
| 142 | @synchronized(self) { |
| 143 | _isActive = isActive; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | - (BOOL)isActive { |
| 148 | @synchronized(self) { |
| 149 | return _isActive; |
| 150 | } |
| 151 | } |
| 152 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 153 | - (void)setUseManualAudio:(BOOL)useManualAudio { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 154 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 155 | if (_useManualAudio == useManualAudio) { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 158 | _useManualAudio = useManualAudio; |
| 159 | } |
| 160 | [self updateCanPlayOrRecord]; |
| 161 | } |
| 162 | |
| 163 | - (BOOL)useManualAudio { |
| 164 | @synchronized(self) { |
| 165 | return _useManualAudio; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 169 | - (void)setIsAudioEnabled:(BOOL)isAudioEnabled { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 170 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 171 | if (_isAudioEnabled == isAudioEnabled) { |
| 172 | return; |
| 173 | } |
| 174 | _isAudioEnabled = isAudioEnabled; |
| 175 | } |
| 176 | [self updateCanPlayOrRecord]; |
| 177 | } |
| 178 | |
| 179 | - (BOOL)isAudioEnabled { |
| 180 | @synchronized(self) { |
| 181 | return _isAudioEnabled; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Joe Chen | 0c05b1a | 2019-05-07 10:46:22 -0700 | [diff] [blame] | 185 | - (void)setIgnoresPreferredAttributeConfigurationErrors: |
| 186 | (BOOL)ignoresPreferredAttributeConfigurationErrors { |
| 187 | @synchronized(self) { |
| 188 | if (_ignoresPreferredAttributeConfigurationErrors == |
| 189 | ignoresPreferredAttributeConfigurationErrors) { |
| 190 | return; |
| 191 | } |
| 192 | _ignoresPreferredAttributeConfigurationErrors = ignoresPreferredAttributeConfigurationErrors; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | - (BOOL)ignoresPreferredAttributeConfigurationErrors { |
| 197 | @synchronized(self) { |
| 198 | return _ignoresPreferredAttributeConfigurationErrors; |
| 199 | } |
| 200 | } |
| 201 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 202 | // TODO(tkchin): Check for duplicates. |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 203 | - (void)addDelegate:(id<RTC_OBJC_TYPE(RTCAudioSessionDelegate)>)delegate { |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 204 | RTCLog(@"Adding delegate: (%p)", delegate); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 205 | if (!delegate) { |
| 206 | return; |
| 207 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 208 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 209 | _delegates.push_back(delegate); |
| 210 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 214 | - (void)removeDelegate:(id<RTC_OBJC_TYPE(RTCAudioSessionDelegate)>)delegate { |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 215 | RTCLog(@"Removing delegate: (%p)", delegate); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 216 | if (!delegate) { |
| 217 | return; |
| 218 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 219 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 220 | _delegates.erase(std::remove(_delegates.begin(), |
| 221 | _delegates.end(), |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 222 | delegate), |
| 223 | _delegates.end()); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 224 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
kthelgason | ee8b861 | 2017-03-31 04:50:27 -0700 | [diff] [blame] | 228 | #pragma clang diagnostic push |
| 229 | #pragma clang diagnostic ignored "-Wthread-safety-analysis" |
| 230 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 231 | - (void)lockForConfiguration { |
Niels Möller | 072c008 | 2021-02-15 16:30:44 +0100 | [diff] [blame] | 232 | _mutex.Lock(); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | - (void)unlockForConfiguration { |
Niels Möller | 072c008 | 2021-02-15 16:30:44 +0100 | [diff] [blame] | 236 | _mutex.Unlock(); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 237 | } |
| 238 | |
kthelgason | ee8b861 | 2017-03-31 04:50:27 -0700 | [diff] [blame] | 239 | #pragma clang diagnostic pop |
| 240 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 241 | #pragma mark - AVAudioSession proxy methods |
| 242 | |
| 243 | - (NSString *)category { |
| 244 | return self.session.category; |
| 245 | } |
| 246 | |
| 247 | - (AVAudioSessionCategoryOptions)categoryOptions { |
| 248 | return self.session.categoryOptions; |
| 249 | } |
| 250 | |
| 251 | - (NSString *)mode { |
| 252 | return self.session.mode; |
| 253 | } |
| 254 | |
| 255 | - (BOOL)secondaryAudioShouldBeSilencedHint { |
| 256 | return self.session.secondaryAudioShouldBeSilencedHint; |
| 257 | } |
| 258 | |
| 259 | - (AVAudioSessionRouteDescription *)currentRoute { |
| 260 | return self.session.currentRoute; |
| 261 | } |
| 262 | |
| 263 | - (NSInteger)maximumInputNumberOfChannels { |
| 264 | return self.session.maximumInputNumberOfChannels; |
| 265 | } |
| 266 | |
| 267 | - (NSInteger)maximumOutputNumberOfChannels { |
| 268 | return self.session.maximumOutputNumberOfChannels; |
| 269 | } |
| 270 | |
| 271 | - (float)inputGain { |
| 272 | return self.session.inputGain; |
| 273 | } |
| 274 | |
| 275 | - (BOOL)inputGainSettable { |
| 276 | return self.session.inputGainSettable; |
| 277 | } |
| 278 | |
| 279 | - (BOOL)inputAvailable { |
| 280 | return self.session.inputAvailable; |
| 281 | } |
| 282 | |
| 283 | - (NSArray<AVAudioSessionDataSourceDescription *> *)inputDataSources { |
| 284 | return self.session.inputDataSources; |
| 285 | } |
| 286 | |
| 287 | - (AVAudioSessionDataSourceDescription *)inputDataSource { |
| 288 | return self.session.inputDataSource; |
| 289 | } |
| 290 | |
| 291 | - (NSArray<AVAudioSessionDataSourceDescription *> *)outputDataSources { |
| 292 | return self.session.outputDataSources; |
| 293 | } |
| 294 | |
| 295 | - (AVAudioSessionDataSourceDescription *)outputDataSource { |
| 296 | return self.session.outputDataSource; |
| 297 | } |
| 298 | |
| 299 | - (double)sampleRate { |
| 300 | return self.session.sampleRate; |
| 301 | } |
| 302 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 303 | - (double)preferredSampleRate { |
| 304 | return self.session.preferredSampleRate; |
| 305 | } |
| 306 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 307 | - (NSInteger)inputNumberOfChannels { |
| 308 | return self.session.inputNumberOfChannels; |
| 309 | } |
| 310 | |
| 311 | - (NSInteger)outputNumberOfChannels { |
| 312 | return self.session.outputNumberOfChannels; |
| 313 | } |
| 314 | |
| 315 | - (float)outputVolume { |
| 316 | return self.session.outputVolume; |
| 317 | } |
| 318 | |
| 319 | - (NSTimeInterval)inputLatency { |
| 320 | return self.session.inputLatency; |
| 321 | } |
| 322 | |
| 323 | - (NSTimeInterval)outputLatency { |
| 324 | return self.session.outputLatency; |
| 325 | } |
| 326 | |
| 327 | - (NSTimeInterval)IOBufferDuration { |
| 328 | return self.session.IOBufferDuration; |
| 329 | } |
| 330 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 331 | - (NSTimeInterval)preferredIOBufferDuration { |
| 332 | return self.session.preferredIOBufferDuration; |
| 333 | } |
| 334 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 335 | - (BOOL)setActive:(BOOL)active |
| 336 | error:(NSError **)outError { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 337 | int activationCount = _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 338 | if (!active && activationCount == 0) { |
| 339 | RTCLogWarning(@"Attempting to deactivate without prior activation."); |
| 340 | } |
JT Teh | c1f083d | 2018-04-25 09:19:35 -0700 | [diff] [blame] | 341 | [self notifyWillSetActive:active]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 342 | BOOL success = YES; |
| 343 | BOOL isActive = self.isActive; |
| 344 | // Keep a local error so we can log it. |
| 345 | NSError *error = nil; |
| 346 | BOOL shouldSetActive = |
| 347 | (active && !isActive) || (!active && isActive && activationCount == 1); |
| 348 | // Attempt to activate if we're not active. |
| 349 | // Attempt to deactivate if we're active and it's the last unbalanced call. |
| 350 | if (shouldSetActive) { |
| 351 | AVAudioSession *session = self.session; |
| 352 | // AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation is used to ensure |
| 353 | // that other audio sessions that were interrupted by our session can return |
| 354 | // to their active state. It is recommended for VoIP apps to use this |
| 355 | // option. |
| 356 | AVAudioSessionSetActiveOptions options = |
| 357 | active ? 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 358 | success = [session setActive:active |
| 359 | withOptions:options |
| 360 | error:&error]; |
| 361 | if (outError) { |
| 362 | *outError = error; |
| 363 | } |
| 364 | } |
| 365 | if (success) { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 366 | if (active) { |
Abby Yeh | 3135772 | 2021-03-09 01:55:31 +0800 | [diff] [blame] | 367 | if (shouldSetActive) { |
| 368 | self.isActive = active; |
| 369 | if (self.isInterrupted) { |
| 370 | self.isInterrupted = NO; |
| 371 | [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
| 372 | } |
| 373 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 374 | [self incrementActivationCount]; |
Abby Yeh | 3135772 | 2021-03-09 01:55:31 +0800 | [diff] [blame] | 375 | [self notifyDidSetActive:active]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 376 | } |
| 377 | } else { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 378 | RTCLogError(@"Failed to setActive:%d. Error: %@", |
| 379 | active, error.localizedDescription); |
JT Teh | c1f083d | 2018-04-25 09:19:35 -0700 | [diff] [blame] | 380 | [self notifyFailedToSetActive:active error:error]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 381 | } |
Abby Yeh | 3135772 | 2021-03-09 01:55:31 +0800 | [diff] [blame] | 382 | // Set isActive and decrement activation count on deactivation |
| 383 | // whether or not it succeeded. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 384 | if (!active) { |
Abby Yeh | 3135772 | 2021-03-09 01:55:31 +0800 | [diff] [blame] | 385 | self.isActive = active; |
| 386 | [self notifyDidSetActive:active]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 387 | [self decrementActivationCount]; |
| 388 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 389 | RTCLog(@"Number of current activations: %d", _activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 390 | return success; |
| 391 | } |
| 392 | |
| 393 | - (BOOL)setCategory:(NSString *)category |
| 394 | withOptions:(AVAudioSessionCategoryOptions)options |
| 395 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 396 | return [self.session setCategory:category withOptions:options error:outError]; |
| 397 | } |
| 398 | |
| 399 | - (BOOL)setMode:(NSString *)mode error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 400 | return [self.session setMode:mode error:outError]; |
| 401 | } |
| 402 | |
| 403 | - (BOOL)setInputGain:(float)gain error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 404 | return [self.session setInputGain:gain error:outError]; |
| 405 | } |
| 406 | |
| 407 | - (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 408 | return [self.session setPreferredSampleRate:sampleRate error:outError]; |
| 409 | } |
| 410 | |
| 411 | - (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration |
| 412 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 413 | return [self.session setPreferredIOBufferDuration:duration error:outError]; |
| 414 | } |
| 415 | |
| 416 | - (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count |
| 417 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 418 | return [self.session setPreferredInputNumberOfChannels:count error:outError]; |
| 419 | } |
| 420 | - (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count |
| 421 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 422 | return [self.session setPreferredOutputNumberOfChannels:count error:outError]; |
| 423 | } |
| 424 | |
| 425 | - (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride |
| 426 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 427 | return [self.session overrideOutputAudioPort:portOverride error:outError]; |
| 428 | } |
| 429 | |
| 430 | - (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort |
| 431 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 432 | return [self.session setPreferredInput:inPort error:outError]; |
| 433 | } |
| 434 | |
| 435 | - (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 436 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 437 | return [self.session setInputDataSource:dataSource error:outError]; |
| 438 | } |
| 439 | |
| 440 | - (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 441 | error:(NSError **)outError { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 442 | return [self.session setOutputDataSource:dataSource error:outError]; |
| 443 | } |
| 444 | |
| 445 | #pragma mark - Notifications |
| 446 | |
| 447 | - (void)handleInterruptionNotification:(NSNotification *)notification { |
| 448 | NSNumber* typeNumber = |
| 449 | notification.userInfo[AVAudioSessionInterruptionTypeKey]; |
| 450 | AVAudioSessionInterruptionType type = |
| 451 | (AVAudioSessionInterruptionType)typeNumber.unsignedIntegerValue; |
| 452 | switch (type) { |
| 453 | case AVAudioSessionInterruptionTypeBegan: |
| 454 | RTCLog(@"Audio session interruption began."); |
| 455 | self.isActive = NO; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 456 | self.isInterrupted = YES; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 457 | [self notifyDidBeginInterruption]; |
| 458 | break; |
| 459 | case AVAudioSessionInterruptionTypeEnded: { |
| 460 | RTCLog(@"Audio session interruption ended."); |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 461 | self.isInterrupted = NO; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 462 | [self updateAudioSessionAfterEvent]; |
| 463 | NSNumber *optionsNumber = |
| 464 | notification.userInfo[AVAudioSessionInterruptionOptionKey]; |
| 465 | AVAudioSessionInterruptionOptions options = |
| 466 | optionsNumber.unsignedIntegerValue; |
| 467 | BOOL shouldResume = |
| 468 | options & AVAudioSessionInterruptionOptionShouldResume; |
| 469 | [self notifyDidEndInterruptionWithShouldResumeSession:shouldResume]; |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | - (void)handleRouteChangeNotification:(NSNotification *)notification { |
| 476 | // Get reason for current route change. |
| 477 | NSNumber* reasonNumber = |
| 478 | notification.userInfo[AVAudioSessionRouteChangeReasonKey]; |
| 479 | AVAudioSessionRouteChangeReason reason = |
| 480 | (AVAudioSessionRouteChangeReason)reasonNumber.unsignedIntegerValue; |
| 481 | RTCLog(@"Audio route changed:"); |
| 482 | switch (reason) { |
| 483 | case AVAudioSessionRouteChangeReasonUnknown: |
| 484 | RTCLog(@"Audio route changed: ReasonUnknown"); |
| 485 | break; |
| 486 | case AVAudioSessionRouteChangeReasonNewDeviceAvailable: |
| 487 | RTCLog(@"Audio route changed: NewDeviceAvailable"); |
| 488 | break; |
| 489 | case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: |
| 490 | RTCLog(@"Audio route changed: OldDeviceUnavailable"); |
| 491 | break; |
| 492 | case AVAudioSessionRouteChangeReasonCategoryChange: |
| 493 | RTCLog(@"Audio route changed: CategoryChange to :%@", |
| 494 | self.session.category); |
| 495 | break; |
| 496 | case AVAudioSessionRouteChangeReasonOverride: |
| 497 | RTCLog(@"Audio route changed: Override"); |
| 498 | break; |
| 499 | case AVAudioSessionRouteChangeReasonWakeFromSleep: |
| 500 | RTCLog(@"Audio route changed: WakeFromSleep"); |
| 501 | break; |
| 502 | case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory: |
| 503 | RTCLog(@"Audio route changed: NoSuitableRouteForCategory"); |
| 504 | break; |
| 505 | case AVAudioSessionRouteChangeReasonRouteConfigurationChange: |
| 506 | RTCLog(@"Audio route changed: RouteConfigurationChange"); |
| 507 | break; |
| 508 | } |
| 509 | AVAudioSessionRouteDescription* previousRoute = |
| 510 | notification.userInfo[AVAudioSessionRouteChangePreviousRouteKey]; |
| 511 | // Log previous route configuration. |
| 512 | RTCLog(@"Previous route: %@\nCurrent route:%@", |
| 513 | previousRoute, self.session.currentRoute); |
| 514 | [self notifyDidChangeRouteWithReason:reason previousRoute:previousRoute]; |
| 515 | } |
| 516 | |
| 517 | - (void)handleMediaServicesWereLost:(NSNotification *)notification { |
| 518 | RTCLog(@"Media services were lost."); |
| 519 | [self updateAudioSessionAfterEvent]; |
| 520 | [self notifyMediaServicesWereLost]; |
| 521 | } |
| 522 | |
| 523 | - (void)handleMediaServicesWereReset:(NSNotification *)notification { |
| 524 | RTCLog(@"Media services were reset."); |
| 525 | [self updateAudioSessionAfterEvent]; |
| 526 | [self notifyMediaServicesWereReset]; |
| 527 | } |
| 528 | |
henrika | f1363fd | 2016-09-27 06:06:44 -0700 | [diff] [blame] | 529 | - (void)handleSilenceSecondaryAudioHintNotification:(NSNotification *)notification { |
| 530 | // TODO(henrika): just adding logs here for now until we know if we are ever |
| 531 | // see this notification and might be affected by it or if further actions |
| 532 | // are required. |
| 533 | NSNumber *typeNumber = |
| 534 | notification.userInfo[AVAudioSessionSilenceSecondaryAudioHintTypeKey]; |
| 535 | AVAudioSessionSilenceSecondaryAudioHintType type = |
| 536 | (AVAudioSessionSilenceSecondaryAudioHintType)typeNumber.unsignedIntegerValue; |
| 537 | switch (type) { |
| 538 | case AVAudioSessionSilenceSecondaryAudioHintTypeBegin: |
| 539 | RTCLog(@"Another application's primary audio has started."); |
| 540 | break; |
| 541 | case AVAudioSessionSilenceSecondaryAudioHintTypeEnd: |
| 542 | RTCLog(@"Another application's primary audio has stopped."); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 547 | - (void)handleApplicationDidBecomeActive:(NSNotification *)notification { |
Zeke Chin | 8280a56 | 2018-07-10 13:53:55 -0700 | [diff] [blame] | 548 | BOOL isInterrupted = self.isInterrupted; |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 549 | RTCLog(@"Application became active after an interruption. Treating as interruption " |
Zeke Chin | 8280a56 | 2018-07-10 13:53:55 -0700 | [diff] [blame] | 550 | "end. isInterrupted changed from %d to 0.", |
| 551 | isInterrupted); |
| 552 | if (isInterrupted) { |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 553 | self.isInterrupted = NO; |
| 554 | [self updateAudioSessionAfterEvent]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 555 | } |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 556 | // Always treat application becoming active as an interruption end event. |
| 557 | [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 560 | #pragma mark - Private |
| 561 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 562 | - (std::vector<__weak id<RTC_OBJC_TYPE(RTCAudioSessionDelegate)> >)delegates { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 563 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 564 | // Note: this returns a copy. |
| 565 | return _delegates; |
| 566 | } |
| 567 | } |
| 568 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 569 | // TODO(tkchin): check for duplicates. |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 570 | - (void)pushDelegate:(id<RTC_OBJC_TYPE(RTCAudioSessionDelegate)>)delegate { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 571 | @synchronized(self) { |
| 572 | _delegates.insert(_delegates.begin(), delegate); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | - (void)removeZeroedDelegates { |
| 577 | @synchronized(self) { |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 578 | _delegates.erase( |
| 579 | std::remove_if(_delegates.begin(), |
| 580 | _delegates.end(), |
| 581 | [](id delegate) -> bool { return delegate == nil; }), |
| 582 | _delegates.end()); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 586 | - (int)activationCount { |
| 587 | return _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 590 | - (int)incrementActivationCount { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 591 | RTCLog(@"Incrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 592 | return rtc::AtomicOps::Increment(&_activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | - (NSInteger)decrementActivationCount { |
| 596 | RTCLog(@"Decrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 597 | return rtc::AtomicOps::Decrement(&_activationCount); |
| 598 | } |
| 599 | |
| 600 | - (int)webRTCSessionCount { |
| 601 | return _webRTCSessionCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 602 | } |
| 603 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 604 | - (BOOL)canPlayOrRecord { |
| 605 | return !self.useManualAudio || self.isAudioEnabled; |
| 606 | } |
| 607 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 608 | - (BOOL)isInterrupted { |
| 609 | @synchronized(self) { |
| 610 | return _isInterrupted; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | - (void)setIsInterrupted:(BOOL)isInterrupted { |
| 615 | @synchronized(self) { |
| 616 | if (_isInterrupted == isInterrupted) { |
| 617 | return; |
| 618 | } |
| 619 | _isInterrupted = isInterrupted; |
| 620 | } |
| 621 | } |
| 622 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 623 | - (BOOL)beginWebRTCSession:(NSError **)outError { |
| 624 | if (outError) { |
| 625 | *outError = nil; |
| 626 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 627 | rtc::AtomicOps::Increment(&_webRTCSessionCount); |
| 628 | [self notifyDidStartPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 629 | return YES; |
| 630 | } |
| 631 | |
| 632 | - (BOOL)endWebRTCSession:(NSError **)outError { |
| 633 | if (outError) { |
| 634 | *outError = nil; |
| 635 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 636 | rtc::AtomicOps::Decrement(&_webRTCSessionCount); |
| 637 | [self notifyDidStopPlayOrRecord]; |
| 638 | return YES; |
| 639 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 640 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 641 | - (BOOL)configureWebRTCSession:(NSError **)outError { |
| 642 | if (outError) { |
| 643 | *outError = nil; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 644 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 645 | RTCLog(@"Configuring audio session for WebRTC."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 646 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 647 | // Configure the AVAudioSession and activate it. |
| 648 | // Provide an error even if there isn't one so we can log it. |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 649 | NSError *error = nil; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 650 | RTC_OBJC_TYPE(RTCAudioSessionConfiguration) *webRTCConfig = |
| 651 | [RTC_OBJC_TYPE(RTCAudioSessionConfiguration) webRTCConfiguration]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 652 | if (![self setConfiguration:webRTCConfig active:YES error:&error]) { |
| 653 | RTCLogError(@"Failed to set WebRTC audio configuration: %@", |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 654 | error.localizedDescription); |
jtteh | f84c1d6 | 2017-04-21 13:56:39 -0700 | [diff] [blame] | 655 | // Do not call setActive:NO if setActive:YES failed. |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 656 | if (outError) { |
| 657 | *outError = error; |
| 658 | } |
| 659 | return NO; |
| 660 | } |
| 661 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 662 | // Ensure that the device currently supports audio input. |
| 663 | // TODO(tkchin): Figure out if this is really necessary. |
| 664 | if (!self.inputAvailable) { |
| 665 | RTCLogError(@"No audio input path is available!"); |
| 666 | [self unconfigureWebRTCSession:nil]; |
| 667 | if (outError) { |
| 668 | *outError = [self configurationErrorWithDescription:@"No input path."]; |
| 669 | } |
| 670 | return NO; |
| 671 | } |
| 672 | |
henrika | 2d014be | 2016-06-16 14:26:55 +0200 | [diff] [blame] | 673 | // It can happen (e.g. in combination with BT devices) that the attempt to set |
| 674 | // the preferred sample rate for WebRTC (48kHz) fails. If so, make a new |
| 675 | // configuration attempt using the sample rate that worked using the active |
| 676 | // audio session. A typical case is that only 8 or 16kHz can be set, e.g. in |
| 677 | // combination with BT headsets. Using this "trick" seems to avoid a state |
| 678 | // where Core Audio asks for a different number of audio frames than what the |
| 679 | // session's I/O buffer duration corresponds to. |
| 680 | // TODO(henrika): this fix resolves bugs.webrtc.org/6004 but it has only been |
| 681 | // tested on a limited set of iOS devices and BT devices. |
| 682 | double sessionSampleRate = self.sampleRate; |
| 683 | double preferredSampleRate = webRTCConfig.sampleRate; |
| 684 | if (sessionSampleRate != preferredSampleRate) { |
| 685 | RTCLogWarning( |
| 686 | @"Current sample rate (%.2f) is not the preferred rate (%.2f)", |
| 687 | sessionSampleRate, preferredSampleRate); |
| 688 | if (![self setPreferredSampleRate:sessionSampleRate |
| 689 | error:&error]) { |
| 690 | RTCLogError(@"Failed to set preferred sample rate: %@", |
| 691 | error.localizedDescription); |
| 692 | if (outError) { |
| 693 | *outError = error; |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 698 | return YES; |
| 699 | } |
| 700 | |
| 701 | - (BOOL)unconfigureWebRTCSession:(NSError **)outError { |
| 702 | if (outError) { |
| 703 | *outError = nil; |
| 704 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 705 | RTCLog(@"Unconfiguring audio session for WebRTC."); |
| 706 | [self setActive:NO error:outError]; |
| 707 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 708 | return YES; |
| 709 | } |
| 710 | |
| 711 | - (NSError *)configurationErrorWithDescription:(NSString *)description { |
| 712 | NSDictionary* userInfo = @{ |
| 713 | NSLocalizedDescriptionKey: description, |
| 714 | }; |
| 715 | return [[NSError alloc] initWithDomain:kRTCAudioSessionErrorDomain |
| 716 | code:kRTCAudioSessionErrorConfiguration |
| 717 | userInfo:userInfo]; |
| 718 | } |
| 719 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 720 | - (void)updateAudioSessionAfterEvent { |
| 721 | BOOL shouldActivate = self.activationCount > 0; |
| 722 | AVAudioSessionSetActiveOptions options = shouldActivate ? |
| 723 | 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 724 | NSError *error = nil; |
| 725 | if ([self.session setActive:shouldActivate |
| 726 | withOptions:options |
| 727 | error:&error]) { |
| 728 | self.isActive = shouldActivate; |
| 729 | } else { |
| 730 | RTCLogError(@"Failed to set session active to %d. Error:%@", |
| 731 | shouldActivate, error.localizedDescription); |
| 732 | } |
| 733 | } |
| 734 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 735 | - (void)updateCanPlayOrRecord { |
| 736 | BOOL canPlayOrRecord = NO; |
| 737 | BOOL shouldNotify = NO; |
| 738 | @synchronized(self) { |
| 739 | canPlayOrRecord = !self.useManualAudio || self.isAudioEnabled; |
| 740 | if (_canPlayOrRecord == canPlayOrRecord) { |
| 741 | return; |
| 742 | } |
| 743 | _canPlayOrRecord = canPlayOrRecord; |
| 744 | shouldNotify = YES; |
| 745 | } |
| 746 | if (shouldNotify) { |
| 747 | [self notifyDidChangeCanPlayOrRecord:canPlayOrRecord]; |
| 748 | } |
| 749 | } |
| 750 | |
jtteh | 3c9a6c0 | 2017-04-18 09:09:35 -0700 | [diff] [blame] | 751 | - (void)audioSessionDidActivate:(AVAudioSession *)session { |
| 752 | if (_session != session) { |
| 753 | RTCLogError(@"audioSessionDidActivate called on different AVAudioSession"); |
| 754 | } |
Zeke Chin | 8280a56 | 2018-07-10 13:53:55 -0700 | [diff] [blame] | 755 | RTCLog(@"Audio session was externally activated."); |
jtteh | 3c9a6c0 | 2017-04-18 09:09:35 -0700 | [diff] [blame] | 756 | [self incrementActivationCount]; |
| 757 | self.isActive = YES; |
Zeke Chin | 8280a56 | 2018-07-10 13:53:55 -0700 | [diff] [blame] | 758 | // When a CallKit call begins, it's possible that we receive an interruption |
| 759 | // begin without a corresponding end. Since we know that we have an activated |
| 760 | // audio session at this point, just clear any saved interruption flag since |
| 761 | // the app may never be foregrounded during the duration of the call. |
| 762 | if (self.isInterrupted) { |
| 763 | RTCLog(@"Clearing interrupted state due to external activation."); |
| 764 | self.isInterrupted = NO; |
| 765 | } |
| 766 | // Treat external audio session activation as an end interruption event. |
| 767 | [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
jtteh | 3c9a6c0 | 2017-04-18 09:09:35 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | - (void)audioSessionDidDeactivate:(AVAudioSession *)session { |
| 771 | if (_session != session) { |
| 772 | RTCLogError(@"audioSessionDidDeactivate called on different AVAudioSession"); |
| 773 | } |
Zeke Chin | 8280a56 | 2018-07-10 13:53:55 -0700 | [diff] [blame] | 774 | RTCLog(@"Audio session was externally deactivated."); |
jtteh | 3c9a6c0 | 2017-04-18 09:09:35 -0700 | [diff] [blame] | 775 | self.isActive = NO; |
| 776 | [self decrementActivationCount]; |
| 777 | } |
| 778 | |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 779 | - (void)observeValueForKeyPath:(NSString *)keyPath |
| 780 | ofObject:(id)object |
| 781 | change:(NSDictionary *)change |
| 782 | context:(void *)context { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 783 | if (context == (__bridge void *)RTC_OBJC_TYPE(RTCAudioSession).class) { |
Peter Hanspers | 4721736 | 2017-10-05 11:39:15 +0200 | [diff] [blame] | 784 | if (object == _session) { |
| 785 | NSNumber *newVolume = change[NSKeyValueChangeNewKey]; |
| 786 | RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue); |
| 787 | [self notifyDidChangeOutputVolume:newVolume.floatValue]; |
| 788 | } |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 789 | } else { |
| 790 | [super observeValueForKeyPath:keyPath |
| 791 | ofObject:object |
| 792 | change:change |
| 793 | context:context]; |
| 794 | } |
| 795 | } |
| 796 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 797 | - (void)notifyDidBeginInterruption { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 798 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 799 | SEL sel = @selector(audioSessionDidBeginInterruption:); |
| 800 | if ([delegate respondsToSelector:sel]) { |
| 801 | [delegate audioSessionDidBeginInterruption:self]; |
| 802 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | - (void)notifyDidEndInterruptionWithShouldResumeSession: |
| 807 | (BOOL)shouldResumeSession { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 808 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 809 | SEL sel = @selector(audioSessionDidEndInterruption:shouldResumeSession:); |
| 810 | if ([delegate respondsToSelector:sel]) { |
| 811 | [delegate audioSessionDidEndInterruption:self |
| 812 | shouldResumeSession:shouldResumeSession]; |
| 813 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 814 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | - (void)notifyDidChangeRouteWithReason:(AVAudioSessionRouteChangeReason)reason |
| 818 | previousRoute:(AVAudioSessionRouteDescription *)previousRoute { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 819 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 820 | SEL sel = @selector(audioSessionDidChangeRoute:reason:previousRoute:); |
| 821 | if ([delegate respondsToSelector:sel]) { |
| 822 | [delegate audioSessionDidChangeRoute:self |
| 823 | reason:reason |
| 824 | previousRoute:previousRoute]; |
| 825 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | - (void)notifyMediaServicesWereLost { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 830 | for (auto delegate : self.delegates) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 831 | SEL sel = @selector(audioSessionMediaServerTerminated:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 832 | if ([delegate respondsToSelector:sel]) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 833 | [delegate audioSessionMediaServerTerminated:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 834 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | - (void)notifyMediaServicesWereReset { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 839 | for (auto delegate : self.delegates) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 840 | SEL sel = @selector(audioSessionMediaServerReset:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 841 | if ([delegate respondsToSelector:sel]) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 842 | [delegate audioSessionMediaServerReset:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 847 | - (void)notifyDidChangeCanPlayOrRecord:(BOOL)canPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 848 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 849 | SEL sel = @selector(audioSession:didChangeCanPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 850 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 851 | [delegate audioSession:self didChangeCanPlayOrRecord:canPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 856 | - (void)notifyDidStartPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 857 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 858 | SEL sel = @selector(audioSessionDidStartPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 859 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 860 | [delegate audioSessionDidStartPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 865 | - (void)notifyDidStopPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 866 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 867 | SEL sel = @selector(audioSessionDidStopPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 868 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 869 | [delegate audioSessionDidStopPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 870 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
jtteh | 13ae11a | 2017-05-25 17:52:20 -0700 | [diff] [blame] | 874 | - (void)notifyDidChangeOutputVolume:(float)volume { |
| 875 | for (auto delegate : self.delegates) { |
| 876 | SEL sel = @selector(audioSession:didChangeOutputVolume:); |
| 877 | if ([delegate respondsToSelector:sel]) { |
| 878 | [delegate audioSession:self didChangeOutputVolume:volume]; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
Anders Carlsson | 121ea32 | 2017-06-26 15:34:47 +0200 | [diff] [blame] | 883 | - (void)notifyDidDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches { |
| 884 | for (auto delegate : self.delegates) { |
| 885 | SEL sel = @selector(audioSession:didDetectPlayoutGlitch:); |
| 886 | if ([delegate respondsToSelector:sel]) { |
| 887 | [delegate audioSession:self didDetectPlayoutGlitch:totalNumberOfGlitches]; |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
JT Teh | c1f083d | 2018-04-25 09:19:35 -0700 | [diff] [blame] | 892 | - (void)notifyWillSetActive:(BOOL)active { |
| 893 | for (id delegate : self.delegates) { |
| 894 | SEL sel = @selector(audioSession:willSetActive:); |
| 895 | if ([delegate respondsToSelector:sel]) { |
| 896 | [delegate audioSession:self willSetActive:active]; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | - (void)notifyDidSetActive:(BOOL)active { |
| 902 | for (id delegate : self.delegates) { |
| 903 | SEL sel = @selector(audioSession:didSetActive:); |
| 904 | if ([delegate respondsToSelector:sel]) { |
| 905 | [delegate audioSession:self didSetActive:active]; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | - (void)notifyFailedToSetActive:(BOOL)active error:(NSError *)error { |
| 911 | for (id delegate : self.delegates) { |
| 912 | SEL sel = @selector(audioSession:failedToSetActive:error:); |
| 913 | if ([delegate respondsToSelector:sel]) { |
| 914 | [delegate audioSession:self failedToSetActive:active error:error]; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 919 | @end |