Hardware volume support in ALSA modules.
[profile/ivi/pulseaudio.git] / src / modules / module-alsa-source.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 <assert.h>
27 #include <stdio.h>
28
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #else
32 #include "poll.h"
33 #endif
34
35 #include <asoundlib.h>
36
37 #include <polypcore/core.h>
38 #include <polypcore/module.h>
39 #include <polypcore/memchunk.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/modargs.h>
42 #include <polypcore/util.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/xmalloc.h>
45 #include <polypcore/log.h>
46
47 #include "alsa-util.h"
48 #include "module-alsa-source-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("ALSA Source")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE("source_name=<name for the source> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
54
55 struct userdata {
56     snd_pcm_t *pcm_handle;
57     snd_mixer_t *mixer_handle;
58     snd_mixer_elem_t *mixer_elem;
59     pa_source *source;
60     pa_io_event **io_events;
61     unsigned n_io_events;
62     long hw_volume_max, hw_volume_min;
63
64     size_t frame_size, fragment_size;
65     pa_memchunk memchunk;
66     pa_module *module;
67 };
68
69 static const char* const valid_modargs[] = {
70     "device",
71     "source_name",
72     "channels",
73     "rate",
74     "format",
75     "fragments",
76     "fragment_size",
77     NULL
78 };
79
80 #define DEFAULT_SOURCE_NAME "alsa_input"
81 #define DEFAULT_DEVICE "hw:0,0"
82
83 static void update_usage(struct userdata *u) {
84    pa_module_set_used(u->module,
85                       (u->source ? pa_idxset_size(u->source->outputs) : 0));
86 }
87
88 static void xrun_recovery(struct userdata *u) {
89     assert(u);
90
91     pa_log(__FILE__": *** ALSA-XRUN (capture) ***");
92     
93     if (snd_pcm_prepare(u->pcm_handle) < 0)
94         pa_log(__FILE__": snd_pcm_prepare() failed");
95 }
96
97 static void do_read(struct userdata *u) {
98     assert(u);
99
100     update_usage(u);
101     
102     for (;;) {
103         pa_memchunk post_memchunk;
104         snd_pcm_sframes_t frames;
105         size_t l;
106         
107         if (!u->memchunk.memblock) {
108             u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
109             u->memchunk.index = 0;
110         }
111             
112         assert(u->memchunk.memblock && u->memchunk.memblock->data && u->memchunk.length && u->memchunk.memblock->length && (u->memchunk.length % u->frame_size) == 0);
113
114         if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
115             if (frames == -EAGAIN)
116                 return;
117             
118             if (frames == -EPIPE) {
119                 xrun_recovery(u);
120                 continue;
121             }
122
123             pa_log(__FILE__": snd_pcm_readi() failed: %s", strerror(-frames));
124             return;
125         }
126
127         l = frames * u->frame_size;
128         
129         post_memchunk = u->memchunk;
130         post_memchunk.length = l;
131
132         pa_source_post(u->source, &post_memchunk);
133
134         u->memchunk.index += l;
135         u->memchunk.length -= l;
136         
137         if (u->memchunk.length == 0) {
138             pa_memblock_unref(u->memchunk.memblock);
139             u->memchunk.memblock = NULL;
140             u->memchunk.index = u->memchunk.length = 0;
141         }
142         
143         break;
144     }
145 }
146
147 static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
148     struct userdata *u = userdata;
149     assert(u && a && e);
150
151     if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
152         xrun_recovery(u);
153
154     do_read(u);
155 }
156
157 static pa_usec_t source_get_latency_cb(pa_source *s) {
158     struct userdata *u = s->userdata;
159     snd_pcm_sframes_t frames;
160     assert(s && u && u->source);
161
162     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
163         pa_log(__FILE__": failed to get delay");
164         s->get_latency = NULL;
165         return 0;
166     }
167
168     return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
169 }
170
171 static int source_get_hw_volume_cb(pa_source *s) {
172     struct userdata *u = s->userdata;
173     long vol;
174     int err;
175
176     assert(u && u->mixer_elem);
177
178     if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
179         err = snd_mixer_selem_get_capture_volume(u->mixer_elem, 0, &vol);
180         if (err < 0)
181             goto fail;
182         pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
183             (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min));
184     } else {
185         int i;
186
187         for (i = 0;i < s->hw_volume.channels;i++) {
188             err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
189             if (err < 0)
190                 goto fail;
191             s->hw_volume.values[i] =
192                 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
193         }
194     }
195
196     return 0;
197
198 fail:
199     pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
200     s->get_hw_volume = NULL;
201     s->set_hw_volume = NULL;
202     return -1;
203 }
204
205 static int source_set_hw_volume_cb(pa_source *s) {
206     struct userdata *u = s->userdata;
207     int err;
208     pa_volume_t vol;
209
210     assert(u && u->mixer_elem);
211
212     if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
213         vol = pa_cvolume_avg(&s->hw_volume) * (u->hw_volume_max - u->hw_volume_min) /
214             PA_VOLUME_NORM + u->hw_volume_min;
215         err = snd_mixer_selem_set_capture_volume_all(u->mixer_elem, vol);
216         if (err < 0)
217             goto fail;
218     } else {
219         int i;
220
221         for (i = 0;i < s->hw_volume.channels;i++) {
222             vol = s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min) /
223                 PA_VOLUME_NORM + u->hw_volume_min;
224             err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
225             if (err < 0)
226                 goto fail;
227         }
228     }
229
230     return 0;
231
232 fail:
233     pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
234     s->get_hw_volume = NULL;
235     s->set_hw_volume = NULL;
236     return -1;
237 }
238
239 static int source_get_hw_mute_cb(pa_source *s) {
240     struct userdata *u = s->userdata;
241     int err, sw;
242
243     assert(u && u->mixer_elem);
244
245     err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
246     if (err) {
247         pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
248         s->get_hw_mute = NULL;
249         s->set_hw_mute = NULL;
250         return -1;
251     }
252
253     s->hw_muted = !sw;
254
255     return 0;
256 }
257
258 static int source_set_hw_mute_cb(pa_source *s) {
259     struct userdata *u = s->userdata;
260     int err;
261
262     assert(u && u->mixer_elem);
263
264     err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
265     if (err) {
266         pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
267         s->get_hw_mute = NULL;
268         s->set_hw_mute = NULL;
269         return -1;
270     }
271
272     return 0;
273 }
274
275 int pa__init(pa_core *c, pa_module*m) {
276     pa_modargs *ma = NULL;
277     int ret = -1;
278     struct userdata *u = NULL;
279     const char *dev;
280     pa_sample_spec ss;
281     unsigned periods, fragsize;
282     snd_pcm_uframes_t period_size;
283     size_t frame_size;
284     int err;
285     
286     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
287         pa_log(__FILE__": failed to parse module arguments");
288         goto fail;
289     }
290
291     ss = c->default_sample_spec;
292     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
293         pa_log(__FILE__": failed to parse sample specification");
294         goto fail;
295     }
296     frame_size = pa_frame_size(&ss);
297     
298     periods = 12;
299     fragsize = 1024;
300     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
301         pa_log(__FILE__": failed to parse buffer metrics");
302         goto fail;
303     }
304     period_size = fragsize;
305     
306     u = pa_xmalloc0(sizeof(struct userdata));
307     m->userdata = u;
308     u->module = m;
309     
310     snd_config_update_free_global();
311     if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
312         pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
313         goto fail;
314     }
315
316     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
317         pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
318         goto fail;
319     }
320
321     if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
322         pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
323         goto fail;
324     }
325
326     if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
327         !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture"))) {
328         snd_mixer_close(u->mixer_handle);
329         u->mixer_handle = NULL;
330     }
331
332     u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL);
333     assert(u->source);
334
335     u->source->userdata = u;
336     u->source->get_latency = source_get_latency_cb;
337     if (u->mixer_handle) {
338         assert(u->mixer_elem);
339         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
340             u->source->get_hw_volume = source_get_hw_volume_cb;
341             u->source->set_hw_volume = source_set_hw_volume_cb;
342             snd_mixer_selem_get_capture_volume_range(
343                 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
344         }
345         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
346             u->source->get_hw_mute = source_get_hw_mute_cb;
347             u->source->set_hw_mute = source_set_hw_mute_cb;
348         }
349     }
350     pa_source_set_owner(u->source, m);
351     u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
352
353     if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
354         pa_log(__FILE__": failed to obtain file descriptors");
355         goto fail;
356     }
357
358     u->frame_size = frame_size;
359     u->fragment_size = period_size;
360
361     pa_log(__FILE__": using %u fragments of size %u bytes.", periods, u->fragment_size);
362
363     u->memchunk.memblock = NULL;
364     u->memchunk.index = u->memchunk.length = 0;
365
366     snd_pcm_start(u->pcm_handle);
367     
368     ret = 0;
369
370 finish:
371      if (ma)
372          pa_modargs_free(ma);
373
374     /* Get initial mixer settings */
375     if (u->source->get_hw_volume)
376         u->source->get_hw_volume(u->source);
377     if (u->source->get_hw_mute)
378         u->source->get_hw_mute(u->source);
379     
380     return ret;
381
382 fail:
383     
384     if (u)
385         pa__done(c, m);
386
387     goto finish;
388 }
389
390 void pa__done(pa_core *c, pa_module*m) {
391     struct userdata *u;
392     assert(c && m);
393
394     if (!(u = m->userdata))
395         return;
396     
397     if (u->source) {
398         pa_source_disconnect(u->source);
399         pa_source_unref(u->source);
400     }
401     
402     if (u->io_events)
403         pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
404
405     if (u->mixer_handle)
406         snd_mixer_close(u->mixer_handle);
407     
408     if (u->pcm_handle) {
409         snd_pcm_drop(u->pcm_handle);
410         snd_pcm_close(u->pcm_handle);
411     }
412     
413     if (u->memchunk.memblock)
414         pa_memblock_unref(u->memchunk.memblock);
415     
416     pa_xfree(u);
417 }
418