2 #include "qemu/osdep.h"
3 #include "qemu-common.h"
6 #include <pulse/pulseaudio.h>
8 #define AUDIO_CAP "pulseaudio"
10 #include "audio_pt_int.h"
21 pa_threaded_mainloop
*mainloop
;
46 const void *read_data
;
47 size_t read_index
, read_length
;
51 static void qpa_audio_fini(void *opaque
);
53 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
58 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
61 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
64 #ifndef PA_CONTEXT_IS_GOOD
65 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
68 x
== PA_CONTEXT_CONNECTING
||
69 x
== PA_CONTEXT_AUTHORIZING
||
70 x
== PA_CONTEXT_SETTING_NAME
||
71 x
== PA_CONTEXT_READY
;
75 #ifndef PA_STREAM_IS_GOOD
76 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
79 x
== PA_STREAM_CREATING
||
84 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
86 if (!(expression)) { \
88 *(rerror) = pa_context_errno ((c)->context); \
94 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
96 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
97 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
98 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
99 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
101 *(rerror) = pa_context_errno ((c)->context); \
105 *(rerror) = PA_ERR_BADSTATE; \
112 static int qpa_simple_read (PAVoiceIn
*p
, void *data
, size_t length
, int *rerror
)
116 pa_threaded_mainloop_lock (g
->mainloop
);
118 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
123 while (!p
->read_data
) {
126 r
= pa_stream_peek (p
->stream
, &p
->read_data
, &p
->read_length
);
127 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
130 pa_threaded_mainloop_wait (g
->mainloop
);
131 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
137 l
= p
->read_length
< length
? p
->read_length
: length
;
138 memcpy (data
, (const uint8_t *) p
->read_data
+p
->read_index
, l
);
140 data
= (uint8_t *) data
+ l
;
146 if (!p
->read_length
) {
149 r
= pa_stream_drop (p
->stream
);
154 CHECK_SUCCESS_GOTO (g
, rerror
, r
== 0, unlock_and_fail
);
158 pa_threaded_mainloop_unlock (g
->mainloop
);
162 pa_threaded_mainloop_unlock (g
->mainloop
);
166 static int qpa_simple_write (PAVoiceOut
*p
, const void *data
, size_t length
, int *rerror
)
170 pa_threaded_mainloop_lock (g
->mainloop
);
172 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
178 while (!(l
= pa_stream_writable_size (p
->stream
))) {
179 pa_threaded_mainloop_wait (g
->mainloop
);
180 CHECK_DEAD_GOTO (g
, p
->stream
, rerror
, unlock_and_fail
);
183 CHECK_SUCCESS_GOTO (g
, rerror
, l
!= (size_t) -1, unlock_and_fail
);
189 r
= pa_stream_write (p
->stream
, data
, l
, NULL
, 0LL, PA_SEEK_RELATIVE
);
190 CHECK_SUCCESS_GOTO (g
, rerror
, r
>= 0, unlock_and_fail
);
192 data
= (const uint8_t *) data
+ l
;
196 pa_threaded_mainloop_unlock (g
->mainloop
);
200 pa_threaded_mainloop_unlock (g
->mainloop
);
204 static void *qpa_thread_out (void *arg
)
206 PAVoiceOut
*pa
= arg
;
207 HWVoiceOut
*hw
= &pa
->hw
;
209 if (audio_pt_lock(&pa
->pt
, __func__
)) {
214 int decr
, to_mix
, rpos
;
225 if (audio_pt_wait(&pa
->pt
, __func__
)) {
230 decr
= to_mix
= audio_MIN(pa
->live
, pa
->g
->conf
.samples
>> 5);
233 if (audio_pt_unlock(&pa
->pt
, __func__
)) {
239 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
240 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
242 hw
->clip (pa
->pcm_buf
, src
, chunk
);
244 if (qpa_simple_write (pa
, pa
->pcm_buf
,
245 chunk
<< hw
->info
.shift
, &error
) < 0) {
246 qpa_logerr (error
, "pa_simple_write failed\n");
250 rpos
= (rpos
+ chunk
) % hw
->samples
;
254 if (audio_pt_lock(&pa
->pt
, __func__
)) {
264 audio_pt_unlock(&pa
->pt
, __func__
);
268 static int qpa_run_out (HWVoiceOut
*hw
, int live
)
271 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
273 if (audio_pt_lock(&pa
->pt
, __func__
)) {
277 decr
= audio_MIN (live
, pa
->decr
);
279 pa
->live
= live
- decr
;
282 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
285 audio_pt_unlock(&pa
->pt
, __func__
);
290 static int qpa_write (SWVoiceOut
*sw
, void *buf
, int len
)
292 return audio_pcm_sw_write (sw
, buf
, len
);
296 static void *qpa_thread_in (void *arg
)
299 HWVoiceIn
*hw
= &pa
->hw
;
301 if (audio_pt_lock(&pa
->pt
, __func__
)) {
306 int incr
, to_grab
, wpos
;
317 if (audio_pt_wait(&pa
->pt
, __func__
)) {
322 incr
= to_grab
= audio_MIN(pa
->dead
, pa
->g
->conf
.samples
>> 5);
325 if (audio_pt_unlock(&pa
->pt
, __func__
)) {
331 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
332 void *buf
= advance (pa
->pcm_buf
, wpos
);
334 if (qpa_simple_read (pa
, buf
,
335 chunk
<< hw
->info
.shift
, &error
) < 0) {
336 qpa_logerr (error
, "pa_simple_read failed\n");
340 hw
->conv (hw
->conv_buf
+ wpos
, buf
, chunk
);
341 wpos
= (wpos
+ chunk
) % hw
->samples
;
345 if (audio_pt_lock(&pa
->pt
, __func__
)) {
355 audio_pt_unlock(&pa
->pt
, __func__
);
359 static int qpa_run_in (HWVoiceIn
*hw
)
361 int live
, incr
, dead
;
362 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
364 if (audio_pt_lock(&pa
->pt
, __func__
)) {
368 live
= audio_pcm_hw_get_live_in (hw
);
369 dead
= hw
->samples
- live
;
370 incr
= audio_MIN (dead
, pa
->incr
);
372 pa
->dead
= dead
- incr
;
375 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
378 audio_pt_unlock(&pa
->pt
, __func__
);
383 static int qpa_read (SWVoiceIn
*sw
, void *buf
, int len
)
385 return audio_pcm_sw_read (sw
, buf
, len
);
388 static pa_sample_format_t
audfmt_to_pa (audfmt_e afmt
, int endianness
)
395 format
= PA_SAMPLE_U8
;
399 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
403 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
406 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
407 format
= PA_SAMPLE_U8
;
413 static audfmt_e
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
418 case PA_SAMPLE_S16BE
:
421 case PA_SAMPLE_S16LE
:
424 case PA_SAMPLE_S32BE
:
427 case PA_SAMPLE_S32LE
:
431 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
436 static void context_state_cb (pa_context
*c
, void *userdata
)
438 paaudio
*g
= userdata
;
440 switch (pa_context_get_state(c
)) {
441 case PA_CONTEXT_READY
:
442 case PA_CONTEXT_TERMINATED
:
443 case PA_CONTEXT_FAILED
:
444 pa_threaded_mainloop_signal (g
->mainloop
, 0);
447 case PA_CONTEXT_UNCONNECTED
:
448 case PA_CONTEXT_CONNECTING
:
449 case PA_CONTEXT_AUTHORIZING
:
450 case PA_CONTEXT_SETTING_NAME
:
455 static void stream_state_cb (pa_stream
*s
, void * userdata
)
457 paaudio
*g
= userdata
;
459 switch (pa_stream_get_state (s
)) {
461 case PA_STREAM_READY
:
462 case PA_STREAM_FAILED
:
463 case PA_STREAM_TERMINATED
:
464 pa_threaded_mainloop_signal (g
->mainloop
, 0);
467 case PA_STREAM_UNCONNECTED
:
468 case PA_STREAM_CREATING
:
473 static void stream_request_cb (pa_stream
*s
, size_t length
, void *userdata
)
475 paaudio
*g
= userdata
;
477 pa_threaded_mainloop_signal (g
->mainloop
, 0);
480 static pa_stream
*qpa_simple_new (
483 pa_stream_direction_t dir
,
485 const pa_sample_spec
*ss
,
486 const pa_channel_map
*map
,
487 const pa_buffer_attr
*attr
,
493 pa_threaded_mainloop_lock (g
->mainloop
);
495 stream
= pa_stream_new (g
->context
, name
, ss
, map
);
500 pa_stream_set_state_callback (stream
, stream_state_cb
, g
);
501 pa_stream_set_read_callback (stream
, stream_request_cb
, g
);
502 pa_stream_set_write_callback (stream
, stream_request_cb
, g
);
504 if (dir
== PA_STREAM_PLAYBACK
) {
505 r
= pa_stream_connect_playback (stream
, dev
, attr
,
506 PA_STREAM_INTERPOLATE_TIMING
507 #ifdef PA_STREAM_ADJUST_LATENCY
508 |PA_STREAM_ADJUST_LATENCY
510 |PA_STREAM_AUTO_TIMING_UPDATE
, NULL
, NULL
);
512 r
= pa_stream_connect_record (stream
, dev
, attr
,
513 PA_STREAM_INTERPOLATE_TIMING
514 #ifdef PA_STREAM_ADJUST_LATENCY
515 |PA_STREAM_ADJUST_LATENCY
517 |PA_STREAM_AUTO_TIMING_UPDATE
);
524 pa_threaded_mainloop_unlock (g
->mainloop
);
529 pa_threaded_mainloop_unlock (g
->mainloop
);
532 pa_stream_unref (stream
);
535 *rerror
= pa_context_errno (g
->context
);
540 static int qpa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
546 struct audsettings obt_as
= *as
;
547 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
548 paaudio
*g
= pa
->g
= drv_opaque
;
550 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
551 ss
.channels
= as
->nchannels
;
555 * qemu audio tick runs at 100 Hz (by default), so processing
556 * data chunks worth 10 ms of sound should be a good fit.
558 ba
.tlength
= pa_usec_to_bytes (10 * 1000, &ss
);
559 ba
.minreq
= pa_usec_to_bytes (5 * 1000, &ss
);
563 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
565 pa
->stream
= qpa_simple_new (
571 NULL
, /* channel map */
572 &ba
, /* buffering attributes */
576 qpa_logerr (error
, "pa_simple_new for playback failed\n");
580 audio_pcm_init_info (&hw
->info
, &obt_as
);
581 hw
->samples
= g
->conf
.samples
;
582 pa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
585 dolog ("Could not allocate buffer (%d bytes)\n",
586 hw
->samples
<< hw
->info
.shift
);
590 if (audio_pt_init(&pa
->pt
, qpa_thread_out
, hw
, AUDIO_CAP
, __func__
)) {
597 g_free (pa
->pcm_buf
);
601 pa_stream_unref (pa
->stream
);
608 static int qpa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
612 struct audsettings obt_as
= *as
;
613 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
614 paaudio
*g
= pa
->g
= drv_opaque
;
616 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
617 ss
.channels
= as
->nchannels
;
620 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
622 pa
->stream
= qpa_simple_new (
628 NULL
, /* channel map */
629 NULL
, /* buffering attributes */
633 qpa_logerr (error
, "pa_simple_new for capture failed\n");
637 audio_pcm_init_info (&hw
->info
, &obt_as
);
638 hw
->samples
= g
->conf
.samples
;
639 pa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
642 dolog ("Could not allocate buffer (%d bytes)\n",
643 hw
->samples
<< hw
->info
.shift
);
647 if (audio_pt_init(&pa
->pt
, qpa_thread_in
, hw
, AUDIO_CAP
, __func__
)) {
654 g_free (pa
->pcm_buf
);
658 pa_stream_unref (pa
->stream
);
665 static void qpa_fini_out (HWVoiceOut
*hw
)
668 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
670 audio_pt_lock(&pa
->pt
, __func__
);
672 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
673 audio_pt_join(&pa
->pt
, &ret
, __func__
);
676 pa_stream_unref (pa
->stream
);
680 audio_pt_fini(&pa
->pt
, __func__
);
681 g_free (pa
->pcm_buf
);
685 static void qpa_fini_in (HWVoiceIn
*hw
)
688 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
690 audio_pt_lock(&pa
->pt
, __func__
);
692 audio_pt_unlock_and_signal(&pa
->pt
, __func__
);
693 audio_pt_join(&pa
->pt
, &ret
, __func__
);
696 pa_stream_unref (pa
->stream
);
700 audio_pt_fini(&pa
->pt
, __func__
);
701 g_free (pa
->pcm_buf
);
705 static int qpa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
707 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
712 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
713 pa_cvolume_init (&v
); /* function is present in 0.9.13+ */
723 sw
= va_arg (ap
, SWVoiceOut
*);
727 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
728 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
730 pa_threaded_mainloop_lock (g
->mainloop
);
732 op
= pa_context_set_sink_input_volume (g
->context
,
733 pa_stream_get_index (pa
->stream
),
736 qpa_logerr (pa_context_errno (g
->context
),
737 "set_sink_input_volume() failed\n");
739 pa_operation_unref (op
);
741 op
= pa_context_set_sink_input_mute (g
->context
,
742 pa_stream_get_index (pa
->stream
),
743 sw
->vol
.mute
, NULL
, NULL
);
745 qpa_logerr (pa_context_errno (g
->context
),
746 "set_sink_input_mute() failed\n");
748 pa_operation_unref (op
);
751 pa_threaded_mainloop_unlock (g
->mainloop
);
757 static int qpa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
759 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
764 #ifdef PA_CHECK_VERSION
765 pa_cvolume_init (&v
);
775 sw
= va_arg (ap
, SWVoiceIn
*);
779 v
.values
[0] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.l
) / UINT32_MAX
;
780 v
.values
[1] = ((PA_VOLUME_NORM
- PA_VOLUME_MUTED
) * sw
->vol
.r
) / UINT32_MAX
;
782 pa_threaded_mainloop_lock (g
->mainloop
);
784 op
= pa_context_set_source_output_volume (g
->context
,
785 pa_stream_get_index (pa
->stream
),
788 qpa_logerr (pa_context_errno (g
->context
),
789 "set_source_output_volume() failed\n");
791 pa_operation_unref(op
);
794 op
= pa_context_set_source_output_mute (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_output_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 static 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
955 static void register_audio_pa(void)
957 audio_driver_register(&pa_audio_driver
);
959 type_init(register_audio_pa
);