12e445703SGeoffrey McRae /*
22e445703SGeoffrey McRae * QEMU JACK Audio Connection Kit Client
32e445703SGeoffrey McRae *
42e445703SGeoffrey McRae * Copyright (c) 2020 Geoffrey McRae (gnif)
52e445703SGeoffrey McRae *
62e445703SGeoffrey McRae * Permission is hereby granted, free of charge, to any person obtaining a copy
72e445703SGeoffrey McRae * of this software and associated documentation files (the "Software"), to deal
82e445703SGeoffrey McRae * in the Software without restriction, including without limitation the rights
92e445703SGeoffrey McRae * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102e445703SGeoffrey McRae * copies of the Software, and to permit persons to whom the Software is
112e445703SGeoffrey McRae * furnished to do so, subject to the following conditions:
122e445703SGeoffrey McRae *
132e445703SGeoffrey McRae * The above copyright notice and this permission notice shall be included in
142e445703SGeoffrey McRae * all copies or substantial portions of the Software.
152e445703SGeoffrey McRae *
162e445703SGeoffrey McRae * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
172e445703SGeoffrey McRae * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
182e445703SGeoffrey McRae * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
192e445703SGeoffrey McRae * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
202e445703SGeoffrey McRae * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
212e445703SGeoffrey McRae * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
222e445703SGeoffrey McRae * THE SOFTWARE.
232e445703SGeoffrey McRae */
242e445703SGeoffrey McRae
252e445703SGeoffrey McRae #include "qemu/osdep.h"
262e445703SGeoffrey McRae #include "qemu/module.h"
272e445703SGeoffrey McRae #include "qemu/atomic.h"
28a6e03739SGeoffrey McRae #include "qemu/main-loop.h"
292e445703SGeoffrey McRae #include "audio.h"
302e445703SGeoffrey McRae
312e445703SGeoffrey McRae #define AUDIO_CAP "jack"
322e445703SGeoffrey McRae #include "audio_int.h"
332e445703SGeoffrey McRae
342e445703SGeoffrey McRae #include <jack/jack.h>
352e445703SGeoffrey McRae #include <jack/thread.h>
362e445703SGeoffrey McRae
372e445703SGeoffrey McRae struct QJack;
382e445703SGeoffrey McRae
392e445703SGeoffrey McRae typedef enum QJackState {
402e445703SGeoffrey McRae QJACK_STATE_DISCONNECTED,
412e445703SGeoffrey McRae QJACK_STATE_RUNNING,
422e445703SGeoffrey McRae QJACK_STATE_SHUTDOWN
432e445703SGeoffrey McRae }
442e445703SGeoffrey McRae QJackState;
452e445703SGeoffrey McRae
462e445703SGeoffrey McRae typedef struct QJackBuffer {
472e445703SGeoffrey McRae int channels;
482e445703SGeoffrey McRae int frames;
492e445703SGeoffrey McRae uint32_t used;
502e445703SGeoffrey McRae int rptr, wptr;
512e445703SGeoffrey McRae float **data;
522e445703SGeoffrey McRae }
532e445703SGeoffrey McRae QJackBuffer;
542e445703SGeoffrey McRae
552e445703SGeoffrey McRae typedef struct QJackClient {
562e445703SGeoffrey McRae AudiodevJackPerDirectionOptions *opt;
572e445703SGeoffrey McRae
582e445703SGeoffrey McRae bool out;
5981e0efb2SGeoffrey McRae bool enabled;
602e445703SGeoffrey McRae bool connect_ports;
612e445703SGeoffrey McRae int packets;
622e445703SGeoffrey McRae
632e445703SGeoffrey McRae QJackState state;
642e445703SGeoffrey McRae jack_client_t *client;
652e445703SGeoffrey McRae jack_nframes_t freq;
66a6e03739SGeoffrey McRae QEMUBH *shutdown_bh;
672e445703SGeoffrey McRae
682e445703SGeoffrey McRae struct QJack *j;
692e445703SGeoffrey McRae int nchannels;
702e445703SGeoffrey McRae int buffersize;
712e445703SGeoffrey McRae jack_port_t **port;
722e445703SGeoffrey McRae QJackBuffer fifo;
7307ffc4b9SPeter Maydell
7407ffc4b9SPeter Maydell /* Used as workspace by qjack_process() */
7507ffc4b9SPeter Maydell float **process_buffers;
762e445703SGeoffrey McRae }
772e445703SGeoffrey McRae QJackClient;
782e445703SGeoffrey McRae
792e445703SGeoffrey McRae typedef struct QJackOut {
802e445703SGeoffrey McRae HWVoiceOut hw;
812e445703SGeoffrey McRae QJackClient c;
822e445703SGeoffrey McRae }
832e445703SGeoffrey McRae QJackOut;
842e445703SGeoffrey McRae
852e445703SGeoffrey McRae typedef struct QJackIn {
862e445703SGeoffrey McRae HWVoiceIn hw;
872e445703SGeoffrey McRae QJackClient c;
882e445703SGeoffrey McRae }
892e445703SGeoffrey McRae QJackIn;
902e445703SGeoffrey McRae
912e445703SGeoffrey McRae static int qjack_client_init(QJackClient *c);
922e445703SGeoffrey McRae static void qjack_client_connect_ports(QJackClient *c);
932e445703SGeoffrey McRae static void qjack_client_fini(QJackClient *c);
94a6e03739SGeoffrey McRae static QemuMutex qjack_shutdown_lock;
952e445703SGeoffrey McRae
qjack_buffer_create(QJackBuffer * buffer,int channels,int frames)962e445703SGeoffrey McRae static void qjack_buffer_create(QJackBuffer *buffer, int channels, int frames)
972e445703SGeoffrey McRae {
982e445703SGeoffrey McRae buffer->channels = channels;
992e445703SGeoffrey McRae buffer->frames = frames;
1002e445703SGeoffrey McRae buffer->used = 0;
1012e445703SGeoffrey McRae buffer->rptr = 0;
1022e445703SGeoffrey McRae buffer->wptr = 0;
103b21e2380SMarkus Armbruster buffer->data = g_new(float *, channels);
1042e445703SGeoffrey McRae for (int i = 0; i < channels; ++i) {
105b21e2380SMarkus Armbruster buffer->data[i] = g_new(float, frames);
1062e445703SGeoffrey McRae }
1072e445703SGeoffrey McRae }
1082e445703SGeoffrey McRae
qjack_buffer_clear(QJackBuffer * buffer)1092e445703SGeoffrey McRae static void qjack_buffer_clear(QJackBuffer *buffer)
1102e445703SGeoffrey McRae {
1112e445703SGeoffrey McRae assert(buffer->data);
112d73415a3SStefan Hajnoczi qatomic_store_release(&buffer->used, 0);
1132e445703SGeoffrey McRae buffer->rptr = 0;
1142e445703SGeoffrey McRae buffer->wptr = 0;
1152e445703SGeoffrey McRae }
1162e445703SGeoffrey McRae
qjack_buffer_free(QJackBuffer * buffer)1172e445703SGeoffrey McRae static void qjack_buffer_free(QJackBuffer *buffer)
1182e445703SGeoffrey McRae {
1192e445703SGeoffrey McRae if (!buffer->data) {
1202e445703SGeoffrey McRae return;
1212e445703SGeoffrey McRae }
1222e445703SGeoffrey McRae
1232e445703SGeoffrey McRae for (int i = 0; i < buffer->channels; ++i) {
1242e445703SGeoffrey McRae g_free(buffer->data[i]);
1252e445703SGeoffrey McRae }
1262e445703SGeoffrey McRae
1272e445703SGeoffrey McRae g_free(buffer->data);
1282e445703SGeoffrey McRae buffer->data = NULL;
1292e445703SGeoffrey McRae }
1302e445703SGeoffrey McRae
1312e445703SGeoffrey McRae /* write PCM interleaved */
qjack_buffer_write(QJackBuffer * buffer,float * data,int size)1322e445703SGeoffrey McRae static int qjack_buffer_write(QJackBuffer *buffer, float *data, int size)
1332e445703SGeoffrey McRae {
1342e445703SGeoffrey McRae assert(buffer->data);
1352e445703SGeoffrey McRae const int samples = size / sizeof(float);
1362e445703SGeoffrey McRae int frames = samples / buffer->channels;
137d73415a3SStefan Hajnoczi const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
1382e445703SGeoffrey McRae
1392e445703SGeoffrey McRae if (frames > avail) {
1402e445703SGeoffrey McRae frames = avail;
1412e445703SGeoffrey McRae }
1422e445703SGeoffrey McRae
1432e445703SGeoffrey McRae int copy = frames;
1442e445703SGeoffrey McRae int wptr = buffer->wptr;
1452e445703SGeoffrey McRae
1462e445703SGeoffrey McRae while (copy) {
1472e445703SGeoffrey McRae
1482e445703SGeoffrey McRae for (int c = 0; c < buffer->channels; ++c) {
1492e445703SGeoffrey McRae buffer->data[c][wptr] = *data++;
1502e445703SGeoffrey McRae }
1512e445703SGeoffrey McRae
1522e445703SGeoffrey McRae if (++wptr == buffer->frames) {
1532e445703SGeoffrey McRae wptr = 0;
1542e445703SGeoffrey McRae }
1552e445703SGeoffrey McRae
1562e445703SGeoffrey McRae --copy;
1572e445703SGeoffrey McRae }
1582e445703SGeoffrey McRae
1592e445703SGeoffrey McRae buffer->wptr = wptr;
1602e445703SGeoffrey McRae
161d73415a3SStefan Hajnoczi qatomic_add(&buffer->used, frames);
1622e445703SGeoffrey McRae return frames * buffer->channels * sizeof(float);
1632e445703SGeoffrey McRae };
1642e445703SGeoffrey McRae
1652e445703SGeoffrey McRae /* write PCM linear */
qjack_buffer_write_l(QJackBuffer * buffer,float ** dest,int frames)1662e445703SGeoffrey McRae static int qjack_buffer_write_l(QJackBuffer *buffer, float **dest, int frames)
1672e445703SGeoffrey McRae {
1682e445703SGeoffrey McRae assert(buffer->data);
169d73415a3SStefan Hajnoczi const int avail = buffer->frames - qatomic_load_acquire(&buffer->used);
1702e445703SGeoffrey McRae int wptr = buffer->wptr;
1712e445703SGeoffrey McRae
1722e445703SGeoffrey McRae if (frames > avail) {
1732e445703SGeoffrey McRae frames = avail;
1742e445703SGeoffrey McRae }
1752e445703SGeoffrey McRae
1762e445703SGeoffrey McRae int right = buffer->frames - wptr;
1772e445703SGeoffrey McRae if (right > frames) {
1782e445703SGeoffrey McRae right = frames;
1792e445703SGeoffrey McRae }
1802e445703SGeoffrey McRae
1812e445703SGeoffrey McRae const int left = frames - right;
1822e445703SGeoffrey McRae for (int c = 0; c < buffer->channels; ++c) {
1832e445703SGeoffrey McRae memcpy(buffer->data[c] + wptr, dest[c] , right * sizeof(float));
1842e445703SGeoffrey McRae memcpy(buffer->data[c] , dest[c] + right, left * sizeof(float));
1852e445703SGeoffrey McRae }
1862e445703SGeoffrey McRae
1872e445703SGeoffrey McRae wptr += frames;
1882e445703SGeoffrey McRae if (wptr >= buffer->frames) {
1892e445703SGeoffrey McRae wptr -= buffer->frames;
1902e445703SGeoffrey McRae }
1912e445703SGeoffrey McRae buffer->wptr = wptr;
1922e445703SGeoffrey McRae
193d73415a3SStefan Hajnoczi qatomic_add(&buffer->used, frames);
1942e445703SGeoffrey McRae return frames;
1952e445703SGeoffrey McRae }
1962e445703SGeoffrey McRae
1972e445703SGeoffrey McRae /* read PCM interleaved */
qjack_buffer_read(QJackBuffer * buffer,float * dest,int size)1982e445703SGeoffrey McRae static int qjack_buffer_read(QJackBuffer *buffer, float *dest, int size)
1992e445703SGeoffrey McRae {
2002e445703SGeoffrey McRae assert(buffer->data);
2012e445703SGeoffrey McRae const int samples = size / sizeof(float);
2022e445703SGeoffrey McRae int frames = samples / buffer->channels;
203d73415a3SStefan Hajnoczi const int avail = qatomic_load_acquire(&buffer->used);
2042e445703SGeoffrey McRae
2052e445703SGeoffrey McRae if (frames > avail) {
2062e445703SGeoffrey McRae frames = avail;
2072e445703SGeoffrey McRae }
2082e445703SGeoffrey McRae
2092e445703SGeoffrey McRae int copy = frames;
2102e445703SGeoffrey McRae int rptr = buffer->rptr;
2112e445703SGeoffrey McRae
2122e445703SGeoffrey McRae while (copy) {
2132e445703SGeoffrey McRae
2142e445703SGeoffrey McRae for (int c = 0; c < buffer->channels; ++c) {
2152e445703SGeoffrey McRae *dest++ = buffer->data[c][rptr];
2162e445703SGeoffrey McRae }
2172e445703SGeoffrey McRae
2182e445703SGeoffrey McRae if (++rptr == buffer->frames) {
2192e445703SGeoffrey McRae rptr = 0;
2202e445703SGeoffrey McRae }
2212e445703SGeoffrey McRae
2222e445703SGeoffrey McRae --copy;
2232e445703SGeoffrey McRae }
2242e445703SGeoffrey McRae
2252e445703SGeoffrey McRae buffer->rptr = rptr;
2262e445703SGeoffrey McRae
227d73415a3SStefan Hajnoczi qatomic_sub(&buffer->used, frames);
2282e445703SGeoffrey McRae return frames * buffer->channels * sizeof(float);
2292e445703SGeoffrey McRae }
2302e445703SGeoffrey McRae
2312e445703SGeoffrey McRae /* read PCM linear */
qjack_buffer_read_l(QJackBuffer * buffer,float ** dest,int frames)2322e445703SGeoffrey McRae static int qjack_buffer_read_l(QJackBuffer *buffer, float **dest, int frames)
2332e445703SGeoffrey McRae {
2342e445703SGeoffrey McRae assert(buffer->data);
2352e445703SGeoffrey McRae int copy = frames;
236d73415a3SStefan Hajnoczi const int used = qatomic_load_acquire(&buffer->used);
2372e445703SGeoffrey McRae int rptr = buffer->rptr;
2382e445703SGeoffrey McRae
2392e445703SGeoffrey McRae if (copy > used) {
2402e445703SGeoffrey McRae copy = used;
2412e445703SGeoffrey McRae }
2422e445703SGeoffrey McRae
2432e445703SGeoffrey McRae int right = buffer->frames - rptr;
2442e445703SGeoffrey McRae if (right > copy) {
2452e445703SGeoffrey McRae right = copy;
2462e445703SGeoffrey McRae }
2472e445703SGeoffrey McRae
2482e445703SGeoffrey McRae const int left = copy - right;
2492e445703SGeoffrey McRae for (int c = 0; c < buffer->channels; ++c) {
2502e445703SGeoffrey McRae memcpy(dest[c] , buffer->data[c] + rptr, right * sizeof(float));
2512e445703SGeoffrey McRae memcpy(dest[c] + right, buffer->data[c] , left * sizeof(float));
2522e445703SGeoffrey McRae }
2532e445703SGeoffrey McRae
2542e445703SGeoffrey McRae rptr += copy;
2552e445703SGeoffrey McRae if (rptr >= buffer->frames) {
2562e445703SGeoffrey McRae rptr -= buffer->frames;
2572e445703SGeoffrey McRae }
2582e445703SGeoffrey McRae buffer->rptr = rptr;
2592e445703SGeoffrey McRae
260d73415a3SStefan Hajnoczi qatomic_sub(&buffer->used, copy);
2612e445703SGeoffrey McRae return copy;
2622e445703SGeoffrey McRae }
2632e445703SGeoffrey McRae
qjack_process(jack_nframes_t nframes,void * arg)2642e445703SGeoffrey McRae static int qjack_process(jack_nframes_t nframes, void *arg)
2652e445703SGeoffrey McRae {
2662e445703SGeoffrey McRae QJackClient *c = (QJackClient *)arg;
2672e445703SGeoffrey McRae
2682e445703SGeoffrey McRae if (c->state != QJACK_STATE_RUNNING) {
2692e445703SGeoffrey McRae return 0;
2702e445703SGeoffrey McRae }
2712e445703SGeoffrey McRae
2722e445703SGeoffrey McRae /* get the buffers for the ports */
2732e445703SGeoffrey McRae for (int i = 0; i < c->nchannels; ++i) {
27407ffc4b9SPeter Maydell c->process_buffers[i] = jack_port_get_buffer(c->port[i], nframes);
2752e445703SGeoffrey McRae }
2762e445703SGeoffrey McRae
2772e445703SGeoffrey McRae if (c->out) {
27881e0efb2SGeoffrey McRae if (likely(c->enabled)) {
27907ffc4b9SPeter Maydell qjack_buffer_read_l(&c->fifo, c->process_buffers, nframes);
2802e445703SGeoffrey McRae } else {
28181e0efb2SGeoffrey McRae for (int i = 0; i < c->nchannels; ++i) {
28207ffc4b9SPeter Maydell memset(c->process_buffers[i], 0, nframes * sizeof(float));
28381e0efb2SGeoffrey McRae }
28481e0efb2SGeoffrey McRae }
28581e0efb2SGeoffrey McRae } else {
28681e0efb2SGeoffrey McRae if (likely(c->enabled)) {
28707ffc4b9SPeter Maydell qjack_buffer_write_l(&c->fifo, c->process_buffers, nframes);
2882e445703SGeoffrey McRae }
28981e0efb2SGeoffrey McRae }
2902e445703SGeoffrey McRae
2912e445703SGeoffrey McRae return 0;
2922e445703SGeoffrey McRae }
2932e445703SGeoffrey McRae
qjack_port_registration(jack_port_id_t port,int reg,void * arg)2942e445703SGeoffrey McRae static void qjack_port_registration(jack_port_id_t port, int reg, void *arg)
2952e445703SGeoffrey McRae {
2962e445703SGeoffrey McRae if (reg) {
2972e445703SGeoffrey McRae QJackClient *c = (QJackClient *)arg;
2982e445703SGeoffrey McRae c->connect_ports = true;
2992e445703SGeoffrey McRae }
3002e445703SGeoffrey McRae }
3012e445703SGeoffrey McRae
qjack_xrun(void * arg)3022e445703SGeoffrey McRae static int qjack_xrun(void *arg)
3032e445703SGeoffrey McRae {
3042e445703SGeoffrey McRae QJackClient *c = (QJackClient *)arg;
3052e445703SGeoffrey McRae if (c->state != QJACK_STATE_RUNNING) {
3062e445703SGeoffrey McRae return 0;
3072e445703SGeoffrey McRae }
3082e445703SGeoffrey McRae
3092e445703SGeoffrey McRae qjack_buffer_clear(&c->fifo);
3102e445703SGeoffrey McRae return 0;
3112e445703SGeoffrey McRae }
3122e445703SGeoffrey McRae
qjack_shutdown_bh(void * opaque)313a6e03739SGeoffrey McRae static void qjack_shutdown_bh(void *opaque)
314a6e03739SGeoffrey McRae {
315a6e03739SGeoffrey McRae QJackClient *c = (QJackClient *)opaque;
316a6e03739SGeoffrey McRae qjack_client_fini(c);
317a6e03739SGeoffrey McRae }
318a6e03739SGeoffrey McRae
qjack_shutdown(void * arg)3192e445703SGeoffrey McRae static void qjack_shutdown(void *arg)
3202e445703SGeoffrey McRae {
3212e445703SGeoffrey McRae QJackClient *c = (QJackClient *)arg;
3222e445703SGeoffrey McRae c->state = QJACK_STATE_SHUTDOWN;
323a6e03739SGeoffrey McRae qemu_bh_schedule(c->shutdown_bh);
3242e445703SGeoffrey McRae }
3252e445703SGeoffrey McRae
qjack_client_recover(QJackClient * c)3262e445703SGeoffrey McRae static void qjack_client_recover(QJackClient *c)
3272e445703SGeoffrey McRae {
328a6e03739SGeoffrey McRae if (c->state != QJACK_STATE_DISCONNECTED) {
329a6e03739SGeoffrey McRae return;
3302e445703SGeoffrey McRae }
3312e445703SGeoffrey McRae
3322e445703SGeoffrey McRae /* packets is used simply to throttle this */
333a6e03739SGeoffrey McRae if (c->packets % 100 == 0) {
3342e445703SGeoffrey McRae
33581e0efb2SGeoffrey McRae /* if enabled then attempt to recover */
33681e0efb2SGeoffrey McRae if (c->enabled) {
3372e445703SGeoffrey McRae dolog("attempting to reconnect to server\n");
3382e445703SGeoffrey McRae qjack_client_init(c);
3392e445703SGeoffrey McRae }
3402e445703SGeoffrey McRae }
3412e445703SGeoffrey McRae }
3422e445703SGeoffrey McRae
qjack_write(HWVoiceOut * hw,void * buf,size_t len)3432e445703SGeoffrey McRae static size_t qjack_write(HWVoiceOut *hw, void *buf, size_t len)
3442e445703SGeoffrey McRae {
3452e445703SGeoffrey McRae QJackOut *jo = (QJackOut *)hw;
3462e445703SGeoffrey McRae ++jo->c.packets;
3472e445703SGeoffrey McRae
3482e445703SGeoffrey McRae if (jo->c.state != QJACK_STATE_RUNNING) {
3492e445703SGeoffrey McRae qjack_client_recover(&jo->c);
3502e445703SGeoffrey McRae return len;
3512e445703SGeoffrey McRae }
3522e445703SGeoffrey McRae
3532e445703SGeoffrey McRae qjack_client_connect_ports(&jo->c);
3542e445703SGeoffrey McRae return qjack_buffer_write(&jo->c.fifo, buf, len);
3552e445703SGeoffrey McRae }
3562e445703SGeoffrey McRae
qjack_read(HWVoiceIn * hw,void * buf,size_t len)3572e445703SGeoffrey McRae static size_t qjack_read(HWVoiceIn *hw, void *buf, size_t len)
3582e445703SGeoffrey McRae {
3592e445703SGeoffrey McRae QJackIn *ji = (QJackIn *)hw;
3602e445703SGeoffrey McRae ++ji->c.packets;
3612e445703SGeoffrey McRae
3622e445703SGeoffrey McRae if (ji->c.state != QJACK_STATE_RUNNING) {
3632e445703SGeoffrey McRae qjack_client_recover(&ji->c);
3642e445703SGeoffrey McRae return len;
3652e445703SGeoffrey McRae }
3662e445703SGeoffrey McRae
3672e445703SGeoffrey McRae qjack_client_connect_ports(&ji->c);
3682e445703SGeoffrey McRae return qjack_buffer_read(&ji->c.fifo, buf, len);
3692e445703SGeoffrey McRae }
3702e445703SGeoffrey McRae
qjack_client_connect_ports(QJackClient * c)3712e445703SGeoffrey McRae static void qjack_client_connect_ports(QJackClient *c)
3722e445703SGeoffrey McRae {
3732e445703SGeoffrey McRae if (!c->connect_ports || !c->opt->connect_ports) {
3742e445703SGeoffrey McRae return;
3752e445703SGeoffrey McRae }
3762e445703SGeoffrey McRae
3772e445703SGeoffrey McRae c->connect_ports = false;
3782e445703SGeoffrey McRae const char **ports;
3792e445703SGeoffrey McRae ports = jack_get_ports(c->client, c->opt->connect_ports, NULL,
3802e445703SGeoffrey McRae c->out ? JackPortIsInput : JackPortIsOutput);
3812e445703SGeoffrey McRae
3822e445703SGeoffrey McRae if (!ports) {
3832e445703SGeoffrey McRae return;
3842e445703SGeoffrey McRae }
3852e445703SGeoffrey McRae
3862e445703SGeoffrey McRae for (int i = 0; i < c->nchannels && ports[i]; ++i) {
3872e445703SGeoffrey McRae const char *p = jack_port_name(c->port[i]);
3882e445703SGeoffrey McRae if (jack_port_connected_to(c->port[i], ports[i])) {
3892e445703SGeoffrey McRae continue;
3902e445703SGeoffrey McRae }
3912e445703SGeoffrey McRae
3922e445703SGeoffrey McRae if (c->out) {
3932e445703SGeoffrey McRae dolog("connect %s -> %s\n", p, ports[i]);
3942e445703SGeoffrey McRae jack_connect(c->client, p, ports[i]);
3952e445703SGeoffrey McRae } else {
3962e445703SGeoffrey McRae dolog("connect %s -> %s\n", ports[i], p);
3972e445703SGeoffrey McRae jack_connect(c->client, ports[i], p);
3982e445703SGeoffrey McRae }
3992e445703SGeoffrey McRae }
4002e445703SGeoffrey McRae }
4012e445703SGeoffrey McRae
qjack_client_init(QJackClient * c)4022e445703SGeoffrey McRae static int qjack_client_init(QJackClient *c)
4032e445703SGeoffrey McRae {
4042e445703SGeoffrey McRae jack_status_t status;
405d71c3d30SPeter Maydell int client_name_len = jack_client_name_size(); /* includes NUL */
406d71c3d30SPeter Maydell g_autofree char *client_name = g_new(char, client_name_len);
4072e445703SGeoffrey McRae jack_options_t options = JackNullOption;
4082e445703SGeoffrey McRae
409bc81e6e5SGeoffrey McRae if (c->state == QJACK_STATE_RUNNING) {
410bc81e6e5SGeoffrey McRae return 0;
411bc81e6e5SGeoffrey McRae }
412bc81e6e5SGeoffrey McRae
4132e445703SGeoffrey McRae c->connect_ports = true;
4142e445703SGeoffrey McRae
415d71c3d30SPeter Maydell snprintf(client_name, client_name_len, "%s-%s",
4162e445703SGeoffrey McRae c->out ? "out" : "in",
4172833d697SVolker Rümelin c->opt->client_name ? c->opt->client_name : audio_application_name());
4182e445703SGeoffrey McRae
4192e445703SGeoffrey McRae if (c->opt->exact_name) {
4202e445703SGeoffrey McRae options |= JackUseExactName;
4212e445703SGeoffrey McRae }
4222e445703SGeoffrey McRae
4232e445703SGeoffrey McRae if (!c->opt->start_server) {
4242e445703SGeoffrey McRae options |= JackNoStartServer;
4252e445703SGeoffrey McRae }
4262e445703SGeoffrey McRae
4272e445703SGeoffrey McRae if (c->opt->server_name) {
4282e445703SGeoffrey McRae options |= JackServerName;
4292e445703SGeoffrey McRae }
4302e445703SGeoffrey McRae
4312e445703SGeoffrey McRae c->client = jack_client_open(client_name, options, &status,
4322e445703SGeoffrey McRae c->opt->server_name);
4332e445703SGeoffrey McRae
4342e445703SGeoffrey McRae if (c->client == NULL) {
4352e445703SGeoffrey McRae dolog("jack_client_open failed: status = 0x%2.0x\n", status);
4362e445703SGeoffrey McRae if (status & JackServerFailed) {
4372e445703SGeoffrey McRae dolog("unable to connect to JACK server\n");
4382e445703SGeoffrey McRae }
4392e445703SGeoffrey McRae return -1;
4402e445703SGeoffrey McRae }
4412e445703SGeoffrey McRae
4422e445703SGeoffrey McRae c->freq = jack_get_sample_rate(c->client);
4432e445703SGeoffrey McRae
4442e445703SGeoffrey McRae if (status & JackServerStarted) {
4452e445703SGeoffrey McRae dolog("JACK server started\n");
4462e445703SGeoffrey McRae }
4472e445703SGeoffrey McRae
4482e445703SGeoffrey McRae if (status & JackNameNotUnique) {
4492e445703SGeoffrey McRae dolog("JACK unique name assigned %s\n",
4502e445703SGeoffrey McRae jack_get_client_name(c->client));
4512e445703SGeoffrey McRae }
4522e445703SGeoffrey McRae
45307ffc4b9SPeter Maydell /* Allocate working buffer for process callback */
45407ffc4b9SPeter Maydell c->process_buffers = g_new(float *, c->nchannels);
45507ffc4b9SPeter Maydell
4562e445703SGeoffrey McRae jack_set_process_callback(c->client, qjack_process , c);
4572e445703SGeoffrey McRae jack_set_port_registration_callback(c->client, qjack_port_registration, c);
4582e445703SGeoffrey McRae jack_set_xrun_callback(c->client, qjack_xrun, c);
4592e445703SGeoffrey McRae jack_on_shutdown(c->client, qjack_shutdown, c);
4602e445703SGeoffrey McRae
4612e445703SGeoffrey McRae /* allocate and register the ports */
462b21e2380SMarkus Armbruster c->port = g_new(jack_port_t *, c->nchannels);
4632e445703SGeoffrey McRae for (int i = 0; i < c->nchannels; ++i) {
4642e445703SGeoffrey McRae
4652e445703SGeoffrey McRae char port_name[16];
4662e445703SGeoffrey McRae snprintf(
4672e445703SGeoffrey McRae port_name,
4682e445703SGeoffrey McRae sizeof(port_name),
4692e445703SGeoffrey McRae c->out ? "output %d" : "input %d",
4702e445703SGeoffrey McRae i);
4712e445703SGeoffrey McRae
4722e445703SGeoffrey McRae c->port[i] = jack_port_register(
4732e445703SGeoffrey McRae c->client,
4742e445703SGeoffrey McRae port_name,
4752e445703SGeoffrey McRae JACK_DEFAULT_AUDIO_TYPE,
4762e445703SGeoffrey McRae c->out ? JackPortIsOutput : JackPortIsInput,
4772e445703SGeoffrey McRae 0);
4782e445703SGeoffrey McRae }
4792e445703SGeoffrey McRae
4802e445703SGeoffrey McRae /* activate the session */
4812e445703SGeoffrey McRae jack_activate(c->client);
4822e445703SGeoffrey McRae c->buffersize = jack_get_buffer_size(c->client);
4832e445703SGeoffrey McRae
48436963ed1SGeoffrey McRae /*
48536963ed1SGeoffrey McRae * ensure the buffersize is no smaller then 512 samples, some (all?) qemu
48636963ed1SGeoffrey McRae * virtual devices do not work correctly otherwise
48736963ed1SGeoffrey McRae */
48836963ed1SGeoffrey McRae if (c->buffersize < 512) {
48936963ed1SGeoffrey McRae c->buffersize = 512;
49036963ed1SGeoffrey McRae }
49136963ed1SGeoffrey McRae
492369829a4SVolker Rümelin /* create a 3 period buffer */
493369829a4SVolker Rümelin qjack_buffer_create(&c->fifo, c->nchannels, c->buffersize * 3);
49436963ed1SGeoffrey McRae
4952e445703SGeoffrey McRae qjack_client_connect_ports(c);
4962e445703SGeoffrey McRae c->state = QJACK_STATE_RUNNING;
4972e445703SGeoffrey McRae return 0;
4982e445703SGeoffrey McRae }
4992e445703SGeoffrey McRae
qjack_init_out(HWVoiceOut * hw,struct audsettings * as,void * drv_opaque)5002e445703SGeoffrey McRae static int qjack_init_out(HWVoiceOut *hw, struct audsettings *as,
5012e445703SGeoffrey McRae void *drv_opaque)
5022e445703SGeoffrey McRae {
5032e445703SGeoffrey McRae QJackOut *jo = (QJackOut *)hw;
5042e445703SGeoffrey McRae Audiodev *dev = (Audiodev *)drv_opaque;
5052e445703SGeoffrey McRae
5062e445703SGeoffrey McRae jo->c.out = true;
50781e0efb2SGeoffrey McRae jo->c.enabled = false;
5082e445703SGeoffrey McRae jo->c.nchannels = as->nchannels;
5092e445703SGeoffrey McRae jo->c.opt = dev->u.jack.out;
51081e0efb2SGeoffrey McRae
511a6e03739SGeoffrey McRae jo->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &jo->c);
512a6e03739SGeoffrey McRae
5132e445703SGeoffrey McRae int ret = qjack_client_init(&jo->c);
5142e445703SGeoffrey McRae if (ret != 0) {
515a6e03739SGeoffrey McRae qemu_bh_delete(jo->c.shutdown_bh);
5162e445703SGeoffrey McRae return ret;
5172e445703SGeoffrey McRae }
5182e445703SGeoffrey McRae
5192e445703SGeoffrey McRae /* report the buffer size to qemu */
5202e445703SGeoffrey McRae hw->samples = jo->c.buffersize;
5212e445703SGeoffrey McRae
5222e445703SGeoffrey McRae /* report the audio format we support */
5232e445703SGeoffrey McRae struct audsettings os = {
5242e445703SGeoffrey McRae .freq = jo->c.freq,
5252e445703SGeoffrey McRae .nchannels = jo->c.nchannels,
5262e445703SGeoffrey McRae .fmt = AUDIO_FORMAT_F32,
5272e445703SGeoffrey McRae .endianness = 0
5282e445703SGeoffrey McRae };
5292e445703SGeoffrey McRae audio_pcm_init_info(&hw->info, &os);
5302e445703SGeoffrey McRae
5312e445703SGeoffrey McRae dolog("JACK output configured for %dHz (%d samples)\n",
5322e445703SGeoffrey McRae jo->c.freq, jo->c.buffersize);
5332e445703SGeoffrey McRae
5342e445703SGeoffrey McRae return 0;
5352e445703SGeoffrey McRae }
5362e445703SGeoffrey McRae
qjack_init_in(HWVoiceIn * hw,struct audsettings * as,void * drv_opaque)5372e445703SGeoffrey McRae static int qjack_init_in(HWVoiceIn *hw, struct audsettings *as,
5382e445703SGeoffrey McRae void *drv_opaque)
5392e445703SGeoffrey McRae {
5402e445703SGeoffrey McRae QJackIn *ji = (QJackIn *)hw;
5412e445703SGeoffrey McRae Audiodev *dev = (Audiodev *)drv_opaque;
5422e445703SGeoffrey McRae
5432e445703SGeoffrey McRae ji->c.out = false;
54481e0efb2SGeoffrey McRae ji->c.enabled = false;
5452e445703SGeoffrey McRae ji->c.nchannels = as->nchannels;
5462e445703SGeoffrey McRae ji->c.opt = dev->u.jack.in;
54781e0efb2SGeoffrey McRae
548a6e03739SGeoffrey McRae ji->c.shutdown_bh = qemu_bh_new(qjack_shutdown_bh, &ji->c);
549a6e03739SGeoffrey McRae
5502e445703SGeoffrey McRae int ret = qjack_client_init(&ji->c);
5512e445703SGeoffrey McRae if (ret != 0) {
552a6e03739SGeoffrey McRae qemu_bh_delete(ji->c.shutdown_bh);
5532e445703SGeoffrey McRae return ret;
5542e445703SGeoffrey McRae }
5552e445703SGeoffrey McRae
5562e445703SGeoffrey McRae /* report the buffer size to qemu */
5572e445703SGeoffrey McRae hw->samples = ji->c.buffersize;
5582e445703SGeoffrey McRae
5592e445703SGeoffrey McRae /* report the audio format we support */
5602e445703SGeoffrey McRae struct audsettings is = {
5612e445703SGeoffrey McRae .freq = ji->c.freq,
5622e445703SGeoffrey McRae .nchannels = ji->c.nchannels,
5632e445703SGeoffrey McRae .fmt = AUDIO_FORMAT_F32,
5642e445703SGeoffrey McRae .endianness = 0
5652e445703SGeoffrey McRae };
5662e445703SGeoffrey McRae audio_pcm_init_info(&hw->info, &is);
5672e445703SGeoffrey McRae
5682e445703SGeoffrey McRae dolog("JACK input configured for %dHz (%d samples)\n",
5692e445703SGeoffrey McRae ji->c.freq, ji->c.buffersize);
5702e445703SGeoffrey McRae
5712e445703SGeoffrey McRae return 0;
5722e445703SGeoffrey McRae }
5732e445703SGeoffrey McRae
qjack_client_fini_locked(QJackClient * c)574a6e03739SGeoffrey McRae static void qjack_client_fini_locked(QJackClient *c)
5752e445703SGeoffrey McRae {
5762e445703SGeoffrey McRae switch (c->state) {
5772e445703SGeoffrey McRae case QJACK_STATE_RUNNING:
5782e445703SGeoffrey McRae jack_deactivate(c->client);
5792e445703SGeoffrey McRae /* fallthrough */
5802e445703SGeoffrey McRae
5812e445703SGeoffrey McRae case QJACK_STATE_SHUTDOWN:
5822e445703SGeoffrey McRae jack_client_close(c->client);
583a6e03739SGeoffrey McRae c->client = NULL;
5842e445703SGeoffrey McRae
5852e445703SGeoffrey McRae qjack_buffer_free(&c->fifo);
5862e445703SGeoffrey McRae g_free(c->port);
58707ffc4b9SPeter Maydell g_free(c->process_buffers);
5882e445703SGeoffrey McRae
5892e445703SGeoffrey McRae c->state = QJACK_STATE_DISCONNECTED;
590a6e03739SGeoffrey McRae /* fallthrough */
591a6e03739SGeoffrey McRae
592a6e03739SGeoffrey McRae case QJACK_STATE_DISCONNECTED:
593a6e03739SGeoffrey McRae break;
594a6e03739SGeoffrey McRae }
595a6e03739SGeoffrey McRae }
596a6e03739SGeoffrey McRae
qjack_client_fini(QJackClient * c)597a6e03739SGeoffrey McRae static void qjack_client_fini(QJackClient *c)
598a6e03739SGeoffrey McRae {
599a6e03739SGeoffrey McRae qemu_mutex_lock(&qjack_shutdown_lock);
600a6e03739SGeoffrey McRae qjack_client_fini_locked(c);
601a6e03739SGeoffrey McRae qemu_mutex_unlock(&qjack_shutdown_lock);
6022e445703SGeoffrey McRae }
6032e445703SGeoffrey McRae
qjack_fini_out(HWVoiceOut * hw)6042e445703SGeoffrey McRae static void qjack_fini_out(HWVoiceOut *hw)
6052e445703SGeoffrey McRae {
6062e445703SGeoffrey McRae QJackOut *jo = (QJackOut *)hw;
6072e445703SGeoffrey McRae qjack_client_fini(&jo->c);
608a6e03739SGeoffrey McRae
609a6e03739SGeoffrey McRae qemu_bh_delete(jo->c.shutdown_bh);
6102e445703SGeoffrey McRae }
6112e445703SGeoffrey McRae
qjack_fini_in(HWVoiceIn * hw)6122e445703SGeoffrey McRae static void qjack_fini_in(HWVoiceIn *hw)
6132e445703SGeoffrey McRae {
6142e445703SGeoffrey McRae QJackIn *ji = (QJackIn *)hw;
6152e445703SGeoffrey McRae qjack_client_fini(&ji->c);
616a6e03739SGeoffrey McRae
617a6e03739SGeoffrey McRae qemu_bh_delete(ji->c.shutdown_bh);
6182e445703SGeoffrey McRae }
6192e445703SGeoffrey McRae
qjack_enable_out(HWVoiceOut * hw,bool enable)6202e445703SGeoffrey McRae static void qjack_enable_out(HWVoiceOut *hw, bool enable)
6212e445703SGeoffrey McRae {
62281e0efb2SGeoffrey McRae QJackOut *jo = (QJackOut *)hw;
62381e0efb2SGeoffrey McRae jo->c.enabled = enable;
6242e445703SGeoffrey McRae }
6252e445703SGeoffrey McRae
qjack_enable_in(HWVoiceIn * hw,bool enable)6262e445703SGeoffrey McRae static void qjack_enable_in(HWVoiceIn *hw, bool enable)
6272e445703SGeoffrey McRae {
62881e0efb2SGeoffrey McRae QJackIn *ji = (QJackIn *)hw;
62981e0efb2SGeoffrey McRae ji->c.enabled = enable;
6302e445703SGeoffrey McRae }
6312e445703SGeoffrey McRae
632ead789ebSVolker Rümelin #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
qjack_thread_creator(jack_native_thread_t * thread,const pthread_attr_t * attr,void * (* function)(void *),void * arg)6332e445703SGeoffrey McRae static int qjack_thread_creator(jack_native_thread_t *thread,
6342e445703SGeoffrey McRae const pthread_attr_t *attr, void *(*function)(void *), void *arg)
6352e445703SGeoffrey McRae {
6362e445703SGeoffrey McRae int ret = pthread_create(thread, attr, function, arg);
6372e445703SGeoffrey McRae if (ret != 0) {
6382e445703SGeoffrey McRae return ret;
6392e445703SGeoffrey McRae }
6402e445703SGeoffrey McRae
6412e445703SGeoffrey McRae /* set the name of the thread */
6422e445703SGeoffrey McRae pthread_setname_np(*thread, "jack-client");
6432e445703SGeoffrey McRae
6442e445703SGeoffrey McRae return ret;
6452e445703SGeoffrey McRae }
646ead789ebSVolker Rümelin #endif
6472e445703SGeoffrey McRae
qjack_init(Audiodev * dev,Error ** errp)648*f6061733SPaolo Bonzini static void *qjack_init(Audiodev *dev, Error **errp)
6492e445703SGeoffrey McRae {
6502e445703SGeoffrey McRae assert(dev->driver == AUDIODEV_DRIVER_JACK);
6512e445703SGeoffrey McRae return dev;
6522e445703SGeoffrey McRae }
6532e445703SGeoffrey McRae
qjack_fini(void * opaque)6542e445703SGeoffrey McRae static void qjack_fini(void *opaque)
6552e445703SGeoffrey McRae {
6562e445703SGeoffrey McRae }
6572e445703SGeoffrey McRae
6582e445703SGeoffrey McRae static struct audio_pcm_ops jack_pcm_ops = {
6592e445703SGeoffrey McRae .init_out = qjack_init_out,
6602e445703SGeoffrey McRae .fini_out = qjack_fini_out,
6612e445703SGeoffrey McRae .write = qjack_write,
6629833438eSVolker Rümelin .buffer_get_free = audio_generic_buffer_get_free,
6632e445703SGeoffrey McRae .run_buffer_out = audio_generic_run_buffer_out,
6642e445703SGeoffrey McRae .enable_out = qjack_enable_out,
6652e445703SGeoffrey McRae
6662e445703SGeoffrey McRae .init_in = qjack_init_in,
6672e445703SGeoffrey McRae .fini_in = qjack_fini_in,
6682e445703SGeoffrey McRae .read = qjack_read,
669a2893c83SVolker Rümelin .run_buffer_in = audio_generic_run_buffer_in,
6702e445703SGeoffrey McRae .enable_in = qjack_enable_in
6712e445703SGeoffrey McRae };
6722e445703SGeoffrey McRae
6732e445703SGeoffrey McRae static struct audio_driver jack_driver = {
6742e445703SGeoffrey McRae .name = "jack",
6752e445703SGeoffrey McRae .descr = "JACK Audio Connection Kit Client",
6762e445703SGeoffrey McRae .init = qjack_init,
6772e445703SGeoffrey McRae .fini = qjack_fini,
6782e445703SGeoffrey McRae .pcm_ops = &jack_pcm_ops,
6792e445703SGeoffrey McRae .max_voices_out = INT_MAX,
6802e445703SGeoffrey McRae .max_voices_in = INT_MAX,
6812e445703SGeoffrey McRae .voice_size_out = sizeof(QJackOut),
6822e445703SGeoffrey McRae .voice_size_in = sizeof(QJackIn)
6832e445703SGeoffrey McRae };
6842e445703SGeoffrey McRae
qjack_error(const char * msg)6852e445703SGeoffrey McRae static void qjack_error(const char *msg)
6862e445703SGeoffrey McRae {
6872e445703SGeoffrey McRae dolog("E: %s\n", msg);
6882e445703SGeoffrey McRae }
6892e445703SGeoffrey McRae
qjack_info(const char * msg)6902e445703SGeoffrey McRae static void qjack_info(const char *msg)
6912e445703SGeoffrey McRae {
6922e445703SGeoffrey McRae dolog("I: %s\n", msg);
6932e445703SGeoffrey McRae }
6942e445703SGeoffrey McRae
register_audio_jack(void)6952e445703SGeoffrey McRae static void register_audio_jack(void)
6962e445703SGeoffrey McRae {
697a6e03739SGeoffrey McRae qemu_mutex_init(&qjack_shutdown_lock);
6982e445703SGeoffrey McRae audio_driver_register(&jack_driver);
699ead789ebSVolker Rümelin #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
7002e445703SGeoffrey McRae jack_set_thread_creator(qjack_thread_creator);
701ead789ebSVolker Rümelin #endif
7022e445703SGeoffrey McRae jack_set_error_function(qjack_error);
7032e445703SGeoffrey McRae jack_set_info_function(qjack_info);
7042e445703SGeoffrey McRae }
7052e445703SGeoffrey McRae type_init(register_audio_jack);
706