blob: b4b4be1850e525ed4c34a2e5140ccf4548e1e66f [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -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>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
45#define __force
46#define __bitwise
47#define __user
48#include <sound/asound.h>
49
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030050#include <tinyalsa/pcm.h>
Simon Wilson79d39652011-05-25 13:44:23 -070051
52#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070053#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070054
55static inline int param_is_mask(int p)
56{
57 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
58 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
59}
60
61static inline int param_is_interval(int p)
62{
63 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
64 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
65}
66
67static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
68{
69 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
70}
71
72static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
73{
74 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
75}
76
77static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
78{
79 if (bit >= SNDRV_MASK_MAX)
80 return;
81 if (param_is_mask(n)) {
82 struct snd_mask *m = param_to_mask(p, n);
83 m->bits[0] = 0;
84 m->bits[1] = 0;
85 m->bits[bit >> 5] |= (1 << (bit & 31));
86 }
87}
88
89static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
90{
91 if (param_is_interval(n)) {
92 struct snd_interval *i = param_to_interval(p, n);
93 i->min = val;
94 }
95}
96
Simon Wilson43544882012-10-31 12:52:39 -070097static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
98{
99 if (param_is_interval(n)) {
100 struct snd_interval *i = param_to_interval(p, n);
101 return i->min;
102 }
103 return 0;
104}
105
106static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
107{
108 if (param_is_interval(n)) {
109 struct snd_interval *i = param_to_interval(p, n);
110 return i->max;
111 }
112 return 0;
113}
114
Simon Wilson79d39652011-05-25 13:44:23 -0700115static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
116{
117 if (param_is_interval(n)) {
118 struct snd_interval *i = param_to_interval(p, n);
119 i->min = val;
120 i->max = val;
121 i->integer = 1;
122 }
123}
124
Liam Girdwood6be28f12011-10-13 12:59:51 -0700125static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
126{
127 if (param_is_interval(n)) {
128 struct snd_interval *i = param_to_interval(p, n);
129 if (i->integer)
130 return i->max;
131 }
132 return 0;
133}
134
Simon Wilson79d39652011-05-25 13:44:23 -0700135static void param_init(struct snd_pcm_hw_params *p)
136{
137 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700138
Simon Wilson79d39652011-05-25 13:44:23 -0700139 memset(p, 0, sizeof(*p));
140 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
141 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
142 struct snd_mask *m = param_to_mask(p, n);
143 m->bits[0] = ~0;
144 m->bits[1] = ~0;
145 }
146 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
147 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
148 struct snd_interval *i = param_to_interval(p, n);
149 i->min = 0;
150 i->max = ~0;
151 }
Simon Wilson43544882012-10-31 12:52:39 -0700152 p->rmask = ~0U;
153 p->cmask = 0;
154 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700155}
156
157#define PCM_ERROR_MAX 128
158
Taylor Holberton6d58e012016-10-01 18:32:30 -0400159/** A PCM handle.
160 * @ingroup tinyalsa-pcm
161 */
Simon Wilson79d39652011-05-25 13:44:23 -0700162struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400163 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700164 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400165 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700166 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400167 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700168 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400169 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530170 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400171 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700172 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400173 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700174 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700175 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400176 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700177 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400178 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700179 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700180 struct snd_pcm_mmap_status *mmap_status;
181 struct snd_pcm_mmap_control *mmap_control;
182 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700183 void *mmap_buffer;
184 unsigned int noirq_frames_per_msec;
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530185 long pcm_delay;
David Wagner4cddf192014-04-02 15:12:54 +0200186 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700187};
188
Taylor Holberton6d58e012016-10-01 18:32:30 -0400189/** Gets the buffer size of the PCM.
190 * @param pcm A PCM handle.
191 * @return The buffer size of the PCM.
192 * @ingroup tinyalsa-pcm
193 */
Simon Wilson851aa5c2011-05-30 21:18:26 -0700194unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700195{
196 return pcm->buffer_size;
197}
198
Taylor Holberton6d58e012016-10-01 18:32:30 -0400199/** Gets the file descriptor of the PCM.
200 * Useful for extending functionality of the PCM when needed.
201 * @return The file descriptor of the PCM.
202 * @ingroup tinyalsa-pcm
203 */
Taylor Holbertonbb402602016-08-03 10:15:46 -0400204int pcm_get_file_descriptor(struct pcm *pcm)
205{
206 return pcm->fd;
207}
208
Taylor Holberton6d58e012016-10-01 18:32:30 -0400209/** Gets the error message for the last error that occured.
210 * If no error occured and this function is called, the results are undefined.
211 * @param pcm A PCM handle.
212 * @return The error message of the last error that occured.
213 * @ingroup tinyalsa-pcm
214 */
Simon Wilson79d39652011-05-25 13:44:23 -0700215const char* pcm_get_error(struct pcm *pcm)
216{
217 return pcm->error;
218}
219
Taylor Holberton6d58e012016-10-01 18:32:30 -0400220/** Gets the subdevice on which the pcm has been opened.
221 * @param pcm A PCM handle.
222 * @return The subdevice on which the pcm has been opened */
David Wagner4cddf192014-04-02 15:12:54 +0200223unsigned int pcm_get_subdevice(struct pcm *pcm)
224{
225 return pcm->subdevice;
226}
227
Simon Wilson79d39652011-05-25 13:44:23 -0700228static int oops(struct pcm *pcm, int e, const char *fmt, ...)
229{
230 va_list ap;
231 int sz;
232
233 va_start(ap, fmt);
234 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
235 va_end(ap);
236 sz = strlen(pcm->error);
237
238 if (errno)
239 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
240 ": %s", strerror(e));
241 return -1;
242}
243
Simon Wilsonbc03b622011-06-15 17:19:01 -0700244static unsigned int pcm_format_to_alsa(enum pcm_format format)
245{
246 switch (format) {
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400247
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500248 case PCM_FORMAT_S8:
249 return SNDRV_PCM_FORMAT_S8;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400250
Simon Wilsonbc03b622011-06-15 17:19:01 -0700251 default:
252 case PCM_FORMAT_S16_LE:
253 return SNDRV_PCM_FORMAT_S16_LE;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400254 case PCM_FORMAT_S16_BE:
255 return SNDRV_PCM_FORMAT_S16_BE;
256
257 case PCM_FORMAT_S24_LE:
258 return SNDRV_PCM_FORMAT_S24_LE;
259 case PCM_FORMAT_S24_BE:
260 return SNDRV_PCM_FORMAT_S24_BE;
261
262 case PCM_FORMAT_S24_3LE:
263 return SNDRV_PCM_FORMAT_S24_3LE;
264 case PCM_FORMAT_S24_3BE:
265 return SNDRV_PCM_FORMAT_S24_3BE;
266
267 case PCM_FORMAT_S32_LE:
268 return SNDRV_PCM_FORMAT_S32_LE;
269 case PCM_FORMAT_S32_BE:
270 return SNDRV_PCM_FORMAT_S32_BE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700271 };
272}
273
Taylor Holberton6d58e012016-10-01 18:32:30 -0400274/** Determines the number of bits occupied by a @ref pcm_format.
275 * @param format A PCM format.
276 * @return The number of bits associated with @p format
277 * @ingroup tinyalsa-pcm
278 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700279unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700280{
281 switch (format) {
282 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400283 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700284 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400285 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700286 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400287 case PCM_FORMAT_S24_3LE:
288 case PCM_FORMAT_S24_3BE:
289 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700290 default:
291 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400292 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700293 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400294 case PCM_FORMAT_S8:
295 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700296 };
297}
298
Taylor Holberton6d58e012016-10-01 18:32:30 -0400299/** Determines how many frames of a PCM can fit into a number of bytes.
300 * @param pcm A PCM handle.
301 * @param bytes The number of bytes.
302 * @return The number of frames that may fit into @p bytes
303 * @ingroup tinyalsa-pcm
304 */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700305unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
306{
307 return bytes / (pcm->config.channels *
308 (pcm_format_to_bits(pcm->config.format) >> 3));
309}
310
Taylor Holberton6d58e012016-10-01 18:32:30 -0400311/** Determines how many bytes are occupied by a number of frames of a PCM.
312 * @param pcm A PCM handle.
313 * @param frames The number of frames of a PCM.
314 * @return The bytes occupied by @p frames.
315 * @ingroup tinyalsa-pcm
316 */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700317unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
318{
319 return frames * pcm->config.channels *
320 (pcm_format_to_bits(pcm->config.format) >> 3);
321}
322
Taylor Holberton4f556062016-09-16 09:54:36 -0400323static int pcm_sync_ptr(struct pcm *pcm, int flags)
324{
Eric Laurent40b018e2011-06-18 10:10:23 -0700325 if (pcm->sync_ptr) {
326 pcm->sync_ptr->flags = flags;
327 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
328 return -1;
329 }
330 return 0;
331}
332
Taylor Holberton4f556062016-09-16 09:54:36 -0400333static int pcm_hw_mmap_status(struct pcm *pcm)
334{
Eric Laurent40b018e2011-06-18 10:10:23 -0700335 if (pcm->sync_ptr)
336 return 0;
337
338 int page_size = sysconf(_SC_PAGE_SIZE);
339 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
340 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
341 if (pcm->mmap_status == MAP_FAILED)
342 pcm->mmap_status = NULL;
343 if (!pcm->mmap_status)
344 goto mmap_error;
345
346 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
347 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
348 if (pcm->mmap_control == MAP_FAILED)
349 pcm->mmap_control = NULL;
350 if (!pcm->mmap_control) {
351 munmap(pcm->mmap_status, page_size);
352 pcm->mmap_status = NULL;
353 goto mmap_error;
354 }
355 pcm->mmap_control->avail_min = 1;
356
357 return 0;
358
359mmap_error:
360
361 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
362 if (!pcm->sync_ptr)
363 return -ENOMEM;
364 pcm->mmap_status = &pcm->sync_ptr->s.status;
365 pcm->mmap_control = &pcm->sync_ptr->c.control;
366 pcm->mmap_control->avail_min = 1;
367 pcm_sync_ptr(pcm, 0);
368
369 return 0;
370}
371
372static void pcm_hw_munmap_status(struct pcm *pcm) {
373 if (pcm->sync_ptr) {
374 free(pcm->sync_ptr);
375 pcm->sync_ptr = NULL;
376 } else {
377 int page_size = sysconf(_SC_PAGE_SIZE);
378 if (pcm->mmap_status)
379 munmap(pcm->mmap_status, page_size);
380 if (pcm->mmap_control)
381 munmap(pcm->mmap_control, page_size);
382 }
383 pcm->mmap_status = NULL;
384 pcm->mmap_control = NULL;
385}
386
Liam Girdwood6be28f12011-10-13 12:59:51 -0700387static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700388 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700389 unsigned int frames)
390{
391 int size_bytes = pcm_frames_to_bytes(pcm, frames);
392 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
393 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
394
395 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700396 if (pcm->flags & PCM_IN)
397 memcpy(buf + src_offset_bytes,
398 (char*)pcm->mmap_buffer + pcm_offset_bytes,
399 size_bytes);
400 else
401 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
402 buf + src_offset_bytes,
403 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700404 return 0;
405}
406
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700407static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700408 unsigned int offset, unsigned int size)
409{
410 void *pcm_areas;
411 int commit;
412 unsigned int pcm_offset, frames, count = 0;
413
414 while (size > 0) {
415 frames = size;
416 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700417 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700418 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
419 if (commit < 0) {
420 oops(pcm, commit, "failed to commit %d frames\n", frames);
421 return commit;
422 }
423
424 offset += commit;
425 count += commit;
426 size -= commit;
427 }
428 return count;
429}
430
Taylor Holberton6d58e012016-10-01 18:32:30 -0400431/** Returns available frames in pcm buffer and corresponding time stamp.
432 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
433 * otherwise the clock is CLOCK_REALTIME.
434 * For an input stream, frames available are frames ready for the application to read.
435 * For an output stream, frames available are the number of empty frames available for the application to write.
436 * Only available for PCMs opened with the @ref PCM_MMAP flag.
437 * @param pcm A PCM handle.
438 * @param avail The number of available frames
439 * @param tstamp The timestamp
440 * @return On success, zero is returned; on failure, negative one.
441 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700442int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
443 struct timespec *tstamp)
444{
445 int frames;
446 int rc;
447 snd_pcm_uframes_t hw_ptr;
448
449 if (!pcm_is_ready(pcm))
450 return -1;
451
452 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
453 if (rc < 0)
454 return -1;
455
Eric Laurent7db48582011-11-17 11:47:59 -0800456 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
457 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800458 return -1;
459
Eric Laurent40b018e2011-06-18 10:10:23 -0700460 *tstamp = pcm->mmap_status->tstamp;
461 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
462 return -1;
463
464 hw_ptr = pcm->mmap_status->hw_ptr;
465 if (pcm->flags & PCM_IN)
466 frames = hw_ptr - pcm->mmap_control->appl_ptr;
467 else
468 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
469
470 if (frames < 0)
471 return -1;
472
473 *avail = (unsigned int)frames;
474
475 return 0;
476}
477
Taylor Holberton6d58e012016-10-01 18:32:30 -0400478/** Writes audio samples to PCM.
479 * If the PCM has not been started, it is started in this function.
480 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
481 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
482 * @param pcm A PCM handle.
483 * @param data The audio sample array
484 * @param count The number of bytes occupied by the sample array.
485 * @return On success, this function returns zero; otherwise, a negative number.
486 * @ingroup tinyalsa-pcm
487 */
Mark Brown6bbe77a2012-02-10 22:07:09 +0000488int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700489{
490 struct snd_xferi x;
491
492 if (pcm->flags & PCM_IN)
493 return -EINVAL;
494
Mark Brown6bbe77a2012-02-10 22:07:09 +0000495 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700496 x.frames = count / (pcm->config.channels *
497 pcm_format_to_bits(pcm->config.format) / 8);
Taylor Holberton2386a422016-11-18 20:38:40 -0800498 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700499 for (;;) {
500 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530501 int prepare_error = pcm_prepare(pcm);
502 if (prepare_error)
503 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700504 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
505 return oops(pcm, errno, "cannot write initial data");
506 pcm->running = 1;
507 return 0;
508 }
509 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530510 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700511 pcm->running = 0;
512 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700513 /* we failed to make our window -- try to restart if we are
514 * allowed to do so. Otherwise, simply allow the EPIPE error to
515 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700516 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700517 if (pcm->flags & PCM_NORESTART)
518 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700519 continue;
520 }
521 return oops(pcm, errno, "cannot write stream data");
522 }
523 return 0;
524 }
525}
526
Taylor Holberton6d58e012016-10-01 18:32:30 -0400527/** Reads audio samples from PCM.
528 * If the PCM has not been started, it is started in this function.
529 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
530 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
531 * @param pcm A PCM handle.
532 * @param data The audio sample array
533 * @param count The number of bytes occupied by the sample array.
534 * @return On success, this function returns zero; otherwise, a negative number.
535 * @ingroup tinyalsa-pcm
536 */
Simon Wilson79d39652011-05-25 13:44:23 -0700537int pcm_read(struct pcm *pcm, void *data, unsigned int count)
538{
539 struct snd_xferi x;
540
541 if (!(pcm->flags & PCM_IN))
542 return -EINVAL;
543
544 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700545 x.frames = count / (pcm->config.channels *
546 pcm_format_to_bits(pcm->config.format) / 8);
Taylor Holberton2386a422016-11-18 20:38:40 -0800547 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700548 for (;;) {
549 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700550 if (pcm_start(pcm) < 0) {
551 fprintf(stderr, "start error");
552 return -errno;
553 }
Simon Wilson79d39652011-05-25 13:44:23 -0700554 }
555 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530556 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700557 pcm->running = 0;
558 if (errno == EPIPE) {
559 /* we failed to make our window -- try to restart */
560 pcm->underruns++;
561 continue;
562 }
563 return oops(pcm, errno, "cannot read stream data");
564 }
565 return 0;
566 }
567}
568
569static struct pcm bad_pcm = {
570 .fd = -1,
571};
572
Taylor Holberton6d58e012016-10-01 18:32:30 -0400573/** Gets the hardware parameters of a PCM, without created a PCM handle.
574 * @param card The card of the PCM.
575 * The default card is zero.
576 * @param device The device of the PCM.
577 * The default device is zero.
578 * @param flags Specifies whether the PCM is an input or output.
579 * May be one of the following:
580 * - @ref PCM_IN
581 * - @ref PCM_OUT
582 * @return On success, the hardware parameters of the PCM; on failure, NULL.
583 * @ingroup tinyalsa-pcm
584 */
Simon Wilson43544882012-10-31 12:52:39 -0700585struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
586 unsigned int flags)
587{
588 struct snd_pcm_hw_params *params;
589 char fn[256];
590 int fd;
591
592 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
593 flags & PCM_IN ? 'c' : 'p');
594
595 fd = open(fn, O_RDWR);
596 if (fd < 0) {
597 fprintf(stderr, "cannot open device '%s'\n", fn);
598 goto err_open;
599 }
600
601 params = calloc(1, sizeof(struct snd_pcm_hw_params));
602 if (!params)
603 goto err_calloc;
604
605 param_init(params);
606 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
607 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
608 goto err_hw_refine;
609 }
610
611 close(fd);
612
613 return (struct pcm_params *)params;
614
615err_hw_refine:
616 free(params);
617err_calloc:
618 close(fd);
619err_open:
620 return NULL;
621}
622
Taylor Holberton6d58e012016-10-01 18:32:30 -0400623/** Frees the hardware parameters returned by @ref pcm_params_open.
624 * @param pcm_params Hardware parameters of a PCM.
625 * May be NULL.
626 * @ingroup tinyalsa-pcm
627 */
Simon Wilson43544882012-10-31 12:52:39 -0700628void pcm_params_free(struct pcm_params *pcm_params)
629{
630 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
631
632 if (params)
633 free(params);
634}
635
636static int pcm_param_to_alsa(enum pcm_param param)
637{
638 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700639 case PCM_PARAM_ACCESS:
640 return SNDRV_PCM_HW_PARAM_ACCESS;
641 case PCM_PARAM_FORMAT:
642 return SNDRV_PCM_HW_PARAM_FORMAT;
643 case PCM_PARAM_SUBFORMAT:
644 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700645 case PCM_PARAM_SAMPLE_BITS:
646 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
647 break;
648 case PCM_PARAM_FRAME_BITS:
649 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
650 break;
651 case PCM_PARAM_CHANNELS:
652 return SNDRV_PCM_HW_PARAM_CHANNELS;
653 break;
654 case PCM_PARAM_RATE:
655 return SNDRV_PCM_HW_PARAM_RATE;
656 break;
657 case PCM_PARAM_PERIOD_TIME:
658 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
659 break;
660 case PCM_PARAM_PERIOD_SIZE:
661 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
662 break;
663 case PCM_PARAM_PERIOD_BYTES:
664 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
665 break;
666 case PCM_PARAM_PERIODS:
667 return SNDRV_PCM_HW_PARAM_PERIODS;
668 break;
669 case PCM_PARAM_BUFFER_TIME:
670 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
671 break;
672 case PCM_PARAM_BUFFER_SIZE:
673 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
674 break;
675 case PCM_PARAM_BUFFER_BYTES:
676 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
677 break;
678 case PCM_PARAM_TICK_TIME:
679 return SNDRV_PCM_HW_PARAM_TICK_TIME;
680 break;
681
682 default:
683 return -1;
684 }
685}
686
Taylor Holberton6d58e012016-10-01 18:32:30 -0400687/** Gets a mask from a PCM's hardware parameters.
688 * @param pcm_params A PCM's hardware parameters.
689 * @param param The parameter to get.
690 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
691 * Otherwise, the mask associated with @p param is returned.
692 * @ingroup tinyalsa-pcm
693 */
Andy Hungad807622014-03-10 18:08:15 -0700694struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
695 enum pcm_param param)
696{
697 int p;
698 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
699 if (params == NULL) {
700 return NULL;
701 }
702
703 p = pcm_param_to_alsa(param);
704 if (p < 0 || !param_is_mask(p)) {
705 return NULL;
706 }
707
708 return (struct pcm_mask *)param_to_mask(params, p);
709}
710
Simon Wilson43544882012-10-31 12:52:39 -0700711unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
712 enum pcm_param param)
713{
714 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
715 int p;
716
717 if (!params)
718 return 0;
719
720 p = pcm_param_to_alsa(param);
721 if (p < 0)
722 return 0;
723
724 return param_get_min(params, p);
725}
726
727unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
728 enum pcm_param param)
729{
730 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
731 int p;
732
733 if (!params)
734 return 0;
735
736 p = pcm_param_to_alsa(param);
737 if (p < 0)
738 return 0;
739
740 return param_get_max(params, p);
741}
742
Taylor Holberton6d58e012016-10-01 18:32:30 -0400743/** Closes a PCM returned by @ref pcm_open.
744 * @param pcm A PCM returned by @ref pcm_open.
745 * May not be NULL.
746 * @return Always returns zero.
747 * @ingroup tinyalsa-pcm
748 */
Simon Wilson79d39652011-05-25 13:44:23 -0700749int pcm_close(struct pcm *pcm)
750{
751 if (pcm == &bad_pcm)
752 return 0;
753
Eric Laurent40b018e2011-06-18 10:10:23 -0700754 pcm_hw_munmap_status(pcm);
755
Liam Girdwood6be28f12011-10-13 12:59:51 -0700756 if (pcm->flags & PCM_MMAP) {
757 pcm_stop(pcm);
758 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
759 }
760
Simon Wilson79d39652011-05-25 13:44:23 -0700761 if (pcm->fd >= 0)
762 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530763 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700764 pcm->running = 0;
765 pcm->buffer_size = 0;
766 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700767 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700768 return 0;
769}
770
Taylor Holberton6d58e012016-10-01 18:32:30 -0400771/** Opens a PCM.
772 * @param card The card that the pcm belongs to.
773 * The default card is zero.
774 * @param device The device that the pcm belongs to.
775 * The default device is zero.
776 * @param flags Specify characteristics and functionality about the pcm.
777 * May be a bitwise AND of the following:
778 * - @ref PCM_IN
779 * - @ref PCM_OUT
780 * - @ref PCM_MMAP
781 * - @ref PCM_NOIRQ
782 * - @ref PCM_MONOTONIC
783 * @param config The hardware and software parameters to open the PCM with.
784 * @returns On success, returns an initialized pcm, ready for reading or writing.
785 * On error, returns NULL.
786 * @ingroup tinyalsa-pcm
787 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700788struct pcm *pcm_open(unsigned int card, unsigned int device,
789 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700790{
Simon Wilson79d39652011-05-25 13:44:23 -0700791 struct pcm *pcm;
792 struct snd_pcm_info info;
793 struct snd_pcm_hw_params params;
794 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700795 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700796 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700797
798 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400799 if (!pcm)
800 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700801
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400802 if (config == NULL) {
803 config = &pcm->config;
804 config->channels = 2;
805 config->rate = 48000;
806 config->period_size = 1024;
807 config->period_count = 4;
808 config->format = PCM_FORMAT_S16_LE;
809 config->start_threshold = config->period_count * config->period_size;
810 config->stop_threshold = config->period_count * config->period_size;
811 config->silence_threshold = 0;
812 } else {
813 pcm->config = *config;
814 }
Simon Wilson79d39652011-05-25 13:44:23 -0700815
Simon Wilson1bd580f2011-06-02 15:58:41 -0700816 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
817 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700818
819 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700820 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700821 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700822 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700823 return pcm;
824 }
825
826 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700827 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700828 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700829 }
David Wagner4cddf192014-04-02 15:12:54 +0200830 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700831
832 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700833 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
834 pcm_format_to_alsa(config->format));
835 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
836 SNDRV_PCM_SUBFORMAT_STD);
837 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
838 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
839 pcm_format_to_bits(config->format));
840 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
841 pcm_format_to_bits(config->format) * config->channels);
842 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
843 config->channels);
844 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
845 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
846
Liam Girdwood6be28f12011-10-13 12:59:51 -0700847 if (flags & PCM_NOIRQ) {
848
849 if (!(flags & PCM_MMAP)) {
850 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
851 goto fail;
852 }
853
854 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
855 pcm->noirq_frames_per_msec = config->rate / 1000;
856 }
857
858 if (flags & PCM_MMAP)
859 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
860 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
861 else
862 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
863 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
864
Simon Wilson79d39652011-05-25 13:44:23 -0700865 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
866 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700867 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700868 }
869
Liam Girdwood6be28f12011-10-13 12:59:51 -0700870 /* get our refined hw_params */
871 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
872 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
873 pcm->buffer_size = config->period_count * config->period_size;
874
875 if (flags & PCM_MMAP) {
876 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
877 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
878 if (pcm->mmap_buffer == MAP_FAILED) {
879 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
880 pcm_frames_to_bytes(pcm, pcm->buffer_size));
881 goto fail_close;
882 }
883 }
884
885
Simon Wilson79d39652011-05-25 13:44:23 -0700886 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700887 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700888 sparams.period_step = 1;
889 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700890
Eric Laurent93e7b672012-08-22 16:18:14 -0700891 if (!config->start_threshold) {
892 if (pcm->flags & PCM_IN)
893 pcm->config.start_threshold = sparams.start_threshold = 1;
894 else
895 pcm->config.start_threshold = sparams.start_threshold =
896 config->period_count * config->period_size / 2;
897 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700898 sparams.start_threshold = config->start_threshold;
899
Liam Girdwood6be28f12011-10-13 12:59:51 -0700900 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800901 if (!config->stop_threshold) {
902 if (pcm->flags & PCM_IN)
903 pcm->config.stop_threshold = sparams.stop_threshold =
904 config->period_count * config->period_size * 10;
905 else
906 pcm->config.stop_threshold = sparams.stop_threshold =
907 config->period_count * config->period_size;
908 }
John Grossman3bb114a2011-07-21 10:59:55 -0700909 else
910 sparams.stop_threshold = config->stop_threshold;
911
Simon Wilson79d39652011-05-25 13:44:23 -0700912 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
913 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700914 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700915 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700916
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600917 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700918 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700919
920 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
921 oops(pcm, errno, "cannot set sw params");
922 goto fail;
923 }
924
Eric Laurent40b018e2011-06-18 10:10:23 -0700925 rc = pcm_hw_mmap_status(pcm);
926 if (rc < 0) {
927 oops(pcm, rc, "mmap status failed");
928 goto fail;
929 }
930
Glenn Kasten81012402013-08-22 15:11:48 -0700931#ifdef SNDRV_PCM_IOCTL_TTSTAMP
932 if (pcm->flags & PCM_MONOTONIC) {
933 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
934 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
935 if (rc < 0) {
936 oops(pcm, rc, "cannot set timestamp type");
937 goto fail;
938 }
939 }
940#endif
941
Simon Wilson79d39652011-05-25 13:44:23 -0700942 pcm->underruns = 0;
943 return pcm;
944
945fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700946 if (flags & PCM_MMAP)
947 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
948fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700949 close(pcm->fd);
950 pcm->fd = -1;
951 return pcm;
952}
953
Taylor Holberton6d58e012016-10-01 18:32:30 -0400954/** Checks if a PCM file has been opened without error.
955 * @param pcm A PCM handle.
956 * @return If a PCM's file descriptor is not valid, it returns zero.
957 * Otherwise, the function returns one.
958 * @ingroup tinyalsa-pcm
959 */
Simon Wilson79d39652011-05-25 13:44:23 -0700960int pcm_is_ready(struct pcm *pcm)
961{
962 return pcm->fd >= 0;
963}
Simon Wilsond6458e62011-06-21 14:58:11 -0700964
Taylor Holberton6d58e012016-10-01 18:32:30 -0400965/** Prepares a PCM, if it has not been prepared already.
966 * @param pcm A PCM handle.
967 * @return On success, zero; on failure, a negative number.
968 * @ingroup tinyalsa-pcm
969 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530970int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700971{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530972 if (pcm->prepared)
973 return 0;
974
Simon Wilsond6458e62011-06-21 14:58:11 -0700975 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
976 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700977
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530978 pcm->prepared = 1;
979 return 0;
980}
981
Taylor Holberton6d58e012016-10-01 18:32:30 -0400982/** Starts a PCM.
983 * If the PCM has not been prepared,
984 * it is prepared in this function.
985 * @param pcm A PCM handle.
986 * @return On success, zero; on failure, a negative number.
987 * @ingroup tinyalsa-pcm
988 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530989int pcm_start(struct pcm *pcm)
990{
991 int prepare_error = pcm_prepare(pcm);
992 if (prepare_error)
993 return prepare_error;
994
Liam Girdwood6be28f12011-10-13 12:59:51 -0700995 if (pcm->flags & PCM_MMAP)
996 pcm_sync_ptr(pcm, 0);
997
Simon Wilsond6458e62011-06-21 14:58:11 -0700998 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
999 return oops(pcm, errno, "cannot start channel");
1000
Liam Girdwood6be28f12011-10-13 12:59:51 -07001001 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001002 return 0;
1003}
1004
Taylor Holberton6d58e012016-10-01 18:32:30 -04001005/** Stops a PCM.
1006 * @param pcm A PCM handle.
1007 * @return On success, zero; on failure, a negative number.
1008 * @ingroup tinyalsa-pcm
1009 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001010int pcm_stop(struct pcm *pcm)
1011{
1012 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1013 return oops(pcm, errno, "cannot stop channel");
1014
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301015 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001016 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001017 return 0;
1018}
1019
Liam Girdwood6be28f12011-10-13 12:59:51 -07001020static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1021{
1022 int avail;
1023
1024 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1025
1026 if (avail < 0)
1027 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001028 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001029 avail -= pcm->boundary;
1030
1031 return avail;
1032}
1033
1034static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1035{
1036 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1037 if (avail < 0)
1038 avail += pcm->boundary;
1039 return avail;
1040}
1041
1042static inline int pcm_mmap_avail(struct pcm *pcm)
1043{
1044 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1045 if (pcm->flags & PCM_IN)
1046 return pcm_mmap_capture_avail(pcm);
1047 else
1048 return pcm_mmap_playback_avail(pcm);
1049}
1050
1051static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1052{
1053 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1054 appl_ptr += frames;
1055
1056 /* check for boundary wrap */
1057 if (appl_ptr > pcm->boundary)
1058 appl_ptr -= pcm->boundary;
1059 pcm->mmap_control->appl_ptr = appl_ptr;
1060}
1061
1062int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1063 unsigned int *frames)
1064{
1065 unsigned int continuous, copy_frames, avail;
1066
1067 /* return the mmap buffer */
1068 *areas = pcm->mmap_buffer;
1069
1070 /* and the application offset in frames */
1071 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1072
1073 avail = pcm_mmap_avail(pcm);
1074 if (avail > pcm->buffer_size)
1075 avail = pcm->buffer_size;
1076 continuous = pcm->buffer_size - *offset;
1077
1078 /* we can only copy frames if the are availabale and continuos */
1079 copy_frames = *frames;
1080 if (copy_frames > avail)
1081 copy_frames = avail;
1082 if (copy_frames > continuous)
1083 copy_frames = continuous;
1084 *frames = copy_frames;
1085
1086 return 0;
1087}
1088
1089int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1090{
Taylor Holberton73466c02016-10-01 12:51:59 -04001091 /* not used */
1092 (void) offset;
1093
Liam Girdwood6be28f12011-10-13 12:59:51 -07001094 /* update the application pointer in userspace and kernel */
1095 pcm_mmap_appl_forward(pcm, frames);
1096 pcm_sync_ptr(pcm, 0);
1097
1098 return frames;
1099}
1100
1101int pcm_avail_update(struct pcm *pcm)
1102{
1103 pcm_sync_ptr(pcm, 0);
1104 return pcm_mmap_avail(pcm);
1105}
1106
1107int pcm_state(struct pcm *pcm)
1108{
1109 int err = pcm_sync_ptr(pcm, 0);
1110 if (err < 0)
1111 return err;
1112
1113 return pcm->mmap_status->state;
1114}
1115
1116int pcm_wait(struct pcm *pcm, int timeout)
1117{
1118 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001119 int err;
1120
1121 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001122 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001123
1124 do {
1125 /* let's wait for avail or timeout */
1126 err = poll(&pfd, 1, timeout);
1127 if (err < 0)
1128 return -errno;
1129
1130 /* timeout ? */
1131 if (err == 0)
1132 return 0;
1133
1134 /* have we been interrupted ? */
1135 if (errno == -EINTR)
1136 continue;
1137
1138 /* check for any errors */
1139 if (pfd.revents & (POLLERR | POLLNVAL)) {
1140 switch (pcm_state(pcm)) {
1141 case PCM_STATE_XRUN:
1142 return -EPIPE;
1143 case PCM_STATE_SUSPENDED:
1144 return -ESTRPIPE;
1145 case PCM_STATE_DISCONNECTED:
1146 return -ENODEV;
1147 default:
1148 return -EIO;
1149 }
1150 }
1151 /* poll again if fd not ready for IO */
1152 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1153
1154 return 1;
1155}
1156
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001157int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001158{
1159 int err = 0, frames, avail;
1160 unsigned int offset = 0, count;
1161
1162 if (bytes == 0)
1163 return 0;
1164
1165 count = pcm_bytes_to_frames(pcm, bytes);
1166
1167 while (count > 0) {
1168
1169 /* get the available space for writing new frames */
1170 avail = pcm_avail_update(pcm);
1171 if (avail < 0) {
1172 fprintf(stderr, "cannot determine available mmap frames");
1173 return err;
1174 }
1175
1176 /* start the audio if we reach the threshold */
1177 if (!pcm->running &&
1178 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1179 if (pcm_start(pcm) < 0) {
1180 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1181 (unsigned int)pcm->mmap_status->hw_ptr,
1182 (unsigned int)pcm->mmap_control->appl_ptr,
1183 avail);
1184 return -errno;
1185 }
1186 }
1187
1188 /* sleep until we have space to write new frames */
1189 if (pcm->running &&
1190 (unsigned int)avail < pcm->mmap_control->avail_min) {
1191 int time = -1;
1192
1193 if (pcm->flags & PCM_NOIRQ)
1194 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1195 / pcm->noirq_frames_per_msec;
1196
1197 err = pcm_wait(pcm, time);
1198 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301199 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001200 pcm->running = 0;
1201 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1202 (unsigned int)pcm->mmap_status->hw_ptr,
1203 (unsigned int)pcm->mmap_control->appl_ptr,
1204 avail);
1205 pcm->mmap_control->appl_ptr = 0;
1206 return err;
1207 }
1208 continue;
1209 }
1210
1211 frames = count;
1212 if (frames > avail)
1213 frames = avail;
1214
1215 if (!frames)
1216 break;
1217
1218 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001219 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001220 if (frames < 0) {
1221 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1222 (unsigned int)pcm->mmap_status->hw_ptr,
1223 (unsigned int)pcm->mmap_control->appl_ptr,
1224 avail);
1225 return frames;
1226 }
1227
1228 offset += frames;
1229 count -= frames;
1230 }
1231
Liam Girdwood6be28f12011-10-13 12:59:51 -07001232 return 0;
1233}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001234
1235int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1236{
1237 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1238 return -ENOSYS;
1239
1240 return pcm_mmap_transfer(pcm, (void *)data, count);
1241}
1242
1243int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1244{
1245 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1246 return -ENOSYS;
1247
1248 return pcm_mmap_transfer(pcm, data, count);
1249}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301250
1251long pcm_get_delay(struct pcm *pcm)
1252{
1253 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1254 return -1;
1255
1256 return pcm->pcm_delay;
1257}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001258