2 #include "qemu-common.h"
5 #include <pulse/pulseaudio.h>
7 #define AUDIO_CAP "pulseaudio"
9 #include "audio_pt_int.h"
20 pa_threaded_mainloop
*mainloop
;
45 const void *read_data
;
46 size_t read_index
, read_length
;
50 static void qpa_audio_fini(void *opaque
);
52 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
57 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
60 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
63 #ifndef PA_CONTEXT_IS_GOOD
64 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
67 x
== PA_CONTEXT_CONNECTING
||
68 x
== PA_CONTEXT_AUTHORIZING
||
69 x
== PA_CONTEXT_SETTING_NAME
||
70 x
== PA_CONTEXT_READY
;
74 #ifndef PA_STREAM_IS_GOOD
75 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
78 x
== PA_STREAM_CREATING
||
83 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
85 if (!(expression)) { \
87 *(rerror) = pa_context_errno ((c)->context); \
93 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
95 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
96 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
97 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
98 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
100 *(rerror) = pa_context_errno ((c)->context); \
104 *(rerror) = PA_ERR_BADSTATE; \
111 static int qpa_simple_read (PAVoiceIn
*p
, void *data
, size_t length
, int *rerror
)
115 pa_threaded_mainloop_lock (g
->mainloop
);
117 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
122 while (!p
->read_data
) {
125 r
= pa_stream_peek (p
->stream
, &p
->read_data
, &p
->read_length
);
126 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
129 pa_threaded_mainloop_wait (g
->mainloop
);
130 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
136 l
= p
->read_length
< length
? p
->read_length
: length
;
137 memcpy (data
, (const uint8_t *) p
->read_data
+p
->read_index
, l
);
139 data
= (uint8_t *) data
+ l
;
145 if (!p
->read_length
) {
148 r
= pa_stream_drop (p
->stream
);
153 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
157 pa_threaded_mainloop_unlock (g
->mainloop
);
161 pa_threaded_mainloop_unlock (g
->mainloop
);
165 static int qpa_simple_write (PAVoiceOut
*p
, const void *data
, size_t length
, int *rerror
)
169 pa_threaded_mainloop_lock (g
->mainloop
);
171 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
177 while (!(l
= pa_stream_writable_size (p
->stream
))) {
178 pa_threaded_mainloop_wait (g
->mainloop
);
179 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
182 CHECK_SUCCESS_GOTO (g
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
188 r
= pa_stream_write (p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
189 CHECK_SUCCESS_GOTO (g
, rerror
, r
>= 0, unlock_and_fail
);
191 data
= (const uint8_t *) data
+ l
;
195 pa_threaded_mainloop_unlock (g
->mainloop
);
199 pa_threaded_mainloop_unlock (g
->mainloop
);
203 static void *qpa_thread_out (void *arg
)
205 PAVoiceOut
*pa
= arg
;
206 HWVoiceOut
*hw
= &pa
->hw
;
208 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
213 int decr
, to_mix
, rpos
;
224 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
229 decr
= to_mix
= audio_MIN (pa
->live
, pa
->g
->conf
.samples
>> 2);
232 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
238 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
239 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
241 hw
->clip (pa
->pcm_buf
, src
, chunk
);
243 if (qpa_simple_write (pa
, pa
->pcm_buf
,
244 chunk
<< hw
->info
.shift
, &error
) < 0) {
245 qpa_logerr (error
, "pa_simple_write failed\n");
249 rpos
= (rpos
+ chunk
) % hw
->samples
;
253 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
263 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
267 static int qpa_run_out (HWVoiceOut
*hw
, int live
)
270 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
272 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
276 decr
= audio_MIN (live
, pa
->decr
);
278 pa
->live
= live
- decr
;
281 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
284 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
289 static int qpa_write (SWVoiceOut
*sw
, void *buf
, int len
)
291 return audio_pcm_sw_write (sw
, buf
, len
);
295 static void *qpa_thread_in (void *arg
)
298 HWVoiceIn
*hw
= &pa
->hw
;
300 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
305 int incr
, to_grab
, wpos
;
316 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
321 incr
= to_grab
= audio_MIN (pa
->dead
, pa
->g
->conf
.samples
>> 2);
324 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
330 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
331 void *buf
= advance (pa
->pcm_buf
, wpos
);
333 if (qpa_simple_read (pa
, buf
,
334 chunk
<< hw
->info
.shift
, &error
) < 0) {
335 qpa_logerr (error
, "pa_simple_read failed\n");
339 hw
->conv (hw
->conv_buf
+ wpos
, buf
, chunk
);
340 wpos
= (wpos
+ chunk
) % hw
->samples
;
344 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
354 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
358 static int qpa_run_in (HWVoiceIn
*hw
)
360 int live
, incr
, dead
;
361 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
363 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
367 live
= audio_pcm_hw_get_live_in (hw
);
368 dead
= hw
->samples
- live
;
369 incr
= audio_MIN (dead
, pa
->incr
);
371 pa
->dead
= dead
- incr
;
374 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
377 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
382 static int qpa_read (SWVoiceIn
*sw
, void *buf
, int len
)
384 return audio_pcm_sw_read (sw
, buf
, len
);
387 static pa_sample_format_t
audfmt_to_pa (audfmt_e afmt
, int endianness
)
394 format
= PA_SAMPLE_U8
;
398 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
402 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
405 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
406 format
= PA_SAMPLE_U8
;
412 static audfmt_e
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
417 case PA_SAMPLE_S16BE
:
420 case PA_SAMPLE_S16LE
:
423 case PA_SAMPLE_S32BE
:
426 case PA_SAMPLE_S32LE
:
430 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
435 static void context_state_cb (pa_context
*c
, void *userdata
)
437 paaudio
*g
= userdata
;
439 switch (pa_context_get_state(c
)) {
440 case PA_CONTEXT_READY
:
441 case PA_CONTEXT_TERMINATED
:
442 case PA_CONTEXT_FAILED
:
443 pa_threaded_mainloop_signal (g
->mainloop
, 0);
446 case PA_CONTEXT_UNCONNECTED
:
447 case PA_CONTEXT_CONNECTING
:
448 case PA_CONTEXT_AUTHORIZING
:
449 case PA_CONTEXT_SETTING_NAME
:
454 static void stream_state_cb (pa_stream
*s
, void * userdata
)
456 paaudio
*g
= userdata
;
458 switch (pa_stream_get_state (s
)) {
460 case PA_STREAM_READY
:
461 case PA_STREAM_FAILED
:
462 case PA_STREAM_TERMINATED
:
463 pa_threaded_mainloop_signal (g
->mainloop
, 0);
466 case PA_STREAM_UNCONNECTED
:
467 case PA_STREAM_CREATING
:
472 static void stream_request_cb (pa_stream
*s
, size_t length
, void *userdata
)
474 paaudio
*g
= userdata
;
476 pa_threaded_mainloop_signal (g
->mainloop
, 0);
479 static pa_stream
*qpa_simple_new (
482 pa_stream_direction_t dir
,
484 const pa_sample_spec
*ss
,
485 const pa_channel_map
*map
,
486 const pa_buffer_attr
*attr
,
492 pa_threaded_mainloop_lock (g
->mainloop
);
494 stream
= pa_stream_new (g
->context
, name
, ss
, map
);
499 pa_stream_set_state_callback (stream
, stream_state_cb
, g
);
500 pa_stream_set_read_callback (stream
, stream_request_cb
, g
);
501 pa_stream_set_write_callback (stream
, stream_request_cb
, g
);
503 if (dir
== PA_STREAM_PLAYBACK
) {
504 r
= pa_stream_connect_playback (stream
, dev
, attr
,
505 PA_STREAM_INTERPOLATE_TIMING
506 #ifdef PA_STREAM_ADJUST_LATENCY
507 |PA_STREAM_ADJUST_LATENCY
509 |PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
);
511 r
= pa_stream_connect_record (stream
, dev
, attr
,
512 PA_STREAM_INTERPOLATE_TIMING
513 #ifdef PA_STREAM_ADJUST_LATENCY
514 |PA_STREAM_ADJUST_LATENCY
516 |PA_STREAM_AUTO_TIMING_UPDATE
);
523 pa_threaded_mainloop_unlock (g
->mainloop
);
528 pa_threaded_mainloop_unlock (g
->mainloop
);
531 pa_stream_unref (stream
);
534 *rerror
= pa_context_errno (g
->context
);
539 static int qpa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
545 struct audsettings obt_as
= *as
;
546 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
547 paaudio
*g
= pa
->g
= drv_opaque
;
549 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
550 ss
.channels
= as
->nchannels
;
554 * qemu audio tick runs at 100 Hz (by default), so processing
555 * data chunks worth 10 ms of sound should be a good fit.
557 ba
.tlength
= pa_usec_to_bytes (10 * 1000, &ss
);
558 ba
.minreq
= pa_usec_to_bytes (5 * 1000, &ss
);
562 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
564 pa
->stream
= qpa_simple_new (
570 NULL
, /* channel map */
571 &ba
, /* buffering attributes */
575 qpa_logerr (error
, "pa_simple_new for playback failed\n");
579 audio_pcm_init_info (&hw
->info
, &obt_as
);
580 hw
->samples
= g
->conf
.samples
;
581 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
584 dolog ("Could not allocate buffer (%d bytes)\n",
585 hw
->samples
<< hw
->info
.shift
);
589 if (audio_pt_init (&pa
->pt
, qpa_thread_out
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
596 g_free (pa
->pcm_buf
);
600 pa_stream_unref (pa
->stream
);
607 static int qpa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
611 struct audsettings obt_as
= *as
;
612 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
613 paaudio
*g
= pa
->g
= drv_opaque
;
615 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
616 ss
.channels
= as
->nchannels
;
619 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
621 pa
->stream
= qpa_simple_new (
627 NULL
, /* channel map */
628 NULL
, /* buffering attributes */
632 qpa_logerr (error
, "pa_simple_new for capture failed\n");
636 audio_pcm_init_info (&hw
->info
, &obt_as
);
637 hw
->samples
= g
->conf
.samples
;
638 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
641 dolog ("Could not allocate buffer (%d bytes)\n",
642 hw
->samples
<< hw
->info
.shift
);
646 if (audio_pt_init (&pa
->pt
, qpa_thread_in
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
653 g_free (pa
->pcm_buf
);
657 pa_stream_unref (pa
->stream
);
664 static void qpa_fini_out (HWVoiceOut
*hw
)
667 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
669 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
671 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
672 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
675 pa_stream_unref (pa
->stream
);
679 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
680 g_free (pa
->pcm_buf
);
684 static void qpa_fini_in (HWVoiceIn
*hw
)
687 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
689 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
691 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
692 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
695 pa_stream_unref (pa
->stream
);
699 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
700 g_free (pa
->pcm_buf
);
704 static int qpa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
706 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
711 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
712 pa_cvolume_init (&v
); /* function is present in 0.9.13+ */
722 sw
= va_arg (ap
, SWVoiceOut
*);
726 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
727 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
729 pa_threaded_mainloop_lock (g
->mainloop
);
731 op
= pa_context_set_sink_input_volume (g
->context
,
732 pa_stream_get_index (pa
->stream
),
735 qpa_logerr (pa_context_errno (g
->context
),
736 "set_sink_input_volume() failed\n");
738 pa_operation_unref (op
);
740 op
= pa_context_set_sink_input_mute (g
->context
,
741 pa_stream_get_index (pa
->stream
),
742 sw
->vol
.mute
, NULL
, NULL
);
744 qpa_logerr (pa_context_errno (g
->context
),
745 "set_sink_input_mute() failed\n");
747 pa_operation_unref (op
);
750 pa_threaded_mainloop_unlock (g
->mainloop
);
756 static int qpa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
758 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
763 #ifdef PA_CHECK_VERSION
764 pa_cvolume_init (&v
);
774 sw
= va_arg (ap
, SWVoiceIn
*);
778 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
779 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
781 pa_threaded_mainloop_lock (g
->mainloop
);
783 /* FIXME: use the upcoming "set_source_output_{volume,mute}" */
784 op
= pa_context_set_source_volume_by_index (g
->context
,
785 pa_stream_get_device_index (pa
->stream
),
788 qpa_logerr (pa_context_errno (g
->context
),
789 "set_source_volume() failed\n");
791 pa_operation_unref(op
);
794 op
= pa_context_set_source_mute_by_index (g
->context
,
795 pa_stream_get_index (pa
->stream
),
796 sw
->vol
.mute
, NULL
, NULL
);
798 qpa_logerr (pa_context_errno (g
->context
),
799 "set_source_mute() failed\n");
801 pa_operation_unref (op
);
804 pa_threaded_mainloop_unlock (g
->mainloop
);
811 static PAConf glob_conf
= {
815 static void *qpa_audio_init (void)
817 paaudio
*g
= g_malloc(sizeof(paaudio
));
822 g
->mainloop
= pa_threaded_mainloop_new ();
827 g
->context
= pa_context_new (pa_threaded_mainloop_get_api (g
->mainloop
),
833 pa_context_set_state_callback (g
->context
, context_state_cb
, g
);
835 if (pa_context_connect (g
->context
, g
->conf
.server
, 0, NULL
) < 0) {
836 qpa_logerr (pa_context_errno (g
->context
),
837 "pa_context_connect() failed\n");
841 pa_threaded_mainloop_lock (g
->mainloop
);
843 if (pa_threaded_mainloop_start (g
->mainloop
) < 0) {
844 goto unlock_and_fail
;
848 pa_context_state_t state
;
850 state
= pa_context_get_state (g
->context
);
852 if (state
== PA_CONTEXT_READY
) {
856 if (!PA_CONTEXT_IS_GOOD (state
)) {
857 qpa_logerr (pa_context_errno (g
->context
),
858 "Wrong context state\n");
859 goto unlock_and_fail
;
862 /* Wait until the context is ready */
863 pa_threaded_mainloop_wait (g
->mainloop
);
866 pa_threaded_mainloop_unlock (g
->mainloop
);
871 pa_threaded_mainloop_unlock (g
->mainloop
);
873 AUD_log (AUDIO_CAP
, "Failed to initialize PA context");
878 static void qpa_audio_fini (void *opaque
)
883 pa_threaded_mainloop_stop (g
->mainloop
);
887 pa_context_disconnect (g
->context
);
888 pa_context_unref (g
->context
);
892 pa_threaded_mainloop_free (g
->mainloop
);
898 struct audio_option qpa_options
[] = {
902 .valp
= &glob_conf
.samples
,
903 .descr
= "buffer size in samples"
908 .valp
= &glob_conf
.server
,
909 .descr
= "server address"
914 .valp
= &glob_conf
.sink
,
915 .descr
= "sink device name"
920 .valp
= &glob_conf
.source
,
921 .descr
= "source device name"
923 { /* End of list */ }
926 static struct audio_pcm_ops qpa_pcm_ops
= {
927 .init_out
= qpa_init_out
,
928 .fini_out
= qpa_fini_out
,
929 .run_out
= qpa_run_out
,
931 .ctl_out
= qpa_ctl_out
,
933 .init_in
= qpa_init_in
,
934 .fini_in
= qpa_fini_in
,
935 .run_in
= qpa_run_in
,
940 struct audio_driver pa_audio_driver
= {
942 .descr
= "http://www.pulseaudio.org/",
943 .options
= qpa_options
,
944 .init
= qpa_audio_init
,
945 .fini
= qpa_audio_fini
,
946 .pcm_ops
= &qpa_pcm_ops
,
948 .max_voices_out
= INT_MAX
,
949 .max_voices_in
= INT_MAX
,
950 .voice_size_out
= sizeof (PAVoiceOut
),
951 .voice_size_in
= sizeof (PAVoiceIn
),
952 .ctl_caps
= VOICE_VOLUME_CAP