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"
29 #include "qemu-common.h"
32 #define AUDIO_CAP "jack"
33 #include "audio_int.h"
35 #include <jack/jack.h>
36 #include <jack/thread.h>
40 typedef enum QJackState
{
41 QJACK_STATE_DISCONNECTED
,
47 typedef struct QJackBuffer
{
56 typedef struct QJackClient
{
57 AudiodevJackPerDirectionOptions
*opt
;
65 jack_client_t
*client
;
77 typedef struct QJackOut
{
83 typedef struct 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
;
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);
115 static void qjack_buffer_free(QJackBuffer
*buffer
)
121 for (int i
= 0; i
< buffer
->channels
; ++i
) {
122 g_free(buffer
->data
[i
]);
125 g_free(buffer
->data
);
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
) {
142 int wptr
= buffer
->wptr
;
146 for (int c
= 0; c
< buffer
->channels
; ++c
) {
147 buffer
->data
[c
][wptr
] = *data
++;
150 if (++wptr
== buffer
->frames
) {
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
) {
174 int right
= buffer
->frames
- wptr
;
175 if (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));
186 if (wptr
>= buffer
->frames
) {
187 wptr
-= buffer
->frames
;
191 qatomic_add(&buffer
->used
, 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
) {
208 int rptr
= buffer
->rptr
;
212 for (int c
= 0; c
< buffer
->channels
; ++c
) {
213 *dest
++ = buffer
->data
[c
][rptr
];
216 if (++rptr
== buffer
->frames
) {
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
);
234 const int used
= qatomic_load_acquire(&buffer
->used
);
235 int rptr
= buffer
->rptr
;
241 int right
= buffer
->frames
- rptr
;
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));
253 if (rptr
>= buffer
->frames
) {
254 rptr
-= buffer
->frames
;
258 qatomic_sub(&buffer
->used
, copy
);
262 static int qjack_process(jack_nframes_t nframes
, void *arg
)
264 QJackClient
*c
= (QJackClient
*)arg
;
266 if (c
->state
!= QJACK_STATE_RUNNING
) {
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
);
277 if (likely(c
->enabled
)) {
278 qjack_buffer_read_l(&c
->fifo
, buffers
, nframes
);
280 for (int i
= 0; i
< c
->nchannels
; ++i
) {
281 memset(buffers
[i
], 0, nframes
* sizeof(float));
285 if (likely(c
->enabled
)) {
286 qjack_buffer_write_l(&c
->fifo
, buffers
, nframes
);
293 static void qjack_port_registration(jack_port_id_t port
, int reg
, void *arg
)
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
) {
308 qjack_buffer_clear(&c
->fifo
);
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
) {
331 /* packets is used simply to throttle this */
332 if (c
->packets
% 100 == 0) {
334 /* if enabled then attempt to recover */
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
;
347 if (jo
->c
.state
!= QJACK_STATE_RUNNING
) {
348 qjack_client_recover(&jo
->c
);
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
;
361 if (ji
->c
.state
!= QJACK_STATE_RUNNING
) {
362 qjack_client_recover(&ji
->c
);
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
) {
376 c
->connect_ports
= false;
378 ports
= jack_get_ports(c
->client
, c
->opt
->connect_ports
, NULL
,
379 c
->out
? JackPortIsInput
: JackPortIsOutput
);
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
])) {
392 dolog("connect %s -> %s\n", p
, ports
[i
]);
393 jack_connect(c
->client
, p
, ports
[i
]);
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
) {
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");
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
) {
464 c
->out
? "output %d" : "input %d",
467 c
->port
[i
] = jack_port_register(
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) {
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
;
495 static int qjack_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
498 QJackOut
*jo
= (QJackOut
*)hw
;
499 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
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
);
510 qemu_bh_delete(jo
->c
.shutdown_bh
);
514 /* report the buffer size to qemu */
515 hw
->samples
= jo
->c
.buffersize
;
517 /* report the audio format we support */
518 struct audsettings os
= {
520 .nchannels
= jo
->c
.nchannels
,
521 .fmt
= AUDIO_FORMAT_F32
,
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
);
532 static int qjack_init_in(HWVoiceIn
*hw
, struct audsettings
*as
,
535 QJackIn
*ji
= (QJackIn
*)hw
;
536 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
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
);
547 qemu_bh_delete(ji
->c
.shutdown_bh
);
551 /* report the buffer size to qemu */
552 hw
->samples
= ji
->c
.buffersize
;
554 /* report the audio format we support */
555 struct audsettings is
= {
557 .nchannels
= ji
->c
.nchannels
,
558 .fmt
= AUDIO_FORMAT_F32
,
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
);
569 static void qjack_client_fini_locked(QJackClient
*c
)
572 case QJACK_STATE_RUNNING
:
573 jack_deactivate(c
->client
);
576 case QJACK_STATE_SHUTDOWN
:
577 jack_client_close(c
->client
);
580 qjack_buffer_free(&c
->fifo
);
583 c
->state
= QJACK_STATE_DISCONNECTED
;
586 case QJACK_STATE_DISCONNECTED
:
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
);
634 /* set the name of the thread */
635 pthread_setname_np(*thread
, "jack-client");
640 static void *qjack_init(Audiodev
*dev
)
642 assert(dev
->driver
== AUDIODEV_DRIVER_JACK
);
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
,
660 .run_buffer_in
= audio_generic_run_buffer_in
,
661 .enable_in
= qjack_enable_in
664 static struct audio_driver jack_driver
= {
666 .descr
= "JACK Audio Connection Kit Client",
669 .pcm_ops
= &jack_pcm_ops
,
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
);