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 void *qpa_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
104 PAVoiceIn
*p
= (PAVoiceIn
*) hw
;
105 PAConnection
*c
= p
->g
->conn
;
108 pa_threaded_mainloop_lock(c
->mainloop
);
110 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
111 "pa_threaded_mainloop_lock failed\n");
113 if (!p
->read_length
) {
114 r
= pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
);
115 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock_and_fail
,
116 "pa_stream_peek failed\n");
119 *size
= MIN(p
->read_length
, *size
);
121 pa_threaded_mainloop_unlock(c
->mainloop
);
122 return (void *) p
->read_data
;
125 pa_threaded_mainloop_unlock(c
->mainloop
);
130 static void qpa_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
132 PAVoiceIn
*p
= (PAVoiceIn
*) hw
;
133 PAConnection
*c
= p
->g
->conn
;
136 pa_threaded_mainloop_lock(c
->mainloop
);
138 CHECK_DEAD_GOTO(c
, p
->stream
, unlock
,
139 "pa_threaded_mainloop_lock failed\n");
141 assert(buf
== p
->read_data
&& size
<= p
->read_length
);
143 p
->read_data
+= size
;
144 p
->read_length
-= size
;
146 if (size
&& !p
->read_length
) {
147 r
= pa_stream_drop(p
->stream
);
148 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock
, "pa_stream_drop failed\n");
152 pa_threaded_mainloop_unlock(c
->mainloop
);
155 static size_t qpa_read(HWVoiceIn
*hw
, void *data
, size_t length
)
157 PAVoiceIn
*p
= (PAVoiceIn
*) hw
;
158 PAConnection
*c
= p
->g
->conn
;
162 pa_threaded_mainloop_lock(c
->mainloop
);
164 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
165 "pa_threaded_mainloop_lock failed\n");
167 if (!p
->read_length
) {
168 r
= pa_stream_peek(p
->stream
, &p
->read_data
, &p
->read_length
);
169 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock_and_fail
,
170 "pa_stream_peek failed\n");
173 l
= MIN(p
->read_length
, length
);
174 memcpy(data
, p
->read_data
, l
);
179 if (!p
->read_length
) {
180 r
= pa_stream_drop(p
->stream
);
181 CHECK_SUCCESS_GOTO(c
, r
== 0, unlock_and_fail
,
182 "pa_stream_drop failed\n");
185 pa_threaded_mainloop_unlock(c
->mainloop
);
189 pa_threaded_mainloop_unlock(c
->mainloop
);
193 static void *qpa_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
195 PAVoiceOut
*p
= (PAVoiceOut
*) hw
;
196 PAConnection
*c
= p
->g
->conn
;
200 pa_threaded_mainloop_lock(c
->mainloop
);
202 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
203 "pa_threaded_mainloop_lock failed\n");
206 r
= pa_stream_begin_write(p
->stream
, &ret
, size
);
207 CHECK_SUCCESS_GOTO(c
, r
>= 0, unlock_and_fail
,
208 "pa_stream_begin_write failed\n");
210 pa_threaded_mainloop_unlock(c
->mainloop
);
214 pa_threaded_mainloop_unlock(c
->mainloop
);
219 static size_t qpa_write(HWVoiceOut
*hw
, void *data
, size_t length
)
221 PAVoiceOut
*p
= (PAVoiceOut
*) hw
;
222 PAConnection
*c
= p
->g
->conn
;
226 pa_threaded_mainloop_lock(c
->mainloop
);
228 CHECK_DEAD_GOTO(c
, p
->stream
, unlock_and_fail
,
229 "pa_threaded_mainloop_lock failed\n");
231 l
= pa_stream_writable_size(p
->stream
);
233 CHECK_SUCCESS_GOTO(c
, l
!= (size_t) -1, unlock_and_fail
,
234 "pa_stream_writable_size failed\n");
240 r
= pa_stream_write(p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
241 CHECK_SUCCESS_GOTO(c
, r
>= 0, unlock_and_fail
, "pa_stream_write failed\n");
243 pa_threaded_mainloop_unlock(c
->mainloop
);
247 pa_threaded_mainloop_unlock(c
->mainloop
);
251 static pa_sample_format_t
audfmt_to_pa (AudioFormat afmt
, int endianness
)
256 case AUDIO_FORMAT_S8
:
257 case AUDIO_FORMAT_U8
:
258 format
= PA_SAMPLE_U8
;
260 case AUDIO_FORMAT_S16
:
261 case AUDIO_FORMAT_U16
:
262 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
264 case AUDIO_FORMAT_S32
:
265 case AUDIO_FORMAT_U32
:
266 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
269 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
270 format
= PA_SAMPLE_U8
;
276 static AudioFormat
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
280 return AUDIO_FORMAT_U8
;
281 case PA_SAMPLE_S16BE
:
283 return AUDIO_FORMAT_S16
;
284 case PA_SAMPLE_S16LE
:
286 return AUDIO_FORMAT_S16
;
287 case PA_SAMPLE_S32BE
:
289 return AUDIO_FORMAT_S32
;
290 case PA_SAMPLE_S32LE
:
292 return AUDIO_FORMAT_S32
;
294 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
295 return AUDIO_FORMAT_U8
;
299 static void context_state_cb (pa_context
*c
, void *userdata
)
301 PAConnection
*conn
= userdata
;
303 switch (pa_context_get_state(c
)) {
304 case PA_CONTEXT_READY
:
305 case PA_CONTEXT_TERMINATED
:
306 case PA_CONTEXT_FAILED
:
307 pa_threaded_mainloop_signal(conn
->mainloop
, 0);
310 case PA_CONTEXT_UNCONNECTED
:
311 case PA_CONTEXT_CONNECTING
:
312 case PA_CONTEXT_AUTHORIZING
:
313 case PA_CONTEXT_SETTING_NAME
:
318 static void stream_state_cb (pa_stream
*s
, void * userdata
)
320 PAConnection
*c
= userdata
;
322 switch (pa_stream_get_state (s
)) {
324 case PA_STREAM_READY
:
325 case PA_STREAM_FAILED
:
326 case PA_STREAM_TERMINATED
:
327 pa_threaded_mainloop_signal(c
->mainloop
, 0);
330 case PA_STREAM_UNCONNECTED
:
331 case PA_STREAM_CREATING
:
336 static pa_stream
*qpa_simple_new (
339 pa_stream_direction_t dir
,
341 const pa_sample_spec
*ss
,
342 const pa_buffer_attr
*attr
,
346 pa_stream
*stream
= NULL
;
347 pa_stream_flags_t flags
;
350 pa_threaded_mainloop_lock(c
->mainloop
);
352 pa_channel_map_init(&map
);
353 map
.channels
= ss
->channels
;
356 * TODO: This currently expects the only frontend supporting more than 2
357 * channels is the usb-audio. We will need some means to set channel
358 * order when a new frontend gains multi-channel support.
360 switch (ss
->channels
) {
362 map
.map
[0] = PA_CHANNEL_POSITION_MONO
;
366 map
.map
[0] = PA_CHANNEL_POSITION_LEFT
;
367 map
.map
[1] = PA_CHANNEL_POSITION_RIGHT
;
371 map
.map
[0] = PA_CHANNEL_POSITION_FRONT_LEFT
;
372 map
.map
[1] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
373 map
.map
[2] = PA_CHANNEL_POSITION_CENTER
;
374 map
.map
[3] = PA_CHANNEL_POSITION_LFE
;
375 map
.map
[4] = PA_CHANNEL_POSITION_REAR_LEFT
;
376 map
.map
[5] = PA_CHANNEL_POSITION_REAR_RIGHT
;
380 map
.map
[0] = PA_CHANNEL_POSITION_FRONT_LEFT
;
381 map
.map
[1] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
382 map
.map
[2] = PA_CHANNEL_POSITION_CENTER
;
383 map
.map
[3] = PA_CHANNEL_POSITION_LFE
;
384 map
.map
[4] = PA_CHANNEL_POSITION_REAR_LEFT
;
385 map
.map
[5] = PA_CHANNEL_POSITION_REAR_RIGHT
;
386 map
.map
[6] = PA_CHANNEL_POSITION_SIDE_LEFT
;
387 map
.map
[7] = PA_CHANNEL_POSITION_SIDE_RIGHT
;
390 dolog("Internal error: unsupported channel count %d\n", ss
->channels
);
394 stream
= pa_stream_new(c
->context
, name
, ss
, &map
);
399 pa_stream_set_state_callback(stream
, stream_state_cb
, c
);
402 PA_STREAM_INTERPOLATE_TIMING
403 | PA_STREAM_AUTO_TIMING_UPDATE
404 | PA_STREAM_EARLY_REQUESTS
;
407 /* don't move the stream if the user specified a sink/source */
408 flags
|= PA_STREAM_DONT_MOVE
;
411 if (dir
== PA_STREAM_PLAYBACK
) {
412 r
= pa_stream_connect_playback(stream
, dev
, attr
, flags
, NULL
, NULL
);
414 r
= pa_stream_connect_record(stream
, dev
, attr
, flags
);
421 pa_threaded_mainloop_unlock(c
->mainloop
);
426 pa_threaded_mainloop_unlock(c
->mainloop
);
429 pa_stream_unref (stream
);
432 *rerror
= pa_context_errno(c
->context
);
437 static int qpa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
443 struct audsettings obt_as
= *as
;
444 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
445 paaudio
*g
= pa
->g
= drv_opaque
;
446 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
447 AudiodevPaPerDirectionOptions
*ppdo
= popts
->out
;
448 PAConnection
*c
= g
->conn
;
450 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
451 ss
.channels
= as
->nchannels
;
454 ba
.tlength
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
459 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
461 pa
->stream
= qpa_simple_new (
463 ppdo
->has_stream_name
? ppdo
->stream_name
: g
->dev
->id
,
465 ppdo
->has_name
? ppdo
->name
: NULL
,
467 &ba
, /* buffering attributes */
471 qpa_logerr (error
, "pa_simple_new for playback failed\n");
475 audio_pcm_init_info (&hw
->info
, &obt_as
);
476 hw
->samples
= pa
->samples
= audio_buffer_samples(
477 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
478 &obt_as
, ppdo
->buffer_length
);
486 static int qpa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
491 struct audsettings obt_as
= *as
;
492 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
493 paaudio
*g
= pa
->g
= drv_opaque
;
494 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
495 AudiodevPaPerDirectionOptions
*ppdo
= popts
->in
;
496 PAConnection
*c
= g
->conn
;
498 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
499 ss
.channels
= as
->nchannels
;
502 ba
.fragsize
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
503 ba
.maxlength
= pa_usec_to_bytes(ppdo
->latency
* 2, &ss
);
507 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
509 pa
->stream
= qpa_simple_new (
511 ppdo
->has_stream_name
? ppdo
->stream_name
: g
->dev
->id
,
513 ppdo
->has_name
? ppdo
->name
: NULL
,
515 &ba
, /* buffering attributes */
519 qpa_logerr (error
, "pa_simple_new for capture failed\n");
523 audio_pcm_init_info (&hw
->info
, &obt_as
);
524 hw
->samples
= pa
->samples
= audio_buffer_samples(
525 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
526 &obt_as
, ppdo
->buffer_length
);
534 static void qpa_simple_disconnect(PAConnection
*c
, pa_stream
*stream
)
538 pa_threaded_mainloop_lock(c
->mainloop
);
540 * wait until actually connects. workaround pa bug #247
541 * https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/247
543 while (pa_stream_get_state(stream
) == PA_STREAM_CREATING
) {
544 pa_threaded_mainloop_wait(c
->mainloop
);
547 err
= pa_stream_disconnect(stream
);
549 dolog("Failed to disconnect! err=%d\n", err
);
551 pa_stream_unref(stream
);
552 pa_threaded_mainloop_unlock(c
->mainloop
);
555 static void qpa_fini_out (HWVoiceOut
*hw
)
557 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
560 qpa_simple_disconnect(pa
->g
->conn
, pa
->stream
);
565 static void qpa_fini_in (HWVoiceIn
*hw
)
567 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
570 qpa_simple_disconnect(pa
->g
->conn
, pa
->stream
);
575 static void qpa_volume_out(HWVoiceOut
*hw
, Volume
*vol
)
577 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
580 PAConnection
*c
= pa
->g
->conn
;
583 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
584 pa_cvolume_init (&v
); /* function is present in 0.9.13+ */
587 v
.channels
= vol
->channels
;
588 for (i
= 0; i
< vol
->channels
; ++i
) {
589 v
.values
[i
] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->vol
[i
]) / 255;
592 pa_threaded_mainloop_lock(c
->mainloop
);
594 op
= pa_context_set_sink_input_volume(c
->context
,
595 pa_stream_get_index(pa
->stream
),
598 qpa_logerr(pa_context_errno(c
->context
),
599 "set_sink_input_volume() failed\n");
601 pa_operation_unref(op
);
604 op
= pa_context_set_sink_input_mute(c
->context
,
605 pa_stream_get_index(pa
->stream
),
606 vol
->mute
, NULL
, NULL
);
608 qpa_logerr(pa_context_errno(c
->context
),
609 "set_sink_input_mute() failed\n");
611 pa_operation_unref(op
);
614 pa_threaded_mainloop_unlock(c
->mainloop
);
617 static void qpa_volume_in(HWVoiceIn
*hw
, Volume
*vol
)
619 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
622 PAConnection
*c
= pa
->g
->conn
;
625 #ifdef PA_CHECK_VERSION
626 pa_cvolume_init (&v
);
629 v
.channels
= vol
->channels
;
630 for (i
= 0; i
< vol
->channels
; ++i
) {
631 v
.values
[i
] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * vol
->vol
[i
]) / 255;
634 pa_threaded_mainloop_lock(c
->mainloop
);
636 op
= pa_context_set_source_output_volume(c
->context
,
637 pa_stream_get_index(pa
->stream
),
640 qpa_logerr(pa_context_errno(c
->context
),
641 "set_source_output_volume() failed\n");
643 pa_operation_unref(op
);
646 op
= pa_context_set_source_output_mute(c
->context
,
647 pa_stream_get_index(pa
->stream
),
648 vol
->mute
, NULL
, NULL
);
650 qpa_logerr(pa_context_errno(c
->context
),
651 "set_source_output_mute() failed\n");
653 pa_operation_unref(op
);
656 pa_threaded_mainloop_unlock(c
->mainloop
);
659 static int qpa_validate_per_direction_opts(Audiodev
*dev
,
660 AudiodevPaPerDirectionOptions
*pdo
)
662 if (!pdo
->has_buffer_length
) {
663 pdo
->has_buffer_length
= true;
664 pdo
->buffer_length
= 46440;
666 if (!pdo
->has_latency
) {
667 pdo
->has_latency
= true;
668 pdo
->latency
= 15000;
674 static void *qpa_conn_init(const char *server
)
677 PAConnection
*c
= g_malloc0(sizeof(PAConnection
));
678 QTAILQ_INSERT_TAIL(&pa_conns
, c
, list
);
680 c
->mainloop
= pa_threaded_mainloop_new();
685 vm_name
= qemu_get_vm_name();
686 c
->context
= pa_context_new(pa_threaded_mainloop_get_api(c
->mainloop
),
687 vm_name
? vm_name
: "qemu");
692 pa_context_set_state_callback(c
->context
, context_state_cb
, c
);
694 if (pa_context_connect(c
->context
, server
, 0, NULL
) < 0) {
695 qpa_logerr(pa_context_errno(c
->context
),
696 "pa_context_connect() failed\n");
700 pa_threaded_mainloop_lock(c
->mainloop
);
702 if (pa_threaded_mainloop_start(c
->mainloop
) < 0) {
703 goto unlock_and_fail
;
707 pa_context_state_t state
;
709 state
= pa_context_get_state(c
->context
);
711 if (state
== PA_CONTEXT_READY
) {
715 if (!PA_CONTEXT_IS_GOOD(state
)) {
716 qpa_logerr(pa_context_errno(c
->context
),
717 "Wrong context state\n");
718 goto unlock_and_fail
;
721 /* Wait until the context is ready */
722 pa_threaded_mainloop_wait(c
->mainloop
);
725 pa_threaded_mainloop_unlock(c
->mainloop
);
729 pa_threaded_mainloop_unlock(c
->mainloop
);
731 AUD_log (AUDIO_CAP
, "Failed to initialize PA context");
736 static void *qpa_audio_init(Audiodev
*dev
)
739 AudiodevPaOptions
*popts
= &dev
->u
.pa
;
743 assert(dev
->driver
== AUDIODEV_DRIVER_PA
);
745 if (!popts
->has_server
) {
750 runtime
= getenv("XDG_RUNTIME_DIR");
754 snprintf(pidfile
, sizeof(pidfile
), "%s/pulse/pid", runtime
);
755 if (stat(pidfile
, &st
) != 0) {
760 if (!qpa_validate_per_direction_opts(dev
, popts
->in
)) {
763 if (!qpa_validate_per_direction_opts(dev
, popts
->out
)) {
767 g
= g_malloc0(sizeof(paaudio
));
768 server
= popts
->has_server
? popts
->server
: NULL
;
772 QTAILQ_FOREACH(c
, &pa_conns
, list
) {
773 if (server
== NULL
|| c
->server
== NULL
?
774 server
== c
->server
:
775 strcmp(server
, c
->server
) == 0) {
781 g
->conn
= qpa_conn_init(server
);
792 static void qpa_conn_fini(PAConnection
*c
)
795 pa_threaded_mainloop_stop(c
->mainloop
);
799 pa_context_disconnect(c
->context
);
800 pa_context_unref(c
->context
);
804 pa_threaded_mainloop_free(c
->mainloop
);
807 QTAILQ_REMOVE(&pa_conns
, c
, list
);
811 static void qpa_audio_fini (void *opaque
)
814 PAConnection
*c
= g
->conn
;
816 if (--c
->refcount
== 0) {
823 static struct audio_pcm_ops qpa_pcm_ops
= {
824 .init_out
= qpa_init_out
,
825 .fini_out
= qpa_fini_out
,
827 .get_buffer_out
= qpa_get_buffer_out
,
828 .put_buffer_out
= qpa_write
, /* pa handles it */
829 .volume_out
= qpa_volume_out
,
831 .init_in
= qpa_init_in
,
832 .fini_in
= qpa_fini_in
,
834 .get_buffer_in
= qpa_get_buffer_in
,
835 .put_buffer_in
= qpa_put_buffer_in
,
836 .volume_in
= qpa_volume_in
839 static struct audio_driver pa_audio_driver
= {
841 .descr
= "http://www.pulseaudio.org/",
842 .init
= qpa_audio_init
,
843 .fini
= qpa_audio_fini
,
844 .pcm_ops
= &qpa_pcm_ops
,
846 .max_voices_out
= INT_MAX
,
847 .max_voices_in
= INT_MAX
,
848 .voice_size_out
= sizeof (PAVoiceOut
),
849 .voice_size_in
= sizeof (PAVoiceIn
),
852 static void register_audio_pa(void)
854 audio_driver_register(&pa_audio_driver
);
856 type_init(register_audio_pa
);