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