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> |
| 36 | |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <sys/time.h> |
| 40 | |
| 41 | #include <linux/ioctl.h> |
| 42 | #define __force |
| 43 | #define __bitwise |
| 44 | #define __user |
| 45 | #include <sound/asound.h> |
| 46 | |
| 47 | #include <tinyalsa/asoundlib.h> |
| 48 | |
| 49 | #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL |
| 50 | |
| 51 | static inline int param_is_mask(int p) |
| 52 | { |
| 53 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) && |
| 54 | (p <= SNDRV_PCM_HW_PARAM_LAST_MASK); |
| 55 | } |
| 56 | |
| 57 | static inline int param_is_interval(int p) |
| 58 | { |
| 59 | return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) && |
| 60 | (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL); |
| 61 | } |
| 62 | |
| 63 | static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n) |
| 64 | { |
| 65 | return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]); |
| 66 | } |
| 67 | |
| 68 | static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n) |
| 69 | { |
| 70 | return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]); |
| 71 | } |
| 72 | |
| 73 | static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit) |
| 74 | { |
| 75 | if (bit >= SNDRV_MASK_MAX) |
| 76 | return; |
| 77 | if (param_is_mask(n)) { |
| 78 | struct snd_mask *m = param_to_mask(p, n); |
| 79 | m->bits[0] = 0; |
| 80 | m->bits[1] = 0; |
| 81 | m->bits[bit >> 5] |= (1 << (bit & 31)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 86 | { |
| 87 | if (param_is_interval(n)) { |
| 88 | struct snd_interval *i = param_to_interval(p, n); |
| 89 | i->min = val; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 94 | { |
| 95 | if (param_is_interval(n)) { |
| 96 | struct snd_interval *i = param_to_interval(p, n); |
| 97 | i->max = val; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val) |
| 102 | { |
| 103 | if (param_is_interval(n)) { |
| 104 | struct snd_interval *i = param_to_interval(p, n); |
| 105 | i->min = val; |
| 106 | i->max = val; |
| 107 | i->integer = 1; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void param_init(struct snd_pcm_hw_params *p) |
| 112 | { |
| 113 | int n; |
| 114 | memset(p, 0, sizeof(*p)); |
| 115 | for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK; |
| 116 | n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) { |
| 117 | struct snd_mask *m = param_to_mask(p, n); |
| 118 | m->bits[0] = ~0; |
| 119 | m->bits[1] = ~0; |
| 120 | } |
| 121 | for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; |
| 122 | n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) { |
| 123 | struct snd_interval *i = param_to_interval(p, n); |
| 124 | i->min = 0; |
| 125 | i->max = ~0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #define PCM_ERROR_MAX 128 |
| 130 | |
| 131 | struct pcm { |
| 132 | int fd; |
| 133 | unsigned int flags; |
| 134 | int running:1; |
| 135 | int underruns; |
| 136 | unsigned int buffer_size; |
| 137 | char error[PCM_ERROR_MAX]; |
| 138 | struct pcm_config config; |
| 139 | }; |
| 140 | |
| 141 | unsigned int pcm_buffer_size(struct pcm *pcm) |
| 142 | { |
| 143 | return pcm->buffer_size; |
| 144 | } |
| 145 | |
| 146 | const char* pcm_get_error(struct pcm *pcm) |
| 147 | { |
| 148 | return pcm->error; |
| 149 | } |
| 150 | |
| 151 | static int oops(struct pcm *pcm, int e, const char *fmt, ...) |
| 152 | { |
| 153 | va_list ap; |
| 154 | int sz; |
| 155 | |
| 156 | va_start(ap, fmt); |
| 157 | vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap); |
| 158 | va_end(ap); |
| 159 | sz = strlen(pcm->error); |
| 160 | |
| 161 | if (errno) |
| 162 | snprintf(pcm->error + sz, PCM_ERROR_MAX - sz, |
| 163 | ": %s", strerror(e)); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | int pcm_write(struct pcm *pcm, void *data, unsigned int count) |
| 168 | { |
| 169 | struct snd_xferi x; |
| 170 | |
| 171 | if (pcm->flags & PCM_IN) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | x.buf = data; |
| 175 | x.frames = count / (pcm->config.channels * 2); /* TODO: handle 32bit */ |
| 176 | |
| 177 | for (;;) { |
| 178 | if (!pcm->running) { |
| 179 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE)) |
| 180 | return oops(pcm, errno, "cannot prepare channel"); |
| 181 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) |
| 182 | return oops(pcm, errno, "cannot write initial data"); |
| 183 | pcm->running = 1; |
| 184 | return 0; |
| 185 | } |
| 186 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) { |
| 187 | pcm->running = 0; |
| 188 | if (errno == EPIPE) { |
| 189 | /* we failed to make our window -- try to restart */ |
| 190 | pcm->underruns++; |
| 191 | continue; |
| 192 | } |
| 193 | return oops(pcm, errno, "cannot write stream data"); |
| 194 | } |
| 195 | return 0; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int pcm_read(struct pcm *pcm, void *data, unsigned int count) |
| 200 | { |
| 201 | struct snd_xferi x; |
| 202 | |
| 203 | if (!(pcm->flags & PCM_IN)) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | x.buf = data; |
| 207 | x.frames = count / (pcm->config.channels * 2); /* TODO: handle 32bit */ |
| 208 | |
| 209 | for (;;) { |
| 210 | if (!pcm->running) { |
| 211 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE)) |
| 212 | return oops(pcm, errno, "cannot prepare channel"); |
| 213 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START)) |
| 214 | return oops(pcm, errno, "cannot start channel"); |
| 215 | pcm->running = 1; |
| 216 | } |
| 217 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) { |
| 218 | pcm->running = 0; |
| 219 | if (errno == EPIPE) { |
| 220 | /* we failed to make our window -- try to restart */ |
| 221 | pcm->underruns++; |
| 222 | continue; |
| 223 | } |
| 224 | return oops(pcm, errno, "cannot read stream data"); |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static struct pcm bad_pcm = { |
| 231 | .fd = -1, |
| 232 | }; |
| 233 | |
| 234 | int pcm_close(struct pcm *pcm) |
| 235 | { |
| 236 | if (pcm == &bad_pcm) |
| 237 | return 0; |
| 238 | |
| 239 | if (pcm->fd >= 0) |
| 240 | close(pcm->fd); |
| 241 | pcm->running = 0; |
| 242 | pcm->buffer_size = 0; |
| 243 | pcm->fd = -1; |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static unsigned int pcm_format_to_alsa(enum pcm_format format) |
| 248 | { |
| 249 | switch (format) { |
| 250 | case PCM_FORMAT_S32_LE: |
| 251 | return SNDRV_PCM_FORMAT_S32_LE; |
| 252 | default: |
| 253 | case PCM_FORMAT_S16_LE: |
| 254 | return SNDRV_PCM_FORMAT_S16_LE; |
| 255 | }; |
| 256 | } |
| 257 | |
| 258 | static unsigned int pcm_format_to_bits(enum pcm_format format) |
| 259 | { |
| 260 | switch (format) { |
| 261 | case PCM_FORMAT_S32_LE: |
| 262 | return 32; |
| 263 | default: |
| 264 | case PCM_FORMAT_S16_LE: |
| 265 | return 16; |
| 266 | }; |
| 267 | } |
| 268 | |
| 269 | struct pcm *pcm_open(unsigned int device, unsigned int flags, struct pcm_config *config) |
| 270 | { |
| 271 | const char *dname; |
| 272 | struct pcm *pcm; |
| 273 | struct snd_pcm_info info; |
| 274 | struct snd_pcm_hw_params params; |
| 275 | struct snd_pcm_sw_params sparams; |
| 276 | |
| 277 | pcm = calloc(1, sizeof(struct pcm)); |
| 278 | if (!pcm || !config) |
| 279 | return &bad_pcm; /* TODO: could support default config here */ |
| 280 | |
| 281 | pcm->config = *config; |
| 282 | |
| 283 | if (flags & PCM_IN) { |
| 284 | dname = "/dev/snd/pcmC0D0c"; |
| 285 | } else { |
| 286 | dname = "/dev/snd/pcmC0D0p"; |
| 287 | } |
| 288 | |
| 289 | pcm->flags = flags; |
| 290 | pcm->fd = open(dname, O_RDWR); |
| 291 | if (pcm->fd < 0) { |
| 292 | oops(pcm, errno, "cannot open device '%s'"); |
| 293 | return pcm; |
| 294 | } |
| 295 | |
| 296 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) { |
| 297 | oops(pcm, errno, "cannot get info - %s"); |
| 298 | goto fail; |
| 299 | } |
| 300 | |
| 301 | param_init(¶ms); |
| 302 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS, |
| 303 | SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 304 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT, |
| 305 | pcm_format_to_alsa(config->format)); |
| 306 | param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 307 | SNDRV_PCM_SUBFORMAT_STD); |
| 308 | param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size); |
| 309 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 310 | pcm_format_to_bits(config->format)); |
| 311 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 312 | pcm_format_to_bits(config->format) * config->channels); |
| 313 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 314 | config->channels); |
| 315 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count); |
| 316 | param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate); |
| 317 | |
| 318 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) { |
| 319 | oops(pcm, errno, "cannot set hw params"); |
| 320 | goto fail; |
| 321 | } |
| 322 | |
| 323 | memset(&sparams, 0, sizeof(sparams)); |
| 324 | sparams.tstamp_mode = SNDRV_PCM_TSTAMP_NONE; |
| 325 | sparams.period_step = 1; |
| 326 | sparams.avail_min = 1; |
| 327 | sparams.start_threshold = config->period_count * config->period_size; |
| 328 | sparams.stop_threshold = config->period_count * config->period_size; |
| 329 | sparams.xfer_align = config->period_size / 2; /* needed for old kernels */ |
| 330 | sparams.silence_size = 0; |
| 331 | sparams.silence_threshold = 0; |
| 332 | |
| 333 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) { |
| 334 | oops(pcm, errno, "cannot set sw params"); |
| 335 | goto fail; |
| 336 | } |
| 337 | |
| 338 | pcm->buffer_size = config->period_count * config->period_size; |
| 339 | pcm->underruns = 0; |
| 340 | return pcm; |
| 341 | |
| 342 | fail: |
| 343 | close(pcm->fd); |
| 344 | pcm->fd = -1; |
| 345 | return pcm; |
| 346 | } |
| 347 | |
| 348 | int pcm_is_ready(struct pcm *pcm) |
| 349 | { |
| 350 | return pcm->fd >= 0; |
| 351 | } |