blob: 8a08e1412a5288a7488b722c2a7ad53e214b4aaa [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* pcm.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
45#define __force
46#define __bitwise
47#define __user
48#include <sound/asound.h>
49
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030050#include <tinyalsa/pcm.h>
Taylor Holbertonea06b972017-04-06 23:14:14 -070051#include <tinyalsa/limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070052
53#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070054#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070055
56static inline int param_is_mask(int p)
57{
58 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
59 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
60}
61
62static inline int param_is_interval(int p)
63{
64 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
65 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
66}
67
Taylor Holberton2f387d22016-12-01 15:58:16 -080068static inline const struct snd_interval *param_get_interval(const struct snd_pcm_hw_params *p, int n)
69{
Taylor Holberton25976dc2017-04-10 11:46:40 -070070 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
Taylor Holberton2f387d22016-12-01 15:58:16 -080071}
72
Simon Wilson79d39652011-05-25 13:44:23 -070073static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
74{
75 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
76}
77
78static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
79{
80 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
81}
82
83static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
84{
85 if (bit >= SNDRV_MASK_MAX)
86 return;
87 if (param_is_mask(n)) {
88 struct snd_mask *m = param_to_mask(p, n);
89 m->bits[0] = 0;
90 m->bits[1] = 0;
91 m->bits[bit >> 5] |= (1 << (bit & 31));
92 }
93}
94
95static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
96{
97 if (param_is_interval(n)) {
98 struct snd_interval *i = param_to_interval(p, n);
99 i->min = val;
100 }
101}
102
Taylor Holberton2f387d22016-12-01 15:58:16 -0800103static unsigned int param_get_min(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700104{
105 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800106 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700107 return i->min;
108 }
109 return 0;
110}
111
Taylor Holberton2f387d22016-12-01 15:58:16 -0800112static unsigned int param_get_max(const struct snd_pcm_hw_params *p, int n)
Simon Wilson43544882012-10-31 12:52:39 -0700113{
114 if (param_is_interval(n)) {
Taylor Holberton2f387d22016-12-01 15:58:16 -0800115 const struct snd_interval *i = param_get_interval(p, n);
Simon Wilson43544882012-10-31 12:52:39 -0700116 return i->max;
117 }
118 return 0;
119}
120
Simon Wilson79d39652011-05-25 13:44:23 -0700121static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
122{
123 if (param_is_interval(n)) {
124 struct snd_interval *i = param_to_interval(p, n);
125 i->min = val;
126 i->max = val;
127 i->integer = 1;
128 }
129}
130
Liam Girdwood6be28f12011-10-13 12:59:51 -0700131static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
132{
133 if (param_is_interval(n)) {
134 struct snd_interval *i = param_to_interval(p, n);
135 if (i->integer)
136 return i->max;
137 }
138 return 0;
139}
140
Simon Wilson79d39652011-05-25 13:44:23 -0700141static void param_init(struct snd_pcm_hw_params *p)
142{
143 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700144
Simon Wilson79d39652011-05-25 13:44:23 -0700145 memset(p, 0, sizeof(*p));
146 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
147 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
148 struct snd_mask *m = param_to_mask(p, n);
149 m->bits[0] = ~0;
150 m->bits[1] = ~0;
151 }
152 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
153 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
154 struct snd_interval *i = param_to_interval(p, n);
155 i->min = 0;
156 i->max = ~0;
157 }
Simon Wilson43544882012-10-31 12:52:39 -0700158 p->rmask = ~0U;
159 p->cmask = 0;
160 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700161}
162
163#define PCM_ERROR_MAX 128
164
Taylor Holberton6d58e012016-10-01 18:32:30 -0400165/** A PCM handle.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800166 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400167 */
Simon Wilson79d39652011-05-25 13:44:23 -0700168struct pcm {
Taylor Holberton6d58e012016-10-01 18:32:30 -0400169 /** The PCM's file descriptor */
Simon Wilson79d39652011-05-25 13:44:23 -0700170 int fd;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400171 /** Flags that were passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700172 unsigned int flags;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400173 /** Whether the PCM is running or not */
Simon Wilson79d39652011-05-25 13:44:23 -0700174 int running:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400175 /** Whether or not the PCM has been prepared */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530176 int prepared:1;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400177 /** The number of underruns that have occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700178 int underruns;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400179 /** Size of the buffer */
Simon Wilson79d39652011-05-25 13:44:23 -0700180 unsigned int buffer_size;
Taylor Holberton17a10242016-11-23 13:18:24 -0800181 /** The boundary for ring buffer pointers */
Liam Girdwood6be28f12011-10-13 12:59:51 -0700182 unsigned int boundary;
Taylor Holberton6d58e012016-10-01 18:32:30 -0400183 /** Description of the last error that occured */
Simon Wilson79d39652011-05-25 13:44:23 -0700184 char error[PCM_ERROR_MAX];
Taylor Holberton6d58e012016-10-01 18:32:30 -0400185 /** Configuration that was passed to @ref pcm_open */
Simon Wilson79d39652011-05-25 13:44:23 -0700186 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700187 struct snd_pcm_mmap_status *mmap_status;
188 struct snd_pcm_mmap_control *mmap_control;
189 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700190 void *mmap_buffer;
191 unsigned int noirq_frames_per_msec;
Taylor Holberton17a10242016-11-23 13:18:24 -0800192 /** The delay of the PCM, in terms of frames */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530193 long pcm_delay;
Taylor Holberton17a10242016-11-23 13:18:24 -0800194 /** The subdevice corresponding to the PCM */
David Wagner4cddf192014-04-02 15:12:54 +0200195 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700196};
197
Taylor Holberton6d58e012016-10-01 18:32:30 -0400198/** Gets the buffer size of the PCM.
199 * @param pcm A PCM handle.
200 * @return The buffer size of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800201 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400202 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800203unsigned int pcm_get_buffer_size(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700204{
205 return pcm->buffer_size;
206}
207
Taylor Holberton77979a82016-12-01 20:04:04 -0800208/** Gets the channel count of the PCM.
209 * @param pcm A PCM handle.
210 * @return The channel count of the PCM.
211 * @ingroup libtinyalsa-pcm
212 */
213unsigned int pcm_get_channels(const struct pcm *pcm)
214{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700215 return pcm->config.channels;
Taylor Holberton77979a82016-12-01 20:04:04 -0800216}
217
Taylor Holberton08bb5902017-04-10 11:45:44 -0700218/** Gets the PCM configuration.
219 * @param pcm A PCM handle.
220 * @return The PCM configuration.
221 * This function only returns NULL if
222 * @p pcm is NULL.
223 * @ingroup libtinyalsa-pcm
224 * */
225const struct pcm_config * pcm_get_config(const struct pcm *pcm)
226{
227 if (pcm == NULL)
228 return NULL;
229 return &pcm->config;
230}
231
Taylor Holberton77979a82016-12-01 20:04:04 -0800232/** Gets the rate of the PCM.
233 * The rate is given in frames per second.
234 * @param pcm A PCM handle.
235 * @return The rate of the PCM.
236 * @ingroup libtinyalsa-pcm
237 */
238unsigned int pcm_get_rate(const struct pcm *pcm)
239{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700240 return pcm->config.rate;
Taylor Holberton77979a82016-12-01 20:04:04 -0800241}
242
243/** Gets the format of the PCM.
244 * @param pcm A PCM handle.
245 * @return The format of the PCM.
246 * @ingroup libtinyalsa-pcm
247 */
248enum pcm_format pcm_get_format(const struct pcm *pcm)
249{
Taylor Holberton25976dc2017-04-10 11:46:40 -0700250 return pcm->config.format;
Taylor Holberton77979a82016-12-01 20:04:04 -0800251}
252
Taylor Holberton6d58e012016-10-01 18:32:30 -0400253/** Gets the file descriptor of the PCM.
254 * Useful for extending functionality of the PCM when needed.
Taylor Holbertond265c272016-11-23 13:22:56 -0800255 * @param pcm A PCM handle.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400256 * @return The file descriptor of the PCM.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800257 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400258 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800259int pcm_get_file_descriptor(const struct pcm *pcm)
Taylor Holbertonbb402602016-08-03 10:15:46 -0400260{
261 return pcm->fd;
262}
263
Taylor Holberton6d58e012016-10-01 18:32:30 -0400264/** Gets the error message for the last error that occured.
265 * If no error occured and this function is called, the results are undefined.
266 * @param pcm A PCM handle.
267 * @return The error message of the last error that occured.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800268 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400269 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800270const char* pcm_get_error(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700271{
272 return pcm->error;
273}
274
Taylor Holberton6d58e012016-10-01 18:32:30 -0400275/** Gets the subdevice on which the pcm has been opened.
276 * @param pcm A PCM handle.
277 * @return The subdevice on which the pcm has been opened */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800278unsigned int pcm_get_subdevice(const struct pcm *pcm)
David Wagner4cddf192014-04-02 15:12:54 +0200279{
280 return pcm->subdevice;
281}
282
Simon Wilson79d39652011-05-25 13:44:23 -0700283static int oops(struct pcm *pcm, int e, const char *fmt, ...)
284{
285 va_list ap;
286 int sz;
287
288 va_start(ap, fmt);
289 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
290 va_end(ap);
291 sz = strlen(pcm->error);
292
293 if (errno)
294 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
295 ": %s", strerror(e));
296 return -1;
297}
298
Simon Wilsonbc03b622011-06-15 17:19:01 -0700299static unsigned int pcm_format_to_alsa(enum pcm_format format)
300{
301 switch (format) {
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400302
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500303 case PCM_FORMAT_S8:
304 return SNDRV_PCM_FORMAT_S8;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400305
Simon Wilsonbc03b622011-06-15 17:19:01 -0700306 default:
307 case PCM_FORMAT_S16_LE:
308 return SNDRV_PCM_FORMAT_S16_LE;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400309 case PCM_FORMAT_S16_BE:
310 return SNDRV_PCM_FORMAT_S16_BE;
311
312 case PCM_FORMAT_S24_LE:
313 return SNDRV_PCM_FORMAT_S24_LE;
314 case PCM_FORMAT_S24_BE:
315 return SNDRV_PCM_FORMAT_S24_BE;
316
317 case PCM_FORMAT_S24_3LE:
318 return SNDRV_PCM_FORMAT_S24_3LE;
319 case PCM_FORMAT_S24_3BE:
320 return SNDRV_PCM_FORMAT_S24_3BE;
321
322 case PCM_FORMAT_S32_LE:
323 return SNDRV_PCM_FORMAT_S32_LE;
324 case PCM_FORMAT_S32_BE:
325 return SNDRV_PCM_FORMAT_S32_BE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700326 };
327}
328
Taylor Holberton6d58e012016-10-01 18:32:30 -0400329/** Determines the number of bits occupied by a @ref pcm_format.
330 * @param format A PCM format.
331 * @return The number of bits associated with @p format
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800332 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400333 */
Simon Wilson7136cf72013-07-17 10:30:35 -0700334unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700335{
336 switch (format) {
337 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400338 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700339 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400340 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700341 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400342 case PCM_FORMAT_S24_3LE:
343 case PCM_FORMAT_S24_3BE:
344 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700345 default:
346 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400347 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700348 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400349 case PCM_FORMAT_S8:
350 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700351 };
352}
353
Taylor Holberton6d58e012016-10-01 18:32:30 -0400354/** Determines how many frames of a PCM can fit into a number of bytes.
355 * @param pcm A PCM handle.
356 * @param bytes The number of bytes.
357 * @return The number of frames that may fit into @p bytes
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800358 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400359 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800360unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700361{
362 return bytes / (pcm->config.channels *
363 (pcm_format_to_bits(pcm->config.format) >> 3));
364}
365
Taylor Holberton6d58e012016-10-01 18:32:30 -0400366/** Determines how many bytes are occupied by a number of frames of a PCM.
367 * @param pcm A PCM handle.
368 * @param frames The number of frames of a PCM.
369 * @return The bytes occupied by @p frames.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800370 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400371 */
Taylor Holberton147d7ad2016-12-01 17:50:31 -0800372unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700373{
374 return frames * pcm->config.channels *
375 (pcm_format_to_bits(pcm->config.format) >> 3);
376}
377
Taylor Holberton4f556062016-09-16 09:54:36 -0400378static int pcm_sync_ptr(struct pcm *pcm, int flags)
379{
Eric Laurent40b018e2011-06-18 10:10:23 -0700380 if (pcm->sync_ptr) {
381 pcm->sync_ptr->flags = flags;
Taylor Holbertone123a652017-01-13 21:39:48 -0800382 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0) {
383 oops(pcm, errno, "failed to sync mmap ptr");
Eric Laurent40b018e2011-06-18 10:10:23 -0700384 return -1;
Taylor Holbertone123a652017-01-13 21:39:48 -0800385 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800386 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700387 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800388 return -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700389}
390
Taylor Holberton4f556062016-09-16 09:54:36 -0400391static int pcm_hw_mmap_status(struct pcm *pcm)
392{
Eric Laurent40b018e2011-06-18 10:10:23 -0700393 if (pcm->sync_ptr)
394 return 0;
395
396 int page_size = sysconf(_SC_PAGE_SIZE);
397 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
398 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
399 if (pcm->mmap_status == MAP_FAILED)
400 pcm->mmap_status = NULL;
401 if (!pcm->mmap_status)
402 goto mmap_error;
403
404 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
405 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
406 if (pcm->mmap_control == MAP_FAILED)
407 pcm->mmap_control = NULL;
408 if (!pcm->mmap_control) {
409 munmap(pcm->mmap_status, page_size);
410 pcm->mmap_status = NULL;
411 goto mmap_error;
412 }
413 pcm->mmap_control->avail_min = 1;
414
415 return 0;
416
417mmap_error:
418
419 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
420 if (!pcm->sync_ptr)
421 return -ENOMEM;
422 pcm->mmap_status = &pcm->sync_ptr->s.status;
423 pcm->mmap_control = &pcm->sync_ptr->c.control;
424 pcm->mmap_control->avail_min = 1;
425 pcm_sync_ptr(pcm, 0);
426
427 return 0;
428}
429
430static void pcm_hw_munmap_status(struct pcm *pcm) {
431 if (pcm->sync_ptr) {
432 free(pcm->sync_ptr);
433 pcm->sync_ptr = NULL;
434 } else {
435 int page_size = sysconf(_SC_PAGE_SIZE);
436 if (pcm->mmap_status)
437 munmap(pcm->mmap_status, page_size);
438 if (pcm->mmap_control)
439 munmap(pcm->mmap_control, page_size);
440 }
441 pcm->mmap_status = NULL;
442 pcm->mmap_control = NULL;
443}
444
Liam Girdwood6be28f12011-10-13 12:59:51 -0700445static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700446 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700447 unsigned int frames)
448{
449 int size_bytes = pcm_frames_to_bytes(pcm, frames);
450 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
451 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
452
453 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700454 if (pcm->flags & PCM_IN)
455 memcpy(buf + src_offset_bytes,
456 (char*)pcm->mmap_buffer + pcm_offset_bytes,
457 size_bytes);
458 else
459 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
460 buf + src_offset_bytes,
461 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700462 return 0;
463}
464
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700465static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700466 unsigned int offset, unsigned int size)
467{
468 void *pcm_areas;
469 int commit;
470 unsigned int pcm_offset, frames, count = 0;
471
472 while (size > 0) {
473 frames = size;
474 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700475 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700476 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
477 if (commit < 0) {
478 oops(pcm, commit, "failed to commit %d frames\n", frames);
479 return commit;
480 }
481
482 offset += commit;
483 count += commit;
484 size -= commit;
485 }
486 return count;
487}
488
Taylor Holberton6d58e012016-10-01 18:32:30 -0400489/** Returns available frames in pcm buffer and corresponding time stamp.
490 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
491 * otherwise the clock is CLOCK_REALTIME.
492 * For an input stream, frames available are frames ready for the application to read.
493 * For an output stream, frames available are the number of empty frames available for the application to write.
494 * Only available for PCMs opened with the @ref PCM_MMAP flag.
495 * @param pcm A PCM handle.
496 * @param avail The number of available frames
497 * @param tstamp The timestamp
498 * @return On success, zero is returned; on failure, negative one.
499 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700500int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
501 struct timespec *tstamp)
502{
503 int frames;
504 int rc;
505 snd_pcm_uframes_t hw_ptr;
506
507 if (!pcm_is_ready(pcm))
508 return -1;
509
510 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
511 if (rc < 0)
512 return -1;
513
Eric Laurent7db48582011-11-17 11:47:59 -0800514 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
515 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800516 return -1;
517
Eric Laurent40b018e2011-06-18 10:10:23 -0700518 *tstamp = pcm->mmap_status->tstamp;
519 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
520 return -1;
521
522 hw_ptr = pcm->mmap_status->hw_ptr;
523 if (pcm->flags & PCM_IN)
524 frames = hw_ptr - pcm->mmap_control->appl_ptr;
525 else
526 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
527
528 if (frames < 0)
529 return -1;
530
531 *avail = (unsigned int)frames;
532
533 return 0;
534}
535
Taylor Holberton6d58e012016-10-01 18:32:30 -0400536/** Writes audio samples to PCM.
537 * If the PCM has not been started, it is started in this function.
538 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
539 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
540 * @param pcm A PCM handle.
541 * @param data The audio sample array
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800542 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700543 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
544 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800545 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800546 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400547 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800548int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700549{
550 struct snd_xferi x;
551
552 if (pcm->flags & PCM_IN)
553 return -EINVAL;
Taylor Holberton851dd802017-04-06 23:12:01 -0700554 else if ((frame_count > TINYALSA_FRAMES_MAX)
555 || (frame_count > INT_MAX))
556 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700557
Mark Brown6bbe77a2012-02-10 22:07:09 +0000558 x.buf = (void*)data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800559 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800560 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700561 for (;;) {
562 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530563 int prepare_error = pcm_prepare(pcm);
564 if (prepare_error)
565 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700566 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
567 return oops(pcm, errno, "cannot write initial data");
568 pcm->running = 1;
569 return 0;
570 }
571 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530572 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700573 pcm->running = 0;
574 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700575 /* we failed to make our window -- try to restart if we are
576 * allowed to do so. Otherwise, simply allow the EPIPE error to
577 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700578 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700579 if (pcm->flags & PCM_NORESTART)
580 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700581 continue;
582 }
583 return oops(pcm, errno, "cannot write stream data");
584 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800585 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700586 }
587}
588
Taylor Holberton6d58e012016-10-01 18:32:30 -0400589/** Reads audio samples from PCM.
590 * If the PCM has not been started, it is started in this function.
591 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
592 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
593 * @param pcm A PCM handle.
594 * @param data The audio sample array
Taylor Holbertond1c98e42016-12-01 21:21:49 -0800595 * @param frame_count The number of frames occupied by the sample array.
Taylor Holberton851dd802017-04-06 23:12:01 -0700596 * This value should not be greater than @ref TINYALSA_FRAMES_MAX
597 * or INT_MAX.
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800598 * @return On success, this function returns the number of frames written; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800599 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400600 */
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800601int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count)
Simon Wilson79d39652011-05-25 13:44:23 -0700602{
603 struct snd_xferi x;
604
605 if (!(pcm->flags & PCM_IN))
606 return -EINVAL;
Taylor Holberton851dd802017-04-06 23:12:01 -0700607 else if ((frame_count > TINYALSA_FRAMES_MAX)
608 || (frame_count > INT_MAX))
609 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700610
611 x.buf = data;
Taylor Holbertond7b140a2016-12-01 20:43:28 -0800612 x.frames = frame_count;
Taylor Holberton2386a422016-11-18 20:38:40 -0800613 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700614 for (;;) {
Taylor Holberton1137fc72017-04-06 23:06:36 -0700615 if ((!pcm->running) && (pcm_start(pcm) < 0))
616 return -errno;
617 else if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530618 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700619 pcm->running = 0;
620 if (errno == EPIPE) {
621 /* we failed to make our window -- try to restart */
622 pcm->underruns++;
623 continue;
624 }
625 return oops(pcm, errno, "cannot read stream data");
626 }
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800627 return x.result;
Simon Wilson79d39652011-05-25 13:44:23 -0700628 }
629}
630
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800631/** Writes audio samples to PCM.
632 * If the PCM has not been started, it is started in this function.
633 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
634 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
635 * @param pcm A PCM handle.
636 * @param data The audio sample array
637 * @param count The number of bytes occupied by the sample array.
638 * @return On success, this function returns zero; otherwise, a negative number.
639 * @deprecated
640 * @ingroup libtinyalsa-pcm
641 */
642int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
643{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700644 return pcm_writei(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800645}
646
647/** Reads audio samples from PCM.
648 * If the PCM has not been started, it is started in this function.
649 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
650 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
651 * @param pcm A PCM handle.
652 * @param data The audio sample array
653 * @param count The number of bytes occupied by the sample array.
654 * @return On success, this function returns zero; otherwise, a negative number.
655 * @deprecated
656 * @ingroup libtinyalsa-pcm
657 */
658int pcm_read(struct pcm *pcm, void *data, unsigned int count)
659{
Taylor Holbertonea06b972017-04-06 23:14:14 -0700660 return pcm_readi(pcm, data, pcm_bytes_to_frames(pcm, count));
Taylor Holbertonf9834ee2016-12-01 20:25:41 -0800661}
662
Simon Wilson79d39652011-05-25 13:44:23 -0700663static struct pcm bad_pcm = {
664 .fd = -1,
665};
666
Taylor Holberton6d58e012016-10-01 18:32:30 -0400667/** Gets the hardware parameters of a PCM, without created a PCM handle.
668 * @param card The card of the PCM.
669 * The default card is zero.
670 * @param device The device of the PCM.
671 * The default device is zero.
672 * @param flags Specifies whether the PCM is an input or output.
673 * May be one of the following:
674 * - @ref PCM_IN
675 * - @ref PCM_OUT
676 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800677 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400678 */
Simon Wilson43544882012-10-31 12:52:39 -0700679struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
680 unsigned int flags)
681{
682 struct snd_pcm_hw_params *params;
683 char fn[256];
684 int fd;
685
686 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
687 flags & PCM_IN ? 'c' : 'p');
688
689 fd = open(fn, O_RDWR);
690 if (fd < 0) {
691 fprintf(stderr, "cannot open device '%s'\n", fn);
692 goto err_open;
693 }
694
695 params = calloc(1, sizeof(struct snd_pcm_hw_params));
696 if (!params)
697 goto err_calloc;
698
699 param_init(params);
700 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
701 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
702 goto err_hw_refine;
703 }
704
705 close(fd);
706
707 return (struct pcm_params *)params;
708
709err_hw_refine:
710 free(params);
711err_calloc:
712 close(fd);
713err_open:
714 return NULL;
715}
716
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500717/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400718 * @param pcm_params Hardware parameters of a PCM.
719 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800720 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400721 */
Simon Wilson43544882012-10-31 12:52:39 -0700722void pcm_params_free(struct pcm_params *pcm_params)
723{
724 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
725
726 if (params)
727 free(params);
728}
729
730static int pcm_param_to_alsa(enum pcm_param param)
731{
732 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700733 case PCM_PARAM_ACCESS:
734 return SNDRV_PCM_HW_PARAM_ACCESS;
735 case PCM_PARAM_FORMAT:
736 return SNDRV_PCM_HW_PARAM_FORMAT;
737 case PCM_PARAM_SUBFORMAT:
738 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700739 case PCM_PARAM_SAMPLE_BITS:
740 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
741 break;
742 case PCM_PARAM_FRAME_BITS:
743 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
744 break;
745 case PCM_PARAM_CHANNELS:
746 return SNDRV_PCM_HW_PARAM_CHANNELS;
747 break;
748 case PCM_PARAM_RATE:
749 return SNDRV_PCM_HW_PARAM_RATE;
750 break;
751 case PCM_PARAM_PERIOD_TIME:
752 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
753 break;
754 case PCM_PARAM_PERIOD_SIZE:
755 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
756 break;
757 case PCM_PARAM_PERIOD_BYTES:
758 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
759 break;
760 case PCM_PARAM_PERIODS:
761 return SNDRV_PCM_HW_PARAM_PERIODS;
762 break;
763 case PCM_PARAM_BUFFER_TIME:
764 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
765 break;
766 case PCM_PARAM_BUFFER_SIZE:
767 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
768 break;
769 case PCM_PARAM_BUFFER_BYTES:
770 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
771 break;
772 case PCM_PARAM_TICK_TIME:
773 return SNDRV_PCM_HW_PARAM_TICK_TIME;
774 break;
775
776 default:
777 return -1;
778 }
779}
780
Taylor Holberton6d58e012016-10-01 18:32:30 -0400781/** Gets a mask from a PCM's hardware parameters.
782 * @param pcm_params A PCM's hardware parameters.
783 * @param param The parameter to get.
784 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
785 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800786 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400787 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800788const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700789 enum pcm_param param)
790{
791 int p;
792 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
793 if (params == NULL) {
794 return NULL;
795 }
796
797 p = pcm_param_to_alsa(param);
798 if (p < 0 || !param_is_mask(p)) {
799 return NULL;
800 }
801
Taylor Holberton2f387d22016-12-01 15:58:16 -0800802 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700803}
804
Taylor Holberton17a10242016-11-23 13:18:24 -0800805/** Get the minimum of a specified PCM parameter.
806 * @param pcm_params A PCM parameters structure.
807 * @param param The specified parameter to get the minimum of.
808 * @returns On success, the parameter minimum.
809 * On failure, zero.
810 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800811unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700812 enum pcm_param param)
813{
814 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
815 int p;
816
817 if (!params)
818 return 0;
819
820 p = pcm_param_to_alsa(param);
821 if (p < 0)
822 return 0;
823
824 return param_get_min(params, p);
825}
826
Taylor Holberton17a10242016-11-23 13:18:24 -0800827/** Get the maximum of a specified PCM parameter.
828 * @param pcm_params A PCM parameters structure.
829 * @param param The specified parameter to get the maximum of.
830 * @returns On success, the parameter maximum.
831 * On failure, zero.
832 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800833unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700834 enum pcm_param param)
835{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800836 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700837 int p;
838
839 if (!params)
840 return 0;
841
842 p = pcm_param_to_alsa(param);
843 if (p < 0)
844 return 0;
845
846 return param_get_max(params, p);
847}
848
Taylor Holberton6d58e012016-10-01 18:32:30 -0400849/** Closes a PCM returned by @ref pcm_open.
850 * @param pcm A PCM returned by @ref pcm_open.
851 * May not be NULL.
852 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800853 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400854 */
Simon Wilson79d39652011-05-25 13:44:23 -0700855int pcm_close(struct pcm *pcm)
856{
857 if (pcm == &bad_pcm)
858 return 0;
859
Eric Laurent40b018e2011-06-18 10:10:23 -0700860 pcm_hw_munmap_status(pcm);
861
Liam Girdwood6be28f12011-10-13 12:59:51 -0700862 if (pcm->flags & PCM_MMAP) {
863 pcm_stop(pcm);
864 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
865 }
866
Simon Wilson79d39652011-05-25 13:44:23 -0700867 if (pcm->fd >= 0)
868 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530869 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700870 pcm->running = 0;
871 pcm->buffer_size = 0;
872 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700873 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700874 return 0;
875}
876
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800877/** Opens a PCM by it's name.
878 * @param name The name of the PCM.
879 * The name is given in the format: <i>hw</i>:<b>card</b>,<b>device</b>
880 * @param flags Specify characteristics and functionality about the pcm.
881 * May be a bitwise AND of the following:
882 * - @ref PCM_IN
883 * - @ref PCM_OUT
884 * - @ref PCM_MMAP
885 * - @ref PCM_NOIRQ
886 * - @ref PCM_MONOTONIC
887 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800888 * @returns A PCM structure.
889 * If an error occurs allocating memory for the PCM, NULL is returned.
890 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
891 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holbertonc6f908e2016-12-24 20:33:33 -0800892 * @ingroup libtinyalsa-pcm
893 */
894struct pcm *pcm_open_by_name(const char *name,
895 unsigned int flags,
896 const struct pcm_config *config)
897{
898 unsigned int card, device;
899 if ((name[0] != 'h')
900 || (name[1] != 'w')
901 || (name[2] != ':')) {
902 return NULL;
903 } else if (sscanf(&name[3], "%u,%u", &card, &device) != 2) {
904 return NULL;
905 }
906 return pcm_open(card, device, flags, config);
907}
908
Taylor Holberton6d58e012016-10-01 18:32:30 -0400909/** Opens a PCM.
910 * @param card The card that the pcm belongs to.
911 * The default card is zero.
912 * @param device The device that the pcm belongs to.
913 * The default device is zero.
914 * @param flags Specify characteristics and functionality about the pcm.
915 * May be a bitwise AND of the following:
916 * - @ref PCM_IN
917 * - @ref PCM_OUT
918 * - @ref PCM_MMAP
919 * - @ref PCM_NOIRQ
920 * - @ref PCM_MONOTONIC
921 * @param config The hardware and software parameters to open the PCM with.
Taylor Holbertone123a652017-01-13 21:39:48 -0800922 * @returns A PCM structure.
923 * If an error occurs allocating memory for the PCM, NULL is returned.
924 * Otherwise, client code should check that the PCM opened properly by calling @ref pcm_is_ready.
925 * If @ref pcm_is_ready, check @ref pcm_get_error for more information.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800926 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400927 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700928struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800929 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700930{
Simon Wilson79d39652011-05-25 13:44:23 -0700931 struct pcm *pcm;
932 struct snd_pcm_info info;
933 struct snd_pcm_hw_params params;
934 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700935 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700936 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700937
938 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400939 if (!pcm)
940 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700941
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400942 if (config == NULL) {
943 config = &pcm->config;
Taylor Holberton94803b02016-12-01 16:07:14 -0800944 pcm->config.channels = 2;
945 pcm->config.rate = 48000;
946 pcm->config.period_size = 1024;
947 pcm->config.period_count = 4;
948 pcm->config.format = PCM_FORMAT_S16_LE;
949 pcm->config.start_threshold = config->period_count * config->period_size;
950 pcm->config.stop_threshold = config->period_count * config->period_size;
951 pcm->config.silence_threshold = 0;
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400952 } else {
953 pcm->config = *config;
954 }
Simon Wilson79d39652011-05-25 13:44:23 -0700955
Simon Wilson1bd580f2011-06-02 15:58:41 -0700956 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
957 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700958
959 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700960 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700961 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700962 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700963 return pcm;
964 }
965
966 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700967 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700968 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700969 }
David Wagner4cddf192014-04-02 15:12:54 +0200970 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700971
972 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700973 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
974 pcm_format_to_alsa(config->format));
975 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
976 SNDRV_PCM_SUBFORMAT_STD);
977 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
978 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
979 pcm_format_to_bits(config->format));
980 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
981 pcm_format_to_bits(config->format) * config->channels);
982 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
983 config->channels);
984 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
985 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
986
Liam Girdwood6be28f12011-10-13 12:59:51 -0700987 if (flags & PCM_NOIRQ) {
988
989 if (!(flags & PCM_MMAP)) {
990 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
991 goto fail;
992 }
993
994 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
995 pcm->noirq_frames_per_msec = config->rate / 1000;
996 }
997
998 if (flags & PCM_MMAP)
999 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
1000 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
1001 else
1002 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
1003 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
1004
Simon Wilson79d39652011-05-25 13:44:23 -07001005 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
1006 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001007 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -07001008 }
1009
Liam Girdwood6be28f12011-10-13 12:59:51 -07001010 /* get our refined hw_params */
Taylor Holberton94803b02016-12-01 16:07:14 -08001011 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1012 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001013 pcm->buffer_size = config->period_count * config->period_size;
1014
1015 if (flags & PCM_MMAP) {
1016 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
1017 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
1018 if (pcm->mmap_buffer == MAP_FAILED) {
1019 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
1020 pcm_frames_to_bytes(pcm, pcm->buffer_size));
1021 goto fail_close;
1022 }
1023 }
1024
1025
Simon Wilson79d39652011-05-25 13:44:23 -07001026 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -07001027 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -07001028 sparams.period_step = 1;
1029 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -07001030
Eric Laurent93e7b672012-08-22 16:18:14 -07001031 if (!config->start_threshold) {
1032 if (pcm->flags & PCM_IN)
1033 pcm->config.start_threshold = sparams.start_threshold = 1;
1034 else
1035 pcm->config.start_threshold = sparams.start_threshold =
1036 config->period_count * config->period_size / 2;
1037 } else
John Grossman3bb114a2011-07-21 10:59:55 -07001038 sparams.start_threshold = config->start_threshold;
1039
Liam Girdwood6be28f12011-10-13 12:59:51 -07001040 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -08001041 if (!config->stop_threshold) {
1042 if (pcm->flags & PCM_IN)
1043 pcm->config.stop_threshold = sparams.stop_threshold =
1044 config->period_count * config->period_size * 10;
1045 else
1046 pcm->config.stop_threshold = sparams.stop_threshold =
1047 config->period_count * config->period_size;
1048 }
John Grossman3bb114a2011-07-21 10:59:55 -07001049 else
1050 sparams.stop_threshold = config->stop_threshold;
1051
Simon Wilson79d39652011-05-25 13:44:23 -07001052 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
1053 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -07001054 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001055 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -07001056
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -06001057 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Taylor Holberton25976dc2017-04-10 11:46:40 -07001058 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -07001059
1060 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
1061 oops(pcm, errno, "cannot set sw params");
1062 goto fail;
1063 }
1064
Eric Laurent40b018e2011-06-18 10:10:23 -07001065 rc = pcm_hw_mmap_status(pcm);
1066 if (rc < 0) {
1067 oops(pcm, rc, "mmap status failed");
1068 goto fail;
1069 }
1070
Glenn Kasten81012402013-08-22 15:11:48 -07001071#ifdef SNDRV_PCM_IOCTL_TTSTAMP
1072 if (pcm->flags & PCM_MONOTONIC) {
1073 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
1074 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
1075 if (rc < 0) {
1076 oops(pcm, rc, "cannot set timestamp type");
1077 goto fail;
1078 }
1079 }
1080#endif
1081
Simon Wilson79d39652011-05-25 13:44:23 -07001082 pcm->underruns = 0;
1083 return pcm;
1084
1085fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -07001086 if (flags & PCM_MMAP)
1087 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1088fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001089 close(pcm->fd);
1090 pcm->fd = -1;
1091 return pcm;
1092}
1093
Taylor Holberton6d58e012016-10-01 18:32:30 -04001094/** Checks if a PCM file has been opened without error.
1095 * @param pcm A PCM handle.
Taylor Holbertone123a652017-01-13 21:39:48 -08001096 * May be NULL.
1097 * @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 -04001098 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001099 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001100 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001101int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001102{
Taylor Holbertone123a652017-01-13 21:39:48 -08001103 if (pcm != NULL) {
1104 return pcm->fd >= 0;
1105 }
1106 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -07001107}
Simon Wilsond6458e62011-06-21 14:58:11 -07001108
Taylor Holberton558e5942016-12-04 13:42:28 -08001109/** Links two PCMs.
1110 * After this function is called, the two PCMs will prepare, start and stop in sync (at the same time).
1111 * If an error occurs, the error message will be written to @p pcm1.
1112 * @param pcm1 A PCM handle.
1113 * @param pcm2 Another PCM handle.
1114 * @return On success, zero; on failure, a negative number.
1115 * @ingroup libtinyalsa-pcm
1116 */
1117int pcm_link(struct pcm *pcm1, struct pcm *pcm2)
1118{
1119 int err = ioctl(pcm1->fd, SNDRV_PCM_IOCTL_LINK, pcm2->fd);
1120 if (err == -1) {
1121 return oops(pcm1, errno, "cannot link PCM");
1122 }
1123 return 0;
1124}
1125
1126/** Unlinks a PCM.
1127 * @see @ref pcm_link
1128 * @param pcm A PCM handle.
1129 * @return On success, zero; on failure, a negative number.
1130 * @ingroup libtinyalsa-pcm
1131 */
1132int pcm_unlink(struct pcm *pcm)
1133{
1134 int err = ioctl(pcm->fd, SNDRV_PCM_IOCTL_UNLINK);
1135 if (err == -1) {
1136 return oops(pcm, errno, "cannot unlink PCM");
1137 }
1138 return 0;
1139}
1140
Taylor Holberton6d58e012016-10-01 18:32:30 -04001141/** Prepares a PCM, if it has not been prepared already.
1142 * @param pcm A PCM handle.
1143 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001144 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001145 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301146int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001147{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301148 if (pcm->prepared)
1149 return 0;
1150
Simon Wilsond6458e62011-06-21 14:58:11 -07001151 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1152 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001153
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301154 pcm->prepared = 1;
1155 return 0;
1156}
1157
Taylor Holberton6d58e012016-10-01 18:32:30 -04001158/** Starts a PCM.
1159 * If the PCM has not been prepared,
1160 * it is prepared in this function.
1161 * @param pcm A PCM handle.
1162 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001163 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001164 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301165int pcm_start(struct pcm *pcm)
1166{
1167 int prepare_error = pcm_prepare(pcm);
1168 if (prepare_error)
1169 return prepare_error;
1170
Liam Girdwood6be28f12011-10-13 12:59:51 -07001171 if (pcm->flags & PCM_MMAP)
Taylor Holberton25976dc2017-04-10 11:46:40 -07001172 pcm_sync_ptr(pcm, 0);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001173
Simon Wilsond6458e62011-06-21 14:58:11 -07001174 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1175 return oops(pcm, errno, "cannot start channel");
1176
Liam Girdwood6be28f12011-10-13 12:59:51 -07001177 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001178 return 0;
1179}
1180
Taylor Holberton6d58e012016-10-01 18:32:30 -04001181/** Stops a PCM.
1182 * @param pcm A PCM handle.
1183 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001184 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001185 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001186int pcm_stop(struct pcm *pcm)
1187{
1188 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1189 return oops(pcm, errno, "cannot stop channel");
1190
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301191 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001192 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001193 return 0;
1194}
1195
Liam Girdwood6be28f12011-10-13 12:59:51 -07001196static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1197{
1198 int avail;
1199
1200 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1201
1202 if (avail < 0)
1203 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001204 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001205 avail -= pcm->boundary;
1206
1207 return avail;
1208}
1209
1210static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1211{
1212 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1213 if (avail < 0)
1214 avail += pcm->boundary;
1215 return avail;
1216}
1217
1218static inline int pcm_mmap_avail(struct pcm *pcm)
1219{
1220 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1221 if (pcm->flags & PCM_IN)
1222 return pcm_mmap_capture_avail(pcm);
1223 else
1224 return pcm_mmap_playback_avail(pcm);
1225}
1226
1227static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1228{
1229 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1230 appl_ptr += frames;
1231
1232 /* check for boundary wrap */
1233 if (appl_ptr > pcm->boundary)
1234 appl_ptr -= pcm->boundary;
1235 pcm->mmap_control->appl_ptr = appl_ptr;
1236}
1237
1238int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1239 unsigned int *frames)
1240{
1241 unsigned int continuous, copy_frames, avail;
1242
1243 /* return the mmap buffer */
1244 *areas = pcm->mmap_buffer;
1245
1246 /* and the application offset in frames */
1247 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1248
1249 avail = pcm_mmap_avail(pcm);
1250 if (avail > pcm->buffer_size)
1251 avail = pcm->buffer_size;
1252 continuous = pcm->buffer_size - *offset;
1253
1254 /* we can only copy frames if the are availabale and continuos */
1255 copy_frames = *frames;
1256 if (copy_frames > avail)
1257 copy_frames = avail;
1258 if (copy_frames > continuous)
1259 copy_frames = continuous;
1260 *frames = copy_frames;
1261
1262 return 0;
1263}
1264
1265int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1266{
Taylor Holberton72e44222016-11-22 09:54:47 -08001267 int ret;
1268
Taylor Holberton73466c02016-10-01 12:51:59 -04001269 /* not used */
1270 (void) offset;
1271
Liam Girdwood6be28f12011-10-13 12:59:51 -07001272 /* update the application pointer in userspace and kernel */
1273 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001274 ret = pcm_sync_ptr(pcm, 0);
Taylor Holbertone123a652017-01-13 21:39:48 -08001275 if (ret != 0){
1276 printf("%d\n", ret);
Taylor Holberton72e44222016-11-22 09:54:47 -08001277 return ret;
Taylor Holbertone123a652017-01-13 21:39:48 -08001278 }
Liam Girdwood6be28f12011-10-13 12:59:51 -07001279
1280 return frames;
1281}
1282
1283int pcm_avail_update(struct pcm *pcm)
1284{
1285 pcm_sync_ptr(pcm, 0);
1286 return pcm_mmap_avail(pcm);
1287}
1288
1289int pcm_state(struct pcm *pcm)
1290{
1291 int err = pcm_sync_ptr(pcm, 0);
1292 if (err < 0)
1293 return err;
1294
1295 return pcm->mmap_status->state;
1296}
1297
Taylor Holberton17a10242016-11-23 13:18:24 -08001298/** Waits for frames to be available for read or write operations.
1299 * @param pcm A PCM handle.
1300 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1301 * @returns If frames became available, one is returned.
1302 * If a timeout occured, zero is returned.
1303 * If an error occured, a negative number is returned.
1304 * @ingroup libtinyalsa-pcm
1305 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001306int pcm_wait(struct pcm *pcm, int timeout)
1307{
1308 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001309 int err;
1310
1311 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001312 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001313
1314 do {
1315 /* let's wait for avail or timeout */
1316 err = poll(&pfd, 1, timeout);
1317 if (err < 0)
1318 return -errno;
1319
1320 /* timeout ? */
1321 if (err == 0)
1322 return 0;
1323
1324 /* have we been interrupted ? */
1325 if (errno == -EINTR)
1326 continue;
1327
1328 /* check for any errors */
1329 if (pfd.revents & (POLLERR | POLLNVAL)) {
1330 switch (pcm_state(pcm)) {
1331 case PCM_STATE_XRUN:
1332 return -EPIPE;
1333 case PCM_STATE_SUSPENDED:
1334 return -ESTRPIPE;
1335 case PCM_STATE_DISCONNECTED:
1336 return -ENODEV;
1337 default:
1338 return -EIO;
1339 }
1340 }
1341 /* poll again if fd not ready for IO */
1342 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1343
1344 return 1;
1345}
1346
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001347int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001348{
1349 int err = 0, frames, avail;
1350 unsigned int offset = 0, count;
1351
1352 if (bytes == 0)
1353 return 0;
1354
1355 count = pcm_bytes_to_frames(pcm, bytes);
1356
1357 while (count > 0) {
1358
1359 /* get the available space for writing new frames */
1360 avail = pcm_avail_update(pcm);
1361 if (avail < 0) {
1362 fprintf(stderr, "cannot determine available mmap frames");
1363 return err;
1364 }
1365
1366 /* start the audio if we reach the threshold */
Taylor Holberton25976dc2017-04-10 11:46:40 -07001367 if (!pcm->running &&
Liam Girdwood6be28f12011-10-13 12:59:51 -07001368 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1369 if (pcm_start(pcm) < 0) {
1370 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1371 (unsigned int)pcm->mmap_status->hw_ptr,
1372 (unsigned int)pcm->mmap_control->appl_ptr,
1373 avail);
1374 return -errno;
1375 }
1376 }
1377
1378 /* sleep until we have space to write new frames */
1379 if (pcm->running &&
1380 (unsigned int)avail < pcm->mmap_control->avail_min) {
1381 int time = -1;
1382
1383 if (pcm->flags & PCM_NOIRQ)
1384 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1385 / pcm->noirq_frames_per_msec;
1386
1387 err = pcm_wait(pcm, time);
1388 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301389 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001390 pcm->running = 0;
1391 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1392 (unsigned int)pcm->mmap_status->hw_ptr,
1393 (unsigned int)pcm->mmap_control->appl_ptr,
1394 avail);
1395 pcm->mmap_control->appl_ptr = 0;
1396 return err;
1397 }
1398 continue;
1399 }
1400
1401 frames = count;
1402 if (frames > avail)
1403 frames = avail;
1404
1405 if (!frames)
1406 break;
1407
1408 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001409 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001410 if (frames < 0) {
1411 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1412 (unsigned int)pcm->mmap_status->hw_ptr,
1413 (unsigned int)pcm->mmap_control->appl_ptr,
1414 avail);
1415 return frames;
1416 }
1417
1418 offset += frames;
1419 count -= frames;
1420 }
1421
Liam Girdwood6be28f12011-10-13 12:59:51 -07001422 return 0;
1423}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001424
1425int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1426{
1427 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1428 return -ENOSYS;
1429
1430 return pcm_mmap_transfer(pcm, (void *)data, count);
1431}
1432
1433int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1434{
1435 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1436 return -ENOSYS;
1437
1438 return pcm_mmap_transfer(pcm, data, count);
1439}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301440
Taylor Holberton17a10242016-11-23 13:18:24 -08001441/** Gets the delay of the PCM, in terms of frames.
1442 * @param pcm A PCM handle.
1443 * @returns On success, the delay of the PCM.
1444 * On failure, a negative number.
1445 * @ingroup libtinyalsa-pcm
1446 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301447long pcm_get_delay(struct pcm *pcm)
1448{
1449 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1450 return -1;
1451
1452 return pcm->pcm_delay;
1453}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001454