blob: 24f4f2b46105f0946b6fb289dfa87a32cee9c7fc [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 Holberton851dd802017-04-06 23:12:01 -0700680 else if ((frame_count > TINYALSA_FRAMES_MAX)
681 || (frame_count > INT_MAX))
682 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700683
Mark Brown6bbe77a2012-02-10 22:07:09 +0000684 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800685 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800686 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700687 for (;;) {
688 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530689 int prepare_error = pcm_prepare(pcm);
690 if (prepare_error)
691 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700692 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
693 return oops(pcm, errno, "cannot write initial data");
694 pcm->running = 1;
695 return 0;
696 }
697 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530698 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700699 pcm->running = 0;
700 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700701 /* we failed to make our window -- try to restart if we are
702 * allowed to do so. Otherwise, simply allow the EPIPE error to
703 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700704 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700705 if (pcm->flags & PCM_NORESTART)
706 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700707 continue;
708 }
709 return oops(pcm, errno, "cannot write stream data");
710 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800711 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700712 }
713}
714
Taylor Holberton6d58e012016-10-01 18:32:30 -0400715/** Reads audio samples from PCM.
716 * If the PCM has not been started, it is started in this function.
717 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
718 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
719 * @param pcm A PCM handle.
720 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800721 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700722 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
723 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800724 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800725 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400726 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800727int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700728{
729 struct snd_xferi x;
730
731 if (!(pcm->flags & PCM_IN))
732 return -EINVAL;
Taylor Holberton851dd802017-04-06 23:12:01 -0700733 else if ((frame_count > TINYALSA_FRAMES_MAX)
734 || (frame_count > INT_MAX))
735 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700736
737 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800738 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800739 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700740 for (;;) {
Taylor Holberton1137fc72017-04-06 23:06:36 -0700741 if ((!pcm->running) && (pcm_start(pcm) < 0))
742 return -errno;
743 else if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530744 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700745 pcm->running = 0;
746 if (errno == EPIPE) {
747 /* we failed to make our window -- try to restart */
748 pcm->underruns++;
749 continue;
750 }
751 return oops(pcm, errno, "cannot read stream data");
752 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800753 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700754 }
755}
756
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800757/** Writes audio samples to PCM.
758 * If the PCM has not been started, it is started in this function.
759 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
760 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
761 * @param pcm A PCM handle.
762 * @param data The audio sample array
763 * @param count The number of bytes occupied by the sample array.
764 * @return On success, this function returns zero; otherwise, a negative number.
765 * @deprecated
766 * @ingroup libtinyalsa-pcm
767 */
768int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
769{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700770 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800771}
772
773/** Reads audio samples from PCM.
774 * If the PCM has not been started, it is started in this function.
775 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
776 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
777 * @param pcm A PCM handle.
778 * @param data The audio sample array
779 * @param count The number of bytes occupied by the sample array.
780 * @return On success, this function returns zero; otherwise, a negative number.
781 * @deprecated
782 * @ingroup libtinyalsa-pcm
783 */
784int pcm_read(struct pcm *pcm, void *data, unsigned int count)
785{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700786 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800787}
788
Simon Wilson79d39652011-05-25 13:44:23 -0700789static struct pcm bad_pcm = {
790 .fd = -1,
791};
792
Taylor Holberton6d58e012016-10-01 18:32:30 -0400793/** Gets the hardware parameters of a PCM, without created a PCM handle.
794 * @param card The card of the PCM.
795 * The default card is zero.
796 * @param device The device of the PCM.
797 * The default device is zero.
798 * @param flags Specifies whether the PCM is an input or output.
799 * May be one of the following:
800 * - @ref PCM_IN
801 * - @ref PCM_OUT
802 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800803 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400804 */
Simon Wilson43544882012-10-31 12:52:39 -0700805struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
806 unsigned int flags)
807{
808 struct snd_pcm_hw_params *params;
809 char fn[256];
810 int fd;
811
812 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
813 flags & PCM_IN ? 'c' : 'p');
814
815 fd = open(fn, O_RDWR);
816 if (fd < 0) {
817 fprintf(stderr, "cannot open device '%s'\n", fn);
818 goto err_open;
819 }
820
821 params = calloc(1, sizeof(struct snd_pcm_hw_params));
822 if (!params)
823 goto err_calloc;
824
825 param_init(params);
826 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
827 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
828 goto err_hw_refine;
829 }
830
831 close(fd);
832
833 return (struct pcm_params *)params;
834
835err_hw_refine:
836 free(params);
837err_calloc:
838 close(fd);
839err_open:
840 return NULL;
841}
842
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500843/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400844 * @param pcm_params Hardware parameters of a PCM.
845 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800846 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400847 */
Simon Wilson43544882012-10-31 12:52:39 -0700848void pcm_params_free(struct pcm_params *pcm_params)
849{
850 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
851
852 if (params)
853 free(params);
854}
855
856static int pcm_param_to_alsa(enum pcm_param param)
857{
858 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700859 case PCM_PARAM_ACCESS:
860 return SNDRV_PCM_HW_PARAM_ACCESS;
861 case PCM_PARAM_FORMAT:
862 return SNDRV_PCM_HW_PARAM_FORMAT;
863 case PCM_PARAM_SUBFORMAT:
864 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700865 case PCM_PARAM_SAMPLE_BITS:
866 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
867 break;
868 case PCM_PARAM_FRAME_BITS:
869 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
870 break;
871 case PCM_PARAM_CHANNELS:
872 return SNDRV_PCM_HW_PARAM_CHANNELS;
873 break;
874 case PCM_PARAM_RATE:
875 return SNDRV_PCM_HW_PARAM_RATE;
876 break;
877 case PCM_PARAM_PERIOD_TIME:
878 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
879 break;
880 case PCM_PARAM_PERIOD_SIZE:
881 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
882 break;
883 case PCM_PARAM_PERIOD_BYTES:
884 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
885 break;
886 case PCM_PARAM_PERIODS:
887 return SNDRV_PCM_HW_PARAM_PERIODS;
888 break;
889 case PCM_PARAM_BUFFER_TIME:
890 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
891 break;
892 case PCM_PARAM_BUFFER_SIZE:
893 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
894 break;
895 case PCM_PARAM_BUFFER_BYTES:
896 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
897 break;
898 case PCM_PARAM_TICK_TIME:
899 return SNDRV_PCM_HW_PARAM_TICK_TIME;
900 break;
901
902 default:
903 return -1;
904 }
905}
906
Taylor Holberton6d58e012016-10-01 18:32:30 -0400907/** Gets a mask from a PCM's hardware parameters.
908 * @param pcm_params A PCM's hardware parameters.
909 * @param param The parameter to get.
910 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
911 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800912 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400913 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800914const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700915 enum pcm_param param)
916{
917 int p;
918 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
919 if (params == NULL) {
920 return NULL;
921 }
922
923 p = pcm_param_to_alsa(param);
924 if (p < 0 || !param_is_mask(p)) {
925 return NULL;
926 }
927
Taylor Holberton2f387d22016-12-01 15:58:16 -0800928 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700929}
930
Taylor Holberton17a10242016-11-23 13:18:24 -0800931/** Get the minimum of a specified PCM parameter.
932 * @param pcm_params A PCM parameters structure.
933 * @param param The specified parameter to get the minimum of.
934 * @returns On success, the parameter minimum.
935 * On failure, zero.
936 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800937unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700938 enum pcm_param param)
939{
940 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
941 int p;
942
943 if (!params)
944 return 0;
945
946 p = pcm_param_to_alsa(param);
947 if (p < 0)
948 return 0;
949
950 return param_get_min(params, p);
951}
952
Taylor Holberton17a10242016-11-23 13:18:24 -0800953/** Get the maximum of a specified PCM parameter.
954 * @param pcm_params A PCM parameters structure.
955 * @param param The specified parameter to get the maximum of.
956 * @returns On success, the parameter maximum.
957 * On failure, zero.
958 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800959unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700960 enum pcm_param param)
961{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800962 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700963 int p;
964
965 if (!params)
966 return 0;
967
968 p = pcm_param_to_alsa(param);
969 if (p < 0)
970 return 0;
971
972 return param_get_max(params, p);
973}
974
Taylor Holberton6d58e012016-10-01 18:32:30 -0400975/** Closes a PCM returned by @ref pcm_open.
976 * @param pcm A PCM returned by @ref pcm_open.
977 * May not be NULL.
978 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800979 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400980 */
Simon Wilson79d39652011-05-25 13:44:23 -0700981int pcm_close(struct pcm *pcm)
982{
983 if (pcm == &bad_pcm)
984 return 0;
985
Eric Laurent40b018e2011-06-18 10:10:23 -0700986 pcm_hw_munmap_status(pcm);
987
Liam Girdwood6be28f12011-10-13 12:59:51 -0700988 if (pcm->flags & PCM_MMAP) {
989 pcm_stop(pcm);
990 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
991 }
992
Simon Wilson79d39652011-05-25 13:44:23 -0700993 if (pcm->fd >= 0)
994 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530995 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700996 pcm->running = 0;
997 pcm->buffer_size = 0;
998 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700999 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -07001000 return 0;
1001}
1002
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001003/** Opens a PCM by it's name.
1004 * @param name The name of the PCM.
1005 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
1006 * @param flags Specify characteristics and functionality about the pcm.
1007 * May be a bitwise AND of the following:
1008 * - @ref PCM_IN
1009 * - @ref PCM_OUT
1010 * - @ref PCM_MMAP
1011 * - @ref PCM_NOIRQ
1012 * - @ref PCM_MONOTONIC
1013 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001014 * @returns A PCM structure.
1015 * If an error occurs allocating memory for the PCM, NULL is returned.
1016 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1017 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -08001018 * @ingroup libtinyalsa-pcm
1019 */
1020struct pcm *pcm_open_by_name(const char *name,
1021 unsigned int flags,
1022 const struct pcm_config *config)
1023{
1024 unsigned int card, device;
1025 if ((name[0] != 'h')
1026 || (name[1] != 'w')
1027 || (name[2] != ':')) {
1028 return NULL;
1029 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
1030 return NULL;
1031 }
1032 return pcm_open(card, device, flags, config);
1033}
1034
Taylor Holberton6d58e012016-10-01 18:32:30 -04001035/** Opens a PCM.
1036 * @param card The card that the pcm belongs to.
1037 * The default card is zero.
1038 * @param device The device that the pcm belongs to.
1039 * The default device is zero.
1040 * @param flags Specify characteristics and functionality about the pcm.
1041 * May be a bitwise AND of the following:
1042 * - @ref PCM_IN
1043 * - @ref PCM_OUT
1044 * - @ref PCM_MMAP
1045 * - @ref PCM_NOIRQ
1046 * - @ref PCM_MONOTONIC
1047 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -08001048 * @returns A PCM structure.
1049 * If an error occurs allocating memory for the PCM, NULL is returned.
1050 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
1051 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001052 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001053 */
Simon Wilson1bd580f2011-06-02 15:58:41 -07001054struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -08001055 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -07001056{
Simon Wilson79d39652011-05-25 13:44:23 -07001057 struct pcm *pcm;
1058 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001059 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -07001060 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -07001061
1062 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -04001063 if (!pcm)
1064 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -07001065
Simon Wilson1bd580f2011-06-02 15:58:41 -07001066 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
1067 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -07001068
1069 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -07001070 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -07001071 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001072 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001073 return pcm;
1074 }
1075
1076 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001077 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001078 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001079 }
David Wagner4cddf192014-04-02 15:12:54 +02001080 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001081
Taylor Holberton861da7a2017-04-10 12:05:51 -07001082 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001083 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001084
Eric Laurent40b018e2011-06-18 10:10:23 -07001085 rc = pcm_hw_mmap_status(pcm);
1086 if (rc < 0) {
1087 oops(pcm, rc, "mmap status failed");
1088 goto fail;
1089 }
1090
Glenn Kasten81012402013-08-22 15:11:48 -07001091#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1092 if (pcm->flags & PCM_MONOTONIC) {
1093 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1094 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1095 if (rc < 0) {
1096 oops(pcm, rc, "cannot set timestamp type");
1097 goto fail;
1098 }
1099 }
1100#endif
1101
Simon Wilson79d39652011-05-25 13:44:23 -07001102 pcm->underruns = 0;
1103 return pcm;
1104
1105fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001106 if (flags & PCM_MMAP)
1107 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1108fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001109 close(pcm->fd);
1110 pcm->fd = -1;
1111 return pcm;
1112}
1113
Taylor Holberton6d58e012016-10-01 18:32:30 -04001114/** Checks if a PCM file has been opened without error.
1115 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001116 * May be NULL.
1117 * @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 -04001118 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001119 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001120 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001121int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001122{
Taylor Holbertone123a652017-01-13 21:39:48 -08001123 if (pcm != NULL) {
1124 return pcm->fd >= 0;
1125 }
1126 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001127}
Simon Wilsond6458e62011-06-21 14:58:11 -07001128
Taylor Holberton558e5942016-12-04 13:42:28 -08001129/** Links two PCMs.
1130 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1131 * If an error occurs, the error message will be written to @p pcm1.
1132 * @param pcm1 A PCM handle.
1133 * @param pcm2 Another PCM handle.
1134 * @return On success, zero; on failure, a negative number.
1135 * @ingroup libtinyalsa-pcm
1136 */
1137int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1138{
1139 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1140 if (err == -1) {
1141 return oops(pcm1, errno, "cannot link PCM");
1142 }
1143 return 0;
1144}
1145
1146/** Unlinks a PCM.
1147 * @see @ref pcm_link
1148 * @param pcm A PCM handle.
1149 * @return On success, zero; on failure, a negative number.
1150 * @ingroup libtinyalsa-pcm
1151 */
1152int pcm_unlink(struct pcm *pcm)
1153{
1154 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1155 if (err == -1) {
1156 return oops(pcm, errno, "cannot unlink PCM");
1157 }
1158 return 0;
1159}
1160
Taylor Holberton6d58e012016-10-01 18:32:30 -04001161/** Prepares a PCM, if it has not been prepared already.
1162 * @param pcm A PCM handle.
1163 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001164 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001165 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301166int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001167{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301168 if (pcm->prepared)
1169 return 0;
1170
Simon Wilsond6458e62011-06-21 14:58:11 -07001171 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1172 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001173
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301174 pcm->prepared = 1;
1175 return 0;
1176}
1177
Taylor Holberton6d58e012016-10-01 18:32:30 -04001178/** Starts a PCM.
1179 * If the PCM has not been prepared,
1180 * it is prepared in this function.
1181 * @param pcm A PCM handle.
1182 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001183 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001184 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301185int pcm_start(struct pcm *pcm)
1186{
1187 int prepare_error = pcm_prepare(pcm);
1188 if (prepare_error)
1189 return prepare_error;
1190
Liam Girdwood6be28f12011-10-13 12:59:51 -07001191 if (pcm->flags & PCM_MMAP)
Taylor Holberton25976dc2017-04-10 11:46:40 -07001192 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001193
Simon Wilsond6458e62011-06-21 14:58:11 -07001194 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1195 return oops(pcm, errno, "cannot start channel");
1196
Liam Girdwood6be28f12011-10-13 12:59:51 -07001197 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001198 return 0;
1199}
1200
Taylor Holberton6d58e012016-10-01 18:32:30 -04001201/** Stops a PCM.
1202 * @param pcm A PCM handle.
1203 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001204 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001205 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001206int pcm_stop(struct pcm *pcm)
1207{
1208 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1209 return oops(pcm, errno, "cannot stop channel");
1210
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301211 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001212 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001213 return 0;
1214}
1215
Liam Girdwood6be28f12011-10-13 12:59:51 -07001216static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1217{
1218 int avail;
1219
1220 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1221
1222 if (avail < 0)
1223 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001224 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001225 avail -= pcm->boundary;
1226
1227 return avail;
1228}
1229
1230static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1231{
1232 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1233 if (avail < 0)
1234 avail += pcm->boundary;
1235 return avail;
1236}
1237
1238static inline int pcm_mmap_avail(struct pcm *pcm)
1239{
1240 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1241 if (pcm->flags & PCM_IN)
1242 return pcm_mmap_capture_avail(pcm);
1243 else
1244 return pcm_mmap_playback_avail(pcm);
1245}
1246
1247static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1248{
1249 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1250 appl_ptr += frames;
1251
1252 /* check for boundary wrap */
1253 if (appl_ptr > pcm->boundary)
1254 appl_ptr -= pcm->boundary;
1255 pcm->mmap_control->appl_ptr = appl_ptr;
1256}
1257
1258int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1259 unsigned int *frames)
1260{
1261 unsigned int continuous, copy_frames, avail;
1262
1263 /* return the mmap buffer */
1264 *areas = pcm->mmap_buffer;
1265
1266 /* and the application offset in frames */
1267 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1268
1269 avail = pcm_mmap_avail(pcm);
1270 if (avail > pcm->buffer_size)
1271 avail = pcm->buffer_size;
1272 continuous = pcm->buffer_size - *offset;
1273
1274 /* we can only copy frames if the are availabale and continuos */
1275 copy_frames = *frames;
1276 if (copy_frames > avail)
1277 copy_frames = avail;
1278 if (copy_frames > continuous)
1279 copy_frames = continuous;
1280 *frames = copy_frames;
1281
1282 return 0;
1283}
1284
1285int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1286{
Taylor Holberton72e44222016-11-22 09:54:47 -08001287 int ret;
1288
Taylor Holberton73466c02016-10-01 12:51:59 -04001289 /* not used */
1290 (void) offset;
1291
Liam Girdwood6be28f12011-10-13 12:59:51 -07001292 /* update the application pointer in userspace and kernel */
1293 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001294 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001295 if (ret != 0){
1296 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001297 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001298 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001299
1300 return frames;
1301}
1302
1303int pcm_avail_update(struct pcm *pcm)
1304{
1305 pcm_sync_ptr(pcm, 0);
1306 return pcm_mmap_avail(pcm);
1307}
1308
1309int pcm_state(struct pcm *pcm)
1310{
1311 int err = pcm_sync_ptr(pcm, 0);
1312 if (err < 0)
1313 return err;
1314
1315 return pcm->mmap_status->state;
1316}
1317
Taylor Holberton17a10242016-11-23 13:18:24 -08001318/** Waits for frames to be available for read or write operations.
1319 * @param pcm A PCM handle.
1320 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1321 * @returns If frames became available, one is returned.
1322 * If a timeout occured, zero is returned.
1323 * If an error occured, a negative number is returned.
1324 * @ingroup libtinyalsa-pcm
1325 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001326int pcm_wait(struct pcm *pcm, int timeout)
1327{
1328 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001329 int err;
1330
1331 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001332 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001333
1334 do {
1335 /* let's wait for avail or timeout */
1336 err = poll(&pfd, 1, timeout);
1337 if (err < 0)
1338 return -errno;
1339
1340 /* timeout ? */
1341 if (err == 0)
1342 return 0;
1343
1344 /* have we been interrupted ? */
1345 if (errno == -EINTR)
1346 continue;
1347
1348 /* check for any errors */
1349 if (pfd.revents & (POLLERR | POLLNVAL)) {
1350 switch (pcm_state(pcm)) {
1351 case PCM_STATE_XRUN:
1352 return -EPIPE;
1353 case PCM_STATE_SUSPENDED:
1354 return -ESTRPIPE;
1355 case PCM_STATE_DISCONNECTED:
1356 return -ENODEV;
1357 default:
1358 return -EIO;
1359 }
1360 }
1361 /* poll again if fd not ready for IO */
1362 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1363
1364 return 1;
1365}
1366
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001367int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001368{
1369 int err = 0, frames, avail;
1370 unsigned int offset = 0, count;
1371
1372 if (bytes == 0)
1373 return 0;
1374
1375 count = pcm_bytes_to_frames(pcm, bytes);
1376
1377 while (count > 0) {
1378
1379 /* get the available space for writing new frames */
1380 avail = pcm_avail_update(pcm);
1381 if (avail < 0) {
1382 fprintf(stderr, "cannot determine available mmap frames");
1383 return err;
1384 }
1385
1386 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001387 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001388 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1389 if (pcm_start(pcm) < 0) {
1390 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1391 (unsigned int)pcm->mmap_status->hw_ptr,
1392 (unsigned int)pcm->mmap_control->appl_ptr,
1393 avail);
1394 return -errno;
1395 }
1396 }
1397
1398 /* sleep until we have space to write new frames */
1399 if (pcm->running &&
1400 (unsigned int)avail < pcm->mmap_control->avail_min) {
1401 int time = -1;
1402
1403 if (pcm->flags & PCM_NOIRQ)
1404 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1405 / pcm->noirq_frames_per_msec;
1406
1407 err = pcm_wait(pcm, time);
1408 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301409 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001410 pcm->running = 0;
1411 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1412 (unsigned int)pcm->mmap_status->hw_ptr,
1413 (unsigned int)pcm->mmap_control->appl_ptr,
1414 avail);
1415 pcm->mmap_control->appl_ptr = 0;
1416 return err;
1417 }
1418 continue;
1419 }
1420
1421 frames = count;
1422 if (frames > avail)
1423 frames = avail;
1424
1425 if (!frames)
1426 break;
1427
1428 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001429 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001430 if (frames < 0) {
1431 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1432 (unsigned int)pcm->mmap_status->hw_ptr,
1433 (unsigned int)pcm->mmap_control->appl_ptr,
1434 avail);
1435 return frames;
1436 }
1437
1438 offset += frames;
1439 count -= frames;
1440 }
1441
Liam Girdwood6be28f12011-10-13 12:59:51 -07001442 return 0;
1443}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001444
1445int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1446{
1447 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1448 return -ENOSYS;
1449
1450 return pcm_mmap_transfer(pcm, (void *)data, count);
1451}
1452
1453int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1454{
1455 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1456 return -ENOSYS;
1457
1458 return pcm_mmap_transfer(pcm, data, count);
1459}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301460
Taylor Holberton17a10242016-11-23 13:18:24 -08001461/** Gets the delay of the PCM, in terms of frames.
1462 * @param pcm A PCM handle.
1463 * @returns On success, the delay of the PCM.
1464 * On failure, a negative number.
1465 * @ingroup libtinyalsa-pcm
1466 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301467long pcm_get_delay(struct pcm *pcm)
1468{
1469 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1470 return -1;
1471
1472 return pcm->pcm_delay;
1473}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001474