pwaudio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * QEMU PipeWire audio driver
  3. *
  4. * Copyright (c) 2023 Red Hat Inc.
  5. *
  6. * Author: Dorinda Bassey <dbassey@redhat.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/module.h"
  12. #include "audio.h"
  13. #include "qemu/error-report.h"
  14. #include "qapi/error.h"
  15. #include <spa/param/audio/format-utils.h>
  16. #include <spa/utils/ringbuffer.h>
  17. #include <spa/utils/result.h>
  18. #include <spa/param/props.h>
  19. #include <pipewire/pipewire.h>
  20. #include "trace.h"
  21. #define AUDIO_CAP "pipewire"
  22. #define RINGBUFFER_SIZE (1u << 22)
  23. #define RINGBUFFER_MASK (RINGBUFFER_SIZE - 1)
  24. #include "audio_int.h"
  25. typedef struct pwvolume {
  26. uint32_t channels;
  27. float values[SPA_AUDIO_MAX_CHANNELS];
  28. } pwvolume;
  29. typedef struct pwaudio {
  30. Audiodev *dev;
  31. struct pw_thread_loop *thread_loop;
  32. struct pw_context *context;
  33. struct pw_core *core;
  34. struct spa_hook core_listener;
  35. int last_seq, pending_seq, error;
  36. } pwaudio;
  37. typedef struct PWVoice {
  38. pwaudio *g;
  39. struct pw_stream *stream;
  40. struct spa_hook stream_listener;
  41. struct spa_audio_info_raw info;
  42. uint32_t highwater_mark;
  43. uint32_t frame_size, req;
  44. struct spa_ringbuffer ring;
  45. uint8_t buffer[RINGBUFFER_SIZE];
  46. pwvolume volume;
  47. bool muted;
  48. } PWVoice;
  49. typedef struct PWVoiceOut {
  50. HWVoiceOut hw;
  51. PWVoice v;
  52. } PWVoiceOut;
  53. typedef struct PWVoiceIn {
  54. HWVoiceIn hw;
  55. PWVoice v;
  56. } PWVoiceIn;
  57. #define PW_VOICE_IN(v) ((PWVoiceIn *)v)
  58. #define PW_VOICE_OUT(v) ((PWVoiceOut *)v)
  59. static void
  60. stream_destroy(void *data)
  61. {
  62. PWVoice *v = (PWVoice *) data;
  63. spa_hook_remove(&v->stream_listener);
  64. v->stream = NULL;
  65. }
  66. /* output data processing function to read stuffs from the buffer */
  67. static void
  68. playback_on_process(void *data)
  69. {
  70. PWVoice *v = data;
  71. void *p;
  72. struct pw_buffer *b;
  73. struct spa_buffer *buf;
  74. uint32_t req, index, n_bytes;
  75. int32_t avail;
  76. assert(v->stream);
  77. /* obtain a buffer to read from */
  78. b = pw_stream_dequeue_buffer(v->stream);
  79. if (b == NULL) {
  80. error_report("out of buffers: %s", strerror(errno));
  81. return;
  82. }
  83. buf = b->buffer;
  84. p = buf->datas[0].data;
  85. if (p == NULL) {
  86. return;
  87. }
  88. /* calculate the total no of bytes to read data from buffer */
  89. req = b->requested * v->frame_size;
  90. if (req == 0) {
  91. req = v->req;
  92. }
  93. n_bytes = SPA_MIN(req, buf->datas[0].maxsize);
  94. /* get no of available bytes to read data from buffer */
  95. avail = spa_ringbuffer_get_read_index(&v->ring, &index);
  96. if (avail <= 0) {
  97. PWVoiceOut *vo = container_of(data, PWVoiceOut, v);
  98. audio_pcm_info_clear_buf(&vo->hw.info, p, n_bytes / v->frame_size);
  99. } else {
  100. if ((uint32_t) avail < n_bytes) {
  101. /*
  102. * PipeWire immediately calls this callback again if we provide
  103. * less than n_bytes. Then audio_pcm_info_clear_buf() fills the
  104. * rest of the buffer with silence.
  105. */
  106. n_bytes = avail;
  107. }
  108. spa_ringbuffer_read_data(&v->ring,
  109. v->buffer, RINGBUFFER_SIZE,
  110. index & RINGBUFFER_MASK, p, n_bytes);
  111. index += n_bytes;
  112. spa_ringbuffer_read_update(&v->ring, index);
  113. }
  114. buf->datas[0].chunk->offset = 0;
  115. buf->datas[0].chunk->stride = v->frame_size;
  116. buf->datas[0].chunk->size = n_bytes;
  117. /* queue the buffer for playback */
  118. pw_stream_queue_buffer(v->stream, b);
  119. }
  120. /* output data processing function to generate stuffs in the buffer */
  121. static void
  122. capture_on_process(void *data)
  123. {
  124. PWVoice *v = (PWVoice *) data;
  125. void *p;
  126. struct pw_buffer *b;
  127. struct spa_buffer *buf;
  128. int32_t filled;
  129. uint32_t index, offs, n_bytes;
  130. assert(v->stream);
  131. /* obtain a buffer */
  132. b = pw_stream_dequeue_buffer(v->stream);
  133. if (b == NULL) {
  134. error_report("out of buffers: %s", strerror(errno));
  135. return;
  136. }
  137. /* Write data into buffer */
  138. buf = b->buffer;
  139. p = buf->datas[0].data;
  140. if (p == NULL) {
  141. return;
  142. }
  143. offs = SPA_MIN(buf->datas[0].chunk->offset, buf->datas[0].maxsize);
  144. n_bytes = SPA_MIN(buf->datas[0].chunk->size, buf->datas[0].maxsize - offs);
  145. filled = spa_ringbuffer_get_write_index(&v->ring, &index);
  146. if (filled < 0) {
  147. error_report("%p: underrun write:%u filled:%d", p, index, filled);
  148. } else {
  149. if ((uint32_t) filled + n_bytes > RINGBUFFER_SIZE) {
  150. error_report("%p: overrun write:%u filled:%d + size:%u > max:%u",
  151. p, index, filled, n_bytes, RINGBUFFER_SIZE);
  152. }
  153. }
  154. spa_ringbuffer_write_data(&v->ring,
  155. v->buffer, RINGBUFFER_SIZE,
  156. index & RINGBUFFER_MASK,
  157. SPA_PTROFF(p, offs, void), n_bytes);
  158. index += n_bytes;
  159. spa_ringbuffer_write_update(&v->ring, index);
  160. /* queue the buffer for playback */
  161. pw_stream_queue_buffer(v->stream, b);
  162. }
  163. static void
  164. on_stream_state_changed(void *data, enum pw_stream_state old,
  165. enum pw_stream_state state, const char *error)
  166. {
  167. PWVoice *v = (PWVoice *) data;
  168. trace_pw_state_changed(pw_stream_get_node_id(v->stream),
  169. pw_stream_state_as_string(state));
  170. }
  171. static const struct pw_stream_events capture_stream_events = {
  172. PW_VERSION_STREAM_EVENTS,
  173. .destroy = stream_destroy,
  174. .state_changed = on_stream_state_changed,
  175. .process = capture_on_process
  176. };
  177. static const struct pw_stream_events playback_stream_events = {
  178. PW_VERSION_STREAM_EVENTS,
  179. .destroy = stream_destroy,
  180. .state_changed = on_stream_state_changed,
  181. .process = playback_on_process
  182. };
  183. static size_t
  184. qpw_read(HWVoiceIn *hw, void *data, size_t len)
  185. {
  186. PWVoiceIn *pw = (PWVoiceIn *) hw;
  187. PWVoice *v = &pw->v;
  188. pwaudio *c = v->g;
  189. const char *error = NULL;
  190. size_t l;
  191. int32_t avail;
  192. uint32_t index;
  193. pw_thread_loop_lock(c->thread_loop);
  194. if (pw_stream_get_state(v->stream, &error) != PW_STREAM_STATE_STREAMING) {
  195. /* wait for stream to become ready */
  196. l = 0;
  197. goto done_unlock;
  198. }
  199. /* get no of available bytes to read data from buffer */
  200. avail = spa_ringbuffer_get_read_index(&v->ring, &index);
  201. trace_pw_read(avail, index, len);
  202. if (avail < (int32_t) len) {
  203. len = avail;
  204. }
  205. spa_ringbuffer_read_data(&v->ring,
  206. v->buffer, RINGBUFFER_SIZE,
  207. index & RINGBUFFER_MASK, data, len);
  208. index += len;
  209. spa_ringbuffer_read_update(&v->ring, index);
  210. l = len;
  211. done_unlock:
  212. pw_thread_loop_unlock(c->thread_loop);
  213. return l;
  214. }
  215. static size_t qpw_buffer_get_free(HWVoiceOut *hw)
  216. {
  217. PWVoiceOut *pw = (PWVoiceOut *)hw;
  218. PWVoice *v = &pw->v;
  219. pwaudio *c = v->g;
  220. const char *error = NULL;
  221. int32_t filled, avail;
  222. uint32_t index;
  223. pw_thread_loop_lock(c->thread_loop);
  224. if (pw_stream_get_state(v->stream, &error) != PW_STREAM_STATE_STREAMING) {
  225. /* wait for stream to become ready */
  226. avail = 0;
  227. goto done_unlock;
  228. }
  229. filled = spa_ringbuffer_get_write_index(&v->ring, &index);
  230. avail = v->highwater_mark - filled;
  231. done_unlock:
  232. pw_thread_loop_unlock(c->thread_loop);
  233. return avail;
  234. }
  235. static size_t
  236. qpw_write(HWVoiceOut *hw, void *data, size_t len)
  237. {
  238. PWVoiceOut *pw = (PWVoiceOut *) hw;
  239. PWVoice *v = &pw->v;
  240. pwaudio *c = v->g;
  241. const char *error = NULL;
  242. int32_t filled, avail;
  243. uint32_t index;
  244. pw_thread_loop_lock(c->thread_loop);
  245. if (pw_stream_get_state(v->stream, &error) != PW_STREAM_STATE_STREAMING) {
  246. /* wait for stream to become ready */
  247. len = 0;
  248. goto done_unlock;
  249. }
  250. filled = spa_ringbuffer_get_write_index(&v->ring, &index);
  251. avail = v->highwater_mark - filled;
  252. trace_pw_write(filled, avail, index, len);
  253. if (len > avail) {
  254. len = avail;
  255. }
  256. if (filled < 0) {
  257. error_report("%p: underrun write:%u filled:%d", pw, index, filled);
  258. } else {
  259. if ((uint32_t) filled + len > RINGBUFFER_SIZE) {
  260. error_report("%p: overrun write:%u filled:%d + size:%zu > max:%u",
  261. pw, index, filled, len, RINGBUFFER_SIZE);
  262. }
  263. }
  264. spa_ringbuffer_write_data(&v->ring,
  265. v->buffer, RINGBUFFER_SIZE,
  266. index & RINGBUFFER_MASK, data, len);
  267. index += len;
  268. spa_ringbuffer_write_update(&v->ring, index);
  269. done_unlock:
  270. pw_thread_loop_unlock(c->thread_loop);
  271. return len;
  272. }
  273. static int
  274. audfmt_to_pw(AudioFormat fmt, int endianness)
  275. {
  276. int format;
  277. switch (fmt) {
  278. case AUDIO_FORMAT_S8:
  279. format = SPA_AUDIO_FORMAT_S8;
  280. break;
  281. case AUDIO_FORMAT_U8:
  282. format = SPA_AUDIO_FORMAT_U8;
  283. break;
  284. case AUDIO_FORMAT_S16:
  285. format = endianness ? SPA_AUDIO_FORMAT_S16_BE : SPA_AUDIO_FORMAT_S16_LE;
  286. break;
  287. case AUDIO_FORMAT_U16:
  288. format = endianness ? SPA_AUDIO_FORMAT_U16_BE : SPA_AUDIO_FORMAT_U16_LE;
  289. break;
  290. case AUDIO_FORMAT_S32:
  291. format = endianness ? SPA_AUDIO_FORMAT_S32_BE : SPA_AUDIO_FORMAT_S32_LE;
  292. break;
  293. case AUDIO_FORMAT_U32:
  294. format = endianness ? SPA_AUDIO_FORMAT_U32_BE : SPA_AUDIO_FORMAT_U32_LE;
  295. break;
  296. case AUDIO_FORMAT_F32:
  297. format = endianness ? SPA_AUDIO_FORMAT_F32_BE : SPA_AUDIO_FORMAT_F32_LE;
  298. break;
  299. default:
  300. dolog("Internal logic error: Bad audio format %d\n", fmt);
  301. format = SPA_AUDIO_FORMAT_U8;
  302. break;
  303. }
  304. return format;
  305. }
  306. static AudioFormat
  307. pw_to_audfmt(enum spa_audio_format fmt, int *endianness,
  308. uint32_t *sample_size)
  309. {
  310. switch (fmt) {
  311. case SPA_AUDIO_FORMAT_S8:
  312. *sample_size = 1;
  313. return AUDIO_FORMAT_S8;
  314. case SPA_AUDIO_FORMAT_U8:
  315. *sample_size = 1;
  316. return AUDIO_FORMAT_U8;
  317. case SPA_AUDIO_FORMAT_S16_BE:
  318. *sample_size = 2;
  319. *endianness = 1;
  320. return AUDIO_FORMAT_S16;
  321. case SPA_AUDIO_FORMAT_S16_LE:
  322. *sample_size = 2;
  323. *endianness = 0;
  324. return AUDIO_FORMAT_S16;
  325. case SPA_AUDIO_FORMAT_U16_BE:
  326. *sample_size = 2;
  327. *endianness = 1;
  328. return AUDIO_FORMAT_U16;
  329. case SPA_AUDIO_FORMAT_U16_LE:
  330. *sample_size = 2;
  331. *endianness = 0;
  332. return AUDIO_FORMAT_U16;
  333. case SPA_AUDIO_FORMAT_S32_BE:
  334. *sample_size = 4;
  335. *endianness = 1;
  336. return AUDIO_FORMAT_S32;
  337. case SPA_AUDIO_FORMAT_S32_LE:
  338. *sample_size = 4;
  339. *endianness = 0;
  340. return AUDIO_FORMAT_S32;
  341. case SPA_AUDIO_FORMAT_U32_BE:
  342. *sample_size = 4;
  343. *endianness = 1;
  344. return AUDIO_FORMAT_U32;
  345. case SPA_AUDIO_FORMAT_U32_LE:
  346. *sample_size = 4;
  347. *endianness = 0;
  348. return AUDIO_FORMAT_U32;
  349. case SPA_AUDIO_FORMAT_F32_BE:
  350. *sample_size = 4;
  351. *endianness = 1;
  352. return AUDIO_FORMAT_F32;
  353. case SPA_AUDIO_FORMAT_F32_LE:
  354. *sample_size = 4;
  355. *endianness = 0;
  356. return AUDIO_FORMAT_F32;
  357. default:
  358. *sample_size = 1;
  359. dolog("Internal logic error: Bad spa_audio_format %d\n", fmt);
  360. return AUDIO_FORMAT_U8;
  361. }
  362. }
  363. static int
  364. qpw_stream_new(pwaudio *c, PWVoice *v, const char *stream_name,
  365. const char *name, enum spa_direction dir)
  366. {
  367. int res;
  368. uint32_t n_params;
  369. const struct spa_pod *params[2];
  370. uint8_t buffer[1024];
  371. struct spa_pod_builder b;
  372. uint64_t buf_samples;
  373. struct pw_properties *props;
  374. props = pw_properties_new(NULL, NULL);
  375. if (!props) {
  376. error_report("Failed to create PW properties: %s", g_strerror(errno));
  377. return -1;
  378. }
  379. /* 75% of the timer period for faster updates */
  380. buf_samples = (uint64_t)v->g->dev->timer_period * v->info.rate
  381. * 3 / 4 / 1000000;
  382. pw_properties_setf(props, PW_KEY_NODE_LATENCY, "%" PRIu64 "/%u",
  383. buf_samples, v->info.rate);
  384. trace_pw_period(buf_samples, v->info.rate);
  385. if (name) {
  386. pw_properties_set(props, PW_KEY_TARGET_OBJECT, name);
  387. }
  388. v->stream = pw_stream_new(c->core, stream_name, props);
  389. if (v->stream == NULL) {
  390. error_report("Failed to create PW stream: %s", g_strerror(errno));
  391. return -1;
  392. }
  393. if (dir == SPA_DIRECTION_INPUT) {
  394. pw_stream_add_listener(v->stream,
  395. &v->stream_listener, &capture_stream_events, v);
  396. } else {
  397. pw_stream_add_listener(v->stream,
  398. &v->stream_listener, &playback_stream_events, v);
  399. }
  400. n_params = 0;
  401. spa_pod_builder_init(&b, buffer, sizeof(buffer));
  402. params[n_params++] = spa_format_audio_raw_build(&b,
  403. SPA_PARAM_EnumFormat,
  404. &v->info);
  405. /* connect the stream to a sink or source */
  406. res = pw_stream_connect(v->stream,
  407. dir ==
  408. SPA_DIRECTION_INPUT ? PW_DIRECTION_INPUT :
  409. PW_DIRECTION_OUTPUT, PW_ID_ANY,
  410. PW_STREAM_FLAG_AUTOCONNECT |
  411. PW_STREAM_FLAG_INACTIVE |
  412. PW_STREAM_FLAG_MAP_BUFFERS |
  413. PW_STREAM_FLAG_RT_PROCESS, params, n_params);
  414. if (res < 0) {
  415. error_report("Failed to connect PW stream: %s", g_strerror(errno));
  416. pw_stream_destroy(v->stream);
  417. return -1;
  418. }
  419. return 0;
  420. }
  421. static void
  422. qpw_set_position(uint32_t channels, uint32_t position[SPA_AUDIO_MAX_CHANNELS])
  423. {
  424. memcpy(position, (uint32_t[SPA_AUDIO_MAX_CHANNELS]) { SPA_AUDIO_CHANNEL_UNKNOWN, },
  425. sizeof(uint32_t) * SPA_AUDIO_MAX_CHANNELS);
  426. /*
  427. * TODO: This currently expects the only frontend supporting more than 2
  428. * channels is the usb-audio. We will need some means to set channel
  429. * order when a new frontend gains multi-channel support.
  430. */
  431. switch (channels) {
  432. case 8:
  433. position[6] = SPA_AUDIO_CHANNEL_SL;
  434. position[7] = SPA_AUDIO_CHANNEL_SR;
  435. /* fallthrough */
  436. case 6:
  437. position[2] = SPA_AUDIO_CHANNEL_FC;
  438. position[3] = SPA_AUDIO_CHANNEL_LFE;
  439. position[4] = SPA_AUDIO_CHANNEL_RL;
  440. position[5] = SPA_AUDIO_CHANNEL_RR;
  441. /* fallthrough */
  442. case 2:
  443. position[0] = SPA_AUDIO_CHANNEL_FL;
  444. position[1] = SPA_AUDIO_CHANNEL_FR;
  445. break;
  446. case 1:
  447. position[0] = SPA_AUDIO_CHANNEL_MONO;
  448. break;
  449. default:
  450. dolog("Internal error: unsupported channel count %d\n", channels);
  451. }
  452. }
  453. static int
  454. qpw_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque)
  455. {
  456. PWVoiceOut *pw = (PWVoiceOut *) hw;
  457. PWVoice *v = &pw->v;
  458. struct audsettings obt_as = *as;
  459. pwaudio *c = v->g = drv_opaque;
  460. AudiodevPipewireOptions *popts = &c->dev->u.pipewire;
  461. AudiodevPipewirePerDirectionOptions *ppdo = popts->out;
  462. int r;
  463. pw_thread_loop_lock(c->thread_loop);
  464. v->info.format = audfmt_to_pw(as->fmt, as->endianness);
  465. v->info.channels = as->nchannels;
  466. qpw_set_position(as->nchannels, v->info.position);
  467. v->info.rate = as->freq;
  468. obt_as.fmt =
  469. pw_to_audfmt(v->info.format, &obt_as.endianness, &v->frame_size);
  470. v->frame_size *= as->nchannels;
  471. v->req = (uint64_t)c->dev->timer_period * v->info.rate
  472. * 1 / 2 / 1000000 * v->frame_size;
  473. /* call the function that creates a new stream for playback */
  474. r = qpw_stream_new(c, v, ppdo->stream_name ? : c->dev->id,
  475. ppdo->name, SPA_DIRECTION_OUTPUT);
  476. if (r < 0) {
  477. pw_thread_loop_unlock(c->thread_loop);
  478. return -1;
  479. }
  480. /* report the audio format we support */
  481. audio_pcm_init_info(&hw->info, &obt_as);
  482. /* report the buffer size to qemu */
  483. hw->samples = audio_buffer_frames(
  484. qapi_AudiodevPipewirePerDirectionOptions_base(ppdo), &obt_as, 46440);
  485. v->highwater_mark = MIN(RINGBUFFER_SIZE,
  486. (ppdo->has_latency ? ppdo->latency : 46440)
  487. * (uint64_t)v->info.rate / 1000000 * v->frame_size);
  488. pw_thread_loop_unlock(c->thread_loop);
  489. return 0;
  490. }
  491. static int
  492. qpw_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
  493. {
  494. PWVoiceIn *pw = (PWVoiceIn *) hw;
  495. PWVoice *v = &pw->v;
  496. struct audsettings obt_as = *as;
  497. pwaudio *c = v->g = drv_opaque;
  498. AudiodevPipewireOptions *popts = &c->dev->u.pipewire;
  499. AudiodevPipewirePerDirectionOptions *ppdo = popts->in;
  500. int r;
  501. pw_thread_loop_lock(c->thread_loop);
  502. v->info.format = audfmt_to_pw(as->fmt, as->endianness);
  503. v->info.channels = as->nchannels;
  504. qpw_set_position(as->nchannels, v->info.position);
  505. v->info.rate = as->freq;
  506. obt_as.fmt =
  507. pw_to_audfmt(v->info.format, &obt_as.endianness, &v->frame_size);
  508. v->frame_size *= as->nchannels;
  509. /* call the function that creates a new stream for recording */
  510. r = qpw_stream_new(c, v, ppdo->stream_name ? : c->dev->id,
  511. ppdo->name, SPA_DIRECTION_INPUT);
  512. if (r < 0) {
  513. pw_thread_loop_unlock(c->thread_loop);
  514. return -1;
  515. }
  516. /* report the audio format we support */
  517. audio_pcm_init_info(&hw->info, &obt_as);
  518. /* report the buffer size to qemu */
  519. hw->samples = audio_buffer_frames(
  520. qapi_AudiodevPipewirePerDirectionOptions_base(ppdo), &obt_as, 46440);
  521. pw_thread_loop_unlock(c->thread_loop);
  522. return 0;
  523. }
  524. static void
  525. qpw_voice_fini(PWVoice *v)
  526. {
  527. pwaudio *c = v->g;
  528. if (!v->stream) {
  529. return;
  530. }
  531. pw_thread_loop_lock(c->thread_loop);
  532. pw_stream_destroy(v->stream);
  533. v->stream = NULL;
  534. pw_thread_loop_unlock(c->thread_loop);
  535. }
  536. static void
  537. qpw_fini_out(HWVoiceOut *hw)
  538. {
  539. qpw_voice_fini(&PW_VOICE_OUT(hw)->v);
  540. }
  541. static void
  542. qpw_fini_in(HWVoiceIn *hw)
  543. {
  544. qpw_voice_fini(&PW_VOICE_IN(hw)->v);
  545. }
  546. static void
  547. qpw_voice_set_enabled(PWVoice *v, bool enable)
  548. {
  549. pwaudio *c = v->g;
  550. pw_thread_loop_lock(c->thread_loop);
  551. pw_stream_set_active(v->stream, enable);
  552. pw_thread_loop_unlock(c->thread_loop);
  553. }
  554. static void
  555. qpw_enable_out(HWVoiceOut *hw, bool enable)
  556. {
  557. qpw_voice_set_enabled(&PW_VOICE_OUT(hw)->v, enable);
  558. }
  559. static void
  560. qpw_enable_in(HWVoiceIn *hw, bool enable)
  561. {
  562. qpw_voice_set_enabled(&PW_VOICE_IN(hw)->v, enable);
  563. }
  564. static void
  565. qpw_voice_set_volume(PWVoice *v, Volume *vol)
  566. {
  567. pwaudio *c = v->g;
  568. int i, ret;
  569. pw_thread_loop_lock(c->thread_loop);
  570. v->volume.channels = vol->channels;
  571. for (i = 0; i < vol->channels; ++i) {
  572. v->volume.values[i] = (float)vol->vol[i] / 255;
  573. }
  574. ret = pw_stream_set_control(v->stream,
  575. SPA_PROP_channelVolumes, v->volume.channels, v->volume.values, 0);
  576. trace_pw_vol(ret == 0 ? "success" : "failed");
  577. v->muted = vol->mute;
  578. float val = v->muted ? 1.f : 0.f;
  579. ret = pw_stream_set_control(v->stream, SPA_PROP_mute, 1, &val, 0);
  580. pw_thread_loop_unlock(c->thread_loop);
  581. }
  582. static void
  583. qpw_volume_out(HWVoiceOut *hw, Volume *vol)
  584. {
  585. qpw_voice_set_volume(&PW_VOICE_OUT(hw)->v, vol);
  586. }
  587. static void
  588. qpw_volume_in(HWVoiceIn *hw, Volume *vol)
  589. {
  590. qpw_voice_set_volume(&PW_VOICE_IN(hw)->v, vol);
  591. }
  592. static int wait_resync(pwaudio *pw)
  593. {
  594. int res;
  595. pw->pending_seq = pw_core_sync(pw->core, PW_ID_CORE, pw->pending_seq);
  596. while (true) {
  597. pw_thread_loop_wait(pw->thread_loop);
  598. res = pw->error;
  599. if (res < 0) {
  600. pw->error = 0;
  601. return res;
  602. }
  603. if (pw->pending_seq == pw->last_seq) {
  604. break;
  605. }
  606. }
  607. return 0;
  608. }
  609. static void
  610. on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
  611. {
  612. pwaudio *pw = data;
  613. error_report("error id:%u seq:%d res:%d (%s): %s",
  614. id, seq, res, spa_strerror(res), message);
  615. /* stop and exit the thread loop */
  616. pw_thread_loop_signal(pw->thread_loop, FALSE);
  617. }
  618. static void
  619. on_core_done(void *data, uint32_t id, int seq)
  620. {
  621. pwaudio *pw = data;
  622. assert(id == PW_ID_CORE);
  623. pw->last_seq = seq;
  624. if (pw->pending_seq == seq) {
  625. /* stop and exit the thread loop */
  626. pw_thread_loop_signal(pw->thread_loop, FALSE);
  627. }
  628. }
  629. static const struct pw_core_events core_events = {
  630. PW_VERSION_CORE_EVENTS,
  631. .done = on_core_done,
  632. .error = on_core_error,
  633. };
  634. static void *
  635. qpw_audio_init(Audiodev *dev, Error **errp)
  636. {
  637. g_autofree pwaudio *pw = g_new0(pwaudio, 1);
  638. assert(dev->driver == AUDIODEV_DRIVER_PIPEWIRE);
  639. trace_pw_audio_init();
  640. pw_init(NULL, NULL);
  641. pw->dev = dev;
  642. pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  643. if (pw->thread_loop == NULL) {
  644. error_setg_errno(errp, errno, "Could not create PipeWire loop");
  645. goto fail;
  646. }
  647. pw->context =
  648. pw_context_new(pw_thread_loop_get_loop(pw->thread_loop), NULL, 0);
  649. if (pw->context == NULL) {
  650. error_setg_errno(errp, errno, "Could not create PipeWire context");
  651. goto fail;
  652. }
  653. if (pw_thread_loop_start(pw->thread_loop) < 0) {
  654. error_setg_errno(errp, errno, "Could not start PipeWire loop");
  655. goto fail;
  656. }
  657. pw_thread_loop_lock(pw->thread_loop);
  658. pw->core = pw_context_connect(pw->context, NULL, 0);
  659. if (pw->core == NULL) {
  660. pw_thread_loop_unlock(pw->thread_loop);
  661. error_setg_errno(errp, errno, "Failed to connect to PipeWire instance");
  662. goto fail;
  663. }
  664. if (pw_core_add_listener(pw->core, &pw->core_listener,
  665. &core_events, pw) < 0) {
  666. pw_thread_loop_unlock(pw->thread_loop);
  667. error_setg(errp, "Failed to add PipeWire listener");
  668. goto fail;
  669. }
  670. if (wait_resync(pw) < 0) {
  671. pw_thread_loop_unlock(pw->thread_loop);
  672. }
  673. pw_thread_loop_unlock(pw->thread_loop);
  674. return g_steal_pointer(&pw);
  675. fail:
  676. if (pw->thread_loop) {
  677. pw_thread_loop_stop(pw->thread_loop);
  678. }
  679. g_clear_pointer(&pw->context, pw_context_destroy);
  680. g_clear_pointer(&pw->thread_loop, pw_thread_loop_destroy);
  681. return NULL;
  682. }
  683. static void
  684. qpw_audio_fini(void *opaque)
  685. {
  686. pwaudio *pw = opaque;
  687. if (pw->thread_loop) {
  688. pw_thread_loop_stop(pw->thread_loop);
  689. }
  690. if (pw->core) {
  691. spa_hook_remove(&pw->core_listener);
  692. spa_zero(pw->core_listener);
  693. pw_core_disconnect(pw->core);
  694. }
  695. if (pw->context) {
  696. pw_context_destroy(pw->context);
  697. }
  698. pw_thread_loop_destroy(pw->thread_loop);
  699. g_free(pw);
  700. }
  701. static struct audio_pcm_ops qpw_pcm_ops = {
  702. .init_out = qpw_init_out,
  703. .fini_out = qpw_fini_out,
  704. .write = qpw_write,
  705. .buffer_get_free = qpw_buffer_get_free,
  706. .run_buffer_out = audio_generic_run_buffer_out,
  707. .enable_out = qpw_enable_out,
  708. .volume_out = qpw_volume_out,
  709. .volume_in = qpw_volume_in,
  710. .init_in = qpw_init_in,
  711. .fini_in = qpw_fini_in,
  712. .read = qpw_read,
  713. .run_buffer_in = audio_generic_run_buffer_in,
  714. .enable_in = qpw_enable_in
  715. };
  716. static struct audio_driver pw_audio_driver = {
  717. .name = "pipewire",
  718. .descr = "http://www.pipewire.org/",
  719. .init = qpw_audio_init,
  720. .fini = qpw_audio_fini,
  721. .pcm_ops = &qpw_pcm_ops,
  722. .max_voices_out = INT_MAX,
  723. .max_voices_in = INT_MAX,
  724. .voice_size_out = sizeof(PWVoiceOut),
  725. .voice_size_in = sizeof(PWVoiceIn),
  726. };
  727. static void
  728. register_audio_pw(void)
  729. {
  730. audio_driver_register(&pw_audio_driver);
  731. }
  732. type_init(register_audio_pw);