blob: 751734a6449b612fec921028c89f842363c63922 [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
mbonadeibcc21762017-09-12 04:45:24 -070014#import "webrtc/sdk/objc/Framework/Headers/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
denicija59ee91b2017-06-05 05:48:47 -070087@end
88
89/** This is a protocol used to inform RTCAudioSession when the audio session
90 * activation state has changed outside of RTCAudioSession. The current known use
91 * case of this is when CallKit activates the audio session for the application
92 */
93RTC_EXPORT
94@protocol RTCAudioSessionActivationDelegate <NSObject>
95
96/** Called when the audio session is activated outside of the app by iOS. */
97- (void)audioSessionDidActivate:(AVAudioSession *)session;
98
99/** Called when the audio session is deactivated outside of the app by iOS. */
100- (void)audioSessionDidDeactivate:(AVAudioSession *)session;
101
102@end
103
104/** Proxy class for AVAudioSession that adds a locking mechanism similar to
105 * AVCaptureDevice. This is used to that interleaving configurations between
106 * WebRTC and the application layer are avoided.
107 *
108 * RTCAudioSession also coordinates activation so that the audio session is
109 * activated only once. See |setActive:error:|.
110 */
111RTC_EXPORT
112@interface RTCAudioSession : NSObject <RTCAudioSessionActivationDelegate>
113
114/** Convenience property to access the AVAudioSession singleton. Callers should
115 * not call setters on AVAudioSession directly, but other method invocations
116 * are fine.
117 */
118@property(nonatomic, readonly) AVAudioSession *session;
119
120/** Our best guess at whether the session is active based on results of calls to
121 * AVAudioSession.
122 */
123@property(nonatomic, readonly) BOOL isActive;
124/** Whether RTCAudioSession is currently locked for configuration. */
125@property(nonatomic, readonly) BOOL isLocked;
126
127/** If YES, WebRTC will not initialize the audio unit automatically when an
128 * audio track is ready for playout or recording. Instead, applications should
129 * call setIsAudioEnabled. If NO, WebRTC will initialize the audio unit
130 * as soon as an audio track is ready for playout or recording.
131 */
132@property(nonatomic, assign) BOOL useManualAudio;
133
134/** This property is only effective if useManualAudio is YES.
135 * Represents permission for WebRTC to initialize the VoIP audio unit.
136 * When set to NO, if the VoIP audio unit used by WebRTC is active, it will be
137 * stopped and uninitialized. This will stop incoming and outgoing audio.
138 * When set to YES, WebRTC will initialize and start the audio unit when it is
139 * needed (e.g. due to establishing an audio connection).
140 * This property was introduced to work around an issue where if an AVPlayer is
141 * playing audio while the VoIP audio unit is initialized, its audio would be
142 * either cut off completely or played at a reduced volume. By preventing
143 * the audio unit from being initialized until after the audio has completed,
144 * we are able to prevent the abrupt cutoff.
145 */
146@property(nonatomic, assign) BOOL isAudioEnabled;
147
148// Proxy properties.
149@property(readonly) NSString *category;
150@property(readonly) AVAudioSessionCategoryOptions categoryOptions;
151@property(readonly) NSString *mode;
152@property(readonly) BOOL secondaryAudioShouldBeSilencedHint;
153@property(readonly) AVAudioSessionRouteDescription *currentRoute;
154@property(readonly) NSInteger maximumInputNumberOfChannels;
155@property(readonly) NSInteger maximumOutputNumberOfChannels;
156@property(readonly) float inputGain;
157@property(readonly) BOOL inputGainSettable;
158@property(readonly) BOOL inputAvailable;
159@property(readonly, nullable)
160 NSArray<AVAudioSessionDataSourceDescription *> * inputDataSources;
161@property(readonly, nullable)
162 AVAudioSessionDataSourceDescription *inputDataSource;
163@property(readonly, nullable)
164 NSArray<AVAudioSessionDataSourceDescription *> * outputDataSources;
165@property(readonly, nullable)
166 AVAudioSessionDataSourceDescription *outputDataSource;
167@property(readonly) double sampleRate;
168@property(readonly) double preferredSampleRate;
169@property(readonly) NSInteger inputNumberOfChannels;
170@property(readonly) NSInteger outputNumberOfChannels;
171@property(readonly) float outputVolume;
172@property(readonly) NSTimeInterval inputLatency;
173@property(readonly) NSTimeInterval outputLatency;
174@property(readonly) NSTimeInterval IOBufferDuration;
175@property(readonly) NSTimeInterval preferredIOBufferDuration;
176
177/** Default constructor. */
178+ (instancetype)sharedInstance;
179- (instancetype)init NS_UNAVAILABLE;
180
181/** Adds a delegate, which is held weakly. */
182- (void)addDelegate:(id<RTCAudioSessionDelegate>)delegate;
183/** Removes an added delegate. */
184- (void)removeDelegate:(id<RTCAudioSessionDelegate>)delegate;
185
186/** Request exclusive access to the audio session for configuration. This call
187 * will block if the lock is held by another object.
188 */
189- (void)lockForConfiguration;
190/** Relinquishes exclusive access to the audio session. */
191- (void)unlockForConfiguration;
192
193/** If |active|, activates the audio session if it isn't already active.
194 * Successful calls must be balanced with a setActive:NO when activation is no
195 * longer required. If not |active|, deactivates the audio session if one is
196 * active and this is the last balanced call. When deactivating, the
197 * AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option is passed to
198 * AVAudioSession.
199 */
200- (BOOL)setActive:(BOOL)active
201 error:(NSError **)outError;
202
203// The following methods are proxies for the associated methods on
204// AVAudioSession. |lockForConfiguration| must be called before using them
205// otherwise they will fail with kRTCAudioSessionErrorLockRequired.
206
207- (BOOL)setCategory:(NSString *)category
208 withOptions:(AVAudioSessionCategoryOptions)options
209 error:(NSError **)outError;
210- (BOOL)setMode:(NSString *)mode error:(NSError **)outError;
211- (BOOL)setInputGain:(float)gain error:(NSError **)outError;
212- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError;
213- (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration
214 error:(NSError **)outError;
215- (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count
216 error:(NSError **)outError;
217- (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count
218 error:(NSError **)outError;
219- (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride
220 error:(NSError **)outError;
221- (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort
222 error:(NSError **)outError;
223- (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
224 error:(NSError **)outError;
225- (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
226 error:(NSError **)outError;
227@end
228
229@interface RTCAudioSession (Configuration)
230
231/** Applies the configuration to the current session. Attempts to set all
232 * properties even if previous ones fail. Only the last error will be
233 * returned.
234 * |lockForConfiguration| must be called first.
235 */
236- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
237 error:(NSError **)outError;
238
239/** Convenience method that calls both setConfiguration and setActive.
240 * |lockForConfiguration| must be called first.
241 */
242- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
243 active:(BOOL)active
244 error:(NSError **)outError;
245
246@end
247
248NS_ASSUME_NONNULL_END