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