3 #include "qemu/osdep.h"
4 #include "qemu/module.h"
5 #include "qemu-common.h"
7 #include "qapi/opts-visitor.h"
9 #include <pulse/pulseaudio.h>
11 #define AUDIO_CAP "pulseaudio"
12 #include "audio_int.h"
14 typedef struct PAConnection
{
17 QTAILQ_ENTRY(PAConnection
) list
;
19 pa_threaded_mainloop
*mainloop
;
23 static QTAILQ_HEAD(PAConnectionHead
, PAConnection
) pa_conns
=
24 QTAILQ_HEAD_INITIALIZER(pa_conns
);
41 const void *read_data
;
47 static void qpa_conn_fini(PAConnection
*c
);
49 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
54 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
57 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
60 #ifndef PA_CONTEXT_IS_GOOD
61 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
64 x
== PA_CONTEXT_CONNECTING
||
65 x
== PA_CONTEXT_AUTHORIZING
||
66 x
== PA_CONTEXT_SETTING_NAME
||
67 x
== PA_CONTEXT_READY
;
71 #ifndef PA_STREAM_IS_GOOD
72 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
75 x
== PA_STREAM_CREATING
||
80 #define CHECK_SUCCESS_GOTO(c, expression, label, msg) \
82 if (!(expression)) { \
83 qpa_logerr(pa_context_errno((c)->context), msg); \
88 #define CHECK_DEAD_GOTO(c, stream, label, msg) \
90 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
91 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
92 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
93 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
94 qpa_logerr(pa_context_errno((c)->context), msg); \
96 qpa_logerr(PA_ERR_BADSTATE, msg); \
102 static size_t qpa_read(HWVoiceIn
*hw
, void *data
, size_t length
)
104 PAVoiceIn
*p
= (PAVoiceIn
*) hw
;
105 PAConnection
*c
= p
->g
->conn
;
109 pa_threaded_mainloop_lock(c
->mainloop
);
111 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
112 "pa_threaded_mainloop_lock failed\n");
114 if (!p
->read_length
) {
115 r
= pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
);
116 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock_and_fail
,
117 "pa_stream_peek failed\n");
120 l
= MIN(p
->read_length
, length
);
121 memcpy(data
, p
->read_data
, l
);
126 if (!p
->read_length
) {
127 r
= pa_stream_drop(p
->stream
);
128 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock_and_fail
,
129 "pa_stream_drop failed\n");
132 pa_threaded_mainloop_unlock(c
->mainloop
);
136 pa_threaded_mainloop_unlock(c
->mainloop
);
140 static size_t qpa_write(HWVoiceOut
*hw
, void *data
, size_t length
)
142 PAVoiceOut
*p
= (PAVoiceOut
*) hw
;
143 PAConnection
*c
= p
->g
->conn
;
147 pa_threaded_mainloop_lock(c
->mainloop
);
149 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
150 "pa_threaded_mainloop_lock failed\n");
152 l
= pa_stream_writable_size(p
->stream
);
154 CHECK_SUCCESS_GOTO(c
, l
!= (size_t) -1, unlock_and_fail
,
155 "pa_stream_writable_size failed\n");
161 r
= pa_stream_write(p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
162 CHECK_SUCCESS_GOTO(c
, r
>= 0, unlock_and_fail
, "pa_stream_write failed\n");
164 pa_threaded_mainloop_unlock(c
->mainloop
);
168 pa_threaded_mainloop_unlock(c
->mainloop
);
172 static pa_sample_format_t
audfmt_to_pa (AudioFormat afmt
, int endianness
)
177 case AUDIO_FORMAT_S8
:
178 case AUDIO_FORMAT_U8
:
179 format
= PA_SAMPLE_U8
;
181 case AUDIO_FORMAT_S16
:
182 case AUDIO_FORMAT_U16
:
183 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
185 case AUDIO_FORMAT_S32
:
186 case AUDIO_FORMAT_U32
:
187 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
190 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
191 format
= PA_SAMPLE_U8
;
197 static AudioFormat
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
201 return AUDIO_FORMAT_U8
;
202 case PA_SAMPLE_S16BE
:
204 return AUDIO_FORMAT_S16
;
205 case PA_SAMPLE_S16LE
:
207 return AUDIO_FORMAT_S16
;
208 case PA_SAMPLE_S32BE
:
210 return AUDIO_FORMAT_S32
;
211 case PA_SAMPLE_S32LE
:
213 return AUDIO_FORMAT_S32
;
215 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
216 return AUDIO_FORMAT_U8
;
220 static void context_state_cb (pa_context
*c
, void *userdata
)
222 PAConnection
*conn
= userdata
;
224 switch (pa_context_get_state(c
)) {
225 case PA_CONTEXT_READY
:
226 case PA_CONTEXT_TERMINATED
:
227 case PA_CONTEXT_FAILED
:
228 pa_threaded_mainloop_signal(conn
->mainloop
, 0);
231 case PA_CONTEXT_UNCONNECTED
:
232 case PA_CONTEXT_CONNECTING
:
233 case PA_CONTEXT_AUTHORIZING
:
234 case PA_CONTEXT_SETTING_NAME
:
239 static void stream_state_cb (pa_stream
*s
, void * userdata
)
241 PAConnection
*c
= userdata
;
243 switch (pa_stream_get_state (s
)) {
245 case PA_STREAM_READY
:
246 case PA_STREAM_FAILED
:
247 case PA_STREAM_TERMINATED
:
248 pa_threaded_mainloop_signal(c
->mainloop
, 0);
251 case PA_STREAM_UNCONNECTED
:
252 case PA_STREAM_CREATING
:
257 static pa_stream
*qpa_simple_new (
260 pa_stream_direction_t dir
,
262 const pa_sample_spec
*ss
,
263 const pa_channel_map
*map
,
264 const pa_buffer_attr
*attr
,
269 pa_stream_flags_t flags
;
271 pa_threaded_mainloop_lock(c
->mainloop
);
273 stream
= pa_stream_new(c
->context
, name
, ss
, map
);
278 pa_stream_set_state_callback(stream
, stream_state_cb
, c
);
281 PA_STREAM_INTERPOLATE_TIMING
282 | PA_STREAM_AUTO_TIMING_UPDATE
283 | PA_STREAM_EARLY_REQUESTS
;
286 /* don't move the stream if the user specified a sink/source */
287 flags
|= PA_STREAM_DONT_MOVE
;
290 if (dir
== PA_STREAM_PLAYBACK
) {
291 r
= pa_stream_connect_playback(stream
, dev
, attr
, flags
, NULL
, NULL
);
293 r
= pa_stream_connect_record(stream
, dev
, attr
, flags
);
300 pa_threaded_mainloop_unlock(c
->mainloop
);
305 pa_threaded_mainloop_unlock(c
->mainloop
);
308 pa_stream_unref (stream
);
311 *rerror
= pa_context_errno(c
->context
);
316 static int qpa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
322 struct audsettings obt_as
= *as
;
323 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
324 paaudio
*g
= pa
->g
= drv_opaque
;
325 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
326 AudiodevPaPerDirectionOptions
*ppdo
= popts
->out
;
327 PAConnection
*c
= g
->conn
;
329 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
330 ss
.channels
= as
->nchannels
;
333 ba
.tlength
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
338 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
340 pa
->stream
= qpa_simple_new (
344 ppdo
->has_name
? ppdo
->name
: NULL
,
346 NULL
, /* channel map */
347 &ba
, /* buffering attributes */
351 qpa_logerr (error
, "pa_simple_new for playback failed\n");
355 audio_pcm_init_info (&hw
->info
, &obt_as
);
356 hw
->samples
= pa
->samples
= audio_buffer_samples(
357 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
358 &obt_as
, ppdo
->buffer_length
);
366 static int qpa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
371 struct audsettings obt_as
= *as
;
372 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
373 paaudio
*g
= pa
->g
= drv_opaque
;
374 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
375 AudiodevPaPerDirectionOptions
*ppdo
= popts
->in
;
376 PAConnection
*c
= g
->conn
;
378 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
379 ss
.channels
= as
->nchannels
;
382 ba
.fragsize
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
383 ba
.maxlength
= pa_usec_to_bytes(ppdo
->latency
* 2, &ss
);
387 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
389 pa
->stream
= qpa_simple_new (
393 ppdo
->has_name
? ppdo
->name
: NULL
,
395 NULL
, /* channel map */
396 &ba
, /* buffering attributes */
400 qpa_logerr (error
, "pa_simple_new for capture failed\n");
404 audio_pcm_init_info (&hw
->info
, &obt_as
);
405 hw
->samples
= pa
->samples
= audio_buffer_samples(
406 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
407 &obt_as
, ppdo
->buffer_length
);
415 static void qpa_simple_disconnect(PAConnection
*c
, pa_stream
*stream
)
419 pa_threaded_mainloop_lock(c
->mainloop
);
421 * wait until actually connects. workaround pa bug #247
422 * https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/247
424 while (pa_stream_get_state(stream
) == PA_STREAM_CREATING
) {
425 pa_threaded_mainloop_wait(c
->mainloop
);
428 err
= pa_stream_disconnect(stream
);
430 dolog("Failed to disconnect! err=%d\n", err
);
432 pa_stream_unref(stream
);
433 pa_threaded_mainloop_unlock(c
->mainloop
);
436 static void qpa_fini_out (HWVoiceOut
*hw
)
438 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
441 qpa_simple_disconnect(pa
->g
->conn
, pa
->stream
);
446 static void qpa_fini_in (HWVoiceIn
*hw
)
448 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
451 qpa_simple_disconnect(pa
->g
->conn
, pa
->stream
);
456 static void qpa_volume_out(HWVoiceOut
*hw
, struct mixeng_volume
*vol
)
458 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
461 PAConnection
*c
= pa
->g
->conn
;
463 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
464 pa_cvolume_init (&v
); /* function is present in 0.9.13+ */
468 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->l
) / UINT32_MAX
;
469 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->r
) / UINT32_MAX
;
471 pa_threaded_mainloop_lock(c
->mainloop
);
473 op
= pa_context_set_sink_input_volume(c
->context
,
474 pa_stream_get_index(pa
->stream
),
477 qpa_logerr(pa_context_errno(c
->context
),
478 "set_sink_input_volume() failed\n");
480 pa_operation_unref(op
);
483 op
= pa_context_set_sink_input_mute(c
->context
,
484 pa_stream_get_index(pa
->stream
),
485 vol
->mute
, NULL
, NULL
);
487 qpa_logerr(pa_context_errno(c
->context
),
488 "set_sink_input_mute() failed\n");
490 pa_operation_unref(op
);
493 pa_threaded_mainloop_unlock(c
->mainloop
);
496 static void qpa_volume_in(HWVoiceIn
*hw
, struct mixeng_volume
*vol
)
498 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
501 PAConnection
*c
= pa
->g
->conn
;
503 #ifdef PA_CHECK_VERSION
504 pa_cvolume_init (&v
);
508 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->l
) / UINT32_MAX
;
509 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->r
) / UINT32_MAX
;
511 pa_threaded_mainloop_lock(c
->mainloop
);
513 op
= pa_context_set_source_output_volume(c
->context
,
514 pa_stream_get_index(pa
->stream
),
517 qpa_logerr(pa_context_errno(c
->context
),
518 "set_source_output_volume() failed\n");
520 pa_operation_unref(op
);
523 op
= pa_context_set_source_output_mute(c
->context
,
524 pa_stream_get_index(pa
->stream
),
525 vol
->mute
, NULL
, NULL
);
527 qpa_logerr(pa_context_errno(c
->context
),
528 "set_source_output_mute() failed\n");
530 pa_operation_unref(op
);
533 pa_threaded_mainloop_unlock(c
->mainloop
);
536 static int qpa_validate_per_direction_opts(Audiodev
*dev
,
537 AudiodevPaPerDirectionOptions
*pdo
)
539 if (!pdo
->has_buffer_length
) {
540 pdo
->has_buffer_length
= true;
541 pdo
->buffer_length
= 46440;
543 if (!pdo
->has_latency
) {
544 pdo
->has_latency
= true;
545 pdo
->latency
= 15000;
551 static void *qpa_conn_init(const char *server
)
554 PAConnection
*c
= g_malloc0(sizeof(PAConnection
));
555 QTAILQ_INSERT_TAIL(&pa_conns
, c
, list
);
557 c
->mainloop
= pa_threaded_mainloop_new();
562 vm_name
= qemu_get_vm_name();
563 c
->context
= pa_context_new(pa_threaded_mainloop_get_api(c
->mainloop
),
564 vm_name
? vm_name
: "qemu");
569 pa_context_set_state_callback(c
->context
, context_state_cb
, c
);
571 if (pa_context_connect(c
->context
, server
, 0, NULL
) < 0) {
572 qpa_logerr(pa_context_errno(c
->context
),
573 "pa_context_connect() failed\n");
577 pa_threaded_mainloop_lock(c
->mainloop
);
579 if (pa_threaded_mainloop_start(c
->mainloop
) < 0) {
580 goto unlock_and_fail
;
584 pa_context_state_t state
;
586 state
= pa_context_get_state(c
->context
);
588 if (state
== PA_CONTEXT_READY
) {
592 if (!PA_CONTEXT_IS_GOOD(state
)) {
593 qpa_logerr(pa_context_errno(c
->context
),
594 "Wrong context state\n");
595 goto unlock_and_fail
;
598 /* Wait until the context is ready */
599 pa_threaded_mainloop_wait(c
->mainloop
);
602 pa_threaded_mainloop_unlock(c
->mainloop
);
606 pa_threaded_mainloop_unlock(c
->mainloop
);
608 AUD_log (AUDIO_CAP
, "Failed to initialize PA context");
613 static void *qpa_audio_init(Audiodev
*dev
)
616 AudiodevPaOptions
*popts
= &dev
->u
.pa
;
620 assert(dev
->driver
== AUDIODEV_DRIVER_PA
);
622 if (!popts
->has_server
) {
627 runtime
= getenv("XDG_RUNTIME_DIR");
631 snprintf(pidfile
, sizeof(pidfile
), "%s/pulse/pid", runtime
);
632 if (stat(pidfile
, &st
) != 0) {
637 if (!qpa_validate_per_direction_opts(dev
, popts
->in
)) {
640 if (!qpa_validate_per_direction_opts(dev
, popts
->out
)) {
644 g
= g_malloc0(sizeof(paaudio
));
645 server
= popts
->has_server
? popts
->server
: NULL
;
649 QTAILQ_FOREACH(c
, &pa_conns
, list
) {
650 if (server
== NULL
|| c
->server
== NULL
?
651 server
== c
->server
:
652 strcmp(server
, c
->server
) == 0) {
658 g
->conn
= qpa_conn_init(server
);
669 static void qpa_conn_fini(PAConnection
*c
)
672 pa_threaded_mainloop_stop(c
->mainloop
);
676 pa_context_disconnect(c
->context
);
677 pa_context_unref(c
->context
);
681 pa_threaded_mainloop_free(c
->mainloop
);
684 QTAILQ_REMOVE(&pa_conns
, c
, list
);
688 static void qpa_audio_fini (void *opaque
)
691 PAConnection
*c
= g
->conn
;
693 if (--c
->refcount
== 0) {
700 static struct audio_pcm_ops qpa_pcm_ops
= {
701 .init_out
= qpa_init_out
,
702 .fini_out
= qpa_fini_out
,
704 .volume_out
= qpa_volume_out
,
706 .init_in
= qpa_init_in
,
707 .fini_in
= qpa_fini_in
,
709 .volume_in
= qpa_volume_in
712 static struct audio_driver pa_audio_driver
= {
714 .descr
= "http://www.pulseaudio.org/",
715 .init
= qpa_audio_init
,
716 .fini
= qpa_audio_fini
,
717 .pcm_ops
= &qpa_pcm_ops
,
719 .max_voices_out
= INT_MAX
,
720 .max_voices_in
= INT_MAX
,
721 .voice_size_out
= sizeof (PAVoiceOut
),
722 .voice_size_in
= sizeof (PAVoiceIn
),
725 static void register_audio_pa(void)
727 audio_driver_register(&pa_audio_driver
);
729 type_init(register_audio_pa
);