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