| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" | 11 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" |
| 12 | 12 |
| 13 #import <UIKit/UIKit.h> | 13 #import <UIKit/UIKit.h> |
| 14 | 14 |
| 15 #include "webrtc/base/atomicops.h" | 15 #include "webrtc/base/atomicops.h" |
| 16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
| 18 #include "webrtc/modules/audio_device/ios/audio_device_ios.h" | 18 #include "webrtc/modules/audio_device/ios/audio_device_ios.h" |
| 19 | 19 |
| 20 #import "WebRTC/RTCLogging.h" | 20 #import "WebRTC/RTCLogging.h" |
| 21 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession+Private.h" | 21 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession+Private.h" |
| 22 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSessionConfiguration.h" | 22 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSessionConfiguration.h" |
| 23 | 23 |
| 24 NSString * const kRTCAudioSessionErrorDomain = @"org.webrtc.RTCAudioSession"; | 24 NSString * const kRTCAudioSessionErrorDomain = @"org.webrtc.RTCAudioSession"; |
| 25 NSInteger const kRTCAudioSessionErrorLockRequired = -1; | 25 NSInteger const kRTCAudioSessionErrorLockRequired = -1; |
| 26 NSInteger const kRTCAudioSessionErrorConfiguration = -2; | 26 NSInteger const kRTCAudioSessionErrorConfiguration = -2; |
| 27 NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume"; |
| 27 | 28 |
| 28 // This class needs to be thread-safe because it is accessed from many threads. | 29 // 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 // 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 // lock contention so coarse locks should be fine for now. |
| 31 @implementation RTCAudioSession { | 32 @implementation RTCAudioSession { |
| 32 rtc::CriticalSection _crit; | 33 rtc::CriticalSection _crit; |
| 33 AVAudioSession *_session; | 34 AVAudioSession *_session; |
| 34 volatile int _activationCount; | 35 volatile int _activationCount; |
| 35 volatile int _lockRecursionCount; | 36 volatile int _lockRecursionCount; |
| 36 volatile int _webRTCSessionCount; | 37 volatile int _webRTCSessionCount; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // hint to enable or disable audio that is secondary. | 80 // hint to enable or disable audio that is secondary. |
| 80 [center addObserver:self | 81 [center addObserver:self |
| 81 selector:@selector(handleSilenceSecondaryAudioHintNotification:) | 82 selector:@selector(handleSilenceSecondaryAudioHintNotification:) |
| 82 name:AVAudioSessionSilenceSecondaryAudioHintNotification | 83 name:AVAudioSessionSilenceSecondaryAudioHintNotification |
| 83 object:nil]; | 84 object:nil]; |
| 84 // Also track foreground event in order to deal with interruption ended situ
ation. | 85 // Also track foreground event in order to deal with interruption ended situ
ation. |
| 85 [center addObserver:self | 86 [center addObserver:self |
| 86 selector:@selector(handleApplicationDidBecomeActive:) | 87 selector:@selector(handleApplicationDidBecomeActive:) |
| 87 name:UIApplicationDidBecomeActiveNotification | 88 name:UIApplicationDidBecomeActiveNotification |
| 88 object:nil]; | 89 object:nil]; |
| 90 [_session addObserver:self |
| 91 forKeyPath:kRTCAudioSessionOutputVolumeSelector |
| 92 options:NSKeyValueObservingOptionNew | NSKeyValueObservingOpti
onOld |
| 93 context:nil]; |
| 94 |
| 89 RTCLog(@"RTCAudioSession (%p): init.", self); | 95 RTCLog(@"RTCAudioSession (%p): init.", self); |
| 90 } | 96 } |
| 91 return self; | 97 return self; |
| 92 } | 98 } |
| 93 | 99 |
| 94 - (void)dealloc { | 100 - (void)dealloc { |
| 95 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 101 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 102 [_session removeObserver:self forKeyPath:kRTCAudioSessionOutputVolumeSelector
context:nil]; |
| 96 RTCLog(@"RTCAudioSession (%p): dealloc.", self); | 103 RTCLog(@"RTCAudioSession (%p): dealloc.", self); |
| 97 } | 104 } |
| 98 | 105 |
| 99 - (NSString *)description { | 106 - (NSString *)description { |
| 100 NSString *format = | 107 NSString *format = |
| 101 @"RTCAudioSession: {\n" | 108 @"RTCAudioSession: {\n" |
| 102 " category: %@\n" | 109 " category: %@\n" |
| 103 " categoryOptions: %ld\n" | 110 " categoryOptions: %ld\n" |
| 104 " mode: %@\n" | 111 " mode: %@\n" |
| 105 " isActive: %d\n" | 112 " isActive: %d\n" |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 } | 803 } |
| 797 | 804 |
| 798 - (void)audioSessionDidDeactivate:(AVAudioSession *)session { | 805 - (void)audioSessionDidDeactivate:(AVAudioSession *)session { |
| 799 if (_session != session) { | 806 if (_session != session) { |
| 800 RTCLogError(@"audioSessionDidDeactivate called on different AVAudioSession")
; | 807 RTCLogError(@"audioSessionDidDeactivate called on different AVAudioSession")
; |
| 801 } | 808 } |
| 802 self.isActive = NO; | 809 self.isActive = NO; |
| 803 [self decrementActivationCount]; | 810 [self decrementActivationCount]; |
| 804 } | 811 } |
| 805 | 812 |
| 813 - (void)observeValueForKeyPath:(NSString *)keyPath |
| 814 ofObject:(id)object |
| 815 change:(NSDictionary *)change |
| 816 context:(void *)context { |
| 817 if (object == _session) { |
| 818 NSNumber *newVolume = change[NSKeyValueChangeNewKey]; |
| 819 RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue); |
| 820 [self notifyDidChangeOutputVolume:newVolume.floatValue]; |
| 821 } else { |
| 822 [super observeValueForKeyPath:keyPath |
| 823 ofObject:object |
| 824 change:change |
| 825 context:context]; |
| 826 } |
| 827 } |
| 828 |
| 806 - (void)notifyDidBeginInterruption { | 829 - (void)notifyDidBeginInterruption { |
| 807 for (auto delegate : self.delegates) { | 830 for (auto delegate : self.delegates) { |
| 808 SEL sel = @selector(audioSessionDidBeginInterruption:); | 831 SEL sel = @selector(audioSessionDidBeginInterruption:); |
| 809 if ([delegate respondsToSelector:sel]) { | 832 if ([delegate respondsToSelector:sel]) { |
| 810 [delegate audioSessionDidBeginInterruption:self]; | 833 [delegate audioSessionDidBeginInterruption:self]; |
| 811 } | 834 } |
| 812 } | 835 } |
| 813 } | 836 } |
| 814 | 837 |
| 815 - (void)notifyDidEndInterruptionWithShouldResumeSession: | 838 - (void)notifyDidEndInterruptionWithShouldResumeSession: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 | 896 |
| 874 - (void)notifyDidStopPlayOrRecord { | 897 - (void)notifyDidStopPlayOrRecord { |
| 875 for (auto delegate : self.delegates) { | 898 for (auto delegate : self.delegates) { |
| 876 SEL sel = @selector(audioSessionDidStopPlayOrRecord:); | 899 SEL sel = @selector(audioSessionDidStopPlayOrRecord:); |
| 877 if ([delegate respondsToSelector:sel]) { | 900 if ([delegate respondsToSelector:sel]) { |
| 878 [delegate audioSessionDidStopPlayOrRecord:self]; | 901 [delegate audioSessionDidStopPlayOrRecord:self]; |
| 879 } | 902 } |
| 880 } | 903 } |
| 881 } | 904 } |
| 882 | 905 |
| 906 - (void)notifyDidChangeOutputVolume:(float)volume { |
| 907 for (auto delegate : self.delegates) { |
| 908 SEL sel = @selector(audioSession:didChangeOutputVolume:); |
| 909 if ([delegate respondsToSelector:sel]) { |
| 910 [delegate audioSession:self didChangeOutputVolume:volume]; |
| 911 } |
| 912 } |
| 913 } |
| 914 |
| 883 @end | 915 @end |
| OLD | NEW |