-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaoo_source.hpp
More file actions
463 lines (401 loc) · 15.8 KB
/
aoo_source.hpp
File metadata and controls
463 lines (401 loc) · 15.8 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
452
453
454
455
456
457
458
459
460
461
462
463
/* 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 source
*/
#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 AooSource AooSource;
/** \endcond */
/** \brief create a new AOO source instance
*
* \param id the ID
* \return new AooSource instance on success; `NULL` on failure
*/
AOO_API AooSource * AOO_CALL AooSource_new(AooId id);
/** \brief destroy the AOO source instance */
AOO_API void AOO_CALL AooSource_free(AooSource *source);
/*------------------------------------------------------------*/
/** \brief AOO source interface */
struct AooSource {
public:
#if AOO_HAVE_CXX11
/** \brief custom deleter for AooSource */
class Deleter {
public:
void operator()(AooSource *obj){
AooSource_free(obj);
}
};
/** \brief smart pointer for AOO source instance */
using Ptr = std::unique_ptr<AooSource, Deleter>;
/** \brief create a new managed AOO source instance
*
* \return valid Ptr on success; empty Ptr on failure
*/
static Ptr create(AooId id) {
return Ptr(AooSource_new(id));
}
#endif
/*------------------- methods -----------------------*/
/** \brief setup AOO source
*
* \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 sink 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 add a stream message
*
* \note Threadsafe and RT-safe; call on the audio thread
*
* Call this function to add messages for the \em next process block.
*
* \note if the sample offset is larger than the next process block,
* the message will be scheduled for later.
*
* \param message the message
*/
virtual AooError AOO_CALL addStreamMessage(const AooStreamMessage& message) = 0;
/** \brief process audio
*
* \note Threadsafe and RT-safe; call on the audio thread
*
* \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
*/
virtual AooError AOO_CALL process(
AooSample **data, AooInt32 numSamples, AooNtpTime t) = 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 Start a new stream
*
* \note Threadsafe, RT-safe and reentrant
*
* \param sampleOffset the sample offset in the next processing block where the
* stream is supposed to start.
* \param metadata an optional AooData structure which will be sent as additional
* stream metadata. For example, it could contain information about the
* channel layout, the musical content, etc.
*/
virtual AooError AOO_CALL startStream(
AooInt32 sampleOffset, const AooData *metadata) = 0;
/** \brief Stop the current stream
*
* \note Threadsafe, RT-safe and reentrant
*
* \param sampleOffset the sample offset in the next processing block where the
* stream is supposed to end.
*/
virtual AooError AOO_CALL stopStream(AooInt32 sampleOffset) = 0;
/** \brief add sink
*
* If `active` is true, the sink will start active, otherwise
* it has to be activated manually with AooSource_activateSink().
*/
virtual AooError AOO_CALL addSink(
const AooEndpoint& sink, AooBool active) = 0;
/** \brief remove sink */
virtual AooError AOO_CALL removeSink(const AooEndpoint& sink) = 0;
/** \brief remove all sinks */
virtual AooError AOO_CALL removeAll() = 0;
/** \brief accept/decline an invitation
*
* When you receive an #kAooEventInvite event, you can decide to
* accept or decline the invitation.
*
* \param sink The sink that has sent the invitation
* \param token The invitation token (from the `AooEventInvite` event)
* \param accept `kAooTrue`: accept invitation (activates the sink)
* `kAooFalse`: declines the invitation
*/
virtual AooError AOO_CALL handleInvite(
const AooEndpoint& sink, AooId token, AooBool accept) = 0;
/** \brief accept/decline an uninvitation
*
* When you receive an #kAooEventUninvite event, you can decide to
* accept or decline the uninvitation.
*
* \param sink The sink that has sent the uninvitation
* \param token The uninvitation token (from the `AooEventUninvite` event)
* \param accept `kAooTrue`: accept invitation (deactivates the sink)
* `kAooFalse`: declines the uninvitation
*/
virtual AooError AOO_CALL handleUninvite(
const AooEndpoint& sink, AooId token, AooBool accept) = 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 (De)activate the given sink */
AooError activate(const AooEndpoint& sink, AooBool active) {
return control(kAooCtlActivate, (AooIntPtr)&sink, AOO_ARG(active));
}
/** \brief Check whether the given sink is active */
AooError isActive(const AooEndpoint& sink, AooBool& active) {
return control(kAooCtlIsActive, (AooIntPtr)&sink, AOO_ARG(active));
}
/** \brief reset the source */
AooError reset() {
return control(kAooCtlReset, 0, nullptr, 0);
}
/** \brief Set the stream format
*
* \param [in,out] format Pointer to the format header.
* The format struct is validated and updated on success!
*
* This will change the streaming format and consequently start a new stream.
* The sink(s) will receive a `kAooEventFormatChange` event.
*/
AooError setFormat(AooFormat& format) {
return control(kAooCtlSetFormat, 0, AOO_ARG(format));
}
/** \brief Get the stream format
*
* \param [out] format instance of `AooFormatStorage` that should contain
* the format; on success, the `structSize` member is set to the actual size.
*/
AooError getFormat(AooFormatStorage& format) {
return control(kAooCtlGetFormat, 0, AOO_ARG(format));
}
/** \brief Set AOO source ID
* \param id The new ID
*
* \attention Do not change the ID while the source 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 Set the buffer size in seconds (in seconds)
*
* This is the size of the ring buffer between the audio and network thread.
* The value can be rather small, as you only have to compensate for the time
* it takes to wake up the network thread.
*/
AooError setBufferSize(AooSeconds s) {
return control(kAooCtlSetBufferSize, 0, AOO_ARG(s));
}
/** \brief Get the current buffer size (in seconds) */
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 AooSource::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. If a audio block exceeds the max. UDP packet size,
* it will be automatically broken up into several "frames" and then reassembled in the sink.
*/
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 Set the resend buffer size (in seconds)
*
* The source keeps the last N seconds of audio in a buffer, so it can resend
* parts of it if requested by the sink (because of packet loss).
*
* If set to 0.0, resending is disabled.
*/
AooError setResendBufferSize(AooSeconds s) {
return control(kAooCtlSetResendBufferSize, 0, AOO_ARG(s));
}
/** \brief Get the resend buffer size (in seconds) */
AooError getResendBufferSize(AooSeconds& s) {
return control(kAooCtlGetResendBufferSize, 0, AOO_ARG(s));
}
/** \brief Set redundancy
*
* The number of times each frames is sent (default = 1). This is a primitive
* strategy to cope with possible packet loss, but it can be counterproductive:
* packet loss is often the result of network contention and sending more data
* would only make it worse.
*/
AooError setRedundancy(AooInt32 n) {
return control(kAooCtlSetRedundancy, 0, AOO_ARG(n));
}
/** \brief Get redundancy */
AooError getRedundancy(AooInt32& n) {
return control(kAooCtlGetRedundancy, 0, AOO_ARG(n));
}
/** \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));
}
/** \brief Stream time send interval (in seconds)
*
* The source may periodically send timestamps for more accurate
* stream time measurement. Set to 0 to disable.
*
* \note This is only relevant in conjuction with dynamic resampling;
* otherwise the stream time can be inferred from the sequence number.
*/
AooError setStreamTimeSendInterval(AooSeconds s) {
return control(kAooCtlSetStreamTimeSendInterval, 0, AOO_ARG(s));
}
/** \brief Get stream time send interval (in seconds) */
AooError getStreamTimeSendInterval(AooSeconds& s) {
return control(kAooCtlGetStreamTimeSendInterval, 0, AOO_ARG(s));
}
/** \brief Set the sink channel offset
*
* Set the starting channel where the source signal should be received
* in the given sink. For example, if the channel offset is 5, a 2-channel
* source signal will be summed into sink channels 5 and 6.
* The default is 0 (= the first channel).
*/
AooError setSinkChannelOffset(const AooEndpoint& sink, AooInt32 onset) {
return control(kAooCtlSetSinkChannelOffset, (AooIntPtr)&sink, AOO_ARG(onset));
}
/** \brief Get the sink channel offset for the given sink */
AooError getSinkChannelOffset(const AooEndpoint& sink, AooInt32& onset) {
return control(kAooCtlSetSinkChannelOffset, (AooIntPtr)&sink, AOO_ARG(onset));
}
protected:
~AooSource(){} // non-virtual!
};