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