blob: 3006d6b1d4945e5f2e13b8687f4e4cc2cb80ef18 [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -07001/* 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>
Simon Wilsone9942c82011-10-13 13:57:25 -070036#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Simon Wilsone9942c82011-10-13 13:57:25 -070041#include <limits.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070042
43#include <linux/ioctl.h>
44#define __force
45#define __bitwise
46#define __user
47#include <sound/asound.h>
48
49#include <tinyalsa/asoundlib.h>
50
51#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Simon Wilsone9942c82011-10-13 13:57:25 -070052#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilsonedff7082011-06-06 15:33:34 -070053
54static 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}
59
60static 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}
65
66static 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}
70
71static 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}
75
76static 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}
87
88static 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}
95
Simon Wilson42fc2d32012-12-03 11:18:57 -080096static 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}
104
105static 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}
113
Simon Wilsonedff7082011-06-06 15:33:34 -0700114static 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}
123
Simon Wilsone9942c82011-10-13 13:57:25 -0700124static 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}
133
Simon Wilsonedff7082011-06-06 15:33:34 -0700134static void param_init(struct snd_pcm_hw_params *p)
135{
136 int n;
137
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 }
Simon Wilson42fc2d32012-12-03 11:18:57 -0800151 p->rmask = ~0U;
152 p->cmask = 0;
153 p->info = ~0U;
Simon Wilsonedff7082011-06-06 15:33:34 -0700154}
155
156#define PCM_ERROR_MAX 128
157
158struct pcm {
159 int fd;
160 unsigned int flags;
161 int running:1;
162 int underruns;
163 unsigned int buffer_size;
Simon Wilsone9942c82011-10-13 13:57:25 -0700164 unsigned int boundary;
Simon Wilsonedff7082011-06-06 15:33:34 -0700165 char error[PCM_ERROR_MAX];
166 struct pcm_config config;
Simon Wilsondd88f132011-07-25 10:58:30 -0700167 struct snd_pcm_mmap_status *mmap_status;
168 struct snd_pcm_mmap_control *mmap_control;
169 struct snd_pcm_sync_ptr *sync_ptr;
Simon Wilsone9942c82011-10-13 13:57:25 -0700170 void *mmap_buffer;
171 unsigned int noirq_frames_per_msec;
Eric Laurent73b9c672011-10-14 11:12:24 -0700172 int wait_for_avail_min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700173};
174
175unsigned int pcm_get_buffer_size(struct pcm *pcm)
176{
177 return pcm->buffer_size;
178}
179
180const char* pcm_get_error(struct pcm *pcm)
181{
182 return pcm->error;
183}
184
185static int oops(struct pcm *pcm, int e, const char *fmt, ...)
186{
187 va_list ap;
188 int sz;
189
190 va_start(ap, fmt);
191 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
192 va_end(ap);
193 sz = strlen(pcm->error);
194
195 if (errno)
196 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
197 ": %s", strerror(e));
198 return -1;
199}
200
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700201static 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;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800206 case PCM_FORMAT_S8:
207 return SNDRV_PCM_FORMAT_S8;
208 case PCM_FORMAT_S24_LE:
209 return SNDRV_PCM_FORMAT_S24_LE;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700210 default:
211 case PCM_FORMAT_S16_LE:
212 return SNDRV_PCM_FORMAT_S16_LE;
213 };
214}
215
Simon Wilson36ea2d82013-07-17 11:10:45 -0700216unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700217{
218 switch (format) {
219 case PCM_FORMAT_S32_LE:
Simon Wilson36ea2d82013-07-17 11:10:45 -0700220 case PCM_FORMAT_S24_LE:
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700221 return 32;
222 default:
223 case PCM_FORMAT_S16_LE:
224 return 16;
225 };
226}
227
Simon Wilsone9942c82011-10-13 13:57:25 -0700228unsigned 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}
233
234unsigned 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}
239
Simon Wilsondd88f132011-07-25 10:58:30 -0700240static 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}
248
249static int pcm_hw_mmap_status(struct pcm *pcm) {
250
251 if (pcm->sync_ptr)
252 return 0;
253
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;
261
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 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700271 if (pcm->flags & PCM_MMAP)
272 pcm->mmap_control->avail_min = pcm->config.avail_min;
273 else
274 pcm->mmap_control->avail_min = 1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700275
276 return 0;
277
278mmap_error:
279
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;
Eric Laurent73b9c672011-10-14 11:12:24 -0700285 if (pcm->flags & PCM_MMAP)
286 pcm->mmap_control->avail_min = pcm->config.avail_min;
287 else
288 pcm->mmap_control->avail_min = 1;
289
Simon Wilsondd88f132011-07-25 10:58:30 -0700290 pcm_sync_ptr(pcm, 0);
291
292 return 0;
293}
294
295static 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}
309
Simon Wilsone9942c82011-10-13 13:57:25 -0700310static 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);
317
318 /* interleaved only atm */
319 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
320 src + src_offset_bytes, size_bytes);
321 return 0;
322}
323
Simon Wilsondaa83292012-02-28 15:26:02 -0800324static int pcm_mmap_write_areas(struct pcm *pcm, const char *src,
Simon Wilsone9942c82011-10-13 13:57:25 -0700325 unsigned int offset, unsigned int size)
326{
327 void *pcm_areas;
328 int commit;
329 unsigned int pcm_offset, frames, count = 0;
330
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 }
340
341 offset += commit;
342 count += commit;
343 size -= commit;
344 }
345 return count;
346}
347
Simon Wilsondd88f132011-07-25 10:58:30 -0700348int 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;
354
355 if (!pcm_is_ready(pcm))
356 return -1;
357
358 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
359 if (rc < 0)
360 return -1;
361
Simon Wilson8dd366f2011-11-17 13:16:52 -0800362 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
363 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Simon Wilson5aed71d2011-11-16 14:45:38 -0800364 return -1;
365
Simon Wilsondd88f132011-07-25 10:58:30 -0700366 *tstamp = pcm->mmap_status->tstamp;
367 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
368 return -1;
369
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;
375
376 if (frames < 0)
Eric Laurent73b9c672011-10-14 11:12:24 -0700377 frames += pcm->boundary;
378 else if (frames > (int)pcm->boundary)
379 frames -= pcm->boundary;
Simon Wilsondd88f132011-07-25 10:58:30 -0700380
381 *avail = (unsigned int)frames;
382
383 return 0;
384}
385
Simon Wilsondaa83292012-02-28 15:26:02 -0800386int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700387{
388 struct snd_xferi x;
389
390 if (pcm->flags & PCM_IN)
391 return -EINVAL;
392
Simon Wilsondaa83292012-02-28 15:26:02 -0800393 x.buf = (void*)data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700394 x.frames = count / (pcm->config.channels *
395 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700396
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) {
John Grossman673253a2012-04-03 16:37:38 -0700409 /* 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 */
Simon Wilsonedff7082011-06-06 15:33:34 -0700412 pcm->underruns++;
John Grossman673253a2012-04-03 16:37:38 -0700413 if (pcm->flags & PCM_NORESTART)
414 return -EPIPE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700415 continue;
416 }
417 return oops(pcm, errno, "cannot write stream data");
418 }
419 return 0;
420 }
421}
422
423int pcm_read(struct pcm *pcm, void *data, unsigned int count)
424{
425 struct snd_xferi x;
426
427 if (!(pcm->flags & PCM_IN))
428 return -EINVAL;
429
430 x.buf = data;
Simon Wilson83b1d6d2011-06-15 17:20:55 -0700431 x.frames = count / (pcm->config.channels *
432 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilsonedff7082011-06-06 15:33:34 -0700433
434 for (;;) {
435 if (!pcm->running) {
Simon Wilson85dc38f2012-05-15 17:37:19 -0700436 if (pcm_start(pcm) < 0) {
437 fprintf(stderr, "start error");
438 return -errno;
439 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700440 }
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}
453
454static struct pcm bad_pcm = {
455 .fd = -1,
456};
457
Simon Wilson42fc2d32012-12-03 11:18:57 -0800458struct 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;
464
465 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
466 flags & PCM_IN ? 'c' : 'p');
467
468 fd = open(fn, O_RDWR);
469 if (fd < 0) {
470 fprintf(stderr, "cannot open device '%s'\n", fn);
471 goto err_open;
472 }
473
474 params = calloc(1, sizeof(struct snd_pcm_hw_params));
475 if (!params)
476 goto err_calloc;
477
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 }
483
484 close(fd);
485
486 return (struct pcm_params *)params;
487
488err_hw_refine:
489 free(params);
490err_calloc:
491 close(fd);
492err_open:
493 return NULL;
494}
495
496void pcm_params_free(struct pcm_params *pcm_params)
497{
498 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
499
500 if (params)
501 free(params);
502}
503
504static 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;
543
544 default:
545 return -1;
546 }
547}
548
549unsigned 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;
554
555 if (!params)
556 return 0;
557
558 p = pcm_param_to_alsa(param);
559 if (p < 0)
560 return 0;
561
562 return param_get_min(params, p);
563}
564
565unsigned 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;
570
571 if (!params)
572 return 0;
573
574 p = pcm_param_to_alsa(param);
575 if (p < 0)
576 return 0;
577
578 return param_get_max(params, p);
579}
580
Simon Wilsonedff7082011-06-06 15:33:34 -0700581int pcm_close(struct pcm *pcm)
582{
583 if (pcm == &bad_pcm)
584 return 0;
585
Simon Wilsondd88f132011-07-25 10:58:30 -0700586 pcm_hw_munmap_status(pcm);
587
Simon Wilsone9942c82011-10-13 13:57:25 -0700588 if (pcm->flags & PCM_MMAP) {
589 pcm_stop(pcm);
590 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
591 }
592
Simon Wilsonedff7082011-06-06 15:33:34 -0700593 if (pcm->fd >= 0)
594 close(pcm->fd);
595 pcm->running = 0;
596 pcm->buffer_size = 0;
597 pcm->fd = -1;
Simon Wilsondd88f132011-07-25 10:58:30 -0700598 free(pcm);
Simon Wilsonedff7082011-06-06 15:33:34 -0700599 return 0;
600}
601
Simon Wilsonedff7082011-06-06 15:33:34 -0700602struct 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];
Simon Wilsondd88f132011-07-25 10:58:30 -0700610 int rc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700611
612 pcm = calloc(1, sizeof(struct pcm));
613 if (!pcm || !config)
614 return &bad_pcm; /* TODO: could support default config here */
615
616 pcm->config = *config;
617
618 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
619 flags & PCM_IN ? 'c' : 'p');
620
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 }
627
628 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
629 oops(pcm, errno, "cannot get info");
Simon Wilsone9942c82011-10-13 13:57:25 -0700630 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700631 }
632
633 param_init(&params);
Simon Wilsonedff7082011-06-06 15:33:34 -0700634 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
635 pcm_format_to_alsa(config->format));
636 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
637 SNDRV_PCM_SUBFORMAT_STD);
638 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
639 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
640 pcm_format_to_bits(config->format));
641 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
642 pcm_format_to_bits(config->format) * config->channels);
643 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
644 config->channels);
645 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
646 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
647
Simon Wilsone9942c82011-10-13 13:57:25 -0700648 if (flags & PCM_NOIRQ) {
649
650 if (!(flags & PCM_MMAP)) {
651 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
652 goto fail;
653 }
654
655 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
656 pcm->noirq_frames_per_msec = config->rate / 1000;
657 }
658
659 if (flags & PCM_MMAP)
660 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
661 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
662 else
663 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
664 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
665
Simon Wilsonedff7082011-06-06 15:33:34 -0700666 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
667 oops(pcm, errno, "cannot set hw params");
Simon Wilsone9942c82011-10-13 13:57:25 -0700668 goto fail_close;
Simon Wilsonedff7082011-06-06 15:33:34 -0700669 }
670
Simon Wilsone9942c82011-10-13 13:57:25 -0700671 /* get our refined hw_params */
672 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
673 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
674 pcm->buffer_size = config->period_count * config->period_size;
675
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 }
685
686
Simon Wilsonedff7082011-06-06 15:33:34 -0700687 memset(&sparams, 0, sizeof(sparams));
Simon Wilsondd88f132011-07-25 10:58:30 -0700688 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilsonedff7082011-06-06 15:33:34 -0700689 sparams.period_step = 1;
Simon Wilsonc1239622011-07-27 15:08:34 -0700690
Eric Laurentff2e5422012-08-22 16:18:14 -0700691 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
Simon Wilsonc1239622011-07-27 15:08:34 -0700698 sparams.start_threshold = config->start_threshold;
699
Simon Wilsone9942c82011-10-13 13:57:25 -0700700 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent1b32ddf2012-01-30 11:31:56 -0800701 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 }
Simon Wilsonc1239622011-07-27 15:08:34 -0700709 else
710 sparams.stop_threshold = config->stop_threshold;
711
Eric Laurent73b9c672011-10-14 11:12:24 -0700712 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;
719
Simon Wilsonedff7082011-06-06 15:33:34 -0700720 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
721 sparams.silence_size = 0;
Simon Wilsonc1239622011-07-27 15:08:34 -0700722 sparams.silence_threshold = config->silence_threshold;
Simon Wilsone9942c82011-10-13 13:57:25 -0700723 pcm->boundary = sparams.boundary = pcm->buffer_size;
Simon Wilsonc1239622011-07-27 15:08:34 -0700724
Simon Wilsondaa83292012-02-28 15:26:02 -0800725 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Simon Wilsone9942c82011-10-13 13:57:25 -0700726 pcm->boundary *= 2;
Simon Wilsonedff7082011-06-06 15:33:34 -0700727
728 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
729 oops(pcm, errno, "cannot set sw params");
730 goto fail;
731 }
732
Simon Wilsondd88f132011-07-25 10:58:30 -0700733 rc = pcm_hw_mmap_status(pcm);
734 if (rc < 0) {
735 oops(pcm, rc, "mmap status failed");
736 goto fail;
737 }
738
Simon Wilsonedff7082011-06-06 15:33:34 -0700739 pcm->underruns = 0;
740 return pcm;
741
742fail:
Simon Wilsone9942c82011-10-13 13:57:25 -0700743 if (flags & PCM_MMAP)
744 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
745fail_close:
Simon Wilsonedff7082011-06-06 15:33:34 -0700746 close(pcm->fd);
747 pcm->fd = -1;
748 return pcm;
749}
750
751int pcm_is_ready(struct pcm *pcm)
752{
753 return pcm->fd >= 0;
754}
Simon Wilson70d77082011-06-24 11:08:10 -0700755
756int pcm_start(struct pcm *pcm)
757{
758 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
759 return oops(pcm, errno, "cannot prepare channel");
Simon Wilsone9942c82011-10-13 13:57:25 -0700760
761 if (pcm->flags & PCM_MMAP)
762 pcm_sync_ptr(pcm, 0);
763
Simon Wilson70d77082011-06-24 11:08:10 -0700764 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
765 return oops(pcm, errno, "cannot start channel");
766
Simon Wilsone9942c82011-10-13 13:57:25 -0700767 pcm->running = 1;
Simon Wilson70d77082011-06-24 11:08:10 -0700768 return 0;
769}
770
771int pcm_stop(struct pcm *pcm)
772{
773 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
774 return oops(pcm, errno, "cannot stop channel");
775
Simon Wilsone9942c82011-10-13 13:57:25 -0700776 pcm->running = 0;
Simon Wilson70d77082011-06-24 11:08:10 -0700777 return 0;
778}
779
Simon Wilsone9942c82011-10-13 13:57:25 -0700780static inline int pcm_mmap_playback_avail(struct pcm *pcm)
781{
782 int avail;
783
784 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
785
786 if (avail < 0)
787 avail += pcm->boundary;
788 else if (avail > (int)pcm->boundary)
789 avail -= pcm->boundary;
790
791 return avail;
792}
793
794static inline int pcm_mmap_capture_avail(struct pcm *pcm)
795{
796 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
797 if (avail < 0)
798 avail += pcm->boundary;
799 return avail;
800}
801
802static inline int pcm_mmap_avail(struct pcm *pcm)
803{
804 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
805 if (pcm->flags & PCM_IN)
806 return pcm_mmap_capture_avail(pcm);
807 else
808 return pcm_mmap_playback_avail(pcm);
809}
810
811static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
812{
813 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
814 appl_ptr += frames;
815
816 /* check for boundary wrap */
817 if (appl_ptr > pcm->boundary)
818 appl_ptr -= pcm->boundary;
819 pcm->mmap_control->appl_ptr = appl_ptr;
820}
821
822int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
823 unsigned int *frames)
824{
825 unsigned int continuous, copy_frames, avail;
826
827 /* return the mmap buffer */
828 *areas = pcm->mmap_buffer;
829
830 /* and the application offset in frames */
831 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
832
833 avail = pcm_mmap_avail(pcm);
834 if (avail > pcm->buffer_size)
835 avail = pcm->buffer_size;
836 continuous = pcm->buffer_size - *offset;
837
838 /* we can only copy frames if the are availabale and continuos */
839 copy_frames = *frames;
840 if (copy_frames > avail)
841 copy_frames = avail;
842 if (copy_frames > continuous)
843 copy_frames = continuous;
844 *frames = copy_frames;
845
846 return 0;
847}
848
849int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
850{
851 /* update the application pointer in userspace and kernel */
852 pcm_mmap_appl_forward(pcm, frames);
853 pcm_sync_ptr(pcm, 0);
854
855 return frames;
856}
857
858int pcm_avail_update(struct pcm *pcm)
859{
860 pcm_sync_ptr(pcm, 0);
861 return pcm_mmap_avail(pcm);
862}
863
864int pcm_state(struct pcm *pcm)
865{
866 int err = pcm_sync_ptr(pcm, 0);
867 if (err < 0)
868 return err;
869
870 return pcm->mmap_status->state;
871}
872
Eric Laurent73b9c672011-10-14 11:12:24 -0700873int pcm_set_avail_min(struct pcm *pcm, int avail_min)
874{
875 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
876 return -ENOSYS;
877
878 pcm->config.avail_min = avail_min;
879 return 0;
880}
881
Simon Wilsone9942c82011-10-13 13:57:25 -0700882int pcm_wait(struct pcm *pcm, int timeout)
883{
884 struct pollfd pfd;
Simon Wilsone9942c82011-10-13 13:57:25 -0700885 int err;
886
887 pfd.fd = pcm->fd;
888 pfd.events = POLLOUT | POLLERR | POLLNVAL;
889
890 do {
891 /* let's wait for avail or timeout */
892 err = poll(&pfd, 1, timeout);
893 if (err < 0)
894 return -errno;
895
896 /* timeout ? */
897 if (err == 0)
898 return 0;
899
900 /* have we been interrupted ? */
901 if (errno == -EINTR)
902 continue;
903
904 /* check for any errors */
905 if (pfd.revents & (POLLERR | POLLNVAL)) {
906 switch (pcm_state(pcm)) {
907 case PCM_STATE_XRUN:
908 return -EPIPE;
909 case PCM_STATE_SUSPENDED:
910 return -ESTRPIPE;
911 case PCM_STATE_DISCONNECTED:
912 return -ENODEV;
913 default:
914 return -EIO;
915 }
916 }
917 /* poll again if fd not ready for IO */
918 } while (!(pfd.revents & (POLLIN | POLLOUT)));
919
920 return 1;
921}
922
Simon Wilsondaa83292012-02-28 15:26:02 -0800923int pcm_mmap_write(struct pcm *pcm, const void *buffer, unsigned int bytes)
Simon Wilsone9942c82011-10-13 13:57:25 -0700924{
925 int err = 0, frames, avail;
926 unsigned int offset = 0, count;
927
928 if (bytes == 0)
929 return 0;
930
931 count = pcm_bytes_to_frames(pcm, bytes);
932
933 while (count > 0) {
934
935 /* get the available space for writing new frames */
936 avail = pcm_avail_update(pcm);
937 if (avail < 0) {
938 fprintf(stderr, "cannot determine available mmap frames");
939 return err;
940 }
941
942 /* start the audio if we reach the threshold */
943 if (!pcm->running &&
944 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
945 if (pcm_start(pcm) < 0) {
946 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
947 (unsigned int)pcm->mmap_status->hw_ptr,
948 (unsigned int)pcm->mmap_control->appl_ptr,
949 avail);
950 return -errno;
951 }
Eric Laurent73b9c672011-10-14 11:12:24 -0700952 pcm->wait_for_avail_min = 0;
Simon Wilsone9942c82011-10-13 13:57:25 -0700953 }
954
955 /* sleep until we have space to write new frames */
Eric Laurent73b9c672011-10-14 11:12:24 -0700956 if (pcm->running) {
957 /* enable waiting for avail_min threshold when less frames than we have to write
958 * are available. */
959 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
960 pcm->wait_for_avail_min = 1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700961
Eric Laurent73b9c672011-10-14 11:12:24 -0700962 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
963 int time = -1;
Simon Wilsone9942c82011-10-13 13:57:25 -0700964
Eric Laurent73b9c672011-10-14 11:12:24 -0700965 /* disable waiting for avail_min threshold to allow small amounts of data to be
966 * written without waiting as long as there is enough room in buffer. */
967 pcm->wait_for_avail_min = 0;
968
969 if (pcm->flags & PCM_NOIRQ)
970 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
971
972 err = pcm_wait(pcm, time);
973 if (err < 0) {
974 pcm->running = 0;
975 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
976 (unsigned int)pcm->mmap_status->hw_ptr,
977 (unsigned int)pcm->mmap_control->appl_ptr,
978 avail);
979 pcm->mmap_control->appl_ptr = 0;
980 return err;
981 }
982 continue;
Simon Wilsone9942c82011-10-13 13:57:25 -0700983 }
Simon Wilsone9942c82011-10-13 13:57:25 -0700984 }
985
986 frames = count;
987 if (frames > avail)
988 frames = avail;
989
990 if (!frames)
991 break;
992
993 /* copy frames from buffer */
994 frames = pcm_mmap_write_areas(pcm, buffer, offset, frames);
995 if (frames < 0) {
996 fprintf(stderr, "write 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 return frames;
1001 }
1002
1003 offset += frames;
1004 count -= frames;
1005 }
1006
Simon Wilsone9942c82011-10-13 13:57:25 -07001007 return 0;
1008}