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