blob: daddf314a43c4f8af8c2d96299de02f01db7e8cf [file] [log] [blame]
Peter Hanspers8d95e3b2018-05-15 10:22:36 +02001/*
2 * Copyright 2018 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 "RTCNativeAudioSessionDelegateAdapter.h"
12
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#include "sdk/objc/native/src/audio/audio_session_observer.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020014
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020015#import "base/RTCLogging.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020016
17@implementation RTCNativeAudioSessionDelegateAdapter {
18 webrtc::AudioSessionObserver *_observer;
19}
20
21- (instancetype)initWithObserver:(webrtc::AudioSessionObserver *)observer {
22 RTC_DCHECK(observer);
23 if (self = [super init]) {
24 _observer = observer;
25 }
26 return self;
27}
28
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020029#pragma mark - RTC_OBJC_TYPE(RTCAudioSessionDelegate)
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020030
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020031- (void)audioSessionDidBeginInterruption:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020032 _observer->OnInterruptionBegin();
33}
34
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020035- (void)audioSessionDidEndInterruption:(RTC_OBJC_TYPE(RTCAudioSession) *)session
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020036 shouldResumeSession:(BOOL)shouldResumeSession {
37 _observer->OnInterruptionEnd();
38}
39
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020040- (void)audioSessionDidChangeRoute:(RTC_OBJC_TYPE(RTCAudioSession) *)session
41 reason:(AVAudioSessionRouteChangeReason)reason
42 previousRoute:(AVAudioSessionRouteDescription *)previousRoute {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020043 switch (reason) {
44 case AVAudioSessionRouteChangeReasonUnknown:
45 case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
46 case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
47 case AVAudioSessionRouteChangeReasonCategoryChange:
48 // It turns out that we see a category change (at least in iOS 9.2)
49 // when making a switch from a BT device to e.g. Speaker using the
50 // iOS Control Center and that we therefore must check if the sample
51 // rate has changed. And if so is the case, restart the audio unit.
52 case AVAudioSessionRouteChangeReasonOverride:
53 case AVAudioSessionRouteChangeReasonWakeFromSleep:
54 case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
55 _observer->OnValidRouteChange();
56 break;
57 case AVAudioSessionRouteChangeReasonRouteConfigurationChange:
58 // The set of input and output ports has not changed, but their
59 // configuration has, e.g., a port’s selected data source has
60 // changed. Ignore this type of route change since we are focusing
61 // on detecting headset changes.
62 RTCLog(@"Ignoring RouteConfigurationChange");
63 break;
64 }
65}
66
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020067- (void)audioSessionMediaServerTerminated:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020068}
69
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020070- (void)audioSessionMediaServerReset:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020071}
72
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020073- (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)session
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020074 didChangeCanPlayOrRecord:(BOOL)canPlayOrRecord {
75 _observer->OnCanPlayOrRecordChange(canPlayOrRecord);
76}
77
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020078- (void)audioSessionDidStartPlayOrRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020079}
80
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020081- (void)audioSessionDidStopPlayOrRecord:(RTC_OBJC_TYPE(RTCAudioSession) *)session {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020082}
83
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020084- (void)audioSession:(RTC_OBJC_TYPE(RTCAudioSession) *)audioSession
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020085 didChangeOutputVolume:(float)outputVolume {
86 _observer->OnChangedOutputVolume();
87}
88
89@end