4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
32 #ifdef HAVE_SYS_POLL_H
38 #include <asoundlib.h>
40 #include <pulse/xmalloc.h>
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>
52 #include "alsa-util.h"
53 #include "module-alsa-source-symdef.h"
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("ALSA Source")
57 PA_MODULE_VERSION(PACKAGE_VERSION)
59 "source_name=<name for the source> "
60 "device=<ALSA device> "
61 "format=<sample format> "
62 "channels=<number of channels> "
64 "fragments=<number of fragments> "
65 "fragment_size=<fragment size> "
66 "channel_map=<channel map>")
69 snd_pcm_t *pcm_handle;
70 snd_mixer_t *mixer_handle;
71 snd_mixer_elem_t *mixer_elem;
73 struct pa_alsa_fdlist *pcm_fdl;
74 struct pa_alsa_fdlist *mixer_fdl;
75 long hw_volume_max, hw_volume_min;
77 size_t frame_size, fragment_size;
82 static const char* const valid_modargs[] = {
94 #define DEFAULT_DEVICE "default"
96 static void update_usage(struct userdata *u) {
97 pa_module_set_used(u->module, u->source ? pa_source_used_by(u->source) : 0);
100 static void clear_up(struct userdata *u) {
104 pa_source_disconnect(u->source);
105 pa_source_unref(u->source);
110 pa_alsa_fdlist_free(u->pcm_fdl);
112 pa_alsa_fdlist_free(u->mixer_fdl);
114 u->pcm_fdl = u->mixer_fdl = NULL;
116 if (u->mixer_handle) {
117 snd_mixer_close(u->mixer_handle);
118 u->mixer_handle = NULL;
122 snd_pcm_drop(u->pcm_handle);
123 snd_pcm_close(u->pcm_handle);
124 u->pcm_handle = NULL;
128 static int xrun_recovery(struct userdata *u) {
132 pa_log_info("*** ALSA-XRUN (capture) ***");
134 if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0) {
135 pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
138 pa_module_unload_request(u->module);
146 static void do_read(struct userdata *u) {
152 pa_memchunk post_memchunk;
153 snd_pcm_sframes_t frames;
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;
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);
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)
171 if (frames == -EPIPE) {
172 if (xrun_recovery(u) < 0)
178 pa_log("snd_pcm_readi() failed: %s", snd_strerror(-frames));
181 pa_module_unload_request(u->module);
185 l = frames * u->frame_size;
187 post_memchunk = u->memchunk;
188 post_memchunk.length = l;
190 pa_source_post(u->source, &post_memchunk);
192 u->memchunk.index += l;
193 u->memchunk.length -= l;
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;
205 static void fdl_callback(void *userdata) {
206 struct userdata *u = userdata;
209 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
210 if (xrun_recovery(u) < 0)
216 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
217 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
219 assert(u && u->mixer_handle);
221 if (mask == SND_CTL_EVENT_MASK_REMOVE)
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);
230 pa_subscription_post(u->source->core,
231 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
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);
243 if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
244 pa_log("failed to get delay");
245 s->get_latency = NULL;
249 return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
252 static int source_get_hw_volume_cb(pa_source *s) {
253 struct userdata *u = s->userdata;
258 assert(u && u->mixer_elem);
260 for (i = 0;i < s->hw_volume.channels;i++) {
263 assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
265 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol)) < 0)
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;
270 /* Try to avoid superfluous volume changes */
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));
278 pa_log_error("Unable to read volume: %s", snd_strerror(err));
279 s->get_hw_volume = NULL;
280 s->set_hw_volume = NULL;
284 static int source_set_hw_volume_cb(pa_source *s) {
285 struct userdata *u = s->userdata;
290 assert(u && u->mixer_elem);
292 for (i = 0;i < s->hw_volume.channels;i++) {
293 assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
295 vol = s->hw_volume.values[i];
297 if (vol > PA_VOLUME_NORM)
298 vol = PA_VOLUME_NORM;
300 vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
302 if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol)) < 0)
309 pa_log_error("Unable to set volume: %s", snd_strerror(err));
310 s->get_hw_volume = NULL;
311 s->set_hw_volume = NULL;
315 static int source_get_hw_mute_cb(pa_source *s) {
316 struct userdata *u = s->userdata;
319 assert(u && u->mixer_elem);
321 err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
323 pa_log_error("Unable to get switch: %s", snd_strerror(err));
324 s->get_hw_mute = NULL;
325 s->set_hw_mute = NULL;
334 static int source_set_hw_mute_cb(pa_source *s) {
335 struct userdata *u = s->userdata;
338 assert(u && u->mixer_elem);
340 err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
342 pa_log_error("Unable to set switch: %s", snd_strerror(err));
343 s->get_hw_mute = NULL;
344 s->set_hw_mute = NULL;
351 int pa__init(pa_core *c, pa_module*m) {
352 pa_modargs *ma = NULL;
354 struct userdata *u = NULL;
358 unsigned periods, fragsize;
359 snd_pcm_uframes_t period_size;
361 snd_pcm_info_t *pcm_info = NULL;
365 char *name_buf = NULL;
368 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
369 pa_log("failed to parse module arguments");
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");
379 frame_size = pa_frame_size(&ss);
381 /* Fix latency to 100ms */
383 fragsize = pa_bytes_per_second(&ss)/128;
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");
389 period_size = fragsize/frame_size;
391 u = pa_xnew0(struct userdata, 1);
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));
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));
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));
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);
416 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
417 pa_log("Error opening mixer: %s", snd_strerror(err));
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;
427 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
430 name = name_buf = pa_sprintf_malloc("alsa_input.%s", dev);
434 if (!(u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map))) {
435 pa_log("Failed to create source object");
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)) {
447 for (i = 0;i < ss.channels;i++) {
448 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
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);
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;
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)));
468 u->pcm_fdl = pa_alsa_fdlist_new();
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");
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");
482 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
483 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
487 u->frame_size = frame_size;
488 u->fragment_size = period_size * frame_size;
490 pa_log_info("using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
492 u->memchunk.memblock = NULL;
493 u->memchunk.index = u->memchunk.length = 0;
495 snd_pcm_start(u->pcm_handle);
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);
512 snd_pcm_info_free(pcm_info);
524 void pa__done(pa_core *c, pa_module*m) {
528 if (!(u = m->userdata))
533 if (u->memchunk.memblock)
534 pa_memblock_unref(u->memchunk.memblock);