blob: c28e0989322b72b62359a3a8204500b238479c9f [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);
901 return pcm;
902 }
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);
Simon Wilson79d39652011-05-25 13:44:23 -0700910 return pcm;
911 }
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
957 pcm->ops->close(pcm->data);
Simon Wilson79d39652011-05-25 13:44:23 -0700958 pcm->fd = -1;
959 return pcm;
960}
961
Taylor Holberton6d58e012016-10-01 18:32:30 -0400962/** Checks if a PCM file has been opened without error.
963 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -0800964 * May be NULL.
965 * @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 -0400966 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800967 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400968 */
Taylor Holberton15d58482016-12-01 17:46:29 -0800969int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700970{
Taylor Holbertone123a652017-01-13 21:39:48 -0800971 if (pcm != NULL) {
972 return pcm->fd >= 0;
973 }
974 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700975}
Simon Wilsond6458e62011-06-21 14:58:11 -0700976
Taylor Holberton558e5942016-12-04 13:42:28 -0800977/** Links two PCMs.
978 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
979 * If an error occurs, the error message will be written to @p pcm1.
980 * @param pcm1 A PCM handle.
981 * @param pcm2 Another PCM handle.
982 * @return On success, zero; on failure, a negative number.
983 * @ingroup libtinyalsa-pcm
984 */
985int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
986{
987 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
988 if (err == -1) {
989 return oops(pcm1, errno, "cannot link PCM");
990 }
991 return 0;
992}
993
994/** Unlinks a PCM.
995 * @see @ref pcm_link
996 * @param pcm A PCM handle.
997 * @return On success, zero; on failure, a negative number.
998 * @ingroup libtinyalsa-pcm
999 */
1000int pcm_unlink(struct pcm *pcm)
1001{
1002 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1003 if (err == -1) {
1004 return oops(pcm, errno, "cannot unlink PCM");
1005 }
1006 return 0;
1007}
1008
Taylor Holberton6d58e012016-10-01 18:32:30 -04001009/** Prepares a PCM, if it has not been prepared already.
1010 * @param pcm A PCM handle.
1011 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001012 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001013 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301014int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001015{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001016 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001017 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001018
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001019 /* get appl_ptr and avail_min from kernel */
1020 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1021
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301022 return 0;
1023}
1024
Taylor Holberton6d58e012016-10-01 18:32:30 -04001025/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001026 * @param pcm A PCM handle.
1027 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001028 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001029 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301030int pcm_start(struct pcm *pcm)
1031{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001032 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -02001033 if (pcm_sync_ptr(pcm, 0) < 0)
1034 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001035
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001036 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001037 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001038 return oops(pcm, errno, "cannot start channel");
1039 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001040
1041 return 0;
1042}
1043
Taylor Holberton6d58e012016-10-01 18:32:30 -04001044/** Stops a PCM.
1045 * @param pcm A PCM handle.
1046 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001047 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001048 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001049int pcm_stop(struct pcm *pcm)
1050{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001051 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001052 return oops(pcm, errno, "cannot stop channel");
1053
1054 return 0;
1055}
1056
Liam Girdwood6be28f12011-10-13 12:59:51 -07001057static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1058{
1059 int avail;
1060
1061 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1062
1063 if (avail < 0)
1064 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001065 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001066 avail -= pcm->boundary;
1067
1068 return avail;
1069}
1070
1071static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1072{
1073 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1074 if (avail < 0)
1075 avail += pcm->boundary;
1076 return avail;
1077}
1078
1079static inline int pcm_mmap_avail(struct pcm *pcm)
1080{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001081 if (pcm->flags & PCM_IN)
1082 return pcm_mmap_capture_avail(pcm);
1083 else
1084 return pcm_mmap_playback_avail(pcm);
1085}
1086
1087static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1088{
1089 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1090 appl_ptr += frames;
1091
1092 /* check for boundary wrap */
1093 if (appl_ptr > pcm->boundary)
1094 appl_ptr -= pcm->boundary;
1095 pcm->mmap_control->appl_ptr = appl_ptr;
1096}
1097
1098int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1099 unsigned int *frames)
1100{
1101 unsigned int continuous, copy_frames, avail;
1102
1103 /* return the mmap buffer */
1104 *areas = pcm->mmap_buffer;
1105
1106 /* and the application offset in frames */
1107 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1108
1109 avail = pcm_mmap_avail(pcm);
1110 if (avail > pcm->buffer_size)
1111 avail = pcm->buffer_size;
1112 continuous = pcm->buffer_size - *offset;
1113
1114 /* we can only copy frames if the are availabale and continuos */
1115 copy_frames = *frames;
1116 if (copy_frames > avail)
1117 copy_frames = avail;
1118 if (copy_frames > continuous)
1119 copy_frames = continuous;
1120 *frames = copy_frames;
1121
1122 return 0;
1123}
1124
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001125static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1126 char *buf, unsigned int src_offset,
1127 unsigned int frames)
1128{
1129 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1130 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1131 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1132
1133 /* interleaved only atm */
1134 if (pcm->flags & PCM_IN)
1135 memcpy(buf + src_offset_bytes,
1136 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1137 size_bytes);
1138 else
1139 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1140 buf + src_offset_bytes,
1141 size_bytes);
1142 return 0;
1143}
1144
Liam Girdwood6be28f12011-10-13 12:59:51 -07001145int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1146{
Taylor Holberton72e44222016-11-22 09:54:47 -08001147 int ret;
1148
Taylor Holberton73466c02016-10-01 12:51:59 -04001149 /* not used */
1150 (void) offset;
1151
Liam Girdwood6be28f12011-10-13 12:59:51 -07001152 /* update the application pointer in userspace and kernel */
1153 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001154 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001155 if (ret != 0){
1156 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001157 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001158 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001159
1160 return frames;
1161}
1162
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001163static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1164 unsigned int offset, unsigned int size)
1165{
1166 void *pcm_areas;
1167 int commit;
1168 unsigned int pcm_offset, frames, count = 0;
1169
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001170 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001171 frames = size;
1172 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1173 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1174 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1175 if (commit < 0) {
1176 oops(pcm, commit, "failed to commit %d frames\n", frames);
1177 return commit;
1178 }
1179
1180 offset += commit;
1181 count += commit;
1182 size -= commit;
1183 }
1184 return count;
1185}
1186
Liam Girdwood6be28f12011-10-13 12:59:51 -07001187int pcm_avail_update(struct pcm *pcm)
1188{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001189 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001190 return pcm_mmap_avail(pcm);
1191}
1192
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001193/** Returns available frames in pcm buffer and corresponding time stamp.
1194 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1195 * otherwise the clock is CLOCK_REALTIME.
1196 * For an input stream, frames available are frames ready for the application to read.
1197 * 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 -02001198 * @param pcm A PCM handle.
1199 * @param avail The number of available frames
1200 * @param tstamp The timestamp
1201 * @return On success, zero is returned; on failure, negative one.
1202 */
1203int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1204 struct timespec *tstamp)
1205{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001206 int checking;
1207 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001208
1209 if (!pcm_is_ready(pcm))
1210 return -1;
1211
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001212 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001213
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001214again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001215
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001216 tmp = pcm_avail_update(pcm);
1217 if (tmp < 0)
1218 return tmp; /* error */
1219
1220 if (checking && (unsigned int) tmp == *avail)
1221 return 0;
1222
1223 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001224 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001225
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001226 /*
1227 * When status is mmaped, get avail again to ensure
1228 * valid timestamp.
1229 */
1230 if (!pcm->sync_ptr) {
1231 checking = 1;
1232 goto again;
1233 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001234
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001235 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001236 return 0;
1237}
1238
Liam Girdwood6be28f12011-10-13 12:59:51 -07001239int pcm_state(struct pcm *pcm)
1240{
1241 int err = pcm_sync_ptr(pcm, 0);
1242 if (err < 0)
1243 return err;
1244
1245 return pcm->mmap_status->state;
1246}
1247
Taylor Holberton17a10242016-11-23 13:18:24 -08001248/** Waits for frames to be available for read or write operations.
1249 * @param pcm A PCM handle.
1250 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1251 * @returns If frames became available, one is returned.
1252 * If a timeout occured, zero is returned.
1253 * If an error occured, a negative number is returned.
1254 * @ingroup libtinyalsa-pcm
1255 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001256int pcm_wait(struct pcm *pcm, int timeout)
1257{
1258 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001259 int err;
1260
1261 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001262 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001263
1264 do {
1265 /* let's wait for avail or timeout */
1266 err = poll(&pfd, 1, timeout);
1267 if (err < 0)
1268 return -errno;
1269
1270 /* timeout ? */
1271 if (err == 0)
1272 return 0;
1273
1274 /* have we been interrupted ? */
1275 if (errno == -EINTR)
1276 continue;
1277
1278 /* check for any errors */
1279 if (pfd.revents & (POLLERR | POLLNVAL)) {
1280 switch (pcm_state(pcm)) {
1281 case PCM_STATE_XRUN:
1282 return -EPIPE;
1283 case PCM_STATE_SUSPENDED:
1284 return -ESTRPIPE;
1285 case PCM_STATE_DISCONNECTED:
1286 return -ENODEV;
1287 default:
1288 return -EIO;
1289 }
1290 }
1291 /* poll again if fd not ready for IO */
1292 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1293
1294 return 1;
1295}
1296
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001297/*
1298 * Transfer data to/from mmaped buffer. This imitates the
1299 * behavior of read/write system calls.
1300 *
1301 * However, this doesn't seems to offer any advantage over
1302 * the read/write syscalls. Should it be removed?
1303 */
1304int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001305{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001306 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001307
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001308 int state;
1309 unsigned int avail;
1310 unsigned int user_offset;
1311
1312 int err;
1313 int tmp;
1314
1315 is_playback = !(pcm->flags & PCM_IN);
1316
1317 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001318 return 0;
1319
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001320 /* update hardware pointer and get state */
1321 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1322 SNDRV_PCM_SYNC_PTR_APPL |
1323 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1324 if (err == -1)
1325 return -1;
1326 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001327
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001328 /*
1329 * If frames < start_threshold, wait indefinitely.
1330 * Another thread may start capture
1331 */
1332 if (!is_playback && state == PCM_STATE_PREPARED &&
1333 frames >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001334 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001335 if (err == -1)
1336 return -1;
1337 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001338 }
1339
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001340 avail = pcm_mmap_avail(pcm);
1341 user_offset = 0;
1342
1343 while (frames) {
1344 if (!avail) {
1345 if (pcm->flags & PCM_NONBLOCK) {
1346 errno = EAGAIN;
1347 break;
1348 }
1349
1350 /* wait for interrupt */
1351 err = pcm_wait(pcm, -1);
1352 if (err < 0) {
1353 errno = -err;
1354 break;
1355 }
1356
1357 /* get hardware pointer */
1358 avail = pcm_avail_update(pcm);
1359 }
1360
1361 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1362 if (tmp < 0)
1363 break;
1364
1365 user_offset += tmp;
1366 frames -= tmp;
1367 avail -= tmp;
1368
1369 /* start playback if written >= start_threshold */
1370 if (is_playback && state == PCM_STATE_PREPARED &&
1371 pcm->buffer_size - avail >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001372 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001373 if (err == -1)
1374 break;
1375 }
1376 }
1377
1378 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001379}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001380
1381int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1382{
1383 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1384 return -ENOSYS;
1385
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001386 return pcm_mmap_transfer(pcm, (void *)data,
1387 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001388}
1389
1390int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1391{
1392 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1393 return -ENOSYS;
1394
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001395 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001396}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301397
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001398static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1399{
1400 int is_playback;
1401
1402 struct snd_xferi transfer;
1403 int res;
1404
1405 is_playback = !(pcm->flags & PCM_IN);
1406
1407 transfer.buf = data;
1408 transfer.frames = frames;
1409 transfer.result = 0;
1410
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001411 res = pcm->ops->ioctl(pcm->data, is_playback
1412 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1413 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001414
1415 return res == 0 ? (int) transfer.result : -1;
1416}
1417
1418static int pcm_generic_transfer(struct pcm *pcm, void *data,
1419 unsigned int frames)
1420{
1421 int res;
1422
1423#if UINT_MAX > TINYALSA_FRAMES_MAX
1424 if (frames > TINYALSA_FRAMES_MAX)
1425 return -EINVAL;
1426#endif
1427 if (frames > INT_MAX)
1428 return -EINVAL;
1429
1430again:
1431
1432 if (pcm->flags & PCM_MMAP)
1433 res = pcm_mmap_transfer(pcm, data, frames);
1434 else
1435 res = pcm_rw_transfer(pcm, data, frames);
1436
1437 if (res < 0) {
1438 switch (errno) {
1439 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001440 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001441 /* fallthrough */
1442 case ESTRPIPE:
1443 /*
1444 * Try to restart if we are allowed to do so.
1445 * Otherwise, return error.
1446 */
1447 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1448 return -1;
1449 goto again;
1450 case EAGAIN:
1451 if (pcm->flags & PCM_NONBLOCK)
1452 return -1;
1453 /* fallthrough */
1454 default:
1455 return oops(pcm, errno, "cannot read/write stream data");
1456 }
1457 }
1458
1459 return res;
1460}
1461
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001462/** 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 * @param pcm A PCM handle.
1466 * @param data The audio sample array
1467 * @param frame_count The number of frames occupied by the sample array.
1468 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1469 * or INT_MAX.
1470 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1471 * @ingroup libtinyalsa-pcm
1472 */
1473int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1474{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001475 if (pcm->flags & PCM_IN)
1476 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001477
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001478 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001479}
1480
1481/** Reads audio samples from PCM.
1482 * If the PCM has not been started, it is started in this function.
1483 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1484 * @param pcm A PCM handle.
1485 * @param data The audio sample array
1486 * @param frame_count The number of frames occupied by the sample array.
1487 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1488 * or INT_MAX.
1489 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1490 * @ingroup libtinyalsa-pcm
1491 */
1492int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1493{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001494 if (!(pcm->flags & PCM_IN))
1495 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001496
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001497 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001498}
1499
1500/** Writes audio samples to PCM.
1501 * If the PCM has not been started, it is started in this function.
1502 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1503 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1504 * @param pcm A PCM handle.
1505 * @param data The audio sample array
1506 * @param count The number of bytes occupied by the sample array.
1507 * @return On success, this function returns zero; otherwise, a negative number.
1508 * @deprecated
1509 * @ingroup libtinyalsa-pcm
1510 */
1511int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1512{
1513 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
1514}
1515
1516/** Reads audio samples from PCM.
1517 * If the PCM has not been started, it is started in this function.
1518 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1519 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1520 * @param pcm A PCM handle.
1521 * @param data The audio sample array
1522 * @param count The number of bytes occupied by the sample array.
1523 * @return On success, this function returns zero; otherwise, a negative number.
1524 * @deprecated
1525 * @ingroup libtinyalsa-pcm
1526 */
1527int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1528{
1529 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
1530}
1531
Taylor Holberton17a10242016-11-23 13:18:24 -08001532/** Gets the delay of the PCM, in terms of frames.
1533 * @param pcm A PCM handle.
1534 * @returns On success, the delay of the PCM.
1535 * On failure, a negative number.
1536 * @ingroup libtinyalsa-pcm
1537 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301538long pcm_get_delay(struct pcm *pcm)
1539{
1540 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1541 return -1;
1542
1543 return pcm->pcm_delay;
1544}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001545