blob: 3b54ec735247c65f8997120041856f02ab5aa9a1 [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
dvdli9c9a4422020-10-28 16:05:19 +080073/* Logs information into a string; follows snprintf() in that
74 * offset may be greater than size, and though no characters are copied
75 * into string, characters are still counted into offset. */
76#define STRLOG(string, offset, size, ...) \
77 do { int temp, clipoffset = offset > size ? size : offset; \
78 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
79 if (temp > 0) offset += temp; } while (0)
80
81#ifndef ARRAY_SIZE
82#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
83#endif
84
85/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
86static const char * const access_lookup[] = {
87 "MMAP_INTERLEAVED",
88 "MMAP_NONINTERLEAVED",
89 "MMAP_COMPLEX",
90 "RW_INTERLEAVED",
91 "RW_NONINTERLEAVED",
92};
93
94/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
95static const char * const format_lookup[] = {
96 /*[0] =*/ "S8",
97 "U8",
98 "S16_LE",
99 "S16_BE",
100 "U16_LE",
101 "U16_BE",
102 "S24_LE",
103 "S24_BE",
104 "U24_LE",
105 "U24_BE",
106 "S32_LE",
107 "S32_BE",
108 "U32_LE",
109 "U32_BE",
110 "FLOAT_LE",
111 "FLOAT_BE",
112 "FLOAT64_LE",
113 "FLOAT64_BE",
114 "IEC958_SUBFRAME_LE",
115 "IEC958_SUBFRAME_BE",
116 "MU_LAW",
117 "A_LAW",
118 "IMA_ADPCM",
119 "MPEG",
120 /*[24] =*/ "GSM",
121 /* gap */
122 [31] = "SPECIAL",
123 "S24_3LE",
124 "S24_3BE",
125 "U24_3LE",
126 "U24_3BE",
127 "S20_3LE",
128 "S20_3BE",
129 "U20_3LE",
130 "U20_3BE",
131 "S18_3LE",
132 "S18_3BE",
133 "U18_3LE",
134 /*[43] =*/ "U18_3BE",
135#if 0
136 /* recent additions, may not be present on local asound.h */
137 "G723_24",
138 "G723_24_1B",
139 "G723_40",
140 "G723_40_1B",
141 "DSD_U8",
142 "DSD_U16_LE",
143#endif
144};
145
146/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
147static const char * const subformat_lookup[] = {
148 "STD",
149};
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700150
Simon Wilson79d39652011-05-25 13:44:23 -0700151static inline int param_is_mask(int p)
152{
153 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
154 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
155}
156
157static inline int param_is_interval(int p)
158{
159 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
160 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
161}
162
Taylor Holberton2f387d22016-12-01 15:58:16 -0800163static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
164{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700165 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -0800166}
167
Simon Wilson79d39652011-05-25 13:44:23 -0700168static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
169{
170 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
171}
172
173static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
174{
175 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
176}
177
178static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
179{
180 if (bit >= SNDRV_MASK_MAX)
181 return;
182 if (param_is_mask(n)) {
183 struct snd_mask *m = param_to_mask(p, n);
184 m->bits[0] = 0;
185 m->bits[1] = 0;
186 m->bits[bit >> 5] |= (1 << (bit & 31));
187 }
188}
189
190static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
191{
192 if (param_is_interval(n)) {
193 struct snd_interval *i = param_to_interval(p, n);
194 i->min = val;
195 }
196}
197
Taylor Holberton2f387d22016-12-01 15:58:16 -0800198static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700199{
200 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800201 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700202 return i->min;
203 }
204 return 0;
205}
206
Taylor Holberton2f387d22016-12-01 15:58:16 -0800207static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700208{
209 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800210 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700211 return i->max;
212 }
213 return 0;
214}
215
Simon Wilson79d39652011-05-25 13:44:23 -0700216static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
217{
218 if (param_is_interval(n)) {
219 struct snd_interval *i = param_to_interval(p, n);
220 i->min = val;
221 i->max = val;
222 i->integer = 1;
223 }
224}
225
Liam Girdwood6be28f12011-10-13 12:59:51 -0700226static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
227{
228 if (param_is_interval(n)) {
229 struct snd_interval *i = param_to_interval(p, n);
230 if (i->integer)
231 return i->max;
232 }
233 return 0;
234}
235
Simon Wilson79d39652011-05-25 13:44:23 -0700236static void param_init(struct snd_pcm_hw_params *p)
237{
238 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700239
Simon Wilson79d39652011-05-25 13:44:23 -0700240 memset(p, 0, sizeof(*p));
241 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
242 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
243 struct snd_mask *m = param_to_mask(p, n);
244 m->bits[0] = ~0;
245 m->bits[1] = ~0;
246 }
247 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
248 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
249 struct snd_interval *i = param_to_interval(p, n);
250 i->min = 0;
251 i->max = ~0;
252 }
Simon Wilson43544882012-10-31 12:52:39 -0700253 p->rmask = ~0U;
254 p->cmask = 0;
255 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700256}
257
Taylor Holberton861da7a2017-04-10 12:05:51 -0700258static unsigned int pcm_format_to_alsa(enum pcm_format format)
259{
260 switch (format) {
261
262 case PCM_FORMAT_S8:
263 return SNDRV_PCM_FORMAT_S8;
264
265 default:
266 case PCM_FORMAT_S16_LE:
267 return SNDRV_PCM_FORMAT_S16_LE;
268 case PCM_FORMAT_S16_BE:
269 return SNDRV_PCM_FORMAT_S16_BE;
270
271 case PCM_FORMAT_S24_LE:
272 return SNDRV_PCM_FORMAT_S24_LE;
273 case PCM_FORMAT_S24_BE:
274 return SNDRV_PCM_FORMAT_S24_BE;
275
276 case PCM_FORMAT_S24_3LE:
277 return SNDRV_PCM_FORMAT_S24_3LE;
278 case PCM_FORMAT_S24_3BE:
279 return SNDRV_PCM_FORMAT_S24_3BE;
280
281 case PCM_FORMAT_S32_LE:
282 return SNDRV_PCM_FORMAT_S32_LE;
283 case PCM_FORMAT_S32_BE:
284 return SNDRV_PCM_FORMAT_S32_BE;
285 };
286}
287
Simon Wilson79d39652011-05-25 13:44:23 -0700288#define PCM_ERROR_MAX 128
289
Taylor Holberton6d58e012016-10-01 18:32:30 -0400290/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800291 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400292 */
Simon Wilson79d39652011-05-25 13:44:23 -0700293struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400294 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700295 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400296 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700297 unsigned int flags;
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200298 /** The number of (under/over)runs that have occured */
299 int xruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400300 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700301 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800302 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700303 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400304 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700305 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400306 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700307 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700308 struct snd_pcm_mmap_status *mmap_status;
309 struct snd_pcm_mmap_control *mmap_control;
310 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700311 void *mmap_buffer;
312 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800313 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530314 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800315 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200316 unsigned int subdevice;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700317 /** Pointer to the pcm ops, either hw or plugin */
318 const struct pcm_ops *ops;
319 /** Private data for pcm_hw or pcm_plugin */
320 void *data;
321 /** Pointer to the pcm node from snd card definition */
322 struct snd_node *snd_node;
Simon Wilson79d39652011-05-25 13:44:23 -0700323};
324
Taylor Holberton861da7a2017-04-10 12:05:51 -0700325static int oops(struct pcm *pcm, int e, const char *fmt, ...)
326{
327 va_list ap;
328 int sz;
329
330 va_start(ap, fmt);
331 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
332 va_end(ap);
333 sz = strlen(pcm->error);
334
335 if (errno)
336 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
337 ": %s", strerror(e));
338 return -1;
339}
340
Taylor Holberton6d58e012016-10-01 18:32:30 -0400341/** Gets the buffer size of the PCM.
342 * @param pcm A PCM handle.
343 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800344 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400345 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800346unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700347{
348 return pcm->buffer_size;
349}
350
Taylor Holberton77979a82016-12-01 20:04:04 -0800351/** Gets the channel count of the PCM.
352 * @param pcm A PCM handle.
353 * @return The channel count of the PCM.
354 * @ingroup libtinyalsa-pcm
355 */
356unsigned int pcm_get_channels(const struct pcm *pcm)
357{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700358 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800359}
360
Taylor Holberton08bb5902017-04-10 11:45:44 -0700361/** Gets the PCM configuration.
362 * @param pcm A PCM handle.
363 * @return The PCM configuration.
364 * This function only returns NULL if
365 * @p pcm is NULL.
366 * @ingroup libtinyalsa-pcm
367 * */
368const struct pcm_config * pcm_get_config(const struct pcm *pcm)
369{
370 if (pcm == NULL)
371 return NULL;
372 return &pcm->config;
373}
374
Taylor Holberton77979a82016-12-01 20:04:04 -0800375/** Gets the rate of the PCM.
376 * The rate is given in frames per second.
377 * @param pcm A PCM handle.
378 * @return The rate of the PCM.
379 * @ingroup libtinyalsa-pcm
380 */
381unsigned int pcm_get_rate(const struct pcm *pcm)
382{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700383 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800384}
385
386/** Gets the format of the PCM.
387 * @param pcm A PCM handle.
388 * @return The format of the PCM.
389 * @ingroup libtinyalsa-pcm
390 */
391enum pcm_format pcm_get_format(const struct pcm *pcm)
392{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700393 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800394}
395
Taylor Holberton6d58e012016-10-01 18:32:30 -0400396/** Gets the file descriptor of the PCM.
397 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800398 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400399 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800400 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400401 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800402int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400403{
404 return pcm->fd;
405}
406
Taylor Holberton6d58e012016-10-01 18:32:30 -0400407/** Gets the error message for the last error that occured.
408 * If no error occured and this function is called, the results are undefined.
409 * @param pcm A PCM handle.
410 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800411 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400412 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800413const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700414{
415 return pcm->error;
416}
417
Taylor Holberton861da7a2017-04-10 12:05:51 -0700418/** Sets the PCM configuration.
419 * @param pcm A PCM handle.
420 * @param config The configuration to use for the
421 * PCM. This parameter may be NULL, in which case
422 * the default configuration is used.
423 * @returns Zero on success, a negative errno value
424 * on failure.
425 * @ingroup libtinyalsa-pcm
426 * */
427int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
428{
429 if (pcm == NULL)
430 return -EFAULT;
431 else if (config == NULL) {
432 config = &pcm->config;
433 pcm->config.channels = 2;
434 pcm->config.rate = 48000;
435 pcm->config.period_size = 1024;
436 pcm->config.period_count = 4;
437 pcm->config.format = PCM_FORMAT_S16_LE;
438 pcm->config.start_threshold = config->period_count * config->period_size;
439 pcm->config.stop_threshold = config->period_count * config->period_size;
440 pcm->config.silence_threshold = 0;
dvdli1e75a162020-10-28 17:37:19 +0800441 pcm->config.silence_size = 0;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700442 } else
443 pcm->config = *config;
444
445 struct snd_pcm_hw_params params;
446 param_init(&params);
447 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
448 pcm_format_to_alsa(config->format));
Taylor Holberton861da7a2017-04-10 12:05:51 -0700449 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700450 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
451 config->channels);
452 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
453 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
454
455 if (pcm->flags & PCM_NOIRQ) {
456
457 if (!(pcm->flags & PCM_MMAP)) {
458 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
459 return -EINVAL;
460 }
461
462 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
463 pcm->noirq_frames_per_msec = config->rate / 1000;
464 }
465
466 if (pcm->flags & PCM_MMAP)
467 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
468 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
469 else
470 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
471 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
472
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700473 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700474 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200475 oops(pcm, errno, "cannot set hw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700476 return -errno_copy;
477 }
478
479 /* get our refined hw_params */
480 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
481 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
482 pcm->buffer_size = config->period_count * config->period_size;
483
484 if (pcm->flags & PCM_MMAP) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530485 pcm->mmap_buffer = pcm->ops->mmap(pcm->data, NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
486 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700487 if (pcm->mmap_buffer == MAP_FAILED) {
488 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200489 oops(pcm, errno, "failed to mmap buffer %d bytes\n",
Taylor Holberton861da7a2017-04-10 12:05:51 -0700490 pcm_frames_to_bytes(pcm, pcm->buffer_size));
491 return -errno_copy;
492 }
493 }
494
495 struct snd_pcm_sw_params sparams;
496 memset(&sparams, 0, sizeof(sparams));
497 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
498 sparams.period_step = 1;
Miguel GAIO05c64c32020-02-06 21:09:31 +0100499 sparams.avail_min = config->period_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700500
501 if (!config->start_threshold) {
502 if (pcm->flags & PCM_IN)
503 pcm->config.start_threshold = sparams.start_threshold = 1;
504 else
505 pcm->config.start_threshold = sparams.start_threshold =
506 config->period_count * config->period_size / 2;
507 } else
508 sparams.start_threshold = config->start_threshold;
509
510 /* pick a high stop threshold - todo: does this need further tuning */
511 if (!config->stop_threshold) {
512 if (pcm->flags & PCM_IN)
513 pcm->config.stop_threshold = sparams.stop_threshold =
514 config->period_count * config->period_size * 10;
515 else
516 pcm->config.stop_threshold = sparams.stop_threshold =
517 config->period_count * config->period_size;
518 }
519 else
520 sparams.stop_threshold = config->stop_threshold;
521
522 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
dvdli1e75a162020-10-28 17:37:19 +0800523 sparams.silence_size = config->silence_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700524 sparams.silence_threshold = config->silence_threshold;
525 pcm->boundary = sparams.boundary = pcm->buffer_size;
526
527 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
528 pcm->boundary *= 2;
529
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700530 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700531 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200532 oops(pcm, errno, "cannot set sw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700533 return -errno_copy;
534 }
535
536 return 0;
537}
538
Taylor Holberton6d58e012016-10-01 18:32:30 -0400539/** Gets the subdevice on which the pcm has been opened.
540 * @param pcm A PCM handle.
541 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800542unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200543{
544 return pcm->subdevice;
545}
546
Taylor Holberton6d58e012016-10-01 18:32:30 -0400547/** Determines the number of bits occupied by a @ref pcm_format.
548 * @param format A PCM format.
549 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800550 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400551 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700552unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700553{
554 switch (format) {
555 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400556 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700557 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400558 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700559 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400560 case PCM_FORMAT_S24_3LE:
561 case PCM_FORMAT_S24_3BE:
562 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700563 default:
564 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400565 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700566 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400567 case PCM_FORMAT_S8:
568 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700569 };
570}
571
Taylor Holberton6d58e012016-10-01 18:32:30 -0400572/** Determines how many frames of a PCM can fit into a number of bytes.
573 * @param pcm A PCM handle.
574 * @param bytes The number of bytes.
575 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800576 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400577 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800578unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700579{
580 return bytes / (pcm->config.channels *
581 (pcm_format_to_bits(pcm->config.format) >> 3));
582}
583
Taylor Holberton6d58e012016-10-01 18:32:30 -0400584/** Determines how many bytes are occupied by a number of frames of a PCM.
585 * @param pcm A PCM handle.
586 * @param frames The number of frames of a PCM.
587 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800588 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400589 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800590unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700591{
592 return frames * pcm->config.channels *
593 (pcm_format_to_bits(pcm->config.format) >> 3);
594}
595
Taylor Holberton4f556062016-09-16 09:54:36 -0400596static int pcm_sync_ptr(struct pcm *pcm, int flags)
597{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200598 if (pcm->sync_ptr == NULL) {
599 /* status and control are mmaped */
600
601 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
602 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
603 oops(pcm, errno, "failed to sync hardware pointer");
604 return -1;
605 }
606 }
607 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700608 pcm->sync_ptr->flags = flags;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700609 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
610 pcm->sync_ptr) < 0) {
Taylor Holbertone123a652017-01-13 21:39:48 -0800611 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700612 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800613 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700614 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200615
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200616 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700617}
618
Taylor Holberton4f556062016-09-16 09:54:36 -0400619static int pcm_hw_mmap_status(struct pcm *pcm)
620{
Eric Laurent40b018e2011-06-18 10:10:23 -0700621 if (pcm->sync_ptr)
622 return 0;
623
624 int page_size = sysconf(_SC_PAGE_SIZE);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530625 pcm->mmap_status = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ, MAP_SHARED,
626 SNDRV_PCM_MMAP_OFFSET_STATUS);
Eric Laurent40b018e2011-06-18 10:10:23 -0700627 if (pcm->mmap_status == MAP_FAILED)
628 pcm->mmap_status = NULL;
629 if (!pcm->mmap_status)
630 goto mmap_error;
631
Rohit kumarf29b8df2020-08-19 15:19:33 +0530632 pcm->mmap_control = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ | PROT_WRITE,
633 MAP_SHARED, SNDRV_PCM_MMAP_OFFSET_CONTROL);
Eric Laurent40b018e2011-06-18 10:10:23 -0700634 if (pcm->mmap_control == MAP_FAILED)
635 pcm->mmap_control = NULL;
636 if (!pcm->mmap_control) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530637 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700638 pcm->mmap_status = NULL;
639 goto mmap_error;
640 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700641
642 return 0;
643
644mmap_error:
645
646 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
647 if (!pcm->sync_ptr)
648 return -ENOMEM;
649 pcm->mmap_status = &pcm->sync_ptr->s.status;
650 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700651
652 return 0;
653}
654
655static void pcm_hw_munmap_status(struct pcm *pcm) {
656 if (pcm->sync_ptr) {
657 free(pcm->sync_ptr);
658 pcm->sync_ptr = NULL;
659 } else {
660 int page_size = sysconf(_SC_PAGE_SIZE);
661 if (pcm->mmap_status)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530662 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700663 if (pcm->mmap_control)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530664 pcm->ops->munmap(pcm->data, pcm->mmap_control, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700665 }
666 pcm->mmap_status = NULL;
667 pcm->mmap_control = NULL;
668}
669
Simon Wilson79d39652011-05-25 13:44:23 -0700670static struct pcm bad_pcm = {
671 .fd = -1,
672};
673
Taylor Holberton6d58e012016-10-01 18:32:30 -0400674/** Gets the hardware parameters of a PCM, without created a PCM handle.
675 * @param card The card of the PCM.
676 * The default card is zero.
677 * @param device The device of the PCM.
678 * The default device is zero.
679 * @param flags Specifies whether the PCM is an input or output.
680 * May be one of the following:
681 * - @ref PCM_IN
682 * - @ref PCM_OUT
683 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800684 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400685 */
Simon Wilson43544882012-10-31 12:52:39 -0700686struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
687 unsigned int flags)
688{
689 struct snd_pcm_hw_params *params;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700690 void *snd_node = NULL, *data;
691 const struct pcm_ops *ops;
Simon Wilson43544882012-10-31 12:52:39 -0700692 int fd;
693
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700694 ops = &hw_ops;
695 fd = ops->open(card, device, flags, &data, snd_node);
Simon Wilson43544882012-10-31 12:52:39 -0700696
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700697#ifdef TINYALSA_USES_PLUGINS
Simon Wilson43544882012-10-31 12:52:39 -0700698 if (fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700699 int pcm_type;
700 snd_node = snd_utils_open_pcm(card, device);
701 pcm_type = snd_utils_get_node_type(snd_node);
702 if (!snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
703 fprintf(stderr, "no device (hw/plugin) for card(%u), device(%u)",
704 card, device);
705 goto err_open;
706 }
707 ops = &plug_ops;
708 fd = ops->open(card, device, flags, &data, snd_node);
709 }
710#endif
711 if (fd < 0) {
712 fprintf(stderr, "cannot open card(%d) device (%d): %s\n",
713 card, device, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700714 goto err_open;
715 }
716
717 params = calloc(1, sizeof(struct snd_pcm_hw_params));
718 if (!params)
719 goto err_calloc;
720
721 param_init(params);
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700722 if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
Simon Wilson43544882012-10-31 12:52:39 -0700723 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
724 goto err_hw_refine;
725 }
726
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700727#ifdef TINYALSA_USES_PLUGINS
728 if (snd_node)
729 snd_utils_close_dev_node(snd_node);
730#endif
731 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700732
733 return (struct pcm_params *)params;
734
735err_hw_refine:
736 free(params);
737err_calloc:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700738#ifdef TINYALSA_USES_PLUGINS
739 if (snd_node)
740 snd_utils_close_dev_node(snd_node);
741#endif
742 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700743err_open:
744 return NULL;
745}
746
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500747/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400748 * @param pcm_params Hardware parameters of a PCM.
749 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800750 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400751 */
Simon Wilson43544882012-10-31 12:52:39 -0700752void pcm_params_free(struct pcm_params *pcm_params)
753{
754 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
755
756 if (params)
757 free(params);
758}
759
760static int pcm_param_to_alsa(enum pcm_param param)
761{
762 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700763 case PCM_PARAM_ACCESS:
764 return SNDRV_PCM_HW_PARAM_ACCESS;
765 case PCM_PARAM_FORMAT:
766 return SNDRV_PCM_HW_PARAM_FORMAT;
767 case PCM_PARAM_SUBFORMAT:
768 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700769 case PCM_PARAM_SAMPLE_BITS:
770 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
771 break;
772 case PCM_PARAM_FRAME_BITS:
773 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
774 break;
775 case PCM_PARAM_CHANNELS:
776 return SNDRV_PCM_HW_PARAM_CHANNELS;
777 break;
778 case PCM_PARAM_RATE:
779 return SNDRV_PCM_HW_PARAM_RATE;
780 break;
781 case PCM_PARAM_PERIOD_TIME:
782 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
783 break;
784 case PCM_PARAM_PERIOD_SIZE:
785 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
786 break;
787 case PCM_PARAM_PERIOD_BYTES:
788 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
789 break;
790 case PCM_PARAM_PERIODS:
791 return SNDRV_PCM_HW_PARAM_PERIODS;
792 break;
793 case PCM_PARAM_BUFFER_TIME:
794 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
795 break;
796 case PCM_PARAM_BUFFER_SIZE:
797 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
798 break;
799 case PCM_PARAM_BUFFER_BYTES:
800 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
801 break;
802 case PCM_PARAM_TICK_TIME:
803 return SNDRV_PCM_HW_PARAM_TICK_TIME;
804 break;
805
806 default:
807 return -1;
808 }
809}
810
Taylor Holberton6d58e012016-10-01 18:32:30 -0400811/** Gets a mask from a PCM's hardware parameters.
812 * @param pcm_params A PCM's hardware parameters.
813 * @param param The parameter to get.
814 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
815 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800816 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400817 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800818const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700819 enum pcm_param param)
820{
821 int p;
822 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
823 if (params == NULL) {
824 return NULL;
825 }
826
827 p = pcm_param_to_alsa(param);
828 if (p < 0 || !param_is_mask(p)) {
829 return NULL;
830 }
831
Taylor Holberton2f387d22016-12-01 15:58:16 -0800832 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700833}
834
Taylor Holberton17a10242016-11-23 13:18:24 -0800835/** Get the minimum of a specified PCM parameter.
836 * @param pcm_params A PCM parameters structure.
837 * @param param The specified parameter to get the minimum of.
838 * @returns On success, the parameter minimum.
839 * On failure, zero.
840 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800841unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700842 enum pcm_param param)
843{
844 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
845 int p;
846
847 if (!params)
848 return 0;
849
850 p = pcm_param_to_alsa(param);
851 if (p < 0)
852 return 0;
853
854 return param_get_min(params, p);
855}
856
Taylor Holberton17a10242016-11-23 13:18:24 -0800857/** Get the maximum of a specified PCM parameter.
858 * @param pcm_params A PCM parameters structure.
859 * @param param The specified parameter to get the maximum of.
860 * @returns On success, the parameter maximum.
861 * On failure, zero.
862 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800863unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700864 enum pcm_param param)
865{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800866 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700867 int p;
868
869 if (!params)
870 return 0;
871
872 p = pcm_param_to_alsa(param);
873 if (p < 0)
874 return 0;
875
876 return param_get_max(params, p);
877}
878
dvdli9c9a4422020-10-28 16:05:19 +0800879static int pcm_mask_test(const struct pcm_mask *m, unsigned int index)
880{
881 const unsigned int bitshift = 5; /* for 32 bit integer */
882 const unsigned int bitmask = (1 << bitshift) - 1;
883 unsigned int element;
884
885 element = index >> bitshift;
886 if (element >= ARRAY_SIZE(m->bits))
887 return 0; /* for safety, but should never occur */
888 return (m->bits[element] >> (index & bitmask)) & 1;
889}
890
891static int pcm_mask_to_string(const struct pcm_mask *m, char *string, unsigned int size,
892 char *mask_name,
893 const char * const *bit_array_name, size_t bit_array_size)
894{
895 unsigned int i;
896 unsigned int offset = 0;
897
898 if (m == NULL)
899 return 0;
900 if (bit_array_size < 32) {
901 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
902 } else { /* spans two or more bitfields, print with an array index */
903 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
904 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
905 mask_name, i, m->bits[i]);
906 }
907 }
908 for (i = 0; i < bit_array_size; ++i) {
909 if (pcm_mask_test(m, i)) {
910 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
911 }
912 }
913 return offset;
914}
915
916int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
917{
918 const struct pcm_mask *m;
919 unsigned int min, max;
920 unsigned int clipoffset, offset;
921
922 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
923 offset = pcm_mask_to_string(m, string, size,
924 "Access", access_lookup, ARRAY_SIZE(access_lookup));
925 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
926 clipoffset = offset > size ? size : offset;
927 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
928 "Format", format_lookup, ARRAY_SIZE(format_lookup));
929 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
930 clipoffset = offset > size ? size : offset;
931 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
932 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
933 min = pcm_params_get_min(params, PCM_PARAM_RATE);
934 max = pcm_params_get_max(params, PCM_PARAM_RATE);
935 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
936 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
937 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
938 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
939 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
940 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
941 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
942 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
943 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
944 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
945 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
946 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
947 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
948 return offset;
949}
950
951int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
952{
953 unsigned int alsa_format = pcm_format_to_alsa(format);
954
955 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
956 return 0; /* caution: format not recognized is equivalent to S16_LE */
957 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
958}
959
Taylor Holberton6d58e012016-10-01 18:32:30 -0400960/** Closes a PCM returned by @ref pcm_open.
961 * @param pcm A PCM returned by @ref pcm_open.
962 * May not be NULL.
963 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800964 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400965 */
Simon Wilson79d39652011-05-25 13:44:23 -0700966int pcm_close(struct pcm *pcm)
967{
968 if (pcm == &bad_pcm)
969 return 0;
970
Eric Laurent40b018e2011-06-18 10:10:23 -0700971 pcm_hw_munmap_status(pcm);
972
Liam Girdwood6be28f12011-10-13 12:59:51 -0700973 if (pcm->flags & PCM_MMAP) {
974 pcm_stop(pcm);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530975 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -0700976 }
977
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700978 snd_utils_close_dev_node(pcm->snd_node);
979 pcm->ops->close(pcm->data);
Simon Wilson79d39652011-05-25 13:44:23 -0700980 pcm->buffer_size = 0;
981 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700982 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700983 return 0;
984}
985
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800986/** Opens a PCM by it's name.
987 * @param name The name of the PCM.
988 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
989 * @param flags Specify characteristics and functionality about the pcm.
990 * May be a bitwise AND of the following:
991 * - @ref PCM_IN
992 * - @ref PCM_OUT
993 * - @ref PCM_MMAP
994 * - @ref PCM_NOIRQ
995 * - @ref PCM_MONOTONIC
996 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800997 * @returns A PCM structure.
998 * If an error occurs allocating memory for the PCM, NULL is returned.
999 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1000 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001001 * @ingroup libtinyalsa-pcm
1002 */
1003struct pcm *pcm_open_by_name(const char *name,
1004 unsigned int flags,
1005 const struct pcm_config *config)
1006{
1007 unsigned int card, device;
1008 if ((name[0] != 'h')
1009 || (name[1] != 'w')
1010 || (name[2] != ':')) {
1011 return NULL;
1012 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1013 return NULL;
1014 }
1015 return pcm_open(card, device, flags, config);
1016}
1017
Taylor Holberton6d58e012016-10-01 18:32:30 -04001018/** Opens a PCM.
1019 * @param card The card that the pcm belongs to.
1020 * The default card is zero.
1021 * @param device The device that the pcm belongs to.
1022 * The default device is zero.
1023 * @param flags Specify characteristics and functionality about the pcm.
1024 * May be a bitwise AND of the following:
1025 * - @ref PCM_IN
1026 * - @ref PCM_OUT
1027 * - @ref PCM_MMAP
1028 * - @ref PCM_NOIRQ
1029 * - @ref PCM_MONOTONIC
1030 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001031 * @returns A PCM structure.
1032 * If an error occurs allocating memory for the PCM, NULL is returned.
1033 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1034 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001035 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001036 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001037struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001038 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001039{
Simon Wilson79d39652011-05-25 13:44:23 -07001040 struct pcm *pcm;
1041 struct snd_pcm_info info;
Eric Laurent40b018e2011-06-18 10:10:23 -07001042 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001043
1044 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001045 if (!pcm)
1046 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001047
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001048 /* Default to hw_ops, attemp plugin open only if hw (/dev/snd/pcm*) open fails */
1049 pcm->ops = &hw_ops;
1050 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, NULL);
Simon Wilson79d39652011-05-25 13:44:23 -07001051
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001052#ifdef TINYALSA_USES_PLUGINS
Simon Wilson79d39652011-05-25 13:44:23 -07001053 if (pcm->fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001054 int pcm_type;
1055 pcm->snd_node = snd_utils_open_pcm(card, device);
1056 pcm_type = snd_utils_get_node_type(pcm->snd_node);
1057 if (!pcm->snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
1058 oops(pcm, -ENODEV, "no device (hw/plugin) for card(%u), device(%u)",
1059 card, device);
Miguel GAIO859adb22020-04-18 08:40:41 +02001060 goto fail_close_dev_node;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001061 }
1062 pcm->ops = &plug_ops;
1063 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
1064 }
1065#endif
1066 if (pcm->fd < 0) {
1067 oops(pcm, errno, "cannot open device (%u) for card (%u)",
1068 device, card);
Miguel GAIO859adb22020-04-18 08:40:41 +02001069 goto fail_close_dev_node;
Simon Wilson79d39652011-05-25 13:44:23 -07001070 }
1071
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001072 pcm->flags = flags;
1073
1074 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001075 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001076 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001077 }
David Wagner4cddf192014-04-02 15:12:54 +02001078 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001079
Taylor Holberton861da7a2017-04-10 12:05:51 -07001080 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001081 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001082
Eric Laurent40b018e2011-06-18 10:10:23 -07001083 rc = pcm_hw_mmap_status(pcm);
1084 if (rc < 0) {
1085 oops(pcm, rc, "mmap status failed");
1086 goto fail;
1087 }
1088
Glenn Kasten81012402013-08-22 15:11:48 -07001089#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1090 if (pcm->flags & PCM_MONOTONIC) {
1091 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001092 rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
Glenn Kasten81012402013-08-22 15:11:48 -07001093 if (rc < 0) {
1094 oops(pcm, rc, "cannot set timestamp type");
1095 goto fail;
1096 }
1097 }
1098#endif
1099
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -03001100 /* prepare here so the user does not need to do this later */
1101 if (pcm_prepare(pcm))
1102 goto fail;
1103
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001104 pcm->xruns = 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001105 return pcm;
1106
1107fail:
Ricardo Biehl Pasquali0bfac892019-01-04 14:45:35 -02001108 pcm_hw_munmap_status(pcm);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001109 if (flags & PCM_MMAP)
Rohit kumarf29b8df2020-08-19 15:19:33 +05301110 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -07001111fail_close:
Miguel GAIO859adb22020-04-18 08:40:41 +02001112 pcm->ops->close(pcm->data);
1113fail_close_dev_node:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001114#ifdef TINYALSA_USES_PLUGINS
1115 if (pcm->snd_node)
1116 snd_utils_close_dev_node(pcm->snd_node);
1117#endif
Miguel GAIO859adb22020-04-18 08:40:41 +02001118 free(pcm);
Lubomir Rintel00f5aa12020-04-10 09:10:33 +02001119 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001120}
1121
Taylor Holberton6d58e012016-10-01 18:32:30 -04001122/** Checks if a PCM file has been opened without error.
1123 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001124 * May be NULL.
1125 * @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 -04001126 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001127 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001128 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001129int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001130{
Taylor Holbertone123a652017-01-13 21:39:48 -08001131 if (pcm != NULL) {
1132 return pcm->fd >= 0;
1133 }
1134 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001135}
Simon Wilsond6458e62011-06-21 14:58:11 -07001136
Taylor Holberton558e5942016-12-04 13:42:28 -08001137/** Links two PCMs.
1138 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1139 * If an error occurs, the error message will be written to @p pcm1.
1140 * @param pcm1 A PCM handle.
1141 * @param pcm2 Another PCM handle.
1142 * @return On success, zero; on failure, a negative number.
1143 * @ingroup libtinyalsa-pcm
1144 */
1145int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1146{
1147 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1148 if (err == -1) {
1149 return oops(pcm1, errno, "cannot link PCM");
1150 }
1151 return 0;
1152}
1153
1154/** Unlinks a PCM.
1155 * @see @ref pcm_link
1156 * @param pcm A PCM handle.
1157 * @return On success, zero; on failure, a negative number.
1158 * @ingroup libtinyalsa-pcm
1159 */
1160int pcm_unlink(struct pcm *pcm)
1161{
1162 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1163 if (err == -1) {
1164 return oops(pcm, errno, "cannot unlink PCM");
1165 }
1166 return 0;
1167}
1168
Taylor Holberton6d58e012016-10-01 18:32:30 -04001169/** Prepares a PCM, if it has not been prepared already.
1170 * @param pcm A PCM handle.
1171 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001172 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001173 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301174int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001175{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001176 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001177 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001178
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001179 /* get appl_ptr and avail_min from kernel */
1180 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1181
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301182 return 0;
1183}
1184
Taylor Holberton6d58e012016-10-01 18:32:30 -04001185/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001186 * @param pcm A PCM handle.
1187 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001188 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001189 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301190int pcm_start(struct pcm *pcm)
1191{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001192 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -02001193 if (pcm_sync_ptr(pcm, 0) < 0)
1194 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001195
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001196 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001197 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001198 return oops(pcm, errno, "cannot start channel");
1199 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001200
1201 return 0;
1202}
1203
Taylor Holberton6d58e012016-10-01 18:32:30 -04001204/** Stops a PCM.
1205 * @param pcm A PCM handle.
1206 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001207 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001208 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001209int pcm_stop(struct pcm *pcm)
1210{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001211 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001212 return oops(pcm, errno, "cannot stop channel");
1213
1214 return 0;
1215}
1216
Liam Girdwood6be28f12011-10-13 12:59:51 -07001217static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1218{
1219 int avail;
1220
1221 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1222
1223 if (avail < 0)
1224 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001225 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001226 avail -= pcm->boundary;
1227
1228 return avail;
1229}
1230
1231static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1232{
1233 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1234 if (avail < 0)
1235 avail += pcm->boundary;
1236 return avail;
1237}
1238
dvdlifaaa6972020-10-28 22:18:40 +08001239int pcm_mmap_avail(struct pcm *pcm)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001240{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001241 if (pcm->flags & PCM_IN)
1242 return pcm_mmap_capture_avail(pcm);
1243 else
1244 return pcm_mmap_playback_avail(pcm);
1245}
1246
1247static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1248{
1249 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1250 appl_ptr += frames;
1251
1252 /* check for boundary wrap */
1253 if (appl_ptr > pcm->boundary)
1254 appl_ptr -= pcm->boundary;
1255 pcm->mmap_control->appl_ptr = appl_ptr;
1256}
1257
1258int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1259 unsigned int *frames)
1260{
1261 unsigned int continuous, copy_frames, avail;
1262
1263 /* return the mmap buffer */
1264 *areas = pcm->mmap_buffer;
1265
1266 /* and the application offset in frames */
1267 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1268
1269 avail = pcm_mmap_avail(pcm);
1270 if (avail > pcm->buffer_size)
1271 avail = pcm->buffer_size;
1272 continuous = pcm->buffer_size - *offset;
1273
1274 /* we can only copy frames if the are availabale and continuos */
1275 copy_frames = *frames;
1276 if (copy_frames > avail)
1277 copy_frames = avail;
1278 if (copy_frames > continuous)
1279 copy_frames = continuous;
1280 *frames = copy_frames;
1281
1282 return 0;
1283}
1284
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001285static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1286 char *buf, unsigned int src_offset,
1287 unsigned int frames)
1288{
1289 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1290 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1291 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1292
1293 /* interleaved only atm */
1294 if (pcm->flags & PCM_IN)
1295 memcpy(buf + src_offset_bytes,
1296 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1297 size_bytes);
1298 else
1299 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1300 buf + src_offset_bytes,
1301 size_bytes);
1302 return 0;
1303}
1304
Liam Girdwood6be28f12011-10-13 12:59:51 -07001305int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1306{
Taylor Holberton72e44222016-11-22 09:54:47 -08001307 int ret;
1308
Taylor Holberton73466c02016-10-01 12:51:59 -04001309 /* not used */
1310 (void) offset;
1311
Liam Girdwood6be28f12011-10-13 12:59:51 -07001312 /* update the application pointer in userspace and kernel */
1313 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001314 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001315 if (ret != 0){
1316 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001317 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001318 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001319
1320 return frames;
1321}
1322
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001323static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1324 unsigned int offset, unsigned int size)
1325{
1326 void *pcm_areas;
1327 int commit;
1328 unsigned int pcm_offset, frames, count = 0;
1329
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001330 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001331 frames = size;
1332 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1333 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1334 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1335 if (commit < 0) {
1336 oops(pcm, commit, "failed to commit %d frames\n", frames);
1337 return commit;
1338 }
1339
1340 offset += commit;
1341 count += commit;
1342 size -= commit;
1343 }
1344 return count;
1345}
1346
dvdli72216212020-10-28 22:22:27 +08001347int pcm_get_poll_fd(struct pcm *pcm)
1348{
1349 return pcm->fd;
1350}
1351
Liam Girdwood6be28f12011-10-13 12:59:51 -07001352int pcm_avail_update(struct pcm *pcm)
1353{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001354 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001355 return pcm_mmap_avail(pcm);
1356}
1357
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001358/** Returns available frames in pcm buffer and corresponding time stamp.
1359 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1360 * otherwise the clock is CLOCK_REALTIME.
1361 * For an input stream, frames available are frames ready for the application to read.
1362 * 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 -02001363 * @param pcm A PCM handle.
1364 * @param avail The number of available frames
1365 * @param tstamp The timestamp
1366 * @return On success, zero is returned; on failure, negative one.
1367 */
1368int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1369 struct timespec *tstamp)
1370{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001371 int checking;
1372 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001373
1374 if (!pcm_is_ready(pcm))
1375 return -1;
1376
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001377 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001378
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001379again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001380
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001381 tmp = pcm_avail_update(pcm);
1382 if (tmp < 0)
1383 return tmp; /* error */
1384
1385 if (checking && (unsigned int) tmp == *avail)
1386 return 0;
1387
1388 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001389 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001390
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001391 /*
1392 * When status is mmaped, get avail again to ensure
1393 * valid timestamp.
1394 */
1395 if (!pcm->sync_ptr) {
1396 checking = 1;
1397 goto again;
1398 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001399
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001400 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001401 return 0;
1402}
1403
Liam Girdwood6be28f12011-10-13 12:59:51 -07001404int pcm_state(struct pcm *pcm)
1405{
1406 int err = pcm_sync_ptr(pcm, 0);
1407 if (err < 0)
1408 return err;
1409
1410 return pcm->mmap_status->state;
1411}
1412
Taylor Holberton17a10242016-11-23 13:18:24 -08001413/** Waits for frames to be available for read or write operations.
1414 * @param pcm A PCM handle.
1415 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1416 * @returns If frames became available, one is returned.
1417 * If a timeout occured, zero is returned.
1418 * If an error occured, a negative number is returned.
1419 * @ingroup libtinyalsa-pcm
1420 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001421int pcm_wait(struct pcm *pcm, int timeout)
1422{
1423 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001424 int err;
1425
1426 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001427 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001428
1429 do {
1430 /* let's wait for avail or timeout */
Rohit kumarf29b8df2020-08-19 15:19:33 +05301431 err = pcm->ops->poll(pcm->data, &pfd, 1, timeout);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001432 if (err < 0)
1433 return -errno;
1434
1435 /* timeout ? */
1436 if (err == 0)
1437 return 0;
1438
1439 /* have we been interrupted ? */
1440 if (errno == -EINTR)
1441 continue;
1442
1443 /* check for any errors */
1444 if (pfd.revents & (POLLERR | POLLNVAL)) {
1445 switch (pcm_state(pcm)) {
1446 case PCM_STATE_XRUN:
1447 return -EPIPE;
1448 case PCM_STATE_SUSPENDED:
1449 return -ESTRPIPE;
1450 case PCM_STATE_DISCONNECTED:
1451 return -ENODEV;
1452 default:
1453 return -EIO;
1454 }
1455 }
1456 /* poll again if fd not ready for IO */
1457 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1458
1459 return 1;
1460}
1461
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001462/*
1463 * Transfer data to/from mmaped buffer. This imitates the
1464 * behavior of read/write system calls.
1465 *
1466 * However, this doesn't seems to offer any advantage over
1467 * the read/write syscalls. Should it be removed?
1468 */
1469int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001470{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001471 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001472
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001473 int state;
1474 unsigned int avail;
1475 unsigned int user_offset;
1476
1477 int err;
1478 int tmp;
1479
1480 is_playback = !(pcm->flags & PCM_IN);
1481
1482 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001483 return 0;
1484
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001485 /* update hardware pointer and get state */
1486 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1487 SNDRV_PCM_SYNC_PTR_APPL |
1488 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1489 if (err == -1)
1490 return -1;
1491 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001492
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001493 /*
1494 * If frames < start_threshold, wait indefinitely.
1495 * Another thread may start capture
1496 */
1497 if (!is_playback && state == PCM_STATE_PREPARED &&
1498 frames >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001499 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001500 if (err == -1)
1501 return -1;
1502 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001503 }
1504
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001505 avail = pcm_mmap_avail(pcm);
1506 user_offset = 0;
1507
1508 while (frames) {
1509 if (!avail) {
1510 if (pcm->flags & PCM_NONBLOCK) {
1511 errno = EAGAIN;
1512 break;
1513 }
1514
1515 /* wait for interrupt */
1516 err = pcm_wait(pcm, -1);
1517 if (err < 0) {
1518 errno = -err;
1519 break;
1520 }
1521
1522 /* get hardware pointer */
1523 avail = pcm_avail_update(pcm);
1524 }
1525
1526 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1527 if (tmp < 0)
1528 break;
1529
1530 user_offset += tmp;
1531 frames -= tmp;
1532 avail -= tmp;
1533
1534 /* start playback if written >= start_threshold */
1535 if (is_playback && state == PCM_STATE_PREPARED &&
1536 pcm->buffer_size - avail >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001537 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001538 if (err == -1)
1539 break;
1540 }
1541 }
1542
1543 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001544}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001545
1546int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1547{
1548 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1549 return -ENOSYS;
1550
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001551 return pcm_mmap_transfer(pcm, (void *)data,
1552 pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001553}
1554
1555int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1556{
1557 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1558 return -ENOSYS;
1559
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001560 return pcm_mmap_transfer(pcm, data, pcm_bytes_to_frames(pcm, count));
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001561}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301562
dvdlie43e85c2020-10-28 22:56:53 +08001563/* Returns current read/write position in the mmap buffer with associated time stamp. */
1564int pcm_mmap_get_hw_ptr(struct pcm* pcm, unsigned int *hw_ptr, struct timespec *tstamp)
1565{
1566 int rc;
1567
1568 if (pcm == NULL || hw_ptr == NULL || tstamp == NULL)
1569 return oops(pcm, EINVAL, "pcm %p, hw_ptr %p, tstamp %p", pcm, hw_ptr, tstamp);
1570
1571 if (!pcm_is_ready(pcm))
1572 return oops(pcm, errno, "pcm_is_ready failed");
1573
1574 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1575 if (rc < 0)
1576 return oops(pcm, errno, "pcm_sync_ptr failed");
1577
1578 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
1579 (pcm->mmap_status->state != PCM_STATE_DRAINING))
1580 return oops(pcm, ENOSYS, "invalid stream state %d", pcm->mmap_status->state);
1581
1582 *tstamp = pcm->mmap_status->tstamp;
1583 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
1584 return oops(pcm, errno, "invalid time stamp");
1585
1586 *hw_ptr = pcm->mmap_status->hw_ptr;
1587
1588 return 0;
1589}
1590
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001591static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1592{
1593 int is_playback;
1594
1595 struct snd_xferi transfer;
1596 int res;
1597
1598 is_playback = !(pcm->flags & PCM_IN);
1599
1600 transfer.buf = data;
1601 transfer.frames = frames;
1602 transfer.result = 0;
1603
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001604 res = pcm->ops->ioctl(pcm->data, is_playback
1605 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1606 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001607
1608 return res == 0 ? (int) transfer.result : -1;
1609}
1610
1611static int pcm_generic_transfer(struct pcm *pcm, void *data,
1612 unsigned int frames)
1613{
1614 int res;
1615
1616#if UINT_MAX > TINYALSA_FRAMES_MAX
1617 if (frames > TINYALSA_FRAMES_MAX)
1618 return -EINVAL;
1619#endif
1620 if (frames > INT_MAX)
1621 return -EINVAL;
1622
1623again:
1624
1625 if (pcm->flags & PCM_MMAP)
1626 res = pcm_mmap_transfer(pcm, data, frames);
1627 else
1628 res = pcm_rw_transfer(pcm, data, frames);
1629
1630 if (res < 0) {
1631 switch (errno) {
1632 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001633 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001634 /* fallthrough */
1635 case ESTRPIPE:
1636 /*
1637 * Try to restart if we are allowed to do so.
1638 * Otherwise, return error.
1639 */
1640 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1641 return -1;
1642 goto again;
1643 case EAGAIN:
1644 if (pcm->flags & PCM_NONBLOCK)
1645 return -1;
1646 /* fallthrough */
1647 default:
1648 return oops(pcm, errno, "cannot read/write stream data");
1649 }
1650 }
1651
1652 return res;
1653}
1654
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001655/** Writes audio samples to PCM.
1656 * If the PCM has not been started, it is started in this function.
1657 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1658 * @param pcm A PCM handle.
1659 * @param data The audio sample array
1660 * @param frame_count The number of frames occupied by the sample array.
1661 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1662 * or INT_MAX.
1663 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1664 * @ingroup libtinyalsa-pcm
1665 */
1666int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1667{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001668 if (pcm->flags & PCM_IN)
1669 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001670
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001671 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001672}
1673
1674/** Reads audio samples from PCM.
1675 * If the PCM has not been started, it is started in this function.
1676 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1677 * @param pcm A PCM handle.
1678 * @param data The audio sample array
1679 * @param frame_count The number of frames occupied by the sample array.
1680 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1681 * or INT_MAX.
1682 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1683 * @ingroup libtinyalsa-pcm
1684 */
1685int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1686{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001687 if (!(pcm->flags & PCM_IN))
1688 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001689
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001690 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001691}
1692
1693/** Writes audio samples to PCM.
1694 * If the PCM has not been started, it is started in this function.
1695 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1696 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1697 * @param pcm A PCM handle.
1698 * @param data The audio sample array
1699 * @param count The number of bytes occupied by the sample array.
1700 * @return On success, this function returns zero; otherwise, a negative number.
1701 * @deprecated
1702 * @ingroup libtinyalsa-pcm
1703 */
1704int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1705{
1706 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
1707}
1708
1709/** Reads audio samples from PCM.
1710 * If the PCM has not been started, it is started in this function.
1711 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1712 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1713 * @param pcm A PCM handle.
1714 * @param data The audio sample array
1715 * @param count The number of bytes occupied by the sample array.
1716 * @return On success, this function returns zero; otherwise, a negative number.
1717 * @deprecated
1718 * @ingroup libtinyalsa-pcm
1719 */
1720int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1721{
1722 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
1723}
1724
Taylor Holberton17a10242016-11-23 13:18:24 -08001725/** Gets the delay of the PCM, in terms of frames.
1726 * @param pcm A PCM handle.
1727 * @returns On success, the delay of the PCM.
1728 * On failure, a negative number.
1729 * @ingroup libtinyalsa-pcm
1730 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301731long pcm_get_delay(struct pcm *pcm)
1732{
1733 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1734 return -1;
1735
1736 return pcm->pcm_delay;
1737}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001738