blob: eb2f4b843c57b6c5c19ac869c137eef46fe46828 [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
Simon Wilson79d39652011-05-25 13:44:23 -0700588static struct pcm bad_pcm = {
589 .fd = -1,
590};
591
Taylor Holberton6d58e012016-10-01 18:32:30 -0400592/** Gets the hardware parameters of a PCM, without created a PCM handle.
593 * @param card The card of the PCM.
594 * The default card is zero.
595 * @param device The device of the PCM.
596 * The default device is zero.
597 * @param flags Specifies whether the PCM is an input or output.
598 * May be one of the following:
599 * - @ref PCM_IN
600 * - @ref PCM_OUT
601 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800602 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400603 */
Simon Wilson43544882012-10-31 12:52:39 -0700604struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
605 unsigned int flags)
606{
607 struct snd_pcm_hw_params *params;
608 char fn[256];
609 int fd;
610
611 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
612 flags & PCM_IN ? 'c' : 'p');
613
Taylor Holberton093b8782017-10-12 20:28:30 -0400614 if (flags & PCM_NONBLOCK)
615 fd = open(fn, O_RDWR | O_NONBLOCK);
616 else
617 fd = open(fn, O_RDWR);
618
Simon Wilson43544882012-10-31 12:52:39 -0700619 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400620 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700621 goto err_open;
622 }
623
624 params = calloc(1, sizeof(struct snd_pcm_hw_params));
625 if (!params)
626 goto err_calloc;
627
628 param_init(params);
629 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
630 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
631 goto err_hw_refine;
632 }
633
634 close(fd);
635
636 return (struct pcm_params *)params;
637
638err_hw_refine:
639 free(params);
640err_calloc:
641 close(fd);
642err_open:
643 return NULL;
644}
645
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500646/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400647 * @param pcm_params Hardware parameters of a PCM.
648 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800649 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400650 */
Simon Wilson43544882012-10-31 12:52:39 -0700651void pcm_params_free(struct pcm_params *pcm_params)
652{
653 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
654
655 if (params)
656 free(params);
657}
658
659static int pcm_param_to_alsa(enum pcm_param param)
660{
661 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700662 case PCM_PARAM_ACCESS:
663 return SNDRV_PCM_HW_PARAM_ACCESS;
664 case PCM_PARAM_FORMAT:
665 return SNDRV_PCM_HW_PARAM_FORMAT;
666 case PCM_PARAM_SUBFORMAT:
667 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700668 case PCM_PARAM_SAMPLE_BITS:
669 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
670 break;
671 case PCM_PARAM_FRAME_BITS:
672 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
673 break;
674 case PCM_PARAM_CHANNELS:
675 return SNDRV_PCM_HW_PARAM_CHANNELS;
676 break;
677 case PCM_PARAM_RATE:
678 return SNDRV_PCM_HW_PARAM_RATE;
679 break;
680 case PCM_PARAM_PERIOD_TIME:
681 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
682 break;
683 case PCM_PARAM_PERIOD_SIZE:
684 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
685 break;
686 case PCM_PARAM_PERIOD_BYTES:
687 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
688 break;
689 case PCM_PARAM_PERIODS:
690 return SNDRV_PCM_HW_PARAM_PERIODS;
691 break;
692 case PCM_PARAM_BUFFER_TIME:
693 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
694 break;
695 case PCM_PARAM_BUFFER_SIZE:
696 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
697 break;
698 case PCM_PARAM_BUFFER_BYTES:
699 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
700 break;
701 case PCM_PARAM_TICK_TIME:
702 return SNDRV_PCM_HW_PARAM_TICK_TIME;
703 break;
704
705 default:
706 return -1;
707 }
708}
709
Taylor Holberton6d58e012016-10-01 18:32:30 -0400710/** Gets a mask from a PCM's hardware parameters.
711 * @param pcm_params A PCM's hardware parameters.
712 * @param param The parameter to get.
713 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
714 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800715 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400716 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800717const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700718 enum pcm_param param)
719{
720 int p;
721 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
722 if (params == NULL) {
723 return NULL;
724 }
725
726 p = pcm_param_to_alsa(param);
727 if (p < 0 || !param_is_mask(p)) {
728 return NULL;
729 }
730
Taylor Holberton2f387d22016-12-01 15:58:16 -0800731 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700732}
733
Taylor Holberton17a10242016-11-23 13:18:24 -0800734/** Get the minimum of a specified PCM parameter.
735 * @param pcm_params A PCM parameters structure.
736 * @param param The specified parameter to get the minimum of.
737 * @returns On success, the parameter minimum.
738 * On failure, zero.
739 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800740unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700741 enum pcm_param param)
742{
743 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
744 int p;
745
746 if (!params)
747 return 0;
748
749 p = pcm_param_to_alsa(param);
750 if (p < 0)
751 return 0;
752
753 return param_get_min(params, p);
754}
755
Taylor Holberton17a10242016-11-23 13:18:24 -0800756/** Get the maximum of a specified PCM parameter.
757 * @param pcm_params A PCM parameters structure.
758 * @param param The specified parameter to get the maximum of.
759 * @returns On success, the parameter maximum.
760 * On failure, zero.
761 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800762unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700763 enum pcm_param param)
764{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800765 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700766 int p;
767
768 if (!params)
769 return 0;
770
771 p = pcm_param_to_alsa(param);
772 if (p < 0)
773 return 0;
774
775 return param_get_max(params, p);
776}
777
Taylor Holberton6d58e012016-10-01 18:32:30 -0400778/** Closes a PCM returned by @ref pcm_open.
779 * @param pcm A PCM returned by @ref pcm_open.
780 * May not be NULL.
781 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800782 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400783 */
Simon Wilson79d39652011-05-25 13:44:23 -0700784int pcm_close(struct pcm *pcm)
785{
786 if (pcm == &bad_pcm)
787 return 0;
788
Eric Laurent40b018e2011-06-18 10:10:23 -0700789 pcm_hw_munmap_status(pcm);
790
Liam Girdwood6be28f12011-10-13 12:59:51 -0700791 if (pcm->flags & PCM_MMAP) {
792 pcm_stop(pcm);
793 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
794 }
795
Simon Wilson79d39652011-05-25 13:44:23 -0700796 if (pcm->fd >= 0)
797 close(pcm->fd);
Simon Wilson79d39652011-05-25 13:44:23 -0700798 pcm->buffer_size = 0;
799 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700800 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700801 return 0;
802}
803
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800804/** Opens a PCM by it's name.
805 * @param name The name of the PCM.
806 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
807 * @param flags Specify characteristics and functionality about the pcm.
808 * May be a bitwise AND of the following:
809 * - @ref PCM_IN
810 * - @ref PCM_OUT
811 * - @ref PCM_MMAP
812 * - @ref PCM_NOIRQ
813 * - @ref PCM_MONOTONIC
814 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800815 * @returns A PCM structure.
816 * If an error occurs allocating memory for the PCM, NULL is returned.
817 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
818 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800819 * @ingroup libtinyalsa-pcm
820 */
821struct pcm *pcm_open_by_name(const char *name,
822 unsigned int flags,
823 const struct pcm_config *config)
824{
825 unsigned int card, device;
826 if ((name[0] != 'h')
827 || (name[1] != 'w')
828 || (name[2] != ':')) {
829 return NULL;
830 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
831 return NULL;
832 }
833 return pcm_open(card, device, flags, config);
834}
835
Taylor Holberton6d58e012016-10-01 18:32:30 -0400836/** Opens a PCM.
837 * @param card The card that the pcm belongs to.
838 * The default card is zero.
839 * @param device The device that the pcm belongs to.
840 * The default device is zero.
841 * @param flags Specify characteristics and functionality about the pcm.
842 * May be a bitwise AND of the following:
843 * - @ref PCM_IN
844 * - @ref PCM_OUT
845 * - @ref PCM_MMAP
846 * - @ref PCM_NOIRQ
847 * - @ref PCM_MONOTONIC
848 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800849 * @returns A PCM structure.
850 * If an error occurs allocating memory for the PCM, NULL is returned.
851 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
852 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800853 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400854 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700855struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800856 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700857{
Simon Wilson79d39652011-05-25 13:44:23 -0700858 struct pcm *pcm;
859 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700860 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700861 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700862
863 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400864 if (!pcm)
865 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700866
Simon Wilson1bd580f2011-06-02 15:58:41 -0700867 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
868 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700869
870 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -0400871
872 if (flags & PCM_NONBLOCK)
873 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
874 else
875 pcm->fd = open(fn, O_RDWR);
876
Simon Wilson79d39652011-05-25 13:44:23 -0700877 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700878 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700879 return pcm;
880 }
881
882 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700883 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700884 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700885 }
David Wagner4cddf192014-04-02 15:12:54 +0200886 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700887
Taylor Holberton861da7a2017-04-10 12:05:51 -0700888 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700889 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700890
Eric Laurent40b018e2011-06-18 10:10:23 -0700891 rc = pcm_hw_mmap_status(pcm);
892 if (rc < 0) {
893 oops(pcm, rc, "mmap status failed");
894 goto fail;
895 }
896
Glenn Kasten81012402013-08-22 15:11:48 -0700897#ifdef SNDRV_PCM_IOCTL_TTSTAMP
898 if (pcm->flags & PCM_MONOTONIC) {
899 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
900 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
901 if (rc < 0) {
902 oops(pcm, rc, "cannot set timestamp type");
903 goto fail;
904 }
905 }
906#endif
907
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -0300908 /* prepare here so the user does not need to do this later */
909 if (pcm_prepare(pcm))
910 goto fail;
911
Simon Wilson79d39652011-05-25 13:44:23 -0700912 pcm->underruns = 0;
913 return pcm;
914
915fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700916 if (flags & PCM_MMAP)
917 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
918fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700919 close(pcm->fd);
920 pcm->fd = -1;
921 return pcm;
922}
923
Taylor Holberton6d58e012016-10-01 18:32:30 -0400924/** Checks if a PCM file has been opened without error.
925 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -0800926 * May be NULL.
927 * @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 -0400928 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800929 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400930 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800931int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700932{
Taylor Holbertone123a652017-01-13 21:39:48 -0800933 if (pcm != NULL) {
934 return pcm->fd >= 0;
935 }
936 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700937}
Simon Wilsond6458e62011-06-21 14:58:11 -0700938
Taylor Holberton558e5942016-12-04 13:42:28 -0800939/** Links two PCMs.
940 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
941 * If an error occurs, the error message will be written to @p pcm1.
942 * @param pcm1 A PCM handle.
943 * @param pcm2 Another PCM handle.
944 * @return On success, zero; on failure, a negative number.
945 * @ingroup libtinyalsa-pcm
946 */
947int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
948{
949 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
950 if (err == -1) {
951 return oops(pcm1, errno, "cannot link PCM");
952 }
953 return 0;
954}
955
956/** Unlinks a PCM.
957 * @see @ref pcm_link
958 * @param pcm A PCM handle.
959 * @return On success, zero; on failure, a negative number.
960 * @ingroup libtinyalsa-pcm
961 */
962int pcm_unlink(struct pcm *pcm)
963{
964 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
965 if (err == -1) {
966 return oops(pcm, errno, "cannot unlink PCM");
967 }
968 return 0;
969}
970
Taylor Holberton6d58e012016-10-01 18:32:30 -0400971/** Prepares a PCM, if it has not been prepared already.
972 * @param pcm A PCM handle.
973 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800974 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400975 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530976int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700977{
978 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
979 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700980
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -0200981 /* get appl_ptr and avail_min from kernel */
982 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
983
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530984 return 0;
985}
986
Taylor Holberton6d58e012016-10-01 18:32:30 -0400987/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400988 * @param pcm A PCM handle.
989 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800990 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400991 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530992int pcm_start(struct pcm *pcm)
993{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -0200994 /* set appl_ptr and avail_min in kernel */
Miguel Gaiocf5f0632018-07-17 13:30:24 +0200995 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700996
Miguel Gaiocf5f0632018-07-17 13:30:24 +0200997 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
998 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
999 return oops(pcm, errno, "cannot start channel");
1000 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001001
1002 return 0;
1003}
1004
Taylor Holberton6d58e012016-10-01 18:32:30 -04001005/** Stops a PCM.
1006 * @param pcm A PCM handle.
1007 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001008 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001009 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001010int pcm_stop(struct pcm *pcm)
1011{
1012 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1013 return oops(pcm, errno, "cannot stop channel");
1014
1015 return 0;
1016}
1017
Liam Girdwood6be28f12011-10-13 12:59:51 -07001018static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1019{
1020 int avail;
1021
1022 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1023
1024 if (avail < 0)
1025 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001026 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001027 avail -= pcm->boundary;
1028
1029 return avail;
1030}
1031
1032static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1033{
1034 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1035 if (avail < 0)
1036 avail += pcm->boundary;
1037 return avail;
1038}
1039
1040static inline int pcm_mmap_avail(struct pcm *pcm)
1041{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001042 if (pcm->flags & PCM_IN)
1043 return pcm_mmap_capture_avail(pcm);
1044 else
1045 return pcm_mmap_playback_avail(pcm);
1046}
1047
1048static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1049{
1050 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1051 appl_ptr += frames;
1052
1053 /* check for boundary wrap */
1054 if (appl_ptr > pcm->boundary)
1055 appl_ptr -= pcm->boundary;
1056 pcm->mmap_control->appl_ptr = appl_ptr;
1057}
1058
1059int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1060 unsigned int *frames)
1061{
1062 unsigned int continuous, copy_frames, avail;
1063
1064 /* return the mmap buffer */
1065 *areas = pcm->mmap_buffer;
1066
1067 /* and the application offset in frames */
1068 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1069
1070 avail = pcm_mmap_avail(pcm);
1071 if (avail > pcm->buffer_size)
1072 avail = pcm->buffer_size;
1073 continuous = pcm->buffer_size - *offset;
1074
1075 /* we can only copy frames if the are availabale and continuos */
1076 copy_frames = *frames;
1077 if (copy_frames > avail)
1078 copy_frames = avail;
1079 if (copy_frames > continuous)
1080 copy_frames = continuous;
1081 *frames = copy_frames;
1082
1083 return 0;
1084}
1085
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001086static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1087 char *buf, unsigned int src_offset,
1088 unsigned int frames)
1089{
1090 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1091 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1092 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1093
1094 /* interleaved only atm */
1095 if (pcm->flags & PCM_IN)
1096 memcpy(buf + src_offset_bytes,
1097 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1098 size_bytes);
1099 else
1100 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1101 buf + src_offset_bytes,
1102 size_bytes);
1103 return 0;
1104}
1105
Liam Girdwood6be28f12011-10-13 12:59:51 -07001106int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1107{
Taylor Holberton72e44222016-11-22 09:54:47 -08001108 int ret;
1109
Taylor Holberton73466c02016-10-01 12:51:59 -04001110 /* not used */
1111 (void) offset;
1112
Liam Girdwood6be28f12011-10-13 12:59:51 -07001113 /* update the application pointer in userspace and kernel */
1114 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001115 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001116 if (ret != 0){
1117 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001118 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001119 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001120
1121 return frames;
1122}
1123
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001124static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1125 unsigned int offset, unsigned int size)
1126{
1127 void *pcm_areas;
1128 int commit;
1129 unsigned int pcm_offset, frames, count = 0;
1130
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001131 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001132 frames = size;
1133 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1134 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1135 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1136 if (commit < 0) {
1137 oops(pcm, commit, "failed to commit %d frames\n", frames);
1138 return commit;
1139 }
1140
1141 offset += commit;
1142 count += commit;
1143 size -= commit;
1144 }
1145 return count;
1146}
1147
Liam Girdwood6be28f12011-10-13 12:59:51 -07001148int pcm_avail_update(struct pcm *pcm)
1149{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001150 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001151 return pcm_mmap_avail(pcm);
1152}
1153
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001154/** Returns available frames in pcm buffer and corresponding time stamp.
1155 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1156 * otherwise the clock is CLOCK_REALTIME.
1157 * For an input stream, frames available are frames ready for the application to read.
1158 * 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 -02001159 * @param pcm A PCM handle.
1160 * @param avail The number of available frames
1161 * @param tstamp The timestamp
1162 * @return On success, zero is returned; on failure, negative one.
1163 */
1164int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1165 struct timespec *tstamp)
1166{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001167 int checking;
1168 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001169
1170 if (!pcm_is_ready(pcm))
1171 return -1;
1172
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001173 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001174
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001175again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001176
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001177 tmp = pcm_avail_update(pcm);
1178 if (tmp < 0)
1179 return tmp; /* error */
1180
1181 if (checking && (unsigned int) tmp == *avail)
1182 return 0;
1183
1184 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001185 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001186
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001187 /*
1188 * When status is mmaped, get avail again to ensure
1189 * valid timestamp.
1190 */
1191 if (!pcm->sync_ptr) {
1192 checking = 1;
1193 goto again;
1194 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001195
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001196 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001197 return 0;
1198}
1199
Liam Girdwood6be28f12011-10-13 12:59:51 -07001200int pcm_state(struct pcm *pcm)
1201{
1202 int err = pcm_sync_ptr(pcm, 0);
1203 if (err < 0)
1204 return err;
1205
1206 return pcm->mmap_status->state;
1207}
1208
Taylor Holberton17a10242016-11-23 13:18:24 -08001209/** Waits for frames to be available for read or write operations.
1210 * @param pcm A PCM handle.
1211 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1212 * @returns If frames became available, one is returned.
1213 * If a timeout occured, zero is returned.
1214 * If an error occured, a negative number is returned.
1215 * @ingroup libtinyalsa-pcm
1216 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001217int pcm_wait(struct pcm *pcm, int timeout)
1218{
1219 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001220 int err;
1221
1222 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001223 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001224
1225 do {
1226 /* let's wait for avail or timeout */
1227 err = poll(&pfd, 1, timeout);
1228 if (err < 0)
1229 return -errno;
1230
1231 /* timeout ? */
1232 if (err == 0)
1233 return 0;
1234
1235 /* have we been interrupted ? */
1236 if (errno == -EINTR)
1237 continue;
1238
1239 /* check for any errors */
1240 if (pfd.revents & (POLLERR | POLLNVAL)) {
1241 switch (pcm_state(pcm)) {
1242 case PCM_STATE_XRUN:
1243 return -EPIPE;
1244 case PCM_STATE_SUSPENDED:
1245 return -ESTRPIPE;
1246 case PCM_STATE_DISCONNECTED:
1247 return -ENODEV;
1248 default:
1249 return -EIO;
1250 }
1251 }
1252 /* poll again if fd not ready for IO */
1253 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1254
1255 return 1;
1256}
1257
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001258/*
1259 * Transfer data to/from mmaped buffer. This imitates the
1260 * behavior of read/write system calls.
1261 *
1262 * However, this doesn't seems to offer any advantage over
1263 * the read/write syscalls. Should it be removed?
1264 */
1265int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001266{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001267 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001268
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001269 int state;
1270 unsigned int avail;
1271 unsigned int user_offset;
1272
1273 int err;
1274 int tmp;
1275
1276 is_playback = !(pcm->flags & PCM_IN);
1277
1278 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001279 return 0;
1280
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001281 /* update hardware pointer and get state */
1282 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1283 SNDRV_PCM_SYNC_PTR_APPL |
1284 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1285 if (err == -1)
1286 return -1;
1287 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001288
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001289 /* start capture if frames >= threshold */
1290 if (!is_playback && state == PCM_STATE_PREPARED) {
1291 if (frames >= pcm->config.start_threshold) {
1292 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1293 if (err == -1)
1294 return -1;
1295 /* state = PCM_STATE_RUNNING */
1296 } else {
1297 /* nothing to do */
1298 return 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001299 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001300 }
1301
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001302 avail = pcm_mmap_avail(pcm);
1303 user_offset = 0;
1304
1305 while (frames) {
1306 if (!avail) {
1307 if (pcm->flags & PCM_NONBLOCK) {
1308 errno = EAGAIN;
1309 break;
1310 }
1311
1312 /* wait for interrupt */
1313 err = pcm_wait(pcm, -1);
1314 if (err < 0) {
1315 errno = -err;
1316 break;
1317 }
1318
1319 /* get hardware pointer */
1320 avail = pcm_avail_update(pcm);
1321 }
1322
1323 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1324 if (tmp < 0)
1325 break;
1326
1327 user_offset += tmp;
1328 frames -= tmp;
1329 avail -= tmp;
1330
1331 /* start playback if written >= start_threshold */
1332 if (is_playback && state == PCM_STATE_PREPARED &&
1333 pcm->buffer_size - avail >= pcm->config.start_threshold) {
1334 err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_START);
1335 if (err == -1)
1336 break;
1337 }
1338 }
1339
1340 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001341}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001342
1343int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1344{
1345 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1346 return -ENOSYS;
1347
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001348 return pcm_mmap_transfer(pcm, (void *)data,
1349 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001350}
1351
1352int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1353{
1354 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1355 return -ENOSYS;
1356
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001357 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001358}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301359
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001360static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1361{
1362 int is_playback;
1363
1364 struct snd_xferi transfer;
1365 int res;
1366
1367 is_playback = !(pcm->flags & PCM_IN);
1368
1369 transfer.buf = data;
1370 transfer.frames = frames;
1371 transfer.result = 0;
1372
1373 res = ioctl(pcm->fd, is_playback
1374 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1375 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
1376
1377 return res == 0 ? (int) transfer.result : -1;
1378}
1379
1380static int pcm_generic_transfer(struct pcm *pcm, void *data,
1381 unsigned int frames)
1382{
1383 int res;
1384
1385#if UINT_MAX > TINYALSA_FRAMES_MAX
1386 if (frames > TINYALSA_FRAMES_MAX)
1387 return -EINVAL;
1388#endif
1389 if (frames > INT_MAX)
1390 return -EINVAL;
1391
1392again:
1393
1394 if (pcm->flags & PCM_MMAP)
1395 res = pcm_mmap_transfer(pcm, data, frames);
1396 else
1397 res = pcm_rw_transfer(pcm, data, frames);
1398
1399 if (res < 0) {
1400 switch (errno) {
1401 case EPIPE:
1402 pcm->underruns++;
1403 /* fallthrough */
1404 case ESTRPIPE:
1405 /*
1406 * Try to restart if we are allowed to do so.
1407 * Otherwise, return error.
1408 */
1409 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1410 return -1;
1411 goto again;
1412 case EAGAIN:
1413 if (pcm->flags & PCM_NONBLOCK)
1414 return -1;
1415 /* fallthrough */
1416 default:
1417 return oops(pcm, errno, "cannot read/write stream data");
1418 }
1419 }
1420
1421 return res;
1422}
1423
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001424/** Writes audio samples to PCM.
1425 * If the PCM has not been started, it is started in this function.
1426 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1427 * @param pcm A PCM handle.
1428 * @param data The audio sample array
1429 * @param frame_count The number of frames occupied by the sample array.
1430 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1431 * or INT_MAX.
1432 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1433 * @ingroup libtinyalsa-pcm
1434 */
1435int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1436{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001437 if (pcm->flags & PCM_IN)
1438 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001439
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001440 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001441}
1442
1443/** Reads audio samples from PCM.
1444 * If the PCM has not been started, it is started in this function.
1445 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1446 * @param pcm A PCM handle.
1447 * @param data The audio sample array
1448 * @param frame_count The number of frames occupied by the sample array.
1449 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1450 * or INT_MAX.
1451 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1452 * @ingroup libtinyalsa-pcm
1453 */
1454int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1455{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001456 if (!(pcm->flags & PCM_IN))
1457 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001458
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001459 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001460}
1461
1462/** Writes audio samples to PCM.
1463 * If the PCM has not been started, it is started in this function.
1464 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1465 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1466 * @param pcm A PCM handle.
1467 * @param data The audio sample array
1468 * @param count The number of bytes occupied by the sample array.
1469 * @return On success, this function returns zero; otherwise, a negative number.
1470 * @deprecated
1471 * @ingroup libtinyalsa-pcm
1472 */
1473int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1474{
1475 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
1476}
1477
1478/** Reads audio samples from PCM.
1479 * If the PCM has not been started, it is started in this function.
1480 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1481 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1482 * @param pcm A PCM handle.
1483 * @param data The audio sample array
1484 * @param count The number of bytes occupied by the sample array.
1485 * @return On success, this function returns zero; otherwise, a negative number.
1486 * @deprecated
1487 * @ingroup libtinyalsa-pcm
1488 */
1489int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1490{
1491 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
1492}
1493
Taylor Holberton17a10242016-11-23 13:18:24 -08001494/** Gets the delay of the PCM, in terms of frames.
1495 * @param pcm A PCM handle.
1496 * @returns On success, the delay of the PCM.
1497 * On failure, a negative number.
1498 * @ingroup libtinyalsa-pcm
1499 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301500long pcm_get_delay(struct pcm *pcm)
1501{
1502 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1503 return -1;
1504
1505 return pcm->pcm_delay;
1506}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001507