]>
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 | |
49ddd7e1 | 102 | static size_t qpa_read(HWVoiceIn *hw, void *data, size_t length) |
ea9ebc2c | 103 | { |
49ddd7e1 | 104 | PAVoiceIn *p = (PAVoiceIn *) hw; |
9d34e6d8 | 105 | PAConnection *c = p->g->conn; |
49ddd7e1 KZ |
106 | size_t l; |
107 | int r; | |
ea9ebc2c | 108 | |
9d34e6d8 | 109 | pa_threaded_mainloop_lock(c->mainloop); |
ea9ebc2c | 110 | |
49ddd7e1 KZ |
111 | CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, |
112 | "pa_threaded_mainloop_lock failed\n"); | |
ea9ebc2c | 113 | |
49ddd7e1 KZ |
114 | if (!p->read_length) { |
115 | r = pa_stream_peek(p->stream, &p->read_data, &p->read_length); | |
116 | CHECK_SUCCESS_GOTO(c, r == 0, unlock_and_fail, | |
117 | "pa_stream_peek failed\n"); | |
118 | } | |
ea9ebc2c | 119 | |
49ddd7e1 KZ |
120 | l = MIN(p->read_length, length); |
121 | memcpy(data, p->read_data, l); | |
ea9ebc2c | 122 | |
49ddd7e1 KZ |
123 | p->read_data += l; |
124 | p->read_length -= l; | |
ea9ebc2c | 125 | |
49ddd7e1 KZ |
126 | if (!p->read_length) { |
127 | r = pa_stream_drop(p->stream); | |
128 | CHECK_SUCCESS_GOTO(c, r == 0, unlock_and_fail, | |
129 | "pa_stream_drop failed\n"); | |
ea9ebc2c MAL |
130 | } |
131 | ||
9d34e6d8 | 132 | pa_threaded_mainloop_unlock(c->mainloop); |
49ddd7e1 | 133 | return l; |
ea9ebc2c MAL |
134 | |
135 | unlock_and_fail: | |
9d34e6d8 | 136 | pa_threaded_mainloop_unlock(c->mainloop); |
49ddd7e1 | 137 | return 0; |
ea9ebc2c MAL |
138 | } |
139 | ||
49ddd7e1 | 140 | static size_t qpa_write(HWVoiceOut *hw, void *data, size_t length) |
ea9ebc2c | 141 | { |
49ddd7e1 | 142 | PAVoiceOut *p = (PAVoiceOut *) hw; |
9d34e6d8 | 143 | PAConnection *c = p->g->conn; |
49ddd7e1 KZ |
144 | size_t l; |
145 | int r; | |
ea9ebc2c | 146 | |
9d34e6d8 | 147 | pa_threaded_mainloop_lock(c->mainloop); |
ea9ebc2c | 148 | |
49ddd7e1 KZ |
149 | CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, |
150 | "pa_threaded_mainloop_lock failed\n"); | |
ea9ebc2c | 151 | |
49ddd7e1 | 152 | l = pa_stream_writable_size(p->stream); |
ea9ebc2c | 153 | |
49ddd7e1 KZ |
154 | CHECK_SUCCESS_GOTO(c, l != (size_t) -1, unlock_and_fail, |
155 | "pa_stream_writable_size failed\n"); | |
ea9ebc2c | 156 | |
49ddd7e1 KZ |
157 | if (l > length) { |
158 | l = length; | |
ea9ebc2c MAL |
159 | } |
160 | ||
49ddd7e1 KZ |
161 | r = pa_stream_write(p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE); |
162 | CHECK_SUCCESS_GOTO(c, r >= 0, unlock_and_fail, "pa_stream_write failed\n"); | |
163 | ||
9d34e6d8 | 164 | pa_threaded_mainloop_unlock(c->mainloop); |
49ddd7e1 | 165 | return l; |
ea9ebc2c MAL |
166 | |
167 | unlock_and_fail: | |
9d34e6d8 | 168 | pa_threaded_mainloop_unlock(c->mainloop); |
49ddd7e1 | 169 | return 0; |
b8e59f18 | 170 | } |
171 | ||
85bc5852 | 172 | static pa_sample_format_t audfmt_to_pa (AudioFormat afmt, int endianness) |
b8e59f18 | 173 | { |
174 | int format; | |
175 | ||
176 | switch (afmt) { | |
85bc5852 KZ |
177 | case AUDIO_FORMAT_S8: |
178 | case AUDIO_FORMAT_U8: | |
b8e59f18 | 179 | format = PA_SAMPLE_U8; |
180 | break; | |
85bc5852 KZ |
181 | case AUDIO_FORMAT_S16: |
182 | case AUDIO_FORMAT_U16: | |
b8e59f18 | 183 | format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE; |
184 | break; | |
85bc5852 KZ |
185 | case AUDIO_FORMAT_S32: |
186 | case AUDIO_FORMAT_U32: | |
b8e59f18 | 187 | format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE; |
188 | break; | |
189 | default: | |
190 | dolog ("Internal logic error: Bad audio format %d\n", afmt); | |
191 | format = PA_SAMPLE_U8; | |
192 | break; | |
193 | } | |
194 | return format; | |
195 | } | |
196 | ||
85bc5852 | 197 | static AudioFormat pa_to_audfmt (pa_sample_format_t fmt, int *endianness) |
b8e59f18 | 198 | { |
199 | switch (fmt) { | |
200 | case PA_SAMPLE_U8: | |
85bc5852 | 201 | return AUDIO_FORMAT_U8; |
b8e59f18 | 202 | case PA_SAMPLE_S16BE: |
203 | *endianness = 1; | |
85bc5852 | 204 | return AUDIO_FORMAT_S16; |
b8e59f18 | 205 | case PA_SAMPLE_S16LE: |
206 | *endianness = 0; | |
85bc5852 | 207 | return AUDIO_FORMAT_S16; |
b8e59f18 | 208 | case PA_SAMPLE_S32BE: |
209 | *endianness = 1; | |
85bc5852 | 210 | return AUDIO_FORMAT_S32; |
b8e59f18 | 211 | case PA_SAMPLE_S32LE: |
212 | *endianness = 0; | |
85bc5852 | 213 | return AUDIO_FORMAT_S32; |
b8e59f18 | 214 | default: |
215 | dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt); | |
85bc5852 | 216 | return AUDIO_FORMAT_U8; |
b8e59f18 | 217 | } |
218 | } | |
219 | ||
ea9ebc2c MAL |
220 | static void context_state_cb (pa_context *c, void *userdata) |
221 | { | |
9d34e6d8 | 222 | PAConnection *conn = userdata; |
ea9ebc2c MAL |
223 | |
224 | switch (pa_context_get_state(c)) { | |
225 | case PA_CONTEXT_READY: | |
226 | case PA_CONTEXT_TERMINATED: | |
227 | case PA_CONTEXT_FAILED: | |
9d34e6d8 | 228 | pa_threaded_mainloop_signal(conn->mainloop, 0); |
ea9ebc2c MAL |
229 | break; |
230 | ||
231 | case PA_CONTEXT_UNCONNECTED: | |
232 | case PA_CONTEXT_CONNECTING: | |
233 | case PA_CONTEXT_AUTHORIZING: | |
234 | case PA_CONTEXT_SETTING_NAME: | |
235 | break; | |
236 | } | |
237 | } | |
238 | ||
239 | static void stream_state_cb (pa_stream *s, void * userdata) | |
240 | { | |
9d34e6d8 | 241 | PAConnection *c = userdata; |
ea9ebc2c MAL |
242 | |
243 | switch (pa_stream_get_state (s)) { | |
244 | ||
245 | case PA_STREAM_READY: | |
246 | case PA_STREAM_FAILED: | |
247 | case PA_STREAM_TERMINATED: | |
9d34e6d8 | 248 | pa_threaded_mainloop_signal(c->mainloop, 0); |
ea9ebc2c MAL |
249 | break; |
250 | ||
251 | case PA_STREAM_UNCONNECTED: | |
252 | case PA_STREAM_CREATING: | |
253 | break; | |
254 | } | |
255 | } | |
256 | ||
ea9ebc2c | 257 | static pa_stream *qpa_simple_new ( |
9d34e6d8 | 258 | PAConnection *c, |
ea9ebc2c MAL |
259 | const char *name, |
260 | pa_stream_direction_t dir, | |
261 | const char *dev, | |
ea9ebc2c MAL |
262 | const pa_sample_spec *ss, |
263 | const pa_channel_map *map, | |
264 | const pa_buffer_attr *attr, | |
265 | int *rerror) | |
266 | { | |
ea9ebc2c MAL |
267 | int r; |
268 | pa_stream *stream; | |
9d34e6d8 | 269 | pa_stream_flags_t flags; |
ea9ebc2c | 270 | |
9d34e6d8 | 271 | pa_threaded_mainloop_lock(c->mainloop); |
ea9ebc2c | 272 | |
9d34e6d8 | 273 | stream = pa_stream_new(c->context, name, ss, map); |
ea9ebc2c MAL |
274 | if (!stream) { |
275 | goto fail; | |
276 | } | |
277 | ||
9d34e6d8 | 278 | pa_stream_set_state_callback(stream, stream_state_cb, c); |
ea9ebc2c | 279 | |
9d34e6d8 KZ |
280 | flags = |
281 | PA_STREAM_INTERPOLATE_TIMING | |
10d5e750 KZ |
282 | | PA_STREAM_AUTO_TIMING_UPDATE |
283 | | PA_STREAM_EARLY_REQUESTS; | |
9d34e6d8 | 284 | |
8a435f74 KZ |
285 | if (dev) { |
286 | /* don't move the stream if the user specified a sink/source */ | |
287 | flags |= PA_STREAM_DONT_MOVE; | |
288 | } | |
289 | ||
9d34e6d8 KZ |
290 | if (dir == PA_STREAM_PLAYBACK) { |
291 | r = pa_stream_connect_playback(stream, dev, attr, flags, NULL, NULL); | |
ea9ebc2c | 292 | } else { |
9d34e6d8 | 293 | r = pa_stream_connect_record(stream, dev, attr, flags); |
ea9ebc2c MAL |
294 | } |
295 | ||
296 | if (r < 0) { | |
297 | goto fail; | |
298 | } | |
299 | ||
9d34e6d8 | 300 | pa_threaded_mainloop_unlock(c->mainloop); |
ea9ebc2c MAL |
301 | |
302 | return stream; | |
303 | ||
304 | fail: | |
9d34e6d8 | 305 | pa_threaded_mainloop_unlock(c->mainloop); |
ea9ebc2c MAL |
306 | |
307 | if (stream) { | |
308 | pa_stream_unref (stream); | |
309 | } | |
310 | ||
9d34e6d8 | 311 | *rerror = pa_context_errno(c->context); |
ea9ebc2c MAL |
312 | |
313 | return NULL; | |
314 | } | |
315 | ||
5706db1d KZ |
316 | static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as, |
317 | void *drv_opaque) | |
b8e59f18 | 318 | { |
319 | int error; | |
9a644c4b KZ |
320 | pa_sample_spec ss; |
321 | pa_buffer_attr ba; | |
1ea879e5 | 322 | struct audsettings obt_as = *as; |
b8e59f18 | 323 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
9a644c4b | 324 | paaudio *g = pa->g = drv_opaque; |
2c324b28 KZ |
325 | AudiodevPaOptions *popts = &g->dev->u.pa; |
326 | AudiodevPaPerDirectionOptions *ppdo = popts->out; | |
9d34e6d8 | 327 | PAConnection *c = g->conn; |
b8e59f18 | 328 | |
329 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
330 | ss.channels = as->nchannels; | |
331 | ss.rate = as->freq; | |
332 | ||
f6142777 MS |
333 | ba.tlength = pa_usec_to_bytes(ppdo->latency, &ss); |
334 | ba.minreq = -1; | |
e6d16fa4 GH |
335 | ba.maxlength = -1; |
336 | ba.prebuf = -1; | |
337 | ||
b8e59f18 | 338 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); |
339 | ||
ea9ebc2c | 340 | pa->stream = qpa_simple_new ( |
9d34e6d8 | 341 | c, |
f47dffe8 | 342 | ppdo->has_stream_name ? ppdo->stream_name : g->dev->id, |
b8e59f18 | 343 | PA_STREAM_PLAYBACK, |
2c324b28 | 344 | ppdo->has_name ? ppdo->name : NULL, |
b8e59f18 | 345 | &ss, |
346 | NULL, /* channel map */ | |
e6d16fa4 | 347 | &ba, /* buffering attributes */ |
b8e59f18 | 348 | &error |
349 | ); | |
ea9ebc2c | 350 | if (!pa->stream) { |
b8e59f18 | 351 | qpa_logerr (error, "pa_simple_new for playback failed\n"); |
352 | goto fail1; | |
353 | } | |
354 | ||
355 | audio_pcm_init_info (&hw->info, &obt_as); | |
2c324b28 | 356 | hw->samples = pa->samples = audio_buffer_samples( |
baea032e MS |
357 | qapi_AudiodevPaPerDirectionOptions_base(ppdo), |
358 | &obt_as, ppdo->buffer_length); | |
b8e59f18 | 359 | |
360 | return 0; | |
361 | ||
b8e59f18 | 362 | fail1: |
363 | return -1; | |
364 | } | |
365 | ||
5706db1d | 366 | static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
b8e59f18 | 367 | { |
368 | int error; | |
9a644c4b | 369 | pa_sample_spec ss; |
ade10301 | 370 | pa_buffer_attr ba; |
1ea879e5 | 371 | struct audsettings obt_as = *as; |
b8e59f18 | 372 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
9a644c4b | 373 | paaudio *g = pa->g = drv_opaque; |
2c324b28 KZ |
374 | AudiodevPaOptions *popts = &g->dev->u.pa; |
375 | AudiodevPaPerDirectionOptions *ppdo = popts->in; | |
9d34e6d8 | 376 | PAConnection *c = g->conn; |
b8e59f18 | 377 | |
378 | ss.format = audfmt_to_pa (as->fmt, as->endianness); | |
379 | ss.channels = as->nchannels; | |
380 | ss.rate = as->freq; | |
381 | ||
ade10301 | 382 | ba.fragsize = pa_usec_to_bytes(ppdo->latency, &ss); |
58c15e52 | 383 | ba.maxlength = pa_usec_to_bytes(ppdo->latency * 2, &ss); |
ade10301 MS |
384 | ba.minreq = -1; |
385 | ba.prebuf = -1; | |
386 | ||
b8e59f18 | 387 | obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); |
388 | ||
ea9ebc2c | 389 | pa->stream = qpa_simple_new ( |
9d34e6d8 | 390 | c, |
f47dffe8 | 391 | ppdo->has_stream_name ? ppdo->stream_name : g->dev->id, |
b8e59f18 | 392 | PA_STREAM_RECORD, |
2c324b28 | 393 | ppdo->has_name ? ppdo->name : NULL, |
b8e59f18 | 394 | &ss, |
395 | NULL, /* channel map */ | |
ade10301 | 396 | &ba, /* buffering attributes */ |
b8e59f18 | 397 | &error |
398 | ); | |
ea9ebc2c | 399 | if (!pa->stream) { |
b8e59f18 | 400 | qpa_logerr (error, "pa_simple_new for capture failed\n"); |
401 | goto fail1; | |
402 | } | |
403 | ||
404 | audio_pcm_init_info (&hw->info, &obt_as); | |
2c324b28 | 405 | hw->samples = pa->samples = audio_buffer_samples( |
baea032e MS |
406 | qapi_AudiodevPaPerDirectionOptions_base(ppdo), |
407 | &obt_as, ppdo->buffer_length); | |
b8e59f18 | 408 | |
409 | return 0; | |
410 | ||
b8e59f18 | 411 | fail1: |
412 | return -1; | |
413 | } | |
414 | ||
8692bf7d KZ |
415 | static void qpa_simple_disconnect(PAConnection *c, pa_stream *stream) |
416 | { | |
417 | int err; | |
418 | ||
419 | pa_threaded_mainloop_lock(c->mainloop); | |
420 | /* | |
421 | * wait until actually connects. workaround pa bug #247 | |
422 | * https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/247 | |
423 | */ | |
424 | while (pa_stream_get_state(stream) == PA_STREAM_CREATING) { | |
425 | pa_threaded_mainloop_wait(c->mainloop); | |
426 | } | |
427 | ||
428 | err = pa_stream_disconnect(stream); | |
429 | if (err != 0) { | |
430 | dolog("Failed to disconnect! err=%d\n", err); | |
431 | } | |
432 | pa_stream_unref(stream); | |
433 | pa_threaded_mainloop_unlock(c->mainloop); | |
434 | } | |
435 | ||
b8e59f18 | 436 | static void qpa_fini_out (HWVoiceOut *hw) |
437 | { | |
b8e59f18 | 438 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
439 | ||
ea9ebc2c | 440 | if (pa->stream) { |
8692bf7d | 441 | qpa_simple_disconnect(pa->g->conn, pa->stream); |
ea9ebc2c | 442 | pa->stream = NULL; |
b8e59f18 | 443 | } |
b8e59f18 | 444 | } |
445 | ||
446 | static void qpa_fini_in (HWVoiceIn *hw) | |
447 | { | |
b8e59f18 | 448 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
449 | ||
ea9ebc2c | 450 | if (pa->stream) { |
8692bf7d | 451 | qpa_simple_disconnect(pa->g->conn, pa->stream); |
ea9ebc2c | 452 | pa->stream = NULL; |
b8e59f18 | 453 | } |
b8e59f18 | 454 | } |
455 | ||
571a8c52 | 456 | static void qpa_volume_out(HWVoiceOut *hw, struct mixeng_volume *vol) |
b8e59f18 | 457 | { |
6e7a7f3d MAL |
458 | PAVoiceOut *pa = (PAVoiceOut *) hw; |
459 | pa_operation *op; | |
460 | pa_cvolume v; | |
9d34e6d8 | 461 | PAConnection *c = pa->g->conn; |
6e7a7f3d | 462 | |
8f473dd1 GH |
463 | #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ |
464 | pa_cvolume_init (&v); /* function is present in 0.9.13+ */ | |
465 | #endif | |
6e7a7f3d | 466 | |
571a8c52 KZ |
467 | v.channels = 2; |
468 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->l) / UINT32_MAX; | |
469 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->r) / UINT32_MAX; | |
470 | ||
471 | pa_threaded_mainloop_lock(c->mainloop); | |
472 | ||
473 | op = pa_context_set_sink_input_volume(c->context, | |
474 | pa_stream_get_index(pa->stream), | |
475 | &v, NULL, NULL); | |
476 | if (!op) { | |
477 | qpa_logerr(pa_context_errno(c->context), | |
478 | "set_sink_input_volume() failed\n"); | |
479 | } else { | |
480 | pa_operation_unref(op); | |
6e7a7f3d | 481 | } |
571a8c52 KZ |
482 | |
483 | op = pa_context_set_sink_input_mute(c->context, | |
484 | pa_stream_get_index(pa->stream), | |
485 | vol->mute, NULL, NULL); | |
486 | if (!op) { | |
487 | qpa_logerr(pa_context_errno(c->context), | |
488 | "set_sink_input_mute() failed\n"); | |
489 | } else { | |
490 | pa_operation_unref(op); | |
491 | } | |
492 | ||
493 | pa_threaded_mainloop_unlock(c->mainloop); | |
b8e59f18 | 494 | } |
495 | ||
571a8c52 | 496 | static void qpa_volume_in(HWVoiceIn *hw, struct mixeng_volume *vol) |
b8e59f18 | 497 | { |
6e7a7f3d MAL |
498 | PAVoiceIn *pa = (PAVoiceIn *) hw; |
499 | pa_operation *op; | |
500 | pa_cvolume v; | |
9d34e6d8 | 501 | PAConnection *c = pa->g->conn; |
6e7a7f3d | 502 | |
8f473dd1 | 503 | #ifdef PA_CHECK_VERSION |
6e7a7f3d | 504 | pa_cvolume_init (&v); |
8f473dd1 | 505 | #endif |
6e7a7f3d | 506 | |
571a8c52 KZ |
507 | v.channels = 2; |
508 | v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->l) / UINT32_MAX; | |
509 | v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->r) / UINT32_MAX; | |
510 | ||
511 | pa_threaded_mainloop_lock(c->mainloop); | |
512 | ||
513 | op = pa_context_set_source_output_volume(c->context, | |
514 | pa_stream_get_index(pa->stream), | |
515 | &v, NULL, NULL); | |
516 | if (!op) { | |
517 | qpa_logerr(pa_context_errno(c->context), | |
518 | "set_source_output_volume() failed\n"); | |
519 | } else { | |
520 | pa_operation_unref(op); | |
6e7a7f3d | 521 | } |
571a8c52 KZ |
522 | |
523 | op = pa_context_set_source_output_mute(c->context, | |
524 | pa_stream_get_index(pa->stream), | |
525 | vol->mute, NULL, NULL); | |
526 | if (!op) { | |
527 | qpa_logerr(pa_context_errno(c->context), | |
528 | "set_source_output_mute() failed\n"); | |
529 | } else { | |
530 | pa_operation_unref(op); | |
531 | } | |
532 | ||
533 | pa_threaded_mainloop_unlock(c->mainloop); | |
b8e59f18 | 534 | } |
535 | ||
baea032e MS |
536 | static int qpa_validate_per_direction_opts(Audiodev *dev, |
537 | AudiodevPaPerDirectionOptions *pdo) | |
538 | { | |
539 | if (!pdo->has_buffer_length) { | |
540 | pdo->has_buffer_length = true; | |
541 | pdo->buffer_length = 46440; | |
542 | } | |
f6142777 MS |
543 | if (!pdo->has_latency) { |
544 | pdo->has_latency = true; | |
545 | pdo->latency = 15000; | |
546 | } | |
baea032e MS |
547 | return 1; |
548 | } | |
549 | ||
9d34e6d8 KZ |
550 | /* common */ |
551 | static void *qpa_conn_init(const char *server) | |
552 | { | |
3443ad4e | 553 | const char *vm_name; |
9d34e6d8 KZ |
554 | PAConnection *c = g_malloc0(sizeof(PAConnection)); |
555 | QTAILQ_INSERT_TAIL(&pa_conns, c, list); | |
556 | ||
557 | c->mainloop = pa_threaded_mainloop_new(); | |
558 | if (!c->mainloop) { | |
559 | goto fail; | |
560 | } | |
561 | ||
3443ad4e | 562 | vm_name = qemu_get_vm_name(); |
9d34e6d8 | 563 | c->context = pa_context_new(pa_threaded_mainloop_get_api(c->mainloop), |
3443ad4e | 564 | vm_name ? vm_name : "qemu"); |
9d34e6d8 KZ |
565 | if (!c->context) { |
566 | goto fail; | |
567 | } | |
568 | ||
569 | pa_context_set_state_callback(c->context, context_state_cb, c); | |
570 | ||
571 | if (pa_context_connect(c->context, server, 0, NULL) < 0) { | |
572 | qpa_logerr(pa_context_errno(c->context), | |
573 | "pa_context_connect() failed\n"); | |
574 | goto fail; | |
575 | } | |
576 | ||
577 | pa_threaded_mainloop_lock(c->mainloop); | |
578 | ||
579 | if (pa_threaded_mainloop_start(c->mainloop) < 0) { | |
580 | goto unlock_and_fail; | |
581 | } | |
582 | ||
583 | for (;;) { | |
584 | pa_context_state_t state; | |
585 | ||
586 | state = pa_context_get_state(c->context); | |
587 | ||
588 | if (state == PA_CONTEXT_READY) { | |
589 | break; | |
590 | } | |
591 | ||
592 | if (!PA_CONTEXT_IS_GOOD(state)) { | |
593 | qpa_logerr(pa_context_errno(c->context), | |
594 | "Wrong context state\n"); | |
595 | goto unlock_and_fail; | |
596 | } | |
597 | ||
598 | /* Wait until the context is ready */ | |
599 | pa_threaded_mainloop_wait(c->mainloop); | |
600 | } | |
601 | ||
602 | pa_threaded_mainloop_unlock(c->mainloop); | |
603 | return c; | |
604 | ||
605 | unlock_and_fail: | |
606 | pa_threaded_mainloop_unlock(c->mainloop); | |
607 | fail: | |
608 | AUD_log (AUDIO_CAP, "Failed to initialize PA context"); | |
609 | qpa_conn_fini(c); | |
610 | return NULL; | |
611 | } | |
612 | ||
71830221 | 613 | static void *qpa_audio_init(Audiodev *dev) |
b8e59f18 | 614 | { |
2c324b28 KZ |
615 | paaudio *g; |
616 | AudiodevPaOptions *popts = &dev->u.pa; | |
617 | const char *server; | |
9d34e6d8 KZ |
618 | PAConnection *c; |
619 | ||
620 | assert(dev->driver == AUDIODEV_DRIVER_PA); | |
2c324b28 KZ |
621 | |
622 | if (!popts->has_server) { | |
d175505b GH |
623 | char pidfile[64]; |
624 | char *runtime; | |
625 | struct stat st; | |
626 | ||
627 | runtime = getenv("XDG_RUNTIME_DIR"); | |
628 | if (!runtime) { | |
629 | return NULL; | |
630 | } | |
631 | snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime); | |
632 | if (stat(pidfile, &st) != 0) { | |
633 | return NULL; | |
634 | } | |
635 | } | |
636 | ||
baea032e | 637 | if (!qpa_validate_per_direction_opts(dev, popts->in)) { |
9d34e6d8 | 638 | return NULL; |
baea032e MS |
639 | } |
640 | if (!qpa_validate_per_direction_opts(dev, popts->out)) { | |
9d34e6d8 | 641 | return NULL; |
baea032e MS |
642 | } |
643 | ||
9d34e6d8 KZ |
644 | g = g_malloc0(sizeof(paaudio)); |
645 | server = popts->has_server ? popts->server : NULL; | |
646 | ||
2c324b28 | 647 | g->dev = dev; |
ea9ebc2c | 648 | |
9d34e6d8 KZ |
649 | QTAILQ_FOREACH(c, &pa_conns, list) { |
650 | if (server == NULL || c->server == NULL ? | |
651 | server == c->server : | |
652 | strcmp(server, c->server) == 0) { | |
653 | g->conn = c; | |
654 | break; | |
655 | } | |
ea9ebc2c | 656 | } |
9d34e6d8 KZ |
657 | if (!g->conn) { |
658 | g->conn = qpa_conn_init(server); | |
ea9ebc2c | 659 | } |
9d34e6d8 KZ |
660 | if (!g->conn) { |
661 | g_free(g); | |
662 | return NULL; | |
ea9ebc2c MAL |
663 | } |
664 | ||
9d34e6d8 KZ |
665 | ++g->conn->refcount; |
666 | return g; | |
667 | } | |
ea9ebc2c | 668 | |
9d34e6d8 KZ |
669 | static void qpa_conn_fini(PAConnection *c) |
670 | { | |
671 | if (c->mainloop) { | |
672 | pa_threaded_mainloop_stop(c->mainloop); | |
ea9ebc2c MAL |
673 | } |
674 | ||
9d34e6d8 KZ |
675 | if (c->context) { |
676 | pa_context_disconnect(c->context); | |
677 | pa_context_unref(c->context); | |
ea9ebc2c MAL |
678 | } |
679 | ||
9d34e6d8 KZ |
680 | if (c->mainloop) { |
681 | pa_threaded_mainloop_free(c->mainloop); | |
682 | } | |
ea9ebc2c | 683 | |
9d34e6d8 KZ |
684 | QTAILQ_REMOVE(&pa_conns, c, list); |
685 | g_free(c); | |
b8e59f18 | 686 | } |
687 | ||
688 | static void qpa_audio_fini (void *opaque) | |
689 | { | |
ea9ebc2c | 690 | paaudio *g = opaque; |
9d34e6d8 | 691 | PAConnection *c = g->conn; |
ea9ebc2c | 692 | |
9d34e6d8 KZ |
693 | if (--c->refcount == 0) { |
694 | qpa_conn_fini(c); | |
ea9ebc2c MAL |
695 | } |
696 | ||
9a644c4b | 697 | g_free(g); |
b8e59f18 | 698 | } |
699 | ||
35f4b58c | 700 | static struct audio_pcm_ops qpa_pcm_ops = { |
1dd3e4d1 JQ |
701 | .init_out = qpa_init_out, |
702 | .fini_out = qpa_fini_out, | |
49ddd7e1 | 703 | .write = qpa_write, |
571a8c52 | 704 | .volume_out = qpa_volume_out, |
1dd3e4d1 JQ |
705 | |
706 | .init_in = qpa_init_in, | |
707 | .fini_in = qpa_fini_in, | |
49ddd7e1 | 708 | .read = qpa_read, |
571a8c52 | 709 | .volume_in = qpa_volume_in |
b8e59f18 | 710 | }; |
711 | ||
d3893a39 | 712 | static struct audio_driver pa_audio_driver = { |
bee37f32 JQ |
713 | .name = "pa", |
714 | .descr = "http://www.pulseaudio.org/", | |
bee37f32 JQ |
715 | .init = qpa_audio_init, |
716 | .fini = qpa_audio_fini, | |
717 | .pcm_ops = &qpa_pcm_ops, | |
1a4ea1e3 | 718 | .can_be_default = 1, |
bee37f32 JQ |
719 | .max_voices_out = INT_MAX, |
720 | .max_voices_in = INT_MAX, | |
721 | .voice_size_out = sizeof (PAVoiceOut), | |
6e7a7f3d | 722 | .voice_size_in = sizeof (PAVoiceIn), |
b8e59f18 | 723 | }; |
d3893a39 GH |
724 | |
725 | static void register_audio_pa(void) | |
726 | { | |
727 | audio_driver_register(&pa_audio_driver); | |
728 | } | |
729 | type_init(register_audio_pa); |