Adding a KVO context to avoid issues with future super/sub-classing.
Bug: webrtc:8342 Change-Id: I457858056ffc7f33bbfb261153301ea2ccd71a51 Reviewed-on: https://webrtc-review.googlesource.com/6440 Commit-Queue: Peter Hanspers <peterhanspers@webrtc.org> Reviewed-by: Anders Carlsson <andersc@webrtc.org> Reviewed-by: Daniela Jovanoska Petrenko <denicija@webrtc.org> Reviewed-by: Kári Helgason <kthelgason@webrtc.org> Cr-Commit-Position: refs/heads/master@{#20389}
This commit is contained in:
committed by
Commit Bot
parent
bd92d8dd2a
commit
47217364f5
@ -56,8 +56,13 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
return [self initWithAudioSession:[AVAudioSession sharedInstance]];
|
||||
}
|
||||
|
||||
/** This initializer provides a way for unit tests to inject a fake/mock audio session. */
|
||||
- (instancetype)initWithAudioSession:(id)audioSession {
|
||||
if (self = [super init]) {
|
||||
_session = [AVAudioSession sharedInstance];
|
||||
_session = audioSession;
|
||||
|
||||
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||
[center addObserver:self
|
||||
@ -91,7 +96,7 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
|
||||
[_session addObserver:self
|
||||
forKeyPath:kRTCAudioSessionOutputVolumeSelector
|
||||
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
|
||||
context:nil];
|
||||
context:(__bridge void*)RTCAudioSession.class];
|
||||
|
||||
RTCLog(@"RTCAudioSession (%p): init.", self);
|
||||
}
|
||||
@ -100,7 +105,9 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
[_session removeObserver:self forKeyPath:kRTCAudioSessionOutputVolumeSelector context:nil];
|
||||
[_session removeObserver:self
|
||||
forKeyPath:kRTCAudioSessionOutputVolumeSelector
|
||||
context:(__bridge void*)RTCAudioSession.class];
|
||||
RTCLog(@"RTCAudioSession (%p): dealloc.", self);
|
||||
}
|
||||
|
||||
@ -815,10 +822,12 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
|
||||
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];
|
||||
if (context == (__bridge void*)RTCAudioSession.class) {
|
||||
if (object == _session) {
|
||||
NSNumber *newVolume = change[NSKeyValueChangeNewKey];
|
||||
RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue);
|
||||
[self notifyDidChangeOutputVolume:newVolume.floatValue];
|
||||
}
|
||||
} else {
|
||||
[super observeValueForKeyPath:keyPath
|
||||
ofObject:object
|
||||
|
||||
Reference in New Issue
Block a user