blob: f1d733f59dc64fa2d9cc4146d58511f2a82cee56 [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>
45#define __force
46#define __bitwise
47#define __user
48#include <sound/asound.h>
49
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030050#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070051#include <tinyalsa/limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070052
53#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070054#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070055
56static inline int param_is_mask(int p)
57{
58 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
59 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
60}
61
62static inline int param_is_interval(int p)
63{
64 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
65 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
66}
67
Taylor Holberton2f387d22016-12-01 15:58:16 -080068static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
69{
Taylor Holberton25976dc2017-04-10 11:46:40 -070070 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080071}
72
Simon Wilson79d39652011-05-25 13:44:23 -070073static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
74{
75 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
76}
77
78static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
79{
80 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
81}
82
83static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
84{
85 if (bit >= SNDRV_MASK_MAX)
86 return;
87 if (param_is_mask(n)) {
88 struct snd_mask *m = param_to_mask(p, n);
89 m->bits[0] = 0;
90 m->bits[1] = 0;
91 m->bits[bit >> 5] |= (1 << (bit & 31));
92 }
93}
94
95static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
96{
97 if (param_is_interval(n)) {
98 struct snd_interval *i = param_to_interval(p, n);
99 i->min = val;
100 }
101}
102
Taylor Holberton2f387d22016-12-01 15:58:16 -0800103static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700104{
105 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800106 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700107 return i->min;
108 }
109 return 0;
110}
111
Taylor Holberton2f387d22016-12-01 15:58:16 -0800112static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700113{
114 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800115 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700116 return i->max;
117 }
118 return 0;
119}
120
Simon Wilson79d39652011-05-25 13:44:23 -0700121static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
122{
123 if (param_is_interval(n)) {
124 struct snd_interval *i = param_to_interval(p, n);
125 i->min = val;
126 i->max = val;
127 i->integer = 1;
128 }
129}
130
Liam Girdwood6be28f12011-10-13 12:59:51 -0700131static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
132{
133 if (param_is_interval(n)) {
134 struct snd_interval *i = param_to_interval(p, n);
135 if (i->integer)
136 return i->max;
137 }
138 return 0;
139}
140
Simon Wilson79d39652011-05-25 13:44:23 -0700141static void param_init(struct snd_pcm_hw_params *p)
142{
143 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700144
Simon Wilson79d39652011-05-25 13:44:23 -0700145 memset(p, 0, sizeof(*p));
146 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
147 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
148 struct snd_mask *m = param_to_mask(p, n);
149 m->bits[0] = ~0;
150 m->bits[1] = ~0;
151 }
152 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
153 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
154 struct snd_interval *i = param_to_interval(p, n);
155 i->min = 0;
156 i->max = ~0;
157 }
Simon Wilson43544882012-10-31 12:52:39 -0700158 p->rmask = ~0U;
159 p->cmask = 0;
160 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700161}
162
Taylor Holberton861da7a2017-04-10 12:05:51 -0700163static unsigned int pcm_format_to_alsa(enum pcm_format format)
164{
165 switch (format) {
166
167 case PCM_FORMAT_S8:
168 return SNDRV_PCM_FORMAT_S8;
169
170 default:
171 case PCM_FORMAT_S16_LE:
172 return SNDRV_PCM_FORMAT_S16_LE;
173 case PCM_FORMAT_S16_BE:
174 return SNDRV_PCM_FORMAT_S16_BE;
175
176 case PCM_FORMAT_S24_LE:
177 return SNDRV_PCM_FORMAT_S24_LE;
178 case PCM_FORMAT_S24_BE:
179 return SNDRV_PCM_FORMAT_S24_BE;
180
181 case PCM_FORMAT_S24_3LE:
182 return SNDRV_PCM_FORMAT_S24_3LE;
183 case PCM_FORMAT_S24_3BE:
184 return SNDRV_PCM_FORMAT_S24_3BE;
185
186 case PCM_FORMAT_S32_LE:
187 return SNDRV_PCM_FORMAT_S32_LE;
188 case PCM_FORMAT_S32_BE:
189 return SNDRV_PCM_FORMAT_S32_BE;
190 };
191}
192
Simon Wilson79d39652011-05-25 13:44:23 -0700193#define PCM_ERROR_MAX 128
194
Taylor Holberton6d58e012016-10-01 18:32:30 -0400195/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800196 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400197 */
Simon Wilson79d39652011-05-25 13:44:23 -0700198struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400199 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700200 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400201 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700202 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400203 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700204 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400205 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530206 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400207 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700208 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400209 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700210 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800211 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700212 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400213 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700214 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400215 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700216 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700217 struct snd_pcm_mmap_status *mmap_status;
218 struct snd_pcm_mmap_control *mmap_control;
219 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700220 void *mmap_buffer;
221 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800222 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530223 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800224 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200225 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700226};
227
Taylor Holberton861da7a2017-04-10 12:05:51 -0700228static int oops(struct pcm *pcm, int e, const char *fmt, ...)
229{
230 va_list ap;
231 int sz;
232
233 va_start(ap, fmt);
234 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
235 va_end(ap);
236 sz = strlen(pcm->error);
237
238 if (errno)
239 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
240 ": %s", strerror(e));
241 return -1;
242}
243
Taylor Holberton6d58e012016-10-01 18:32:30 -0400244/** Gets the buffer size of the PCM.
245 * @param pcm A PCM handle.
246 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800247 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400248 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800249unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700250{
251 return pcm->buffer_size;
252}
253
Taylor Holberton77979a82016-12-01 20:04:04 -0800254/** Gets the channel count of the PCM.
255 * @param pcm A PCM handle.
256 * @return The channel count of the PCM.
257 * @ingroup libtinyalsa-pcm
258 */
259unsigned int pcm_get_channels(const struct pcm *pcm)
260{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700261 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800262}
263
Taylor Holberton08bb5902017-04-10 11:45:44 -0700264/** Gets the PCM configuration.
265 * @param pcm A PCM handle.
266 * @return The PCM configuration.
267 * This function only returns NULL if
268 * @p pcm is NULL.
269 * @ingroup libtinyalsa-pcm
270 * */
271const struct pcm_config * pcm_get_config(const struct pcm *pcm)
272{
273 if (pcm == NULL)
274 return NULL;
275 return &pcm->config;
276}
277
Taylor Holberton77979a82016-12-01 20:04:04 -0800278/** Gets the rate of the PCM.
279 * The rate is given in frames per second.
280 * @param pcm A PCM handle.
281 * @return The rate of the PCM.
282 * @ingroup libtinyalsa-pcm
283 */
284unsigned int pcm_get_rate(const struct pcm *pcm)
285{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700286 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800287}
288
289/** Gets the format of the PCM.
290 * @param pcm A PCM handle.
291 * @return The format of the PCM.
292 * @ingroup libtinyalsa-pcm
293 */
294enum pcm_format pcm_get_format(const struct pcm *pcm)
295{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700296 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800297}
298
Taylor Holberton6d58e012016-10-01 18:32:30 -0400299/** Gets the file descriptor of the PCM.
300 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800301 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400302 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800303 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400304 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800305int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400306{
307 return pcm->fd;
308}
309
Taylor Holberton6d58e012016-10-01 18:32:30 -0400310/** Gets the error message for the last error that occured.
311 * If no error occured and this function is called, the results are undefined.
312 * @param pcm A PCM handle.
313 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800314 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400315 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800316const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700317{
318 return pcm->error;
319}
320
Taylor Holberton861da7a2017-04-10 12:05:51 -0700321/** Sets the PCM configuration.
322 * @param pcm A PCM handle.
323 * @param config The configuration to use for the
324 * PCM. This parameter may be NULL, in which case
325 * the default configuration is used.
326 * @returns Zero on success, a negative errno value
327 * on failure.
328 * @ingroup libtinyalsa-pcm
329 * */
330int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
331{
332 if (pcm == NULL)
333 return -EFAULT;
334 else if (config == NULL) {
335 config = &pcm->config;
336 pcm->config.channels = 2;
337 pcm->config.rate = 48000;
338 pcm->config.period_size = 1024;
339 pcm->config.period_count = 4;
340 pcm->config.format = PCM_FORMAT_S16_LE;
341 pcm->config.start_threshold = config->period_count * config->period_size;
342 pcm->config.stop_threshold = config->period_count * config->period_size;
343 pcm->config.silence_threshold = 0;
344 } else
345 pcm->config = *config;
346
347 struct snd_pcm_hw_params params;
348 param_init(&params);
349 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
350 pcm_format_to_alsa(config->format));
351 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
352 SNDRV_PCM_SUBFORMAT_STD);
353 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
354 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
355 pcm_format_to_bits(config->format));
356 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
357 pcm_format_to_bits(config->format) * config->channels);
358 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
359 config->channels);
360 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
361 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
362
363 if (pcm->flags & PCM_NOIRQ) {
364
365 if (!(pcm->flags & PCM_MMAP)) {
366 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
367 return -EINVAL;
368 }
369
370 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
371 pcm->noirq_frames_per_msec = config->rate / 1000;
372 }
373
374 if (pcm->flags & PCM_MMAP)
375 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
376 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
377 else
378 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
379 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
380
381 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
382 int errno_copy = errno;
383 oops(pcm, -errno, "cannot set hw params");
384 return -errno_copy;
385 }
386
387 /* get our refined hw_params */
388 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
389 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
390 pcm->buffer_size = config->period_count * config->period_size;
391
392 if (pcm->flags & PCM_MMAP) {
393 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
394 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
395 if (pcm->mmap_buffer == MAP_FAILED) {
396 int errno_copy = errno;
397 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
398 pcm_frames_to_bytes(pcm, pcm->buffer_size));
399 return -errno_copy;
400 }
401 }
402
403 struct snd_pcm_sw_params sparams;
404 memset(&sparams, 0, sizeof(sparams));
405 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
406 sparams.period_step = 1;
407 sparams.avail_min = 1;
408
409 if (!config->start_threshold) {
410 if (pcm->flags & PCM_IN)
411 pcm->config.start_threshold = sparams.start_threshold = 1;
412 else
413 pcm->config.start_threshold = sparams.start_threshold =
414 config->period_count * config->period_size / 2;
415 } else
416 sparams.start_threshold = config->start_threshold;
417
418 /* pick a high stop threshold - todo: does this need further tuning */
419 if (!config->stop_threshold) {
420 if (pcm->flags & PCM_IN)
421 pcm->config.stop_threshold = sparams.stop_threshold =
422 config->period_count * config->period_size * 10;
423 else
424 pcm->config.stop_threshold = sparams.stop_threshold =
425 config->period_count * config->period_size;
426 }
427 else
428 sparams.stop_threshold = config->stop_threshold;
429
430 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
431 sparams.silence_size = 0;
432 sparams.silence_threshold = config->silence_threshold;
433 pcm->boundary = sparams.boundary = pcm->buffer_size;
434
435 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
436 pcm->boundary *= 2;
437
438 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
439 int errno_copy = errno;
440 oops(pcm, -errno, "cannot set sw params");
441 return -errno_copy;
442 }
443
444 return 0;
445}
446
Taylor Holberton6d58e012016-10-01 18:32:30 -0400447/** Gets the subdevice on which the pcm has been opened.
448 * @param pcm A PCM handle.
449 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800450unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200451{
452 return pcm->subdevice;
453}
454
Taylor Holberton6d58e012016-10-01 18:32:30 -0400455/** Determines the number of bits occupied by a @ref pcm_format.
456 * @param format A PCM format.
457 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800458 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400459 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700460unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700461{
462 switch (format) {
463 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400464 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700465 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400466 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700467 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400468 case PCM_FORMAT_S24_3LE:
469 case PCM_FORMAT_S24_3BE:
470 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700471 default:
472 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400473 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700474 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400475 case PCM_FORMAT_S8:
476 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700477 };
478}
479
Taylor Holberton6d58e012016-10-01 18:32:30 -0400480/** Determines how many frames of a PCM can fit into a number of bytes.
481 * @param pcm A PCM handle.
482 * @param bytes The number of bytes.
483 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800484 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400485 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800486unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700487{
488 return bytes / (pcm->config.channels *
489 (pcm_format_to_bits(pcm->config.format) >> 3));
490}
491
Taylor Holberton6d58e012016-10-01 18:32:30 -0400492/** Determines how many bytes are occupied by a number of frames of a PCM.
493 * @param pcm A PCM handle.
494 * @param frames The number of frames of a PCM.
495 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800496 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400497 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800498unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700499{
500 return frames * pcm->config.channels *
501 (pcm_format_to_bits(pcm->config.format) >> 3);
502}
503
Taylor Holberton4f556062016-09-16 09:54:36 -0400504static int pcm_sync_ptr(struct pcm *pcm, int flags)
505{
Eric Laurent40b018e2011-06-18 10:10:23 -0700506 if (pcm->sync_ptr) {
507 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800508 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
509 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700510 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800511 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800512 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700513 }
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200514 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700515}
516
Taylor Holberton4f556062016-09-16 09:54:36 -0400517static int pcm_hw_mmap_status(struct pcm *pcm)
518{
Eric Laurent40b018e2011-06-18 10:10:23 -0700519 if (pcm->sync_ptr)
520 return 0;
521
522 int page_size = sysconf(_SC_PAGE_SIZE);
523 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
524 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
525 if (pcm->mmap_status == MAP_FAILED)
526 pcm->mmap_status = NULL;
527 if (!pcm->mmap_status)
528 goto mmap_error;
529
530 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
531 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
532 if (pcm->mmap_control == MAP_FAILED)
533 pcm->mmap_control = NULL;
534 if (!pcm->mmap_control) {
535 munmap(pcm->mmap_status, page_size);
536 pcm->mmap_status = NULL;
537 goto mmap_error;
538 }
539 pcm->mmap_control->avail_min = 1;
540
541 return 0;
542
543mmap_error:
544
545 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
546 if (!pcm->sync_ptr)
547 return -ENOMEM;
548 pcm->mmap_status = &pcm->sync_ptr->s.status;
549 pcm->mmap_control = &pcm->sync_ptr->c.control;
550 pcm->mmap_control->avail_min = 1;
551 pcm_sync_ptr(pcm, 0);
552
553 return 0;
554}
555
556static void pcm_hw_munmap_status(struct pcm *pcm) {
557 if (pcm->sync_ptr) {
558 free(pcm->sync_ptr);
559 pcm->sync_ptr = NULL;
560 } else {
561 int page_size = sysconf(_SC_PAGE_SIZE);
562 if (pcm->mmap_status)
563 munmap(pcm->mmap_status, page_size);
564 if (pcm->mmap_control)
565 munmap(pcm->mmap_control, page_size);
566 }
567 pcm->mmap_status = NULL;
568 pcm->mmap_control = NULL;
569}
570
Liam Girdwood6be28f12011-10-13 12:59:51 -0700571static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700572 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700573 unsigned int frames)
574{
575 int size_bytes = pcm_frames_to_bytes(pcm, frames);
576 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
577 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
578
579 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700580 if (pcm->flags & PCM_IN)
581 memcpy(buf + src_offset_bytes,
582 (char*)pcm->mmap_buffer + pcm_offset_bytes,
583 size_bytes);
584 else
585 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
586 buf + src_offset_bytes,
587 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700588 return 0;
589}
590
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700591static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700592 unsigned int offset, unsigned int size)
593{
594 void *pcm_areas;
595 int commit;
596 unsigned int pcm_offset, frames, count = 0;
597
598 while (size > 0) {
599 frames = size;
600 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700601 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700602 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
603 if (commit < 0) {
604 oops(pcm, commit, "failed to commit %d frames\n", frames);
605 return commit;
606 }
607
608 offset += commit;
609 count += commit;
610 size -= commit;
611 }
612 return count;
613}
614
Taylor Holberton6d58e012016-10-01 18:32:30 -0400615/** Returns available frames in pcm buffer and corresponding time stamp.
616 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
617 * otherwise the clock is CLOCK_REALTIME.
618 * For an input stream, frames available are frames ready for the application to read.
619 * For an output stream, frames available are the number of empty frames available for the application to write.
620 * Only available for PCMs opened with the @ref PCM_MMAP flag.
621 * @param pcm A PCM handle.
622 * @param avail The number of available frames
623 * @param tstamp The timestamp
624 * @return On success, zero is returned; on failure, negative one.
625 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700626int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
627 struct timespec *tstamp)
628{
629 int frames;
630 int rc;
631 snd_pcm_uframes_t hw_ptr;
632
633 if (!pcm_is_ready(pcm))
634 return -1;
635
636 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
637 if (rc < 0)
638 return -1;
639
Eric Laurent7db48582011-11-17 11:47:59 -0800640 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
641 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800642 return -1;
643
Eric Laurent40b018e2011-06-18 10:10:23 -0700644 *tstamp = pcm->mmap_status->tstamp;
645 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
646 return -1;
647
648 hw_ptr = pcm->mmap_status->hw_ptr;
649 if (pcm->flags & PCM_IN)
650 frames = hw_ptr - pcm->mmap_control->appl_ptr;
651 else
652 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
653
654 if (frames < 0)
655 return -1;
656
657 *avail = (unsigned int)frames;
658
659 return 0;
660}
661
Taylor Holberton6d58e012016-10-01 18:32:30 -0400662/** Writes audio samples to PCM.
663 * If the PCM has not been started, it is started in this function.
664 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
665 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
666 * @param pcm A PCM handle.
667 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800668 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700669 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
670 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800671 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800672 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400673 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800674int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700675{
676 struct snd_xferi x;
677
678 if (pcm->flags & PCM_IN)
679 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700680#if UINT_MAX > TINYALSA_FRAMES_MAX
681 if (frame_count > TINYALSA_FRAMES_MAX)
682 return -EINVAL;
683#endif
684 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700685 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700686
Mark Brown6bbe77a2012-02-10 22:07:09 +0000687 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800688 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800689 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700690 for (;;) {
691 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530692 int prepare_error = pcm_prepare(pcm);
693 if (prepare_error)
694 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700695 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
696 return oops(pcm, errno, "cannot write initial data");
697 pcm->running = 1;
Miguel GAIOf6fe5232018-04-10 06:35:07 +0200698 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700699 }
700 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530701 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700702 pcm->running = 0;
703 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700704 /* we failed to make our window -- try to restart if we are
705 * allowed to do so. Otherwise, simply allow the EPIPE error to
706 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700707 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700708 if (pcm->flags & PCM_NORESTART)
709 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700710 continue;
711 }
712 return oops(pcm, errno, "cannot write stream data");
713 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800714 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700715 }
716}
717
Taylor Holberton6d58e012016-10-01 18:32:30 -0400718/** Reads audio samples from PCM.
719 * If the PCM has not been started, it is started in this function.
720 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
721 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
722 * @param pcm A PCM handle.
723 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800724 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700725 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
726 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800727 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800728 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400729 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800730int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700731{
732 struct snd_xferi x;
733
734 if (!(pcm->flags & PCM_IN))
735 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700736#if UINT_MAX > TINYALSA_FRAMES_MAX
737 if (frame_count > TINYALSA_FRAMES_MAX)
738 return -EINVAL;
739#endif
740 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700741 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700742
743 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800744 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800745 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700746 for (;;) {
Taylor Holberton1137fc72017-04-06 23:06:36 -0700747 if ((!pcm->running) && (pcm_start(pcm) < 0))
748 return -errno;
749 else if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530750 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700751 pcm->running = 0;
752 if (errno == EPIPE) {
753 /* we failed to make our window -- try to restart */
754 pcm->underruns++;
755 continue;
756 }
757 return oops(pcm, errno, "cannot read stream data");
758 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800759 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700760 }
761}
762
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800763/** Writes audio samples to PCM.
764 * If the PCM has not been started, it is started in this function.
765 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
766 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
767 * @param pcm A PCM handle.
768 * @param data The audio sample array
769 * @param count The number of bytes occupied by the sample array.
770 * @return On success, this function returns zero; otherwise, a negative number.
771 * @deprecated
772 * @ingroup libtinyalsa-pcm
773 */
774int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
775{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700776 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800777}
778
779/** Reads audio samples from PCM.
780 * If the PCM has not been started, it is started in this function.
781 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
782 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
783 * @param pcm A PCM handle.
784 * @param data The audio sample array
785 * @param count The number of bytes occupied by the sample array.
786 * @return On success, this function returns zero; otherwise, a negative number.
787 * @deprecated
788 * @ingroup libtinyalsa-pcm
789 */
790int pcm_read(struct pcm *pcm, void *data, unsigned int count)
791{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700792 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800793}
794
Simon Wilson79d39652011-05-25 13:44:23 -0700795static struct pcm bad_pcm = {
796 .fd = -1,
797};
798
Taylor Holberton6d58e012016-10-01 18:32:30 -0400799/** Gets the hardware parameters of a PCM, without created a PCM handle.
800 * @param card The card of the PCM.
801 * The default card is zero.
802 * @param device The device of the PCM.
803 * The default device is zero.
804 * @param flags Specifies whether the PCM is an input or output.
805 * May be one of the following:
806 * - @ref PCM_IN
807 * - @ref PCM_OUT
808 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800809 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400810 */
Simon Wilson43544882012-10-31 12:52:39 -0700811struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
812 unsigned int flags)
813{
814 struct snd_pcm_hw_params *params;
815 char fn[256];
816 int fd;
817
818 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
819 flags & PCM_IN ? 'c' : 'p');
820
Taylor Holberton093b8782017-10-12 20:28:30 -0400821 if (flags & PCM_NONBLOCK)
822 fd = open(fn, O_RDWR | O_NONBLOCK);
823 else
824 fd = open(fn, O_RDWR);
825
Simon Wilson43544882012-10-31 12:52:39 -0700826 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400827 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700828 goto err_open;
829 }
830
831 params = calloc(1, sizeof(struct snd_pcm_hw_params));
832 if (!params)
833 goto err_calloc;
834
835 param_init(params);
836 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
837 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
838 goto err_hw_refine;
839 }
840
841 close(fd);
842
843 return (struct pcm_params *)params;
844
845err_hw_refine:
846 free(params);
847err_calloc:
848 close(fd);
849err_open:
850 return NULL;
851}
852
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500853/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400854 * @param pcm_params Hardware parameters of a PCM.
855 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800856 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400857 */
Simon Wilson43544882012-10-31 12:52:39 -0700858void pcm_params_free(struct pcm_params *pcm_params)
859{
860 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
861
862 if (params)
863 free(params);
864}
865
866static int pcm_param_to_alsa(enum pcm_param param)
867{
868 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700869 case PCM_PARAM_ACCESS:
870 return SNDRV_PCM_HW_PARAM_ACCESS;
871 case PCM_PARAM_FORMAT:
872 return SNDRV_PCM_HW_PARAM_FORMAT;
873 case PCM_PARAM_SUBFORMAT:
874 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700875 case PCM_PARAM_SAMPLE_BITS:
876 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
877 break;
878 case PCM_PARAM_FRAME_BITS:
879 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
880 break;
881 case PCM_PARAM_CHANNELS:
882 return SNDRV_PCM_HW_PARAM_CHANNELS;
883 break;
884 case PCM_PARAM_RATE:
885 return SNDRV_PCM_HW_PARAM_RATE;
886 break;
887 case PCM_PARAM_PERIOD_TIME:
888 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
889 break;
890 case PCM_PARAM_PERIOD_SIZE:
891 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
892 break;
893 case PCM_PARAM_PERIOD_BYTES:
894 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
895 break;
896 case PCM_PARAM_PERIODS:
897 return SNDRV_PCM_HW_PARAM_PERIODS;
898 break;
899 case PCM_PARAM_BUFFER_TIME:
900 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
901 break;
902 case PCM_PARAM_BUFFER_SIZE:
903 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
904 break;
905 case PCM_PARAM_BUFFER_BYTES:
906 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
907 break;
908 case PCM_PARAM_TICK_TIME:
909 return SNDRV_PCM_HW_PARAM_TICK_TIME;
910 break;
911
912 default:
913 return -1;
914 }
915}
916
Taylor Holberton6d58e012016-10-01 18:32:30 -0400917/** Gets a mask from a PCM's hardware parameters.
918 * @param pcm_params A PCM's hardware parameters.
919 * @param param The parameter to get.
920 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
921 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800922 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400923 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800924const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700925 enum pcm_param param)
926{
927 int p;
928 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
929 if (params == NULL) {
930 return NULL;
931 }
932
933 p = pcm_param_to_alsa(param);
934 if (p < 0 || !param_is_mask(p)) {
935 return NULL;
936 }
937
Taylor Holberton2f387d22016-12-01 15:58:16 -0800938 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700939}
940
Taylor Holberton17a10242016-11-23 13:18:24 -0800941/** Get the minimum of a specified PCM parameter.
942 * @param pcm_params A PCM parameters structure.
943 * @param param The specified parameter to get the minimum of.
944 * @returns On success, the parameter minimum.
945 * On failure, zero.
946 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800947unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700948 enum pcm_param param)
949{
950 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
951 int p;
952
953 if (!params)
954 return 0;
955
956 p = pcm_param_to_alsa(param);
957 if (p < 0)
958 return 0;
959
960 return param_get_min(params, p);
961}
962
Taylor Holberton17a10242016-11-23 13:18:24 -0800963/** Get the maximum of a specified PCM parameter.
964 * @param pcm_params A PCM parameters structure.
965 * @param param The specified parameter to get the maximum of.
966 * @returns On success, the parameter maximum.
967 * On failure, zero.
968 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800969unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700970 enum pcm_param param)
971{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800972 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700973 int p;
974
975 if (!params)
976 return 0;
977
978 p = pcm_param_to_alsa(param);
979 if (p < 0)
980 return 0;
981
982 return param_get_max(params, p);
983}
984
Taylor Holberton6d58e012016-10-01 18:32:30 -0400985/** Closes a PCM returned by @ref pcm_open.
986 * @param pcm A PCM returned by @ref pcm_open.
987 * May not be NULL.
988 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800989 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400990 */
Simon Wilson79d39652011-05-25 13:44:23 -0700991int pcm_close(struct pcm *pcm)
992{
993 if (pcm == &bad_pcm)
994 return 0;
995
Eric Laurent40b018e2011-06-18 10:10:23 -0700996 pcm_hw_munmap_status(pcm);
997
Liam Girdwood6be28f12011-10-13 12:59:51 -0700998 if (pcm->flags & PCM_MMAP) {
999 pcm_stop(pcm);
1000 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1001 }
1002
Simon Wilson79d39652011-05-25 13:44:23 -07001003 if (pcm->fd >= 0)
1004 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301005 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001006 pcm->running = 0;
1007 pcm->buffer_size = 0;
1008 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -07001009 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -07001010 return 0;
1011}
1012
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001013/** Opens a PCM by it's name.
1014 * @param name The name of the PCM.
1015 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
1016 * @param flags Specify characteristics and functionality about the pcm.
1017 * May be a bitwise AND of the following:
1018 * - @ref PCM_IN
1019 * - @ref PCM_OUT
1020 * - @ref PCM_MMAP
1021 * - @ref PCM_NOIRQ
1022 * - @ref PCM_MONOTONIC
1023 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001024 * @returns A PCM structure.
1025 * If an error occurs allocating memory for the PCM, NULL is returned.
1026 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1027 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001028 * @ingroup libtinyalsa-pcm
1029 */
1030struct pcm *pcm_open_by_name(const char *name,
1031 unsigned int flags,
1032 const struct pcm_config *config)
1033{
1034 unsigned int card, device;
1035 if ((name[0] != 'h')
1036 || (name[1] != 'w')
1037 || (name[2] != ':')) {
1038 return NULL;
1039 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1040 return NULL;
1041 }
1042 return pcm_open(card, device, flags, config);
1043}
1044
Taylor Holberton6d58e012016-10-01 18:32:30 -04001045/** Opens a PCM.
1046 * @param card The card that the pcm belongs to.
1047 * The default card is zero.
1048 * @param device The device that the pcm belongs to.
1049 * The default device is zero.
1050 * @param flags Specify characteristics and functionality about the pcm.
1051 * May be a bitwise AND of the following:
1052 * - @ref PCM_IN
1053 * - @ref PCM_OUT
1054 * - @ref PCM_MMAP
1055 * - @ref PCM_NOIRQ
1056 * - @ref PCM_MONOTONIC
1057 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001058 * @returns A PCM structure.
1059 * If an error occurs allocating memory for the PCM, NULL is returned.
1060 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1061 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001062 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001063 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001064struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001065 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001066{
Simon Wilson79d39652011-05-25 13:44:23 -07001067 struct pcm *pcm;
1068 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001069 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -07001070 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001071
1072 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001073 if (!pcm)
1074 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001075
Simon Wilson1bd580f2011-06-02 15:58:41 -07001076 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
1077 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -07001078
1079 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -04001080
1081 if (flags & PCM_NONBLOCK)
1082 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
1083 else
1084 pcm->fd = open(fn, O_RDWR);
1085
Simon Wilson79d39652011-05-25 13:44:23 -07001086 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001087 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001088 return pcm;
1089 }
1090
1091 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001092 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001093 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001094 }
David Wagner4cddf192014-04-02 15:12:54 +02001095 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001096
Taylor Holberton861da7a2017-04-10 12:05:51 -07001097 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001098 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001099
Eric Laurent40b018e2011-06-18 10:10:23 -07001100 rc = pcm_hw_mmap_status(pcm);
1101 if (rc < 0) {
1102 oops(pcm, rc, "mmap status failed");
1103 goto fail;
1104 }
1105
Glenn Kasten81012402013-08-22 15:11:48 -07001106#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1107 if (pcm->flags & PCM_MONOTONIC) {
1108 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1109 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1110 if (rc < 0) {
1111 oops(pcm, rc, "cannot set timestamp type");
1112 goto fail;
1113 }
1114 }
1115#endif
1116
Simon Wilson79d39652011-05-25 13:44:23 -07001117 pcm->underruns = 0;
1118 return pcm;
1119
1120fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001121 if (flags & PCM_MMAP)
1122 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1123fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001124 close(pcm->fd);
1125 pcm->fd = -1;
1126 return pcm;
1127}
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{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301183 if (pcm->prepared)
1184 return 0;
1185
Simon Wilsond6458e62011-06-21 14:58:11 -07001186 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1187 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001188
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301189 pcm->prepared = 1;
1190 return 0;
1191}
1192
Taylor Holberton6d58e012016-10-01 18:32:30 -04001193/** Starts a PCM.
1194 * If the PCM has not been prepared,
1195 * it is prepared in this function.
1196 * @param pcm A PCM handle.
1197 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001198 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001199 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301200int pcm_start(struct pcm *pcm)
1201{
1202 int prepare_error = pcm_prepare(pcm);
1203 if (prepare_error)
1204 return prepare_error;
1205
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001206 /* if pcm is linked, it may be already started by other pcm */
1207 /* check pcm state is not in running state */
1208 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001209
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001210 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
1211 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1212 return oops(pcm, errno, "cannot start channel");
1213 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001214
Liam Girdwood6be28f12011-10-13 12:59:51 -07001215 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001216 return 0;
1217}
1218
Taylor Holberton6d58e012016-10-01 18:32:30 -04001219/** Stops a PCM.
1220 * @param pcm A PCM handle.
1221 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001222 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001223 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001224int pcm_stop(struct pcm *pcm)
1225{
1226 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1227 return oops(pcm, errno, "cannot stop channel");
1228
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301229 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001230 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001231 return 0;
1232}
1233
Liam Girdwood6be28f12011-10-13 12:59:51 -07001234static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1235{
1236 int avail;
1237
1238 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1239
1240 if (avail < 0)
1241 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001242 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001243 avail -= pcm->boundary;
1244
1245 return avail;
1246}
1247
1248static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1249{
1250 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1251 if (avail < 0)
1252 avail += pcm->boundary;
1253 return avail;
1254}
1255
1256static inline int pcm_mmap_avail(struct pcm *pcm)
1257{
1258 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1259 if (pcm->flags & PCM_IN)
1260 return pcm_mmap_capture_avail(pcm);
1261 else
1262 return pcm_mmap_playback_avail(pcm);
1263}
1264
1265static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1266{
1267 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1268 appl_ptr += frames;
1269
1270 /* check for boundary wrap */
1271 if (appl_ptr > pcm->boundary)
1272 appl_ptr -= pcm->boundary;
1273 pcm->mmap_control->appl_ptr = appl_ptr;
1274}
1275
1276int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1277 unsigned int *frames)
1278{
1279 unsigned int continuous, copy_frames, avail;
1280
1281 /* return the mmap buffer */
1282 *areas = pcm->mmap_buffer;
1283
1284 /* and the application offset in frames */
1285 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1286
1287 avail = pcm_mmap_avail(pcm);
1288 if (avail > pcm->buffer_size)
1289 avail = pcm->buffer_size;
1290 continuous = pcm->buffer_size - *offset;
1291
1292 /* we can only copy frames if the are availabale and continuos */
1293 copy_frames = *frames;
1294 if (copy_frames > avail)
1295 copy_frames = avail;
1296 if (copy_frames > continuous)
1297 copy_frames = continuous;
1298 *frames = copy_frames;
1299
1300 return 0;
1301}
1302
1303int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1304{
Taylor Holberton72e44222016-11-22 09:54:47 -08001305 int ret;
1306
Taylor Holberton73466c02016-10-01 12:51:59 -04001307 /* not used */
1308 (void) offset;
1309
Liam Girdwood6be28f12011-10-13 12:59:51 -07001310 /* update the application pointer in userspace and kernel */
1311 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001312 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001313 if (ret != 0){
1314 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001315 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001316 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001317
1318 return frames;
1319}
1320
1321int pcm_avail_update(struct pcm *pcm)
1322{
1323 pcm_sync_ptr(pcm, 0);
1324 return pcm_mmap_avail(pcm);
1325}
1326
1327int pcm_state(struct pcm *pcm)
1328{
1329 int err = pcm_sync_ptr(pcm, 0);
1330 if (err < 0)
1331 return err;
1332
1333 return pcm->mmap_status->state;
1334}
1335
Taylor Holberton17a10242016-11-23 13:18:24 -08001336/** Waits for frames to be available for read or write operations.
1337 * @param pcm A PCM handle.
1338 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1339 * @returns If frames became available, one is returned.
1340 * If a timeout occured, zero is returned.
1341 * If an error occured, a negative number is returned.
1342 * @ingroup libtinyalsa-pcm
1343 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001344int pcm_wait(struct pcm *pcm, int timeout)
1345{
1346 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001347 int err;
1348
1349 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001350 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001351
1352 do {
1353 /* let's wait for avail or timeout */
1354 err = poll(&pfd, 1, timeout);
1355 if (err < 0)
1356 return -errno;
1357
1358 /* timeout ? */
1359 if (err == 0)
1360 return 0;
1361
1362 /* have we been interrupted ? */
1363 if (errno == -EINTR)
1364 continue;
1365
1366 /* check for any errors */
1367 if (pfd.revents & (POLLERR | POLLNVAL)) {
1368 switch (pcm_state(pcm)) {
1369 case PCM_STATE_XRUN:
1370 return -EPIPE;
1371 case PCM_STATE_SUSPENDED:
1372 return -ESTRPIPE;
1373 case PCM_STATE_DISCONNECTED:
1374 return -ENODEV;
1375 default:
1376 return -EIO;
1377 }
1378 }
1379 /* poll again if fd not ready for IO */
1380 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1381
1382 return 1;
1383}
1384
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001385int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001386{
1387 int err = 0, frames, avail;
1388 unsigned int offset = 0, count;
1389
1390 if (bytes == 0)
1391 return 0;
1392
1393 count = pcm_bytes_to_frames(pcm, bytes);
1394
1395 while (count > 0) {
1396
1397 /* get the available space for writing new frames */
1398 avail = pcm_avail_update(pcm);
1399 if (avail < 0) {
1400 fprintf(stderr, "cannot determine available mmap frames");
1401 return err;
1402 }
1403
1404 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001405 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001406 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1407 if (pcm_start(pcm) < 0) {
1408 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1409 (unsigned int)pcm->mmap_status->hw_ptr,
1410 (unsigned int)pcm->mmap_control->appl_ptr,
1411 avail);
1412 return -errno;
1413 }
1414 }
1415
1416 /* sleep until we have space to write new frames */
1417 if (pcm->running &&
1418 (unsigned int)avail < pcm->mmap_control->avail_min) {
1419 int time = -1;
1420
1421 if (pcm->flags & PCM_NOIRQ)
1422 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1423 / pcm->noirq_frames_per_msec;
1424
1425 err = pcm_wait(pcm, time);
1426 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301427 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001428 pcm->running = 0;
1429 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1430 (unsigned int)pcm->mmap_status->hw_ptr,
1431 (unsigned int)pcm->mmap_control->appl_ptr,
1432 avail);
1433 pcm->mmap_control->appl_ptr = 0;
1434 return err;
1435 }
1436 continue;
1437 }
1438
1439 frames = count;
1440 if (frames > avail)
1441 frames = avail;
1442
1443 if (!frames)
1444 break;
1445
1446 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001447 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001448 if (frames < 0) {
1449 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1450 (unsigned int)pcm->mmap_status->hw_ptr,
1451 (unsigned int)pcm->mmap_control->appl_ptr,
1452 avail);
1453 return frames;
1454 }
1455
1456 offset += frames;
1457 count -= frames;
1458 }
1459
Liam Girdwood6be28f12011-10-13 12:59:51 -07001460 return 0;
1461}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001462
1463int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1464{
1465 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1466 return -ENOSYS;
1467
1468 return pcm_mmap_transfer(pcm, (void *)data, count);
1469}
1470
1471int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1472{
1473 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1474 return -ENOSYS;
1475
1476 return pcm_mmap_transfer(pcm, data, count);
1477}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301478
Taylor Holberton17a10242016-11-23 13:18:24 -08001479/** Gets the delay of the PCM, in terms of frames.
1480 * @param pcm A PCM handle.
1481 * @returns On success, the delay of the PCM.
1482 * On failure, a negative number.
1483 * @ingroup libtinyalsa-pcm
1484 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301485long pcm_get_delay(struct pcm *pcm)
1486{
1487 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1488 return -1;
1489
1490 return pcm->pcm_delay;
1491}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001492