It's safer to set buffer size than to try setting number of periods.
[profile/ivi/pulseaudio-panda.git] / src / modules / alsa-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <asoundlib.h>
28
29 #include <polyp/sample.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/log.h>
32
33 #include "alsa-util.h"
34
35 struct pa_alsa_fdlist {
36     int num_fds;
37     struct pollfd *fds;
38     /* This is a temporary buffer used to avoid lots of mallocs */
39     struct pollfd *work_fds;
40
41     snd_pcm_t *pcm;
42     snd_mixer_t *mixer;
43
44     pa_mainloop_api *m;
45     pa_defer_event *defer;
46     pa_io_event **ios;
47
48     int polled;
49
50     void (*cb)(void *userdata);
51     void *userdata;
52 };
53
54 static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
55     struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
56     int err, i;
57     unsigned short revents;
58
59     assert(a && fdl && (fdl->pcm || fdl->mixer) && fdl->fds && fdl->work_fds);
60
61     if (fdl->polled)
62         return;
63
64     fdl->polled = 1;
65
66     memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
67
68     for (i = 0;i < fdl->num_fds;i++) {
69         if (e == fdl->ios[i]) {
70             if (events & PA_IO_EVENT_INPUT)
71                 fdl->work_fds[i].revents |= POLLIN;
72             if (events & PA_IO_EVENT_OUTPUT)
73                 fdl->work_fds[i].revents |= POLLOUT;
74             if (events & PA_IO_EVENT_ERROR)
75                 fdl->work_fds[i].revents |= POLLERR;
76             if (events & PA_IO_EVENT_HANGUP)
77                 fdl->work_fds[i].revents |= POLLHUP;
78             break;
79         }
80     }
81
82     assert(i != fdl->num_fds);
83
84     if (fdl->pcm)
85         err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
86     else
87         err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents);
88
89     if (err < 0) {
90         pa_log_error(__FILE__": Unable to get poll revent: %s",
91             snd_strerror(err));
92         a->defer_enable(fdl->defer, 0);
93         return;
94     }
95
96     if (revents) {
97         if (fdl->pcm)
98             fdl->cb(fdl->userdata);
99         else
100             snd_mixer_handle_events(fdl->mixer);
101     }
102 }
103
104 static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
105     struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
106     int num_fds, i, err;
107     struct pollfd *temp;
108
109     assert(a && fdl && (fdl->pcm || fdl->mixer));
110
111     if (fdl->pcm)
112         num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
113     else
114         num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
115     assert(num_fds > 0);
116
117     if (num_fds != fdl->num_fds) {
118         if (fdl->fds)
119             pa_xfree(fdl->fds);
120         if (fdl->work_fds)
121             pa_xfree(fdl->work_fds);
122         fdl->fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
123         fdl->work_fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
124     }
125
126     memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
127
128     if (fdl->pcm)
129         err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
130     else
131         err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds);
132
133     if (err < 0) {
134         pa_log_error(__FILE__": Unable to get poll descriptors: %s",
135             snd_strerror(err));
136         a->defer_enable(fdl->defer, 0);
137         return;
138     }
139
140     fdl->polled = 0;
141
142     if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
143         return;
144
145     if (fdl->ios) {
146         for (i = 0;i < fdl->num_fds;i++)
147             a->io_free(fdl->ios[i]);
148         if (num_fds != fdl->num_fds) {
149             pa_xfree(fdl->ios);
150             fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
151             assert(fdl->ios);
152         }
153     } else {
154         fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
155         assert(fdl->ios);
156     }
157
158     /* Swap pointers */
159     temp = fdl->work_fds;
160     fdl->work_fds = fdl->fds;
161     fdl->fds = temp;
162
163     fdl->num_fds = num_fds;
164
165     for (i = 0;i < num_fds;i++) {
166         fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
167             ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
168             ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
169             io_cb, fdl);
170         assert(fdl->ios[i]);
171     }
172 }
173
174 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
175     struct pa_alsa_fdlist *fdl;
176
177     fdl = pa_xmalloc(sizeof(struct pa_alsa_fdlist));
178     assert(fdl);
179
180     fdl->num_fds = 0;
181     fdl->fds = NULL;
182     fdl->work_fds = NULL;
183
184     fdl->pcm = NULL;
185     fdl->mixer = NULL;
186
187     fdl->m = NULL;
188     fdl->defer = NULL;
189     fdl->ios = NULL;
190
191     fdl->polled = 0;
192
193     return fdl;
194 }
195
196 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
197     assert(fdl);
198
199     if (fdl->defer) {
200         assert(fdl->m);
201         fdl->m->defer_free(fdl->defer);
202     }
203
204     if (fdl->ios) {
205         int i;
206         assert(fdl->m);
207         for (i = 0;i < fdl->num_fds;i++)
208             fdl->m->io_free(fdl->ios[0]);
209         pa_xfree(fdl->ios);
210     }
211
212     if (fdl->fds)
213         pa_xfree(fdl->fds);
214     if (fdl->work_fds)
215         pa_xfree(fdl->work_fds);
216
217     pa_xfree(fdl);
218 }
219
220 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) {
221     assert(fdl && pcm_handle && m && !fdl->m && cb);
222
223     fdl->pcm = pcm_handle;
224     fdl->m = m;
225
226     fdl->defer = m->defer_new(m, defer_cb, fdl);
227     assert(fdl->defer);
228
229     fdl->cb = cb;
230     fdl->userdata = userdata;
231
232     return 0;
233 }
234
235 int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
236     assert(fdl && mixer_handle && m && !fdl->m);
237
238     fdl->mixer = mixer_handle;
239     fdl->m = m;
240
241     fdl->defer = m->defer_new(m, defer_cb, fdl);
242     assert(fdl->defer);
243
244     return 0;
245 }
246
247 /* Set the hardware parameters of the given ALSA device. Returns the
248  * selected fragment settings in *period and *period_size */
249 int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, const pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) {
250     int ret = -1;
251     snd_pcm_uframes_t buffer_size;
252     snd_pcm_hw_params_t *hwparams = NULL;
253     unsigned int r = ss->rate;
254     
255     static const snd_pcm_format_t format_trans[] = {
256         [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
257         [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
258         [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
259         [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
260         [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
261         [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
262         [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
263     };
264     assert(pcm_handle && ss && periods && period_size);
265
266     buffer_size = *periods * *period_size;
267     
268     if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
269         (ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
270         (ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0 ||
271         (ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format])) < 0 ||
272         (ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0 ||
273         (ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels)) < 0 ||
274         (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0) ||
275         (*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
276         (ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
277         goto finish;
278
279     if (ss->rate != r) 
280         pa_log_info(__FILE__": device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
281     
282     if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
283         goto finish;
284
285     if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
286         (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
287         goto finish;
288     
289     assert(buffer_size > 0);
290     assert(*period_size > 0);
291     *periods = buffer_size / *period_size;
292     assert(*periods > 0);
293     
294     ret = 0;
295     
296 finish:
297     if (hwparams)
298         snd_pcm_hw_params_free(hwparams);
299     
300     return ret;
301 }
302
303 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
304     int err;
305
306     assert(mixer && dev);
307
308     if ((err = snd_mixer_attach(mixer, dev)) < 0) {
309         pa_log_warn(__FILE__": Unable to attach to mixer %s: %s", dev, snd_strerror(err));
310         return -1;
311     }
312
313     if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
314         pa_log_warn(__FILE__": Unable to register mixer: %s", snd_strerror(err));
315         return -1;
316     }
317
318     if ((err = snd_mixer_load(mixer)) < 0) {
319         pa_log_warn(__FILE__": Unable to load mixer: %s", snd_strerror(err));
320         return -1;
321     }
322
323     return 0;
324 }
325
326 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name) {
327     snd_mixer_elem_t *elem;
328     snd_mixer_selem_id_t *sid;
329     snd_mixer_selem_id_alloca(&sid);
330
331     assert(mixer && name);
332
333     snd_mixer_selem_id_set_name(sid, name);
334
335     elem = snd_mixer_find_selem(mixer, sid);
336     if (!elem)
337         pa_log_warn(__FILE__": Cannot find mixer control %s", snd_mixer_selem_id_get_name(sid));
338
339     return elem;
340 }