]> git.proxmox.com Git - qemu.git/blame - audio/paaudio.c
pulseaudio: setup buffer attrs
[qemu.git] / audio / paaudio.c
CommitLineData
b8e59f18 1/* public domain */
2#include "qemu-common.h"
3#include "audio.h"
4
5#include <pulse/simple.h>
6#include <pulse/error.h>
7
8#define AUDIO_CAP "pulseaudio"
9#include "audio_int.h"
10#include "audio_pt_int.h"
11
12typedef struct {
13 HWVoiceOut hw;
14 int done;
15 int live;
16 int decr;
17 int rpos;
18 pa_simple *s;
19 void *pcm_buf;
20 struct audio_pt pt;
21} PAVoiceOut;
22
23typedef struct {
24 HWVoiceIn hw;
25 int done;
26 int dead;
27 int incr;
28 int wpos;
29 pa_simple *s;
30 void *pcm_buf;
31 struct audio_pt pt;
32} PAVoiceIn;
33
34static struct {
35 int samples;
36 int divisor;
37 char *server;
38 char *sink;
39 char *source;
40} conf = {
1a40d5e2
JQ
41 .samples = 1024,
42 .divisor = 2,
b8e59f18 43};
44
45static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
46{
47 va_list ap;
48
49 va_start (ap, fmt);
50 AUD_vlog (AUDIO_CAP, fmt, ap);
51 va_end (ap);
52
53 AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err));
54}
55
56static void *qpa_thread_out (void *arg)
57{
58 PAVoiceOut *pa = arg;
59 HWVoiceOut *hw = &pa->hw;
b8e59f18 60
61 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
62 return NULL;
63 }
64
65 for (;;) {
66 int decr, to_mix, rpos;
67
68 for (;;) {
69 if (pa->done) {
70 goto exit;
71 }
72
6315633b 73 if (pa->live > 0) {
b8e59f18 74 break;
75 }
76
77 if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
78 goto exit;
79 }
80 }
81
6315633b
GH
82 decr = to_mix = audio_MIN (pa->live, conf.samples >> 2);
83 rpos = pa->rpos;
b8e59f18 84
85 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
86 return NULL;
87 }
88
89 while (to_mix) {
90 int error;
91 int chunk = audio_MIN (to_mix, hw->samples - rpos);
1ea879e5 92 struct st_sample *src = hw->mix_buf + rpos;
b8e59f18 93
94 hw->clip (pa->pcm_buf, src, chunk);
95
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");
99 return NULL;
100 }
101
102 rpos = (rpos + chunk) % hw->samples;
103 to_mix -= chunk;
104 }
105
106 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
107 return NULL;
108 }
109
110 pa->rpos = rpos;
6315633b 111 pa->live -= decr;
b8e59f18 112 pa->decr += decr;
113 }
114
115 exit:
116 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
117 return NULL;
118}
119
bdff253c 120static int qpa_run_out (HWVoiceOut *hw, int live)
b8e59f18 121{
bdff253c 122 int decr;
b8e59f18 123 PAVoiceOut *pa = (PAVoiceOut *) hw;
124
125 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
126 return 0;
127 }
128
b8e59f18 129 decr = audio_MIN (live, pa->decr);
130 pa->decr -= decr;
131 pa->live = live - decr;
132 hw->rpos = pa->rpos;
133 if (pa->live > 0) {
134 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
135 }
136 else {
137 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
138 }
139 return decr;
140}
141
142static int qpa_write (SWVoiceOut *sw, void *buf, int len)
143{
144 return audio_pcm_sw_write (sw, buf, len);
145}
146
147/* capture */
148static void *qpa_thread_in (void *arg)
149{
150 PAVoiceIn *pa = arg;
151 HWVoiceIn *hw = &pa->hw;
b8e59f18 152
153 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
154 return NULL;
155 }
156
157 for (;;) {
158 int incr, to_grab, wpos;
159
160 for (;;) {
161 if (pa->done) {
162 goto exit;
163 }
164
6315633b 165 if (pa->dead > 0) {
b8e59f18 166 break;
167 }
168
169 if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) {
170 goto exit;
171 }
172 }
173
6315633b
GH
174 incr = to_grab = audio_MIN (pa->dead, conf.samples >> 2);
175 wpos = pa->wpos;
b8e59f18 176
177 if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
178 return NULL;
179 }
180
181 while (to_grab) {
182 int error;
183 int chunk = audio_MIN (to_grab, hw->samples - wpos);
184 void *buf = advance (pa->pcm_buf, wpos);
185
186 if (pa_simple_read (pa->s, buf,
187 chunk << hw->info.shift, &error) < 0) {
188 qpa_logerr (error, "pa_simple_read failed\n");
189 return NULL;
190 }
191
00e07679 192 hw->conv (hw->conv_buf + wpos, buf, chunk);
b8e59f18 193 wpos = (wpos + chunk) % hw->samples;
194 to_grab -= chunk;
195 }
196
197 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
198 return NULL;
199 }
200
201 pa->wpos = wpos;
202 pa->dead -= incr;
203 pa->incr += incr;
204 }
205
206 exit:
207 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
208 return NULL;
209}
210
211static int qpa_run_in (HWVoiceIn *hw)
212{
213 int live, incr, dead;
214 PAVoiceIn *pa = (PAVoiceIn *) hw;
215
216 if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) {
217 return 0;
218 }
219
220 live = audio_pcm_hw_get_live_in (hw);
221 dead = hw->samples - live;
222 incr = audio_MIN (dead, pa->incr);
223 pa->incr -= incr;
224 pa->dead = dead - incr;
225 hw->wpos = pa->wpos;
226 if (pa->dead > 0) {
227 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
228 }
229 else {
230 audio_pt_unlock (&pa->pt, AUDIO_FUNC);
231 }
232 return incr;
233}
234
235static int qpa_read (SWVoiceIn *sw, void *buf, int len)
236{
237 return audio_pcm_sw_read (sw, buf, len);
238}
239
240static pa_sample_format_t audfmt_to_pa (audfmt_e afmt, int endianness)
241{
242 int format;
243
244 switch (afmt) {
245 case AUD_FMT_S8:
246 case AUD_FMT_U8:
247 format = PA_SAMPLE_U8;
248 break;
249 case AUD_FMT_S16:
250 case AUD_FMT_U16:
251 format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE;
252 break;
253 case AUD_FMT_S32:
254 case AUD_FMT_U32:
255 format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE;
256 break;
257 default:
258 dolog ("Internal logic error: Bad audio format %d\n", afmt);
259 format = PA_SAMPLE_U8;
260 break;
261 }
262 return format;
263}
264
265static audfmt_e pa_to_audfmt (pa_sample_format_t fmt, int *endianness)
266{
267 switch (fmt) {
268 case PA_SAMPLE_U8:
269 return AUD_FMT_U8;
270 case PA_SAMPLE_S16BE:
271 *endianness = 1;
272 return AUD_FMT_S16;
273 case PA_SAMPLE_S16LE:
274 *endianness = 0;
275 return AUD_FMT_S16;
276 case PA_SAMPLE_S32BE:
277 *endianness = 1;
278 return AUD_FMT_S32;
279 case PA_SAMPLE_S32LE:
280 *endianness = 0;
281 return AUD_FMT_S32;
282 default:
283 dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt);
284 return AUD_FMT_U8;
285 }
286}
287
1ea879e5 288static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
b8e59f18 289{
290 int error;
291 static pa_sample_spec ss;
e6d16fa4 292 static pa_buffer_attr ba;
1ea879e5 293 struct audsettings obt_as = *as;
b8e59f18 294 PAVoiceOut *pa = (PAVoiceOut *) hw;
295
296 ss.format = audfmt_to_pa (as->fmt, as->endianness);
297 ss.channels = as->nchannels;
298 ss.rate = as->freq;
299
e6d16fa4
GH
300 /*
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.
303 */
304 ba.tlength = pa_usec_to_bytes (4 * 1000, &ss);
305 ba.minreq = pa_usec_to_bytes (2 * 1000, &ss);
306 ba.maxlength = -1;
307 ba.prebuf = -1;
308
b8e59f18 309 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
310
311 pa->s = pa_simple_new (
312 conf.server,
313 "qemu",
314 PA_STREAM_PLAYBACK,
315 conf.sink,
316 "pcm.playback",
317 &ss,
318 NULL, /* channel map */
e6d16fa4 319 &ba, /* buffering attributes */
b8e59f18 320 &error
321 );
322 if (!pa->s) {
323 qpa_logerr (error, "pa_simple_new for playback failed\n");
324 goto fail1;
325 }
326
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);
6315633b 330 pa->rpos = hw->rpos;
b8e59f18 331 if (!pa->pcm_buf) {
332 dolog ("Could not allocate buffer (%d bytes)\n",
333 hw->samples << hw->info.shift);
334 goto fail2;
335 }
336
337 if (audio_pt_init (&pa->pt, qpa_thread_out, hw, AUDIO_CAP, AUDIO_FUNC)) {
338 goto fail3;
339 }
340
341 return 0;
342
343 fail3:
5d928867 344 qemu_free (pa->pcm_buf);
b8e59f18 345 pa->pcm_buf = NULL;
346 fail2:
347 pa_simple_free (pa->s);
348 pa->s = NULL;
349 fail1:
350 return -1;
351}
352
1ea879e5 353static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
b8e59f18 354{
355 int error;
356 static pa_sample_spec ss;
1ea879e5 357 struct audsettings obt_as = *as;
b8e59f18 358 PAVoiceIn *pa = (PAVoiceIn *) hw;
359
360 ss.format = audfmt_to_pa (as->fmt, as->endianness);
361 ss.channels = as->nchannels;
362 ss.rate = as->freq;
363
364 obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
365
366 pa->s = pa_simple_new (
367 conf.server,
368 "qemu",
369 PA_STREAM_RECORD,
370 conf.source,
371 "pcm.capture",
372 &ss,
373 NULL, /* channel map */
374 NULL, /* buffering attributes */
375 &error
376 );
377 if (!pa->s) {
378 qpa_logerr (error, "pa_simple_new for capture failed\n");
379 goto fail1;
380 }
381
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);
6315633b 385 pa->wpos = hw->wpos;
b8e59f18 386 if (!pa->pcm_buf) {
387 dolog ("Could not allocate buffer (%d bytes)\n",
388 hw->samples << hw->info.shift);
389 goto fail2;
390 }
391
392 if (audio_pt_init (&pa->pt, qpa_thread_in, hw, AUDIO_CAP, AUDIO_FUNC)) {
393 goto fail3;
394 }
395
396 return 0;
397
398 fail3:
5d928867 399 qemu_free (pa->pcm_buf);
b8e59f18 400 pa->pcm_buf = NULL;
401 fail2:
402 pa_simple_free (pa->s);
403 pa->s = NULL;
404 fail1:
405 return -1;
406}
407
408static void qpa_fini_out (HWVoiceOut *hw)
409{
410 void *ret;
411 PAVoiceOut *pa = (PAVoiceOut *) hw;
412
413 audio_pt_lock (&pa->pt, AUDIO_FUNC);
414 pa->done = 1;
415 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
416 audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
417
418 if (pa->s) {
419 pa_simple_free (pa->s);
420 pa->s = NULL;
421 }
422
423 audio_pt_fini (&pa->pt, AUDIO_FUNC);
424 qemu_free (pa->pcm_buf);
425 pa->pcm_buf = NULL;
426}
427
428static void qpa_fini_in (HWVoiceIn *hw)
429{
430 void *ret;
431 PAVoiceIn *pa = (PAVoiceIn *) hw;
432
433 audio_pt_lock (&pa->pt, AUDIO_FUNC);
434 pa->done = 1;
435 audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
436 audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
437
438 if (pa->s) {
439 pa_simple_free (pa->s);
440 pa->s = NULL;
441 }
442
443 audio_pt_fini (&pa->pt, AUDIO_FUNC);
444 qemu_free (pa->pcm_buf);
445 pa->pcm_buf = NULL;
446}
447
448static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
449{
450 (void) hw;
451 (void) cmd;
452 return 0;
453}
454
455static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
456{
457 (void) hw;
458 (void) cmd;
459 return 0;
460}
461
462/* common */
463static void *qpa_audio_init (void)
464{
465 return &conf;
466}
467
468static void qpa_audio_fini (void *opaque)
469{
470 (void) opaque;
471}
472
473struct audio_option qpa_options[] = {
98f9f48c 474 {
475 .name = "SAMPLES",
476 .tag = AUD_OPT_INT,
477 .valp = &conf.samples,
478 .descr = "buffer size in samples"
479 },
480 {
481 .name = "DIVISOR",
482 .tag = AUD_OPT_INT,
483 .valp = &conf.divisor,
484 .descr = "threshold divisor"
485 },
486 {
487 .name = "SERVER",
488 .tag = AUD_OPT_STR,
489 .valp = &conf.server,
490 .descr = "server address"
491 },
492 {
493 .name = "SINK",
494 .tag = AUD_OPT_STR,
495 .valp = &conf.sink,
496 .descr = "sink device name"
497 },
498 {
499 .name = "SOURCE",
500 .tag = AUD_OPT_STR,
501 .valp = &conf.source,
502 .descr = "source device name"
503 },
2700efa3 504 { /* End of list */ }
b8e59f18 505};
506
35f4b58c 507static struct audio_pcm_ops qpa_pcm_ops = {
1dd3e4d1
JQ
508 .init_out = qpa_init_out,
509 .fini_out = qpa_fini_out,
510 .run_out = qpa_run_out,
511 .write = qpa_write,
512 .ctl_out = qpa_ctl_out,
513
514 .init_in = qpa_init_in,
515 .fini_in = qpa_fini_in,
516 .run_in = qpa_run_in,
517 .read = qpa_read,
518 .ctl_in = qpa_ctl_in
b8e59f18 519};
520
521struct audio_driver pa_audio_driver = {
bee37f32
JQ
522 .name = "pa",
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,
1a4ea1e3 528 .can_be_default = 1,
bee37f32
JQ
529 .max_voices_out = INT_MAX,
530 .max_voices_in = INT_MAX,
531 .voice_size_out = sizeof (PAVoiceOut),
532 .voice_size_in = sizeof (PAVoiceIn)
b8e59f18 533};