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