pci: Make query-pci stub consistent with the real one
[qemu.git] / audio / jackaudio.c
blob5bdf3d7a78d6ebd9cca54d6b6b409d554f9aa74c
1 /*
2 * QEMU JACK Audio Connection Kit Client
4 * Copyright (c) 2020 Geoffrey McRae (gnif)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/module.h"
27 #include "qemu/atomic.h"
28 #include "qemu/main-loop.h"
29 #include "audio.h"
31 #define AUDIO_CAP "jack"
32 #include "audio_int.h"
34 #include <jack/jack.h>
35 #include <jack/thread.h>
37 struct QJack;
39 typedef enum QJackState {
40 QJACK_STATE_DISCONNECTED,
41 QJACK_STATE_RUNNING,
42 QJACK_STATE_SHUTDOWN
44 QJackState;
46 typedef struct QJackBuffer {
47 int channels;
48 int frames;
49 uint32_t used;
50 int rptr, wptr;
51 float **data;
53 QJackBuffer;
55 typedef struct QJackClient {
56 AudiodevJackPerDirectionOptions *opt;
58 bool out;
59 bool enabled;
60 bool connect_ports;
61 int packets;
63 QJackState state;
64 jack_client_t *client;
65 jack_nframes_t freq;
66 QEMUBH *shutdown_bh;
68 struct QJack *j;
69 int nchannels;
70 int buffersize;
71 jack_port_t **port;
72 QJackBuffer fifo;
74 QJackClient;
76 typedef struct QJackOut {
77 HWVoiceOut hw;
78 QJackClient c;
80 QJackOut;
82 typedef struct QJackIn {
83 HWVoiceIn hw;
84 QJackClient c;
86 QJackIn;
88 static int qjack_client_init(QJackClient *c);
89 static void qjack_client_connect_ports(QJackClient *c);
90 static void qjack_client_fini(QJackClient *c);
91 static QemuMutex qjack_shutdown_lock;
93 static void qjack_buffer_create(QJackBuffer *buffer, int channels, int frames)
95 buffer->channels = channels;
96 buffer->frames = frames;
97 buffer->used = 0;
98 buffer->rptr = 0;
99 buffer->wptr = 0;
100 buffer->data = g_new(float *, channels);
101 for (int i = 0; i < channels; ++i) {
102 buffer->data[i] = g_new(float, frames);
106 static void qjack_buffer_clear(QJackBuffer *buffer)
108 assert(buffer->data);
109 qatomic_store_release(&buffer->used, 0);
110 buffer->rptr = 0;
111 buffer->wptr = 0;
114 static void qjack_buffer_free(QJackBuffer *buffer)
116 if (!buffer->data) {
117 return;
120 for (int i = 0; i < buffer->channels; ++i) {
121 g_free(buffer->data[i]);
124 g_free(buffer->data);
125 buffer->data = NULL;
128 /* write PCM interleaved */
129 static int qjack_buffer_write(QJackBuffer *buffer, float *data, int size)
131 assert(buffer->data);
132 const int samples = size / sizeof(float);
133 int frames = samples / buffer->channels;
134 const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
136 if (frames > avail) {
137 frames = avail;
140 int copy = frames;
141 int wptr = buffer->wptr;
143 while (copy) {
145 for (int c = 0; c < buffer->channels; ++c) {
146 buffer->data[c][wptr] = *data++;
149 if (++wptr == buffer->frames) {
150 wptr = 0;
153 --copy;
156 buffer->wptr = wptr;
158 qatomic_add(&buffer->used, frames);
159 return frames * buffer->channels * sizeof(float);
162 /* write PCM linear */
163 static int qjack_buffer_write_l(QJackBuffer *buffer, float **dest, int frames)
165 assert(buffer->data);
166 const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
167 int wptr = buffer->wptr;
169 if (frames > avail) {
170 frames = avail;
173 int right = buffer->frames - wptr;
174 if (right > frames) {
175 right = frames;
178 const int left = frames - right;
179 for (int c = 0; c < buffer->channels; ++c) {
180 memcpy(buffer->data[c] + wptr, dest[c] , right * sizeof(float));
181 memcpy(buffer->data[c] , dest[c] + right, left * sizeof(float));
184 wptr += frames;
185 if (wptr >= buffer->frames) {
186 wptr -= buffer->frames;
188 buffer->wptr = wptr;
190 qatomic_add(&buffer->used, frames);
191 return frames;
194 /* read PCM interleaved */
195 static int qjack_buffer_read(QJackBuffer *buffer, float *dest, int size)
197 assert(buffer->data);
198 const int samples = size / sizeof(float);
199 int frames = samples / buffer->channels;
200 const int avail = qatomic_load_acquire(&buffer->used);
202 if (frames > avail) {
203 frames = avail;
206 int copy = frames;
207 int rptr = buffer->rptr;
209 while (copy) {
211 for (int c = 0; c < buffer->channels; ++c) {
212 *dest++ = buffer->data[c][rptr];
215 if (++rptr == buffer->frames) {
216 rptr = 0;
219 --copy;
222 buffer->rptr = rptr;
224 qatomic_sub(&buffer->used, frames);
225 return frames * buffer->channels * sizeof(float);
228 /* read PCM linear */
229 static int qjack_buffer_read_l(QJackBuffer *buffer, float **dest, int frames)
231 assert(buffer->data);
232 int copy = frames;
233 const int used = qatomic_load_acquire(&buffer->used);
234 int rptr = buffer->rptr;
236 if (copy > used) {
237 copy = used;
240 int right = buffer->frames - rptr;
241 if (right > copy) {
242 right = copy;
245 const int left = copy - right;
246 for (int c = 0; c < buffer->channels; ++c) {
247 memcpy(dest[c] , buffer->data[c] + rptr, right * sizeof(float));
248 memcpy(dest[c] + right, buffer->data[c] , left * sizeof(float));
251 rptr += copy;
252 if (rptr >= buffer->frames) {
253 rptr -= buffer->frames;
255 buffer->rptr = rptr;
257 qatomic_sub(&buffer->used, copy);
258 return copy;
261 static int qjack_process(jack_nframes_t nframes, void *arg)
263 QJackClient *c = (QJackClient *)arg;
265 if (c->state != QJACK_STATE_RUNNING) {
266 return 0;
269 /* get the buffers for the ports */
270 float *buffers[c->nchannels];
271 for (int i = 0; i < c->nchannels; ++i) {
272 buffers[i] = jack_port_get_buffer(c->port[i], nframes);
275 if (c->out) {
276 if (likely(c->enabled)) {
277 qjack_buffer_read_l(&c->fifo, buffers, nframes);
278 } else {
279 for (int i = 0; i < c->nchannels; ++i) {
280 memset(buffers[i], 0, nframes * sizeof(float));
283 } else {
284 if (likely(c->enabled)) {
285 qjack_buffer_write_l(&c->fifo, buffers, nframes);
289 return 0;
292 static void qjack_port_registration(jack_port_id_t port, int reg, void *arg)
294 if (reg) {
295 QJackClient *c = (QJackClient *)arg;
296 c->connect_ports = true;
300 static int qjack_xrun(void *arg)
302 QJackClient *c = (QJackClient *)arg;
303 if (c->state != QJACK_STATE_RUNNING) {
304 return 0;
307 qjack_buffer_clear(&c->fifo);
308 return 0;
311 static void qjack_shutdown_bh(void *opaque)
313 QJackClient *c = (QJackClient *)opaque;
314 qjack_client_fini(c);
317 static void qjack_shutdown(void *arg)
319 QJackClient *c = (QJackClient *)arg;
320 c->state = QJACK_STATE_SHUTDOWN;
321 qemu_bh_schedule(c->shutdown_bh);
324 static void qjack_client_recover(QJackClient *c)
326 if (c->state != QJACK_STATE_DISCONNECTED) {
327 return;
330 /* packets is used simply to throttle this */
331 if (c->packets % 100 == 0) {
333 /* if enabled then attempt to recover */
334 if (c->enabled) {
335 dolog("attempting to reconnect to server\n");
336 qjack_client_init(c);
341 static size_t qjack_write(HWVoiceOut *hw, void *buf, size_t len)
343 QJackOut *jo = (QJackOut *)hw;
344 ++jo->c.packets;
346 if (jo->c.state != QJACK_STATE_RUNNING) {
347 qjack_client_recover(&jo->c);
348 return len;
351 qjack_client_connect_ports(&jo->c);
352 return qjack_buffer_write(&jo->c.fifo, buf, len);
355 static size_t qjack_read(HWVoiceIn *hw, void *buf, size_t len)
357 QJackIn *ji = (QJackIn *)hw;
358 ++ji->c.packets;
360 if (ji->c.state != QJACK_STATE_RUNNING) {
361 qjack_client_recover(&ji->c);
362 return len;
365 qjack_client_connect_ports(&ji->c);
366 return qjack_buffer_read(&ji->c.fifo, buf, len);
369 static void qjack_client_connect_ports(QJackClient *c)
371 if (!c->connect_ports || !c->opt->connect_ports) {
372 return;
375 c->connect_ports = false;
376 const char **ports;
377 ports = jack_get_ports(c->client, c->opt->connect_ports, NULL,
378 c->out ? JackPortIsInput : JackPortIsOutput);
380 if (!ports) {
381 return;
384 for (int i = 0; i < c->nchannels && ports[i]; ++i) {
385 const char *p = jack_port_name(c->port[i]);
386 if (jack_port_connected_to(c->port[i], ports[i])) {
387 continue;
390 if (c->out) {
391 dolog("connect %s -> %s\n", p, ports[i]);
392 jack_connect(c->client, p, ports[i]);
393 } else {
394 dolog("connect %s -> %s\n", ports[i], p);
395 jack_connect(c->client, ports[i], p);
400 static int qjack_client_init(QJackClient *c)
402 jack_status_t status;
403 char client_name[jack_client_name_size()];
404 jack_options_t options = JackNullOption;
406 if (c->state == QJACK_STATE_RUNNING) {
407 return 0;
410 c->connect_ports = true;
412 snprintf(client_name, sizeof(client_name), "%s-%s",
413 c->out ? "out" : "in",
414 c->opt->client_name ? c->opt->client_name : audio_application_name());
416 if (c->opt->exact_name) {
417 options |= JackUseExactName;
420 if (!c->opt->start_server) {
421 options |= JackNoStartServer;
424 if (c->opt->server_name) {
425 options |= JackServerName;
428 c->client = jack_client_open(client_name, options, &status,
429 c->opt->server_name);
431 if (c->client == NULL) {
432 dolog("jack_client_open failed: status = 0x%2.0x\n", status);
433 if (status & JackServerFailed) {
434 dolog("unable to connect to JACK server\n");
436 return -1;
439 c->freq = jack_get_sample_rate(c->client);
441 if (status & JackServerStarted) {
442 dolog("JACK server started\n");
445 if (status & JackNameNotUnique) {
446 dolog("JACK unique name assigned %s\n",
447 jack_get_client_name(c->client));
450 jack_set_process_callback(c->client, qjack_process , c);
451 jack_set_port_registration_callback(c->client, qjack_port_registration, c);
452 jack_set_xrun_callback(c->client, qjack_xrun, c);
453 jack_on_shutdown(c->client, qjack_shutdown, c);
455 /* allocate and register the ports */
456 c->port = g_new(jack_port_t *, c->nchannels);
457 for (int i = 0; i < c->nchannels; ++i) {
459 char port_name[16];
460 snprintf(
461 port_name,
462 sizeof(port_name),
463 c->out ? "output %d" : "input %d",
466 c->port[i] = jack_port_register(
467 c->client,
468 port_name,
469 JACK_DEFAULT_AUDIO_TYPE,
470 c->out ? JackPortIsOutput : JackPortIsInput,
474 /* activate the session */
475 jack_activate(c->client);
476 c->buffersize = jack_get_buffer_size(c->client);
479 * ensure the buffersize is no smaller then 512 samples, some (all?) qemu
480 * virtual devices do not work correctly otherwise
482 if (c->buffersize < 512) {
483 c->buffersize = 512;
486 /* create a 3 period buffer */
487 qjack_buffer_create(&c->fifo, c->nchannels, c->buffersize * 3);
489 qjack_client_connect_ports(c);
490 c->state = QJACK_STATE_RUNNING;
491 return 0;
494 static int qjack_init_out(HWVoiceOut *hw, struct audsettings *as,
495 void *drv_opaque)
497 QJackOut *jo = (QJackOut *)hw;
498 Audiodev *dev = (Audiodev *)drv_opaque;
500 jo->c.out = true;
501 jo->c.enabled = false;
502 jo->c.nchannels = as->nchannels;
503 jo->c.opt = dev->u.jack.out;
505 jo->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &jo->c);
507 int ret = qjack_client_init(&jo->c);
508 if (ret != 0) {
509 qemu_bh_delete(jo->c.shutdown_bh);
510 return ret;
513 /* report the buffer size to qemu */
514 hw->samples = jo->c.buffersize;
516 /* report the audio format we support */
517 struct audsettings os = {
518 .freq = jo->c.freq,
519 .nchannels = jo->c.nchannels,
520 .fmt = AUDIO_FORMAT_F32,
521 .endianness = 0
523 audio_pcm_init_info(&hw->info, &os);
525 dolog("JACK output configured for %dHz (%d samples)\n",
526 jo->c.freq, jo->c.buffersize);
528 return 0;
531 static int qjack_init_in(HWVoiceIn *hw, struct audsettings *as,
532 void *drv_opaque)
534 QJackIn *ji = (QJackIn *)hw;
535 Audiodev *dev = (Audiodev *)drv_opaque;
537 ji->c.out = false;
538 ji->c.enabled = false;
539 ji->c.nchannels = as->nchannels;
540 ji->c.opt = dev->u.jack.in;
542 ji->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &ji->c);
544 int ret = qjack_client_init(&ji->c);
545 if (ret != 0) {
546 qemu_bh_delete(ji->c.shutdown_bh);
547 return ret;
550 /* report the buffer size to qemu */
551 hw->samples = ji->c.buffersize;
553 /* report the audio format we support */
554 struct audsettings is = {
555 .freq = ji->c.freq,
556 .nchannels = ji->c.nchannels,
557 .fmt = AUDIO_FORMAT_F32,
558 .endianness = 0
560 audio_pcm_init_info(&hw->info, &is);
562 dolog("JACK input configured for %dHz (%d samples)\n",
563 ji->c.freq, ji->c.buffersize);
565 return 0;
568 static void qjack_client_fini_locked(QJackClient *c)
570 switch (c->state) {
571 case QJACK_STATE_RUNNING:
572 jack_deactivate(c->client);
573 /* fallthrough */
575 case QJACK_STATE_SHUTDOWN:
576 jack_client_close(c->client);
577 c->client = NULL;
579 qjack_buffer_free(&c->fifo);
580 g_free(c->port);
582 c->state = QJACK_STATE_DISCONNECTED;
583 /* fallthrough */
585 case QJACK_STATE_DISCONNECTED:
586 break;
590 static void qjack_client_fini(QJackClient *c)
592 qemu_mutex_lock(&qjack_shutdown_lock);
593 qjack_client_fini_locked(c);
594 qemu_mutex_unlock(&qjack_shutdown_lock);
597 static void qjack_fini_out(HWVoiceOut *hw)
599 QJackOut *jo = (QJackOut *)hw;
600 qjack_client_fini(&jo->c);
602 qemu_bh_delete(jo->c.shutdown_bh);
605 static void qjack_fini_in(HWVoiceIn *hw)
607 QJackIn *ji = (QJackIn *)hw;
608 qjack_client_fini(&ji->c);
610 qemu_bh_delete(ji->c.shutdown_bh);
613 static void qjack_enable_out(HWVoiceOut *hw, bool enable)
615 QJackOut *jo = (QJackOut *)hw;
616 jo->c.enabled = enable;
619 static void qjack_enable_in(HWVoiceIn *hw, bool enable)
621 QJackIn *ji = (QJackIn *)hw;
622 ji->c.enabled = enable;
625 #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
626 static int qjack_thread_creator(jack_native_thread_t *thread,
627 const pthread_attr_t *attr, void *(*function)(void *), void *arg)
629 int ret = pthread_create(thread, attr, function, arg);
630 if (ret != 0) {
631 return ret;
634 /* set the name of the thread */
635 pthread_setname_np(*thread, "jack-client");
637 return ret;
639 #endif
641 static void *qjack_init(Audiodev *dev)
643 assert(dev->driver == AUDIODEV_DRIVER_JACK);
644 return dev;
647 static void qjack_fini(void *opaque)
651 static struct audio_pcm_ops jack_pcm_ops = {
652 .init_out = qjack_init_out,
653 .fini_out = qjack_fini_out,
654 .write = qjack_write,
655 .buffer_get_free = audio_generic_buffer_get_free,
656 .run_buffer_out = audio_generic_run_buffer_out,
657 .enable_out = qjack_enable_out,
659 .init_in = qjack_init_in,
660 .fini_in = qjack_fini_in,
661 .read = qjack_read,
662 .run_buffer_in = audio_generic_run_buffer_in,
663 .enable_in = qjack_enable_in
666 static struct audio_driver jack_driver = {
667 .name = "jack",
668 .descr = "JACK Audio Connection Kit Client",
669 .init = qjack_init,
670 .fini = qjack_fini,
671 .pcm_ops = &jack_pcm_ops,
672 .can_be_default = 1,
673 .max_voices_out = INT_MAX,
674 .max_voices_in = INT_MAX,
675 .voice_size_out = sizeof(QJackOut),
676 .voice_size_in = sizeof(QJackIn)
679 static void qjack_error(const char *msg)
681 dolog("E: %s\n", msg);
684 static void qjack_info(const char *msg)
686 dolog("I: %s\n", msg);
689 static void register_audio_jack(void)
691 qemu_mutex_init(&qjack_shutdown_lock);
692 audio_driver_register(&jack_driver);
693 #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
694 jack_set_thread_creator(qjack_thread_creator);
695 #endif
696 jack_set_error_function(qjack_error);
697 jack_set_info_function(qjack_info);
699 type_init(register_audio_jack);