blob: f49be90e3f09f358f3b2ba2f881617f1573da4d1 [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 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700219 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400220 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700221 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800222 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700223 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400224 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700225 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400226 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700227 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700228 struct snd_pcm_mmap_status *mmap_status;
229 struct snd_pcm_mmap_control *mmap_control;
230 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700231 void *mmap_buffer;
232 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800233 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530234 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800235 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200236 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700237};
238
Taylor Holberton861da7a2017-04-10 12:05:51 -0700239static int oops(struct pcm *pcm, int e, const char *fmt, ...)
240{
241 va_list ap;
242 int sz;
243
244 va_start(ap, fmt);
245 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
246 va_end(ap);
247 sz = strlen(pcm->error);
248
249 if (errno)
250 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
251 ": %s", strerror(e));
252 return -1;
253}
254
Taylor Holberton6d58e012016-10-01 18:32:30 -0400255/** Gets the buffer size of the PCM.
256 * @param pcm A PCM handle.
257 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800258 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400259 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800260unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700261{
262 return pcm->buffer_size;
263}
264
Taylor Holberton77979a82016-12-01 20:04:04 -0800265/** Gets the channel count of the PCM.
266 * @param pcm A PCM handle.
267 * @return The channel count of the PCM.
268 * @ingroup libtinyalsa-pcm
269 */
270unsigned int pcm_get_channels(const struct pcm *pcm)
271{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700272 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800273}
274
Taylor Holberton08bb5902017-04-10 11:45:44 -0700275/** Gets the PCM configuration.
276 * @param pcm A PCM handle.
277 * @return The PCM configuration.
278 * This function only returns NULL if
279 * @p pcm is NULL.
280 * @ingroup libtinyalsa-pcm
281 * */
282const struct pcm_config * pcm_get_config(const struct pcm *pcm)
283{
284 if (pcm == NULL)
285 return NULL;
286 return &pcm->config;
287}
288
Taylor Holberton77979a82016-12-01 20:04:04 -0800289/** Gets the rate of the PCM.
290 * The rate is given in frames per second.
291 * @param pcm A PCM handle.
292 * @return The rate of the PCM.
293 * @ingroup libtinyalsa-pcm
294 */
295unsigned int pcm_get_rate(const struct pcm *pcm)
296{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700297 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800298}
299
300/** Gets the format of the PCM.
301 * @param pcm A PCM handle.
302 * @return The format of the PCM.
303 * @ingroup libtinyalsa-pcm
304 */
305enum pcm_format pcm_get_format(const struct pcm *pcm)
306{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700307 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800308}
309
Taylor Holberton6d58e012016-10-01 18:32:30 -0400310/** Gets the file descriptor of the PCM.
311 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800312 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400313 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800314 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400315 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800316int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400317{
318 return pcm->fd;
319}
320
Taylor Holberton6d58e012016-10-01 18:32:30 -0400321/** Gets the error message for the last error that occured.
322 * If no error occured and this function is called, the results are undefined.
323 * @param pcm A PCM handle.
324 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800325 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400326 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800327const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700328{
329 return pcm->error;
330}
331
Taylor Holberton861da7a2017-04-10 12:05:51 -0700332/** Sets the PCM configuration.
333 * @param pcm A PCM handle.
334 * @param config The configuration to use for the
335 * PCM. This parameter may be NULL, in which case
336 * the default configuration is used.
337 * @returns Zero on success, a negative errno value
338 * on failure.
339 * @ingroup libtinyalsa-pcm
340 * */
341int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
342{
343 if (pcm == NULL)
344 return -EFAULT;
345 else if (config == NULL) {
346 config = &pcm->config;
347 pcm->config.channels = 2;
348 pcm->config.rate = 48000;
349 pcm->config.period_size = 1024;
350 pcm->config.period_count = 4;
351 pcm->config.format = PCM_FORMAT_S16_LE;
352 pcm->config.start_threshold = config->period_count * config->period_size;
353 pcm->config.stop_threshold = config->period_count * config->period_size;
354 pcm->config.silence_threshold = 0;
355 } else
356 pcm->config = *config;
357
358 struct snd_pcm_hw_params params;
359 param_init(&params);
360 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
361 pcm_format_to_alsa(config->format));
362 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
363 SNDRV_PCM_SUBFORMAT_STD);
364 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
365 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
366 pcm_format_to_bits(config->format));
367 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
368 pcm_format_to_bits(config->format) * config->channels);
369 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
370 config->channels);
371 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
372 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
373
374 if (pcm->flags & PCM_NOIRQ) {
375
376 if (!(pcm->flags & PCM_MMAP)) {
377 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
378 return -EINVAL;
379 }
380
381 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
382 pcm->noirq_frames_per_msec = config->rate / 1000;
383 }
384
385 if (pcm->flags & PCM_MMAP)
386 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
387 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
388 else
389 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
390 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
391
392 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
393 int errno_copy = errno;
394 oops(pcm, -errno, "cannot set hw params");
395 return -errno_copy;
396 }
397
398 /* get our refined hw_params */
399 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
400 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
401 pcm->buffer_size = config->period_count * config->period_size;
402
403 if (pcm->flags & PCM_MMAP) {
404 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
405 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
406 if (pcm->mmap_buffer == MAP_FAILED) {
407 int errno_copy = errno;
408 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
409 pcm_frames_to_bytes(pcm, pcm->buffer_size));
410 return -errno_copy;
411 }
412 }
413
414 struct snd_pcm_sw_params sparams;
415 memset(&sparams, 0, sizeof(sparams));
416 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
417 sparams.period_step = 1;
418 sparams.avail_min = 1;
419
420 if (!config->start_threshold) {
421 if (pcm->flags & PCM_IN)
422 pcm->config.start_threshold = sparams.start_threshold = 1;
423 else
424 pcm->config.start_threshold = sparams.start_threshold =
425 config->period_count * config->period_size / 2;
426 } else
427 sparams.start_threshold = config->start_threshold;
428
429 /* pick a high stop threshold - todo: does this need further tuning */
430 if (!config->stop_threshold) {
431 if (pcm->flags & PCM_IN)
432 pcm->config.stop_threshold = sparams.stop_threshold =
433 config->period_count * config->period_size * 10;
434 else
435 pcm->config.stop_threshold = sparams.stop_threshold =
436 config->period_count * config->period_size;
437 }
438 else
439 sparams.stop_threshold = config->stop_threshold;
440
441 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
442 sparams.silence_size = 0;
443 sparams.silence_threshold = config->silence_threshold;
444 pcm->boundary = sparams.boundary = pcm->buffer_size;
445
446 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
447 pcm->boundary *= 2;
448
449 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
450 int errno_copy = errno;
451 oops(pcm, -errno, "cannot set sw params");
452 return -errno_copy;
453 }
454
455 return 0;
456}
457
Taylor Holberton6d58e012016-10-01 18:32:30 -0400458/** Gets the subdevice on which the pcm has been opened.
459 * @param pcm A PCM handle.
460 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800461unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200462{
463 return pcm->subdevice;
464}
465
Taylor Holberton6d58e012016-10-01 18:32:30 -0400466/** Determines the number of bits occupied by a @ref pcm_format.
467 * @param format A PCM format.
468 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800469 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400470 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700471unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700472{
473 switch (format) {
474 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400475 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700476 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400477 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700478 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400479 case PCM_FORMAT_S24_3LE:
480 case PCM_FORMAT_S24_3BE:
481 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700482 default:
483 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400484 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700485 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400486 case PCM_FORMAT_S8:
487 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700488 };
489}
490
Taylor Holberton6d58e012016-10-01 18:32:30 -0400491/** Determines how many frames of a PCM can fit into a number of bytes.
492 * @param pcm A PCM handle.
493 * @param bytes The number of bytes.
494 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800495 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400496 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800497unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700498{
499 return bytes / (pcm->config.channels *
500 (pcm_format_to_bits(pcm->config.format) >> 3));
501}
502
Taylor Holberton6d58e012016-10-01 18:32:30 -0400503/** Determines how many bytes are occupied by a number of frames of a PCM.
504 * @param pcm A PCM handle.
505 * @param frames The number of frames of a PCM.
506 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800507 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400508 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800509unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700510{
511 return frames * pcm->config.channels *
512 (pcm_format_to_bits(pcm->config.format) >> 3);
513}
514
Taylor Holberton4f556062016-09-16 09:54:36 -0400515static int pcm_sync_ptr(struct pcm *pcm, int flags)
516{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200517 if (pcm->sync_ptr == NULL) {
518 /* status and control are mmaped */
519
520 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
521 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
522 oops(pcm, errno, "failed to sync hardware pointer");
523 return -1;
524 }
525 }
526 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700527 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800528 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
529 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700530 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800531 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700532 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200533
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200534 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700535}
536
Taylor Holberton4f556062016-09-16 09:54:36 -0400537static int pcm_hw_mmap_status(struct pcm *pcm)
538{
Eric Laurent40b018e2011-06-18 10:10:23 -0700539 if (pcm->sync_ptr)
540 return 0;
541
542 int page_size = sysconf(_SC_PAGE_SIZE);
543 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
544 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
545 if (pcm->mmap_status == MAP_FAILED)
546 pcm->mmap_status = NULL;
547 if (!pcm->mmap_status)
548 goto mmap_error;
549
550 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
551 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
552 if (pcm->mmap_control == MAP_FAILED)
553 pcm->mmap_control = NULL;
554 if (!pcm->mmap_control) {
555 munmap(pcm->mmap_status, page_size);
556 pcm->mmap_status = NULL;
557 goto mmap_error;
558 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700559
560 return 0;
561
562mmap_error:
563
564 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
565 if (!pcm->sync_ptr)
566 return -ENOMEM;
567 pcm->mmap_status = &pcm->sync_ptr->s.status;
568 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700569
570 return 0;
571}
572
573static void pcm_hw_munmap_status(struct pcm *pcm) {
574 if (pcm->sync_ptr) {
575 free(pcm->sync_ptr);
576 pcm->sync_ptr = NULL;
577 } else {
578 int page_size = sysconf(_SC_PAGE_SIZE);
579 if (pcm->mmap_status)
580 munmap(pcm->mmap_status, page_size);
581 if (pcm->mmap_control)
582 munmap(pcm->mmap_control, page_size);
583 }
584 pcm->mmap_status = NULL;
585 pcm->mmap_control = NULL;
586}
587
Taylor Holberton6d58e012016-10-01 18:32:30 -0400588/** Writes audio samples to PCM.
589 * If the PCM has not been started, it is started in this function.
590 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
591 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
592 * @param pcm A PCM handle.
593 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800594 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700595 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
596 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800597 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800598 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400599 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800600int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700601{
602 struct snd_xferi x;
603
604 if (pcm->flags & PCM_IN)
605 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700606#if UINT_MAX > TINYALSA_FRAMES_MAX
607 if (frame_count > TINYALSA_FRAMES_MAX)
608 return -EINVAL;
609#endif
610 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700611 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700612
Mark Brown6bbe77a2012-02-10 22:07:09 +0000613 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800614 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800615 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700616 for (;;) {
Simon Wilson79d39652011-05-25 13:44:23 -0700617 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Simon Wilson79d39652011-05-25 13:44:23 -0700618 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700619 /* we failed to make our window -- try to restart if we are
620 * allowed to do so. Otherwise, simply allow the EPIPE error to
621 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700622 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700623 if (pcm->flags & PCM_NORESTART)
624 return -EPIPE;
Ricardo Biehl Pasquali7c40a9e2018-09-09 21:40:01 -0300625 if (pcm_prepare(pcm))
626 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700627 continue;
628 }
629 return oops(pcm, errno, "cannot write stream data");
630 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800631 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700632 }
633}
634
Taylor Holberton6d58e012016-10-01 18:32:30 -0400635/** Reads audio samples from PCM.
636 * If the PCM has not been started, it is started in this function.
637 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
638 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
639 * @param pcm A PCM handle.
640 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800641 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700642 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
643 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800644 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800645 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400646 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800647int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700648{
649 struct snd_xferi x;
650
651 if (!(pcm->flags & PCM_IN))
652 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700653#if UINT_MAX > TINYALSA_FRAMES_MAX
654 if (frame_count > TINYALSA_FRAMES_MAX)
655 return -EINVAL;
656#endif
657 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700658 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700659
660 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800661 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800662 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700663 for (;;) {
Ricardo Biehl Pasquali13e11fe2018-08-21 14:42:23 -0300664 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Simon Wilson79d39652011-05-25 13:44:23 -0700665 if (errno == EPIPE) {
666 /* we failed to make our window -- try to restart */
667 pcm->underruns++;
Ricardo Biehl Pasqualif85cf622019-01-07 19:33:13 -0200668 if (pcm->flags & PCM_NORESTART)
669 return -EPIPE;
670 if (pcm_prepare(pcm))
671 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700672 continue;
673 }
674 return oops(pcm, errno, "cannot read stream data");
675 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800676 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700677 }
678}
679
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800680/** Writes audio samples to PCM.
681 * If the PCM has not been started, it is started in this function.
682 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
683 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
684 * @param pcm A PCM handle.
685 * @param data The audio sample array
686 * @param count The number of bytes occupied by the sample array.
687 * @return On success, this function returns zero; otherwise, a negative number.
688 * @deprecated
689 * @ingroup libtinyalsa-pcm
690 */
691int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
692{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700693 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800694}
695
696/** Reads audio samples from PCM.
697 * If the PCM has not been started, it is started in this function.
698 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
699 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
700 * @param pcm A PCM handle.
701 * @param data The audio sample array
702 * @param count The number of bytes occupied by the sample array.
703 * @return On success, this function returns zero; otherwise, a negative number.
704 * @deprecated
705 * @ingroup libtinyalsa-pcm
706 */
707int pcm_read(struct pcm *pcm, void *data, unsigned int count)
708{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700709 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800710}
711
Simon Wilson79d39652011-05-25 13:44:23 -0700712static struct pcm bad_pcm = {
713 .fd = -1,
714};
715
Taylor Holberton6d58e012016-10-01 18:32:30 -0400716/** Gets the hardware parameters of a PCM, without created a PCM handle.
717 * @param card The card of the PCM.
718 * The default card is zero.
719 * @param device The device of the PCM.
720 * The default device is zero.
721 * @param flags Specifies whether the PCM is an input or output.
722 * May be one of the following:
723 * - @ref PCM_IN
724 * - @ref PCM_OUT
725 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800726 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400727 */
Simon Wilson43544882012-10-31 12:52:39 -0700728struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
729 unsigned int flags)
730{
731 struct snd_pcm_hw_params *params;
732 char fn[256];
733 int fd;
734
735 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
736 flags & PCM_IN ? 'c' : 'p');
737
Taylor Holberton093b8782017-10-12 20:28:30 -0400738 if (flags & PCM_NONBLOCK)
739 fd = open(fn, O_RDWR | O_NONBLOCK);
740 else
741 fd = open(fn, O_RDWR);
742
Simon Wilson43544882012-10-31 12:52:39 -0700743 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400744 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700745 goto err_open;
746 }
747
748 params = calloc(1, sizeof(struct snd_pcm_hw_params));
749 if (!params)
750 goto err_calloc;
751
752 param_init(params);
753 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
754 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
755 goto err_hw_refine;
756 }
757
758 close(fd);
759
760 return (struct pcm_params *)params;
761
762err_hw_refine:
763 free(params);
764err_calloc:
765 close(fd);
766err_open:
767 return NULL;
768}
769
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500770/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400771 * @param pcm_params Hardware parameters of a PCM.
772 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800773 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400774 */
Simon Wilson43544882012-10-31 12:52:39 -0700775void pcm_params_free(struct pcm_params *pcm_params)
776{
777 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
778
779 if (params)
780 free(params);
781}
782
783static int pcm_param_to_alsa(enum pcm_param param)
784{
785 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700786 case PCM_PARAM_ACCESS:
787 return SNDRV_PCM_HW_PARAM_ACCESS;
788 case PCM_PARAM_FORMAT:
789 return SNDRV_PCM_HW_PARAM_FORMAT;
790 case PCM_PARAM_SUBFORMAT:
791 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700792 case PCM_PARAM_SAMPLE_BITS:
793 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
794 break;
795 case PCM_PARAM_FRAME_BITS:
796 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
797 break;
798 case PCM_PARAM_CHANNELS:
799 return SNDRV_PCM_HW_PARAM_CHANNELS;
800 break;
801 case PCM_PARAM_RATE:
802 return SNDRV_PCM_HW_PARAM_RATE;
803 break;
804 case PCM_PARAM_PERIOD_TIME:
805 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
806 break;
807 case PCM_PARAM_PERIOD_SIZE:
808 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
809 break;
810 case PCM_PARAM_PERIOD_BYTES:
811 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
812 break;
813 case PCM_PARAM_PERIODS:
814 return SNDRV_PCM_HW_PARAM_PERIODS;
815 break;
816 case PCM_PARAM_BUFFER_TIME:
817 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
818 break;
819 case PCM_PARAM_BUFFER_SIZE:
820 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
821 break;
822 case PCM_PARAM_BUFFER_BYTES:
823 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
824 break;
825 case PCM_PARAM_TICK_TIME:
826 return SNDRV_PCM_HW_PARAM_TICK_TIME;
827 break;
828
829 default:
830 return -1;
831 }
832}
833
Taylor Holberton6d58e012016-10-01 18:32:30 -0400834/** Gets a mask from a PCM's hardware parameters.
835 * @param pcm_params A PCM's hardware parameters.
836 * @param param The parameter to get.
837 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
838 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800839 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400840 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800841const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700842 enum pcm_param param)
843{
844 int p;
845 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
846 if (params == NULL) {
847 return NULL;
848 }
849
850 p = pcm_param_to_alsa(param);
851 if (p < 0 || !param_is_mask(p)) {
852 return NULL;
853 }
854
Taylor Holberton2f387d22016-12-01 15:58:16 -0800855 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700856}
857
Taylor Holberton17a10242016-11-23 13:18:24 -0800858/** Get the minimum of a specified PCM parameter.
859 * @param pcm_params A PCM parameters structure.
860 * @param param The specified parameter to get the minimum of.
861 * @returns On success, the parameter minimum.
862 * On failure, zero.
863 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800864unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700865 enum pcm_param param)
866{
867 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
868 int p;
869
870 if (!params)
871 return 0;
872
873 p = pcm_param_to_alsa(param);
874 if (p < 0)
875 return 0;
876
877 return param_get_min(params, p);
878}
879
Taylor Holberton17a10242016-11-23 13:18:24 -0800880/** Get the maximum of a specified PCM parameter.
881 * @param pcm_params A PCM parameters structure.
882 * @param param The specified parameter to get the maximum of.
883 * @returns On success, the parameter maximum.
884 * On failure, zero.
885 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800886unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700887 enum pcm_param param)
888{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800889 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700890 int p;
891
892 if (!params)
893 return 0;
894
895 p = pcm_param_to_alsa(param);
896 if (p < 0)
897 return 0;
898
899 return param_get_max(params, p);
900}
901
Taylor Holberton6d58e012016-10-01 18:32:30 -0400902/** Closes a PCM returned by @ref pcm_open.
903 * @param pcm A PCM returned by @ref pcm_open.
904 * May not be NULL.
905 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800906 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400907 */
Simon Wilson79d39652011-05-25 13:44:23 -0700908int pcm_close(struct pcm *pcm)
909{
910 if (pcm == &bad_pcm)
911 return 0;
912
Eric Laurent40b018e2011-06-18 10:10:23 -0700913 pcm_hw_munmap_status(pcm);
914
Liam Girdwood6be28f12011-10-13 12:59:51 -0700915 if (pcm->flags & PCM_MMAP) {
916 pcm_stop(pcm);
917 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
918 }
919
Simon Wilson79d39652011-05-25 13:44:23 -0700920 if (pcm->fd >= 0)
921 close(pcm->fd);
Simon Wilson79d39652011-05-25 13:44:23 -0700922 pcm->buffer_size = 0;
923 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700924 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700925 return 0;
926}
927
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800928/** Opens a PCM by it's name.
929 * @param name The name of the PCM.
930 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
931 * @param flags Specify characteristics and functionality about the pcm.
932 * May be a bitwise AND of the following:
933 * - @ref PCM_IN
934 * - @ref PCM_OUT
935 * - @ref PCM_MMAP
936 * - @ref PCM_NOIRQ
937 * - @ref PCM_MONOTONIC
938 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800939 * @returns A PCM structure.
940 * If an error occurs allocating memory for the PCM, NULL is returned.
941 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
942 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800943 * @ingroup libtinyalsa-pcm
944 */
945struct pcm *pcm_open_by_name(const char *name,
946 unsigned int flags,
947 const struct pcm_config *config)
948{
949 unsigned int card, device;
950 if ((name[0] != 'h')
951 || (name[1] != 'w')
952 || (name[2] != ':')) {
953 return NULL;
954 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
955 return NULL;
956 }
957 return pcm_open(card, device, flags, config);
958}
959
Taylor Holberton6d58e012016-10-01 18:32:30 -0400960/** Opens a PCM.
961 * @param card The card that the pcm belongs to.
962 * The default card is zero.
963 * @param device The device that the pcm belongs to.
964 * The default device is zero.
965 * @param flags Specify characteristics and functionality about the pcm.
966 * May be a bitwise AND of the following:
967 * - @ref PCM_IN
968 * - @ref PCM_OUT
969 * - @ref PCM_MMAP
970 * - @ref PCM_NOIRQ
971 * - @ref PCM_MONOTONIC
972 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800973 * @returns A PCM structure.
974 * If an error occurs allocating memory for the PCM, NULL is returned.
975 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
976 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800977 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400978 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700979struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800980 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700981{
Simon Wilson79d39652011-05-25 13:44:23 -0700982 struct pcm *pcm;
983 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700984 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700985 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700986
987 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400988 if (!pcm)
989 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700990
Simon Wilson1bd580f2011-06-02 15:58:41 -0700991 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
992 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700993
994 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -0400995
996 if (flags & PCM_NONBLOCK)
997 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
998 else
999 pcm->fd = open(fn, O_RDWR);
1000
Simon Wilson79d39652011-05-25 13:44:23 -07001001 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001002 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001003 return pcm;
1004 }
1005
1006 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001007 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001008 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001009 }
David Wagner4cddf192014-04-02 15:12:54 +02001010 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001011
Taylor Holberton861da7a2017-04-10 12:05:51 -07001012 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001013 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001014
Eric Laurent40b018e2011-06-18 10:10:23 -07001015 rc = pcm_hw_mmap_status(pcm);
1016 if (rc < 0) {
1017 oops(pcm, rc, "mmap status failed");
1018 goto fail;
1019 }
1020
Glenn Kasten81012402013-08-22 15:11:48 -07001021#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1022 if (pcm->flags & PCM_MONOTONIC) {
1023 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1024 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1025 if (rc < 0) {
1026 oops(pcm, rc, "cannot set timestamp type");
1027 goto fail;
1028 }
1029 }
1030#endif
1031
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -03001032 /* prepare here so the user does not need to do this later */
1033 if (pcm_prepare(pcm))
1034 goto fail;
1035
Simon Wilson79d39652011-05-25 13:44:23 -07001036 pcm->underruns = 0;
1037 return pcm;
1038
1039fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001040 if (flags & PCM_MMAP)
1041 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1042fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001043 close(pcm->fd);
1044 pcm->fd = -1;
1045 return pcm;
1046}
1047
Taylor Holberton6d58e012016-10-01 18:32:30 -04001048/** Checks if a PCM file has been opened without error.
1049 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001050 * May be NULL.
1051 * @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 -04001052 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001053 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001054 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001055int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001056{
Taylor Holbertone123a652017-01-13 21:39:48 -08001057 if (pcm != NULL) {
1058 return pcm->fd >= 0;
1059 }
1060 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001061}
Simon Wilsond6458e62011-06-21 14:58:11 -07001062
Taylor Holberton558e5942016-12-04 13:42:28 -08001063/** Links two PCMs.
1064 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1065 * If an error occurs, the error message will be written to @p pcm1.
1066 * @param pcm1 A PCM handle.
1067 * @param pcm2 Another PCM handle.
1068 * @return On success, zero; on failure, a negative number.
1069 * @ingroup libtinyalsa-pcm
1070 */
1071int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1072{
1073 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1074 if (err == -1) {
1075 return oops(pcm1, errno, "cannot link PCM");
1076 }
1077 return 0;
1078}
1079
1080/** Unlinks a PCM.
1081 * @see @ref pcm_link
1082 * @param pcm A PCM handle.
1083 * @return On success, zero; on failure, a negative number.
1084 * @ingroup libtinyalsa-pcm
1085 */
1086int pcm_unlink(struct pcm *pcm)
1087{
1088 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1089 if (err == -1) {
1090 return oops(pcm, errno, "cannot unlink PCM");
1091 }
1092 return 0;
1093}
1094
Taylor Holberton6d58e012016-10-01 18:32:30 -04001095/** Prepares a PCM, if it has not been prepared already.
1096 * @param pcm A PCM handle.
1097 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001098 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001099 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301100int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001101{
1102 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1103 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001104
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001105 /* get appl_ptr and avail_min from kernel */
1106 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1107
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301108 return 0;
1109}
1110
Taylor Holberton6d58e012016-10-01 18:32:30 -04001111/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001112 * @param pcm A PCM handle.
1113 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001114 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001115 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301116int pcm_start(struct pcm *pcm)
1117{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001118 /* set appl_ptr and avail_min in kernel */
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001119 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001120
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001121 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
1122 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1123 return oops(pcm, errno, "cannot start channel");
1124 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001125
1126 return 0;
1127}
1128
Taylor Holberton6d58e012016-10-01 18:32:30 -04001129/** Stops a PCM.
1130 * @param pcm A PCM handle.
1131 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001132 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001133 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001134int pcm_stop(struct pcm *pcm)
1135{
1136 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1137 return oops(pcm, errno, "cannot stop channel");
1138
1139 return 0;
1140}
1141
Liam Girdwood6be28f12011-10-13 12:59:51 -07001142static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1143{
1144 int avail;
1145
1146 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1147
1148 if (avail < 0)
1149 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001150 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001151 avail -= pcm->boundary;
1152
1153 return avail;
1154}
1155
1156static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1157{
1158 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1159 if (avail < 0)
1160 avail += pcm->boundary;
1161 return avail;
1162}
1163
1164static inline int pcm_mmap_avail(struct pcm *pcm)
1165{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001166 if (pcm->flags & PCM_IN)
1167 return pcm_mmap_capture_avail(pcm);
1168 else
1169 return pcm_mmap_playback_avail(pcm);
1170}
1171
1172static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1173{
1174 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1175 appl_ptr += frames;
1176
1177 /* check for boundary wrap */
1178 if (appl_ptr > pcm->boundary)
1179 appl_ptr -= pcm->boundary;
1180 pcm->mmap_control->appl_ptr = appl_ptr;
1181}
1182
1183int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1184 unsigned int *frames)
1185{
1186 unsigned int continuous, copy_frames, avail;
1187
1188 /* return the mmap buffer */
1189 *areas = pcm->mmap_buffer;
1190
1191 /* and the application offset in frames */
1192 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1193
1194 avail = pcm_mmap_avail(pcm);
1195 if (avail > pcm->buffer_size)
1196 avail = pcm->buffer_size;
1197 continuous = pcm->buffer_size - *offset;
1198
1199 /* we can only copy frames if the are availabale and continuos */
1200 copy_frames = *frames;
1201 if (copy_frames > avail)
1202 copy_frames = avail;
1203 if (copy_frames > continuous)
1204 copy_frames = continuous;
1205 *frames = copy_frames;
1206
1207 return 0;
1208}
1209
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001210static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1211 char *buf, unsigned int src_offset,
1212 unsigned int frames)
1213{
1214 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1215 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1216 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1217
1218 /* interleaved only atm */
1219 if (pcm->flags & PCM_IN)
1220 memcpy(buf + src_offset_bytes,
1221 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1222 size_bytes);
1223 else
1224 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1225 buf + src_offset_bytes,
1226 size_bytes);
1227 return 0;
1228}
1229
Liam Girdwood6be28f12011-10-13 12:59:51 -07001230int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1231{
Taylor Holberton72e44222016-11-22 09:54:47 -08001232 int ret;
1233
Taylor Holberton73466c02016-10-01 12:51:59 -04001234 /* not used */
1235 (void) offset;
1236
Liam Girdwood6be28f12011-10-13 12:59:51 -07001237 /* update the application pointer in userspace and kernel */
1238 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001239 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001240 if (ret != 0){
1241 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001242 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001243 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001244
1245 return frames;
1246}
1247
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001248static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1249 unsigned int offset, unsigned int size)
1250{
1251 void *pcm_areas;
1252 int commit;
1253 unsigned int pcm_offset, frames, count = 0;
1254
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001255 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001256 frames = size;
1257 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1258 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1259 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1260 if (commit < 0) {
1261 oops(pcm, commit, "failed to commit %d frames\n", frames);
1262 return commit;
1263 }
1264
1265 offset += commit;
1266 count += commit;
1267 size -= commit;
1268 }
1269 return count;
1270}
1271
Liam Girdwood6be28f12011-10-13 12:59:51 -07001272int pcm_avail_update(struct pcm *pcm)
1273{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001274 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001275 return pcm_mmap_avail(pcm);
1276}
1277
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001278/** Returns available frames in pcm buffer and corresponding time stamp.
1279 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1280 * otherwise the clock is CLOCK_REALTIME.
1281 * For an input stream, frames available are frames ready for the application to read.
1282 * For an output stream, frames available are the number of empty frames available for the application to write.
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001283 * @param pcm A PCM handle.
1284 * @param avail The number of available frames
1285 * @param tstamp The timestamp
1286 * @return On success, zero is returned; on failure, negative one.
1287 */
1288int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1289 struct timespec *tstamp)
1290{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001291 int checking;
1292 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001293
1294 if (!pcm_is_ready(pcm))
1295 return -1;
1296
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001297 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001298
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001299again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001300
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001301 tmp = pcm_avail_update(pcm);
1302 if (tmp < 0)
1303 return tmp; /* error */
1304
1305 if (checking && (unsigned int) tmp == *avail)
1306 return 0;
1307
1308 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001309 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001310
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001311 /*
1312 * When status is mmaped, get avail again to ensure
1313 * valid timestamp.
1314 */
1315 if (!pcm->sync_ptr) {
1316 checking = 1;
1317 goto again;
1318 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001319
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001320 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001321 return 0;
1322}
1323
Liam Girdwood6be28f12011-10-13 12:59:51 -07001324int pcm_state(struct pcm *pcm)
1325{
1326 int err = pcm_sync_ptr(pcm, 0);
1327 if (err < 0)
1328 return err;
1329
1330 return pcm->mmap_status->state;
1331}
1332
Taylor Holberton17a10242016-11-23 13:18:24 -08001333/** Waits for frames to be available for read or write operations.
1334 * @param pcm A PCM handle.
1335 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1336 * @returns If frames became available, one is returned.
1337 * If a timeout occured, zero is returned.
1338 * If an error occured, a negative number is returned.
1339 * @ingroup libtinyalsa-pcm
1340 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001341int pcm_wait(struct pcm *pcm, int timeout)
1342{
1343 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001344 int err;
1345
1346 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001347 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001348
1349 do {
1350 /* let's wait for avail or timeout */
1351 err = poll(&pfd, 1, timeout);
1352 if (err < 0)
1353 return -errno;
1354
1355 /* timeout ? */
1356 if (err == 0)
1357 return 0;
1358
1359 /* have we been interrupted ? */
1360 if (errno == -EINTR)
1361 continue;
1362
1363 /* check for any errors */
1364 if (pfd.revents & (POLLERR | POLLNVAL)) {
1365 switch (pcm_state(pcm)) {
1366 case PCM_STATE_XRUN:
1367 return -EPIPE;
1368 case PCM_STATE_SUSPENDED:
1369 return -ESTRPIPE;
1370 case PCM_STATE_DISCONNECTED:
1371 return -ENODEV;
1372 default:
1373 return -EIO;
1374 }
1375 }
1376 /* poll again if fd not ready for IO */
1377 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1378
1379 return 1;
1380}
1381
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001382/*
1383 * Transfer data to/from mmaped buffer. This imitates the
1384 * behavior of read/write system calls.
1385 *
1386 * However, this doesn't seems to offer any advantage over
1387 * the read/write syscalls. Should it be removed?
1388 */
1389int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001390{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001391 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001392
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001393 int state;
1394 unsigned int avail;
1395 unsigned int user_offset;
1396
1397 int err;
1398 int tmp;
1399
1400 is_playback = !(pcm->flags & PCM_IN);
1401
1402 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001403 return 0;
1404
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001405 /* update hardware pointer and get state */
1406 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1407 SNDRV_PCM_SYNC_PTR_APPL |
1408 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1409 if (err == -1)
1410 return -1;
1411 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001412
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001413 /* start capture if frames >= threshold */
1414 if (!is_playback && state == PCM_STATE_PREPARED) {
1415 if (frames >= pcm->config.start_threshold) {
1416 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1417 if (err == -1)
1418 return -1;
1419 /* state = PCM_STATE_RUNNING */
1420 } else {
1421 /* nothing to do */
1422 return 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001423 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001424 }
1425
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001426 avail = pcm_mmap_avail(pcm);
1427 user_offset = 0;
1428
1429 while (frames) {
1430 if (!avail) {
1431 if (pcm->flags & PCM_NONBLOCK) {
1432 errno = EAGAIN;
1433 break;
1434 }
1435
1436 /* wait for interrupt */
1437 err = pcm_wait(pcm, -1);
1438 if (err < 0) {
1439 errno = -err;
1440 break;
1441 }
1442
1443 /* get hardware pointer */
1444 avail = pcm_avail_update(pcm);
1445 }
1446
1447 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1448 if (tmp < 0)
1449 break;
1450
1451 user_offset += tmp;
1452 frames -= tmp;
1453 avail -= tmp;
1454
1455 /* start playback if written >= start_threshold */
1456 if (is_playback && state == PCM_STATE_PREPARED &&
1457 pcm->buffer_size - avail >= pcm->config.start_threshold) {
1458 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1459 if (err == -1)
1460 break;
1461 }
1462 }
1463
1464 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001465}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001466
1467int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1468{
1469 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1470 return -ENOSYS;
1471
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001472 return pcm_mmap_transfer(pcm, (void *)data,
1473 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001474}
1475
1476int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1477{
1478 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1479 return -ENOSYS;
1480
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001481 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001482}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301483
Taylor Holberton17a10242016-11-23 13:18:24 -08001484/** Gets the delay of the PCM, in terms of frames.
1485 * @param pcm A PCM handle.
1486 * @returns On success, the delay of the PCM.
1487 * On failure, a negative number.
1488 * @ingroup libtinyalsa-pcm
1489 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301490long pcm_get_delay(struct pcm *pcm)
1491{
1492 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1493 return -1;
1494
1495 return pcm->pcm_delay;
1496}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001497