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; |
Ricardo Biehl Pasquali | 1804d15 | 2019-01-04 00:32:40 -0200 | [diff] [blame] | 218 | /** The number of (under/over)runs that have occured */ |
| 219 | int xruns; |
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)); |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 362 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 363 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 364 | config->channels); |
| 365 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 366 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 367 | |
| 368 | if (pcm->flags & PCM_NOIRQ) { |
| 369 | |
| 370 | if (!(pcm->flags & PCM_MMAP)) { |
| 371 | oops(pcm, -EINVAL, "noirq only currently supported with mmap()."); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; |
| 376 | pcm->noirq_frames_per_msec = config->rate / 1000; |
| 377 | } |
| 378 | |
| 379 | if (pcm->flags & PCM_MMAP) |
| 380 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 381 | SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
| 382 | else |
| 383 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 384 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 385 | |
| 386 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 387 | int errno_copy = errno; |
| 388 | oops(pcm, -errno, "cannot set hw params"); |
| 389 | return -errno_copy; |
| 390 | } |
| 391 | |
| 392 | /* get our refined hw_params */ |
| 393 | pcm->config.period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 394 | pcm->config.period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS); |
| 395 | pcm->buffer_size = config->period_count * config->period_size; |
| 396 | |
| 397 | if (pcm->flags & PCM_MMAP) { |
| 398 | pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size), |
| 399 | PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0); |
| 400 | if (pcm->mmap_buffer == MAP_FAILED) { |
| 401 | int errno_copy = errno; |
| 402 | oops(pcm, -errno, "failed to mmap buffer %d bytes\n", |
| 403 | pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 404 | return -errno_copy; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | struct snd_pcm_sw_params sparams; |
| 409 | memset(&sparams, 0, sizeof(sparams)); |
| 410 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE; |
| 411 | sparams.period_step = 1; |
| 412 | sparams.avail_min = 1; |
| 413 | |
| 414 | if (!config->start_threshold) { |
| 415 | if (pcm->flags & PCM_IN) |
| 416 | pcm->config.start_threshold = sparams.start_threshold = 1; |
| 417 | else |
| 418 | pcm->config.start_threshold = sparams.start_threshold = |
| 419 | config->period_count * config->period_size / 2; |
| 420 | } else |
| 421 | sparams.start_threshold = config->start_threshold; |
| 422 | |
| 423 | /* pick a high stop threshold - todo: does this need further tuning */ |
| 424 | if (!config->stop_threshold) { |
| 425 | if (pcm->flags & PCM_IN) |
| 426 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 427 | config->period_count * config->period_size * 10; |
| 428 | else |
| 429 | pcm->config.stop_threshold = sparams.stop_threshold = |
| 430 | config->period_count * config->period_size; |
| 431 | } |
| 432 | else |
| 433 | sparams.stop_threshold = config->stop_threshold; |
| 434 | |
| 435 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 436 | sparams.silence_size = 0; |
| 437 | sparams.silence_threshold = config->silence_threshold; |
| 438 | pcm->boundary = sparams.boundary = pcm->buffer_size; |
| 439 | |
| 440 | while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size) |
| 441 | pcm->boundary *= 2; |
| 442 | |
| 443 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 444 | int errno_copy = errno; |
| 445 | oops(pcm, -errno, "cannot set sw params"); |
| 446 | return -errno_copy; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 452 | /** Gets the subdevice on which the pcm has been opened. |
| 453 | * @param pcm A PCM handle. |
| 454 | * @return The subdevice on which the pcm has been opened */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 455 | unsigned int pcm_get_subdevice(const struct pcm *pcm) |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 456 | { |
| 457 | return pcm->subdevice; |
| 458 | } |
| 459 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 460 | /** Determines the number of bits occupied by a @ref pcm_format. |
| 461 | * @param format A PCM format. |
| 462 | * @return The number of bits associated with @p format |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 463 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 464 | */ |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 465 | unsigned int pcm_format_to_bits(enum pcm_format format) |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 466 | { |
| 467 | switch (format) { |
| 468 | case PCM_FORMAT_S32_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 469 | case PCM_FORMAT_S32_BE: |
Simon Wilson | 7136cf7 | 2013-07-17 10:30:35 -0700 | [diff] [blame] | 470 | case PCM_FORMAT_S24_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 471 | case PCM_FORMAT_S24_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 472 | return 32; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 473 | case PCM_FORMAT_S24_3LE: |
| 474 | case PCM_FORMAT_S24_3BE: |
| 475 | return 24; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 476 | default: |
| 477 | case PCM_FORMAT_S16_LE: |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 478 | case PCM_FORMAT_S16_BE: |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 479 | return 16; |
Taylor Holberton | c01d4a3 | 2016-10-01 12:22:43 -0400 | [diff] [blame] | 480 | case PCM_FORMAT_S8: |
| 481 | return 8; |
Simon Wilson | bc03b62 | 2011-06-15 17:19:01 -0700 | [diff] [blame] | 482 | }; |
| 483 | } |
| 484 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 485 | /** Determines how many frames of a PCM can fit into a number of bytes. |
| 486 | * @param pcm A PCM handle. |
| 487 | * @param bytes The number of bytes. |
| 488 | * @return The number of frames that may fit into @p bytes |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 489 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 490 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 491 | 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] | 492 | { |
| 493 | return bytes / (pcm->config.channels * |
| 494 | (pcm_format_to_bits(pcm->config.format) >> 3)); |
| 495 | } |
| 496 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 497 | /** Determines how many bytes are occupied by a number of frames of a PCM. |
| 498 | * @param pcm A PCM handle. |
| 499 | * @param frames The number of frames of a PCM. |
| 500 | * @return The bytes occupied by @p frames. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 501 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 502 | */ |
Taylor Holberton | 147d7ad | 2016-12-01 17:50:31 -0800 | [diff] [blame] | 503 | 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] | 504 | { |
| 505 | return frames * pcm->config.channels * |
| 506 | (pcm_format_to_bits(pcm->config.format) >> 3); |
| 507 | } |
| 508 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 509 | static int pcm_sync_ptr(struct pcm *pcm, int flags) |
| 510 | { |
Ricardo Biehl Pasquali | c144675 | 2018-12-18 23:13:05 -0200 | [diff] [blame] | 511 | if (pcm->sync_ptr == NULL) { |
| 512 | /* status and control are mmaped */ |
| 513 | |
| 514 | if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { |
| 515 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) { |
| 516 | oops(pcm, errno, "failed to sync hardware pointer"); |
| 517 | return -1; |
| 518 | } |
| 519 | } |
| 520 | } else { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 521 | pcm->sync_ptr->flags = flags; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 522 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) { |
| 523 | oops(pcm, errno, "failed to sync mmap ptr"); |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 524 | return -1; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 525 | } |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 526 | } |
Ricardo Biehl Pasquali | c144675 | 2018-12-18 23:13:05 -0200 | [diff] [blame] | 527 | |
Miguel Gaio | c9f97da | 2018-07-17 10:05:54 +0200 | [diff] [blame] | 528 | return 0; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Taylor Holberton | 4f55606 | 2016-09-16 09:54:36 -0400 | [diff] [blame] | 531 | static int pcm_hw_mmap_status(struct pcm *pcm) |
| 532 | { |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 533 | if (pcm->sync_ptr) |
| 534 | return 0; |
| 535 | |
| 536 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 537 | pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED, |
| 538 | pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS); |
| 539 | if (pcm->mmap_status == MAP_FAILED) |
| 540 | pcm->mmap_status = NULL; |
| 541 | if (!pcm->mmap_status) |
| 542 | goto mmap_error; |
| 543 | |
| 544 | pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE, |
| 545 | MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL); |
| 546 | if (pcm->mmap_control == MAP_FAILED) |
| 547 | pcm->mmap_control = NULL; |
| 548 | if (!pcm->mmap_control) { |
| 549 | munmap(pcm->mmap_status, page_size); |
| 550 | pcm->mmap_status = NULL; |
| 551 | goto mmap_error; |
| 552 | } |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 553 | |
| 554 | return 0; |
| 555 | |
| 556 | mmap_error: |
| 557 | |
| 558 | pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr)); |
| 559 | if (!pcm->sync_ptr) |
| 560 | return -ENOMEM; |
| 561 | pcm->mmap_status = &pcm->sync_ptr->s.status; |
| 562 | pcm->mmap_control = &pcm->sync_ptr->c.control; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static void pcm_hw_munmap_status(struct pcm *pcm) { |
| 568 | if (pcm->sync_ptr) { |
| 569 | free(pcm->sync_ptr); |
| 570 | pcm->sync_ptr = NULL; |
| 571 | } else { |
| 572 | int page_size = sysconf(_SC_PAGE_SIZE); |
| 573 | if (pcm->mmap_status) |
| 574 | munmap(pcm->mmap_status, page_size); |
| 575 | if (pcm->mmap_control) |
| 576 | munmap(pcm->mmap_control, page_size); |
| 577 | } |
| 578 | pcm->mmap_status = NULL; |
| 579 | pcm->mmap_control = NULL; |
| 580 | } |
| 581 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 582 | static struct pcm bad_pcm = { |
| 583 | .fd = -1, |
| 584 | }; |
| 585 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 586 | /** Gets the hardware parameters of a PCM, without created a PCM handle. |
| 587 | * @param card The card of the PCM. |
| 588 | * The default card is zero. |
| 589 | * @param device The device of the PCM. |
| 590 | * The default device is zero. |
| 591 | * @param flags Specifies whether the PCM is an input or output. |
| 592 | * May be one of the following: |
| 593 | * - @ref PCM_IN |
| 594 | * - @ref PCM_OUT |
| 595 | * @return On success, the hardware parameters of the PCM; on failure, NULL. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 596 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 597 | */ |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 598 | struct pcm_params *pcm_params_get(unsigned int card, unsigned int device, |
| 599 | unsigned int flags) |
| 600 | { |
| 601 | struct snd_pcm_hw_params *params; |
| 602 | char fn[256]; |
| 603 | int fd; |
| 604 | |
| 605 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 606 | flags & PCM_IN ? 'c' : 'p'); |
| 607 | |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 608 | if (flags & PCM_NONBLOCK) |
| 609 | fd = open(fn, O_RDWR | O_NONBLOCK); |
| 610 | else |
| 611 | fd = open(fn, O_RDWR); |
| 612 | |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 613 | if (fd < 0) { |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 614 | fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno)); |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 615 | goto err_open; |
| 616 | } |
| 617 | |
| 618 | params = calloc(1, sizeof(struct snd_pcm_hw_params)); |
| 619 | if (!params) |
| 620 | goto err_calloc; |
| 621 | |
| 622 | param_init(params); |
| 623 | if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) { |
| 624 | fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno); |
| 625 | goto err_hw_refine; |
| 626 | } |
| 627 | |
| 628 | close(fd); |
| 629 | |
| 630 | return (struct pcm_params *)params; |
| 631 | |
| 632 | err_hw_refine: |
| 633 | free(params); |
| 634 | err_calloc: |
| 635 | close(fd); |
| 636 | err_open: |
| 637 | return NULL; |
| 638 | } |
| 639 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 640 | /** Frees the hardware parameters returned by @ref pcm_params_get. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 641 | * @param pcm_params Hardware parameters of a PCM. |
| 642 | * May be NULL. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 643 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 644 | */ |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 645 | void pcm_params_free(struct pcm_params *pcm_params) |
| 646 | { |
| 647 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 648 | |
| 649 | if (params) |
| 650 | free(params); |
| 651 | } |
| 652 | |
| 653 | static int pcm_param_to_alsa(enum pcm_param param) |
| 654 | { |
| 655 | switch (param) { |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 656 | case PCM_PARAM_ACCESS: |
| 657 | return SNDRV_PCM_HW_PARAM_ACCESS; |
| 658 | case PCM_PARAM_FORMAT: |
| 659 | return SNDRV_PCM_HW_PARAM_FORMAT; |
| 660 | case PCM_PARAM_SUBFORMAT: |
| 661 | return SNDRV_PCM_HW_PARAM_SUBFORMAT; |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 662 | case PCM_PARAM_SAMPLE_BITS: |
| 663 | return SNDRV_PCM_HW_PARAM_SAMPLE_BITS; |
| 664 | break; |
| 665 | case PCM_PARAM_FRAME_BITS: |
| 666 | return SNDRV_PCM_HW_PARAM_FRAME_BITS; |
| 667 | break; |
| 668 | case PCM_PARAM_CHANNELS: |
| 669 | return SNDRV_PCM_HW_PARAM_CHANNELS; |
| 670 | break; |
| 671 | case PCM_PARAM_RATE: |
| 672 | return SNDRV_PCM_HW_PARAM_RATE; |
| 673 | break; |
| 674 | case PCM_PARAM_PERIOD_TIME: |
| 675 | return SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 676 | break; |
| 677 | case PCM_PARAM_PERIOD_SIZE: |
| 678 | return SNDRV_PCM_HW_PARAM_PERIOD_SIZE; |
| 679 | break; |
| 680 | case PCM_PARAM_PERIOD_BYTES: |
| 681 | return SNDRV_PCM_HW_PARAM_PERIOD_BYTES; |
| 682 | break; |
| 683 | case PCM_PARAM_PERIODS: |
| 684 | return SNDRV_PCM_HW_PARAM_PERIODS; |
| 685 | break; |
| 686 | case PCM_PARAM_BUFFER_TIME: |
| 687 | return SNDRV_PCM_HW_PARAM_BUFFER_TIME; |
| 688 | break; |
| 689 | case PCM_PARAM_BUFFER_SIZE: |
| 690 | return SNDRV_PCM_HW_PARAM_BUFFER_SIZE; |
| 691 | break; |
| 692 | case PCM_PARAM_BUFFER_BYTES: |
| 693 | return SNDRV_PCM_HW_PARAM_BUFFER_BYTES; |
| 694 | break; |
| 695 | case PCM_PARAM_TICK_TIME: |
| 696 | return SNDRV_PCM_HW_PARAM_TICK_TIME; |
| 697 | break; |
| 698 | |
| 699 | default: |
| 700 | return -1; |
| 701 | } |
| 702 | } |
| 703 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 704 | /** Gets a mask from a PCM's hardware parameters. |
| 705 | * @param pcm_params A PCM's hardware parameters. |
| 706 | * @param param The parameter to get. |
| 707 | * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned. |
| 708 | * Otherwise, the mask associated with @p param is returned. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 709 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 710 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 711 | 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] | 712 | enum pcm_param param) |
| 713 | { |
| 714 | int p; |
| 715 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 716 | if (params == NULL) { |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | p = pcm_param_to_alsa(param); |
| 721 | if (p < 0 || !param_is_mask(p)) { |
| 722 | return NULL; |
| 723 | } |
| 724 | |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 725 | return (const struct pcm_mask *)param_to_mask(params, p); |
Andy Hung | ad80762 | 2014-03-10 18:08:15 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 728 | /** Get the minimum of a specified PCM parameter. |
| 729 | * @param pcm_params A PCM parameters structure. |
| 730 | * @param param The specified parameter to get the minimum of. |
| 731 | * @returns On success, the parameter minimum. |
| 732 | * On failure, zero. |
| 733 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 734 | unsigned int pcm_params_get_min(const struct pcm_params *pcm_params, |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 735 | enum pcm_param param) |
| 736 | { |
| 737 | struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params; |
| 738 | int p; |
| 739 | |
| 740 | if (!params) |
| 741 | return 0; |
| 742 | |
| 743 | p = pcm_param_to_alsa(param); |
| 744 | if (p < 0) |
| 745 | return 0; |
| 746 | |
| 747 | return param_get_min(params, p); |
| 748 | } |
| 749 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 750 | /** Get the maximum of a specified PCM parameter. |
| 751 | * @param pcm_params A PCM parameters structure. |
| 752 | * @param param The specified parameter to get the maximum of. |
| 753 | * @returns On success, the parameter maximum. |
| 754 | * On failure, zero. |
| 755 | */ |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 756 | unsigned int pcm_params_get_max(const struct pcm_params *pcm_params, |
Simon Wilson | 4354488 | 2012-10-31 12:52:39 -0700 | [diff] [blame] | 757 | enum pcm_param param) |
| 758 | { |
Taylor Holberton | 2f387d2 | 2016-12-01 15:58:16 -0800 | [diff] [blame] | 759 | 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] | 760 | int p; |
| 761 | |
| 762 | if (!params) |
| 763 | return 0; |
| 764 | |
| 765 | p = pcm_param_to_alsa(param); |
| 766 | if (p < 0) |
| 767 | return 0; |
| 768 | |
| 769 | return param_get_max(params, p); |
| 770 | } |
| 771 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 772 | /** Closes a PCM returned by @ref pcm_open. |
| 773 | * @param pcm A PCM returned by @ref pcm_open. |
| 774 | * May not be NULL. |
| 775 | * @return Always returns zero. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 776 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 777 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 778 | int pcm_close(struct pcm *pcm) |
| 779 | { |
| 780 | if (pcm == &bad_pcm) |
| 781 | return 0; |
| 782 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 783 | pcm_hw_munmap_status(pcm); |
| 784 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 785 | if (pcm->flags & PCM_MMAP) { |
| 786 | pcm_stop(pcm); |
| 787 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 788 | } |
| 789 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 790 | if (pcm->fd >= 0) |
| 791 | close(pcm->fd); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 792 | pcm->buffer_size = 0; |
| 793 | pcm->fd = -1; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 794 | free(pcm); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 795 | return 0; |
| 796 | } |
| 797 | |
Taylor Holberton | c6f908e | 2016-12-24 20:33:33 -0800 | [diff] [blame] | 798 | /** Opens a PCM by it's name. |
| 799 | * @param name The name of the PCM. |
| 800 | * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b> |
| 801 | * @param flags Specify characteristics and functionality about the pcm. |
| 802 | * May be a bitwise AND of the following: |
| 803 | * - @ref PCM_IN |
| 804 | * - @ref PCM_OUT |
| 805 | * - @ref PCM_MMAP |
| 806 | * - @ref PCM_NOIRQ |
| 807 | * - @ref PCM_MONOTONIC |
| 808 | * @param config The hardware and software parameters to open the PCM with. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 809 | * @returns A PCM structure. |
| 810 | * If an error occurs allocating memory for the PCM, NULL is returned. |
| 811 | * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready. |
| 812 | * 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] | 813 | * @ingroup libtinyalsa-pcm |
| 814 | */ |
| 815 | struct pcm *pcm_open_by_name(const char *name, |
| 816 | unsigned int flags, |
| 817 | const struct pcm_config *config) |
| 818 | { |
| 819 | unsigned int card, device; |
| 820 | if ((name[0] != 'h') |
| 821 | || (name[1] != 'w') |
| 822 | || (name[2] != ':')) { |
| 823 | return NULL; |
| 824 | } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) { |
| 825 | return NULL; |
| 826 | } |
| 827 | return pcm_open(card, device, flags, config); |
| 828 | } |
| 829 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 830 | /** Opens a PCM. |
| 831 | * @param card The card that the pcm belongs to. |
| 832 | * The default card is zero. |
| 833 | * @param device The device that the pcm belongs to. |
| 834 | * The default device is zero. |
| 835 | * @param flags Specify characteristics and functionality about the pcm. |
| 836 | * May be a bitwise AND of the following: |
| 837 | * - @ref PCM_IN |
| 838 | * - @ref PCM_OUT |
| 839 | * - @ref PCM_MMAP |
| 840 | * - @ref PCM_NOIRQ |
| 841 | * - @ref PCM_MONOTONIC |
| 842 | * @param config The hardware and software parameters to open the PCM with. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 843 | * @returns A PCM structure. |
| 844 | * If an error occurs allocating memory for the PCM, NULL is returned. |
| 845 | * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready. |
| 846 | * 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] | 847 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 848 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 849 | struct pcm *pcm_open(unsigned int card, unsigned int device, |
Taylor Holberton | 94803b0 | 2016-12-01 16:07:14 -0800 | [diff] [blame] | 850 | unsigned int flags, const struct pcm_config *config) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 851 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 852 | struct pcm *pcm; |
| 853 | struct snd_pcm_info info; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 854 | char fn[256]; |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 855 | int rc; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 856 | |
| 857 | pcm = calloc(1, sizeof(struct pcm)); |
Taylor Holberton | f319eb0 | 2016-10-14 20:05:30 -0400 | [diff] [blame] | 858 | if (!pcm) |
| 859 | return &bad_pcm; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 860 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 861 | snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device, |
| 862 | flags & PCM_IN ? 'c' : 'p'); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 863 | |
| 864 | pcm->flags = flags; |
Taylor Holberton | 093b878 | 2017-10-12 20:28:30 -0400 | [diff] [blame] | 865 | |
| 866 | if (flags & PCM_NONBLOCK) |
| 867 | pcm->fd = open(fn, O_RDWR | O_NONBLOCK); |
| 868 | else |
| 869 | pcm->fd = open(fn, O_RDWR); |
| 870 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 871 | if (pcm->fd < 0) { |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 872 | oops(pcm, errno, "cannot open device '%s'", fn); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 873 | return pcm; |
| 874 | } |
| 875 | |
| 876 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
Simon Wilson | 851aa5c | 2011-05-30 21:18:26 -0700 | [diff] [blame] | 877 | oops(pcm, errno, "cannot get info"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 878 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 879 | } |
David Wagner | 4cddf19 | 2014-04-02 15:12:54 +0200 | [diff] [blame] | 880 | pcm->subdevice = info.subdevice; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 881 | |
Taylor Holberton | 861da7a | 2017-04-10 12:05:51 -0700 | [diff] [blame] | 882 | if (pcm_set_config(pcm, config) != 0) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 883 | goto fail_close; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 884 | |
Eric Laurent | 40b018e | 2011-06-18 10:10:23 -0700 | [diff] [blame] | 885 | rc = pcm_hw_mmap_status(pcm); |
| 886 | if (rc < 0) { |
| 887 | oops(pcm, rc, "mmap status failed"); |
| 888 | goto fail; |
| 889 | } |
| 890 | |
Glenn Kasten | 8101240 | 2013-08-22 15:11:48 -0700 | [diff] [blame] | 891 | #ifdef SNDRV_PCM_IOCTL_TTSTAMP |
| 892 | if (pcm->flags & PCM_MONOTONIC) { |
| 893 | int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
| 894 | rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg); |
| 895 | if (rc < 0) { |
| 896 | oops(pcm, rc, "cannot set timestamp type"); |
| 897 | goto fail; |
| 898 | } |
| 899 | } |
| 900 | #endif |
| 901 | |
Ricardo Biehl Pasquali | 13bf629 | 2018-08-22 16:10:45 -0300 | [diff] [blame] | 902 | /* prepare here so the user does not need to do this later */ |
| 903 | if (pcm_prepare(pcm)) |
| 904 | goto fail; |
| 905 | |
Ricardo Biehl Pasquali | 1804d15 | 2019-01-04 00:32:40 -0200 | [diff] [blame] | 906 | pcm->xruns = 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 907 | return pcm; |
| 908 | |
| 909 | fail: |
Ricardo Biehl Pasquali | 0bfac89 | 2019-01-04 14:45:35 -0200 | [diff] [blame] | 910 | pcm_hw_munmap_status(pcm); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 911 | if (flags & PCM_MMAP) |
| 912 | munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size)); |
| 913 | fail_close: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 914 | close(pcm->fd); |
| 915 | pcm->fd = -1; |
| 916 | return pcm; |
| 917 | } |
| 918 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 919 | /** Checks if a PCM file has been opened without error. |
| 920 | * @param pcm A PCM handle. |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 921 | * May be NULL. |
| 922 | * @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] | 923 | * Otherwise, the function returns one. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 924 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 925 | */ |
Taylor Holberton | 15d5848 | 2016-12-01 17:46:29 -0800 | [diff] [blame] | 926 | int pcm_is_ready(const struct pcm *pcm) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 927 | { |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 928 | if (pcm != NULL) { |
| 929 | return pcm->fd >= 0; |
| 930 | } |
| 931 | return 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 932 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 933 | |
Taylor Holberton | 558e594 | 2016-12-04 13:42:28 -0800 | [diff] [blame] | 934 | /** Links two PCMs. |
| 935 | * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time). |
| 936 | * If an error occurs, the error message will be written to @p pcm1. |
| 937 | * @param pcm1 A PCM handle. |
| 938 | * @param pcm2 Another PCM handle. |
| 939 | * @return On success, zero; on failure, a negative number. |
| 940 | * @ingroup libtinyalsa-pcm |
| 941 | */ |
| 942 | int pcm_link(struct pcm *pcm1, struct pcm *pcm2) |
| 943 | { |
| 944 | int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd); |
| 945 | if (err == -1) { |
| 946 | return oops(pcm1, errno, "cannot link PCM"); |
| 947 | } |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | /** Unlinks a PCM. |
| 952 | * @see @ref pcm_link |
| 953 | * @param pcm A PCM handle. |
| 954 | * @return On success, zero; on failure, a negative number. |
| 955 | * @ingroup libtinyalsa-pcm |
| 956 | */ |
| 957 | int pcm_unlink(struct pcm *pcm) |
| 958 | { |
| 959 | int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK); |
| 960 | if (err == -1) { |
| 961 | return oops(pcm, errno, "cannot unlink PCM"); |
| 962 | } |
| 963 | return 0; |
| 964 | } |
| 965 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 966 | /** Prepares a PCM, if it has not been prepared already. |
| 967 | * @param pcm A PCM handle. |
| 968 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 969 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 970 | */ |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 971 | int pcm_prepare(struct pcm *pcm) |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 972 | { |
| 973 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0) |
| 974 | return oops(pcm, errno, "cannot prepare channel"); |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 975 | |
Ricardo Biehl Pasquali | 535a6ba | 2018-12-30 11:53:54 -0200 | [diff] [blame] | 976 | /* get appl_ptr and avail_min from kernel */ |
| 977 | pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN); |
| 978 | |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 982 | /** Starts a PCM. |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 983 | * @param pcm A PCM handle. |
| 984 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 985 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 986 | */ |
Omair Mohammed Abdullah | c9032a0 | 2013-01-31 16:35:39 +0530 | [diff] [blame] | 987 | int pcm_start(struct pcm *pcm) |
| 988 | { |
Ricardo Biehl Pasquali | 7ff9cde | 2018-12-31 17:23:39 -0200 | [diff] [blame] | 989 | /* set appl_ptr and avail_min in kernel */ |
Ricardo Biehl Pasquali | b38b6a6 | 2019-01-07 11:54:47 -0200 | [diff] [blame] | 990 | if (pcm_sync_ptr(pcm, 0) < 0) |
| 991 | return -1; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 992 | |
Miguel Gaio | cf5f063 | 2018-07-17 13:30:24 +0200 | [diff] [blame] | 993 | if (pcm->mmap_status->state != PCM_STATE_RUNNING) { |
| 994 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0) |
| 995 | return oops(pcm, errno, "cannot start channel"); |
| 996 | } |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1001 | /** Stops a PCM. |
| 1002 | * @param pcm A PCM handle. |
| 1003 | * @return On success, zero; on failure, a negative number. |
Taylor Holberton | 8e1b102 | 2016-11-19 10:34:50 -0800 | [diff] [blame] | 1004 | * @ingroup libtinyalsa-pcm |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1005 | */ |
Simon Wilson | d6458e6 | 2011-06-21 14:58:11 -0700 | [diff] [blame] | 1006 | int pcm_stop(struct pcm *pcm) |
| 1007 | { |
| 1008 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0) |
| 1009 | return oops(pcm, errno, "cannot stop channel"); |
| 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1014 | static inline int pcm_mmap_playback_avail(struct pcm *pcm) |
| 1015 | { |
| 1016 | int avail; |
| 1017 | |
| 1018 | avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr; |
| 1019 | |
| 1020 | if (avail < 0) |
| 1021 | avail += pcm->boundary; |
StevenNAN | b0fc3e9 | 2014-03-17 11:14:49 +0800 | [diff] [blame] | 1022 | else if (avail >= (int)pcm->boundary) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1023 | avail -= pcm->boundary; |
| 1024 | |
| 1025 | return avail; |
| 1026 | } |
| 1027 | |
| 1028 | static inline int pcm_mmap_capture_avail(struct pcm *pcm) |
| 1029 | { |
| 1030 | int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr; |
| 1031 | if (avail < 0) |
| 1032 | avail += pcm->boundary; |
| 1033 | return avail; |
| 1034 | } |
| 1035 | |
| 1036 | static inline int pcm_mmap_avail(struct pcm *pcm) |
| 1037 | { |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1038 | if (pcm->flags & PCM_IN) |
| 1039 | return pcm_mmap_capture_avail(pcm); |
| 1040 | else |
| 1041 | return pcm_mmap_playback_avail(pcm); |
| 1042 | } |
| 1043 | |
| 1044 | static void pcm_mmap_appl_forward(struct pcm *pcm, int frames) |
| 1045 | { |
| 1046 | unsigned int appl_ptr = pcm->mmap_control->appl_ptr; |
| 1047 | appl_ptr += frames; |
| 1048 | |
| 1049 | /* check for boundary wrap */ |
| 1050 | if (appl_ptr > pcm->boundary) |
| 1051 | appl_ptr -= pcm->boundary; |
| 1052 | pcm->mmap_control->appl_ptr = appl_ptr; |
| 1053 | } |
| 1054 | |
| 1055 | int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, |
| 1056 | unsigned int *frames) |
| 1057 | { |
| 1058 | unsigned int continuous, copy_frames, avail; |
| 1059 | |
| 1060 | /* return the mmap buffer */ |
| 1061 | *areas = pcm->mmap_buffer; |
| 1062 | |
| 1063 | /* and the application offset in frames */ |
| 1064 | *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size; |
| 1065 | |
| 1066 | avail = pcm_mmap_avail(pcm); |
| 1067 | if (avail > pcm->buffer_size) |
| 1068 | avail = pcm->buffer_size; |
| 1069 | continuous = pcm->buffer_size - *offset; |
| 1070 | |
| 1071 | /* we can only copy frames if the are availabale and continuos */ |
| 1072 | copy_frames = *frames; |
| 1073 | if (copy_frames > avail) |
| 1074 | copy_frames = avail; |
| 1075 | if (copy_frames > continuous) |
| 1076 | copy_frames = continuous; |
| 1077 | *frames = copy_frames; |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1082 | static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset, |
| 1083 | char *buf, unsigned int src_offset, |
| 1084 | unsigned int frames) |
| 1085 | { |
| 1086 | int size_bytes = pcm_frames_to_bytes(pcm, frames); |
| 1087 | int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset); |
| 1088 | int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset); |
| 1089 | |
| 1090 | /* interleaved only atm */ |
| 1091 | if (pcm->flags & PCM_IN) |
| 1092 | memcpy(buf + src_offset_bytes, |
| 1093 | (char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 1094 | size_bytes); |
| 1095 | else |
| 1096 | memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes, |
| 1097 | buf + src_offset_bytes, |
| 1098 | size_bytes); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1102 | int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames) |
| 1103 | { |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1104 | int ret; |
| 1105 | |
Taylor Holberton | 73466c0 | 2016-10-01 12:51:59 -0400 | [diff] [blame] | 1106 | /* not used */ |
| 1107 | (void) offset; |
| 1108 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1109 | /* update the application pointer in userspace and kernel */ |
| 1110 | pcm_mmap_appl_forward(pcm, frames); |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1111 | ret = pcm_sync_ptr(pcm, 0); |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 1112 | if (ret != 0){ |
| 1113 | printf("%d\n", ret); |
Taylor Holberton | 72e4422 | 2016-11-22 09:54:47 -0800 | [diff] [blame] | 1114 | return ret; |
Taylor Holberton | e123a65 | 2017-01-13 21:39:48 -0800 | [diff] [blame] | 1115 | } |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1116 | |
| 1117 | return frames; |
| 1118 | } |
| 1119 | |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1120 | static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf, |
| 1121 | unsigned int offset, unsigned int size) |
| 1122 | { |
| 1123 | void *pcm_areas; |
| 1124 | int commit; |
| 1125 | unsigned int pcm_offset, frames, count = 0; |
| 1126 | |
Ricardo Biehl Pasquali | 7aacaed | 2018-12-18 23:24:19 -0200 | [diff] [blame] | 1127 | while (pcm_mmap_avail(pcm) && size) { |
Ricardo Biehl Pasquali | d759f48 | 2018-12-22 11:17:32 -0200 | [diff] [blame] | 1128 | frames = size; |
| 1129 | pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames); |
| 1130 | pcm_areas_copy(pcm, pcm_offset, buf, offset, frames); |
| 1131 | commit = pcm_mmap_commit(pcm, pcm_offset, frames); |
| 1132 | if (commit < 0) { |
| 1133 | oops(pcm, commit, "failed to commit %d frames\n", frames); |
| 1134 | return commit; |
| 1135 | } |
| 1136 | |
| 1137 | offset += commit; |
| 1138 | count += commit; |
| 1139 | size -= commit; |
| 1140 | } |
| 1141 | return count; |
| 1142 | } |
| 1143 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1144 | int pcm_avail_update(struct pcm *pcm) |
| 1145 | { |
Ricardo Biehl Pasquali | b00f90a | 2018-12-22 12:51:21 -0200 | [diff] [blame] | 1146 | 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] | 1147 | return pcm_mmap_avail(pcm); |
| 1148 | } |
| 1149 | |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1150 | /** Returns available frames in pcm buffer and corresponding time stamp. |
| 1151 | * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open, |
| 1152 | * otherwise the clock is CLOCK_REALTIME. |
| 1153 | * For an input stream, frames available are frames ready for the application to read. |
| 1154 | * 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] | 1155 | * @param pcm A PCM handle. |
| 1156 | * @param avail The number of available frames |
| 1157 | * @param tstamp The timestamp |
| 1158 | * @return On success, zero is returned; on failure, negative one. |
| 1159 | */ |
| 1160 | int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail, |
| 1161 | struct timespec *tstamp) |
| 1162 | { |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1163 | int checking; |
| 1164 | int tmp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1165 | |
| 1166 | if (!pcm_is_ready(pcm)) |
| 1167 | return -1; |
| 1168 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1169 | checking = 0; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1170 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1171 | again: |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1172 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1173 | tmp = pcm_avail_update(pcm); |
| 1174 | if (tmp < 0) |
| 1175 | return tmp; /* error */ |
| 1176 | |
| 1177 | if (checking && (unsigned int) tmp == *avail) |
| 1178 | return 0; |
| 1179 | |
| 1180 | *avail = (unsigned int) tmp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1181 | *tstamp = pcm->mmap_status->tstamp; |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1182 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1183 | /* |
| 1184 | * When status is mmaped, get avail again to ensure |
| 1185 | * valid timestamp. |
| 1186 | */ |
| 1187 | if (!pcm->sync_ptr) { |
| 1188 | checking = 1; |
| 1189 | goto again; |
| 1190 | } |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1191 | |
Ricardo Biehl Pasquali | 7bdb05d | 2018-12-22 14:42:57 -0200 | [diff] [blame] | 1192 | /* SYNC_PTR ioctl was used, no need to check avail */ |
Ricardo Biehl Pasquali | 19f84c3 | 2018-12-24 12:40:31 -0200 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1196 | int pcm_state(struct pcm *pcm) |
| 1197 | { |
| 1198 | int err = pcm_sync_ptr(pcm, 0); |
| 1199 | if (err < 0) |
| 1200 | return err; |
| 1201 | |
| 1202 | return pcm->mmap_status->state; |
| 1203 | } |
| 1204 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 1205 | /** Waits for frames to be available for read or write operations. |
| 1206 | * @param pcm A PCM handle. |
| 1207 | * @param timeout The maximum amount of time to wait for, in terms of milliseconds. |
| 1208 | * @returns If frames became available, one is returned. |
| 1209 | * If a timeout occured, zero is returned. |
| 1210 | * If an error occured, a negative number is returned. |
| 1211 | * @ingroup libtinyalsa-pcm |
| 1212 | */ |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1213 | int pcm_wait(struct pcm *pcm, int timeout) |
| 1214 | { |
| 1215 | struct pollfd pfd; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1216 | int err; |
| 1217 | |
| 1218 | pfd.fd = pcm->fd; |
Apelete Seketeli | 84889d0 | 2014-02-14 14:34:32 +0100 | [diff] [blame] | 1219 | pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1220 | |
| 1221 | do { |
| 1222 | /* let's wait for avail or timeout */ |
| 1223 | err = poll(&pfd, 1, timeout); |
| 1224 | if (err < 0) |
| 1225 | return -errno; |
| 1226 | |
| 1227 | /* timeout ? */ |
| 1228 | if (err == 0) |
| 1229 | return 0; |
| 1230 | |
| 1231 | /* have we been interrupted ? */ |
| 1232 | if (errno == -EINTR) |
| 1233 | continue; |
| 1234 | |
| 1235 | /* check for any errors */ |
| 1236 | if (pfd.revents & (POLLERR | POLLNVAL)) { |
| 1237 | switch (pcm_state(pcm)) { |
| 1238 | case PCM_STATE_XRUN: |
| 1239 | return -EPIPE; |
| 1240 | case PCM_STATE_SUSPENDED: |
| 1241 | return -ESTRPIPE; |
| 1242 | case PCM_STATE_DISCONNECTED: |
| 1243 | return -ENODEV; |
| 1244 | default: |
| 1245 | return -EIO; |
| 1246 | } |
| 1247 | } |
| 1248 | /* poll again if fd not ready for IO */ |
| 1249 | } while (!(pfd.revents & (POLLIN | POLLOUT))); |
| 1250 | |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1254 | /* |
| 1255 | * Transfer data to/from mmaped buffer. This imitates the |
| 1256 | * behavior of read/write system calls. |
| 1257 | * |
| 1258 | * However, this doesn't seems to offer any advantage over |
| 1259 | * the read/write syscalls. Should it be removed? |
| 1260 | */ |
| 1261 | int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1262 | { |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1263 | int is_playback; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1264 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1265 | int state; |
| 1266 | unsigned int avail; |
| 1267 | unsigned int user_offset; |
| 1268 | |
| 1269 | int err; |
| 1270 | int tmp; |
| 1271 | |
| 1272 | is_playback = !(pcm->flags & PCM_IN); |
| 1273 | |
| 1274 | if (frames == 0) |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1275 | return 0; |
| 1276 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1277 | /* update hardware pointer and get state */ |
| 1278 | err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC | |
| 1279 | SNDRV_PCM_SYNC_PTR_APPL | |
| 1280 | SNDRV_PCM_SYNC_PTR_AVAIL_MIN); |
| 1281 | if (err == -1) |
| 1282 | return -1; |
| 1283 | state = pcm->mmap_status->state; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1284 | |
Ricardo Biehl Pasquali | c6c0d78 | 2019-03-12 16:31:04 -0300 | [diff] [blame] | 1285 | /* |
| 1286 | * If frames < start_threshold, wait indefinitely. |
| 1287 | * Another thread may start capture |
| 1288 | */ |
| 1289 | if (!is_playback && state == PCM_STATE_PREPARED && |
| 1290 | frames >= pcm->config.start_threshold) { |
| 1291 | err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START); |
| 1292 | if (err == -1) |
| 1293 | return -1; |
| 1294 | /* state = PCM_STATE_RUNNING */ |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1297 | avail = pcm_mmap_avail(pcm); |
| 1298 | user_offset = 0; |
| 1299 | |
| 1300 | while (frames) { |
| 1301 | if (!avail) { |
| 1302 | if (pcm->flags & PCM_NONBLOCK) { |
| 1303 | errno = EAGAIN; |
| 1304 | break; |
| 1305 | } |
| 1306 | |
| 1307 | /* wait for interrupt */ |
| 1308 | err = pcm_wait(pcm, -1); |
| 1309 | if (err < 0) { |
| 1310 | errno = -err; |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | /* get hardware pointer */ |
| 1315 | avail = pcm_avail_update(pcm); |
| 1316 | } |
| 1317 | |
| 1318 | tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames); |
| 1319 | if (tmp < 0) |
| 1320 | break; |
| 1321 | |
| 1322 | user_offset += tmp; |
| 1323 | frames -= tmp; |
| 1324 | avail -= tmp; |
| 1325 | |
| 1326 | /* start playback if written >= start_threshold */ |
| 1327 | if (is_playback && state == PCM_STATE_PREPARED && |
| 1328 | pcm->buffer_size - avail >= pcm->config.start_threshold) { |
| 1329 | err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START); |
| 1330 | if (err == -1) |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | return user_offset ? (int) user_offset : -1; |
Liam Girdwood | 6be28f1 | 2011-10-13 12:59:51 -0700 | [diff] [blame] | 1336 | } |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1337 | |
| 1338 | int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1339 | { |
| 1340 | if ((~pcm->flags) & (PCM_OUT | PCM_MMAP)) |
| 1341 | return -ENOSYS; |
| 1342 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1343 | return pcm_mmap_transfer(pcm, (void *)data, |
| 1344 | pcm_bytes_to_frames(pcm, count)); |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count) |
| 1348 | { |
| 1349 | if ((~pcm->flags) & (PCM_IN | PCM_MMAP)) |
| 1350 | return -ENOSYS; |
| 1351 | |
Ricardo Biehl Pasquali | 4ee09a9 | 2018-12-18 23:26:15 -0200 | [diff] [blame] | 1352 | return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count)); |
Eric Laurent | bb7c5df | 2013-09-16 14:31:17 -0700 | [diff] [blame] | 1353 | } |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 1354 | |
Ricardo Biehl Pasquali | 03cc7c5 | 2019-01-04 00:20:14 -0200 | [diff] [blame] | 1355 | static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames) |
| 1356 | { |
| 1357 | int is_playback; |
| 1358 | |
| 1359 | struct snd_xferi transfer; |
| 1360 | int res; |
| 1361 | |
| 1362 | is_playback = !(pcm->flags & PCM_IN); |
| 1363 | |
| 1364 | transfer.buf = data; |
| 1365 | transfer.frames = frames; |
| 1366 | transfer.result = 0; |
| 1367 | |
| 1368 | res = ioctl(pcm->fd, is_playback |
| 1369 | ? SNDRV_PCM_IOCTL_WRITEI_FRAMES |
| 1370 | : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer); |
| 1371 | |
| 1372 | return res == 0 ? (int) transfer.result : -1; |
| 1373 | } |
| 1374 | |
| 1375 | static int pcm_generic_transfer(struct pcm *pcm, void *data, |
| 1376 | unsigned int frames) |
| 1377 | { |
| 1378 | int res; |
| 1379 | |
| 1380 | #if UINT_MAX > TINYALSA_FRAMES_MAX |
| 1381 | if (frames > TINYALSA_FRAMES_MAX) |
| 1382 | return -EINVAL; |
| 1383 | #endif |
| 1384 | if (frames > INT_MAX) |
| 1385 | return -EINVAL; |
| 1386 | |
| 1387 | again: |
| 1388 | |
| 1389 | if (pcm->flags & PCM_MMAP) |
| 1390 | res = pcm_mmap_transfer(pcm, data, frames); |
| 1391 | else |
| 1392 | res = pcm_rw_transfer(pcm, data, frames); |
| 1393 | |
| 1394 | if (res < 0) { |
| 1395 | switch (errno) { |
| 1396 | case EPIPE: |
Ricardo Biehl Pasquali | 1804d15 | 2019-01-04 00:32:40 -0200 | [diff] [blame] | 1397 | pcm->xruns++; |
Ricardo Biehl Pasquali | 03cc7c5 | 2019-01-04 00:20:14 -0200 | [diff] [blame] | 1398 | /* fallthrough */ |
| 1399 | case ESTRPIPE: |
| 1400 | /* |
| 1401 | * Try to restart if we are allowed to do so. |
| 1402 | * Otherwise, return error. |
| 1403 | */ |
| 1404 | if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm)) |
| 1405 | return -1; |
| 1406 | goto again; |
| 1407 | case EAGAIN: |
| 1408 | if (pcm->flags & PCM_NONBLOCK) |
| 1409 | return -1; |
| 1410 | /* fallthrough */ |
| 1411 | default: |
| 1412 | return oops(pcm, errno, "cannot read/write stream data"); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | return res; |
| 1417 | } |
| 1418 | |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1419 | /** Writes audio samples to PCM. |
| 1420 | * If the PCM has not been started, it is started in this function. |
| 1421 | * This function is only valid for PCMs opened with the @ref PCM_OUT flag. |
| 1422 | * @param pcm A PCM handle. |
| 1423 | * @param data The audio sample array |
| 1424 | * @param frame_count The number of frames occupied by the sample array. |
| 1425 | * This value should not be greater than @ref TINYALSA_FRAMES_MAX |
| 1426 | * or INT_MAX. |
| 1427 | * @return On success, this function returns the number of frames written; otherwise, a negative number. |
| 1428 | * @ingroup libtinyalsa-pcm |
| 1429 | */ |
| 1430 | int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count) |
| 1431 | { |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1432 | if (pcm->flags & PCM_IN) |
| 1433 | return -EINVAL; |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1434 | |
Ricardo Biehl Pasquali | 03cc7c5 | 2019-01-04 00:20:14 -0200 | [diff] [blame] | 1435 | return pcm_generic_transfer(pcm, (void*) data, frame_count); |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /** Reads audio samples from PCM. |
| 1439 | * If the PCM has not been started, it is started in this function. |
| 1440 | * This function is only valid for PCMs opened with the @ref PCM_IN flag. |
| 1441 | * @param pcm A PCM handle. |
| 1442 | * @param data The audio sample array |
| 1443 | * @param frame_count The number of frames occupied by the sample array. |
| 1444 | * This value should not be greater than @ref TINYALSA_FRAMES_MAX |
| 1445 | * or INT_MAX. |
| 1446 | * @return On success, this function returns the number of frames written; otherwise, a negative number. |
| 1447 | * @ingroup libtinyalsa-pcm |
| 1448 | */ |
| 1449 | int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count) |
| 1450 | { |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1451 | if (!(pcm->flags & PCM_IN)) |
| 1452 | return -EINVAL; |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1453 | |
Ricardo Biehl Pasquali | 03cc7c5 | 2019-01-04 00:20:14 -0200 | [diff] [blame] | 1454 | return pcm_generic_transfer(pcm, data, frame_count); |
Ricardo Biehl Pasquali | b8ea4c4 | 2018-12-24 18:34:48 -0200 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /** Writes audio samples to PCM. |
| 1458 | * If the PCM has not been started, it is started in this function. |
| 1459 | * This function is only valid for PCMs opened with the @ref PCM_OUT flag. |
| 1460 | * This function is not valid for PCMs opened with the @ref PCM_MMAP flag. |
| 1461 | * @param pcm A PCM handle. |
| 1462 | * @param data The audio sample array |
| 1463 | * @param count The number of bytes occupied by the sample array. |
| 1464 | * @return On success, this function returns zero; otherwise, a negative number. |
| 1465 | * @deprecated |
| 1466 | * @ingroup libtinyalsa-pcm |
| 1467 | */ |
| 1468 | int pcm_write(struct pcm *pcm, const void *data, unsigned int count) |
| 1469 | { |
| 1470 | return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count)); |
| 1471 | } |
| 1472 | |
| 1473 | /** Reads audio samples from PCM. |
| 1474 | * If the PCM has not been started, it is started in this function. |
| 1475 | * This function is only valid for PCMs opened with the @ref PCM_IN flag. |
| 1476 | * This function is not valid for PCMs opened with the @ref PCM_MMAP flag. |
| 1477 | * @param pcm A PCM handle. |
| 1478 | * @param data The audio sample array |
| 1479 | * @param count The number of bytes occupied by the sample array. |
| 1480 | * @return On success, this function returns zero; otherwise, a negative number. |
| 1481 | * @deprecated |
| 1482 | * @ingroup libtinyalsa-pcm |
| 1483 | */ |
| 1484 | int pcm_read(struct pcm *pcm, void *data, unsigned int count) |
| 1485 | { |
| 1486 | return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count)); |
| 1487 | } |
| 1488 | |
Taylor Holberton | 17a1024 | 2016-11-23 13:18:24 -0800 | [diff] [blame] | 1489 | /** Gets the delay of the PCM, in terms of frames. |
| 1490 | * @param pcm A PCM handle. |
| 1491 | * @returns On success, the delay of the PCM. |
| 1492 | * On failure, a negative number. |
| 1493 | * @ingroup libtinyalsa-pcm |
| 1494 | */ |
Hardik T Shah | 9ecb93f | 2014-04-10 18:03:52 +0530 | [diff] [blame] | 1495 | long pcm_get_delay(struct pcm *pcm) |
| 1496 | { |
| 1497 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0) |
| 1498 | return -1; |
| 1499 | |
| 1500 | return pcm->pcm_delay; |
| 1501 | } |
Taylor Holberton | 6d58e01 | 2016-10-01 18:32:30 -0400 | [diff] [blame] | 1502 | |