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