blob: 43e8279abd9385eae830950dfc38254c4298823f [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>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070061#include <tinyalsa/limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070062
Taylor Holberton1f741562018-11-28 16:33:24 -050063#ifndef PARAM_MAX
Simon Wilson79d39652011-05-25 13:44:23 -070064#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Taylor Holberton1f741562018-11-28 16:33:24 -050065#endif /* PARAM_MAX */
66
67#ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
Liam Girdwood6be28f12011-10-13 12:59:51 -070068#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Taylor Holberton1f741562018-11-28 16:33:24 -050069#endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */
Simon Wilson79d39652011-05-25 13:44:23 -070070
71static inline int param_is_mask(int p)
72{
73 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
74 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
75}
76
77static inline int param_is_interval(int p)
78{
79 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
80 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
81}
82
Taylor Holberton2f387d22016-12-01 15:58:16 -080083static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
84{
Taylor Holberton25976dc2017-04-10 11:46:40 -070085 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080086}
87
Simon Wilson79d39652011-05-25 13:44:23 -070088static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
89{
90 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
91}
92
93static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
94{
95 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
96}
97
98static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
99{
100 if (bit >= SNDRV_MASK_MAX)
101 return;
102 if (param_is_mask(n)) {
103 struct snd_mask *m = param_to_mask(p, n);
104 m->bits[0] = 0;
105 m->bits[1] = 0;
106 m->bits[bit >> 5] |= (1 << (bit & 31));
107 }
108}
109
110static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
111{
112 if (param_is_interval(n)) {
113 struct snd_interval *i = param_to_interval(p, n);
114 i->min = val;
115 }
116}
117
Taylor Holberton2f387d22016-12-01 15:58:16 -0800118static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700119{
120 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800121 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700122 return i->min;
123 }
124 return 0;
125}
126
Taylor Holberton2f387d22016-12-01 15:58:16 -0800127static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700128{
129 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800130 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700131 return i->max;
132 }
133 return 0;
134}
135
Simon Wilson79d39652011-05-25 13:44:23 -0700136static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
137{
138 if (param_is_interval(n)) {
139 struct snd_interval *i = param_to_interval(p, n);
140 i->min = val;
141 i->max = val;
142 i->integer = 1;
143 }
144}
145
Liam Girdwood6be28f12011-10-13 12:59:51 -0700146static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
147{
148 if (param_is_interval(n)) {
149 struct snd_interval *i = param_to_interval(p, n);
150 if (i->integer)
151 return i->max;
152 }
153 return 0;
154}
155
Simon Wilson79d39652011-05-25 13:44:23 -0700156static void param_init(struct snd_pcm_hw_params *p)
157{
158 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700159
Simon Wilson79d39652011-05-25 13:44:23 -0700160 memset(p, 0, sizeof(*p));
161 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
162 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
163 struct snd_mask *m = param_to_mask(p, n);
164 m->bits[0] = ~0;
165 m->bits[1] = ~0;
166 }
167 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
168 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
169 struct snd_interval *i = param_to_interval(p, n);
170 i->min = 0;
171 i->max = ~0;
172 }
Simon Wilson43544882012-10-31 12:52:39 -0700173 p->rmask = ~0U;
174 p->cmask = 0;
175 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700176}
177
Taylor Holberton861da7a2017-04-10 12:05:51 -0700178static unsigned int pcm_format_to_alsa(enum pcm_format format)
179{
180 switch (format) {
181
182 case PCM_FORMAT_S8:
183 return SNDRV_PCM_FORMAT_S8;
184
185 default:
186 case PCM_FORMAT_S16_LE:
187 return SNDRV_PCM_FORMAT_S16_LE;
188 case PCM_FORMAT_S16_BE:
189 return SNDRV_PCM_FORMAT_S16_BE;
190
191 case PCM_FORMAT_S24_LE:
192 return SNDRV_PCM_FORMAT_S24_LE;
193 case PCM_FORMAT_S24_BE:
194 return SNDRV_PCM_FORMAT_S24_BE;
195
196 case PCM_FORMAT_S24_3LE:
197 return SNDRV_PCM_FORMAT_S24_3LE;
198 case PCM_FORMAT_S24_3BE:
199 return SNDRV_PCM_FORMAT_S24_3BE;
200
201 case PCM_FORMAT_S32_LE:
202 return SNDRV_PCM_FORMAT_S32_LE;
203 case PCM_FORMAT_S32_BE:
204 return SNDRV_PCM_FORMAT_S32_BE;
205 };
206}
207
Simon Wilson79d39652011-05-25 13:44:23 -0700208#define PCM_ERROR_MAX 128
209
Taylor Holberton6d58e012016-10-01 18:32:30 -0400210/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800211 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400212 */
Simon Wilson79d39652011-05-25 13:44:23 -0700213struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400214 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700215 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400216 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700217 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400218 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700219 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400220 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530221 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400222 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700223 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400224 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700225 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800226 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700227 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400228 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700229 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400230 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700231 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700232 struct snd_pcm_mmap_status *mmap_status;
233 struct snd_pcm_mmap_control *mmap_control;
234 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700235 void *mmap_buffer;
236 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800237 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530238 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800239 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200240 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700241};
242
Taylor Holberton861da7a2017-04-10 12:05:51 -0700243static int oops(struct pcm *pcm, int e, const char *fmt, ...)
244{
245 va_list ap;
246 int sz;
247
248 va_start(ap, fmt);
249 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
250 va_end(ap);
251 sz = strlen(pcm->error);
252
253 if (errno)
254 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
255 ": %s", strerror(e));
256 return -1;
257}
258
Taylor Holberton6d58e012016-10-01 18:32:30 -0400259/** Gets the buffer size of the PCM.
260 * @param pcm A PCM handle.
261 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800262 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400263 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800264unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700265{
266 return pcm->buffer_size;
267}
268
Taylor Holberton77979a82016-12-01 20:04:04 -0800269/** Gets the channel count of the PCM.
270 * @param pcm A PCM handle.
271 * @return The channel count of the PCM.
272 * @ingroup libtinyalsa-pcm
273 */
274unsigned int pcm_get_channels(const struct pcm *pcm)
275{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700276 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800277}
278
Taylor Holberton08bb5902017-04-10 11:45:44 -0700279/** Gets the PCM configuration.
280 * @param pcm A PCM handle.
281 * @return The PCM configuration.
282 * This function only returns NULL if
283 * @p pcm is NULL.
284 * @ingroup libtinyalsa-pcm
285 * */
286const struct pcm_config * pcm_get_config(const struct pcm *pcm)
287{
288 if (pcm == NULL)
289 return NULL;
290 return &pcm->config;
291}
292
Taylor Holberton77979a82016-12-01 20:04:04 -0800293/** Gets the rate of the PCM.
294 * The rate is given in frames per second.
295 * @param pcm A PCM handle.
296 * @return The rate of the PCM.
297 * @ingroup libtinyalsa-pcm
298 */
299unsigned int pcm_get_rate(const struct pcm *pcm)
300{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700301 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800302}
303
304/** Gets the format of the PCM.
305 * @param pcm A PCM handle.
306 * @return The format of the PCM.
307 * @ingroup libtinyalsa-pcm
308 */
309enum pcm_format pcm_get_format(const struct pcm *pcm)
310{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700311 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800312}
313
Taylor Holberton6d58e012016-10-01 18:32:30 -0400314/** Gets the file descriptor of the PCM.
315 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800316 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400317 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800318 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400319 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800320int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400321{
322 return pcm->fd;
323}
324
Taylor Holberton6d58e012016-10-01 18:32:30 -0400325/** Gets the error message for the last error that occured.
326 * If no error occured and this function is called, the results are undefined.
327 * @param pcm A PCM handle.
328 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800329 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400330 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800331const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700332{
333 return pcm->error;
334}
335
Taylor Holberton861da7a2017-04-10 12:05:51 -0700336/** Sets the PCM configuration.
337 * @param pcm A PCM handle.
338 * @param config The configuration to use for the
339 * PCM. This parameter may be NULL, in which case
340 * the default configuration is used.
341 * @returns Zero on success, a negative errno value
342 * on failure.
343 * @ingroup libtinyalsa-pcm
344 * */
345int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
346{
347 if (pcm == NULL)
348 return -EFAULT;
349 else if (config == NULL) {
350 config = &pcm->config;
351 pcm->config.channels = 2;
352 pcm->config.rate = 48000;
353 pcm->config.period_size = 1024;
354 pcm->config.period_count = 4;
355 pcm->config.format = PCM_FORMAT_S16_LE;
356 pcm->config.start_threshold = config->period_count * config->period_size;
357 pcm->config.stop_threshold = config->period_count * config->period_size;
358 pcm->config.silence_threshold = 0;
359 } else
360 pcm->config = *config;
361
362 struct snd_pcm_hw_params params;
363 param_init(&params);
364 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
365 pcm_format_to_alsa(config->format));
366 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
367 SNDRV_PCM_SUBFORMAT_STD);
368 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
369 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
370 pcm_format_to_bits(config->format));
371 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
372 pcm_format_to_bits(config->format) * config->channels);
373 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
374 config->channels);
375 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
376 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
377
378 if (pcm->flags & PCM_NOIRQ) {
379
380 if (!(pcm->flags & PCM_MMAP)) {
381 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
382 return -EINVAL;
383 }
384
385 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
386 pcm->noirq_frames_per_msec = config->rate / 1000;
387 }
388
389 if (pcm->flags & PCM_MMAP)
390 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
391 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
392 else
393 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
394 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
395
396 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
397 int errno_copy = errno;
398 oops(pcm, -errno, "cannot set hw params");
399 return -errno_copy;
400 }
401
402 /* get our refined hw_params */
403 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
404 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
405 pcm->buffer_size = config->period_count * config->period_size;
406
407 if (pcm->flags & PCM_MMAP) {
408 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
409 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
410 if (pcm->mmap_buffer == MAP_FAILED) {
411 int errno_copy = errno;
412 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
413 pcm_frames_to_bytes(pcm, pcm->buffer_size));
414 return -errno_copy;
415 }
416 }
417
418 struct snd_pcm_sw_params sparams;
419 memset(&sparams, 0, sizeof(sparams));
420 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
421 sparams.period_step = 1;
422 sparams.avail_min = 1;
423
424 if (!config->start_threshold) {
425 if (pcm->flags & PCM_IN)
426 pcm->config.start_threshold = sparams.start_threshold = 1;
427 else
428 pcm->config.start_threshold = sparams.start_threshold =
429 config->period_count * config->period_size / 2;
430 } else
431 sparams.start_threshold = config->start_threshold;
432
433 /* pick a high stop threshold - todo: does this need further tuning */
434 if (!config->stop_threshold) {
435 if (pcm->flags & PCM_IN)
436 pcm->config.stop_threshold = sparams.stop_threshold =
437 config->period_count * config->period_size * 10;
438 else
439 pcm->config.stop_threshold = sparams.stop_threshold =
440 config->period_count * config->period_size;
441 }
442 else
443 sparams.stop_threshold = config->stop_threshold;
444
445 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
446 sparams.silence_size = 0;
447 sparams.silence_threshold = config->silence_threshold;
448 pcm->boundary = sparams.boundary = pcm->buffer_size;
449
450 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
451 pcm->boundary *= 2;
452
453 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
454 int errno_copy = errno;
455 oops(pcm, -errno, "cannot set sw params");
456 return -errno_copy;
457 }
458
459 return 0;
460}
461
Taylor Holberton6d58e012016-10-01 18:32:30 -0400462/** Gets the subdevice on which the pcm has been opened.
463 * @param pcm A PCM handle.
464 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800465unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200466{
467 return pcm->subdevice;
468}
469
Taylor Holberton6d58e012016-10-01 18:32:30 -0400470/** Determines the number of bits occupied by a @ref pcm_format.
471 * @param format A PCM format.
472 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800473 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400474 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700475unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700476{
477 switch (format) {
478 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400479 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700480 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400481 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700482 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400483 case PCM_FORMAT_S24_3LE:
484 case PCM_FORMAT_S24_3BE:
485 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700486 default:
487 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400488 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700489 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400490 case PCM_FORMAT_S8:
491 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700492 };
493}
494
Taylor Holberton6d58e012016-10-01 18:32:30 -0400495/** Determines how many frames of a PCM can fit into a number of bytes.
496 * @param pcm A PCM handle.
497 * @param bytes The number of bytes.
498 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800499 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400500 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800501unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700502{
503 return bytes / (pcm->config.channels *
504 (pcm_format_to_bits(pcm->config.format) >> 3));
505}
506
Taylor Holberton6d58e012016-10-01 18:32:30 -0400507/** Determines how many bytes are occupied by a number of frames of a PCM.
508 * @param pcm A PCM handle.
509 * @param frames The number of frames of a PCM.
510 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800511 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400512 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800513unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700514{
515 return frames * pcm->config.channels *
516 (pcm_format_to_bits(pcm->config.format) >> 3);
517}
518
Taylor Holberton4f556062016-09-16 09:54:36 -0400519static int pcm_sync_ptr(struct pcm *pcm, int flags)
520{
Eric Laurent40b018e2011-06-18 10:10:23 -0700521 if (pcm->sync_ptr) {
522 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800523 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
524 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700525 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800526 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800527 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700528 }
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200529 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700530}
531
Taylor Holberton4f556062016-09-16 09:54:36 -0400532static int pcm_hw_mmap_status(struct pcm *pcm)
533{
Eric Laurent40b018e2011-06-18 10:10:23 -0700534 if (pcm->sync_ptr)
535 return 0;
536
537 int page_size = sysconf(_SC_PAGE_SIZE);
538 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
539 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
540 if (pcm->mmap_status == MAP_FAILED)
541 pcm->mmap_status = NULL;
542 if (!pcm->mmap_status)
543 goto mmap_error;
544
545 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
546 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
547 if (pcm->mmap_control == MAP_FAILED)
548 pcm->mmap_control = NULL;
549 if (!pcm->mmap_control) {
550 munmap(pcm->mmap_status, page_size);
551 pcm->mmap_status = NULL;
552 goto mmap_error;
553 }
554 pcm->mmap_control->avail_min = 1;
555
556 return 0;
557
558mmap_error:
559
560 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
561 if (!pcm->sync_ptr)
562 return -ENOMEM;
563 pcm->mmap_status = &pcm->sync_ptr->s.status;
564 pcm->mmap_control = &pcm->sync_ptr->c.control;
565 pcm->mmap_control->avail_min = 1;
566 pcm_sync_ptr(pcm, 0);
567
568 return 0;
569}
570
571static void pcm_hw_munmap_status(struct pcm *pcm) {
572 if (pcm->sync_ptr) {
573 free(pcm->sync_ptr);
574 pcm->sync_ptr = NULL;
575 } else {
576 int page_size = sysconf(_SC_PAGE_SIZE);
577 if (pcm->mmap_status)
578 munmap(pcm->mmap_status, page_size);
579 if (pcm->mmap_control)
580 munmap(pcm->mmap_control, page_size);
581 }
582 pcm->mmap_status = NULL;
583 pcm->mmap_control = NULL;
584}
585
Liam Girdwood6be28f12011-10-13 12:59:51 -0700586static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700587 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700588 unsigned int frames)
589{
590 int size_bytes = pcm_frames_to_bytes(pcm, frames);
591 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
592 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
593
594 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700595 if (pcm->flags & PCM_IN)
596 memcpy(buf + src_offset_bytes,
597 (char*)pcm->mmap_buffer + pcm_offset_bytes,
598 size_bytes);
599 else
600 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
601 buf + src_offset_bytes,
602 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700603 return 0;
604}
605
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700606static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700607 unsigned int offset, unsigned int size)
608{
609 void *pcm_areas;
610 int commit;
611 unsigned int pcm_offset, frames, count = 0;
612
613 while (size > 0) {
614 frames = size;
615 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700616 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700617 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
618 if (commit < 0) {
619 oops(pcm, commit, "failed to commit %d frames\n", frames);
620 return commit;
621 }
622
623 offset += commit;
624 count += commit;
625 size -= commit;
626 }
627 return count;
628}
629
Taylor Holberton6d58e012016-10-01 18:32:30 -0400630/** Returns available frames in pcm buffer and corresponding time stamp.
631 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
632 * otherwise the clock is CLOCK_REALTIME.
633 * For an input stream, frames available are frames ready for the application to read.
634 * For an output stream, frames available are the number of empty frames available for the application to write.
635 * Only available for PCMs opened with the @ref PCM_MMAP flag.
636 * @param pcm A PCM handle.
637 * @param avail The number of available frames
638 * @param tstamp The timestamp
639 * @return On success, zero is returned; on failure, negative one.
640 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700641int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
642 struct timespec *tstamp)
643{
644 int frames;
645 int rc;
646 snd_pcm_uframes_t hw_ptr;
647
648 if (!pcm_is_ready(pcm))
649 return -1;
650
651 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
652 if (rc < 0)
653 return -1;
654
Eric Laurent7db48582011-11-17 11:47:59 -0800655 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
656 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800657 return -1;
658
Eric Laurent40b018e2011-06-18 10:10:23 -0700659 *tstamp = pcm->mmap_status->tstamp;
660 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
661 return -1;
662
663 hw_ptr = pcm->mmap_status->hw_ptr;
664 if (pcm->flags & PCM_IN)
665 frames = hw_ptr - pcm->mmap_control->appl_ptr;
666 else
667 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
668
669 if (frames < 0)
670 return -1;
671
672 *avail = (unsigned int)frames;
673
674 return 0;
675}
676
Taylor Holberton6d58e012016-10-01 18:32:30 -0400677/** Writes audio samples to PCM.
678 * If the PCM has not been started, it is started in this function.
679 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
680 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
681 * @param pcm A PCM handle.
682 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800683 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700684 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
685 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800686 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800687 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400688 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800689int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700690{
691 struct snd_xferi x;
692
693 if (pcm->flags & PCM_IN)
694 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700695#if UINT_MAX > TINYALSA_FRAMES_MAX
696 if (frame_count > TINYALSA_FRAMES_MAX)
697 return -EINVAL;
698#endif
699 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700700 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700701
Mark Brown6bbe77a2012-02-10 22:07:09 +0000702 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800703 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800704 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700705 for (;;) {
706 if (!pcm->running) {
Simon Wilson79d39652011-05-25 13:44:23 -0700707 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
708 return oops(pcm, errno, "cannot write initial data");
709 pcm->running = 1;
Miguel GAIOf6fe5232018-04-10 06:35:07 +0200710 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700711 }
712 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530713 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700714 pcm->running = 0;
715 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700716 /* we failed to make our window -- try to restart if we are
717 * allowed to do so. Otherwise, simply allow the EPIPE error to
718 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700719 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700720 if (pcm->flags & PCM_NORESTART)
721 return -EPIPE;
Ricardo Biehl Pasquali7c40a9e2018-09-09 21:40:01 -0300722 if (pcm_prepare(pcm))
723 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700724 continue;
725 }
726 return oops(pcm, errno, "cannot write stream data");
727 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800728 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700729 }
730}
731
Taylor Holberton6d58e012016-10-01 18:32:30 -0400732/** Reads audio samples from PCM.
733 * If the PCM has not been started, it is started in this function.
734 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
735 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
736 * @param pcm A PCM handle.
737 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800738 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700739 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
740 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800741 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800742 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400743 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800744int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700745{
746 struct snd_xferi x;
747
748 if (!(pcm->flags & PCM_IN))
749 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700750#if UINT_MAX > TINYALSA_FRAMES_MAX
751 if (frame_count > TINYALSA_FRAMES_MAX)
752 return -EINVAL;
753#endif
754 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700755 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700756
757 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800758 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800759 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700760 for (;;) {
Taylor Holberton1137fc72017-04-06 23:06:36 -0700761 if ((!pcm->running) && (pcm_start(pcm) < 0))
762 return -errno;
763 else if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530764 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700765 pcm->running = 0;
766 if (errno == EPIPE) {
767 /* we failed to make our window -- try to restart */
768 pcm->underruns++;
769 continue;
770 }
771 return oops(pcm, errno, "cannot read stream data");
772 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800773 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700774 }
775}
776
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800777/** Writes audio samples to PCM.
778 * If the PCM has not been started, it is started in this function.
779 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
780 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
781 * @param pcm A PCM handle.
782 * @param data The audio sample array
783 * @param count The number of bytes occupied by the sample array.
784 * @return On success, this function returns zero; otherwise, a negative number.
785 * @deprecated
786 * @ingroup libtinyalsa-pcm
787 */
788int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
789{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700790 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800791}
792
793/** Reads audio samples from PCM.
794 * If the PCM has not been started, it is started in this function.
795 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
796 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
797 * @param pcm A PCM handle.
798 * @param data The audio sample array
799 * @param count The number of bytes occupied by the sample array.
800 * @return On success, this function returns zero; otherwise, a negative number.
801 * @deprecated
802 * @ingroup libtinyalsa-pcm
803 */
804int pcm_read(struct pcm *pcm, void *data, unsigned int count)
805{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700806 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800807}
808
Simon Wilson79d39652011-05-25 13:44:23 -0700809static struct pcm bad_pcm = {
810 .fd = -1,
811};
812
Taylor Holberton6d58e012016-10-01 18:32:30 -0400813/** Gets the hardware parameters of a PCM, without created a PCM handle.
814 * @param card The card of the PCM.
815 * The default card is zero.
816 * @param device The device of the PCM.
817 * The default device is zero.
818 * @param flags Specifies whether the PCM is an input or output.
819 * May be one of the following:
820 * - @ref PCM_IN
821 * - @ref PCM_OUT
822 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800823 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400824 */
Simon Wilson43544882012-10-31 12:52:39 -0700825struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
826 unsigned int flags)
827{
828 struct snd_pcm_hw_params *params;
829 char fn[256];
830 int fd;
831
832 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
833 flags & PCM_IN ? 'c' : 'p');
834
Taylor Holberton093b8782017-10-12 20:28:30 -0400835 if (flags & PCM_NONBLOCK)
836 fd = open(fn, O_RDWR | O_NONBLOCK);
837 else
838 fd = open(fn, O_RDWR);
839
Simon Wilson43544882012-10-31 12:52:39 -0700840 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400841 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700842 goto err_open;
843 }
844
845 params = calloc(1, sizeof(struct snd_pcm_hw_params));
846 if (!params)
847 goto err_calloc;
848
849 param_init(params);
850 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
851 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
852 goto err_hw_refine;
853 }
854
855 close(fd);
856
857 return (struct pcm_params *)params;
858
859err_hw_refine:
860 free(params);
861err_calloc:
862 close(fd);
863err_open:
864 return NULL;
865}
866
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500867/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400868 * @param pcm_params Hardware parameters of a PCM.
869 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800870 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400871 */
Simon Wilson43544882012-10-31 12:52:39 -0700872void pcm_params_free(struct pcm_params *pcm_params)
873{
874 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
875
876 if (params)
877 free(params);
878}
879
880static int pcm_param_to_alsa(enum pcm_param param)
881{
882 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700883 case PCM_PARAM_ACCESS:
884 return SNDRV_PCM_HW_PARAM_ACCESS;
885 case PCM_PARAM_FORMAT:
886 return SNDRV_PCM_HW_PARAM_FORMAT;
887 case PCM_PARAM_SUBFORMAT:
888 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700889 case PCM_PARAM_SAMPLE_BITS:
890 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
891 break;
892 case PCM_PARAM_FRAME_BITS:
893 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
894 break;
895 case PCM_PARAM_CHANNELS:
896 return SNDRV_PCM_HW_PARAM_CHANNELS;
897 break;
898 case PCM_PARAM_RATE:
899 return SNDRV_PCM_HW_PARAM_RATE;
900 break;
901 case PCM_PARAM_PERIOD_TIME:
902 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
903 break;
904 case PCM_PARAM_PERIOD_SIZE:
905 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
906 break;
907 case PCM_PARAM_PERIOD_BYTES:
908 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
909 break;
910 case PCM_PARAM_PERIODS:
911 return SNDRV_PCM_HW_PARAM_PERIODS;
912 break;
913 case PCM_PARAM_BUFFER_TIME:
914 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
915 break;
916 case PCM_PARAM_BUFFER_SIZE:
917 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
918 break;
919 case PCM_PARAM_BUFFER_BYTES:
920 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
921 break;
922 case PCM_PARAM_TICK_TIME:
923 return SNDRV_PCM_HW_PARAM_TICK_TIME;
924 break;
925
926 default:
927 return -1;
928 }
929}
930
Taylor Holberton6d58e012016-10-01 18:32:30 -0400931/** Gets a mask from a PCM's hardware parameters.
932 * @param pcm_params A PCM's hardware parameters.
933 * @param param The parameter to get.
934 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
935 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800936 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400937 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800938const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700939 enum pcm_param param)
940{
941 int p;
942 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
943 if (params == NULL) {
944 return NULL;
945 }
946
947 p = pcm_param_to_alsa(param);
948 if (p < 0 || !param_is_mask(p)) {
949 return NULL;
950 }
951
Taylor Holberton2f387d22016-12-01 15:58:16 -0800952 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700953}
954
Taylor Holberton17a10242016-11-23 13:18:24 -0800955/** Get the minimum of a specified PCM parameter.
956 * @param pcm_params A PCM parameters structure.
957 * @param param The specified parameter to get the minimum of.
958 * @returns On success, the parameter minimum.
959 * On failure, zero.
960 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800961unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700962 enum pcm_param param)
963{
964 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
965 int p;
966
967 if (!params)
968 return 0;
969
970 p = pcm_param_to_alsa(param);
971 if (p < 0)
972 return 0;
973
974 return param_get_min(params, p);
975}
976
Taylor Holberton17a10242016-11-23 13:18:24 -0800977/** Get the maximum of a specified PCM parameter.
978 * @param pcm_params A PCM parameters structure.
979 * @param param The specified parameter to get the maximum of.
980 * @returns On success, the parameter maximum.
981 * On failure, zero.
982 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800983unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700984 enum pcm_param param)
985{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800986 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700987 int p;
988
989 if (!params)
990 return 0;
991
992 p = pcm_param_to_alsa(param);
993 if (p < 0)
994 return 0;
995
996 return param_get_max(params, p);
997}
998
Taylor Holberton6d58e012016-10-01 18:32:30 -0400999/** Closes a PCM returned by @ref pcm_open.
1000 * @param pcm A PCM returned by @ref pcm_open.
1001 * May not be NULL.
1002 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001003 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001004 */
Simon Wilson79d39652011-05-25 13:44:23 -07001005int pcm_close(struct pcm *pcm)
1006{
1007 if (pcm == &bad_pcm)
1008 return 0;
1009
Eric Laurent40b018e2011-06-18 10:10:23 -07001010 pcm_hw_munmap_status(pcm);
1011
Liam Girdwood6be28f12011-10-13 12:59:51 -07001012 if (pcm->flags & PCM_MMAP) {
1013 pcm_stop(pcm);
1014 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1015 }
1016
Simon Wilson79d39652011-05-25 13:44:23 -07001017 if (pcm->fd >= 0)
1018 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301019 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001020 pcm->running = 0;
1021 pcm->buffer_size = 0;
1022 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -07001023 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -07001024 return 0;
1025}
1026
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001027/** Opens a PCM by it's name.
1028 * @param name The name of the PCM.
1029 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
1030 * @param flags Specify characteristics and functionality about the pcm.
1031 * May be a bitwise AND of the following:
1032 * - @ref PCM_IN
1033 * - @ref PCM_OUT
1034 * - @ref PCM_MMAP
1035 * - @ref PCM_NOIRQ
1036 * - @ref PCM_MONOTONIC
1037 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001038 * @returns A PCM structure.
1039 * If an error occurs allocating memory for the PCM, NULL is returned.
1040 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1041 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001042 * @ingroup libtinyalsa-pcm
1043 */
1044struct pcm *pcm_open_by_name(const char *name,
1045 unsigned int flags,
1046 const struct pcm_config *config)
1047{
1048 unsigned int card, device;
1049 if ((name[0] != 'h')
1050 || (name[1] != 'w')
1051 || (name[2] != ':')) {
1052 return NULL;
1053 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1054 return NULL;
1055 }
1056 return pcm_open(card, device, flags, config);
1057}
1058
Taylor Holberton6d58e012016-10-01 18:32:30 -04001059/** Opens a PCM.
1060 * @param card The card that the pcm belongs to.
1061 * The default card is zero.
1062 * @param device The device that the pcm belongs to.
1063 * The default device is zero.
1064 * @param flags Specify characteristics and functionality about the pcm.
1065 * May be a bitwise AND of the following:
1066 * - @ref PCM_IN
1067 * - @ref PCM_OUT
1068 * - @ref PCM_MMAP
1069 * - @ref PCM_NOIRQ
1070 * - @ref PCM_MONOTONIC
1071 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001072 * @returns A PCM structure.
1073 * If an error occurs allocating memory for the PCM, NULL is returned.
1074 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1075 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001076 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001077 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001078struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001079 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001080{
Simon Wilson79d39652011-05-25 13:44:23 -07001081 struct pcm *pcm;
1082 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001083 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -07001084 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001085
1086 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001087 if (!pcm)
1088 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001089
Simon Wilson1bd580f2011-06-02 15:58:41 -07001090 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
1091 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -07001092
1093 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -04001094
1095 if (flags & PCM_NONBLOCK)
1096 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
1097 else
1098 pcm->fd = open(fn, O_RDWR);
1099
Simon Wilson79d39652011-05-25 13:44:23 -07001100 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001101 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001102 return pcm;
1103 }
1104
1105 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001106 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001107 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001108 }
David Wagner4cddf192014-04-02 15:12:54 +02001109 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001110
Taylor Holberton861da7a2017-04-10 12:05:51 -07001111 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001112 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001113
Eric Laurent40b018e2011-06-18 10:10:23 -07001114 rc = pcm_hw_mmap_status(pcm);
1115 if (rc < 0) {
1116 oops(pcm, rc, "mmap status failed");
1117 goto fail;
1118 }
1119
Glenn Kasten81012402013-08-22 15:11:48 -07001120#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1121 if (pcm->flags & PCM_MONOTONIC) {
1122 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1123 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1124 if (rc < 0) {
1125 oops(pcm, rc, "cannot set timestamp type");
1126 goto fail;
1127 }
1128 }
1129#endif
1130
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -03001131 /* prepare here so the user does not need to do this later */
1132 if (pcm_prepare(pcm))
1133 goto fail;
1134
Simon Wilson79d39652011-05-25 13:44:23 -07001135 pcm->underruns = 0;
1136 return pcm;
1137
1138fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001139 if (flags & PCM_MMAP)
1140 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1141fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001142 close(pcm->fd);
1143 pcm->fd = -1;
1144 return pcm;
1145}
1146
Taylor Holberton6d58e012016-10-01 18:32:30 -04001147/** Checks if a PCM file has been opened without error.
1148 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001149 * May be NULL.
1150 * @return If a PCM's file descriptor is not valid or the pointer is NULL, it returns zero.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001151 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001152 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001153 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001154int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001155{
Taylor Holbertone123a652017-01-13 21:39:48 -08001156 if (pcm != NULL) {
1157 return pcm->fd >= 0;
1158 }
1159 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001160}
Simon Wilsond6458e62011-06-21 14:58:11 -07001161
Taylor Holberton558e5942016-12-04 13:42:28 -08001162/** Links two PCMs.
1163 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1164 * If an error occurs, the error message will be written to @p pcm1.
1165 * @param pcm1 A PCM handle.
1166 * @param pcm2 Another PCM handle.
1167 * @return On success, zero; on failure, a negative number.
1168 * @ingroup libtinyalsa-pcm
1169 */
1170int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1171{
1172 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1173 if (err == -1) {
1174 return oops(pcm1, errno, "cannot link PCM");
1175 }
1176 return 0;
1177}
1178
1179/** Unlinks a PCM.
1180 * @see @ref pcm_link
1181 * @param pcm A PCM handle.
1182 * @return On success, zero; on failure, a negative number.
1183 * @ingroup libtinyalsa-pcm
1184 */
1185int pcm_unlink(struct pcm *pcm)
1186{
1187 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1188 if (err == -1) {
1189 return oops(pcm, errno, "cannot unlink PCM");
1190 }
1191 return 0;
1192}
1193
Taylor Holberton6d58e012016-10-01 18:32:30 -04001194/** Prepares a PCM, if it has not been prepared already.
1195 * @param pcm A PCM handle.
1196 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001197 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001198 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301199int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001200{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301201 if (pcm->prepared)
1202 return 0;
1203
Simon Wilsond6458e62011-06-21 14:58:11 -07001204 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1205 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001206
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301207 pcm->prepared = 1;
1208 return 0;
1209}
1210
Taylor Holberton6d58e012016-10-01 18:32:30 -04001211/** Starts a PCM.
1212 * If the PCM has not been prepared,
1213 * it is prepared in this function.
1214 * @param pcm A PCM handle.
1215 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001216 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001217 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301218int pcm_start(struct pcm *pcm)
1219{
1220 int prepare_error = pcm_prepare(pcm);
1221 if (prepare_error)
1222 return prepare_error;
1223
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001224 /* if pcm is linked, it may be already started by other pcm */
1225 /* check pcm state is not in running state */
1226 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001227
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001228 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
1229 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1230 return oops(pcm, errno, "cannot start channel");
1231 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001232
Liam Girdwood6be28f12011-10-13 12:59:51 -07001233 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001234 return 0;
1235}
1236
Taylor Holberton6d58e012016-10-01 18:32:30 -04001237/** Stops a PCM.
1238 * @param pcm A PCM handle.
1239 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001240 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001241 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001242int pcm_stop(struct pcm *pcm)
1243{
1244 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1245 return oops(pcm, errno, "cannot stop channel");
1246
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301247 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001248 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001249 return 0;
1250}
1251
Liam Girdwood6be28f12011-10-13 12:59:51 -07001252static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1253{
1254 int avail;
1255
1256 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1257
1258 if (avail < 0)
1259 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001260 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001261 avail -= pcm->boundary;
1262
1263 return avail;
1264}
1265
1266static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1267{
1268 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1269 if (avail < 0)
1270 avail += pcm->boundary;
1271 return avail;
1272}
1273
1274static inline int pcm_mmap_avail(struct pcm *pcm)
1275{
1276 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1277 if (pcm->flags & PCM_IN)
1278 return pcm_mmap_capture_avail(pcm);
1279 else
1280 return pcm_mmap_playback_avail(pcm);
1281}
1282
1283static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1284{
1285 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1286 appl_ptr += frames;
1287
1288 /* check for boundary wrap */
1289 if (appl_ptr > pcm->boundary)
1290 appl_ptr -= pcm->boundary;
1291 pcm->mmap_control->appl_ptr = appl_ptr;
1292}
1293
1294int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1295 unsigned int *frames)
1296{
1297 unsigned int continuous, copy_frames, avail;
1298
1299 /* return the mmap buffer */
1300 *areas = pcm->mmap_buffer;
1301
1302 /* and the application offset in frames */
1303 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1304
1305 avail = pcm_mmap_avail(pcm);
1306 if (avail > pcm->buffer_size)
1307 avail = pcm->buffer_size;
1308 continuous = pcm->buffer_size - *offset;
1309
1310 /* we can only copy frames if the are availabale and continuos */
1311 copy_frames = *frames;
1312 if (copy_frames > avail)
1313 copy_frames = avail;
1314 if (copy_frames > continuous)
1315 copy_frames = continuous;
1316 *frames = copy_frames;
1317
1318 return 0;
1319}
1320
1321int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1322{
Taylor Holberton72e44222016-11-22 09:54:47 -08001323 int ret;
1324
Taylor Holberton73466c02016-10-01 12:51:59 -04001325 /* not used */
1326 (void) offset;
1327
Liam Girdwood6be28f12011-10-13 12:59:51 -07001328 /* update the application pointer in userspace and kernel */
1329 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001330 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001331 if (ret != 0){
1332 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001333 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001334 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001335
1336 return frames;
1337}
1338
1339int pcm_avail_update(struct pcm *pcm)
1340{
1341 pcm_sync_ptr(pcm, 0);
1342 return pcm_mmap_avail(pcm);
1343}
1344
1345int pcm_state(struct pcm *pcm)
1346{
1347 int err = pcm_sync_ptr(pcm, 0);
1348 if (err < 0)
1349 return err;
1350
1351 return pcm->mmap_status->state;
1352}
1353
Taylor Holberton17a10242016-11-23 13:18:24 -08001354/** Waits for frames to be available for read or write operations.
1355 * @param pcm A PCM handle.
1356 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1357 * @returns If frames became available, one is returned.
1358 * If a timeout occured, zero is returned.
1359 * If an error occured, a negative number is returned.
1360 * @ingroup libtinyalsa-pcm
1361 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001362int pcm_wait(struct pcm *pcm, int timeout)
1363{
1364 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001365 int err;
1366
1367 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001368 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001369
1370 do {
1371 /* let's wait for avail or timeout */
1372 err = poll(&pfd, 1, timeout);
1373 if (err < 0)
1374 return -errno;
1375
1376 /* timeout ? */
1377 if (err == 0)
1378 return 0;
1379
1380 /* have we been interrupted ? */
1381 if (errno == -EINTR)
1382 continue;
1383
1384 /* check for any errors */
1385 if (pfd.revents & (POLLERR | POLLNVAL)) {
1386 switch (pcm_state(pcm)) {
1387 case PCM_STATE_XRUN:
1388 return -EPIPE;
1389 case PCM_STATE_SUSPENDED:
1390 return -ESTRPIPE;
1391 case PCM_STATE_DISCONNECTED:
1392 return -ENODEV;
1393 default:
1394 return -EIO;
1395 }
1396 }
1397 /* poll again if fd not ready for IO */
1398 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1399
1400 return 1;
1401}
1402
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001403int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001404{
1405 int err = 0, frames, avail;
1406 unsigned int offset = 0, count;
1407
1408 if (bytes == 0)
1409 return 0;
1410
1411 count = pcm_bytes_to_frames(pcm, bytes);
1412
1413 while (count > 0) {
1414
1415 /* get the available space for writing new frames */
1416 avail = pcm_avail_update(pcm);
1417 if (avail < 0) {
1418 fprintf(stderr, "cannot determine available mmap frames");
1419 return err;
1420 }
1421
1422 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001423 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001424 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1425 if (pcm_start(pcm) < 0) {
1426 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1427 (unsigned int)pcm->mmap_status->hw_ptr,
1428 (unsigned int)pcm->mmap_control->appl_ptr,
1429 avail);
1430 return -errno;
1431 }
1432 }
1433
1434 /* sleep until we have space to write new frames */
1435 if (pcm->running &&
1436 (unsigned int)avail < pcm->mmap_control->avail_min) {
1437 int time = -1;
1438
1439 if (pcm->flags & PCM_NOIRQ)
1440 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1441 / pcm->noirq_frames_per_msec;
1442
1443 err = pcm_wait(pcm, time);
1444 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301445 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001446 pcm->running = 0;
1447 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1448 (unsigned int)pcm->mmap_status->hw_ptr,
1449 (unsigned int)pcm->mmap_control->appl_ptr,
1450 avail);
1451 pcm->mmap_control->appl_ptr = 0;
1452 return err;
1453 }
1454 continue;
1455 }
1456
1457 frames = count;
1458 if (frames > avail)
1459 frames = avail;
1460
1461 if (!frames)
1462 break;
1463
1464 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001465 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001466 if (frames < 0) {
1467 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1468 (unsigned int)pcm->mmap_status->hw_ptr,
1469 (unsigned int)pcm->mmap_control->appl_ptr,
1470 avail);
1471 return frames;
1472 }
1473
1474 offset += frames;
1475 count -= frames;
1476 }
1477
Liam Girdwood6be28f12011-10-13 12:59:51 -07001478 return 0;
1479}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001480
1481int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1482{
1483 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1484 return -ENOSYS;
1485
1486 return pcm_mmap_transfer(pcm, (void *)data, count);
1487}
1488
1489int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1490{
1491 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1492 return -ENOSYS;
1493
1494 return pcm_mmap_transfer(pcm, data, count);
1495}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301496
Taylor Holberton17a10242016-11-23 13:18:24 -08001497/** Gets the delay of the PCM, in terms of frames.
1498 * @param pcm A PCM handle.
1499 * @returns On success, the delay of the PCM.
1500 * On failure, a negative number.
1501 * @ingroup libtinyalsa-pcm
1502 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301503long pcm_get_delay(struct pcm *pcm)
1504{
1505 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1506 return -1;
1507
1508 return pcm->pcm_delay;
1509}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001510