Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / modules / module-alsa-source.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <stdio.h>
31
32 #ifdef HAVE_SYS_POLL_H
33 #include <sys/poll.h>
34 #else
35 #include "poll.h"
36 #endif
37
38 #include <asoundlib.h>
39
40 #include <pulse/xmalloc.h>
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/core.h>
44 #include <pulsecore/module.h>
45 #include <pulsecore/memchunk.h>
46 #include <pulsecore/sink.h>
47 #include <pulsecore/modargs.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/sample-util.h>
50 #include <pulsecore/log.h>
51
52 #include "alsa-util.h"
53 #include "module-alsa-source-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("ALSA Source")
57 PA_MODULE_VERSION(PACKAGE_VERSION)
58 PA_MODULE_USAGE(
59         "source_name=<name for the source> "
60         "device=<ALSA device> "
61         "format=<sample format> "
62         "channels=<number of channels> "
63         "rate=<sample rate> "
64         "fragments=<number of fragments> "
65         "fragment_size=<fragment size> "
66         "channel_map=<channel map>")
67
68 struct userdata {
69     snd_pcm_t *pcm_handle;
70     snd_mixer_t *mixer_handle;
71     snd_mixer_elem_t *mixer_elem;
72     pa_source *source;
73     struct pa_alsa_fdlist *pcm_fdl;
74     struct pa_alsa_fdlist *mixer_fdl;
75     long hw_volume_max, hw_volume_min;
76
77     size_t frame_size, fragment_size;
78     pa_memchunk memchunk;
79     pa_module *module;
80 };
81
82 static const char* const valid_modargs[] = {
83     "device",
84     "source_name",
85     "channels",
86     "rate",
87     "format",
88     "fragments",
89     "fragment_size",
90     "channel_map",
91     NULL
92 };
93
94 #define DEFAULT_DEVICE "default"
95
96 static void update_usage(struct userdata *u) {
97    pa_module_set_used(u->module, u->source ? pa_source_used_by(u->source) : 0);
98 }
99
100 static void clear_up(struct userdata *u) {
101     assert(u);
102
103     if (u->source) {
104         pa_source_disconnect(u->source);
105         pa_source_unref(u->source);
106         u->source = NULL;
107     }
108
109     if (u->pcm_fdl)
110         pa_alsa_fdlist_free(u->pcm_fdl);
111     if (u->mixer_fdl)
112         pa_alsa_fdlist_free(u->mixer_fdl);
113
114     u->pcm_fdl = u->mixer_fdl = NULL;
115
116     if (u->mixer_handle) {
117         snd_mixer_close(u->mixer_handle);
118         u->mixer_handle = NULL;
119     }
120
121     if (u->pcm_handle) {
122         snd_pcm_drop(u->pcm_handle);
123         snd_pcm_close(u->pcm_handle);
124         u->pcm_handle = NULL;
125     }
126 }
127
128 static int xrun_recovery(struct userdata *u) {
129     int ret;
130     assert(u);
131
132     pa_log_info("*** ALSA-XRUN (capture) ***");
133
134     if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0) {
135         pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
136
137         clear_up(u);
138         pa_module_unload_request(u->module);
139
140         return -1;
141     }
142
143     return 0;
144 }
145
146 static void do_read(struct userdata *u) {
147     assert(u);
148
149     update_usage(u);
150
151     for (;;) {
152         pa_memchunk post_memchunk;
153         snd_pcm_sframes_t frames;
154         size_t l;
155
156         if (!u->memchunk.memblock) {
157             u->memchunk.memblock = pa_memblock_new(u->source->core->mempool, u->memchunk.length = u->fragment_size);
158             u->memchunk.index = 0;
159         }
160
161         assert(u->memchunk.memblock);
162         assert(u->memchunk.length);
163         assert(u->memchunk.memblock->data);
164         assert(u->memchunk.memblock->length);
165         assert(u->memchunk.length % u->frame_size == 0);
166
167         if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
168             if (frames == -EAGAIN)
169                 return;
170
171             if (frames == -EPIPE) {
172                 if (xrun_recovery(u) < 0)
173                     return;
174
175                 continue;
176             }
177
178             pa_log("snd_pcm_readi() failed: %s", snd_strerror(-frames));
179
180             clear_up(u);
181             pa_module_unload_request(u->module);
182             return;
183         }
184
185         l = frames * u->frame_size;
186
187         post_memchunk = u->memchunk;
188         post_memchunk.length = l;
189
190         pa_source_post(u->source, &post_memchunk);
191
192         u->memchunk.index += l;
193         u->memchunk.length -= l;
194
195         if (u->memchunk.length == 0) {
196             pa_memblock_unref(u->memchunk.memblock);
197             u->memchunk.memblock = NULL;
198             u->memchunk.index = u->memchunk.length = 0;
199         }
200
201         break;
202     }
203 }
204
205 static void fdl_callback(void *userdata) {
206     struct userdata *u = userdata;
207     assert(u);
208
209     if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
210         if (xrun_recovery(u) < 0)
211             return;
212
213     do_read(u);
214 }
215
216 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
217     struct userdata *u = snd_mixer_elem_get_callback_private(elem);
218
219     assert(u && u->mixer_handle);
220
221     if (mask == SND_CTL_EVENT_MASK_REMOVE)
222         return 0;
223
224     if (mask & SND_CTL_EVENT_MASK_VALUE) {
225         if (u->source->get_hw_volume)
226             u->source->get_hw_volume(u->source);
227         if (u->source->get_hw_mute)
228             u->source->get_hw_mute(u->source);
229
230         pa_subscription_post(u->source->core,
231             PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
232             u->source->index);
233     }
234
235     return 0;
236 }
237
238 static pa_usec_t source_get_latency_cb(pa_source *s) {
239     struct userdata *u = s->userdata;
240     snd_pcm_sframes_t frames;
241     assert(s && u && u->source);
242
243     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
244         pa_log("failed to get delay");
245         s->get_latency = NULL;
246         return 0;
247     }
248
249     return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
250 }
251
252 static int source_get_hw_volume_cb(pa_source *s) {
253     struct userdata *u = s->userdata;
254     long vol;
255     int err;
256     int i;
257
258     assert(u && u->mixer_elem);
259
260     for (i = 0;i < s->hw_volume.channels;i++) {
261         long set_vol;
262
263         assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
264
265         if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol)) < 0)
266             goto fail;
267
268         set_vol = (long) roundf(((float) s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
269
270         /* Try to avoid superfluous volume changes */
271         if (set_vol != vol)
272             s->hw_volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
273     }
274
275     return 0;
276
277 fail:
278     pa_log_error("Unable to read volume: %s", snd_strerror(err));
279     s->get_hw_volume = NULL;
280     s->set_hw_volume = NULL;
281     return -1;
282 }
283
284 static int source_set_hw_volume_cb(pa_source *s) {
285     struct userdata *u = s->userdata;
286     int err;
287     pa_volume_t vol;
288     int i;
289
290     assert(u && u->mixer_elem);
291
292     for (i = 0;i < s->hw_volume.channels;i++) {
293         assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
294
295         vol = s->hw_volume.values[i];
296
297         if (vol > PA_VOLUME_NORM)
298             vol = PA_VOLUME_NORM;
299
300         vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
301
302         if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol)) < 0)
303             goto fail;
304     }
305
306     return 0;
307
308 fail:
309     pa_log_error("Unable to set volume: %s", snd_strerror(err));
310     s->get_hw_volume = NULL;
311     s->set_hw_volume = NULL;
312     return -1;
313 }
314
315 static int source_get_hw_mute_cb(pa_source *s) {
316     struct userdata *u = s->userdata;
317     int err, sw;
318
319     assert(u && u->mixer_elem);
320
321     err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
322     if (err) {
323         pa_log_error("Unable to get switch: %s", snd_strerror(err));
324         s->get_hw_mute = NULL;
325         s->set_hw_mute = NULL;
326         return -1;
327     }
328
329     s->hw_muted = !sw;
330
331     return 0;
332 }
333
334 static int source_set_hw_mute_cb(pa_source *s) {
335     struct userdata *u = s->userdata;
336     int err;
337
338     assert(u && u->mixer_elem);
339
340     err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
341     if (err) {
342         pa_log_error("Unable to set switch: %s", snd_strerror(err));
343         s->get_hw_mute = NULL;
344         s->set_hw_mute = NULL;
345         return -1;
346     }
347
348     return 0;
349 }
350
351 int pa__init(pa_core *c, pa_module*m) {
352     pa_modargs *ma = NULL;
353     int ret = -1;
354     struct userdata *u = NULL;
355     const char *dev;
356     pa_sample_spec ss;
357     pa_channel_map map;
358     unsigned periods, fragsize;
359     snd_pcm_uframes_t period_size;
360     size_t frame_size;
361     snd_pcm_info_t *pcm_info = NULL;
362     int err;
363     char *t;
364     const char *name;
365     char *name_buf = NULL;
366     int namereg_fail;
367
368     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
369         pa_log("failed to parse module arguments");
370         goto fail;
371     }
372
373     ss = c->default_sample_spec;
374     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
375         pa_log("failed to parse sample specification");
376         goto fail;
377     }
378
379     frame_size = pa_frame_size(&ss);
380
381     /* Fix latency to 100ms */
382     periods = 12;
383     fragsize = pa_bytes_per_second(&ss)/128;
384
385     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
386         pa_log("failed to parse buffer metrics");
387         goto fail;
388     }
389     period_size = fragsize/frame_size;
390
391     u = pa_xnew0(struct userdata, 1);
392     m->userdata = u;
393     u->module = m;
394
395     snd_config_update_free_global();
396     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) {
397         pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
398         goto fail;
399     }
400
401     if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
402         (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
403         pa_log("Error fetching PCM info: %s", snd_strerror(err));
404         goto fail;
405     }
406
407     if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
408         pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
409         goto fail;
410     }
411
412     if (ss.channels != map.channels)
413         /* Seems ALSA didn't like the channel number, so let's fix the channel map */
414         pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
415
416     if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
417         pa_log("Error opening mixer: %s", snd_strerror(err));
418         goto fail;
419     }
420
421     if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
422         !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", "Mic"))) {
423         snd_mixer_close(u->mixer_handle);
424         u->mixer_handle = NULL;
425     }
426
427     if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
428         namereg_fail = 1;
429     else {
430         name = name_buf = pa_sprintf_malloc("alsa_input.%s", dev);
431         namereg_fail = 0;
432     }
433
434     if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map))) {
435         pa_log("Failed to create source object");
436         goto fail;
437     }
438
439     u->source->is_hardware = 1;
440     u->source->userdata = u;
441     u->source->get_latency = source_get_latency_cb;
442     if (u->mixer_handle) {
443         assert(u->mixer_elem);
444         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
445             int i;
446
447             for (i = 0;i < ss.channels;i++) {
448                 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
449                     break;
450             }
451
452             if (i == ss.channels) {
453                 u->source->get_hw_volume = source_get_hw_volume_cb;
454                 u->source->set_hw_volume = source_set_hw_volume_cb;
455                 snd_mixer_selem_get_capture_volume_range(
456                     u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
457             }
458         }
459         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
460             u->source->get_hw_mute = source_get_hw_mute_cb;
461             u->source->set_hw_mute = source_set_hw_mute_cb;
462         }
463     }
464     pa_source_set_owner(u->source, m);
465     pa_source_set_description(u->source, t = pa_sprintf_malloc("ALSA PCM on %s (%s)", dev, snd_pcm_info_get_name(pcm_info)));
466     pa_xfree(t);
467
468     u->pcm_fdl = pa_alsa_fdlist_new();
469     assert(u->pcm_fdl);
470     if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
471         pa_log("failed to initialise file descriptor monitoring");
472         goto fail;
473     }
474
475     if (u->mixer_handle) {
476         u->mixer_fdl = pa_alsa_fdlist_new();
477         assert(u->mixer_fdl);
478         if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
479             pa_log("failed to initialise file descriptor monitoring");
480             goto fail;
481         }
482         snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
483         snd_mixer_elem_set_callback_private(u->mixer_elem, u);
484     } else
485         u->mixer_fdl = NULL;
486
487     u->frame_size = frame_size;
488     u->fragment_size = period_size * frame_size;
489
490     pa_log_info("using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
491
492     u->memchunk.memblock = NULL;
493     u->memchunk.index = u->memchunk.length = 0;
494
495     snd_pcm_start(u->pcm_handle);
496
497     ret = 0;
498
499     /* Get initial mixer settings */
500     if (u->source->get_hw_volume)
501         u->source->get_hw_volume(u->source);
502     if (u->source->get_hw_mute)
503         u->source->get_hw_mute(u->source);
504
505 finish:
506     pa_xfree(name_buf);
507
508     if (ma)
509          pa_modargs_free(ma);
510
511     if (pcm_info)
512         snd_pcm_info_free(pcm_info);
513
514     return ret;
515
516 fail:
517
518     if (u)
519         pa__done(c, m);
520
521     goto finish;
522 }
523
524 void pa__done(pa_core *c, pa_module*m) {
525     struct userdata *u;
526     assert(c && m);
527
528     if (!(u = m->userdata))
529         return;
530
531     clear_up(u);
532
533     if (u->memchunk.memblock)
534         pa_memblock_unref(u->memchunk.memblock);
535
536     pa_xfree(u);
537 }
538