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 */
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 #include <poll.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <sys/time.h>
41 #include <limits.h>
43 #include <linux/ioctl.h>
44 #define __force
45 #define __bitwise
46 #define __user
47 #include <sound/asound.h>
49 #include <tinyalsa/asoundlib.h>
51 #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
52 #define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
54 static inline int param_is_mask(int p)
55 {
56 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
57 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
58 }
60 static inline int param_is_interval(int p)
61 {
62 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
63 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
64 }
66 static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
67 {
68 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
69 }
71 static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
72 {
73 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
74 }
76 static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
77 {
78 if (bit >= SNDRV_MASK_MAX)
79 return;
80 if (param_is_mask(n)) {
81 struct snd_mask *m = param_to_mask(p, n);
82 m->bits[0] = 0;
83 m->bits[1] = 0;
84 m->bits[bit >> 5] |= (1 << (bit & 31));
85 }
86 }
88 static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
89 {
90 if (param_is_interval(n)) {
91 struct snd_interval *i = param_to_interval(p, n);
92 i->min = val;
93 }
94 }
96 static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
97 {
98 if (param_is_interval(n)) {
99 struct snd_interval *i = param_to_interval(p, n);
100 return i->min;
101 }
102 return 0;
103 }
105 static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
106 {
107 if (param_is_interval(n)) {
108 struct snd_interval *i = param_to_interval(p, n);
109 return i->max;
110 }
111 return 0;
112 }
114 static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
115 {
116 if (param_is_interval(n)) {
117 struct snd_interval *i = param_to_interval(p, n);
118 i->min = val;
119 i->max = val;
120 i->integer = 1;
121 }
122 }
124 static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
125 {
126 if (param_is_interval(n)) {
127 struct snd_interval *i = param_to_interval(p, n);
128 if (i->integer)
129 return i->max;
130 }
131 return 0;
132 }
134 static void param_init(struct snd_pcm_hw_params *p)
135 {
136 int n;
138 memset(p, 0, sizeof(*p));
139 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
140 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
141 struct snd_mask *m = param_to_mask(p, n);
142 m->bits[0] = ~0;
143 m->bits[1] = ~0;
144 }
145 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
146 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
147 struct snd_interval *i = param_to_interval(p, n);
148 i->min = 0;
149 i->max = ~0;
150 }
151 p->rmask = ~0U;
152 p->cmask = 0;
153 p->info = ~0U;
154 }
156 #define PCM_ERROR_MAX 128
158 struct pcm {
159 int fd;
160 unsigned int flags;
161 int running:1;
162 int underruns;
163 unsigned int buffer_size;
164 unsigned int boundary;
165 char error[PCM_ERROR_MAX];
166 struct pcm_config config;
167 struct snd_pcm_mmap_status *mmap_status;
168 struct snd_pcm_mmap_control *mmap_control;
169 struct snd_pcm_sync_ptr *sync_ptr;
170 void *mmap_buffer;
171 unsigned int noirq_frames_per_msec;
172 int wait_for_avail_min;
173 };
175 unsigned int pcm_get_buffer_size(struct pcm *pcm)
176 {
177 return pcm->buffer_size;
178 }
180 const char* pcm_get_error(struct pcm *pcm)
181 {
182 return pcm->error;
183 }
185 static int oops(struct pcm *pcm, int e, const char *fmt, ...)
186 {
187 va_list ap;
188 int sz;
190 va_start(ap, fmt);
191 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
192 va_end(ap);
193 sz = strlen(pcm->error);
195 if (errno)
196 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
197 ": %s", strerror(e));
198 return -1;
199 }
201 static unsigned int pcm_format_to_alsa(enum pcm_format format)
202 {
203 switch (format) {
204 case PCM_FORMAT_S32_LE:
205 return SNDRV_PCM_FORMAT_S32_LE;
206 case PCM_FORMAT_S8:
207 return SNDRV_PCM_FORMAT_S8;
208 case PCM_FORMAT_S24_LE:
209 return SNDRV_PCM_FORMAT_S24_LE;
210 default:
211 case PCM_FORMAT_S16_LE:
212 return SNDRV_PCM_FORMAT_S16_LE;
213 };
214 }
216 unsigned int pcm_format_to_bits(enum pcm_format format)
217 {
218 switch (format) {
219 case PCM_FORMAT_S32_LE:
220 case PCM_FORMAT_S24_LE:
221 return 32;
222 default:
223 case PCM_FORMAT_S16_LE:
224 return 16;
225 };
226 }
228 unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
229 {
230 return bytes / (pcm->config.channels *
231 (pcm_format_to_bits(pcm->config.format) >> 3));
232 }
234 unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
235 {
236 return frames * pcm->config.channels *
237 (pcm_format_to_bits(pcm->config.format) >> 3);
238 }
240 static int pcm_sync_ptr(struct pcm *pcm, int flags) {
241 if (pcm->sync_ptr) {
242 pcm->sync_ptr->flags = flags;
243 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
244 return -1;
245 }
246 return 0;
247 }
249 static int pcm_hw_mmap_status(struct pcm *pcm) {
251 if (pcm->sync_ptr)
252 return 0;
254 int page_size = sysconf(_SC_PAGE_SIZE);
255 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
256 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
257 if (pcm->mmap_status == MAP_FAILED)
258 pcm->mmap_status = NULL;
259 if (!pcm->mmap_status)
260 goto mmap_error;
262 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
263 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
264 if (pcm->mmap_control == MAP_FAILED)
265 pcm->mmap_control = NULL;
266 if (!pcm->mmap_control) {
267 munmap(pcm->mmap_status, page_size);
268 pcm->mmap_status = NULL;
269 goto mmap_error;
270 }
271 if (pcm->flags & PCM_MMAP)
272 pcm->mmap_control->avail_min = pcm->config.avail_min;
273 else
274 pcm->mmap_control->avail_min = 1;
276 return 0;
278 mmap_error:
280 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
281 if (!pcm->sync_ptr)
282 return -ENOMEM;
283 pcm->mmap_status = &pcm->sync_ptr->s.status;
284 pcm->mmap_control = &pcm->sync_ptr->c.control;
285 if (pcm->flags & PCM_MMAP)
286 pcm->mmap_control->avail_min = pcm->config.avail_min;
287 else
288 pcm->mmap_control->avail_min = 1;
290 pcm_sync_ptr(pcm, 0);
292 return 0;
293 }
295 static void pcm_hw_munmap_status(struct pcm *pcm) {
296 if (pcm->sync_ptr) {
297 free(pcm->sync_ptr);
298 pcm->sync_ptr = NULL;
299 } else {
300 int page_size = sysconf(_SC_PAGE_SIZE);
301 if (pcm->mmap_status)
302 munmap(pcm->mmap_status, page_size);
303 if (pcm->mmap_control)
304 munmap(pcm->mmap_control, page_size);
305 }
306 pcm->mmap_status = NULL;
307 pcm->mmap_control = NULL;
308 }
310 static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
311 const char *src, unsigned int src_offset,
312 unsigned int frames)
313 {
314 int size_bytes = pcm_frames_to_bytes(pcm, frames);
315 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
316 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
318 /* interleaved only atm */
319 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
320 src + src_offset_bytes, size_bytes);
321 return 0;
322 }
324 static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
325 unsigned int offset, unsigned int size)
326 {
327 void *pcm_areas;
328 int commit;
329 unsigned int pcm_offset, frames, count = 0;
331 while (size > 0) {
332 frames = size;
333 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
334 pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
335 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
336 if (commit < 0) {
337 oops(pcm, commit, "failed to commit %d frames\n", frames);
338 return commit;
339 }
341 offset += commit;
342 count += commit;
343 size -= commit;
344 }
345 return count;
346 }
348 int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
349 struct timespec *tstamp)
350 {
351 int frames;
352 int rc;
353 snd_pcm_uframes_t hw_ptr;
355 if (!pcm_is_ready(pcm))
356 return -1;
358 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
359 if (rc < 0)
360 return -1;
362 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
363 (pcm->mmap_status->state != PCM_STATE_DRAINING))
364 return -1;
366 *tstamp = pcm->mmap_status->tstamp;
367 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
368 return -1;
370 hw_ptr = pcm->mmap_status->hw_ptr;
371 if (pcm->flags & PCM_IN)
372 frames = hw_ptr - pcm->mmap_control->appl_ptr;
373 else
374 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
376 if (frames < 0)
377 frames += pcm->boundary;
378 else if (frames > (int)pcm->boundary)
379 frames -= pcm->boundary;
381 *avail = (unsigned int)frames;
383 return 0;
384 }
386 int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
387 {
388 struct snd_xferi x;
390 if (pcm->flags & PCM_IN)
391 return -EINVAL;
393 x.buf = (void*)data;
394 x.frames = count / (pcm->config.channels *
395 pcm_format_to_bits(pcm->config.format) / 8);
397 for (;;) {
398 if (!pcm->running) {
399 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
400 return oops(pcm, errno, "cannot prepare channel");
401 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
402 return oops(pcm, errno, "cannot write initial data");
403 pcm->running = 1;
404 return 0;
405 }
406 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
407 pcm->running = 0;
408 if (errno == EPIPE) {
409 /* we failed to make our window -- try to restart if we are
410 * allowed to do so. Otherwise, simply allow the EPIPE error to
411 * propagate up to the app level */
412 pcm->underruns++;
413 if (pcm->flags & PCM_NORESTART)
414 return -EPIPE;
415 continue;
416 }
417 return oops(pcm, errno, "cannot write stream data");
418 }
419 return 0;
420 }
421 }
423 int pcm_read(struct pcm *pcm, void *data, unsigned int count)
424 {
425 struct snd_xferi x;
427 if (!(pcm->flags & PCM_IN))
428 return -EINVAL;
430 x.buf = data;
431 x.frames = count / (pcm->config.channels *
432 pcm_format_to_bits(pcm->config.format) / 8);
434 for (;;) {
435 if (!pcm->running) {
436 if (pcm_start(pcm) < 0) {
437 fprintf(stderr, "start error");
438 return -errno;
439 }
440 }
441 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
442 pcm->running = 0;
443 if (errno == EPIPE) {
444 /* we failed to make our window -- try to restart */
445 pcm->underruns++;
446 continue;
447 }
448 return oops(pcm, errno, "cannot read stream data");
449 }
450 return 0;
451 }
452 }
454 static struct pcm bad_pcm = {
455 .fd = -1,
456 };
458 struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
459 unsigned int flags)
460 {
461 struct snd_pcm_hw_params *params;
462 char fn[256];
463 int fd;
465 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
466 flags & PCM_IN ? 'c' : 'p');
468 fd = open(fn, O_RDWR);
469 if (fd < 0) {
470 fprintf(stderr, "cannot open device '%s'\n", fn);
471 goto err_open;
472 }
474 params = calloc(1, sizeof(struct snd_pcm_hw_params));
475 if (!params)
476 goto err_calloc;
478 param_init(params);
479 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
480 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
481 goto err_hw_refine;
482 }
484 close(fd);
486 return (struct pcm_params *)params;
488 err_hw_refine:
489 free(params);
490 err_calloc:
491 close(fd);
492 err_open:
493 return NULL;
494 }
496 void pcm_params_free(struct pcm_params *pcm_params)
497 {
498 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
500 if (params)
501 free(params);
502 }
504 static int pcm_param_to_alsa(enum pcm_param param)
505 {
506 switch (param) {
507 case PCM_PARAM_SAMPLE_BITS:
508 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
509 break;
510 case PCM_PARAM_FRAME_BITS:
511 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
512 break;
513 case PCM_PARAM_CHANNELS:
514 return SNDRV_PCM_HW_PARAM_CHANNELS;
515 break;
516 case PCM_PARAM_RATE:
517 return SNDRV_PCM_HW_PARAM_RATE;
518 break;
519 case PCM_PARAM_PERIOD_TIME:
520 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
521 break;
522 case PCM_PARAM_PERIOD_SIZE:
523 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
524 break;
525 case PCM_PARAM_PERIOD_BYTES:
526 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
527 break;
528 case PCM_PARAM_PERIODS:
529 return SNDRV_PCM_HW_PARAM_PERIODS;
530 break;
531 case PCM_PARAM_BUFFER_TIME:
532 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
533 break;
534 case PCM_PARAM_BUFFER_SIZE:
535 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
536 break;
537 case PCM_PARAM_BUFFER_BYTES:
538 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
539 break;
540 case PCM_PARAM_TICK_TIME:
541 return SNDRV_PCM_HW_PARAM_TICK_TIME;
542 break;
544 default:
545 return -1;
546 }
547 }
549 unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
550 enum pcm_param param)
551 {
552 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
553 int p;
555 if (!params)
556 return 0;
558 p = pcm_param_to_alsa(param);
559 if (p < 0)
560 return 0;
562 return param_get_min(params, p);
563 }
565 unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
566 enum pcm_param param)
567 {
568 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
569 int p;
571 if (!params)
572 return 0;
574 p = pcm_param_to_alsa(param);
575 if (p < 0)
576 return 0;
578 return param_get_max(params, p);
579 }
581 int pcm_close(struct pcm *pcm)
582 {
583 if (pcm == &bad_pcm)
584 return 0;
586 pcm_hw_munmap_status(pcm);
588 if (pcm->flags & PCM_MMAP) {
589 pcm_stop(pcm);
590 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
591 }
593 if (pcm->fd >= 0)
594 close(pcm->fd);
595 pcm->running = 0;
596 pcm->buffer_size = 0;
597 pcm->fd = -1;
598 free(pcm);
599 return 0;
600 }
602 struct pcm *pcm_open(unsigned int card, unsigned int device,
603 unsigned int flags, struct pcm_config *config)
604 {
605 struct pcm *pcm;
606 struct snd_pcm_info info;
607 struct snd_pcm_hw_params params;
608 struct snd_pcm_sw_params sparams;
609 char fn[256];
610 int rc;
612 pcm = calloc(1, sizeof(struct pcm));
613 if (!pcm || !config)
614 return &bad_pcm; /* TODO: could support default config here */
616 pcm->config = *config;
618 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
619 flags & PCM_IN ? 'c' : 'p');
621 pcm->flags = flags;
622 pcm->fd = open(fn, O_RDWR);
623 if (pcm->fd < 0) {
624 oops(pcm, errno, "cannot open device '%s'", fn);
625 return pcm;
626 }
628 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
629 oops(pcm, errno, "cannot get info");
630 goto fail_close;
631 }
633 param_init(¶ms);
634 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT,
635 pcm_format_to_alsa(config->format));
636 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT,
637 SNDRV_PCM_SUBFORMAT_STD);
638 param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
639 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
640 pcm_format_to_bits(config->format));
641 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS,
642 pcm_format_to_bits(config->format) * config->channels);
643 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS,
644 config->channels);
645 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
646 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate);
648 if (flags & PCM_NOIRQ) {
650 if (!(flags & PCM_MMAP)) {
651 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
652 goto fail;
653 }
655 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
656 pcm->noirq_frames_per_msec = config->rate / 1000;
657 }
659 if (flags & PCM_MMAP)
660 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
661 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
662 else
663 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
664 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
666 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) {
667 oops(pcm, errno, "cannot set hw params");
668 goto fail_close;
669 }
671 /* get our refined hw_params */
672 config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
673 config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS);
674 pcm->buffer_size = config->period_count * config->period_size;
676 if (flags & PCM_MMAP) {
677 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
678 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
679 if (pcm->mmap_buffer == MAP_FAILED) {
680 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
681 pcm_frames_to_bytes(pcm, pcm->buffer_size));
682 goto fail_close;
683 }
684 }
687 memset(&sparams, 0, sizeof(sparams));
688 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
689 sparams.period_step = 1;
691 if (!config->start_threshold) {
692 if (pcm->flags & PCM_IN)
693 pcm->config.start_threshold = sparams.start_threshold = 1;
694 else
695 pcm->config.start_threshold = sparams.start_threshold =
696 config->period_count * config->period_size / 2;
697 } else
698 sparams.start_threshold = config->start_threshold;
700 /* pick a high stop threshold - todo: does this need further tuning */
701 if (!config->stop_threshold) {
702 if (pcm->flags & PCM_IN)
703 pcm->config.stop_threshold = sparams.stop_threshold =
704 config->period_count * config->period_size * 10;
705 else
706 pcm->config.stop_threshold = sparams.stop_threshold =
707 config->period_count * config->period_size;
708 }
709 else
710 sparams.stop_threshold = config->stop_threshold;
712 if (!pcm->config.avail_min) {
713 if (pcm->flags & PCM_MMAP)
714 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
715 else
716 pcm->config.avail_min = sparams.avail_min = 1;
717 } else
718 sparams.avail_min = config->avail_min;
720 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
721 sparams.silence_size = 0;
722 sparams.silence_threshold = config->silence_threshold;
723 pcm->boundary = sparams.boundary = pcm->buffer_size;
725 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
726 pcm->boundary *= 2;
728 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
729 oops(pcm, errno, "cannot set sw params");
730 goto fail;
731 }
733 rc = pcm_hw_mmap_status(pcm);
734 if (rc < 0) {
735 oops(pcm, rc, "mmap status failed");
736 goto fail;
737 }
739 #ifdef SNDRV_PCM_IOCTL_TTSTAMP
740 if (pcm->flags & PCM_MONOTONIC) {
741 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
742 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
743 if (rc < 0) {
744 oops(pcm, rc, "cannot set timestamp type");
745 goto fail;
746 }
747 }
748 #endif
750 pcm->underruns = 0;
751 return pcm;
753 fail:
754 if (flags & PCM_MMAP)
755 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
756 fail_close:
757 close(pcm->fd);
758 pcm->fd = -1;
759 return pcm;
760 }
762 int pcm_is_ready(struct pcm *pcm)
763 {
764 return pcm->fd >= 0;
765 }
767 int pcm_start(struct pcm *pcm)
768 {
769 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
770 return oops(pcm, errno, "cannot prepare channel");
772 if (pcm->flags & PCM_MMAP)
773 pcm_sync_ptr(pcm, 0);
775 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
776 return oops(pcm, errno, "cannot start channel");
778 pcm->running = 1;
779 return 0;
780 }
782 int pcm_stop(struct pcm *pcm)
783 {
784 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
785 return oops(pcm, errno, "cannot stop channel");
787 pcm->running = 0;
788 return 0;
789 }
791 static inline int pcm_mmap_playback_avail(struct pcm *pcm)
792 {
793 int avail;
795 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
797 if (avail < 0)
798 avail += pcm->boundary;
799 else if (avail > (int)pcm->boundary)
800 avail -= pcm->boundary;
802 return avail;
803 }
805 static inline int pcm_mmap_capture_avail(struct pcm *pcm)
806 {
807 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
808 if (avail < 0)
809 avail += pcm->boundary;
810 return avail;
811 }
813 static inline int pcm_mmap_avail(struct pcm *pcm)
814 {
815 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
816 if (pcm->flags & PCM_IN)
817 return pcm_mmap_capture_avail(pcm);
818 else
819 return pcm_mmap_playback_avail(pcm);
820 }
822 static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
823 {
824 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
825 appl_ptr += frames;
827 /* check for boundary wrap */
828 if (appl_ptr > pcm->boundary)
829 appl_ptr -= pcm->boundary;
830 pcm->mmap_control->appl_ptr = appl_ptr;
831 }
833 int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
834 unsigned int *frames)
835 {
836 unsigned int continuous, copy_frames, avail;
838 /* return the mmap buffer */
839 *areas = pcm->mmap_buffer;
841 /* and the application offset in frames */
842 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
844 avail = pcm_mmap_avail(pcm);
845 if (avail > pcm->buffer_size)
846 avail = pcm->buffer_size;
847 continuous = pcm->buffer_size - *offset;
849 /* we can only copy frames if the are availabale and continuos */
850 copy_frames = *frames;
851 if (copy_frames > avail)
852 copy_frames = avail;
853 if (copy_frames > continuous)
854 copy_frames = continuous;
855 *frames = copy_frames;
857 return 0;
858 }
860 int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
861 {
862 /* update the application pointer in userspace and kernel */
863 pcm_mmap_appl_forward(pcm, frames);
864 pcm_sync_ptr(pcm, 0);
866 return frames;
867 }
869 int pcm_avail_update(struct pcm *pcm)
870 {
871 pcm_sync_ptr(pcm, 0);
872 return pcm_mmap_avail(pcm);
873 }
875 int pcm_state(struct pcm *pcm)
876 {
877 int err = pcm_sync_ptr(pcm, 0);
878 if (err < 0)
879 return err;
881 return pcm->mmap_status->state;
882 }
884 int pcm_set_avail_min(struct pcm *pcm, int avail_min)
885 {
886 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
887 return -ENOSYS;
889 pcm->config.avail_min = avail_min;
890 return 0;
891 }
893 int pcm_wait(struct pcm *pcm, int timeout)
894 {
895 struct pollfd pfd;
896 int err;
898 pfd.fd = pcm->fd;
899 pfd.events = POLLOUT | POLLERR | POLLNVAL;
901 do {
902 /* let's wait for avail or timeout */
903 err = poll(&pfd, 1, timeout);
904 if (err < 0)
905 return -errno;
907 /* timeout ? */
908 if (err == 0)
909 return 0;
911 /* have we been interrupted ? */
912 if (errno == -EINTR)
913 continue;
915 /* check for any errors */
916 if (pfd.revents & (POLLERR | POLLNVAL)) {
917 switch (pcm_state(pcm)) {
918 case PCM_STATE_XRUN:
919 return -EPIPE;
920 case PCM_STATE_SUSPENDED:
921 return -ESTRPIPE;
922 case PCM_STATE_DISCONNECTED:
923 return -ENODEV;
924 default:
925 return -EIO;
926 }
927 }
928 /* poll again if fd not ready for IO */
929 } while (!(pfd.revents & (POLLIN | POLLOUT)));
931 return 1;
932 }
934 int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
935 {
936 int err = 0, frames, avail;
937 unsigned int offset = 0, count;
939 if (bytes == 0)
940 return 0;
942 count = pcm_bytes_to_frames(pcm, bytes);
944 while (count > 0) {
946 /* get the available space for writing new frames */
947 avail = pcm_avail_update(pcm);
948 if (avail < 0) {
949 fprintf(stderr, "cannot determine available mmap frames");
950 return err;
951 }
953 /* start the audio if we reach the threshold */
954 if (!pcm->running &&
955 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
956 if (pcm_start(pcm) < 0) {
957 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
958 (unsigned int)pcm->mmap_status->hw_ptr,
959 (unsigned int)pcm->mmap_control->appl_ptr,
960 avail);
961 return -errno;
962 }
963 pcm->wait_for_avail_min = 0;
964 }
966 /* sleep until we have space to write new frames */
967 if (pcm->running) {
968 /* enable waiting for avail_min threshold when less frames than we have to write
969 * are available. */
970 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
971 pcm->wait_for_avail_min = 1;
973 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
974 int time = -1;
976 /* disable waiting for avail_min threshold to allow small amounts of data to be
977 * written without waiting as long as there is enough room in buffer. */
978 pcm->wait_for_avail_min = 0;
980 if (pcm->flags & PCM_NOIRQ)
981 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
983 err = pcm_wait(pcm, time);
984 if (err < 0) {
985 pcm->running = 0;
986 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
987 (unsigned int)pcm->mmap_status->hw_ptr,
988 (unsigned int)pcm->mmap_control->appl_ptr,
989 avail);
990 pcm->mmap_control->appl_ptr = 0;
991 return err;
992 }
993 continue;
994 }
995 }
997 frames = count;
998 if (frames > avail)
999 frames = avail;
1001 if (!frames)
1002 break;
1004 /* copy frames from buffer */
1005 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
1006 if (frames < 0) {
1007 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1008 (unsigned int)pcm->mmap_status->hw_ptr,
1009 (unsigned int)pcm->mmap_control->appl_ptr,
1010 avail);
1011 return frames;
1012 }
1014 offset += frames;
1015 count -= frames;
1016 }
1018 return 0;
1019 }