blob: 540fd72d773e97a82caa87ec33da231e42725c0a [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
Taylor Holberton2f387d22016-12-01 15:58:16 -080067static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
68{
69 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
70}
71
Simon Wilson79d39652011-05-25 13:44:23 -070072static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
73{
74 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
75}
76
77static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
78{
79 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
80}
81
82static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
83{
84 if (bit >= SNDRV_MASK_MAX)
85 return;
86 if (param_is_mask(n)) {
87 struct snd_mask *m = param_to_mask(p, n);
88 m->bits[0] = 0;
89 m->bits[1] = 0;
90 m->bits[bit >> 5] |= (1 << (bit & 31));
91 }
92}
93
94static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
95{
96 if (param_is_interval(n)) {
97 struct snd_interval *i = param_to_interval(p, n);
98 i->min = val;
99 }
100}
101
Taylor Holberton2f387d22016-12-01 15:58:16 -0800102static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700103{
104 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800105 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700106 return i->min;
107 }
108 return 0;
109}
110
Taylor Holberton2f387d22016-12-01 15:58:16 -0800111static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700112{
113 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800114 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700115 return i->max;
116 }
117 return 0;
118}
119
Simon Wilson79d39652011-05-25 13:44:23 -0700120static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
121{
122 if (param_is_interval(n)) {
123 struct snd_interval *i = param_to_interval(p, n);
124 i->min = val;
125 i->max = val;
126 i->integer = 1;
127 }
128}
129
Liam Girdwood6be28f12011-10-13 12:59:51 -0700130static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
131{
132 if (param_is_interval(n)) {
133 struct snd_interval *i = param_to_interval(p, n);
134 if (i->integer)
135 return i->max;
136 }
137 return 0;
138}
139
Simon Wilson79d39652011-05-25 13:44:23 -0700140static void param_init(struct snd_pcm_hw_params *p)
141{
142 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700143
Simon Wilson79d39652011-05-25 13:44:23 -0700144 memset(p, 0, sizeof(*p));
145 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
146 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
147 struct snd_mask *m = param_to_mask(p, n);
148 m->bits[0] = ~0;
149 m->bits[1] = ~0;
150 }
151 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
152 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
153 struct snd_interval *i = param_to_interval(p, n);
154 i->min = 0;
155 i->max = ~0;
156 }
Simon Wilson43544882012-10-31 12:52:39 -0700157 p->rmask = ~0U;
158 p->cmask = 0;
159 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700160}
161
162#define PCM_ERROR_MAX 128
163
Taylor Holberton6d58e012016-10-01 18:32:30 -0400164/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800165 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400166 */
Simon Wilson79d39652011-05-25 13:44:23 -0700167struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400168 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700169 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400170 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700171 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400172 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700173 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400174 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530175 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400176 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700177 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400178 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700179 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800180 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700181 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400182 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700183 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400184 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700185 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700186 struct snd_pcm_mmap_status *mmap_status;
187 struct snd_pcm_mmap_control *mmap_control;
188 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700189 void *mmap_buffer;
190 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800191 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530192 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800193 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200194 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700195};
196
Taylor Holberton6d58e012016-10-01 18:32:30 -0400197/** Gets the buffer size of the PCM.
198 * @param pcm A PCM handle.
199 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800200 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400201 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800202unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700203{
204 return pcm->buffer_size;
205}
206
Taylor Holberton6d58e012016-10-01 18:32:30 -0400207/** Gets the file descriptor of the PCM.
208 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800209 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400210 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800211 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400212 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800213int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400214{
215 return pcm->fd;
216}
217
Taylor Holberton6d58e012016-10-01 18:32:30 -0400218/** Gets the error message for the last error that occured.
219 * If no error occured and this function is called, the results are undefined.
220 * @param pcm A PCM handle.
221 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800222 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400223 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800224const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700225{
226 return pcm->error;
227}
228
Taylor Holberton6d58e012016-10-01 18:32:30 -0400229/** Gets the subdevice on which the pcm has been opened.
230 * @param pcm A PCM handle.
231 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800232unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200233{
234 return pcm->subdevice;
235}
236
Simon Wilson79d39652011-05-25 13:44:23 -0700237static int oops(struct pcm *pcm, int e, const char *fmt, ...)
238{
239 va_list ap;
240 int sz;
241
242 va_start(ap, fmt);
243 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
244 va_end(ap);
245 sz = strlen(pcm->error);
246
247 if (errno)
248 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
249 ": %s", strerror(e));
250 return -1;
251}
252
Simon Wilsonbc03b622011-06-15 17:19:01 -0700253static unsigned int pcm_format_to_alsa(enum pcm_format format)
254{
255 switch (format) {
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400256
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500257 case PCM_FORMAT_S8:
258 return SNDRV_PCM_FORMAT_S8;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400259
Simon Wilsonbc03b622011-06-15 17:19:01 -0700260 default:
261 case PCM_FORMAT_S16_LE:
262 return SNDRV_PCM_FORMAT_S16_LE;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400263 case PCM_FORMAT_S16_BE:
264 return SNDRV_PCM_FORMAT_S16_BE;
265
266 case PCM_FORMAT_S24_LE:
267 return SNDRV_PCM_FORMAT_S24_LE;
268 case PCM_FORMAT_S24_BE:
269 return SNDRV_PCM_FORMAT_S24_BE;
270
271 case PCM_FORMAT_S24_3LE:
272 return SNDRV_PCM_FORMAT_S24_3LE;
273 case PCM_FORMAT_S24_3BE:
274 return SNDRV_PCM_FORMAT_S24_3BE;
275
276 case PCM_FORMAT_S32_LE:
277 return SNDRV_PCM_FORMAT_S32_LE;
278 case PCM_FORMAT_S32_BE:
279 return SNDRV_PCM_FORMAT_S32_BE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700280 };
281}
282
Taylor Holberton6d58e012016-10-01 18:32:30 -0400283/** Determines the number of bits occupied by a @ref pcm_format.
284 * @param format A PCM format.
285 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800286 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400287 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700288unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700289{
290 switch (format) {
291 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400292 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700293 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400294 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700295 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400296 case PCM_FORMAT_S24_3LE:
297 case PCM_FORMAT_S24_3BE:
298 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700299 default:
300 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400301 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700302 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400303 case PCM_FORMAT_S8:
304 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700305 };
306}
307
Taylor Holberton6d58e012016-10-01 18:32:30 -0400308/** Determines how many frames of a PCM can fit into a number of bytes.
309 * @param pcm A PCM handle.
310 * @param bytes The number of bytes.
311 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800312 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400313 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800314unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700315{
316 return bytes / (pcm->config.channels *
317 (pcm_format_to_bits(pcm->config.format) >> 3));
318}
319
Taylor Holberton6d58e012016-10-01 18:32:30 -0400320/** Determines how many bytes are occupied by a number of frames of a PCM.
321 * @param pcm A PCM handle.
322 * @param frames The number of frames of a PCM.
323 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800324 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400325 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800326unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700327{
328 return frames * pcm->config.channels *
329 (pcm_format_to_bits(pcm->config.format) >> 3);
330}
331
Taylor Holberton4f556062016-09-16 09:54:36 -0400332static int pcm_sync_ptr(struct pcm *pcm, int flags)
333{
Eric Laurent40b018e2011-06-18 10:10:23 -0700334 if (pcm->sync_ptr) {
335 pcm->sync_ptr->flags = flags;
336 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
337 return -1;
Taylor Holberton72e44222016-11-22 09:54:47 -0800338 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700339 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800340 return -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700341}
342
Taylor Holberton4f556062016-09-16 09:54:36 -0400343static int pcm_hw_mmap_status(struct pcm *pcm)
344{
Eric Laurent40b018e2011-06-18 10:10:23 -0700345 if (pcm->sync_ptr)
346 return 0;
347
348 int page_size = sysconf(_SC_PAGE_SIZE);
349 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
350 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
351 if (pcm->mmap_status == MAP_FAILED)
352 pcm->mmap_status = NULL;
353 if (!pcm->mmap_status)
354 goto mmap_error;
355
356 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
357 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
358 if (pcm->mmap_control == MAP_FAILED)
359 pcm->mmap_control = NULL;
360 if (!pcm->mmap_control) {
361 munmap(pcm->mmap_status, page_size);
362 pcm->mmap_status = NULL;
363 goto mmap_error;
364 }
365 pcm->mmap_control->avail_min = 1;
366
367 return 0;
368
369mmap_error:
370
371 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
372 if (!pcm->sync_ptr)
373 return -ENOMEM;
374 pcm->mmap_status = &pcm->sync_ptr->s.status;
375 pcm->mmap_control = &pcm->sync_ptr->c.control;
376 pcm->mmap_control->avail_min = 1;
377 pcm_sync_ptr(pcm, 0);
378
379 return 0;
380}
381
382static void pcm_hw_munmap_status(struct pcm *pcm) {
383 if (pcm->sync_ptr) {
384 free(pcm->sync_ptr);
385 pcm->sync_ptr = NULL;
386 } else {
387 int page_size = sysconf(_SC_PAGE_SIZE);
388 if (pcm->mmap_status)
389 munmap(pcm->mmap_status, page_size);
390 if (pcm->mmap_control)
391 munmap(pcm->mmap_control, page_size);
392 }
393 pcm->mmap_status = NULL;
394 pcm->mmap_control = NULL;
395}
396
Liam Girdwood6be28f12011-10-13 12:59:51 -0700397static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700398 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700399 unsigned int frames)
400{
401 int size_bytes = pcm_frames_to_bytes(pcm, frames);
402 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
403 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
404
405 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700406 if (pcm->flags & PCM_IN)
407 memcpy(buf + src_offset_bytes,
408 (char*)pcm->mmap_buffer + pcm_offset_bytes,
409 size_bytes);
410 else
411 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
412 buf + src_offset_bytes,
413 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700414 return 0;
415}
416
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700417static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700418 unsigned int offset, unsigned int size)
419{
420 void *pcm_areas;
421 int commit;
422 unsigned int pcm_offset, frames, count = 0;
423
424 while (size > 0) {
425 frames = size;
426 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700427 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700428 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
429 if (commit < 0) {
430 oops(pcm, commit, "failed to commit %d frames\n", frames);
431 return commit;
432 }
433
434 offset += commit;
435 count += commit;
436 size -= commit;
437 }
438 return count;
439}
440
Taylor Holberton6d58e012016-10-01 18:32:30 -0400441/** Returns available frames in pcm buffer and corresponding time stamp.
442 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
443 * otherwise the clock is CLOCK_REALTIME.
444 * For an input stream, frames available are frames ready for the application to read.
445 * For an output stream, frames available are the number of empty frames available for the application to write.
446 * Only available for PCMs opened with the @ref PCM_MMAP flag.
447 * @param pcm A PCM handle.
448 * @param avail The number of available frames
449 * @param tstamp The timestamp
450 * @return On success, zero is returned; on failure, negative one.
451 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700452int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
453 struct timespec *tstamp)
454{
455 int frames;
456 int rc;
457 snd_pcm_uframes_t hw_ptr;
458
459 if (!pcm_is_ready(pcm))
460 return -1;
461
462 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
463 if (rc < 0)
464 return -1;
465
Eric Laurent7db48582011-11-17 11:47:59 -0800466 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
467 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800468 return -1;
469
Eric Laurent40b018e2011-06-18 10:10:23 -0700470 *tstamp = pcm->mmap_status->tstamp;
471 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
472 return -1;
473
474 hw_ptr = pcm->mmap_status->hw_ptr;
475 if (pcm->flags & PCM_IN)
476 frames = hw_ptr - pcm->mmap_control->appl_ptr;
477 else
478 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
479
480 if (frames < 0)
481 return -1;
482
483 *avail = (unsigned int)frames;
484
485 return 0;
486}
487
Taylor Holberton6d58e012016-10-01 18:32:30 -0400488/** Writes audio samples to PCM.
489 * If the PCM has not been started, it is started in this function.
490 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
491 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
492 * @param pcm A PCM handle.
493 * @param data The audio sample array
494 * @param count The number of bytes occupied by the sample array.
495 * @return On success, this function returns zero; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800496 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400497 */
Mark Brown6bbe77a2012-02-10 22:07:09 +0000498int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700499{
500 struct snd_xferi x;
501
502 if (pcm->flags & PCM_IN)
503 return -EINVAL;
504
Mark Brown6bbe77a2012-02-10 22:07:09 +0000505 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700506 x.frames = count / (pcm->config.channels *
507 pcm_format_to_bits(pcm->config.format) / 8);
Taylor Holberton2386a422016-11-18 20:38:40 -0800508 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700509 for (;;) {
510 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530511 int prepare_error = pcm_prepare(pcm);
512 if (prepare_error)
513 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700514 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
515 return oops(pcm, errno, "cannot write initial data");
516 pcm->running = 1;
517 return 0;
518 }
519 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530520 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700521 pcm->running = 0;
522 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700523 /* we failed to make our window -- try to restart if we are
524 * allowed to do so. Otherwise, simply allow the EPIPE error to
525 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700526 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700527 if (pcm->flags & PCM_NORESTART)
528 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700529 continue;
530 }
531 return oops(pcm, errno, "cannot write stream data");
532 }
533 return 0;
534 }
535}
536
Taylor Holberton6d58e012016-10-01 18:32:30 -0400537/** Reads audio samples from PCM.
538 * If the PCM has not been started, it is started in this function.
539 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
540 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
541 * @param pcm A PCM handle.
542 * @param data The audio sample array
543 * @param count The number of bytes occupied by the sample array.
544 * @return On success, this function returns zero; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800545 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400546 */
Simon Wilson79d39652011-05-25 13:44:23 -0700547int pcm_read(struct pcm *pcm, void *data, unsigned int count)
548{
549 struct snd_xferi x;
550
551 if (!(pcm->flags & PCM_IN))
552 return -EINVAL;
553
554 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700555 x.frames = count / (pcm->config.channels *
556 pcm_format_to_bits(pcm->config.format) / 8);
Taylor Holberton2386a422016-11-18 20:38:40 -0800557 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700558 for (;;) {
559 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700560 if (pcm_start(pcm) < 0) {
561 fprintf(stderr, "start error");
562 return -errno;
563 }
Simon Wilson79d39652011-05-25 13:44:23 -0700564 }
565 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530566 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700567 pcm->running = 0;
568 if (errno == EPIPE) {
569 /* we failed to make our window -- try to restart */
570 pcm->underruns++;
571 continue;
572 }
573 return oops(pcm, errno, "cannot read stream data");
574 }
575 return 0;
576 }
577}
578
579static struct pcm bad_pcm = {
580 .fd = -1,
581};
582
Taylor Holberton6d58e012016-10-01 18:32:30 -0400583/** Gets the hardware parameters of a PCM, without created a PCM handle.
584 * @param card The card of the PCM.
585 * The default card is zero.
586 * @param device The device of the PCM.
587 * The default device is zero.
588 * @param flags Specifies whether the PCM is an input or output.
589 * May be one of the following:
590 * - @ref PCM_IN
591 * - @ref PCM_OUT
592 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800593 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400594 */
Simon Wilson43544882012-10-31 12:52:39 -0700595struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
596 unsigned int flags)
597{
598 struct snd_pcm_hw_params *params;
599 char fn[256];
600 int fd;
601
602 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
603 flags & PCM_IN ? 'c' : 'p');
604
605 fd = open(fn, O_RDWR);
606 if (fd < 0) {
607 fprintf(stderr, "cannot open device '%s'\n", fn);
608 goto err_open;
609 }
610
611 params = calloc(1, sizeof(struct snd_pcm_hw_params));
612 if (!params)
613 goto err_calloc;
614
615 param_init(params);
616 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
617 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
618 goto err_hw_refine;
619 }
620
621 close(fd);
622
623 return (struct pcm_params *)params;
624
625err_hw_refine:
626 free(params);
627err_calloc:
628 close(fd);
629err_open:
630 return NULL;
631}
632
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500633/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400634 * @param pcm_params Hardware parameters of a PCM.
635 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800636 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400637 */
Simon Wilson43544882012-10-31 12:52:39 -0700638void pcm_params_free(struct pcm_params *pcm_params)
639{
640 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
641
642 if (params)
643 free(params);
644}
645
646static int pcm_param_to_alsa(enum pcm_param param)
647{
648 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700649 case PCM_PARAM_ACCESS:
650 return SNDRV_PCM_HW_PARAM_ACCESS;
651 case PCM_PARAM_FORMAT:
652 return SNDRV_PCM_HW_PARAM_FORMAT;
653 case PCM_PARAM_SUBFORMAT:
654 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700655 case PCM_PARAM_SAMPLE_BITS:
656 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
657 break;
658 case PCM_PARAM_FRAME_BITS:
659 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
660 break;
661 case PCM_PARAM_CHANNELS:
662 return SNDRV_PCM_HW_PARAM_CHANNELS;
663 break;
664 case PCM_PARAM_RATE:
665 return SNDRV_PCM_HW_PARAM_RATE;
666 break;
667 case PCM_PARAM_PERIOD_TIME:
668 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
669 break;
670 case PCM_PARAM_PERIOD_SIZE:
671 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
672 break;
673 case PCM_PARAM_PERIOD_BYTES:
674 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
675 break;
676 case PCM_PARAM_PERIODS:
677 return SNDRV_PCM_HW_PARAM_PERIODS;
678 break;
679 case PCM_PARAM_BUFFER_TIME:
680 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
681 break;
682 case PCM_PARAM_BUFFER_SIZE:
683 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
684 break;
685 case PCM_PARAM_BUFFER_BYTES:
686 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
687 break;
688 case PCM_PARAM_TICK_TIME:
689 return SNDRV_PCM_HW_PARAM_TICK_TIME;
690 break;
691
692 default:
693 return -1;
694 }
695}
696
Taylor Holberton6d58e012016-10-01 18:32:30 -0400697/** Gets a mask from a PCM's hardware parameters.
698 * @param pcm_params A PCM's hardware parameters.
699 * @param param The parameter to get.
700 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
701 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800702 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400703 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800704const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700705 enum pcm_param param)
706{
707 int p;
708 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
709 if (params == NULL) {
710 return NULL;
711 }
712
713 p = pcm_param_to_alsa(param);
714 if (p < 0 || !param_is_mask(p)) {
715 return NULL;
716 }
717
Taylor Holberton2f387d22016-12-01 15:58:16 -0800718 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700719}
720
Taylor Holberton17a10242016-11-23 13:18:24 -0800721/** Get the minimum of a specified PCM parameter.
722 * @param pcm_params A PCM parameters structure.
723 * @param param The specified parameter to get the minimum of.
724 * @returns On success, the parameter minimum.
725 * On failure, zero.
726 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800727unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700728 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_min(params, p);
741}
742
Taylor Holberton17a10242016-11-23 13:18:24 -0800743/** Get the maximum of a specified PCM parameter.
744 * @param pcm_params A PCM parameters structure.
745 * @param param The specified parameter to get the maximum of.
746 * @returns On success, the parameter maximum.
747 * On failure, zero.
748 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800749unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700750 enum pcm_param param)
751{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800752 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700753 int p;
754
755 if (!params)
756 return 0;
757
758 p = pcm_param_to_alsa(param);
759 if (p < 0)
760 return 0;
761
762 return param_get_max(params, p);
763}
764
Taylor Holberton6d58e012016-10-01 18:32:30 -0400765/** Closes a PCM returned by @ref pcm_open.
766 * @param pcm A PCM returned by @ref pcm_open.
767 * May not be NULL.
768 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800769 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400770 */
Simon Wilson79d39652011-05-25 13:44:23 -0700771int pcm_close(struct pcm *pcm)
772{
773 if (pcm == &bad_pcm)
774 return 0;
775
Eric Laurent40b018e2011-06-18 10:10:23 -0700776 pcm_hw_munmap_status(pcm);
777
Liam Girdwood6be28f12011-10-13 12:59:51 -0700778 if (pcm->flags & PCM_MMAP) {
779 pcm_stop(pcm);
780 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
781 }
782
Simon Wilson79d39652011-05-25 13:44:23 -0700783 if (pcm->fd >= 0)
784 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530785 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700786 pcm->running = 0;
787 pcm->buffer_size = 0;
788 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700789 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700790 return 0;
791}
792
Taylor Holberton6d58e012016-10-01 18:32:30 -0400793/** Opens a PCM.
794 * @param card The card that the pcm belongs to.
795 * The default card is zero.
796 * @param device The device that the pcm belongs to.
797 * The default device is zero.
798 * @param flags Specify characteristics and functionality about the pcm.
799 * May be a bitwise AND of the following:
800 * - @ref PCM_IN
801 * - @ref PCM_OUT
802 * - @ref PCM_MMAP
803 * - @ref PCM_NOIRQ
804 * - @ref PCM_MONOTONIC
805 * @param config The hardware and software parameters to open the PCM with.
806 * @returns On success, returns an initialized pcm, ready for reading or writing.
807 * On error, returns NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800808 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400809 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700810struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800811 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700812{
Simon Wilson79d39652011-05-25 13:44:23 -0700813 struct pcm *pcm;
814 struct snd_pcm_info info;
815 struct snd_pcm_hw_params params;
816 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700817 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700818 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700819
820 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400821 if (!pcm)
822 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700823
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400824 if (config == NULL) {
825 config = &pcm->config;
Taylor Holberton94803b02016-12-01 16:07:14 -0800826 pcm->config.channels = 2;
827 pcm->config.rate = 48000;
828 pcm->config.period_size = 1024;
829 pcm->config.period_count = 4;
830 pcm->config.format = PCM_FORMAT_S16_LE;
831 pcm->config.start_threshold = config->period_count * config->period_size;
832 pcm->config.stop_threshold = config->period_count * config->period_size;
833 pcm->config.silence_threshold = 0;
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400834 } else {
835 pcm->config = *config;
836 }
Simon Wilson79d39652011-05-25 13:44:23 -0700837
Simon Wilson1bd580f2011-06-02 15:58:41 -0700838 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
839 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700840
841 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700842 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700843 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700844 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700845 return pcm;
846 }
847
848 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700849 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700850 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700851 }
David Wagner4cddf192014-04-02 15:12:54 +0200852 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700853
854 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700855 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
856 pcm_format_to_alsa(config->format));
857 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
858 SNDRV_PCM_SUBFORMAT_STD);
859 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
860 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
861 pcm_format_to_bits(config->format));
862 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
863 pcm_format_to_bits(config->format) * config->channels);
864 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
865 config->channels);
866 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
867 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
868
Liam Girdwood6be28f12011-10-13 12:59:51 -0700869 if (flags & PCM_NOIRQ) {
870
871 if (!(flags & PCM_MMAP)) {
872 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
873 goto fail;
874 }
875
876 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
877 pcm->noirq_frames_per_msec = config->rate / 1000;
878 }
879
880 if (flags & PCM_MMAP)
881 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
882 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
883 else
884 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
885 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
886
Simon Wilson79d39652011-05-25 13:44:23 -0700887 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
888 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700889 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700890 }
891
Liam Girdwood6be28f12011-10-13 12:59:51 -0700892 /* get our refined hw_params */
Taylor Holberton94803b02016-12-01 16:07:14 -0800893 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
894 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700895 pcm->buffer_size = config->period_count * config->period_size;
896
897 if (flags & PCM_MMAP) {
898 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
899 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
900 if (pcm->mmap_buffer == MAP_FAILED) {
901 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
902 pcm_frames_to_bytes(pcm, pcm->buffer_size));
903 goto fail_close;
904 }
905 }
906
907
Simon Wilson79d39652011-05-25 13:44:23 -0700908 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700909 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700910 sparams.period_step = 1;
911 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700912
Eric Laurent93e7b672012-08-22 16:18:14 -0700913 if (!config->start_threshold) {
914 if (pcm->flags & PCM_IN)
915 pcm->config.start_threshold = sparams.start_threshold = 1;
916 else
917 pcm->config.start_threshold = sparams.start_threshold =
918 config->period_count * config->period_size / 2;
919 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700920 sparams.start_threshold = config->start_threshold;
921
Liam Girdwood6be28f12011-10-13 12:59:51 -0700922 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800923 if (!config->stop_threshold) {
924 if (pcm->flags & PCM_IN)
925 pcm->config.stop_threshold = sparams.stop_threshold =
926 config->period_count * config->period_size * 10;
927 else
928 pcm->config.stop_threshold = sparams.stop_threshold =
929 config->period_count * config->period_size;
930 }
John Grossman3bb114a2011-07-21 10:59:55 -0700931 else
932 sparams.stop_threshold = config->stop_threshold;
933
Simon Wilson79d39652011-05-25 13:44:23 -0700934 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
935 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700936 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700937 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700938
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600939 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700940 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700941
942 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
943 oops(pcm, errno, "cannot set sw params");
944 goto fail;
945 }
946
Eric Laurent40b018e2011-06-18 10:10:23 -0700947 rc = pcm_hw_mmap_status(pcm);
948 if (rc < 0) {
949 oops(pcm, rc, "mmap status failed");
950 goto fail;
951 }
952
Glenn Kasten81012402013-08-22 15:11:48 -0700953#ifdef SNDRV_PCM_IOCTL_TTSTAMP
954 if (pcm->flags & PCM_MONOTONIC) {
955 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
956 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
957 if (rc < 0) {
958 oops(pcm, rc, "cannot set timestamp type");
959 goto fail;
960 }
961 }
962#endif
963
Simon Wilson79d39652011-05-25 13:44:23 -0700964 pcm->underruns = 0;
965 return pcm;
966
967fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700968 if (flags & PCM_MMAP)
969 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
970fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700971 close(pcm->fd);
972 pcm->fd = -1;
973 return pcm;
974}
975
Taylor Holberton6d58e012016-10-01 18:32:30 -0400976/** Checks if a PCM file has been opened without error.
977 * @param pcm A PCM handle.
978 * @return If a PCM's file descriptor is not valid, it returns zero.
979 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800980 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400981 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800982int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700983{
984 return pcm->fd >= 0;
985}
Simon Wilsond6458e62011-06-21 14:58:11 -0700986
Taylor Holberton6d58e012016-10-01 18:32:30 -0400987/** Prepares a PCM, if it has not been prepared already.
988 * @param pcm A PCM handle.
989 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800990 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400991 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530992int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700993{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530994 if (pcm->prepared)
995 return 0;
996
Simon Wilsond6458e62011-06-21 14:58:11 -0700997 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
998 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700999
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301000 pcm->prepared = 1;
1001 return 0;
1002}
1003
Taylor Holberton6d58e012016-10-01 18:32:30 -04001004/** Starts a PCM.
1005 * If the PCM has not been prepared,
1006 * it is prepared in this function.
1007 * @param pcm A PCM handle.
1008 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001009 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001010 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301011int pcm_start(struct pcm *pcm)
1012{
1013 int prepare_error = pcm_prepare(pcm);
1014 if (prepare_error)
1015 return prepare_error;
1016
Liam Girdwood6be28f12011-10-13 12:59:51 -07001017 if (pcm->flags & PCM_MMAP)
1018 pcm_sync_ptr(pcm, 0);
1019
Simon Wilsond6458e62011-06-21 14:58:11 -07001020 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1021 return oops(pcm, errno, "cannot start channel");
1022
Liam Girdwood6be28f12011-10-13 12:59:51 -07001023 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001024 return 0;
1025}
1026
Taylor Holberton6d58e012016-10-01 18:32:30 -04001027/** Stops a PCM.
1028 * @param pcm A PCM handle.
1029 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001030 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001031 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001032int pcm_stop(struct pcm *pcm)
1033{
1034 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1035 return oops(pcm, errno, "cannot stop channel");
1036
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301037 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001038 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001039 return 0;
1040}
1041
Liam Girdwood6be28f12011-10-13 12:59:51 -07001042static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1043{
1044 int avail;
1045
1046 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1047
1048 if (avail < 0)
1049 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001050 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001051 avail -= pcm->boundary;
1052
1053 return avail;
1054}
1055
1056static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1057{
1058 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1059 if (avail < 0)
1060 avail += pcm->boundary;
1061 return avail;
1062}
1063
1064static inline int pcm_mmap_avail(struct pcm *pcm)
1065{
1066 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1067 if (pcm->flags & PCM_IN)
1068 return pcm_mmap_capture_avail(pcm);
1069 else
1070 return pcm_mmap_playback_avail(pcm);
1071}
1072
1073static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1074{
1075 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1076 appl_ptr += frames;
1077
1078 /* check for boundary wrap */
1079 if (appl_ptr > pcm->boundary)
1080 appl_ptr -= pcm->boundary;
1081 pcm->mmap_control->appl_ptr = appl_ptr;
1082}
1083
1084int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1085 unsigned int *frames)
1086{
1087 unsigned int continuous, copy_frames, avail;
1088
1089 /* return the mmap buffer */
1090 *areas = pcm->mmap_buffer;
1091
1092 /* and the application offset in frames */
1093 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1094
1095 avail = pcm_mmap_avail(pcm);
1096 if (avail > pcm->buffer_size)
1097 avail = pcm->buffer_size;
1098 continuous = pcm->buffer_size - *offset;
1099
1100 /* we can only copy frames if the are availabale and continuos */
1101 copy_frames = *frames;
1102 if (copy_frames > avail)
1103 copy_frames = avail;
1104 if (copy_frames > continuous)
1105 copy_frames = continuous;
1106 *frames = copy_frames;
1107
1108 return 0;
1109}
1110
1111int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1112{
Taylor Holberton72e44222016-11-22 09:54:47 -08001113 int ret;
1114
Taylor Holberton73466c02016-10-01 12:51:59 -04001115 /* not used */
1116 (void) offset;
1117
Liam Girdwood6be28f12011-10-13 12:59:51 -07001118 /* update the application pointer in userspace and kernel */
1119 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001120 ret = pcm_sync_ptr(pcm, 0);
1121 if (ret != 0)
1122 return ret;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001123
1124 return frames;
1125}
1126
1127int pcm_avail_update(struct pcm *pcm)
1128{
1129 pcm_sync_ptr(pcm, 0);
1130 return pcm_mmap_avail(pcm);
1131}
1132
1133int pcm_state(struct pcm *pcm)
1134{
1135 int err = pcm_sync_ptr(pcm, 0);
1136 if (err < 0)
1137 return err;
1138
1139 return pcm->mmap_status->state;
1140}
1141
Taylor Holberton17a10242016-11-23 13:18:24 -08001142/** Waits for frames to be available for read or write operations.
1143 * @param pcm A PCM handle.
1144 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1145 * @returns If frames became available, one is returned.
1146 * If a timeout occured, zero is returned.
1147 * If an error occured, a negative number is returned.
1148 * @ingroup libtinyalsa-pcm
1149 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001150int pcm_wait(struct pcm *pcm, int timeout)
1151{
1152 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001153 int err;
1154
1155 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001156 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001157
1158 do {
1159 /* let's wait for avail or timeout */
1160 err = poll(&pfd, 1, timeout);
1161 if (err < 0)
1162 return -errno;
1163
1164 /* timeout ? */
1165 if (err == 0)
1166 return 0;
1167
1168 /* have we been interrupted ? */
1169 if (errno == -EINTR)
1170 continue;
1171
1172 /* check for any errors */
1173 if (pfd.revents & (POLLERR | POLLNVAL)) {
1174 switch (pcm_state(pcm)) {
1175 case PCM_STATE_XRUN:
1176 return -EPIPE;
1177 case PCM_STATE_SUSPENDED:
1178 return -ESTRPIPE;
1179 case PCM_STATE_DISCONNECTED:
1180 return -ENODEV;
1181 default:
1182 return -EIO;
1183 }
1184 }
1185 /* poll again if fd not ready for IO */
1186 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1187
1188 return 1;
1189}
1190
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001191int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001192{
1193 int err = 0, frames, avail;
1194 unsigned int offset = 0, count;
1195
1196 if (bytes == 0)
1197 return 0;
1198
1199 count = pcm_bytes_to_frames(pcm, bytes);
1200
1201 while (count > 0) {
1202
1203 /* get the available space for writing new frames */
1204 avail = pcm_avail_update(pcm);
1205 if (avail < 0) {
1206 fprintf(stderr, "cannot determine available mmap frames");
1207 return err;
1208 }
1209
1210 /* start the audio if we reach the threshold */
1211 if (!pcm->running &&
1212 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1213 if (pcm_start(pcm) < 0) {
1214 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1215 (unsigned int)pcm->mmap_status->hw_ptr,
1216 (unsigned int)pcm->mmap_control->appl_ptr,
1217 avail);
1218 return -errno;
1219 }
1220 }
1221
1222 /* sleep until we have space to write new frames */
1223 if (pcm->running &&
1224 (unsigned int)avail < pcm->mmap_control->avail_min) {
1225 int time = -1;
1226
1227 if (pcm->flags & PCM_NOIRQ)
1228 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1229 / pcm->noirq_frames_per_msec;
1230
1231 err = pcm_wait(pcm, time);
1232 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301233 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001234 pcm->running = 0;
1235 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1236 (unsigned int)pcm->mmap_status->hw_ptr,
1237 (unsigned int)pcm->mmap_control->appl_ptr,
1238 avail);
1239 pcm->mmap_control->appl_ptr = 0;
1240 return err;
1241 }
1242 continue;
1243 }
1244
1245 frames = count;
1246 if (frames > avail)
1247 frames = avail;
1248
1249 if (!frames)
1250 break;
1251
1252 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001253 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001254 if (frames < 0) {
1255 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1256 (unsigned int)pcm->mmap_status->hw_ptr,
1257 (unsigned int)pcm->mmap_control->appl_ptr,
1258 avail);
1259 return frames;
1260 }
1261
1262 offset += frames;
1263 count -= frames;
1264 }
1265
Liam Girdwood6be28f12011-10-13 12:59:51 -07001266 return 0;
1267}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001268
1269int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1270{
1271 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1272 return -ENOSYS;
1273
1274 return pcm_mmap_transfer(pcm, (void *)data, count);
1275}
1276
1277int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1278{
1279 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1280 return -ENOSYS;
1281
1282 return pcm_mmap_transfer(pcm, data, count);
1283}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301284
Taylor Holberton17a10242016-11-23 13:18:24 -08001285/** Gets the delay of the PCM, in terms of frames.
1286 * @param pcm A PCM handle.
1287 * @returns On success, the delay of the PCM.
1288 * On failure, a negative number.
1289 * @ingroup libtinyalsa-pcm
1290 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301291long pcm_get_delay(struct pcm *pcm)
1292{
1293 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1294 return -1;
1295
1296 return pcm->pcm_delay;
1297}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001298