qapi: Replace ad hoc "since" documentation by member documentation
[qemu/armbru.git] / audio / sndioaudio.c
blob3fde01fdbd59a4432cdc67eb8d893d37b5578bb1
1 /*
2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2019 Alexandre Ratchov <alex@caoua.org>
5 */
7 /*
8 * TODO :
10 * Use a single device and open it in full-duplex rather than
11 * opening it twice (once for playback once for recording).
13 * This is the only way to ensure that playback doesn't drift with respect
14 * to recording, which is what guest systems expect.
17 #include "qemu/osdep.h"
18 #include <poll.h>
19 #include <sndio.h>
20 #include "qemu/main-loop.h"
21 #include "audio.h"
22 #include "trace.h"
24 #define AUDIO_CAP "sndio"
25 #include "audio_int.h"
27 /* default latency in microseconds if no option is set */
28 #define SNDIO_LATENCY_US 50000
30 typedef struct SndioVoice {
31 union {
32 HWVoiceOut out;
33 HWVoiceIn in;
34 } hw;
35 struct sio_par par;
36 struct sio_hdl *hdl;
37 struct pollfd *pfds;
38 struct pollindex {
39 struct SndioVoice *self;
40 int index;
41 } *pindexes;
42 unsigned char *buf;
43 size_t buf_size;
44 size_t sndio_pos;
45 size_t qemu_pos;
46 unsigned int mode;
47 unsigned int nfds;
48 bool enabled;
49 } SndioVoice;
51 typedef struct SndioConf {
52 const char *devname;
53 unsigned int latency;
54 } SndioConf;
56 /* needed for forward reference */
57 static void sndio_poll_in(void *arg);
58 static void sndio_poll_out(void *arg);
61 * stop polling descriptors
63 static void sndio_poll_clear(SndioVoice *self)
65 struct pollfd *pfd;
66 int i;
68 for (i = 0; i < self->nfds; i++) {
69 pfd = &self->pfds[i];
70 qemu_set_fd_handler(pfd->fd, NULL, NULL, NULL);
73 self->nfds = 0;
77 * write data to the device until it blocks or
78 * all of our buffered data is written
80 static void sndio_write(SndioVoice *self)
82 size_t todo, n;
84 todo = self->qemu_pos - self->sndio_pos;
87 * transfer data to device, until it blocks
89 while (todo > 0) {
90 n = sio_write(self->hdl, self->buf + self->sndio_pos, todo);
91 if (n == 0) {
92 break;
94 self->sndio_pos += n;
95 todo -= n;
98 if (self->sndio_pos == self->buf_size) {
100 * we complete the block
102 self->sndio_pos = 0;
103 self->qemu_pos = 0;
108 * read data from the device until it blocks or
109 * there no room any longer
111 static void sndio_read(SndioVoice *self)
113 size_t todo, n;
115 todo = self->buf_size - self->sndio_pos;
118 * transfer data from the device, until it blocks
120 while (todo > 0) {
121 n = sio_read(self->hdl, self->buf + self->sndio_pos, todo);
122 if (n == 0) {
123 break;
125 self->sndio_pos += n;
126 todo -= n;
131 * Set handlers for all descriptors libsndio needs to
132 * poll
134 static void sndio_poll_wait(SndioVoice *self)
136 struct pollfd *pfd;
137 int events, i;
139 events = 0;
140 if (self->mode == SIO_PLAY) {
141 if (self->sndio_pos < self->qemu_pos) {
142 events |= POLLOUT;
144 } else {
145 if (self->sndio_pos < self->buf_size) {
146 events |= POLLIN;
151 * fill the given array of descriptors with the events sndio
152 * wants, they are different from our 'event' variable because
153 * sndio may use descriptors internally.
155 self->nfds = sio_pollfd(self->hdl, self->pfds, events);
157 for (i = 0; i < self->nfds; i++) {
158 pfd = &self->pfds[i];
159 if (pfd->fd < 0) {
160 continue;
162 qemu_set_fd_handler(pfd->fd,
163 (pfd->events & POLLIN) ? sndio_poll_in : NULL,
164 (pfd->events & POLLOUT) ? sndio_poll_out : NULL,
165 &self->pindexes[i]);
166 pfd->revents = 0;
171 * call-back called when one of the descriptors
172 * became readable or writable
174 static void sndio_poll_event(SndioVoice *self, int index, int event)
176 int revents;
179 * ensure we're not called twice this cycle
181 sndio_poll_clear(self);
184 * make self->pfds[] look as we're returning from poll syscal,
185 * this is how sio_revents expects events to be.
187 self->pfds[index].revents = event;
190 * tell sndio to handle events and return whether we can read or
191 * write without blocking.
193 revents = sio_revents(self->hdl, self->pfds);
194 if (self->mode == SIO_PLAY) {
195 if (revents & POLLOUT) {
196 sndio_write(self);
199 if (self->qemu_pos < self->buf_size) {
200 audio_run(self->hw.out.s, "sndio_out");
202 } else {
203 if (revents & POLLIN) {
204 sndio_read(self);
207 if (self->qemu_pos < self->sndio_pos) {
208 audio_run(self->hw.in.s, "sndio_in");
213 * audio_run() may have changed state
215 if (self->enabled) {
216 sndio_poll_wait(self);
221 * return the upper limit of the amount of free play buffer space
223 static size_t sndio_buffer_get_free(HWVoiceOut *hw)
225 SndioVoice *self = (SndioVoice *) hw;
227 return self->buf_size - self->qemu_pos;
231 * return a buffer where data to play can be stored,
232 * its size is stored in the location pointed by the size argument.
234 static void *sndio_get_buffer_out(HWVoiceOut *hw, size_t *size)
236 SndioVoice *self = (SndioVoice *) hw;
238 *size = self->buf_size - self->qemu_pos;
239 return self->buf + self->qemu_pos;
243 * put back to sndio back-end a buffer returned by sndio_get_buffer_out()
245 static size_t sndio_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
247 SndioVoice *self = (SndioVoice *) hw;
249 self->qemu_pos += size;
250 sndio_poll_wait(self);
251 return size;
255 * return a buffer from where recorded data is available,
256 * its size is stored in the location pointed by the size argument.
257 * it may not exceed the initial value of "*size".
259 static void *sndio_get_buffer_in(HWVoiceIn *hw, size_t *size)
261 SndioVoice *self = (SndioVoice *) hw;
262 size_t todo, max_todo;
265 * unlike the get_buffer_out() method, get_buffer_in()
266 * must return a buffer of at most the given size, see audio.c
268 max_todo = *size;
270 todo = self->sndio_pos - self->qemu_pos;
271 if (todo > max_todo) {
272 todo = max_todo;
275 *size = todo;
276 return self->buf + self->qemu_pos;
280 * discard the given amount of recorded data
282 static void sndio_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
284 SndioVoice *self = (SndioVoice *) hw;
286 self->qemu_pos += size;
287 if (self->qemu_pos == self->buf_size) {
288 self->qemu_pos = 0;
289 self->sndio_pos = 0;
291 sndio_poll_wait(self);
295 * call-back called when one of our descriptors becomes writable
297 static void sndio_poll_out(void *arg)
299 struct pollindex *pindex = (struct pollindex *) arg;
301 sndio_poll_event(pindex->self, pindex->index, POLLOUT);
305 * call-back called when one of our descriptors becomes readable
307 static void sndio_poll_in(void *arg)
309 struct pollindex *pindex = (struct pollindex *) arg;
311 sndio_poll_event(pindex->self, pindex->index, POLLIN);
314 static void sndio_fini(SndioVoice *self)
316 if (self->hdl) {
317 sio_close(self->hdl);
318 self->hdl = NULL;
321 g_free(self->pfds);
322 g_free(self->pindexes);
323 g_free(self->buf);
326 static int sndio_init(SndioVoice *self,
327 struct audsettings *as, int mode, Audiodev *dev)
329 AudiodevSndioOptions *opts = &dev->u.sndio;
330 unsigned long long latency;
331 const char *dev_name;
332 struct sio_par req;
333 unsigned int nch;
334 int i, nfds;
336 dev_name = opts->dev ?: SIO_DEVANY;
337 latency = opts->has_latency ? opts->latency : SNDIO_LATENCY_US;
339 /* open the device in non-blocking mode */
340 self->hdl = sio_open(dev_name, mode, 1);
341 if (self->hdl == NULL) {
342 dolog("failed to open device\n");
343 return -1;
346 self->mode = mode;
348 sio_initpar(&req);
350 switch (as->fmt) {
351 case AUDIO_FORMAT_S8:
352 req.bits = 8;
353 req.sig = 1;
354 break;
355 case AUDIO_FORMAT_U8:
356 req.bits = 8;
357 req.sig = 0;
358 break;
359 case AUDIO_FORMAT_S16:
360 req.bits = 16;
361 req.sig = 1;
362 break;
363 case AUDIO_FORMAT_U16:
364 req.bits = 16;
365 req.sig = 0;
366 break;
367 case AUDIO_FORMAT_S32:
368 req.bits = 32;
369 req.sig = 1;
370 break;
371 case AUDIO_FORMAT_U32:
372 req.bits = 32;
373 req.sig = 0;
374 break;
375 default:
376 dolog("unknown audio sample format\n");
377 return -1;
380 if (req.bits > 8) {
381 req.le = as->endianness ? 0 : 1;
384 req.rate = as->freq;
385 if (mode == SIO_PLAY) {
386 req.pchan = as->nchannels;
387 } else {
388 req.rchan = as->nchannels;
391 /* set on-device buffer size */
392 req.appbufsz = req.rate * latency / 1000000;
394 if (!sio_setpar(self->hdl, &req)) {
395 dolog("failed set audio params\n");
396 goto fail;
399 if (!sio_getpar(self->hdl, &self->par)) {
400 dolog("failed get audio params\n");
401 goto fail;
404 nch = (mode == SIO_PLAY) ? self->par.pchan : self->par.rchan;
407 * With the default setup, sndio supports any combination of parameters
408 * so these checks are mostly to catch configuration errors.
410 if (self->par.bits != req.bits || self->par.bps != req.bits / 8 ||
411 self->par.sig != req.sig || (req.bits > 8 && self->par.le != req.le) ||
412 self->par.rate != as->freq || nch != as->nchannels) {
413 dolog("unsupported audio params\n");
414 goto fail;
418 * we use one block as buffer size; this is how
419 * transfers get well aligned
421 self->buf_size = self->par.round * self->par.bps * nch;
423 self->buf = g_malloc(self->buf_size);
424 if (self->buf == NULL) {
425 dolog("failed to allocate audio buffer\n");
426 goto fail;
429 nfds = sio_nfds(self->hdl);
431 self->pfds = g_malloc_n(nfds, sizeof(struct pollfd));
432 if (self->pfds == NULL) {
433 dolog("failed to allocate pollfd structures\n");
434 goto fail;
437 self->pindexes = g_malloc_n(nfds, sizeof(struct pollindex));
438 if (self->pindexes == NULL) {
439 dolog("failed to allocate pollindex structures\n");
440 goto fail;
443 for (i = 0; i < nfds; i++) {
444 self->pindexes[i].self = self;
445 self->pindexes[i].index = i;
448 return 0;
449 fail:
450 sndio_fini(self);
451 return -1;
454 static void sndio_enable(SndioVoice *self, bool enable)
456 if (enable) {
457 sio_start(self->hdl);
458 self->enabled = true;
459 sndio_poll_wait(self);
460 } else {
461 self->enabled = false;
462 sndio_poll_clear(self);
463 sio_stop(self->hdl);
467 static void sndio_enable_out(HWVoiceOut *hw, bool enable)
469 SndioVoice *self = (SndioVoice *) hw;
471 sndio_enable(self, enable);
474 static void sndio_enable_in(HWVoiceIn *hw, bool enable)
476 SndioVoice *self = (SndioVoice *) hw;
478 sndio_enable(self, enable);
481 static int sndio_init_out(HWVoiceOut *hw, struct audsettings *as, void *opaque)
483 SndioVoice *self = (SndioVoice *) hw;
485 if (sndio_init(self, as, SIO_PLAY, opaque) == -1) {
486 return -1;
489 audio_pcm_init_info(&hw->info, as);
490 hw->samples = self->par.round;
491 return 0;
494 static int sndio_init_in(HWVoiceIn *hw, struct audsettings *as, void *opaque)
496 SndioVoice *self = (SndioVoice *) hw;
498 if (sndio_init(self, as, SIO_REC, opaque) == -1) {
499 return -1;
502 audio_pcm_init_info(&hw->info, as);
503 hw->samples = self->par.round;
504 return 0;
507 static void sndio_fini_out(HWVoiceOut *hw)
509 SndioVoice *self = (SndioVoice *) hw;
511 sndio_fini(self);
514 static void sndio_fini_in(HWVoiceIn *hw)
516 SndioVoice *self = (SndioVoice *) hw;
518 sndio_fini(self);
521 static void *sndio_audio_init(Audiodev *dev)
523 assert(dev->driver == AUDIODEV_DRIVER_SNDIO);
524 return dev;
527 static void sndio_audio_fini(void *opaque)
531 static struct audio_pcm_ops sndio_pcm_ops = {
532 .init_out = sndio_init_out,
533 .fini_out = sndio_fini_out,
534 .enable_out = sndio_enable_out,
535 .write = audio_generic_write,
536 .buffer_get_free = sndio_buffer_get_free,
537 .get_buffer_out = sndio_get_buffer_out,
538 .put_buffer_out = sndio_put_buffer_out,
539 .init_in = sndio_init_in,
540 .fini_in = sndio_fini_in,
541 .read = audio_generic_read,
542 .enable_in = sndio_enable_in,
543 .get_buffer_in = sndio_get_buffer_in,
544 .put_buffer_in = sndio_put_buffer_in,
547 static struct audio_driver sndio_audio_driver = {
548 .name = "sndio",
549 .descr = "sndio https://sndio.org",
550 .init = sndio_audio_init,
551 .fini = sndio_audio_fini,
552 .pcm_ops = &sndio_pcm_ops,
553 .can_be_default = 1,
554 .max_voices_out = INT_MAX,
555 .max_voices_in = INT_MAX,
556 .voice_size_out = sizeof(SndioVoice),
557 .voice_size_in = sizeof(SndioVoice)
560 static void register_audio_sndio(void)
562 audio_driver_register(&sndio_audio_driver);
565 type_init(register_audio_sndio);