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_3LE:
209 return SNDRV_PCM_FORMAT_S24_3LE;
210 case PCM_FORMAT_S24_LE:
211 return SNDRV_PCM_FORMAT_S24_LE;
212 default:
213 case PCM_FORMAT_S16_LE:
214 return SNDRV_PCM_FORMAT_S16_LE;
215 };
216 }
218 unsigned int pcm_format_to_bits(enum pcm_format format)
219 {
220 switch (format) {
221 case PCM_FORMAT_S32_LE:
222 case PCM_FORMAT_S24_LE:
223 return 32;
224 case PCM_FORMAT_S24_3LE:
225 return 24;
226 default:
227 case PCM_FORMAT_S16_LE:
228 return 16;
229 };
230 }
232 unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
233 {
234 return bytes / (pcm->config.channels *
235 (pcm_format_to_bits(pcm->config.format) >> 3));
236 }
238 unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
239 {
240 return frames * pcm->config.channels *
241 (pcm_format_to_bits(pcm->config.format) >> 3);
242 }
244 static int pcm_sync_ptr(struct pcm *pcm, int flags) {
245 if (pcm->sync_ptr) {
246 pcm->sync_ptr->flags = flags;
247 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
248 return -1;
249 }
250 return 0;
251 }
253 static int pcm_hw_mmap_status(struct pcm *pcm) {
255 if (pcm->sync_ptr)
256 return 0;
258 int page_size = sysconf(_SC_PAGE_SIZE);
259 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
260 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
261 if (pcm->mmap_status == MAP_FAILED)
262 pcm->mmap_status = NULL;
263 if (!pcm->mmap_status)
264 goto mmap_error;
266 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
267 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
268 if (pcm->mmap_control == MAP_FAILED)
269 pcm->mmap_control = NULL;
270 if (!pcm->mmap_control) {
271 munmap(pcm->mmap_status, page_size);
272 pcm->mmap_status = NULL;
273 goto mmap_error;
274 }
275 if (pcm->flags & PCM_MMAP)
276 pcm->mmap_control->avail_min = pcm->config.avail_min;
277 else
278 pcm->mmap_control->avail_min = 1;
280 return 0;
282 mmap_error:
284 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
285 if (!pcm->sync_ptr)
286 return -ENOMEM;
287 pcm->mmap_status = &pcm->sync_ptr->s.status;
288 pcm->mmap_control = &pcm->sync_ptr->c.control;
289 if (pcm->flags & PCM_MMAP)
290 pcm->mmap_control->avail_min = pcm->config.avail_min;
291 else
292 pcm->mmap_control->avail_min = 1;
294 pcm_sync_ptr(pcm, 0);
296 return 0;
297 }
299 static void pcm_hw_munmap_status(struct pcm *pcm) {
300 if (pcm->sync_ptr) {
301 free(pcm->sync_ptr);
302 pcm->sync_ptr = NULL;
303 } else {
304 int page_size = sysconf(_SC_PAGE_SIZE);
305 if (pcm->mmap_status)
306 munmap(pcm->mmap_status, page_size);
307 if (pcm->mmap_control)
308 munmap(pcm->mmap_control, page_size);
309 }
310 pcm->mmap_status = NULL;
311 pcm->mmap_control = NULL;
312 }
314 static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
315 char *buf, unsigned int src_offset,
316 unsigned int frames)
317 {
318 int size_bytes = pcm_frames_to_bytes(pcm, frames);
319 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
320 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
322 /* interleaved only atm */
323 if (pcm->flags & PCM_IN)
324 memcpy(buf + src_offset_bytes,
325 (char*)pcm->mmap_buffer + pcm_offset_bytes,
326 size_bytes);
327 else
328 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
329 buf + src_offset_bytes,
330 size_bytes);
331 return 0;
332 }
334 static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
335 unsigned int offset, unsigned int size)
336 {
337 void *pcm_areas;
338 int commit;
339 unsigned int pcm_offset, frames, count = 0;
341 while (size > 0) {
342 frames = size;
343 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
344 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
345 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
346 if (commit < 0) {
347 oops(pcm, commit, "failed to commit %d frames\n", frames);
348 return commit;
349 }
351 offset += commit;
352 count += commit;
353 size -= commit;
354 }
355 return count;
356 }
358 int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
359 struct timespec *tstamp)
360 {
361 int frames;
362 int rc;
363 snd_pcm_uframes_t hw_ptr;
365 if (!pcm_is_ready(pcm))
366 return -1;
368 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
369 if (rc < 0)
370 return -1;
372 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
373 (pcm->mmap_status->state != PCM_STATE_DRAINING))
374 return -1;
376 *tstamp = pcm->mmap_status->tstamp;
377 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
378 return -1;
380 hw_ptr = pcm->mmap_status->hw_ptr;
381 if (pcm->flags & PCM_IN)
382 frames = hw_ptr - pcm->mmap_control->appl_ptr;
383 else
384 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
386 if (frames < 0)
387 frames += pcm->boundary;
388 else if (frames > (int)pcm->boundary)
389 frames -= pcm->boundary;
391 *avail = (unsigned int)frames;
393 return 0;
394 }
396 int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
397 {
398 struct snd_xferi x;
400 if (pcm->flags & PCM_IN)
401 return -EINVAL;
403 x.buf = (void*)data;
404 x.frames = count / (pcm->config.channels *
405 pcm_format_to_bits(pcm->config.format) / 8);
407 for (;;) {
408 if (!pcm->running) {
409 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
410 return oops(pcm, errno, "cannot prepare channel");
411 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
412 return oops(pcm, errno, "cannot write initial data");
413 pcm->running = 1;
414 return 0;
415 }
416 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
417 pcm->running = 0;
418 if (errno == EPIPE) {
419 /* we failed to make our window -- try to restart if we are
420 * allowed to do so. Otherwise, simply allow the EPIPE error to
421 * propagate up to the app level */
422 pcm->underruns++;
423 if (pcm->flags & PCM_NORESTART)
424 return -EPIPE;
425 continue;
426 }
427 return oops(pcm, errno, "cannot write stream data");
428 }
429 return 0;
430 }
431 }
433 int pcm_read(struct pcm *pcm, void *data, unsigned int count)
434 {
435 struct snd_xferi x;
437 if (!(pcm->flags & PCM_IN))
438 return -EINVAL;
440 x.buf = data;
441 x.frames = count / (pcm->config.channels *
442 pcm_format_to_bits(pcm->config.format) / 8);
444 for (;;) {
445 if (!pcm->running) {
446 if (pcm_start(pcm) < 0) {
447 fprintf(stderr, "start error");
448 return -errno;
449 }
450 }
451 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
452 pcm->running = 0;
453 if (errno == EPIPE) {
454 /* we failed to make our window -- try to restart */
455 pcm->underruns++;
456 continue;
457 }
458 return oops(pcm, errno, "cannot read stream data");
459 }
460 return 0;
461 }
462 }
464 static struct pcm bad_pcm = {
465 .fd = -1,
466 };
468 struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
469 unsigned int flags)
470 {
471 struct snd_pcm_hw_params *params;
472 char fn[256];
473 int fd;
475 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
476 flags & PCM_IN ? 'c' : 'p');
478 fd = open(fn, O_RDWR);
479 if (fd < 0) {
480 fprintf(stderr, "cannot open device '%s'\n", fn);
481 goto err_open;
482 }
484 params = calloc(1, sizeof(struct snd_pcm_hw_params));
485 if (!params)
486 goto err_calloc;
488 param_init(params);
489 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
490 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
491 goto err_hw_refine;
492 }
494 close(fd);
496 return (struct pcm_params *)params;
498 err_hw_refine:
499 free(params);
500 err_calloc:
501 close(fd);
502 err_open:
503 return NULL;
504 }
506 void pcm_params_free(struct pcm_params *pcm_params)
507 {
508 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
510 if (params)
511 free(params);
512 }
514 static int pcm_param_to_alsa(enum pcm_param param)
515 {
516 switch (param) {
517 case PCM_PARAM_SAMPLE_BITS:
518 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
519 break;
520 case PCM_PARAM_FRAME_BITS:
521 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
522 break;
523 case PCM_PARAM_CHANNELS:
524 return SNDRV_PCM_HW_PARAM_CHANNELS;
525 break;
526 case PCM_PARAM_RATE:
527 return SNDRV_PCM_HW_PARAM_RATE;
528 break;
529 case PCM_PARAM_PERIOD_TIME:
530 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
531 break;
532 case PCM_PARAM_PERIOD_SIZE:
533 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
534 break;
535 case PCM_PARAM_PERIOD_BYTES:
536 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
537 break;
538 case PCM_PARAM_PERIODS:
539 return SNDRV_PCM_HW_PARAM_PERIODS;
540 break;
541 case PCM_PARAM_BUFFER_TIME:
542 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
543 break;
544 case PCM_PARAM_BUFFER_SIZE:
545 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
546 break;
547 case PCM_PARAM_BUFFER_BYTES:
548 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
549 break;
550 case PCM_PARAM_TICK_TIME:
551 return SNDRV_PCM_HW_PARAM_TICK_TIME;
552 break;
554 default:
555 return -1;
556 }
557 }
559 unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
560 enum pcm_param param)
561 {
562 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
563 int p;
565 if (!params)
566 return 0;
568 p = pcm_param_to_alsa(param);
569 if (p < 0)
570 return 0;
572 return param_get_min(params, p);
573 }
575 unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
576 enum pcm_param param)
577 {
578 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
579 int p;
581 if (!params)
582 return 0;
584 p = pcm_param_to_alsa(param);
585 if (p < 0)
586 return 0;
588 return param_get_max(params, p);
589 }
591 int pcm_close(struct pcm *pcm)
592 {
593 if (pcm == &bad_pcm)
594 return 0;
596 pcm_hw_munmap_status(pcm);
598 if (pcm->flags & PCM_MMAP) {
599 pcm_stop(pcm);
600 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
601 }
603 if (pcm->fd >= 0)
604 close(pcm->fd);
605 pcm->running = 0;
606 pcm->buffer_size = 0;
607 pcm->fd = -1;
608 free(pcm);
609 return 0;
610 }
612 struct pcm *pcm_open(unsigned int card, unsigned int device,
613 unsigned int flags, struct pcm_config *config)
614 {
615 struct pcm *pcm;
616 struct snd_pcm_info info;
617 struct snd_pcm_hw_params params;
618 struct snd_pcm_sw_params sparams;
619 char fn[256];
620 int rc;
622 pcm = calloc(1, sizeof(struct pcm));
623 if (!pcm || !config)
624 return &bad_pcm; /* TODO: could support default config here */
626 pcm->config = *config;
628 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
629 flags & PCM_IN ? 'c' : 'p');
631 pcm->flags = flags;
632 pcm->fd = open(fn, O_RDWR);
633 if (pcm->fd < 0) {
634 oops(pcm, errno, "cannot open device '%s'", fn);
635 return pcm;
636 }
638 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
639 oops(pcm, errno, "cannot get info");
640 goto fail_close;
641 }
643 param_init(¶ms);
644 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT,
645 pcm_format_to_alsa(config->format));
646 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT,
647 SNDRV_PCM_SUBFORMAT_STD);
648 param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
649 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
650 pcm_format_to_bits(config->format));
651 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS,
652 pcm_format_to_bits(config->format) * config->channels);
653 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS,
654 config->channels);
655 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
656 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate);
658 if (flags & PCM_NOIRQ) {
660 if (!(flags & PCM_MMAP)) {
661 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
662 goto fail;
663 }
665 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
666 pcm->noirq_frames_per_msec = config->rate / 1000;
667 }
669 if (flags & PCM_MMAP)
670 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
671 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
672 else
673 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
674 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
676 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) {
677 oops(pcm, errno, "cannot set hw params");
678 goto fail_close;
679 }
681 /* get our refined hw_params */
682 config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
683 config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS);
684 pcm->buffer_size = config->period_count * config->period_size;
686 if (flags & PCM_MMAP) {
687 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
688 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
689 if (pcm->mmap_buffer == MAP_FAILED) {
690 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
691 pcm_frames_to_bytes(pcm, pcm->buffer_size));
692 goto fail_close;
693 }
694 }
697 memset(&sparams, 0, sizeof(sparams));
698 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
699 sparams.period_step = 1;
701 if (!config->start_threshold) {
702 if (pcm->flags & PCM_IN)
703 pcm->config.start_threshold = sparams.start_threshold = 1;
704 else
705 pcm->config.start_threshold = sparams.start_threshold =
706 config->period_count * config->period_size / 2;
707 } else
708 sparams.start_threshold = config->start_threshold;
710 /* pick a high stop threshold - todo: does this need further tuning */
711 if (!config->stop_threshold) {
712 if (pcm->flags & PCM_IN)
713 pcm->config.stop_threshold = sparams.stop_threshold =
714 config->period_count * config->period_size * 10;
715 else
716 pcm->config.stop_threshold = sparams.stop_threshold =
717 config->period_count * config->period_size;
718 }
719 else
720 sparams.stop_threshold = config->stop_threshold;
722 if (!pcm->config.avail_min) {
723 if (pcm->flags & PCM_MMAP)
724 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
725 else
726 pcm->config.avail_min = sparams.avail_min = 1;
727 } else
728 sparams.avail_min = config->avail_min;
730 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
731 sparams.silence_size = 0;
732 sparams.silence_threshold = config->silence_threshold;
733 pcm->boundary = sparams.boundary = pcm->buffer_size;
735 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
736 pcm->boundary *= 2;
738 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
739 oops(pcm, errno, "cannot set sw params");
740 goto fail;
741 }
743 rc = pcm_hw_mmap_status(pcm);
744 if (rc < 0) {
745 oops(pcm, rc, "mmap status failed");
746 goto fail;
747 }
749 #ifdef SNDRV_PCM_IOCTL_TTSTAMP
750 if (pcm->flags & PCM_MONOTONIC) {
751 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
752 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
753 if (rc < 0) {
754 oops(pcm, rc, "cannot set timestamp type");
755 goto fail;
756 }
757 }
758 #endif
760 pcm->underruns = 0;
761 return pcm;
763 fail:
764 if (flags & PCM_MMAP)
765 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
766 fail_close:
767 close(pcm->fd);
768 pcm->fd = -1;
769 return pcm;
770 }
772 int pcm_is_ready(struct pcm *pcm)
773 {
774 return pcm->fd >= 0;
775 }
777 int pcm_start(struct pcm *pcm)
778 {
779 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
780 return oops(pcm, errno, "cannot prepare channel");
782 if (pcm->flags & PCM_MMAP)
783 pcm_sync_ptr(pcm, 0);
785 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
786 return oops(pcm, errno, "cannot start channel");
788 pcm->running = 1;
789 return 0;
790 }
792 int pcm_stop(struct pcm *pcm)
793 {
794 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
795 return oops(pcm, errno, "cannot stop channel");
797 pcm->running = 0;
798 return 0;
799 }
801 static inline int pcm_mmap_playback_avail(struct pcm *pcm)
802 {
803 int avail;
805 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
807 if (avail < 0)
808 avail += pcm->boundary;
809 else if (avail > (int)pcm->boundary)
810 avail -= pcm->boundary;
812 return avail;
813 }
815 static inline int pcm_mmap_capture_avail(struct pcm *pcm)
816 {
817 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
818 if (avail < 0)
819 avail += pcm->boundary;
820 return avail;
821 }
823 static inline int pcm_mmap_avail(struct pcm *pcm)
824 {
825 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
826 if (pcm->flags & PCM_IN)
827 return pcm_mmap_capture_avail(pcm);
828 else
829 return pcm_mmap_playback_avail(pcm);
830 }
832 static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
833 {
834 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
835 appl_ptr += frames;
837 /* check for boundary wrap */
838 if (appl_ptr > pcm->boundary)
839 appl_ptr -= pcm->boundary;
840 pcm->mmap_control->appl_ptr = appl_ptr;
841 }
843 int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
844 unsigned int *frames)
845 {
846 unsigned int continuous, copy_frames, avail;
848 /* return the mmap buffer */
849 *areas = pcm->mmap_buffer;
851 /* and the application offset in frames */
852 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
854 avail = pcm_mmap_avail(pcm);
855 if (avail > pcm->buffer_size)
856 avail = pcm->buffer_size;
857 continuous = pcm->buffer_size - *offset;
859 /* we can only copy frames if the are availabale and continuos */
860 copy_frames = *frames;
861 if (copy_frames > avail)
862 copy_frames = avail;
863 if (copy_frames > continuous)
864 copy_frames = continuous;
865 *frames = copy_frames;
867 return 0;
868 }
870 int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
871 {
872 /* update the application pointer in userspace and kernel */
873 pcm_mmap_appl_forward(pcm, frames);
874 pcm_sync_ptr(pcm, 0);
876 return frames;
877 }
879 int pcm_avail_update(struct pcm *pcm)
880 {
881 pcm_sync_ptr(pcm, 0);
882 return pcm_mmap_avail(pcm);
883 }
885 int pcm_state(struct pcm *pcm)
886 {
887 int err = pcm_sync_ptr(pcm, 0);
888 if (err < 0)
889 return err;
891 return pcm->mmap_status->state;
892 }
894 int pcm_set_avail_min(struct pcm *pcm, int avail_min)
895 {
896 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
897 return -ENOSYS;
899 pcm->config.avail_min = avail_min;
900 return 0;
901 }
903 int pcm_wait(struct pcm *pcm, int timeout)
904 {
905 struct pollfd pfd;
906 int err;
908 pfd.fd = pcm->fd;
909 pfd.events = POLLOUT | POLLERR | POLLNVAL;
911 do {
912 /* let's wait for avail or timeout */
913 err = poll(&pfd, 1, timeout);
914 if (err < 0)
915 return -errno;
917 /* timeout ? */
918 if (err == 0)
919 return 0;
921 /* have we been interrupted ? */
922 if (errno == -EINTR)
923 continue;
925 /* check for any errors */
926 if (pfd.revents & (POLLERR | POLLNVAL)) {
927 switch (pcm_state(pcm)) {
928 case PCM_STATE_XRUN:
929 return -EPIPE;
930 case PCM_STATE_SUSPENDED:
931 return -ESTRPIPE;
932 case PCM_STATE_DISCONNECTED:
933 return -ENODEV;
934 default:
935 return -EIO;
936 }
937 }
938 /* poll again if fd not ready for IO */
939 } while (!(pfd.revents & (POLLIN | POLLOUT)));
941 return 1;
942 }
944 int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
945 {
946 int err = 0, frames, avail;
947 unsigned int offset = 0, count;
949 if (bytes == 0)
950 return 0;
952 count = pcm_bytes_to_frames(pcm, bytes);
954 while (count > 0) {
956 /* get the available space for writing new frames */
957 avail = pcm_avail_update(pcm);
958 if (avail < 0) {
959 fprintf(stderr, "cannot determine available mmap frames");
960 return err;
961 }
963 /* start the audio if we reach the threshold */
964 if (!pcm->running &&
965 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
966 if (pcm_start(pcm) < 0) {
967 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
968 (unsigned int)pcm->mmap_status->hw_ptr,
969 (unsigned int)pcm->mmap_control->appl_ptr,
970 avail);
971 return -errno;
972 }
973 pcm->wait_for_avail_min = 0;
974 }
976 /* sleep until we have space to write new frames */
977 if (pcm->running) {
978 /* enable waiting for avail_min threshold when less frames than we have to write
979 * are available. */
980 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
981 pcm->wait_for_avail_min = 1;
983 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
984 int time = -1;
986 /* disable waiting for avail_min threshold to allow small amounts of data to be
987 * written without waiting as long as there is enough room in buffer. */
988 pcm->wait_for_avail_min = 0;
990 if (pcm->flags & PCM_NOIRQ)
991 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
993 err = pcm_wait(pcm, time);
994 if (err < 0) {
995 pcm->running = 0;
996 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
997 (unsigned int)pcm->mmap_status->hw_ptr,
998 (unsigned int)pcm->mmap_control->appl_ptr,
999 avail);
1000 pcm->mmap_control->appl_ptr = 0;
1001 return err;
1002 }
1003 continue;
1004 }
1005 }
1007 frames = count;
1008 if (frames > avail)
1009 frames = avail;
1011 if (!frames)
1012 break;
1014 /* copy frames from buffer */
1015 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
1016 if (frames < 0) {
1017 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1018 (unsigned int)pcm->mmap_status->hw_ptr,
1019 (unsigned int)pcm->mmap_control->appl_ptr,
1020 avail);
1021 return frames;
1022 }
1024 offset += frames;
1025 count -= frames;
1026 }
1028 return 0;
1029 }
1031 int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1032 {
1033 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1034 return -ENOSYS;
1036 return pcm_mmap_transfer(pcm, (void *)data, count);
1037 }
1039 int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1040 {
1041 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1042 return -ENOSYS;
1044 return pcm_mmap_transfer(pcm, data, count);
1045 }