]>
Commit | Line | Data |
---|---|---|
b8e59f18 | 1 | /* public domain */ |
0b8fa32f | 2 | |
6086a565 | 3 | #include "qemu/osdep.h" |
0b8fa32f | 4 | #include "qemu/module.h" |
b8e59f18 | 5 | #include "audio.h" |
2c324b28 | 6 | #include "qapi/opts-visitor.h" |
b8e59f18 | 7 | |
ea9ebc2c | 8 | #include <pulse/pulseaudio.h> |
b8e59f18 | 9 | |
10 | #define AUDIO_CAP "pulseaudio" | |
11 | #include "audio_int.h" | |
12 | #include "audio_pt_int.h" | |
13 | ||
9a644c4b | 14 | typedef struct { |
2c324b28 | 15 | Audiodev *dev; |
9a644c4b KZ |
16 | pa_threaded_mainloop *mainloop; |
17 | pa_context *context; | |
18 | } paaudio; | |
19 | ||
b8e59f18 | 20 | typedef struct { |
21 | HWVoiceOut hw; | |
22 | int done; | |
23 | int live; | |
24 | int decr; | |
25 | int rpos; | |
ea9ebc2c | 26 | pa_stream *stream; |
b8e59f18 | 27 | void *pcm_buf; |
28 | struct audio_pt pt; | |
9a644c4b | 29 | paaudio *g; |
2c324b28 | 30 | int samples; |
b8e59f18 | 31 | } PAVoiceOut; |
32 | ||
33 | typedef struct { | |
34 | HWVoiceIn hw; | |
35 | int done; | |
36 | int dead; | |
37 | int incr; | |
38 | int wpos; | |
ea9ebc2c | 39 | pa_stream *stream; |
b8e59f18 | 40 | void *pcm_buf; |
41 | struct audio_pt pt; | |
ea9ebc2c MAL |
42 | const void *read_data; |
43 | size_t read_index, read_length; | |
9a644c4b | 44 | paaudio *g; |
2c324b28 | 45 | int samples; |
b8e59f18 | 46 | } PAVoiceIn; |
47 | ||
49dd6d0d KZ |
48 | static void qpa_audio_fini(void *opaque); |
49 | ||
b8e59f18 | 50 | static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...) |
51 | { | |
52 | va_list ap; | |
53 | ||
54 | va_start (ap, fmt); | |
55 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
56 | va_end (ap); | |
57 | ||
58 | AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err)); | |
59 | } | |
60 | ||
8f473dd1 GH |
61 | #ifndef PA_CONTEXT_IS_GOOD |
62 | static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) | |
63 | { | |
64 | return | |
65 | x == PA_CONTEXT_CONNECTING || | |
66 | x == PA_CONTEXT_AUTHORIZING || | |
67 | x == PA_CONTEXT_SETTING_NAME || | |
68 | x == PA_CONTEXT_READY; | |
69 | } | |
70 | #endif | |
71 | ||
72 | #ifndef PA_STREAM_IS_GOOD | |
73 | static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) | |
74 | { | |
75 | return | |
76 | x == PA_STREAM_CREATING || | |
77 | x == PA_STREAM_READY; | |
78 | } | |
79 | #endif | |
80 | ||
ea9ebc2c MAL |
81 | #define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \ |
82 | do { \ | |
83 | if (!(expression)) { \ | |
84 | if (rerror) { \ | |
85 | *(rerror) = pa_context_errno ((c)->context); \ | |
86 | } \ | |
87 | goto label; \ | |
88 | } \ | |
2562755e | 89 | } while (0) |
ea9ebc2c MAL |
90 | |
91 | #define CHECK_DEAD_GOTO(c, stream, rerror, label) \ | |
92 | do { \ | |
93 | if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \ | |
94 | !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \ | |
95 | if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \ | |
96 | ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \ | |
97 | if (rerror) { \ | |
98 | *(rerror) = pa_context_errno ((c)->context); \ | |
99 | } \ | |
100 | } else { \ | |
101 | if (rerror) { \ | |
102 | *(rerror) = PA_ERR_BADSTATE; \ | |
103 | } \ | |
104 | } \ | |
105 | goto label; \ | |
106 | } \ | |
2562755e | 107 | } while (0) |
ea9ebc2c MAL |
108 | |
109 | static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror) | |
110 | { | |
9a644c4b | 111 | paaudio *g = p->g; |
ea9ebc2c MAL |
112 | |
113 | pa_threaded_mainloop_lock (g->mainloop); | |
114 | ||
115 | CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail); | |
116 | ||
117 | while (length > 0) { | |
118 | size_t l; | |
119 | ||
120 | while (!p->read_data) { | |
121 | int r; | |
122 | ||
123 | r = pa_stream_peek (p->stream, &p->read_data, &p->read_length); | |
124 | CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail); | |
125 | ||
126 | if (!p->read_data) { | |
127 | pa_threaded_mainloop_wait (g->mainloop); | |
128 | CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail); | |
129 | } else { | |
130 | p->read_index = 0; | |
131 | } | |
132 | } | |
133 | ||
134 | l = p->read_length < length ? p->read_length : length; | |
135 | memcpy (data, (const uint8_t *) p->read_data+p->read_index, l); | |
136 | ||
137 | data = (uint8_t *) data + l; | |
138 | length -= l; | |
139 | ||
140 | p->read_index += l; | |
141 | p->read_length -= l; | |
142 | ||
143 | if (!p->read_length) { | |
144 | int r; | |
145 | ||
146 | r = pa_stream_drop (p->stream); | |
147 | p->read_data = NULL; | |
148 | p->read_length = 0; | |
149 | p->read_index = 0; | |
150 | ||
151 | CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail); | |
152 | } | |
153 | } | |
154 | ||
155 | pa_threaded_mainloop_unlock (g->mainloop); | |
156 | return 0; | |
157 | ||
158 | unlock_and_fail: | |
159 | pa_threaded_mainloop_unlock (g->mainloop); | |
160 | return -1; | |
161 | } | |
162 | ||
163 | static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror) | |
164 | { | |
9a644c4b | 165 | paaudio *g = p->g; |
ea9ebc2c MAL |
166 | |
167 | pa_threaded_mainloop_lock (g->mainloop); | |
168 | ||
169 | CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail); | |
170 | ||
171 | while (length > 0) { | |
172 | size_t l; | |
173 | int r; | |
174 | ||
175 | while (!(l = pa_stream_writable_size (p->stream))) { | |
176 | pa_threaded_mainloop_wait (g->mainloop); | |
177 | CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail); | |
178 | } | |
179 | ||
180 | CHECK_SUCCESS_GOTO (g, rerror, l != (size_t) -1, unlock_and_fail); | |
181 | ||
182 | if (l > length) { | |
183 | l = length; | |
184 | } | |
185 | ||
186 | r = pa_stream_write (p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE); | |
187 | CHECK_SUCCESS_GOTO (g, rerror, r >= 0, unlock_and_fail); | |
188 | ||
189 | data = (const uint8_t *) data + l; | |
190 | length -= l; | |
191 | } | |
192 | ||
193 | pa_threaded_mainloop_unlock (g->mainloop); | |
194 | return 0; | |
195 | ||
196 | unlock_and_fail: | |
197 | pa_threaded_mainloop_unlock (g->mainloop); | |
198 | return -1; | |
199 | } | |
200 | ||
b8e59f18 | 201 | static void *qpa_thread_out (void *arg) |
202 | { | |
203 | PAVoiceOut *pa = arg; | |
204 | HWVoiceOut *hw = &pa->hw; | |
b8e59f18 | 205 | |
470bcabd | 206 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 207 | return NULL; |
208 | } | |
209 | ||
210 | for (;;) { | |
211 | int decr, to_mix, rpos; | |
212 | ||
213 | for (;;) { | |
214 | if (pa->done) { | |
215 | goto exit; | |
216 | } | |
217 | ||
6315633b | 218 | if (pa->live > 0) { |
b8e59f18 | 219 | break; |
220 | } | |
221 | ||
470bcabd | 222 | if (audio_pt_wait(&pa->pt, __func__)) { |
b8e59f18 | 223 | goto exit; |
224 | } | |
225 | } | |
226 | ||
2c324b28 | 227 | decr = to_mix = audio_MIN(pa->live, pa->samples >> 5); |
6315633b | 228 | rpos = pa->rpos; |
b8e59f18 | 229 | |
470bcabd | 230 | if (audio_pt_unlock(&pa->pt, __func__)) { |
b8e59f18 | 231 | return NULL; |
232 | } | |
233 | ||
234 | while (to_mix) { | |
235 | int error; | |
236 | int chunk = audio_MIN (to_mix, hw->samples - rpos); | |
1ea879e5 | 237 | struct st_sample *src = hw->mix_buf + rpos; |
b8e59f18 | 238 | |
239 | hw->clip (pa->pcm_buf, src, chunk); | |
240 | ||
ea9ebc2c MAL |
241 | if (qpa_simple_write (pa, pa->pcm_buf, |
242 | chunk << hw->info.shift, &error) < 0) { | |
b8e59f18 | 243 | qpa_logerr (error, "pa_simple_write failed\n"); |
244 | return NULL; | |
245 | } | |
246 | ||
247 | rpos = (rpos + chunk) % hw->samples; | |
248 | to_mix -= chunk; | |
249 | } | |
250 | ||
470bcabd | 251 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 252 | return NULL; |
253 | } | |
254 | ||
255 | pa->rpos = rpos; | |
6315633b | 256 | pa->live -= decr; |
b8e59f18 | 257 | pa->decr += decr; |
258 | } | |
259 | ||
260 | exit: | |
470bcabd | 261 | audio_pt_unlock(&pa->pt, __func__); |
b8e59f18 | 262 | return NULL; |
263 | } | |
264 | ||
bdff253c | 265 | static int qpa_run_out (HWVoiceOut *hw, int live) |
b8e59f18 | 266 | { |
bdff253c | 267 | int decr; |
b8e59f18 | 268 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
269 | ||
470bcabd | 270 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 271 | return 0; |
272 | } | |
273 | ||
b8e59f18 | 274 | decr = audio_MIN (live, pa->decr); |
275 | pa->decr -= decr; | |
276 | pa->live = live - decr; | |
277 | hw->rpos = pa->rpos; | |
278 | if (pa->live > 0) { | |
470bcabd | 279 | audio_pt_unlock_and_signal(&pa->pt, __func__); |
b8e59f18 | 280 | } |
281 | else { | |
470bcabd | 282 | audio_pt_unlock(&pa->pt, __func__); |
b8e59f18 | 283 | } |
284 | return decr; | |
285 | } | |
286 | ||
287 | static int qpa_write (SWVoiceOut *sw, void *buf, int len) | |
288 | { | |
289 | return audio_pcm_sw_write (sw, buf, len); | |
290 | } | |
291 | ||
292 | /* capture */ | |
293 | static void *qpa_thread_in (void *arg) | |
294 | { | |
295 | PAVoiceIn *pa = arg; | |
296 | HWVoiceIn *hw = &pa->hw; | |
b8e59f18 | 297 | |
470bcabd | 298 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 299 | return NULL; |
300 | } | |
301 | ||
302 | for (;;) { | |
303 | int incr, to_grab, wpos; | |
304 | ||
305 | for (;;) { | |
306 | if (pa->done) { | |
307 | goto exit; | |
308 | } | |
309 | ||
6315633b | 310 | if (pa->dead > 0) { |
b8e59f18 | 311 | break; |
312 | } | |
313 | ||
470bcabd | 314 | if (audio_pt_wait(&pa->pt, __func__)) { |
b8e59f18 | 315 | goto exit; |
316 | } | |
317 | } | |
318 | ||
2c324b28 | 319 | incr = to_grab = audio_MIN(pa->dead, pa->samples >> 5); |
6315633b | 320 | wpos = pa->wpos; |
b8e59f18 | 321 | |
470bcabd | 322 | if (audio_pt_unlock(&pa->pt, __func__)) { |
b8e59f18 | 323 | return NULL; |
324 | } | |
325 | ||
326 | while (to_grab) { | |
327 | int error; | |
328 | int chunk = audio_MIN (to_grab, hw->samples - wpos); | |
329 | void *buf = advance (pa->pcm_buf, wpos); | |
330 | ||
ea9ebc2c MAL |
331 | if (qpa_simple_read (pa, buf, |
332 | chunk << hw->info.shift, &error) < 0) { | |
b8e59f18 | 333 | qpa_logerr (error, "pa_simple_read failed\n"); |
334 | return NULL; | |
335 | } | |
336 | ||
00e07679 | 337 | hw->conv (hw->conv_buf + wpos, buf, chunk); |
b8e59f18 | 338 | wpos = (wpos + chunk) % hw->samples; |
339 | to_grab -= chunk; | |
340 | } | |
341 | ||
470bcabd | 342 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 343 | return NULL; |
344 | } | |
345 | ||
346 | pa->wpos = wpos; | |
347 | pa->dead -= incr; | |
348 | pa->incr += incr; | |
349 | } | |
350 | ||
351 | exit: | |
470bcabd | 352 | audio_pt_unlock(&pa->pt, __func__); |
b8e59f18 | 353 | return NULL; |
354 | } | |
355 | ||
356 | static int qpa_run_in (HWVoiceIn *hw) | |
357 | { | |
358 | int live, incr, dead; | |
359 | PAVoiceIn *pa = (PAVoiceIn *) hw; | |
360 | ||
470bcabd | 361 | if (audio_pt_lock(&pa->pt, __func__)) { |
b8e59f18 | 362 | return 0; |
363 | } | |
364 | ||
365 | live = audio_pcm_hw_get_live_in (hw); | |
366 | dead = hw->samples - live; | |
367 | incr = audio_MIN (dead, pa->incr); | |
368 | pa->incr -= incr; | |
369 | pa->dead = dead - incr; | |
370 | hw->wpos = pa->wpos; | |
371 | if (pa->dead > 0) { | |
470bcabd | 372 | audio_pt_unlock_and_signal(&pa->pt, __func__); |
b8e59f18 | 373 | } |
374 | else { | |
470bcabd | 375 | audio_pt_unlock(&pa->pt, __func__); |
b8e59f18 | 376 | } |
377 | return incr; | |
378 | } | |
379 | ||
380 | static int qpa_read (SWVoiceIn *sw, void *buf, int len) | |
381 | { | |
382 | return audio_pcm_sw_read (sw, buf, len); | |
383 | } | |
384 | ||
85bc5852 | 385 | static pa_sample_format_t audfmt_to_pa (AudioFormat afmt, int endianness) |
b8e59f18 | 386 | { |
387 | int format; | |
388 | ||
389 | switch (afmt) { | |
85bc5852 KZ |
390 | case AUDIO_FORMAT_S8: |
391 | case AUDIO_FORMAT_U8: | |
b8e59f18 | 392 | format = PA_SAMPLE_U8; |
393 | break; | |
85bc5852 KZ |
394 | case AUDIO_FORMAT_S16: |
395 | case AUDIO_FORMAT_U16: | |
b8e59f18 | 396 | format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE; |
397 | break; | |
85bc5852 KZ |
398 | case AUDIO_FORMAT_S32: |
399 | case AUDIO_FORMAT_U32: | |
b8e59f18 | 400 | format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE; |
401 | break; | |
402 | default: | |
403 | dolog ("Internal logic error: Bad audio format %d\n", afmt); | |
404 | format = PA_SAMPLE_U8; | |
405 | break; | |
406 | } | |
407 | return format; | |
408 | } | |
409 | ||
85bc5852 | 410 | static AudioFormat pa_to_audfmt (pa_sample_format_t fmt, int *endianness) |
b8e59f18 | 411 | { |
412 | switch (fmt) { | |
413 | case PA_SAMPLE_U8: | |
85bc5852 | 414 | return AUDIO_FORMAT_U8; |
b8e59f18 | 415 | case PA_SAMPLE_S16BE: |
416 | *endianness = 1; | |
85bc5852 | 417 | return AUDIO_FORMAT_S16; |
b8e59f18 | 418 | case PA_SAMPLE_S16LE: |
419 | *endianness = 0; | |
85bc5852 | 420 | return AUDIO_FORMAT_S16; |
b8e59f18 | 421 | case PA_SAMPLE_S32BE: |
422 | *endianness = 1; | |
85bc5852 | 423 | return AUDIO_FORMAT_S32; |
b8e59f18 | 424 | case PA_SAMPLE_S32LE: |
425 | *endianness = 0; | |
85bc5852 | 426 | return AUDIO_FORMAT_S32; |
b8e59f18 | 427 | default: |
428 | dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt); | |
85bc5852 | 429 | return AUDIO_FORMAT_U8; |
b8e59f18 | 430 | } |
431 | } | |
432 | ||
ea9ebc2c MAL |
433 | static void context_state_cb (pa_context *c, void *userdata) |
434 | { | |
9a644c4b | 435 | paaudio *g = userdata; |
ea9ebc2c MAL |
436 | |
437 | switch (pa_context_get_state(c)) { | |
438 | case PA_CONTEXT_READY: | |
439 | case PA_CONTEXT_TERMINATED: | |
440 | case PA_CONTEXT_FAILED: | |
441 | pa_threaded_mainloop_signal (g->mainloop, 0); | |
442 | break; | |
443 | ||
444 | case PA_CONTEXT_UNCONNECTED: | |
445 | case PA_CONTEXT_CONNECTING: | |
446 | case PA_CONTEXT_AUTHORIZING: | |
447 | case PA_CONTEXT_SETTING_NAME: | |
448 | break; | |
449 | } | |
450 | } | |
451 | ||
452 | static void stream_state_cb (pa_stream *s, void * userdata) | |
453 | { | |
9a644c4b | 454 | paaudio *g = userdata; |
ea9ebc2c MAL |
455 | |
456 | switch (pa_stream_get_state (s)) { | |
457 | ||
458 | case PA_STREAM_READY: | |
459 | case PA_STREAM_FAILED: | |
460 | case PA_STREAM_TERMINATED: | |
461 | pa_threaded_mainloop_signal (g->mainloop, 0); | |
462 | break; | |
463 | ||
464 | case PA_STREAM_UNCONNECTED: | |
465 | case PA_STREAM_CREATING: | |
466 | break; | |
467 | } | |
468 | } | |
469 | ||
470 | static void stream_request_cb (pa_stream *s, size_t length, void *userdata) | |
471 | { | |
9a644c4b | 472 | paaudio *g = userdata; |
ea9ebc2c MAL |
473 | |
474 | pa_threaded_mainloop_signal (g->mainloop, 0); | |
475 | } | |
476 | ||
477 | static pa_stream *qpa_simple_new ( | |
9a644c4b | 478 | paaudio *g, |
ea9ebc2c MAL |
479 | const char *name, |
480 | pa_stream_direction_t dir, | |
481 | const char *dev, | |
ea9ebc2c MAL |
482 | const pa_sample_spec *ss, |
483 | const pa_channel_map *map, | |
484 | const pa_buffer_attr *attr, | |
485 | int *rerror) | |
486 | { | |
ea9ebc2c MAL |
487 | int r; |
488 | pa_stream *stream; | |
489 | ||
490 | pa_threaded_mainloop_lock (g->mainloop); | |
491 | ||
492 | stream = pa_stream_new (g->context, name, ss, map); | |
493 | if (!stream) { | |
494 | goto fail; | |
495 | } | |
496 | ||
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); | |
500 | ||
501 | if (dir == PA_STREAM_PLAYBACK) { | |
502 | r = pa_stream_connect_playback (stream, dev, attr, | |
503 | PA_STREAM_INTERPOLATE_TIMING | |
8f473dd1 | 504 | #ifdef PA_STREAM_ADJUST_LATENCY |
ea9ebc2c | 505 | |PA_STREAM_ADJUST_LATENCY |
8f473dd1 | 506 | #endif |
ea9ebc2c MAL |
507 | |PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL); |
508 | } else { | |
509 | r = pa_stream_connect_record (stream, dev, attr, | |
510 | PA_STREAM_INTERPOLATE_TIMING | |
8f473dd1 | 511 | #ifdef PA_STREAM_ADJUST_LATENCY |
ea9ebc2c | 512 | |PA_STREAM_ADJUST_LATENCY |
8f473dd1 | 513 | #endif |
ea9ebc2c MAL |
514 | |PA_STREAM_AUTO_TIMING_UPDATE); |
515 | } | |
516 | ||
517 | if (r < 0) { | |
518 | goto fail; | |
519 | } | |
520 | ||
521 | pa_threaded_mainloop_unlock (g->mainloop); | |
522 | ||
523 | return stream; | |
524 | ||
525 | fail: | |
526 | pa_threaded_mainloop_unlock (g->mainloop); | |
527 | ||
528 | if (stream) { | |
529 | pa_stream_unref (stream); | |
530 | } | |
531 | ||
d6c05bbf | 532 | *rerror = pa_context_errno (g->context); |
ea9ebc2c MAL |
533 | |
534 | return NULL; | |
535 | } | |
536 | ||
5706db1d KZ |
537 | static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as, |
538 | void *drv_opaque) | |
b8e59f18 | 539 | { |
540 | int error; | |
9a644c4b KZ |
541 | pa_sample_spec ss; |
542 | pa_buffer_attr ba; | |
1ea879e5 | 543 | struct audsettings obt_as = *as; |
b8e59f18 | 544 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
9a644c4b | 545 | paaudio *g = pa->g = drv_opaque; |
2c324b28 KZ |
546 | AudiodevPaOptions *popts = &g->dev->u.pa; |
547 | AudiodevPaPerDirectionOptions *ppdo = popts->out; | |
b8e59f18 | 548 | |
549 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
550 | ss.channels = as->nchannels; | |
551 | ss.rate = as->freq; | |
552 | ||
f6142777 MS |
553 | ba.tlength = pa_usec_to_bytes(ppdo->latency, &ss); |
554 | ba.minreq = -1; | |
e6d16fa4 GH |
555 | ba.maxlength = -1; |
556 | ba.prebuf = -1; | |
557 | ||
b8e59f18 | 558 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); |
559 | ||
ea9ebc2c | 560 | pa->stream = qpa_simple_new ( |
9a644c4b | 561 | g, |
b8e59f18 | 562 | "qemu", |
563 | PA_STREAM_PLAYBACK, | |
2c324b28 | 564 | ppdo->has_name ? ppdo->name : NULL, |
b8e59f18 | 565 | &ss, |
566 | NULL, /* channel map */ | |
e6d16fa4 | 567 | &ba, /* buffering attributes */ |
b8e59f18 | 568 | &error |
569 | ); | |
ea9ebc2c | 570 | if (!pa->stream) { |
b8e59f18 | 571 | qpa_logerr (error, "pa_simple_new for playback failed\n"); |
572 | goto fail1; | |
573 | } | |
574 | ||
575 | audio_pcm_init_info (&hw->info, &obt_as); | |
2c324b28 | 576 | hw->samples = pa->samples = audio_buffer_samples( |
baea032e MS |
577 | qapi_AudiodevPaPerDirectionOptions_base(ppdo), |
578 | &obt_as, ppdo->buffer_length); | |
470bcabd | 579 | pa->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift); |
6315633b | 580 | pa->rpos = hw->rpos; |
b8e59f18 | 581 | if (!pa->pcm_buf) { |
582 | dolog ("Could not allocate buffer (%d bytes)\n", | |
583 | hw->samples << hw->info.shift); | |
584 | goto fail2; | |
585 | } | |
586 | ||
470bcabd | 587 | if (audio_pt_init(&pa->pt, qpa_thread_out, hw, AUDIO_CAP, __func__)) { |
b8e59f18 | 588 | goto fail3; |
589 | } | |
590 | ||
591 | return 0; | |
592 | ||
593 | fail3: | |
7267c094 | 594 | g_free (pa->pcm_buf); |
b8e59f18 | 595 | pa->pcm_buf = NULL; |
596 | fail2: | |
ea9ebc2c MAL |
597 | if (pa->stream) { |
598 | pa_stream_unref (pa->stream); | |
599 | pa->stream = NULL; | |
600 | } | |
b8e59f18 | 601 | fail1: |
602 | return -1; | |
603 | } | |
604 | ||
5706db1d | 605 | static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
b8e59f18 | 606 | { |
607 | int error; | |
9a644c4b | 608 | pa_sample_spec ss; |
ade10301 | 609 | pa_buffer_attr ba; |
1ea879e5 | 610 | struct audsettings obt_as = *as; |
b8e59f18 | 611 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
9a644c4b | 612 | paaudio *g = pa->g = drv_opaque; |
2c324b28 KZ |
613 | AudiodevPaOptions *popts = &g->dev->u.pa; |
614 | AudiodevPaPerDirectionOptions *ppdo = popts->in; | |
b8e59f18 | 615 | |
616 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
617 | ss.channels = as->nchannels; | |
618 | ss.rate = as->freq; | |
619 | ||
ade10301 MS |
620 | ba.fragsize = pa_usec_to_bytes(ppdo->latency, &ss); |
621 | ba.maxlength = -1; | |
622 | ba.minreq = -1; | |
623 | ba.prebuf = -1; | |
624 | ||
b8e59f18 | 625 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); |
626 | ||
ea9ebc2c | 627 | pa->stream = qpa_simple_new ( |
9a644c4b | 628 | g, |
b8e59f18 | 629 | "qemu", |
630 | PA_STREAM_RECORD, | |
2c324b28 | 631 | ppdo->has_name ? ppdo->name : NULL, |
b8e59f18 | 632 | &ss, |
633 | NULL, /* channel map */ | |
ade10301 | 634 | &ba, /* buffering attributes */ |
b8e59f18 | 635 | &error |
636 | ); | |
ea9ebc2c | 637 | if (!pa->stream) { |
b8e59f18 | 638 | qpa_logerr (error, "pa_simple_new for capture failed\n"); |
639 | goto fail1; | |
640 | } | |
641 | ||
642 | audio_pcm_init_info (&hw->info, &obt_as); | |
2c324b28 | 643 | hw->samples = pa->samples = audio_buffer_samples( |
baea032e MS |
644 | qapi_AudiodevPaPerDirectionOptions_base(ppdo), |
645 | &obt_as, ppdo->buffer_length); | |
470bcabd | 646 | pa->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift); |
6315633b | 647 | pa->wpos = hw->wpos; |
b8e59f18 | 648 | if (!pa->pcm_buf) { |
649 | dolog ("Could not allocate buffer (%d bytes)\n", | |
650 | hw->samples << hw->info.shift); | |
651 | goto fail2; | |
652 | } | |
653 | ||
470bcabd | 654 | if (audio_pt_init(&pa->pt, qpa_thread_in, hw, AUDIO_CAP, __func__)) { |
b8e59f18 | 655 | goto fail3; |
656 | } | |
657 | ||
658 | return 0; | |
659 | ||
660 | fail3: | |
7267c094 | 661 | g_free (pa->pcm_buf); |
b8e59f18 | 662 | pa->pcm_buf = NULL; |
663 | fail2: | |
ea9ebc2c MAL |
664 | if (pa->stream) { |
665 | pa_stream_unref (pa->stream); | |
666 | pa->stream = NULL; | |
667 | } | |
b8e59f18 | 668 | fail1: |
669 | return -1; | |
670 | } | |
671 | ||
672 | static void qpa_fini_out (HWVoiceOut *hw) | |
673 | { | |
674 | void *ret; | |
675 | PAVoiceOut *pa = (PAVoiceOut *) hw; | |
676 | ||
470bcabd | 677 | audio_pt_lock(&pa->pt, __func__); |
b8e59f18 | 678 | pa->done = 1; |
470bcabd AF |
679 | audio_pt_unlock_and_signal(&pa->pt, __func__); |
680 | audio_pt_join(&pa->pt, &ret, __func__); | |
b8e59f18 | 681 | |
ea9ebc2c MAL |
682 | if (pa->stream) { |
683 | pa_stream_unref (pa->stream); | |
684 | pa->stream = NULL; | |
b8e59f18 | 685 | } |
686 | ||
470bcabd | 687 | audio_pt_fini(&pa->pt, __func__); |
7267c094 | 688 | g_free (pa->pcm_buf); |
b8e59f18 | 689 | pa->pcm_buf = NULL; |
690 | } | |
691 | ||
692 | static void qpa_fini_in (HWVoiceIn *hw) | |
693 | { | |
694 | void *ret; | |
695 | PAVoiceIn *pa = (PAVoiceIn *) hw; | |
696 | ||
470bcabd | 697 | audio_pt_lock(&pa->pt, __func__); |
b8e59f18 | 698 | pa->done = 1; |
470bcabd AF |
699 | audio_pt_unlock_and_signal(&pa->pt, __func__); |
700 | audio_pt_join(&pa->pt, &ret, __func__); | |
b8e59f18 | 701 | |
ea9ebc2c MAL |
702 | if (pa->stream) { |
703 | pa_stream_unref (pa->stream); | |
704 | pa->stream = NULL; | |
b8e59f18 | 705 | } |
706 | ||
470bcabd | 707 | audio_pt_fini(&pa->pt, __func__); |
7267c094 | 708 | g_free (pa->pcm_buf); |
b8e59f18 | 709 | pa->pcm_buf = NULL; |
710 | } | |
711 | ||
712 | static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...) | |
713 | { | |
6e7a7f3d MAL |
714 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
715 | pa_operation *op; | |
716 | pa_cvolume v; | |
9a644c4b | 717 | paaudio *g = pa->g; |
6e7a7f3d | 718 | |
8f473dd1 GH |
719 | #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ |
720 | pa_cvolume_init (&v); /* function is present in 0.9.13+ */ | |
721 | #endif | |
6e7a7f3d MAL |
722 | |
723 | switch (cmd) { | |
724 | case VOICE_VOLUME: | |
725 | { | |
726 | SWVoiceOut *sw; | |
727 | va_list ap; | |
728 | ||
729 | va_start (ap, cmd); | |
730 | sw = va_arg (ap, SWVoiceOut *); | |
731 | va_end (ap); | |
732 | ||
733 | v.channels = 2; | |
734 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX; | |
735 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX; | |
736 | ||
737 | pa_threaded_mainloop_lock (g->mainloop); | |
738 | ||
739 | op = pa_context_set_sink_input_volume (g->context, | |
740 | pa_stream_get_index (pa->stream), | |
741 | &v, NULL, NULL); | |
742 | if (!op) | |
743 | qpa_logerr (pa_context_errno (g->context), | |
744 | "set_sink_input_volume() failed\n"); | |
745 | else | |
746 | pa_operation_unref (op); | |
747 | ||
748 | op = pa_context_set_sink_input_mute (g->context, | |
749 | pa_stream_get_index (pa->stream), | |
750 | sw->vol.mute, NULL, NULL); | |
751 | if (!op) { | |
752 | qpa_logerr (pa_context_errno (g->context), | |
753 | "set_sink_input_mute() failed\n"); | |
754 | } else { | |
755 | pa_operation_unref (op); | |
756 | } | |
757 | ||
758 | pa_threaded_mainloop_unlock (g->mainloop); | |
759 | } | |
760 | } | |
b8e59f18 | 761 | return 0; |
762 | } | |
763 | ||
764 | static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
765 | { | |
6e7a7f3d MAL |
766 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
767 | pa_operation *op; | |
768 | pa_cvolume v; | |
9a644c4b | 769 | paaudio *g = pa->g; |
6e7a7f3d | 770 | |
8f473dd1 | 771 | #ifdef PA_CHECK_VERSION |
6e7a7f3d | 772 | pa_cvolume_init (&v); |
8f473dd1 | 773 | #endif |
6e7a7f3d MAL |
774 | |
775 | switch (cmd) { | |
776 | case VOICE_VOLUME: | |
777 | { | |
778 | SWVoiceIn *sw; | |
779 | va_list ap; | |
780 | ||
781 | va_start (ap, cmd); | |
782 | sw = va_arg (ap, SWVoiceIn *); | |
783 | va_end (ap); | |
784 | ||
785 | v.channels = 2; | |
786 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX; | |
787 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX; | |
788 | ||
789 | pa_threaded_mainloop_lock (g->mainloop); | |
790 | ||
e58ff62d PK |
791 | op = pa_context_set_source_output_volume (g->context, |
792 | pa_stream_get_index (pa->stream), | |
6e7a7f3d MAL |
793 | &v, NULL, NULL); |
794 | if (!op) { | |
795 | qpa_logerr (pa_context_errno (g->context), | |
e58ff62d | 796 | "set_source_output_volume() failed\n"); |
6e7a7f3d MAL |
797 | } else { |
798 | pa_operation_unref(op); | |
799 | } | |
800 | ||
e58ff62d | 801 | op = pa_context_set_source_output_mute (g->context, |
6e7a7f3d MAL |
802 | pa_stream_get_index (pa->stream), |
803 | sw->vol.mute, NULL, NULL); | |
804 | if (!op) { | |
805 | qpa_logerr (pa_context_errno (g->context), | |
e58ff62d | 806 | "set_source_output_mute() failed\n"); |
6e7a7f3d MAL |
807 | } else { |
808 | pa_operation_unref (op); | |
809 | } | |
810 | ||
811 | pa_threaded_mainloop_unlock (g->mainloop); | |
812 | } | |
813 | } | |
b8e59f18 | 814 | return 0; |
815 | } | |
816 | ||
baea032e MS |
817 | static int qpa_validate_per_direction_opts(Audiodev *dev, |
818 | AudiodevPaPerDirectionOptions *pdo) | |
819 | { | |
820 | if (!pdo->has_buffer_length) { | |
821 | pdo->has_buffer_length = true; | |
822 | pdo->buffer_length = 46440; | |
823 | } | |
f6142777 MS |
824 | if (!pdo->has_latency) { |
825 | pdo->has_latency = true; | |
826 | pdo->latency = 15000; | |
827 | } | |
baea032e MS |
828 | return 1; |
829 | } | |
830 | ||
71830221 | 831 | static void *qpa_audio_init(Audiodev *dev) |
b8e59f18 | 832 | { |
2c324b28 KZ |
833 | paaudio *g; |
834 | AudiodevPaOptions *popts = &dev->u.pa; | |
835 | const char *server; | |
836 | ||
837 | if (!popts->has_server) { | |
d175505b GH |
838 | char pidfile[64]; |
839 | char *runtime; | |
840 | struct stat st; | |
841 | ||
842 | runtime = getenv("XDG_RUNTIME_DIR"); | |
843 | if (!runtime) { | |
844 | return NULL; | |
845 | } | |
846 | snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime); | |
847 | if (stat(pidfile, &st) != 0) { | |
848 | return NULL; | |
849 | } | |
850 | } | |
851 | ||
2c324b28 KZ |
852 | assert(dev->driver == AUDIODEV_DRIVER_PA); |
853 | ||
854 | g = g_malloc(sizeof(paaudio)); | |
855 | server = popts->has_server ? popts->server : NULL; | |
856 | ||
baea032e MS |
857 | if (!qpa_validate_per_direction_opts(dev, popts->in)) { |
858 | goto fail; | |
859 | } | |
860 | if (!qpa_validate_per_direction_opts(dev, popts->out)) { | |
861 | goto fail; | |
862 | } | |
863 | ||
2c324b28 | 864 | g->dev = dev; |
49dd6d0d KZ |
865 | g->mainloop = NULL; |
866 | g->context = NULL; | |
ea9ebc2c MAL |
867 | |
868 | g->mainloop = pa_threaded_mainloop_new (); | |
869 | if (!g->mainloop) { | |
870 | goto fail; | |
871 | } | |
872 | ||
9a644c4b | 873 | g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), |
2c324b28 | 874 | server); |
ea9ebc2c MAL |
875 | if (!g->context) { |
876 | goto fail; | |
877 | } | |
878 | ||
879 | pa_context_set_state_callback (g->context, context_state_cb, g); | |
880 | ||
2c324b28 | 881 | if (pa_context_connect(g->context, server, 0, NULL) < 0) { |
ea9ebc2c MAL |
882 | qpa_logerr (pa_context_errno (g->context), |
883 | "pa_context_connect() failed\n"); | |
884 | goto fail; | |
885 | } | |
886 | ||
887 | pa_threaded_mainloop_lock (g->mainloop); | |
888 | ||
889 | if (pa_threaded_mainloop_start (g->mainloop) < 0) { | |
890 | goto unlock_and_fail; | |
891 | } | |
892 | ||
893 | for (;;) { | |
894 | pa_context_state_t state; | |
895 | ||
896 | state = pa_context_get_state (g->context); | |
897 | ||
898 | if (state == PA_CONTEXT_READY) { | |
899 | break; | |
900 | } | |
901 | ||
902 | if (!PA_CONTEXT_IS_GOOD (state)) { | |
903 | qpa_logerr (pa_context_errno (g->context), | |
904 | "Wrong context state\n"); | |
905 | goto unlock_and_fail; | |
906 | } | |
907 | ||
908 | /* Wait until the context is ready */ | |
909 | pa_threaded_mainloop_wait (g->mainloop); | |
910 | } | |
911 | ||
912 | pa_threaded_mainloop_unlock (g->mainloop); | |
913 | ||
9a644c4b | 914 | return g; |
ea9ebc2c MAL |
915 | |
916 | unlock_and_fail: | |
917 | pa_threaded_mainloop_unlock (g->mainloop); | |
918 | fail: | |
919 | AUD_log (AUDIO_CAP, "Failed to initialize PA context"); | |
49dd6d0d | 920 | qpa_audio_fini(g); |
ea9ebc2c | 921 | return NULL; |
b8e59f18 | 922 | } |
923 | ||
924 | static void qpa_audio_fini (void *opaque) | |
925 | { | |
ea9ebc2c MAL |
926 | paaudio *g = opaque; |
927 | ||
928 | if (g->mainloop) { | |
929 | pa_threaded_mainloop_stop (g->mainloop); | |
930 | } | |
931 | ||
932 | if (g->context) { | |
933 | pa_context_disconnect (g->context); | |
934 | pa_context_unref (g->context); | |
ea9ebc2c MAL |
935 | } |
936 | ||
937 | if (g->mainloop) { | |
938 | pa_threaded_mainloop_free (g->mainloop); | |
939 | } | |
940 | ||
9a644c4b | 941 | g_free(g); |
b8e59f18 | 942 | } |
943 | ||
35f4b58c | 944 | static struct audio_pcm_ops qpa_pcm_ops = { |
1dd3e4d1 JQ |
945 | .init_out = qpa_init_out, |
946 | .fini_out = qpa_fini_out, | |
947 | .run_out = qpa_run_out, | |
948 | .write = qpa_write, | |
949 | .ctl_out = qpa_ctl_out, | |
950 | ||
951 | .init_in = qpa_init_in, | |
952 | .fini_in = qpa_fini_in, | |
953 | .run_in = qpa_run_in, | |
954 | .read = qpa_read, | |
955 | .ctl_in = qpa_ctl_in | |
b8e59f18 | 956 | }; |
957 | ||
d3893a39 | 958 | static struct audio_driver pa_audio_driver = { |
bee37f32 JQ |
959 | .name = "pa", |
960 | .descr = "http://www.pulseaudio.org/", | |
bee37f32 JQ |
961 | .init = qpa_audio_init, |
962 | .fini = qpa_audio_fini, | |
963 | .pcm_ops = &qpa_pcm_ops, | |
1a4ea1e3 | 964 | .can_be_default = 1, |
bee37f32 JQ |
965 | .max_voices_out = INT_MAX, |
966 | .max_voices_in = INT_MAX, | |
967 | .voice_size_out = sizeof (PAVoiceOut), | |
6e7a7f3d MAL |
968 | .voice_size_in = sizeof (PAVoiceIn), |
969 | .ctl_caps = VOICE_VOLUME_CAP | |
b8e59f18 | 970 | }; |
d3893a39 GH |
971 | |
972 | static void register_audio_pa(void) | |
973 | { | |
974 | audio_driver_register(&pa_audio_driver); | |
975 | } | |
976 | type_init(register_audio_pa); |