]>
Commit | Line | Data |
---|---|---|
b8e59f18 | 1 | /* public domain */ |
2 | #include "qemu-common.h" | |
3 | #include "audio.h" | |
4 | ||
ea9ebc2c | 5 | #include <pulse/pulseaudio.h> |
b8e59f18 | 6 | |
7 | #define AUDIO_CAP "pulseaudio" | |
8 | #include "audio_int.h" | |
9 | #include "audio_pt_int.h" | |
10 | ||
9a644c4b KZ |
11 | typedef struct { |
12 | int samples; | |
13 | char *server; | |
14 | char *sink; | |
15 | char *source; | |
16 | } PAConf; | |
17 | ||
18 | typedef struct { | |
19 | PAConf conf; | |
20 | pa_threaded_mainloop *mainloop; | |
21 | pa_context *context; | |
22 | } paaudio; | |
23 | ||
b8e59f18 | 24 | typedef struct { |
25 | HWVoiceOut hw; | |
26 | int done; | |
27 | int live; | |
28 | int decr; | |
29 | int rpos; | |
ea9ebc2c | 30 | pa_stream *stream; |
b8e59f18 | 31 | void *pcm_buf; |
32 | struct audio_pt pt; | |
9a644c4b | 33 | paaudio *g; |
b8e59f18 | 34 | } PAVoiceOut; |
35 | ||
36 | typedef struct { | |
37 | HWVoiceIn hw; | |
38 | int done; | |
39 | int dead; | |
40 | int incr; | |
41 | int wpos; | |
ea9ebc2c | 42 | pa_stream *stream; |
b8e59f18 | 43 | void *pcm_buf; |
44 | struct audio_pt pt; | |
ea9ebc2c MAL |
45 | const void *read_data; |
46 | size_t read_index, read_length; | |
9a644c4b | 47 | paaudio *g; |
b8e59f18 | 48 | } PAVoiceIn; |
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 | } \ | |
89 | } while (0); | |
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 | } \ | |
107 | } while (0); | |
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 | |
206 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
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 | ||
222 | if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) { | |
223 | goto exit; | |
224 | } | |
225 | } | |
226 | ||
9a644c4b | 227 | decr = to_mix = audio_MIN (pa->live, pa->g->conf.samples >> 2); |
6315633b | 228 | rpos = pa->rpos; |
b8e59f18 | 229 | |
230 | if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { | |
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 | ||
251 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | pa->rpos = rpos; | |
6315633b | 256 | pa->live -= decr; |
b8e59f18 | 257 | pa->decr += decr; |
258 | } | |
259 | ||
260 | exit: | |
261 | audio_pt_unlock (&pa->pt, AUDIO_FUNC); | |
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 | ||
270 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
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) { | |
279 | audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC); | |
280 | } | |
281 | else { | |
282 | audio_pt_unlock (&pa->pt, AUDIO_FUNC); | |
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 | |
298 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
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 | ||
314 | if (audio_pt_wait (&pa->pt, AUDIO_FUNC)) { | |
315 | goto exit; | |
316 | } | |
317 | } | |
318 | ||
9a644c4b | 319 | incr = to_grab = audio_MIN (pa->dead, pa->g->conf.samples >> 2); |
6315633b | 320 | wpos = pa->wpos; |
b8e59f18 | 321 | |
322 | if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { | |
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 | ||
342 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | pa->wpos = wpos; | |
347 | pa->dead -= incr; | |
348 | pa->incr += incr; | |
349 | } | |
350 | ||
351 | exit: | |
352 | audio_pt_unlock (&pa->pt, AUDIO_FUNC); | |
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 | ||
361 | if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { | |
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) { | |
372 | audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC); | |
373 | } | |
374 | else { | |
375 | audio_pt_unlock (&pa->pt, AUDIO_FUNC); | |
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 | ||
385 | static pa_sample_format_t audfmt_to_pa (audfmt_e afmt, int endianness) | |
386 | { | |
387 | int format; | |
388 | ||
389 | switch (afmt) { | |
390 | case AUD_FMT_S8: | |
391 | case AUD_FMT_U8: | |
392 | format = PA_SAMPLE_U8; | |
393 | break; | |
394 | case AUD_FMT_S16: | |
395 | case AUD_FMT_U16: | |
396 | format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE; | |
397 | break; | |
398 | case AUD_FMT_S32: | |
399 | case AUD_FMT_U32: | |
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 | ||
410 | static audfmt_e pa_to_audfmt (pa_sample_format_t fmt, int *endianness) | |
411 | { | |
412 | switch (fmt) { | |
413 | case PA_SAMPLE_U8: | |
414 | return AUD_FMT_U8; | |
415 | case PA_SAMPLE_S16BE: | |
416 | *endianness = 1; | |
417 | return AUD_FMT_S16; | |
418 | case PA_SAMPLE_S16LE: | |
419 | *endianness = 0; | |
420 | return AUD_FMT_S16; | |
421 | case PA_SAMPLE_S32BE: | |
422 | *endianness = 1; | |
423 | return AUD_FMT_S32; | |
424 | case PA_SAMPLE_S32LE: | |
425 | *endianness = 0; | |
426 | return AUD_FMT_S32; | |
427 | default: | |
428 | dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt); | |
429 | return AUD_FMT_U8; | |
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; |
b8e59f18 | 546 | |
547 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
548 | ss.channels = as->nchannels; | |
549 | ss.rate = as->freq; | |
550 | ||
e6d16fa4 | 551 | /* |
0e8ae611 GH |
552 | * qemu audio tick runs at 100 Hz (by default), so processing |
553 | * data chunks worth 10 ms of sound should be a good fit. | |
e6d16fa4 | 554 | */ |
0e8ae611 GH |
555 | ba.tlength = pa_usec_to_bytes (10 * 1000, &ss); |
556 | ba.minreq = pa_usec_to_bytes (5 * 1000, &ss); | |
e6d16fa4 GH |
557 | ba.maxlength = -1; |
558 | ba.prebuf = -1; | |
559 | ||
b8e59f18 | 560 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); |
561 | ||
ea9ebc2c | 562 | pa->stream = qpa_simple_new ( |
9a644c4b | 563 | g, |
b8e59f18 | 564 | "qemu", |
565 | PA_STREAM_PLAYBACK, | |
9a644c4b | 566 | g->conf.sink, |
b8e59f18 | 567 | &ss, |
568 | NULL, /* channel map */ | |
e6d16fa4 | 569 | &ba, /* buffering attributes */ |
b8e59f18 | 570 | &error |
571 | ); | |
ea9ebc2c | 572 | if (!pa->stream) { |
b8e59f18 | 573 | qpa_logerr (error, "pa_simple_new for playback failed\n"); |
574 | goto fail1; | |
575 | } | |
576 | ||
577 | audio_pcm_init_info (&hw->info, &obt_as); | |
9a644c4b | 578 | hw->samples = g->conf.samples; |
b8e59f18 | 579 | pa->pcm_buf = audio_calloc (AUDIO_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 | ||
587 | if (audio_pt_init (&pa->pt, qpa_thread_out, hw, AUDIO_CAP, AUDIO_FUNC)) { | |
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; |
1ea879e5 | 609 | struct audsettings obt_as = *as; |
b8e59f18 | 610 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
9a644c4b | 611 | paaudio *g = pa->g = drv_opaque; |
b8e59f18 | 612 | |
613 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
614 | ss.channels = as->nchannels; | |
615 | ss.rate = as->freq; | |
616 | ||
617 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); | |
618 | ||
ea9ebc2c | 619 | pa->stream = qpa_simple_new ( |
9a644c4b | 620 | g, |
b8e59f18 | 621 | "qemu", |
622 | PA_STREAM_RECORD, | |
9a644c4b | 623 | g->conf.source, |
b8e59f18 | 624 | &ss, |
625 | NULL, /* channel map */ | |
626 | NULL, /* buffering attributes */ | |
627 | &error | |
628 | ); | |
ea9ebc2c | 629 | if (!pa->stream) { |
b8e59f18 | 630 | qpa_logerr (error, "pa_simple_new for capture failed\n"); |
631 | goto fail1; | |
632 | } | |
633 | ||
634 | audio_pcm_init_info (&hw->info, &obt_as); | |
9a644c4b | 635 | hw->samples = g->conf.samples; |
b8e59f18 | 636 | pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); |
6315633b | 637 | pa->wpos = hw->wpos; |
b8e59f18 | 638 | if (!pa->pcm_buf) { |
639 | dolog ("Could not allocate buffer (%d bytes)\n", | |
640 | hw->samples << hw->info.shift); | |
641 | goto fail2; | |
642 | } | |
643 | ||
644 | if (audio_pt_init (&pa->pt, qpa_thread_in, hw, AUDIO_CAP, AUDIO_FUNC)) { | |
645 | goto fail3; | |
646 | } | |
647 | ||
648 | return 0; | |
649 | ||
650 | fail3: | |
7267c094 | 651 | g_free (pa->pcm_buf); |
b8e59f18 | 652 | pa->pcm_buf = NULL; |
653 | fail2: | |
ea9ebc2c MAL |
654 | if (pa->stream) { |
655 | pa_stream_unref (pa->stream); | |
656 | pa->stream = NULL; | |
657 | } | |
b8e59f18 | 658 | fail1: |
659 | return -1; | |
660 | } | |
661 | ||
662 | static void qpa_fini_out (HWVoiceOut *hw) | |
663 | { | |
664 | void *ret; | |
665 | PAVoiceOut *pa = (PAVoiceOut *) hw; | |
666 | ||
667 | audio_pt_lock (&pa->pt, AUDIO_FUNC); | |
668 | pa->done = 1; | |
669 | audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC); | |
670 | audio_pt_join (&pa->pt, &ret, AUDIO_FUNC); | |
671 | ||
ea9ebc2c MAL |
672 | if (pa->stream) { |
673 | pa_stream_unref (pa->stream); | |
674 | pa->stream = NULL; | |
b8e59f18 | 675 | } |
676 | ||
677 | audio_pt_fini (&pa->pt, AUDIO_FUNC); | |
7267c094 | 678 | g_free (pa->pcm_buf); |
b8e59f18 | 679 | pa->pcm_buf = NULL; |
680 | } | |
681 | ||
682 | static void qpa_fini_in (HWVoiceIn *hw) | |
683 | { | |
684 | void *ret; | |
685 | PAVoiceIn *pa = (PAVoiceIn *) hw; | |
686 | ||
687 | audio_pt_lock (&pa->pt, AUDIO_FUNC); | |
688 | pa->done = 1; | |
689 | audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC); | |
690 | audio_pt_join (&pa->pt, &ret, AUDIO_FUNC); | |
691 | ||
ea9ebc2c MAL |
692 | if (pa->stream) { |
693 | pa_stream_unref (pa->stream); | |
694 | pa->stream = NULL; | |
b8e59f18 | 695 | } |
696 | ||
697 | audio_pt_fini (&pa->pt, AUDIO_FUNC); | |
7267c094 | 698 | g_free (pa->pcm_buf); |
b8e59f18 | 699 | pa->pcm_buf = NULL; |
700 | } | |
701 | ||
702 | static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...) | |
703 | { | |
6e7a7f3d MAL |
704 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
705 | pa_operation *op; | |
706 | pa_cvolume v; | |
9a644c4b | 707 | paaudio *g = pa->g; |
6e7a7f3d | 708 | |
8f473dd1 GH |
709 | #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ |
710 | pa_cvolume_init (&v); /* function is present in 0.9.13+ */ | |
711 | #endif | |
6e7a7f3d MAL |
712 | |
713 | switch (cmd) { | |
714 | case VOICE_VOLUME: | |
715 | { | |
716 | SWVoiceOut *sw; | |
717 | va_list ap; | |
718 | ||
719 | va_start (ap, cmd); | |
720 | sw = va_arg (ap, SWVoiceOut *); | |
721 | va_end (ap); | |
722 | ||
723 | v.channels = 2; | |
724 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX; | |
725 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX; | |
726 | ||
727 | pa_threaded_mainloop_lock (g->mainloop); | |
728 | ||
729 | op = pa_context_set_sink_input_volume (g->context, | |
730 | pa_stream_get_index (pa->stream), | |
731 | &v, NULL, NULL); | |
732 | if (!op) | |
733 | qpa_logerr (pa_context_errno (g->context), | |
734 | "set_sink_input_volume() failed\n"); | |
735 | else | |
736 | pa_operation_unref (op); | |
737 | ||
738 | op = pa_context_set_sink_input_mute (g->context, | |
739 | pa_stream_get_index (pa->stream), | |
740 | sw->vol.mute, NULL, NULL); | |
741 | if (!op) { | |
742 | qpa_logerr (pa_context_errno (g->context), | |
743 | "set_sink_input_mute() failed\n"); | |
744 | } else { | |
745 | pa_operation_unref (op); | |
746 | } | |
747 | ||
748 | pa_threaded_mainloop_unlock (g->mainloop); | |
749 | } | |
750 | } | |
b8e59f18 | 751 | return 0; |
752 | } | |
753 | ||
754 | static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
755 | { | |
6e7a7f3d MAL |
756 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
757 | pa_operation *op; | |
758 | pa_cvolume v; | |
9a644c4b | 759 | paaudio *g = pa->g; |
6e7a7f3d | 760 | |
8f473dd1 | 761 | #ifdef PA_CHECK_VERSION |
6e7a7f3d | 762 | pa_cvolume_init (&v); |
8f473dd1 | 763 | #endif |
6e7a7f3d MAL |
764 | |
765 | switch (cmd) { | |
766 | case VOICE_VOLUME: | |
767 | { | |
768 | SWVoiceIn *sw; | |
769 | va_list ap; | |
770 | ||
771 | va_start (ap, cmd); | |
772 | sw = va_arg (ap, SWVoiceIn *); | |
773 | va_end (ap); | |
774 | ||
775 | v.channels = 2; | |
776 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX; | |
777 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX; | |
778 | ||
779 | pa_threaded_mainloop_lock (g->mainloop); | |
780 | ||
781 | /* FIXME: use the upcoming "set_source_output_{volume,mute}" */ | |
782 | op = pa_context_set_source_volume_by_index (g->context, | |
783 | pa_stream_get_device_index (pa->stream), | |
784 | &v, NULL, NULL); | |
785 | if (!op) { | |
786 | qpa_logerr (pa_context_errno (g->context), | |
787 | "set_source_volume() failed\n"); | |
788 | } else { | |
789 | pa_operation_unref(op); | |
790 | } | |
791 | ||
792 | op = pa_context_set_source_mute_by_index (g->context, | |
793 | pa_stream_get_index (pa->stream), | |
794 | sw->vol.mute, NULL, NULL); | |
795 | if (!op) { | |
796 | qpa_logerr (pa_context_errno (g->context), | |
797 | "set_source_mute() failed\n"); | |
798 | } else { | |
799 | pa_operation_unref (op); | |
800 | } | |
801 | ||
802 | pa_threaded_mainloop_unlock (g->mainloop); | |
803 | } | |
804 | } | |
b8e59f18 | 805 | return 0; |
806 | } | |
807 | ||
808 | /* common */ | |
9a644c4b KZ |
809 | static PAConf glob_conf = { |
810 | .samples = 4096, | |
811 | }; | |
812 | ||
b8e59f18 | 813 | static void *qpa_audio_init (void) |
814 | { | |
9a644c4b KZ |
815 | paaudio *g = g_malloc(sizeof(paaudio)); |
816 | g->conf = glob_conf; | |
ea9ebc2c MAL |
817 | |
818 | g->mainloop = pa_threaded_mainloop_new (); | |
819 | if (!g->mainloop) { | |
820 | goto fail; | |
821 | } | |
822 | ||
9a644c4b KZ |
823 | g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), |
824 | g->conf.server); | |
ea9ebc2c MAL |
825 | if (!g->context) { |
826 | goto fail; | |
827 | } | |
828 | ||
829 | pa_context_set_state_callback (g->context, context_state_cb, g); | |
830 | ||
9a644c4b | 831 | if (pa_context_connect (g->context, g->conf.server, 0, NULL) < 0) { |
ea9ebc2c MAL |
832 | qpa_logerr (pa_context_errno (g->context), |
833 | "pa_context_connect() failed\n"); | |
834 | goto fail; | |
835 | } | |
836 | ||
837 | pa_threaded_mainloop_lock (g->mainloop); | |
838 | ||
839 | if (pa_threaded_mainloop_start (g->mainloop) < 0) { | |
840 | goto unlock_and_fail; | |
841 | } | |
842 | ||
843 | for (;;) { | |
844 | pa_context_state_t state; | |
845 | ||
846 | state = pa_context_get_state (g->context); | |
847 | ||
848 | if (state == PA_CONTEXT_READY) { | |
849 | break; | |
850 | } | |
851 | ||
852 | if (!PA_CONTEXT_IS_GOOD (state)) { | |
853 | qpa_logerr (pa_context_errno (g->context), | |
854 | "Wrong context state\n"); | |
855 | goto unlock_and_fail; | |
856 | } | |
857 | ||
858 | /* Wait until the context is ready */ | |
859 | pa_threaded_mainloop_wait (g->mainloop); | |
860 | } | |
861 | ||
862 | pa_threaded_mainloop_unlock (g->mainloop); | |
863 | ||
9a644c4b | 864 | return g; |
ea9ebc2c MAL |
865 | |
866 | unlock_and_fail: | |
867 | pa_threaded_mainloop_unlock (g->mainloop); | |
868 | fail: | |
869 | AUD_log (AUDIO_CAP, "Failed to initialize PA context"); | |
9a644c4b | 870 | g_free(g); |
ea9ebc2c | 871 | return NULL; |
b8e59f18 | 872 | } |
873 | ||
874 | static void qpa_audio_fini (void *opaque) | |
875 | { | |
ea9ebc2c MAL |
876 | paaudio *g = opaque; |
877 | ||
878 | if (g->mainloop) { | |
879 | pa_threaded_mainloop_stop (g->mainloop); | |
880 | } | |
881 | ||
882 | if (g->context) { | |
883 | pa_context_disconnect (g->context); | |
884 | pa_context_unref (g->context); | |
ea9ebc2c MAL |
885 | } |
886 | ||
887 | if (g->mainloop) { | |
888 | pa_threaded_mainloop_free (g->mainloop); | |
889 | } | |
890 | ||
9a644c4b | 891 | g_free(g); |
b8e59f18 | 892 | } |
893 | ||
894 | struct audio_option qpa_options[] = { | |
98f9f48c | 895 | { |
896 | .name = "SAMPLES", | |
897 | .tag = AUD_OPT_INT, | |
9a644c4b | 898 | .valp = &glob_conf.samples, |
98f9f48c | 899 | .descr = "buffer size in samples" |
900 | }, | |
98f9f48c | 901 | { |
902 | .name = "SERVER", | |
903 | .tag = AUD_OPT_STR, | |
9a644c4b | 904 | .valp = &glob_conf.server, |
98f9f48c | 905 | .descr = "server address" |
906 | }, | |
907 | { | |
908 | .name = "SINK", | |
909 | .tag = AUD_OPT_STR, | |
9a644c4b | 910 | .valp = &glob_conf.sink, |
98f9f48c | 911 | .descr = "sink device name" |
912 | }, | |
913 | { | |
914 | .name = "SOURCE", | |
915 | .tag = AUD_OPT_STR, | |
9a644c4b | 916 | .valp = &glob_conf.source, |
98f9f48c | 917 | .descr = "source device name" |
918 | }, | |
2700efa3 | 919 | { /* End of list */ } |
b8e59f18 | 920 | }; |
921 | ||
35f4b58c | 922 | static struct audio_pcm_ops qpa_pcm_ops = { |
1dd3e4d1 JQ |
923 | .init_out = qpa_init_out, |
924 | .fini_out = qpa_fini_out, | |
925 | .run_out = qpa_run_out, | |
926 | .write = qpa_write, | |
927 | .ctl_out = qpa_ctl_out, | |
928 | ||
929 | .init_in = qpa_init_in, | |
930 | .fini_in = qpa_fini_in, | |
931 | .run_in = qpa_run_in, | |
932 | .read = qpa_read, | |
933 | .ctl_in = qpa_ctl_in | |
b8e59f18 | 934 | }; |
935 | ||
936 | struct audio_driver pa_audio_driver = { | |
bee37f32 JQ |
937 | .name = "pa", |
938 | .descr = "http://www.pulseaudio.org/", | |
939 | .options = qpa_options, | |
940 | .init = qpa_audio_init, | |
941 | .fini = qpa_audio_fini, | |
942 | .pcm_ops = &qpa_pcm_ops, | |
1a4ea1e3 | 943 | .can_be_default = 1, |
bee37f32 JQ |
944 | .max_voices_out = INT_MAX, |
945 | .max_voices_in = INT_MAX, | |
946 | .voice_size_out = sizeof (PAVoiceOut), | |
6e7a7f3d MAL |
947 | .voice_size_in = sizeof (PAVoiceIn), |
948 | .ctl_caps = VOICE_VOLUME_CAP | |
b8e59f18 | 949 | }; |