blob: 75fc85c9605cc21b22086586a53c7a3e772ee46d [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>
Simon Wilson79d39652011-05-25 13:44:23 -070051
52#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070053#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070054
55static inline int param_is_mask(int p)
56{
57 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
58 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
59}
60
61static inline int param_is_interval(int p)
62{
63 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
64 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
65}
66
Taylor Holberton2f387d22016-12-01 15:58:16 -080067static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
68{
69 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
70}
71
Simon Wilson79d39652011-05-25 13:44:23 -070072static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
73{
74 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
75}
76
77static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
78{
79 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
80}
81
82static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
83{
84 if (bit >= SNDRV_MASK_MAX)
85 return;
86 if (param_is_mask(n)) {
87 struct snd_mask *m = param_to_mask(p, n);
88 m->bits[0] = 0;
89 m->bits[1] = 0;
90 m->bits[bit >> 5] |= (1 << (bit & 31));
91 }
92}
93
94static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
95{
96 if (param_is_interval(n)) {
97 struct snd_interval *i = param_to_interval(p, n);
98 i->min = val;
99 }
100}
101
Taylor Holberton2f387d22016-12-01 15:58:16 -0800102static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700103{
104 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800105 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700106 return i->min;
107 }
108 return 0;
109}
110
Taylor Holberton2f387d22016-12-01 15:58:16 -0800111static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700112{
113 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800114 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700115 return i->max;
116 }
117 return 0;
118}
119
Simon Wilson79d39652011-05-25 13:44:23 -0700120static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
121{
122 if (param_is_interval(n)) {
123 struct snd_interval *i = param_to_interval(p, n);
124 i->min = val;
125 i->max = val;
126 i->integer = 1;
127 }
128}
129
Liam Girdwood6be28f12011-10-13 12:59:51 -0700130static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
131{
132 if (param_is_interval(n)) {
133 struct snd_interval *i = param_to_interval(p, n);
134 if (i->integer)
135 return i->max;
136 }
137 return 0;
138}
139
Simon Wilson79d39652011-05-25 13:44:23 -0700140static void param_init(struct snd_pcm_hw_params *p)
141{
142 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700143
Simon Wilson79d39652011-05-25 13:44:23 -0700144 memset(p, 0, sizeof(*p));
145 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
146 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
147 struct snd_mask *m = param_to_mask(p, n);
148 m->bits[0] = ~0;
149 m->bits[1] = ~0;
150 }
151 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
152 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
153 struct snd_interval *i = param_to_interval(p, n);
154 i->min = 0;
155 i->max = ~0;
156 }
Simon Wilson43544882012-10-31 12:52:39 -0700157 p->rmask = ~0U;
158 p->cmask = 0;
159 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700160}
161
162#define PCM_ERROR_MAX 128
163
Taylor Holberton6d58e012016-10-01 18:32:30 -0400164/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800165 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400166 */
Simon Wilson79d39652011-05-25 13:44:23 -0700167struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400168 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700169 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400170 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700171 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400172 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700173 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400174 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530175 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400176 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700177 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400178 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700179 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800180 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700181 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400182 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700183 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400184 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700185 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700186 struct snd_pcm_mmap_status *mmap_status;
187 struct snd_pcm_mmap_control *mmap_control;
188 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700189 void *mmap_buffer;
190 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800191 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530192 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800193 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200194 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700195};
196
Taylor Holberton6d58e012016-10-01 18:32:30 -0400197/** Gets the buffer size of the PCM.
198 * @param pcm A PCM handle.
199 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800200 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400201 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800202unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700203{
204 return pcm->buffer_size;
205}
206
Taylor Holberton77979a82016-12-01 20:04:04 -0800207/** Gets the channel count of the PCM.
208 * @param pcm A PCM handle.
209 * @return The channel count of the PCM.
210 * @ingroup libtinyalsa-pcm
211 */
212unsigned int pcm_get_channels(const struct pcm *pcm)
213{
214 return pcm->config.channels;
215}
216
217/** Gets the rate of the PCM.
218 * The rate is given in frames per second.
219 * @param pcm A PCM handle.
220 * @return The rate of the PCM.
221 * @ingroup libtinyalsa-pcm
222 */
223unsigned int pcm_get_rate(const struct pcm *pcm)
224{
225 return pcm->config.rate;
226}
227
228/** Gets the format of the PCM.
229 * @param pcm A PCM handle.
230 * @return The format of the PCM.
231 * @ingroup libtinyalsa-pcm
232 */
233enum pcm_format pcm_get_format(const struct pcm *pcm)
234{
235 return pcm->config.format;
236}
237
Taylor Holberton6d58e012016-10-01 18:32:30 -0400238/** Gets the file descriptor of the PCM.
239 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800240 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400241 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800242 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400243 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800244int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400245{
246 return pcm->fd;
247}
248
Taylor Holberton6d58e012016-10-01 18:32:30 -0400249/** Gets the error message for the last error that occured.
250 * If no error occured and this function is called, the results are undefined.
251 * @param pcm A PCM handle.
252 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800253 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400254 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800255const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700256{
257 return pcm->error;
258}
259
Taylor Holberton6d58e012016-10-01 18:32:30 -0400260/** Gets the subdevice on which the pcm has been opened.
261 * @param pcm A PCM handle.
262 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800263unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200264{
265 return pcm->subdevice;
266}
267
Simon Wilson79d39652011-05-25 13:44:23 -0700268static int oops(struct pcm *pcm, int e, const char *fmt, ...)
269{
270 va_list ap;
271 int sz;
272
273 va_start(ap, fmt);
274 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
275 va_end(ap);
276 sz = strlen(pcm->error);
277
278 if (errno)
279 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
280 ": %s", strerror(e));
281 return -1;
282}
283
Simon Wilsonbc03b622011-06-15 17:19:01 -0700284static unsigned int pcm_format_to_alsa(enum pcm_format format)
285{
286 switch (format) {
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400287
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500288 case PCM_FORMAT_S8:
289 return SNDRV_PCM_FORMAT_S8;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400290
Simon Wilsonbc03b622011-06-15 17:19:01 -0700291 default:
292 case PCM_FORMAT_S16_LE:
293 return SNDRV_PCM_FORMAT_S16_LE;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400294 case PCM_FORMAT_S16_BE:
295 return SNDRV_PCM_FORMAT_S16_BE;
296
297 case PCM_FORMAT_S24_LE:
298 return SNDRV_PCM_FORMAT_S24_LE;
299 case PCM_FORMAT_S24_BE:
300 return SNDRV_PCM_FORMAT_S24_BE;
301
302 case PCM_FORMAT_S24_3LE:
303 return SNDRV_PCM_FORMAT_S24_3LE;
304 case PCM_FORMAT_S24_3BE:
305 return SNDRV_PCM_FORMAT_S24_3BE;
306
307 case PCM_FORMAT_S32_LE:
308 return SNDRV_PCM_FORMAT_S32_LE;
309 case PCM_FORMAT_S32_BE:
310 return SNDRV_PCM_FORMAT_S32_BE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700311 };
312}
313
Taylor Holberton6d58e012016-10-01 18:32:30 -0400314/** Determines the number of bits occupied by a @ref pcm_format.
315 * @param format A PCM format.
316 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800317 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400318 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700319unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700320{
321 switch (format) {
322 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400323 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700324 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400325 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700326 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400327 case PCM_FORMAT_S24_3LE:
328 case PCM_FORMAT_S24_3BE:
329 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700330 default:
331 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400332 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700333 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400334 case PCM_FORMAT_S8:
335 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700336 };
337}
338
Taylor Holberton6d58e012016-10-01 18:32:30 -0400339/** Determines how many frames of a PCM can fit into a number of bytes.
340 * @param pcm A PCM handle.
341 * @param bytes The number of bytes.
342 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800343 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400344 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800345unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700346{
347 return bytes / (pcm->config.channels *
348 (pcm_format_to_bits(pcm->config.format) >> 3));
349}
350
Taylor Holberton6d58e012016-10-01 18:32:30 -0400351/** Determines how many bytes are occupied by a number of frames of a PCM.
352 * @param pcm A PCM handle.
353 * @param frames The number of frames of a PCM.
354 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800355 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400356 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800357unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700358{
359 return frames * pcm->config.channels *
360 (pcm_format_to_bits(pcm->config.format) >> 3);
361}
362
Taylor Holberton4f556062016-09-16 09:54:36 -0400363static int pcm_sync_ptr(struct pcm *pcm, int flags)
364{
Eric Laurent40b018e2011-06-18 10:10:23 -0700365 if (pcm->sync_ptr) {
366 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800367 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
368 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700369 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800370 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800371 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700372 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800373 return -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700374}
375
Taylor Holberton4f556062016-09-16 09:54:36 -0400376static int pcm_hw_mmap_status(struct pcm *pcm)
377{
Eric Laurent40b018e2011-06-18 10:10:23 -0700378 if (pcm->sync_ptr)
379 return 0;
380
381 int page_size = sysconf(_SC_PAGE_SIZE);
382 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
383 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
384 if (pcm->mmap_status == MAP_FAILED)
385 pcm->mmap_status = NULL;
386 if (!pcm->mmap_status)
387 goto mmap_error;
388
389 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
390 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
391 if (pcm->mmap_control == MAP_FAILED)
392 pcm->mmap_control = NULL;
393 if (!pcm->mmap_control) {
394 munmap(pcm->mmap_status, page_size);
395 pcm->mmap_status = NULL;
396 goto mmap_error;
397 }
398 pcm->mmap_control->avail_min = 1;
399
400 return 0;
401
402mmap_error:
403
404 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
405 if (!pcm->sync_ptr)
406 return -ENOMEM;
407 pcm->mmap_status = &pcm->sync_ptr->s.status;
408 pcm->mmap_control = &pcm->sync_ptr->c.control;
409 pcm->mmap_control->avail_min = 1;
410 pcm_sync_ptr(pcm, 0);
411
412 return 0;
413}
414
415static void pcm_hw_munmap_status(struct pcm *pcm) {
416 if (pcm->sync_ptr) {
417 free(pcm->sync_ptr);
418 pcm->sync_ptr = NULL;
419 } else {
420 int page_size = sysconf(_SC_PAGE_SIZE);
421 if (pcm->mmap_status)
422 munmap(pcm->mmap_status, page_size);
423 if (pcm->mmap_control)
424 munmap(pcm->mmap_control, page_size);
425 }
426 pcm->mmap_status = NULL;
427 pcm->mmap_control = NULL;
428}
429
Liam Girdwood6be28f12011-10-13 12:59:51 -0700430static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700431 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700432 unsigned int frames)
433{
434 int size_bytes = pcm_frames_to_bytes(pcm, frames);
435 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
436 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
437
438 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700439 if (pcm->flags & PCM_IN)
440 memcpy(buf + src_offset_bytes,
441 (char*)pcm->mmap_buffer + pcm_offset_bytes,
442 size_bytes);
443 else
444 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
445 buf + src_offset_bytes,
446 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700447 return 0;
448}
449
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700450static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700451 unsigned int offset, unsigned int size)
452{
453 void *pcm_areas;
454 int commit;
455 unsigned int pcm_offset, frames, count = 0;
456
457 while (size > 0) {
458 frames = size;
459 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700460 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700461 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
462 if (commit < 0) {
463 oops(pcm, commit, "failed to commit %d frames\n", frames);
464 return commit;
465 }
466
467 offset += commit;
468 count += commit;
469 size -= commit;
470 }
471 return count;
472}
473
Taylor Holberton6d58e012016-10-01 18:32:30 -0400474/** Returns available frames in pcm buffer and corresponding time stamp.
475 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
476 * otherwise the clock is CLOCK_REALTIME.
477 * For an input stream, frames available are frames ready for the application to read.
478 * For an output stream, frames available are the number of empty frames available for the application to write.
479 * Only available for PCMs opened with the @ref PCM_MMAP flag.
480 * @param pcm A PCM handle.
481 * @param avail The number of available frames
482 * @param tstamp The timestamp
483 * @return On success, zero is returned; on failure, negative one.
484 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700485int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
486 struct timespec *tstamp)
487{
488 int frames;
489 int rc;
490 snd_pcm_uframes_t hw_ptr;
491
492 if (!pcm_is_ready(pcm))
493 return -1;
494
495 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
496 if (rc < 0)
497 return -1;
498
Eric Laurent7db48582011-11-17 11:47:59 -0800499 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
500 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800501 return -1;
502
Eric Laurent40b018e2011-06-18 10:10:23 -0700503 *tstamp = pcm->mmap_status->tstamp;
504 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
505 return -1;
506
507 hw_ptr = pcm->mmap_status->hw_ptr;
508 if (pcm->flags & PCM_IN)
509 frames = hw_ptr - pcm->mmap_control->appl_ptr;
510 else
511 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
512
513 if (frames < 0)
514 return -1;
515
516 *avail = (unsigned int)frames;
517
518 return 0;
519}
520
Taylor Holberton6d58e012016-10-01 18:32:30 -0400521/** Writes audio samples to PCM.
522 * If the PCM has not been started, it is started in this function.
523 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
524 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
525 * @param pcm A PCM handle.
526 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800527 * @param frame_count The number of frames occupied by the sample array.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800528 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800529 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400530 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800531int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700532{
533 struct snd_xferi x;
534
535 if (pcm->flags & PCM_IN)
536 return -EINVAL;
537
Mark Brown6bbe77a2012-02-10 22:07:09 +0000538 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800539 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800540 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700541 for (;;) {
542 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530543 int prepare_error = pcm_prepare(pcm);
544 if (prepare_error)
545 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700546 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
547 return oops(pcm, errno, "cannot write initial data");
548 pcm->running = 1;
549 return 0;
550 }
551 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530552 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700553 pcm->running = 0;
554 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700555 /* we failed to make our window -- try to restart if we are
556 * allowed to do so. Otherwise, simply allow the EPIPE error to
557 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700558 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700559 if (pcm->flags & PCM_NORESTART)
560 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700561 continue;
562 }
563 return oops(pcm, errno, "cannot write stream data");
564 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800565 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700566 }
567}
568
Taylor Holberton6d58e012016-10-01 18:32:30 -0400569/** Reads audio samples from PCM.
570 * If the PCM has not been started, it is started in this function.
571 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
572 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
573 * @param pcm A PCM handle.
574 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800575 * @param frame_count The number of frames occupied by the sample array.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800576 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800577 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400578 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800579int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700580{
581 struct snd_xferi x;
582
583 if (!(pcm->flags & PCM_IN))
584 return -EINVAL;
585
586 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800587 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800588 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700589 for (;;) {
590 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700591 if (pcm_start(pcm) < 0) {
592 fprintf(stderr, "start error");
593 return -errno;
594 }
Simon Wilson79d39652011-05-25 13:44:23 -0700595 }
596 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530597 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700598 pcm->running = 0;
599 if (errno == EPIPE) {
600 /* we failed to make our window -- try to restart */
601 pcm->underruns++;
602 continue;
603 }
604 return oops(pcm, errno, "cannot read stream data");
605 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800606 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700607 }
608}
609
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800610/** Writes audio samples to PCM.
611 * If the PCM has not been started, it is started in this function.
612 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
613 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
614 * @param pcm A PCM handle.
615 * @param data The audio sample array
616 * @param count The number of bytes occupied by the sample array.
617 * @return On success, this function returns zero; otherwise, a negative number.
618 * @deprecated
619 * @ingroup libtinyalsa-pcm
620 */
621int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
622{
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800623 int ret = pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800624 if (ret < 0){
625 return ret;
626 }
627 return 0;
628}
629
630/** Reads audio samples from PCM.
631 * If the PCM has not been started, it is started in this function.
632 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
633 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
634 * @param pcm A PCM handle.
635 * @param data The audio sample array
636 * @param count The number of bytes occupied by the sample array.
637 * @return On success, this function returns zero; otherwise, a negative number.
638 * @deprecated
639 * @ingroup libtinyalsa-pcm
640 */
641int pcm_read(struct pcm *pcm, void *data, unsigned int count)
642{
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800643 int ret = pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800644 if (ret < 0) {
645 return ret;
646 }
647 return 0;
648}
649
Simon Wilson79d39652011-05-25 13:44:23 -0700650static struct pcm bad_pcm = {
651 .fd = -1,
652};
653
Taylor Holberton6d58e012016-10-01 18:32:30 -0400654/** Gets the hardware parameters of a PCM, without created a PCM handle.
655 * @param card The card of the PCM.
656 * The default card is zero.
657 * @param device The device of the PCM.
658 * The default device is zero.
659 * @param flags Specifies whether the PCM is an input or output.
660 * May be one of the following:
661 * - @ref PCM_IN
662 * - @ref PCM_OUT
663 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800664 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400665 */
Simon Wilson43544882012-10-31 12:52:39 -0700666struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
667 unsigned int flags)
668{
669 struct snd_pcm_hw_params *params;
670 char fn[256];
671 int fd;
672
673 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
674 flags & PCM_IN ? 'c' : 'p');
675
676 fd = open(fn, O_RDWR);
677 if (fd < 0) {
678 fprintf(stderr, "cannot open device '%s'\n", fn);
679 goto err_open;
680 }
681
682 params = calloc(1, sizeof(struct snd_pcm_hw_params));
683 if (!params)
684 goto err_calloc;
685
686 param_init(params);
687 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
688 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
689 goto err_hw_refine;
690 }
691
692 close(fd);
693
694 return (struct pcm_params *)params;
695
696err_hw_refine:
697 free(params);
698err_calloc:
699 close(fd);
700err_open:
701 return NULL;
702}
703
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500704/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400705 * @param pcm_params Hardware parameters of a PCM.
706 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800707 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400708 */
Simon Wilson43544882012-10-31 12:52:39 -0700709void pcm_params_free(struct pcm_params *pcm_params)
710{
711 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
712
713 if (params)
714 free(params);
715}
716
717static int pcm_param_to_alsa(enum pcm_param param)
718{
719 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700720 case PCM_PARAM_ACCESS:
721 return SNDRV_PCM_HW_PARAM_ACCESS;
722 case PCM_PARAM_FORMAT:
723 return SNDRV_PCM_HW_PARAM_FORMAT;
724 case PCM_PARAM_SUBFORMAT:
725 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700726 case PCM_PARAM_SAMPLE_BITS:
727 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
728 break;
729 case PCM_PARAM_FRAME_BITS:
730 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
731 break;
732 case PCM_PARAM_CHANNELS:
733 return SNDRV_PCM_HW_PARAM_CHANNELS;
734 break;
735 case PCM_PARAM_RATE:
736 return SNDRV_PCM_HW_PARAM_RATE;
737 break;
738 case PCM_PARAM_PERIOD_TIME:
739 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
740 break;
741 case PCM_PARAM_PERIOD_SIZE:
742 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
743 break;
744 case PCM_PARAM_PERIOD_BYTES:
745 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
746 break;
747 case PCM_PARAM_PERIODS:
748 return SNDRV_PCM_HW_PARAM_PERIODS;
749 break;
750 case PCM_PARAM_BUFFER_TIME:
751 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
752 break;
753 case PCM_PARAM_BUFFER_SIZE:
754 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
755 break;
756 case PCM_PARAM_BUFFER_BYTES:
757 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
758 break;
759 case PCM_PARAM_TICK_TIME:
760 return SNDRV_PCM_HW_PARAM_TICK_TIME;
761 break;
762
763 default:
764 return -1;
765 }
766}
767
Taylor Holberton6d58e012016-10-01 18:32:30 -0400768/** Gets a mask from a PCM's hardware parameters.
769 * @param pcm_params A PCM's hardware parameters.
770 * @param param The parameter to get.
771 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
772 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800773 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400774 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800775const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700776 enum pcm_param param)
777{
778 int p;
779 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
780 if (params == NULL) {
781 return NULL;
782 }
783
784 p = pcm_param_to_alsa(param);
785 if (p < 0 || !param_is_mask(p)) {
786 return NULL;
787 }
788
Taylor Holberton2f387d22016-12-01 15:58:16 -0800789 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700790}
791
Taylor Holberton17a10242016-11-23 13:18:24 -0800792/** Get the minimum of a specified PCM parameter.
793 * @param pcm_params A PCM parameters structure.
794 * @param param The specified parameter to get the minimum of.
795 * @returns On success, the parameter minimum.
796 * On failure, zero.
797 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800798unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700799 enum pcm_param param)
800{
801 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
802 int p;
803
804 if (!params)
805 return 0;
806
807 p = pcm_param_to_alsa(param);
808 if (p < 0)
809 return 0;
810
811 return param_get_min(params, p);
812}
813
Taylor Holberton17a10242016-11-23 13:18:24 -0800814/** Get the maximum of a specified PCM parameter.
815 * @param pcm_params A PCM parameters structure.
816 * @param param The specified parameter to get the maximum of.
817 * @returns On success, the parameter maximum.
818 * On failure, zero.
819 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800820unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700821 enum pcm_param param)
822{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800823 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700824 int p;
825
826 if (!params)
827 return 0;
828
829 p = pcm_param_to_alsa(param);
830 if (p < 0)
831 return 0;
832
833 return param_get_max(params, p);
834}
835
Taylor Holberton6d58e012016-10-01 18:32:30 -0400836/** Closes a PCM returned by @ref pcm_open.
837 * @param pcm A PCM returned by @ref pcm_open.
838 * May not be NULL.
839 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800840 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400841 */
Simon Wilson79d39652011-05-25 13:44:23 -0700842int pcm_close(struct pcm *pcm)
843{
844 if (pcm == &bad_pcm)
845 return 0;
846
Eric Laurent40b018e2011-06-18 10:10:23 -0700847 pcm_hw_munmap_status(pcm);
848
Liam Girdwood6be28f12011-10-13 12:59:51 -0700849 if (pcm->flags & PCM_MMAP) {
850 pcm_stop(pcm);
851 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
852 }
853
Simon Wilson79d39652011-05-25 13:44:23 -0700854 if (pcm->fd >= 0)
855 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530856 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700857 pcm->running = 0;
858 pcm->buffer_size = 0;
859 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700860 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700861 return 0;
862}
863
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800864/** Opens a PCM by it's name.
865 * @param name The name of the PCM.
866 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
867 * @param flags Specify characteristics and functionality about the pcm.
868 * May be a bitwise AND of the following:
869 * - @ref PCM_IN
870 * - @ref PCM_OUT
871 * - @ref PCM_MMAP
872 * - @ref PCM_NOIRQ
873 * - @ref PCM_MONOTONIC
874 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800875 * @returns A PCM structure.
876 * If an error occurs allocating memory for the PCM, NULL is returned.
877 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
878 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800879 * @ingroup libtinyalsa-pcm
880 */
881struct pcm *pcm_open_by_name(const char *name,
882 unsigned int flags,
883 const struct pcm_config *config)
884{
885 unsigned int card, device;
886 if ((name[0] != 'h')
887 || (name[1] != 'w')
888 || (name[2] != ':')) {
889 return NULL;
890 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
891 return NULL;
892 }
893 return pcm_open(card, device, flags, config);
894}
895
Taylor Holberton6d58e012016-10-01 18:32:30 -0400896/** Opens a PCM.
897 * @param card The card that the pcm belongs to.
898 * The default card is zero.
899 * @param device The device that the pcm belongs to.
900 * The default device is zero.
901 * @param flags Specify characteristics and functionality about the pcm.
902 * May be a bitwise AND of the following:
903 * - @ref PCM_IN
904 * - @ref PCM_OUT
905 * - @ref PCM_MMAP
906 * - @ref PCM_NOIRQ
907 * - @ref PCM_MONOTONIC
908 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800909 * @returns A PCM structure.
910 * If an error occurs allocating memory for the PCM, NULL is returned.
911 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
912 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800913 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400914 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700915struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800916 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700917{
Simon Wilson79d39652011-05-25 13:44:23 -0700918 struct pcm *pcm;
919 struct snd_pcm_info info;
920 struct snd_pcm_hw_params params;
921 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700922 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700923 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700924
925 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400926 if (!pcm)
927 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700928
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400929 if (config == NULL) {
930 config = &pcm->config;
Taylor Holberton94803b02016-12-01 16:07:14 -0800931 pcm->config.channels = 2;
932 pcm->config.rate = 48000;
933 pcm->config.period_size = 1024;
934 pcm->config.period_count = 4;
935 pcm->config.format = PCM_FORMAT_S16_LE;
936 pcm->config.start_threshold = config->period_count * config->period_size;
937 pcm->config.stop_threshold = config->period_count * config->period_size;
938 pcm->config.silence_threshold = 0;
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400939 } else {
940 pcm->config = *config;
941 }
Simon Wilson79d39652011-05-25 13:44:23 -0700942
Simon Wilson1bd580f2011-06-02 15:58:41 -0700943 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
944 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700945
946 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700947 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700948 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700949 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700950 return pcm;
951 }
952
953 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700954 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700955 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700956 }
David Wagner4cddf192014-04-02 15:12:54 +0200957 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700958
959 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700960 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
961 pcm_format_to_alsa(config->format));
962 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
963 SNDRV_PCM_SUBFORMAT_STD);
964 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
965 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
966 pcm_format_to_bits(config->format));
967 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
968 pcm_format_to_bits(config->format) * config->channels);
969 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
970 config->channels);
971 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
972 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
973
Liam Girdwood6be28f12011-10-13 12:59:51 -0700974 if (flags & PCM_NOIRQ) {
975
976 if (!(flags & PCM_MMAP)) {
977 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
978 goto fail;
979 }
980
981 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
982 pcm->noirq_frames_per_msec = config->rate / 1000;
983 }
984
985 if (flags & PCM_MMAP)
986 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
987 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
988 else
989 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
990 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
991
Simon Wilson79d39652011-05-25 13:44:23 -0700992 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
993 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700994 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700995 }
996
Liam Girdwood6be28f12011-10-13 12:59:51 -0700997 /* get our refined hw_params */
Taylor Holberton94803b02016-12-01 16:07:14 -0800998 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
999 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001000 pcm->buffer_size = config->period_count * config->period_size;
1001
1002 if (flags & PCM_MMAP) {
1003 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
1004 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
1005 if (pcm->mmap_buffer == MAP_FAILED) {
1006 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
1007 pcm_frames_to_bytes(pcm, pcm->buffer_size));
1008 goto fail_close;
1009 }
1010 }
1011
1012
Simon Wilson79d39652011-05-25 13:44:23 -07001013 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -07001014 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -07001015 sparams.period_step = 1;
1016 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -07001017
Eric Laurent93e7b672012-08-22 16:18:14 -07001018 if (!config->start_threshold) {
1019 if (pcm->flags & PCM_IN)
1020 pcm->config.start_threshold = sparams.start_threshold = 1;
1021 else
1022 pcm->config.start_threshold = sparams.start_threshold =
1023 config->period_count * config->period_size / 2;
1024 } else
John Grossman3bb114a2011-07-21 10:59:55 -07001025 sparams.start_threshold = config->start_threshold;
1026
Liam Girdwood6be28f12011-10-13 12:59:51 -07001027 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -08001028 if (!config->stop_threshold) {
1029 if (pcm->flags & PCM_IN)
1030 pcm->config.stop_threshold = sparams.stop_threshold =
1031 config->period_count * config->period_size * 10;
1032 else
1033 pcm->config.stop_threshold = sparams.stop_threshold =
1034 config->period_count * config->period_size;
1035 }
John Grossman3bb114a2011-07-21 10:59:55 -07001036 else
1037 sparams.stop_threshold = config->stop_threshold;
1038
Simon Wilson79d39652011-05-25 13:44:23 -07001039 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
1040 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -07001041 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001042 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -07001043
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -06001044 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001045 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -07001046
1047 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
1048 oops(pcm, errno, "cannot set sw params");
1049 goto fail;
1050 }
1051
Eric Laurent40b018e2011-06-18 10:10:23 -07001052 rc = pcm_hw_mmap_status(pcm);
1053 if (rc < 0) {
1054 oops(pcm, rc, "mmap status failed");
1055 goto fail;
1056 }
1057
Glenn Kasten81012402013-08-22 15:11:48 -07001058#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1059 if (pcm->flags & PCM_MONOTONIC) {
1060 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1061 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1062 if (rc < 0) {
1063 oops(pcm, rc, "cannot set timestamp type");
1064 goto fail;
1065 }
1066 }
1067#endif
1068
Simon Wilson79d39652011-05-25 13:44:23 -07001069 pcm->underruns = 0;
1070 return pcm;
1071
1072fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001073 if (flags & PCM_MMAP)
1074 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1075fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001076 close(pcm->fd);
1077 pcm->fd = -1;
1078 return pcm;
1079}
1080
Taylor Holberton6d58e012016-10-01 18:32:30 -04001081/** Checks if a PCM file has been opened without error.
1082 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001083 * May be NULL.
1084 * @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 -04001085 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001086 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001087 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001088int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001089{
Taylor Holbertone123a652017-01-13 21:39:48 -08001090 if (pcm != NULL) {
1091 return pcm->fd >= 0;
1092 }
1093 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001094}
Simon Wilsond6458e62011-06-21 14:58:11 -07001095
Taylor Holberton558e5942016-12-04 13:42:28 -08001096/** Links two PCMs.
1097 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1098 * If an error occurs, the error message will be written to @p pcm1.
1099 * @param pcm1 A PCM handle.
1100 * @param pcm2 Another PCM handle.
1101 * @return On success, zero; on failure, a negative number.
1102 * @ingroup libtinyalsa-pcm
1103 */
1104int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1105{
1106 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1107 if (err == -1) {
1108 return oops(pcm1, errno, "cannot link PCM");
1109 }
1110 return 0;
1111}
1112
1113/** Unlinks a PCM.
1114 * @see @ref pcm_link
1115 * @param pcm A PCM handle.
1116 * @return On success, zero; on failure, a negative number.
1117 * @ingroup libtinyalsa-pcm
1118 */
1119int pcm_unlink(struct pcm *pcm)
1120{
1121 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1122 if (err == -1) {
1123 return oops(pcm, errno, "cannot unlink PCM");
1124 }
1125 return 0;
1126}
1127
Taylor Holberton6d58e012016-10-01 18:32:30 -04001128/** Prepares a PCM, if it has not been prepared already.
1129 * @param pcm A PCM handle.
1130 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001131 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001132 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301133int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001134{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301135 if (pcm->prepared)
1136 return 0;
1137
Simon Wilsond6458e62011-06-21 14:58:11 -07001138 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1139 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001140
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301141 pcm->prepared = 1;
1142 return 0;
1143}
1144
Taylor Holberton6d58e012016-10-01 18:32:30 -04001145/** Starts a PCM.
1146 * If the PCM has not been prepared,
1147 * it is prepared in this function.
1148 * @param pcm A PCM handle.
1149 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001150 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001151 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301152int pcm_start(struct pcm *pcm)
1153{
1154 int prepare_error = pcm_prepare(pcm);
1155 if (prepare_error)
1156 return prepare_error;
1157
Liam Girdwood6be28f12011-10-13 12:59:51 -07001158 if (pcm->flags & PCM_MMAP)
1159 pcm_sync_ptr(pcm, 0);
1160
Simon Wilsond6458e62011-06-21 14:58:11 -07001161 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1162 return oops(pcm, errno, "cannot start channel");
1163
Liam Girdwood6be28f12011-10-13 12:59:51 -07001164 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001165 return 0;
1166}
1167
Taylor Holberton6d58e012016-10-01 18:32:30 -04001168/** Stops a PCM.
1169 * @param pcm A PCM handle.
1170 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001171 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001172 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001173int pcm_stop(struct pcm *pcm)
1174{
1175 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1176 return oops(pcm, errno, "cannot stop channel");
1177
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301178 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001179 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001180 return 0;
1181}
1182
Liam Girdwood6be28f12011-10-13 12:59:51 -07001183static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1184{
1185 int avail;
1186
1187 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1188
1189 if (avail < 0)
1190 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001191 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001192 avail -= pcm->boundary;
1193
1194 return avail;
1195}
1196
1197static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1198{
1199 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1200 if (avail < 0)
1201 avail += pcm->boundary;
1202 return avail;
1203}
1204
1205static inline int pcm_mmap_avail(struct pcm *pcm)
1206{
1207 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1208 if (pcm->flags & PCM_IN)
1209 return pcm_mmap_capture_avail(pcm);
1210 else
1211 return pcm_mmap_playback_avail(pcm);
1212}
1213
1214static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1215{
1216 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1217 appl_ptr += frames;
1218
1219 /* check for boundary wrap */
1220 if (appl_ptr > pcm->boundary)
1221 appl_ptr -= pcm->boundary;
1222 pcm->mmap_control->appl_ptr = appl_ptr;
1223}
1224
1225int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1226 unsigned int *frames)
1227{
1228 unsigned int continuous, copy_frames, avail;
1229
1230 /* return the mmap buffer */
1231 *areas = pcm->mmap_buffer;
1232
1233 /* and the application offset in frames */
1234 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1235
1236 avail = pcm_mmap_avail(pcm);
1237 if (avail > pcm->buffer_size)
1238 avail = pcm->buffer_size;
1239 continuous = pcm->buffer_size - *offset;
1240
1241 /* we can only copy frames if the are availabale and continuos */
1242 copy_frames = *frames;
1243 if (copy_frames > avail)
1244 copy_frames = avail;
1245 if (copy_frames > continuous)
1246 copy_frames = continuous;
1247 *frames = copy_frames;
1248
1249 return 0;
1250}
1251
1252int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1253{
Taylor Holberton72e44222016-11-22 09:54:47 -08001254 int ret;
1255
Taylor Holberton73466c02016-10-01 12:51:59 -04001256 /* not used */
1257 (void) offset;
1258
Liam Girdwood6be28f12011-10-13 12:59:51 -07001259 /* update the application pointer in userspace and kernel */
1260 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001261 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001262 if (ret != 0){
1263 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001264 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001265 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001266
1267 return frames;
1268}
1269
1270int pcm_avail_update(struct pcm *pcm)
1271{
1272 pcm_sync_ptr(pcm, 0);
1273 return pcm_mmap_avail(pcm);
1274}
1275
1276int pcm_state(struct pcm *pcm)
1277{
1278 int err = pcm_sync_ptr(pcm, 0);
1279 if (err < 0)
1280 return err;
1281
1282 return pcm->mmap_status->state;
1283}
1284
Taylor Holberton17a10242016-11-23 13:18:24 -08001285/** Waits for frames to be available for read or write operations.
1286 * @param pcm A PCM handle.
1287 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1288 * @returns If frames became available, one is returned.
1289 * If a timeout occured, zero is returned.
1290 * If an error occured, a negative number is returned.
1291 * @ingroup libtinyalsa-pcm
1292 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001293int pcm_wait(struct pcm *pcm, int timeout)
1294{
1295 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001296 int err;
1297
1298 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001299 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001300
1301 do {
1302 /* let's wait for avail or timeout */
1303 err = poll(&pfd, 1, timeout);
1304 if (err < 0)
1305 return -errno;
1306
1307 /* timeout ? */
1308 if (err == 0)
1309 return 0;
1310
1311 /* have we been interrupted ? */
1312 if (errno == -EINTR)
1313 continue;
1314
1315 /* check for any errors */
1316 if (pfd.revents & (POLLERR | POLLNVAL)) {
1317 switch (pcm_state(pcm)) {
1318 case PCM_STATE_XRUN:
1319 return -EPIPE;
1320 case PCM_STATE_SUSPENDED:
1321 return -ESTRPIPE;
1322 case PCM_STATE_DISCONNECTED:
1323 return -ENODEV;
1324 default:
1325 return -EIO;
1326 }
1327 }
1328 /* poll again if fd not ready for IO */
1329 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1330
1331 return 1;
1332}
1333
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001334int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001335{
1336 int err = 0, frames, avail;
1337 unsigned int offset = 0, count;
1338
1339 if (bytes == 0)
1340 return 0;
1341
1342 count = pcm_bytes_to_frames(pcm, bytes);
1343
1344 while (count > 0) {
1345
1346 /* get the available space for writing new frames */
1347 avail = pcm_avail_update(pcm);
1348 if (avail < 0) {
1349 fprintf(stderr, "cannot determine available mmap frames");
1350 return err;
1351 }
1352
1353 /* start the audio if we reach the threshold */
1354 if (!pcm->running &&
1355 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1356 if (pcm_start(pcm) < 0) {
1357 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1358 (unsigned int)pcm->mmap_status->hw_ptr,
1359 (unsigned int)pcm->mmap_control->appl_ptr,
1360 avail);
1361 return -errno;
1362 }
1363 }
1364
1365 /* sleep until we have space to write new frames */
1366 if (pcm->running &&
1367 (unsigned int)avail < pcm->mmap_control->avail_min) {
1368 int time = -1;
1369
1370 if (pcm->flags & PCM_NOIRQ)
1371 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1372 / pcm->noirq_frames_per_msec;
1373
1374 err = pcm_wait(pcm, time);
1375 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301376 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001377 pcm->running = 0;
1378 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1379 (unsigned int)pcm->mmap_status->hw_ptr,
1380 (unsigned int)pcm->mmap_control->appl_ptr,
1381 avail);
1382 pcm->mmap_control->appl_ptr = 0;
1383 return err;
1384 }
1385 continue;
1386 }
1387
1388 frames = count;
1389 if (frames > avail)
1390 frames = avail;
1391
1392 if (!frames)
1393 break;
1394
1395 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001396 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001397 if (frames < 0) {
1398 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1399 (unsigned int)pcm->mmap_status->hw_ptr,
1400 (unsigned int)pcm->mmap_control->appl_ptr,
1401 avail);
1402 return frames;
1403 }
1404
1405 offset += frames;
1406 count -= frames;
1407 }
1408
Liam Girdwood6be28f12011-10-13 12:59:51 -07001409 return 0;
1410}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001411
1412int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1413{
1414 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1415 return -ENOSYS;
1416
1417 return pcm_mmap_transfer(pcm, (void *)data, count);
1418}
1419
1420int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1421{
1422 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1423 return -ENOSYS;
1424
1425 return pcm_mmap_transfer(pcm, data, count);
1426}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301427
Taylor Holberton17a10242016-11-23 13:18:24 -08001428/** Gets the delay of the PCM, in terms of frames.
1429 * @param pcm A PCM handle.
1430 * @returns On success, the delay of the PCM.
1431 * On failure, a negative number.
1432 * @ingroup libtinyalsa-pcm
1433 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301434long pcm_get_delay(struct pcm *pcm)
1435{
1436 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1437 return -1;
1438
1439 return pcm->pcm_delay;
1440}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001441