3 #include "qemu/osdep.h"
4 #include "qemu/module.h"
6 #include "qapi/opts-visitor.h"
8 #include <pulse/pulseaudio.h>
10 #define AUDIO_CAP "pulseaudio"
11 #include "audio_int.h"
12 #include "audio_pt_int.h"
16 pa_threaded_mainloop
*mainloop
;
42 const void *read_data
;
43 size_t read_index
, read_length
;
48 static void qpa_audio_fini(void *opaque
);
50 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
55 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
58 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
61 #ifndef PA_CONTEXT_IS_GOOD
62 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
65 x
== PA_CONTEXT_CONNECTING
||
66 x
== PA_CONTEXT_AUTHORIZING
||
67 x
== PA_CONTEXT_SETTING_NAME
||
68 x
== PA_CONTEXT_READY
;
72 #ifndef PA_STREAM_IS_GOOD
73 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
76 x
== PA_STREAM_CREATING
||
81 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
83 if (!(expression)) { \
85 *(rerror) = pa_context_errno ((c)->context); \
91 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
93 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
94 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
95 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
96 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
98 *(rerror) = pa_context_errno ((c)->context); \
102 *(rerror) = PA_ERR_BADSTATE; \
109 static int qpa_simple_read (PAVoiceIn
*p
, void *data
, size_t length
, int *rerror
)
113 pa_threaded_mainloop_lock (g
->mainloop
);
115 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
120 while (!p
->read_data
) {
123 r
= pa_stream_peek (p
->stream
, &p
->read_data
, &p
->read_length
);
124 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
127 pa_threaded_mainloop_wait (g
->mainloop
);
128 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
134 l
= p
->read_length
< length
? p
->read_length
: length
;
135 memcpy (data
, (const uint8_t *) p
->read_data
+p
->read_index
, l
);
137 data
= (uint8_t *) data
+ l
;
143 if (!p
->read_length
) {
146 r
= pa_stream_drop (p
->stream
);
151 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
155 pa_threaded_mainloop_unlock (g
->mainloop
);
159 pa_threaded_mainloop_unlock (g
->mainloop
);
163 static int qpa_simple_write (PAVoiceOut
*p
, const void *data
, size_t length
, int *rerror
)
167 pa_threaded_mainloop_lock (g
->mainloop
);
169 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
175 while (!(l
= pa_stream_writable_size (p
->stream
))) {
176 pa_threaded_mainloop_wait (g
->mainloop
);
177 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
180 CHECK_SUCCESS_GOTO (g
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
186 r
= pa_stream_write (p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
187 CHECK_SUCCESS_GOTO (g
, rerror
, r
>= 0, unlock_and_fail
);
189 data
= (const uint8_t *) data
+ l
;
193 pa_threaded_mainloop_unlock (g
->mainloop
);
197 pa_threaded_mainloop_unlock (g
->mainloop
);
201 static void *qpa_thread_out (void *arg
)
203 PAVoiceOut
*pa
= arg
;
204 HWVoiceOut
*hw
= &pa
->hw
;
206 if (audio_pt_lock(&pa
->pt
, __func__
)) {
211 int decr
, to_mix
, rpos
;
222 if (audio_pt_wait(&pa
->pt
, __func__
)) {
227 decr
= to_mix
= audio_MIN(pa
->live
, pa
->samples
>> 5);
230 if (audio_pt_unlock(&pa
->pt
, __func__
)) {
236 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
237 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
239 hw
->clip (pa
->pcm_buf
, src
, chunk
);
241 if (qpa_simple_write (pa
, pa
->pcm_buf
,
242 chunk
<< hw
->info
.shift
, &error
) < 0) {
243 qpa_logerr (error
, "pa_simple_write failed\n");
247 rpos
= (rpos
+ chunk
) % hw
->samples
;
251 if (audio_pt_lock(&pa
->pt
, __func__
)) {
261 audio_pt_unlock(&pa
->pt
, __func__
);
265 static int qpa_run_out (HWVoiceOut
*hw
, int live
)
268 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
270 if (audio_pt_lock(&pa
->pt
, __func__
)) {
274 decr
= audio_MIN (live
, pa
->decr
);
276 pa
->live
= live
- decr
;
279 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
282 audio_pt_unlock(&pa
->pt
, __func__
);
287 static int qpa_write (SWVoiceOut
*sw
, void *buf
, int len
)
289 return audio_pcm_sw_write (sw
, buf
, len
);
293 static void *qpa_thread_in (void *arg
)
296 HWVoiceIn
*hw
= &pa
->hw
;
298 if (audio_pt_lock(&pa
->pt
, __func__
)) {
303 int incr
, to_grab
, wpos
;
314 if (audio_pt_wait(&pa
->pt
, __func__
)) {
319 incr
= to_grab
= audio_MIN(pa
->dead
, pa
->samples
>> 5);
322 if (audio_pt_unlock(&pa
->pt
, __func__
)) {
328 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
329 void *buf
= advance (pa
->pcm_buf
, wpos
);
331 if (qpa_simple_read (pa
, buf
,
332 chunk
<< hw
->info
.shift
, &error
) < 0) {
333 qpa_logerr (error
, "pa_simple_read failed\n");
337 hw
->conv (hw
->conv_buf
+ wpos
, buf
, chunk
);
338 wpos
= (wpos
+ chunk
) % hw
->samples
;
342 if (audio_pt_lock(&pa
->pt
, __func__
)) {
352 audio_pt_unlock(&pa
->pt
, __func__
);
356 static int qpa_run_in (HWVoiceIn
*hw
)
358 int live
, incr
, dead
;
359 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
361 if (audio_pt_lock(&pa
->pt
, __func__
)) {
365 live
= audio_pcm_hw_get_live_in (hw
);
366 dead
= hw
->samples
- live
;
367 incr
= audio_MIN (dead
, pa
->incr
);
369 pa
->dead
= dead
- incr
;
372 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
375 audio_pt_unlock(&pa
->pt
, __func__
);
380 static int qpa_read (SWVoiceIn
*sw
, void *buf
, int len
)
382 return audio_pcm_sw_read (sw
, buf
, len
);
385 static pa_sample_format_t
audfmt_to_pa (AudioFormat afmt
, int endianness
)
390 case AUDIO_FORMAT_S8
:
391 case AUDIO_FORMAT_U8
:
392 format
= PA_SAMPLE_U8
;
394 case AUDIO_FORMAT_S16
:
395 case AUDIO_FORMAT_U16
:
396 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
398 case AUDIO_FORMAT_S32
:
399 case AUDIO_FORMAT_U32
:
400 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
403 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
404 format
= PA_SAMPLE_U8
;
410 static AudioFormat
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
414 return AUDIO_FORMAT_U8
;
415 case PA_SAMPLE_S16BE
:
417 return AUDIO_FORMAT_S16
;
418 case PA_SAMPLE_S16LE
:
420 return AUDIO_FORMAT_S16
;
421 case PA_SAMPLE_S32BE
:
423 return AUDIO_FORMAT_S32
;
424 case PA_SAMPLE_S32LE
:
426 return AUDIO_FORMAT_S32
;
428 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
429 return AUDIO_FORMAT_U8
;
433 static void context_state_cb (pa_context
*c
, void *userdata
)
435 paaudio
*g
= userdata
;
437 switch (pa_context_get_state(c
)) {
438 case PA_CONTEXT_READY
:
439 case PA_CONTEXT_TERMINATED
:
440 case PA_CONTEXT_FAILED
:
441 pa_threaded_mainloop_signal (g
->mainloop
, 0);
444 case PA_CONTEXT_UNCONNECTED
:
445 case PA_CONTEXT_CONNECTING
:
446 case PA_CONTEXT_AUTHORIZING
:
447 case PA_CONTEXT_SETTING_NAME
:
452 static void stream_state_cb (pa_stream
*s
, void * userdata
)
454 paaudio
*g
= userdata
;
456 switch (pa_stream_get_state (s
)) {
458 case PA_STREAM_READY
:
459 case PA_STREAM_FAILED
:
460 case PA_STREAM_TERMINATED
:
461 pa_threaded_mainloop_signal (g
->mainloop
, 0);
464 case PA_STREAM_UNCONNECTED
:
465 case PA_STREAM_CREATING
:
470 static void stream_request_cb (pa_stream
*s
, size_t length
, void *userdata
)
472 paaudio
*g
= userdata
;
474 pa_threaded_mainloop_signal (g
->mainloop
, 0);
477 static pa_stream
*qpa_simple_new (
480 pa_stream_direction_t dir
,
482 const pa_sample_spec
*ss
,
483 const pa_channel_map
*map
,
484 const pa_buffer_attr
*attr
,
490 pa_threaded_mainloop_lock (g
->mainloop
);
492 stream
= pa_stream_new (g
->context
, name
, ss
, map
);
497 pa_stream_set_state_callback (stream
, stream_state_cb
, g
);
498 pa_stream_set_read_callback (stream
, stream_request_cb
, g
);
499 pa_stream_set_write_callback (stream
, stream_request_cb
, g
);
501 if (dir
== PA_STREAM_PLAYBACK
) {
502 r
= pa_stream_connect_playback (stream
, dev
, attr
,
503 PA_STREAM_INTERPOLATE_TIMING
504 #ifdef PA_STREAM_ADJUST_LATENCY
505 |PA_STREAM_ADJUST_LATENCY
507 |PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
);
509 r
= pa_stream_connect_record (stream
, dev
, attr
,
510 PA_STREAM_INTERPOLATE_TIMING
511 #ifdef PA_STREAM_ADJUST_LATENCY
512 |PA_STREAM_ADJUST_LATENCY
514 |PA_STREAM_AUTO_TIMING_UPDATE
);
521 pa_threaded_mainloop_unlock (g
->mainloop
);
526 pa_threaded_mainloop_unlock (g
->mainloop
);
529 pa_stream_unref (stream
);
532 *rerror
= pa_context_errno (g
->context
);
537 static int qpa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
543 struct audsettings obt_as
= *as
;
544 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
545 paaudio
*g
= pa
->g
= drv_opaque
;
546 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
547 AudiodevPaPerDirectionOptions
*ppdo
= popts
->out
;
549 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
550 ss
.channels
= as
->nchannels
;
553 ba
.tlength
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
558 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
560 pa
->stream
= qpa_simple_new (
564 ppdo
->has_name
? ppdo
->name
: NULL
,
566 NULL
, /* channel map */
567 &ba
, /* buffering attributes */
571 qpa_logerr (error
, "pa_simple_new for playback failed\n");
575 audio_pcm_init_info (&hw
->info
, &obt_as
);
576 hw
->samples
= pa
->samples
= audio_buffer_samples(
577 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
578 &obt_as
, ppdo
->buffer_length
);
579 pa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
582 dolog ("Could not allocate buffer (%d bytes)\n",
583 hw
->samples
<< hw
->info
.shift
);
587 if (audio_pt_init(&pa
->pt
, qpa_thread_out
, hw
, AUDIO_CAP
, __func__
)) {
594 g_free (pa
->pcm_buf
);
598 pa_stream_unref (pa
->stream
);
605 static int qpa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
610 struct audsettings obt_as
= *as
;
611 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
612 paaudio
*g
= pa
->g
= drv_opaque
;
613 AudiodevPaOptions
*popts
= &g
->dev
->u
.pa
;
614 AudiodevPaPerDirectionOptions
*ppdo
= popts
->in
;
616 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
617 ss
.channels
= as
->nchannels
;
620 ba
.fragsize
= pa_usec_to_bytes(ppdo
->latency
, &ss
);
625 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
627 pa
->stream
= qpa_simple_new (
631 ppdo
->has_name
? ppdo
->name
: NULL
,
633 NULL
, /* channel map */
634 &ba
, /* buffering attributes */
638 qpa_logerr (error
, "pa_simple_new for capture failed\n");
642 audio_pcm_init_info (&hw
->info
, &obt_as
);
643 hw
->samples
= pa
->samples
= audio_buffer_samples(
644 qapi_AudiodevPaPerDirectionOptions_base(ppdo
),
645 &obt_as
, ppdo
->buffer_length
);
646 pa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
649 dolog ("Could not allocate buffer (%d bytes)\n",
650 hw
->samples
<< hw
->info
.shift
);
654 if (audio_pt_init(&pa
->pt
, qpa_thread_in
, hw
, AUDIO_CAP
, __func__
)) {
661 g_free (pa
->pcm_buf
);
665 pa_stream_unref (pa
->stream
);
672 static void qpa_fini_out (HWVoiceOut
*hw
)
675 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
677 audio_pt_lock(&pa
->pt
, __func__
);
679 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
680 audio_pt_join(&pa
->pt
, &ret
, __func__
);
683 pa_stream_unref (pa
->stream
);
687 audio_pt_fini(&pa
->pt
, __func__
);
688 g_free (pa
->pcm_buf
);
692 static void qpa_fini_in (HWVoiceIn
*hw
)
695 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
697 audio_pt_lock(&pa
->pt
, __func__
);
699 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
700 audio_pt_join(&pa
->pt
, &ret
, __func__
);
703 pa_stream_unref (pa
->stream
);
707 audio_pt_fini(&pa
->pt
, __func__
);
708 g_free (pa
->pcm_buf
);
712 static int qpa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
714 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
719 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
720 pa_cvolume_init (&v
); /* function is present in 0.9.13+ */
730 sw
= va_arg (ap
, SWVoiceOut
*);
734 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
735 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
737 pa_threaded_mainloop_lock (g
->mainloop
);
739 op
= pa_context_set_sink_input_volume (g
->context
,
740 pa_stream_get_index (pa
->stream
),
743 qpa_logerr (pa_context_errno (g
->context
),
744 "set_sink_input_volume() failed\n");
746 pa_operation_unref (op
);
748 op
= pa_context_set_sink_input_mute (g
->context
,
749 pa_stream_get_index (pa
->stream
),
750 sw
->vol
.mute
, NULL
, NULL
);
752 qpa_logerr (pa_context_errno (g
->context
),
753 "set_sink_input_mute() failed\n");
755 pa_operation_unref (op
);
758 pa_threaded_mainloop_unlock (g
->mainloop
);
764 static int qpa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
766 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
771 #ifdef PA_CHECK_VERSION
772 pa_cvolume_init (&v
);
782 sw
= va_arg (ap
, SWVoiceIn
*);
786 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
787 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
789 pa_threaded_mainloop_lock (g
->mainloop
);
791 op
= pa_context_set_source_output_volume (g
->context
,
792 pa_stream_get_index (pa
->stream
),
795 qpa_logerr (pa_context_errno (g
->context
),
796 "set_source_output_volume() failed\n");
798 pa_operation_unref(op
);
801 op
= pa_context_set_source_output_mute (g
->context
,
802 pa_stream_get_index (pa
->stream
),
803 sw
->vol
.mute
, NULL
, NULL
);
805 qpa_logerr (pa_context_errno (g
->context
),
806 "set_source_output_mute() failed\n");
808 pa_operation_unref (op
);
811 pa_threaded_mainloop_unlock (g
->mainloop
);
817 static int qpa_validate_per_direction_opts(Audiodev
*dev
,
818 AudiodevPaPerDirectionOptions
*pdo
)
820 if (!pdo
->has_buffer_length
) {
821 pdo
->has_buffer_length
= true;
822 pdo
->buffer_length
= 46440;
824 if (!pdo
->has_latency
) {
825 pdo
->has_latency
= true;
826 pdo
->latency
= 15000;
831 static void *qpa_audio_init(Audiodev
*dev
)
834 AudiodevPaOptions
*popts
= &dev
->u
.pa
;
837 if (!popts
->has_server
) {
842 runtime
= getenv("XDG_RUNTIME_DIR");
846 snprintf(pidfile
, sizeof(pidfile
), "%s/pulse/pid", runtime
);
847 if (stat(pidfile
, &st
) != 0) {
852 assert(dev
->driver
== AUDIODEV_DRIVER_PA
);
854 g
= g_malloc(sizeof(paaudio
));
855 server
= popts
->has_server
? popts
->server
: NULL
;
857 if (!qpa_validate_per_direction_opts(dev
, popts
->in
)) {
860 if (!qpa_validate_per_direction_opts(dev
, popts
->out
)) {
868 g
->mainloop
= pa_threaded_mainloop_new ();
873 g
->context
= pa_context_new (pa_threaded_mainloop_get_api (g
->mainloop
),
879 pa_context_set_state_callback (g
->context
, context_state_cb
, g
);
881 if (pa_context_connect(g
->context
, server
, 0, NULL
) < 0) {
882 qpa_logerr (pa_context_errno (g
->context
),
883 "pa_context_connect() failed\n");
887 pa_threaded_mainloop_lock (g
->mainloop
);
889 if (pa_threaded_mainloop_start (g
->mainloop
) < 0) {
890 goto unlock_and_fail
;
894 pa_context_state_t state
;
896 state
= pa_context_get_state (g
->context
);
898 if (state
== PA_CONTEXT_READY
) {
902 if (!PA_CONTEXT_IS_GOOD (state
)) {
903 qpa_logerr (pa_context_errno (g
->context
),
904 "Wrong context state\n");
905 goto unlock_and_fail
;
908 /* Wait until the context is ready */
909 pa_threaded_mainloop_wait (g
->mainloop
);
912 pa_threaded_mainloop_unlock (g
->mainloop
);
917 pa_threaded_mainloop_unlock (g
->mainloop
);
919 AUD_log (AUDIO_CAP
, "Failed to initialize PA context");
924 static void qpa_audio_fini (void *opaque
)
929 pa_threaded_mainloop_stop (g
->mainloop
);
933 pa_context_disconnect (g
->context
);
934 pa_context_unref (g
->context
);
938 pa_threaded_mainloop_free (g
->mainloop
);
944 static struct audio_pcm_ops qpa_pcm_ops
= {
945 .init_out
= qpa_init_out
,
946 .fini_out
= qpa_fini_out
,
947 .run_out
= qpa_run_out
,
949 .ctl_out
= qpa_ctl_out
,
951 .init_in
= qpa_init_in
,
952 .fini_in
= qpa_fini_in
,
953 .run_in
= qpa_run_in
,
958 static struct audio_driver pa_audio_driver
= {
960 .descr
= "http://www.pulseaudio.org/",
961 .init
= qpa_audio_init
,
962 .fini
= qpa_audio_fini
,
963 .pcm_ops
= &qpa_pcm_ops
,
965 .max_voices_out
= INT_MAX
,
966 .max_voices_in
= INT_MAX
,
967 .voice_size_out
= sizeof (PAVoiceOut
),
968 .voice_size_in
= sizeof (PAVoiceIn
),
969 .ctl_caps
= VOICE_VOLUME_CAP
972 static void register_audio_pa(void)
974 audio_driver_register(&pa_audio_driver
);
976 type_init(register_audio_pa
);