blob: c0ea2163ae2a908d16d2ba49f56fc84606a9bb69 [file] [log] [blame]
Zeke Chinb3fb71c2016-02-18 15:44:07 -08001/*
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
tkchin5fa51e22016-10-05 13:16:03 -070014#import "WebRTC/RTCMacros.h"
15
Zeke Chinb3fb71c2016-02-18 15:44:07 -080016NS_ASSUME_NONNULL_BEGIN
17
18extern NSString * const kRTCAudioSessionErrorDomain;
tkchin9f987d32016-03-12 20:06:28 -080019/** Method that requires lock was called without lock. */
Zeke Chinb3fb71c2016-02-18 15:44:07 -080020extern NSInteger const kRTCAudioSessionErrorLockRequired;
tkchin9f987d32016-03-12 20:06:28 -080021/** Unknown configuration error occurred. */
22extern NSInteger const kRTCAudioSessionErrorConfiguration;
Zeke Chinb3fb71c2016-02-18 15:44:07 -080023
24@class RTCAudioSession;
tkchin9f987d32016-03-12 20:06:28 -080025@class RTCAudioSessionConfiguration;
Zeke Chinb3fb71c2016-02-18 15:44:07 -080026
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.
tkchin5fa51e22016-10-05 13:16:03 -070030RTC_EXPORT
Zeke Chinb3fb71c2016-02-18 15:44:07 -080031@protocol RTCAudioSessionDelegate <NSObject>
32
Tze Kwang Chin307a0922016-03-21 13:57:40 -070033@optional
34/** Called on a system notification thread when AVAudioSession starts an
35 * interruption event.
36 */
Zeke Chinb3fb71c2016-02-18 15:44:07 -080037- (void)audioSessionDidBeginInterruption:(RTCAudioSession *)session;
38
Tze Kwang Chin307a0922016-03-21 13:57:40 -070039/** Called on a system notification thread when AVAudioSession ends an
40 * interruption event.
41 */
Zeke Chinb3fb71c2016-02-18 15:44:07 -080042- (void)audioSessionDidEndInterruption:(RTCAudioSession *)session
43 shouldResumeSession:(BOOL)shouldResumeSession;
44
Tze Kwang Chin307a0922016-03-21 13:57:40 -070045/** Called on a system notification thread when AVAudioSession changes the
46 * route.
47 */
Zeke Chinb3fb71c2016-02-18 15:44:07 -080048- (void)audioSessionDidChangeRoute:(RTCAudioSession *)session
49 reason:(AVAudioSessionRouteChangeReason)reason
50 previousRoute:(AVAudioSessionRouteDescription *)previousRoute;
51
Tze Kwang Chin307a0922016-03-21 13:57:40 -070052/** Called on a system notification thread when AVAudioSession media server
53 * terminates.
54 */
kthelgason1634e162017-02-07 02:48:55 -080055- (void)audioSessionMediaServerTerminated:(RTCAudioSession *)session;
Zeke Chinb3fb71c2016-02-18 15:44:07 -080056
Tze Kwang Chin307a0922016-03-21 13:57:40 -070057/** Called on a system notification thread when AVAudioSession media server
58 * restarts.
59 */
kthelgason1634e162017-02-07 02:48:55 -080060- (void)audioSessionMediaServerReset:(RTCAudioSession *)session;
Zeke Chinb3fb71c2016-02-18 15:44:07 -080061
Tze Kwang Chin307a0922016-03-21 13:57:40 -070062// TODO(tkchin): Maybe handle SilenceSecondaryAudioHintNotification.
63
tkchind2511962016-05-06 18:54:15 -070064- (void)audioSession:(RTCAudioSession *)session
65 didChangeCanPlayOrRecord:(BOOL)canPlayOrRecord;
tkchin9f987d32016-03-12 20:06:28 -080066
tkchind2511962016-05-06 18:54:15 -070067/** Called on a WebRTC thread when the audio device is notified to begin
68 * playback or recording.
tkchin9f987d32016-03-12 20:06:28 -080069 */
tkchind2511962016-05-06 18:54:15 -070070- (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session;
tkchin9f987d32016-03-12 20:06:28 -080071
tkchind2511962016-05-06 18:54:15 -070072/** Called on a WebRTC thread when the audio device is notified to stop
73 * playback or recording.
Tze Kwang Chin307a0922016-03-21 13:57:40 -070074 */
tkchind2511962016-05-06 18:54:15 -070075- (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session;
Zeke Chinb3fb71c2016-02-18 15:44:07 -080076
jtteh13ae11a2017-05-25 17:52:20 -070077/** Called when the AVAudioSession output volume value changes. */
78- (void)audioSession:(RTCAudioSession *)audioSession
79 didChangeOutputVolume:(float)outputVolume;
80
Zeke Chinb3fb71c2016-02-18 15:44:07 -080081@end
82
jtteh3c9a6c02017-04-18 09:09:35 -070083/** This is a protocol used to inform RTCAudioSession when the audio session
84 * activation state has changed outside of RTCAudioSession. The current known use
85 * case of this is when CallKit activates the audio session for the application
86 */
87RTC_EXPORT
88@protocol RTCAudioSessionActivationDelegate <NSObject>
89
90/** Called when the audio session is activated outside of the app by iOS. */
91- (void)audioSessionDidActivate:(AVAudioSession *)session;
92
93/** Called when the audio session is deactivated outside of the app by iOS. */
94- (void)audioSessionDidDeactivate:(AVAudioSession *)session;
95
96@end
97
Zeke Chinb3fb71c2016-02-18 15:44:07 -080098/** Proxy class for AVAudioSession that adds a locking mechanism similar to
99 * AVCaptureDevice. This is used to that interleaving configurations between
Tze Kwang Chin307a0922016-03-21 13:57:40 -0700100 * WebRTC and the application layer are avoided.
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800101 *
102 * RTCAudioSession also coordinates activation so that the audio session is
103 * activated only once. See |setActive:error:|.
104 */
tkchin5fa51e22016-10-05 13:16:03 -0700105RTC_EXPORT
jtteh3c9a6c02017-04-18 09:09:35 -0700106@interface RTCAudioSession : NSObject <RTCAudioSessionActivationDelegate>
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800107
108/** Convenience property to access the AVAudioSession singleton. Callers should
109 * not call setters on AVAudioSession directly, but other method invocations
110 * are fine.
111 */
112@property(nonatomic, readonly) AVAudioSession *session;
113
114/** Our best guess at whether the session is active based on results of calls to
115 * AVAudioSession.
116 */
117@property(nonatomic, readonly) BOOL isActive;
118/** Whether RTCAudioSession is currently locked for configuration. */
119@property(nonatomic, readonly) BOOL isLocked;
120
tkchin9f987d32016-03-12 20:06:28 -0800121/** If YES, WebRTC will not initialize the audio unit automatically when an
122 * audio track is ready for playout or recording. Instead, applications should
tkchind2511962016-05-06 18:54:15 -0700123 * call setIsAudioEnabled. If NO, WebRTC will initialize the audio unit
124 * as soon as an audio track is ready for playout or recording.
tkchin9f987d32016-03-12 20:06:28 -0800125 */
tkchind2511962016-05-06 18:54:15 -0700126@property(nonatomic, assign) BOOL useManualAudio;
127
128/** This property is only effective if useManualAudio is YES.
129 * Represents permission for WebRTC to initialize the VoIP audio unit.
130 * When set to NO, if the VoIP audio unit used by WebRTC is active, it will be
131 * stopped and uninitialized. This will stop incoming and outgoing audio.
132 * When set to YES, WebRTC will initialize and start the audio unit when it is
133 * needed (e.g. due to establishing an audio connection).
134 * This property was introduced to work around an issue where if an AVPlayer is
135 * playing audio while the VoIP audio unit is initialized, its audio would be
136 * either cut off completely or played at a reduced volume. By preventing
137 * the audio unit from being initialized until after the audio has completed,
138 * we are able to prevent the abrupt cutoff.
139 */
140@property(nonatomic, assign) BOOL isAudioEnabled;
tkchin9f987d32016-03-12 20:06:28 -0800141
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800142// Proxy properties.
143@property(readonly) NSString *category;
144@property(readonly) AVAudioSessionCategoryOptions categoryOptions;
145@property(readonly) NSString *mode;
146@property(readonly) BOOL secondaryAudioShouldBeSilencedHint;
147@property(readonly) AVAudioSessionRouteDescription *currentRoute;
148@property(readonly) NSInteger maximumInputNumberOfChannels;
149@property(readonly) NSInteger maximumOutputNumberOfChannels;
150@property(readonly) float inputGain;
151@property(readonly) BOOL inputGainSettable;
152@property(readonly) BOOL inputAvailable;
153@property(readonly, nullable)
154 NSArray<AVAudioSessionDataSourceDescription *> * inputDataSources;
155@property(readonly, nullable)
156 AVAudioSessionDataSourceDescription *inputDataSource;
157@property(readonly, nullable)
158 NSArray<AVAudioSessionDataSourceDescription *> * outputDataSources;
159@property(readonly, nullable)
160 AVAudioSessionDataSourceDescription *outputDataSource;
161@property(readonly) double sampleRate;
tkchind2511962016-05-06 18:54:15 -0700162@property(readonly) double preferredSampleRate;
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800163@property(readonly) NSInteger inputNumberOfChannels;
164@property(readonly) NSInteger outputNumberOfChannels;
165@property(readonly) float outputVolume;
166@property(readonly) NSTimeInterval inputLatency;
167@property(readonly) NSTimeInterval outputLatency;
168@property(readonly) NSTimeInterval IOBufferDuration;
tkchind2511962016-05-06 18:54:15 -0700169@property(readonly) NSTimeInterval preferredIOBufferDuration;
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800170
Tze Kwang Chin307a0922016-03-21 13:57:40 -0700171/** Default constructor. */
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800172+ (instancetype)sharedInstance;
Tze Kwang Chin307a0922016-03-21 13:57:40 -0700173- (instancetype)init NS_UNAVAILABLE;
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800174
tkchine54467f2016-03-15 16:54:03 -0700175/** Adds a delegate, which is held weakly. */
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800176- (void)addDelegate:(id<RTCAudioSessionDelegate>)delegate;
177/** Removes an added delegate. */
178- (void)removeDelegate:(id<RTCAudioSessionDelegate>)delegate;
179
180/** Request exclusive access to the audio session for configuration. This call
181 * will block if the lock is held by another object.
182 */
183- (void)lockForConfiguration;
184/** Relinquishes exclusive access to the audio session. */
185- (void)unlockForConfiguration;
186
187/** If |active|, activates the audio session if it isn't already active.
188 * Successful calls must be balanced with a setActive:NO when activation is no
189 * longer required. If not |active|, deactivates the audio session if one is
190 * active and this is the last balanced call. When deactivating, the
191 * AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option is passed to
192 * AVAudioSession.
193 */
194- (BOOL)setActive:(BOOL)active
195 error:(NSError **)outError;
196
197// The following methods are proxies for the associated methods on
198// AVAudioSession. |lockForConfiguration| must be called before using them
199// otherwise they will fail with kRTCAudioSessionErrorLockRequired.
200
201- (BOOL)setCategory:(NSString *)category
202 withOptions:(AVAudioSessionCategoryOptions)options
203 error:(NSError **)outError;
204- (BOOL)setMode:(NSString *)mode error:(NSError **)outError;
205- (BOOL)setInputGain:(float)gain error:(NSError **)outError;
206- (BOOL)setPreferredSampleRate:(double)sampleRate error:(NSError **)outError;
207- (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration
208 error:(NSError **)outError;
209- (BOOL)setPreferredInputNumberOfChannels:(NSInteger)count
210 error:(NSError **)outError;
211- (BOOL)setPreferredOutputNumberOfChannels:(NSInteger)count
212 error:(NSError **)outError;
213- (BOOL)overrideOutputAudioPort:(AVAudioSessionPortOverride)portOverride
214 error:(NSError **)outError;
215- (BOOL)setPreferredInput:(AVAudioSessionPortDescription *)inPort
216 error:(NSError **)outError;
217- (BOOL)setInputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
218 error:(NSError **)outError;
219- (BOOL)setOutputDataSource:(AVAudioSessionDataSourceDescription *)dataSource
220 error:(NSError **)outError;
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800221@end
222
tkchin9f987d32016-03-12 20:06:28 -0800223@interface RTCAudioSession (Configuration)
224
225/** Applies the configuration to the current session. Attempts to set all
226 * properties even if previous ones fail. Only the last error will be
tkchind2511962016-05-06 18:54:15 -0700227 * returned.
228 * |lockForConfiguration| must be called first.
229 */
230- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
231 error:(NSError **)outError;
232
233/** Convenience method that calls both setConfiguration and setActive.
tkchin9f987d32016-03-12 20:06:28 -0800234 * |lockForConfiguration| must be called first.
235 */
236- (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration
237 active:(BOOL)active
238 error:(NSError **)outError;
239
tkchin9f987d32016-03-12 20:06:28 -0800240@end
241
Zeke Chinb3fb71c2016-02-18 15:44:07 -0800242NS_ASSUME_NONNULL_END