]> git.proxmox.com Git - mirror_qemu.git/blame - audio/paaudio.c
paaudio: drop recording stream in qpa_fini_in
[mirror_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
8692bf7d
KZ
539 /*
540 * wait until actually connects. workaround pa bug #247
541 * https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/247
542 */
543 while (pa_stream_get_state(stream) == PA_STREAM_CREATING) {
544 pa_threaded_mainloop_wait(c->mainloop);
545 }
546
547 err = pa_stream_disconnect(stream);
548 if (err != 0) {
549 dolog("Failed to disconnect! err=%d\n", err);
550 }
551 pa_stream_unref(stream);
8692bf7d
KZ
552}
553
b8e59f18 554static void qpa_fini_out (HWVoiceOut *hw)
555{
b8e59f18 556 PAVoiceOut *pa = (PAVoiceOut *) hw;
557
ea9ebc2c 558 if (pa->stream) {
4db3e634
VR
559 PAConnection *c = pa->g->conn;
560
561 pa_threaded_mainloop_lock(c->mainloop);
562 qpa_simple_disconnect(c, pa->stream);
ea9ebc2c 563 pa->stream = NULL;
4db3e634 564 pa_threaded_mainloop_unlock(c->mainloop);
b8e59f18 565 }
b8e59f18 566}
567
568static void qpa_fini_in (HWVoiceIn *hw)
569{
b8e59f18 570 PAVoiceIn *pa = (PAVoiceIn *) hw;
571
ea9ebc2c 572 if (pa->stream) {
4db3e634
VR
573 PAConnection *c = pa->g->conn;
574
575 pa_threaded_mainloop_lock(c->mainloop);
576 if (pa->read_length) {
577 int r = pa_stream_drop(pa->stream);
578 if (r) {
579 qpa_logerr(pa_context_errno(c->context),
580 "pa_stream_drop failed\n");
581 }
582 pa->read_length = 0;
583 }
584 qpa_simple_disconnect(c, pa->stream);
ea9ebc2c 585 pa->stream = NULL;
4db3e634 586 pa_threaded_mainloop_unlock(c->mainloop);
b8e59f18 587 }
b8e59f18 588}
589
cecc1e79 590static void qpa_volume_out(HWVoiceOut *hw, Volume *vol)
b8e59f18 591{
6e7a7f3d
MAL
592 PAVoiceOut *pa = (PAVoiceOut *) hw;
593 pa_operation *op;
594 pa_cvolume v;
9d34e6d8 595 PAConnection *c = pa->g->conn;
cecc1e79 596 int i;
6e7a7f3d 597
8f473dd1
GH
598#ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
599 pa_cvolume_init (&v); /* function is present in 0.9.13+ */
600#endif
6e7a7f3d 601
cecc1e79
KZ
602 v.channels = vol->channels;
603 for (i = 0; i < vol->channels; ++i) {
604 v.values[i] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->vol[i]) / 255;
605 }
571a8c52
KZ
606
607 pa_threaded_mainloop_lock(c->mainloop);
608
609 op = pa_context_set_sink_input_volume(c->context,
610 pa_stream_get_index(pa->stream),
611 &v, NULL, NULL);
612 if (!op) {
613 qpa_logerr(pa_context_errno(c->context),
614 "set_sink_input_volume() failed\n");
615 } else {
616 pa_operation_unref(op);
6e7a7f3d 617 }
571a8c52
KZ
618
619 op = pa_context_set_sink_input_mute(c->context,
620 pa_stream_get_index(pa->stream),
621 vol->mute, NULL, NULL);
622 if (!op) {
623 qpa_logerr(pa_context_errno(c->context),
624 "set_sink_input_mute() failed\n");
625 } else {
626 pa_operation_unref(op);
627 }
628
629 pa_threaded_mainloop_unlock(c->mainloop);
b8e59f18 630}
631
cecc1e79 632static void qpa_volume_in(HWVoiceIn *hw, Volume *vol)
b8e59f18 633{
6e7a7f3d
MAL
634 PAVoiceIn *pa = (PAVoiceIn *) hw;
635 pa_operation *op;
636 pa_cvolume v;
9d34e6d8 637 PAConnection *c = pa->g->conn;
cecc1e79 638 int i;
6e7a7f3d 639
8f473dd1 640#ifdef PA_CHECK_VERSION
6e7a7f3d 641 pa_cvolume_init (&v);
8f473dd1 642#endif
6e7a7f3d 643
cecc1e79
KZ
644 v.channels = vol->channels;
645 for (i = 0; i < vol->channels; ++i) {
646 v.values[i] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->vol[i]) / 255;
647 }
571a8c52
KZ
648
649 pa_threaded_mainloop_lock(c->mainloop);
650
651 op = pa_context_set_source_output_volume(c->context,
652 pa_stream_get_index(pa->stream),
653 &v, NULL, NULL);
654 if (!op) {
655 qpa_logerr(pa_context_errno(c->context),
656 "set_source_output_volume() failed\n");
657 } else {
658 pa_operation_unref(op);
6e7a7f3d 659 }
571a8c52
KZ
660
661 op = pa_context_set_source_output_mute(c->context,
662 pa_stream_get_index(pa->stream),
663 vol->mute, NULL, NULL);
664 if (!op) {
665 qpa_logerr(pa_context_errno(c->context),
666 "set_source_output_mute() failed\n");
667 } else {
668 pa_operation_unref(op);
669 }
670
671 pa_threaded_mainloop_unlock(c->mainloop);
b8e59f18 672}
673
baea032e
MS
674static int qpa_validate_per_direction_opts(Audiodev *dev,
675 AudiodevPaPerDirectionOptions *pdo)
676{
677 if (!pdo->has_buffer_length) {
678 pdo->has_buffer_length = true;
679 pdo->buffer_length = 46440;
680 }
f6142777
MS
681 if (!pdo->has_latency) {
682 pdo->has_latency = true;
683 pdo->latency = 15000;
684 }
baea032e
MS
685 return 1;
686}
687
9d34e6d8
KZ
688/* common */
689static void *qpa_conn_init(const char *server)
690{
3443ad4e 691 const char *vm_name;
9d34e6d8
KZ
692 PAConnection *c = g_malloc0(sizeof(PAConnection));
693 QTAILQ_INSERT_TAIL(&pa_conns, c, list);
694
695 c->mainloop = pa_threaded_mainloop_new();
696 if (!c->mainloop) {
697 goto fail;
698 }
699
3443ad4e 700 vm_name = qemu_get_vm_name();
9d34e6d8 701 c->context = pa_context_new(pa_threaded_mainloop_get_api(c->mainloop),
3443ad4e 702 vm_name ? vm_name : "qemu");
9d34e6d8
KZ
703 if (!c->context) {
704 goto fail;
705 }
706
707 pa_context_set_state_callback(c->context, context_state_cb, c);
708
709 if (pa_context_connect(c->context, server, 0, NULL) < 0) {
710 qpa_logerr(pa_context_errno(c->context),
711 "pa_context_connect() failed\n");
712 goto fail;
713 }
714
715 pa_threaded_mainloop_lock(c->mainloop);
716
717 if (pa_threaded_mainloop_start(c->mainloop) < 0) {
718 goto unlock_and_fail;
719 }
720
721 for (;;) {
722 pa_context_state_t state;
723
724 state = pa_context_get_state(c->context);
725
726 if (state == PA_CONTEXT_READY) {
727 break;
728 }
729
730 if (!PA_CONTEXT_IS_GOOD(state)) {
731 qpa_logerr(pa_context_errno(c->context),
732 "Wrong context state\n");
733 goto unlock_and_fail;
734 }
735
736 /* Wait until the context is ready */
737 pa_threaded_mainloop_wait(c->mainloop);
738 }
739
740 pa_threaded_mainloop_unlock(c->mainloop);
741 return c;
742
743unlock_and_fail:
744 pa_threaded_mainloop_unlock(c->mainloop);
745fail:
746 AUD_log (AUDIO_CAP, "Failed to initialize PA context");
747 qpa_conn_fini(c);
748 return NULL;
749}
750
71830221 751static void *qpa_audio_init(Audiodev *dev)
b8e59f18 752{
2c324b28
KZ
753 paaudio *g;
754 AudiodevPaOptions *popts = &dev->u.pa;
755 const char *server;
9d34e6d8
KZ
756 PAConnection *c;
757
758 assert(dev->driver == AUDIODEV_DRIVER_PA);
2c324b28
KZ
759
760 if (!popts->has_server) {
d175505b
GH
761 char pidfile[64];
762 char *runtime;
763 struct stat st;
764
765 runtime = getenv("XDG_RUNTIME_DIR");
766 if (!runtime) {
767 return NULL;
768 }
769 snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime);
770 if (stat(pidfile, &st) != 0) {
771 return NULL;
772 }
773 }
774
baea032e 775 if (!qpa_validate_per_direction_opts(dev, popts->in)) {
9d34e6d8 776 return NULL;
baea032e
MS
777 }
778 if (!qpa_validate_per_direction_opts(dev, popts->out)) {
9d34e6d8 779 return NULL;
baea032e
MS
780 }
781
9d34e6d8
KZ
782 g = g_malloc0(sizeof(paaudio));
783 server = popts->has_server ? popts->server : NULL;
784
2c324b28 785 g->dev = dev;
ea9ebc2c 786
9d34e6d8
KZ
787 QTAILQ_FOREACH(c, &pa_conns, list) {
788 if (server == NULL || c->server == NULL ?
789 server == c->server :
790 strcmp(server, c->server) == 0) {
791 g->conn = c;
792 break;
793 }
ea9ebc2c 794 }
9d34e6d8
KZ
795 if (!g->conn) {
796 g->conn = qpa_conn_init(server);
ea9ebc2c 797 }
9d34e6d8
KZ
798 if (!g->conn) {
799 g_free(g);
800 return NULL;
ea9ebc2c
MAL
801 }
802
9d34e6d8
KZ
803 ++g->conn->refcount;
804 return g;
805}
ea9ebc2c 806
9d34e6d8
KZ
807static void qpa_conn_fini(PAConnection *c)
808{
809 if (c->mainloop) {
810 pa_threaded_mainloop_stop(c->mainloop);
ea9ebc2c
MAL
811 }
812
9d34e6d8
KZ
813 if (c->context) {
814 pa_context_disconnect(c->context);
815 pa_context_unref(c->context);
ea9ebc2c
MAL
816 }
817
9d34e6d8
KZ
818 if (c->mainloop) {
819 pa_threaded_mainloop_free(c->mainloop);
820 }
ea9ebc2c 821
9d34e6d8
KZ
822 QTAILQ_REMOVE(&pa_conns, c, list);
823 g_free(c);
b8e59f18 824}
825
826static void qpa_audio_fini (void *opaque)
827{
ea9ebc2c 828 paaudio *g = opaque;
9d34e6d8 829 PAConnection *c = g->conn;
ea9ebc2c 830
9d34e6d8
KZ
831 if (--c->refcount == 0) {
832 qpa_conn_fini(c);
ea9ebc2c
MAL
833 }
834
9a644c4b 835 g_free(g);
b8e59f18 836}
837
35f4b58c 838static struct audio_pcm_ops qpa_pcm_ops = {
1dd3e4d1
JQ
839 .init_out = qpa_init_out,
840 .fini_out = qpa_fini_out,
49ddd7e1 841 .write = qpa_write,
337e8de6
KZ
842 .get_buffer_out = qpa_get_buffer_out,
843 .put_buffer_out = qpa_write, /* pa handles it */
571a8c52 844 .volume_out = qpa_volume_out,
1dd3e4d1
JQ
845
846 .init_in = qpa_init_in,
847 .fini_in = qpa_fini_in,
49ddd7e1 848 .read = qpa_read,
337e8de6
KZ
849 .get_buffer_in = qpa_get_buffer_in,
850 .put_buffer_in = qpa_put_buffer_in,
571a8c52 851 .volume_in = qpa_volume_in
b8e59f18 852};
853
d3893a39 854static struct audio_driver pa_audio_driver = {
bee37f32
JQ
855 .name = "pa",
856 .descr = "http://www.pulseaudio.org/",
bee37f32
JQ
857 .init = qpa_audio_init,
858 .fini = qpa_audio_fini,
859 .pcm_ops = &qpa_pcm_ops,
1a4ea1e3 860 .can_be_default = 1,
bee37f32
JQ
861 .max_voices_out = INT_MAX,
862 .max_voices_in = INT_MAX,
863 .voice_size_out = sizeof (PAVoiceOut),
6e7a7f3d 864 .voice_size_in = sizeof (PAVoiceIn),
b8e59f18 865};
d3893a39
GH
866
867static void register_audio_pa(void)
868{
869 audio_driver_register(&pa_audio_driver);
870}
871type_init(register_audio_pa);