summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/sdk/objc/components/audio/RTCAudioDevice.h
blob: f445825ff0ee01c7bb8a39e06e9a3e26b840ef0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *  Copyright 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#import <AudioUnit/AudioUnit.h>
#import <Foundation/Foundation.h>

#import "RTCMacros.h"

NS_ASSUME_NONNULL_BEGIN

typedef OSStatus (^RTC_OBJC_TYPE(RTCAudioDeviceGetPlayoutDataBlock))(
    AudioUnitRenderActionFlags *_Nonnull actionFlags,
    const AudioTimeStamp *_Nonnull timestamp,
    NSInteger inputBusNumber,
    UInt32 frameCount,
    AudioBufferList *_Nonnull outputData);

typedef OSStatus (^RTC_OBJC_TYPE(RTCAudioDeviceRenderRecordedDataBlock))(
    AudioUnitRenderActionFlags *_Nonnull actionFlags,
    const AudioTimeStamp *_Nonnull timestamp,
    NSInteger inputBusNumber,
    UInt32 frameCount,
    AudioBufferList *_Nonnull inputData,
    void *_Nullable renderContext);

typedef OSStatus (^RTC_OBJC_TYPE(RTCAudioDeviceDeliverRecordedDataBlock))(
    AudioUnitRenderActionFlags *_Nonnull actionFlags,
    const AudioTimeStamp *_Nonnull timestamp,
    NSInteger inputBusNumber,
    UInt32 frameCount,
    const AudioBufferList *_Nullable inputData,
    void *_Nullable renderContext,
    NS_NOESCAPE RTC_OBJC_TYPE(RTCAudioDeviceRenderRecordedDataBlock) _Nullable renderBlock);

/**
 * Delegate object provided by native ADM during RTCAudioDevice initialization.
 * Provides blocks to poll playback audio samples from native ADM and to feed
 * recorded audio samples into native ADM.
 */
RTC_OBJC_EXPORT @protocol RTC_OBJC_TYPE
(RTCAudioDeviceDelegate)<NSObject>
    /**
     * Implementation of RTCAudioSource should call this block to feed recorded PCM (16-bit integer)
     * into native ADM. Stereo data is expected to be interleaved starting with the left channel.
     * Either `inputData` with pre-filled audio data must be provided during block
     * call or `renderBlock` must be provided which must fill provided audio buffer with recorded
     * samples.
     *
     * NOTE: Implementation of RTCAudioDevice is expected to call the block on the same thread until
     * `notifyAudioInterrupted` is called. When `notifyAudioInterrupted` is called implementation
     * can call the block on a different thread.
     */
    @property(readonly, nonnull)
        RTC_OBJC_TYPE(RTCAudioDeviceDeliverRecordedDataBlock) deliverRecordedData;

/**
 * Provides input sample rate preference as it preferred by native ADM.
 */
@property(readonly) double preferredInputSampleRate;

/**
 * Provides input IO buffer duration preference as it preferred by native ADM.
 */
@property(readonly) NSTimeInterval preferredInputIOBufferDuration;

/**
 * Provides output sample rate preference as it preferred by native ADM.
 */
@property(readonly) double preferredOutputSampleRate;

/**
 * Provides output IO buffer duration preference as it preferred by native ADM.
 */
@property(readonly) NSTimeInterval preferredOutputIOBufferDuration;

/**
 * Implementation of RTCAudioDevice should call this block to request PCM (16-bit integer)
 * from native ADM to play. Stereo data is interleaved starting with the left channel.
 *
 * NOTE: Implementation of RTCAudioDevice is expected to invoke of this block on the
 * same thread until `notifyAudioInterrupted` is called. When `notifyAudioInterrupted` is called
 * implementation can call the block from a different thread.
 */
@property(readonly, nonnull) RTC_OBJC_TYPE(RTCAudioDeviceGetPlayoutDataBlock) getPlayoutData;

