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