blob: 41f072c04a88e7b0846e370078b9dd5a58d99399 [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;
367 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
368 return -1;
Taylor Holberton72e44222016-11-22 09:54:47 -0800369 return 0;
Eric Laurent40b018e2011-06-18 10:10:23 -0700370 }
Taylor Holberton72e44222016-11-22 09:54:47 -0800371 return -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700372}
373
Taylor Holberton4f556062016-09-16 09:54:36 -0400374static int pcm_hw_mmap_status(struct pcm *pcm)
375{
Eric Laurent40b018e2011-06-18 10:10:23 -0700376 if (pcm->sync_ptr)
377 return 0;
378
379 int page_size = sysconf(_SC_PAGE_SIZE);
380 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
381 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
382 if (pcm->mmap_status == MAP_FAILED)
383 pcm->mmap_status = NULL;
384 if (!pcm->mmap_status)
385 goto mmap_error;
386
387 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
388 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
389 if (pcm->mmap_control == MAP_FAILED)
390 pcm->mmap_control = NULL;
391 if (!pcm->mmap_control) {
392 munmap(pcm->mmap_status, page_size);
393 pcm->mmap_status = NULL;
394 goto mmap_error;
395 }
396 pcm->mmap_control->avail_min = 1;
397
398 return 0;
399
400mmap_error:
401
402 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
403 if (!pcm->sync_ptr)
404 return -ENOMEM;
405 pcm->mmap_status = &pcm->sync_ptr->s.status;
406 pcm->mmap_control = &pcm->sync_ptr->c.control;
407 pcm->mmap_control->avail_min = 1;
408 pcm_sync_ptr(pcm, 0);
409
410 return 0;
411}
412
413static void pcm_hw_munmap_status(struct pcm *pcm) {
414 if (pcm->sync_ptr) {
415 free(pcm->sync_ptr);
416 pcm->sync_ptr = NULL;
417 } else {
418 int page_size = sysconf(_SC_PAGE_SIZE);
419 if (pcm->mmap_status)
420 munmap(pcm->mmap_status, page_size);
421 if (pcm->mmap_control)
422 munmap(pcm->mmap_control, page_size);
423 }
424 pcm->mmap_status = NULL;
425 pcm->mmap_control = NULL;
426}
427
Liam Girdwood6be28f12011-10-13 12:59:51 -0700428static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700429 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700430 unsigned int frames)
431{
432 int size_bytes = pcm_frames_to_bytes(pcm, frames);
433 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
434 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
435
436 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700437 if (pcm->flags & PCM_IN)
438 memcpy(buf + src_offset_bytes,
439 (char*)pcm->mmap_buffer + pcm_offset_bytes,
440 size_bytes);
441 else
442 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
443 buf + src_offset_bytes,
444 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700445 return 0;
446}
447
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700448static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700449 unsigned int offset, unsigned int size)
450{
451 void *pcm_areas;
452 int commit;
453 unsigned int pcm_offset, frames, count = 0;
454
455 while (size > 0) {
456 frames = size;
457 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700458 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700459 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
460 if (commit < 0) {
461 oops(pcm, commit, "failed to commit %d frames\n", frames);
462 return commit;
463 }
464
465 offset += commit;
466 count += commit;
467 size -= commit;
468 }
469 return count;
470}
471
Taylor Holberton6d58e012016-10-01 18:32:30 -0400472/** Returns available frames in pcm buffer and corresponding time stamp.
473 * The clock is CLOCK_MONOTONIC if flag @ref PCM_MONOTONIC was specified in @ref pcm_open,
474 * otherwise the clock is CLOCK_REALTIME.
475 * For an input stream, frames available are frames ready for the application to read.
476 * For an output stream, frames available are the number of empty frames available for the application to write.
477 * Only available for PCMs opened with the @ref PCM_MMAP flag.
478 * @param pcm A PCM handle.
479 * @param avail The number of available frames
480 * @param tstamp The timestamp
481 * @return On success, zero is returned; on failure, negative one.
482 */
Eric Laurent40b018e2011-06-18 10:10:23 -0700483int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
484 struct timespec *tstamp)
485{
486 int frames;
487 int rc;
488 snd_pcm_uframes_t hw_ptr;
489
490 if (!pcm_is_ready(pcm))
491 return -1;
492
493 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
494 if (rc < 0)
495 return -1;
496
Eric Laurent7db48582011-11-17 11:47:59 -0800497 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
498 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800499 return -1;
500
Eric Laurent40b018e2011-06-18 10:10:23 -0700501 *tstamp = pcm->mmap_status->tstamp;
502 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
503 return -1;
504
505 hw_ptr = pcm->mmap_status->hw_ptr;
506 if (pcm->flags & PCM_IN)
507 frames = hw_ptr - pcm->mmap_control->appl_ptr;
508 else
509 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
510
511 if (frames < 0)
512 return -1;
513
514 *avail = (unsigned int)frames;
515
516 return 0;
517}
518
Taylor Holberton6d58e012016-10-01 18:32:30 -0400519/** Writes audio samples to PCM.
520 * If the PCM has not been started, it is started in this function.
521 * This function is only valid for PCMs opened with the @ref PCM_OUT flag.
522 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
523 * @param pcm A PCM handle.
524 * @param data The audio sample array
525 * @param count The number of bytes occupied by the sample array.
526 * @return On success, this function returns zero; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800527 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400528 */
Mark Brown6bbe77a2012-02-10 22:07:09 +0000529int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700530{
531 struct snd_xferi x;
532
533 if (pcm->flags & PCM_IN)
534 return -EINVAL;
535
Mark Brown6bbe77a2012-02-10 22:07:09 +0000536 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700537 x.frames = count / (pcm->config.channels *
538 pcm_format_to_bits(pcm->config.format) / 8);
Taylor Holberton2386a422016-11-18 20:38:40 -0800539 x.result = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700540 for (;;) {
541 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530542 int prepare_error = pcm_prepare(pcm);
543 if (prepare_error)
544 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700545 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
546 return oops(pcm, errno, "cannot write initial data");
547 pcm->running = 1;
548 return 0;
549 }
550 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530551 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700552 pcm->running = 0;
553 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700554 /* we failed to make our window -- try to restart if we are
555 * allowed to do so. Otherwise, simply allow the EPIPE error to
556 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700557 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700558 if (pcm->flags & PCM_NORESTART)
559 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700560 continue;
561 }
562 return oops(pcm, errno, "cannot write stream data");
563 }
564 return 0;
565 }
566}
567
Taylor Holberton6d58e012016-10-01 18:32:30 -0400568/** Reads audio samples from PCM.
569 * If the PCM has not been started, it is started in this function.
570 * This function is only valid for PCMs opened with the @ref PCM_IN flag.
571 * This function is not valid for PCMs opened with the @ref PCM_MMAP flag.
572 * @param pcm A PCM handle.
573 * @param data The audio sample array
574 * @param count The number of bytes occupied by the sample array.
575 * @return On success, this function returns zero; otherwise, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800576 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400577 */
Simon Wilson79d39652011-05-25 13:44:23 -0700578int pcm_read(struct pcm *pcm, void *data, unsigned int count)
579{
580 struct snd_xferi x;
581
582 if (!(pcm->flags & PCM_IN))
583 return -EINVAL;
584
585 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700586 x.frames = count / (pcm->config.channels *
587 pcm_format_to_bits(pcm->config.format) / 8);
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 }
606 return 0;
607 }
608}
609
610static struct pcm bad_pcm = {
611 .fd = -1,
612};
613
Taylor Holberton6d58e012016-10-01 18:32:30 -0400614/** Gets the hardware parameters of a PCM, without created a PCM handle.
615 * @param card The card of the PCM.
616 * The default card is zero.
617 * @param device The device of the PCM.
618 * The default device is zero.
619 * @param flags Specifies whether the PCM is an input or output.
620 * May be one of the following:
621 * - @ref PCM_IN
622 * - @ref PCM_OUT
623 * @return On success, the hardware parameters of the PCM; on failure, NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800624 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400625 */
Simon Wilson43544882012-10-31 12:52:39 -0700626struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
627 unsigned int flags)
628{
629 struct snd_pcm_hw_params *params;
630 char fn[256];
631 int fd;
632
633 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
634 flags & PCM_IN ? 'c' : 'p');
635
636 fd = open(fn, O_RDWR);
637 if (fd < 0) {
638 fprintf(stderr, "cannot open device '%s'\n", fn);
639 goto err_open;
640 }
641
642 params = calloc(1, sizeof(struct snd_pcm_hw_params));
643 if (!params)
644 goto err_calloc;
645
646 param_init(params);
647 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
648 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
649 goto err_hw_refine;
650 }
651
652 close(fd);
653
654 return (struct pcm_params *)params;
655
656err_hw_refine:
657 free(params);
658err_calloc:
659 close(fd);
660err_open:
661 return NULL;
662}
663
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500664/** Frees the hardware parameters returned by @ref pcm_params_get.
Taylor Holberton6d58e012016-10-01 18:32:30 -0400665 * @param pcm_params Hardware parameters of a PCM.
666 * May be NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800667 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400668 */
Simon Wilson43544882012-10-31 12:52:39 -0700669void pcm_params_free(struct pcm_params *pcm_params)
670{
671 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
672
673 if (params)
674 free(params);
675}
676
677static int pcm_param_to_alsa(enum pcm_param param)
678{
679 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700680 case PCM_PARAM_ACCESS:
681 return SNDRV_PCM_HW_PARAM_ACCESS;
682 case PCM_PARAM_FORMAT:
683 return SNDRV_PCM_HW_PARAM_FORMAT;
684 case PCM_PARAM_SUBFORMAT:
685 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700686 case PCM_PARAM_SAMPLE_BITS:
687 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
688 break;
689 case PCM_PARAM_FRAME_BITS:
690 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
691 break;
692 case PCM_PARAM_CHANNELS:
693 return SNDRV_PCM_HW_PARAM_CHANNELS;
694 break;
695 case PCM_PARAM_RATE:
696 return SNDRV_PCM_HW_PARAM_RATE;
697 break;
698 case PCM_PARAM_PERIOD_TIME:
699 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
700 break;
701 case PCM_PARAM_PERIOD_SIZE:
702 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
703 break;
704 case PCM_PARAM_PERIOD_BYTES:
705 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
706 break;
707 case PCM_PARAM_PERIODS:
708 return SNDRV_PCM_HW_PARAM_PERIODS;
709 break;
710 case PCM_PARAM_BUFFER_TIME:
711 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
712 break;
713 case PCM_PARAM_BUFFER_SIZE:
714 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
715 break;
716 case PCM_PARAM_BUFFER_BYTES:
717 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
718 break;
719 case PCM_PARAM_TICK_TIME:
720 return SNDRV_PCM_HW_PARAM_TICK_TIME;
721 break;
722
723 default:
724 return -1;
725 }
726}
727
Taylor Holberton6d58e012016-10-01 18:32:30 -0400728/** Gets a mask from a PCM's hardware parameters.
729 * @param pcm_params A PCM's hardware parameters.
730 * @param param The parameter to get.
731 * @return If @p pcm_params is NULL or @p param is not a mask, NULL is returned.
732 * Otherwise, the mask associated with @p param is returned.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800733 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400734 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800735const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params,
Andy Hungad807622014-03-10 18:08:15 -0700736 enum pcm_param param)
737{
738 int p;
739 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
740 if (params == NULL) {
741 return NULL;
742 }
743
744 p = pcm_param_to_alsa(param);
745 if (p < 0 || !param_is_mask(p)) {
746 return NULL;
747 }
748
Taylor Holberton2f387d22016-12-01 15:58:16 -0800749 return (const struct pcm_mask *)param_to_mask(params, p);
Andy Hungad807622014-03-10 18:08:15 -0700750}
751
Taylor Holberton17a10242016-11-23 13:18:24 -0800752/** Get the minimum of a specified PCM parameter.
753 * @param pcm_params A PCM parameters structure.
754 * @param param The specified parameter to get the minimum of.
755 * @returns On success, the parameter minimum.
756 * On failure, zero.
757 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800758unsigned int pcm_params_get_min(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700759 enum pcm_param param)
760{
761 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
762 int p;
763
764 if (!params)
765 return 0;
766
767 p = pcm_param_to_alsa(param);
768 if (p < 0)
769 return 0;
770
771 return param_get_min(params, p);
772}
773
Taylor Holberton17a10242016-11-23 13:18:24 -0800774/** Get the maximum of a specified PCM parameter.
775 * @param pcm_params A PCM parameters structure.
776 * @param param The specified parameter to get the maximum of.
777 * @returns On success, the parameter maximum.
778 * On failure, zero.
779 */
Taylor Holberton2f387d22016-12-01 15:58:16 -0800780unsigned int pcm_params_get_max(const struct pcm_params *pcm_params,
Simon Wilson43544882012-10-31 12:52:39 -0700781 enum pcm_param param)
782{
Taylor Holberton2f387d22016-12-01 15:58:16 -0800783 const struct snd_pcm_hw_params *params = (const struct snd_pcm_hw_params *)pcm_params;
Simon Wilson43544882012-10-31 12:52:39 -0700784 int p;
785
786 if (!params)
787 return 0;
788
789 p = pcm_param_to_alsa(param);
790 if (p < 0)
791 return 0;
792
793 return param_get_max(params, p);
794}
795
Taylor Holberton6d58e012016-10-01 18:32:30 -0400796/** Closes a PCM returned by @ref pcm_open.
797 * @param pcm A PCM returned by @ref pcm_open.
798 * May not be NULL.
799 * @return Always returns zero.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800800 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400801 */
Simon Wilson79d39652011-05-25 13:44:23 -0700802int pcm_close(struct pcm *pcm)
803{
804 if (pcm == &bad_pcm)
805 return 0;
806
Eric Laurent40b018e2011-06-18 10:10:23 -0700807 pcm_hw_munmap_status(pcm);
808
Liam Girdwood6be28f12011-10-13 12:59:51 -0700809 if (pcm->flags & PCM_MMAP) {
810 pcm_stop(pcm);
811 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
812 }
813
Simon Wilson79d39652011-05-25 13:44:23 -0700814 if (pcm->fd >= 0)
815 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530816 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700817 pcm->running = 0;
818 pcm->buffer_size = 0;
819 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700820 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700821 return 0;
822}
823
Taylor Holberton6d58e012016-10-01 18:32:30 -0400824/** Opens a PCM.
825 * @param card The card that the pcm belongs to.
826 * The default card is zero.
827 * @param device The device that the pcm belongs to.
828 * The default device is zero.
829 * @param flags Specify characteristics and functionality about the pcm.
830 * May be a bitwise AND of the following:
831 * - @ref PCM_IN
832 * - @ref PCM_OUT
833 * - @ref PCM_MMAP
834 * - @ref PCM_NOIRQ
835 * - @ref PCM_MONOTONIC
836 * @param config The hardware and software parameters to open the PCM with.
837 * @returns On success, returns an initialized pcm, ready for reading or writing.
838 * On error, returns NULL.
Taylor Holberton8e1b1022016-11-19 10:34:50 -0800839 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -0400840 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700841struct pcm *pcm_open(unsigned int card, unsigned int device,
Taylor Holberton94803b02016-12-01 16:07:14 -0800842 unsigned int flags, const struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700843{
Simon Wilson79d39652011-05-25 13:44:23 -0700844 struct pcm *pcm;
845 struct snd_pcm_info info;
846 struct snd_pcm_hw_params params;
847 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700848 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700849 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700850
851 pcm = calloc(1, sizeof(struct pcm));
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400852 if (!pcm)
853 return &bad_pcm;
Simon Wilson79d39652011-05-25 13:44:23 -0700854
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400855 if (config == NULL) {
856 config = &pcm->config;
Taylor Holberton94803b02016-12-01 16:07:14 -0800857 pcm->config.channels = 2;
858 pcm->config.rate = 48000;
859 pcm->config.period_size = 1024;
860 pcm->config.period_count = 4;
861 pcm->config.format = PCM_FORMAT_S16_LE;
862 pcm->config.start_threshold = config->period_count * config->period_size;
863 pcm->config.stop_threshold = config->period_count * config->period_size;
864 pcm->config.silence_threshold = 0;
Taylor Holbertonf319eb02016-10-14 20:05:30 -0400865 } else {
866 pcm->config = *config;
867 }
Simon Wilson79d39652011-05-25 13:44:23 -0700868
Simon Wilson1bd580f2011-06-02 15:58:41 -0700869 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
870 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700871
872 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700873 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700874 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700875 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700876 return pcm;
877 }
878
879 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700880 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700881 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700882 }
David Wagner4cddf192014-04-02 15:12:54 +0200883 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700884
885 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700886 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
887 pcm_format_to_alsa(config->format));
888 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
889 SNDRV_PCM_SUBFORMAT_STD);
890 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
891 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
892 pcm_format_to_bits(config->format));
893 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
894 pcm_format_to_bits(config->format) * config->channels);
895 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
896 config->channels);
897 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
898 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
899
Liam Girdwood6be28f12011-10-13 12:59:51 -0700900 if (flags & PCM_NOIRQ) {
901
902 if (!(flags & PCM_MMAP)) {
903 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
904 goto fail;
905 }
906
907 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
908 pcm->noirq_frames_per_msec = config->rate / 1000;
909 }
910
911 if (flags & PCM_MMAP)
912 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
913 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
914 else
915 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
916 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
917
Simon Wilson79d39652011-05-25 13:44:23 -0700918 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
919 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700920 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700921 }
922
Liam Girdwood6be28f12011-10-13 12:59:51 -0700923 /* get our refined hw_params */
Taylor Holberton94803b02016-12-01 16:07:14 -0800924 pcm->config.period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
925 pcm->config.period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700926 pcm->buffer_size = config->period_count * config->period_size;
927
928 if (flags & PCM_MMAP) {
929 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
930 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
931 if (pcm->mmap_buffer == MAP_FAILED) {
932 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
933 pcm_frames_to_bytes(pcm, pcm->buffer_size));
934 goto fail_close;
935 }
936 }
937
938
Simon Wilson79d39652011-05-25 13:44:23 -0700939 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700940 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700941 sparams.period_step = 1;
942 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700943
Eric Laurent93e7b672012-08-22 16:18:14 -0700944 if (!config->start_threshold) {
945 if (pcm->flags & PCM_IN)
946 pcm->config.start_threshold = sparams.start_threshold = 1;
947 else
948 pcm->config.start_threshold = sparams.start_threshold =
949 config->period_count * config->period_size / 2;
950 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700951 sparams.start_threshold = config->start_threshold;
952
Liam Girdwood6be28f12011-10-13 12:59:51 -0700953 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800954 if (!config->stop_threshold) {
955 if (pcm->flags & PCM_IN)
956 pcm->config.stop_threshold = sparams.stop_threshold =
957 config->period_count * config->period_size * 10;
958 else
959 pcm->config.stop_threshold = sparams.stop_threshold =
960 config->period_count * config->period_size;
961 }
John Grossman3bb114a2011-07-21 10:59:55 -0700962 else
963 sparams.stop_threshold = config->stop_threshold;
964
Simon Wilson79d39652011-05-25 13:44:23 -0700965 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
966 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700967 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700968 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700969
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600970 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700971 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700972
973 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
974 oops(pcm, errno, "cannot set sw params");
975 goto fail;
976 }
977
Eric Laurent40b018e2011-06-18 10:10:23 -0700978 rc = pcm_hw_mmap_status(pcm);
979 if (rc < 0) {
980 oops(pcm, rc, "mmap status failed");
981 goto fail;
982 }
983
Glenn Kasten81012402013-08-22 15:11:48 -0700984#ifdef SNDRV_PCM_IOCTL_TTSTAMP
985 if (pcm->flags & PCM_MONOTONIC) {
986 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
987 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
988 if (rc < 0) {
989 oops(pcm, rc, "cannot set timestamp type");
990 goto fail;
991 }
992 }
993#endif
994
Simon Wilson79d39652011-05-25 13:44:23 -0700995 pcm->underruns = 0;
996 return pcm;
997
998fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700999 if (flags & PCM_MMAP)
1000 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
1001fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -07001002 close(pcm->fd);
1003 pcm->fd = -1;
1004 return pcm;
1005}
1006
Taylor Holberton6d58e012016-10-01 18:32:30 -04001007/** Checks if a PCM file has been opened without error.
1008 * @param pcm A PCM handle.
1009 * @return If a PCM's file descriptor is not valid, it returns zero.
1010 * Otherwise, the function returns one.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001011 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001012 */
Taylor Holberton15d58482016-12-01 17:46:29 -08001013int pcm_is_ready(const struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -07001014{
1015 return pcm->fd >= 0;
1016}
Simon Wilsond6458e62011-06-21 14:58:11 -07001017
Taylor Holberton6d58e012016-10-01 18:32:30 -04001018/** Prepares a PCM, if it has not been prepared already.
1019 * @param pcm A PCM handle.
1020 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001021 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001022 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301023int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -07001024{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301025 if (pcm->prepared)
1026 return 0;
1027
Simon Wilsond6458e62011-06-21 14:58:11 -07001028 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1029 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -07001030
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301031 pcm->prepared = 1;
1032 return 0;
1033}
1034
Taylor Holberton6d58e012016-10-01 18:32:30 -04001035/** Starts a PCM.
1036 * If the PCM has not been prepared,
1037 * it is prepared in this function.
1038 * @param pcm A PCM handle.
1039 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001040 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001041 */
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301042int pcm_start(struct pcm *pcm)
1043{
1044 int prepare_error = pcm_prepare(pcm);
1045 if (prepare_error)
1046 return prepare_error;
1047
Liam Girdwood6be28f12011-10-13 12:59:51 -07001048 if (pcm->flags & PCM_MMAP)
1049 pcm_sync_ptr(pcm, 0);
1050
Simon Wilsond6458e62011-06-21 14:58:11 -07001051 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1052 return oops(pcm, errno, "cannot start channel");
1053
Liam Girdwood6be28f12011-10-13 12:59:51 -07001054 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -07001055 return 0;
1056}
1057
Taylor Holberton6d58e012016-10-01 18:32:30 -04001058/** Stops a PCM.
1059 * @param pcm A PCM handle.
1060 * @return On success, zero; on failure, a negative number.
Taylor Holberton8e1b1022016-11-19 10:34:50 -08001061 * @ingroup libtinyalsa-pcm
Taylor Holberton6d58e012016-10-01 18:32:30 -04001062 */
Simon Wilsond6458e62011-06-21 14:58:11 -07001063int pcm_stop(struct pcm *pcm)
1064{
1065 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1066 return oops(pcm, errno, "cannot stop channel");
1067
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301068 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001069 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -07001070 return 0;
1071}
1072
Liam Girdwood6be28f12011-10-13 12:59:51 -07001073static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1074{
1075 int avail;
1076
1077 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1078
1079 if (avail < 0)
1080 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +08001081 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001082 avail -= pcm->boundary;
1083
1084 return avail;
1085}
1086
1087static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1088{
1089 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1090 if (avail < 0)
1091 avail += pcm->boundary;
1092 return avail;
1093}
1094
1095static inline int pcm_mmap_avail(struct pcm *pcm)
1096{
1097 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1098 if (pcm->flags & PCM_IN)
1099 return pcm_mmap_capture_avail(pcm);
1100 else
1101 return pcm_mmap_playback_avail(pcm);
1102}
1103
1104static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1105{
1106 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1107 appl_ptr += frames;
1108
1109 /* check for boundary wrap */
1110 if (appl_ptr > pcm->boundary)
1111 appl_ptr -= pcm->boundary;
1112 pcm->mmap_control->appl_ptr = appl_ptr;
1113}
1114
1115int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1116 unsigned int *frames)
1117{
1118 unsigned int continuous, copy_frames, avail;
1119
1120 /* return the mmap buffer */
1121 *areas = pcm->mmap_buffer;
1122
1123 /* and the application offset in frames */
1124 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1125
1126 avail = pcm_mmap_avail(pcm);
1127 if (avail > pcm->buffer_size)
1128 avail = pcm->buffer_size;
1129 continuous = pcm->buffer_size - *offset;
1130
1131 /* we can only copy frames if the are availabale and continuos */
1132 copy_frames = *frames;
1133 if (copy_frames > avail)
1134 copy_frames = avail;
1135 if (copy_frames > continuous)
1136 copy_frames = continuous;
1137 *frames = copy_frames;
1138
1139 return 0;
1140}
1141
1142int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
1143{
Taylor Holberton72e44222016-11-22 09:54:47 -08001144 int ret;
1145
Taylor Holberton73466c02016-10-01 12:51:59 -04001146 /* not used */
1147 (void) offset;
1148
Liam Girdwood6be28f12011-10-13 12:59:51 -07001149 /* update the application pointer in userspace and kernel */
1150 pcm_mmap_appl_forward(pcm, frames);
Taylor Holberton72e44222016-11-22 09:54:47 -08001151 ret = pcm_sync_ptr(pcm, 0);
1152 if (ret != 0)
1153 return ret;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001154
1155 return frames;
1156}
1157
1158int pcm_avail_update(struct pcm *pcm)
1159{
1160 pcm_sync_ptr(pcm, 0);
1161 return pcm_mmap_avail(pcm);
1162}
1163
1164int pcm_state(struct pcm *pcm)
1165{
1166 int err = pcm_sync_ptr(pcm, 0);
1167 if (err < 0)
1168 return err;
1169
1170 return pcm->mmap_status->state;
1171}
1172
Taylor Holberton17a10242016-11-23 13:18:24 -08001173/** Waits for frames to be available for read or write operations.
1174 * @param pcm A PCM handle.
1175 * @param timeout The maximum amount of time to wait for, in terms of milliseconds.
1176 * @returns If frames became available, one is returned.
1177 * If a timeout occured, zero is returned.
1178 * If an error occured, a negative number is returned.
1179 * @ingroup libtinyalsa-pcm
1180 */
Liam Girdwood6be28f12011-10-13 12:59:51 -07001181int pcm_wait(struct pcm *pcm, int timeout)
1182{
1183 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001184 int err;
1185
1186 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +01001187 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001188
1189 do {
1190 /* let's wait for avail or timeout */
1191 err = poll(&pfd, 1, timeout);
1192 if (err < 0)
1193 return -errno;
1194
1195 /* timeout ? */
1196 if (err == 0)
1197 return 0;
1198
1199 /* have we been interrupted ? */
1200 if (errno == -EINTR)
1201 continue;
1202
1203 /* check for any errors */
1204 if (pfd.revents & (POLLERR | POLLNVAL)) {
1205 switch (pcm_state(pcm)) {
1206 case PCM_STATE_XRUN:
1207 return -EPIPE;
1208 case PCM_STATE_SUSPENDED:
1209 return -ESTRPIPE;
1210 case PCM_STATE_DISCONNECTED:
1211 return -ENODEV;
1212 default:
1213 return -EIO;
1214 }
1215 }
1216 /* poll again if fd not ready for IO */
1217 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1218
1219 return 1;
1220}
1221
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001222int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -07001223{
1224 int err = 0, frames, avail;
1225 unsigned int offset = 0, count;
1226
1227 if (bytes == 0)
1228 return 0;
1229
1230 count = pcm_bytes_to_frames(pcm, bytes);
1231
1232 while (count > 0) {
1233
1234 /* get the available space for writing new frames */
1235 avail = pcm_avail_update(pcm);
1236 if (avail < 0) {
1237 fprintf(stderr, "cannot determine available mmap frames");
1238 return err;
1239 }
1240
1241 /* start the audio if we reach the threshold */
1242 if (!pcm->running &&
1243 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1244 if (pcm_start(pcm) < 0) {
1245 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1246 (unsigned int)pcm->mmap_status->hw_ptr,
1247 (unsigned int)pcm->mmap_control->appl_ptr,
1248 avail);
1249 return -errno;
1250 }
1251 }
1252
1253 /* sleep until we have space to write new frames */
1254 if (pcm->running &&
1255 (unsigned int)avail < pcm->mmap_control->avail_min) {
1256 int time = -1;
1257
1258 if (pcm->flags & PCM_NOIRQ)
1259 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1260 / pcm->noirq_frames_per_msec;
1261
1262 err = pcm_wait(pcm, time);
1263 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301264 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001265 pcm->running = 0;
1266 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1267 (unsigned int)pcm->mmap_status->hw_ptr,
1268 (unsigned int)pcm->mmap_control->appl_ptr,
1269 avail);
1270 pcm->mmap_control->appl_ptr = 0;
1271 return err;
1272 }
1273 continue;
1274 }
1275
1276 frames = count;
1277 if (frames > avail)
1278 frames = avail;
1279
1280 if (!frames)
1281 break;
1282
1283 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001284 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001285 if (frames < 0) {
1286 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1287 (unsigned int)pcm->mmap_status->hw_ptr,
1288 (unsigned int)pcm->mmap_control->appl_ptr,
1289 avail);
1290 return frames;
1291 }
1292
1293 offset += frames;
1294 count -= frames;
1295 }
1296
Liam Girdwood6be28f12011-10-13 12:59:51 -07001297 return 0;
1298}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001299
1300int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1301{
1302 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1303 return -ENOSYS;
1304
1305 return pcm_mmap_transfer(pcm, (void *)data, count);
1306}
1307
1308int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1309{
1310 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1311 return -ENOSYS;
1312
1313 return pcm_mmap_transfer(pcm, data, count);
1314}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301315
Taylor Holberton17a10242016-11-23 13:18:24 -08001316/** Gets the delay of the PCM, in terms of frames.
1317 * @param pcm A PCM handle.
1318 * @returns On success, the delay of the PCM.
1319 * On failure, a negative number.
1320 * @ingroup libtinyalsa-pcm
1321 */
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301322long pcm_get_delay(struct pcm *pcm)
1323{
1324 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1325 return -1;
1326
1327 return pcm->pcm_delay;
1328}
Taylor Holberton6d58e012016-10-01 18:32:30 -04001329