blob: 3d50ea7211aed828f082d8ef7613b9f908f07fbb [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;
dvdlib1b35822021-02-22 17:24:30 +0800285
286 case PCM_FORMAT_FLOAT_LE:
287 return SNDRV_PCM_FORMAT_FLOAT_LE;
288 case PCM_FORMAT_FLOAT_BE:
289 return SNDRV_PCM_FORMAT_FLOAT_BE;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700290 };
291}
292
Simon Wilson79d39652011-05-25 13:44:23 -0700293#define PCM_ERROR_MAX 128
294
Taylor Holberton6d58e012016-10-01 18:32:30 -0400295/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800296 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400297 */
Simon Wilson79d39652011-05-25 13:44:23 -0700298struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400299 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700300 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400301 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700302 unsigned int flags;
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -0200303 /** The number of (under/over)runs that have occured */
304 int xruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400305 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700306 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800307 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700308 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400309 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700310 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400311 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700312 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700313 struct snd_pcm_mmap_status *mmap_status;
314 struct snd_pcm_mmap_control *mmap_control;
315 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700316 void *mmap_buffer;
317 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800318 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530319 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800320 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200321 unsigned int subdevice;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700322 /** Pointer to the pcm ops, either hw or plugin */
323 const struct pcm_ops *ops;
324 /** Private data for pcm_hw or pcm_plugin */
325 void *data;
326 /** Pointer to the pcm node from snd card definition */
327 struct snd_node *snd_node;
Simon Wilson79d39652011-05-25 13:44:23 -0700328};
329
Taylor Holberton861da7a2017-04-10 12:05:51 -0700330static int oops(struct pcm *pcm, int e, const char *fmt, ...)
331{
332 va_list ap;
333 int sz;
334
335 va_start(ap, fmt);
336 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
337 va_end(ap);
338 sz = strlen(pcm->error);
339
dvdlib39234c2020-10-29 16:24:43 +0800340 if (e)
Taylor Holberton861da7a2017-04-10 12:05:51 -0700341 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
342 ": %s", strerror(e));
343 return -1;
344}
345
Taylor Holberton6d58e012016-10-01 18:32:30 -0400346/** Gets the buffer size of the PCM.
347 * @param pcm A PCM handle.
348 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800349 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400350 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800351unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700352{
353 return pcm->buffer_size;
354}
355
Taylor Holberton77979a82016-12-01 20:04:04 -0800356/** Gets the channel count of the PCM.
357 * @param pcm A PCM handle.
358 * @return The channel count of the PCM.
359 * @ingroup libtinyalsa-pcm
360 */
361unsigned int pcm_get_channels(const struct pcm *pcm)
362{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700363 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800364}
365
Taylor Holberton08bb5902017-04-10 11:45:44 -0700366/** Gets the PCM configuration.
367 * @param pcm A PCM handle.
368 * @return The PCM configuration.
369 * This function only returns NULL if
370 * @p pcm is NULL.
371 * @ingroup libtinyalsa-pcm
372 * */
373const struct pcm_config * pcm_get_config(const struct pcm *pcm)
374{
375 if (pcm == NULL)
376 return NULL;
377 return &pcm->config;
378}
379
Taylor Holberton77979a82016-12-01 20:04:04 -0800380/** Gets the rate of the PCM.
381 * The rate is given in frames per second.
382 * @param pcm A PCM handle.
383 * @return The rate of the PCM.
384 * @ingroup libtinyalsa-pcm
385 */
386unsigned int pcm_get_rate(const struct pcm *pcm)
387{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700388 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800389}
390
391/** Gets the format of the PCM.
392 * @param pcm A PCM handle.
393 * @return The format of the PCM.
394 * @ingroup libtinyalsa-pcm
395 */
396enum pcm_format pcm_get_format(const struct pcm *pcm)
397{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700398 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800399}
400
Taylor Holberton6d58e012016-10-01 18:32:30 -0400401/** Gets the file descriptor of the PCM.
402 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800403 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400404 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800405 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400406 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800407int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400408{
409 return pcm->fd;
410}
411
Taylor Holberton6d58e012016-10-01 18:32:30 -0400412/** Gets the error message for the last error that occured.
413 * If no error occured and this function is called, the results are undefined.
414 * @param pcm A PCM handle.
415 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800416 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400417 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800418const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700419{
420 return pcm->error;
421}
422
Taylor Holberton861da7a2017-04-10 12:05:51 -0700423/** Sets the PCM configuration.
424 * @param pcm A PCM handle.
425 * @param config The configuration to use for the
426 * PCM. This parameter may be NULL, in which case
427 * the default configuration is used.
428 * @returns Zero on success, a negative errno value
429 * on failure.
430 * @ingroup libtinyalsa-pcm
431 * */
432int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
433{
434 if (pcm == NULL)
435 return -EFAULT;
436 else if (config == NULL) {
437 config = &pcm->config;
438 pcm->config.channels = 2;
439 pcm->config.rate = 48000;
440 pcm->config.period_size = 1024;
441 pcm->config.period_count = 4;
442 pcm->config.format = PCM_FORMAT_S16_LE;
443 pcm->config.start_threshold = config->period_count * config->period_size;
444 pcm->config.stop_threshold = config->period_count * config->period_size;
445 pcm->config.silence_threshold = 0;
dvdli1e75a162020-10-28 17:37:19 +0800446 pcm->config.silence_size = 0;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700447 } else
448 pcm->config = *config;
449
450 struct snd_pcm_hw_params params;
451 param_init(&params);
452 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
453 pcm_format_to_alsa(config->format));
Taylor Holberton861da7a2017-04-10 12:05:51 -0700454 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700455 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
456 config->channels);
457 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
458 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
459
460 if (pcm->flags & PCM_NOIRQ) {
461
462 if (!(pcm->flags & PCM_MMAP)) {
dvdlib39234c2020-10-29 16:24:43 +0800463 oops(pcm, EINVAL, "noirq only currently supported with mmap().");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700464 return -EINVAL;
465 }
466
467 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
468 pcm->noirq_frames_per_msec = config->rate / 1000;
469 }
470
471 if (pcm->flags & PCM_MMAP)
472 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
473 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
474 else
475 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
476 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
477
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700478 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700479 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200480 oops(pcm, errno, "cannot set hw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700481 return -errno_copy;
482 }
483
484 /* get our refined hw_params */
485 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
486 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
487 pcm->buffer_size = config->period_count * config->period_size;
488
489 if (pcm->flags & PCM_MMAP) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530490 pcm->mmap_buffer = pcm->ops->mmap(pcm->data, NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
491 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
Taylor Holberton861da7a2017-04-10 12:05:51 -0700492 if (pcm->mmap_buffer == MAP_FAILED) {
493 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200494 oops(pcm, errno, "failed to mmap buffer %d bytes\n",
Taylor Holberton861da7a2017-04-10 12:05:51 -0700495 pcm_frames_to_bytes(pcm, pcm->buffer_size));
496 return -errno_copy;
497 }
498 }
499
500 struct snd_pcm_sw_params sparams;
501 memset(&sparams, 0, sizeof(sparams));
502 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
503 sparams.period_step = 1;
Miguel GAIO05c64c32020-02-06 21:09:31 +0100504 sparams.avail_min = config->period_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700505
506 if (!config->start_threshold) {
507 if (pcm->flags & PCM_IN)
508 pcm->config.start_threshold = sparams.start_threshold = 1;
509 else
510 pcm->config.start_threshold = sparams.start_threshold =
511 config->period_count * config->period_size / 2;
512 } else
513 sparams.start_threshold = config->start_threshold;
514
515 /* pick a high stop threshold - todo: does this need further tuning */
516 if (!config->stop_threshold) {
517 if (pcm->flags & PCM_IN)
518 pcm->config.stop_threshold = sparams.stop_threshold =
519 config->period_count * config->period_size * 10;
520 else
521 pcm->config.stop_threshold = sparams.stop_threshold =
522 config->period_count * config->period_size;
523 }
524 else
525 sparams.stop_threshold = config->stop_threshold;
526
527 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
dvdli1e75a162020-10-28 17:37:19 +0800528 sparams.silence_size = config->silence_size;
Taylor Holberton861da7a2017-04-10 12:05:51 -0700529 sparams.silence_threshold = config->silence_threshold;
530 pcm->boundary = sparams.boundary = pcm->buffer_size;
531
532 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
533 pcm->boundary *= 2;
534
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700535 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
Taylor Holberton861da7a2017-04-10 12:05:51 -0700536 int errno_copy = errno;
Lubomir Rintel8fca97e2020-04-10 09:09:31 +0200537 oops(pcm, errno, "cannot set sw params");
Taylor Holberton861da7a2017-04-10 12:05:51 -0700538 return -errno_copy;
539 }
540
541 return 0;
542}
543
Taylor Holberton6d58e012016-10-01 18:32:30 -0400544/** Gets the subdevice on which the pcm has been opened.
545 * @param pcm A PCM handle.
546 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800547unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200548{
549 return pcm->subdevice;
550}
551
Taylor Holberton6d58e012016-10-01 18:32:30 -0400552/** Determines the number of bits occupied by a @ref pcm_format.
553 * @param format A PCM format.
554 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800555 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400556 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700557unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700558{
559 switch (format) {
560 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400561 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700562 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400563 case PCM_FORMAT_S24_BE:
dvdlib1b35822021-02-22 17:24:30 +0800564 case PCM_FORMAT_FLOAT_LE:
565 case PCM_FORMAT_FLOAT_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700566 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400567 case PCM_FORMAT_S24_3LE:
568 case PCM_FORMAT_S24_3BE:
569 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700570 default:
571 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400572 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700573 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400574 case PCM_FORMAT_S8:
575 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700576 };
577}
578
Taylor Holberton6d58e012016-10-01 18:32:30 -0400579/** Determines how many frames of a PCM can fit into a number of bytes.
580 * @param pcm A PCM handle.
581 * @param bytes The number of bytes.
582 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800583 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400584 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800585unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700586{
587 return bytes / (pcm->config.channels *
588 (pcm_format_to_bits(pcm->config.format) >> 3));
589}
590
Taylor Holberton6d58e012016-10-01 18:32:30 -0400591/** Determines how many bytes are occupied by a number of frames of a PCM.
592 * @param pcm A PCM handle.
593 * @param frames The number of frames of a PCM.
594 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800595 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400596 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800597unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700598{
599 return frames * pcm->config.channels *
600 (pcm_format_to_bits(pcm->config.format) >> 3);
601}
602
Taylor Holberton4f556062016-09-16 09:54:36 -0400603static int pcm_sync_ptr(struct pcm *pcm, int flags)
604{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200605 if (pcm->sync_ptr == NULL) {
606 /* status and control are mmaped */
607
608 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
609 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
610 oops(pcm, errno, "failed to sync hardware pointer");
611 return -1;
612 }
613 }
614 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700615 pcm->sync_ptr->flags = flags;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700616 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
617 pcm->sync_ptr) < 0) {
Taylor Holbertone123a652017-01-13 21:39:48 -0800618 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700619 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800620 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700621 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200622
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200623 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700624}
625
Taylor Holberton4f556062016-09-16 09:54:36 -0400626static int pcm_hw_mmap_status(struct pcm *pcm)
627{
Eric Laurent40b018e2011-06-18 10:10:23 -0700628 if (pcm->sync_ptr)
629 return 0;
630
631 int page_size = sysconf(_SC_PAGE_SIZE);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530632 pcm->mmap_status = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ, MAP_SHARED,
633 SNDRV_PCM_MMAP_OFFSET_STATUS);
Eric Laurent40b018e2011-06-18 10:10:23 -0700634 if (pcm->mmap_status == MAP_FAILED)
635 pcm->mmap_status = NULL;
636 if (!pcm->mmap_status)
637 goto mmap_error;
638
Rohit kumarf29b8df2020-08-19 15:19:33 +0530639 pcm->mmap_control = pcm->ops->mmap(pcm->data, NULL, page_size, PROT_READ | PROT_WRITE,
640 MAP_SHARED, SNDRV_PCM_MMAP_OFFSET_CONTROL);
Eric Laurent40b018e2011-06-18 10:10:23 -0700641 if (pcm->mmap_control == MAP_FAILED)
642 pcm->mmap_control = NULL;
643 if (!pcm->mmap_control) {
Rohit kumarf29b8df2020-08-19 15:19:33 +0530644 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700645 pcm->mmap_status = NULL;
646 goto mmap_error;
647 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700648
649 return 0;
650
651mmap_error:
652
653 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
654 if (!pcm->sync_ptr)
655 return -ENOMEM;
656 pcm->mmap_status = &pcm->sync_ptr->s.status;
657 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700658
659 return 0;
660}
661
662static void pcm_hw_munmap_status(struct pcm *pcm) {
663 if (pcm->sync_ptr) {
664 free(pcm->sync_ptr);
665 pcm->sync_ptr = NULL;
666 } else {
667 int page_size = sysconf(_SC_PAGE_SIZE);
668 if (pcm->mmap_status)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530669 pcm->ops->munmap(pcm->data, pcm->mmap_status, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700670 if (pcm->mmap_control)
Rohit kumarf29b8df2020-08-19 15:19:33 +0530671 pcm->ops->munmap(pcm->data, pcm->mmap_control, page_size);
Eric Laurent40b018e2011-06-18 10:10:23 -0700672 }
673 pcm->mmap_status = NULL;
674 pcm->mmap_control = NULL;
675}
676
Simon Wilson79d39652011-05-25 13:44:23 -0700677static struct pcm bad_pcm = {
678 .fd = -1,
679};
680
Taylor Holberton6d58e012016-10-01 18:32:30 -0400681/** Gets the hardware parameters of a PCM, without created a PCM handle.
682 * @param card The card of the PCM.
683 * The default card is zero.
684 * @param device The device of the PCM.
685 * The default device is zero.
686 * @param flags Specifies whether the PCM is an input or output.
687 * May be one of the following:
688 * - @ref PCM_IN
689 * - @ref PCM_OUT
690 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800691 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400692 */
Simon Wilson43544882012-10-31 12:52:39 -0700693struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
694 unsigned int flags)
695{
696 struct snd_pcm_hw_params *params;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700697 void *snd_node = NULL, *data;
698 const struct pcm_ops *ops;
Simon Wilson43544882012-10-31 12:52:39 -0700699 int fd;
700
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700701 ops = &hw_ops;
702 fd = ops->open(card, device, flags, &data, snd_node);
Simon Wilson43544882012-10-31 12:52:39 -0700703
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700704#ifdef TINYALSA_USES_PLUGINS
Simon Wilson43544882012-10-31 12:52:39 -0700705 if (fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700706 int pcm_type;
707 snd_node = snd_utils_open_pcm(card, device);
708 pcm_type = snd_utils_get_node_type(snd_node);
709 if (!snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
710 fprintf(stderr, "no device (hw/plugin) for card(%u), device(%u)",
711 card, device);
712 goto err_open;
713 }
714 ops = &plug_ops;
715 fd = ops->open(card, device, flags, &data, snd_node);
716 }
717#endif
718 if (fd < 0) {
719 fprintf(stderr, "cannot open card(%d) device (%d): %s\n",
720 card, device, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700721 goto err_open;
722 }
723
724 params = calloc(1, sizeof(struct snd_pcm_hw_params));
725 if (!params)
726 goto err_calloc;
727
728 param_init(params);
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700729 if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
Simon Wilson43544882012-10-31 12:52:39 -0700730 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
731 goto err_hw_refine;
732 }
733
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700734#ifdef TINYALSA_USES_PLUGINS
735 if (snd_node)
736 snd_utils_close_dev_node(snd_node);
737#endif
738 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700739
740 return (struct pcm_params *)params;
741
742err_hw_refine:
743 free(params);
744err_calloc:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700745#ifdef TINYALSA_USES_PLUGINS
746 if (snd_node)
747 snd_utils_close_dev_node(snd_node);
748#endif
749 ops->close(data);
Simon Wilson43544882012-10-31 12:52:39 -0700750err_open:
751 return NULL;
752}
753
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500754/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400755 * @param pcm_params Hardware parameters of a PCM.
756 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800757 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400758 */
Simon Wilson43544882012-10-31 12:52:39 -0700759void pcm_params_free(struct pcm_params *pcm_params)
760{
761 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
762
763 if (params)
764 free(params);
765}
766
767static int pcm_param_to_alsa(enum pcm_param param)
768{
769 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700770 case PCM_PARAM_ACCESS:
771 return SNDRV_PCM_HW_PARAM_ACCESS;
772 case PCM_PARAM_FORMAT:
773 return SNDRV_PCM_HW_PARAM_FORMAT;
774 case PCM_PARAM_SUBFORMAT:
775 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700776 case PCM_PARAM_SAMPLE_BITS:
777 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
778 break;
779 case PCM_PARAM_FRAME_BITS:
780 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
781 break;
782 case PCM_PARAM_CHANNELS:
783 return SNDRV_PCM_HW_PARAM_CHANNELS;
784 break;
785 case PCM_PARAM_RATE:
786 return SNDRV_PCM_HW_PARAM_RATE;
787 break;
788 case PCM_PARAM_PERIOD_TIME:
789 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
790 break;
791 case PCM_PARAM_PERIOD_SIZE:
792 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
793 break;
794 case PCM_PARAM_PERIOD_BYTES:
795 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
796 break;
797 case PCM_PARAM_PERIODS:
798 return SNDRV_PCM_HW_PARAM_PERIODS;
799 break;
800 case PCM_PARAM_BUFFER_TIME:
801 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
802 break;
803 case PCM_PARAM_BUFFER_SIZE:
804 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
805 break;
806 case PCM_PARAM_BUFFER_BYTES:
807 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
808 break;
809 case PCM_PARAM_TICK_TIME:
810 return SNDRV_PCM_HW_PARAM_TICK_TIME;
811 break;
812
813 default:
814 return -1;
815 }
816}
817
Taylor Holberton6d58e012016-10-01 18:32:30 -0400818/** Gets a mask from a PCM's hardware parameters.
819 * @param pcm_params A PCM's hardware parameters.
820 * @param param The parameter to get.
821 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
822 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800823 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400824 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800825const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700826 enum pcm_param param)
827{
828 int p;
829 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
830 if (params == NULL) {
831 return NULL;
832 }
833
834 p = pcm_param_to_alsa(param);
835 if (p < 0 || !param_is_mask(p)) {
836 return NULL;
837 }
838
Taylor Holberton2f387d22016-12-01 15:58:16 -0800839 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700840}
841
Taylor Holberton17a10242016-11-23 13:18:24 -0800842/** Get the minimum of a specified PCM parameter.
843 * @param pcm_params A PCM parameters structure.
844 * @param param The specified parameter to get the minimum of.
845 * @returns On success, the parameter minimum.
846 * On failure, zero.
847 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800848unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700849 enum pcm_param param)
850{
851 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
852 int p;
853
854 if (!params)
855 return 0;
856
857 p = pcm_param_to_alsa(param);
858 if (p < 0)
859 return 0;
860
861 return param_get_min(params, p);
862}
863
Taylor Holberton17a10242016-11-23 13:18:24 -0800864/** Get the maximum of a specified PCM parameter.
865 * @param pcm_params A PCM parameters structure.
866 * @param param The specified parameter to get the maximum of.
867 * @returns On success, the parameter maximum.
868 * On failure, zero.
869 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800870unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700871 enum pcm_param param)
872{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800873 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700874 int p;
875
876 if (!params)
877 return 0;
878
879 p = pcm_param_to_alsa(param);
880 if (p < 0)
881 return 0;
882
883 return param_get_max(params, p);
884}
885
dvdli9c9a4422020-10-28 16:05:19 +0800886static int pcm_mask_test(const struct pcm_mask *m, unsigned int index)
887{
888 const unsigned int bitshift = 5; /* for 32 bit integer */
889 const unsigned int bitmask = (1 << bitshift) - 1;
890 unsigned int element;
891
892 element = index >> bitshift;
893 if (element >= ARRAY_SIZE(m->bits))
894 return 0; /* for safety, but should never occur */
895 return (m->bits[element] >> (index & bitmask)) & 1;
896}
897
898static int pcm_mask_to_string(const struct pcm_mask *m, char *string, unsigned int size,
899 char *mask_name,
900 const char * const *bit_array_name, size_t bit_array_size)
901{
902 unsigned int i;
903 unsigned int offset = 0;
904
905 if (m == NULL)
906 return 0;
907 if (bit_array_size < 32) {
908 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
909 } else { /* spans two or more bitfields, print with an array index */
910 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
911 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
912 mask_name, i, m->bits[i]);
913 }
914 }
915 for (i = 0; i < bit_array_size; ++i) {
916 if (pcm_mask_test(m, i)) {
917 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
918 }
919 }
920 return offset;
921}
922
923int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
924{
925 const struct pcm_mask *m;
926 unsigned int min, max;
927 unsigned int clipoffset, offset;
928
929 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
930 offset = pcm_mask_to_string(m, string, size,
931 "Access", access_lookup, ARRAY_SIZE(access_lookup));
932 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
933 clipoffset = offset > size ? size : offset;
934 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
935 "Format", format_lookup, ARRAY_SIZE(format_lookup));
936 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
937 clipoffset = offset > size ? size : offset;
938 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
939 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
940 min = pcm_params_get_min(params, PCM_PARAM_RATE);
941 max = pcm_params_get_max(params, PCM_PARAM_RATE);
942 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
943 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
944 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
945 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
946 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
947 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
948 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
949 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
950 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
951 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
952 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
953 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
954 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
955 return offset;
956}
957
958int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
959{
960 unsigned int alsa_format = pcm_format_to_alsa(format);
961
962 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
963 return 0; /* caution: format not recognized is equivalent to S16_LE */
964 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
965}
966
Taylor Holberton6d58e012016-10-01 18:32:30 -0400967/** Closes a PCM returned by @ref pcm_open.
968 * @param pcm A PCM returned by @ref pcm_open.
969 * May not be NULL.
970 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800971 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400972 */
Simon Wilson79d39652011-05-25 13:44:23 -0700973int pcm_close(struct pcm *pcm)
974{
975 if (pcm == &bad_pcm)
976 return 0;
977
Eric Laurent40b018e2011-06-18 10:10:23 -0700978 pcm_hw_munmap_status(pcm);
979
Liam Girdwood6be28f12011-10-13 12:59:51 -0700980 if (pcm->flags & PCM_MMAP) {
981 pcm_stop(pcm);
Rohit kumarf29b8df2020-08-19 15:19:33 +0530982 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -0700983 }
984
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -0700985 snd_utils_close_dev_node(pcm->snd_node);
986 pcm->ops->close(pcm->data);
Simon Wilson79d39652011-05-25 13:44:23 -0700987 pcm->buffer_size = 0;
988 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700989 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700990 return 0;
991}
992
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800993/** Opens a PCM by it's name.
994 * @param name The name of the PCM.
995 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
996 * @param flags Specify characteristics and functionality about the pcm.
997 * May be a bitwise AND of the following:
998 * - @ref PCM_IN
999 * - @ref PCM_OUT
1000 * - @ref PCM_MMAP
1001 * - @ref PCM_NOIRQ
1002 * - @ref PCM_MONOTONIC
1003 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001004 * @returns A PCM structure.
dvdlif64fb392020-12-07 18:34:13 +08001005 * If an error occurs, the pointer of bad_pcm is returned.
1006 * Otherwise, it returns the pointer of PCM object.
1007 * Client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1008 * If @ref pcm_is_ready returns false, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001009 * @ingroup libtinyalsa-pcm
1010 */
1011struct pcm *pcm_open_by_name(const char *name,
1012 unsigned int flags,
1013 const struct pcm_config *config)
1014{
dvdlif64fb392020-12-07 18:34:13 +08001015 unsigned int card, device;
1016 if (name[0] != 'h' || name[1] != 'w' || name[2] != ':') {
1017 oops(&bad_pcm, 0, "name format is not matched");
1018 return &bad_pcm;
1019 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1020 oops(&bad_pcm, 0, "name format is not matched");
1021 return &bad_pcm;
1022 }
1023 return pcm_open(card, device, flags, config);
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001024}
1025
Taylor Holberton6d58e012016-10-01 18:32:30 -04001026/** Opens a PCM.
1027 * @param card The card that the pcm belongs to.
1028 * The default card is zero.
1029 * @param device The device that the pcm belongs to.
1030 * The default device is zero.
1031 * @param flags Specify characteristics and functionality about the pcm.
1032 * May be a bitwise AND of the following:
1033 * - @ref PCM_IN
1034 * - @ref PCM_OUT
1035 * - @ref PCM_MMAP
1036 * - @ref PCM_NOIRQ
1037 * - @ref PCM_MONOTONIC
1038 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001039 * @returns A PCM structure.
dvdlif64fb392020-12-07 18:34:13 +08001040 * If an error occurs, the pointer of bad_pcm is returned.
1041 * Otherwise, it returns the pointer of PCM object.
1042 * Client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1043 * If @ref pcm_is_ready returns false, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001044 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001045 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001046struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001047 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001048{
Simon Wilson79d39652011-05-25 13:44:23 -07001049 struct pcm *pcm;
1050 struct snd_pcm_info info;
Eric Laurent40b018e2011-06-18 10:10:23 -07001051 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001052
1053 pcm = calloc(1, sizeof(struct pcm));
dvdlif64fb392020-12-07 18:34:13 +08001054 if (!pcm) {
1055 oops(&bad_pcm, ENOMEM, "can't allocate PCM object");
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001056 return &bad_pcm;
dvdlif64fb392020-12-07 18:34:13 +08001057 }
Simon Wilson79d39652011-05-25 13:44:23 -07001058
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001059 /* Default to hw_ops, attemp plugin open only if hw (/dev/snd/pcm*) open fails */
1060 pcm->ops = &hw_ops;
1061 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, NULL);
Simon Wilson79d39652011-05-25 13:44:23 -07001062
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001063#ifdef TINYALSA_USES_PLUGINS
Simon Wilson79d39652011-05-25 13:44:23 -07001064 if (pcm->fd < 0) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001065 int pcm_type;
1066 pcm->snd_node = snd_utils_open_pcm(card, device);
1067 pcm_type = snd_utils_get_node_type(pcm->snd_node);
1068 if (!pcm->snd_node || pcm_type != SND_NODE_TYPE_PLUGIN) {
dvdlif64fb392020-12-07 18:34:13 +08001069 oops(&bad_pcm, ENODEV, "no device (hw/plugin) for card(%u), device(%u)",
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001070 card, device);
Miguel GAIO859adb22020-04-18 08:40:41 +02001071 goto fail_close_dev_node;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001072 }
1073 pcm->ops = &plug_ops;
1074 pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
1075 }
1076#endif
1077 if (pcm->fd < 0) {
dvdlif64fb392020-12-07 18:34:13 +08001078 oops(&bad_pcm, errno, "cannot open device (%u) for card (%u)",
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001079 device, card);
Miguel GAIO859adb22020-04-18 08:40:41 +02001080 goto fail_close_dev_node;
Simon Wilson79d39652011-05-25 13:44:23 -07001081 }
1082
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001083 pcm->flags = flags;
1084
1085 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
dvdlif64fb392020-12-07 18:34:13 +08001086 oops(&bad_pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001087 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001088 }
David Wagner4cddf192014-04-02 15:12:54 +02001089 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001090
Taylor Holberton861da7a2017-04-10 12:05:51 -07001091 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001092 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001093
Eric Laurent40b018e2011-06-18 10:10:23 -07001094 rc = pcm_hw_mmap_status(pcm);
1095 if (rc < 0) {
dvdlif64fb392020-12-07 18:34:13 +08001096 oops(&bad_pcm, errno, "mmap status failed");
Eric Laurent40b018e2011-06-18 10:10:23 -07001097 goto fail;
1098 }
1099
Glenn Kasten81012402013-08-22 15:11:48 -07001100#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1101 if (pcm->flags & PCM_MONOTONIC) {
1102 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001103 rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
Glenn Kasten81012402013-08-22 15:11:48 -07001104 if (rc < 0) {
dvdlif64fb392020-12-07 18:34:13 +08001105 oops(&bad_pcm, errno, "cannot set timestamp type");
Glenn Kasten81012402013-08-22 15:11:48 -07001106 goto fail;
1107 }
1108 }
1109#endif
1110
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001111 pcm->xruns = 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001112 return pcm;
1113
1114fail:
Ricardo Biehl Pasquali0bfac892019-01-04 14:45:35 -02001115 pcm_hw_munmap_status(pcm);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001116 if (flags & PCM_MMAP)
Rohit kumarf29b8df2020-08-19 15:19:33 +05301117 pcm->ops->munmap(pcm->data, pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
Liam Girdwood6be28f12011-10-13 12:59:51 -07001118fail_close:
Miguel GAIO859adb22020-04-18 08:40:41 +02001119 pcm->ops->close(pcm->data);
1120fail_close_dev_node:
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001121#ifdef TINYALSA_USES_PLUGINS
1122 if (pcm->snd_node)
1123 snd_utils_close_dev_node(pcm->snd_node);
1124#endif
Miguel GAIO859adb22020-04-18 08:40:41 +02001125 free(pcm);
Lubomir Rintel00f5aa12020-04-10 09:10:33 +02001126 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001127}
1128
Taylor Holberton6d58e012016-10-01 18:32:30 -04001129/** Checks if a PCM file has been opened without error.
1130 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001131 * May be NULL.
1132 * @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 -04001133 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001134 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001135 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001136int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001137{
Taylor Holbertone123a652017-01-13 21:39:48 -08001138 if (pcm != NULL) {
1139 return pcm->fd >= 0;
1140 }
1141 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001142}
Simon Wilsond6458e62011-06-21 14:58:11 -07001143
Taylor Holberton558e5942016-12-04 13:42:28 -08001144/** Links two PCMs.
1145 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1146 * If an error occurs, the error message will be written to @p pcm1.
1147 * @param pcm1 A PCM handle.
1148 * @param pcm2 Another PCM handle.
1149 * @return On success, zero; on failure, a negative number.
1150 * @ingroup libtinyalsa-pcm
1151 */
1152int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1153{
1154 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1155 if (err == -1) {
1156 return oops(pcm1, errno, "cannot link PCM");
1157 }
1158 return 0;
1159}
1160
1161/** Unlinks a PCM.
1162 * @see @ref pcm_link
1163 * @param pcm A PCM handle.
1164 * @return On success, zero; on failure, a negative number.
1165 * @ingroup libtinyalsa-pcm
1166 */
1167int pcm_unlink(struct pcm *pcm)
1168{
1169 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1170 if (err == -1) {
1171 return oops(pcm, errno, "cannot unlink PCM");
1172 }
1173 return 0;
1174}
1175
Taylor Holberton6d58e012016-10-01 18:32:30 -04001176/** Prepares a PCM, if it has not been prepared already.
1177 * @param pcm A PCM handle.
1178 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001179 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001180 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301181int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001182{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001183 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001184 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001185
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001186 /* get appl_ptr and avail_min from kernel */
1187 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1188
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301189 return 0;
1190}
1191
Taylor Holberton6d58e012016-10-01 18:32:30 -04001192/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001193 * @param pcm A PCM handle.
1194 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001195 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001196 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301197int pcm_start(struct pcm *pcm)
1198{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001199 /* set appl_ptr and avail_min in kernel */
Ricardo Biehl Pasqualib38b6a62019-01-07 11:54:47 -02001200 if (pcm_sync_ptr(pcm, 0) < 0)
1201 return -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001202
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001203 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001204 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001205 return oops(pcm, errno, "cannot start channel");
1206 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001207
1208 return 0;
1209}
1210
Taylor Holberton6d58e012016-10-01 18:32:30 -04001211/** Stops a PCM.
1212 * @param pcm A PCM handle.
1213 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001214 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001215 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001216int pcm_stop(struct pcm *pcm)
1217{
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001218 if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
Simon Wilsond6458e62011-06-21 14:58:11 -07001219 return oops(pcm, errno, "cannot stop channel");
1220
1221 return 0;
1222}
1223
Liam Girdwood6be28f12011-10-13 12:59:51 -07001224static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1225{
1226 int avail;
1227
1228 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1229
1230 if (avail < 0)
1231 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001232 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001233 avail -= pcm->boundary;
1234
1235 return avail;
1236}
1237
1238static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1239{
1240 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1241 if (avail < 0)
1242 avail += pcm->boundary;
1243 return avail;
1244}
1245
dvdlifaaa6972020-10-28 22:18:40 +08001246int pcm_mmap_avail(struct pcm *pcm)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001247{
dvdli565fc0e2020-12-09 15:45:04 +08001248 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001249 if (pcm->flags & PCM_IN)
1250 return pcm_mmap_capture_avail(pcm);
1251 else
1252 return pcm_mmap_playback_avail(pcm);
1253}
1254
1255static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1256{
1257 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1258 appl_ptr += frames;
1259
1260 /* check for boundary wrap */
1261 if (appl_ptr > pcm->boundary)
1262 appl_ptr -= pcm->boundary;
1263 pcm->mmap_control->appl_ptr = appl_ptr;
1264}
1265
1266int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1267 unsigned int *frames)
1268{
1269 unsigned int continuous, copy_frames, avail;
1270
1271 /* return the mmap buffer */
1272 *areas = pcm->mmap_buffer;
1273
1274 /* and the application offset in frames */
1275 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1276
1277 avail = pcm_mmap_avail(pcm);
1278 if (avail > pcm->buffer_size)
1279 avail = pcm->buffer_size;
1280 continuous = pcm->buffer_size - *offset;
1281
1282 /* we can only copy frames if the are availabale and continuos */
1283 copy_frames = *frames;
1284 if (copy_frames > avail)
1285 copy_frames = avail;
1286 if (copy_frames > continuous)
1287 copy_frames = continuous;
1288 *frames = copy_frames;
1289
1290 return 0;
1291}
1292
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001293static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1294 char *buf, unsigned int src_offset,
1295 unsigned int frames)
1296{
1297 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1298 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1299 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1300
1301 /* interleaved only atm */
1302 if (pcm->flags & PCM_IN)
1303 memcpy(buf + src_offset_bytes,
1304 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1305 size_bytes);
1306 else
1307 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1308 buf + src_offset_bytes,
1309 size_bytes);
1310 return 0;
1311}
1312
Liam Girdwood6be28f12011-10-13 12:59:51 -07001313int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1314{
Taylor Holberton72e44222016-11-22 09:54:47 -08001315 int ret;
1316
Taylor Holberton73466c02016-10-01 12:51:59 -04001317 /* not used */
1318 (void) offset;
1319
Liam Girdwood6be28f12011-10-13 12:59:51 -07001320 /* update the application pointer in userspace and kernel */
1321 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001322 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001323 if (ret != 0){
1324 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001325 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001326 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001327
1328 return frames;
1329}
1330
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001331static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1332 unsigned int offset, unsigned int size)
1333{
1334 void *pcm_areas;
1335 int commit;
1336 unsigned int pcm_offset, frames, count = 0;
1337
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001338 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001339 frames = size;
1340 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1341 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1342 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1343 if (commit < 0) {
1344 oops(pcm, commit, "failed to commit %d frames\n", frames);
1345 return commit;
1346 }
1347
1348 offset += commit;
1349 count += commit;
1350 size -= commit;
1351 }
1352 return count;
1353}
1354
dvdli72216212020-10-28 22:22:27 +08001355int pcm_get_poll_fd(struct pcm *pcm)
1356{
1357 return pcm->fd;
1358}
1359
Liam Girdwood6be28f12011-10-13 12:59:51 -07001360int pcm_avail_update(struct pcm *pcm)
1361{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001362 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001363 return pcm_mmap_avail(pcm);
1364}
1365
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001366/** Returns available frames in pcm buffer and corresponding time stamp.
1367 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1368 * otherwise the clock is CLOCK_REALTIME.
1369 * For an input stream, frames available are frames ready for the application to read.
1370 * 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 -02001371 * @param pcm A PCM handle.
1372 * @param avail The number of available frames
1373 * @param tstamp The timestamp
1374 * @return On success, zero is returned; on failure, negative one.
1375 */
1376int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1377 struct timespec *tstamp)
1378{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001379 int checking;
1380 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001381
1382 if (!pcm_is_ready(pcm))
1383 return -1;
1384
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001385 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001386
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001387again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001388
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001389 tmp = pcm_avail_update(pcm);
1390 if (tmp < 0)
1391 return tmp; /* error */
1392
1393 if (checking && (unsigned int) tmp == *avail)
1394 return 0;
1395
1396 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001397 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001398
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001399 /*
1400 * When status is mmaped, get avail again to ensure
1401 * valid timestamp.
1402 */
1403 if (!pcm->sync_ptr) {
1404 checking = 1;
1405 goto again;
1406 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001407
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001408 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001409 return 0;
1410}
1411
Liam Girdwood6be28f12011-10-13 12:59:51 -07001412int pcm_state(struct pcm *pcm)
1413{
1414 int err = pcm_sync_ptr(pcm, 0);
1415 if (err < 0)
1416 return err;
1417
1418 return pcm->mmap_status->state;
1419}
1420
Taylor Holberton17a10242016-11-23 13:18:24 -08001421/** Waits for frames to be available for read or write operations.
1422 * @param pcm A PCM handle.
1423 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1424 * @returns If frames became available, one is returned.
1425 * If a timeout occured, zero is returned.
1426 * If an error occured, a negative number is returned.
1427 * @ingroup libtinyalsa-pcm
1428 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001429int pcm_wait(struct pcm *pcm, int timeout)
1430{
1431 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001432 int err;
1433
1434 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001435 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001436
1437 do {
1438 /* let's wait for avail or timeout */
Rohit kumarf29b8df2020-08-19 15:19:33 +05301439 err = pcm->ops->poll(pcm->data, &pfd, 1, timeout);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001440 if (err < 0)
1441 return -errno;
1442
1443 /* timeout ? */
1444 if (err == 0)
1445 return 0;
1446
1447 /* have we been interrupted ? */
1448 if (errno == -EINTR)
1449 continue;
1450
1451 /* check for any errors */
1452 if (pfd.revents & (POLLERR | POLLNVAL)) {
1453 switch (pcm_state(pcm)) {
1454 case PCM_STATE_XRUN:
1455 return -EPIPE;
1456 case PCM_STATE_SUSPENDED:
1457 return -ESTRPIPE;
1458 case PCM_STATE_DISCONNECTED:
1459 return -ENODEV;
1460 default:
1461 return -EIO;
1462 }
1463 }
1464 /* poll again if fd not ready for IO */
1465 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1466
1467 return 1;
1468}
1469
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001470/*
1471 * Transfer data to/from mmaped buffer. This imitates the
1472 * behavior of read/write system calls.
1473 *
1474 * However, this doesn't seems to offer any advantage over
1475 * the read/write syscalls. Should it be removed?
1476 */
1477int pcm_mmap_transfer(struct pcm *pcm, void *buffer, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001478{
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001479 int is_playback;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001480
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001481 int state;
1482 unsigned int avail;
1483 unsigned int user_offset;
1484
1485 int err;
1486 int tmp;
1487
1488 is_playback = !(pcm->flags & PCM_IN);
1489
1490 if (frames == 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001491 return 0;
1492
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001493 /* update hardware pointer and get state */
1494 err = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC |
1495 SNDRV_PCM_SYNC_PTR_APPL |
1496 SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1497 if (err == -1)
1498 return -1;
1499 state = pcm->mmap_status->state;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001500
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001501 /*
1502 * If frames < start_threshold, wait indefinitely.
1503 * Another thread may start capture
1504 */
1505 if (!is_playback && state == PCM_STATE_PREPARED &&
1506 frames >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001507 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasqualic6c0d782019-03-12 16:31:04 -03001508 if (err == -1)
1509 return -1;
1510 /* state = PCM_STATE_RUNNING */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001511 }
1512
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001513 avail = pcm_mmap_avail(pcm);
1514 user_offset = 0;
1515
1516 while (frames) {
1517 if (!avail) {
1518 if (pcm->flags & PCM_NONBLOCK) {
1519 errno = EAGAIN;
1520 break;
1521 }
1522
1523 /* wait for interrupt */
1524 err = pcm_wait(pcm, -1);
1525 if (err < 0) {
1526 errno = -err;
1527 break;
1528 }
1529
1530 /* get hardware pointer */
1531 avail = pcm_avail_update(pcm);
1532 }
1533
1534 tmp = pcm_mmap_transfer_areas(pcm, buffer, user_offset, frames);
1535 if (tmp < 0)
1536 break;
1537
1538 user_offset += tmp;
1539 frames -= tmp;
1540 avail -= tmp;
1541
1542 /* start playback if written >= start_threshold */
1543 if (is_playback && state == PCM_STATE_PREPARED &&
1544 pcm->buffer_size - avail >= pcm->config.start_threshold) {
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001545 err = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START);
Ricardo Biehl Pasquali4ee09a92018-12-18 23:26:15 -02001546 if (err == -1)
1547 break;
1548 }
1549 }
1550
1551 return user_offset ? (int) user_offset : -1;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001552}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001553
1554int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1555{
1556 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
dvdli36949252021-01-28 15:03:17 +08001557 return -EINVAL;
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001558
dvdli565fc0e2020-12-09 15:45:04 +08001559 unsigned int frames = pcm_bytes_to_frames(pcm, count);
dvdli36949252021-01-28 15:03:17 +08001560 int res = pcm_writei(pcm, (void *) data, frames);
dvdli565fc0e2020-12-09 15:45:04 +08001561
1562 if (res < 0) {
1563 return res;
1564 }
1565
1566 return (unsigned int) res == frames ? 0 : -EIO;
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001567}
1568
1569int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1570{
1571 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
dvdli36949252021-01-28 15:03:17 +08001572 return -EINVAL;
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001573
dvdli565fc0e2020-12-09 15:45:04 +08001574 unsigned int frames = pcm_bytes_to_frames(pcm, count);
dvdli36949252021-01-28 15:03:17 +08001575 int res = pcm_readi(pcm, data, frames);
dvdli565fc0e2020-12-09 15:45:04 +08001576
1577 if (res < 0) {
1578 return res;
1579 }
1580
1581 return (unsigned int) res == frames ? 0 : -EIO;
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001582}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301583
dvdlie43e85c2020-10-28 22:56:53 +08001584/* Returns current read/write position in the mmap buffer with associated time stamp. */
1585int pcm_mmap_get_hw_ptr(struct pcm* pcm, unsigned int *hw_ptr, struct timespec *tstamp)
1586{
1587 int rc;
1588
1589 if (pcm == NULL || hw_ptr == NULL || tstamp == NULL)
1590 return oops(pcm, EINVAL, "pcm %p, hw_ptr %p, tstamp %p", pcm, hw_ptr, tstamp);
1591
1592 if (!pcm_is_ready(pcm))
1593 return oops(pcm, errno, "pcm_is_ready failed");
1594
1595 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1596 if (rc < 0)
1597 return oops(pcm, errno, "pcm_sync_ptr failed");
1598
dvdlidbb6ba62020-10-29 16:32:44 +08001599 if (pcm->mmap_status == NULL)
1600 return oops(pcm, EINVAL, "pcm %p, mmap_status is NULL", pcm);
1601
dvdlie43e85c2020-10-28 22:56:53 +08001602 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
1603 (pcm->mmap_status->state != PCM_STATE_DRAINING))
1604 return oops(pcm, ENOSYS, "invalid stream state %d", pcm->mmap_status->state);
1605
1606 *tstamp = pcm->mmap_status->tstamp;
1607 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
1608 return oops(pcm, errno, "invalid time stamp");
1609
1610 *hw_ptr = pcm->mmap_status->hw_ptr;
1611
1612 return 0;
1613}
1614
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001615static int pcm_rw_transfer(struct pcm *pcm, void *data, unsigned int frames)
1616{
1617 int is_playback;
1618
1619 struct snd_xferi transfer;
1620 int res;
1621
1622 is_playback = !(pcm->flags & PCM_IN);
1623
1624 transfer.buf = data;
1625 transfer.frames = frames;
1626 transfer.result = 0;
1627
Bhalchandra Gajare986b8e32019-09-04 15:32:35 -07001628 res = pcm->ops->ioctl(pcm->data, is_playback
1629 ? SNDRV_PCM_IOCTL_WRITEI_FRAMES
1630 : SNDRV_PCM_IOCTL_READI_FRAMES, &transfer);
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001631
1632 return res == 0 ? (int) transfer.result : -1;
1633}
1634
1635static int pcm_generic_transfer(struct pcm *pcm, void *data,
1636 unsigned int frames)
1637{
1638 int res;
1639
1640#if UINT_MAX > TINYALSA_FRAMES_MAX
1641 if (frames > TINYALSA_FRAMES_MAX)
1642 return -EINVAL;
1643#endif
1644 if (frames > INT_MAX)
1645 return -EINVAL;
1646
dvdlic0f92472021-01-28 11:53:30 +08001647 if (pcm_state(pcm) == PCM_STATE_SETUP && pcm_prepare(pcm) != 0) {
1648 return -1;
1649 }
1650
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001651again:
1652
1653 if (pcm->flags & PCM_MMAP)
1654 res = pcm_mmap_transfer(pcm, data, frames);
1655 else
1656 res = pcm_rw_transfer(pcm, data, frames);
1657
1658 if (res < 0) {
1659 switch (errno) {
1660 case EPIPE:
Ricardo Biehl Pasquali1804d152019-01-04 00:32:40 -02001661 pcm->xruns++;
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001662 /* fallthrough */
1663 case ESTRPIPE:
1664 /*
1665 * Try to restart if we are allowed to do so.
1666 * Otherwise, return error.
1667 */
1668 if (pcm->flags & PCM_NORESTART || pcm_prepare(pcm))
1669 return -1;
1670 goto again;
1671 case EAGAIN:
1672 if (pcm->flags & PCM_NONBLOCK)
1673 return -1;
1674 /* fallthrough */
1675 default:
1676 return oops(pcm, errno, "cannot read/write stream data");
1677 }
1678 }
1679
1680 return res;
1681}
1682
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001683/** Writes audio samples to PCM.
1684 * If the PCM has not been started, it is started in this function.
1685 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1686 * @param pcm A PCM handle.
1687 * @param data The audio sample array
1688 * @param frame_count The number of frames occupied by the sample array.
1689 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1690 * or INT_MAX.
1691 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1692 * @ingroup libtinyalsa-pcm
1693 */
1694int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
1695{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001696 if (pcm->flags & PCM_IN)
1697 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001698
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001699 return pcm_generic_transfer(pcm, (void*) data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001700}
1701
1702/** Reads audio samples from PCM.
1703 * If the PCM has not been started, it is started in this function.
1704 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1705 * @param pcm A PCM handle.
1706 * @param data The audio sample array
1707 * @param frame_count The number of frames occupied by the sample array.
1708 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
1709 * or INT_MAX.
1710 * @return On success, this function returns the number of frames written; otherwise, a negative number.
1711 * @ingroup libtinyalsa-pcm
1712 */
1713int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
1714{
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001715 if (!(pcm->flags & PCM_IN))
1716 return -EINVAL;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001717
Ricardo Biehl Pasquali03cc7c52019-01-04 00:20:14 -02001718 return pcm_generic_transfer(pcm, data, frame_count);
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001719}
1720
1721/** Writes audio samples to PCM.
1722 * If the PCM has not been started, it is started in this function.
1723 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
1724 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1725 * @param pcm A PCM handle.
1726 * @param data The audio sample array
1727 * @param count The number of bytes occupied by the sample array.
1728 * @return On success, this function returns zero; otherwise, a negative number.
1729 * @deprecated
1730 * @ingroup libtinyalsa-pcm
1731 */
1732int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
1733{
Kui Wang4655fed2020-11-01 23:27:53 +08001734 unsigned int requested_frames = pcm_bytes_to_frames(pcm, count);
1735 int ret = pcm_writei(pcm, data, requested_frames);
1736
1737 if (ret < 0)
1738 return ret;
1739
1740 return ((unsigned int )ret == requested_frames) ? 0 : -EIO;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001741}
1742
1743/** Reads audio samples from PCM.
1744 * If the PCM has not been started, it is started in this function.
1745 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
1746 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
1747 * @param pcm A PCM handle.
1748 * @param data The audio sample array
1749 * @param count The number of bytes occupied by the sample array.
1750 * @return On success, this function returns zero; otherwise, a negative number.
1751 * @deprecated
1752 * @ingroup libtinyalsa-pcm
1753 */
1754int pcm_read(struct pcm *pcm, void *data, unsigned int count)
1755{
Kui Wang4655fed2020-11-01 23:27:53 +08001756 unsigned int requested_frames = pcm_bytes_to_frames(pcm, count);
1757 int ret = pcm_readi(pcm, data, requested_frames);
1758
1759 if (ret < 0)
1760 return ret;
1761
1762 return ((unsigned int )ret == requested_frames) ? 0 : -EIO;
Ricardo Biehl Pasqualib8ea4c42018-12-24 18:34:48 -02001763}
1764
Taylor Holberton17a10242016-11-23 13:18:24 -08001765/** Gets the delay of the PCM, in terms of frames.
1766 * @param pcm A PCM handle.
1767 * @returns On success, the delay of the PCM.
1768 * On failure, a negative number.
1769 * @ingroup libtinyalsa-pcm
1770 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301771long pcm_get_delay(struct pcm *pcm)
1772{
1773 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1774 return -1;
1775
1776 return pcm->pcm_delay;
1777}
David Lic0d68b12020-12-02 07:46:19 +00001778
1779// TODO: Currently in Android, there are some libraries using this function to control the driver.
1780// We should remove this function as soon as possible.
1781int pcm_ioctl(struct pcm *pcm, int request, ...)
1782{
1783 va_list ap;
1784 void * arg;
1785
1786 if (!pcm_is_ready(pcm))
1787 return -1;
1788
1789 va_start(ap, request);
1790 arg = va_arg(ap, void *);
1791 va_end(ap);
1792
1793 // FIXME Does not handle plugins
1794 return ioctl(pcm->fd, request, arg);
1795}