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 | |
| 11 | #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" |
| 12 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 13 | #import <UIKit/UIKit.h> |
| 14 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 15 | #include "webrtc/base/atomicops.h" |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 17 | #include "webrtc/base/criticalsection.h" |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 18 | #include "webrtc/modules/audio_device/ios/audio_device_ios.h" |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 19 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 20 | #import "WebRTC/RTCLogging.h" |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 21 | #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession+Private.h" |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 22 | #import "webrtc/modules/audio_device/ios/objc/RTCAudioSessionConfiguration.h" |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 23 | |
| 24 | NSString * const kRTCAudioSessionErrorDomain = @"org.webrtc.RTCAudioSession"; |
| 25 | NSInteger const kRTCAudioSessionErrorLockRequired = -1; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 26 | NSInteger const kRTCAudioSessionErrorConfiguration = -2; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 27 | |
| 28 | // This class needs to be thread-safe because it is accessed from many threads. |
| 29 | // TODO(tkchin): Consider more granular locking. We're not expecting a lot of |
| 30 | // lock contention so coarse locks should be fine for now. |
| 31 | @implementation RTCAudioSession { |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 32 | rtc::CriticalSection _crit; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 33 | AVAudioSession *_session; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 34 | volatile int _activationCount; |
| 35 | volatile int _lockRecursionCount; |
| 36 | volatile int _webRTCSessionCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 37 | BOOL _isActive; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 38 | BOOL _useManualAudio; |
| 39 | BOOL _isAudioEnabled; |
| 40 | BOOL _canPlayOrRecord; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 41 | BOOL _isInterrupted; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @synthesize session = _session; |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 45 | @synthesize delegates = _delegates; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 46 | |
| 47 | + (instancetype)sharedInstance { |
| 48 | static dispatch_once_t onceToken; |
| 49 | static RTCAudioSession *sharedInstance = nil; |
| 50 | dispatch_once(&onceToken, ^{ |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 51 | sharedInstance = [[self alloc] init]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 52 | }); |
| 53 | return sharedInstance; |
| 54 | } |
| 55 | |
| 56 | - (instancetype)init { |
| 57 | if (self = [super init]) { |
| 58 | _session = [AVAudioSession sharedInstance]; |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 59 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 60 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 61 | [center addObserver:self |
| 62 | selector:@selector(handleInterruptionNotification:) |
| 63 | name:AVAudioSessionInterruptionNotification |
| 64 | object:nil]; |
| 65 | [center addObserver:self |
| 66 | selector:@selector(handleRouteChangeNotification:) |
| 67 | name:AVAudioSessionRouteChangeNotification |
| 68 | object:nil]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 69 | [center addObserver:self |
| 70 | selector:@selector(handleMediaServicesWereLost:) |
| 71 | name:AVAudioSessionMediaServicesWereLostNotification |
| 72 | object:nil]; |
| 73 | [center addObserver:self |
| 74 | selector:@selector(handleMediaServicesWereReset:) |
| 75 | name:AVAudioSessionMediaServicesWereResetNotification |
| 76 | object:nil]; |
henrika | f1363fd | 2016-09-27 06:06:44 -0700 | [diff] [blame] | 77 | // Posted on the main thread when the primary audio from other applications |
| 78 | // starts and stops. Foreground applications may use this notification as a |
| 79 | // hint to enable or disable audio that is secondary. |
| 80 | [center addObserver:self |
| 81 | selector:@selector(handleSilenceSecondaryAudioHintNotification:) |
| 82 | name:AVAudioSessionSilenceSecondaryAudioHintNotification |
| 83 | object:nil]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 84 | // Also track foreground event in order to deal with interruption ended situation. |
| 85 | [center addObserver:self |
| 86 | selector:@selector(handleApplicationDidBecomeActive:) |
| 87 | name:UIApplicationDidBecomeActiveNotification |
| 88 | object:nil]; |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 89 | RTCLog(@"RTCAudioSession (%p): init.", self); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 90 | } |
| 91 | return self; |
| 92 | } |
| 93 | |
| 94 | - (void)dealloc { |
| 95 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 96 | RTCLog(@"RTCAudioSession (%p): dealloc.", self); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 99 | - (NSString *)description { |
| 100 | NSString *format = |
| 101 | @"RTCAudioSession: {\n" |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 102 | " category: %@\n" |
| 103 | " categoryOptions: %ld\n" |
| 104 | " mode: %@\n" |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 105 | " isActive: %d\n" |
| 106 | " sampleRate: %.2f\n" |
| 107 | " IOBufferDuration: %f\n" |
| 108 | " outputNumberOfChannels: %ld\n" |
| 109 | " inputNumberOfChannels: %ld\n" |
| 110 | " outputLatency: %f\n" |
| 111 | " inputLatency: %f\n" |
henrika | c5aea65 | 2016-09-21 07:45:55 -0700 | [diff] [blame] | 112 | " outputVolume: %f\n" |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 113 | "}"; |
| 114 | NSString *description = [NSString stringWithFormat:format, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 115 | self.category, (long)self.categoryOptions, self.mode, |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 116 | self.isActive, self.sampleRate, self.IOBufferDuration, |
| 117 | self.outputNumberOfChannels, self.inputNumberOfChannels, |
henrika | c5aea65 | 2016-09-21 07:45:55 -0700 | [diff] [blame] | 118 | self.outputLatency, self.inputLatency, self.outputVolume]; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 119 | return description; |
| 120 | } |
| 121 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 122 | - (void)setIsActive:(BOOL)isActive { |
| 123 | @synchronized(self) { |
| 124 | _isActive = isActive; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | - (BOOL)isActive { |
| 129 | @synchronized(self) { |
| 130 | return _isActive; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | - (BOOL)isLocked { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 135 | return _lockRecursionCount > 0; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 136 | } |
| 137 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 138 | - (void)setUseManualAudio:(BOOL)useManualAudio { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 139 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 140 | if (_useManualAudio == useManualAudio) { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 143 | _useManualAudio = useManualAudio; |
| 144 | } |
| 145 | [self updateCanPlayOrRecord]; |
| 146 | } |
| 147 | |
| 148 | - (BOOL)useManualAudio { |
| 149 | @synchronized(self) { |
| 150 | return _useManualAudio; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 154 | - (void)setIsAudioEnabled:(BOOL)isAudioEnabled { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 155 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 156 | if (_isAudioEnabled == isAudioEnabled) { |
| 157 | return; |
| 158 | } |
| 159 | _isAudioEnabled = isAudioEnabled; |
| 160 | } |
| 161 | [self updateCanPlayOrRecord]; |
| 162 | } |
| 163 | |
| 164 | - (BOOL)isAudioEnabled { |
| 165 | @synchronized(self) { |
| 166 | return _isAudioEnabled; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 170 | // TODO(tkchin): Check for duplicates. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 171 | - (void)addDelegate:(id<RTCAudioSessionDelegate>)delegate { |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 172 | RTCLog(@"Adding delegate: (%p)", delegate); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 173 | if (!delegate) { |
| 174 | return; |
| 175 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 176 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 177 | _delegates.push_back(delegate); |
| 178 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | - (void)removeDelegate:(id<RTCAudioSessionDelegate>)delegate { |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 183 | RTCLog(@"Removing delegate: (%p)", delegate); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 184 | if (!delegate) { |
| 185 | return; |
| 186 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 187 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 188 | _delegates.erase(std::remove(_delegates.begin(), |
| 189 | _delegates.end(), |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 190 | delegate), |
| 191 | _delegates.end()); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 192 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | - (void)lockForConfiguration { |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 197 | _crit.Enter(); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 198 | rtc::AtomicOps::Increment(&_lockRecursionCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | - (void)unlockForConfiguration { |
| 202 | // Don't let threads other than the one that called lockForConfiguration |
| 203 | // unlock. |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 204 | if (_crit.TryEnter()) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 205 | rtc::AtomicOps::Decrement(&_lockRecursionCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 206 | // One unlock for the tryLock, and another one to actually unlock. If this |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 207 | // was called without anyone calling lock, we will hit an assertion. |
| 208 | _crit.Leave(); |
| 209 | _crit.Leave(); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | #pragma mark - AVAudioSession proxy methods |
| 214 | |
| 215 | - (NSString *)category { |
| 216 | return self.session.category; |
| 217 | } |
| 218 | |
| 219 | - (AVAudioSessionCategoryOptions)categoryOptions { |
| 220 | return self.session.categoryOptions; |
| 221 | } |
| 222 | |
| 223 | - (NSString *)mode { |
| 224 | return self.session.mode; |
| 225 | } |
| 226 | |
| 227 | - (BOOL)secondaryAudioShouldBeSilencedHint { |
| 228 | return self.session.secondaryAudioShouldBeSilencedHint; |
| 229 | } |
| 230 | |
| 231 | - (AVAudioSessionRouteDescription *)currentRoute { |
| 232 | return self.session.currentRoute; |
| 233 | } |
| 234 | |
| 235 | - (NSInteger)maximumInputNumberOfChannels { |
| 236 | return self.session.maximumInputNumberOfChannels; |
| 237 | } |
| 238 | |
| 239 | - (NSInteger)maximumOutputNumberOfChannels { |
| 240 | return self.session.maximumOutputNumberOfChannels; |
| 241 | } |
| 242 | |
| 243 | - (float)inputGain { |
| 244 | return self.session.inputGain; |
| 245 | } |
| 246 | |
| 247 | - (BOOL)inputGainSettable { |
| 248 | return self.session.inputGainSettable; |
| 249 | } |
| 250 | |
| 251 | - (BOOL)inputAvailable { |
| 252 | return self.session.inputAvailable; |
| 253 | } |
| 254 | |
| 255 | - (NSArray<AVAudioSessionDataSourceDescription *> *)inputDataSources { |
| 256 | return self.session.inputDataSources; |
| 257 | } |
| 258 | |
| 259 | - (AVAudioSessionDataSourceDescription *)inputDataSource { |
| 260 | return self.session.inputDataSource; |
| 261 | } |
| 262 | |
| 263 | - (NSArray<AVAudioSessionDataSourceDescription *> *)outputDataSources { |
| 264 | return self.session.outputDataSources; |
| 265 | } |
| 266 | |
| 267 | - (AVAudioSessionDataSourceDescription *)outputDataSource { |
| 268 | return self.session.outputDataSource; |
| 269 | } |
| 270 | |
| 271 | - (double)sampleRate { |
| 272 | return self.session.sampleRate; |
| 273 | } |
| 274 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 275 | - (double)preferredSampleRate { |
| 276 | return self.session.preferredSampleRate; |
| 277 | } |
| 278 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 279 | - (NSInteger)inputNumberOfChannels { |
| 280 | return self.session.inputNumberOfChannels; |
| 281 | } |
| 282 | |
| 283 | - (NSInteger)outputNumberOfChannels { |
| 284 | return self.session.outputNumberOfChannels; |
| 285 | } |
| 286 | |
| 287 | - (float)outputVolume { |
| 288 | return self.session.outputVolume; |
| 289 | } |
| 290 | |
| 291 | - (NSTimeInterval)inputLatency { |
| 292 | return self.session.inputLatency; |
| 293 | } |
| 294 | |
| 295 | - (NSTimeInterval)outputLatency { |
| 296 | return self.session.outputLatency; |
| 297 | } |
| 298 | |
| 299 | - (NSTimeInterval)IOBufferDuration { |
| 300 | return self.session.IOBufferDuration; |
| 301 | } |
| 302 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 303 | - (NSTimeInterval)preferredIOBufferDuration { |
| 304 | return self.session.preferredIOBufferDuration; |
| 305 | } |
| 306 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 307 | // TODO(tkchin): Simplify the amount of locking happening here. Likely that we |
| 308 | // can just do atomic increments / decrements. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 309 | - (BOOL)setActive:(BOOL)active |
| 310 | error:(NSError **)outError { |
| 311 | if (![self checkLock:outError]) { |
| 312 | return NO; |
| 313 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 314 | int activationCount = _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 315 | if (!active && activationCount == 0) { |
| 316 | RTCLogWarning(@"Attempting to deactivate without prior activation."); |
| 317 | } |
| 318 | BOOL success = YES; |
| 319 | BOOL isActive = self.isActive; |
| 320 | // Keep a local error so we can log it. |
| 321 | NSError *error = nil; |
| 322 | BOOL shouldSetActive = |
| 323 | (active && !isActive) || (!active && isActive && activationCount == 1); |
| 324 | // Attempt to activate if we're not active. |
| 325 | // Attempt to deactivate if we're active and it's the last unbalanced call. |
| 326 | if (shouldSetActive) { |
| 327 | AVAudioSession *session = self.session; |
| 328 | // AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation is used to ensure |
| 329 | // that other audio sessions that were interrupted by our session can return |
| 330 | // to their active state. It is recommended for VoIP apps to use this |
| 331 | // option. |
| 332 | AVAudioSessionSetActiveOptions options = |
| 333 | active ? 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 334 | success = [session setActive:active |
| 335 | withOptions:options |
| 336 | error:&error]; |
| 337 | if (outError) { |
| 338 | *outError = error; |
| 339 | } |
| 340 | } |
| 341 | if (success) { |
| 342 | if (shouldSetActive) { |
| 343 | self.isActive = active; |
| 344 | } |
| 345 | if (active) { |
| 346 | [self incrementActivationCount]; |
| 347 | } |
| 348 | } else { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 349 | RTCLogError(@"Failed to setActive:%d. Error: %@", |
| 350 | active, error.localizedDescription); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 351 | } |
| 352 | // Decrement activation count on deactivation whether or not it succeeded. |
| 353 | if (!active) { |
| 354 | [self decrementActivationCount]; |
| 355 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 356 | RTCLog(@"Number of current activations: %d", _activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 357 | return success; |
| 358 | } |
| 359 | |
| 360 | - (BOOL)setCategory:(NSString *)category |
| 361 | withOptions:(AVAudioSessionCategoryOptions)options |
| 362 | error:(NSError **)outError { |
| 363 | if (![self checkLock:outError]) { |
| 364 | return NO; |
| 365 | } |
| 366 | return [self.session setCategory:category withOptions:options error:outError]; |
| 367 | } |
| 368 | |
| 369 | - (BOOL)setMode:(NSString *)mode error:(NSError **)outError { |
| 370 | if (![self checkLock:outError]) { |
| 371 | return NO; |
| 372 | } |
| 373 | return [self.session setMode:mode error:outError]; |
| 374 | } |
| 375 | |
| 376 | - (BOOL)setInputGain:(float)gain error:(NSError **)outError { |
| 377 | if (![self checkLock:outError]) { |
| 378 | return NO; |
| 379 | } |
| 380 | return [self.session setInputGain:gain error:outError]; |
| 381 | } |
| 382 | |
| 383 | - (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError { |
| 384 | if (![self checkLock:outError]) { |
| 385 | return NO; |
| 386 | } |
| 387 | return [self.session setPreferredSampleRate:sampleRate error:outError]; |
| 388 | } |
| 389 | |
| 390 | - (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration |
| 391 | error:(NSError **)outError { |
| 392 | if (![self checkLock:outError]) { |
| 393 | return NO; |
| 394 | } |
| 395 | return [self.session setPreferredIOBufferDuration:duration error:outError]; |
| 396 | } |
| 397 | |
| 398 | - (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count |
| 399 | error:(NSError **)outError { |
| 400 | if (![self checkLock:outError]) { |
| 401 | return NO; |
| 402 | } |
| 403 | return [self.session setPreferredInputNumberOfChannels:count error:outError]; |
| 404 | } |
| 405 | - (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count |
| 406 | error:(NSError **)outError { |
| 407 | if (![self checkLock:outError]) { |
| 408 | return NO; |
| 409 | } |
| 410 | return [self.session setPreferredOutputNumberOfChannels:count error:outError]; |
| 411 | } |
| 412 | |
| 413 | - (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride |
| 414 | error:(NSError **)outError { |
| 415 | if (![self checkLock:outError]) { |
| 416 | return NO; |
| 417 | } |
| 418 | return [self.session overrideOutputAudioPort:portOverride error:outError]; |
| 419 | } |
| 420 | |
| 421 | - (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort |
| 422 | error:(NSError **)outError { |
| 423 | if (![self checkLock:outError]) { |
| 424 | return NO; |
| 425 | } |
| 426 | return [self.session setPreferredInput:inPort error:outError]; |
| 427 | } |
| 428 | |
| 429 | - (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 430 | error:(NSError **)outError { |
| 431 | if (![self checkLock:outError]) { |
| 432 | return NO; |
| 433 | } |
| 434 | return [self.session setInputDataSource:dataSource error:outError]; |
| 435 | } |
| 436 | |
| 437 | - (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 438 | error:(NSError **)outError { |
| 439 | if (![self checkLock:outError]) { |
| 440 | return NO; |
| 441 | } |
| 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 { |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 548 | RTCLog(@"Application became active after an interruption. Treating as interruption " |
| 549 | " end. isInterrupted changed from %d to 0.", self.isInterrupted); |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 550 | if (self.isInterrupted) { |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 551 | self.isInterrupted = NO; |
| 552 | [self updateAudioSessionAfterEvent]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 553 | } |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame^] | 554 | // Always treat application becoming active as an interruption end event. |
| 555 | [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 558 | #pragma mark - Private |
| 559 | |
| 560 | + (NSError *)lockError { |
| 561 | NSDictionary *userInfo = @{ |
| 562 | NSLocalizedDescriptionKey: |
| 563 | @"Must call lockForConfiguration before calling this method." |
| 564 | }; |
| 565 | NSError *error = |
| 566 | [[NSError alloc] initWithDomain:kRTCAudioSessionErrorDomain |
| 567 | code:kRTCAudioSessionErrorLockRequired |
| 568 | userInfo:userInfo]; |
| 569 | return error; |
| 570 | } |
| 571 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 572 | - (std::vector<__weak id<RTCAudioSessionDelegate> >)delegates { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 573 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 574 | // Note: this returns a copy. |
| 575 | return _delegates; |
| 576 | } |
| 577 | } |
| 578 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 579 | // TODO(tkchin): check for duplicates. |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 580 | - (void)pushDelegate:(id<RTCAudioSessionDelegate>)delegate { |
| 581 | @synchronized(self) { |
| 582 | _delegates.insert(_delegates.begin(), delegate); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | - (void)removeZeroedDelegates { |
| 587 | @synchronized(self) { |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 588 | _delegates.erase( |
| 589 | std::remove_if(_delegates.begin(), |
| 590 | _delegates.end(), |
| 591 | [](id delegate) -> bool { return delegate == nil; }), |
| 592 | _delegates.end()); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 596 | - (int)activationCount { |
| 597 | return _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 600 | - (int)incrementActivationCount { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 601 | RTCLog(@"Incrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 602 | return rtc::AtomicOps::Increment(&_activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | - (NSInteger)decrementActivationCount { |
| 606 | RTCLog(@"Decrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 607 | return rtc::AtomicOps::Decrement(&_activationCount); |
| 608 | } |
| 609 | |
| 610 | - (int)webRTCSessionCount { |
| 611 | return _webRTCSessionCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 612 | } |
| 613 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 614 | - (BOOL)canPlayOrRecord { |
| 615 | return !self.useManualAudio || self.isAudioEnabled; |
| 616 | } |
| 617 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 618 | - (BOOL)isInterrupted { |
| 619 | @synchronized(self) { |
| 620 | return _isInterrupted; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | - (void)setIsInterrupted:(BOOL)isInterrupted { |
| 625 | @synchronized(self) { |
| 626 | if (_isInterrupted == isInterrupted) { |
| 627 | return; |
| 628 | } |
| 629 | _isInterrupted = isInterrupted; |
| 630 | } |
| 631 | } |
| 632 | |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 633 | - (BOOL)checkLock:(NSError **)outError { |
| 634 | // Check ivar instead of trying to acquire lock so that we won't accidentally |
| 635 | // acquire lock if it hasn't already been called. |
| 636 | if (!self.isLocked) { |
| 637 | if (outError) { |
| 638 | *outError = [RTCAudioSession lockError]; |
| 639 | } |
| 640 | return NO; |
| 641 | } |
| 642 | return YES; |
| 643 | } |
| 644 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 645 | - (BOOL)beginWebRTCSession:(NSError **)outError { |
| 646 | if (outError) { |
| 647 | *outError = nil; |
| 648 | } |
| 649 | if (![self checkLock:outError]) { |
| 650 | return NO; |
| 651 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 652 | rtc::AtomicOps::Increment(&_webRTCSessionCount); |
| 653 | [self notifyDidStartPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 654 | return YES; |
| 655 | } |
| 656 | |
| 657 | - (BOOL)endWebRTCSession:(NSError **)outError { |
| 658 | if (outError) { |
| 659 | *outError = nil; |
| 660 | } |
| 661 | if (![self checkLock:outError]) { |
| 662 | return NO; |
| 663 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 664 | rtc::AtomicOps::Decrement(&_webRTCSessionCount); |
| 665 | [self notifyDidStopPlayOrRecord]; |
| 666 | return YES; |
| 667 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 668 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 669 | - (BOOL)configureWebRTCSession:(NSError **)outError { |
| 670 | if (outError) { |
| 671 | *outError = nil; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 672 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 673 | if (![self checkLock:outError]) { |
| 674 | return NO; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 675 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 676 | RTCLog(@"Configuring audio session for WebRTC."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 677 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 678 | // Configure the AVAudioSession and activate it. |
| 679 | // 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] | 680 | NSError *error = nil; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 681 | RTCAudioSessionConfiguration *webRTCConfig = |
| 682 | [RTCAudioSessionConfiguration webRTCConfiguration]; |
| 683 | if (![self setConfiguration:webRTCConfig active:YES error:&error]) { |
| 684 | RTCLogError(@"Failed to set WebRTC audio configuration: %@", |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 685 | error.localizedDescription); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 686 | [self unconfigureWebRTCSession:nil]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 687 | if (outError) { |
| 688 | *outError = error; |
| 689 | } |
| 690 | return NO; |
| 691 | } |
| 692 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 693 | // Ensure that the device currently supports audio input. |
| 694 | // TODO(tkchin): Figure out if this is really necessary. |
| 695 | if (!self.inputAvailable) { |
| 696 | RTCLogError(@"No audio input path is available!"); |
| 697 | [self unconfigureWebRTCSession:nil]; |
| 698 | if (outError) { |
| 699 | *outError = [self configurationErrorWithDescription:@"No input path."]; |
| 700 | } |
| 701 | return NO; |
| 702 | } |
| 703 | |
henrika | 2d014be | 2016-06-16 14:26:55 +0200 | [diff] [blame] | 704 | // It can happen (e.g. in combination with BT devices) that the attempt to set |
| 705 | // the preferred sample rate for WebRTC (48kHz) fails. If so, make a new |
| 706 | // configuration attempt using the sample rate that worked using the active |
| 707 | // audio session. A typical case is that only 8 or 16kHz can be set, e.g. in |
| 708 | // combination with BT headsets. Using this "trick" seems to avoid a state |
| 709 | // where Core Audio asks for a different number of audio frames than what the |
| 710 | // session's I/O buffer duration corresponds to. |
| 711 | // TODO(henrika): this fix resolves bugs.webrtc.org/6004 but it has only been |
| 712 | // tested on a limited set of iOS devices and BT devices. |
| 713 | double sessionSampleRate = self.sampleRate; |
| 714 | double preferredSampleRate = webRTCConfig.sampleRate; |
| 715 | if (sessionSampleRate != preferredSampleRate) { |
| 716 | RTCLogWarning( |
| 717 | @"Current sample rate (%.2f) is not the preferred rate (%.2f)", |
| 718 | sessionSampleRate, preferredSampleRate); |
| 719 | if (![self setPreferredSampleRate:sessionSampleRate |
| 720 | error:&error]) { |
| 721 | RTCLogError(@"Failed to set preferred sample rate: %@", |
| 722 | error.localizedDescription); |
| 723 | if (outError) { |
| 724 | *outError = error; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 729 | return YES; |
| 730 | } |
| 731 | |
| 732 | - (BOOL)unconfigureWebRTCSession:(NSError **)outError { |
| 733 | if (outError) { |
| 734 | *outError = nil; |
| 735 | } |
| 736 | if (![self checkLock:outError]) { |
| 737 | return NO; |
| 738 | } |
| 739 | RTCLog(@"Unconfiguring audio session for WebRTC."); |
| 740 | [self setActive:NO error:outError]; |
| 741 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 742 | return YES; |
| 743 | } |
| 744 | |
| 745 | - (NSError *)configurationErrorWithDescription:(NSString *)description { |
| 746 | NSDictionary* userInfo = @{ |
| 747 | NSLocalizedDescriptionKey: description, |
| 748 | }; |
| 749 | return [[NSError alloc] initWithDomain:kRTCAudioSessionErrorDomain |
| 750 | code:kRTCAudioSessionErrorConfiguration |
| 751 | userInfo:userInfo]; |
| 752 | } |
| 753 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 754 | - (void)updateAudioSessionAfterEvent { |
| 755 | BOOL shouldActivate = self.activationCount > 0; |
| 756 | AVAudioSessionSetActiveOptions options = shouldActivate ? |
| 757 | 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 758 | NSError *error = nil; |
| 759 | if ([self.session setActive:shouldActivate |
| 760 | withOptions:options |
| 761 | error:&error]) { |
| 762 | self.isActive = shouldActivate; |
| 763 | } else { |
| 764 | RTCLogError(@"Failed to set session active to %d. Error:%@", |
| 765 | shouldActivate, error.localizedDescription); |
| 766 | } |
| 767 | } |
| 768 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 769 | - (void)updateCanPlayOrRecord { |
| 770 | BOOL canPlayOrRecord = NO; |
| 771 | BOOL shouldNotify = NO; |
| 772 | @synchronized(self) { |
| 773 | canPlayOrRecord = !self.useManualAudio || self.isAudioEnabled; |
| 774 | if (_canPlayOrRecord == canPlayOrRecord) { |
| 775 | return; |
| 776 | } |
| 777 | _canPlayOrRecord = canPlayOrRecord; |
| 778 | shouldNotify = YES; |
| 779 | } |
| 780 | if (shouldNotify) { |
| 781 | [self notifyDidChangeCanPlayOrRecord:canPlayOrRecord]; |
| 782 | } |
| 783 | } |
| 784 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 785 | - (void)notifyDidBeginInterruption { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 786 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 787 | SEL sel = @selector(audioSessionDidBeginInterruption:); |
| 788 | if ([delegate respondsToSelector:sel]) { |
| 789 | [delegate audioSessionDidBeginInterruption:self]; |
| 790 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | - (void)notifyDidEndInterruptionWithShouldResumeSession: |
| 795 | (BOOL)shouldResumeSession { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 796 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 797 | SEL sel = @selector(audioSessionDidEndInterruption:shouldResumeSession:); |
| 798 | if ([delegate respondsToSelector:sel]) { |
| 799 | [delegate audioSessionDidEndInterruption:self |
| 800 | shouldResumeSession:shouldResumeSession]; |
| 801 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 802 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | - (void)notifyDidChangeRouteWithReason:(AVAudioSessionRouteChangeReason)reason |
| 806 | previousRoute:(AVAudioSessionRouteDescription *)previousRoute { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 807 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 808 | SEL sel = @selector(audioSessionDidChangeRoute:reason:previousRoute:); |
| 809 | if ([delegate respondsToSelector:sel]) { |
| 810 | [delegate audioSessionDidChangeRoute:self |
| 811 | reason:reason |
| 812 | previousRoute:previousRoute]; |
| 813 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
| 817 | - (void)notifyMediaServicesWereLost { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 818 | for (auto delegate : self.delegates) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 819 | SEL sel = @selector(audioSessionMediaServerTerminated:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 820 | if ([delegate respondsToSelector:sel]) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 821 | [delegate audioSessionMediaServerTerminated:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 822 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | - (void)notifyMediaServicesWereReset { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 827 | for (auto delegate : self.delegates) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 828 | SEL sel = @selector(audioSessionMediaServerReset:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 829 | if ([delegate respondsToSelector:sel]) { |
kthelgason | 1634e16 | 2017-02-07 02:48:55 -0800 | [diff] [blame] | 830 | [delegate audioSessionMediaServerReset:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 835 | - (void)notifyDidChangeCanPlayOrRecord:(BOOL)canPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 836 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 837 | SEL sel = @selector(audioSession:didChangeCanPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 838 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 839 | [delegate audioSession:self didChangeCanPlayOrRecord:canPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 844 | - (void)notifyDidStartPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 845 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 846 | SEL sel = @selector(audioSessionDidStartPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 847 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 848 | [delegate audioSessionDidStartPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 853 | - (void)notifyDidStopPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 854 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 855 | SEL sel = @selector(audioSessionDidStopPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 856 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 857 | [delegate audioSessionDidStopPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 858 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | @end |