2 #include "qemu/osdep.h"
3 #include "qemu-common.h"
5 #include "qapi/opts-visitor.h"
7 #include <pulse/pulseaudio.h>
9 #define AUDIO_CAP "pulseaudio"
10 #include "audio_int.h"
11 #include "audio_pt_int.h"
15 pa_threaded_mainloop *mainloop;
41 const void *read_data;
42 size_t read_index, read_length;
47 static void qpa_audio_fini(void *opaque);
49 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
54 AUD_vlog (AUDIO_CAP, fmt, ap);
57 AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err));
60 #ifndef PA_CONTEXT_IS_GOOD
61 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x)
64 x == PA_CONTEXT_CONNECTING ||
65 x == PA_CONTEXT_AUTHORIZING ||
66 x == PA_CONTEXT_SETTING_NAME ||
67 x == PA_CONTEXT_READY;
71 #ifndef PA_STREAM_IS_GOOD
72 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
75 x == PA_STREAM_CREATING ||
80 #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
82 if (!(expression)) { \
84 *(rerror) = pa_context_errno ((c)->context); \
90 #define CHECK_DEAD_GOTO(c, stream, rerror, label) \
92 if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
93 !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
94 if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
95 ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
97 *(rerror) = pa_context_errno ((c)->context); \
101 *(rerror) = PA_ERR_BADSTATE; \
108 static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
112 pa_threaded_mainloop_lock (g->mainloop);
114 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
119 while (!p->read_data) {
122 r = pa_stream_peek (p->stream, &p->read_data, &p->read_length);
123 CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
126 pa_threaded_mainloop_wait (g->mainloop);
127 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
133 l = p->read_length < length ? p->read_length : length;
134 memcpy (data, (const uint8_t *) p->read_data+p->read_index, l);
136 data = (uint8_t *) data + l;
142 if (!p->read_length) {
145 r = pa_stream_drop (p->stream);
150 CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
154 pa_threaded_mainloop_unlock (g->mainloop);
158 pa_threaded_mainloop_unlock (g->mainloop);
162 static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
166 pa_threaded_mainloop_lock (g->mainloop);
168 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
174 while (!(l = pa_stream_writable_size (p->stream))) {
175 pa_threaded_mainloop_wait (g->mainloop);
176 CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
179 CHECK_SUCCESS_GOTO (g, rerror, l != (size_t) -1, unlock_and_fail);
185 r = pa_stream_write (p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE);
186 CHECK_SUCCESS_GOTO (g, rerror, r >= 0, unlock_and_fail);
188 data = (const uint8_t *) data + l;
192 pa_threaded_mainloop_unlock (g->mainloop);
196 pa_threaded_mainloop_unlock (g->mainloop);
200 static void *qpa_thread_out (void *arg)
202 PAVoiceOut *pa = arg;
203 HWVoiceOut *hw = &pa->hw;
205 if (audio_pt_lock(&pa->pt, __func__)) {
210 int decr, to_mix, rpos;
221 if (audio_pt_wait(&pa->pt, __func__)) {
226 decr = to_mix = audio_MIN(pa->live, pa->samples >> 5);
229 if (audio_pt_unlock(&pa->pt, __func__)) {
235 int chunk = audio_MIN (to_mix, hw->samples - rpos);
236 struct st_sample *src = hw->mix_buf + rpos;
238 hw->clip (pa->pcm_buf, src, chunk);
240 if (qpa_simple_write (pa, pa->pcm_buf,
241 chunk << hw->info.shift, &error) < 0) {
242 qpa_logerr (error, "pa_simple_write failed\n");
246 rpos = (rpos + chunk) % hw->samples;
250 if (audio_pt_lock(&pa->pt, __func__)) {
260 audio_pt_unlock(&pa->pt, __func__);
264 static int qpa_run_out (HWVoiceOut *hw, int live)
267 PAVoiceOut *pa = (PAVoiceOut *) hw;
269 if (audio_pt_lock(&pa->pt, __func__)) {
273 decr = audio_MIN (live, pa->decr);
275 pa->live = live - decr;
278 audio_pt_unlock_and_signal(&pa->pt, __func__);
281 audio_pt_unlock(&pa->pt, __func__);
286 static int qpa_write (SWVoiceOut *sw, void *buf, int len)
288 return audio_pcm_sw_write (sw, buf, len);
292 static void *qpa_thread_in (void *arg)
295 HWVoiceIn *hw = &pa->hw;
297 if (audio_pt_lock(&pa->pt, __func__)) {
302 int incr, to_grab, wpos;
313 if (audio_pt_wait(&pa->pt, __func__)) {
318 incr = to_grab = audio_MIN(pa->dead, pa->samples >> 5);
321 if (audio_pt_unlock(&pa->pt, __func__)) {
327 int chunk = audio_MIN (to_grab, hw->samples - wpos);
328 void *buf = advance (pa->pcm_buf, wpos);
330 if (qpa_simple_read (pa, buf,
331 chunk << hw->info.shift, &error) < 0) {
332 qpa_logerr (error, "pa_simple_read failed\n");
336 hw->conv (hw->conv_buf + wpos, buf, chunk);
337 wpos = (wpos + chunk) % hw->samples;
341 if (audio_pt_lock(&pa->pt, __func__)) {
351 audio_pt_unlock(&pa->pt, __func__);
355 static int qpa_run_in (HWVoiceIn *hw)
357 int live, incr, dead;
358 PAVoiceIn *pa = (PAVoiceIn *) hw;
360 if (audio_pt_lock(&pa->pt, __func__)) {
364 live = audio_pcm_hw_get_live_in (hw);
365 dead = hw->samples - live;
366 incr = audio_MIN (dead, pa->incr);
368 pa->dead = dead - incr;
371 audio_pt_unlock_and_signal(&pa->pt, __func__);
374 audio_pt_unlock(&pa->pt, __func__);
379 static int qpa_read (SWVoiceIn *sw, void *buf, int len)
381 return audio_pcm_sw_read (sw, buf, len);
384 static pa_sample_format_t audfmt_to_pa (AudioFormat afmt, int endianness)
389 case AUDIO_FORMAT_S8:
390 case AUDIO_FORMAT_U8:
391 format = PA_SAMPLE_U8;
393 case AUDIO_FORMAT_S16:
394 case AUDIO_FORMAT_U16:
395 format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE;
397 case AUDIO_FORMAT_S32:
398 case AUDIO_FORMAT_U32:
399 format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE;
402 dolog ("Internal logic error: Bad audio format %d\n", afmt);
403 format = PA_SAMPLE_U8;
409 static AudioFormat pa_to_audfmt (pa_sample_format_t fmt, int *endianness)
413 return AUDIO_FORMAT_U8;
414 case PA_SAMPLE_S16BE:
416 return AUDIO_FORMAT_S16;
417 case PA_SAMPLE_S16LE:
419 return AUDIO_FORMAT_S16;
420 case PA_SAMPLE_S32BE:
422 return AUDIO_FORMAT_S32;
423 case PA_SAMPLE_S32LE:
425 return AUDIO_FORMAT_S32;
427 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt);
428 return AUDIO_FORMAT_U8;
432 static void context_state_cb (pa_context *c, void *userdata)
434 paaudio *g = userdata;
436 switch (pa_context_get_state(c)) {
437 case PA_CONTEXT_READY:
438 case PA_CONTEXT_TERMINATED:
439 case PA_CONTEXT_FAILED:
440 pa_threaded_mainloop_signal (g->mainloop, 0);
443 case PA_CONTEXT_UNCONNECTED:
444 case PA_CONTEXT_CONNECTING:
445 case PA_CONTEXT_AUTHORIZING:
446 case PA_CONTEXT_SETTING_NAME:
451 static void stream_state_cb (pa_stream *s, void * userdata)
453 paaudio *g = userdata;
455 switch (pa_stream_get_state (s)) {
457 case PA_STREAM_READY:
458 case PA_STREAM_FAILED:
459 case PA_STREAM_TERMINATED:
460 pa_threaded_mainloop_signal (g->mainloop, 0);
463 case PA_STREAM_UNCONNECTED:
464 case PA_STREAM_CREATING:
469 static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
471 paaudio *g = userdata;
473 pa_threaded_mainloop_signal (g->mainloop, 0);
476 static pa_stream *qpa_simple_new (
479 pa_stream_direction_t dir,
481 const pa_sample_spec *ss,
482 const pa_channel_map *map,
483 const pa_buffer_attr *attr,
489 pa_threaded_mainloop_lock (g->mainloop);
491 stream = pa_stream_new (g->context, name, ss, map);
496 pa_stream_set_state_callback (stream, stream_state_cb, g);
497 pa_stream_set_read_callback (stream, stream_request_cb, g);
498 pa_stream_set_write_callback (stream, stream_request_cb, g);
500 if (dir == PA_STREAM_PLAYBACK) {
501 r = pa_stream_connect_playback (stream, dev, attr,
502 PA_STREAM_INTERPOLATE_TIMING
503 #ifdef PA_STREAM_ADJUST_LATENCY
504 |PA_STREAM_ADJUST_LATENCY
506 |PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
508 r = pa_stream_connect_record (stream, dev, attr,
509 PA_STREAM_INTERPOLATE_TIMING
510 #ifdef PA_STREAM_ADJUST_LATENCY
511 |PA_STREAM_ADJUST_LATENCY
513 |PA_STREAM_AUTO_TIMING_UPDATE);
520 pa_threaded_mainloop_unlock (g->mainloop);
525 pa_threaded_mainloop_unlock (g->mainloop);
528 pa_stream_unref (stream);
531 *rerror = pa_context_errno (g->context);
536 static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
542 struct audsettings obt_as = *as;
543 PAVoiceOut *pa = (PAVoiceOut *) hw;
544 paaudio *g = pa->g = drv_opaque;
545 AudiodevPaOptions *popts = &g->dev->u.pa;
546 AudiodevPaPerDirectionOptions *ppdo = popts->out;
548 ss.format = audfmt_to_pa (as->fmt, as->endianness);
549 ss.channels = as->nchannels;
552 ba.tlength = pa_usec_to_bytes(ppdo->latency, &ss);
557 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
559 pa->stream = qpa_simple_new (
563 ppdo->has_name ? ppdo->name : NULL,
565 NULL, /* channel map */
566 &ba, /* buffering attributes */
570 qpa_logerr (error, "pa_simple_new for playback failed\n");
574 audio_pcm_init_info (&hw->info, &obt_as);
575 hw->samples = pa->samples = audio_buffer_samples(
576 qapi_AudiodevPaPerDirectionOptions_base(ppdo),
577 &obt_as, ppdo->buffer_length);
578 pa->pcm_buf = audio_calloc(__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, __func__)) {
593 g_free (pa->pcm_buf);
597 pa_stream_unref (pa->stream);
604 static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
609 struct audsettings obt_as = *as;
610 PAVoiceIn *pa = (PAVoiceIn *) hw;
611 paaudio *g = pa->g = drv_opaque;
612 AudiodevPaOptions *popts = &g->dev->u.pa;
613 AudiodevPaPerDirectionOptions *ppdo = popts->in;
615 ss.format = audfmt_to_pa (as->fmt, as->endianness);
616 ss.channels = as->nchannels;
619 ba.fragsize = pa_usec_to_bytes(ppdo->latency, &ss);
624 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
626 pa->stream = qpa_simple_new (
630 ppdo->has_name ? ppdo->name : NULL,
632 NULL, /* channel map */
633 &ba, /* buffering attributes */
637 qpa_logerr (error, "pa_simple_new for capture failed\n");
641 audio_pcm_init_info (&hw->info, &obt_as);
642 hw->samples = pa->samples = audio_buffer_samples(
643 qapi_AudiodevPaPerDirectionOptions_base(ppdo),
644 &obt_as, ppdo->buffer_length);
645 pa->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
648 dolog ("Could not allocate buffer (%d bytes)\n",
649 hw->samples << hw->info.shift);
653 if (audio_pt_init(&pa->pt, qpa_thread_in, hw, AUDIO_CAP, __func__)) {
660 g_free (pa->pcm_buf);
664 pa_stream_unref (pa->stream);
671 static void qpa_fini_out (HWVoiceOut *hw)
674 PAVoiceOut *pa = (PAVoiceOut *) hw;
676 audio_pt_lock(&pa->pt, __func__);
678 audio_pt_unlock_and_signal(&pa->pt, __func__);
679 audio_pt_join(&pa->pt, &ret, __func__);
682 pa_stream_unref (pa->stream);
686 audio_pt_fini(&pa->pt, __func__);
687 g_free (pa->pcm_buf);
691 static void qpa_fini_in (HWVoiceIn *hw)
694 PAVoiceIn *pa = (PAVoiceIn *) hw;
696 audio_pt_lock(&pa->pt, __func__);
698 audio_pt_unlock_and_signal(&pa->pt, __func__);
699 audio_pt_join(&pa->pt, &ret, __func__);
702 pa_stream_unref (pa->stream);
706 audio_pt_fini(&pa->pt, __func__);
707 g_free (pa->pcm_buf);
711 static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
713 PAVoiceOut *pa = (PAVoiceOut *) hw;
718 #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
719 pa_cvolume_init (&v); /* function is present in 0.9.13+ */
729 sw = va_arg (ap, SWVoiceOut *);
733 v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
734 v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
736 pa_threaded_mainloop_lock (g->mainloop);
738 op = pa_context_set_sink_input_volume (g->context,
739 pa_stream_get_index (pa->stream),
742 qpa_logerr (pa_context_errno (g->context),
743 "set_sink_input_volume() failed\n");
745 pa_operation_unref (op);
747 op = pa_context_set_sink_input_mute (g->context,
748 pa_stream_get_index (pa->stream),
749 sw->vol.mute, NULL, NULL);
751 qpa_logerr (pa_context_errno (g->context),
752 "set_sink_input_mute() failed\n");
754 pa_operation_unref (op);
757 pa_threaded_mainloop_unlock (g->mainloop);
763 static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
765 PAVoiceIn *pa = (PAVoiceIn *) hw;
770 #ifdef PA_CHECK_VERSION
771 pa_cvolume_init (&v);
781 sw = va_arg (ap, SWVoiceIn *);
785 v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
786 v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
788 pa_threaded_mainloop_lock (g->mainloop);
790 op = pa_context_set_source_output_volume (g->context,
791 pa_stream_get_index (pa->stream),
794 qpa_logerr (pa_context_errno (g->context),
795 "set_source_output_volume() failed\n");
797 pa_operation_unref(op);
800 op = pa_context_set_source_output_mute (g->context,
801 pa_stream_get_index (pa->stream),
802 sw->vol.mute, NULL, NULL);
804 qpa_logerr (pa_context_errno (g->context),
805 "set_source_output_mute() failed\n");
807 pa_operation_unref (op);
810 pa_threaded_mainloop_unlock (g->mainloop);
816 static int qpa_validate_per_direction_opts(Audiodev *dev,
817 AudiodevPaPerDirectionOptions *pdo)
819 if (!pdo->has_buffer_length) {
820 pdo->has_buffer_length = true;
821 pdo->buffer_length = 46440;
823 if (!pdo->has_latency) {
824 pdo->has_latency = true;
825 pdo->latency = 15000;
830 static void *qpa_audio_init(Audiodev *dev)
833 AudiodevPaOptions *popts = &dev->u.pa;
836 if (!popts->has_server) {
841 runtime = getenv("XDG_RUNTIME_DIR");
845 snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime);
846 if (stat(pidfile, &st) != 0) {
851 assert(dev->driver == AUDIODEV_DRIVER_PA);
853 g = g_malloc(sizeof(paaudio));
854 server = popts->has_server ? popts->server : NULL;
856 if (!qpa_validate_per_direction_opts(dev, popts->in)) {
859 if (!qpa_validate_per_direction_opts(dev, popts->out)) {
867 g->mainloop = pa_threaded_mainloop_new ();
872 g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop),
878 pa_context_set_state_callback (g->context, context_state_cb, g);
880 if (pa_context_connect(g->context, server, 0, NULL) < 0) {
881 qpa_logerr (pa_context_errno (g->context),
882 "pa_context_connect() failed\n");
886 pa_threaded_mainloop_lock (g->mainloop);
888 if (pa_threaded_mainloop_start (g->mainloop) < 0) {
889 goto unlock_and_fail;
893 pa_context_state_t state;
895 state = pa_context_get_state (g->context);
897 if (state == PA_CONTEXT_READY) {
901 if (!PA_CONTEXT_IS_GOOD (state)) {
902 qpa_logerr (pa_context_errno (g->context),
903 "Wrong context state\n");
904 goto unlock_and_fail;
907 /* Wait until the context is ready */
908 pa_threaded_mainloop_wait (g->mainloop);
911 pa_threaded_mainloop_unlock (g->mainloop);
916 pa_threaded_mainloop_unlock (g->mainloop);
918 AUD_log (AUDIO_CAP, "Failed to initialize PA context");
923 static void qpa_audio_fini (void *opaque)
928 pa_threaded_mainloop_stop (g->mainloop);
932 pa_context_disconnect (g->context);
933 pa_context_unref (g->context);
937 pa_threaded_mainloop_free (g->mainloop);
943 static struct audio_pcm_ops qpa_pcm_ops = {
944 .init_out = qpa_init_out,
945 .fini_out = qpa_fini_out,
946 .run_out = qpa_run_out,
948 .ctl_out = qpa_ctl_out,
950 .init_in = qpa_init_in,
951 .fini_in = qpa_fini_in,
952 .run_in = qpa_run_in,
957 static struct audio_driver pa_audio_driver = {
959 .descr = "http://www.pulseaudio.org/",
960 .init = qpa_audio_init,
961 .fini = qpa_audio_fini,
962 .pcm_ops = &qpa_pcm_ops,
964 .max_voices_out = INT_MAX,
965 .max_voices_in = INT_MAX,
966 .voice_size_out = sizeof (PAVoiceOut),
967 .voice_size_in = sizeof (PAVoiceIn),
968 .ctl_caps = VOICE_VOLUME_CAP
971 static void register_audio_pa(void)
973 audio_driver_register(&pa_audio_driver);
975 type_init(register_audio_pa);