hw/block/pflash_cfi: Replace DPRINTF with trace events
[qemu/ar7.git] / audio / jackaudio.c
blob3031c4e29bd457252cb5284b572aec955630551c
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 "qemu-common.h"
30 #include "audio.h"
32 #define AUDIO_CAP "jack"
33 #include "audio_int.h"
35 #include <jack/jack.h>
36 #include <jack/thread.h>
38 struct QJack;
40 typedef enum QJackState {
41 QJACK_STATE_DISCONNECTED,
42 QJACK_STATE_RUNNING,
43 QJACK_STATE_SHUTDOWN
45 QJackState;
47 typedef struct QJackBuffer {
48 int channels;
49 int frames;
50 uint32_t used;
51 int rptr, wptr;
52 float **data;
54 QJackBuffer;
56 typedef struct QJackClient {
57 AudiodevJackPerDirectionOptions *opt;
59 bool out;
60 bool enabled;
61 bool connect_ports;
62 int packets;
64 QJackState state;
65 jack_client_t *client;
66 jack_nframes_t freq;
67 QEMUBH *shutdown_bh;
69 struct QJack *j;
70 int nchannels;
71 int buffersize;
72 jack_port_t **port;
73 QJackBuffer fifo;
75 QJackClient;
77 typedef struct QJackOut {
78 HWVoiceOut hw;
79 QJackClient c;
81 QJackOut;
83 typedef struct QJackIn {
84 HWVoiceIn hw;
85 QJackClient c;
87 QJackIn;
89 static int qjack_client_init(QJackClient *c);
90 static void qjack_client_connect_ports(QJackClient *c);
91 static void qjack_client_fini(QJackClient *c);
92 static QemuMutex qjack_shutdown_lock;
94 static void qjack_buffer_create(QJackBuffer *buffer, int channels, int frames)
96 buffer->channels = channels;
97 buffer->frames = frames;
98 buffer->used = 0;
99 buffer->rptr = 0;
100 buffer->wptr = 0;
101 buffer->data = g_malloc(channels * sizeof(float *));
102 for (int i = 0; i < channels; ++i) {
103 buffer->data[i] = g_malloc(frames * sizeof(float));
107 static void qjack_buffer_clear(QJackBuffer *buffer)
109 assert(buffer->data);
110 qatomic_store_release(&buffer->used, 0);
111 buffer->rptr = 0;
112 buffer->wptr = 0;
115 static void qjack_buffer_free(QJackBuffer *buffer)
117 if (!buffer->data) {
118 return;
121 for (int i = 0; i < buffer->channels; ++i) {
122 g_free(buffer->data[i]);
125 g_free(buffer->data);
126 buffer->data = NULL;
129 /* write PCM interleaved */
130 static int qjack_buffer_write(QJackBuffer *buffer, float *data, int size)
132 assert(buffer->data);
133 const int samples = size / sizeof(float);
134 int frames = samples / buffer->channels;
135 const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
137 if (frames > avail) {
138 frames = avail;
141 int copy = frames;
142 int wptr = buffer->wptr;
144 while (copy) {
146 for (int c = 0; c < buffer->channels; ++c) {
147 buffer->data[c][wptr] = *data++;
150 if (++wptr == buffer->frames) {
151 wptr = 0;
154 --copy;
157 buffer->wptr = wptr;
159 qatomic_add(&buffer->used, frames);
160 return frames * buffer->channels * sizeof(float);
163 /* write PCM linear */
164 static int qjack_buffer_write_l(QJackBuffer *buffer, float **dest, int frames)
166 assert(buffer->data);
167 const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
168 int wptr = buffer->wptr;
170 if (frames > avail) {
171 frames = avail;
174 int right = buffer->frames - wptr;
175 if (right > frames) {
176 right = frames;
179 const int left = frames - right;
180 for (int c = 0; c < buffer->channels; ++c) {
181 memcpy(buffer->data[c] + wptr, dest[c] , right * sizeof(float));
182 memcpy(buffer->data[c] , dest[c] + right, left * sizeof(float));
185 wptr += frames;
186 if (wptr >= buffer->frames) {
187 wptr -= buffer->frames;
189 buffer->wptr = wptr;
191 qatomic_add(&buffer->used, frames);
192 return frames;
195 /* read PCM interleaved */
196 static int qjack_buffer_read(QJackBuffer *buffer, float *dest, int size)
198 assert(buffer->data);
199 const int samples = size / sizeof(float);
200 int frames = samples / buffer->channels;
201 const int avail = qatomic_load_acquire(&buffer->used);
203 if (frames > avail) {
204 frames = avail;
207 int copy = frames;
208 int rptr = buffer->rptr;
210 while (copy) {
212 for (int c = 0; c < buffer->channels; ++c) {
213 *dest++ = buffer->data[c][rptr];
216 if (++rptr == buffer->frames) {
217 rptr = 0;
220 --copy;
223 buffer->rptr = rptr;
225 qatomic_sub(&buffer->used, frames);
226 return frames * buffer->channels * sizeof(float);
229 /* read PCM linear */
230 static int qjack_buffer_read_l(QJackBuffer *buffer, float **dest, int frames)
232 assert(buffer->data);
233 int copy = frames;
234 const int used = qatomic_load_acquire(&buffer->used);
235 int rptr = buffer->rptr;
237 if (copy > used) {
238 copy = used;
241 int right = buffer->frames - rptr;
242 if (right > copy) {
243 right = copy;
246 const int left = copy - right;
247 for (int c = 0; c < buffer->channels; ++c) {
248 memcpy(dest[c] , buffer->data[c] + rptr, right * sizeof(float));
249 memcpy(dest[c] + right, buffer->data[c] , left * sizeof(float));
252 rptr += copy;
253 if (rptr >= buffer->frames) {
254 rptr -= buffer->frames;
256 buffer->rptr = rptr;
258 qatomic_sub(&buffer->used, copy);
259 return copy;
262 static int qjack_process(jack_nframes_t nframes, void *arg)
264 QJackClient *c = (QJackClient *)arg;
266 if (c->state != QJACK_STATE_RUNNING) {
267 return 0;
270 /* get the buffers for the ports */
271 float *buffers[c->nchannels];
272 for (int i = 0; i < c->nchannels; ++i) {
273 buffers[i] = jack_port_get_buffer(c->port[i], nframes);
276 if (c->out) {
277 if (likely(c->enabled)) {
278 qjack_buffer_read_l(&c->fifo, buffers, nframes);
279 } else {
280 for (int i = 0; i < c->nchannels; ++i) {
281 memset(buffers[i], 0, nframes * sizeof(float));
284 } else {
285 if (likely(c->enabled)) {
286 qjack_buffer_write_l(&c->fifo, buffers, nframes);
290 return 0;
293 static void qjack_port_registration(jack_port_id_t port, int reg, void *arg)
295 if (reg) {
296 QJackClient *c = (QJackClient *)arg;
297 c->connect_ports = true;
301 static int qjack_xrun(void *arg)
303 QJackClient *c = (QJackClient *)arg;
304 if (c->state != QJACK_STATE_RUNNING) {
305 return 0;
308 qjack_buffer_clear(&c->fifo);
309 return 0;
312 static void qjack_shutdown_bh(void *opaque)
314 QJackClient *c = (QJackClient *)opaque;
315 qjack_client_fini(c);
318 static void qjack_shutdown(void *arg)
320 QJackClient *c = (QJackClient *)arg;
321 c->state = QJACK_STATE_SHUTDOWN;
322 qemu_bh_schedule(c->shutdown_bh);
325 static void qjack_client_recover(QJackClient *c)
327 if (c->state != QJACK_STATE_DISCONNECTED) {
328 return;
331 /* packets is used simply to throttle this */
332 if (c->packets % 100 == 0) {
334 /* if enabled then attempt to recover */
335 if (c->enabled) {
336 dolog("attempting to reconnect to server\n");
337 qjack_client_init(c);
342 static size_t qjack_write(HWVoiceOut *hw, void *buf, size_t len)
344 QJackOut *jo = (QJackOut *)hw;
345 ++jo->c.packets;
347 if (jo->c.state != QJACK_STATE_RUNNING) {
348 qjack_client_recover(&jo->c);
349 return len;
352 qjack_client_connect_ports(&jo->c);
353 return qjack_buffer_write(&jo->c.fifo, buf, len);
356 static size_t qjack_read(HWVoiceIn *hw, void *buf, size_t len)
358 QJackIn *ji = (QJackIn *)hw;
359 ++ji->c.packets;
361 if (ji->c.state != QJACK_STATE_RUNNING) {
362 qjack_client_recover(&ji->c);
363 return len;
366 qjack_client_connect_ports(&ji->c);
367 return qjack_buffer_read(&ji->c.fifo, buf, len);
370 static void qjack_client_connect_ports(QJackClient *c)
372 if (!c->connect_ports || !c->opt->connect_ports) {
373 return;
376 c->connect_ports = false;
377 const char **ports;
378 ports = jack_get_ports(c->client, c->opt->connect_ports, NULL,
379 c->out ? JackPortIsInput : JackPortIsOutput);
381 if (!ports) {
382 return;
385 for (int i = 0; i < c->nchannels && ports[i]; ++i) {
386 const char *p = jack_port_name(c->port[i]);
387 if (jack_port_connected_to(c->port[i], ports[i])) {
388 continue;
391 if (c->out) {
392 dolog("connect %s -> %s\n", p, ports[i]);
393 jack_connect(c->client, p, ports[i]);
394 } else {
395 dolog("connect %s -> %s\n", ports[i], p);
396 jack_connect(c->client, ports[i], p);
401 static int qjack_client_init(QJackClient *c)
403 jack_status_t status;
404 char client_name[jack_client_name_size()];
405 jack_options_t options = JackNullOption;
407 if (c->state == QJACK_STATE_RUNNING) {
408 return 0;
411 c->connect_ports = true;
413 snprintf(client_name, sizeof(client_name), "%s-%s",
414 c->out ? "out" : "in",
415 c->opt->client_name ? c->opt->client_name : qemu_get_vm_name());
417 if (c->opt->exact_name) {
418 options |= JackUseExactName;
421 if (!c->opt->start_server) {
422 options |= JackNoStartServer;
425 if (c->opt->server_name) {
426 options |= JackServerName;
429 c->client = jack_client_open(client_name, options, &status,
430 c->opt->server_name);
432 if (c->client == NULL) {
433 dolog("jack_client_open failed: status = 0x%2.0x\n", status);
434 if (status & JackServerFailed) {
435 dolog("unable to connect to JACK server\n");
437 return -1;
440 c->freq = jack_get_sample_rate(c->client);
442 if (status & JackServerStarted) {
443 dolog("JACK server started\n");
446 if (status & JackNameNotUnique) {
447 dolog("JACK unique name assigned %s\n",
448 jack_get_client_name(c->client));
451 jack_set_process_callback(c->client, qjack_process , c);
452 jack_set_port_registration_callback(c->client, qjack_port_registration, c);
453 jack_set_xrun_callback(c->client, qjack_xrun, c);
454 jack_on_shutdown(c->client, qjack_shutdown, c);
456 /* allocate and register the ports */
457 c->port = g_malloc(sizeof(jack_port_t *) * c->nchannels);
458 for (int i = 0; i < c->nchannels; ++i) {
460 char port_name[16];
461 snprintf(
462 port_name,
463 sizeof(port_name),
464 c->out ? "output %d" : "input %d",
467 c->port[i] = jack_port_register(
468 c->client,
469 port_name,
470 JACK_DEFAULT_AUDIO_TYPE,
471 c->out ? JackPortIsOutput : JackPortIsInput,
475 /* activate the session */
476 jack_activate(c->client);
477 c->buffersize = jack_get_buffer_size(c->client);
480 * ensure the buffersize is no smaller then 512 samples, some (all?) qemu
481 * virtual devices do not work correctly otherwise
483 if (c->buffersize < 512) {
484 c->buffersize = 512;
487 /* create a 2 period buffer */
488 qjack_buffer_create(&c->fifo, c->nchannels, c->buffersize * 2);
490 qjack_client_connect_ports(c);
491 c->state = QJACK_STATE_RUNNING;
492 return 0;
495 static int qjack_init_out(HWVoiceOut *hw, struct audsettings *as,
496 void *drv_opaque)
498 QJackOut *jo = (QJackOut *)hw;
499 Audiodev *dev = (Audiodev *)drv_opaque;
501 jo->c.out = true;
502 jo->c.enabled = false;
503 jo->c.nchannels = as->nchannels;
504 jo->c.opt = dev->u.jack.out;
506 jo->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &jo->c);
508 int ret = qjack_client_init(&jo->c);
509 if (ret != 0) {
510 qemu_bh_delete(jo->c.shutdown_bh);
511 return ret;
514 /* report the buffer size to qemu */
515 hw->samples = jo->c.buffersize;
517 /* report the audio format we support */
518 struct audsettings os = {
519 .freq = jo->c.freq,
520 .nchannels = jo->c.nchannels,
521 .fmt = AUDIO_FORMAT_F32,
522 .endianness = 0
524 audio_pcm_init_info(&hw->info, &os);
526 dolog("JACK output configured for %dHz (%d samples)\n",
527 jo->c.freq, jo->c.buffersize);
529 return 0;
532 static int qjack_init_in(HWVoiceIn *hw, struct audsettings *as,
533 void *drv_opaque)
535 QJackIn *ji = (QJackIn *)hw;
536 Audiodev *dev = (Audiodev *)drv_opaque;
538 ji->c.out = false;
539 ji->c.enabled = false;
540 ji->c.nchannels = as->nchannels;
541 ji->c.opt = dev->u.jack.in;
543 ji->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &ji->c);
545 int ret = qjack_client_init(&ji->c);
546 if (ret != 0) {
547 qemu_bh_delete(ji->c.shutdown_bh);
548 return ret;
551 /* report the buffer size to qemu */
552 hw->samples = ji->c.buffersize;
554 /* report the audio format we support */
555 struct audsettings is = {
556 .freq = ji->c.freq,
557 .nchannels = ji->c.nchannels,
558 .fmt = AUDIO_FORMAT_F32,
559 .endianness = 0
561 audio_pcm_init_info(&hw->info, &is);
563 dolog("JACK input configured for %dHz (%d samples)\n",
564 ji->c.freq, ji->c.buffersize);
566 return 0;
569 static void qjack_client_fini_locked(QJackClient *c)
571 switch (c->state) {
572 case QJACK_STATE_RUNNING:
573 jack_deactivate(c->client);
574 /* fallthrough */
576 case QJACK_STATE_SHUTDOWN:
577 jack_client_close(c->client);
578 c->client = NULL;
580 qjack_buffer_free(&c->fifo);
581 g_free(c->port);
583 c->state = QJACK_STATE_DISCONNECTED;
584 /* fallthrough */
586 case QJACK_STATE_DISCONNECTED:
587 break;
591 static void qjack_client_fini(QJackClient *c)
593 qemu_mutex_lock(&qjack_shutdown_lock);
594 qjack_client_fini_locked(c);
595 qemu_mutex_unlock(&qjack_shutdown_lock);
598 static void qjack_fini_out(HWVoiceOut *hw)
600 QJackOut *jo = (QJackOut *)hw;
601 qjack_client_fini(&jo->c);
603 qemu_bh_delete(jo->c.shutdown_bh);
606 static void qjack_fini_in(HWVoiceIn *hw)
608 QJackIn *ji = (QJackIn *)hw;
609 qjack_client_fini(&ji->c);
611 qemu_bh_delete(ji->c.shutdown_bh);
614 static void qjack_enable_out(HWVoiceOut *hw, bool enable)
616 QJackOut *jo = (QJackOut *)hw;
617 jo->c.enabled = enable;
620 static void qjack_enable_in(HWVoiceIn *hw, bool enable)
622 QJackIn *ji = (QJackIn *)hw;
623 ji->c.enabled = enable;
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;
640 static void *qjack_init(Audiodev *dev)
642 assert(dev->driver == AUDIODEV_DRIVER_JACK);
643 return dev;
646 static void qjack_fini(void *opaque)
650 static struct audio_pcm_ops jack_pcm_ops = {
651 .init_out = qjack_init_out,
652 .fini_out = qjack_fini_out,
653 .write = qjack_write,
654 .run_buffer_out = audio_generic_run_buffer_out,
655 .enable_out = qjack_enable_out,
657 .init_in = qjack_init_in,
658 .fini_in = qjack_fini_in,
659 .read = qjack_read,
660 .run_buffer_in = audio_generic_run_buffer_in,
661 .enable_in = qjack_enable_in
664 static struct audio_driver jack_driver = {
665 .name = "jack",
666 .descr = "JACK Audio Connection Kit Client",
667 .init = qjack_init,
668 .fini = qjack_fini,
669 .pcm_ops = &jack_pcm_ops,
670 .can_be_default = 1,
671 .max_voices_out = INT_MAX,
672 .max_voices_in = INT_MAX,
673 .voice_size_out = sizeof(QJackOut),
674 .voice_size_in = sizeof(QJackIn)
677 static void qjack_error(const char *msg)
679 dolog("E: %s\n", msg);
682 static void qjack_info(const char *msg)
684 dolog("I: %s\n", msg);
687 static void register_audio_jack(void)
689 qemu_mutex_init(&qjack_shutdown_lock);
690 audio_driver_register(&jack_driver);
691 jack_set_thread_creator(qjack_thread_creator);
692 jack_set_error_function(qjack_error);
693 jack_set_info_function(qjack_info);
695 type_init(register_audio_jack);