/**
 * Notifies native ADM that some of the audio input parameters of RTCAudioDevice like
 * samle rate and/or IO buffer duration and/or IO latency had possibly changed.
 * Native ADM will adjust its audio input buffer to match current parameters of audio device.
 *
 * NOTE: Must be called within block executed via `dispatchAsync` or `dispatchSync`.
 */
- (void)notifyAudioInputParametersChange;

/**
 * Notifies native ADM that some of the audio output parameters of RTCAudioDevice like
 * samle rate and/or IO buffer duration and/or IO latency had possibly changed.
 * Native ADM will adjust its audio output buffer to match current parameters of audio device.
 *
 * NOTE: Must be called within block executed via `dispatchAsync` or `dispatchSync`.
 */
- (void)notifyAudioOutputParametersChange;

/**
 * Notifies native ADM that audio input is interrupted and further audio playout
 * and recording might happen on a different thread.
 *
 * NOTE: Must be called within block executed via `dispatchAsync` or `dispatchSync`.
 */
- (void)notifyAudioInputInterrupted;

/**
 * Notifies native ADM that audio output is interrupted and further audio playout
 * and recording might happen on a different thread.
 *
 * NOTE: Must be called within block executed via `dispatchAsync` or `dispatchSync`.
 */
- (void)notifyAudioOutputInterrupted;

/**
 * Asynchronously execute block of code within the context of
 * thread which owns native ADM.
 *
 * NOTE: Intended to be used to invoke `notifyAudioInputParametersChange`,
 * `notifyAudioOutputParametersChange`, `notifyAudioInputInterrupted`,
 * `notifyAudioOutputInterrupted` on native ADM thread.
 * Also could be used by `RTCAudioDevice` implementation to tie
 * mutations of underlying audio objects (AVAudioEngine, AudioUnit, etc)
 * to the native ADM thread. Could be useful to handle events like audio route change, which
 * could lead to audio parameters change.
 */
- (void)dispatchAsync:(dispatch_block_t)block;

/**
 * Synchronously execute block of code within the context of
 * thread which owns native ADM. Allows reentrancy.
 *
 * NOTE: Intended to be used to invoke `notifyAudioInputParametersChange`,
 * `notifyAudioOutputParametersChange`, `notifyAudioInputInterrupted`,
 * `notifyAudioOutputInterrupted` on native ADM thread and make sure
 * aforementioned is completed before `dispatchSync` returns. Could be useful
 * when implementation of `RTCAudioDevice` tie mutation to underlying audio objects (AVAudioEngine,
 * AudioUnit, etc) to own thread to satisfy requirement that native ADM audio parameters
 * must be kept in sync with current audio parameters before audio is actually played or recorded.
 */
- (void)dispatchSync:(dispatch_block_t)block;

@end

/**
 * Protocol to abstract platform specific ways to implement playback and recording.
 *
 * NOTE: All the members of protocol are called by native ADM from the same thread
 * between calls to `initializeWithDelegate` and `terminate`.
 * NOTE: Implementation is fully responsible for configuring application's AVAudioSession.
 * An example implementation of RTCAudioDevice: https://github.com/mstyura/RTCAudioDevice
 * TODO(yura.yaroshevich): Implement custom RTCAudioDevice for AppRTCMobile demo app.
 */
RTC_OBJC_EXPORT @protocol RTC_OBJC_TYPE
(RTCAudioDevice)<NSObject>

    /**
     * Indicates current sample rate of audio recording. Changes to this property
     * must be notified back to native ADM via `-[RTCAudioDeviceDelegate
     * notifyAudioParametersChange]`.
     */
    @property(readonly) double deviceInputSampleRate;

/**
 * Indicates current size of record buffer. Changes to this property
 * must be notified back to native ADM via `-[RTCAudioDeviceDelegate notifyAudioParametersChange]`.
 */
@property(readonly) NSTimeInterval inputIOBufferDuration;

/**
 * Indicates current number of recorded audio channels. Changes to this property
 * must be notified back to native ADM via `-[RTCAudioDeviceDelegate notifyAudioParametersChange]`.
 */
@property(readonly) NSInteger inputNumberOfChannels;

/**
 * Indicates current input latency
 */
