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 [self notifyAudioVolumeDidChange:newVolume.floatValue]; | |
820 } else { | |
821 [super observeValueForKeyPath:keyPath ofObject:object change:change | |
tkchin_webrtc
2017/05/25 23:01:00
nit: either all args on same line, or all on diffe
| |
822 context:context]; | |
823 } | |
824 } | |
825 | |
806 - (void)notifyDidBeginInterruption { | 826 - (void)notifyDidBeginInterruption { |
807 for (auto delegate : self.delegates) { | 827 for (auto delegate : self.delegates) { |
808 SEL sel = @selector(audioSessionDidBeginInterruption:); | 828 SEL sel = @selector(audioSessionDidBeginInterruption:); |
809 if ([delegate respondsToSelector:sel]) { | 829 if ([delegate respondsToSelector:sel]) { |
810 [delegate audioSessionDidBeginInterruption:self]; | 830 [delegate audioSessionDidBeginInterruption:self]; |
811 } | 831 } |
812 } | 832 } |
813 } | 833 } |
814 | 834 |
815 - (void)notifyDidEndInterruptionWithShouldResumeSession: | 835 - (void)notifyDidEndInterruptionWithShouldResumeSession: |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
873 | 893 |
874 - (void)notifyDidStopPlayOrRecord { | 894 - (void)notifyDidStopPlayOrRecord { |
875 for (auto delegate : self.delegates) { | 895 for (auto delegate : self.delegates) { |
876 SEL sel = @selector(audioSessionDidStopPlayOrRecord:); | 896 SEL sel = @selector(audioSessionDidStopPlayOrRecord:); |
877 if ([delegate respondsToSelector:sel]) { | 897 if ([delegate respondsToSelector:sel]) { |
878 [delegate audioSessionDidStopPlayOrRecord:self]; | 898 [delegate audioSessionDidStopPlayOrRecord:self]; |
879 } | 899 } |
880 } | 900 } |
881 } | 901 } |
882 | 902 |
903 - (void)notifyAudioVolumeDidChange:(float)volume { | |
tkchin_webrtc
2017/05/25 23:01:00
nit: notifyDidChangeOutputVolume
| |
904 RTCLog(@"AudioVolumeDidChange to %f", volume); | |
tkchin_webrtc
2017/05/25 23:01:00
Would place this at the point where you know it ch
| |
905 for (auto delegate : self.delegates) { | |
906 SEL sel = @selector(audioSession:didChangeOutputVolume:); | |
907 if ([delegate respondsToSelector:sel]) { | |
908 [delegate audioSession:self didChangeOutputVolume:volume]; | |
909 } | |
910 } | |
911 } | |
912 | |
883 @end | 913 @end |
OLD | NEW |