Don't stop hardware on buffer underruns. Instead continue playing to guarantee that...
[profile/ivi/pulseaudio-panda.git] / src / modules / alsa-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include <asoundlib.h>
31
32 #include <pulse/sample.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37
38 #include "alsa-util.h"
39
40 struct pa_alsa_fdlist {
41     int num_fds;
42     struct pollfd *fds;
43     /* This is a temporary buffer used to avoid lots of mallocs */
44     struct pollfd *work_fds;
45
46     snd_mixer_t *mixer;
47
48     pa_mainloop_api *m;
49     pa_defer_event *defer;
50     pa_io_event **ios;
51
52     int polled;
53
54     void (*cb)(void *userdata);
55     void *userdata;
56 };
57
58 static void io_cb(pa_mainloop_api*a, pa_io_event* e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void *userdata) {
59     
60     struct pa_alsa_fdlist *fdl = userdata;
61     int err, i;
62     unsigned short revents;
63
64     pa_assert(a);
65     pa_assert(fdl);
66     pa_assert(fdl->mixer);
67     pa_assert(fdl->fds);
68     pa_assert(fdl->work_fds);
69
70     if (fdl->polled)
71         return;
72
73     fdl->polled = 1;
74
75     memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
76
77     for (i = 0;i < fdl->num_fds; i++) {
78         if (e == fdl->ios[i]) {
79             if (events & PA_IO_EVENT_INPUT)
80                 fdl->work_fds[i].revents |= POLLIN;
81             if (events & PA_IO_EVENT_OUTPUT)
82                 fdl->work_fds[i].revents |= POLLOUT;
83             if (events & PA_IO_EVENT_ERROR)
84                 fdl->work_fds[i].revents |= POLLERR;
85             if (events & PA_IO_EVENT_HANGUP)
86                 fdl->work_fds[i].revents |= POLLHUP;
87             break;
88         }
89     }
90
91     assert(i != fdl->num_fds);
92
93     if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) {
94         pa_log_error("Unable to get poll revent: %s", snd_strerror(err));
95         return;
96     }
97
98     a->defer_enable(fdl->defer, 1);
99
100     if (revents)
101         snd_mixer_handle_events(fdl->mixer);
102 }
103
104 static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event* e, void *userdata) {
105     struct pa_alsa_fdlist *fdl = userdata;
106     int num_fds, i, err;
107     struct pollfd *temp;
108
109     pa_assert(a);
110     pa_assert(fdl);
111     pa_assert(fdl->mixer);
112
113     a->defer_enable(fdl->defer, 0);
114
115     num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
116     pa_assert(num_fds > 0);
117
118     if (num_fds != fdl->num_fds) {
119         if (fdl->fds)
120             pa_xfree(fdl->fds);
121         if (fdl->work_fds)
122             pa_xfree(fdl->work_fds);
123         fdl->fds = pa_xnew0(struct pollfd, num_fds);
124         fdl->work_fds = pa_xnew(struct pollfd, num_fds);
125     }
126
127     memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
128
129     if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) {
130         pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err));
131         return;
132     }
133
134     fdl->polled = 0;
135
136     if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
137         return;
138
139     if (fdl->ios) {
140         for (i = 0; i < fdl->num_fds; i++)
141             a->io_free(fdl->ios[i]);
142         
143         if (num_fds != fdl->num_fds) {
144             pa_xfree(fdl->ios);
145             fdl->ios = NULL;
146         }
147     }
148
149     if (!fdl->ios)
150         fdl->ios = pa_xnew(pa_io_event*, num_fds);
151
152     /* Swap pointers */
153     temp = fdl->work_fds;
154     fdl->work_fds = fdl->fds;
155     fdl->fds = temp;
156
157     fdl->num_fds = num_fds;
158
159     for (i = 0;i < num_fds;i++)
160         fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
161             ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
162             ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
163             io_cb, fdl);
164 }
165
166 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
167     struct pa_alsa_fdlist *fdl;
168
169     fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
170
171     fdl->num_fds = 0;
172     fdl->fds = NULL;
173     fdl->work_fds = NULL;
174     fdl->mixer = NULL;
175     fdl->m = NULL;
176     fdl->defer = NULL;
177     fdl->ios = NULL;
178     fdl->polled = 0;
179
180     return fdl;
181 }
182
183 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
184     pa_assert(fdl);
185
186     if (fdl->defer) {
187         pa_assert(fdl->m);
188         fdl->m->defer_free(fdl->defer);
189     }
190
191     if (fdl->ios) {
192         int i;
193         pa_assert(fdl->m);
194         for (i = 0;i < fdl->num_fds;i++)
195             fdl->m->io_free(fdl->ios[i]);
196         pa_xfree(fdl->ios);
197     }
198
199     if (fdl->fds)
200         pa_xfree(fdl->fds);
201     if (fdl->work_fds)
202         pa_xfree(fdl->work_fds);
203
204     pa_xfree(fdl);
205 }
206
207 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
208     pa_assert(fdl);
209     pa_assert(mixer_handle);
210     pa_assert(m);
211     pa_assert(!fdl->m);
212
213     fdl->mixer = mixer_handle;
214     fdl->m = m;
215     fdl->defer = m->defer_new(m, defer_cb, fdl);
216
217     return 0;
218 }
219
220 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
221
222     static const snd_pcm_format_t format_trans[] = {
223         [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
224         [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
225         [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
226         [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
227         [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
228         [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
229         [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
230     };
231
232     static const pa_sample_format_t try_order[] = {
233         PA_SAMPLE_S16NE,
234         PA_SAMPLE_S16RE,
235         PA_SAMPLE_FLOAT32NE,
236         PA_SAMPLE_FLOAT32RE,
237         PA_SAMPLE_ULAW,
238         PA_SAMPLE_ALAW,
239         PA_SAMPLE_U8,
240         PA_SAMPLE_INVALID
241     };
242
243     int i, ret;
244
245     pa_assert(pcm_handle);
246     pa_assert(f);
247
248     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
249         return ret;
250
251     if (*f == PA_SAMPLE_FLOAT32BE)
252         *f = PA_SAMPLE_FLOAT32LE;
253     else if (*f == PA_SAMPLE_FLOAT32LE)
254         *f = PA_SAMPLE_FLOAT32BE;
255     else if (*f == PA_SAMPLE_S16BE)
256         *f = PA_SAMPLE_S16LE;
257     else if (*f == PA_SAMPLE_S16LE)
258         *f = PA_SAMPLE_S16BE;
259     else
260         goto try_auto;
261
262     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
263         return ret;
264
265 try_auto:
266
267     for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
268         *f = try_order[i];
269
270         if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
271             return ret;
272     }
273
274     return -1;
275 }
276
277 /* Set the hardware parameters of the given ALSA device. Returns the
278  * selected fragment settings in *period and *period_size */
279 int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size, int *use_mmap) {
280     int ret = -1;
281     snd_pcm_uframes_t buffer_size;
282     unsigned int r = ss->rate;
283     unsigned int c = ss->channels;
284     pa_sample_format_t f = ss->format;
285     snd_pcm_hw_params_t *hwparams;
286
287     pa_assert(pcm_handle);
288     pa_assert(ss);
289     pa_assert(periods);
290     pa_assert(period_size);
291
292     snd_pcm_hw_params_alloca(&hwparams);
293     
294     buffer_size = *periods * *period_size;
295
296     if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
297         (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
298         goto finish;
299
300     if (use_mmap && *use_mmap) {
301         if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
302             if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
303                 goto finish;
304
305     } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) 
306         goto finish;
307     else if (*use_mmap)
308         *use_mmap = 0;
309     
310     if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
311         goto finish;
312
313     if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
314         goto finish;
315
316     if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
317         goto finish;
318
319     if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
320         (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
321         goto finish;
322
323     if  ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
324         goto finish;
325
326     if (ss->rate != r) {
327         pa_log_warn("device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
328
329         /* If the sample rate deviates too much, we need to resample */
330         if (r < ss->rate*.95 || r > ss->rate*1.05)
331             ss->rate = r;
332     }
333
334     if (ss->channels != c) {
335         pa_log_warn("device doesn't support %u channels, changed to %u.", ss->channels, c);
336         ss->channels = c;
337     }
338
339     if (ss->format != f) {
340         pa_log_warn("device doesn't support sample format %s, changed to %s.", pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f));
341         ss->format = f;
342     }
343
344     if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
345         goto finish;
346
347     if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
348         (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
349         goto finish;
350
351     pa_assert(buffer_size > 0);
352     pa_assert(*period_size > 0);
353     *periods = buffer_size / *period_size;
354     pa_assert(*periods > 0);
355
356     ret = 0;
357
358 finish:
359
360     return ret;
361 }
362
363 int pa_alsa_set_sw_params(snd_pcm_t *pcm) {
364     snd_pcm_sw_params_t *swparams;
365     int err;
366     
367     pa_assert(pcm);
368
369     snd_pcm_sw_params_alloca(&swparams);
370
371     if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
372         pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err));
373         return err;
374     }
375
376     if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
377         pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err));
378         return err;
379     }
380     
381     if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
382         pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
383         return err;
384     }
385
386     return 0;
387 }
388
389 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
390     int err;
391
392     pa_assert(mixer);
393     pa_assert(dev);
394
395     if ((err = snd_mixer_attach(mixer, dev)) < 0) {
396         pa_log_warn("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
397         return -1;
398     }
399
400     if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
401         pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
402         return -1;
403     }
404
405     if ((err = snd_mixer_load(mixer)) < 0) {
406         pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
407         return -1;
408     }
409
410     return 0;
411 }
412
413 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
414     snd_mixer_elem_t *elem;
415     snd_mixer_selem_id_t *sid = NULL;
416     
417     snd_mixer_selem_id_alloca(&sid);
418
419     pa_assert(mixer);
420     pa_assert(name);
421
422     snd_mixer_selem_id_set_name(sid, name);
423
424     if (!(elem = snd_mixer_find_selem(mixer, sid))) {
425         pa_log_warn("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
426
427         if (fallback) {
428             snd_mixer_selem_id_set_name(sid, fallback);
429
430             if (!(elem = snd_mixer_find_selem(mixer, sid)))
431                 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
432         }
433     }
434
435     if (elem)
436         pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
437
438     return elem;
439 }