split polypcore/util.[ch] into polypcore/core-util.[ch] and polyp/util.[ch]
[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 <polyp/xmalloc.h>
38
39 #include <polypcore/core.h>
40 #include <polypcore/module.h>
41 #include <polypcore/memchunk.h>
42 #include <polypcore/sink.h>
43 #include <polypcore/modargs.h>
44 #include <polypcore/core-util.h>
45 #include <polypcore/sample-util.h>
46 #include <polypcore/log.h>
47
48 #include "alsa-util.h"
49 #include "module-alsa-source-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("ALSA Source")
53 PA_MODULE_VERSION(PACKAGE_VERSION)
54 PA_MODULE_USAGE(
55         "source_name=<name for the source> "
56         "device=<ALSA device> "
57         "format=<sample format> "
58         "channels=<number of channels> "
59         "rate=<sample rate> "
60         "fragments=<number of fragments> "
61         "fragment_size=<fragment size> "
62         "channel_map=<channel map>")
63
64 struct userdata {
65     snd_pcm_t *pcm_handle;
66     snd_mixer_t *mixer_handle;
67     snd_mixer_elem_t *mixer_elem;
68     pa_source *source;
69     struct pa_alsa_fdlist *pcm_fdl;
70     struct pa_alsa_fdlist *mixer_fdl;
71     long hw_volume_max, hw_volume_min;
72
73     size_t frame_size, fragment_size;
74     pa_memchunk memchunk;
75     pa_module *module;
76 };
77
78 static const char* const valid_modargs[] = {
79     "device",
80     "source_name",
81     "channels",
82     "rate",
83     "format",
84     "fragments",
85     "fragment_size",
86     "channel_map",
87     NULL
88 };
89
90 #define DEFAULT_SOURCE_NAME "alsa_input"
91 #define DEFAULT_DEVICE "default"
92
93 static void update_usage(struct userdata *u) {
94    pa_module_set_used(u->module,
95                       (u->source ? pa_idxset_size(u->source->outputs) : 0));
96 }
97
98 static void xrun_recovery(struct userdata *u) {
99     assert(u);
100
101     pa_log(__FILE__": *** ALSA-XRUN (capture) ***");
102     
103     if (snd_pcm_prepare(u->pcm_handle) < 0)
104         pa_log(__FILE__": snd_pcm_prepare() failed");
105 }
106
107 static void do_read(struct userdata *u) {
108     assert(u);
109
110     update_usage(u);
111     
112     for (;;) {
113         pa_memchunk post_memchunk;
114         snd_pcm_sframes_t frames;
115         size_t l;
116         
117         if (!u->memchunk.memblock) {
118             u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
119             u->memchunk.index = 0;
120         }
121             
122         assert(u->memchunk.memblock);
123         assert(u->memchunk.length);
124         assert(u->memchunk.memblock->data);
125         assert(u->memchunk.memblock->length);
126         assert(u->memchunk.length % u->frame_size == 0);
127
128         if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
129             if (frames == -EAGAIN)
130                 return;
131             
132             if (frames == -EPIPE) {
133                 xrun_recovery(u);
134                 continue;
135             }
136
137             pa_log(__FILE__": snd_pcm_readi() failed: %s", strerror(-frames));
138             return;
139         }
140
141         l = frames * u->frame_size;
142         
143         post_memchunk = u->memchunk;
144         post_memchunk.length = l;
145
146         pa_source_post(u->source, &post_memchunk);
147
148         u->memchunk.index += l;
149         u->memchunk.length -= l;
150         
151         if (u->memchunk.length == 0) {
152             pa_memblock_unref(u->memchunk.memblock);
153             u->memchunk.memblock = NULL;
154             u->memchunk.index = u->memchunk.length = 0;
155         }
156         
157         break;
158     }
159 }
160
161 static void fdl_callback(void *userdata) {
162     struct userdata *u = userdata;
163     assert(u);
164
165     if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
166         xrun_recovery(u);
167
168     do_read(u);
169 }
170
171 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
172     struct userdata *u = snd_mixer_elem_get_callback_private(elem);
173
174     assert(u && u->mixer_handle);
175
176     if (mask == SND_CTL_EVENT_MASK_REMOVE)
177         return 0;
178
179     if (mask & SND_CTL_EVENT_MASK_VALUE) {
180         if (u->source->get_hw_volume)
181             u->source->get_hw_volume(u->source);
182         if (u->source->get_hw_mute)
183             u->source->get_hw_mute(u->source);
184         pa_subscription_post(u->source->core,
185             PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
186             u->source->index);
187     }
188
189     return 0;
190 }
191
192 static pa_usec_t source_get_latency_cb(pa_source *s) {
193     struct userdata *u = s->userdata;
194     snd_pcm_sframes_t frames;
195     assert(s && u && u->source);
196
197     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
198         pa_log(__FILE__": failed to get delay");
199         s->get_latency = NULL;
200         return 0;
201     }
202
203     return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
204 }
205
206 static int source_get_hw_volume_cb(pa_source *s) {
207     struct userdata *u = s->userdata;
208     long vol;
209     int err;
210     int i;
211
212     assert(u && u->mixer_elem);
213
214     for (i = 0;i < s->hw_volume.channels;i++) {
215         assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
216
217         err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
218         if (err < 0)
219             goto fail;
220         s->hw_volume.values[i] =
221             (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
222     }
223
224     return 0;
225
226 fail:
227     pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
228     s->get_hw_volume = NULL;
229     s->set_hw_volume = NULL;
230     return -1;
231 }
232
233 static int source_set_hw_volume_cb(pa_source *s) {
234     struct userdata *u = s->userdata;
235     int err;
236     pa_volume_t vol;
237     int i;
238
239     assert(u && u->mixer_elem);
240
241     for (i = 0;i < s->hw_volume.channels;i++) {
242         assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
243
244         vol = s->hw_volume.values[i];
245
246         if (vol > PA_VOLUME_NORM)
247             vol = PA_VOLUME_NORM;
248
249         vol = vol * (u->hw_volume_max - u->hw_volume_min) /
250             PA_VOLUME_NORM + u->hw_volume_min;
251         err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
252         if (err < 0)
253             goto fail;
254     }
255
256     return 0;
257
258 fail:
259     pa_log_error(__FILE__": Unable to set volume: %s", snd_strerror(err));
260     s->get_hw_volume = NULL;
261     s->set_hw_volume = NULL;
262     return -1;
263 }
264
265 static int source_get_hw_mute_cb(pa_source *s) {
266     struct userdata *u = s->userdata;
267     int err, sw;
268
269     assert(u && u->mixer_elem);
270
271     err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
272     if (err) {
273         pa_log_error(__FILE__": Unable to get switch: %s", snd_strerror(err));
274         s->get_hw_mute = NULL;
275         s->set_hw_mute = NULL;
276         return -1;
277     }
278
279     s->hw_muted = !sw;
280
281     return 0;
282 }
283
284 static int source_set_hw_mute_cb(pa_source *s) {
285     struct userdata *u = s->userdata;
286     int err;
287
288     assert(u && u->mixer_elem);
289
290     err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
291     if (err) {
292         pa_log_error(__FILE__": Unable to set switch: %s", snd_strerror(err));
293         s->get_hw_mute = NULL;
294         s->set_hw_mute = NULL;
295         return -1;
296     }
297
298     return 0;
299 }
300
301 int pa__init(pa_core *c, pa_module*m) {
302     pa_modargs *ma = NULL;
303     int ret = -1;
304     struct userdata *u = NULL;
305     const char *dev;
306     pa_sample_spec ss;
307     pa_channel_map map;
308     unsigned periods, fragsize;
309     snd_pcm_uframes_t period_size;
310     size_t frame_size;
311     snd_pcm_info_t *pcm_info = NULL;
312     int err;
313     
314     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
315         pa_log(__FILE__": failed to parse module arguments");
316         goto fail;
317     }
318
319     ss = c->default_sample_spec;
320     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
321         pa_log(__FILE__": failed to parse sample specification");
322         goto fail;
323     }
324
325     frame_size = pa_frame_size(&ss);
326
327     /* Fix latency to 100ms */
328     periods = 12;
329     fragsize = pa_bytes_per_second(&ss)/128;
330     
331     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
332         pa_log(__FILE__": failed to parse buffer metrics");
333         goto fail;
334     }
335     period_size = fragsize/frame_size;
336     
337     u = pa_xmalloc0(sizeof(struct userdata));
338     m->userdata = u;
339     u->module = m;
340     
341     snd_config_update_free_global();
342     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) {
343         pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
344         goto fail;
345     }
346
347     if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
348         (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
349         pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
350         goto fail;
351     }
352
353     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
354         pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
355         goto fail;
356     }
357
358     if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
359         pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
360         goto fail;
361     }
362
363     if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
364         !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture"))) {
365         snd_mixer_close(u->mixer_handle);
366         u->mixer_handle = NULL;
367     }
368
369     u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
370     assert(u->source);
371
372     u->source->userdata = u;
373     u->source->get_latency = source_get_latency_cb;
374     if (u->mixer_handle) {
375         assert(u->mixer_elem);
376         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
377             int i;
378
379             for (i = 0;i < ss.channels;i++) {
380                 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
381                     break;
382             }
383
384             if (i == ss.channels) {
385                 u->source->get_hw_volume = source_get_hw_volume_cb;
386                 u->source->set_hw_volume = source_set_hw_volume_cb;
387                 snd_mixer_selem_get_capture_volume_range(
388                     u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
389             }
390         }
391         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
392             u->source->get_hw_mute = source_get_hw_mute_cb;
393             u->source->set_hw_mute = source_set_hw_mute_cb;
394         }
395     }
396     pa_source_set_owner(u->source, m);
397     u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
398
399     u->pcm_fdl = pa_alsa_fdlist_new();
400     assert(u->pcm_fdl);
401     if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
402         pa_log(__FILE__": failed to initialise file descriptor monitoring");
403         goto fail;
404     }
405
406     if (u->mixer_handle) {
407         u->mixer_fdl = pa_alsa_fdlist_new();
408         assert(u->mixer_fdl);
409         if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
410             pa_log(__FILE__": failed to initialise file descriptor monitoring");
411             goto fail;
412         }
413         snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
414         snd_mixer_elem_set_callback_private(u->mixer_elem, u);
415     }
416
417     u->frame_size = frame_size;
418     u->fragment_size = period_size * frame_size;
419
420     pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
421
422     u->memchunk.memblock = NULL;
423     u->memchunk.index = u->memchunk.length = 0;
424
425     snd_pcm_start(u->pcm_handle);
426     
427     ret = 0;
428
429     /* Get initial mixer settings */
430     if (u->source->get_hw_volume)
431         u->source->get_hw_volume(u->source);
432     if (u->source->get_hw_mute)
433         u->source->get_hw_mute(u->source);
434
435 finish:
436      if (ma)
437          pa_modargs_free(ma);
438
439     if (pcm_info)
440         snd_pcm_info_free(pcm_info);
441     
442     return ret;
443
444 fail:
445     
446     if (u)
447         pa__done(c, m);
448
449     goto finish;
450 }
451
452 void pa__done(pa_core *c, pa_module*m) {
453     struct userdata *u;
454     assert(c && m);
455
456     if (!(u = m->userdata))
457         return;
458     
459     if (u->source) {
460         pa_source_disconnect(u->source);
461         pa_source_unref(u->source);
462     }
463     
464     if (u->pcm_fdl)
465         pa_alsa_fdlist_free(u->pcm_fdl);
466     if (u->mixer_fdl)
467         pa_alsa_fdlist_free(u->mixer_fdl);
468
469     if (u->mixer_handle)
470         snd_mixer_close(u->mixer_handle);
471     
472     if (u->pcm_handle) {
473         snd_pcm_drop(u->pcm_handle);
474         snd_pcm_close(u->pcm_handle);
475     }
476     
477     if (u->memchunk.memblock)
478         pa_memblock_unref(u->memchunk.memblock);
479     
480     pa_xfree(u);
481 }
482