blob: 2933dec5a50df69417cf8bd3eeb6a5558e9ca78a [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>
36
37#include <sys/ioctl.h>
38#include <sys/mman.h>
39#include <sys/time.h>
40
41#include <linux/ioctl.h>
42#define __force
43#define __bitwise
44#define __user
45#include <sound/asound.h>
46
47#include <tinyalsa/asoundlib.h>
48
49#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
50
51static inline int param_is_mask(int p)
52{
53 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
54 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
55}
56
57static inline int param_is_interval(int p)
58{
59 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
60 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
61}
62
63static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
64{
65 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
66}
67
68static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
69{
70 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
71}
72
73static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
74{
75 if (bit >= SNDRV_MASK_MAX)
76 return;
77 if (param_is_mask(n)) {
78 struct snd_mask *m = param_to_mask(p, n);
79 m->bits[0] = 0;
80 m->bits[1] = 0;
81 m->bits[bit >> 5] |= (1 << (bit & 31));
82 }
83}
84
85static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
86{
87 if (param_is_interval(n)) {
88 struct snd_interval *i = param_to_interval(p, n);
89 i->min = val;
90 }
91}
92
93static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
94{
95 if (param_is_interval(n)) {
96 struct snd_interval *i = param_to_interval(p, n);
97 i->max = val;
98 }
99}
100
101static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
102{
103 if (param_is_interval(n)) {
104 struct snd_interval *i = param_to_interval(p, n);
105 i->min = val;
106 i->max = val;
107 i->integer = 1;
108 }
109}
110
111static void param_init(struct snd_pcm_hw_params *p)
112{
113 int n;
114 memset(p, 0, sizeof(*p));
115 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
116 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
117 struct snd_mask *m = param_to_mask(p, n);
118 m->bits[0] = ~0;
119 m->bits[1] = ~0;
120 }
121 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
122 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
123 struct snd_interval *i = param_to_interval(p, n);
124 i->min = 0;
125 i->max = ~0;
126 }
127}
128
129#define PCM_ERROR_MAX 128
130
131struct pcm {
132 int fd;
133 unsigned int flags;
134 int running:1;
135 int underruns;
136 unsigned int buffer_size;
137 char error[PCM_ERROR_MAX];
138 struct pcm_config config;
139};
140
Simon Wilson851aa5c2011-05-30 21:18:26 -0700141unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700142{
143 return pcm->buffer_size;
144}
145
146const char* pcm_get_error(struct pcm *pcm)
147{
148 return pcm->error;
149}
150
151static int oops(struct pcm *pcm, int e, const char *fmt, ...)
152{
153 va_list ap;
154 int sz;
155
156 va_start(ap, fmt);
157 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
158 va_end(ap);
159 sz = strlen(pcm->error);
160
161 if (errno)
162 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
163 ": %s", strerror(e));
164 return -1;
165}
166
167int pcm_write(struct pcm *pcm, void *data, unsigned int count)
168{
169 struct snd_xferi x;
170
171 if (pcm->flags & PCM_IN)
172 return -EINVAL;
173
174 x.buf = data;
175 x.frames = count / (pcm->config.channels * 2); /* TODO: handle 32bit */
176
177 for (;;) {
178 if (!pcm->running) {
179 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
180 return oops(pcm, errno, "cannot prepare channel");
181 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
182 return oops(pcm, errno, "cannot write initial data");
183 pcm->running = 1;
184 return 0;
185 }
186 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
187 pcm->running = 0;
188 if (errno == EPIPE) {
189 /* we failed to make our window -- try to restart */
190 pcm->underruns++;
191 continue;
192 }
193 return oops(pcm, errno, "cannot write stream data");
194 }
195 return 0;
196 }
197}
198
199int pcm_read(struct pcm *pcm, void *data, unsigned int count)
200{
201 struct snd_xferi x;
202
203 if (!(pcm->flags & PCM_IN))
204 return -EINVAL;
205
206 x.buf = data;
207 x.frames = count / (pcm->config.channels * 2); /* TODO: handle 32bit */
208
209 for (;;) {
210 if (!pcm->running) {
211 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE))
212 return oops(pcm, errno, "cannot prepare channel");
213 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START))
214 return oops(pcm, errno, "cannot start channel");
215 pcm->running = 1;
216 }
217 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
218 pcm->running = 0;
219 if (errno == EPIPE) {
220 /* we failed to make our window -- try to restart */
221 pcm->underruns++;
222 continue;
223 }
224 return oops(pcm, errno, "cannot read stream data");
225 }
226 return 0;
227 }
228}
229
230static struct pcm bad_pcm = {
231 .fd = -1,
232};
233
234int pcm_close(struct pcm *pcm)
235{
236 if (pcm == &bad_pcm)
237 return 0;
238
239 if (pcm->fd >= 0)
240 close(pcm->fd);
241 pcm->running = 0;
242 pcm->buffer_size = 0;
243 pcm->fd = -1;
244 return 0;
245}
246
247static unsigned int pcm_format_to_alsa(enum pcm_format format)
248{
249 switch (format) {
250 case PCM_FORMAT_S32_LE:
251 return SNDRV_PCM_FORMAT_S32_LE;
252 default:
253 case PCM_FORMAT_S16_LE:
254 return SNDRV_PCM_FORMAT_S16_LE;
255 };
256}
257
258static unsigned int pcm_format_to_bits(enum pcm_format format)
259{
260 switch (format) {
261 case PCM_FORMAT_S32_LE:
262 return 32;
263 default:
264 case PCM_FORMAT_S16_LE:
265 return 16;
266 };
267}
268
Simon Wilson1bd580f2011-06-02 15:58:41 -0700269struct pcm *pcm_open(unsigned int card, unsigned int device,
270 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700271{
Simon Wilson79d39652011-05-25 13:44:23 -0700272 struct pcm *pcm;
273 struct snd_pcm_info info;
274 struct snd_pcm_hw_params params;
275 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700276 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700277
278 pcm = calloc(1, sizeof(struct pcm));
279 if (!pcm || !config)
280 return &bad_pcm; /* TODO: could support default config here */
281
282 pcm->config = *config;
283
Simon Wilson1bd580f2011-06-02 15:58:41 -0700284 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
285 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700286
287 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700288 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700289 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700290 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700291 return pcm;
292 }
293
294 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700295 oops(pcm, errno, "cannot get info");
Simon Wilson79d39652011-05-25 13:44:23 -0700296 goto fail;
297 }
298
299 param_init(&params);
300 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
301 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
302 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
303 pcm_format_to_alsa(config->format));
304 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
305 SNDRV_PCM_SUBFORMAT_STD);
306 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
307 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
308 pcm_format_to_bits(config->format));
309 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
310 pcm_format_to_bits(config->format) * config->channels);
311 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
312 config->channels);
313 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
314 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
315
316 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
317 oops(pcm, errno, "cannot set hw params");
318 goto fail;
319 }
320
321 memset(&sparams, 0, sizeof(sparams));
322 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
323 sparams.period_step = 1;
324 sparams.avail_min = 1;
325 sparams.start_threshold = config->period_count * config->period_size;
326 sparams.stop_threshold = config->period_count * config->period_size;
327 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
328 sparams.silence_size = 0;
329 sparams.silence_threshold = 0;
330
331 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
332 oops(pcm, errno, "cannot set sw params");
333 goto fail;
334 }
335
336 pcm->buffer_size = config->period_count * config->period_size;
337 pcm->underruns = 0;
338 return pcm;
339
340fail:
341 close(pcm->fd);
342 pcm->fd = -1;
343 return pcm;
344}
345
346int pcm_is_ready(struct pcm *pcm)
347{
348 return pcm->fd >= 0;
349}