blob: c3807a71694fac0ec5dd99e4b001850bd5e98e5b [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>
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -070062#include "pcm_io.h"
63#include "snd_card_plugin.h"
Simon Wilson79d39652011-05-25 13:44:23 -070064
Taylor Holberton1f741562018-11-28 16:33:24 -050065#ifndef PARAM_MAX
Simon Wilson79d39652011-05-25 13:44:23 -070066#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Taylor Holberton1f741562018-11-28 16:33:24 -050067#endif /* PARAM_MAX */
68
69#ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
Liam Girdwood6be28f12011-10-13 12:59:51 -070070#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Taylor Holberton1f741562018-11-28 16:33:24 -050071#endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */
Simon Wilson79d39652011-05-25 13:44:23 -070072
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -070073
Simon Wilson79d39652011-05-25 13:44:23 -070074static inline int param_is_mask(int p)
75{
76 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
77 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
78}
79
80static inline int param_is_interval(int p)
81{
82 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
83 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
84}
85
Taylor Holberton2f387d22016-12-01 15:58:16 -080086static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
87{
Taylor Holberton25976dc2017-04-10 11:46:40 -070088 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080089}
90
Simon Wilson79d39652011-05-25 13:44:23 -070091static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
92{
93 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
94}
95
96static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
97{
98 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
99}
100
101static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
102{
103 if (bit >= SNDRV_MASK_MAX)
104 return;
105 if (param_is_mask(n)) {
106 struct snd_mask *m = param_to_mask(p, n);
107 m->bits[0] = 0;
108 m->bits[1] = 0;
109 m->bits[bit >> 5] |= (1 << (bit & 31));
110 }
111}
112
113static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
114{
115 if (param_is_interval(n)) {
116 struct snd_interval *i = param_to_interval(p, n);
117 i->min = val;
118 }
119}
120
Taylor Holberton2f387d22016-12-01 15:58:16 -0800121static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700122{
123 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800124 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700125 return i->min;
126 }
127 return 0;
128}
129
Taylor Holberton2f387d22016-12-01 15:58:16 -0800130static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700131{
132 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800133 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700134 return i->max;
135 }
136 return 0;
137}
138
Simon Wilson79d39652011-05-25 13:44:23 -0700139static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
140{
141 if (param_is_interval(n)) {
142 struct snd_interval *i = param_to_interval(p, n);
143 i->min = val;
144 i->max = val;
145 i->integer = 1;
146 }
147}
148
Liam Girdwood6be28f12011-10-13 12:59:51 -0700149static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
150{
151 if (param_is_interval(n)) {
152 struct snd_interval *i = param_to_interval(p, n);
153 if (i->integer)
154 return i->max;
155 }
156 return 0;
157}
158
Simon Wilson79d39652011-05-25 13:44:23 -0700159static void param_init(struct snd_pcm_hw_params *p)
160{
161 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700162
Simon Wilson79d39652011-05-25 13:44:23 -0700163 memset(p, 0, sizeof(*p));
164 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
165 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
166 struct snd_mask *m = param_to_mask(p, n);
167 m->bits[0] = ~0;
168 m->bits[1] = ~0;
169 }
170 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
171 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
172 struct snd_interval *i = param_to_interval(p, n);
173 i->min = 0;
174 i->max = ~0;
175 }
Simon Wilson43544882012-10-31 12:52:39 -0700176 p->rmask = ~0U;
177 p->cmask = 0;
178 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700179}
180
Taylor Holberton861da7a2017-04-10 12:05:51 -0700181static unsigned int pcm_format_to_alsa(enum pcm_format format)
182{
183 switch (format) {
184
185 case PCM_FORMAT_S8:
186 return SNDRV_PCM_FORMAT_S8;
187
188 default:
189 case PCM_FORMAT_S16_LE:
190 return SNDRV_PCM_FORMAT_S16_LE;
191 case PCM_FORMAT_S16_BE:
192 return SNDRV_PCM_FORMAT_S16_BE;
193
194 case PCM_FORMAT_S24_LE:
195 return SNDRV_PCM_FORMAT_S24_LE;
196 case PCM_FORMAT_S24_BE:
197 return SNDRV_PCM_FORMAT_S24_BE;
198
199 case PCM_FORMAT_S24_3LE:
200 return SNDRV_PCM_FORMAT_S24_3LE;
201 case PCM_FORMAT_S24_3BE:
202 return SNDRV_PCM_FORMAT_S24_3BE;
203
204 case PCM_FORMAT_S32_LE:
205 return SNDRV_PCM_FORMAT_S32_LE;
206 case PCM_FORMAT_S32_BE:
207 return SNDRV_PCM_FORMAT_S32_BE;
208 };
209}
210
Simon Wilson79d39652011-05-25 13:44:23 -0700211#define PCM_ERROR_MAX 128
212
Taylor Holberton6d58e012016-10-01 18:32:30 -0400213/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800214 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400215 */
Simon Wilson79d39652011-05-25 13:44:23 -0700216struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400217 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700218 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400219 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700220 unsigned int flags;
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200221 /** The number of (under/over)runs that have occured */
222 int xruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400223 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700224 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800225 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700226 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400227 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700228 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400229 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700230 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700231 struct snd_pcm_mmap_status *mmap_status;
232 struct snd_pcm_mmap_control *mmap_control;
233 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700234 void *mmap_buffer;
235 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800236 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530237 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800238 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200239 unsigned int subdevice;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700240 /** Pointer to the pcm ops, either hw or plugin */
241 const struct pcm_ops *ops;
242 /** Private data for pcm_hw or pcm_plugin */
243 void *data;
244 /** Pointer to the pcm node from snd card definition */
245 struct snd_node *snd_node;
Simon Wilson79d39652011-05-25 13:44:23 -0700246};
247
Taylor Holberton861da7a2017-04-10 12:05:51 -0700248static int oops(struct pcm *pcm, int e, const char *fmt, ...)
249{
250 va_list ap;
251 int sz;
252
253 va_start(ap, fmt);
254 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
255 va_end(ap);
256 sz = strlen(pcm->error);
257
258 if (errno)
259 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
260 ": %s", strerror(e));
261 return -1;
262}
263
Taylor Holberton6d58e012016-10-01 18:32:30 -0400264/** Gets the buffer size of the PCM.
265 * @param pcm A PCM handle.
266 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800267 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400268 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800269unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700270{
271 return pcm->buffer_size;
272}
273
Taylor Holberton77979a82016-12-01 20:04:04 -0800274/** Gets the channel count of the PCM.
275 * @param pcm A PCM handle.
276 * @return The channel count of the PCM.
277 * @ingroup libtinyalsa-pcm
278 */
279unsigned int pcm_get_channels(const struct pcm *pcm)
280{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700281 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800282}
283
Taylor Holberton08bb5902017-04-10 11:45:44 -0700284/** Gets the PCM configuration.
285 * @param pcm A PCM handle.
286 * @return The PCM configuration.
287 * This function only returns NULL if
288 * @p pcm is NULL.
289 * @ingroup libtinyalsa-pcm
290 * */
291const struct pcm_config * pcm_get_config(const struct pcm *pcm)
292{
293 if (pcm == NULL)
294 return NULL;
295 return &pcm->config;
296}
297
Taylor Holberton77979a82016-12-01 20:04:04 -0800298/** Gets the rate of the PCM.
299 * The rate is given in frames per second.
300 * @param pcm A PCM handle.
301 * @return The rate of the PCM.
302 * @ingroup libtinyalsa-pcm
303 */
304unsigned int pcm_get_rate(const struct pcm *pcm)
305{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700306 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800307}
308
309/** Gets the format of the PCM.
310 * @param pcm A PCM handle.
311 * @return The format of the PCM.
312 * @ingroup libtinyalsa-pcm
313 */
314enum pcm_format pcm_get_format(const struct pcm *pcm)
315{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700316 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800317}
318
Taylor Holberton6d58e012016-10-01 18:32:30 -0400319/** Gets the file descriptor of the PCM.
320 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800321 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400322 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800323 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400324 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800325int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400326{
327 return pcm->fd;
328}
329
Taylor Holberton6d58e012016-10-01 18:32:30 -0400330/** Gets the error message for the last error that occured.
331 * If no error occured and this function is called, the results are undefined.
332 * @param pcm A PCM handle.
333 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800334 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400335 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800336const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700337{
338 return pcm->error;
339}
340
Taylor Holberton861da7a2017-04-10 12:05:51 -0700341/** Sets the PCM configuration.
342 * @param pcm A PCM handle.
343 * @param config The configuration to use for the
344 * PCM. This parameter may be NULL, in which case
345 * the default configuration is used.
346 * @returns Zero on success, a negative errno value
347 * on failure.
348 * @ingroup libtinyalsa-pcm
349 * */
350int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
351{
352 if (pcm == NULL)
353 return -EFAULT;
354 else if (config == NULL) {
355 config = &pcm->config;
356 pcm->config.channels = 2;
357 pcm->config.rate = 48000;
358 pcm->config.period_size = 1024;
359 pcm->config.period_count = 4;
360 pcm->config.format = PCM_FORMAT_S16_LE;
361 pcm->config.start_threshold = config->period_count * config->period_size;
362 pcm->config.stop_threshold = config->period_count * config->period_size;
363 pcm->config.silence_threshold = 0;
364 } else
365 pcm->config = *config;
366
367 struct snd_pcm_hw_params params;
368 param_init(&params);
369 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
370 pcm_format_to_alsa(config->format));
Taylor Holberton861da7a2017-04-10 12:05:51 -0700371 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700372 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
373 config->channels);
374 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
375 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
376
377 if (pcm->flags & PCM_NOIRQ) {
378
379 if (!(pcm->flags & PCM_MMAP)) {
380 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
381 return -EINVAL;
382 }
383
384 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
385 pcm->noirq_frames_per_msec = config->rate / 1000;
386 }
387
388 if (pcm->flags & PCM_MMAP)
389 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
390 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
391 else
392 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
393 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
394
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700395 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700396 int errno_copy = errno;
397 oops(pcm, -errno, "cannot set hw params");
398 return -errno_copy;
399 }
400
401 /* get our refined hw_params */
402 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
403 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
404 pcm->buffer_size = config->period_count * config->period_size;
405
406 if (pcm->flags & PCM_MMAP) {
407 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
408 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
409 if (pcm->mmap_buffer == MAP_FAILED) {
410 int errno_copy = errno;
411 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
412 pcm_frames_to_bytes(pcm, pcm->buffer_size));
413 return -errno_copy;
414 }
415 }
416
417 struct snd_pcm_sw_params sparams;
418 memset(&sparams, 0, sizeof(sparams));
419 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
420 sparams.period_step = 1;
Miguel GAIO05c64c32020-02-06 21:09:31 +0100421 sparams.avail_min = config->period_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700422
423 if (!config->start_threshold) {
424 if (pcm->flags & PCM_IN)
425 pcm->config.start_threshold = sparams.start_threshold = 1;
426 else
427 pcm->config.start_threshold = sparams.start_threshold =
428 config->period_count * config->period_size / 2;
429 } else
430 sparams.start_threshold = config->start_threshold;
431
432 /* pick a high stop threshold - todo: does this need further tuning */
433 if (!config->stop_threshold) {
434 if (pcm->flags & PCM_IN)
435 pcm->config.stop_threshold = sparams.stop_threshold =
436 config->period_count * config->period_size * 10;
437 else
438 pcm->config.stop_threshold = sparams.stop_threshold =
439 config->period_count * config->period_size;
440 }
441 else
442 sparams.stop_threshold = config->stop_threshold;
443
444 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
445 sparams.silence_size = 0;
446 sparams.silence_threshold = config->silence_threshold;
447 pcm->boundary = sparams.boundary = pcm->buffer_size;
448
449 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
450 pcm->boundary *= 2;
451
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700452 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700453 int errno_copy = errno;
454 oops(pcm, -errno, "cannot set sw params");
455 return -errno_copy;
456 }
457
458 return 0;
459}
460
Taylor Holberton6d58e012016-10-01 18:32:30 -0400461/** Gets the subdevice on which the pcm has been opened.
462 * @param pcm A PCM handle.
463 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800464unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200465{
466 return pcm->subdevice;
467}
468
Taylor Holberton6d58e012016-10-01 18:32:30 -0400469/** Determines the number of bits occupied by a @ref pcm_format.
470 * @param format A PCM format.
471 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800472 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400473 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700474unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700475{
476 switch (format) {
477 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400478 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700479 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400480 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700481 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400482 case PCM_FORMAT_S24_3LE:
483 case PCM_FORMAT_S24_3BE:
484 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700485 default:
486 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400487 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700488 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400489 case PCM_FORMAT_S8:
490 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700491 };
492}
493
Taylor Holberton6d58e012016-10-01 18:32:30 -0400494/** Determines how many frames of a PCM can fit into a number of bytes.
495 * @param pcm A PCM handle.
496 * @param bytes The number of bytes.
497 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800498 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400499 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800500unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700501{
502 return bytes / (pcm->config.channels *
503 (pcm_format_to_bits(pcm->config.format) >> 3));
504}
505
Taylor Holberton6d58e012016-10-01 18:32:30 -0400506/** Determines how many bytes are occupied by a number of frames of a PCM.
507 * @param pcm A PCM handle.
508 * @param frames The number of frames of a PCM.
509 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800510 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400511 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800512unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700513{
514 return frames * pcm->config.channels *
515 (pcm_format_to_bits(pcm->config.format) >> 3);
516}
517
Taylor Holberton4f556062016-09-16 09:54:36 -0400518static int pcm_sync_ptr(struct pcm *pcm, int flags)
519{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200520 if (pcm->sync_ptr == NULL) {
521 /* status and control are mmaped */
522
523 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
524 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
525 oops(pcm, errno, "failed to sync hardware pointer");
526 return -1;
527 }
528 }
529 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700530 pcm->sync_ptr->flags = flags;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700531 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
532 pcm->sync_ptr) < 0) {
Taylor Holbertone123a652017-01-13 21:39:48 -0800533 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700534 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800535 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700536 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200537
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200538 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700539}
540
Taylor Holberton4f556062016-09-16 09:54:36 -0400541static int pcm_hw_mmap_status(struct pcm *pcm)
542{
Eric Laurent40b018e2011-06-18 10:10:23 -0700543 if (pcm->sync_ptr)
544 return 0;
545
546 int page_size = sysconf(_SC_PAGE_SIZE);
547 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
548 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
549 if (pcm->mmap_status == MAP_FAILED)
550 pcm->mmap_status = NULL;
551 if (!pcm->mmap_status)
552 goto mmap_error;
553
554 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
555 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
556 if (pcm->mmap_control == MAP_FAILED)
557 pcm->mmap_control = NULL;
558 if (!pcm->mmap_control) {
559 munmap(pcm->mmap_status, page_size);
560 pcm->mmap_status = NULL;
561 goto mmap_error;
562 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700563
564 return 0;
565
566mmap_error:
567
568 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
569 if (!pcm->sync_ptr)
570 return -ENOMEM;
571 pcm->mmap_status = &pcm->sync_ptr->s.status;
572 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700573
574 return 0;
575}
576
577static void pcm_hw_munmap_status(struct pcm *pcm) {
578 if (pcm->sync_ptr) {
579 free(pcm->sync_ptr);
580 pcm->sync_ptr = NULL;
581 } else {
582 int page_size = sysconf(_SC_PAGE_SIZE);
583 if (pcm->mmap_status)
584 munmap(pcm->mmap_status, page_size);
585 if (pcm->mmap_control)
586 munmap(pcm->mmap_control, page_size);
587 }
588 pcm->mmap_status = NULL;
589 pcm->mmap_control = NULL;
590}
591
Simon Wilson79d39652011-05-25 13:44:23 -0700592static struct pcm bad_pcm = {
593 .fd = -1,
594};
595
Taylor Holberton6d58e012016-10-01 18:32:30 -0400596/** Gets the hardware parameters of a PCM, without created a PCM handle.
597 * @param card The card of the PCM.
598 * The default card is zero.
599 * @param device The device of the PCM.
600 * The default device is zero.
601 * @param flags Specifies whether the PCM is an input or output.
602 * May be one of the following:
603 * - @ref PCM_IN
604 * - @ref PCM_OUT
605 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800606 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400607 */
Simon Wilson43544882012-10-31 12:52:39 -0700608struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
609 unsigned int flags)
610{
611 struct snd_pcm_hw_params *params;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700612 void *snd_node = NULL, *data;
613 const struct pcm_ops *ops;
Simon Wilson43544882012-10-31 12:52:39 -0700614 int fd;
615
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700616 ops = &hw_ops;
617 fd = ops->open(card, device, flags, &data, snd_node);
Simon Wilson43544882012-10-31 12:52:39 -0700618
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700619#ifdef TINYALSA_USES_PLUGINS
Simon Wilson43544882012-10-31 12:52:39 -0700620 if (fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700621 int pcm_type;
622 snd_node = snd_utils_open_pcm(card, device);
623 pcm_type = snd_utils_get_node_type(snd_node);
624 if (!snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
625 fprintf(stderr, "no device (hw/plugin) for card(%u), device(%u)",
626 card, device);
627 goto err_open;
628 }
629 ops = &plug_ops;
630 fd = ops->open(card, device, flags, &data, snd_node);
631 }
632#endif
633 if (fd < 0) {
634 fprintf(stderr, "cannot open card(%d) device (%d): %s\n",
635 card, device, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700636 goto err_open;
637 }
638
639 params = calloc(1, sizeof(struct snd_pcm_hw_params));
640 if (!params)
641 goto err_calloc;
642
643 param_init(params);
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700644 if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
Simon Wilson43544882012-10-31 12:52:39 -0700645 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
646 goto err_hw_refine;
647 }
648
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700649#ifdef TINYALSA_USES_PLUGINS
650 if (snd_node)
651 snd_utils_close_dev_node(snd_node);
652#endif
653 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700654
655 return (struct pcm_params *)params;
656
657err_hw_refine:
658 free(params);
659err_calloc:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700660#ifdef TINYALSA_USES_PLUGINS
661 if (snd_node)
662 snd_utils_close_dev_node(snd_node);
663#endif
664 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700665err_open:
666 return NULL;
667}
668
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500669/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400670 * @param pcm_params Hardware parameters of a PCM.
671 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800672 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400673 */
Simon Wilson43544882012-10-31 12:52:39 -0700674void pcm_params_free(struct pcm_params *pcm_params)
675{
676 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
677
678 if (params)
679 free(params);
680}
681
682static int pcm_param_to_alsa(enum pcm_param param)
683{
684 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700685 case PCM_PARAM_ACCESS:
686 return SNDRV_PCM_HW_PARAM_ACCESS;
687 case PCM_PARAM_FORMAT:
688 return SNDRV_PCM_HW_PARAM_FORMAT;
689 case PCM_PARAM_SUBFORMAT:
690 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700691 case PCM_PARAM_SAMPLE_BITS:
692 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
693 break;
694 case PCM_PARAM_FRAME_BITS:
695 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
696 break;
697 case PCM_PARAM_CHANNELS:
698 return SNDRV_PCM_HW_PARAM_CHANNELS;
699 break;
700 case PCM_PARAM_RATE:
701 return SNDRV_PCM_HW_PARAM_RATE;
702 break;
703 case PCM_PARAM_PERIOD_TIME:
704 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
705 break;
706 case PCM_PARAM_PERIOD_SIZE:
707 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
708 break;
709 case PCM_PARAM_PERIOD_BYTES:
710 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
711 break;
712 case PCM_PARAM_PERIODS:
713 return SNDRV_PCM_HW_PARAM_PERIODS;
714 break;
715 case PCM_PARAM_BUFFER_TIME:
716 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
717 break;
718 case PCM_PARAM_BUFFER_SIZE:
719 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
720 break;
721 case PCM_PARAM_BUFFER_BYTES:
722 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
723 break;
724 case PCM_PARAM_TICK_TIME:
725 return SNDRV_PCM_HW_PARAM_TICK_TIME;
726 break;
727
728 default:
729 return -1;
730 }
731}
732
Taylor Holberton6d58e012016-10-01 18:32:30 -0400733/** Gets a mask from a PCM's hardware parameters.
734 * @param pcm_params A PCM's hardware parameters.
735 * @param param The parameter to get.
736 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
737 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800738 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400739 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800740const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700741 enum pcm_param param)
742{
743 int p;
744 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
745 if (params == NULL) {
746 return NULL;
747 }
748
749 p = pcm_param_to_alsa(param);
750 if (p < 0 || !param_is_mask(p)) {
751 return NULL;
752 }
753
Taylor Holberton2f387d22016-12-01 15:58:16 -0800754 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700755}
756
Taylor Holberton17a10242016-11-23 13:18:24 -0800757/** Get the minimum of a specified PCM parameter.
758 * @param pcm_params A PCM parameters structure.
759 * @param param The specified parameter to get the minimum of.
760 * @returns On success, the parameter minimum.
761 * On failure, zero.
762 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800763unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700764 enum pcm_param param)
765{
766 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
767 int p;
768
769 if (!params)
770 return 0;
771
772 p = pcm_param_to_alsa(param);
773 if (p < 0)
774 return 0;
775
776 return param_get_min(params, p);
777}
778
Taylor Holberton17a10242016-11-23 13:18:24 -0800779/** Get the maximum of a specified PCM parameter.
780 * @param pcm_params A PCM parameters structure.
781 * @param param The specified parameter to get the maximum of.
782 * @returns On success, the parameter maximum.
783 * On failure, zero.
784 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800785unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700786 enum pcm_param param)
787{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800788 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700789 int p;
790
791 if (!params)
792 return 0;
793
794 p = pcm_param_to_alsa(param);
795 if (p < 0)
796 return 0;
797
798 return param_get_max(params, p);
799}
800
Taylor Holberton6d58e012016-10-01 18:32:30 -0400801/** Closes a PCM returned by @ref pcm_open.
802 * @param pcm A PCM returned by @ref pcm_open.
803 * May not be NULL.
804 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800805 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400806 */
Simon Wilson79d39652011-05-25 13:44:23 -0700807int pcm_close(struct pcm *pcm)
808{
809 if (pcm == &bad_pcm)
810 return 0;
811
Eric Laurent40b018e2011-06-18 10:10:23 -0700812 pcm_hw_munmap_status(pcm);
813
Liam Girdwood6be28f12011-10-13 12:59:51 -0700814 if (pcm->flags & PCM_MMAP) {
815 pcm_stop(pcm);
816 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
817 }
818
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700819 snd_utils_close_dev_node(pcm->snd_node);
820 pcm->ops->close(pcm->data);
Simon Wilson79d39652011-05-25 13:44:23 -0700821 pcm->buffer_size = 0;
822 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700823 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700824 return 0;
825}
826
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800827/** Opens a PCM by it's name.
828 * @param name The name of the PCM.
829 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
830 * @param flags Specify characteristics and functionality about the pcm.
831 * May be a bitwise AND of the following:
832 * - @ref PCM_IN
833 * - @ref PCM_OUT
834 * - @ref PCM_MMAP
835 * - @ref PCM_NOIRQ
836 * - @ref PCM_MONOTONIC
837 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800838 * @returns A PCM structure.
839 * If an error occurs allocating memory for the PCM, NULL is returned.
840 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
841 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800842 * @ingroup libtinyalsa-pcm
843 */
844struct pcm *pcm_open_by_name(const char *name,
845 unsigned int flags,
846 const struct pcm_config *config)
847{
848 unsigned int card, device;
849 if ((name[0] != 'h')
850 || (name[1] != 'w')
851 || (name[2] != ':')) {
852 return NULL;
853 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
854 return NULL;
855 }
856 return pcm_open(card, device, flags, config);
857}
858
Taylor Holberton6d58e012016-10-01 18:32:30 -0400859/** Opens a PCM.
860 * @param card The card that the pcm belongs to.
861 * The default card is zero.
862 * @param device The device that the pcm belongs to.
863 * The default device is zero.
864 * @param flags Specify characteristics and functionality about the pcm.
865 * May be a bitwise AND of the following:
866 * - @ref PCM_IN
867 * - @ref PCM_OUT
868 * - @ref PCM_MMAP
869 * - @ref PCM_NOIRQ
870 * - @ref PCM_MONOTONIC
871 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800872 * @returns A PCM structure.
873 * If an error occurs allocating memory for the PCM, NULL is returned.
874 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
875 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800876 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400877 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700878struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800879 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700880{
Simon Wilson79d39652011-05-25 13:44:23 -0700881 struct pcm *pcm;
882 struct snd_pcm_info info;
Eric Laurent40b018e2011-06-18 10:10:23 -0700883 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700884
885 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400886 if (!pcm)
887 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700888
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700889 /* Default to hw_ops, attemp plugin open only if hw (/dev/snd/pcm*) open fails */
890 pcm->ops = &hw_ops;
891 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, NULL);
Simon Wilson79d39652011-05-25 13:44:23 -0700892
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700893#ifdef TINYALSA_USES_PLUGINS
Simon Wilson79d39652011-05-25 13:44:23 -0700894 if (pcm->fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700895 int pcm_type;
896 pcm->snd_node = snd_utils_open_pcm(card, device);
897 pcm_type = snd_utils_get_node_type(pcm->snd_node);
898 if (!pcm->snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
899 oops(pcm, -ENODEV, "no device (hw/plugin) for card(%u), device(%u)",
900 card, device);
Lubomir Rintel00f5aa12020-04-10 09:10:33 +0200901 goto fail_close;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700902 }
903 pcm->ops = &plug_ops;
904 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
905 }
906#endif
907 if (pcm->fd < 0) {
908 oops(pcm, errno, "cannot open device (%u) for card (%u)",
909 device, card);
Lubomir Rintel00f5aa12020-04-10 09:10:33 +0200910 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700911 }
912
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700913 pcm->flags = flags;
914
915 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700916 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700917 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700918 }
David Wagner4cddf192014-04-02 15:12:54 +0200919 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700920
Taylor Holberton861da7a2017-04-10 12:05:51 -0700921 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700922 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700923
Eric Laurent40b018e2011-06-18 10:10:23 -0700924 rc = pcm_hw_mmap_status(pcm);
925 if (rc < 0) {
926 oops(pcm, rc, "mmap status failed");
927 goto fail;
928 }
929
Glenn Kasten81012402013-08-22 15:11:48 -0700930#ifdef SNDRV_PCM_IOCTL_TTSTAMP
931 if (pcm->flags & PCM_MONOTONIC) {
932 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700933 rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
Glenn Kasten81012402013-08-22 15:11:48 -0700934 if (rc < 0) {
935 oops(pcm, rc, "cannot set timestamp type");
936 goto fail;
937 }
938 }
939#endif
940
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -0300941 /* prepare here so the user does not need to do this later */
942 if (pcm_prepare(pcm))
943 goto fail;
944
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200945 pcm->xruns = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700946 return pcm;
947
948fail:
Ricardo Biehl Pasquali0bfac892019-01-04 14:45:35 -0200949 pcm_hw_munmap_status(pcm);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700950 if (flags & PCM_MMAP)
951 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
952fail_close:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700953#ifdef TINYALSA_USES_PLUGINS
954 if (pcm->snd_node)
955 snd_utils_close_dev_node(pcm->snd_node);
956#endif
Lubomir Rintel00f5aa12020-04-10 09:10:33 +0200957 pcm_close(pcm);
958 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700959}
960
Taylor Holberton6d58e012016-10-01 18:32:30 -0400961/** Checks if a PCM file has been opened without error.
962 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -0800963 * May be NULL.
964 * @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 -0400965 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800966 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400967 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800968int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700969{
Taylor Holbertone123a652017-01-13 21:39:48 -0800970 if (pcm != NULL) {
971 return pcm->fd >= 0;
972 }
973 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700974}
Simon Wilsond6458e62011-06-21 14:58:11 -0700975
Taylor Holberton558e5942016-12-04 13:42:28 -0800976/** Links two PCMs.
977 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
978 * If an error occurs, the error message will be written to @p pcm1.
979 * @param pcm1 A PCM handle.
980 * @param pcm2 Another PCM handle.
981 * @return On success, zero; on failure, a negative number.
982 * @ingroup libtinyalsa-pcm
983 */
984int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
985{
986 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
987 if (err == -1) {
988 return oops(pcm1, errno, "cannot link PCM");
989 }
990 return 0;
991}
992
993/** Unlinks a PCM.
994 * @see @ref pcm_link
995 * @param pcm A PCM handle.
996 * @return On success, zero; on failure, a negative number.
997 * @ingroup libtinyalsa-pcm
998 */
999int pcm_unlink(struct pcm *pcm)
1000{
1001 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1002 if (err == -1) {
1003 return oops(pcm, errno, "cannot unlink PCM");
1004 }
1005 return 0;
1006}
1007
Taylor Holberton6d58e012016-10-01 18:32:30 -04001008/** Prepares a PCM, if it has not been prepared already.
1009 * @param pcm A PCM handle.
1010 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001011 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001012 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301013int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001014{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001015 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001016 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001017
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001018 /* get appl_ptr and avail_min from kernel */
1019 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1020
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301021 return 0;
1022}
1023
Taylor Holberton6d58e012016-10-01 18:32:30 -04001024/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001025 * @param pcm A PCM handle.
1026 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001027 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001028 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301029int pcm_start(struct pcm *pcm)
1030{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001031 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -02001032 if (pcm_sync_ptr(pcm, 0) < 0)
1033 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001034
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001035 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001036 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001037 return oops(pcm, errno, "cannot start channel");
1038 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001039
1040 return 0;
1041}
1042
Taylor Holberton6d58e012016-10-01 18:32:30 -04001043/** Stops a PCM.
1044 * @param pcm A PCM handle.
1045 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001046 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001047 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001048int pcm_stop(struct pcm *pcm)
1049{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001050 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001051 return oops(pcm, errno, "cannot stop channel");
1052
1053 return 0;
1054}
1055
Liam Girdwood6be28f12011-10-13 12:59:51 -07001056static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1057{
1058 int avail;
1059
1060 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1061
1062 if (avail < 0)
1063 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001064 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001065 avail -= pcm->boundary;
1066
1067 return avail;
1068}
1069
1070static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1071{
1072 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1073 if (avail < 0)
1074 avail += pcm->boundary;
1075 return avail;
1076}
1077
1078static inline int pcm_mmap_avail(struct pcm *pcm)
1079{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001080 if (pcm->flags & PCM_IN)
1081 return pcm_mmap_capture_avail(pcm);
1082 else
1083 return pcm_mmap_playback_avail(pcm);
1084}
1085
1086static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1087{
1088 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1089 appl_ptr += frames;
1090
1091 /* check for boundary wrap */
1092 if (appl_ptr > pcm->boundary)
1093 appl_ptr -= pcm->boundary;
1094 pcm->mmap_control->appl_ptr = appl_ptr;
1095}
1096
1097int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1098 unsigned int *frames)
1099{
1100 unsigned int continuous, copy_frames, avail;
1101
1102 /* return the mmap buffer */
1103 *areas = pcm->mmap_buffer;
1104
1105 /* and the application offset in frames */
1106 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1107
1108 avail = pcm_mmap_avail(pcm);
1109 if (avail > pcm->buffer_size)
1110 avail = pcm->buffer_size;
1111 continuous = pcm->buffer_size - *offset;
1112
1113 /* we can only copy frames if the are availabale and continuos */
1114 copy_frames = *frames;
1115 if (copy_frames > avail)
1116 copy_frames = avail;
1117 if (copy_frames > continuous)
1118 copy_frames = continuous;
1119 *frames = copy_frames;
1120
1121 return 0;
1122}
1123
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001124static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1125 char *buf, unsigned int src_offset,
1126 unsigned int frames)
1127{
1128 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1129 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1130 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1131
1132 /* interleaved only atm */
1133 if (pcm->flags & PCM_IN)
1134 memcpy(buf + src_offset_bytes,
1135 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1136 size_bytes);
1137 else
1138 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1139 buf + src_offset_bytes,
1140 size_bytes);
1141 return 0;
1142}
1143
Liam Girdwood6be28f12011-10-13 12:59:51 -07001144int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1145{
Taylor Holberton72e44222016-11-22 09:54:47 -08001146 int ret;
1147
Taylor Holberton73466c02016-10-01 12:51:59 -04001148 /* not used */
1149 (void) offset;
1150
Liam Girdwood6be28f12011-10-13 12:59:51 -07001151 /* update the application pointer in userspace and kernel */
1152 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001153 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001154 if (ret != 0){
1155 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001156 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001157 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001158
1159 return frames;
1160}
1161
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001162static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1163 unsigned int offset, unsigned int size)
1164{
1165 void *pcm_areas;
1166 int commit;
1167 unsigned int pcm_offset, frames, count = 0;
1168
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001169 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001170 frames = size;
1171 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1172 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1173 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1174 if (commit < 0) {
1175 oops(pcm, commit, "failed to commit %d frames\n", frames);
1176 return commit;
1177 }
1178
1179 offset += commit;
1180 count += commit;
1181 size -= commit;
1182 }
1183 return count;
1184}
1185
Liam Girdwood6be28f12011-10-13 12:59:51 -07001186int pcm_avail_update(struct pcm *pcm)
1187{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001188 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001189 return pcm_mmap_avail(pcm);
1190}
1191
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001192/** Returns available frames in pcm buffer and corresponding time stamp.
1193 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1194 * otherwise the clock is CLOCK_REALTIME.
1195 * For an input stream, frames available are frames ready for the application to read.
1196 * 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 -02001197 * @param pcm A PCM handle.
1198 * @param avail The number of available frames
1199 * @param tstamp The timestamp
1200 * @return On success, zero is returned; on failure, negative one.
1201 */
1202int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1203 struct timespec *tstamp)
1204{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001205 int checking;
1206 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001207
1208 if (!pcm_is_ready(pcm))
1209 return -1;
1210
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001211 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001212
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001213again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001214
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001215 tmp = pcm_avail_update(pcm);
1216 if (tmp < 0)
1217 return tmp; /* error */
1218
1219 if (checking && (unsigned int) tmp == *avail)
1220 return 0;
1221
1222 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001223 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001224
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001225 /*
1226 * When status is mmaped, get avail again to ensure
1227 * valid timestamp.
1228 */
1229 if (!pcm->sync_ptr) {
1230 checking = 1;
1231 goto again;
1232 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001233
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001234 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001235 return 0;
1236}
1237
Liam Girdwood6be28f12011-10-13 12:59:51 -07001238int pcm_state(struct pcm *pcm)
1239{
1240 int err = pcm_sync_ptr(pcm, 0);
1241 if (err < 0)
1242 return err;
1243
1244 return pcm->mmap_status->state;
1245}
1246
Taylor Holberton17a10242016-11-23 13:18:24 -08001247/** Waits for frames to be available for read or write operations.
1248 * @param pcm A PCM handle.
1249 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1250 * @returns If frames became available, one is returned.
1251 * If a timeout occured, zero is returned.
1252 * If an error occured, a negative number is returned.
1253 * @ingroup libtinyalsa-pcm
1254 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001255int pcm_wait(struct pcm *pcm, int timeout)
1256{
1257 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001258 int err;
1259
1260 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001261 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001262
1263 do {
1264 /* let's wait for avail or timeout */
1265 err = poll(&pfd, 1, timeout);
1266 if (err < 0)
1267 return -errno;
1268
1269 /* timeout ? */
1270 if (err == 0)
1271 return 0;
1272
1273 /* have we been interrupted ? */
1274 if (errno == -EINTR)
1275 continue;
1276
1277 /* check for any errors */
1278 if (pfd.revents & (POLLERR | POLLNVAL)) {
1279 switch (pcm_state(pcm)) {
1280 case PCM_STATE_XRUN:
1281 return -EPIPE;
1282 case PCM_STATE_SUSPENDED:
1283 return -ESTRPIPE;
1284 case PCM_STATE_DISCONNECTED:
1285 return -ENODEV;
1286 default:
1287 return -EIO;
1288 }
1289 }
1290 /* poll again if fd not ready for IO */
1291 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1292
1293 return 1;
1294}
1295
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001296/*
1297 * Transfer data to/from mmaped buffer. This imitates the
1298 * behavior of read/write system calls.
1299 *
1300 * However, this doesn't seems to offer any advantage over
1301 * the read/write syscalls. Should it be removed?
1302 */
1303int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001304{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001305 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001306
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001307 int state;
1308 unsigned int avail;
1309 unsigned int user_offset;
1310
1311 int err;
1312 int tmp;
1313
1314 is_playback = !(pcm->flags & PCM_IN);
1315
1316 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001317 return 0;
1318
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001319 /* update hardware pointer and get state */
1320 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1321 SNDRV_PCM_SYNC_PTR_APPL |
1322 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1323 if (err == -1)
1324 return -1;
1325 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001326
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001327 /*
1328 * If frames < start_threshold, wait indefinitely.
1329 * Another thread may start capture
1330 */
1331 if (!is_playback && state == PCM_STATE_PREPARED &&
1332 frames >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001333 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001334 if (err == -1)
1335 return -1;
1336 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001337 }
1338
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001339 avail = pcm_mmap_avail(pcm);
1340 user_offset = 0;
1341
1342 while (frames) {
1343 if (!avail) {
1344 if (pcm->flags & PCM_NONBLOCK) {
1345 errno = EAGAIN;
1346 break;
1347 }
1348
1349 /* wait for interrupt */
1350 err = pcm_wait(pcm, -1);
1351 if (err < 0) {
1352 errno = -err;
1353 break;
1354 }
1355
1356 /* get hardware pointer */
1357 avail = pcm_avail_update(pcm);
1358 }
1359
1360 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1361 if (tmp < 0)
1362 break;
1363
1364 user_offset += tmp;
1365 frames -= tmp;
1366 avail -= tmp;
1367
1368 /* start playback if written >= start_threshold */
1369 if (is_playback && state == PCM_STATE_PREPARED &&
1370 pcm->buffer_size - avail >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001371 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001372 if (err == -1)
1373 break;
1374 }
1375 }
1376
1377 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001378}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001379
1380int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1381{
1382 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1383 return -ENOSYS;
1384
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001385 return pcm_mmap_transfer(pcm, (void *)data,
1386 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001387}
1388
1389int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1390{
1391 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1392 return -ENOSYS;
1393
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001394 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001395}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301396
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001397static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1398{
1399 int is_playback;
1400
1401 struct snd_xferi transfer;
1402 int res;
1403
1404 is_playback = !(pcm->flags & PCM_IN);
1405
1406 transfer.buf = data;
1407 transfer.frames = frames;
1408 transfer.result = 0;
1409
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001410 res = pcm->ops->ioctl(pcm->data, is_playback
1411 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1412 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001413
1414 return res == 0 ? (int) transfer.result : -1;
1415}
1416
1417static int pcm_generic_transfer(struct pcm *pcm, void *data,
1418 unsigned int frames)
1419{
1420 int res;
1421
1422#if UINT_MAX > TINYALSA_FRAMES_MAX
1423 if (frames > TINYALSA_FRAMES_MAX)
1424 return -EINVAL;
1425#endif
1426 if (frames > INT_MAX)
1427 return -EINVAL;
1428
1429again:
1430
1431 if (pcm->flags & PCM_MMAP)
1432 res = pcm_mmap_transfer(pcm, data, frames);
1433 else
1434 res = pcm_rw_transfer(pcm, data, frames);
1435
1436 if (res < 0) {
1437 switch (errno) {
1438 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001439 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001440 /* fallthrough */
1441 case ESTRPIPE:
1442 /*
1443 * Try to restart if we are allowed to do so.
1444 * Otherwise, return error.
1445 */
1446 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1447 return -1;
1448 goto again;
1449 case EAGAIN:
1450 if (pcm->flags & PCM_NONBLOCK)
1451 return -1;
1452 /* fallthrough */
1453 default:
1454 return oops(pcm, errno, "cannot read/write stream data");
1455 }
1456 }
1457
1458 return res;
1459}
1460
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001461/** Writes audio samples to PCM.
1462 * If the PCM has not been started, it is started in this function.
1463 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1464 * @param pcm A PCM handle.
1465 * @param data The audio sample array
1466 * @param frame_count The number of frames occupied by the sample array.
1467 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1468 * or INT_MAX.
1469 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1470 * @ingroup libtinyalsa-pcm
1471 */
1472int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1473{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001474 if (pcm->flags & PCM_IN)
1475 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001476
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001477 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001478}
1479
1480/** Reads audio samples from PCM.
1481 * If the PCM has not been started, it is started in this function.
1482 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1483 * @param pcm A PCM handle.
1484 * @param data The audio sample array
1485 * @param frame_count The number of frames occupied by the sample array.
1486 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1487 * or INT_MAX.
1488 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1489 * @ingroup libtinyalsa-pcm
1490 */
1491int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1492{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001493 if (!(pcm->flags & PCM_IN))
1494 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001495
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001496 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001497}
1498
1499/** Writes audio samples to PCM.
1500 * If the PCM has not been started, it is started in this function.
1501 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1502 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1503 * @param pcm A PCM handle.
1504 * @param data The audio sample array
1505 * @param count The number of bytes occupied by the sample array.
1506 * @return On success, this function returns zero; otherwise, a negative number.
1507 * @deprecated
1508 * @ingroup libtinyalsa-pcm
1509 */
1510int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1511{
1512 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
1513}
1514
1515/** Reads audio samples from PCM.
1516 * If the PCM has not been started, it is started in this function.
1517 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1518 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1519 * @param pcm A PCM handle.
1520 * @param data The audio sample array
1521 * @param count The number of bytes occupied by the sample array.
1522 * @return On success, this function returns zero; otherwise, a negative number.
1523 * @deprecated
1524 * @ingroup libtinyalsa-pcm
1525 */
1526int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1527{
1528 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
1529}
1530
Taylor Holberton17a10242016-11-23 13:18:24 -08001531/** Gets the delay of the PCM, in terms of frames.
1532 * @param pcm A PCM handle.
1533 * @returns On success, the delay of the PCM.
1534 * On failure, a negative number.
1535 * @ingroup libtinyalsa-pcm
1536 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301537long pcm_get_delay(struct pcm *pcm)
1538{
1539 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1540 return -1;
1541
1542 return pcm->pcm_delay;
1543}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001544