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_ACCESS:
518 return SNDRV_PCM_HW_PARAM_ACCESS;
519 case PCM_PARAM_FORMAT:
520 return SNDRV_PCM_HW_PARAM_FORMAT;
521 case PCM_PARAM_SUBFORMAT:
522 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
523 case PCM_PARAM_SAMPLE_BITS:
524 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
525 break;
526 case PCM_PARAM_FRAME_BITS:
527 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
528 break;
529 case PCM_PARAM_CHANNELS:
530 return SNDRV_PCM_HW_PARAM_CHANNELS;
531 break;
532 case PCM_PARAM_RATE:
533 return SNDRV_PCM_HW_PARAM_RATE;
534 break;
535 case PCM_PARAM_PERIOD_TIME:
536 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
537 break;
538 case PCM_PARAM_PERIOD_SIZE:
539 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
540 break;
541 case PCM_PARAM_PERIOD_BYTES:
542 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
543 break;
544 case PCM_PARAM_PERIODS:
545 return SNDRV_PCM_HW_PARAM_PERIODS;
546 break;
547 case PCM_PARAM_BUFFER_TIME:
548 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
549 break;
550 case PCM_PARAM_BUFFER_SIZE:
551 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
552 break;
553 case PCM_PARAM_BUFFER_BYTES:
554 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
555 break;
556 case PCM_PARAM_TICK_TIME:
557 return SNDRV_PCM_HW_PARAM_TICK_TIME;
558 break;
560 default:
561 return -1;
562 }
563 }
565 struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
566 enum pcm_param param)
567 {
568 int p;
569 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
570 if (params == NULL) {
571 return NULL;
572 }
574 p = pcm_param_to_alsa(param);
575 if (p < 0 || !param_is_mask(p)) {
576 return NULL;
577 }
579 return (struct pcm_mask *)param_to_mask(params, p);
580 }
582 unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
583 enum pcm_param param)
584 {
585 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
586 int p;
588 if (!params)
589 return 0;
591 p = pcm_param_to_alsa(param);
592 if (p < 0)
593 return 0;
595 return param_get_min(params, p);
596 }
598 unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
599 enum pcm_param param)
600 {
601 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
602 int p;
604 if (!params)
605 return 0;
607 p = pcm_param_to_alsa(param);
608 if (p < 0)
609 return 0;
611 return param_get_max(params, p);
612 }
614 int pcm_close(struct pcm *pcm)
615 {
616 if (pcm == &bad_pcm)
617 return 0;
619 pcm_hw_munmap_status(pcm);
621 if (pcm->flags & PCM_MMAP) {
622 pcm_stop(pcm);
623 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
624 }
626 if (pcm->fd >= 0)
627 close(pcm->fd);
628 pcm->running = 0;
629 pcm->buffer_size = 0;
630 pcm->fd = -1;
631 free(pcm);
632 return 0;
633 }
635 struct pcm *pcm_open(unsigned int card, unsigned int device,
636 unsigned int flags, struct pcm_config *config)
637 {
638 struct pcm *pcm;
639 struct snd_pcm_info info;
640 struct snd_pcm_hw_params params;
641 struct snd_pcm_sw_params sparams;
642 char fn[256];
643 int rc;
645 pcm = calloc(1, sizeof(struct pcm));
646 if (!pcm || !config)
647 return &bad_pcm; /* TODO: could support default config here */
649 pcm->config = *config;
651 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
652 flags & PCM_IN ? 'c' : 'p');
654 pcm->flags = flags;
655 pcm->fd = open(fn, O_RDWR);
656 if (pcm->fd < 0) {
657 oops(pcm, errno, "cannot open device '%s'", fn);
658 return pcm;
659 }
661 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
662 oops(pcm, errno, "cannot get info");
663 goto fail_close;
664 }
666 param_init(¶ms);
667 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT,
668 pcm_format_to_alsa(config->format));
669 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_SUBFORMAT,
670 SNDRV_PCM_SUBFORMAT_STD);
671 param_set_min(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
672 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
673 pcm_format_to_bits(config->format));
674 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_FRAME_BITS,
675 pcm_format_to_bits(config->format) * config->channels);
676 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS,
677 config->channels);
678 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
679 param_set_int(¶ms, SNDRV_PCM_HW_PARAM_RATE, config->rate);
681 if (flags & PCM_NOIRQ) {
683 if (!(flags & PCM_MMAP)) {
684 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
685 goto fail;
686 }
688 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
689 pcm->noirq_frames_per_msec = config->rate / 1000;
690 }
692 if (flags & PCM_MMAP)
693 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
694 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
695 else
696 param_set_mask(¶ms, SNDRV_PCM_HW_PARAM_ACCESS,
697 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
699 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, ¶ms)) {
700 oops(pcm, errno, "cannot set hw params");
701 goto fail_close;
702 }
704 /* get our refined hw_params */
705 config->period_size = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
706 config->period_count = param_get_int(¶ms, SNDRV_PCM_HW_PARAM_PERIODS);
707 pcm->buffer_size = config->period_count * config->period_size;
709 if (flags & PCM_MMAP) {
710 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
711 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
712 if (pcm->mmap_buffer == MAP_FAILED) {
713 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
714 pcm_frames_to_bytes(pcm, pcm->buffer_size));
715 goto fail_close;
716 }
717 }
720 memset(&sparams, 0, sizeof(sparams));
721 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
722 sparams.period_step = 1;
724 if (!config->start_threshold) {
725 if (pcm->flags & PCM_IN)
726 pcm->config.start_threshold = sparams.start_threshold = 1;
727 else
728 pcm->config.start_threshold = sparams.start_threshold =
729 config->period_count * config->period_size / 2;
730 } else
731 sparams.start_threshold = config->start_threshold;
733 /* pick a high stop threshold - todo: does this need further tuning */
734 if (!config->stop_threshold) {
735 if (pcm->flags & PCM_IN)
736 pcm->config.stop_threshold = sparams.stop_threshold =
737 config->period_count * config->period_size * 10;
738 else
739 pcm->config.stop_threshold = sparams.stop_threshold =
740 config->period_count * config->period_size;
741 }
742 else
743 sparams.stop_threshold = config->stop_threshold;
745 if (!pcm->config.avail_min) {
746 if (pcm->flags & PCM_MMAP)
747 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
748 else
749 pcm->config.avail_min = sparams.avail_min = 1;
750 } else
751 sparams.avail_min = config->avail_min;
753 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
754 sparams.silence_size = 0;
755 sparams.silence_threshold = config->silence_threshold;
756 pcm->boundary = sparams.boundary = pcm->buffer_size;
758 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
759 pcm->boundary *= 2;
761 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
762 oops(pcm, errno, "cannot set sw params");
763 goto fail;
764 }
766 rc = pcm_hw_mmap_status(pcm);
767 if (rc < 0) {
768 oops(pcm, rc, "mmap status failed");
769 goto fail;
770 }
772 #ifdef SNDRV_PCM_IOCTL_TTSTAMP
773 if (pcm->flags & PCM_MONOTONIC) {
774 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
775 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
776 if (rc < 0) {
777 oops(pcm, rc, "cannot set timestamp type");
778 goto fail;
779 }
780 }
781 #endif
783 pcm->underruns = 0;
784 return pcm;
786 fail:
787 if (flags & PCM_MMAP)
788 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
789 fail_close:
790 close(pcm->fd);
791 pcm->fd = -1;
792 return pcm;
793 }
795 int pcm_is_ready(struct pcm *pcm)
796 {
797 return pcm->fd >= 0;
798 }
800 int pcm_start(struct pcm *pcm)
801 {
802 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
803 return oops(pcm, errno, "cannot prepare channel");
805 if (pcm->flags & PCM_MMAP)
806 pcm_sync_ptr(pcm, 0);
808 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
809 return oops(pcm, errno, "cannot start channel");
811 pcm->running = 1;
812 return 0;
813 }
815 int pcm_stop(struct pcm *pcm)
816 {
817 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
818 return oops(pcm, errno, "cannot stop channel");
820 pcm->running = 0;
821 return 0;
822 }
824 static inline int pcm_mmap_playback_avail(struct pcm *pcm)
825 {
826 int avail;
828 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
830 if (avail < 0)
831 avail += pcm->boundary;
832 else if (avail > (int)pcm->boundary)
833 avail -= pcm->boundary;
835 return avail;
836 }
838 static inline int pcm_mmap_capture_avail(struct pcm *pcm)
839 {
840 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
841 if (avail < 0)
842 avail += pcm->boundary;
843 return avail;
844 }
846 static inline int pcm_mmap_avail(struct pcm *pcm)
847 {
848 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
849 if (pcm->flags & PCM_IN)
850 return pcm_mmap_capture_avail(pcm);
851 else
852 return pcm_mmap_playback_avail(pcm);
853 }
855 static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
856 {
857 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
858 appl_ptr += frames;
860 /* check for boundary wrap */
861 if (appl_ptr > pcm->boundary)
862 appl_ptr -= pcm->boundary;
863 pcm->mmap_control->appl_ptr = appl_ptr;
864 }
866 int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
867 unsigned int *frames)
868 {
869 unsigned int continuous, copy_frames, avail;
871 /* return the mmap buffer */
872 *areas = pcm->mmap_buffer;
874 /* and the application offset in frames */
875 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
877 avail = pcm_mmap_avail(pcm);
878 if (avail > pcm->buffer_size)
879 avail = pcm->buffer_size;
880 continuous = pcm->buffer_size - *offset;
882 /* we can only copy frames if the are availabale and continuos */
883 copy_frames = *frames;
884 if (copy_frames > avail)
885 copy_frames = avail;
886 if (copy_frames > continuous)
887 copy_frames = continuous;
888 *frames = copy_frames;
890 return 0;
891 }
893 int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
894 {
895 /* update the application pointer in userspace and kernel */
896 pcm_mmap_appl_forward(pcm, frames);
897 pcm_sync_ptr(pcm, 0);
899 return frames;
900 }
902 int pcm_avail_update(struct pcm *pcm)
903 {
904 pcm_sync_ptr(pcm, 0);
905 return pcm_mmap_avail(pcm);
906 }
908 int pcm_state(struct pcm *pcm)
909 {
910 int err = pcm_sync_ptr(pcm, 0);
911 if (err < 0)
912 return err;
914 return pcm->mmap_status->state;
915 }
917 int pcm_set_avail_min(struct pcm *pcm, int avail_min)
918 {
919 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
920 return -ENOSYS;
922 pcm->config.avail_min = avail_min;
923 return 0;
924 }
926 int pcm_wait(struct pcm *pcm, int timeout)
927 {
928 struct pollfd pfd;
929 int err;
931 pfd.fd = pcm->fd;
932 pfd.events = POLLOUT | POLLERR | POLLNVAL;
934 do {
935 /* let's wait for avail or timeout */
936 err = poll(&pfd, 1, timeout);
937 if (err < 0)
938 return -errno;
940 /* timeout ? */
941 if (err == 0)
942 return 0;
944 /* have we been interrupted ? */
945 if (errno == -EINTR)
946 continue;
948 /* check for any errors */
949 if (pfd.revents & (POLLERR | POLLNVAL)) {
950 switch (pcm_state(pcm)) {
951 case PCM_STATE_XRUN:
952 return -EPIPE;
953 case PCM_STATE_SUSPENDED:
954 return -ESTRPIPE;
955 case PCM_STATE_DISCONNECTED:
956 return -ENODEV;
957 default:
958 return -EIO;
959 }
960 }
961 /* poll again if fd not ready for IO */
962 } while (!(pfd.revents & (POLLIN | POLLOUT)));
964 return 1;
965 }
967 int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
968 {
969 int err = 0, frames, avail;
970 unsigned int offset = 0, count;
972 if (bytes == 0)
973 return 0;
975 count = pcm_bytes_to_frames(pcm, bytes);
977 while (count > 0) {
979 /* get the available space for writing new frames */
980 avail = pcm_avail_update(pcm);
981 if (avail < 0) {
982 fprintf(stderr, "cannot determine available mmap frames");
983 return err;
984 }
986 /* start the audio if we reach the threshold */
987 if (!pcm->running &&
988 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
989 if (pcm_start(pcm) < 0) {
990 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
991 (unsigned int)pcm->mmap_status->hw_ptr,
992 (unsigned int)pcm->mmap_control->appl_ptr,
993 avail);
994 return -errno;
995 }
996 pcm->wait_for_avail_min = 0;
997 }
999 /* sleep until we have space to write new frames */
1000 if (pcm->running) {
1001 /* enable waiting for avail_min threshold when less frames than we have to write
1002 * are available. */
1003 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1004 pcm->wait_for_avail_min = 1;
1006 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1007 int time = -1;
1009 /* disable waiting for avail_min threshold to allow small amounts of data to be
1010 * written without waiting as long as there is enough room in buffer. */
1011 pcm->wait_for_avail_min = 0;
1013 if (pcm->flags & PCM_NOIRQ)
1014 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1016 err = pcm_wait(pcm, time);
1017 if (err < 0) {
1018 pcm->running = 0;
1019 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1020 (unsigned int)pcm->mmap_status->hw_ptr,
1021 (unsigned int)pcm->mmap_control->appl_ptr,
1022 avail);
1023 pcm->mmap_control->appl_ptr = 0;
1024 return err;
1025 }
1026 continue;
1027 }
1028 }
1030 frames = count;
1031 if (frames > avail)
1032 frames = avail;
1034 if (!frames)
1035 break;
1037 /* copy frames from buffer */
1038 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
1039 if (frames < 0) {
1040 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1041 (unsigned int)pcm->mmap_status->hw_ptr,
1042 (unsigned int)pcm->mmap_control->appl_ptr,
1043 avail);
1044 return frames;
1045 }
1047 offset += frames;
1048 count -= frames;
1049 }
1051 return 0;
1052 }
1054 int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1055 {
1056 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1057 return -ENOSYS;
1059 return pcm_mmap_transfer(pcm, (void *)data, count);
1060 }
1062 int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1063 {
1064 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1065 return -ENOSYS;
1067 return pcm_mmap_transfer(pcm, data, count);
1068 }