2 #include "qemu-common.h"
5 #include <pulse/pulseaudio.h>
7 #define AUDIO_CAP "pulseaudio"
9 #include "audio_pt_int.h"
31 const void *read_data
;
32 size_t read_index
, read_length
;
40 pa_threaded_mainloop
*mainloop
;
44 static paaudio glob_paaudio
= {
48 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
53 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
56 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
59 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
61 if (!(expression)) { \
63 *(rerror) = pa_context_errno ((c)->context); \
69 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
71 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
72 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
73 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
74 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
76 *(rerror) = pa_context_errno ((c)->context); \
80 *(rerror) = PA_ERR_BADSTATE; \
87 static int qpa_simple_read (PAVoiceIn
*p
, void *data
, size_t length
, int *rerror
)
89 paaudio
*g
= &glob_paaudio
;
91 pa_threaded_mainloop_lock (g
->mainloop
);
93 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
98 while (!p
->read_data
) {
101 r
= pa_stream_peek (p
->stream
, &p
->read_data
, &p
->read_length
);
102 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
105 pa_threaded_mainloop_wait (g
->mainloop
);
106 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
112 l
= p
->read_length
< length
? p
->read_length
: length
;
113 memcpy (data
, (const uint8_t *) p
->read_data
+p
->read_index
, l
);
115 data
= (uint8_t *) data
+ l
;
121 if (!p
->read_length
) {
124 r
= pa_stream_drop (p
->stream
);
129 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
133 pa_threaded_mainloop_unlock (g
->mainloop
);
137 pa_threaded_mainloop_unlock (g
->mainloop
);
141 static int qpa_simple_write (PAVoiceOut
*p
, const void *data
, size_t length
, int *rerror
)
143 paaudio
*g
= &glob_paaudio
;
145 pa_threaded_mainloop_lock (g
->mainloop
);
147 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
153 while (!(l
= pa_stream_writable_size (p
->stream
))) {
154 pa_threaded_mainloop_wait (g
->mainloop
);
155 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
158 CHECK_SUCCESS_GOTO (g
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
164 r
= pa_stream_write (p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
165 CHECK_SUCCESS_GOTO (g
, rerror
, r
>= 0, unlock_and_fail
);
167 data
= (const uint8_t *) data
+ l
;
171 pa_threaded_mainloop_unlock (g
->mainloop
);
175 pa_threaded_mainloop_unlock (g
->mainloop
);
179 static void *qpa_thread_out (void *arg
)
181 PAVoiceOut
*pa
= arg
;
182 HWVoiceOut
*hw
= &pa
->hw
;
184 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
189 int decr
, to_mix
, rpos
;
200 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
205 decr
= to_mix
= audio_MIN (pa
->live
, glob_paaudio
.samples
>> 2);
208 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
214 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
215 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
217 hw
->clip (pa
->pcm_buf
, src
, chunk
);
219 if (qpa_simple_write (pa
, pa
->pcm_buf
,
220 chunk
<< hw
->info
.shift
, &error
) < 0) {
221 qpa_logerr (error
, "pa_simple_write failed\n");
225 rpos
= (rpos
+ chunk
) % hw
->samples
;
229 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
239 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
243 static int qpa_run_out (HWVoiceOut
*hw
, int live
)
246 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
248 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
252 decr
= audio_MIN (live
, pa
->decr
);
254 pa
->live
= live
- decr
;
257 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
260 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
265 static int qpa_write (SWVoiceOut
*sw
, void *buf
, int len
)
267 return audio_pcm_sw_write (sw
, buf
, len
);
271 static void *qpa_thread_in (void *arg
)
274 HWVoiceIn
*hw
= &pa
->hw
;
276 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
281 int incr
, to_grab
, wpos
;
292 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
297 incr
= to_grab
= audio_MIN (pa
->dead
, glob_paaudio
.samples
>> 2);
300 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
306 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
307 void *buf
= advance (pa
->pcm_buf
, wpos
);
309 if (qpa_simple_read (pa
, buf
,
310 chunk
<< hw
->info
.shift
, &error
) < 0) {
311 qpa_logerr (error
, "pa_simple_read failed\n");
315 hw
->conv (hw
->conv_buf
+ wpos
, buf
, chunk
);
316 wpos
= (wpos
+ chunk
) % hw
->samples
;
320 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
330 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
334 static int qpa_run_in (HWVoiceIn
*hw
)
336 int live
, incr
, dead
;
337 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
339 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
343 live
= audio_pcm_hw_get_live_in (hw
);
344 dead
= hw
->samples
- live
;
345 incr
= audio_MIN (dead
, pa
->incr
);
347 pa
->dead
= dead
- incr
;
350 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
353 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
358 static int qpa_read (SWVoiceIn
*sw
, void *buf
, int len
)
360 return audio_pcm_sw_read (sw
, buf
, len
);
363 static pa_sample_format_t
audfmt_to_pa (audfmt_e afmt
, int endianness
)
370 format
= PA_SAMPLE_U8
;
374 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
378 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
381 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
382 format
= PA_SAMPLE_U8
;
388 static audfmt_e
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
393 case PA_SAMPLE_S16BE
:
396 case PA_SAMPLE_S16LE
:
399 case PA_SAMPLE_S32BE
:
402 case PA_SAMPLE_S32LE
:
406 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
411 static void context_state_cb (pa_context
*c
, void *userdata
)
413 paaudio
*g
= &glob_paaudio
;
415 switch (pa_context_get_state(c
)) {
416 case PA_CONTEXT_READY
:
417 case PA_CONTEXT_TERMINATED
:
418 case PA_CONTEXT_FAILED
:
419 pa_threaded_mainloop_signal (g
->mainloop
, 0);
422 case PA_CONTEXT_UNCONNECTED
:
423 case PA_CONTEXT_CONNECTING
:
424 case PA_CONTEXT_AUTHORIZING
:
425 case PA_CONTEXT_SETTING_NAME
:
430 static void stream_state_cb (pa_stream
*s
, void * userdata
)
432 paaudio
*g
= &glob_paaudio
;
434 switch (pa_stream_get_state (s
)) {
436 case PA_STREAM_READY
:
437 case PA_STREAM_FAILED
:
438 case PA_STREAM_TERMINATED
:
439 pa_threaded_mainloop_signal (g
->mainloop
, 0);
442 case PA_STREAM_UNCONNECTED
:
443 case PA_STREAM_CREATING
:
448 static void stream_request_cb (pa_stream
*s
, size_t length
, void *userdata
)
450 paaudio
*g
= &glob_paaudio
;
452 pa_threaded_mainloop_signal (g
->mainloop
, 0);
455 static pa_stream
*qpa_simple_new (
458 pa_stream_direction_t dir
,
460 const char *stream_name
,
461 const pa_sample_spec
*ss
,
462 const pa_channel_map
*map
,
463 const pa_buffer_attr
*attr
,
466 paaudio
*g
= &glob_paaudio
;
470 pa_threaded_mainloop_lock (g
->mainloop
);
472 stream
= pa_stream_new (g
->context
, name
, ss
, map
);
477 pa_stream_set_state_callback (stream
, stream_state_cb
, g
);
478 pa_stream_set_read_callback (stream
, stream_request_cb
, g
);
479 pa_stream_set_write_callback (stream
, stream_request_cb
, g
);
481 if (dir
== PA_STREAM_PLAYBACK
) {
482 r
= pa_stream_connect_playback (stream
, dev
, attr
,
483 PA_STREAM_INTERPOLATE_TIMING
484 |PA_STREAM_ADJUST_LATENCY
485 |PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
);
487 r
= pa_stream_connect_record (stream
, dev
, attr
,
488 PA_STREAM_INTERPOLATE_TIMING
489 |PA_STREAM_ADJUST_LATENCY
490 |PA_STREAM_AUTO_TIMING_UPDATE
);
497 pa_threaded_mainloop_unlock (g
->mainloop
);
502 pa_threaded_mainloop_unlock (g
->mainloop
);
505 pa_stream_unref (stream
);
508 qpa_logerr (pa_context_errno (g
->context
),
509 "stream_new() failed\n");
514 static int qpa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
517 static pa_sample_spec ss
;
518 static pa_buffer_attr ba
;
519 struct audsettings obt_as
= *as
;
520 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
522 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
523 ss
.channels
= as
->nchannels
;
527 * qemu audio tick runs at 250 Hz (by default), so processing
528 * data chunks worth 4 ms of sound should be a good fit.
530 ba
.tlength
= pa_usec_to_bytes (4 * 1000, &ss
);
531 ba
.minreq
= pa_usec_to_bytes (2 * 1000, &ss
);
535 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
537 pa
->stream
= qpa_simple_new (
544 NULL
, /* channel map */
545 &ba
, /* buffering attributes */
549 qpa_logerr (error
, "pa_simple_new for playback failed\n");
553 audio_pcm_init_info (&hw
->info
, &obt_as
);
554 hw
->samples
= glob_paaudio
.samples
;
555 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
558 dolog ("Could not allocate buffer (%d bytes)\n",
559 hw
->samples
<< hw
->info
.shift
);
563 if (audio_pt_init (&pa
->pt
, qpa_thread_out
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
570 g_free (pa
->pcm_buf
);
574 pa_stream_unref (pa
->stream
);
581 static int qpa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
584 static pa_sample_spec ss
;
585 struct audsettings obt_as
= *as
;
586 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
588 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
589 ss
.channels
= as
->nchannels
;
592 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
594 pa
->stream
= qpa_simple_new (
601 NULL
, /* channel map */
602 NULL
, /* buffering attributes */
606 qpa_logerr (error
, "pa_simple_new for capture failed\n");
610 audio_pcm_init_info (&hw
->info
, &obt_as
);
611 hw
->samples
= glob_paaudio
.samples
;
612 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
615 dolog ("Could not allocate buffer (%d bytes)\n",
616 hw
->samples
<< hw
->info
.shift
);
620 if (audio_pt_init (&pa
->pt
, qpa_thread_in
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
627 g_free (pa
->pcm_buf
);
631 pa_stream_unref (pa
->stream
);
638 static void qpa_fini_out (HWVoiceOut
*hw
)
641 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
643 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
645 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
646 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
649 pa_stream_unref (pa
->stream
);
653 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
654 g_free (pa
->pcm_buf
);
658 static void qpa_fini_in (HWVoiceIn
*hw
)
661 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
663 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
665 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
666 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
669 pa_stream_unref (pa
->stream
);
673 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
674 g_free (pa
->pcm_buf
);
678 static int qpa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
680 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
683 paaudio
*g
= &glob_paaudio
;
685 pa_cvolume_init (&v
);
694 sw
= va_arg (ap
, SWVoiceOut
*);
698 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
699 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
701 pa_threaded_mainloop_lock (g
->mainloop
);
703 op
= pa_context_set_sink_input_volume (g
->context
,
704 pa_stream_get_index (pa
->stream
),
707 qpa_logerr (pa_context_errno (g
->context
),
708 "set_sink_input_volume() failed\n");
710 pa_operation_unref (op
);
712 op
= pa_context_set_sink_input_mute (g
->context
,
713 pa_stream_get_index (pa
->stream
),
714 sw
->vol
.mute
, NULL
, NULL
);
716 qpa_logerr (pa_context_errno (g
->context
),
717 "set_sink_input_mute() failed\n");
719 pa_operation_unref (op
);
722 pa_threaded_mainloop_unlock (g
->mainloop
);
728 static int qpa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
730 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
733 paaudio
*g
= &glob_paaudio
;
735 pa_cvolume_init (&v
);
744 sw
= va_arg (ap
, SWVoiceIn
*);
748 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
749 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
751 pa_threaded_mainloop_lock (g
->mainloop
);
753 /* FIXME: use the upcoming "set_source_output_{volume,mute}" */
754 op
= pa_context_set_source_volume_by_index (g
->context
,
755 pa_stream_get_device_index (pa
->stream
),
758 qpa_logerr (pa_context_errno (g
->context
),
759 "set_source_volume() failed\n");
761 pa_operation_unref(op
);
764 op
= pa_context_set_source_mute_by_index (g
->context
,
765 pa_stream_get_index (pa
->stream
),
766 sw
->vol
.mute
, NULL
, NULL
);
768 qpa_logerr (pa_context_errno (g
->context
),
769 "set_source_mute() failed\n");
771 pa_operation_unref (op
);
774 pa_threaded_mainloop_unlock (g
->mainloop
);
781 static void *qpa_audio_init (void)
783 paaudio
*g
= &glob_paaudio
;
785 g
->mainloop
= pa_threaded_mainloop_new ();
790 g
->context
= pa_context_new (pa_threaded_mainloop_get_api (g
->mainloop
), glob_paaudio
.server
);
795 pa_context_set_state_callback (g
->context
, context_state_cb
, g
);
797 if (pa_context_connect (g
->context
, glob_paaudio
.server
, 0, NULL
) < 0) {
798 qpa_logerr (pa_context_errno (g
->context
),
799 "pa_context_connect() failed\n");
803 pa_threaded_mainloop_lock (g
->mainloop
);
805 if (pa_threaded_mainloop_start (g
->mainloop
) < 0) {
806 goto unlock_and_fail
;
810 pa_context_state_t state
;
812 state
= pa_context_get_state (g
->context
);
814 if (state
== PA_CONTEXT_READY
) {
818 if (!PA_CONTEXT_IS_GOOD (state
)) {
819 qpa_logerr (pa_context_errno (g
->context
),
820 "Wrong context state\n");
821 goto unlock_and_fail
;
824 /* Wait until the context is ready */
825 pa_threaded_mainloop_wait (g
->mainloop
);
828 pa_threaded_mainloop_unlock (g
->mainloop
);
830 return &glob_paaudio
;
833 pa_threaded_mainloop_unlock (g
->mainloop
);
835 AUD_log (AUDIO_CAP
, "Failed to initialize PA context");
839 static void qpa_audio_fini (void *opaque
)
844 pa_threaded_mainloop_stop (g
->mainloop
);
848 pa_context_disconnect (g
->context
);
849 pa_context_unref (g
->context
);
854 pa_threaded_mainloop_free (g
->mainloop
);
860 struct audio_option qpa_options
[] = {
864 .valp
= &glob_paaudio
.samples
,
865 .descr
= "buffer size in samples"
870 .valp
= &glob_paaudio
.server
,
871 .descr
= "server address"
876 .valp
= &glob_paaudio
.sink
,
877 .descr
= "sink device name"
882 .valp
= &glob_paaudio
.source
,
883 .descr
= "source device name"
885 { /* End of list */ }
888 static struct audio_pcm_ops qpa_pcm_ops
= {
889 .init_out
= qpa_init_out
,
890 .fini_out
= qpa_fini_out
,
891 .run_out
= qpa_run_out
,
893 .ctl_out
= qpa_ctl_out
,
895 .init_in
= qpa_init_in
,
896 .fini_in
= qpa_fini_in
,
897 .run_in
= qpa_run_in
,
902 struct audio_driver pa_audio_driver
= {
904 .descr
= "http://www.pulseaudio.org/",
905 .options
= qpa_options
,
906 .init
= qpa_audio_init
,
907 .fini
= qpa_audio_fini
,
908 .pcm_ops
= &qpa_pcm_ops
,
910 .max_voices_out
= INT_MAX
,
911 .max_voices_in
= INT_MAX
,
912 .voice_size_out
= sizeof (PAVoiceOut
),
913 .voice_size_in
= sizeof (PAVoiceIn
),
914 .ctl_caps
= VOICE_VOLUME_CAP