bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1 | /* |
| 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 Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | 6086a56 | 2016-01-18 17:33:52 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 26 | #include <alsa/asoundlib.h> |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 27 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
pbrook | 749bc4b | 2007-11-17 17:35:54 +0000 | [diff] [blame] | 29 | #include "audio.h" |
Kővágó, Zoltán | fbb7ef5 | 2015-06-12 14:33:06 +0200 | [diff] [blame] | 30 | #include "trace.h" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 31 | |
malc | 2637872 | 2009-07-17 05:57:04 +0400 | [diff] [blame] | 32 | #pragma GCC diagnostic ignored "-Waddress" |
malc | 2637872 | 2009-07-17 05:57:04 +0400 | [diff] [blame] | 33 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 34 | #define AUDIO_CAP "alsa" |
| 35 | #include "audio_int.h" |
| 36 | |
Volker Rümelin | 2430118 | 2021-05-17 21:46:01 +0200 | [diff] [blame] | 37 | #define DEBUG_ALSA 0 |
| 38 | |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 39 | struct pollhlp { |
| 40 | snd_pcm_t *handle; |
| 41 | struct pollfd *pfds; |
| 42 | int count; |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 43 | int mask; |
Kővágó, Zoltán | 18e2c17 | 2019-08-19 01:06:55 +0200 | [diff] [blame] | 44 | AudioState *s; |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 45 | }; |
| 46 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 47 | typedef struct ALSAVoiceOut { |
| 48 | HWVoiceOut hw; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 49 | snd_pcm_t *handle; |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 50 | struct pollhlp pollhlp; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 51 | Audiodev *dev; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 52 | } ALSAVoiceOut; |
| 53 | |
| 54 | typedef struct ALSAVoiceIn { |
| 55 | HWVoiceIn hw; |
| 56 | snd_pcm_t *handle; |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 57 | struct pollhlp pollhlp; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 58 | Audiodev *dev; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 59 | } ALSAVoiceIn; |
| 60 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 61 | struct alsa_params_req { |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 62 | int freq; |
| 63 | snd_pcm_format_t fmt; |
| 64 | int nchannels; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct alsa_params_obt { |
| 68 | int freq; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 69 | AudioFormat fmt; |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 70 | int endianness; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 71 | int nchannels; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 72 | snd_pcm_uframes_t samples; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 75 | static void G_GNUC_PRINTF (2, 3) alsa_logerr (int err, const char *fmt, ...) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 76 | { |
| 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é Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 86 | static void G_GNUC_PRINTF (3, 4) alsa_logerr2 ( |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 87 | int err, |
| 88 | const char *typ, |
| 89 | const char *fmt, |
| 90 | ... |
| 91 | ) |
| 92 | { |
| 93 | va_list ap; |
| 94 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 95 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 96 | |
| 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 | |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 104 | static 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 Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 113 | g_free (pfds); |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 114 | } |
| 115 | hlp->pfds = NULL; |
| 116 | hlp->count = 0; |
| 117 | hlp->handle = NULL; |
| 118 | } |
| 119 | |
| 120 | static void alsa_anal_close1 (snd_pcm_t **handlep) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 121 | { |
| 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 | |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 129 | static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp) |
| 130 | { |
| 131 | alsa_fini_poll (hlp); |
| 132 | alsa_anal_close1 (handlep); |
| 133 | } |
| 134 | |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 135 | static 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 | |
| 145 | static 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 | |
| 155 | static 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 | |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 181 | if (!(revents & hlp->mask)) { |
Kővágó, Zoltán | fbb7ef5 | 2015-06-12 14:33:06 +0200 | [diff] [blame] | 182 | trace_alsa_revents(revents); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
| 186 | state = snd_pcm_state (hlp->handle); |
| 187 | switch (state) { |
malc | d9812b0 | 2010-02-28 18:34:21 +0300 | [diff] [blame] | 188 | case SND_PCM_STATE_SETUP: |
| 189 | alsa_recover (hlp->handle); |
| 190 | break; |
| 191 | |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 192 | 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án | 18e2c17 | 2019-08-19 01:06:55 +0200 | [diff] [blame] | 201 | audio_run(hlp->s, "alsa run (prepared)"); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 202 | break; |
| 203 | |
| 204 | case SND_PCM_STATE_RUNNING: |
Kővágó, Zoltán | 18e2c17 | 2019-08-19 01:06:55 +0200 | [diff] [blame] | 205 | audio_run(hlp->s, "alsa run (running)"); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 206 | break; |
| 207 | |
| 208 | default: |
| 209 | dolog ("Unexpected state %d\n", state); |
| 210 | } |
| 211 | } |
| 212 | |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 213 | static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask) |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 214 | { |
| 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ümelin | d1def19 | 2023-01-21 10:47:30 +0100 | [diff] [blame] | 225 | pfds = g_new0(struct pollfd, count); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 226 | |
| 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 Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 231 | g_free (pfds); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < count; ++i) { |
| 236 | if (pfds[i].events & POLLIN) { |
Fam Zheng | be93f21 | 2015-06-04 14:45:20 +0800 | [diff] [blame] | 237 | qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 238 | } |
| 239 | if (pfds[i].events & POLLOUT) { |
Kővágó, Zoltán | fbb7ef5 | 2015-06-12 14:33:06 +0200 | [diff] [blame] | 240 | trace_alsa_pollout(i, pfds[i].fd); |
Fam Zheng | be93f21 | 2015-06-04 14:45:20 +0800 | [diff] [blame] | 241 | qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 242 | } |
Kővágó, Zoltán | fbb7ef5 | 2015-06-12 14:33:06 +0200 | [diff] [blame] | 243 | trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 244 | |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 245 | } |
| 246 | hlp->pfds = pfds; |
| 247 | hlp->count = count; |
| 248 | hlp->handle = handle; |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 249 | hlp->mask = mask; |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int alsa_poll_out (HWVoiceOut *hw) |
| 254 | { |
| 255 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
| 256 | |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 257 | return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int alsa_poll_in (HWVoiceIn *hw) |
| 261 | { |
| 262 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
| 263 | |
malc | b4f763b | 2009-09-14 03:53:25 +0400 | [diff] [blame] | 264 | return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 265 | } |
| 266 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 267 | static snd_pcm_format_t aud_to_alsafmt (AudioFormat fmt, int endianness) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 268 | { |
| 269 | switch (fmt) { |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 270 | case AUDIO_FORMAT_S8: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 271 | return SND_PCM_FORMAT_S8; |
| 272 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 273 | case AUDIO_FORMAT_U8: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 274 | return SND_PCM_FORMAT_U8; |
| 275 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 276 | case AUDIO_FORMAT_S16: |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 277 | if (endianness) { |
| 278 | return SND_PCM_FORMAT_S16_BE; |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 279 | } else { |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 280 | return SND_PCM_FORMAT_S16_LE; |
| 281 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 282 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 283 | case AUDIO_FORMAT_U16: |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 284 | if (endianness) { |
| 285 | return SND_PCM_FORMAT_U16_BE; |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 286 | } else { |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 287 | return SND_PCM_FORMAT_U16_LE; |
| 288 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 289 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 290 | case AUDIO_FORMAT_S32: |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 291 | if (endianness) { |
| 292 | return SND_PCM_FORMAT_S32_BE; |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 293 | } else { |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 294 | return SND_PCM_FORMAT_S32_LE; |
| 295 | } |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 296 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 297 | case AUDIO_FORMAT_U32: |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 298 | if (endianness) { |
| 299 | return SND_PCM_FORMAT_U32_BE; |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 300 | } else { |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 301 | return SND_PCM_FORMAT_U32_LE; |
| 302 | } |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 303 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 304 | 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 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 311 | 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án | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 320 | static int alsa_to_audfmt (snd_pcm_format_t alsafmt, AudioFormat *fmt, |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 321 | int *endianness) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 322 | { |
| 323 | switch (alsafmt) { |
| 324 | case SND_PCM_FORMAT_S8: |
| 325 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 326 | *fmt = AUDIO_FORMAT_S8; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 327 | break; |
| 328 | |
| 329 | case SND_PCM_FORMAT_U8: |
| 330 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 331 | *fmt = AUDIO_FORMAT_U8; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 332 | break; |
| 333 | |
| 334 | case SND_PCM_FORMAT_S16_LE: |
| 335 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 336 | *fmt = AUDIO_FORMAT_S16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 337 | break; |
| 338 | |
| 339 | case SND_PCM_FORMAT_U16_LE: |
| 340 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 341 | *fmt = AUDIO_FORMAT_U16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | case SND_PCM_FORMAT_S16_BE: |
| 345 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 346 | *fmt = AUDIO_FORMAT_S16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 347 | break; |
| 348 | |
| 349 | case SND_PCM_FORMAT_U16_BE: |
| 350 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 351 | *fmt = AUDIO_FORMAT_U16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 352 | break; |
| 353 | |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 354 | case SND_PCM_FORMAT_S32_LE: |
| 355 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 356 | *fmt = AUDIO_FORMAT_S32; |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 357 | break; |
| 358 | |
| 359 | case SND_PCM_FORMAT_U32_LE: |
| 360 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 361 | *fmt = AUDIO_FORMAT_U32; |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 362 | break; |
| 363 | |
| 364 | case SND_PCM_FORMAT_S32_BE: |
| 365 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 366 | *fmt = AUDIO_FORMAT_S32; |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 367 | break; |
| 368 | |
| 369 | case SND_PCM_FORMAT_U32_BE: |
| 370 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 371 | *fmt = AUDIO_FORMAT_U32; |
ths | f941aa2 | 2007-02-17 22:19:29 +0000 | [diff] [blame] | 372 | break; |
| 373 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 374 | 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 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 384 | default: |
| 385 | dolog ("Unrecognized audio format %d\n", alsafmt); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 392 | static void alsa_dump_info (struct alsa_params_req *req, |
malc | 8bb414d | 2010-04-21 15:40:23 +0400 | [diff] [blame] | 393 | struct alsa_params_obt *obt, |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 394 | snd_pcm_format_t obtfmt, |
| 395 | AudiodevAlsaPerDirectionOptions *apdo) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 396 | { |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 397 | 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); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 406 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 407 | |
| 408 | static 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) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 417 | dolog ("Could not fully initialize DAC\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 418 | 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) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 424 | dolog ("Could not fully initialize DAC\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 425 | 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) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 432 | dolog ("Could not fully initialize DAC\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 433 | alsa_logerr (err, "Failed to set software parameters\n"); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 438 | static int alsa_open(bool in, struct alsa_params_req *req, |
| 439 | struct alsa_params_obt *obt, snd_pcm_t **handlep, |
| 440 | Audiodev *dev) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 441 | { |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 442 | AudiodevAlsaOptions *aopts = &dev->u.alsa; |
| 443 | AudiodevAlsaPerDirectionOptions *apdo = in ? aopts->in : aopts->out; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 444 | snd_pcm_t *handle; |
| 445 | snd_pcm_hw_params_t *hw_params; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 446 | int err; |
| 447 | unsigned int freq, nchannels; |
Markus Armbruster | ceb19c8 | 2022-11-04 17:06:49 +0100 | [diff] [blame] | 448 | const char *pcm_name = apdo->dev ?: "default"; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 449 | snd_pcm_uframes_t obt_buffer_size; |
| 450 | const char *typ = in ? "ADC" : "DAC"; |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 451 | snd_pcm_format_t obtfmt; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 452 | |
| 453 | freq = req->freq; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 454 | 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án | fbb7ef5 | 2015-06-12 14:33:06 +0200 | [diff] [blame] | 486 | if (err < 0) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 487 | alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 488 | } |
| 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án | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 507 | if (apdo->buffer_length) { |
| 508 | int dir = 0; |
| 509 | unsigned int btime = apdo->buffer_length; |
malc | f3b5298 | 2008-06-21 20:09:32 +0000 | [diff] [blame] | 510 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 511 | err = snd_pcm_hw_params_set_buffer_time_near( |
| 512 | handle, hw_params, &btime, &dir); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 513 | |
malc | 7a24c80 | 2008-06-21 17:14:57 +0000 | [diff] [blame] | 514 | if (err < 0) { |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 515 | alsa_logerr2(err, typ, "Failed to set buffer time to %" PRId32 "\n", |
| 516 | apdo->buffer_length); |
malc | 7a24c80 | 2008-06-21 17:14:57 +0000 | [diff] [blame] | 517 | goto err; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 518 | } |
malc | f3b5298 | 2008-06-21 20:09:32 +0000 | [diff] [blame] | 519 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 520 | 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 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 524 | } |
malc | 7a24c80 | 2008-06-21 17:14:57 +0000 | [diff] [blame] | 525 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 526 | if (apdo->period_length) { |
| 527 | int dir = 0; |
| 528 | unsigned int ptime = apdo->period_length; |
malc | f3b5298 | 2008-06-21 20:09:32 +0000 | [diff] [blame] | 529 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 530 | err = snd_pcm_hw_params_set_period_time_near(handle, hw_params, &ptime, |
| 531 | &dir); |
malc | 7a24c80 | 2008-06-21 17:14:57 +0000 | [diff] [blame] | 532 | |
| 533 | if (err < 0) { |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 534 | alsa_logerr2(err, typ, "Failed to set period time to %" PRId32 "\n", |
| 535 | apdo->period_length); |
malc | 7a24c80 | 2008-06-21 17:14:57 +0000 | [diff] [blame] | 536 | goto err; |
| 537 | } |
malc | f3b5298 | 2008-06-21 20:09:32 +0000 | [diff] [blame] | 538 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 539 | 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 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 543 | } |
| 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 | |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 557 | 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 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 568 | err = snd_pcm_prepare (handle); |
| 569 | if (err < 0) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 570 | alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 571 | goto err; |
| 572 | } |
| 573 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 574 | 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)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 580 | } |
| 581 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 582 | obt->nchannels = nchannels; |
| 583 | obt->freq = freq; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 584 | obt->samples = obt_buffer_size; |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 585 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 586 | *handlep = handle; |
| 587 | |
Volker Rümelin | 2430118 | 2021-05-17 21:46:01 +0200 | [diff] [blame] | 588 | if (DEBUG_ALSA || obtfmt != req->fmt || |
| 589 | obt->nchannels != req->nchannels || obt->freq != req->freq) { |
Vagrant Cascadian | f093feb | 2010-02-26 13:39:46 -0800 | [diff] [blame] | 590 | dolog ("Audio parameters for %s\n", typ); |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 591 | alsa_dump_info(req, obt, obtfmt, apdo); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 592 | } |
| 593 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 594 | return 0; |
| 595 | |
| 596 | err: |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 597 | alsa_anal_close1 (&handle); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 598 | return -1; |
| 599 | } |
| 600 | |
Volker Rümelin | 5a9d7ae | 2022-09-23 20:36:32 +0200 | [diff] [blame] | 601 | static 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án | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 637 | static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 638 | { |
| 639 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 640 | size_t pos = 0; |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 641 | size_t len_frames = len / hw->info.bytes_per_frame; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 642 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 643 | 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án | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 687 | pos += written * hw->info.bytes_per_frame; |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 688 | if (written < len_frames) { |
| 689 | break; |
| 690 | } |
| 691 | len_frames -= written; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 694 | return pos; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static void alsa_fini_out (HWVoiceOut *hw) |
| 698 | { |
| 699 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
| 700 | |
| 701 | ldebug ("alsa_fini\n"); |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 702 | alsa_anal_close (&alsa->handle, &alsa->pollhlp); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 705 | static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as, |
| 706 | void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 707 | { |
| 708 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
| 709 | struct alsa_params_req req; |
| 710 | struct alsa_params_obt obt; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 711 | snd_pcm_t *handle; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 712 | struct audsettings obt_as; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 713 | Audiodev *dev = drv_opaque; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 714 | |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 715 | req.fmt = aud_to_alsafmt (as->fmt, as->endianness); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 716 | req.freq = as->freq; |
| 717 | req.nchannels = as->nchannels; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 718 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 719 | if (alsa_open(0, &req, &obt, &handle, dev)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
| 722 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 723 | obt_as.freq = obt.freq; |
| 724 | obt_as.nchannels = obt.nchannels; |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 725 | obt_as.fmt = obt.fmt; |
| 726 | obt_as.endianness = obt.endianness; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 727 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 728 | audio_pcm_init_info (&hw->info, &obt_as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 729 | hw->samples = obt.samples; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 730 | |
Kővágó, Zoltán | 18e2c17 | 2019-08-19 01:06:55 +0200 | [diff] [blame] | 731 | alsa->pollhlp.s = hw->s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 732 | alsa->handle = handle; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 733 | alsa->dev = dev; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | |
Jindrich Makovicka | 38cc9b6 | 2010-10-17 20:17:19 +0200 | [diff] [blame] | 737 | #define VOICE_CTL_PAUSE 0 |
| 738 | #define VOICE_CTL_PREPARE 1 |
| 739 | #define VOICE_CTL_START 2 |
| 740 | |
| 741 | static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl) |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 742 | { |
| 743 | int err; |
| 744 | |
Jindrich Makovicka | 38cc9b6 | 2010-10-17 20:17:19 +0200 | [diff] [blame] | 745 | if (ctl == VOICE_CTL_PAUSE) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 746 | err = snd_pcm_drop (handle); |
| 747 | if (err < 0) { |
bellard | 32d448c | 2005-11-20 18:53:05 +0000 | [diff] [blame] | 748 | alsa_logerr (err, "Could not stop %s\n", typ); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 749 | return -1; |
| 750 | } |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 751 | } else { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 752 | err = snd_pcm_prepare (handle); |
| 753 | if (err < 0) { |
bellard | 32d448c | 2005-11-20 18:53:05 +0000 | [diff] [blame] | 754 | alsa_logerr (err, "Could not prepare handle for %s\n", typ); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 755 | return -1; |
| 756 | } |
Jindrich Makovicka | 38cc9b6 | 2010-10-17 20:17:19 +0200 | [diff] [blame] | 757 | 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 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 764 | } |
| 765 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 769 | static void alsa_enable_out(HWVoiceOut *hw, bool enable) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 770 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 771 | ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 772 | AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 773 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 774 | if (enable) { |
| 775 | bool poll_mode = apdo->try_poll; |
malc | 301901b | 2009-10-02 02:37:40 +0400 | [diff] [blame] | 776 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 777 | ldebug("enabling voice\n"); |
| 778 | if (poll_mode && alsa_poll_out(hw)) { |
| 779 | poll_mode = 0; |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 780 | } |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 781 | hw->poll_mode = poll_mode; |
| 782 | alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PREPARE); |
| 783 | } else { |
| 784 | ldebug("disabling voice\n"); |
Jindrich Makovicka | 22d948a | 2010-10-17 19:28:14 +0200 | [diff] [blame] | 785 | if (hw->poll_mode) { |
| 786 | hw->poll_mode = 0; |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 787 | alsa_fini_poll(&alsa->pollhlp); |
Jindrich Makovicka | 22d948a | 2010-10-17 19:28:14 +0200 | [diff] [blame] | 788 | } |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 789 | alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PAUSE); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 790 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 793 | static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 794 | { |
| 795 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
| 796 | struct alsa_params_req req; |
| 797 | struct alsa_params_obt obt; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 798 | snd_pcm_t *handle; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 799 | struct audsettings obt_as; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 800 | Audiodev *dev = drv_opaque; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 801 | |
Michael Walle | d66bddd | 2011-01-08 17:53:30 +0100 | [diff] [blame] | 802 | req.fmt = aud_to_alsafmt (as->fmt, as->endianness); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 803 | req.freq = as->freq; |
| 804 | req.nchannels = as->nchannels; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 805 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 806 | if (alsa_open(1, &req, &obt, &handle, dev)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 807 | return -1; |
| 808 | } |
| 809 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 810 | obt_as.freq = obt.freq; |
| 811 | obt_as.nchannels = obt.nchannels; |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 812 | obt_as.fmt = obt.fmt; |
| 813 | obt_as.endianness = obt.endianness; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 814 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 815 | audio_pcm_init_info (&hw->info, &obt_as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 816 | hw->samples = obt.samples; |
| 817 | |
Kővágó, Zoltán | 18e2c17 | 2019-08-19 01:06:55 +0200 | [diff] [blame] | 818 | alsa->pollhlp.s = hw->s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 819 | alsa->handle = handle; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 820 | alsa->dev = dev; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static void alsa_fini_in (HWVoiceIn *hw) |
| 825 | { |
| 826 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
| 827 | |
malc | 6ebfda1 | 2009-09-14 03:51:48 +0400 | [diff] [blame] | 828 | alsa_anal_close (&alsa->handle, &alsa->pollhlp); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 831 | static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 832 | { |
| 833 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 834 | size_t pos = 0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 835 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 836 | while (len) { |
| 837 | void *dst = advance(buf, pos); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 838 | snd_pcm_sframes_t nread; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 839 | |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 840 | nread = snd_pcm_readi( |
| 841 | alsa->handle, dst, len / hw->info.bytes_per_frame); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 842 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 843 | if (nread <= 0) { |
| 844 | switch (nread) { |
| 845 | case 0: |
| 846 | trace_alsa_read_zero(len); |
Philippe Mathieu-Daudé | 3a1bdd1 | 2020-02-18 10:43:51 +0100 | [diff] [blame] | 847 | return pos; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 848 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 849 | case -EPIPE: |
| 850 | if (alsa_recover(alsa->handle)) { |
| 851 | alsa_logerr(nread, "Failed to read %zu frames\n", len); |
| 852 | return pos; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 853 | } |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 854 | 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é | 3a1bdd1 | 2020-02-18 10:43:51 +0100 | [diff] [blame] | 863 | return pos; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 864 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 865 | } |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 866 | |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 867 | pos += nread * hw->info.bytes_per_frame; |
| 868 | len -= nread * hw->info.bytes_per_frame; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 871 | return pos; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 874 | static void alsa_enable_in(HWVoiceIn *hw, bool enable) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 875 | { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 876 | ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 877 | AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 878 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 879 | if (enable) { |
| 880 | bool poll_mode = apdo->try_poll; |
malc | a628b86 | 2009-10-03 03:30:06 +0400 | [diff] [blame] | 881 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 882 | ldebug("enabling voice\n"); |
| 883 | if (poll_mode && alsa_poll_in(hw)) { |
| 884 | poll_mode = 0; |
malc | a628b86 | 2009-10-03 03:30:06 +0400 | [diff] [blame] | 885 | } |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 886 | hw->poll_mode = poll_mode; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 887 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 888 | alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_START); |
| 889 | } else { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 890 | ldebug ("disabling voice\n"); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 891 | if (hw->poll_mode) { |
| 892 | hw->poll_mode = 0; |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 893 | alsa_fini_poll(&alsa->pollhlp); |
malc | 8b438ba | 2009-09-12 02:29:25 +0400 | [diff] [blame] | 894 | } |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 895 | alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_PAUSE); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 896 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 899 | static 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án | 765b37d | 2015-06-03 23:03:48 +0200 | [diff] [blame] | 906 | |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 907 | static void *alsa_audio_init(Audiodev *dev) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 908 | { |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 909 | 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ümelin | 4674473 | 2023-01-21 10:47:34 +0100 | [diff] [blame] | 916 | /* don't set has_* so alsa_open can identify it wasn't set by the user */ |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 917 | if (!dev->u.alsa.out->has_period_length) { |
Volker Rümelin | 4674473 | 2023-01-21 10:47:34 +0100 | [diff] [blame] | 918 | /* 256 frames assuming 44100Hz */ |
| 919 | dev->u.alsa.out->period_length = 5805; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 920 | } |
| 921 | if (!dev->u.alsa.out->has_buffer_length) { |
| 922 | /* 4096 frames assuming 44100Hz */ |
Volker Rümelin | 4674473 | 2023-01-21 10:47:34 +0100 | [diff] [blame] | 923 | dev->u.alsa.out->buffer_length = 92880; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 926 | if (!dev->u.alsa.in->has_period_length) { |
Volker Rümelin | 5140ad8 | 2023-01-21 10:47:35 +0100 | [diff] [blame] | 927 | /* 256 frames assuming 44100Hz */ |
| 928 | dev->u.alsa.in->period_length = 5805; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 929 | } |
| 930 | if (!dev->u.alsa.in->has_buffer_length) { |
Volker Rümelin | 5140ad8 | 2023-01-21 10:47:35 +0100 | [diff] [blame] | 931 | /* 4096 frames assuming 44100Hz */ |
| 932 | dev->u.alsa.in->buffer_length = 92880; |
Kővágó, Zoltán | a93f328 | 2019-03-08 23:34:16 +0100 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | return dev; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static void alsa_audio_fini (void *opaque) |
| 939 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 940 | } |
| 941 | |
blueswir1 | 35f4b58 | 2008-10-06 18:08:30 +0000 | [diff] [blame] | 942 | static struct audio_pcm_ops alsa_pcm_ops = { |
Juan Quintela | 1dd3e4d | 2009-08-11 02:31:15 +0200 | [diff] [blame] | 943 | .init_out = alsa_init_out, |
| 944 | .fini_out = alsa_fini_out, |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 945 | .write = alsa_write, |
Volker Rümelin | 5a9d7ae | 2022-09-23 20:36:32 +0200 | [diff] [blame] | 946 | .buffer_get_free = alsa_buffer_get_free, |
Volker Rümelin | fdc8c5f | 2020-01-23 08:49:39 +0100 | [diff] [blame] | 947 | .run_buffer_out = audio_generic_run_buffer_out, |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 948 | .enable_out = alsa_enable_out, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 949 | |
Juan Quintela | 1dd3e4d | 2009-08-11 02:31:15 +0200 | [diff] [blame] | 950 | .init_in = alsa_init_in, |
| 951 | .fini_in = alsa_fini_in, |
Kővágó, Zoltán | 286a5d2 | 2019-09-19 23:24:10 +0200 | [diff] [blame] | 952 | .read = alsa_read, |
Volker Rümelin | a2893c8 | 2021-01-10 11:02:24 +0100 | [diff] [blame] | 953 | .run_buffer_in = audio_generic_run_buffer_in, |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 954 | .enable_in = alsa_enable_in, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 955 | }; |
| 956 | |
Gerd Hoffmann | d3893a3 | 2018-03-06 08:40:47 +0100 | [diff] [blame] | 957 | static struct audio_driver alsa_audio_driver = { |
Juan Quintela | bee37f3 | 2009-08-11 02:31:14 +0200 | [diff] [blame] | 958 | .name = "alsa", |
| 959 | .descr = "ALSA http://www.alsa-project.org", |
Juan Quintela | bee37f3 | 2009-08-11 02:31:14 +0200 | [diff] [blame] | 960 | .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) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 968 | }; |
Gerd Hoffmann | d3893a3 | 2018-03-06 08:40:47 +0100 | [diff] [blame] | 969 | |
| 970 | static void register_audio_alsa(void) |
| 971 | { |
| 972 | audio_driver_register(&alsa_audio_driver); |
| 973 | } |
| 974 | type_init(register_audio_alsa); |