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 #ifndef PA_CONTEXT_IS_GOOD
60 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x)
63 x == PA_CONTEXT_CONNECTING ||
64 x == PA_CONTEXT_AUTHORIZING ||
65 x == PA_CONTEXT_SETTING_NAME ||
66 x == PA_CONTEXT_READY;
70 #ifndef PA_STREAM_IS_GOOD
71 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
74 x == PA_STREAM_CREATING ||
79 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
81 if (!(expression)) { \
83 *(rerror) = pa_context_errno ((c)->context); \
89 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
91 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
92 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
93 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
94 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
96 *(rerror) = pa_context_errno ((c)->context); \
100 *(rerror) = PA_ERR_BADSTATE; \
107 static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
109 paaudio *g = &glob_paaudio;
111 pa_threaded_mainloop_lock (g->mainloop);
113 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
118 while (!p->read_data) {
121 r = pa_stream_peek (p->stream, &p->read_data, &p->read_length);
122 CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
125 pa_threaded_mainloop_wait (g->mainloop);
126 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
132 l = p->read_length < length ? p->read_length : length;
133 memcpy (data, (const uint8_t *) p->read_data+p->read_index, l);
135 data = (uint8_t *) data + l;
141 if (!p->read_length) {
144 r = pa_stream_drop (p->stream);
149 CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
153 pa_threaded_mainloop_unlock (g->mainloop);
157 pa_threaded_mainloop_unlock (g->mainloop);
161 static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
163 paaudio *g = &glob_paaudio;
165 pa_threaded_mainloop_lock (g->mainloop);
167 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
173 while (!(l = pa_stream_writable_size (p->stream))) {
174 pa_threaded_mainloop_wait (g->mainloop);
175 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
178 CHECK_SUCCESS_GOTO (g, rerror, l != (size_t) -1, unlock_and_fail);
184 r = pa_stream_write (p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE);
185 CHECK_SUCCESS_GOTO (g, rerror, r >= 0, unlock_and_fail);
187 data = (const uint8_t *) data + l;
191 pa_threaded_mainloop_unlock (g->mainloop);
195 pa_threaded_mainloop_unlock (g->mainloop);
199 static void *qpa_thread_out (void *arg)
201 PAVoiceOut *pa = arg;
202 HWVoiceOut *hw = &pa->hw;
204 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
209 int decr, to_mix, rpos;
220 if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
225 decr = to_mix = audio_MIN (pa->live, glob_paaudio.samples >> 2);
228 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
234 int chunk = audio_MIN (to_mix, hw->samples - rpos);
235 struct st_sample *src = hw->mix_buf + rpos;
237 hw->clip (pa->pcm_buf, src, chunk);
239 if (qpa_simple_write (pa, pa->pcm_buf,
240 chunk << hw->info.shift, &error) < 0) {
241 qpa_logerr (error, "pa_simple_write failed\n");
245 rpos = (rpos + chunk) % hw->samples;
249 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
259 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
263 static int qpa_run_out (HWVoiceOut *hw, int live)
266 PAVoiceOut *pa = (PAVoiceOut *) hw;
268 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
272 decr = audio_MIN (live, pa->decr);
274 pa->live = live - decr;
277 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
280 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
285 static int qpa_write (SWVoiceOut *sw, void *buf, int len)
287 return audio_pcm_sw_write (sw, buf, len);
291 static void *qpa_thread_in (void *arg)
294 HWVoiceIn *hw = &pa->hw;
296 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
301 int incr, to_grab, wpos;
312 if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
317 incr = to_grab = audio_MIN (pa->dead, glob_paaudio.samples >> 2);
320 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
326 int chunk = audio_MIN (to_grab, hw->samples - wpos);
327 void *buf = advance (pa->pcm_buf, wpos);
329 if (qpa_simple_read (pa, buf,
330 chunk << hw->info.shift, &error) < 0) {
331 qpa_logerr (error, "pa_simple_read failed\n");
335 hw->conv (hw->conv_buf + wpos, buf, chunk);
336 wpos = (wpos + chunk) % hw->samples;
340 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
350 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
354 static int qpa_run_in (HWVoiceIn *hw)
356 int live, incr, dead;
357 PAVoiceIn *pa = (PAVoiceIn *) hw;
359 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
363 live = audio_pcm_hw_get_live_in (hw);
364 dead = hw->samples - live;
365 incr = audio_MIN (dead, pa->incr);
367 pa->dead = dead - incr;
370 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
373 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
378 static int qpa_read (SWVoiceIn *sw, void *buf, int len)
380 return audio_pcm_sw_read (sw, buf, len);
383 static pa_sample_format_t audfmt_to_pa (audfmt_e afmt, int endianness)
390 format = PA_SAMPLE_U8;
394 format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE;
398 format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE;
401 dolog ("Internal logic error: Bad audio format %d\n", afmt);
402 format = PA_SAMPLE_U8;
408 static audfmt_e pa_to_audfmt (pa_sample_format_t fmt, int *endianness)
413 case PA_SAMPLE_S16BE:
416 case PA_SAMPLE_S16LE:
419 case PA_SAMPLE_S32BE:
422 case PA_SAMPLE_S32LE:
426 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt);
431 static void context_state_cb (pa_context *c, void *userdata)
433 paaudio *g = &glob_paaudio;
435 switch (pa_context_get_state(c)) {
436 case PA_CONTEXT_READY:
437 case PA_CONTEXT_TERMINATED:
438 case PA_CONTEXT_FAILED:
439 pa_threaded_mainloop_signal (g->mainloop, 0);
442 case PA_CONTEXT_UNCONNECTED:
443 case PA_CONTEXT_CONNECTING:
444 case PA_CONTEXT_AUTHORIZING:
445 case PA_CONTEXT_SETTING_NAME:
450 static void stream_state_cb (pa_stream *s, void * userdata)
452 paaudio *g = &glob_paaudio;
454 switch (pa_stream_get_state (s)) {
456 case PA_STREAM_READY:
457 case PA_STREAM_FAILED:
458 case PA_STREAM_TERMINATED:
459 pa_threaded_mainloop_signal (g->mainloop, 0);
462 case PA_STREAM_UNCONNECTED:
463 case PA_STREAM_CREATING:
468 static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
470 paaudio *g = &glob_paaudio;
472 pa_threaded_mainloop_signal (g->mainloop, 0);
475 static pa_stream *qpa_simple_new (
478 pa_stream_direction_t dir,
480 const char *stream_name,
481 const pa_sample_spec *ss,
482 const pa_channel_map *map,
483 const pa_buffer_attr *attr,
486 paaudio *g = &glob_paaudio;
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)
540 static pa_sample_spec ss;
541 static pa_buffer_attr ba;
542 struct audsettings obt_as = *as;
543 PAVoiceOut *pa = (PAVoiceOut *) hw;
545 ss.format = audfmt_to_pa (as->fmt, as->endianness);
546 ss.channels = as->nchannels;
550 * qemu audio tick runs at 100 Hz (by default), so processing
551 * data chunks worth 10 ms of sound should be a good fit.
553 ba.tlength = pa_usec_to_bytes (10 * 1000, &ss);
554 ba.minreq = pa_usec_to_bytes (5 * 1000, &ss);
558 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
560 pa->stream = qpa_simple_new (
567 NULL, /* channel map */
568 &ba, /* buffering attributes */
572 qpa_logerr (error, "pa_simple_new for playback failed\n");
576 audio_pcm_init_info (&hw->info, &obt_as);
577 hw->samples = glob_paaudio.samples;
578 pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
581 dolog ("Could not allocate buffer (%d bytes)\n",
582 hw->samples << hw->info.shift);
586 if (audio_pt_init (&pa->pt, qpa_thread_out, hw, AUDIO_CAP, AUDIO_FUNC)) {
593 g_free (pa->pcm_buf);
597 pa_stream_unref (pa->stream);
604 static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
607 static pa_sample_spec ss;
608 struct audsettings obt_as = *as;
609 PAVoiceIn *pa = (PAVoiceIn *) hw;
611 ss.format = audfmt_to_pa (as->fmt, as->endianness);
612 ss.channels = as->nchannels;
615 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
617 pa->stream = qpa_simple_new (
624 NULL, /* channel map */
625 NULL, /* buffering attributes */
629 qpa_logerr (error, "pa_simple_new for capture failed\n");
633 audio_pcm_init_info (&hw->info, &obt_as);
634 hw->samples = glob_paaudio.samples;
635 pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
638 dolog ("Could not allocate buffer (%d bytes)\n",
639 hw->samples << hw->info.shift);
643 if (audio_pt_init (&pa->pt, qpa_thread_in, hw, AUDIO_CAP, AUDIO_FUNC)) {
650 g_free (pa->pcm_buf);
654 pa_stream_unref (pa->stream);
661 static void qpa_fini_out (HWVoiceOut *hw)
664 PAVoiceOut *pa = (PAVoiceOut *) hw;
666 audio_pt_lock (&pa->pt, AUDIO_FUNC);
668 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
669 audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
672 pa_stream_unref (pa->stream);
676 audio_pt_fini (&pa->pt, AUDIO_FUNC);
677 g_free (pa->pcm_buf);
681 static void qpa_fini_in (HWVoiceIn *hw)
684 PAVoiceIn *pa = (PAVoiceIn *) hw;
686 audio_pt_lock (&pa->pt, AUDIO_FUNC);
688 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
689 audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
692 pa_stream_unref (pa->stream);
696 audio_pt_fini (&pa->pt, AUDIO_FUNC);
697 g_free (pa->pcm_buf);
701 static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
703 PAVoiceOut *pa = (PAVoiceOut *) hw;
706 paaudio *g = &glob_paaudio;
708 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
709 pa_cvolume_init (&v); /* function is present in 0.9.13+ */
719 sw = va_arg (ap, SWVoiceOut *);
723 v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
724 v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
726 pa_threaded_mainloop_lock (g->mainloop);
728 op = pa_context_set_sink_input_volume (g->context,
729 pa_stream_get_index (pa->stream),
732 qpa_logerr (pa_context_errno (g->context),
733 "set_sink_input_volume() failed\n");
735 pa_operation_unref (op);
737 op = pa_context_set_sink_input_mute (g->context,
738 pa_stream_get_index (pa->stream),
739 sw->vol.mute, NULL, NULL);
741 qpa_logerr (pa_context_errno (g->context),
742 "set_sink_input_mute() failed\n");
744 pa_operation_unref (op);
747 pa_threaded_mainloop_unlock (g->mainloop);
753 static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
755 PAVoiceIn *pa = (PAVoiceIn *) hw;
758 paaudio *g = &glob_paaudio;
760 #ifdef PA_CHECK_VERSION
761 pa_cvolume_init (&v);
771 sw = va_arg (ap, SWVoiceIn *);
775 v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
776 v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
778 pa_threaded_mainloop_lock (g->mainloop);
780 /* FIXME: use the upcoming "set_source_output_{volume,mute}" */
781 op = pa_context_set_source_volume_by_index (g->context,
782 pa_stream_get_device_index (pa->stream),
785 qpa_logerr (pa_context_errno (g->context),
786 "set_source_volume() failed\n");
788 pa_operation_unref(op);
791 op = pa_context_set_source_mute_by_index (g->context,
792 pa_stream_get_index (pa->stream),
793 sw->vol.mute, NULL, NULL);
795 qpa_logerr (pa_context_errno (g->context),
796 "set_source_mute() failed\n");
798 pa_operation_unref (op);
801 pa_threaded_mainloop_unlock (g->mainloop);
808 static void *qpa_audio_init (void)
810 paaudio *g = &glob_paaudio;
812 g->mainloop = pa_threaded_mainloop_new ();
817 g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), glob_paaudio.server);
822 pa_context_set_state_callback (g->context, context_state_cb, g);
824 if (pa_context_connect (g->context, glob_paaudio.server, 0, NULL) < 0) {
825 qpa_logerr (pa_context_errno (g->context),
826 "pa_context_connect() failed\n");
830 pa_threaded_mainloop_lock (g->mainloop);
832 if (pa_threaded_mainloop_start (g->mainloop) < 0) {
833 goto unlock_and_fail;
837 pa_context_state_t state;
839 state = pa_context_get_state (g->context);
841 if (state == PA_CONTEXT_READY) {
845 if (!PA_CONTEXT_IS_GOOD (state)) {
846 qpa_logerr (pa_context_errno (g->context),
847 "Wrong context state\n");
848 goto unlock_and_fail;
851 /* Wait until the context is ready */
852 pa_threaded_mainloop_wait (g->mainloop);
855 pa_threaded_mainloop_unlock (g->mainloop);
857 return &glob_paaudio;
860 pa_threaded_mainloop_unlock (g->mainloop);
862 AUD_log (AUDIO_CAP, "Failed to initialize PA context");
866 static void qpa_audio_fini (void *opaque)
871 pa_threaded_mainloop_stop (g->mainloop);
875 pa_context_disconnect (g->context);
876 pa_context_unref (g->context);
881 pa_threaded_mainloop_free (g->mainloop);
887 struct audio_option qpa_options[] = {
891 .valp = &glob_paaudio.samples,
892 .descr = "buffer size in samples"
897 .valp = &glob_paaudio.server,
898 .descr = "server address"
903 .valp = &glob_paaudio.sink,
904 .descr = "sink device name"
909 .valp = &glob_paaudio.source,
910 .descr = "source device name"
912 { /* End of list */ }
915 static struct audio_pcm_ops qpa_pcm_ops = {
916 .init_out = qpa_init_out,
917 .fini_out = qpa_fini_out,
918 .run_out = qpa_run_out,
920 .ctl_out = qpa_ctl_out,
922 .init_in = qpa_init_in,
923 .fini_in = qpa_fini_in,
924 .run_in = qpa_run_in,
929 struct audio_driver pa_audio_driver = {
931 .descr = "http://www.pulseaudio.org/",
932 .options = qpa_options,
933 .init = qpa_audio_init,
934 .fini = qpa_audio_fini,
935 .pcm_ops = &qpa_pcm_ops,
937 .max_voices_out = INT_MAX,
938 .max_voices_in = INT_MAX,
939 .voice_size_out = sizeof (PAVoiceOut),
940 .voice_size_in = sizeof (PAVoiceIn),
941 .ctl_caps = VOICE_VOLUME_CAP