Add observer for AVAudioSession.outputVolume

BUG=webrtc:7696

Review-Url: https://codereview.webrtc.org/2895263006
Cr-Commit-Position: refs/heads/master@{#18267}
This commit is contained in:
jtteh
2017-05-25 17:52:20 -07:00
committed by Commit bot
parent a615e17ec0
commit 13ae11a418
4 changed files with 79 additions and 0 deletions

View File

@ -74,6 +74,10 @@ RTC_EXPORT
*/
- (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session;
/** Called when the AVAudioSession output volume value changes. */
- (void)audioSession:(RTCAudioSession *)audioSession
didChangeOutputVolume:(float)outputVolume;
@end
/** This is a protocol used to inform RTCAudioSession when the audio session

View File

@ -24,6 +24,7 @@
NSString * const kRTCAudioSessionErrorDomain = @"org.webrtc.RTCAudioSession";
NSInteger const kRTCAudioSessionErrorLockRequired = -1;
NSInteger const kRTCAudioSessionErrorConfiguration = -2;
NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
// This class needs to be thread-safe because it is accessed from many threads.
// TODO(tkchin): Consider more granular locking. We're not expecting a lot of
@ -86,6 +87,11 @@ NSInteger const kRTCAudioSessionErrorConfiguration = -2;
selector:@selector(handleApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[_session addObserver:self
forKeyPath:kRTCAudioSessionOutputVolumeSelector
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
RTCLog(@"RTCAudioSession (%p): init.", self);
}
return self;
@ -93,6 +99,7 @@ NSInteger const kRTCAudioSessionErrorConfiguration = -2;
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_session removeObserver:self forKeyPath:kRTCAudioSessionOutputVolumeSelector context:nil];
RTCLog(@"RTCAudioSession (%p): dealloc.", self);
}
@ -803,6 +810,22 @@ NSInteger const kRTCAudioSessionErrorConfiguration = -2;
[self decrementActivationCount];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (object == _session) {
NSNumber *newVolume = change[NSKeyValueChangeNewKey];
RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue);
[self notifyDidChangeOutputVolume:newVolume.floatValue];
} else {
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
- (void)notifyDidBeginInterruption {
for (auto delegate : self.delegates) {
SEL sel = @selector(audioSessionDidBeginInterruption:);
@ -880,4 +903,13 @@ NSInteger const kRTCAudioSessionErrorConfiguration = -2;
}
}
- (void)notifyDidChangeOutputVolume:(float)volume {
for (auto delegate : self.delegates) {
SEL sel = @selector(audioSession:didChangeOutputVolume:);
if ([delegate respondsToSelector:sel]) {
[delegate audioSession:self didChangeOutputVolume:volume];
}
}
}
@end

View File

@ -81,4 +81,8 @@
- (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session {
}
- (void)audioSession:(RTCAudioSession *)audioSession
didChangeOutputVolume:(float)outputVolume {
}
@end

View File

@ -18,10 +18,22 @@
#import "webrtc/modules/audio_device/ios/objc/RTCAudioSessionConfiguration.h"
@interface RTCAudioSessionTestDelegate : NSObject <RTCAudioSessionDelegate>
@property (nonatomic, readonly) float outputVolume;
@end
@implementation RTCAudioSessionTestDelegate
@synthesize outputVolume = _outputVolume;
- (instancetype)init {
if (self = [super init]) {
_outputVolume = -1;
}
return self;
}
- (void)audioSessionDidBeginInterruption:(RTCAudioSession *)session {
}
@ -46,6 +58,11 @@
- (void)audioSessionShouldUnconfigure:(RTCAudioSession *)session {
}
- (void)audioSession:(RTCAudioSession *)audioSession
didChangeOutputVolume:(float)outputVolume {
_outputVolume = outputVolume;
}
@end
// A delegate that adds itself to the audio session on init and removes itself
@ -246,6 +263,23 @@ OCMLocation *OCMMakeLocation(id testCase, const char *fileCString, int line){
[mockAudioSession stopMocking];
}
- (void)testAudioVolumeDidNotify {
RTCAudioSession *session = [RTCAudioSession sharedInstance];
RTCAudioSessionTestDelegate *delegate =
[[RTCAudioSessionTestDelegate alloc] init];
[session addDelegate:delegate];
[session observeValueForKeyPath:@"outputVolume"
ofObject:[AVAudioSession sharedInstance]
change:
@{NSKeyValueChangeNewKey :
@([AVAudioSession sharedInstance].outputVolume) }
context:nil];
EXPECT_NE(delegate.outputVolume, -1);
EXPECT_EQ([AVAudioSession sharedInstance].outputVolume, delegate.outputVolume);
}
@end
namespace webrtc {
@ -295,4 +329,9 @@ TEST_F(AudioSessionTest, ConfigureWebRTCSession) {
[test testConfigureWebRTCSession];
}
TEST_F(AudioSessionTest, AudioVolumeDidNotify) {
RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
[test testAudioVolumeDidNotify];
}
} // namespace webrtc