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