Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1 | /* pcm.c |
| 2 | ** |
| 3 | ** Copyright 2011, The Android Open Source Project |
| 4 | ** |
| 5 | ** Redistribution and use in source and binary forms, with or without |
| 6 | ** modification, are permitted provided that the following conditions are met: |
| 7 | ** * Redistributions of source code must retain the above copyright |
| 8 | ** notice, this list of conditions and the following disclaimer. |
| 9 | ** * Redistributions in binary form must reproduce the above copyright |
| 10 | ** notice, this list of conditions and the following disclaimer in the |
| 11 | ** documentation and/or other materials provided with the distribution. |
| 12 | ** * Neither the name of The Android Open Source Project nor the names of |
| 13 | ** its contributors may be used to endorse or promote products derived |
| 14 | ** from this software without specific prior written permission. |
| 15 | ** |
| 16 | ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND |
| 17 | ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE |
| 20 | ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 | ** DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <string.h> |
| 34 | #include <errno.h> |
| 35 | #include <unistd.h> |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 36 | #include <poll.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 37 | |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/time.h> |
Dima Krasner | 696c448 | 2016-03-05 19:50:02 +0200 | [diff] [blame] | 41 | #include <time.h> |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 42 | #include <limits.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 43 | |
| 44 | #include <linux/ioctl.h> |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 45 | |
| 46 | #ifndef __force |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 47 | #define __force |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | #ifndef __bitwise |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 51 | #define __bitwise |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #ifndef __user |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 55 | #define __user |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 56 | #endif |
| 57 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 58 | #include <sound/asound.h> |
| 59 | |
Ricardo Biehl Pasquali | 04952ee | 2016-10-05 20:32:09 -0300 | [diff] [blame] | 60 | #include <tinyalsa/pcm.h> |
Taylor Holberton | ea06b97 | 2017-04-06 23:14:14 -0700 | [diff] [blame] | 61 | #include <tinyalsa/limits.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 62 | |
Taylor Holberton | 1f74156 | 2018-11-28 16:33:24 -0500 | [diff] [blame] | 63 | #ifndef PARAM_MAX |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 64 | #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL |
Taylor Holberton | 1f74156 | 2018-11-28 16:33:24 -0500 | [diff] [blame] | 65 | #endif /* PARAM_MAX */ |
| 66 | |
| 67 | #ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 68 | #define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2) |
Taylor Holberton | 1f74156 | 2018-11-28 16:33:24 -0500 | [diff] [blame] | 69 | #endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 70 | |
| 71 | static inline int param_is_mask(int p) |
| 72 | { |
| 73 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) && |
| 74 | (p <= SNDRV_PCM_HW_PARAM_LAST_MASK); |
| 75 | } |
| 76 | |
| 77 | static inline int param_is_interval(int p) |
| 78 | { |
| 79 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) && |
| 80 | (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL); |
| 81 | } |
| 82 | |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 83 | static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n) |
| 84 | { |
Taylor Holberton | 25976dc | 2017-04-10 11:46:40 -0700 | [diff] [blame] | 85 | return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 88 | static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n) |
| 89 | { |
| 90 | return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); |
| 91 | } |
| 92 | |
| 93 | static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n) |
| 94 | { |
| 95 | return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]); |
| 96 | } |
| 97 | |
| 98 | static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit) |
| 99 | { |
| 100 | if (bit >= SNDRV_MASK_MAX) |
| 101 | return; |
| 102 | if (param_is_mask(n)) { |
| 103 | struct snd_mask *m = param_to_mask(p, n); |
| 104 | m->bits[0] = 0; |
| 105 | m->bits[1] = 0; |
| 106 | m->bits[bit >> 5] |= (1 << (bit & 31)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 111 | { |
| 112 | if (param_is_interval(n)) { |
| 113 | struct snd_interval *i = param_to_interval(p, n); |
| 114 | i->min = val; |
| 115 | } |
| 116 | } |
| 117 | |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 118 | static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n) |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 119 | { |
| 120 | if (param_is_interval(n)) { |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 121 | const struct snd_interval *i = param_get_interval(p, n); |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 122 | return i->min; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 127 | static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n) |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 128 | { |
| 129 | if (param_is_interval(n)) { |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 130 | const struct snd_interval *i = param_get_interval(p, n); |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 131 | return i->max; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 136 | static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 137 | { |
| 138 | if (param_is_interval(n)) { |
| 139 | struct snd_interval *i = param_to_interval(p, n); |
| 140 | i->min = val; |
| 141 | i->max = val; |
| 142 | i->integer = 1; |
| 143 | } |
| 144 | } |
| 145 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 146 | static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n) |
| 147 | { |
| 148 | if (param_is_interval(n)) { |
| 149 | struct snd_interval *i = param_to_interval(p, n); |
| 150 | if (i->integer) |
| 151 | return i->max; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 156 | static void param_init(struct snd_pcm_hw_params *p) |
| 157 | { |
| 158 | int n; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 159 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 160 | memset(p, 0, sizeof(*p)); |
| 161 | for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK; |
| 162 | n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) { |
| 163 | struct snd_mask *m = param_to_mask(p, n); |
| 164 | m->bits[0] = ~0; |
| 165 | m->bits[1] = ~0; |
| 166 | } |
| 167 | for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; |
| 168 | n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) { |
| 169 | struct snd_interval *i = param_to_interval(p, n); |
| 170 | i->min = 0; |
| 171 | i->max = ~0; |
| 172 | } |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 173 | p->rmask = ~0U; |
| 174 | p->cmask = 0; |
| 175 | p->info = ~0U; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 178 | static unsigned int pcm_format_to_alsa(enum pcm_format format) |
| 179 | { |
| 180 | switch (format) { |
| 181 | |
| 182 | case PCM_FORMAT_S8: |
| 183 | return SNDRV_PCM_FORMAT_S8; |
| 184 | |
| 185 | default: |
| 186 | case PCM_FORMAT_S16_LE: |
| 187 | return SNDRV_PCM_FORMAT_S16_LE; |
| 188 | case PCM_FORMAT_S16_BE: |
| 189 | return SNDRV_PCM_FORMAT_S16_BE; |
| 190 | |
| 191 | case PCM_FORMAT_S24_LE: |
| 192 | return SNDRV_PCM_FORMAT_S24_LE; |
| 193 | case PCM_FORMAT_S24_BE: |
| 194 | return SNDRV_PCM_FORMAT_S24_BE; |
| 195 | |
| 196 | case PCM_FORMAT_S24_3LE: |
| 197 | return SNDRV_PCM_FORMAT_S24_3LE; |
| 198 | case PCM_FORMAT_S24_3BE: |
| 199 | return SNDRV_PCM_FORMAT_S24_3BE; |
| 200 | |
| 201 | case PCM_FORMAT_S32_LE: |
| 202 | return SNDRV_PCM_FORMAT_S32_LE; |
| 203 | case PCM_FORMAT_S32_BE: |
| 204 | return SNDRV_PCM_FORMAT_S32_BE; |
| 205 | }; |
| 206 | } |
| 207 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 208 | #define PCM_ERROR_MAX 128 |
| 209 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 210 | /** A PCM handle. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 211 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 212 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 213 | struct pcm { |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 214 | /** The PCM's file descriptor */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 215 | int fd; |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 216 | /** Flags that were passed to @ref pcm_open */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 217 | unsigned int flags; |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 218 | /** The number of underruns that have occured */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 219 | int underruns; |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 220 | /** Size of the buffer */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 221 | unsigned int buffer_size; |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 222 | /** The boundary for ring buffer pointers */ |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 223 | unsigned int boundary; |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 224 | /** Description of the last error that occured */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 225 | char error[PCM_ERROR_MAX]; |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 226 | /** Configuration that was passed to @ref pcm_open */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 227 | struct pcm_config config; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 228 | struct snd_pcm_mmap_status *mmap_status; |
| 229 | struct snd_pcm_mmap_control *mmap_control; |
| 230 | struct snd_pcm_sync_ptr *sync_ptr; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 231 | void *mmap_buffer; |
| 232 | unsigned int noirq_frames_per_msec; |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 233 | /** The delay of the PCM, in terms of frames */ |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 234 | long pcm_delay; |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 235 | /** The subdevice corresponding to the PCM */ |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 236 | unsigned int subdevice; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 239 | static int oops(struct pcm *pcm, int e, const char *fmt, ...) |
| 240 | { |
| 241 | va_list ap; |
| 242 | int sz; |
| 243 | |
| 244 | va_start(ap, fmt); |
| 245 | vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap); |
| 246 | va_end(ap); |
| 247 | sz = strlen(pcm->error); |
| 248 | |
| 249 | if (errno) |
| 250 | snprintf(pcm->error + sz, PCM_ERROR_MAX - sz, |
| 251 | ": %s", strerror(e)); |
| 252 | return -1; |
| 253 | } |
| 254 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 255 | /** Gets the buffer size of the PCM. |
| 256 | * @param pcm A PCM handle. |
| 257 | * @return The buffer size of the PCM. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 258 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 259 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 260 | unsigned int pcm_get_buffer_size(const struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 261 | { |
| 262 | return pcm->buffer_size; |
| 263 | } |
| 264 | |
Taylor Holberton | 77979a8 | 2016-12-01 20:04:04 -0800 | [diff] [blame] | 265 | /** Gets the channel count of the PCM. |
| 266 | * @param pcm A PCM handle. |
| 267 | * @return The channel count of the PCM. |
| 268 | * @ingroup libtinyalsa-pcm |
| 269 | */ |
| 270 | unsigned int pcm_get_channels(const struct pcm *pcm) |
| 271 | { |
Taylor Holberton | 25976dc | 2017-04-10 11:46:40 -0700 | [diff] [blame] | 272 | return pcm->config.channels; |
Taylor Holberton | 77979a8 | 2016-12-01 20:04:04 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Taylor Holberton | 08bb590 | 2017-04-10 11:45:44 -0700 | [diff] [blame] | 275 | /** Gets the PCM configuration. |
| 276 | * @param pcm A PCM handle. |
| 277 | * @return The PCM configuration. |
| 278 | * This function only returns NULL if |
| 279 | * @p pcm is NULL. |
| 280 | * @ingroup libtinyalsa-pcm |
| 281 | * */ |
| 282 | const struct pcm_config * pcm_get_config(const struct pcm *pcm) |
| 283 | { |
| 284 | if (pcm == NULL) |
| 285 | return NULL; |
| 286 | return &pcm->config; |
| 287 | } |
| 288 | |
Taylor Holberton | 77979a8 | 2016-12-01 20:04:04 -0800 | [diff] [blame] | 289 | /** Gets the rate of the PCM. |
| 290 | * The rate is given in frames per second. |
| 291 | * @param pcm A PCM handle. |
| 292 | * @return The rate of the PCM. |
| 293 | * @ingroup libtinyalsa-pcm |
| 294 | */ |
| 295 | unsigned int pcm_get_rate(const struct pcm *pcm) |
| 296 | { |
Taylor Holberton | 25976dc | 2017-04-10 11:46:40 -0700 | [diff] [blame] | 297 | return pcm->config.rate; |
Taylor Holberton | 77979a8 | 2016-12-01 20:04:04 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** Gets the format of the PCM. |
| 301 | * @param pcm A PCM handle. |
| 302 | * @return The format of the PCM. |
| 303 | * @ingroup libtinyalsa-pcm |
| 304 | */ |
| 305 | enum pcm_format pcm_get_format(const struct pcm *pcm) |
| 306 | { |
Taylor Holberton | 25976dc | 2017-04-10 11:46:40 -0700 | [diff] [blame] | 307 | return pcm->config.format; |
Taylor Holberton | 77979a8 | 2016-12-01 20:04:04 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 310 | /** Gets the file descriptor of the PCM. |
| 311 | * Useful for extending functionality of the PCM when needed. |
Taylor Holberton | d265c27 | 2016-11-23 13:22:56 -0800 | [diff] [blame] | 312 | * @param pcm A PCM handle. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 313 | * @return The file descriptor of the PCM. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 314 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 315 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 316 | int pcm_get_file_descriptor(const struct pcm *pcm) |
Taylor Holberton | bb40260 | 2016-08-03 10:15:46 -0400 | [diff] [blame] | 317 | { |
| 318 | return pcm->fd; |
| 319 | } |
| 320 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 321 | /** Gets the error message for the last error that occured. |
| 322 | * If no error occured and this function is called, the results are undefined. |
| 323 | * @param pcm A PCM handle. |
| 324 | * @return The error message of the last error that occured. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 325 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 326 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 327 | const char* pcm_get_error(const struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 328 | { |
| 329 | return pcm->error; |
| 330 | } |
| 331 | |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 332 | /** Sets the PCM configuration. |
| 333 | * @param pcm A PCM handle. |
| 334 | * @param config The configuration to use for the |
| 335 | * PCM. This parameter may be NULL, in which case |
| 336 | * the default configuration is used. |
| 337 | * @returns Zero on success, a negative errno value |
| 338 | * on failure. |
| 339 | * @ingroup libtinyalsa-pcm |
| 340 | * */ |
| 341 | int pcm_set_config(struct pcm *pcm, const struct pcm_config *config) |
| 342 | { |
| 343 | if (pcm == NULL) |
| 344 | return -EFAULT; |
| 345 | else if (config == NULL) { |
| 346 | config = &pcm->config; |
| 347 | pcm->config.channels = 2; |
| 348 | pcm->config.rate = 48000; |
| 349 | pcm->config.period_size = 1024; |
| 350 | pcm->config.period_count = 4; |
| 351 | pcm->config.format = PCM_FORMAT_S16_LE; |
| 352 | pcm->config.start_threshold = config->period_count * config->period_size; |
| 353 | pcm->config.stop_threshold = config->period_count * config->period_size; |
| 354 | pcm->config.silence_threshold = 0; |
| 355 | } else |
| 356 | pcm->config = *config; |
| 357 | |
| 358 | struct snd_pcm_hw_params params; |
| 359 | param_init(¶ms); |
| 360 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 361 | pcm_format_to_alsa(config->format)); |
| 362 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 363 | SNDRV_PCM_SUBFORMAT_STD); |
| 364 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 365 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 366 | pcm_format_to_bits(config->format)); |
| 367 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 368 | pcm_format_to_bits(config->format) * config->channels); |
| 369 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 370 | config->channels); |
| 371 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 372 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 373 | |
| 374 | if (pcm->flags & PCM_NOIRQ) { |
| 375 | |
| 376 | if (!(pcm->flags & PCM_MMAP)) { |
| 377 | oops(pcm, -EINVAL, "noirq only currently supported with mmap()."); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; |
| 382 | pcm->noirq_frames_per_msec = config->rate / 1000; |
| 383 | } |
| 384 | |
| 385 | if (pcm->flags & PCM_MMAP) |
| 386 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 387 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 388 | else |
| 389 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 390 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 391 | |
| 392 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 393 | int errno_copy = errno; |
| 394 | oops(pcm, -errno, "cannot set hw params"); |
| 395 | return -errno_copy; |
| 396 | } |
| 397 | |
| 398 | /* get our refined hw_params */ |
| 399 | pcm->config.period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 400 | pcm->config.period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 401 | pcm->buffer_size = config->period_count * config->period_size; |
| 402 | |
| 403 | if (pcm->flags & PCM_MMAP) { |
| 404 | pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size), |
| 405 | PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0); |
| 406 | if (pcm->mmap_buffer == MAP_FAILED) { |
| 407 | int errno_copy = errno; |
| 408 | oops(pcm, -errno, "failed to mmap buffer %d bytes\n", |
| 409 | pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 410 | return -errno_copy; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | struct snd_pcm_sw_params sparams; |
| 415 | memset(&sparams, 0, sizeof(sparams)); |
| 416 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE; |
| 417 | sparams.period_step = 1; |
| 418 | sparams.avail_min = 1; |
| 419 | |
| 420 | if (!config->start_threshold) { |
| 421 | if (pcm->flags & PCM_IN) |
| 422 | pcm->config.start_threshold = sparams.start_threshold = 1; |
| 423 | else |
| 424 | pcm->config.start_threshold = sparams.start_threshold = |
| 425 | config->period_count * config->period_size / 2; |
| 426 | } else |
| 427 | sparams.start_threshold = config->start_threshold; |
| 428 | |
| 429 | /* pick a high stop threshold - todo: does this need further tuning */ |
| 430 | if (!config->stop_threshold) { |
| 431 | if (pcm->flags & PCM_IN) |
| 432 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 433 | config->period_count * config->period_size * 10; |
| 434 | else |
| 435 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 436 | config->period_count * config->period_size; |
| 437 | } |
| 438 | else |
| 439 | sparams.stop_threshold = config->stop_threshold; |
| 440 | |
| 441 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 442 | sparams.silence_size = 0; |
| 443 | sparams.silence_threshold = config->silence_threshold; |
| 444 | pcm->boundary = sparams.boundary = pcm->buffer_size; |
| 445 | |
| 446 | while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size) |
| 447 | pcm->boundary *= 2; |
| 448 | |
| 449 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 450 | int errno_copy = errno; |
| 451 | oops(pcm, -errno, "cannot set sw params"); |
| 452 | return -errno_copy; |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 458 | /** Gets the subdevice on which the pcm has been opened. |
| 459 | * @param pcm A PCM handle. |
| 460 | * @return The subdevice on which the pcm has been opened */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 461 | unsigned int pcm_get_subdevice(const struct pcm *pcm) |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 462 | { |
| 463 | return pcm->subdevice; |
| 464 | } |
| 465 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 466 | /** Determines the number of bits occupied by a @ref pcm_format. |
| 467 | * @param format A PCM format. |
| 468 | * @return The number of bits associated with @p format |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 469 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 470 | */ |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 471 | unsigned int pcm_format_to_bits(enum pcm_format format) |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 472 | { |
| 473 | switch (format) { |
| 474 | case PCM_FORMAT_S32_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 475 | case PCM_FORMAT_S32_BE: |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 476 | case PCM_FORMAT_S24_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 477 | case PCM_FORMAT_S24_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 478 | return 32; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 479 | case PCM_FORMAT_S24_3LE: |
| 480 | case PCM_FORMAT_S24_3BE: |
| 481 | return 24; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 482 | default: |
| 483 | case PCM_FORMAT_S16_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 484 | case PCM_FORMAT_S16_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 485 | return 16; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 486 | case PCM_FORMAT_S8: |
| 487 | return 8; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 488 | }; |
| 489 | } |
| 490 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 491 | /** Determines how many frames of a PCM can fit into a number of bytes. |
| 492 | * @param pcm A PCM handle. |
| 493 | * @param bytes The number of bytes. |
| 494 | * @return The number of frames that may fit into @p bytes |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 495 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 496 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 497 | unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 498 | { |
| 499 | return bytes / (pcm->config.channels * |
| 500 | (pcm_format_to_bits(pcm->config.format) >> 3)); |
| 501 | } |
| 502 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 503 | /** Determines how many bytes are occupied by a number of frames of a PCM. |
| 504 | * @param pcm A PCM handle. |
| 505 | * @param frames The number of frames of a PCM. |
| 506 | * @return The bytes occupied by @p frames. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 507 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 508 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 509 | unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 510 | { |
| 511 | return frames * pcm->config.channels * |
| 512 | (pcm_format_to_bits(pcm->config.format) >> 3); |
| 513 | } |
| 514 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 515 | static int pcm_sync_ptr(struct pcm *pcm, int flags) |
| 516 | { |
Ricardo Biehl Pasquali | c144675 | 2018-12-18 23:13:05 -0200 | [diff] [blame] | 517 | if (pcm->sync_ptr == NULL) { |
| 518 | /* status and control are mmaped */ |
| 519 | |
| 520 | if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { |
| 521 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) { |
| 522 | oops(pcm, errno, "failed to sync hardware pointer"); |
| 523 | return -1; |
| 524 | } |
| 525 | } |
| 526 | } else { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 527 | pcm->sync_ptr->flags = flags; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 528 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) { |
| 529 | oops(pcm, errno, "failed to sync mmap ptr"); |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 530 | return -1; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 531 | } |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 532 | } |
Ricardo Biehl Pasquali | c144675 | 2018-12-18 23:13:05 -0200 | [diff] [blame] | 533 | |
Miguel Gaio | c9f97da | 2018-07-17 10:05:54 +0200 | [diff] [blame] | 534 | return 0; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 537 | static int pcm_hw_mmap_status(struct pcm *pcm) |
| 538 | { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 539 | if (pcm->sync_ptr) |
| 540 | return 0; |
| 541 | |
| 542 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 543 | pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED, |
| 544 | pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS); |
| 545 | if (pcm->mmap_status == MAP_FAILED) |
| 546 | pcm->mmap_status = NULL; |
| 547 | if (!pcm->mmap_status) |
| 548 | goto mmap_error; |
| 549 | |
| 550 | pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE, |
| 551 | MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL); |
| 552 | if (pcm->mmap_control == MAP_FAILED) |
| 553 | pcm->mmap_control = NULL; |
| 554 | if (!pcm->mmap_control) { |
| 555 | munmap(pcm->mmap_status, page_size); |
| 556 | pcm->mmap_status = NULL; |
| 557 | goto mmap_error; |
| 558 | } |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 559 | |
| 560 | return 0; |
| 561 | |
| 562 | mmap_error: |
| 563 | |
| 564 | pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr)); |
| 565 | if (!pcm->sync_ptr) |
| 566 | return -ENOMEM; |
| 567 | pcm->mmap_status = &pcm->sync_ptr->s.status; |
| 568 | pcm->mmap_control = &pcm->sync_ptr->c.control; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static void pcm_hw_munmap_status(struct pcm *pcm) { |
| 574 | if (pcm->sync_ptr) { |
| 575 | free(pcm->sync_ptr); |
| 576 | pcm->sync_ptr = NULL; |
| 577 | } else { |
| 578 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 579 | if (pcm->mmap_status) |
| 580 | munmap(pcm->mmap_status, page_size); |
| 581 | if (pcm->mmap_control) |
| 582 | munmap(pcm->mmap_control, page_size); |
| 583 | } |
| 584 | pcm->mmap_status = NULL; |
| 585 | pcm->mmap_control = NULL; |
| 586 | } |
| 587 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 588 | static struct pcm bad_pcm = { |
| 589 | .fd = -1, |
| 590 | }; |
| 591 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 592 | /** Gets the hardware parameters of a PCM, without created a PCM handle. |
| 593 | * @param card The card of the PCM. |
| 594 | * The default card is zero. |
| 595 | * @param device The device of the PCM. |
| 596 | * The default device is zero. |
| 597 | * @param flags Specifies whether the PCM is an input or output. |
| 598 | * May be one of the following: |
| 599 | * - @ref PCM_IN |
| 600 | * - @ref PCM_OUT |
| 601 | * @return On success, the hardware parameters of the PCM; on failure, NULL. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 602 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 603 | */ |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 604 | struct pcm_params *pcm_params_get(unsigned int card, unsigned int device, |
| 605 | unsigned int flags) |
| 606 | { |
| 607 | struct snd_pcm_hw_params *params; |
| 608 | char fn[256]; |
| 609 | int fd; |
| 610 | |
| 611 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 612 | flags & PCM_IN ? 'c' : 'p'); |
| 613 | |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 614 | if (flags & PCM_NONBLOCK) |
| 615 | fd = open(fn, O_RDWR | O_NONBLOCK); |
| 616 | else |
| 617 | fd = open(fn, O_RDWR); |
| 618 | |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 619 | if (fd < 0) { |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 620 | fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno)); |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 621 | goto err_open; |
| 622 | } |
| 623 | |
| 624 | params = calloc(1, sizeof(struct snd_pcm_hw_params)); |
| 625 | if (!params) |
| 626 | goto err_calloc; |
| 627 | |
| 628 | param_init(params); |
| 629 | if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) { |
| 630 | fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno); |
| 631 | goto err_hw_refine; |
| 632 | } |
| 633 | |
| 634 | close(fd); |
| 635 | |
| 636 | return (struct pcm_params *)params; |
| 637 | |
| 638 | err_hw_refine: |
| 639 | free(params); |
| 640 | err_calloc: |
| 641 | close(fd); |
| 642 | err_open: |
| 643 | return NULL; |
| 644 | } |
| 645 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 646 | /** Frees the hardware parameters returned by @ref pcm_params_get. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 647 | * @param pcm_params Hardware parameters of a PCM. |
| 648 | * May be NULL. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 649 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 650 | */ |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 651 | void pcm_params_free(struct pcm_params *pcm_params) |
| 652 | { |
| 653 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 654 | |
| 655 | if (params) |
| 656 | free(params); |
| 657 | } |
| 658 | |
| 659 | static int pcm_param_to_alsa(enum pcm_param param) |
| 660 | { |
| 661 | switch (param) { |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 662 | case PCM_PARAM_ACCESS: |
| 663 | return SNDRV_PCM_HW_PARAM_ACCESS; |
| 664 | case PCM_PARAM_FORMAT: |
| 665 | return SNDRV_PCM_HW_PARAM_FORMAT; |
| 666 | case PCM_PARAM_SUBFORMAT: |
| 667 | return SNDRV_PCM_HW_PARAM_SUBFORMAT; |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 668 | case PCM_PARAM_SAMPLE_BITS: |
| 669 | return SNDRV_PCM_HW_PARAM_SAMPLE_BITS; |
| 670 | break; |
| 671 | case PCM_PARAM_FRAME_BITS: |
| 672 | return SNDRV_PCM_HW_PARAM_FRAME_BITS; |
| 673 | break; |
| 674 | case PCM_PARAM_CHANNELS: |
| 675 | return SNDRV_PCM_HW_PARAM_CHANNELS; |
| 676 | break; |
| 677 | case PCM_PARAM_RATE: |
| 678 | return SNDRV_PCM_HW_PARAM_RATE; |
| 679 | break; |
| 680 | case PCM_PARAM_PERIOD_TIME: |
| 681 | return SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 682 | break; |
| 683 | case PCM_PARAM_PERIOD_SIZE: |
| 684 | return SNDRV_PCM_HW_PARAM_PERIOD_SIZE; |
| 685 | break; |
| 686 | case PCM_PARAM_PERIOD_BYTES: |
| 687 | return SNDRV_PCM_HW_PARAM_PERIOD_BYTES; |
| 688 | break; |
| 689 | case PCM_PARAM_PERIODS: |
| 690 | return SNDRV_PCM_HW_PARAM_PERIODS; |
| 691 | break; |
| 692 | case PCM_PARAM_BUFFER_TIME: |
| 693 | return SNDRV_PCM_HW_PARAM_BUFFER_TIME; |
| 694 | break; |
| 695 | case PCM_PARAM_BUFFER_SIZE: |
| 696 | return SNDRV_PCM_HW_PARAM_BUFFER_SIZE; |
| 697 | break; |
| 698 | case PCM_PARAM_BUFFER_BYTES: |
| 699 | return SNDRV_PCM_HW_PARAM_BUFFER_BYTES; |
| 700 | break; |
| 701 | case PCM_PARAM_TICK_TIME: |
| 702 | return SNDRV_PCM_HW_PARAM_TICK_TIME; |
| 703 | break; |
| 704 | |
| 705 | default: |
| 706 | return -1; |
| 707 | } |
| 708 | } |
| 709 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 710 | /** Gets a mask from a PCM's hardware parameters. |
| 711 | * @param pcm_params A PCM's hardware parameters. |
| 712 | * @param param The parameter to get. |
| 713 | * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned. |
| 714 | * Otherwise, the mask associated with @p param is returned. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 715 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 716 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 717 | const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params, |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 718 | enum pcm_param param) |
| 719 | { |
| 720 | int p; |
| 721 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 722 | if (params == NULL) { |
| 723 | return NULL; |
| 724 | } |
| 725 | |
| 726 | p = pcm_param_to_alsa(param); |
| 727 | if (p < 0 || !param_is_mask(p)) { |
| 728 | return NULL; |
| 729 | } |
| 730 | |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 731 | return (const struct pcm_mask *)param_to_mask(params, p); |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 734 | /** Get the minimum of a specified PCM parameter. |
| 735 | * @param pcm_params A PCM parameters structure. |
| 736 | * @param param The specified parameter to get the minimum of. |
| 737 | * @returns On success, the parameter minimum. |
| 738 | * On failure, zero. |
| 739 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 740 | unsigned int pcm_params_get_min(const struct pcm_params *pcm_params, |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 741 | enum pcm_param param) |
| 742 | { |
| 743 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 744 | int p; |
| 745 | |
| 746 | if (!params) |
| 747 | return 0; |
| 748 | |
| 749 | p = pcm_param_to_alsa(param); |
| 750 | if (p < 0) |
| 751 | return 0; |
| 752 | |
| 753 | return param_get_min(params, p); |
| 754 | } |
| 755 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 756 | /** Get the maximum of a specified PCM parameter. |
| 757 | * @param pcm_params A PCM parameters structure. |
| 758 | * @param param The specified parameter to get the maximum of. |
| 759 | * @returns On success, the parameter maximum. |
| 760 | * On failure, zero. |
| 761 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 762 | unsigned int pcm_params_get_max(const struct pcm_params *pcm_params, |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 763 | enum pcm_param param) |
| 764 | { |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 765 | const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params; |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 766 | int p; |
| 767 | |
| 768 | if (!params) |
| 769 | return 0; |
| 770 | |
| 771 | p = pcm_param_to_alsa(param); |
| 772 | if (p < 0) |
| 773 | return 0; |
| 774 | |
| 775 | return param_get_max(params, p); |
| 776 | } |
| 777 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 778 | /** Closes a PCM returned by @ref pcm_open. |
| 779 | * @param pcm A PCM returned by @ref pcm_open. |
| 780 | * May not be NULL. |
| 781 | * @return Always returns zero. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 782 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 783 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 784 | int pcm_close(struct pcm *pcm) |
| 785 | { |
| 786 | if (pcm == &bad_pcm) |
| 787 | return 0; |
| 788 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 789 | pcm_hw_munmap_status(pcm); |
| 790 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 791 | if (pcm->flags & PCM_MMAP) { |
| 792 | pcm_stop(pcm); |
| 793 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 794 | } |
| 795 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 796 | if (pcm->fd >= 0) |
| 797 | close(pcm->fd); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 798 | pcm->buffer_size = 0; |
| 799 | pcm->fd = -1; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 800 | free(pcm); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
Taylor Holberton | c6f908e | 2016-12-24 20:33:33 -0800 | [diff] [blame] | 804 | /** Opens a PCM by it's name. |
| 805 | * @param name The name of the PCM. |
| 806 | * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b> |
| 807 | * @param flags Specify characteristics and functionality about the pcm. |
| 808 | * May be a bitwise AND of the following: |
| 809 | * - @ref PCM_IN |
| 810 | * - @ref PCM_OUT |
| 811 | * - @ref PCM_MMAP |
| 812 | * - @ref PCM_NOIRQ |
| 813 | * - @ref PCM_MONOTONIC |
| 814 | * @param config The hardware and software parameters to open the PCM with. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 815 | * @returns A PCM structure. |
| 816 | * If an error occurs allocating memory for the PCM, NULL is returned. |
| 817 | * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready. |
| 818 | * If @ref pcm_is_ready, check @ref pcm_get_error for more information. |
Taylor Holberton | c6f908e | 2016-12-24 20:33:33 -0800 | [diff] [blame] | 819 | * @ingroup libtinyalsa-pcm |
| 820 | */ |
| 821 | struct pcm *pcm_open_by_name(const char *name, |
| 822 | unsigned int flags, |
| 823 | const struct pcm_config *config) |
| 824 | { |
| 825 | unsigned int card, device; |
| 826 | if ((name[0] != 'h') |
| 827 | || (name[1] != 'w') |
| 828 | || (name[2] != ':')) { |
| 829 | return NULL; |
| 830 | } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) { |
| 831 | return NULL; |
| 832 | } |
| 833 | return pcm_open(card, device, flags, config); |
| 834 | } |
| 835 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 836 | /** Opens a PCM. |
| 837 | * @param card The card that the pcm belongs to. |
| 838 | * The default card is zero. |
| 839 | * @param device The device that the pcm belongs to. |
| 840 | * The default device is zero. |
| 841 | * @param flags Specify characteristics and functionality about the pcm. |
| 842 | * May be a bitwise AND of the following: |
| 843 | * - @ref PCM_IN |
| 844 | * - @ref PCM_OUT |
| 845 | * - @ref PCM_MMAP |
| 846 | * - @ref PCM_NOIRQ |
| 847 | * - @ref PCM_MONOTONIC |
| 848 | * @param config The hardware and software parameters to open the PCM with. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 849 | * @returns A PCM structure. |
| 850 | * If an error occurs allocating memory for the PCM, NULL is returned. |
| 851 | * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready. |
| 852 | * If @ref pcm_is_ready, check @ref pcm_get_error for more information. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 853 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 854 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 855 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
Taylor Holberton | 94803b0 | 2016-12-01 16:07:14 -0800 | [diff] [blame] | 856 | unsigned int flags, const struct pcm_config *config) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 857 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 858 | struct pcm *pcm; |
| 859 | struct snd_pcm_info info; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 860 | char fn[256]; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 861 | int rc; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 862 | |
| 863 | pcm = calloc(1, sizeof(struct pcm)); |
Taylor Holberton | f319eb0 | 2016-10-14 20:05:30 -0400 | [diff] [blame] | 864 | if (!pcm) |
| 865 | return &bad_pcm; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 866 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 867 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 868 | flags & PCM_IN ? 'c' : 'p'); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 869 | |
| 870 | pcm->flags = flags; |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 871 | |
| 872 | if (flags & PCM_NONBLOCK) |
| 873 | pcm->fd = open(fn, O_RDWR | O_NONBLOCK); |
| 874 | else |
| 875 | pcm->fd = open(fn, O_RDWR); |
| 876 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 877 | if (pcm->fd < 0) { |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 878 | oops(pcm, errno, "cannot open device '%s'", fn); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 879 | return pcm; |
| 880 | } |
| 881 | |
| 882 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 883 | oops(pcm, errno, "cannot get info"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 884 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 885 | } |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 886 | pcm->subdevice = info.subdevice; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 887 | |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 888 | if (pcm_set_config(pcm, config) != 0) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 889 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 890 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 891 | rc = pcm_hw_mmap_status(pcm); |
| 892 | if (rc < 0) { |
| 893 | oops(pcm, rc, "mmap status failed"); |
| 894 | goto fail; |
| 895 | } |
| 896 | |
Glenn Kasten | 8101240 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 897 | #ifdef SNDRV_PCM_IOCTL_TTSTAMP |
| 898 | if (pcm->flags & PCM_MONOTONIC) { |
| 899 | int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
| 900 | rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg); |
| 901 | if (rc < 0) { |
| 902 | oops(pcm, rc, "cannot set timestamp type"); |
| 903 | goto fail; |
| 904 | } |
| 905 | } |
| 906 | #endif |
| 907 | |
Ricardo Biehl Pasquali | 13bf629 | 2018-08-22 16:10:45 -0300 | [diff] [blame] | 908 | /* prepare here so the user does not need to do this later */ |
| 909 | if (pcm_prepare(pcm)) |
| 910 | goto fail; |
| 911 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 912 | pcm->underruns = 0; |
| 913 | return pcm; |
| 914 | |
| 915 | fail: |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 916 | if (flags & PCM_MMAP) |
| 917 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 918 | fail_close: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 919 | close(pcm->fd); |
| 920 | pcm->fd = -1; |
| 921 | return pcm; |
| 922 | } |
| 923 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 924 | /** Checks if a PCM file has been opened without error. |
| 925 | * @param pcm A PCM handle. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 926 | * May be NULL. |
| 927 | * @return If a PCM's file descriptor is not valid or the pointer is NULL, it returns zero. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 928 | * Otherwise, the function returns one. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 929 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 930 | */ |
Taylor Holberton | 15d5848 | 2016-12-01 17:46:29 -0800 | [diff] [blame] | 931 | int pcm_is_ready(const struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 932 | { |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 933 | if (pcm != NULL) { |
| 934 | return pcm->fd >= 0; |
| 935 | } |
| 936 | return 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 937 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 938 | |
Taylor Holberton | 558e594 | 2016-12-04 13:42:28 -0800 | [diff] [blame] | 939 | /** Links two PCMs. |
| 940 | * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time). |
| 941 | * If an error occurs, the error message will be written to @p pcm1. |
| 942 | * @param pcm1 A PCM handle. |
| 943 | * @param pcm2 Another PCM handle. |
| 944 | * @return On success, zero; on failure, a negative number. |
| 945 | * @ingroup libtinyalsa-pcm |
| 946 | */ |
| 947 | int pcm_link(struct pcm *pcm1, struct pcm *pcm2) |
| 948 | { |
| 949 | int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd); |
| 950 | if (err == -1) { |
| 951 | return oops(pcm1, errno, "cannot link PCM"); |
| 952 | } |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | /** Unlinks a PCM. |
| 957 | * @see @ref pcm_link |
| 958 | * @param pcm A PCM handle. |
| 959 | * @return On success, zero; on failure, a negative number. |
| 960 | * @ingroup libtinyalsa-pcm |
| 961 | */ |
| 962 | int pcm_unlink(struct pcm *pcm) |
| 963 | { |
| 964 | int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK); |
| 965 | if (err == -1) { |
| 966 | return oops(pcm, errno, "cannot unlink PCM"); |
| 967 | } |
| 968 | return 0; |
| 969 | } |
| 970 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 971 | /** Prepares a PCM, if it has not been prepared already. |
| 972 | * @param pcm A PCM handle. |
| 973 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 974 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 975 | */ |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 976 | int pcm_prepare(struct pcm *pcm) |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 977 | { |
| 978 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0) |
| 979 | return oops(pcm, errno, "cannot prepare channel"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 980 | |
Ricardo Biehl Pasquali | 535a6ba | 2018-12-30 11:53:54 -0200 | [diff] [blame] | 981 | /* get appl_ptr and avail_min from kernel */ |
| 982 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN); |
| 983 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 984 | return 0; |
| 985 | } |
| 986 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 987 | /** Starts a PCM. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 988 | * @param pcm A PCM handle. |
| 989 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 990 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 991 | */ |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 992 | int pcm_start(struct pcm *pcm) |
| 993 | { |
Ricardo Biehl Pasquali | 7ff9cde | 2018-12-31 17:23:39 -0200 | [diff] [blame] | 994 | /* set appl_ptr and avail_min in kernel */ |
Miguel Gaio | cf5f063 | 2018-07-17 13:30:24 +0200 | [diff] [blame] | 995 | pcm_sync_ptr(pcm, 0); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 996 | |
Miguel Gaio | cf5f063 | 2018-07-17 13:30:24 +0200 | [diff] [blame] | 997 | if (pcm->mmap_status->state != PCM_STATE_RUNNING) { |
| 998 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0) |
| 999 | return oops(pcm, errno, "cannot start channel"); |
| 1000 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 1001 | |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1005 | /** Stops a PCM. |
| 1006 | * @param pcm A PCM handle. |
| 1007 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 1008 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1009 | */ |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 1010 | int pcm_stop(struct pcm *pcm) |
| 1011 | { |
| 1012 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0) |
| 1013 | return oops(pcm, errno, "cannot stop channel"); |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1018 | static inline int pcm_mmap_playback_avail(struct pcm *pcm) |
| 1019 | { |
| 1020 | int avail; |
| 1021 | |
| 1022 | avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 1023 | |
| 1024 | if (avail < 0) |
| 1025 | avail += pcm->boundary; |
StevenNAN | b0fc3e9 | 2014-03-17 11:14:49 +0800 | [diff] [blame] | 1026 | else if (avail >= (int)pcm->boundary) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1027 | avail -= pcm->boundary; |
| 1028 | |
| 1029 | return avail; |
| 1030 | } |
| 1031 | |
| 1032 | static inline int pcm_mmap_capture_avail(struct pcm *pcm) |
| 1033 | { |
| 1034 | int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr; |
| 1035 | if (avail < 0) |
| 1036 | avail += pcm->boundary; |
| 1037 | return avail; |
| 1038 | } |
| 1039 | |
| 1040 | static inline int pcm_mmap_avail(struct pcm *pcm) |
| 1041 | { |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1042 | if (pcm->flags & PCM_IN) |
| 1043 | return pcm_mmap_capture_avail(pcm); |
| 1044 | else |
| 1045 | return pcm_mmap_playback_avail(pcm); |
| 1046 | } |
| 1047 | |
| 1048 | static void pcm_mmap_appl_forward(struct pcm *pcm, int frames) |
| 1049 | { |
| 1050 | unsigned int appl_ptr = pcm->mmap_control->appl_ptr; |
| 1051 | appl_ptr += frames; |
| 1052 | |
| 1053 | /* check for boundary wrap */ |
| 1054 | if (appl_ptr > pcm->boundary) |
| 1055 | appl_ptr -= pcm->boundary; |
| 1056 | pcm->mmap_control->appl_ptr = appl_ptr; |
| 1057 | } |
| 1058 | |
| 1059 | int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, |
| 1060 | unsigned int *frames) |
| 1061 | { |
| 1062 | unsigned int continuous, copy_frames, avail; |
| 1063 | |
| 1064 | /* return the mmap buffer */ |
| 1065 | *areas = pcm->mmap_buffer; |
| 1066 | |
| 1067 | /* and the application offset in frames */ |
| 1068 | *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size; |
| 1069 | |
| 1070 | avail = pcm_mmap_avail(pcm); |
| 1071 | if (avail > pcm->buffer_size) |
| 1072 | avail = pcm->buffer_size; |
| 1073 | continuous = pcm->buffer_size - *offset; |
| 1074 | |
| 1075 | /* we can only copy frames if the are availabale and continuos */ |
| 1076 | copy_frames = *frames; |
| 1077 | if (copy_frames > avail) |
| 1078 | copy_frames = avail; |
| 1079 | if (copy_frames > continuous) |
| 1080 | copy_frames = continuous; |
| 1081 | *frames = copy_frames; |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1086 | static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset, |
| 1087 | char *buf, unsigned int src_offset, |
| 1088 | unsigned int frames) |
| 1089 | { |
| 1090 | int size_bytes = pcm_frames_to_bytes(pcm, frames); |
| 1091 | int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset); |
| 1092 | int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset); |
| 1093 | |
| 1094 | /* interleaved only atm */ |
| 1095 | if (pcm->flags & PCM_IN) |
| 1096 | memcpy(buf + src_offset_bytes, |
| 1097 | (char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 1098 | size_bytes); |
| 1099 | else |
| 1100 | memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 1101 | buf + src_offset_bytes, |
| 1102 | size_bytes); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1106 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames) |
| 1107 | { |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1108 | int ret; |
| 1109 | |
Taylor Holberton | 73466c0 | 2016-10-01 12:51:59 -0400 | [diff] [blame] | 1110 | /* not used */ |
| 1111 | (void) offset; |
| 1112 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1113 | /* update the application pointer in userspace and kernel */ |
| 1114 | pcm_mmap_appl_forward(pcm, frames); |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1115 | ret = pcm_sync_ptr(pcm, 0); |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 1116 | if (ret != 0){ |
| 1117 | printf("%d\n", ret); |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1118 | return ret; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 1119 | } |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1120 | |
| 1121 | return frames; |
| 1122 | } |
| 1123 | |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1124 | static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf, |
| 1125 | unsigned int offset, unsigned int size) |
| 1126 | { |
| 1127 | void *pcm_areas; |
| 1128 | int commit; |
| 1129 | unsigned int pcm_offset, frames, count = 0; |
| 1130 | |
Ricardo Biehl Pasquali | 7aacaed | 2018-12-18 23:24:19 -0200 | [diff] [blame] | 1131 | while (pcm_mmap_avail(pcm) && size) { |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1132 | frames = size; |
| 1133 | pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames); |
| 1134 | pcm_areas_copy(pcm, pcm_offset, buf, offset, frames); |
| 1135 | commit = pcm_mmap_commit(pcm, pcm_offset, frames); |
| 1136 | if (commit < 0) { |
| 1137 | oops(pcm, commit, "failed to commit %d frames\n", frames); |
| 1138 | return commit; |
| 1139 | } |
| 1140 | |
| 1141 | offset += commit; |
| 1142 | count += commit; |
| 1143 | size -= commit; |
| 1144 | } |
| 1145 | return count; |
| 1146 | } |
| 1147 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1148 | int pcm_avail_update(struct pcm *pcm) |
| 1149 | { |
Ricardo Biehl Pasquali | b00f90a | 2018-12-22 12:51:21 -0200 | [diff] [blame] | 1150 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1151 | return pcm_mmap_avail(pcm); |
| 1152 | } |
| 1153 | |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1154 | /** Returns available frames in pcm buffer and corresponding time stamp. |
| 1155 | * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open, |
| 1156 | * otherwise the clock is CLOCK_REALTIME. |
| 1157 | * For an input stream, frames available are frames ready for the application to read. |
| 1158 | * For an output stream, frames available are the number of empty frames available for the application to write. |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1159 | * @param pcm A PCM handle. |
| 1160 | * @param avail The number of available frames |
| 1161 | * @param tstamp The timestamp |
| 1162 | * @return On success, zero is returned; on failure, negative one. |
| 1163 | */ |
| 1164 | int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail, |
| 1165 | struct timespec *tstamp) |
| 1166 | { |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1167 | int checking; |
| 1168 | int tmp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1169 | |
| 1170 | if (!pcm_is_ready(pcm)) |
| 1171 | return -1; |
| 1172 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1173 | checking = 0; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1174 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1175 | again: |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1176 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1177 | tmp = pcm_avail_update(pcm); |
| 1178 | if (tmp < 0) |
| 1179 | return tmp; /* error */ |
| 1180 | |
| 1181 | if (checking && (unsigned int) tmp == *avail) |
| 1182 | return 0; |
| 1183 | |
| 1184 | *avail = (unsigned int) tmp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1185 | *tstamp = pcm->mmap_status->tstamp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1186 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1187 | /* |
| 1188 | * When status is mmaped, get avail again to ensure |
| 1189 | * valid timestamp. |
| 1190 | */ |
| 1191 | if (!pcm->sync_ptr) { |
| 1192 | checking = 1; |
| 1193 | goto again; |
| 1194 | } |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1195 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1196 | /* SYNC_PTR ioctl was used, no need to check avail */ |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1200 | int pcm_state(struct pcm *pcm) |
| 1201 | { |
| 1202 | int err = pcm_sync_ptr(pcm, 0); |
| 1203 | if (err < 0) |
| 1204 | return err; |
| 1205 | |
| 1206 | return pcm->mmap_status->state; |
| 1207 | } |
| 1208 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 1209 | /** Waits for frames to be available for read or write operations. |
| 1210 | * @param pcm A PCM handle. |
| 1211 | * @param timeout The maximum amount of time to wait for, in terms of milliseconds. |
| 1212 | * @returns If frames became available, one is returned. |
| 1213 | * If a timeout occured, zero is returned. |
| 1214 | * If an error occured, a negative number is returned. |
| 1215 | * @ingroup libtinyalsa-pcm |
| 1216 | */ |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1217 | int pcm_wait(struct pcm *pcm, int timeout) |
| 1218 | { |
| 1219 | struct pollfd pfd; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1220 | int err; |
| 1221 | |
| 1222 | pfd.fd = pcm->fd; |
Apelete Seketeli | 84889d0 | 2014-02-14 14:34:32 +0100 | [diff] [blame] | 1223 | pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1224 | |
| 1225 | do { |
| 1226 | /* let's wait for avail or timeout */ |
| 1227 | err = poll(&pfd, 1, timeout); |
| 1228 | if (err < 0) |
| 1229 | return -errno; |
| 1230 | |
| 1231 | /* timeout ? */ |
| 1232 | if (err == 0) |
| 1233 | return 0; |
| 1234 | |
| 1235 | /* have we been interrupted ? */ |
| 1236 | if (errno == -EINTR) |
| 1237 | continue; |
| 1238 | |
| 1239 | /* check for any errors */ |
| 1240 | if (pfd.revents & (POLLERR | POLLNVAL)) { |
| 1241 | switch (pcm_state(pcm)) { |
| 1242 | case PCM_STATE_XRUN: |
| 1243 | return -EPIPE; |
| 1244 | case PCM_STATE_SUSPENDED: |
| 1245 | return -ESTRPIPE; |
| 1246 | case PCM_STATE_DISCONNECTED: |
| 1247 | return -ENODEV; |
| 1248 | default: |
| 1249 | return -EIO; |
| 1250 | } |
| 1251 | } |
| 1252 | /* poll again if fd not ready for IO */ |
| 1253 | } while (!(pfd.revents & (POLLIN | POLLOUT))); |
| 1254 | |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1258 | /* |
| 1259 | * Transfer data to/from mmaped buffer. This imitates the |
| 1260 | * behavior of read/write system calls. |
| 1261 | * |
| 1262 | * However, this doesn't seems to offer any advantage over |
| 1263 | * the read/write syscalls. Should it be removed? |
| 1264 | */ |
| 1265 | int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1266 | { |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1267 | int is_playback; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1268 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1269 | int state; |
| 1270 | unsigned int avail; |
| 1271 | unsigned int user_offset; |
| 1272 | |
| 1273 | int err; |
| 1274 | int tmp; |
| 1275 | |
| 1276 | is_playback = !(pcm->flags & PCM_IN); |
| 1277 | |
| 1278 | if (frames == 0) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1279 | return 0; |
| 1280 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1281 | /* update hardware pointer and get state */ |
| 1282 | err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC | |
| 1283 | SNDRV_PCM_SYNC_PTR_APPL | |
| 1284 | SNDRV_PCM_SYNC_PTR_AVAIL_MIN); |
| 1285 | if (err == -1) |
| 1286 | return -1; |
| 1287 | state = pcm->mmap_status->state; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1288 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1289 | /* start capture if frames >= threshold */ |
| 1290 | if (!is_playback && state == PCM_STATE_PREPARED) { |
| 1291 | if (frames >= pcm->config.start_threshold) { |
| 1292 | err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START); |
| 1293 | if (err == -1) |
| 1294 | return -1; |
| 1295 | /* state = PCM_STATE_RUNNING */ |
| 1296 | } else { |
| 1297 | /* nothing to do */ |
| 1298 | return 0; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1299 | } |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1302 | avail = pcm_mmap_avail(pcm); |
| 1303 | user_offset = 0; |
| 1304 | |
| 1305 | while (frames) { |
| 1306 | if (!avail) { |
| 1307 | if (pcm->flags & PCM_NONBLOCK) { |
| 1308 | errno = EAGAIN; |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | /* wait for interrupt */ |
| 1313 | err = pcm_wait(pcm, -1); |
| 1314 | if (err < 0) { |
| 1315 | errno = -err; |
| 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | /* get hardware pointer */ |
| 1320 | avail = pcm_avail_update(pcm); |
| 1321 | } |
| 1322 | |
| 1323 | tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames); |
| 1324 | if (tmp < 0) |
| 1325 | break; |
| 1326 | |
| 1327 | user_offset += tmp; |
| 1328 | frames -= tmp; |
| 1329 | avail -= tmp; |
| 1330 | |
| 1331 | /* start playback if written >= start_threshold */ |
| 1332 | if (is_playback && state == PCM_STATE_PREPARED && |
| 1333 | pcm->buffer_size - avail >= pcm->config.start_threshold) { |
| 1334 | err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START); |
| 1335 | if (err == -1) |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return user_offset ? (int) user_offset : -1; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1341 | } |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1342 | |
| 1343 | int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1344 | { |
| 1345 | if ((~pcm->flags) & (PCM_OUT | PCM_MMAP)) |
| 1346 | return -ENOSYS; |
| 1347 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1348 | return pcm_mmap_transfer(pcm, (void *)data, |
| 1349 | pcm_bytes_to_frames(pcm, count)); |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) |
| 1353 | { |
| 1354 | if ((~pcm->flags) & (PCM_IN | PCM_MMAP)) |
| 1355 | return -ENOSYS; |
| 1356 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1357 | return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count)); |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1358 | } |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 1359 | |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame^] | 1360 | /** Writes audio samples to PCM. |
| 1361 | * If the PCM has not been started, it is started in this function. |
| 1362 | * This function is only valid for PCMs opened with the @ref PCM_OUT flag. |
| 1363 | * @param pcm A PCM handle. |
| 1364 | * @param data The audio sample array |
| 1365 | * @param frame_count The number of frames occupied by the sample array. |
| 1366 | * This value should not be greater than @ref TINYALSA_FRAMES_MAX |
| 1367 | * or INT_MAX. |
| 1368 | * @return On success, this function returns the number of frames written; otherwise, a negative number. |
| 1369 | * @ingroup libtinyalsa-pcm |
| 1370 | */ |
| 1371 | int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count) |
| 1372 | { |
| 1373 | struct snd_xferi x; |
| 1374 | |
| 1375 | if (pcm->flags & PCM_IN) |
| 1376 | return -EINVAL; |
| 1377 | #if UINT_MAX > TINYALSA_FRAMES_MAX |
| 1378 | if (frame_count > TINYALSA_FRAMES_MAX) |
| 1379 | return -EINVAL; |
| 1380 | #endif |
| 1381 | if (frame_count > INT_MAX) |
| 1382 | return -EINVAL; |
| 1383 | |
| 1384 | x.buf = (void*)data; |
| 1385 | x.frames = frame_count; |
| 1386 | x.result = 0; |
| 1387 | for (;;) { |
| 1388 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) { |
| 1389 | if (errno == EPIPE) { |
| 1390 | /* we failed to make our window -- try to restart if we are |
| 1391 | * allowed to do so. Otherwise, simply allow the EPIPE error to |
| 1392 | * propagate up to the app level */ |
| 1393 | pcm->underruns++; |
| 1394 | if (pcm->flags & PCM_NORESTART) |
| 1395 | return -EPIPE; |
| 1396 | if (pcm_prepare(pcm)) |
| 1397 | return -EPIPE; |
| 1398 | continue; |
| 1399 | } |
| 1400 | return oops(pcm, errno, "cannot write stream data"); |
| 1401 | } |
| 1402 | return x.result; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /** Reads audio samples from PCM. |
| 1407 | * If the PCM has not been started, it is started in this function. |
| 1408 | * This function is only valid for PCMs opened with the @ref PCM_IN flag. |
| 1409 | * @param pcm A PCM handle. |
| 1410 | * @param data The audio sample array |
| 1411 | * @param frame_count The number of frames occupied by the sample array. |
| 1412 | * This value should not be greater than @ref TINYALSA_FRAMES_MAX |
| 1413 | * or INT_MAX. |
| 1414 | * @return On success, this function returns the number of frames written; otherwise, a negative number. |
| 1415 | * @ingroup libtinyalsa-pcm |
| 1416 | */ |
| 1417 | int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count) |
| 1418 | { |
| 1419 | struct snd_xferi x; |
| 1420 | |
| 1421 | if (!(pcm->flags & PCM_IN)) |
| 1422 | return -EINVAL; |
| 1423 | #if UINT_MAX > TINYALSA_FRAMES_MAX |
| 1424 | if (frame_count > TINYALSA_FRAMES_MAX) |
| 1425 | return -EINVAL; |
| 1426 | #endif |
| 1427 | if (frame_count > INT_MAX) |
| 1428 | return -EINVAL; |
| 1429 | |
| 1430 | x.buf = data; |
| 1431 | x.frames = frame_count; |
| 1432 | x.result = 0; |
| 1433 | for (;;) { |
| 1434 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) { |
| 1435 | if (errno == EPIPE) { |
| 1436 | /* we failed to make our window -- try to restart */ |
| 1437 | pcm->underruns++; |
| 1438 | if (pcm->flags & PCM_NORESTART) |
| 1439 | return -EPIPE; |
| 1440 | if (pcm_prepare(pcm)) |
| 1441 | return -EPIPE; |
| 1442 | continue; |
| 1443 | } |
| 1444 | return oops(pcm, errno, "cannot read stream data"); |
| 1445 | } |
| 1446 | return x.result; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /** Writes audio samples to PCM. |
| 1451 | * If the PCM has not been started, it is started in this function. |
| 1452 | * This function is only valid for PCMs opened with the @ref PCM_OUT flag. |
| 1453 | * This function is not valid for PCMs opened with the @ref PCM_MMAP flag. |
| 1454 | * @param pcm A PCM handle. |
| 1455 | * @param data The audio sample array |
| 1456 | * @param count The number of bytes occupied by the sample array. |
| 1457 | * @return On success, this function returns zero; otherwise, a negative number. |
| 1458 | * @deprecated |
| 1459 | * @ingroup libtinyalsa-pcm |
| 1460 | */ |
| 1461 | int pcm_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1462 | { |
| 1463 | return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count)); |
| 1464 | } |
| 1465 | |
| 1466 | /** Reads audio samples from PCM. |
| 1467 | * If the PCM has not been started, it is started in this function. |
| 1468 | * This function is only valid for PCMs opened with the @ref PCM_IN flag. |
| 1469 | * This function is not valid for PCMs opened with the @ref PCM_MMAP flag. |
| 1470 | * @param pcm A PCM handle. |
| 1471 | * @param data The audio sample array |
| 1472 | * @param count The number of bytes occupied by the sample array. |
| 1473 | * @return On success, this function returns zero; otherwise, a negative number. |
| 1474 | * @deprecated |
| 1475 | * @ingroup libtinyalsa-pcm |
| 1476 | */ |
| 1477 | int pcm_read(struct pcm *pcm, void *data, unsigned int count) |
| 1478 | { |
| 1479 | return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count)); |
| 1480 | } |
| 1481 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 1482 | /** Gets the delay of the PCM, in terms of frames. |
| 1483 | * @param pcm A PCM handle. |
| 1484 | * @returns On success, the delay of the PCM. |
| 1485 | * On failure, a negative number. |
| 1486 | * @ingroup libtinyalsa-pcm |
| 1487 | */ |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 1488 | long pcm_get_delay(struct pcm *pcm) |
| 1489 | { |
| 1490 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0) |
| 1491 | return -1; |
| 1492 | |
| 1493 | return pcm->pcm_delay; |
| 1494 | } |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1495 | |