Tried to get the volume information even upon init failure.
[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     struct pa_alsa_fdlist *pcm_fdl;
61     struct pa_alsa_fdlist *mixer_fdl;
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 "default"
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 fdl_callback(void *userdata) {
148     struct userdata *u = userdata;
149     assert(u);
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 int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
158     struct userdata *u = snd_mixer_elem_get_callback_private(elem);
159
160     assert(u && u->mixer_handle);
161
162     if (mask & SND_CTL_EVENT_MASK_VALUE) {
163         if (u->source->get_hw_volume)
164             u->source->get_hw_volume(u->source);
165         if (u->source->get_hw_mute)
166             u->source->get_hw_mute(u->source);
167         pa_subscription_post(u->source->core,
168             PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
169             u->source->index);
170     }
171
172     return 0;
173 }
174
175 static pa_usec_t source_get_latency_cb(pa_source *s) {
176     struct userdata *u = s->userdata;
177     snd_pcm_sframes_t frames;
178     assert(s && u && u->source);
179
180     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
181         pa_log(__FILE__": failed to get delay");
182         s->get_latency = NULL;
183         return 0;
184     }
185
186     return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
187 }
188
189 static int source_get_hw_volume_cb(pa_source *s) {
190     struct userdata *u = s->userdata;
191     long vol;
192     int err;
193
194     assert(u && u->mixer_elem);
195
196     if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
197         err = snd_mixer_selem_get_capture_volume(u->mixer_elem, 0, &vol);
198         if (err < 0)
199             goto fail;
200         pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
201             (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min));
202     } else {
203         int i;
204
205         for (i = 0;i < s->hw_volume.channels;i++) {
206             err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
207             if (err < 0)
208                 goto fail;
209             s->hw_volume.values[i] =
210                 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
211         }
212     }
213
214     return 0;
215
216 fail:
217     pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
218     s->get_hw_volume = NULL;
219     s->set_hw_volume = NULL;
220     return -1;
221 }
222
223 static int source_set_hw_volume_cb(pa_source *s) {
224     struct userdata *u = s->userdata;
225     int err;
226     pa_volume_t vol;
227
228     assert(u && u->mixer_elem);
229
230     if (snd_mixer_selem_has_capture_volume_joined(u->mixer_elem)) {
231         vol = pa_cvolume_avg(&s->hw_volume) * (u->hw_volume_max - u->hw_volume_min) /
232             PA_VOLUME_NORM + u->hw_volume_min;
233         err = snd_mixer_selem_set_capture_volume_all(u->mixer_elem, vol);
234         if (err < 0)
235             goto fail;
236     } else {
237         int i;
238
239         for (i = 0;i < s->hw_volume.channels;i++) {
240             vol = s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min) /
241                 PA_VOLUME_NORM + u->hw_volume_min;
242             err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
243             if (err < 0)
244                 goto fail;
245         }
246     }
247
248     return 0;
249
250 fail:
251     pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
252     s->get_hw_volume = NULL;
253     s->set_hw_volume = NULL;
254     return -1;
255 }
256
257 static int source_get_hw_mute_cb(pa_source *s) {
258     struct userdata *u = s->userdata;
259     int err, sw;
260
261     assert(u && u->mixer_elem);
262
263     err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
264     if (err) {
265         pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
266         s->get_hw_mute = NULL;
267         s->set_hw_mute = NULL;
268         return -1;
269     }
270
271     s->hw_muted = !sw;
272
273     return 0;
274 }
275
276 static int source_set_hw_mute_cb(pa_source *s) {
277     struct userdata *u = s->userdata;
278     int err;
279
280     assert(u && u->mixer_elem);
281
282     err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
283     if (err) {
284         pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
285         s->get_hw_mute = NULL;
286         s->set_hw_mute = NULL;
287         return -1;
288     }
289
290     return 0;
291 }
292
293 int pa__init(pa_core *c, pa_module*m) {
294     pa_modargs *ma = NULL;
295     int ret = -1;
296     struct userdata *u = NULL;
297     const char *dev;
298     pa_sample_spec ss;
299     unsigned periods, fragsize;
300     snd_pcm_uframes_t period_size;
301     size_t frame_size;
302     int err;
303     
304     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
305         pa_log(__FILE__": failed to parse module arguments");
306         goto fail;
307     }
308
309     ss = c->default_sample_spec;
310     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
311         pa_log(__FILE__": failed to parse sample specification");
312         goto fail;
313     }
314     frame_size = pa_frame_size(&ss);
315     
316     periods = 12;
317     fragsize = 1024;
318     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
319         pa_log(__FILE__": failed to parse buffer metrics");
320         goto fail;
321     }
322     period_size = fragsize;
323     
324     u = pa_xmalloc0(sizeof(struct userdata));
325     m->userdata = u;
326     u->module = m;
327     
328     snd_config_update_free_global();
329     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) {
330         pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
331         goto fail;
332     }
333
334     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
335         pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
336         goto fail;
337     }
338
339     if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
340         pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
341         goto fail;
342     }
343
344     if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
345         !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture"))) {
346         snd_mixer_close(u->mixer_handle);
347         u->mixer_handle = NULL;
348     }
349
350     u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL);
351     assert(u->source);
352
353     u->source->userdata = u;
354     u->source->get_latency = source_get_latency_cb;
355     if (u->mixer_handle) {
356         assert(u->mixer_elem);
357         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
358             u->source->get_hw_volume = source_get_hw_volume_cb;
359             u->source->set_hw_volume = source_set_hw_volume_cb;
360             snd_mixer_selem_get_capture_volume_range(
361                 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
362         }
363         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
364             u->source->get_hw_mute = source_get_hw_mute_cb;
365             u->source->set_hw_mute = source_set_hw_mute_cb;
366         }
367     }
368     pa_source_set_owner(u->source, m);
369     u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
370
371     u->pcm_fdl = pa_alsa_fdlist_new();
372     assert(u->pcm_fdl);
373     if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
374         pa_log(__FILE__": failed to initialise file descriptor monitoring");
375         goto fail;
376     }
377
378     if (u->mixer_handle) {
379         u->mixer_fdl = pa_alsa_fdlist_new();
380         assert(u->mixer_fdl);
381         if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
382             pa_log(__FILE__": failed to initialise file descriptor monitoring");
383             goto fail;
384         }
385         snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
386         snd_mixer_elem_set_callback_private(u->mixer_elem, u);
387     }
388
389     u->frame_size = frame_size;
390     u->fragment_size = period_size;
391
392     pa_log(__FILE__": using %u fragments of size %u bytes.", periods, u->fragment_size);
393
394     u->memchunk.memblock = NULL;
395     u->memchunk.index = u->memchunk.length = 0;
396
397     snd_pcm_start(u->pcm_handle);
398     
399     ret = 0;
400
401     /* Get initial mixer settings */
402     if (u->source->get_hw_volume)
403         u->source->get_hw_volume(u->source);
404     if (u->source->get_hw_mute)
405         u->source->get_hw_mute(u->source);
406
407 finish:
408      if (ma)
409          pa_modargs_free(ma);
410     
411     return ret;
412
413 fail:
414     
415     if (u)
416         pa__done(c, m);
417
418     goto finish;
419 }
420
421 void pa__done(pa_core *c, pa_module*m) {
422     struct userdata *u;
423     assert(c && m);
424
425     if (!(u = m->userdata))
426         return;
427     
428     if (u->source) {
429         pa_source_disconnect(u->source);
430         pa_source_unref(u->source);
431     }
432     
433     if (u->pcm_fdl)
434         pa_alsa_fdlist_free(u->pcm_fdl);
435     if (u->mixer_fdl)
436         pa_alsa_fdlist_free(u->mixer_fdl);
437
438     if (u->mixer_handle)
439         snd_mixer_close(u->mixer_handle);
440     
441     if (u->pcm_handle) {
442         snd_pcm_drop(u->pcm_handle);
443         snd_pcm_close(u->pcm_handle);
444     }
445     
446     if (u->memchunk.memblock)
447         pa_memblock_unref(u->memchunk.memblock);
448     
449     pa_xfree(u);
450 }
451