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