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