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
25 #include "qemu/osdep.h"
26 #include "qemu/module.h"
27 #include "qemu/atomic.h"
28 #include "qemu/main-loop.h"
31 #define AUDIO_CAP "jack"
32 #include "audio_int.h"
34 #include <jack/jack.h>
35 #include <jack/thread.h>
39 typedef enum QJackState
{
40 QJACK_STATE_DISCONNECTED
,
46 typedef struct QJackBuffer
{
55 typedef struct QJackClient
{
56 AudiodevJackPerDirectionOptions
*opt
;
64 jack_client_t
*client
;
74 /* Used as workspace by qjack_process() */
75 float **process_buffers
;
79 typedef struct QJackOut
{
85 typedef struct QJackIn
{
91 static int qjack_client_init(QJackClient
*c
);
92 static void qjack_client_connect_ports(QJackClient
*c
);
93 static void qjack_client_fini(QJackClient
*c
);
94 static QemuMutex qjack_shutdown_lock
;
96 static void qjack_buffer_create(QJackBuffer
*buffer
, int channels
, int frames
)
98 buffer
->channels
= channels
;
99 buffer
->frames
= frames
;
103 buffer
->data
= g_new(float *, channels
);
104 for (int i
= 0; i
< channels
; ++i
) {
105 buffer
->data
[i
] = g_new(float, frames
);
109 static void qjack_buffer_clear(QJackBuffer
*buffer
)
111 assert(buffer
->data
);
112 qatomic_store_release(&buffer
->used
, 0);
117 static void qjack_buffer_free(QJackBuffer
*buffer
)
123 for (int i
= 0; i
< buffer
->channels
; ++i
) {
124 g_free(buffer
->data
[i
]);
127 g_free(buffer
->data
);
131 /* write PCM interleaved */
132 static int qjack_buffer_write(QJackBuffer
*buffer
, float *data
, int size
)
134 assert(buffer
->data
);
135 const int samples
= size
/ sizeof(float);
136 int frames
= samples
/ buffer
->channels
;
137 const int avail
= buffer
->frames
- qatomic_load_acquire(&buffer
->used
);
139 if (frames
> avail
) {
144 int wptr
= buffer
->wptr
;
148 for (int c
= 0; c
< buffer
->channels
; ++c
) {
149 buffer
->data
[c
][wptr
] = *data
++;
152 if (++wptr
== buffer
->frames
) {
161 qatomic_add(&buffer
->used
, frames
);
162 return frames
* buffer
->channels
* sizeof(float);
165 /* write PCM linear */
166 static int qjack_buffer_write_l(QJackBuffer
*buffer
, float **dest
, int frames
)
168 assert(buffer
->data
);
169 const int avail
= buffer
->frames
- qatomic_load_acquire(&buffer
->used
);
170 int wptr
= buffer
->wptr
;
172 if (frames
> avail
) {
176 int right
= buffer
->frames
- wptr
;
177 if (right
> frames
) {
181 const int left
= frames
- right
;
182 for (int c
= 0; c
< buffer
->channels
; ++c
) {
183 memcpy(buffer
->data
[c
] + wptr
, dest
[c
] , right
* sizeof(float));
184 memcpy(buffer
->data
[c
] , dest
[c
] + right
, left
* sizeof(float));
188 if (wptr
>= buffer
->frames
) {
189 wptr
-= buffer
->frames
;
193 qatomic_add(&buffer
->used
, frames
);
197 /* read PCM interleaved */
198 static int qjack_buffer_read(QJackBuffer
*buffer
, float *dest
, int size
)
200 assert(buffer
->data
);
201 const int samples
= size
/ sizeof(float);
202 int frames
= samples
/ buffer
->channels
;
203 const int avail
= qatomic_load_acquire(&buffer
->used
);
205 if (frames
> avail
) {
210 int rptr
= buffer
->rptr
;
214 for (int c
= 0; c
< buffer
->channels
; ++c
) {
215 *dest
++ = buffer
->data
[c
][rptr
];
218 if (++rptr
== buffer
->frames
) {
227 qatomic_sub(&buffer
->used
, frames
);
228 return frames
* buffer
->channels
* sizeof(float);
231 /* read PCM linear */
232 static int qjack_buffer_read_l(QJackBuffer
*buffer
, float **dest
, int frames
)
234 assert(buffer
->data
);
236 const int used
= qatomic_load_acquire(&buffer
->used
);
237 int rptr
= buffer
->rptr
;
243 int right
= buffer
->frames
- rptr
;
248 const int left
= copy
- right
;
249 for (int c
= 0; c
< buffer
->channels
; ++c
) {
250 memcpy(dest
[c
] , buffer
->data
[c
] + rptr
, right
* sizeof(float));
251 memcpy(dest
[c
] + right
, buffer
->data
[c
] , left
* sizeof(float));
255 if (rptr
>= buffer
->frames
) {
256 rptr
-= buffer
->frames
;
260 qatomic_sub(&buffer
->used
, copy
);
264 static int qjack_process(jack_nframes_t nframes
, void *arg
)
266 QJackClient
*c
= (QJackClient
*)arg
;
268 if (c
->state
!= QJACK_STATE_RUNNING
) {
272 /* get the buffers for the ports */
273 for (int i
= 0; i
< c
->nchannels
; ++i
) {
274 c
->process_buffers
[i
] = jack_port_get_buffer(c
->port
[i
], nframes
);
278 if (likely(c
->enabled
)) {
279 qjack_buffer_read_l(&c
->fifo
, c
->process_buffers
, nframes
);
281 for (int i
= 0; i
< c
->nchannels
; ++i
) {
282 memset(c
->process_buffers
[i
], 0, nframes
* sizeof(float));
286 if (likely(c
->enabled
)) {
287 qjack_buffer_write_l(&c
->fifo
, c
->process_buffers
, nframes
);
294 static void qjack_port_registration(jack_port_id_t port
, int reg
, void *arg
)
297 QJackClient
*c
= (QJackClient
*)arg
;
298 c
->connect_ports
= true;
302 static int qjack_xrun(void *arg
)
304 QJackClient
*c
= (QJackClient
*)arg
;
305 if (c
->state
!= QJACK_STATE_RUNNING
) {
309 qjack_buffer_clear(&c
->fifo
);
313 static void qjack_shutdown_bh(void *opaque
)
315 QJackClient
*c
= (QJackClient
*)opaque
;
316 qjack_client_fini(c
);
319 static void qjack_shutdown(void *arg
)
321 QJackClient
*c
= (QJackClient
*)arg
;
322 c
->state
= QJACK_STATE_SHUTDOWN
;
323 qemu_bh_schedule(c
->shutdown_bh
);
326 static void qjack_client_recover(QJackClient
*c
)
328 if (c
->state
!= QJACK_STATE_DISCONNECTED
) {
332 /* packets is used simply to throttle this */
333 if (c
->packets
% 100 == 0) {
335 /* if enabled then attempt to recover */
337 dolog("attempting to reconnect to server\n");
338 qjack_client_init(c
);
343 static size_t qjack_write(HWVoiceOut
*hw
, void *buf
, size_t len
)
345 QJackOut
*jo
= (QJackOut
*)hw
;
348 if (jo
->c
.state
!= QJACK_STATE_RUNNING
) {
349 qjack_client_recover(&jo
->c
);
353 qjack_client_connect_ports(&jo
->c
);
354 return qjack_buffer_write(&jo
->c
.fifo
, buf
, len
);
357 static size_t qjack_read(HWVoiceIn
*hw
, void *buf
, size_t len
)
359 QJackIn
*ji
= (QJackIn
*)hw
;
362 if (ji
->c
.state
!= QJACK_STATE_RUNNING
) {
363 qjack_client_recover(&ji
->c
);
367 qjack_client_connect_ports(&ji
->c
);
368 return qjack_buffer_read(&ji
->c
.fifo
, buf
, len
);
371 static void qjack_client_connect_ports(QJackClient
*c
)
373 if (!c
->connect_ports
|| !c
->opt
->connect_ports
) {
377 c
->connect_ports
= false;
379 ports
= jack_get_ports(c
->client
, c
->opt
->connect_ports
, NULL
,
380 c
->out
? JackPortIsInput
: JackPortIsOutput
);
386 for (int i
= 0; i
< c
->nchannels
&& ports
[i
]; ++i
) {
387 const char *p
= jack_port_name(c
->port
[i
]);
388 if (jack_port_connected_to(c
->port
[i
], ports
[i
])) {
393 dolog("connect %s -> %s\n", p
, ports
[i
]);
394 jack_connect(c
->client
, p
, ports
[i
]);
396 dolog("connect %s -> %s\n", ports
[i
], p
);
397 jack_connect(c
->client
, ports
[i
], p
);
402 static int qjack_client_init(QJackClient
*c
)
404 jack_status_t status
;
405 int client_name_len
= jack_client_name_size(); /* includes NUL */
406 g_autofree
char *client_name
= g_new(char, client_name_len
);
407 jack_options_t options
= JackNullOption
;
409 if (c
->state
== QJACK_STATE_RUNNING
) {
413 c
->connect_ports
= true;
415 snprintf(client_name
, client_name_len
, "%s-%s",
416 c
->out
? "out" : "in",
417 c
->opt
->client_name
? c
->opt
->client_name
: audio_application_name());
419 if (c
->opt
->exact_name
) {
420 options
|= JackUseExactName
;
423 if (!c
->opt
->start_server
) {
424 options
|= JackNoStartServer
;
427 if (c
->opt
->server_name
) {
428 options
|= JackServerName
;
431 c
->client
= jack_client_open(client_name
, options
, &status
,
432 c
->opt
->server_name
);
434 if (c
->client
== NULL
) {
435 dolog("jack_client_open failed: status = 0x%2.0x\n", status
);
436 if (status
& JackServerFailed
) {
437 dolog("unable to connect to JACK server\n");
442 c
->freq
= jack_get_sample_rate(c
->client
);
444 if (status
& JackServerStarted
) {
445 dolog("JACK server started\n");
448 if (status
& JackNameNotUnique
) {
449 dolog("JACK unique name assigned %s\n",
450 jack_get_client_name(c
->client
));
453 /* Allocate working buffer for process callback */
454 c
->process_buffers
= g_new(float *, c
->nchannels
);
456 jack_set_process_callback(c
->client
, qjack_process
, c
);
457 jack_set_port_registration_callback(c
->client
, qjack_port_registration
, c
);
458 jack_set_xrun_callback(c
->client
, qjack_xrun
, c
);
459 jack_on_shutdown(c
->client
, qjack_shutdown
, c
);
461 /* allocate and register the ports */
462 c
->port
= g_new(jack_port_t
*, c
->nchannels
);
463 for (int i
= 0; i
< c
->nchannels
; ++i
) {
469 c
->out
? "output %d" : "input %d",
472 c
->port
[i
] = jack_port_register(
475 JACK_DEFAULT_AUDIO_TYPE
,
476 c
->out
? JackPortIsOutput
: JackPortIsInput
,
480 /* activate the session */
481 jack_activate(c
->client
);
482 c
->buffersize
= jack_get_buffer_size(c
->client
);
485 * ensure the buffersize is no smaller then 512 samples, some (all?) qemu
486 * virtual devices do not work correctly otherwise
488 if (c
->buffersize
< 512) {
492 /* create a 3 period buffer */
493 qjack_buffer_create(&c
->fifo
, c
->nchannels
, c
->buffersize
* 3);
495 qjack_client_connect_ports(c
);
496 c
->state
= QJACK_STATE_RUNNING
;
500 static int qjack_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
503 QJackOut
*jo
= (QJackOut
*)hw
;
504 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
507 jo
->c
.enabled
= false;
508 jo
->c
.nchannels
= as
->nchannels
;
509 jo
->c
.opt
= dev
->u
.jack
.out
;
511 jo
->c
.shutdown_bh
= qemu_bh_new(qjack_shutdown_bh
, &jo
->c
);
513 int ret
= qjack_client_init(&jo
->c
);
515 qemu_bh_delete(jo
->c
.shutdown_bh
);
519 /* report the buffer size to qemu */
520 hw
->samples
= jo
->c
.buffersize
;
522 /* report the audio format we support */
523 struct audsettings os
= {
525 .nchannels
= jo
->c
.nchannels
,
526 .fmt
= AUDIO_FORMAT_F32
,
529 audio_pcm_init_info(&hw
->info
, &os
);
531 dolog("JACK output configured for %dHz (%d samples)\n",
532 jo
->c
.freq
, jo
->c
.buffersize
);
537 static int qjack_init_in(HWVoiceIn
*hw
, struct audsettings
*as
,
540 QJackIn
*ji
= (QJackIn
*)hw
;
541 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
544 ji
->c
.enabled
= false;
545 ji
->c
.nchannels
= as
->nchannels
;
546 ji
->c
.opt
= dev
->u
.jack
.in
;
548 ji
->c
.shutdown_bh
= qemu_bh_new(qjack_shutdown_bh
, &ji
->c
);
550 int ret
= qjack_client_init(&ji
->c
);
552 qemu_bh_delete(ji
->c
.shutdown_bh
);
556 /* report the buffer size to qemu */
557 hw
->samples
= ji
->c
.buffersize
;
559 /* report the audio format we support */
560 struct audsettings is
= {
562 .nchannels
= ji
->c
.nchannels
,
563 .fmt
= AUDIO_FORMAT_F32
,
566 audio_pcm_init_info(&hw
->info
, &is
);
568 dolog("JACK input configured for %dHz (%d samples)\n",
569 ji
->c
.freq
, ji
->c
.buffersize
);
574 static void qjack_client_fini_locked(QJackClient
*c
)
577 case QJACK_STATE_RUNNING
:
578 jack_deactivate(c
->client
);
581 case QJACK_STATE_SHUTDOWN
:
582 jack_client_close(c
->client
);
585 qjack_buffer_free(&c
->fifo
);
587 g_free(c
->process_buffers
);
589 c
->state
= QJACK_STATE_DISCONNECTED
;
592 case QJACK_STATE_DISCONNECTED
:
597 static void qjack_client_fini(QJackClient
*c
)
599 qemu_mutex_lock(&qjack_shutdown_lock
);
600 qjack_client_fini_locked(c
);
601 qemu_mutex_unlock(&qjack_shutdown_lock
);
604 static void qjack_fini_out(HWVoiceOut
*hw
)
606 QJackOut
*jo
= (QJackOut
*)hw
;
607 qjack_client_fini(&jo
->c
);
609 qemu_bh_delete(jo
->c
.shutdown_bh
);
612 static void qjack_fini_in(HWVoiceIn
*hw
)
614 QJackIn
*ji
= (QJackIn
*)hw
;
615 qjack_client_fini(&ji
->c
);
617 qemu_bh_delete(ji
->c
.shutdown_bh
);
620 static void qjack_enable_out(HWVoiceOut
*hw
, bool enable
)
622 QJackOut
*jo
= (QJackOut
*)hw
;
623 jo
->c
.enabled
= enable
;
626 static void qjack_enable_in(HWVoiceIn
*hw
, bool enable
)
628 QJackIn
*ji
= (QJackIn
*)hw
;
629 ji
->c
.enabled
= enable
;
632 #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
633 static int qjack_thread_creator(jack_native_thread_t
*thread
,
634 const pthread_attr_t
*attr
, void *(*function
)(void *), void *arg
)
636 int ret
= pthread_create(thread
, attr
, function
, arg
);
641 /* set the name of the thread */
642 pthread_setname_np(*thread
, "jack-client");
648 static void *qjack_init(Audiodev
*dev
, Error
**errp
)
650 assert(dev
->driver
== AUDIODEV_DRIVER_JACK
);
654 static void qjack_fini(void *opaque
)
658 static struct audio_pcm_ops jack_pcm_ops
= {
659 .init_out
= qjack_init_out
,
660 .fini_out
= qjack_fini_out
,
661 .write
= qjack_write
,
662 .buffer_get_free
= audio_generic_buffer_get_free
,
663 .run_buffer_out
= audio_generic_run_buffer_out
,
664 .enable_out
= qjack_enable_out
,
666 .init_in
= qjack_init_in
,
667 .fini_in
= qjack_fini_in
,
669 .run_buffer_in
= audio_generic_run_buffer_in
,
670 .enable_in
= qjack_enable_in
673 static struct audio_driver jack_driver
= {
675 .descr
= "JACK Audio Connection Kit Client",
678 .pcm_ops
= &jack_pcm_ops
,
679 .max_voices_out
= INT_MAX
,
680 .max_voices_in
= INT_MAX
,
681 .voice_size_out
= sizeof(QJackOut
),
682 .voice_size_in
= sizeof(QJackIn
)
685 static void qjack_error(const char *msg
)
687 dolog("E: %s\n", msg
);
690 static void qjack_info(const char *msg
)
692 dolog("I: %s\n", msg
);
695 static void register_audio_jack(void)
697 qemu_mutex_init(&qjack_shutdown_lock
);
698 audio_driver_register(&jack_driver
);
699 #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
700 jack_set_thread_creator(qjack_thread_creator
);
702 jack_set_error_function(qjack_error
);
703 jack_set_info_function(qjack_info
);
705 type_init(register_audio_jack
);