2 #include "qemu-common.h"
5 #include <pulse/simple.h>
6 #include <pulse/error.h>
8 #define AUDIO_CAP "pulseaudio"
10 #include "audio_pt_int.h"
45 static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err
, const char *fmt
, ...)
50 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
53 AUD_log (AUDIO_CAP
, "Reason: %s\n", pa_strerror (err
));
56 static void *qpa_thread_out (void *arg
)
59 HWVoiceOut
*hw
= &pa
->hw
;
61 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
66 int decr
, to_mix
, rpos
;
77 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
82 decr
= to_mix
= audio_MIN (pa
->live
, conf
.samples
>> 2);
85 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
91 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
92 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
94 hw
->clip (pa
->pcm_buf
, src
, chunk
);
96 if (pa_simple_write (pa
->s
, pa
->pcm_buf
,
97 chunk
<< hw
->info
.shift
, &error
) < 0) {
98 qpa_logerr (error
, "pa_simple_write failed\n");
102 rpos
= (rpos
+ chunk
) % hw
->samples
;
106 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
116 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
120 static int qpa_run_out (HWVoiceOut
*hw
, int live
)
123 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
125 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
129 decr
= audio_MIN (live
, pa
->decr
);
131 pa
->live
= live
- decr
;
134 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
137 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
142 static int qpa_write (SWVoiceOut
*sw
, void *buf
, int len
)
144 return audio_pcm_sw_write (sw
, buf
, len
);
148 static void *qpa_thread_in (void *arg
)
151 HWVoiceIn
*hw
= &pa
->hw
;
153 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
158 int incr
, to_grab
, wpos
;
169 if (audio_pt_wait (&pa
->pt
, AUDIO_FUNC
)) {
174 incr
= to_grab
= audio_MIN (pa
->dead
, conf
.samples
>> 2);
177 if (audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
)) {
183 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
184 void *buf
= advance (pa
->pcm_buf
, wpos
);
186 if (pa_simple_read (pa
->s
, buf
,
187 chunk
<< hw
->info
.shift
, &error
) < 0) {
188 qpa_logerr (error
, "pa_simple_read failed\n");
192 hw
->conv (hw
->conv_buf
+ wpos
, buf
, chunk
);
193 wpos
= (wpos
+ chunk
) % hw
->samples
;
197 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
207 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
211 static int qpa_run_in (HWVoiceIn
*hw
)
213 int live
, incr
, dead
;
214 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
216 if (audio_pt_lock (&pa
->pt
, AUDIO_FUNC
)) {
220 live
= audio_pcm_hw_get_live_in (hw
);
221 dead
= hw
->samples
- live
;
222 incr
= audio_MIN (dead
, pa
->incr
);
224 pa
->dead
= dead
- incr
;
227 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
230 audio_pt_unlock (&pa
->pt
, AUDIO_FUNC
);
235 static int qpa_read (SWVoiceIn
*sw
, void *buf
, int len
)
237 return audio_pcm_sw_read (sw
, buf
, len
);
240 static pa_sample_format_t
audfmt_to_pa (audfmt_e afmt
, int endianness
)
247 format
= PA_SAMPLE_U8
;
251 format
= endianness
? PA_SAMPLE_S16BE
: PA_SAMPLE_S16LE
;
255 format
= endianness
? PA_SAMPLE_S32BE
: PA_SAMPLE_S32LE
;
258 dolog ("Internal logic error: Bad audio format %d\n", afmt
);
259 format
= PA_SAMPLE_U8
;
265 static audfmt_e
pa_to_audfmt (pa_sample_format_t fmt
, int *endianness
)
270 case PA_SAMPLE_S16BE
:
273 case PA_SAMPLE_S16LE
:
276 case PA_SAMPLE_S32BE
:
279 case PA_SAMPLE_S32LE
:
283 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt
);
288 static int qpa_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
291 static pa_sample_spec ss
;
292 static pa_buffer_attr ba
;
293 struct audsettings obt_as
= *as
;
294 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
296 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
297 ss
.channels
= as
->nchannels
;
301 * qemu audio tick runs at 250 Hz (by default), so processing
302 * data chunks worth 4 ms of sound should be a good fit.
304 ba
.tlength
= pa_usec_to_bytes (4 * 1000, &ss
);
305 ba
.minreq
= pa_usec_to_bytes (2 * 1000, &ss
);
309 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
311 pa
->s
= pa_simple_new (
318 NULL
, /* channel map */
319 &ba
, /* buffering attributes */
323 qpa_logerr (error
, "pa_simple_new for playback failed\n");
327 audio_pcm_init_info (&hw
->info
, &obt_as
);
328 hw
->samples
= conf
.samples
;
329 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
332 dolog ("Could not allocate buffer (%d bytes)\n",
333 hw
->samples
<< hw
->info
.shift
);
337 if (audio_pt_init (&pa
->pt
, qpa_thread_out
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
344 qemu_free (pa
->pcm_buf
);
347 pa_simple_free (pa
->s
);
353 static int qpa_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
356 static pa_sample_spec ss
;
357 struct audsettings obt_as
= *as
;
358 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
360 ss
.format
= audfmt_to_pa (as
->fmt
, as
->endianness
);
361 ss
.channels
= as
->nchannels
;
364 obt_as
.fmt
= pa_to_audfmt (ss
.format
, &obt_as
.endianness
);
366 pa
->s
= pa_simple_new (
373 NULL
, /* channel map */
374 NULL
, /* buffering attributes */
378 qpa_logerr (error
, "pa_simple_new for capture failed\n");
382 audio_pcm_init_info (&hw
->info
, &obt_as
);
383 hw
->samples
= conf
.samples
;
384 pa
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
387 dolog ("Could not allocate buffer (%d bytes)\n",
388 hw
->samples
<< hw
->info
.shift
);
392 if (audio_pt_init (&pa
->pt
, qpa_thread_in
, hw
, AUDIO_CAP
, AUDIO_FUNC
)) {
399 qemu_free (pa
->pcm_buf
);
402 pa_simple_free (pa
->s
);
408 static void qpa_fini_out (HWVoiceOut
*hw
)
411 PAVoiceOut
*pa
= (PAVoiceOut
*) hw
;
413 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
415 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
416 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
419 pa_simple_free (pa
->s
);
423 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
424 qemu_free (pa
->pcm_buf
);
428 static void qpa_fini_in (HWVoiceIn
*hw
)
431 PAVoiceIn
*pa
= (PAVoiceIn
*) hw
;
433 audio_pt_lock (&pa
->pt
, AUDIO_FUNC
);
435 audio_pt_unlock_and_signal (&pa
->pt
, AUDIO_FUNC
);
436 audio_pt_join (&pa
->pt
, &ret
, AUDIO_FUNC
);
439 pa_simple_free (pa
->s
);
443 audio_pt_fini (&pa
->pt
, AUDIO_FUNC
);
444 qemu_free (pa
->pcm_buf
);
448 static int qpa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
455 static int qpa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
463 static void *qpa_audio_init (void)
468 static void qpa_audio_fini (void *opaque
)
473 struct audio_option qpa_options
[] = {
477 .valp
= &conf
.samples
,
478 .descr
= "buffer size in samples"
483 .valp
= &conf
.divisor
,
484 .descr
= "threshold divisor"
489 .valp
= &conf
.server
,
490 .descr
= "server address"
496 .descr
= "sink device name"
501 .valp
= &conf
.source
,
502 .descr
= "source device name"
504 { /* End of list */ }
507 static struct audio_pcm_ops qpa_pcm_ops
= {
508 .init_out
= qpa_init_out
,
509 .fini_out
= qpa_fini_out
,
510 .run_out
= qpa_run_out
,
512 .ctl_out
= qpa_ctl_out
,
514 .init_in
= qpa_init_in
,
515 .fini_in
= qpa_fini_in
,
516 .run_in
= qpa_run_in
,
521 struct audio_driver pa_audio_driver
= {
523 .descr
= "http://www.pulseaudio.org/",
524 .options
= qpa_options
,
525 .init
= qpa_audio_init
,
526 .fini
= qpa_audio_fini
,
527 .pcm_ops
= &qpa_pcm_ops
,
529 .max_voices_out
= INT_MAX
,
530 .max_voices_in
= INT_MAX
,
531 .voice_size_out
= sizeof (PAVoiceOut
),
532 .voice_size_in
= sizeof (PAVoiceIn
)