blob: ef38c9cf6eba58bcb93aec2469d66ccc4bb399e3 [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 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800514 return -1;
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;
698 return 0;
699 }
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
821 fd = open(fn, O_RDWR);
822 if (fd < 0) {
823 fprintf(stderr, "cannot open device '%s'\n", fn);
824 goto err_open;
825 }
826
827 params = calloc(1, sizeof(struct snd_pcm_hw_params));
828 if (!params)
829 goto err_calloc;
830
831 param_init(params);
832 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
833 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
834 goto err_hw_refine;
835 }
836
837 close(fd);
838
839 return (struct pcm_params *)params;
840
841err_hw_refine:
842 free(params);
843err_calloc:
844 close(fd);
845err_open:
846 return NULL;
847}
848
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500849/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400850 * @param pcm_params Hardware parameters of a PCM.
851 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800852 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400853 */
Simon Wilson43544882012-10-31 12:52:39 -0700854void pcm_params_free(struct pcm_params *pcm_params)
855{
856 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
857
858 if (params)
859 free(params);
860}
861
862static int pcm_param_to_alsa(enum pcm_param param)
863{
864 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700865 case PCM_PARAM_ACCESS:
866 return SNDRV_PCM_HW_PARAM_ACCESS;
867 case PCM_PARAM_FORMAT:
868 return SNDRV_PCM_HW_PARAM_FORMAT;
869 case PCM_PARAM_SUBFORMAT:
870 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700871 case PCM_PARAM_SAMPLE_BITS:
872 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
873 break;
874 case PCM_PARAM_FRAME_BITS:
875 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
876 break;
877 case PCM_PARAM_CHANNELS:
878 return SNDRV_PCM_HW_PARAM_CHANNELS;
879 break;
880 case PCM_PARAM_RATE:
881 return SNDRV_PCM_HW_PARAM_RATE;
882 break;
883 case PCM_PARAM_PERIOD_TIME:
884 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
885 break;
886 case PCM_PARAM_PERIOD_SIZE:
887 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
888 break;
889 case PCM_PARAM_PERIOD_BYTES:
890 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
891 break;
892 case PCM_PARAM_PERIODS:
893 return SNDRV_PCM_HW_PARAM_PERIODS;
894 break;
895 case PCM_PARAM_BUFFER_TIME:
896 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
897 break;
898 case PCM_PARAM_BUFFER_SIZE:
899 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
900 break;
901 case PCM_PARAM_BUFFER_BYTES:
902 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
903 break;
904 case PCM_PARAM_TICK_TIME:
905 return SNDRV_PCM_HW_PARAM_TICK_TIME;
906 break;
907
908 default:
909 return -1;
910 }
911}
912
Taylor Holberton6d58e012016-10-01 18:32:30 -0400913/** Gets a mask from a PCM's hardware parameters.
914 * @param pcm_params A PCM's hardware parameters.
915 * @param param The parameter to get.
916 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
917 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800918 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400919 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800920const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700921 enum pcm_param param)
922{
923 int p;
924 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
925 if (params == NULL) {
926 return NULL;
927 }
928
929 p = pcm_param_to_alsa(param);
930 if (p < 0 || !param_is_mask(p)) {
931 return NULL;
932 }
933
Taylor Holberton2f387d22016-12-01 15:58:16 -0800934 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700935}
936
Taylor Holberton17a10242016-11-23 13:18:24 -0800937/** Get the minimum of a specified PCM parameter.
938 * @param pcm_params A PCM parameters structure.
939 * @param param The specified parameter to get the minimum of.
940 * @returns On success, the parameter minimum.
941 * On failure, zero.
942 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800943unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700944 enum pcm_param param)
945{
946 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
947 int p;
948
949 if (!params)
950 return 0;
951
952 p = pcm_param_to_alsa(param);
953 if (p < 0)
954 return 0;
955
956 return param_get_min(params, p);
957}
958
Taylor Holberton17a10242016-11-23 13:18:24 -0800959/** Get the maximum of a specified PCM parameter.
960 * @param pcm_params A PCM parameters structure.
961 * @param param The specified parameter to get the maximum of.
962 * @returns On success, the parameter maximum.
963 * On failure, zero.
964 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800965unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700966 enum pcm_param param)
967{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800968 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700969 int p;
970
971 if (!params)
972 return 0;
973
974 p = pcm_param_to_alsa(param);
975 if (p < 0)
976 return 0;
977
978 return param_get_max(params, p);
979}
980
Taylor Holberton6d58e012016-10-01 18:32:30 -0400981/** Closes a PCM returned by @ref pcm_open.
982 * @param pcm A PCM returned by @ref pcm_open.
983 * May not be NULL.
984 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800985 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400986 */
Simon Wilson79d39652011-05-25 13:44:23 -0700987int pcm_close(struct pcm *pcm)
988{
989 if (pcm == &bad_pcm)
990 return 0;
991
Eric Laurent40b018e2011-06-18 10:10:23 -0700992 pcm_hw_munmap_status(pcm);
993
Liam Girdwood6be28f12011-10-13 12:59:51 -0700994 if (pcm->flags & PCM_MMAP) {
995 pcm_stop(pcm);
996 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
997 }
998
Simon Wilson79d39652011-05-25 13:44:23 -0700999 if (pcm->fd >= 0)
1000 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301001 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001002 pcm->running = 0;
1003 pcm->buffer_size = 0;
1004 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -07001005 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -07001006 return 0;
1007}
1008
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001009/** Opens a PCM by it's name.
1010 * @param name The name of the PCM.
1011 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
1012 * @param flags Specify characteristics and functionality about the pcm.
1013 * May be a bitwise AND of the following:
1014 * - @ref PCM_IN
1015 * - @ref PCM_OUT
1016 * - @ref PCM_MMAP
1017 * - @ref PCM_NOIRQ
1018 * - @ref PCM_MONOTONIC
1019 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001020 * @returns A PCM structure.
1021 * If an error occurs allocating memory for the PCM, NULL is returned.
1022 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1023 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001024 * @ingroup libtinyalsa-pcm
1025 */
1026struct pcm *pcm_open_by_name(const char *name,
1027 unsigned int flags,
1028 const struct pcm_config *config)
1029{
1030 unsigned int card, device;
1031 if ((name[0] != 'h')
1032 || (name[1] != 'w')
1033 || (name[2] != ':')) {
1034 return NULL;
1035 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1036 return NULL;
1037 }
1038 return pcm_open(card, device, flags, config);
1039}
1040
Taylor Holberton6d58e012016-10-01 18:32:30 -04001041/** Opens a PCM.
1042 * @param card The card that the pcm belongs to.
1043 * The default card is zero.
1044 * @param device The device that the pcm belongs to.
1045 * The default device is zero.
1046 * @param flags Specify characteristics and functionality about the pcm.
1047 * May be a bitwise AND of the following:
1048 * - @ref PCM_IN
1049 * - @ref PCM_OUT
1050 * - @ref PCM_MMAP
1051 * - @ref PCM_NOIRQ
1052 * - @ref PCM_MONOTONIC
1053 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001054 * @returns A PCM structure.
1055 * If an error occurs allocating memory for the PCM, NULL is returned.
1056 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1057 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001058 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001059 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001060struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001061 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001062{
Simon Wilson79d39652011-05-25 13:44:23 -07001063 struct pcm *pcm;
1064 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001065 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -07001066 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001067
1068 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001069 if (!pcm)
1070 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001071
Simon Wilson1bd580f2011-06-02 15:58:41 -07001072 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
1073 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -07001074
1075 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001076 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -07001077 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001078 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001079 return pcm;
1080 }
1081
1082 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001083 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001084 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001085 }
David Wagner4cddf192014-04-02 15:12:54 +02001086 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001087
Taylor Holberton861da7a2017-04-10 12:05:51 -07001088 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001089 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001090
Eric Laurent40b018e2011-06-18 10:10:23 -07001091 rc = pcm_hw_mmap_status(pcm);
1092 if (rc < 0) {
1093 oops(pcm, rc, "mmap status failed");
1094 goto fail;
1095 }
1096
Glenn Kasten81012402013-08-22 15:11:48 -07001097#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1098 if (pcm->flags & PCM_MONOTONIC) {
1099 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1100 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1101 if (rc < 0) {
1102 oops(pcm, rc, "cannot set timestamp type");
1103 goto fail;
1104 }
1105 }
1106#endif
1107
Simon Wilson79d39652011-05-25 13:44:23 -07001108 pcm->underruns = 0;
1109 return pcm;
1110
1111fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001112 if (flags & PCM_MMAP)
1113 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1114fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001115 close(pcm->fd);
1116 pcm->fd = -1;
1117 return pcm;
1118}
1119
Taylor Holberton6d58e012016-10-01 18:32:30 -04001120/** Checks if a PCM file has been opened without error.
1121 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001122 * May be NULL.
1123 * @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 -04001124 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001125 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001126 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001127int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001128{
Taylor Holbertone123a652017-01-13 21:39:48 -08001129 if (pcm != NULL) {
1130 return pcm->fd >= 0;
1131 }
1132 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001133}
Simon Wilsond6458e62011-06-21 14:58:11 -07001134
Taylor Holberton558e5942016-12-04 13:42:28 -08001135/** Links two PCMs.
1136 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1137 * If an error occurs, the error message will be written to @p pcm1.
1138 * @param pcm1 A PCM handle.
1139 * @param pcm2 Another PCM handle.
1140 * @return On success, zero; on failure, a negative number.
1141 * @ingroup libtinyalsa-pcm
1142 */
1143int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1144{
1145 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1146 if (err == -1) {
1147 return oops(pcm1, errno, "cannot link PCM");
1148 }
1149 return 0;
1150}
1151
1152/** Unlinks a PCM.
1153 * @see @ref pcm_link
1154 * @param pcm A PCM handle.
1155 * @return On success, zero; on failure, a negative number.
1156 * @ingroup libtinyalsa-pcm
1157 */
1158int pcm_unlink(struct pcm *pcm)
1159{
1160 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1161 if (err == -1) {
1162 return oops(pcm, errno, "cannot unlink PCM");
1163 }
1164 return 0;
1165}
1166
Taylor Holberton6d58e012016-10-01 18:32:30 -04001167/** Prepares a PCM, if it has not been prepared already.
1168 * @param pcm A PCM handle.
1169 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001170 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001171 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301172int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001173{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301174 if (pcm->prepared)
1175 return 0;
1176
Simon Wilsond6458e62011-06-21 14:58:11 -07001177 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1178 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001179
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301180 pcm->prepared = 1;
1181 return 0;
1182}
1183
Taylor Holberton6d58e012016-10-01 18:32:30 -04001184/** Starts a PCM.
1185 * If the PCM has not been prepared,
1186 * it is prepared in this function.
1187 * @param pcm A PCM handle.
1188 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001189 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001190 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301191int pcm_start(struct pcm *pcm)
1192{
1193 int prepare_error = pcm_prepare(pcm);
1194 if (prepare_error)
1195 return prepare_error;
1196
Liam Girdwood6be28f12011-10-13 12:59:51 -07001197 if (pcm->flags & PCM_MMAP)
Taylor Holberton25976dc2017-04-10 11:46:40 -07001198 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001199
Simon Wilsond6458e62011-06-21 14:58:11 -07001200 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1201 return oops(pcm, errno, "cannot start channel");
1202
Liam Girdwood6be28f12011-10-13 12:59:51 -07001203 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001204 return 0;
1205}
1206
Taylor Holberton6d58e012016-10-01 18:32:30 -04001207/** Stops a PCM.
1208 * @param pcm A PCM handle.
1209 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001210 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001211 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001212int pcm_stop(struct pcm *pcm)
1213{
1214 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1215 return oops(pcm, errno, "cannot stop channel");
1216
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301217 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001218 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001219 return 0;
1220}
1221
Liam Girdwood6be28f12011-10-13 12:59:51 -07001222static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1223{
1224 int avail;
1225
1226 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1227
1228 if (avail < 0)
1229 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001230 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001231 avail -= pcm->boundary;
1232
1233 return avail;
1234}
1235
1236static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1237{
1238 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1239 if (avail < 0)
1240 avail += pcm->boundary;
1241 return avail;
1242}
1243
1244static inline int pcm_mmap_avail(struct pcm *pcm)
1245{
1246 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1247 if (pcm->flags & PCM_IN)
1248 return pcm_mmap_capture_avail(pcm);
1249 else
1250 return pcm_mmap_playback_avail(pcm);
1251}
1252
1253static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1254{
1255 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1256 appl_ptr += frames;
1257
1258 /* check for boundary wrap */
1259 if (appl_ptr > pcm->boundary)
1260 appl_ptr -= pcm->boundary;
1261 pcm->mmap_control->appl_ptr = appl_ptr;
1262}
1263
1264int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1265 unsigned int *frames)
1266{
1267 unsigned int continuous, copy_frames, avail;
1268
1269 /* return the mmap buffer */
1270 *areas = pcm->mmap_buffer;
1271
1272 /* and the application offset in frames */
1273 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1274
1275 avail = pcm_mmap_avail(pcm);
1276 if (avail > pcm->buffer_size)
1277 avail = pcm->buffer_size;
1278 continuous = pcm->buffer_size - *offset;
1279
1280 /* we can only copy frames if the are availabale and continuos */
1281 copy_frames = *frames;
1282 if (copy_frames > avail)
1283 copy_frames = avail;
1284 if (copy_frames > continuous)
1285 copy_frames = continuous;
1286 *frames = copy_frames;
1287
1288 return 0;
1289}
1290
1291int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1292{
Taylor Holberton72e44222016-11-22 09:54:47 -08001293 int ret;
1294
Taylor Holberton73466c02016-10-01 12:51:59 -04001295 /* not used */
1296 (void) offset;
1297
Liam Girdwood6be28f12011-10-13 12:59:51 -07001298 /* update the application pointer in userspace and kernel */
1299 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001300 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001301 if (ret != 0){
1302 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001303 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001304 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001305
1306 return frames;
1307}
1308
1309int pcm_avail_update(struct pcm *pcm)
1310{
1311 pcm_sync_ptr(pcm, 0);
1312 return pcm_mmap_avail(pcm);
1313}
1314
1315int pcm_state(struct pcm *pcm)
1316{
1317 int err = pcm_sync_ptr(pcm, 0);
1318 if (err < 0)
1319 return err;
1320
1321 return pcm->mmap_status->state;
1322}
1323
Taylor Holberton17a10242016-11-23 13:18:24 -08001324/** Waits for frames to be available for read or write operations.
1325 * @param pcm A PCM handle.
1326 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1327 * @returns If frames became available, one is returned.
1328 * If a timeout occured, zero is returned.
1329 * If an error occured, a negative number is returned.
1330 * @ingroup libtinyalsa-pcm
1331 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001332int pcm_wait(struct pcm *pcm, int timeout)
1333{
1334 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001335 int err;
1336
1337 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001338 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001339
1340 do {
1341 /* let's wait for avail or timeout */
1342 err = poll(&pfd, 1, timeout);
1343 if (err < 0)
1344 return -errno;
1345
1346 /* timeout ? */
1347 if (err == 0)
1348 return 0;
1349
1350 /* have we been interrupted ? */
1351 if (errno == -EINTR)
1352 continue;
1353
1354 /* check for any errors */
1355 if (pfd.revents & (POLLERR | POLLNVAL)) {
1356 switch (pcm_state(pcm)) {
1357 case PCM_STATE_XRUN:
1358 return -EPIPE;
1359 case PCM_STATE_SUSPENDED:
1360 return -ESTRPIPE;
1361 case PCM_STATE_DISCONNECTED:
1362 return -ENODEV;
1363 default:
1364 return -EIO;
1365 }
1366 }
1367 /* poll again if fd not ready for IO */
1368 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1369
1370 return 1;
1371}
1372
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001373int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001374{
1375 int err = 0, frames, avail;
1376 unsigned int offset = 0, count;
1377
1378 if (bytes == 0)
1379 return 0;
1380
1381 count = pcm_bytes_to_frames(pcm, bytes);
1382
1383 while (count > 0) {
1384
1385 /* get the available space for writing new frames */
1386 avail = pcm_avail_update(pcm);
1387 if (avail < 0) {
1388 fprintf(stderr, "cannot determine available mmap frames");
1389 return err;
1390 }
1391
1392 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001393 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001394 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1395 if (pcm_start(pcm) < 0) {
1396 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1397 (unsigned int)pcm->mmap_status->hw_ptr,
1398 (unsigned int)pcm->mmap_control->appl_ptr,
1399 avail);
1400 return -errno;
1401 }
1402 }
1403
1404 /* sleep until we have space to write new frames */
1405 if (pcm->running &&
1406 (unsigned int)avail < pcm->mmap_control->avail_min) {
1407 int time = -1;
1408
1409 if (pcm->flags & PCM_NOIRQ)
1410 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1411 / pcm->noirq_frames_per_msec;
1412
1413 err = pcm_wait(pcm, time);
1414 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301415 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001416 pcm->running = 0;
1417 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1418 (unsigned int)pcm->mmap_status->hw_ptr,
1419 (unsigned int)pcm->mmap_control->appl_ptr,
1420 avail);
1421 pcm->mmap_control->appl_ptr = 0;
1422 return err;
1423 }
1424 continue;
1425 }
1426
1427 frames = count;
1428 if (frames > avail)
1429 frames = avail;
1430
1431 if (!frames)
1432 break;
1433
1434 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001435 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001436 if (frames < 0) {
1437 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1438 (unsigned int)pcm->mmap_status->hw_ptr,
1439 (unsigned int)pcm->mmap_control->appl_ptr,
1440 avail);
1441 return frames;
1442 }
1443
1444 offset += frames;
1445 count -= frames;
1446 }
1447
Liam Girdwood6be28f12011-10-13 12:59:51 -07001448 return 0;
1449}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001450
1451int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1452{
1453 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1454 return -ENOSYS;
1455
1456 return pcm_mmap_transfer(pcm, (void *)data, count);
1457}
1458
1459int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1460{
1461 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1462 return -ENOSYS;
1463
1464 return pcm_mmap_transfer(pcm, data, count);
1465}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301466
Taylor Holberton17a10242016-11-23 13:18:24 -08001467/** Gets the delay of the PCM, in terms of frames.
1468 * @param pcm A PCM handle.
1469 * @returns On success, the delay of the PCM.
1470 * On failure, a negative number.
1471 * @ingroup libtinyalsa-pcm
1472 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301473long pcm_get_delay(struct pcm *pcm)
1474{
1475 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1476 return -1;
1477
1478 return pcm->pcm_delay;
1479}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001480