blob: 057571dd1e03c9d30f8a94f5b39e22bec4aa77a5 [file] [log] [blame]
bellard1d14ffa2005-10-30 18:58:22 +00001/*
2 * QEMU ALSA audio driver
3 *
4 * Copyright (c) 2005 Vassili Karpov (malc)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
bellard1d14ffa2005-10-30 18:58:22 +000026#include <alsa/asoundlib.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
pbrook749bc4b2007-11-17 17:35:54 +000029#include "audio.h"
Kővágó, Zoltánfbb7ef52015-06-12 14:33:06 +020030#include "trace.h"
bellard1d14ffa2005-10-30 18:58:22 +000031
malc26378722009-07-17 05:57:04 +040032#pragma GCC diagnostic ignored "-Waddress"
malc26378722009-07-17 05:57:04 +040033
bellard1d14ffa2005-10-30 18:58:22 +000034#define AUDIO_CAP "alsa"
35#include "audio_int.h"
36
Volker Rümelin24301182021-05-17 21:46:01 +020037#define DEBUG_ALSA 0
38
malc8b438ba2009-09-12 02:29:25 +040039struct pollhlp {
40 snd_pcm_t *handle;
41 struct pollfd *pfds;
42 int count;
malcb4f763b2009-09-14 03:53:25 +040043 int mask;
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +020044 AudioState *s;
malc8b438ba2009-09-12 02:29:25 +040045};
46
bellard1d14ffa2005-10-30 18:58:22 +000047typedef struct ALSAVoiceOut {
48 HWVoiceOut hw;
bellard1d14ffa2005-10-30 18:58:22 +000049 snd_pcm_t *handle;
malc8b438ba2009-09-12 02:29:25 +040050 struct pollhlp pollhlp;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +010051 Audiodev *dev;
bellard1d14ffa2005-10-30 18:58:22 +000052} ALSAVoiceOut;
53
54typedef struct ALSAVoiceIn {
55 HWVoiceIn hw;
56 snd_pcm_t *handle;
malc8b438ba2009-09-12 02:29:25 +040057 struct pollhlp pollhlp;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +010058 Audiodev *dev;
bellard1d14ffa2005-10-30 18:58:22 +000059} ALSAVoiceIn;
60
bellard1d14ffa2005-10-30 18:58:22 +000061struct alsa_params_req {
balrogca9cc282008-01-14 04:24:29 +000062 int freq;
63 snd_pcm_format_t fmt;
64 int nchannels;
bellard1d14ffa2005-10-30 18:58:22 +000065};
66
67struct alsa_params_obt {
68 int freq;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010069 AudioFormat fmt;
balrogca9cc282008-01-14 04:24:29 +000070 int endianness;
bellard1d14ffa2005-10-30 18:58:22 +000071 int nchannels;
bellardc0fe3822005-11-05 18:55:28 +000072 snd_pcm_uframes_t samples;
bellard1d14ffa2005-10-30 18:58:22 +000073};
74
Marc-André Lureau9edc6312022-02-20 20:39:25 +040075static void G_GNUC_PRINTF (2, 3) alsa_logerr (int err, const char *fmt, ...)
bellard1d14ffa2005-10-30 18:58:22 +000076{
77 va_list ap;
78
79 va_start (ap, fmt);
80 AUD_vlog (AUDIO_CAP, fmt, ap);
81 va_end (ap);
82
83 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
84}
85
Marc-André Lureau9edc6312022-02-20 20:39:25 +040086static void G_GNUC_PRINTF (3, 4) alsa_logerr2 (
bellard1d14ffa2005-10-30 18:58:22 +000087 int err,
88 const char *typ,
89 const char *fmt,
90 ...
91 )
92{
93 va_list ap;
94
bellardc0fe3822005-11-05 18:55:28 +000095 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
bellard1d14ffa2005-10-30 18:58:22 +000096
97 va_start (ap, fmt);
98 AUD_vlog (AUDIO_CAP, fmt, ap);
99 va_end (ap);
100
101 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
102}
103
malc6ebfda12009-09-14 03:51:48 +0400104static void alsa_fini_poll (struct pollhlp *hlp)
105{
106 int i;
107 struct pollfd *pfds = hlp->pfds;
108
109 if (pfds) {
110 for (i = 0; i < hlp->count; ++i) {
111 qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
112 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500113 g_free (pfds);
malc6ebfda12009-09-14 03:51:48 +0400114 }
115 hlp->pfds = NULL;
116 hlp->count = 0;
117 hlp->handle = NULL;
118}
119
120static void alsa_anal_close1 (snd_pcm_t **handlep)
bellard1d14ffa2005-10-30 18:58:22 +0000121{
122 int err = snd_pcm_close (*handlep);
123 if (err) {
124 alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
125 }
126 *handlep = NULL;
127}
128
malc6ebfda12009-09-14 03:51:48 +0400129static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
130{
131 alsa_fini_poll (hlp);
132 alsa_anal_close1 (handlep);
133}
134
malc8b438ba2009-09-12 02:29:25 +0400135static int alsa_recover (snd_pcm_t *handle)
136{
137 int err = snd_pcm_prepare (handle);
138 if (err < 0) {
139 alsa_logerr (err, "Failed to prepare handle %p\n", handle);
140 return -1;
141 }
142 return 0;
143}
144
145static int alsa_resume (snd_pcm_t *handle)
146{
147 int err = snd_pcm_resume (handle);
148 if (err < 0) {
149 alsa_logerr (err, "Failed to resume handle %p\n", handle);
150 return -1;
151 }
152 return 0;
153}
154
155static void alsa_poll_handler (void *opaque)
156{
157 int err, count;
158 snd_pcm_state_t state;
159 struct pollhlp *hlp = opaque;
160 unsigned short revents;
161
162 count = poll (hlp->pfds, hlp->count, 0);
163 if (count < 0) {
164 dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
165 return;
166 }
167
168 if (!count) {
169 return;
170 }
171
172 /* XXX: ALSA example uses initial count, not the one returned by
173 poll, correct? */
174 err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
175 hlp->count, &revents);
176 if (err < 0) {
177 alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
178 return;
179 }
180
malcb4f763b2009-09-14 03:53:25 +0400181 if (!(revents & hlp->mask)) {
Kővágó, Zoltánfbb7ef52015-06-12 14:33:06 +0200182 trace_alsa_revents(revents);
malc8b438ba2009-09-12 02:29:25 +0400183 return;
184 }
185
186 state = snd_pcm_state (hlp->handle);
187 switch (state) {
malcd9812b02010-02-28 18:34:21 +0300188 case SND_PCM_STATE_SETUP:
189 alsa_recover (hlp->handle);
190 break;
191
malc8b438ba2009-09-12 02:29:25 +0400192 case SND_PCM_STATE_XRUN:
193 alsa_recover (hlp->handle);
194 break;
195
196 case SND_PCM_STATE_SUSPENDED:
197 alsa_resume (hlp->handle);
198 break;
199
200 case SND_PCM_STATE_PREPARED:
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200201 audio_run(hlp->s, "alsa run (prepared)");
malc8b438ba2009-09-12 02:29:25 +0400202 break;
203
204 case SND_PCM_STATE_RUNNING:
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200205 audio_run(hlp->s, "alsa run (running)");
malc8b438ba2009-09-12 02:29:25 +0400206 break;
207
208 default:
209 dolog ("Unexpected state %d\n", state);
210 }
211}
212
malcb4f763b2009-09-14 03:53:25 +0400213static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
malc8b438ba2009-09-12 02:29:25 +0400214{
215 int i, count, err;
216 struct pollfd *pfds;
217
218 count = snd_pcm_poll_descriptors_count (handle);
219 if (count <= 0) {
220 dolog ("Could not initialize poll mode\n"
221 "Invalid number of poll descriptors %d\n", count);
222 return -1;
223 }
224
Volker Rümelind1def192023-01-21 10:47:30 +0100225 pfds = g_new0(struct pollfd, count);
malc8b438ba2009-09-12 02:29:25 +0400226
227 err = snd_pcm_poll_descriptors (handle, pfds, count);
228 if (err < 0) {
229 alsa_logerr (err, "Could not initialize poll mode\n"
230 "Could not obtain poll descriptors\n");
Anthony Liguori7267c092011-08-20 22:09:37 -0500231 g_free (pfds);
malc8b438ba2009-09-12 02:29:25 +0400232 return -1;
233 }
234
235 for (i = 0; i < count; ++i) {
236 if (pfds[i].events & POLLIN) {
Fam Zhengbe93f212015-06-04 14:45:20 +0800237 qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
malc8b438ba2009-09-12 02:29:25 +0400238 }
239 if (pfds[i].events & POLLOUT) {
Kővágó, Zoltánfbb7ef52015-06-12 14:33:06 +0200240 trace_alsa_pollout(i, pfds[i].fd);
Fam Zhengbe93f212015-06-04 14:45:20 +0800241 qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
malc8b438ba2009-09-12 02:29:25 +0400242 }
Kővágó, Zoltánfbb7ef52015-06-12 14:33:06 +0200243 trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
malc8b438ba2009-09-12 02:29:25 +0400244
malc8b438ba2009-09-12 02:29:25 +0400245 }
246 hlp->pfds = pfds;
247 hlp->count = count;
248 hlp->handle = handle;
malcb4f763b2009-09-14 03:53:25 +0400249 hlp->mask = mask;
malc8b438ba2009-09-12 02:29:25 +0400250 return 0;
251}
252
253static int alsa_poll_out (HWVoiceOut *hw)
254{
255 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
256
malcb4f763b2009-09-14 03:53:25 +0400257 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
malc8b438ba2009-09-12 02:29:25 +0400258}
259
260static int alsa_poll_in (HWVoiceIn *hw)
261{
262 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
263
malcb4f763b2009-09-14 03:53:25 +0400264 return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
malc8b438ba2009-09-12 02:29:25 +0400265}
266
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100267static snd_pcm_format_t aud_to_alsafmt (AudioFormat fmt, int endianness)
bellard1d14ffa2005-10-30 18:58:22 +0000268{
269 switch (fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100270 case AUDIO_FORMAT_S8:
bellard1d14ffa2005-10-30 18:58:22 +0000271 return SND_PCM_FORMAT_S8;
272
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100273 case AUDIO_FORMAT_U8:
bellard1d14ffa2005-10-30 18:58:22 +0000274 return SND_PCM_FORMAT_U8;
275
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100276 case AUDIO_FORMAT_S16:
Michael Walled66bddd2011-01-08 17:53:30 +0100277 if (endianness) {
278 return SND_PCM_FORMAT_S16_BE;
Zhang Han6c6886b2021-01-15 09:24:25 +0800279 } else {
Michael Walled66bddd2011-01-08 17:53:30 +0100280 return SND_PCM_FORMAT_S16_LE;
281 }
bellard1d14ffa2005-10-30 18:58:22 +0000282
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100283 case AUDIO_FORMAT_U16:
Michael Walled66bddd2011-01-08 17:53:30 +0100284 if (endianness) {
285 return SND_PCM_FORMAT_U16_BE;
Zhang Han6c6886b2021-01-15 09:24:25 +0800286 } else {
Michael Walled66bddd2011-01-08 17:53:30 +0100287 return SND_PCM_FORMAT_U16_LE;
288 }
bellard1d14ffa2005-10-30 18:58:22 +0000289
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100290 case AUDIO_FORMAT_S32:
Michael Walled66bddd2011-01-08 17:53:30 +0100291 if (endianness) {
292 return SND_PCM_FORMAT_S32_BE;
Zhang Han6c6886b2021-01-15 09:24:25 +0800293 } else {
Michael Walled66bddd2011-01-08 17:53:30 +0100294 return SND_PCM_FORMAT_S32_LE;
295 }
thsf941aa22007-02-17 22:19:29 +0000296
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100297 case AUDIO_FORMAT_U32:
Michael Walled66bddd2011-01-08 17:53:30 +0100298 if (endianness) {
299 return SND_PCM_FORMAT_U32_BE;
Zhang Han6c6886b2021-01-15 09:24:25 +0800300 } else {
Michael Walled66bddd2011-01-08 17:53:30 +0100301 return SND_PCM_FORMAT_U32_LE;
302 }
thsf941aa22007-02-17 22:19:29 +0000303
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100304 case AUDIO_FORMAT_F32:
305 if (endianness) {
306 return SND_PCM_FORMAT_FLOAT_BE;
307 } else {
308 return SND_PCM_FORMAT_FLOAT_LE;
309 }
310
bellard1d14ffa2005-10-30 18:58:22 +0000311 default:
312 dolog ("Internal logic error: Bad audio format %d\n", fmt);
313#ifdef DEBUG_AUDIO
314 abort ();
315#endif
316 return SND_PCM_FORMAT_U8;
317 }
318}
319
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100320static int alsa_to_audfmt (snd_pcm_format_t alsafmt, AudioFormat *fmt,
balrogca9cc282008-01-14 04:24:29 +0000321 int *endianness)
bellard1d14ffa2005-10-30 18:58:22 +0000322{
323 switch (alsafmt) {
324 case SND_PCM_FORMAT_S8:
325 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100326 *fmt = AUDIO_FORMAT_S8;
bellard1d14ffa2005-10-30 18:58:22 +0000327 break;
328
329 case SND_PCM_FORMAT_U8:
330 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100331 *fmt = AUDIO_FORMAT_U8;
bellard1d14ffa2005-10-30 18:58:22 +0000332 break;
333
334 case SND_PCM_FORMAT_S16_LE:
335 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100336 *fmt = AUDIO_FORMAT_S16;
bellard1d14ffa2005-10-30 18:58:22 +0000337 break;
338
339 case SND_PCM_FORMAT_U16_LE:
340 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100341 *fmt = AUDIO_FORMAT_U16;
bellard1d14ffa2005-10-30 18:58:22 +0000342 break;
343
344 case SND_PCM_FORMAT_S16_BE:
345 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100346 *fmt = AUDIO_FORMAT_S16;
bellard1d14ffa2005-10-30 18:58:22 +0000347 break;
348
349 case SND_PCM_FORMAT_U16_BE:
350 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100351 *fmt = AUDIO_FORMAT_U16;
bellard1d14ffa2005-10-30 18:58:22 +0000352 break;
353
thsf941aa22007-02-17 22:19:29 +0000354 case SND_PCM_FORMAT_S32_LE:
355 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100356 *fmt = AUDIO_FORMAT_S32;
thsf941aa22007-02-17 22:19:29 +0000357 break;
358
359 case SND_PCM_FORMAT_U32_LE:
360 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100361 *fmt = AUDIO_FORMAT_U32;
thsf941aa22007-02-17 22:19:29 +0000362 break;
363
364 case SND_PCM_FORMAT_S32_BE:
365 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100366 *fmt = AUDIO_FORMAT_S32;
thsf941aa22007-02-17 22:19:29 +0000367 break;
368
369 case SND_PCM_FORMAT_U32_BE:
370 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100371 *fmt = AUDIO_FORMAT_U32;
thsf941aa22007-02-17 22:19:29 +0000372 break;
373
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100374 case SND_PCM_FORMAT_FLOAT_LE:
375 *endianness = 0;
376 *fmt = AUDIO_FORMAT_F32;
377 break;
378
379 case SND_PCM_FORMAT_FLOAT_BE:
380 *endianness = 1;
381 *fmt = AUDIO_FORMAT_F32;
382 break;
383
bellard1d14ffa2005-10-30 18:58:22 +0000384 default:
385 dolog ("Unrecognized audio format %d\n", alsafmt);
386 return -1;
387 }
388
389 return 0;
390}
391
bellard1d14ffa2005-10-30 18:58:22 +0000392static void alsa_dump_info (struct alsa_params_req *req,
malc8bb414d2010-04-21 15:40:23 +0400393 struct alsa_params_obt *obt,
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100394 snd_pcm_format_t obtfmt,
395 AudiodevAlsaPerDirectionOptions *apdo)
bellard1d14ffa2005-10-30 18:58:22 +0000396{
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100397 dolog("parameter | requested value | obtained value\n");
398 dolog("format | %10d | %10d\n", req->fmt, obtfmt);
399 dolog("channels | %10d | %10d\n",
400 req->nchannels, obt->nchannels);
401 dolog("frequency | %10d | %10d\n", req->freq, obt->freq);
402 dolog("============================================\n");
403 dolog("requested: buffer len %" PRId32 " period len %" PRId32 "\n",
404 apdo->buffer_length, apdo->period_length);
405 dolog("obtained: samples %ld\n", obt->samples);
bellard1d14ffa2005-10-30 18:58:22 +0000406}
bellard1d14ffa2005-10-30 18:58:22 +0000407
408static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
409{
410 int err;
411 snd_pcm_sw_params_t *sw_params;
412
413 snd_pcm_sw_params_alloca (&sw_params);
414
415 err = snd_pcm_sw_params_current (handle, sw_params);
416 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000417 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000418 alsa_logerr (err, "Failed to get current software parameters\n");
419 return;
420 }
421
422 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
423 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000424 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000425 alsa_logerr (err, "Failed to set software threshold to %ld\n",
426 threshold);
427 return;
428 }
429
430 err = snd_pcm_sw_params (handle, sw_params);
431 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000432 dolog ("Could not fully initialize DAC\n");
bellard1d14ffa2005-10-30 18:58:22 +0000433 alsa_logerr (err, "Failed to set software parameters\n");
434 return;
435 }
436}
437
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100438static int alsa_open(bool in, struct alsa_params_req *req,
439 struct alsa_params_obt *obt, snd_pcm_t **handlep,
440 Audiodev *dev)
bellard1d14ffa2005-10-30 18:58:22 +0000441{
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100442 AudiodevAlsaOptions *aopts = &dev->u.alsa;
443 AudiodevAlsaPerDirectionOptions *apdo = in ? aopts->in : aopts->out;
bellard1d14ffa2005-10-30 18:58:22 +0000444 snd_pcm_t *handle;
445 snd_pcm_hw_params_t *hw_params;
ths60fe76f2007-12-16 03:02:09 +0000446 int err;
447 unsigned int freq, nchannels;
Markus Armbrusterceb19c82022-11-04 17:06:49 +0100448 const char *pcm_name = apdo->dev ?: "default";
bellard1d14ffa2005-10-30 18:58:22 +0000449 snd_pcm_uframes_t obt_buffer_size;
450 const char *typ = in ? "ADC" : "DAC";
balrogca9cc282008-01-14 04:24:29 +0000451 snd_pcm_format_t obtfmt;
bellard1d14ffa2005-10-30 18:58:22 +0000452
453 freq = req->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000454 nchannels = req->nchannels;
455
456 snd_pcm_hw_params_alloca (&hw_params);
457
458 err = snd_pcm_open (
459 &handle,
460 pcm_name,
461 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
462 SND_PCM_NONBLOCK
463 );
464 if (err < 0) {
465 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
466 return -1;
467 }
468
469 err = snd_pcm_hw_params_any (handle, hw_params);
470 if (err < 0) {
471 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
472 goto err;
473 }
474
475 err = snd_pcm_hw_params_set_access (
476 handle,
477 hw_params,
478 SND_PCM_ACCESS_RW_INTERLEAVED
479 );
480 if (err < 0) {
481 alsa_logerr2 (err, typ, "Failed to set access type\n");
482 goto err;
483 }
484
485 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
Kővágó, Zoltánfbb7ef52015-06-12 14:33:06 +0200486 if (err < 0) {
bellard1d14ffa2005-10-30 18:58:22 +0000487 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
bellard1d14ffa2005-10-30 18:58:22 +0000488 }
489
490 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
491 if (err < 0) {
492 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
493 goto err;
494 }
495
496 err = snd_pcm_hw_params_set_channels_near (
497 handle,
498 hw_params,
499 &nchannels
500 );
501 if (err < 0) {
502 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
503 req->nchannels);
504 goto err;
505 }
506
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100507 if (apdo->buffer_length) {
508 int dir = 0;
509 unsigned int btime = apdo->buffer_length;
malcf3b52982008-06-21 20:09:32 +0000510
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100511 err = snd_pcm_hw_params_set_buffer_time_near(
512 handle, hw_params, &btime, &dir);
bellard1d14ffa2005-10-30 18:58:22 +0000513
malc7a24c802008-06-21 17:14:57 +0000514 if (err < 0) {
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100515 alsa_logerr2(err, typ, "Failed to set buffer time to %" PRId32 "\n",
516 apdo->buffer_length);
malc7a24c802008-06-21 17:14:57 +0000517 goto err;
bellard1d14ffa2005-10-30 18:58:22 +0000518 }
malcf3b52982008-06-21 20:09:32 +0000519
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100520 if (apdo->has_buffer_length && btime != apdo->buffer_length) {
521 dolog("Requested buffer time %" PRId32
522 " was rejected, using %u\n", apdo->buffer_length, btime);
523 }
bellard1d14ffa2005-10-30 18:58:22 +0000524 }
malc7a24c802008-06-21 17:14:57 +0000525
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100526 if (apdo->period_length) {
527 int dir = 0;
528 unsigned int ptime = apdo->period_length;
malcf3b52982008-06-21 20:09:32 +0000529
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100530 err = snd_pcm_hw_params_set_period_time_near(handle, hw_params, &ptime,
531 &dir);
malc7a24c802008-06-21 17:14:57 +0000532
533 if (err < 0) {
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100534 alsa_logerr2(err, typ, "Failed to set period time to %" PRId32 "\n",
535 apdo->period_length);
malc7a24c802008-06-21 17:14:57 +0000536 goto err;
537 }
malcf3b52982008-06-21 20:09:32 +0000538
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100539 if (apdo->has_period_length && ptime != apdo->period_length) {
540 dolog("Requested period time %" PRId32 " was rejected, using %d\n",
541 apdo->period_length, ptime);
542 }
bellard1d14ffa2005-10-30 18:58:22 +0000543 }
544
545 err = snd_pcm_hw_params (handle, hw_params);
546 if (err < 0) {
547 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
548 goto err;
549 }
550
551 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
552 if (err < 0) {
553 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
554 goto err;
555 }
556
balrogca9cc282008-01-14 04:24:29 +0000557 err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
558 if (err < 0) {
559 alsa_logerr2 (err, typ, "Failed to get format\n");
560 goto err;
561 }
562
563 if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
564 dolog ("Invalid format was returned %d\n", obtfmt);
565 goto err;
566 }
567
bellard1d14ffa2005-10-30 18:58:22 +0000568 err = snd_pcm_prepare (handle);
569 if (err < 0) {
bellardc0fe3822005-11-05 18:55:28 +0000570 alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
bellard1d14ffa2005-10-30 18:58:22 +0000571 goto err;
572 }
573
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100574 if (!in && aopts->has_threshold && aopts->threshold) {
575 struct audsettings as = { .freq = freq };
576 alsa_set_threshold(
577 handle,
578 audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo),
579 &as, aopts->threshold));
bellard1d14ffa2005-10-30 18:58:22 +0000580 }
581
bellard1d14ffa2005-10-30 18:58:22 +0000582 obt->nchannels = nchannels;
583 obt->freq = freq;
bellardc0fe3822005-11-05 18:55:28 +0000584 obt->samples = obt_buffer_size;
balrogca9cc282008-01-14 04:24:29 +0000585
bellard1d14ffa2005-10-30 18:58:22 +0000586 *handlep = handle;
587
Volker Rümelin24301182021-05-17 21:46:01 +0200588 if (DEBUG_ALSA || obtfmt != req->fmt ||
589 obt->nchannels != req->nchannels || obt->freq != req->freq) {
Vagrant Cascadianf093feb2010-02-26 13:39:46 -0800590 dolog ("Audio parameters for %s\n", typ);
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100591 alsa_dump_info(req, obt, obtfmt, apdo);
bellard1d14ffa2005-10-30 18:58:22 +0000592 }
593
bellard1d14ffa2005-10-30 18:58:22 +0000594 return 0;
595
596 err:
malc6ebfda12009-09-14 03:51:48 +0400597 alsa_anal_close1 (&handle);
bellard1d14ffa2005-10-30 18:58:22 +0000598 return -1;
599}
600
Volker Rümelin5a9d7ae2022-09-23 20:36:32 +0200601static size_t alsa_buffer_get_free(HWVoiceOut *hw)
602{
603 ALSAVoiceOut *alsa = (ALSAVoiceOut *)hw;
604 snd_pcm_sframes_t avail;
605 size_t alsa_free, generic_free, generic_in_use;
606
607 avail = snd_pcm_avail_update(alsa->handle);
608 if (avail < 0) {
609 if (avail == -EPIPE) {
610 if (!alsa_recover(alsa->handle)) {
611 avail = snd_pcm_avail_update(alsa->handle);
612 }
613 }
614 if (avail < 0) {
615 alsa_logerr(avail,
616 "Could not obtain number of available frames\n");
617 avail = 0;
618 }
619 }
620
621 alsa_free = avail * hw->info.bytes_per_frame;
622 generic_free = audio_generic_buffer_get_free(hw);
623 generic_in_use = hw->samples * hw->info.bytes_per_frame - generic_free;
624 if (generic_in_use) {
625 /*
626 * This code can only be reached in the unlikely case that
627 * snd_pcm_avail_update() returned a larger number of frames
628 * than snd_pcm_writei() could write. Make sure that all
629 * remaining bytes in the generic buffer can be written.
630 */
631 alsa_free = alsa_free > generic_in_use ? alsa_free - generic_in_use : 0;
632 }
633
634 return alsa_free;
635}
636
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200637static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len)
bellard1d14ffa2005-10-30 18:58:22 +0000638{
639 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200640 size_t pos = 0;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200641 size_t len_frames = len / hw->info.bytes_per_frame;
bellard1d14ffa2005-10-30 18:58:22 +0000642
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200643 while (len_frames) {
644 char *src = advance(buf, pos);
645 snd_pcm_sframes_t written;
646
647 written = snd_pcm_writei(alsa->handle, src, len_frames);
648
649 if (written <= 0) {
650 switch (written) {
651 case 0:
652 trace_alsa_wrote_zero(len_frames);
653 return pos;
654
655 case -EPIPE:
656 if (alsa_recover(alsa->handle)) {
657 alsa_logerr(written, "Failed to write %zu frames\n",
658 len_frames);
659 return pos;
660 }
661 trace_alsa_xrun_out();
662 continue;
663
664 case -ESTRPIPE:
665 /*
666 * stream is suspended and waiting for an application
667 * recovery
668 */
669 if (alsa_resume(alsa->handle)) {
670 alsa_logerr(written, "Failed to write %zu frames\n",
671 len_frames);
672 return pos;
673 }
674 trace_alsa_resume_out();
675 continue;
676
677 case -EAGAIN:
678 return pos;
679
680 default:
681 alsa_logerr(written, "Failed to write %zu frames from %p\n",
682 len, src);
683 return pos;
684 }
685 }
686
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200687 pos += written * hw->info.bytes_per_frame;
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200688 if (written < len_frames) {
689 break;
690 }
691 len_frames -= written;
bellard1d14ffa2005-10-30 18:58:22 +0000692 }
693
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200694 return pos;
bellard1d14ffa2005-10-30 18:58:22 +0000695}
696
697static void alsa_fini_out (HWVoiceOut *hw)
698{
699 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
700
701 ldebug ("alsa_fini\n");
malc6ebfda12009-09-14 03:51:48 +0400702 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
bellard1d14ffa2005-10-30 18:58:22 +0000703}
704
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200705static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
706 void *drv_opaque)
bellard1d14ffa2005-10-30 18:58:22 +0000707{
708 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
709 struct alsa_params_req req;
710 struct alsa_params_obt obt;
bellard1d14ffa2005-10-30 18:58:22 +0000711 snd_pcm_t *handle;
malc1ea879e2008-12-03 22:48:44 +0000712 struct audsettings obt_as;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100713 Audiodev *dev = drv_opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000714
Michael Walled66bddd2011-01-08 17:53:30 +0100715 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
bellardc0fe3822005-11-05 18:55:28 +0000716 req.freq = as->freq;
717 req.nchannels = as->nchannels;
bellard1d14ffa2005-10-30 18:58:22 +0000718
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100719 if (alsa_open(0, &req, &obt, &handle, dev)) {
bellard1d14ffa2005-10-30 18:58:22 +0000720 return -1;
721 }
722
bellardc0fe3822005-11-05 18:55:28 +0000723 obt_as.freq = obt.freq;
724 obt_as.nchannels = obt.nchannels;
balrogca9cc282008-01-14 04:24:29 +0000725 obt_as.fmt = obt.fmt;
726 obt_as.endianness = obt.endianness;
bellardc0fe3822005-11-05 18:55:28 +0000727
bellardd929eba2006-07-04 21:47:22 +0000728 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000729 hw->samples = obt.samples;
bellard1d14ffa2005-10-30 18:58:22 +0000730
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200731 alsa->pollhlp.s = hw->s;
bellard1d14ffa2005-10-30 18:58:22 +0000732 alsa->handle = handle;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100733 alsa->dev = dev;
bellard571ec3d2005-11-20 16:24:34 +0000734 return 0;
735}
736
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200737#define VOICE_CTL_PAUSE 0
738#define VOICE_CTL_PREPARE 1
739#define VOICE_CTL_START 2
740
741static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
bellard571ec3d2005-11-20 16:24:34 +0000742{
743 int err;
744
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200745 if (ctl == VOICE_CTL_PAUSE) {
bellard571ec3d2005-11-20 16:24:34 +0000746 err = snd_pcm_drop (handle);
747 if (err < 0) {
bellard32d448c2005-11-20 18:53:05 +0000748 alsa_logerr (err, "Could not stop %s\n", typ);
bellard571ec3d2005-11-20 16:24:34 +0000749 return -1;
750 }
Zhang Han6c6886b2021-01-15 09:24:25 +0800751 } else {
bellard571ec3d2005-11-20 16:24:34 +0000752 err = snd_pcm_prepare (handle);
753 if (err < 0) {
bellard32d448c2005-11-20 18:53:05 +0000754 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
bellard571ec3d2005-11-20 16:24:34 +0000755 return -1;
756 }
Jindrich Makovicka38cc9b62010-10-17 20:17:19 +0200757 if (ctl == VOICE_CTL_START) {
758 err = snd_pcm_start(handle);
759 if (err < 0) {
760 alsa_logerr (err, "Could not start handle for %s\n", typ);
761 return -1;
762 }
763 }
bellard571ec3d2005-11-20 16:24:34 +0000764 }
765
bellard1d14ffa2005-10-30 18:58:22 +0000766 return 0;
767}
768
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200769static void alsa_enable_out(HWVoiceOut *hw, bool enable)
bellard1d14ffa2005-10-30 18:58:22 +0000770{
bellard1d14ffa2005-10-30 18:58:22 +0000771 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100772 AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out;
bellard1d14ffa2005-10-30 18:58:22 +0000773
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200774 if (enable) {
775 bool poll_mode = apdo->try_poll;
malc301901b2009-10-02 02:37:40 +0400776
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200777 ldebug("enabling voice\n");
778 if (poll_mode && alsa_poll_out(hw)) {
779 poll_mode = 0;
malc8b438ba2009-09-12 02:29:25 +0400780 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200781 hw->poll_mode = poll_mode;
782 alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PREPARE);
783 } else {
784 ldebug("disabling voice\n");
Jindrich Makovicka22d948a2010-10-17 19:28:14 +0200785 if (hw->poll_mode) {
786 hw->poll_mode = 0;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200787 alsa_fini_poll(&alsa->pollhlp);
Jindrich Makovicka22d948a2010-10-17 19:28:14 +0200788 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200789 alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PAUSE);
bellard1d14ffa2005-10-30 18:58:22 +0000790 }
bellard1d14ffa2005-10-30 18:58:22 +0000791}
792
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200793static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
bellard1d14ffa2005-10-30 18:58:22 +0000794{
795 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
796 struct alsa_params_req req;
797 struct alsa_params_obt obt;
bellard1d14ffa2005-10-30 18:58:22 +0000798 snd_pcm_t *handle;
malc1ea879e2008-12-03 22:48:44 +0000799 struct audsettings obt_as;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100800 Audiodev *dev = drv_opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000801
Michael Walled66bddd2011-01-08 17:53:30 +0100802 req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
bellardc0fe3822005-11-05 18:55:28 +0000803 req.freq = as->freq;
804 req.nchannels = as->nchannels;
bellard1d14ffa2005-10-30 18:58:22 +0000805
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100806 if (alsa_open(1, &req, &obt, &handle, dev)) {
bellard1d14ffa2005-10-30 18:58:22 +0000807 return -1;
808 }
809
bellardc0fe3822005-11-05 18:55:28 +0000810 obt_as.freq = obt.freq;
811 obt_as.nchannels = obt.nchannels;
balrogca9cc282008-01-14 04:24:29 +0000812 obt_as.fmt = obt.fmt;
813 obt_as.endianness = obt.endianness;
bellardc0fe3822005-11-05 18:55:28 +0000814
bellardd929eba2006-07-04 21:47:22 +0000815 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000816 hw->samples = obt.samples;
817
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200818 alsa->pollhlp.s = hw->s;
bellard1d14ffa2005-10-30 18:58:22 +0000819 alsa->handle = handle;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100820 alsa->dev = dev;
bellard1d14ffa2005-10-30 18:58:22 +0000821 return 0;
822}
823
824static void alsa_fini_in (HWVoiceIn *hw)
825{
826 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
827
malc6ebfda12009-09-14 03:51:48 +0400828 alsa_anal_close (&alsa->handle, &alsa->pollhlp);
bellard1d14ffa2005-10-30 18:58:22 +0000829}
830
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200831static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)
bellard1d14ffa2005-10-30 18:58:22 +0000832{
833 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200834 size_t pos = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000835
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200836 while (len) {
837 void *dst = advance(buf, pos);
bellard1d14ffa2005-10-30 18:58:22 +0000838 snd_pcm_sframes_t nread;
bellard1d14ffa2005-10-30 18:58:22 +0000839
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200840 nread = snd_pcm_readi(
841 alsa->handle, dst, len / hw->info.bytes_per_frame);
bellard1d14ffa2005-10-30 18:58:22 +0000842
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200843 if (nread <= 0) {
844 switch (nread) {
845 case 0:
846 trace_alsa_read_zero(len);
Philippe Mathieu-Daudé3a1bdd12020-02-18 10:43:51 +0100847 return pos;
bellard1d14ffa2005-10-30 18:58:22 +0000848
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200849 case -EPIPE:
850 if (alsa_recover(alsa->handle)) {
851 alsa_logerr(nread, "Failed to read %zu frames\n", len);
852 return pos;
bellard1d14ffa2005-10-30 18:58:22 +0000853 }
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200854 trace_alsa_xrun_in();
855 continue;
856
857 case -EAGAIN:
858 return pos;
859
860 default:
861 alsa_logerr(nread, "Failed to read %zu frames to %p\n",
862 len, dst);
Philippe Mathieu-Daudé3a1bdd12020-02-18 10:43:51 +0100863 return pos;
bellard1d14ffa2005-10-30 18:58:22 +0000864 }
bellard1d14ffa2005-10-30 18:58:22 +0000865 }
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200866
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200867 pos += nread * hw->info.bytes_per_frame;
868 len -= nread * hw->info.bytes_per_frame;
bellard1d14ffa2005-10-30 18:58:22 +0000869 }
870
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200871 return pos;
bellard1d14ffa2005-10-30 18:58:22 +0000872}
873
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200874static void alsa_enable_in(HWVoiceIn *hw, bool enable)
bellard1d14ffa2005-10-30 18:58:22 +0000875{
bellard571ec3d2005-11-20 16:24:34 +0000876 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100877 AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in;
bellard571ec3d2005-11-20 16:24:34 +0000878
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200879 if (enable) {
880 bool poll_mode = apdo->try_poll;
malca628b862009-10-03 03:30:06 +0400881
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200882 ldebug("enabling voice\n");
883 if (poll_mode && alsa_poll_in(hw)) {
884 poll_mode = 0;
malca628b862009-10-03 03:30:06 +0400885 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200886 hw->poll_mode = poll_mode;
bellard571ec3d2005-11-20 16:24:34 +0000887
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200888 alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_START);
889 } else {
bellard571ec3d2005-11-20 16:24:34 +0000890 ldebug ("disabling voice\n");
malc8b438ba2009-09-12 02:29:25 +0400891 if (hw->poll_mode) {
892 hw->poll_mode = 0;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200893 alsa_fini_poll(&alsa->pollhlp);
malc8b438ba2009-09-12 02:29:25 +0400894 }
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200895 alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_PAUSE);
bellard571ec3d2005-11-20 16:24:34 +0000896 }
bellard1d14ffa2005-10-30 18:58:22 +0000897}
898
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100899static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions *apdo)
900{
901 if (!apdo->has_try_poll) {
902 apdo->try_poll = true;
903 apdo->has_try_poll = true;
904 }
905}
Kővágó, Zoltán765b37d2015-06-03 23:03:48 +0200906
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100907static void *alsa_audio_init(Audiodev *dev)
bellard1d14ffa2005-10-30 18:58:22 +0000908{
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100909 AudiodevAlsaOptions *aopts;
910 assert(dev->driver == AUDIODEV_DRIVER_ALSA);
911
912 aopts = &dev->u.alsa;
913 alsa_init_per_direction(aopts->in);
914 alsa_init_per_direction(aopts->out);
915
Volker Rümelin46744732023-01-21 10:47:34 +0100916 /* don't set has_* so alsa_open can identify it wasn't set by the user */
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100917 if (!dev->u.alsa.out->has_period_length) {
Volker Rümelin46744732023-01-21 10:47:34 +0100918 /* 256 frames assuming 44100Hz */
919 dev->u.alsa.out->period_length = 5805;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100920 }
921 if (!dev->u.alsa.out->has_buffer_length) {
922 /* 4096 frames assuming 44100Hz */
Volker Rümelin46744732023-01-21 10:47:34 +0100923 dev->u.alsa.out->buffer_length = 92880;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100924 }
925
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100926 if (!dev->u.alsa.in->has_period_length) {
Volker Rümelin5140ad82023-01-21 10:47:35 +0100927 /* 256 frames assuming 44100Hz */
928 dev->u.alsa.in->period_length = 5805;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100929 }
930 if (!dev->u.alsa.in->has_buffer_length) {
Volker Rümelin5140ad82023-01-21 10:47:35 +0100931 /* 4096 frames assuming 44100Hz */
932 dev->u.alsa.in->buffer_length = 92880;
Kővágó, Zoltána93f3282019-03-08 23:34:16 +0100933 }
934
935 return dev;
bellard1d14ffa2005-10-30 18:58:22 +0000936}
937
938static void alsa_audio_fini (void *opaque)
939{
bellard1d14ffa2005-10-30 18:58:22 +0000940}
941
blueswir135f4b582008-10-06 18:08:30 +0000942static struct audio_pcm_ops alsa_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200943 .init_out = alsa_init_out,
944 .fini_out = alsa_fini_out,
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200945 .write = alsa_write,
Volker Rümelin5a9d7ae2022-09-23 20:36:32 +0200946 .buffer_get_free = alsa_buffer_get_free,
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100947 .run_buffer_out = audio_generic_run_buffer_out,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200948 .enable_out = alsa_enable_out,
bellard1d14ffa2005-10-30 18:58:22 +0000949
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200950 .init_in = alsa_init_in,
951 .fini_in = alsa_fini_in,
Kővágó, Zoltán286a5d22019-09-19 23:24:10 +0200952 .read = alsa_read,
Volker Rümelina2893c82021-01-10 11:02:24 +0100953 .run_buffer_in = audio_generic_run_buffer_in,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200954 .enable_in = alsa_enable_in,
bellard1d14ffa2005-10-30 18:58:22 +0000955};
956
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100957static struct audio_driver alsa_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +0200958 .name = "alsa",
959 .descr = "ALSA http://www.alsa-project.org",
Juan Quintelabee37f32009-08-11 02:31:14 +0200960 .init = alsa_audio_init,
961 .fini = alsa_audio_fini,
962 .pcm_ops = &alsa_pcm_ops,
963 .can_be_default = 1,
964 .max_voices_out = INT_MAX,
965 .max_voices_in = INT_MAX,
966 .voice_size_out = sizeof (ALSAVoiceOut),
967 .voice_size_in = sizeof (ALSAVoiceIn)
bellard1d14ffa2005-10-30 18:58:22 +0000968};
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100969
970static void register_audio_alsa(void)
971{
972 audio_driver_register(&alsa_audio_driver);
973}
974type_init(register_audio_alsa);