-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaoo_sink.hpp
More file actions
451 lines (391 loc) · 15.3 KB
/
aoo_sink.hpp
File metadata and controls
451 lines (391 loc) · 15.3 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* Copyright (c) 2021 Christof Ressi
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/** \file
* \brief C++ interface for AOO sink
*/
#pragma once
#include "aoo_config.h"
#include "aoo_controls.h"
#include "aoo_defines.h"
#include "aoo_events.h"
#include "aoo_types.h"
#if AOO_HAVE_CXX11
# include <memory>
#endif
/** \cond DO_NOT_DOCUMENT */
typedef struct AooSink AooSink;
/** \endcond */
/** \brief create a new AOO sink instance
*
* \param id the ID
* \return new AooSink instance on success; `NULL` on failure
*/
AOO_API AooSink * AOO_CALL AooSink_new(AooId id);
/** \brief destroy the AOO sink instance */
AOO_API void AOO_CALL AooSink_free(AooSink *sink);
/*---------------------------------------------------------*/
/** \brief AOO sink interface */
struct AooSink {
public:
#if AOO_HAVE_CXX11
/** \brief custom deleter for AooSink */
class Deleter {
public:
void operator()(AooSink *obj){
AooSink_free(obj);
}
};
/** \brief smart pointer for AOO sink instance */
using Ptr = std::unique_ptr<AooSink, Deleter>;
/** \brief create a new managed AOO sink instance
*
* \return valid Ptr on success; empty Ptr on failure
*/
static Ptr create(AooId id) {
return Ptr(AooSink_new(id));
}
#endif
/*-------------------- methods -----------------------------*/
/** \brief setup AOO sink
*
* \attention Not threadsafe - needs to be synchronized with other method calls!
*
* \param numChannels the max. number of channels
* \param sampleRate the sample rate
* \param maxBlockSize the max. number of samples per block
* \param flags optional flags (currently always 0)
*/
virtual AooError AOO_CALL setup(
AooInt32 numChannels, AooSampleRate sampleRate,
AooInt32 maxBlockSize, AooSetupFlags flags) = 0;
/** \brief handle source messages
*
* \note Threadsafe; call on the network thread
*
* \param data the message data
* \param size the message size in bytes
* \param address the remote socket address
* \param addrlen the socket address length
*/
virtual AooError AOO_CALL handleMessage(
const AooByte *data, AooInt32 size,
const void *address, AooAddrSize addrlen) = 0;
/** \brief send outgoing messages
*
* \note Threadsafe; call on the network thread
*
* \param fn the send function
* \param user the user data (passed to the send function)
*/
virtual AooError AOO_CALL send(AooSendFunc fn, void *user) = 0;
/** \brief process audio
*
* \note Threadsafe and RT-safe; call on the audio thread
*
* The audio output for the next N samples is copied into the provided buffers.
* After that, any incoming stream messages that fall into this sample interval
* are dispatched to the message handler function (if provided).
*
* \param data an array of audio channels; the number of
* channels must match the number in #AooSource_setup.
* \param numSamples the number of samples per channel
* \param t current NTP time; \see aoo_getCurrentNtpTime
* \param messageHandler (optional) stream message handler function
* \param user (optional) user data that will be passed to the message handler
*/
virtual AooError AOO_CALL process(
AooSample **data, AooInt32 numSamples, AooNtpTime t,
AooStreamMessageHandler messageHandler, void *user) = 0;
/** \brief set event handler function and event handling mode
*
* \attention Not threadsafe - only call in the beginning!
*/
virtual AooError AOO_CALL setEventHandler(
AooEventHandler fn, void *user, AooEventMode mode) = 0;
/** \brief check for pending events
*
* \note Threadsafe and RT-safe */
virtual AooBool AOO_CALL eventsAvailable() = 0;
/** \brief poll events
*
* \note Threadsafe and RT-safe, but not reentrant.
*
* This function will call the registered event handler one or more times.
* \attention The event handler must have been registered with #kAooEventModePoll.
*/
virtual AooError AOO_CALL pollEvents() = 0;
/** \brief invite source
*
* This will continuously send invitation requests to the source
* The source can either accept the invitation request and start a
* stream or it can ignore it, upon which the sink will eventually
* receive an AooEventInviteTimeout event.
* If you call this function while you are already receiving a stream,
* it will force a new stream. For example, you might want to request
* different format parameters or even ask for different musical content.
*
* \param source the AOO source to be invited
* \param metadata optional metadata that the source can interpret
* before accepting the invitation
*/
virtual AooError AOO_CALL inviteSource(
const AooEndpoint& source, const AooData *metadata) = 0;
/** \brief uninvite source
*
* This will continuously send uninvitation requests to the source.
* The source can either accept the uninvitation request and stop the
* stream, or it can ignore and continue sending, upon which the sink
* will eventually receive an #kAooEventUninviteTimeout event.
*
* \param source the AOO source to be uninvited
*/
virtual AooError AOO_CALL uninviteSource(const AooEndpoint& source) = 0;
/** \brief uninvite all sources */
virtual AooError AOO_CALL uninviteAll() = 0;
/** \brief control interface
*
* Not to be used directly. */
virtual AooError AOO_CALL control(
AooCtl ctl, AooIntPtr index, void *data, AooSize size) = 0;
/** \brief codec control interface
*
* Not to be used directly. */
virtual AooError AOO_CALL codecControl(
const AooChar *codec, AooCtl ctl, AooIntPtr index,
void *data, AooSize size) = 0;
/*--------------------------------------------*/
/* type-safe control functions */
/*--------------------------------------------*/
/** \brief Set AOO source ID
* \param id The new ID
*
* \attention Do not change the ID while the sink is being managed
* by an AooClient instance!
*/
AooError setId(AooId id) {
return control(kAooCtlSetId, 0, AOO_ARG(id));
}
/** \brief Get AOO source ID */
AooError getId(AooId &id) {
return control(kAooCtlGetId, 0, AOO_ARG(id));
}
/** \brief Reset the sink */
AooError reset() {
return control(kAooCtlReset, 0, nullptr, 0);
}
/** \brief Set the latency (in seconds)
*
* If a new stream is started, processing is delayed for the specified
* time resp. until we have received the corresponding number of blocks.
* This gives a buffer for compensating network and audio jitter.
*
* For local networks small latencies of 5-20 ms should work;
* for unreliable/unpredictable networks you might need to increase it
* significantly if you want to avoid dropouts.
*
* Higher latencies also allow for missing packets to be resent (if enabled).
* You can effectively create 'reliable' streams over unstable connections.
*
* Some codecs, such as Opus, offer packet loss concealment, so you can
* use very low latencies and still get acceptable results.
*/
AooError setLatency(AooSeconds s) {
return control(kAooCtlSetLatency, 0, AOO_ARG(s));
}
/** \brief Get the current latency (in seconds) */
AooError getLatency(AooSeconds& s) {
return control(kAooCtlGetLatency, 0, AOO_ARG(s));
}
/** \brief Set the network packet buffer size (in seconds)
*
* By default, the size of the network packet buffer is 2 * latency.
*
* In certain situations, you may want to set the buffer size independently
* from the latency. Only use this if you know what you are doing!
*
* You can revert to the default behavior by passing a value of 0.0.
*/
AooError setBufferSize(AooSeconds s) {
return control(kAooCtlSetBufferSize, 0, AOO_ARG(s));
}
/** \brief Get the current buffer size (in seconds)
*
* 0.0 = default behavior (2 * latency)
*/
AooError getBufferSize(AooSeconds& s) {
return control(kAooCtlGetBufferSize, 0, AOO_ARG(s));
}
/** \brief Report xruns
*
* If your audio backend notifies you about xruns, you may report
* them to AooSink. "Missing" samples will be substituted with empty
* blocks and the dynamic resampler (if enabled) will be reset.
*/
AooError reportXRun(AooInt32 numSamples) {
return control(kAooCtlReportXRun, 0, AOO_ARG(numSamples));
}
/** \brief Set the resample mode */
AOO_INLINE AooError setResampleMethod(AooResampleMethod mode)
{
return control(kAooCtlSetResampleMethod, 0, AOO_ARG(mode));
}
/** \brief Get the resample mode */
AOO_INLINE AooError getResampleMethod(AooResampleMethod& mode)
{
return control(kAooCtlGetResampleMethod, 0, AOO_ARG(mode));
}
/** \brief Enable/disable dynamic resampling
*
* Dynamic resampling attempts to mitigate timing differences
* between different machines caused by internal clock drift.
*
* A DLL filter estimates the effective sample rate on both sides
* and the audio data is resampled accordingly. The behavior can be
* fine-tuned with AooSource::setDllBandWidth().
*
* See the paper "Using a DLL to filter time" by Fons Adriaensen.
*/
AooError setDynamicResampling(AooBool b) {
return control(kAooCtlSetDynamicResampling, 0, AOO_ARG(b));
}
/** \brief Check if dynamic resampling is enabled. */
AooError getDynamicResampling(AooBool b) {
return control(kAooCtlGetDynamicResampling, 0, AOO_ARG(b));
}
/** \brief Get the "real" samplerate as measured by the DLL filter */
AooError getRealSampleRate(AooSampleRate& sr) {
return control(kAooCtlGetRealSampleRate, 0, AOO_ARG(sr));
}
/** \brief Set DLL filter bandwidth
*
* Used for dynamic resampling, see AooSource::setDynamicResampling().
*/
AooError setDllBandwidth(double q) {
return control(kAooCtlSetDllBandwidth, 0, AOO_ARG(q));
}
/** \brief get DLL filter bandwidth */
AooError getDllBandwidth(double& q) {
return control(kAooCtlGetDllBandwidth, 0, AOO_ARG(q));
}
/** \brief Reset the time DLL filter
*
* Use this if the audio thread experiences timing irregularities,
* but you cannot use AooSink::reportXRun(). This function only
* has an effect if dynamic resampling is enabled.
*/
AooError resetDll() {
return control(kAooCtlResetDll, 0, nullptr, 0);
}
/** \brief Set the max. UDP packet size in bytes
*
* The default value should be fine for most networks (including the internet),
* but you might want to increase this value for local networks because larger
* packet sizes have less overhead. This is mostly relevant for resend requests.
*/
AooError setPacketSize(AooInt32 n) {
return control(kAooCtlSetPacketSize, 0, AOO_ARG(n));
}
/** \brief Get the max. UDP packet size */
AooError getPacketSize(AooInt32& n) {
return control(kAooCtlGetPacketSize, 0, AOO_ARG(n));
}
/** \brief Set the ping interval (in seconds) */
AooError setPingInterval(AooSeconds s) {
return control(kAooCtlSetPingInterval, 0, AOO_ARG(s));
}
/** \brief Get the ping interval (in seconds) */
AooError getPingInterval(AooSeconds& s) {
return control(kAooCtlGetPingInterval, 0, AOO_ARG(s));
}
/** \brief Enable/disable data resending */
AooError setResendData(AooBool b) {
return control(kAooCtlSetResendData, 0, AOO_ARG(b));
}
/** \brief Check if data resending is enabled */
AooError getResendData(AooBool& b) {
return control(kAooCtlGetResendData, 0, AOO_ARG(b));
}
/** \brief Set resend interval (in seconds)
*
* This is the interval between individual resend attempts for a specific frame.
* Since there is always a certain roundtrip delay between source and sink,
* it makes sense to wait between resend attempts to not spam the network
* with redundant messages.
*/
AooError setResendInterval(AooSeconds s) {
return control(kAooCtlSetResendInterval, 0, AOO_ARG(s));
}
/** \brief Get resend interval (in seconds) */
AooError getResendInterval(AooSeconds& s) {
return control(kAooCtlGetResendInterval, 0, AOO_ARG(s));
}
/** \brief Set the resend limit
*
* This is the max. number of *blocks* to request in a single process call.
*/
AooError setResendLimit(AooInt32 n) {
return control(kAooCtlSetResendLimit, 0, AOO_ARG(n));
}
/** \brief Get the frame resend limit */
AooError getResendLimit(AooInt32& n) {
return control(kAooCtlGetResendLimit, 0, AOO_ARG(n));
}
/** \brief Set source timeout (in seconds)
*
* The time to wait before removing inactive sources.
* Pass kAooInfinite for no timeout.
*/
AooError setSourceTimeout(AooSeconds s) {
return control(kAooCtlSetSourceTimeout, 0, AOO_ARG(s));
}
/** \brief Get source timeout (in seconds) */
AooError getSourceTimeout(AooSeconds& s) {
return control(kAooCtlGetSourceTimeout, 0, AOO_ARG(s));
}
/** \brief Set (un)invite timeout (in seconds)
*
* Time to wait before stopping the (un)invite process.
*/
AooError setInviteTimeout(AooSeconds s) {
return control(kAooCtlSetInviteTimeout, 0, AOO_ARG(s));
}
/** \brief Get (un)invite timeout (in seconds) */
AooError getInviteTimeout(AooSeconds& s) {
return control(kAooCtlGetInviteTimeout, 0, AOO_ARG(s));
}
/** \brief Reset a specific source */
AooError resetSource(const AooEndpoint& source) {
return control(kAooCtlReset, (AooIntPtr)&source, nullptr, 0);
}
/** \brief Get the source stream format
*
* \param source The source endpoint.
* \param [out] format instance of `AooFormatStorage` that should contain
* the format; on success, the `structSize` member is set to the actual size.
*/
AooError getSourceFormat(const AooEndpoint& source, AooFormatStorage& format) {
return control(kAooCtlGetFormat, (AooIntPtr)&source, AOO_ARG(format));
}
/** \brief Get the current buffer fill ratio
*
* \param source The source endpoint.
* \param [out] ratio The current fill ratio (0.0: empty, 1.0: full)
*/
AooError getBufferFillRatio(const AooEndpoint& source, double& ratio) {
return control(kAooCtlGetBufferFillRatio, (AooIntPtr)&source, AOO_ARG(ratio));
}
/** \brief Enable/disable binary message format
*
* Use a more compact (and faster) binary format for certain messages
*/
AooError setBinaryFormat(AooBool b) {
return control(kAooCtlSetBinaryFormat, 0, AOO_ARG(b));
}
/** \brief Check if binary message format is enabled */
AooError getBinaryFormat(AooBool& b) {
return control(kAooCtlGetBinaryFormat, 0, AOO_ARG(b));
}
protected:
~AooSink(){} // non-virtual!
};