blob: 6c4c96ab05c923b900fc3e259af127c6cce6339c [file] [log] [blame]
denicija59ee91b2017-06-05 05:48:47 -07001/*
2 * Copyright 2016 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 <AVFoundation/AVFoundation.h>
12#import <Foundation/Foundation.h>
13
Anders Carlsson8ecfd802017-09-15 14:07:30 +020014#import <WebRTC/RTCMacros.h>
denicija59ee91b2017-06-05 05:48:47 -070015
16NS_ASSUME_NONNULL_BEGIN
17
18extern NSString * const kRTCAudioSessionErrorDomain;
19/** Method that requires lock was called without lock. */
20extern NSInteger const kRTCAudioSessionErrorLockRequired;
21/** Unknown configuration error occurred. */
22extern NSInteger const kRTCAudioSessionErrorConfiguration;
23
24@class RTCAudioSession;
25@class RTCAudioSessionConfiguration;
26
27// Surfaces AVAudioSession events. WebRTC will listen directly for notifications
28// from AVAudioSession and handle them before calling these delegate methods,
29// at which point applications can perform additional processing if required.
30RTC_EXPORT
31@protocol RTCAudioSessionDelegate <NSObject>
32
33@optional
34/** Called on a system notification thread when AVAudioSession starts an
35 * interruption event.
36 */
37- (void)audioSessionDidBeginInterruption:(RTCAudioSession *)session;
38
39/** Called on a system notification thread when AVAudioSession ends an
40 * interruption event.
41 */
42- (void)audioSessionDidEndInterruption:(RTCAudioSession *)session
43 shouldResumeSession:(BOOL)shouldResumeSession;
44
45/** Called on a system notification thread when AVAudioSession changes the
46 * route.
47 */
48- (void)audioSessionDidChangeRoute:(RTCAudioSession *)session
49 reason:(AVAudioSessionRouteChangeReason)reason
50 previousRoute:(AVAudioSessionRouteDescription *)previousRoute;
51
52/** Called on a system notification thread when AVAudioSession media server
53 * terminates.
54 */
55- (void)audioSessionMediaServerTerminated:(RTCAudioSession *)session;
56
57/** Called on a system notification thread when AVAudioSession media server
58 * restarts.
59 */
60- (void)audioSessionMediaServerReset:(RTCAudioSession *)session;
61
62// TODO(tkchin): Maybe handle SilenceSecondaryAudioHintNotification.
63
64- (void)audioSession:(RTCAudioSession *)session
65 didChangeCanPlayOrRecord:(BOOL)canPlayOrRecord;
66
67/** Called on a WebRTC thread when the audio device is notified to begin
68 * playback or recording.
69 */
70- (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session;
71
72/** Called on a WebRTC thread when the audio device is notified to stop
73 * playback or recording.
74 */
75- (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session;
76
77/** Called when the AVAudioSession output volume value changes. */
78- (void)audioSession:(RTCAudioSession *)audioSession
79 didChangeOutputVolume:(float)outputVolume;
80
Anders Carlsson121ea322017-06-26 15:34:47 +020081/** Called when the audio device detects a playout glitch. The argument is the
82 * number of glitches detected so far in the current audio playout session.
83 */
84- (void)audioSession:(RTCAudioSession *)audioSession
85 didDetectPlayoutGlitch:(int64_t)totalNumberOfGlitches;
86
JT Tehc1f083d2018-04-25 09:19:35 -070087/** Called when the audio session is about to change the active state.
88 */
89- (void)audioSession:(RTCAudioSession *)audioSession willSetActive:(BOOL)active;
90
91/** Called after the audio session sucessfully changed the active state.
92 */
93- (void)audioSession:(RTCAudioSession *)audioSession didSetActive:(BOOL)active;
94
95/** Called after the audio session failed to change the active state.
96 */
97- (void)audioSession:(RTCAudioSession *)audioSession
98 failedToSetActive:(BOOL)active
99 error:(NSError *)error;
100
denicija59ee91b2017-06-05 05:48:47 -0700101@end
102
103/** This is a protocol used to inform RTCAudioSession when the audio session
104 * activation state has changed outside of RTCAudioSession. The current known use
105 * case of this is when CallKit activates the audio session for the application
106 */
107RTC_EXPORT
108@protocol RTCAudioSessionActivationDelegate <NSObject>
109
110/** Called when the audio session is activated outside of the app by iOS. */
111- (void)audioSessionDidActivate:(AVAudioSession *)session;
112
113/** Called when the audio session is deactivated outside of the app by iOS. */
114- (void)audioSessionDidDeactivate:(AVAudioSession *)session;
115
116@end
117
118/** Proxy class for AVAudioSession that adds a locking mechanism similar to
119 * AVCaptureDevice. This is used to that interleaving configurations between
120 * WebRTC and the application layer are avoided.
121 *
122 * RTCAudioSession also coordinates activation so that the audio session is
123 * activated only once. See |setActive:error:|.
124 */
125RTC_EXPORT
126@interface RTCAudioSession : NSObject <RTCAudioSessionActivationDelegate>
127
128/** Convenience property to access the AVAudioSession singleton. Callers should
129 * not call setters on AVAudioSession directly, but other method invocations
130 * are fine.
131 */
132@property(nonatomic, readonly) AVAudioSession *session;
133
134/** Our best guess at whether the session is active based on results of calls to
135 * AVAudioSession.
136 */
137@property(nonatomic, readonly) BOOL isActive;
138/** Whether RTCAudioSession is currently locked for configuration. */
139@property(nonatomic, readonly) BOOL isLocked;
140
141/** If YES, WebRTC will not initialize the audio unit automatically when an
142 * audio track is ready for playout or recording. Instead, applications should
143 * call setIsAudioEnabled. If NO, WebRTC will initialize the audio unit
144 * as soon as an audio track is ready for playout or recording.
145 */
146@property(nonatomic, assign) BOOL useManualAudio;
147
148/** This property is only effective if useManualAudio is YES.
149 * Represents permission for WebRTC to initialize the VoIP audio unit.
150 * When set to NO, if the VoIP audio unit used by WebRTC is active, it will be
151 * stopped and uninitialized. This will stop incoming and outgoing audio.
152 * When set to YES, WebRTC will initialize and start the audio unit when it is
153 * needed (e.g. due to establishing an audio connection).
154 * This property was introduced to work around an issue where if an AVPlayer is
155 * playing audio while the VoIP audio unit is initialized, its audio would be
156 * either cut off completely or played at a reduced volume. By preventing
157 * the audio unit from being initialized until after the audio has completed,
158 * we are able to prevent the abrupt cutoff.
159 */
160@property(nonatomic, assign) BOOL isAudioEnabled;
161
162// Proxy properties.
163@property(readonly) NSString *category;
164@property(readonly) AVAudioSessionCategoryOptions categoryOptions;
165@property(readonly) NSString *mode;
166@property(readonly) BOOL secondaryAudioShouldBeSilencedHint;
167@property(readonly) AVAudioSessionRouteDescription *currentRoute;
168@property(readonly) NSInteger maximumInputNumberOfChannels;
169@property(readonly) NSInteger maximumOutputNumberOfChannels;
170@property(readonly) float inputGain;
171@property(readonly) BOOL inputGainSettable;
172@property(readonly) BOOL inputAvailable;
173@property(readonly, nullable)
174 NSArray<AVAudioSessionDataSourceDescription *> * inputDataSources;
175@property(readonly, nullable)
176 AVAudioSessionDataSourceDescription *inputDataSource;
177@property(readonly, nullable)
178 NSArray<AVAudioSessionDataSourceDescription *> * outputDataSources;
179@property(readonly, nullable)
180 AVAudioSessionDataSourceDescription *outputDataSource;
181@property(readonly) double sampleRate;
182@property(readonly) double preferredSampleRate;
183@property(readonly) NSInteger inputNumberOfChannels;
184@property(readonly) NSInteger outputNumberOfChannels;
185@property(readonly) float outputVolume;
186@property(readonly) NSTimeInterval inputLatency;
187@property(readonly) NSTimeInterval outputLatency;
188@property(readonly) NSTimeInterval IOBufferDuration;
189@property(readonly) NSTimeInterval preferredIOBufferDuration;
190
191/** Default constructor. */
192+ (instancetype)sharedInstance;
193- (instancetype)init NS_UNAVAILABLE;
194
195/** Adds a delegate, which is held weakly. */
196- (void)addDelegate:(id<RTCAudioSessionDelegate>)delegate;
197/** Removes an added delegate. */
198- (void)removeDelegate:(id<RTCAudioSessionDelegate>)delegate;
199
200/** Request exclusive access to the audio session for configuration. This call
201 * will block if the lock is held by another object.
202 */
203- (void)lockForConfiguration;
204/** Relinquishes exclusive access to the audio session. */
205- (void)unlockForConfiguration;
206
207/** If |active|, activates the audio session if it isn't already active.
208 * Successful calls must be balanced with a setActive:NO when activation is no
209 * longer required. If not |active|, deactivates the audio session if one is
210 * active and this is the last balanced call. When deactivating, the
211 * AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option is passed to
212 * AVAudioSession.
213 */
214- (BOOL)setActive:(BOOL)active
215 error:(NSError **)outError;
216
217// The following methods are proxies for the associated methods on
218// AVAudioSession. |lockForConfiguration| must be called before using them
219// otherwise they will fail with kRTCAudioSessionErrorLockRequired.
220
221- (BOOL)setCategory:(NSString *)category
222 withOptions:(AVAudioSessionCategoryOptions)options
223 error:(NSError **)outError;
224- (BOOL)setMode:(NSString *)mode error:(NSError **)outError;
225- (BOOL)setInputGain:(float)gain error:(NSError **)outError;
226- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError;
227- (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration
228 error:(NSError **)outError;
229- (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count
230 error:(NSError **)outError;
231- (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count
232 error:(NSError **)outError;
233- (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride
234 error:(NSError **)outError;
235- (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort
236 error:(NSError **)outError;
237- (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
238 error:(NSError **)outError;
239- (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
240 error:(NSError **)outError;
241@end
242
243@interface RTCAudioSession (Configuration)
244
245/** Applies the configuration to the current session. Attempts to set all
246 * properties even if previous ones fail. Only the last error will be
247 * returned.
248 * |lockForConfiguration| must be called first.
249 */
250- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
251 error:(NSError **)outError;
252
253/** Convenience method that calls both setConfiguration and setActive.
254 * |lockForConfiguration| must be called first.
255 */
256- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
257 active:(BOOL)active
258 error:(NSError **)outError;
259
260@end
261
262NS_ASSUME_NONNULL_END