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