Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.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
37 #include "alsa-util.h"
38
39 struct pa_alsa_fdlist {
40     int num_fds;
41     struct pollfd *fds;
42     /* This is a temporary buffer used to avoid lots of mallocs */
43     struct pollfd *work_fds;
44
45     snd_pcm_t *pcm;
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     struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
60     int err, i;
61     unsigned short revents;
62
63     assert(a && fdl && (fdl->pcm || fdl->mixer) && fdl->fds && fdl->work_fds);
64
65     if (fdl->polled)
66         return;
67
68     fdl->polled = 1;
69
70     memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
71
72     for (i = 0;i < fdl->num_fds;i++) {
73         if (e == fdl->ios[i]) {
74             if (events & PA_IO_EVENT_INPUT)
75                 fdl->work_fds[i].revents |= POLLIN;
76             if (events & PA_IO_EVENT_OUTPUT)
77                 fdl->work_fds[i].revents |= POLLOUT;
78             if (events & PA_IO_EVENT_ERROR)
79                 fdl->work_fds[i].revents |= POLLERR;
80             if (events & PA_IO_EVENT_HANGUP)
81                 fdl->work_fds[i].revents |= POLLHUP;
82             break;
83         }
84     }
85
86     assert(i != fdl->num_fds);
87
88     if (fdl->pcm)
89         err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
90     else
91         err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents);
92
93     if (err < 0) {
94         pa_log_error("Unable to get poll revent: %s",
95             snd_strerror(err));
96         return;
97     }
98
99     a->defer_enable(fdl->defer, 1);
100
101     if (revents) {
102         if (fdl->pcm)
103             fdl->cb(fdl->userdata);
104         else
105             snd_mixer_handle_events(fdl->mixer);
106     }
107 }
108
109 static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event* e, void *userdata) {
110     struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
111     int num_fds, i, err;
112     struct pollfd *temp;
113
114     assert(a && fdl && (fdl->pcm || fdl->mixer));
115
116     a->defer_enable(fdl->defer, 0);
117
118     if (fdl->pcm)
119         num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
120     else
121         num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
122     assert(num_fds > 0);
123
124     if (num_fds != fdl->num_fds) {
125         if (fdl->fds)
126             pa_xfree(fdl->fds);
127         if (fdl->work_fds)
128             pa_xfree(fdl->work_fds);
129         fdl->fds = pa_xmalloc0(sizeof(struct pollfd) * num_fds);
130         fdl->work_fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
131     }
132
133     memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
134
135     if (fdl->pcm)
136         err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
137     else
138         err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds);
139
140     if (err < 0) {
141         pa_log_error("Unable to get poll descriptors: %s",
142             snd_strerror(err));
143         return;
144     }
145
146     fdl->polled = 0;
147
148     if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
149         return;
150
151     if (fdl->ios) {
152         for (i = 0;i < fdl->num_fds;i++)
153             a->io_free(fdl->ios[i]);
154         if (num_fds != fdl->num_fds) {
155             pa_xfree(fdl->ios);
156             fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
157             assert(fdl->ios);
158         }
159     } else {
160         fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
161         assert(fdl->ios);
162     }
163
164     /* Swap pointers */
165     temp = fdl->work_fds;
166     fdl->work_fds = fdl->fds;
167     fdl->fds = temp;
168
169     fdl->num_fds = num_fds;
170
171     for (i = 0;i < num_fds;i++) {
172         fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
173             ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
174             ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
175             io_cb, fdl);
176         assert(fdl->ios[i]);
177     }
178 }
179
180 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
181     struct pa_alsa_fdlist *fdl;
182
183     fdl = pa_xmalloc(sizeof(struct pa_alsa_fdlist));
184
185     fdl->num_fds = 0;
186     fdl->fds = NULL;
187     fdl->work_fds = NULL;
188
189     fdl->pcm = NULL;
190     fdl->mixer = NULL;
191
192     fdl->m = NULL;
193     fdl->defer = NULL;
194     fdl->ios = NULL;
195
196     fdl->polled = 0;
197
198     return fdl;
199 }
200
201 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
202     assert(fdl);
203
204     if (fdl->defer) {
205         assert(fdl->m);
206         fdl->m->defer_free(fdl->defer);
207     }
208
209     if (fdl->ios) {
210         int i;
211         assert(fdl->m);
212         for (i = 0;i < fdl->num_fds;i++)
213             fdl->m->io_free(fdl->ios[i]);
214         pa_xfree(fdl->ios);
215     }
216
217     if (fdl->fds)
218         pa_xfree(fdl->fds);
219     if (fdl->work_fds)
220         pa_xfree(fdl->work_fds);
221
222     pa_xfree(fdl);
223 }
224
225 int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist *fdl, snd_pcm_t *pcm_handle, pa_mainloop_api* m, void (*cb)(void *userdata), void *userdata) {
226     assert(fdl && pcm_handle && m && !fdl->m && cb);
227
228     fdl->pcm = pcm_handle;
229     fdl->m = m;
230
231     fdl->defer = m->defer_new(m, defer_cb, fdl);
232     assert(fdl->defer);
233
234     fdl->cb = cb;
235     fdl->userdata = userdata;
236
237     return 0;
238 }
239
240 int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
241     assert(fdl && mixer_handle && m && !fdl->m);
242
243     fdl->mixer = mixer_handle;
244     fdl->m = m;
245
246     fdl->defer = m->defer_new(m, defer_cb, fdl);
247     assert(fdl->defer);
248
249     return 0;
250 }
251
252 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
253
254     static const snd_pcm_format_t format_trans[] = {
255         [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
256         [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
257         [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
258         [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
259         [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
260         [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
261         [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
262     };
263
264     static const pa_sample_format_t try_order[] = {
265         PA_SAMPLE_S16NE,
266         PA_SAMPLE_S16RE,
267         PA_SAMPLE_FLOAT32NE,
268         PA_SAMPLE_FLOAT32RE,
269         PA_SAMPLE_ULAW,
270         PA_SAMPLE_ALAW,
271         PA_SAMPLE_U8,
272         PA_SAMPLE_INVALID
273     };
274
275     int i, ret;
276
277     assert(pcm_handle);
278     assert(f);
279
280     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
281         return ret;
282
283     if (*f == PA_SAMPLE_FLOAT32BE)
284         *f = PA_SAMPLE_FLOAT32LE;
285     else if (*f == PA_SAMPLE_FLOAT32LE)
286         *f = PA_SAMPLE_FLOAT32BE;
287     else if (*f == PA_SAMPLE_S16BE)
288         *f = PA_SAMPLE_S16LE;
289     else if (*f == PA_SAMPLE_S16LE)
290         *f = PA_SAMPLE_S16BE;
291     else
292         goto try_auto;
293
294     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
295         return ret;
296
297 try_auto:
298
299     for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
300         *f = try_order[i];
301
302         if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
303             return ret;
304     }
305
306     return -1;
307 }
308
309 /* Set the hardware parameters of the given ALSA device. Returns the
310  * selected fragment settings in *period and *period_size */
311 int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) {
312     int ret = -1;
313     snd_pcm_uframes_t buffer_size;
314     unsigned int r = ss->rate;
315     unsigned int c = ss->channels;
316     pa_sample_format_t f = ss->format;
317     snd_pcm_hw_params_t *hwparams;
318
319     assert(pcm_handle);
320     assert(ss);
321     assert(periods);
322     assert(period_size);
323
324     buffer_size = *periods * *period_size;
325
326     if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
327         (ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
328         (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0 ||
329         (ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
330         goto finish;
331
332     if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
333         goto finish;
334
335     if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
336         goto finish;
337
338     if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
339         goto finish;
340
341     if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
342         (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
343         goto finish;
344
345     if  ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
346         goto finish;
347
348     if (ss->rate != r) {
349         pa_log_warn("device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
350
351         /* If the sample rate deviates too much, we need to resample */
352         if (r < ss->rate*.95 || r > ss->rate*1.05)
353             ss->rate = r;
354     }
355
356     if (ss->channels != c) {
357         pa_log_warn("device doesn't support %u channels, changed to %u.", ss->channels, c);
358         ss->channels = c;
359     }
360
361     if (ss->format != f) {
362         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));
363         ss->format = f;
364     }
365
366     if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
367         goto finish;
368
369     if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
370         (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
371         goto finish;
372
373     assert(buffer_size > 0);
374     assert(*period_size > 0);
375     *periods = buffer_size / *period_size;
376     assert(*periods > 0);
377
378     ret = 0;
379
380 finish:
381     if (hwparams)
382         snd_pcm_hw_params_free(hwparams);
383
384     return ret;
385 }
386
387 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
388     int err;
389
390     assert(mixer && dev);
391
392     if ((err = snd_mixer_attach(mixer, dev)) < 0) {
393         pa_log_warn("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
394         return -1;
395     }
396
397     if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
398         pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
399         return -1;
400     }
401
402     if ((err = snd_mixer_load(mixer)) < 0) {
403         pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
404         return -1;
405     }
406
407     return 0;
408 }
409
410 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
411     snd_mixer_elem_t *elem;
412     snd_mixer_selem_id_t *sid = NULL;
413     snd_mixer_selem_id_alloca(&sid);
414
415     assert(mixer);
416     assert(name);
417
418     snd_mixer_selem_id_set_name(sid, name);
419
420     if (!(elem = snd_mixer_find_selem(mixer, sid))) {
421         pa_log_warn("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
422
423         if (fallback) {
424             snd_mixer_selem_id_set_name(sid, fallback);
425
426             if (!(elem = snd_mixer_find_selem(mixer, sid)))
427                 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
428         }
429     }
430
431     if (elem)
432         pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
433
434     return elem;
435 }