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
;
76 typedef struct QJackOut
{
82 typedef struct 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
;
100 buffer
->data
= g_malloc(channels
* sizeof(float *));
101 for (int i
= 0; i
< channels
; ++i
) {
102 buffer
->data
[i
] = g_malloc(frames
* sizeof(float));
106 static void qjack_buffer_clear(QJackBuffer
*buffer
)
108 assert(buffer
->data
);
109 qatomic_store_release(&buffer
->used
, 0);
114 static void qjack_buffer_free(QJackBuffer
*buffer
)
120 for (int i
= 0; i
< buffer
->channels
; ++i
) {
121 g_free(buffer
->data
[i
]);
124 g_free(buffer
->data
);
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
) {
141 int wptr
= buffer
->wptr
;
145 for (int c
= 0; c
< buffer
->channels
; ++c
) {
146 buffer
->data
[c
][wptr
] = *data
++;
149 if (++wptr
== buffer
->frames
) {
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
) {
173 int right
= buffer
->frames
- wptr
;
174 if (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));
185 if (wptr
>= buffer
->frames
) {
186 wptr
-= buffer
->frames
;
190 qatomic_add(&buffer
->used
, 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
) {
207 int rptr
= buffer
->rptr
;
211 for (int c
= 0; c
< buffer
->channels
; ++c
) {
212 *dest
++ = buffer
->data
[c
][rptr
];
215 if (++rptr
== buffer
->frames
) {
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
);
233 const int used
= qatomic_load_acquire(&buffer
->used
);
234 int rptr
= buffer
->rptr
;
240 int right
= buffer
->frames
- rptr
;
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));
252 if (rptr
>= buffer
->frames
) {
253 rptr
-= buffer
->frames
;
257 qatomic_sub(&buffer
->used
, copy
);
261 static int qjack_process(jack_nframes_t nframes
, void *arg
)
263 QJackClient
*c
= (QJackClient
*)arg
;
265 if (c
->state
!= QJACK_STATE_RUNNING
) {
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
);
276 if (likely(c
->enabled
)) {
277 qjack_buffer_read_l(&c
->fifo
, buffers
, nframes
);
279 for (int i
= 0; i
< c
->nchannels
; ++i
) {
280 memset(buffers
[i
], 0, nframes
* sizeof(float));
284 if (likely(c
->enabled
)) {
285 qjack_buffer_write_l(&c
->fifo
, buffers
, nframes
);
292 static void qjack_port_registration(jack_port_id_t port
, int reg
, void *arg
)
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
) {
307 qjack_buffer_clear(&c
->fifo
);
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
) {
330 /* packets is used simply to throttle this */
331 if (c
->packets
% 100 == 0) {
333 /* if enabled then attempt to recover */
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
;
346 if (jo
->c
.state
!= QJACK_STATE_RUNNING
) {
347 qjack_client_recover(&jo
->c
);
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
;
360 if (ji
->c
.state
!= QJACK_STATE_RUNNING
) {
361 qjack_client_recover(&ji
->c
);
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
) {
375 c
->connect_ports
= false;
377 ports
= jack_get_ports(c
->client
, c
->opt
->connect_ports
, NULL
,
378 c
->out
? JackPortIsInput
: JackPortIsOutput
);
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
])) {
391 dolog("connect %s -> %s\n", p
, ports
[i
]);
392 jack_connect(c
->client
, p
, ports
[i
]);
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
) {
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");
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_malloc(sizeof(jack_port_t
*) * c
->nchannels
);
457 for (int i
= 0; i
< c
->nchannels
; ++i
) {
463 c
->out
? "output %d" : "input %d",
466 c
->port
[i
] = jack_port_register(
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) {
486 /* create a 2 period buffer */
487 qjack_buffer_create(&c
->fifo
, c
->nchannels
, c
->buffersize
* 2);
489 qjack_client_connect_ports(c
);
490 c
->state
= QJACK_STATE_RUNNING
;
494 static int qjack_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
497 QJackOut
*jo
= (QJackOut
*)hw
;
498 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
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
);
509 qemu_bh_delete(jo
->c
.shutdown_bh
);
513 /* report the buffer size to qemu */
514 hw
->samples
= jo
->c
.buffersize
;
516 /* report the audio format we support */
517 struct audsettings os
= {
519 .nchannels
= jo
->c
.nchannels
,
520 .fmt
= AUDIO_FORMAT_F32
,
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
);
531 static int qjack_init_in(HWVoiceIn
*hw
, struct audsettings
*as
,
534 QJackIn
*ji
= (QJackIn
*)hw
;
535 Audiodev
*dev
= (Audiodev
*)drv_opaque
;
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
);
546 qemu_bh_delete(ji
->c
.shutdown_bh
);
550 /* report the buffer size to qemu */
551 hw
->samples
= ji
->c
.buffersize
;
553 /* report the audio format we support */
554 struct audsettings is
= {
556 .nchannels
= ji
->c
.nchannels
,
557 .fmt
= AUDIO_FORMAT_F32
,
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
);
568 static void qjack_client_fini_locked(QJackClient
*c
)
571 case QJACK_STATE_RUNNING
:
572 jack_deactivate(c
->client
);
575 case QJACK_STATE_SHUTDOWN
:
576 jack_client_close(c
->client
);
579 qjack_buffer_free(&c
->fifo
);
582 c
->state
= QJACK_STATE_DISCONNECTED
;
585 case QJACK_STATE_DISCONNECTED
:
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
);
634 /* set the name of the thread */
635 pthread_setname_np(*thread
, "jack-client");
641 static void *qjack_init(Audiodev
*dev
)
643 assert(dev
->driver
== AUDIODEV_DRIVER_JACK
);
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 .run_buffer_out
= audio_generic_run_buffer_out
,
656 .enable_out
= qjack_enable_out
,
658 .init_in
= qjack_init_in
,
659 .fini_in
= qjack_fini_in
,
661 .run_buffer_in
= audio_generic_run_buffer_in
,
662 .enable_in
= qjack_enable_in
665 static struct audio_driver jack_driver
= {
667 .descr
= "JACK Audio Connection Kit Client",
670 .pcm_ops
= &jack_pcm_ops
,
672 .max_voices_out
= INT_MAX
,
673 .max_voices_in
= INT_MAX
,
674 .voice_size_out
= sizeof(QJackOut
),
675 .voice_size_in
= sizeof(QJackIn
)
678 static void qjack_error(const char *msg
)
680 dolog("E: %s\n", msg
);
683 static void qjack_info(const char *msg
)
685 dolog("I: %s\n", msg
);
688 static void register_audio_jack(void)
690 qemu_mutex_init(&qjack_shutdown_lock
);
691 audio_driver_register(&jack_driver
);
692 #if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
693 jack_set_thread_creator(qjack_thread_creator
);
695 jack_set_error_function(qjack_error
);
696 jack_set_info_function(qjack_info
);
698 type_init(register_audio_jack
);