add new function pa_yes_no()
[pulseaudio.git] / src / modules / module-alsa-source.c
blobd840cac3090a873bb31f1c141183d4910ac25fa7
1 /* $Id$ */
3 /***
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
22 USA.
23 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdio.h>
31 #include <asoundlib.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/core.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/memchunk.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
51 #include "alsa-util.h"
52 #include "module-alsa-source-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("ALSA Source")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE(
58 "source_name=<name for the source> "
59 "device=<ALSA device> "
60 "format=<sample format> "
61 "channels=<number of channels> "
62 "rate=<sample rate> "
63 "fragments=<number of fragments> "
64 "fragment_size=<fragment size> "
65 "channel_map=<channel map> "
66 "mmap=<enable memory mapping?>")
68 #define DEFAULT_DEVICE "default"
70 struct userdata {
71 pa_core *core;
72 pa_module *module;
73 pa_source *source;
75 pa_thread *thread;
76 pa_thread_mq thread_mq;
77 pa_rtpoll *rtpoll;
79 snd_pcm_t *pcm_handle;
81 pa_alsa_fdlist *mixer_fdl;
82 snd_mixer_t *mixer_handle;
83 snd_mixer_elem_t *mixer_elem;
84 long hw_volume_max, hw_volume_min;
86 size_t frame_size, fragment_size, hwbuf_size;
87 unsigned nfragments;
89 char *device_name;
91 int use_mmap;
93 pa_rtpoll_item *alsa_rtpoll_item;
96 static const char* const valid_modargs[] = {
97 "device",
98 "source_name",
99 "channels",
100 "rate",
101 "format",
102 "fragments",
103 "fragment_size",
104 "channel_map",
105 "mmap",
106 NULL
109 static int mmap_read(struct userdata *u) {
110 int work_done = 0;
112 pa_assert(u);
113 pa_source_assert_ref(u->source);
115 for (;;) {
116 snd_pcm_sframes_t n;
117 int err;
118 const snd_pcm_channel_area_t *areas;
119 snd_pcm_uframes_t offset, frames;
120 pa_memchunk chunk;
121 void *p;
123 if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
125 if (n == -EPIPE)
126 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
128 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
129 continue;
131 if (err == -EAGAIN)
132 return work_done;
134 pa_log("snd_pcm_avail_update: %s", snd_strerror(err));
135 return -1;
138 /* pa_log("Got request for %i samples", (int) n); */
140 if (n <= 0)
141 return work_done;
143 frames = n;
145 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
147 if (err == -EPIPE)
148 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
150 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
151 continue;
153 if (err == -EAGAIN)
154 return work_done;
156 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
157 return -1;
160 /* Check these are multiples of 8 bit */
161 pa_assert((areas[0].first & 7) == 0);
162 pa_assert((areas[0].step & 7)== 0);
164 /* We assume a single interleaved memory buffer */
165 pa_assert((areas[0].first >> 3) == 0);
166 pa_assert((areas[0].step >> 3) == u->frame_size);
168 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
170 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
171 chunk.length = pa_memblock_get_length(chunk.memblock);
172 chunk.index = 0;
174 pa_source_post(u->source, &chunk);
176 /* FIXME: Maybe we can do something to keep this memory block
177 * a little bit longer around? */
178 pa_memblock_unref_fixed(chunk.memblock);
180 if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
182 if (err == -EPIPE)
183 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
185 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
186 continue;
188 if (err == -EAGAIN)
189 return work_done;
191 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
192 return -1;
195 work_done = 1;
197 /* pa_log("wrote %i samples", (int) frames); */
201 static int unix_read(struct userdata *u) {
202 snd_pcm_status_t *status;
203 int work_done = 0;
205 snd_pcm_status_alloca(&status);
207 pa_assert(u);
208 pa_source_assert_ref(u->source);
210 for (;;) {
211 void *p;
212 snd_pcm_sframes_t t, k;
213 ssize_t l;
214 int err;
215 pa_memchunk chunk;
217 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
218 pa_log("Failed to query DSP status data: %s", snd_strerror(err));
219 return -1;
222 if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
223 pa_log_debug("Buffer overrun!");
225 l = snd_pcm_status_get_avail(status) * u->frame_size;
227 if (l <= 0)
228 return work_done;
230 chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
232 k = pa_memblock_get_length(chunk.memblock);
234 if (k > l)
235 k = l;
237 k = (k/u->frame_size)*u->frame_size;
239 p = pa_memblock_acquire(chunk.memblock);
240 t = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, k / u->frame_size);
241 pa_memblock_release(chunk.memblock);
243 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
245 pa_assert(t != 0);
247 if (t < 0) {
248 pa_memblock_unref(chunk.memblock);
250 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
251 continue;
253 if (t == -EAGAIN) {
254 pa_log_debug("EAGAIN");
255 return work_done;
256 } else {
257 pa_log("Failed to read data from DSP: %s", snd_strerror(t));
258 return -1;
262 chunk.index = 0;
263 chunk.length = t * u->frame_size;
265 pa_source_post(u->source, &chunk);
266 pa_memblock_unref(chunk.memblock);
268 work_done = 1;
270 if (t * u->frame_size >= (unsigned) l)
271 return work_done;
275 static pa_usec_t source_get_latency(struct userdata *u) {
276 pa_usec_t r = 0;
277 snd_pcm_status_t *status;
278 snd_pcm_sframes_t frames = 0;
279 int err;
281 snd_pcm_status_alloca(&status);
283 pa_assert(u);
284 pa_assert(u->pcm_handle);
286 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
287 pa_log("Failed to get delay: %s", snd_strerror(err));
288 else
289 frames = snd_pcm_status_get_delay(status);
291 if (frames > 0)
292 r = pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec);
294 return r;
297 static int build_pollfd(struct userdata *u) {
298 int err;
299 struct pollfd *pollfd;
300 int n;
302 pa_assert(u);
303 pa_assert(u->pcm_handle);
305 if ((n = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
306 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
307 return -1;
310 if (u->alsa_rtpoll_item)
311 pa_rtpoll_item_free(u->alsa_rtpoll_item);
313 u->alsa_rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, n);
314 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, NULL);
316 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd, n)) < 0) {
317 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
318 return -1;
321 return 0;
324 static int suspend(struct userdata *u) {
325 pa_assert(u);
326 pa_assert(u->pcm_handle);
328 /* Let's suspend */
329 snd_pcm_close(u->pcm_handle);
330 u->pcm_handle = NULL;
332 if (u->alsa_rtpoll_item) {
333 pa_rtpoll_item_free(u->alsa_rtpoll_item);
334 u->alsa_rtpoll_item = NULL;
337 pa_log_info("Device suspended...");
339 return 0;
342 static int unsuspend(struct userdata *u) {
343 pa_sample_spec ss;
344 int err, b;
345 unsigned nfrags;
346 snd_pcm_uframes_t period_size;
348 pa_assert(u);
349 pa_assert(!u->pcm_handle);
351 pa_log_info("Trying resume...");
353 snd_config_update_free_global();
354 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
355 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
356 goto fail;
359 ss = u->source->sample_spec;
360 nfrags = u->nfragments;
361 period_size = u->fragment_size / u->frame_size;
362 b = u->use_mmap;
364 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
365 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
366 goto fail;
369 if (b != u->use_mmap) {
370 pa_log_warn("Resume failed, couldn't get original access mode.");
371 goto fail;
374 if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
375 pa_log_warn("Resume failed, couldn't restore original sample settings.");
376 goto fail;
379 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
380 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
381 goto fail;
384 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
385 pa_log("Failed to set software parameters: %s", snd_strerror(err));
386 goto fail;
389 if (build_pollfd(u) < 0)
390 goto fail;
392 snd_pcm_start(u->pcm_handle);
394 /* FIXME: We need to reload the volume somehow */
396 pa_log_info("Resumed successfully...");
398 return 0;
400 fail:
401 if (u->pcm_handle) {
402 snd_pcm_close(u->pcm_handle);
403 u->pcm_handle = NULL;
406 return -1;
409 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
410 struct userdata *u = PA_SOURCE(o)->userdata;
412 switch (code) {
414 case PA_SOURCE_MESSAGE_GET_LATENCY: {
415 pa_usec_t r = 0;
417 if (u->pcm_handle)
418 r = source_get_latency(u);
420 *((pa_usec_t*) data) = r;
422 return 0;
425 case PA_SOURCE_MESSAGE_SET_STATE:
427 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
429 case PA_SOURCE_SUSPENDED:
430 pa_assert(PA_SOURCE_OPENED(u->source->thread_info.state));
432 if (suspend(u) < 0)
433 return -1;
435 break;
437 case PA_SOURCE_IDLE:
438 case PA_SOURCE_RUNNING:
440 if (u->source->thread_info.state == PA_SOURCE_INIT) {
441 if (build_pollfd(u) < 0)
442 return -1;
444 snd_pcm_start(u->pcm_handle);
447 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
448 if (unsuspend(u) < 0)
449 return -1;
452 break;
454 case PA_SOURCE_UNLINKED:
455 case PA_SOURCE_INIT:
459 break;
462 return pa_source_process_msg(o, code, data, offset, chunk);
465 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
466 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
468 pa_assert(u);
469 pa_assert(u->mixer_handle);
471 if (mask == SND_CTL_EVENT_MASK_REMOVE)
472 return 0;
474 if (mask & SND_CTL_EVENT_MASK_VALUE) {
475 pa_source_get_volume(u->source);
476 pa_source_get_mute(u->source);
479 return 0;
482 static int source_get_volume_cb(pa_source *s) {
483 struct userdata *u = s->userdata;
484 int err;
485 int i;
487 pa_assert(u);
488 pa_assert(u->mixer_elem);
490 for (i = 0; i < s->sample_spec.channels; i++) {
491 long set_vol, vol;
493 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
495 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol)) < 0)
496 goto fail;
498 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
500 /* Try to avoid superfluous volume changes */
501 if (set_vol != vol)
502 s->volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
505 return 0;
507 fail:
508 pa_log_error("Unable to read volume: %s", snd_strerror(err));
510 s->get_volume = NULL;
511 s->set_volume = NULL;
512 return -1;
515 static int source_set_volume_cb(pa_source *s) {
516 struct userdata *u = s->userdata;
517 int err;
518 int i;
520 pa_assert(u);
521 pa_assert(u->mixer_elem);
523 for (i = 0; i < s->sample_spec.channels; i++) {
524 long alsa_vol;
525 pa_volume_t vol;
527 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
529 vol = s->volume.values[i];
531 if (vol > PA_VOLUME_NORM)
532 vol = PA_VOLUME_NORM;
534 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
536 if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, alsa_vol)) < 0)
537 goto fail;
540 return 0;
542 fail:
543 pa_log_error("Unable to set volume: %s", snd_strerror(err));
545 s->get_volume = NULL;
546 s->set_volume = NULL;
547 return -1;
550 static int source_get_mute_cb(pa_source *s) {
551 struct userdata *u = s->userdata;
552 int err, sw;
554 pa_assert(u);
555 pa_assert(u->mixer_elem);
557 if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) {
558 pa_log_error("Unable to get switch: %s", snd_strerror(err));
560 s->get_mute = NULL;
561 s->set_mute = NULL;
562 return -1;
565 s->muted = !sw;
567 return 0;
570 static int source_set_mute_cb(pa_source *s) {
571 struct userdata *u = s->userdata;
572 int err;
574 pa_assert(u);
575 pa_assert(u->mixer_elem);
577 if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) {
578 pa_log_error("Unable to set switch: %s", snd_strerror(err));
580 s->get_mute = NULL;
581 s->set_mute = NULL;
582 return -1;
585 return 0;
588 static void thread_func(void *userdata) {
589 struct userdata *u = userdata;
591 pa_assert(u);
593 pa_log_debug("Thread starting up");
595 if (u->core->high_priority)
596 pa_make_realtime();
598 pa_thread_mq_install(&u->thread_mq);
599 pa_rtpoll_install(u->rtpoll);
601 for (;;) {
602 int ret;
604 /* Read some data and pass it to the sources */
605 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
607 if (u->use_mmap) {
608 if (mmap_read(u) < 0)
609 goto fail;
611 } else {
612 if (unix_read(u) < 0)
613 goto fail;
617 /* Hmm, nothing to do. Let's sleep */
618 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
619 goto fail;
621 if (ret == 0)
622 goto finish;
624 /* Tell ALSA about this and process its response */
625 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
626 struct pollfd *pollfd;
627 unsigned short revents = 0;
628 int err;
629 unsigned n;
631 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
633 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
634 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
635 goto fail;
638 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
640 if (revents & POLLERR)
641 pa_log_warn("Got POLLERR from ALSA");
642 if (revents & POLLNVAL)
643 pa_log_warn("Got POLLNVAL from ALSA");
644 if (revents & POLLHUP)
645 pa_log_warn("Got POLLHUP from ALSA");
647 /* Try to recover from this error */
649 switch (snd_pcm_state(u->pcm_handle)) {
651 case SND_PCM_STATE_XRUN:
652 if ((err = snd_pcm_recover(u->pcm_handle, -EPIPE, 1)) != 0) {
653 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
654 goto fail;
656 break;
658 case SND_PCM_STATE_SUSPENDED:
659 if ((err = snd_pcm_recover(u->pcm_handle, -ESTRPIPE, 1)) != 0) {
660 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
661 goto fail;
663 break;
665 default:
667 snd_pcm_drop(u->pcm_handle);
669 if ((err = snd_pcm_prepare(u->pcm_handle)) < 0) {
670 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
671 goto fail;
673 break;
679 fail:
680 /* If this was no regular exit from the loop we have to continue
681 * processing messages until we received PA_MESSAGE_SHUTDOWN */
682 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
683 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
685 finish:
686 pa_log_debug("Thread shutting down");
689 int pa__init(pa_module*m) {
691 pa_modargs *ma = NULL;
692 struct userdata *u = NULL;
693 char *dev;
694 pa_sample_spec ss;
695 pa_channel_map map;
696 unsigned nfrags, frag_size;
697 snd_pcm_uframes_t period_size;
698 size_t frame_size;
699 snd_pcm_info_t *pcm_info = NULL;
700 int err;
701 char *t;
702 const char *name;
703 char *name_buf = NULL;
704 int namereg_fail;
705 int use_mmap = 1, b;
707 snd_pcm_info_alloca(&pcm_info);
709 pa_assert(m);
711 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
712 pa_log("Failed to parse module arguments");
713 goto fail;
716 ss = m->core->default_sample_spec;
717 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
718 pa_log("Failed to parse sample specification");
719 goto fail;
722 frame_size = pa_frame_size(&ss);
724 nfrags = m->core->default_n_fragments;
725 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
726 if (frag_size <= 0)
727 frag_size = frame_size;
729 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
730 pa_log("Failed to parse buffer metrics");
731 goto fail;
733 period_size = frag_size/frame_size;
735 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
736 pa_log("Failed to parse mmap argument.");
737 goto fail;
740 u = pa_xnew0(struct userdata, 1);
741 u->core = m->core;
742 u->module = m;
743 m->userdata = u;
744 u->use_mmap = use_mmap;
745 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
746 u->rtpoll = pa_rtpoll_new();
747 u->alsa_rtpoll_item = NULL;
748 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
750 snd_config_update_free_global();
752 dev = pa_xstrdup(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE));
754 for (;;) {
756 if ((err = snd_pcm_open(&u->pcm_handle, dev, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
757 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
758 pa_xfree(dev);
759 goto fail;
762 b = use_mmap;
763 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
765 if (err == -EPERM) {
766 /* Hmm, some hw is very exotic, so we retry with plughw, if hw didn't work */
768 if (pa_startswith(dev, "hw:")) {
769 char *d = pa_sprintf_malloc("plughw:%s", dev+3);
770 pa_log_debug("Opening the device as '%s' didn't work, retrying with '%s'.", dev, d);
771 pa_xfree(dev);
772 dev = d;
774 snd_pcm_close(u->pcm_handle);
775 u->pcm_handle = NULL;
776 continue;
780 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
781 pa_xfree(dev);
782 goto fail;
785 break;
788 u->device_name = dev;
790 if (use_mmap && !b) {
791 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
792 u->use_mmap = use_mmap = b;
795 if (u->use_mmap)
796 pa_log_info("Successfully enabled mmap() mode.");
798 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
799 pa_log("Error fetching PCM info: %s", snd_strerror(err));
800 goto fail;
803 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
804 pa_log("Failed to set software parameters: %s", snd_strerror(err));
805 goto fail;
808 /* ALSA might tweak the sample spec, so recalculate the frame size */
809 frame_size = pa_frame_size(&ss);
811 if (ss.channels != map.channels)
812 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
813 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
815 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
816 pa_log("Error opening mixer: %s", snd_strerror(err));
817 else {
819 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
820 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", NULL))) {
821 snd_mixer_close(u->mixer_handle);
822 u->mixer_handle = NULL;
826 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
827 namereg_fail = 1;
828 else {
829 name = name_buf = pa_sprintf_malloc("alsa_input.%s", dev);
830 namereg_fail = 0;
833 u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
834 pa_xfree(name_buf);
836 if (!u->source) {
837 pa_log("Failed to create source object");
838 goto fail;
841 u->source->parent.process_msg = source_process_msg;
842 u->source->userdata = u;
844 pa_source_set_module(u->source, m);
845 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
846 pa_source_set_rtpoll(u->source, u->rtpoll);
847 pa_source_set_description(u->source, t = pa_sprintf_malloc(
848 "ALSA PCM on %s (%s)%s",
849 dev,
850 snd_pcm_info_get_name(pcm_info),
851 use_mmap ? " via DMA" : ""));
852 pa_xfree(t);
854 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL;
856 u->frame_size = frame_size;
857 u->fragment_size = frag_size = period_size * frame_size;
858 u->nfragments = nfrags;
859 u->hwbuf_size = u->fragment_size * nfrags;
861 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
863 if (u->mixer_handle) {
864 pa_assert(u->mixer_elem);
866 if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
867 int i;
869 for (i = 0;i < ss.channels;i++) {
870 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
871 break;
874 if (i == ss.channels) {
875 u->source->get_volume = source_get_volume_cb;
876 u->source->set_volume = source_set_volume_cb;
877 snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
881 if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
882 u->source->get_mute = source_get_mute_cb;
883 u->source->set_mute = source_set_mute_cb;
886 u->mixer_fdl = pa_alsa_fdlist_new();
888 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
889 pa_log("failed to initialise file descriptor monitoring");
890 goto fail;
893 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
894 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
895 } else
896 u->mixer_fdl = NULL;
898 if (!(u->thread = pa_thread_new(thread_func, u))) {
899 pa_log("Failed to create thread.");
900 goto fail;
902 /* Get initial mixer settings */
903 if (u->source->get_volume)
904 u->source->get_volume(u->source);
905 if (u->source->get_mute)
906 u->source->get_mute(u->source);
908 pa_source_put(u->source);
910 pa_modargs_free(ma);
912 return 0;
914 fail:
916 if (ma)
917 pa_modargs_free(ma);
919 pa__done(m);
921 return -1;
924 void pa__done(pa_module*m) {
925 struct userdata *u;
927 pa_assert(m);
929 if (!(u = m->userdata))
930 return;
932 if (u->source)
933 pa_source_unlink(u->source);
935 if (u->thread) {
936 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
937 pa_thread_free(u->thread);
940 pa_thread_mq_done(&u->thread_mq);
942 if (u->source)
943 pa_source_unref(u->source);
945 if (u->alsa_rtpoll_item)
946 pa_rtpoll_item_free(u->alsa_rtpoll_item);
948 if (u->rtpoll)
949 pa_rtpoll_free(u->rtpoll);
951 if (u->mixer_fdl)
952 pa_alsa_fdlist_free(u->mixer_fdl);
954 if (u->mixer_handle)
955 snd_mixer_close(u->mixer_handle);
957 if (u->pcm_handle) {
958 snd_pcm_drop(u->pcm_handle);
959 snd_pcm_close(u->pcm_handle);
962 pa_xfree(u->device_name);
963 pa_xfree(u);
965 snd_config_update_free_global();