blob: 474295a23afe5540f629545c92335bf5bc4dd16d [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* pcm.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070061#include <tinyalsa/limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070062
Taylor Holberton1f741562018-11-28 16:33:24 -050063#ifndef PARAM_MAX
Simon Wilson79d39652011-05-25 13:44:23 -070064#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Taylor Holberton1f741562018-11-28 16:33:24 -050065#endif /* PARAM_MAX */
66
67#ifndef SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
Liam Girdwood6be28f12011-10-13 12:59:51 -070068#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Taylor Holberton1f741562018-11-28 16:33:24 -050069#endif /* SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP */
Simon Wilson79d39652011-05-25 13:44:23 -070070
71static inline int param_is_mask(int p)
72{
73 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
74 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
75}
76
77static inline int param_is_interval(int p)
78{
79 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
80 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
81}
82
Taylor Holberton2f387d22016-12-01 15:58:16 -080083static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
84{
Taylor Holberton25976dc2017-04-10 11:46:40 -070085 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080086}
87
Simon Wilson79d39652011-05-25 13:44:23 -070088static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
89{
90 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
91}
92
93static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
94{
95 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
96}
97
98static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
99{
100 if (bit >= SNDRV_MASK_MAX)
101 return;
102 if (param_is_mask(n)) {
103 struct snd_mask *m = param_to_mask(p, n);
104 m->bits[0] = 0;
105 m->bits[1] = 0;
106 m->bits[bit >> 5] |= (1 << (bit & 31));
107 }
108}
109
110static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
111{
112 if (param_is_interval(n)) {
113 struct snd_interval *i = param_to_interval(p, n);
114 i->min = val;
115 }
116}
117
Taylor Holberton2f387d22016-12-01 15:58:16 -0800118static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700119{
120 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800121 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700122 return i->min;
123 }
124 return 0;
125}
126
Taylor Holberton2f387d22016-12-01 15:58:16 -0800127static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700128{
129 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800130 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700131 return i->max;
132 }
133 return 0;
134}
135
Simon Wilson79d39652011-05-25 13:44:23 -0700136static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
137{
138 if (param_is_interval(n)) {
139 struct snd_interval *i = param_to_interval(p, n);
140 i->min = val;
141 i->max = val;
142 i->integer = 1;
143 }
144}
145
Liam Girdwood6be28f12011-10-13 12:59:51 -0700146static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
147{
148 if (param_is_interval(n)) {
149 struct snd_interval *i = param_to_interval(p, n);
150 if (i->integer)
151 return i->max;
152 }
153 return 0;
154}
155
Simon Wilson79d39652011-05-25 13:44:23 -0700156static void param_init(struct snd_pcm_hw_params *p)
157{
158 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700159
Simon Wilson79d39652011-05-25 13:44:23 -0700160 memset(p, 0, sizeof(*p));
161 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
162 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
163 struct snd_mask *m = param_to_mask(p, n);
164 m->bits[0] = ~0;
165 m->bits[1] = ~0;
166 }
167 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
168 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
169 struct snd_interval *i = param_to_interval(p, n);
170 i->min = 0;
171 i->max = ~0;
172 }
Simon Wilson43544882012-10-31 12:52:39 -0700173 p->rmask = ~0U;
174 p->cmask = 0;
175 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700176}
177
Taylor Holberton861da7a2017-04-10 12:05:51 -0700178static unsigned int pcm_format_to_alsa(enum pcm_format format)
179{
180 switch (format) {
181
182 case PCM_FORMAT_S8:
183 return SNDRV_PCM_FORMAT_S8;
184
185 default:
186 case PCM_FORMAT_S16_LE:
187 return SNDRV_PCM_FORMAT_S16_LE;
188 case PCM_FORMAT_S16_BE:
189 return SNDRV_PCM_FORMAT_S16_BE;
190
191 case PCM_FORMAT_S24_LE:
192 return SNDRV_PCM_FORMAT_S24_LE;
193 case PCM_FORMAT_S24_BE:
194 return SNDRV_PCM_FORMAT_S24_BE;
195
196 case PCM_FORMAT_S24_3LE:
197 return SNDRV_PCM_FORMAT_S24_3LE;
198 case PCM_FORMAT_S24_3BE:
199 return SNDRV_PCM_FORMAT_S24_3BE;
200
201 case PCM_FORMAT_S32_LE:
202 return SNDRV_PCM_FORMAT_S32_LE;
203 case PCM_FORMAT_S32_BE:
204 return SNDRV_PCM_FORMAT_S32_BE;
205 };
206}
207
Simon Wilson79d39652011-05-25 13:44:23 -0700208#define PCM_ERROR_MAX 128
209
Taylor Holberton6d58e012016-10-01 18:32:30 -0400210/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800211 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400212 */
Simon Wilson79d39652011-05-25 13:44:23 -0700213struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400214 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700215 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400216 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700217 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400218 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700219 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400220 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700221 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400222 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700223 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800224 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700225 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400226 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700227 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400228 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700229 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700230 struct snd_pcm_mmap_status *mmap_status;
231 struct snd_pcm_mmap_control *mmap_control;
232 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700233 void *mmap_buffer;
234 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800235 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530236 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800237 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200238 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700239};
240
Taylor Holberton861da7a2017-04-10 12:05:51 -0700241static int oops(struct pcm *pcm, int e, const char *fmt, ...)
242{
243 va_list ap;
244 int sz;
245
246 va_start(ap, fmt);
247 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
248 va_end(ap);
249 sz = strlen(pcm->error);
250
251 if (errno)
252 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
253 ": %s", strerror(e));
254 return -1;
255}
256
Taylor Holberton6d58e012016-10-01 18:32:30 -0400257/** Gets the buffer size of the PCM.
258 * @param pcm A PCM handle.
259 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800260 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400261 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800262unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700263{
264 return pcm->buffer_size;
265}
266
Taylor Holberton77979a82016-12-01 20:04:04 -0800267/** Gets the channel count of the PCM.
268 * @param pcm A PCM handle.
269 * @return The channel count of the PCM.
270 * @ingroup libtinyalsa-pcm
271 */
272unsigned int pcm_get_channels(const struct pcm *pcm)
273{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700274 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800275}
276
Taylor Holberton08bb5902017-04-10 11:45:44 -0700277/** Gets the PCM configuration.
278 * @param pcm A PCM handle.
279 * @return The PCM configuration.
280 * This function only returns NULL if
281 * @p pcm is NULL.
282 * @ingroup libtinyalsa-pcm
283 * */
284const struct pcm_config * pcm_get_config(const struct pcm *pcm)
285{
286 if (pcm == NULL)
287 return NULL;
288 return &pcm->config;
289}
290
Taylor Holberton77979a82016-12-01 20:04:04 -0800291/** Gets the rate of the PCM.
292 * The rate is given in frames per second.
293 * @param pcm A PCM handle.
294 * @return The rate of the PCM.
295 * @ingroup libtinyalsa-pcm
296 */
297unsigned int pcm_get_rate(const struct pcm *pcm)
298{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700299 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800300}
301
302/** Gets the format of the PCM.
303 * @param pcm A PCM handle.
304 * @return The format of the PCM.
305 * @ingroup libtinyalsa-pcm
306 */
307enum pcm_format pcm_get_format(const struct pcm *pcm)
308{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700309 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800310}
311
Taylor Holberton6d58e012016-10-01 18:32:30 -0400312/** Gets the file descriptor of the PCM.
313 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800314 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400315 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800316 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400317 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800318int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400319{
320 return pcm->fd;
321}
322
Taylor Holberton6d58e012016-10-01 18:32:30 -0400323/** Gets the error message for the last error that occured.
324 * If no error occured and this function is called, the results are undefined.
325 * @param pcm A PCM handle.
326 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800327 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400328 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800329const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700330{
331 return pcm->error;
332}
333
Taylor Holberton861da7a2017-04-10 12:05:51 -0700334/** Sets the PCM configuration.
335 * @param pcm A PCM handle.
336 * @param config The configuration to use for the
337 * PCM. This parameter may be NULL, in which case
338 * the default configuration is used.
339 * @returns Zero on success, a negative errno value
340 * on failure.
341 * @ingroup libtinyalsa-pcm
342 * */
343int pcm_set_config(struct pcm *pcm, const struct pcm_config *config)
344{
345 if (pcm == NULL)
346 return -EFAULT;
347 else if (config == NULL) {
348 config = &pcm->config;
349 pcm->config.channels = 2;
350 pcm->config.rate = 48000;
351 pcm->config.period_size = 1024;
352 pcm->config.period_count = 4;
353 pcm->config.format = PCM_FORMAT_S16_LE;
354 pcm->config.start_threshold = config->period_count * config->period_size;
355 pcm->config.stop_threshold = config->period_count * config->period_size;
356 pcm->config.silence_threshold = 0;
357 } else
358 pcm->config = *config;
359
360 struct snd_pcm_hw_params params;
361 param_init(&params);
362 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
363 pcm_format_to_alsa(config->format));
364 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
365 SNDRV_PCM_SUBFORMAT_STD);
366 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
367 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
368 pcm_format_to_bits(config->format));
369 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
370 pcm_format_to_bits(config->format) * config->channels);
371 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
372 config->channels);
373 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
374 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
375
376 if (pcm->flags & PCM_NOIRQ) {
377
378 if (!(pcm->flags & PCM_MMAP)) {
379 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
380 return -EINVAL;
381 }
382
383 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
384 pcm->noirq_frames_per_msec = config->rate / 1000;
385 }
386
387 if (pcm->flags & PCM_MMAP)
388 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
389 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
390 else
391 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
392 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
393
394 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
395 int errno_copy = errno;
396 oops(pcm, -errno, "cannot set hw params");
397 return -errno_copy;
398 }
399
400 /* get our refined hw_params */
401 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
402 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
403 pcm->buffer_size = config->period_count * config->period_size;
404
405 if (pcm->flags & PCM_MMAP) {
406 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
407 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
408 if (pcm->mmap_buffer == MAP_FAILED) {
409 int errno_copy = errno;
410 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
411 pcm_frames_to_bytes(pcm, pcm->buffer_size));
412 return -errno_copy;
413 }
414 }
415
416 struct snd_pcm_sw_params sparams;
417 memset(&sparams, 0, sizeof(sparams));
418 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
419 sparams.period_step = 1;
420 sparams.avail_min = 1;
421
422 if (!config->start_threshold) {
423 if (pcm->flags & PCM_IN)
424 pcm->config.start_threshold = sparams.start_threshold = 1;
425 else
426 pcm->config.start_threshold = sparams.start_threshold =
427 config->period_count * config->period_size / 2;
428 } else
429 sparams.start_threshold = config->start_threshold;
430
431 /* pick a high stop threshold - todo: does this need further tuning */
432 if (!config->stop_threshold) {
433 if (pcm->flags & PCM_IN)
434 pcm->config.stop_threshold = sparams.stop_threshold =
435 config->period_count * config->period_size * 10;
436 else
437 pcm->config.stop_threshold = sparams.stop_threshold =
438 config->period_count * config->period_size;
439 }
440 else
441 sparams.stop_threshold = config->stop_threshold;
442
443 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
444 sparams.silence_size = 0;
445 sparams.silence_threshold = config->silence_threshold;
446 pcm->boundary = sparams.boundary = pcm->buffer_size;
447
448 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
449 pcm->boundary *= 2;
450
451 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
452 int errno_copy = errno;
453 oops(pcm, -errno, "cannot set sw params");
454 return -errno_copy;
455 }
456
457 return 0;
458}
459
Taylor Holberton6d58e012016-10-01 18:32:30 -0400460/** Gets the subdevice on which the pcm has been opened.
461 * @param pcm A PCM handle.
462 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800463unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200464{
465 return pcm->subdevice;
466}
467
Taylor Holberton6d58e012016-10-01 18:32:30 -0400468/** Determines the number of bits occupied by a @ref pcm_format.
469 * @param format A PCM format.
470 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800471 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400472 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700473unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700474{
475 switch (format) {
476 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400477 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700478 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400479 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700480 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400481 case PCM_FORMAT_S24_3LE:
482 case PCM_FORMAT_S24_3BE:
483 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700484 default:
485 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400486 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700487 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400488 case PCM_FORMAT_S8:
489 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700490 };
491}
492
Taylor Holberton6d58e012016-10-01 18:32:30 -0400493/** Determines how many frames of a PCM can fit into a number of bytes.
494 * @param pcm A PCM handle.
495 * @param bytes The number of bytes.
496 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800497 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400498 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800499unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700500{
501 return bytes / (pcm->config.channels *
502 (pcm_format_to_bits(pcm->config.format) >> 3));
503}
504
Taylor Holberton6d58e012016-10-01 18:32:30 -0400505/** Determines how many bytes are occupied by a number of frames of a PCM.
506 * @param pcm A PCM handle.
507 * @param frames The number of frames of a PCM.
508 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800509 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400510 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800511unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700512{
513 return frames * pcm->config.channels *
514 (pcm_format_to_bits(pcm->config.format) >> 3);
515}
516
Taylor Holberton4f556062016-09-16 09:54:36 -0400517static int pcm_sync_ptr(struct pcm *pcm, int flags)
518{
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200519 if (pcm->sync_ptr == NULL) {
520 /* status and control are mmaped */
521
522 if (flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
523 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HWSYNC) == -1) {
524 oops(pcm, errno, "failed to sync hardware pointer");
525 return -1;
526 }
527 }
528 } else {
Eric Laurent40b018e2011-06-18 10:10:23 -0700529 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800530 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
531 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700532 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800533 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700534 }
Ricardo Biehl Pasqualic1446752018-12-18 23:13:05 -0200535
Miguel Gaioc9f97da2018-07-17 10:05:54 +0200536 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700537}
538
Taylor Holberton4f556062016-09-16 09:54:36 -0400539static int pcm_hw_mmap_status(struct pcm *pcm)
540{
Eric Laurent40b018e2011-06-18 10:10:23 -0700541 if (pcm->sync_ptr)
542 return 0;
543
544 int page_size = sysconf(_SC_PAGE_SIZE);
545 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
546 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
547 if (pcm->mmap_status == MAP_FAILED)
548 pcm->mmap_status = NULL;
549 if (!pcm->mmap_status)
550 goto mmap_error;
551
552 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
553 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
554 if (pcm->mmap_control == MAP_FAILED)
555 pcm->mmap_control = NULL;
556 if (!pcm->mmap_control) {
557 munmap(pcm->mmap_status, page_size);
558 pcm->mmap_status = NULL;
559 goto mmap_error;
560 }
Eric Laurent40b018e2011-06-18 10:10:23 -0700561
562 return 0;
563
564mmap_error:
565
566 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
567 if (!pcm->sync_ptr)
568 return -ENOMEM;
569 pcm->mmap_status = &pcm->sync_ptr->s.status;
570 pcm->mmap_control = &pcm->sync_ptr->c.control;
Eric Laurent40b018e2011-06-18 10:10:23 -0700571
572 return 0;
573}
574
575static void pcm_hw_munmap_status(struct pcm *pcm) {
576 if (pcm->sync_ptr) {
577 free(pcm->sync_ptr);
578 pcm->sync_ptr = NULL;
579 } else {
580 int page_size = sysconf(_SC_PAGE_SIZE);
581 if (pcm->mmap_status)
582 munmap(pcm->mmap_status, page_size);
583 if (pcm->mmap_control)
584 munmap(pcm->mmap_control, page_size);
585 }
586 pcm->mmap_status = NULL;
587 pcm->mmap_control = NULL;
588}
589
Taylor Holberton6d58e012016-10-01 18:32:30 -0400590/** Writes audio samples to PCM.
591 * If the PCM has not been started, it is started in this function.
592 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
593 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
594 * @param pcm A PCM handle.
595 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800596 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700597 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
598 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800599 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800600 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400601 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800602int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700603{
604 struct snd_xferi x;
605
606 if (pcm->flags & PCM_IN)
607 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700608#if UINT_MAX > TINYALSA_FRAMES_MAX
609 if (frame_count > TINYALSA_FRAMES_MAX)
610 return -EINVAL;
611#endif
612 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700613 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700614
Mark Brown6bbe77a2012-02-10 22:07:09 +0000615 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800616 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800617 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700618 for (;;) {
Simon Wilson79d39652011-05-25 13:44:23 -0700619 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
620 pcm->running = 0;
621 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700622 /* we failed to make our window -- try to restart if we are
623 * allowed to do so. Otherwise, simply allow the EPIPE error to
624 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700625 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700626 if (pcm->flags & PCM_NORESTART)
627 return -EPIPE;
Ricardo Biehl Pasquali7c40a9e2018-09-09 21:40:01 -0300628 if (pcm_prepare(pcm))
629 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700630 continue;
631 }
632 return oops(pcm, errno, "cannot write stream data");
633 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800634 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700635 }
636}
637
Taylor Holberton6d58e012016-10-01 18:32:30 -0400638/** Reads audio samples from PCM.
639 * If the PCM has not been started, it is started in this function.
640 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
641 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
642 * @param pcm A PCM handle.
643 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800644 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700645 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
646 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800647 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800648 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400649 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800650int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700651{
652 struct snd_xferi x;
653
654 if (!(pcm->flags & PCM_IN))
655 return -EINVAL;
Taylor Holberton001b25e2017-04-10 12:42:06 -0700656#if UINT_MAX > TINYALSA_FRAMES_MAX
657 if (frame_count > TINYALSA_FRAMES_MAX)
658 return -EINVAL;
659#endif
660 if (frame_count > INT_MAX)
Taylor Holberton851dd802017-04-06 23:12:01 -0700661 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700662
663 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800664 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800665 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700666 for (;;) {
Ricardo Biehl Pasquali13e11fe2018-08-21 14:42:23 -0300667 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Simon Wilson79d39652011-05-25 13:44:23 -0700668 pcm->running = 0;
669 if (errno == EPIPE) {
670 /* we failed to make our window -- try to restart */
671 pcm->underruns++;
Ricardo Biehl Pasqualif85cf622019-01-07 19:33:13 -0200672 if (pcm->flags & PCM_NORESTART)
673 return -EPIPE;
674 if (pcm_prepare(pcm))
675 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700676 continue;
677 }
678 return oops(pcm, errno, "cannot read stream data");
679 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800680 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700681 }
682}
683
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800684/** Writes audio samples to PCM.
685 * If the PCM has not been started, it is started in this function.
686 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
687 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
688 * @param pcm A PCM handle.
689 * @param data The audio sample array
690 * @param count The number of bytes occupied by the sample array.
691 * @return On success, this function returns zero; otherwise, a negative number.
692 * @deprecated
693 * @ingroup libtinyalsa-pcm
694 */
695int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
696{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700697 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800698}
699
700/** Reads audio samples from PCM.
701 * If the PCM has not been started, it is started in this function.
702 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
703 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
704 * @param pcm A PCM handle.
705 * @param data The audio sample array
706 * @param count The number of bytes occupied by the sample array.
707 * @return On success, this function returns zero; otherwise, a negative number.
708 * @deprecated
709 * @ingroup libtinyalsa-pcm
710 */
711int pcm_read(struct pcm *pcm, void *data, unsigned int count)
712{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700713 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800714}
715
Simon Wilson79d39652011-05-25 13:44:23 -0700716static struct pcm bad_pcm = {
717 .fd = -1,
718};
719
Taylor Holberton6d58e012016-10-01 18:32:30 -0400720/** Gets the hardware parameters of a PCM, without created a PCM handle.
721 * @param card The card of the PCM.
722 * The default card is zero.
723 * @param device The device of the PCM.
724 * The default device is zero.
725 * @param flags Specifies whether the PCM is an input or output.
726 * May be one of the following:
727 * - @ref PCM_IN
728 * - @ref PCM_OUT
729 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800730 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400731 */
Simon Wilson43544882012-10-31 12:52:39 -0700732struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
733 unsigned int flags)
734{
735 struct snd_pcm_hw_params *params;
736 char fn[256];
737 int fd;
738
739 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
740 flags & PCM_IN ? 'c' : 'p');
741
Taylor Holberton093b8782017-10-12 20:28:30 -0400742 if (flags & PCM_NONBLOCK)
743 fd = open(fn, O_RDWR | O_NONBLOCK);
744 else
745 fd = open(fn, O_RDWR);
746
Simon Wilson43544882012-10-31 12:52:39 -0700747 if (fd < 0) {
Taylor Holberton093b8782017-10-12 20:28:30 -0400748 fprintf(stderr, "cannot open device '%s': %s\n", fn, strerror(errno));
Simon Wilson43544882012-10-31 12:52:39 -0700749 goto err_open;
750 }
751
752 params = calloc(1, sizeof(struct snd_pcm_hw_params));
753 if (!params)
754 goto err_calloc;
755
756 param_init(params);
757 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
758 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
759 goto err_hw_refine;
760 }
761
762 close(fd);
763
764 return (struct pcm_params *)params;
765
766err_hw_refine:
767 free(params);
768err_calloc:
769 close(fd);
770err_open:
771 return NULL;
772}
773
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500774/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400775 * @param pcm_params Hardware parameters of a PCM.
776 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800777 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400778 */
Simon Wilson43544882012-10-31 12:52:39 -0700779void pcm_params_free(struct pcm_params *pcm_params)
780{
781 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
782
783 if (params)
784 free(params);
785}
786
787static int pcm_param_to_alsa(enum pcm_param param)
788{
789 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700790 case PCM_PARAM_ACCESS:
791 return SNDRV_PCM_HW_PARAM_ACCESS;
792 case PCM_PARAM_FORMAT:
793 return SNDRV_PCM_HW_PARAM_FORMAT;
794 case PCM_PARAM_SUBFORMAT:
795 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700796 case PCM_PARAM_SAMPLE_BITS:
797 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
798 break;
799 case PCM_PARAM_FRAME_BITS:
800 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
801 break;
802 case PCM_PARAM_CHANNELS:
803 return SNDRV_PCM_HW_PARAM_CHANNELS;
804 break;
805 case PCM_PARAM_RATE:
806 return SNDRV_PCM_HW_PARAM_RATE;
807 break;
808 case PCM_PARAM_PERIOD_TIME:
809 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
810 break;
811 case PCM_PARAM_PERIOD_SIZE:
812 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
813 break;
814 case PCM_PARAM_PERIOD_BYTES:
815 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
816 break;
817 case PCM_PARAM_PERIODS:
818 return SNDRV_PCM_HW_PARAM_PERIODS;
819 break;
820 case PCM_PARAM_BUFFER_TIME:
821 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
822 break;
823 case PCM_PARAM_BUFFER_SIZE:
824 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
825 break;
826 case PCM_PARAM_BUFFER_BYTES:
827 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
828 break;
829 case PCM_PARAM_TICK_TIME:
830 return SNDRV_PCM_HW_PARAM_TICK_TIME;
831 break;
832
833 default:
834 return -1;
835 }
836}
837
Taylor Holberton6d58e012016-10-01 18:32:30 -0400838/** Gets a mask from a PCM's hardware parameters.
839 * @param pcm_params A PCM's hardware parameters.
840 * @param param The parameter to get.
841 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
842 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800843 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400844 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800845const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700846 enum pcm_param param)
847{
848 int p;
849 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
850 if (params == NULL) {
851 return NULL;
852 }
853
854 p = pcm_param_to_alsa(param);
855 if (p < 0 || !param_is_mask(p)) {
856 return NULL;
857 }
858
Taylor Holberton2f387d22016-12-01 15:58:16 -0800859 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700860}
861
Taylor Holberton17a10242016-11-23 13:18:24 -0800862/** Get the minimum of a specified PCM parameter.
863 * @param pcm_params A PCM parameters structure.
864 * @param param The specified parameter to get the minimum of.
865 * @returns On success, the parameter minimum.
866 * On failure, zero.
867 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800868unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700869 enum pcm_param param)
870{
871 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
872 int p;
873
874 if (!params)
875 return 0;
876
877 p = pcm_param_to_alsa(param);
878 if (p < 0)
879 return 0;
880
881 return param_get_min(params, p);
882}
883
Taylor Holberton17a10242016-11-23 13:18:24 -0800884/** Get the maximum of a specified PCM parameter.
885 * @param pcm_params A PCM parameters structure.
886 * @param param The specified parameter to get the maximum of.
887 * @returns On success, the parameter maximum.
888 * On failure, zero.
889 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800890unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700891 enum pcm_param param)
892{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800893 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700894 int p;
895
896 if (!params)
897 return 0;
898
899 p = pcm_param_to_alsa(param);
900 if (p < 0)
901 return 0;
902
903 return param_get_max(params, p);
904}
905
Taylor Holberton6d58e012016-10-01 18:32:30 -0400906/** Closes a PCM returned by @ref pcm_open.
907 * @param pcm A PCM returned by @ref pcm_open.
908 * May not be NULL.
909 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800910 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400911 */
Simon Wilson79d39652011-05-25 13:44:23 -0700912int pcm_close(struct pcm *pcm)
913{
914 if (pcm == &bad_pcm)
915 return 0;
916
Eric Laurent40b018e2011-06-18 10:10:23 -0700917 pcm_hw_munmap_status(pcm);
918
Liam Girdwood6be28f12011-10-13 12:59:51 -0700919 if (pcm->flags & PCM_MMAP) {
920 pcm_stop(pcm);
921 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
922 }
923
Simon Wilson79d39652011-05-25 13:44:23 -0700924 if (pcm->fd >= 0)
925 close(pcm->fd);
926 pcm->running = 0;
927 pcm->buffer_size = 0;
928 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700929 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700930 return 0;
931}
932
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800933/** Opens a PCM by it's name.
934 * @param name The name of the PCM.
935 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
936 * @param flags Specify characteristics and functionality about the pcm.
937 * May be a bitwise AND of the following:
938 * - @ref PCM_IN
939 * - @ref PCM_OUT
940 * - @ref PCM_MMAP
941 * - @ref PCM_NOIRQ
942 * - @ref PCM_MONOTONIC
943 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800944 * @returns A PCM structure.
945 * If an error occurs allocating memory for the PCM, NULL is returned.
946 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
947 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800948 * @ingroup libtinyalsa-pcm
949 */
950struct pcm *pcm_open_by_name(const char *name,
951 unsigned int flags,
952 const struct pcm_config *config)
953{
954 unsigned int card, device;
955 if ((name[0] != 'h')
956 || (name[1] != 'w')
957 || (name[2] != ':')) {
958 return NULL;
959 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
960 return NULL;
961 }
962 return pcm_open(card, device, flags, config);
963}
964
Taylor Holberton6d58e012016-10-01 18:32:30 -0400965/** Opens a PCM.
966 * @param card The card that the pcm belongs to.
967 * The default card is zero.
968 * @param device The device that the pcm belongs to.
969 * The default device is zero.
970 * @param flags Specify characteristics and functionality about the pcm.
971 * May be a bitwise AND of the following:
972 * - @ref PCM_IN
973 * - @ref PCM_OUT
974 * - @ref PCM_MMAP
975 * - @ref PCM_NOIRQ
976 * - @ref PCM_MONOTONIC
977 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800978 * @returns A PCM structure.
979 * If an error occurs allocating memory for the PCM, NULL is returned.
980 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
981 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800982 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400983 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700984struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800985 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700986{
Simon Wilson79d39652011-05-25 13:44:23 -0700987 struct pcm *pcm;
988 struct snd_pcm_info info;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700989 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700990 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700991
992 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400993 if (!pcm)
994 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700995
Simon Wilson1bd580f2011-06-02 15:58:41 -0700996 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
997 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700998
999 pcm->flags = flags;
Taylor Holberton093b8782017-10-12 20:28:30 -04001000
1001 if (flags & PCM_NONBLOCK)
1002 pcm->fd = open(fn, O_RDWR | O_NONBLOCK);
1003 else
1004 pcm->fd = open(fn, O_RDWR);
1005
Simon Wilson79d39652011-05-25 13:44:23 -07001006 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -07001007 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -07001008 return pcm;
1009 }
1010
1011 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -07001012 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001013 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001014 }
David Wagner4cddf192014-04-02 15:12:54 +02001015 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -07001016
Taylor Holberton861da7a2017-04-10 12:05:51 -07001017 if (pcm_set_config(pcm, config) != 0)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001018 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001019
Eric Laurent40b018e2011-06-18 10:10:23 -07001020 rc = pcm_hw_mmap_status(pcm);
1021 if (rc < 0) {
1022 oops(pcm, rc, "mmap status failed");
1023 goto fail;
1024 }
1025
Glenn Kasten81012402013-08-22 15:11:48 -07001026#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1027 if (pcm->flags & PCM_MONOTONIC) {
1028 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1029 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1030 if (rc < 0) {
1031 oops(pcm, rc, "cannot set timestamp type");
1032 goto fail;
1033 }
1034 }
1035#endif
1036
Ricardo Biehl Pasquali13bf6292018-08-22 16:10:45 -03001037 /* prepare here so the user does not need to do this later */
1038 if (pcm_prepare(pcm))
1039 goto fail;
1040
Simon Wilson79d39652011-05-25 13:44:23 -07001041 pcm->underruns = 0;
1042 return pcm;
1043
1044fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001045 if (flags & PCM_MMAP)
1046 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1047fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001048 close(pcm->fd);
1049 pcm->fd = -1;
1050 return pcm;
1051}
1052
Taylor Holberton6d58e012016-10-01 18:32:30 -04001053/** Checks if a PCM file has been opened without error.
1054 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001055 * May be NULL.
1056 * @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 -04001057 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001058 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001059 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001060int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001061{
Taylor Holbertone123a652017-01-13 21:39:48 -08001062 if (pcm != NULL) {
1063 return pcm->fd >= 0;
1064 }
1065 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001066}
Simon Wilsond6458e62011-06-21 14:58:11 -07001067
Taylor Holberton558e5942016-12-04 13:42:28 -08001068/** Links two PCMs.
1069 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1070 * If an error occurs, the error message will be written to @p pcm1.
1071 * @param pcm1 A PCM handle.
1072 * @param pcm2 Another PCM handle.
1073 * @return On success, zero; on failure, a negative number.
1074 * @ingroup libtinyalsa-pcm
1075 */
1076int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1077{
1078 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1079 if (err == -1) {
1080 return oops(pcm1, errno, "cannot link PCM");
1081 }
1082 return 0;
1083}
1084
1085/** Unlinks a PCM.
1086 * @see @ref pcm_link
1087 * @param pcm A PCM handle.
1088 * @return On success, zero; on failure, a negative number.
1089 * @ingroup libtinyalsa-pcm
1090 */
1091int pcm_unlink(struct pcm *pcm)
1092{
1093 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1094 if (err == -1) {
1095 return oops(pcm, errno, "cannot unlink PCM");
1096 }
1097 return 0;
1098}
1099
Taylor Holberton6d58e012016-10-01 18:32:30 -04001100/** Prepares a PCM, if it has not been prepared already.
1101 * @param pcm A PCM handle.
1102 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001103 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001104 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301105int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001106{
1107 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1108 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001109
Ricardo Biehl Pasquali535a6ba2018-12-30 11:53:54 -02001110 /* get appl_ptr and avail_min from kernel */
1111 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
1112
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301113 return 0;
1114}
1115
Taylor Holberton6d58e012016-10-01 18:32:30 -04001116/** Starts a PCM.
Taylor Holberton6d58e012016-10-01 18:32:30 -04001117 * @param pcm A PCM handle.
1118 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001119 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001120 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301121int pcm_start(struct pcm *pcm)
1122{
Ricardo Biehl Pasquali7ff9cde2018-12-31 17:23:39 -02001123 /* set appl_ptr and avail_min in kernel */
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001124 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001125
Miguel Gaiocf5f0632018-07-17 13:30:24 +02001126 if (pcm->mmap_status->state != PCM_STATE_RUNNING) {
1127 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1128 return oops(pcm, errno, "cannot start channel");
1129 }
Simon Wilsond6458e62011-06-21 14:58:11 -07001130
Liam Girdwood6be28f12011-10-13 12:59:51 -07001131 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001132 return 0;
1133}
1134
Taylor Holberton6d58e012016-10-01 18:32:30 -04001135/** Stops a PCM.
1136 * @param pcm A PCM handle.
1137 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001138 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001139 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001140int pcm_stop(struct pcm *pcm)
1141{
1142 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1143 return oops(pcm, errno, "cannot stop channel");
1144
Liam Girdwood6be28f12011-10-13 12:59:51 -07001145 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001146 return 0;
1147}
1148
Liam Girdwood6be28f12011-10-13 12:59:51 -07001149static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1150{
1151 int avail;
1152
1153 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1154
1155 if (avail < 0)
1156 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001157 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001158 avail -= pcm->boundary;
1159
1160 return avail;
1161}
1162
1163static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1164{
1165 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1166 if (avail < 0)
1167 avail += pcm->boundary;
1168 return avail;
1169}
1170
1171static inline int pcm_mmap_avail(struct pcm *pcm)
1172{
Liam Girdwood6be28f12011-10-13 12:59:51 -07001173 if (pcm->flags & PCM_IN)
1174 return pcm_mmap_capture_avail(pcm);
1175 else
1176 return pcm_mmap_playback_avail(pcm);
1177}
1178
1179static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1180{
1181 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1182 appl_ptr += frames;
1183
1184 /* check for boundary wrap */
1185 if (appl_ptr > pcm->boundary)
1186 appl_ptr -= pcm->boundary;
1187 pcm->mmap_control->appl_ptr = appl_ptr;
1188}
1189
1190int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1191 unsigned int *frames)
1192{
1193 unsigned int continuous, copy_frames, avail;
1194
1195 /* return the mmap buffer */
1196 *areas = pcm->mmap_buffer;
1197
1198 /* and the application offset in frames */
1199 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1200
1201 avail = pcm_mmap_avail(pcm);
1202 if (avail > pcm->buffer_size)
1203 avail = pcm->buffer_size;
1204 continuous = pcm->buffer_size - *offset;
1205
1206 /* we can only copy frames if the are availabale and continuos */
1207 copy_frames = *frames;
1208 if (copy_frames > avail)
1209 copy_frames = avail;
1210 if (copy_frames > continuous)
1211 copy_frames = continuous;
1212 *frames = copy_frames;
1213
1214 return 0;
1215}
1216
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001217static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
1218 char *buf, unsigned int src_offset,
1219 unsigned int frames)
1220{
1221 int size_bytes = pcm_frames_to_bytes(pcm, frames);
1222 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
1223 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
1224
1225 /* interleaved only atm */
1226 if (pcm->flags & PCM_IN)
1227 memcpy(buf + src_offset_bytes,
1228 (char*)pcm->mmap_buffer + pcm_offset_bytes,
1229 size_bytes);
1230 else
1231 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
1232 buf + src_offset_bytes,
1233 size_bytes);
1234 return 0;
1235}
1236
Liam Girdwood6be28f12011-10-13 12:59:51 -07001237int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1238{
Taylor Holberton72e44222016-11-22 09:54:47 -08001239 int ret;
1240
Taylor Holberton73466c02016-10-01 12:51:59 -04001241 /* not used */
1242 (void) offset;
1243
Liam Girdwood6be28f12011-10-13 12:59:51 -07001244 /* update the application pointer in userspace and kernel */
1245 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001246 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001247 if (ret != 0){
1248 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001249 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001250 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001251
1252 return frames;
1253}
1254
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001255static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
1256 unsigned int offset, unsigned int size)
1257{
1258 void *pcm_areas;
1259 int commit;
1260 unsigned int pcm_offset, frames, count = 0;
1261
Ricardo Biehl Pasquali7aacaed2018-12-18 23:24:19 -02001262 while (pcm_mmap_avail(pcm) && size) {
Ricardo Biehl Pasqualid759f482018-12-22 11:17:32 -02001263 frames = size;
1264 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
1265 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
1266 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
1267 if (commit < 0) {
1268 oops(pcm, commit, "failed to commit %d frames\n", frames);
1269 return commit;
1270 }
1271
1272 offset += commit;
1273 count += commit;
1274 size -= commit;
1275 }
1276 return count;
1277}
1278
Liam Girdwood6be28f12011-10-13 12:59:51 -07001279int pcm_avail_update(struct pcm *pcm)
1280{
Ricardo Biehl Pasqualib00f90a2018-12-22 12:51:21 -02001281 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_AVAIL_MIN);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001282 return pcm_mmap_avail(pcm);
1283}
1284
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001285/** Returns available frames in pcm buffer and corresponding time stamp.
1286 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
1287 * otherwise the clock is CLOCK_REALTIME.
1288 * For an input stream, frames available are frames ready for the application to read.
1289 * For an output stream, frames available are the number of empty frames available for the application to write.
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001290 * @param pcm A PCM handle.
1291 * @param avail The number of available frames
1292 * @param tstamp The timestamp
1293 * @return On success, zero is returned; on failure, negative one.
1294 */
1295int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
1296 struct timespec *tstamp)
1297{
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001298 int checking;
1299 int tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001300
1301 if (!pcm_is_ready(pcm))
1302 return -1;
1303
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001304 checking = 0;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001305
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001306again:
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001307
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001308 tmp = pcm_avail_update(pcm);
1309 if (tmp < 0)
1310 return tmp; /* error */
1311
1312 if (checking && (unsigned int) tmp == *avail)
1313 return 0;
1314
1315 *avail = (unsigned int) tmp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001316 *tstamp = pcm->mmap_status->tstamp;
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001317
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001318 /*
1319 * When status is mmaped, get avail again to ensure
1320 * valid timestamp.
1321 */
1322 if (!pcm->sync_ptr) {
1323 checking = 1;
1324 goto again;
1325 }
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001326
Ricardo Biehl Pasquali7bdb05d2018-12-22 14:42:57 -02001327 /* SYNC_PTR ioctl was used, no need to check avail */
Ricardo Biehl Pasquali19f84c32018-12-24 12:40:31 -02001328 return 0;
1329}
1330
Liam Girdwood6be28f12011-10-13 12:59:51 -07001331int pcm_state(struct pcm *pcm)
1332{
1333 int err = pcm_sync_ptr(pcm, 0);
1334 if (err < 0)
1335 return err;
1336
1337 return pcm->mmap_status->state;
1338}
1339
Taylor Holberton17a10242016-11-23 13:18:24 -08001340/** Waits for frames to be available for read or write operations.
1341 * @param pcm A PCM handle.
1342 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1343 * @returns If frames became available, one is returned.
1344 * If a timeout occured, zero is returned.
1345 * If an error occured, a negative number is returned.
1346 * @ingroup libtinyalsa-pcm
1347 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001348int pcm_wait(struct pcm *pcm, int timeout)
1349{
1350 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001351 int err;
1352
1353 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001354 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001355
1356 do {
1357 /* let's wait for avail or timeout */
1358 err = poll(&pfd, 1, timeout);
1359 if (err < 0)
1360 return -errno;
1361
1362 /* timeout ? */
1363 if (err == 0)
1364 return 0;
1365
1366 /* have we been interrupted ? */
1367 if (errno == -EINTR)
1368 continue;
1369
1370 /* check for any errors */
1371 if (pfd.revents & (POLLERR | POLLNVAL)) {
1372 switch (pcm_state(pcm)) {
1373 case PCM_STATE_XRUN:
1374 return -EPIPE;
1375 case PCM_STATE_SUSPENDED:
1376 return -ESTRPIPE;
1377 case PCM_STATE_DISCONNECTED:
1378 return -ENODEV;
1379 default:
1380 return -EIO;
1381 }
1382 }
1383 /* poll again if fd not ready for IO */
1384 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1385
1386 return 1;
1387}
1388
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001389int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001390{
1391 int err = 0, frames, avail;
1392 unsigned int offset = 0, count;
1393
1394 if (bytes == 0)
1395 return 0;
1396
1397 count = pcm_bytes_to_frames(pcm, bytes);
1398
1399 while (count > 0) {
1400
1401 /* get the available space for writing new frames */
1402 avail = pcm_avail_update(pcm);
1403 if (avail < 0) {
1404 fprintf(stderr, "cannot determine available mmap frames");
1405 return err;
1406 }
1407
1408 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001409 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001410 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1411 if (pcm_start(pcm) < 0) {
1412 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1413 (unsigned int)pcm->mmap_status->hw_ptr,
1414 (unsigned int)pcm->mmap_control->appl_ptr,
1415 avail);
1416 return -errno;
1417 }
1418 }
1419
1420 /* sleep until we have space to write new frames */
1421 if (pcm->running &&
1422 (unsigned int)avail < pcm->mmap_control->avail_min) {
1423 int time = -1;
1424
1425 if (pcm->flags & PCM_NOIRQ)
1426 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1427 / pcm->noirq_frames_per_msec;
1428
1429 err = pcm_wait(pcm, time);
1430 if (err < 0) {
1431 pcm->running = 0;
1432 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1433 (unsigned int)pcm->mmap_status->hw_ptr,
1434 (unsigned int)pcm->mmap_control->appl_ptr,
1435 avail);
1436 pcm->mmap_control->appl_ptr = 0;
1437 return err;
1438 }
1439 continue;
1440 }
1441
1442 frames = count;
1443 if (frames > avail)
1444 frames = avail;
1445
1446 if (!frames)
1447 break;
1448
1449 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001450 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001451 if (frames < 0) {
1452 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1453 (unsigned int)pcm->mmap_status->hw_ptr,
1454 (unsigned int)pcm->mmap_control->appl_ptr,
1455 avail);
1456 return frames;
1457 }
1458
1459 offset += frames;
1460 count -= frames;
1461 }
1462
Liam Girdwood6be28f12011-10-13 12:59:51 -07001463 return 0;
1464}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001465
1466int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1467{
1468 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1469 return -ENOSYS;
1470
1471 return pcm_mmap_transfer(pcm, (void *)data, count);
1472}
1473
1474int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1475{
1476 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1477 return -ENOSYS;
1478
1479 return pcm_mmap_transfer(pcm, data, count);
1480}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301481
Taylor Holberton17a10242016-11-23 13:18:24 -08001482/** Gets the delay of the PCM, in terms of frames.
1483 * @param pcm A PCM handle.
1484 * @returns On success, the delay of the PCM.
1485 * On failure, a negative number.
1486 * @ingroup libtinyalsa-pcm
1487 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301488long pcm_get_delay(struct pcm *pcm)
1489{
1490 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1491 return -1;
1492
1493 return pcm->pcm_delay;
1494}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001495