1b8e59f18Smalc /* public domain */ 20b8fa32fSMarkus Armbruster 36086a565SPeter Maydell #include "qemu/osdep.h" 40b8fa32fSMarkus Armbruster #include "qemu/module.h" 53443ad4eSKővágó, Zoltán #include "qemu-common.h" 6b8e59f18Smalc #include "audio.h" 72c324b28SKővágó, Zoltán #include "qapi/opts-visitor.h" 8b8e59f18Smalc 9ea9ebc2cSMarc-André Lureau #include <pulse/pulseaudio.h> 10b8e59f18Smalc 11b8e59f18Smalc #define AUDIO_CAP "pulseaudio" 12b8e59f18Smalc #include "audio_int.h" 13b8e59f18Smalc 149d34e6d8SKővágó, Zoltán typedef struct PAConnection { 159d34e6d8SKővágó, Zoltán char *server; 169d34e6d8SKővágó, Zoltán int refcount; 179d34e6d8SKővágó, Zoltán QTAILQ_ENTRY(PAConnection) list; 189d34e6d8SKővágó, Zoltán 199a644c4bSKővágó, Zoltán pa_threaded_mainloop *mainloop; 209a644c4bSKővágó, Zoltán pa_context *context; 219d34e6d8SKővágó, Zoltán } PAConnection; 229d34e6d8SKővágó, Zoltán 239d34e6d8SKővágó, Zoltán static QTAILQ_HEAD(PAConnectionHead, PAConnection) pa_conns = 249d34e6d8SKővágó, Zoltán QTAILQ_HEAD_INITIALIZER(pa_conns); 259d34e6d8SKővágó, Zoltán 269d34e6d8SKővágó, Zoltán typedef struct { 279d34e6d8SKővágó, Zoltán Audiodev *dev; 289d34e6d8SKővágó, Zoltán PAConnection *conn; 299a644c4bSKővágó, Zoltán } paaudio; 309a644c4bSKővágó, Zoltán 319a644c4bSKővágó, Zoltán typedef struct { 32b8e59f18Smalc HWVoiceOut hw; 33ea9ebc2cSMarc-André Lureau pa_stream *stream; 349a644c4bSKővágó, Zoltán paaudio *g; 35b8e59f18Smalc } PAVoiceOut; 36b8e59f18Smalc 37b8e59f18Smalc typedef struct { 38b8e59f18Smalc HWVoiceIn hw; 39ea9ebc2cSMarc-André Lureau pa_stream *stream; 40ea9ebc2cSMarc-André Lureau const void *read_data; 4149ddd7e1SKővágó, Zoltán size_t read_length; 429a644c4bSKővágó, Zoltán paaudio *g; 43b8e59f18Smalc } PAVoiceIn; 44b8e59f18Smalc 459d34e6d8SKővágó, Zoltán static void qpa_conn_fini(PAConnection *c); 4649dd6d0dSKővágó, Zoltán 47b8e59f18Smalc static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...) 48b8e59f18Smalc { 49b8e59f18Smalc va_list ap; 50b8e59f18Smalc 51b8e59f18Smalc va_start (ap, fmt); 52b8e59f18Smalc AUD_vlog (AUDIO_CAP, fmt, ap); 53b8e59f18Smalc va_end (ap); 54b8e59f18Smalc 55b8e59f18Smalc AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err)); 56b8e59f18Smalc } 57b8e59f18Smalc 588f473dd1SGerd Hoffmann #ifndef PA_CONTEXT_IS_GOOD 598f473dd1SGerd Hoffmann static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) 608f473dd1SGerd Hoffmann { 618f473dd1SGerd Hoffmann return 628f473dd1SGerd Hoffmann x == PA_CONTEXT_CONNECTING || 638f473dd1SGerd Hoffmann x == PA_CONTEXT_AUTHORIZING || 648f473dd1SGerd Hoffmann x == PA_CONTEXT_SETTING_NAME || 658f473dd1SGerd Hoffmann x == PA_CONTEXT_READY; 668f473dd1SGerd Hoffmann } 678f473dd1SGerd Hoffmann #endif 688f473dd1SGerd Hoffmann 698f473dd1SGerd Hoffmann #ifndef PA_STREAM_IS_GOOD 708f473dd1SGerd Hoffmann static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) 718f473dd1SGerd Hoffmann { 728f473dd1SGerd Hoffmann return 738f473dd1SGerd Hoffmann x == PA_STREAM_CREATING || 748f473dd1SGerd Hoffmann x == PA_STREAM_READY; 758f473dd1SGerd Hoffmann } 768f473dd1SGerd Hoffmann #endif 778f473dd1SGerd Hoffmann 7849ddd7e1SKővágó, Zoltán #define CHECK_SUCCESS_GOTO(c, expression, label, msg) \ 79ea9ebc2cSMarc-André Lureau do { \ 80ea9ebc2cSMarc-André Lureau if (!(expression)) { \ 8149ddd7e1SKővágó, Zoltán qpa_logerr(pa_context_errno((c)->context), msg); \ 82ea9ebc2cSMarc-André Lureau goto label; \ 83ea9ebc2cSMarc-André Lureau } \ 842562755eSEric Blake } while (0) 85ea9ebc2cSMarc-André Lureau 8649ddd7e1SKővágó, Zoltán #define CHECK_DEAD_GOTO(c, stream, label, msg) \ 87ea9ebc2cSMarc-André Lureau do { \ 88ea9ebc2cSMarc-André Lureau if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \ 89ea9ebc2cSMarc-André Lureau !(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \ 90ea9ebc2cSMarc-André Lureau if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \ 91ea9ebc2cSMarc-André Lureau ((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \ 9249ddd7e1SKővágó, Zoltán qpa_logerr(pa_context_errno((c)->context), msg); \ 93ea9ebc2cSMarc-André Lureau } else { \ 9449ddd7e1SKővágó, Zoltán qpa_logerr(PA_ERR_BADSTATE, msg); \ 95ea9ebc2cSMarc-André Lureau } \ 96ea9ebc2cSMarc-André Lureau goto label; \ 97ea9ebc2cSMarc-André Lureau } \ 982562755eSEric Blake } while (0) 99ea9ebc2cSMarc-André Lureau 100337e8de6SKővágó, Zoltán static void *qpa_get_buffer_in(HWVoiceIn *hw, size_t *size) 101337e8de6SKővágó, Zoltán { 102337e8de6SKővágó, Zoltán PAVoiceIn *p = (PAVoiceIn *) hw; 103337e8de6SKővágó, Zoltán PAConnection *c = p->g->conn; 104337e8de6SKővágó, Zoltán int r; 105337e8de6SKővágó, Zoltán 106337e8de6SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 107337e8de6SKővágó, Zoltán 108337e8de6SKővágó, Zoltán CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, 109337e8de6SKővágó, Zoltán "pa_threaded_mainloop_lock failed\n"); 110337e8de6SKővágó, Zoltán 111337e8de6SKővágó, Zoltán if (!p->read_length) { 112337e8de6SKővágó, Zoltán r = pa_stream_peek(p->stream, &p->read_data, &p->read_length); 113337e8de6SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r == 0, unlock_and_fail, 114337e8de6SKővágó, Zoltán "pa_stream_peek failed\n"); 115337e8de6SKővágó, Zoltán } 116337e8de6SKővágó, Zoltán 117337e8de6SKővágó, Zoltán *size = MIN(p->read_length, *size); 118337e8de6SKővágó, Zoltán 119337e8de6SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 120337e8de6SKővágó, Zoltán return (void *) p->read_data; 121337e8de6SKővágó, Zoltán 122337e8de6SKővágó, Zoltán unlock_and_fail: 123337e8de6SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 124337e8de6SKővágó, Zoltán *size = 0; 125337e8de6SKővágó, Zoltán return NULL; 126337e8de6SKővágó, Zoltán } 127337e8de6SKővágó, Zoltán 128337e8de6SKővágó, Zoltán static void qpa_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size) 129337e8de6SKővágó, Zoltán { 130337e8de6SKővágó, Zoltán PAVoiceIn *p = (PAVoiceIn *) hw; 131337e8de6SKővágó, Zoltán PAConnection *c = p->g->conn; 132337e8de6SKővágó, Zoltán int r; 133337e8de6SKővágó, Zoltán 134337e8de6SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 135337e8de6SKővágó, Zoltán 136337e8de6SKővágó, Zoltán CHECK_DEAD_GOTO(c, p->stream, unlock, 137337e8de6SKővágó, Zoltán "pa_threaded_mainloop_lock failed\n"); 138337e8de6SKővágó, Zoltán 139337e8de6SKővágó, Zoltán assert(buf == p->read_data && size <= p->read_length); 140337e8de6SKővágó, Zoltán 141337e8de6SKővágó, Zoltán p->read_data += size; 142337e8de6SKővágó, Zoltán p->read_length -= size; 143337e8de6SKővágó, Zoltán 144337e8de6SKővágó, Zoltán if (size && !p->read_length) { 145337e8de6SKővágó, Zoltán r = pa_stream_drop(p->stream); 146337e8de6SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r == 0, unlock, "pa_stream_drop failed\n"); 147337e8de6SKővágó, Zoltán } 148337e8de6SKővágó, Zoltán 149337e8de6SKővágó, Zoltán unlock: 150337e8de6SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 151337e8de6SKővágó, Zoltán } 152337e8de6SKővágó, Zoltán 15349ddd7e1SKővágó, Zoltán static size_t qpa_read(HWVoiceIn *hw, void *data, size_t length) 154ea9ebc2cSMarc-André Lureau { 15549ddd7e1SKővágó, Zoltán PAVoiceIn *p = (PAVoiceIn *) hw; 1569d34e6d8SKővágó, Zoltán PAConnection *c = p->g->conn; 157acc3b63eSVolker Rümelin size_t total = 0; 158ea9ebc2cSMarc-André Lureau 1599d34e6d8SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 160ea9ebc2cSMarc-André Lureau 16149ddd7e1SKővágó, Zoltán CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, 16249ddd7e1SKővágó, Zoltán "pa_threaded_mainloop_lock failed\n"); 1637c9eb86eSVolker Rümelin if (pa_stream_get_state(p->stream) != PA_STREAM_READY) { 1647c9eb86eSVolker Rümelin /* wait for stream to become ready */ 1657c9eb86eSVolker Rümelin goto unlock; 1667c9eb86eSVolker Rümelin } 167ea9ebc2cSMarc-André Lureau 168acc3b63eSVolker Rümelin while (total < length) { 169acc3b63eSVolker Rümelin size_t l; 170acc3b63eSVolker Rümelin int r; 171acc3b63eSVolker Rümelin 17249ddd7e1SKővágó, Zoltán if (!p->read_length) { 173ea9ebc2cSMarc-André Lureau r = pa_stream_peek(p->stream, &p->read_data, &p->read_length); 17449ddd7e1SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r == 0, unlock_and_fail, 17549ddd7e1SKővágó, Zoltán "pa_stream_peek failed\n"); 176acc3b63eSVolker Rümelin if (!p->read_length) { 177acc3b63eSVolker Rümelin /* buffer is empty */ 178acc3b63eSVolker Rümelin break; 179acc3b63eSVolker Rümelin } 180ea9ebc2cSMarc-André Lureau } 181ea9ebc2cSMarc-André Lureau 182acc3b63eSVolker Rümelin l = MIN(p->read_length, length - total); 183acc3b63eSVolker Rümelin memcpy((char *)data + total, p->read_data, l); 184ea9ebc2cSMarc-André Lureau 18549ddd7e1SKővágó, Zoltán p->read_data += l; 186ea9ebc2cSMarc-André Lureau p->read_length -= l; 187acc3b63eSVolker Rümelin total += l; 188ea9ebc2cSMarc-André Lureau 189ea9ebc2cSMarc-André Lureau if (!p->read_length) { 190ea9ebc2cSMarc-André Lureau r = pa_stream_drop(p->stream); 19149ddd7e1SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r == 0, unlock_and_fail, 19249ddd7e1SKővágó, Zoltán "pa_stream_drop failed\n"); 193ea9ebc2cSMarc-André Lureau } 194acc3b63eSVolker Rümelin } 195ea9ebc2cSMarc-André Lureau 1967c9eb86eSVolker Rümelin unlock: 1979d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 198acc3b63eSVolker Rümelin return total; 199ea9ebc2cSMarc-André Lureau 200ea9ebc2cSMarc-André Lureau unlock_and_fail: 2019d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 20249ddd7e1SKővágó, Zoltán return 0; 203ea9ebc2cSMarc-André Lureau } 204ea9ebc2cSMarc-André Lureau 205337e8de6SKővágó, Zoltán static void *qpa_get_buffer_out(HWVoiceOut *hw, size_t *size) 206337e8de6SKővágó, Zoltán { 207337e8de6SKővágó, Zoltán PAVoiceOut *p = (PAVoiceOut *) hw; 208337e8de6SKővágó, Zoltán PAConnection *c = p->g->conn; 209337e8de6SKővágó, Zoltán void *ret; 210bea29e9fSVolker Rümelin size_t l; 211337e8de6SKővágó, Zoltán int r; 212337e8de6SKővágó, Zoltán 213337e8de6SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 214337e8de6SKővágó, Zoltán 215337e8de6SKővágó, Zoltán CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, 216337e8de6SKővágó, Zoltán "pa_threaded_mainloop_lock failed\n"); 2177007cd3fSVolker Rümelin if (pa_stream_get_state(p->stream) != PA_STREAM_READY) { 2187007cd3fSVolker Rümelin /* wait for stream to become ready */ 2197007cd3fSVolker Rümelin l = 0; 2207007cd3fSVolker Rümelin ret = NULL; 2217007cd3fSVolker Rümelin goto unlock; 2227007cd3fSVolker Rümelin } 223337e8de6SKővágó, Zoltán 224bea29e9fSVolker Rümelin l = pa_stream_writable_size(p->stream); 225bea29e9fSVolker Rümelin CHECK_SUCCESS_GOTO(c, l != (size_t) -1, unlock_and_fail, 226bea29e9fSVolker Rümelin "pa_stream_writable_size failed\n"); 227bea29e9fSVolker Rümelin 228337e8de6SKővágó, Zoltán *size = -1; 229337e8de6SKővágó, Zoltán r = pa_stream_begin_write(p->stream, &ret, size); 230337e8de6SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r >= 0, unlock_and_fail, 231337e8de6SKővágó, Zoltán "pa_stream_begin_write failed\n"); 232337e8de6SKővágó, Zoltán 2337007cd3fSVolker Rümelin unlock: 234337e8de6SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 235bea29e9fSVolker Rümelin if (*size > l) { 236bea29e9fSVolker Rümelin *size = l; 237bea29e9fSVolker Rümelin } 238337e8de6SKővágó, Zoltán return ret; 239337e8de6SKővágó, Zoltán 240337e8de6SKővágó, Zoltán unlock_and_fail: 241337e8de6SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 242337e8de6SKővágó, Zoltán *size = 0; 243337e8de6SKővágó, Zoltán return NULL; 244337e8de6SKővágó, Zoltán } 245337e8de6SKővágó, Zoltán 246bea29e9fSVolker Rümelin static size_t qpa_put_buffer_out(HWVoiceOut *hw, void *data, size_t length) 247bea29e9fSVolker Rümelin { 248bea29e9fSVolker Rümelin PAVoiceOut *p = (PAVoiceOut *)hw; 249bea29e9fSVolker Rümelin PAConnection *c = p->g->conn; 250bea29e9fSVolker Rümelin int r; 251bea29e9fSVolker Rümelin 252bea29e9fSVolker Rümelin pa_threaded_mainloop_lock(c->mainloop); 253bea29e9fSVolker Rümelin 254bea29e9fSVolker Rümelin CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, 255bea29e9fSVolker Rümelin "pa_threaded_mainloop_lock failed\n"); 256bea29e9fSVolker Rümelin 257bea29e9fSVolker Rümelin r = pa_stream_write(p->stream, data, length, NULL, 0LL, PA_SEEK_RELATIVE); 258bea29e9fSVolker Rümelin CHECK_SUCCESS_GOTO(c, r >= 0, unlock_and_fail, "pa_stream_write failed\n"); 259bea29e9fSVolker Rümelin 260bea29e9fSVolker Rümelin pa_threaded_mainloop_unlock(c->mainloop); 261bea29e9fSVolker Rümelin return length; 262bea29e9fSVolker Rümelin 263bea29e9fSVolker Rümelin unlock_and_fail: 264bea29e9fSVolker Rümelin pa_threaded_mainloop_unlock(c->mainloop); 265bea29e9fSVolker Rümelin return 0; 266bea29e9fSVolker Rümelin } 267bea29e9fSVolker Rümelin 26849ddd7e1SKővágó, Zoltán static size_t qpa_write(HWVoiceOut *hw, void *data, size_t length) 269ea9ebc2cSMarc-André Lureau { 27049ddd7e1SKővágó, Zoltán PAVoiceOut *p = (PAVoiceOut *) hw; 2719d34e6d8SKővágó, Zoltán PAConnection *c = p->g->conn; 272ea9ebc2cSMarc-André Lureau size_t l; 273ea9ebc2cSMarc-André Lureau int r; 274ea9ebc2cSMarc-André Lureau 27549ddd7e1SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 276ea9ebc2cSMarc-André Lureau 27749ddd7e1SKővágó, Zoltán CHECK_DEAD_GOTO(c, p->stream, unlock_and_fail, 27849ddd7e1SKővágó, Zoltán "pa_threaded_mainloop_lock failed\n"); 279e270c548SVolker Rümelin if (pa_stream_get_state(p->stream) != PA_STREAM_READY) { 280e270c548SVolker Rümelin /* wait for stream to become ready */ 281e270c548SVolker Rümelin l = 0; 282e270c548SVolker Rümelin goto unlock; 283e270c548SVolker Rümelin } 28449ddd7e1SKővágó, Zoltán 28549ddd7e1SKővágó, Zoltán l = pa_stream_writable_size(p->stream); 28649ddd7e1SKővágó, Zoltán 28749ddd7e1SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, l != (size_t) -1, unlock_and_fail, 28849ddd7e1SKővágó, Zoltán "pa_stream_writable_size failed\n"); 289ea9ebc2cSMarc-André Lureau 290ea9ebc2cSMarc-André Lureau if (l > length) { 291ea9ebc2cSMarc-André Lureau l = length; 292ea9ebc2cSMarc-André Lureau } 293ea9ebc2cSMarc-André Lureau 294ea9ebc2cSMarc-André Lureau r = pa_stream_write(p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE); 29549ddd7e1SKővágó, Zoltán CHECK_SUCCESS_GOTO(c, r >= 0, unlock_and_fail, "pa_stream_write failed\n"); 296ea9ebc2cSMarc-André Lureau 297e270c548SVolker Rümelin unlock: 2989d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 29949ddd7e1SKővágó, Zoltán return l; 300ea9ebc2cSMarc-André Lureau 301ea9ebc2cSMarc-André Lureau unlock_and_fail: 3029d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 303b8e59f18Smalc return 0; 304b8e59f18Smalc } 305b8e59f18Smalc 30685bc5852SKővágó, Zoltán static pa_sample_format_t audfmt_to_pa (AudioFormat afmt, int endianness) 307b8e59f18Smalc { 308b8e59f18Smalc int format; 309b8e59f18Smalc 310b8e59f18Smalc switch (afmt) { 31185bc5852SKővágó, Zoltán case AUDIO_FORMAT_S8: 31285bc5852SKővágó, Zoltán case AUDIO_FORMAT_U8: 313b8e59f18Smalc format = PA_SAMPLE_U8; 314b8e59f18Smalc break; 31585bc5852SKővágó, Zoltán case AUDIO_FORMAT_S16: 31685bc5852SKővágó, Zoltán case AUDIO_FORMAT_U16: 317b8e59f18Smalc format = endianness ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE; 318b8e59f18Smalc break; 31985bc5852SKővágó, Zoltán case AUDIO_FORMAT_S32: 32085bc5852SKővágó, Zoltán case AUDIO_FORMAT_U32: 321b8e59f18Smalc format = endianness ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE; 322b8e59f18Smalc break; 323ed2a4a79SKővágó, Zoltán case AUDIO_FORMAT_F32: 324ed2a4a79SKővágó, Zoltán format = endianness ? PA_SAMPLE_FLOAT32BE : PA_SAMPLE_FLOAT32LE; 325ed2a4a79SKővágó, Zoltán break; 326b8e59f18Smalc default: 327b8e59f18Smalc dolog ("Internal logic error: Bad audio format %d\n", afmt); 328b8e59f18Smalc format = PA_SAMPLE_U8; 329b8e59f18Smalc break; 330b8e59f18Smalc } 331b8e59f18Smalc return format; 332b8e59f18Smalc } 333b8e59f18Smalc 33485bc5852SKővágó, Zoltán static AudioFormat pa_to_audfmt (pa_sample_format_t fmt, int *endianness) 335b8e59f18Smalc { 336b8e59f18Smalc switch (fmt) { 337b8e59f18Smalc case PA_SAMPLE_U8: 33885bc5852SKővágó, Zoltán return AUDIO_FORMAT_U8; 339b8e59f18Smalc case PA_SAMPLE_S16BE: 340b8e59f18Smalc *endianness = 1; 34185bc5852SKővágó, Zoltán return AUDIO_FORMAT_S16; 342b8e59f18Smalc case PA_SAMPLE_S16LE: 343b8e59f18Smalc *endianness = 0; 34485bc5852SKővágó, Zoltán return AUDIO_FORMAT_S16; 345b8e59f18Smalc case PA_SAMPLE_S32BE: 346b8e59f18Smalc *endianness = 1; 34785bc5852SKővágó, Zoltán return AUDIO_FORMAT_S32; 348b8e59f18Smalc case PA_SAMPLE_S32LE: 349b8e59f18Smalc *endianness = 0; 35085bc5852SKővágó, Zoltán return AUDIO_FORMAT_S32; 351ed2a4a79SKővágó, Zoltán case PA_SAMPLE_FLOAT32BE: 352ed2a4a79SKővágó, Zoltán *endianness = 1; 353ed2a4a79SKővágó, Zoltán return AUDIO_FORMAT_F32; 354ed2a4a79SKővágó, Zoltán case PA_SAMPLE_FLOAT32LE: 355ed2a4a79SKővágó, Zoltán *endianness = 0; 356ed2a4a79SKővágó, Zoltán return AUDIO_FORMAT_F32; 357b8e59f18Smalc default: 358b8e59f18Smalc dolog ("Internal logic error: Bad pa_sample_format %d\n", fmt); 35985bc5852SKővágó, Zoltán return AUDIO_FORMAT_U8; 360b8e59f18Smalc } 361b8e59f18Smalc } 362b8e59f18Smalc 363ea9ebc2cSMarc-André Lureau static void context_state_cb (pa_context *c, void *userdata) 364ea9ebc2cSMarc-André Lureau { 3659d34e6d8SKővágó, Zoltán PAConnection *conn = userdata; 366ea9ebc2cSMarc-André Lureau 367ea9ebc2cSMarc-André Lureau switch (pa_context_get_state(c)) { 368ea9ebc2cSMarc-André Lureau case PA_CONTEXT_READY: 369ea9ebc2cSMarc-André Lureau case PA_CONTEXT_TERMINATED: 370ea9ebc2cSMarc-André Lureau case PA_CONTEXT_FAILED: 3719d34e6d8SKővágó, Zoltán pa_threaded_mainloop_signal(conn->mainloop, 0); 372ea9ebc2cSMarc-André Lureau break; 373ea9ebc2cSMarc-André Lureau 374ea9ebc2cSMarc-André Lureau case PA_CONTEXT_UNCONNECTED: 375ea9ebc2cSMarc-André Lureau case PA_CONTEXT_CONNECTING: 376ea9ebc2cSMarc-André Lureau case PA_CONTEXT_AUTHORIZING: 377ea9ebc2cSMarc-André Lureau case PA_CONTEXT_SETTING_NAME: 378ea9ebc2cSMarc-André Lureau break; 379ea9ebc2cSMarc-André Lureau } 380ea9ebc2cSMarc-André Lureau } 381ea9ebc2cSMarc-André Lureau 382ea9ebc2cSMarc-André Lureau static void stream_state_cb (pa_stream *s, void * userdata) 383ea9ebc2cSMarc-André Lureau { 3849d34e6d8SKővágó, Zoltán PAConnection *c = userdata; 385ea9ebc2cSMarc-André Lureau 386ea9ebc2cSMarc-André Lureau switch (pa_stream_get_state (s)) { 387ea9ebc2cSMarc-André Lureau 388ea9ebc2cSMarc-André Lureau case PA_STREAM_READY: 389ea9ebc2cSMarc-André Lureau case PA_STREAM_FAILED: 390ea9ebc2cSMarc-André Lureau case PA_STREAM_TERMINATED: 3919d34e6d8SKővágó, Zoltán pa_threaded_mainloop_signal(c->mainloop, 0); 392ea9ebc2cSMarc-André Lureau break; 393ea9ebc2cSMarc-André Lureau 394ea9ebc2cSMarc-André Lureau case PA_STREAM_UNCONNECTED: 395ea9ebc2cSMarc-André Lureau case PA_STREAM_CREATING: 396ea9ebc2cSMarc-André Lureau break; 397ea9ebc2cSMarc-André Lureau } 398ea9ebc2cSMarc-André Lureau } 399ea9ebc2cSMarc-André Lureau 400ea9ebc2cSMarc-André Lureau static pa_stream *qpa_simple_new ( 4019d34e6d8SKővágó, Zoltán PAConnection *c, 402ea9ebc2cSMarc-André Lureau const char *name, 403ea9ebc2cSMarc-André Lureau pa_stream_direction_t dir, 404ea9ebc2cSMarc-André Lureau const char *dev, 405ea9ebc2cSMarc-André Lureau const pa_sample_spec *ss, 406ea9ebc2cSMarc-André Lureau const pa_buffer_attr *attr, 407ea9ebc2cSMarc-André Lureau int *rerror) 408ea9ebc2cSMarc-André Lureau { 409ea9ebc2cSMarc-André Lureau int r; 4100cf13e36SKővágó, Zoltán pa_stream *stream = NULL; 4119d34e6d8SKővágó, Zoltán pa_stream_flags_t flags; 4120cf13e36SKővágó, Zoltán pa_channel_map map; 413ea9ebc2cSMarc-André Lureau 4149d34e6d8SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 415ea9ebc2cSMarc-André Lureau 4160cf13e36SKővágó, Zoltán pa_channel_map_init(&map); 4170cf13e36SKővágó, Zoltán map.channels = ss->channels; 4180cf13e36SKővágó, Zoltán 4190cf13e36SKővágó, Zoltán /* 4200cf13e36SKővágó, Zoltán * TODO: This currently expects the only frontend supporting more than 2 4210cf13e36SKővágó, Zoltán * channels is the usb-audio. We will need some means to set channel 4220cf13e36SKővágó, Zoltán * order when a new frontend gains multi-channel support. 4230cf13e36SKővágó, Zoltán */ 4240cf13e36SKővágó, Zoltán switch (ss->channels) { 4250cf13e36SKővágó, Zoltán case 1: 4260cf13e36SKővágó, Zoltán map.map[0] = PA_CHANNEL_POSITION_MONO; 4270cf13e36SKővágó, Zoltán break; 4280cf13e36SKővágó, Zoltán 4290cf13e36SKővágó, Zoltán case 2: 4300cf13e36SKővágó, Zoltán map.map[0] = PA_CHANNEL_POSITION_LEFT; 4310cf13e36SKővágó, Zoltán map.map[1] = PA_CHANNEL_POSITION_RIGHT; 4320cf13e36SKővágó, Zoltán break; 4330cf13e36SKővágó, Zoltán 4340cf13e36SKővágó, Zoltán case 6: 4350cf13e36SKővágó, Zoltán map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT; 4360cf13e36SKővágó, Zoltán map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT; 4370cf13e36SKővágó, Zoltán map.map[2] = PA_CHANNEL_POSITION_CENTER; 4380cf13e36SKővágó, Zoltán map.map[3] = PA_CHANNEL_POSITION_LFE; 4390cf13e36SKővágó, Zoltán map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT; 4400cf13e36SKővágó, Zoltán map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT; 4410cf13e36SKővágó, Zoltán break; 4420cf13e36SKővágó, Zoltán 4430cf13e36SKővágó, Zoltán case 8: 4440cf13e36SKővágó, Zoltán map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT; 4450cf13e36SKővágó, Zoltán map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT; 4460cf13e36SKővágó, Zoltán map.map[2] = PA_CHANNEL_POSITION_CENTER; 4470cf13e36SKővágó, Zoltán map.map[3] = PA_CHANNEL_POSITION_LFE; 4480cf13e36SKővágó, Zoltán map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT; 4490cf13e36SKővágó, Zoltán map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT; 4500cf13e36SKővágó, Zoltán map.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT; 4510cf13e36SKővágó, Zoltán map.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT; 45256089565SPaolo Bonzini break; 4530cf13e36SKővágó, Zoltán 4540cf13e36SKővágó, Zoltán default: 4550cf13e36SKővágó, Zoltán dolog("Internal error: unsupported channel count %d\n", ss->channels); 4560cf13e36SKővágó, Zoltán goto fail; 4570cf13e36SKővágó, Zoltán } 4580cf13e36SKővágó, Zoltán 4590cf13e36SKővágó, Zoltán stream = pa_stream_new(c->context, name, ss, &map); 460ea9ebc2cSMarc-André Lureau if (!stream) { 461ea9ebc2cSMarc-André Lureau goto fail; 462ea9ebc2cSMarc-André Lureau } 463ea9ebc2cSMarc-André Lureau 4649d34e6d8SKővágó, Zoltán pa_stream_set_state_callback(stream, stream_state_cb, c); 4659d34e6d8SKővágó, Zoltán 4669d34e6d8SKővágó, Zoltán flags = 4679d34e6d8SKővágó, Zoltán PA_STREAM_INTERPOLATE_TIMING 46810d5e750SKővágó, Zoltán | PA_STREAM_AUTO_TIMING_UPDATE 46910d5e750SKővágó, Zoltán | PA_STREAM_EARLY_REQUESTS; 470ea9ebc2cSMarc-André Lureau 4718a435f74SKővágó, Zoltán if (dev) { 4728a435f74SKővágó, Zoltán /* don't move the stream if the user specified a sink/source */ 4738a435f74SKővágó, Zoltán flags |= PA_STREAM_DONT_MOVE; 4748a435f74SKővágó, Zoltán } 4758a435f74SKővágó, Zoltán 476ea9ebc2cSMarc-André Lureau if (dir == PA_STREAM_PLAYBACK) { 4779d34e6d8SKővágó, Zoltán r = pa_stream_connect_playback(stream, dev, attr, flags, NULL, NULL); 478ea9ebc2cSMarc-André Lureau } else { 4799d34e6d8SKővágó, Zoltán r = pa_stream_connect_record(stream, dev, attr, flags); 480ea9ebc2cSMarc-André Lureau } 481ea9ebc2cSMarc-André Lureau 482ea9ebc2cSMarc-André Lureau if (r < 0) { 483ea9ebc2cSMarc-André Lureau goto fail; 484ea9ebc2cSMarc-André Lureau } 485ea9ebc2cSMarc-André Lureau 4869d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 487ea9ebc2cSMarc-André Lureau 488ea9ebc2cSMarc-André Lureau return stream; 489ea9ebc2cSMarc-André Lureau 490ea9ebc2cSMarc-André Lureau fail: 4919d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 492ea9ebc2cSMarc-André Lureau 493ea9ebc2cSMarc-André Lureau if (stream) { 494ea9ebc2cSMarc-André Lureau pa_stream_unref (stream); 495ea9ebc2cSMarc-André Lureau } 496ea9ebc2cSMarc-André Lureau 4979d34e6d8SKővágó, Zoltán *rerror = pa_context_errno(c->context); 498ea9ebc2cSMarc-André Lureau 499ea9ebc2cSMarc-André Lureau return NULL; 500ea9ebc2cSMarc-André Lureau } 501ea9ebc2cSMarc-André Lureau 5025706db1dSKővágó, Zoltán static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as, 5035706db1dSKővágó, Zoltán void *drv_opaque) 504b8e59f18Smalc { 505b8e59f18Smalc int error; 5069a644c4bSKővágó, Zoltán pa_sample_spec ss; 5079a644c4bSKővágó, Zoltán pa_buffer_attr ba; 5081ea879e5Smalc struct audsettings obt_as = *as; 509b8e59f18Smalc PAVoiceOut *pa = (PAVoiceOut *) hw; 5109a644c4bSKővágó, Zoltán paaudio *g = pa->g = drv_opaque; 5112c324b28SKővágó, Zoltán AudiodevPaOptions *popts = &g->dev->u.pa; 5122c324b28SKővágó, Zoltán AudiodevPaPerDirectionOptions *ppdo = popts->out; 5139d34e6d8SKővágó, Zoltán PAConnection *c = g->conn; 514b8e59f18Smalc 515b8e59f18Smalc ss.format = audfmt_to_pa (as->fmt, as->endianness); 516b8e59f18Smalc ss.channels = as->nchannels; 517b8e59f18Smalc ss.rate = as->freq; 518b8e59f18Smalc 519f6142777SMartin Schrodt ba.tlength = pa_usec_to_bytes(ppdo->latency, &ss); 520*00413ed9SVolker Rümelin ba.minreq = pa_usec_to_bytes(MIN(ppdo->latency >> 2, 521*00413ed9SVolker Rümelin (g->dev->timer_period >> 2) * 3), &ss); 522e6d16fa4SGerd Hoffmann ba.maxlength = -1; 523e6d16fa4SGerd Hoffmann ba.prebuf = -1; 524e6d16fa4SGerd Hoffmann 525b8e59f18Smalc obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); 526b8e59f18Smalc 527ea9ebc2cSMarc-André Lureau pa->stream = qpa_simple_new ( 5289d34e6d8SKővágó, Zoltán c, 529f47dffe8SKővágó, Zoltán ppdo->has_stream_name ? ppdo->stream_name : g->dev->id, 530b8e59f18Smalc PA_STREAM_PLAYBACK, 5312c324b28SKővágó, Zoltán ppdo->has_name ? ppdo->name : NULL, 532b8e59f18Smalc &ss, 533e6d16fa4SGerd Hoffmann &ba, /* buffering attributes */ 534b8e59f18Smalc &error 535b8e59f18Smalc ); 536ea9ebc2cSMarc-André Lureau if (!pa->stream) { 537b8e59f18Smalc qpa_logerr (error, "pa_simple_new for playback failed\n"); 538b8e59f18Smalc goto fail1; 539b8e59f18Smalc } 540b8e59f18Smalc 541b8e59f18Smalc audio_pcm_init_info (&hw->info, &obt_as); 542cffd2fdfSVolker Rümelin /* 543cffd2fdfSVolker Rümelin * This is wrong. hw->samples counts in frames. hw->samples will be 544cffd2fdfSVolker Rümelin * number of channels times larger than expected. 545cffd2fdfSVolker Rümelin */ 546a76e6b87SVolker Rümelin hw->samples = audio_buffer_samples( 547521ce714SVolker Rümelin qapi_AudiodevPaPerDirectionOptions_base(ppdo), &obt_as, 46440); 548b8e59f18Smalc 549b8e59f18Smalc return 0; 550b8e59f18Smalc 551b8e59f18Smalc fail1: 552b8e59f18Smalc return -1; 553b8e59f18Smalc } 554b8e59f18Smalc 5555706db1dSKővágó, Zoltán static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) 556b8e59f18Smalc { 557b8e59f18Smalc int error; 5589a644c4bSKővágó, Zoltán pa_sample_spec ss; 559ade10301SMartin Schrodt pa_buffer_attr ba; 5601ea879e5Smalc struct audsettings obt_as = *as; 561b8e59f18Smalc PAVoiceIn *pa = (PAVoiceIn *) hw; 5629a644c4bSKővágó, Zoltán paaudio *g = pa->g = drv_opaque; 5632c324b28SKővágó, Zoltán AudiodevPaOptions *popts = &g->dev->u.pa; 5642c324b28SKővágó, Zoltán AudiodevPaPerDirectionOptions *ppdo = popts->in; 5659d34e6d8SKővágó, Zoltán PAConnection *c = g->conn; 566b8e59f18Smalc 567b8e59f18Smalc ss.format = audfmt_to_pa (as->fmt, as->endianness); 568b8e59f18Smalc ss.channels = as->nchannels; 569b8e59f18Smalc ss.rate = as->freq; 570b8e59f18Smalc 571ade10301SMartin Schrodt ba.fragsize = pa_usec_to_bytes(ppdo->latency, &ss); 57258c15e52SMartin Schrodt ba.maxlength = pa_usec_to_bytes(ppdo->latency * 2, &ss); 573ade10301SMartin Schrodt ba.minreq = -1; 574ade10301SMartin Schrodt ba.prebuf = -1; 575ade10301SMartin Schrodt 576b8e59f18Smalc obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness); 577b8e59f18Smalc 578ea9ebc2cSMarc-André Lureau pa->stream = qpa_simple_new ( 5799d34e6d8SKővágó, Zoltán c, 580f47dffe8SKővágó, Zoltán ppdo->has_stream_name ? ppdo->stream_name : g->dev->id, 581b8e59f18Smalc PA_STREAM_RECORD, 5822c324b28SKővágó, Zoltán ppdo->has_name ? ppdo->name : NULL, 583b8e59f18Smalc &ss, 584ade10301SMartin Schrodt &ba, /* buffering attributes */ 585b8e59f18Smalc &error 586b8e59f18Smalc ); 587ea9ebc2cSMarc-André Lureau if (!pa->stream) { 588b8e59f18Smalc qpa_logerr (error, "pa_simple_new for capture failed\n"); 589b8e59f18Smalc goto fail1; 590b8e59f18Smalc } 591b8e59f18Smalc 592b8e59f18Smalc audio_pcm_init_info (&hw->info, &obt_as); 593cffd2fdfSVolker Rümelin /* 594cffd2fdfSVolker Rümelin * This is wrong. hw->samples counts in frames. hw->samples will be 595cffd2fdfSVolker Rümelin * number of channels times larger than expected. 596cffd2fdfSVolker Rümelin */ 597a76e6b87SVolker Rümelin hw->samples = audio_buffer_samples( 598521ce714SVolker Rümelin qapi_AudiodevPaPerDirectionOptions_base(ppdo), &obt_as, 46440); 599b8e59f18Smalc 600b8e59f18Smalc return 0; 601b8e59f18Smalc 602b8e59f18Smalc fail1: 603b8e59f18Smalc return -1; 604b8e59f18Smalc } 605b8e59f18Smalc 6068692bf7dSKővágó, Zoltán static void qpa_simple_disconnect(PAConnection *c, pa_stream *stream) 6078692bf7dSKővágó, Zoltán { 6088692bf7dSKővágó, Zoltán int err; 6098692bf7dSKővágó, Zoltán 6108692bf7dSKővágó, Zoltán /* 6118692bf7dSKővágó, Zoltán * wait until actually connects. workaround pa bug #247 6128692bf7dSKővágó, Zoltán * https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/247 6138692bf7dSKővágó, Zoltán */ 6148692bf7dSKővágó, Zoltán while (pa_stream_get_state(stream) == PA_STREAM_CREATING) { 6158692bf7dSKővágó, Zoltán pa_threaded_mainloop_wait(c->mainloop); 6168692bf7dSKővágó, Zoltán } 6178692bf7dSKővágó, Zoltán 6188692bf7dSKővágó, Zoltán err = pa_stream_disconnect(stream); 6198692bf7dSKővágó, Zoltán if (err != 0) { 6208692bf7dSKővágó, Zoltán dolog("Failed to disconnect! err=%d\n", err); 6218692bf7dSKővágó, Zoltán } 6228692bf7dSKővágó, Zoltán pa_stream_unref(stream); 6238692bf7dSKővágó, Zoltán } 6248692bf7dSKővágó, Zoltán 625b8e59f18Smalc static void qpa_fini_out (HWVoiceOut *hw) 626b8e59f18Smalc { 627b8e59f18Smalc PAVoiceOut *pa = (PAVoiceOut *) hw; 628b8e59f18Smalc 629ea9ebc2cSMarc-André Lureau if (pa->stream) { 6304db3e634SVolker Rümelin PAConnection *c = pa->g->conn; 6314db3e634SVolker Rümelin 6324db3e634SVolker Rümelin pa_threaded_mainloop_lock(c->mainloop); 6334db3e634SVolker Rümelin qpa_simple_disconnect(c, pa->stream); 634ea9ebc2cSMarc-André Lureau pa->stream = NULL; 6354db3e634SVolker Rümelin pa_threaded_mainloop_unlock(c->mainloop); 636b8e59f18Smalc } 637b8e59f18Smalc } 638b8e59f18Smalc 639b8e59f18Smalc static void qpa_fini_in (HWVoiceIn *hw) 640b8e59f18Smalc { 641b8e59f18Smalc PAVoiceIn *pa = (PAVoiceIn *) hw; 642b8e59f18Smalc 643ea9ebc2cSMarc-André Lureau if (pa->stream) { 6444db3e634SVolker Rümelin PAConnection *c = pa->g->conn; 6454db3e634SVolker Rümelin 6464db3e634SVolker Rümelin pa_threaded_mainloop_lock(c->mainloop); 6474db3e634SVolker Rümelin if (pa->read_length) { 6484db3e634SVolker Rümelin int r = pa_stream_drop(pa->stream); 6494db3e634SVolker Rümelin if (r) { 6504db3e634SVolker Rümelin qpa_logerr(pa_context_errno(c->context), 6514db3e634SVolker Rümelin "pa_stream_drop failed\n"); 6524db3e634SVolker Rümelin } 6534db3e634SVolker Rümelin pa->read_length = 0; 6544db3e634SVolker Rümelin } 6554db3e634SVolker Rümelin qpa_simple_disconnect(c, pa->stream); 656ea9ebc2cSMarc-André Lureau pa->stream = NULL; 6574db3e634SVolker Rümelin pa_threaded_mainloop_unlock(c->mainloop); 658b8e59f18Smalc } 659b8e59f18Smalc } 660b8e59f18Smalc 661cecc1e79SKővágó, Zoltán static void qpa_volume_out(HWVoiceOut *hw, Volume *vol) 662b8e59f18Smalc { 6636e7a7f3dSMarc-André Lureau PAVoiceOut *pa = (PAVoiceOut *) hw; 6646e7a7f3dSMarc-André Lureau pa_operation *op; 6656e7a7f3dSMarc-André Lureau pa_cvolume v; 6669d34e6d8SKővágó, Zoltán PAConnection *c = pa->g->conn; 667cecc1e79SKővágó, Zoltán int i; 6686e7a7f3dSMarc-André Lureau 6698f473dd1SGerd Hoffmann #ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ 6708f473dd1SGerd Hoffmann pa_cvolume_init (&v); /* function is present in 0.9.13+ */ 6718f473dd1SGerd Hoffmann #endif 6726e7a7f3dSMarc-André Lureau 673cecc1e79SKővágó, Zoltán v.channels = vol->channels; 674cecc1e79SKővágó, Zoltán for (i = 0; i < vol->channels; ++i) { 675cecc1e79SKővágó, Zoltán v.values[i] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->vol[i]) / 255; 676cecc1e79SKővágó, Zoltán } 6776e7a7f3dSMarc-André Lureau 6789d34e6d8SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 6796e7a7f3dSMarc-André Lureau 6809d34e6d8SKővágó, Zoltán op = pa_context_set_sink_input_volume(c->context, 6816e7a7f3dSMarc-André Lureau pa_stream_get_index(pa->stream), 6826e7a7f3dSMarc-André Lureau &v, NULL, NULL); 6839d34e6d8SKővágó, Zoltán if (!op) { 6849d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 6856e7a7f3dSMarc-André Lureau "set_sink_input_volume() failed\n"); 6869d34e6d8SKővágó, Zoltán } else { 6876e7a7f3dSMarc-André Lureau pa_operation_unref(op); 6889d34e6d8SKővágó, Zoltán } 6896e7a7f3dSMarc-André Lureau 6909d34e6d8SKővágó, Zoltán op = pa_context_set_sink_input_mute(c->context, 6916e7a7f3dSMarc-André Lureau pa_stream_get_index(pa->stream), 692571a8c52SKővágó, Zoltán vol->mute, NULL, NULL); 6936e7a7f3dSMarc-André Lureau if (!op) { 6949d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 6956e7a7f3dSMarc-André Lureau "set_sink_input_mute() failed\n"); 6966e7a7f3dSMarc-André Lureau } else { 6976e7a7f3dSMarc-André Lureau pa_operation_unref(op); 6986e7a7f3dSMarc-André Lureau } 6996e7a7f3dSMarc-André Lureau 7009d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 7016e7a7f3dSMarc-André Lureau } 702b8e59f18Smalc 703cecc1e79SKővágó, Zoltán static void qpa_volume_in(HWVoiceIn *hw, Volume *vol) 704b8e59f18Smalc { 7056e7a7f3dSMarc-André Lureau PAVoiceIn *pa = (PAVoiceIn *) hw; 7066e7a7f3dSMarc-André Lureau pa_operation *op; 7076e7a7f3dSMarc-André Lureau pa_cvolume v; 7089d34e6d8SKővágó, Zoltán PAConnection *c = pa->g->conn; 709cecc1e79SKővágó, Zoltán int i; 7106e7a7f3dSMarc-André Lureau 7118f473dd1SGerd Hoffmann #ifdef PA_CHECK_VERSION 7126e7a7f3dSMarc-André Lureau pa_cvolume_init (&v); 7138f473dd1SGerd Hoffmann #endif 7146e7a7f3dSMarc-André Lureau 715cecc1e79SKővágó, Zoltán v.channels = vol->channels; 716cecc1e79SKővágó, Zoltán for (i = 0; i < vol->channels; ++i) { 717cecc1e79SKővágó, Zoltán v.values[i] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * vol->vol[i]) / 255; 718cecc1e79SKővágó, Zoltán } 7196e7a7f3dSMarc-André Lureau 7209d34e6d8SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 7216e7a7f3dSMarc-André Lureau 7229d34e6d8SKővágó, Zoltán op = pa_context_set_source_output_volume(c->context, 723e58ff62dSPeter Krempa pa_stream_get_index(pa->stream), 7246e7a7f3dSMarc-André Lureau &v, NULL, NULL); 7256e7a7f3dSMarc-André Lureau if (!op) { 7269d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 727e58ff62dSPeter Krempa "set_source_output_volume() failed\n"); 7286e7a7f3dSMarc-André Lureau } else { 7296e7a7f3dSMarc-André Lureau pa_operation_unref(op); 7306e7a7f3dSMarc-André Lureau } 7316e7a7f3dSMarc-André Lureau 7329d34e6d8SKővágó, Zoltán op = pa_context_set_source_output_mute(c->context, 7336e7a7f3dSMarc-André Lureau pa_stream_get_index(pa->stream), 734571a8c52SKővágó, Zoltán vol->mute, NULL, NULL); 7356e7a7f3dSMarc-André Lureau if (!op) { 7369d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 737e58ff62dSPeter Krempa "set_source_output_mute() failed\n"); 7386e7a7f3dSMarc-André Lureau } else { 7396e7a7f3dSMarc-André Lureau pa_operation_unref(op); 7406e7a7f3dSMarc-André Lureau } 7416e7a7f3dSMarc-André Lureau 7429d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 7436e7a7f3dSMarc-André Lureau } 744b8e59f18Smalc 745baea032eSMartin Schrodt static int qpa_validate_per_direction_opts(Audiodev *dev, 746baea032eSMartin Schrodt AudiodevPaPerDirectionOptions *pdo) 747baea032eSMartin Schrodt { 748f6142777SMartin Schrodt if (!pdo->has_latency) { 749f6142777SMartin Schrodt pdo->has_latency = true; 750f6142777SMartin Schrodt pdo->latency = 15000; 751f6142777SMartin Schrodt } 752baea032eSMartin Schrodt return 1; 753baea032eSMartin Schrodt } 754baea032eSMartin Schrodt 7559d34e6d8SKővágó, Zoltán /* common */ 7569d34e6d8SKővágó, Zoltán static void *qpa_conn_init(const char *server) 7579d34e6d8SKővágó, Zoltán { 7583443ad4eSKővágó, Zoltán const char *vm_name; 7599d34e6d8SKővágó, Zoltán PAConnection *c = g_malloc0(sizeof(PAConnection)); 7609d34e6d8SKővágó, Zoltán QTAILQ_INSERT_TAIL(&pa_conns, c, list); 7619d34e6d8SKővágó, Zoltán 7629d34e6d8SKővágó, Zoltán c->mainloop = pa_threaded_mainloop_new(); 7639d34e6d8SKővágó, Zoltán if (!c->mainloop) { 7649d34e6d8SKővágó, Zoltán goto fail; 7659d34e6d8SKővágó, Zoltán } 7669d34e6d8SKővágó, Zoltán 7673443ad4eSKővágó, Zoltán vm_name = qemu_get_vm_name(); 7689d34e6d8SKővágó, Zoltán c->context = pa_context_new(pa_threaded_mainloop_get_api(c->mainloop), 7693443ad4eSKővágó, Zoltán vm_name ? vm_name : "qemu"); 7709d34e6d8SKővágó, Zoltán if (!c->context) { 7719d34e6d8SKővágó, Zoltán goto fail; 7729d34e6d8SKővágó, Zoltán } 7739d34e6d8SKővágó, Zoltán 7749d34e6d8SKővágó, Zoltán pa_context_set_state_callback(c->context, context_state_cb, c); 7759d34e6d8SKővágó, Zoltán 7769d34e6d8SKővágó, Zoltán if (pa_context_connect(c->context, server, 0, NULL) < 0) { 7779d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 7789d34e6d8SKővágó, Zoltán "pa_context_connect() failed\n"); 7799d34e6d8SKővágó, Zoltán goto fail; 7809d34e6d8SKővágó, Zoltán } 7819d34e6d8SKővágó, Zoltán 7829d34e6d8SKővágó, Zoltán pa_threaded_mainloop_lock(c->mainloop); 7839d34e6d8SKővágó, Zoltán 7849d34e6d8SKővágó, Zoltán if (pa_threaded_mainloop_start(c->mainloop) < 0) { 7859d34e6d8SKővágó, Zoltán goto unlock_and_fail; 7869d34e6d8SKővágó, Zoltán } 7879d34e6d8SKővágó, Zoltán 7889d34e6d8SKővágó, Zoltán for (;;) { 7899d34e6d8SKővágó, Zoltán pa_context_state_t state; 7909d34e6d8SKővágó, Zoltán 7919d34e6d8SKővágó, Zoltán state = pa_context_get_state(c->context); 7929d34e6d8SKővágó, Zoltán 7939d34e6d8SKővágó, Zoltán if (state == PA_CONTEXT_READY) { 7949d34e6d8SKővágó, Zoltán break; 7959d34e6d8SKővágó, Zoltán } 7969d34e6d8SKővágó, Zoltán 7979d34e6d8SKővágó, Zoltán if (!PA_CONTEXT_IS_GOOD(state)) { 7989d34e6d8SKővágó, Zoltán qpa_logerr(pa_context_errno(c->context), 7999d34e6d8SKővágó, Zoltán "Wrong context state\n"); 8009d34e6d8SKővágó, Zoltán goto unlock_and_fail; 8019d34e6d8SKővágó, Zoltán } 8029d34e6d8SKővágó, Zoltán 8039d34e6d8SKővágó, Zoltán /* Wait until the context is ready */ 8049d34e6d8SKővágó, Zoltán pa_threaded_mainloop_wait(c->mainloop); 8059d34e6d8SKővágó, Zoltán } 8069d34e6d8SKővágó, Zoltán 8079d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 8089d34e6d8SKővágó, Zoltán return c; 8099d34e6d8SKővágó, Zoltán 8109d34e6d8SKővágó, Zoltán unlock_and_fail: 8119d34e6d8SKővágó, Zoltán pa_threaded_mainloop_unlock(c->mainloop); 8129d34e6d8SKővágó, Zoltán fail: 8139d34e6d8SKővágó, Zoltán AUD_log (AUDIO_CAP, "Failed to initialize PA context"); 8149d34e6d8SKővágó, Zoltán qpa_conn_fini(c); 8159d34e6d8SKővágó, Zoltán return NULL; 8169d34e6d8SKővágó, Zoltán } 8179d34e6d8SKővágó, Zoltán 81871830221SKővágó, Zoltán static void *qpa_audio_init(Audiodev *dev) 819b8e59f18Smalc { 8202c324b28SKővágó, Zoltán paaudio *g; 8212c324b28SKővágó, Zoltán AudiodevPaOptions *popts = &dev->u.pa; 8222c324b28SKővágó, Zoltán const char *server; 8239d34e6d8SKővágó, Zoltán PAConnection *c; 8249d34e6d8SKővágó, Zoltán 8259d34e6d8SKővágó, Zoltán assert(dev->driver == AUDIODEV_DRIVER_PA); 8262c324b28SKővágó, Zoltán 8272c324b28SKővágó, Zoltán if (!popts->has_server) { 828d175505bSGerd Hoffmann char pidfile[64]; 829d175505bSGerd Hoffmann char *runtime; 830d175505bSGerd Hoffmann struct stat st; 831d175505bSGerd Hoffmann 832d175505bSGerd Hoffmann runtime = getenv("XDG_RUNTIME_DIR"); 833d175505bSGerd Hoffmann if (!runtime) { 834d175505bSGerd Hoffmann return NULL; 835d175505bSGerd Hoffmann } 836d175505bSGerd Hoffmann snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime); 837d175505bSGerd Hoffmann if (stat(pidfile, &st) != 0) { 838d175505bSGerd Hoffmann return NULL; 839d175505bSGerd Hoffmann } 840d175505bSGerd Hoffmann } 841d175505bSGerd Hoffmann 842baea032eSMartin Schrodt if (!qpa_validate_per_direction_opts(dev, popts->in)) { 8439d34e6d8SKővágó, Zoltán return NULL; 844baea032eSMartin Schrodt } 845baea032eSMartin Schrodt if (!qpa_validate_per_direction_opts(dev, popts->out)) { 8469d34e6d8SKővágó, Zoltán return NULL; 847baea032eSMartin Schrodt } 848baea032eSMartin Schrodt 8499d34e6d8SKővágó, Zoltán g = g_malloc0(sizeof(paaudio)); 8509d34e6d8SKővágó, Zoltán server = popts->has_server ? popts->server : NULL; 8519d34e6d8SKővágó, Zoltán 8522c324b28SKővágó, Zoltán g->dev = dev; 853ea9ebc2cSMarc-André Lureau 8549d34e6d8SKővágó, Zoltán QTAILQ_FOREACH(c, &pa_conns, list) { 8559d34e6d8SKővágó, Zoltán if (server == NULL || c->server == NULL ? 8569d34e6d8SKővágó, Zoltán server == c->server : 8579d34e6d8SKővágó, Zoltán strcmp(server, c->server) == 0) { 8589d34e6d8SKővágó, Zoltán g->conn = c; 859ea9ebc2cSMarc-André Lureau break; 860ea9ebc2cSMarc-André Lureau } 861ea9ebc2cSMarc-André Lureau } 8629d34e6d8SKővágó, Zoltán if (!g->conn) { 8639d34e6d8SKővágó, Zoltán g->conn = qpa_conn_init(server); 864ea9ebc2cSMarc-André Lureau } 8659d34e6d8SKővágó, Zoltán if (!g->conn) { 8669d34e6d8SKővágó, Zoltán g_free(g); 867ea9ebc2cSMarc-André Lureau return NULL; 868b8e59f18Smalc } 869b8e59f18Smalc 8709d34e6d8SKővágó, Zoltán ++g->conn->refcount; 8719d34e6d8SKővágó, Zoltán return g; 8729d34e6d8SKővágó, Zoltán } 8739d34e6d8SKővágó, Zoltán 8749d34e6d8SKővágó, Zoltán static void qpa_conn_fini(PAConnection *c) 8759d34e6d8SKővágó, Zoltán { 8769d34e6d8SKővágó, Zoltán if (c->mainloop) { 8779d34e6d8SKővágó, Zoltán pa_threaded_mainloop_stop(c->mainloop); 8789d34e6d8SKővágó, Zoltán } 8799d34e6d8SKővágó, Zoltán 8809d34e6d8SKővágó, Zoltán if (c->context) { 8819d34e6d8SKővágó, Zoltán pa_context_disconnect(c->context); 8829d34e6d8SKővágó, Zoltán pa_context_unref(c->context); 8839d34e6d8SKővágó, Zoltán } 8849d34e6d8SKővágó, Zoltán 8859d34e6d8SKővágó, Zoltán if (c->mainloop) { 8869d34e6d8SKővágó, Zoltán pa_threaded_mainloop_free(c->mainloop); 8879d34e6d8SKővágó, Zoltán } 8889d34e6d8SKővágó, Zoltán 8899d34e6d8SKővágó, Zoltán QTAILQ_REMOVE(&pa_conns, c, list); 8909d34e6d8SKővágó, Zoltán g_free(c); 8919d34e6d8SKővágó, Zoltán } 8929d34e6d8SKővágó, Zoltán 893b8e59f18Smalc static void qpa_audio_fini (void *opaque) 894b8e59f18Smalc { 895ea9ebc2cSMarc-André Lureau paaudio *g = opaque; 8969d34e6d8SKővágó, Zoltán PAConnection *c = g->conn; 897ea9ebc2cSMarc-André Lureau 8989d34e6d8SKővágó, Zoltán if (--c->refcount == 0) { 8999d34e6d8SKővágó, Zoltán qpa_conn_fini(c); 900ea9ebc2cSMarc-André Lureau } 901ea9ebc2cSMarc-André Lureau 9029a644c4bSKővágó, Zoltán g_free(g); 903b8e59f18Smalc } 904b8e59f18Smalc 90535f4b58cSblueswir1 static struct audio_pcm_ops qpa_pcm_ops = { 9061dd3e4d1SJuan Quintela .init_out = qpa_init_out, 9071dd3e4d1SJuan Quintela .fini_out = qpa_fini_out, 90849ddd7e1SKővágó, Zoltán .write = qpa_write, 909337e8de6SKővágó, Zoltán .get_buffer_out = qpa_get_buffer_out, 910bea29e9fSVolker Rümelin .put_buffer_out = qpa_put_buffer_out, 911571a8c52SKővágó, Zoltán .volume_out = qpa_volume_out, 9121dd3e4d1SJuan Quintela 9131dd3e4d1SJuan Quintela .init_in = qpa_init_in, 9141dd3e4d1SJuan Quintela .fini_in = qpa_fini_in, 91549ddd7e1SKővágó, Zoltán .read = qpa_read, 916337e8de6SKővágó, Zoltán .get_buffer_in = qpa_get_buffer_in, 917337e8de6SKővágó, Zoltán .put_buffer_in = qpa_put_buffer_in, 918571a8c52SKővágó, Zoltán .volume_in = qpa_volume_in 919b8e59f18Smalc }; 920b8e59f18Smalc 921d3893a39SGerd Hoffmann static struct audio_driver pa_audio_driver = { 922bee37f32SJuan Quintela .name = "pa", 923bee37f32SJuan Quintela .descr = "http://www.pulseaudio.org/", 924bee37f32SJuan Quintela .init = qpa_audio_init, 925bee37f32SJuan Quintela .fini = qpa_audio_fini, 926bee37f32SJuan Quintela .pcm_ops = &qpa_pcm_ops, 9271a4ea1e3SMichael S. Tsirkin .can_be_default = 1, 928bee37f32SJuan Quintela .max_voices_out = INT_MAX, 929bee37f32SJuan Quintela .max_voices_in = INT_MAX, 930bee37f32SJuan Quintela .voice_size_out = sizeof (PAVoiceOut), 9316e7a7f3dSMarc-André Lureau .voice_size_in = sizeof (PAVoiceIn), 932b8e59f18Smalc }; 933d3893a39SGerd Hoffmann 934d3893a39SGerd Hoffmann static void register_audio_pa(void) 935d3893a39SGerd Hoffmann { 936d3893a39SGerd Hoffmann audio_driver_register(&pa_audio_driver); 937d3893a39SGerd Hoffmann } 938d3893a39SGerd Hoffmann type_init(register_audio_pa); 939