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]; |
| 69 | // TODO(tkchin): Maybe listen to SilenceSecondaryAudioHintNotification. |
| 70 | [center addObserver:self |
| 71 | selector:@selector(handleMediaServicesWereLost:) |
| 72 | name:AVAudioSessionMediaServicesWereLostNotification |
| 73 | object:nil]; |
| 74 | [center addObserver:self |
| 75 | selector:@selector(handleMediaServicesWereReset:) |
| 76 | name:AVAudioSessionMediaServicesWereResetNotification |
| 77 | object:nil]; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 78 | // Also track foreground event in order to deal with interruption ended situation. |
| 79 | [center addObserver:self |
| 80 | selector:@selector(handleApplicationDidBecomeActive:) |
| 81 | name:UIApplicationDidBecomeActiveNotification |
| 82 | object:nil]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 83 | } |
| 84 | return self; |
| 85 | } |
| 86 | |
| 87 | - (void)dealloc { |
| 88 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 89 | } |
| 90 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 91 | - (NSString *)description { |
| 92 | NSString *format = |
| 93 | @"RTCAudioSession: {\n" |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 94 | " category: %@\n" |
| 95 | " categoryOptions: %ld\n" |
| 96 | " mode: %@\n" |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 97 | " isActive: %d\n" |
| 98 | " sampleRate: %.2f\n" |
| 99 | " IOBufferDuration: %f\n" |
| 100 | " outputNumberOfChannels: %ld\n" |
| 101 | " inputNumberOfChannels: %ld\n" |
| 102 | " outputLatency: %f\n" |
| 103 | " inputLatency: %f\n" |
henrika | c5aea65 | 2016-09-21 07:45:55 -0700 | [diff] [blame^] | 104 | " outputVolume: %f\n" |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 105 | "}"; |
| 106 | NSString *description = [NSString stringWithFormat:format, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 107 | self.category, (long)self.categoryOptions, self.mode, |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 108 | self.isActive, self.sampleRate, self.IOBufferDuration, |
| 109 | self.outputNumberOfChannels, self.inputNumberOfChannels, |
henrika | c5aea65 | 2016-09-21 07:45:55 -0700 | [diff] [blame^] | 110 | self.outputLatency, self.inputLatency, self.outputVolume]; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 111 | return description; |
| 112 | } |
| 113 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 114 | - (void)setIsActive:(BOOL)isActive { |
| 115 | @synchronized(self) { |
| 116 | _isActive = isActive; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | - (BOOL)isActive { |
| 121 | @synchronized(self) { |
| 122 | return _isActive; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | - (BOOL)isLocked { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 127 | return _lockRecursionCount > 0; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 128 | } |
| 129 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 130 | - (void)setUseManualAudio:(BOOL)useManualAudio { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 131 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 132 | if (_useManualAudio == useManualAudio) { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 133 | return; |
| 134 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 135 | _useManualAudio = useManualAudio; |
| 136 | } |
| 137 | [self updateCanPlayOrRecord]; |
| 138 | } |
| 139 | |
| 140 | - (BOOL)useManualAudio { |
| 141 | @synchronized(self) { |
| 142 | return _useManualAudio; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 146 | - (void)setIsAudioEnabled:(BOOL)isAudioEnabled { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 147 | @synchronized(self) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 148 | if (_isAudioEnabled == isAudioEnabled) { |
| 149 | return; |
| 150 | } |
| 151 | _isAudioEnabled = isAudioEnabled; |
| 152 | } |
| 153 | [self updateCanPlayOrRecord]; |
| 154 | } |
| 155 | |
| 156 | - (BOOL)isAudioEnabled { |
| 157 | @synchronized(self) { |
| 158 | return _isAudioEnabled; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 162 | // TODO(tkchin): Check for duplicates. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 163 | - (void)addDelegate:(id<RTCAudioSessionDelegate>)delegate { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 164 | if (!delegate) { |
| 165 | return; |
| 166 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 167 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 168 | _delegates.push_back(delegate); |
| 169 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | - (void)removeDelegate:(id<RTCAudioSessionDelegate>)delegate { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 174 | if (!delegate) { |
| 175 | return; |
| 176 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 177 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 178 | _delegates.erase(std::remove(_delegates.begin(), |
| 179 | _delegates.end(), |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 180 | delegate), |
| 181 | _delegates.end()); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 182 | [self removeZeroedDelegates]; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | - (void)lockForConfiguration { |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 187 | _crit.Enter(); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 188 | rtc::AtomicOps::Increment(&_lockRecursionCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | - (void)unlockForConfiguration { |
| 192 | // Don't let threads other than the one that called lockForConfiguration |
| 193 | // unlock. |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 194 | if (_crit.TryEnter()) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 195 | rtc::AtomicOps::Decrement(&_lockRecursionCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 196 | // One unlock for the tryLock, and another one to actually unlock. If this |
tkchin | 0ce3bf9 | 2016-03-12 16:52:04 -0800 | [diff] [blame] | 197 | // was called without anyone calling lock, we will hit an assertion. |
| 198 | _crit.Leave(); |
| 199 | _crit.Leave(); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | #pragma mark - AVAudioSession proxy methods |
| 204 | |
| 205 | - (NSString *)category { |
| 206 | return self.session.category; |
| 207 | } |
| 208 | |
| 209 | - (AVAudioSessionCategoryOptions)categoryOptions { |
| 210 | return self.session.categoryOptions; |
| 211 | } |
| 212 | |
| 213 | - (NSString *)mode { |
| 214 | return self.session.mode; |
| 215 | } |
| 216 | |
| 217 | - (BOOL)secondaryAudioShouldBeSilencedHint { |
| 218 | return self.session.secondaryAudioShouldBeSilencedHint; |
| 219 | } |
| 220 | |
| 221 | - (AVAudioSessionRouteDescription *)currentRoute { |
| 222 | return self.session.currentRoute; |
| 223 | } |
| 224 | |
| 225 | - (NSInteger)maximumInputNumberOfChannels { |
| 226 | return self.session.maximumInputNumberOfChannels; |
| 227 | } |
| 228 | |
| 229 | - (NSInteger)maximumOutputNumberOfChannels { |
| 230 | return self.session.maximumOutputNumberOfChannels; |
| 231 | } |
| 232 | |
| 233 | - (float)inputGain { |
| 234 | return self.session.inputGain; |
| 235 | } |
| 236 | |
| 237 | - (BOOL)inputGainSettable { |
| 238 | return self.session.inputGainSettable; |
| 239 | } |
| 240 | |
| 241 | - (BOOL)inputAvailable { |
| 242 | return self.session.inputAvailable; |
| 243 | } |
| 244 | |
| 245 | - (NSArray<AVAudioSessionDataSourceDescription *> *)inputDataSources { |
| 246 | return self.session.inputDataSources; |
| 247 | } |
| 248 | |
| 249 | - (AVAudioSessionDataSourceDescription *)inputDataSource { |
| 250 | return self.session.inputDataSource; |
| 251 | } |
| 252 | |
| 253 | - (NSArray<AVAudioSessionDataSourceDescription *> *)outputDataSources { |
| 254 | return self.session.outputDataSources; |
| 255 | } |
| 256 | |
| 257 | - (AVAudioSessionDataSourceDescription *)outputDataSource { |
| 258 | return self.session.outputDataSource; |
| 259 | } |
| 260 | |
| 261 | - (double)sampleRate { |
| 262 | return self.session.sampleRate; |
| 263 | } |
| 264 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 265 | - (double)preferredSampleRate { |
| 266 | return self.session.preferredSampleRate; |
| 267 | } |
| 268 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 269 | - (NSInteger)inputNumberOfChannels { |
| 270 | return self.session.inputNumberOfChannels; |
| 271 | } |
| 272 | |
| 273 | - (NSInteger)outputNumberOfChannels { |
| 274 | return self.session.outputNumberOfChannels; |
| 275 | } |
| 276 | |
| 277 | - (float)outputVolume { |
| 278 | return self.session.outputVolume; |
| 279 | } |
| 280 | |
| 281 | - (NSTimeInterval)inputLatency { |
| 282 | return self.session.inputLatency; |
| 283 | } |
| 284 | |
| 285 | - (NSTimeInterval)outputLatency { |
| 286 | return self.session.outputLatency; |
| 287 | } |
| 288 | |
| 289 | - (NSTimeInterval)IOBufferDuration { |
| 290 | return self.session.IOBufferDuration; |
| 291 | } |
| 292 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 293 | - (NSTimeInterval)preferredIOBufferDuration { |
| 294 | return self.session.preferredIOBufferDuration; |
| 295 | } |
| 296 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 297 | // TODO(tkchin): Simplify the amount of locking happening here. Likely that we |
| 298 | // can just do atomic increments / decrements. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 299 | - (BOOL)setActive:(BOOL)active |
| 300 | error:(NSError **)outError { |
| 301 | if (![self checkLock:outError]) { |
| 302 | return NO; |
| 303 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 304 | int activationCount = _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 305 | if (!active && activationCount == 0) { |
| 306 | RTCLogWarning(@"Attempting to deactivate without prior activation."); |
| 307 | } |
| 308 | BOOL success = YES; |
| 309 | BOOL isActive = self.isActive; |
| 310 | // Keep a local error so we can log it. |
| 311 | NSError *error = nil; |
| 312 | BOOL shouldSetActive = |
| 313 | (active && !isActive) || (!active && isActive && activationCount == 1); |
| 314 | // Attempt to activate if we're not active. |
| 315 | // Attempt to deactivate if we're active and it's the last unbalanced call. |
| 316 | if (shouldSetActive) { |
| 317 | AVAudioSession *session = self.session; |
| 318 | // AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation is used to ensure |
| 319 | // that other audio sessions that were interrupted by our session can return |
| 320 | // to their active state. It is recommended for VoIP apps to use this |
| 321 | // option. |
| 322 | AVAudioSessionSetActiveOptions options = |
| 323 | active ? 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 324 | success = [session setActive:active |
| 325 | withOptions:options |
| 326 | error:&error]; |
| 327 | if (outError) { |
| 328 | *outError = error; |
| 329 | } |
| 330 | } |
| 331 | if (success) { |
| 332 | if (shouldSetActive) { |
| 333 | self.isActive = active; |
| 334 | } |
| 335 | if (active) { |
| 336 | [self incrementActivationCount]; |
| 337 | } |
| 338 | } else { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 339 | RTCLogError(@"Failed to setActive:%d. Error: %@", |
| 340 | active, error.localizedDescription); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 341 | } |
| 342 | // Decrement activation count on deactivation whether or not it succeeded. |
| 343 | if (!active) { |
| 344 | [self decrementActivationCount]; |
| 345 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 346 | RTCLog(@"Number of current activations: %d", _activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 347 | return success; |
| 348 | } |
| 349 | |
| 350 | - (BOOL)setCategory:(NSString *)category |
| 351 | withOptions:(AVAudioSessionCategoryOptions)options |
| 352 | error:(NSError **)outError { |
| 353 | if (![self checkLock:outError]) { |
| 354 | return NO; |
| 355 | } |
| 356 | return [self.session setCategory:category withOptions:options error:outError]; |
| 357 | } |
| 358 | |
| 359 | - (BOOL)setMode:(NSString *)mode error:(NSError **)outError { |
| 360 | if (![self checkLock:outError]) { |
| 361 | return NO; |
| 362 | } |
| 363 | return [self.session setMode:mode error:outError]; |
| 364 | } |
| 365 | |
| 366 | - (BOOL)setInputGain:(float)gain error:(NSError **)outError { |
| 367 | if (![self checkLock:outError]) { |
| 368 | return NO; |
| 369 | } |
| 370 | return [self.session setInputGain:gain error:outError]; |
| 371 | } |
| 372 | |
| 373 | - (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError { |
| 374 | if (![self checkLock:outError]) { |
| 375 | return NO; |
| 376 | } |
| 377 | return [self.session setPreferredSampleRate:sampleRate error:outError]; |
| 378 | } |
| 379 | |
| 380 | - (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration |
| 381 | error:(NSError **)outError { |
| 382 | if (![self checkLock:outError]) { |
| 383 | return NO; |
| 384 | } |
| 385 | return [self.session setPreferredIOBufferDuration:duration error:outError]; |
| 386 | } |
| 387 | |
| 388 | - (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count |
| 389 | error:(NSError **)outError { |
| 390 | if (![self checkLock:outError]) { |
| 391 | return NO; |
| 392 | } |
| 393 | return [self.session setPreferredInputNumberOfChannels:count error:outError]; |
| 394 | } |
| 395 | - (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count |
| 396 | error:(NSError **)outError { |
| 397 | if (![self checkLock:outError]) { |
| 398 | return NO; |
| 399 | } |
| 400 | return [self.session setPreferredOutputNumberOfChannels:count error:outError]; |
| 401 | } |
| 402 | |
| 403 | - (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride |
| 404 | error:(NSError **)outError { |
| 405 | if (![self checkLock:outError]) { |
| 406 | return NO; |
| 407 | } |
| 408 | return [self.session overrideOutputAudioPort:portOverride error:outError]; |
| 409 | } |
| 410 | |
| 411 | - (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort |
| 412 | error:(NSError **)outError { |
| 413 | if (![self checkLock:outError]) { |
| 414 | return NO; |
| 415 | } |
| 416 | return [self.session setPreferredInput:inPort error:outError]; |
| 417 | } |
| 418 | |
| 419 | - (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 420 | error:(NSError **)outError { |
| 421 | if (![self checkLock:outError]) { |
| 422 | return NO; |
| 423 | } |
| 424 | return [self.session setInputDataSource:dataSource error:outError]; |
| 425 | } |
| 426 | |
| 427 | - (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource |
| 428 | error:(NSError **)outError { |
| 429 | if (![self checkLock:outError]) { |
| 430 | return NO; |
| 431 | } |
| 432 | return [self.session setOutputDataSource:dataSource error:outError]; |
| 433 | } |
| 434 | |
| 435 | #pragma mark - Notifications |
| 436 | |
| 437 | - (void)handleInterruptionNotification:(NSNotification *)notification { |
| 438 | NSNumber* typeNumber = |
| 439 | notification.userInfo[AVAudioSessionInterruptionTypeKey]; |
| 440 | AVAudioSessionInterruptionType type = |
| 441 | (AVAudioSessionInterruptionType)typeNumber.unsignedIntegerValue; |
| 442 | switch (type) { |
| 443 | case AVAudioSessionInterruptionTypeBegan: |
| 444 | RTCLog(@"Audio session interruption began."); |
| 445 | self.isActive = NO; |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 446 | self.isInterrupted = YES; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 447 | [self notifyDidBeginInterruption]; |
| 448 | break; |
| 449 | case AVAudioSessionInterruptionTypeEnded: { |
| 450 | RTCLog(@"Audio session interruption ended."); |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 451 | self.isInterrupted = NO; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 452 | [self updateAudioSessionAfterEvent]; |
| 453 | NSNumber *optionsNumber = |
| 454 | notification.userInfo[AVAudioSessionInterruptionOptionKey]; |
| 455 | AVAudioSessionInterruptionOptions options = |
| 456 | optionsNumber.unsignedIntegerValue; |
| 457 | BOOL shouldResume = |
| 458 | options & AVAudioSessionInterruptionOptionShouldResume; |
| 459 | [self notifyDidEndInterruptionWithShouldResumeSession:shouldResume]; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | - (void)handleRouteChangeNotification:(NSNotification *)notification { |
| 466 | // Get reason for current route change. |
| 467 | NSNumber* reasonNumber = |
| 468 | notification.userInfo[AVAudioSessionRouteChangeReasonKey]; |
| 469 | AVAudioSessionRouteChangeReason reason = |
| 470 | (AVAudioSessionRouteChangeReason)reasonNumber.unsignedIntegerValue; |
| 471 | RTCLog(@"Audio route changed:"); |
| 472 | switch (reason) { |
| 473 | case AVAudioSessionRouteChangeReasonUnknown: |
| 474 | RTCLog(@"Audio route changed: ReasonUnknown"); |
| 475 | break; |
| 476 | case AVAudioSessionRouteChangeReasonNewDeviceAvailable: |
| 477 | RTCLog(@"Audio route changed: NewDeviceAvailable"); |
| 478 | break; |
| 479 | case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: |
| 480 | RTCLog(@"Audio route changed: OldDeviceUnavailable"); |
| 481 | break; |
| 482 | case AVAudioSessionRouteChangeReasonCategoryChange: |
| 483 | RTCLog(@"Audio route changed: CategoryChange to :%@", |
| 484 | self.session.category); |
| 485 | break; |
| 486 | case AVAudioSessionRouteChangeReasonOverride: |
| 487 | RTCLog(@"Audio route changed: Override"); |
| 488 | break; |
| 489 | case AVAudioSessionRouteChangeReasonWakeFromSleep: |
| 490 | RTCLog(@"Audio route changed: WakeFromSleep"); |
| 491 | break; |
| 492 | case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory: |
| 493 | RTCLog(@"Audio route changed: NoSuitableRouteForCategory"); |
| 494 | break; |
| 495 | case AVAudioSessionRouteChangeReasonRouteConfigurationChange: |
| 496 | RTCLog(@"Audio route changed: RouteConfigurationChange"); |
| 497 | break; |
| 498 | } |
| 499 | AVAudioSessionRouteDescription* previousRoute = |
| 500 | notification.userInfo[AVAudioSessionRouteChangePreviousRouteKey]; |
| 501 | // Log previous route configuration. |
| 502 | RTCLog(@"Previous route: %@\nCurrent route:%@", |
| 503 | previousRoute, self.session.currentRoute); |
| 504 | [self notifyDidChangeRouteWithReason:reason previousRoute:previousRoute]; |
| 505 | } |
| 506 | |
| 507 | - (void)handleMediaServicesWereLost:(NSNotification *)notification { |
| 508 | RTCLog(@"Media services were lost."); |
| 509 | [self updateAudioSessionAfterEvent]; |
| 510 | [self notifyMediaServicesWereLost]; |
| 511 | } |
| 512 | |
| 513 | - (void)handleMediaServicesWereReset:(NSNotification *)notification { |
| 514 | RTCLog(@"Media services were reset."); |
| 515 | [self updateAudioSessionAfterEvent]; |
| 516 | [self notifyMediaServicesWereReset]; |
| 517 | } |
| 518 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 519 | - (void)handleApplicationDidBecomeActive:(NSNotification *)notification { |
| 520 | if (self.isInterrupted) { |
| 521 | RTCLog(@"Application became active after an interruption. Treating as interruption end."); |
| 522 | self.isInterrupted = NO; |
| 523 | [self updateAudioSessionAfterEvent]; |
| 524 | [self notifyDidEndInterruptionWithShouldResumeSession:YES]; |
| 525 | } |
| 526 | } |
| 527 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 528 | #pragma mark - Private |
| 529 | |
| 530 | + (NSError *)lockError { |
| 531 | NSDictionary *userInfo = @{ |
| 532 | NSLocalizedDescriptionKey: |
| 533 | @"Must call lockForConfiguration before calling this method." |
| 534 | }; |
| 535 | NSError *error = |
| 536 | [[NSError alloc] initWithDomain:kRTCAudioSessionErrorDomain |
| 537 | code:kRTCAudioSessionErrorLockRequired |
| 538 | userInfo:userInfo]; |
| 539 | return error; |
| 540 | } |
| 541 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 542 | - (std::vector<__weak id<RTCAudioSessionDelegate> >)delegates { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 543 | @synchronized(self) { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 544 | // Note: this returns a copy. |
| 545 | return _delegates; |
| 546 | } |
| 547 | } |
| 548 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 549 | // TODO(tkchin): check for duplicates. |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 550 | - (void)pushDelegate:(id<RTCAudioSessionDelegate>)delegate { |
| 551 | @synchronized(self) { |
| 552 | _delegates.insert(_delegates.begin(), delegate); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | - (void)removeZeroedDelegates { |
| 557 | @synchronized(self) { |
tkchin | efdd930 | 2016-04-11 12:00:59 -0700 | [diff] [blame] | 558 | _delegates.erase( |
| 559 | std::remove_if(_delegates.begin(), |
| 560 | _delegates.end(), |
| 561 | [](id delegate) -> bool { return delegate == nil; }), |
| 562 | _delegates.end()); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 566 | - (int)activationCount { |
| 567 | return _activationCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 570 | - (int)incrementActivationCount { |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 571 | RTCLog(@"Incrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 572 | return rtc::AtomicOps::Increment(&_activationCount); |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | - (NSInteger)decrementActivationCount { |
| 576 | RTCLog(@"Decrementing activation count."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 577 | return rtc::AtomicOps::Decrement(&_activationCount); |
| 578 | } |
| 579 | |
| 580 | - (int)webRTCSessionCount { |
| 581 | return _webRTCSessionCount; |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 582 | } |
| 583 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 584 | - (BOOL)canPlayOrRecord { |
| 585 | return !self.useManualAudio || self.isAudioEnabled; |
| 586 | } |
| 587 | |
tkchin | 93dd634 | 2016-07-27 10:17:14 -0700 | [diff] [blame] | 588 | - (BOOL)isInterrupted { |
| 589 | @synchronized(self) { |
| 590 | return _isInterrupted; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | - (void)setIsInterrupted:(BOOL)isInterrupted { |
| 595 | @synchronized(self) { |
| 596 | if (_isInterrupted == isInterrupted) { |
| 597 | return; |
| 598 | } |
| 599 | _isInterrupted = isInterrupted; |
| 600 | } |
| 601 | } |
| 602 | |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 603 | - (BOOL)checkLock:(NSError **)outError { |
| 604 | // Check ivar instead of trying to acquire lock so that we won't accidentally |
| 605 | // acquire lock if it hasn't already been called. |
| 606 | if (!self.isLocked) { |
| 607 | if (outError) { |
| 608 | *outError = [RTCAudioSession lockError]; |
| 609 | } |
| 610 | return NO; |
| 611 | } |
| 612 | return YES; |
| 613 | } |
| 614 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 615 | - (BOOL)beginWebRTCSession:(NSError **)outError { |
| 616 | if (outError) { |
| 617 | *outError = nil; |
| 618 | } |
| 619 | if (![self checkLock:outError]) { |
| 620 | return NO; |
| 621 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 622 | rtc::AtomicOps::Increment(&_webRTCSessionCount); |
| 623 | [self notifyDidStartPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 624 | return YES; |
| 625 | } |
| 626 | |
| 627 | - (BOOL)endWebRTCSession:(NSError **)outError { |
| 628 | if (outError) { |
| 629 | *outError = nil; |
| 630 | } |
| 631 | if (![self checkLock:outError]) { |
| 632 | return NO; |
| 633 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 634 | rtc::AtomicOps::Decrement(&_webRTCSessionCount); |
| 635 | [self notifyDidStopPlayOrRecord]; |
| 636 | return YES; |
| 637 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 638 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 639 | - (BOOL)configureWebRTCSession:(NSError **)outError { |
| 640 | if (outError) { |
| 641 | *outError = nil; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 642 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 643 | if (![self checkLock:outError]) { |
| 644 | return NO; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 645 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 646 | RTCLog(@"Configuring audio session for WebRTC."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 647 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 648 | // Configure the AVAudioSession and activate it. |
| 649 | // 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] | 650 | NSError *error = nil; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 651 | RTCAudioSessionConfiguration *webRTCConfig = |
| 652 | [RTCAudioSessionConfiguration webRTCConfiguration]; |
| 653 | if (![self setConfiguration:webRTCConfig active:YES error:&error]) { |
| 654 | RTCLogError(@"Failed to set WebRTC audio configuration: %@", |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 655 | error.localizedDescription); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 656 | [self unconfigureWebRTCSession:nil]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 657 | if (outError) { |
| 658 | *outError = error; |
| 659 | } |
| 660 | return NO; |
| 661 | } |
| 662 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 663 | // Ensure that the device currently supports audio input. |
| 664 | // TODO(tkchin): Figure out if this is really necessary. |
| 665 | if (!self.inputAvailable) { |
| 666 | RTCLogError(@"No audio input path is available!"); |
| 667 | [self unconfigureWebRTCSession:nil]; |
| 668 | if (outError) { |
| 669 | *outError = [self configurationErrorWithDescription:@"No input path."]; |
| 670 | } |
| 671 | return NO; |
| 672 | } |
| 673 | |
henrika | 2d014be | 2016-06-16 14:26:55 +0200 | [diff] [blame] | 674 | // It can happen (e.g. in combination with BT devices) that the attempt to set |
| 675 | // the preferred sample rate for WebRTC (48kHz) fails. If so, make a new |
| 676 | // configuration attempt using the sample rate that worked using the active |
| 677 | // audio session. A typical case is that only 8 or 16kHz can be set, e.g. in |
| 678 | // combination with BT headsets. Using this "trick" seems to avoid a state |
| 679 | // where Core Audio asks for a different number of audio frames than what the |
| 680 | // session's I/O buffer duration corresponds to. |
| 681 | // TODO(henrika): this fix resolves bugs.webrtc.org/6004 but it has only been |
| 682 | // tested on a limited set of iOS devices and BT devices. |
| 683 | double sessionSampleRate = self.sampleRate; |
| 684 | double preferredSampleRate = webRTCConfig.sampleRate; |
| 685 | if (sessionSampleRate != preferredSampleRate) { |
| 686 | RTCLogWarning( |
| 687 | @"Current sample rate (%.2f) is not the preferred rate (%.2f)", |
| 688 | sessionSampleRate, preferredSampleRate); |
| 689 | if (![self setPreferredSampleRate:sessionSampleRate |
| 690 | error:&error]) { |
| 691 | RTCLogError(@"Failed to set preferred sample rate: %@", |
| 692 | error.localizedDescription); |
| 693 | if (outError) { |
| 694 | *outError = error; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 699 | return YES; |
| 700 | } |
| 701 | |
| 702 | - (BOOL)unconfigureWebRTCSession:(NSError **)outError { |
| 703 | if (outError) { |
| 704 | *outError = nil; |
| 705 | } |
| 706 | if (![self checkLock:outError]) { |
| 707 | return NO; |
| 708 | } |
| 709 | RTCLog(@"Unconfiguring audio session for WebRTC."); |
| 710 | [self setActive:NO error:outError]; |
| 711 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 712 | return YES; |
| 713 | } |
| 714 | |
| 715 | - (NSError *)configurationErrorWithDescription:(NSString *)description { |
| 716 | NSDictionary* userInfo = @{ |
| 717 | NSLocalizedDescriptionKey: description, |
| 718 | }; |
| 719 | return [[NSError alloc] initWithDomain:kRTCAudioSessionErrorDomain |
| 720 | code:kRTCAudioSessionErrorConfiguration |
| 721 | userInfo:userInfo]; |
| 722 | } |
| 723 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 724 | - (void)updateAudioSessionAfterEvent { |
| 725 | BOOL shouldActivate = self.activationCount > 0; |
| 726 | AVAudioSessionSetActiveOptions options = shouldActivate ? |
| 727 | 0 : AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation; |
| 728 | NSError *error = nil; |
| 729 | if ([self.session setActive:shouldActivate |
| 730 | withOptions:options |
| 731 | error:&error]) { |
| 732 | self.isActive = shouldActivate; |
| 733 | } else { |
| 734 | RTCLogError(@"Failed to set session active to %d. Error:%@", |
| 735 | shouldActivate, error.localizedDescription); |
| 736 | } |
| 737 | } |
| 738 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 739 | - (void)updateCanPlayOrRecord { |
| 740 | BOOL canPlayOrRecord = NO; |
| 741 | BOOL shouldNotify = NO; |
| 742 | @synchronized(self) { |
| 743 | canPlayOrRecord = !self.useManualAudio || self.isAudioEnabled; |
| 744 | if (_canPlayOrRecord == canPlayOrRecord) { |
| 745 | return; |
| 746 | } |
| 747 | _canPlayOrRecord = canPlayOrRecord; |
| 748 | shouldNotify = YES; |
| 749 | } |
| 750 | if (shouldNotify) { |
| 751 | [self notifyDidChangeCanPlayOrRecord:canPlayOrRecord]; |
| 752 | } |
| 753 | } |
| 754 | |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 755 | - (void)notifyDidBeginInterruption { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 756 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 757 | SEL sel = @selector(audioSessionDidBeginInterruption:); |
| 758 | if ([delegate respondsToSelector:sel]) { |
| 759 | [delegate audioSessionDidBeginInterruption:self]; |
| 760 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | - (void)notifyDidEndInterruptionWithShouldResumeSession: |
| 765 | (BOOL)shouldResumeSession { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 766 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 767 | SEL sel = @selector(audioSessionDidEndInterruption:shouldResumeSession:); |
| 768 | if ([delegate respondsToSelector:sel]) { |
| 769 | [delegate audioSessionDidEndInterruption:self |
| 770 | shouldResumeSession:shouldResumeSession]; |
| 771 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 772 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | - (void)notifyDidChangeRouteWithReason:(AVAudioSessionRouteChangeReason)reason |
| 776 | previousRoute:(AVAudioSessionRouteDescription *)previousRoute { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 777 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 778 | SEL sel = @selector(audioSessionDidChangeRoute:reason:previousRoute:); |
| 779 | if ([delegate respondsToSelector:sel]) { |
| 780 | [delegate audioSessionDidChangeRoute:self |
| 781 | reason:reason |
| 782 | previousRoute:previousRoute]; |
| 783 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | - (void)notifyMediaServicesWereLost { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 788 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 789 | SEL sel = @selector(audioSessionMediaServicesWereLost:); |
| 790 | if ([delegate respondsToSelector:sel]) { |
| 791 | [delegate audioSessionMediaServicesWereLost:self]; |
| 792 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | - (void)notifyMediaServicesWereReset { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 797 | for (auto delegate : self.delegates) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 798 | SEL sel = @selector(audioSessionMediaServicesWereReset:); |
| 799 | if ([delegate respondsToSelector:sel]) { |
| 800 | [delegate audioSessionMediaServicesWereReset:self]; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 805 | - (void)notifyDidChangeCanPlayOrRecord:(BOOL)canPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 806 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 807 | SEL sel = @selector(audioSession:didChangeCanPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 808 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 809 | [delegate audioSession:self didChangeCanPlayOrRecord:canPlayOrRecord]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 814 | - (void)notifyDidStartPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 815 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 816 | SEL sel = @selector(audioSessionDidStartPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 817 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 818 | [delegate audioSessionDidStartPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 823 | - (void)notifyDidStopPlayOrRecord { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 824 | for (auto delegate : self.delegates) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 825 | SEL sel = @selector(audioSessionDidStopPlayOrRecord:); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 826 | if ([delegate respondsToSelector:sel]) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 827 | [delegate audioSessionDidStopPlayOrRecord:self]; |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 828 | } |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | @end |