@property(readonly) NSTimeInterval inputLatency;

/**
 * Indicates current sample rate of audio playback. Changes to this property
 * must be notified back to native ADM via `-[RTCAudioDeviceDelegate notifyAudioParametersChange]`.
 */
@property(readonly) double deviceOutputSampleRate;

/**
 * Indicates current size of playback buffer. Changes to this property
 * must be notified back to native ADM via `-[RTCAudioDeviceDelegate notifyAudioParametersChange]`.
 */
@property(readonly) NSTimeInterval outputIOBufferDuration;

/**
 * Indicates current number of playback audio channels. Changes to this property
 * must be notified back to WebRTC via `[RTCAudioDeviceDelegate notifyAudioParametersChange]`.
 */
@property(readonly) NSInteger outputNumberOfChannels;

/**
 * Indicates current output latency
 */
@property(readonly) NSTimeInterval outputLatency;

/**
 * Indicates if invocation of `initializeWithDelegate` required before usage of RTCAudioDevice.
 * YES indicates that `initializeWithDelegate` was called earlier without subsequent call to
 * `terminate`. NO indicates that either `initializeWithDelegate` not called or `terminate` called.
 */
@property(readonly) BOOL isInitialized;

/**
 * Initializes RTCAudioDevice with RTCAudioDeviceDelegate.
 * Implementation must return YES if RTCAudioDevice initialized successfully and NO otherwise.
 */
- (BOOL)initializeWithDelegate:(id<RTC_OBJC_TYPE(RTCAudioDeviceDelegate)>)delegate;

/**
 * De-initializes RTCAudioDevice. Implementation should forget about `delegate` provided in
 * `initializeWithDelegate`.
 */
- (BOOL)terminateDevice;

/**
 * Property to indicate if `initializePlayout` call required before invocation of `startPlayout`.
 * YES indicates that `initializePlayout` was successfully invoked earlier or not necessary,
 * NO indicates that `initializePlayout` invocation required.
 */
@property(readonly) BOOL isPlayoutInitialized;

/**
 * Prepares RTCAudioDevice to play audio.
 * Called by native ADM before invocation of `startPlayout`.
 * Implementation is expected to return YES in case of successful playout initialization and NO
 * otherwise.
 */
- (BOOL)initializePlayout;

/**
 * Property to indicate if RTCAudioDevice should be playing according to
 * earlier calls of `startPlayout` and `stopPlayout`.
 */
@property(readonly) BOOL isPlaying;

/**
 * Method is called when native ADM wants to play audio.
 * Implementation is expected to return YES if playback start request
 * successfully handled and NO otherwise.
 */
- (BOOL)startPlayout;

/**
 * Method is called when native ADM no longer needs to play audio.
 * Implementation is expected to return YES if playback stop request
 * successfully handled and NO otherwise.
 */
- (BOOL)stopPlayout;

/**
 * Property to indicate if `initializeRecording` call required before usage of `startRecording`.
 * YES indicates that `initializeRecording` was successfully invoked earlier or not necessary,
 * NO indicates that `initializeRecording` invocation required.
 */
@property(readonly) BOOL isRecordingInitialized;

/**
 * Prepares RTCAudioDevice to record audio.
 * Called by native ADM before invocation of `startRecording`.
 * Implementation may use this method to prepare resources required to record audio.
 * Implementation is expected to return YES in case of successful record initialization and NO
 * otherwise.
 */
- (BOOL)initializeRecording;

/**
 * Property to indicate if RTCAudioDevice should record audio according to
 * earlier calls to `startRecording` and `stopRecording`.
 */
@property(readonly) BOOL isRecording;

/**
 * Method is called when native ADM wants to record audio.
 * Implementation is expected to return YES if recording start request
 * successfully handled and NO otherwise.
 */
- (BOOL)startRecording;

/**
 * Method is called when native ADM no longer needs to record audio.
 * Implementation is expected to return YES if recording stop request
 * successfully handled and NO otherwise.
 */
- (BOOL)stopRecording;

@end

NS_ASSUME_NONNULL_END