Skip to content

Latest commit

 

History

History
429 lines (355 loc) · 13.3 KB

SDL_jackaudio.c

File metadata and controls

429 lines (355 loc) · 13.3 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_AUDIO_DRIVER_JACK
#include "SDL_assert.h"
#include "SDL_timer.h"
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "SDL_jackaudio.h"
#include "SDL_loadso.h"
#include "../../thread/SDL_systhread.h"
static jack_client_t * (*JACK_jack_client_open) (const char *, jack_options_t, jack_status_t *, ...);
static int (*JACK_jack_client_close) (jack_client_t *);
static void (*JACK_jack_on_shutdown) (jack_client_t *, JackShutdownCallback, void *);
static int (*JACK_jack_activate) (jack_client_t *);
Jun 9, 2017
Jun 9, 2017
39
static int (*JACK_jack_deactivate) (jack_client_t *);
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
static void * (*JACK_jack_port_get_buffer) (jack_port_t *, jack_nframes_t);
static int (*JACK_jack_port_unregister) (jack_client_t *, jack_port_t *);
static void (*JACK_jack_free) (void *);
static const char ** (*JACK_jack_get_ports) (jack_client_t *, const char *, const char *, unsigned long);
static jack_nframes_t (*JACK_jack_get_sample_rate) (jack_client_t *);
static jack_nframes_t (*JACK_jack_get_buffer_size) (jack_client_t *);
static jack_port_t * (*JACK_jack_port_register) (jack_client_t *, const char *, const char *, unsigned long, unsigned long);
static const char * (*JACK_jack_port_name) (const jack_port_t *);
static int (*JACK_jack_connect) (jack_client_t *, const char *, const char *);
static int (*JACK_jack_set_process_callback) (jack_client_t *, JackProcessCallback, void *);
static int load_jack_syms(void);
#ifdef SDL_AUDIO_DRIVER_JACK_DYNAMIC
static const char *jack_library = SDL_AUDIO_DRIVER_JACK_DYNAMIC;
static void *jack_handle = NULL;
/* !!! FIXME: this is copy/pasted in several places now */
static int
load_jack_sym(const char *fn, void **addr)
{
*addr = SDL_LoadFunction(jack_handle, fn);
if (*addr == NULL) {
/* Don't call SDL_SetError(): SDL_LoadFunction already did. */
return 0;
}
return 1;
}
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
#define SDL_JACK_SYM(x) \
if (!load_jack_sym(#x, (void **) (char *) &JACK_##x)) return -1
static void
UnloadJackLibrary(void)
{
if (jack_handle != NULL) {
SDL_UnloadObject(jack_handle);
jack_handle = NULL;
}
}
static int
LoadJackLibrary(void)
{
int retval = 0;
if (jack_handle == NULL) {
jack_handle = SDL_LoadObject(jack_library);
if (jack_handle == NULL) {
retval = -1;
/* Don't call SDL_SetError(): SDL_LoadObject already did. */
} else {
retval = load_jack_syms();
if (retval < 0) {
UnloadJackLibrary();
}
}
}
return retval;
}
#else
#define SDL_JACK_SYM(x) JACK_##x = x
static void
UnloadJackLibrary(void)
{
}
static int
LoadJackLibrary(void)
{
load_jack_syms();
return 0;
}
#endif /* SDL_AUDIO_DRIVER_JACK_DYNAMIC */
static int
load_jack_syms(void)
{
SDL_JACK_SYM(jack_client_open);
SDL_JACK_SYM(jack_client_close);
SDL_JACK_SYM(jack_on_shutdown);
SDL_JACK_SYM(jack_activate);
Jun 9, 2017
Jun 9, 2017
130
SDL_JACK_SYM(jack_deactivate);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
SDL_JACK_SYM(jack_port_get_buffer);
SDL_JACK_SYM(jack_port_unregister);
SDL_JACK_SYM(jack_free);
SDL_JACK_SYM(jack_get_ports);
SDL_JACK_SYM(jack_get_sample_rate);
SDL_JACK_SYM(jack_get_buffer_size);
SDL_JACK_SYM(jack_port_register);
SDL_JACK_SYM(jack_port_name);
SDL_JACK_SYM(jack_connect);
SDL_JACK_SYM(jack_set_process_callback);
return 0;
}
static void
Jun 9, 2017
Jun 9, 2017
146
jackShutdownCallback(void *arg) /* JACK went away; device is lost. */
Jun 9, 2017
Jun 9, 2017
148
149
150
SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
SDL_OpenedAudioDeviceDisconnected(this);
SDL_SemPost(this->hidden->iosem); /* unblock the SDL thread. */
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
}
// !!! FIXME: implement and register these!
//typedef int(* JackSampleRateCallback)(jack_nframes_t nframes, void *arg)
//typedef int(* JackBufferSizeCallback)(jack_nframes_t nframes, void *arg)
static int
jackProcessPlaybackCallback(jack_nframes_t nframes, void *arg)
{
SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
jack_port_t **ports = this->hidden->sdlports;
const int total_channels = this->spec.channels;
const int total_frames = this->spec.samples;
int channelsi;
if (!SDL_AtomicGet(&this->enabled)) {
/* silence the buffer to avoid repeats and corruption. */
SDL_memset(this->hidden->iobuffer, '\0', this->spec.size);
}
for (channelsi = 0; channelsi < total_channels; channelsi++) {
float *dst = (float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
if (dst) {
const float *src = ((float *) this->hidden->iobuffer) + channelsi;
int framesi;
for (framesi = 0; framesi < total_frames; framesi++) {
*(dst++) = *src;
src += total_channels;
}
}
}
SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; refill the buffer. */
return 0; /* success */
}
/* This function waits until it is possible to write a full sound buffer */
static void
JACK_WaitDevice(_THIS)
{
if (SDL_AtomicGet(&this->enabled)) {
if (SDL_SemWait(this->hidden->iosem) == -1) {
SDL_OpenedAudioDeviceDisconnected(this);
}
}
}
static Uint8 *
JACK_GetDeviceBuf(_THIS)
{
return (Uint8 *) this->hidden->iobuffer;
}
static int
jackProcessCaptureCallback(jack_nframes_t nframes, void *arg)
{
Jun 9, 2017
Jun 9, 2017
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
if (SDL_AtomicGet(&this->enabled)) {
jack_port_t **ports = this->hidden->sdlports;
const int total_channels = this->spec.channels;
const int total_frames = this->spec.samples;
int channelsi;
for (channelsi = 0; channelsi < total_channels; channelsi++) {
const float *src = (const float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
if (src) {
float *dst = ((float *) this->hidden->iobuffer) + channelsi;
int framesi;
for (framesi = 0; framesi < total_frames; framesi++) {
*dst = *(src++);
dst += total_channels;
}
}
}
}
SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; new buffer is ready! */
return 0; /* success */
231
232
233
234
235
}
static int
JACK_CaptureFromDevice(_THIS, void *buffer, int buflen)
{
Jun 9, 2017
Jun 9, 2017
236
237
238
239
240
241
242
243
244
SDL_assert(buflen == this->spec.size); /* we always fill a full buffer. */
/* Wait for JACK to fill the iobuffer */
if (SDL_SemWait(this->hidden->iosem) == -1) {
return -1;
}
SDL_memcpy(buffer, this->hidden->iobuffer, buflen);
return buflen;
Jun 9, 2017
Jun 9, 2017
246
247
248
249
250
251
252
static void
JACK_FlushCapture(_THIS)
{
SDL_SemWait(this->hidden->iosem);
}
253
254
255
256
static void
JACK_CloseDevice(_THIS)
{
Jun 9, 2017
Jun 9, 2017
257
258
259
260
261
262
263
264
265
266
if (this->hidden->client) {
JACK_jack_deactivate(this->hidden->client);
if (this->hidden->sdlports) {
const int channels = this->spec.channels;
int i;
for (i = 0; i < channels; i++) {
JACK_jack_port_unregister(this->hidden->client, this->hidden->sdlports[i]);
}
SDL_free(this->hidden->sdlports);
Jun 9, 2017
Jun 9, 2017
268
269
JACK_jack_client_close(this->hidden->client);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
}
if (this->hidden->iosem) {
SDL_DestroySemaphore(this->hidden->iosem);
}
if (this->hidden->devports) {
JACK_jack_free(this->hidden->devports);
}
SDL_free(this->hidden->iobuffer);
}
static int
JACK_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
/* Note that JACK uses "output" for capture devices (they output audio
data to us) and "input" for playback (we input audio data to them).
Likewise, SDL's playback port will be "output" (we write data out)
and capture will be "input" (we read data in). */
const unsigned long sysportflags = iscapture ? JackPortIsOutput : JackPortIsInput;
const unsigned long sdlportflags = iscapture ? JackPortIsInput : JackPortIsOutput;
Jun 9, 2017
Jun 9, 2017
292
const JackProcessCallback callback = iscapture ? jackProcessCaptureCallback : jackProcessPlaybackCallback;
293
294
const char *sdlportstr = iscapture ? "input" : "output";
const char **devports = NULL;
Jun 9, 2017
Jun 9, 2017
295
296
jack_client_t *client = NULL;
jack_status_t status;
297
298
299
300
301
302
303
304
305
int channels = 0;
int i;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof (*this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
Jun 9, 2017
Jun 9, 2017
306
307
308
309
310
311
312
313
/* !!! FIXME: we _still_ need an API to specify an app name */
client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
this->hidden->client = client;
if (client == NULL) {
return SDL_SetError("Can't open JACK client");
}
devports = JACK_jack_get_ports(client, NULL, NULL, JackPortIsPhysical | sysportflags);
314
315
316
317
318
319
320
321
322
323
324
325
326
this->hidden->devports = devports;
if (!devports || !devports[0]) {
return SDL_SetError("No physical JACK ports available");
}
while (devports[++channels]) {
/* spin to count devports */
}
/* !!! FIXME: docs say about buffer size: "This size may change, clients that depend on it must register a bufsize_callback so they will be notified if it does." */
/* Jack pretty much demands what it wants. */
this->spec.format = AUDIO_F32SYS;
Jun 9, 2017
Jun 9, 2017
327
this->spec.freq = JACK_jack_get_sample_rate(client);
328
this->spec.channels = channels;
Jun 9, 2017
Jun 9, 2017
329
this->spec.samples = JACK_jack_get_buffer_size(client);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
SDL_CalculateAudioSpec(&this->spec);
this->hidden->iosem = SDL_CreateSemaphore(0);
if (!this->hidden->iosem) {
return -1; /* error was set by SDL_CreateSemaphore */
}
this->hidden->iobuffer = (float *) SDL_calloc(1, this->spec.size);
if (!this->hidden->iobuffer) {
return SDL_OutOfMemory();
}
/* Build SDL's ports, which we will connect to the device ports. */
this->hidden->sdlports = (jack_port_t **) SDL_calloc(channels, sizeof (jack_port_t *));
if (this->hidden->sdlports == NULL) {
return SDL_OutOfMemory();
}
for (i = 0; i < channels; i++) {
char portname[32];
SDL_snprintf(portname, sizeof (portname), "sdl_jack_%s_%d", sdlportstr, i);
Jun 9, 2017
Jun 9, 2017
352
this->hidden->sdlports[i] = JACK_jack_port_register(client, portname, JACK_DEFAULT_AUDIO_TYPE, sdlportflags, 0);
353
354
355
356
357
if (this->hidden->sdlports[i] == NULL) {
return SDL_SetError("jack_port_register failed");
}
}
Jun 9, 2017
Jun 9, 2017
358
if (JACK_jack_set_process_callback(client, callback, this) != 0) {
Jun 9, 2017
Jun 9, 2017
359
360
361
362
363
364
365
return SDL_SetError("JACK: Couldn't set process callback");
}
JACK_jack_on_shutdown(client, jackShutdownCallback, this);
if (JACK_jack_activate(client) != 0) {
return SDL_SetError("Failed to activate JACK client");
366
367
368
369
370
371
372
}
/* once activated, we can connect all the ports. */
for (i = 0; i < channels; i++) {
const char *sdlport = JACK_jack_port_name(this->hidden->sdlports[i]);
const char *srcport = iscapture ? devports[i] : sdlport;
const char *dstport = iscapture ? sdlport : devports[i];
Jun 9, 2017
Jun 9, 2017
373
if (JACK_jack_connect(client, srcport, dstport) != 0) {
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
return SDL_SetError("Couldn't connect JACK ports: %s => %s", srcport, dstport);
}
}
/* don't need these anymore. */
this->hidden->devports = NULL;
JACK_jack_free(devports);
/* We're ready to rock and roll. :-) */
return 0;
}
static void
JACK_Deinitialize(void)
{
UnloadJackLibrary();
}
static int
JACK_Init(SDL_AudioDriverImpl * impl)
{
if (LoadJackLibrary() < 0) {
return 0;
Jun 9, 2017
Jun 9, 2017
397
398
399
400
401
402
403
404
405
} else {
/* Make sure a JACK server is running and available. */
jack_status_t status;
jack_client_t *client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
if (client == NULL) {
UnloadJackLibrary();
return 0;
}
JACK_jack_client_close(client);
406
407
408
409
410
411
412
413
}
/* Set the function pointers */
impl->OpenDevice = JACK_OpenDevice;
impl->WaitDevice = JACK_WaitDevice;
impl->GetDeviceBuf = JACK_GetDeviceBuf;
impl->CloseDevice = JACK_CloseDevice;
impl->Deinitialize = JACK_Deinitialize;
Jun 9, 2017
Jun 9, 2017
414
415
416
417
418
impl->CaptureFromDevice = JACK_CaptureFromDevice;
impl->FlushCapture = JACK_FlushCapture;
impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
impl->HasCaptureSupport = SDL_TRUE;
419
420
421
422
423
424
425
426
427
428
429
return 1; /* this audio target is available. */
}
AudioBootStrap JACK_bootstrap = {
"jack", "JACK Audio Connection Kit", JACK_Init, 0
};
#endif /* SDL_AUDIO_DRIVER_JACK */
/* vi: set ts=4 sw=4 expandtab: */