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