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
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_LOAD_ONCE(FALSE
);
59 "source_name=<name for the source> "
60 "device=<ALSA device> "
61 "device_id=<ALSA device id> "
62 "format=<sample format> "
63 "channels=<number of channels> "
65 "fragments=<number of fragments> "
66 "fragment_size=<fragment size> "
67 "channel_map=<channel map> "
68 "mmap=<enable memory mapping?>");
70 #define DEFAULT_DEVICE "default"
78 pa_thread_mq thread_mq
;
81 snd_pcm_t
*pcm_handle
;
83 pa_alsa_fdlist
*mixer_fdl
;
84 snd_mixer_t
*mixer_handle
;
85 snd_mixer_elem_t
*mixer_elem
;
86 long hw_volume_max
, hw_volume_min
;
88 size_t frame_size
, fragment_size
, hwbuf_size
;
95 pa_rtpoll_item
*alsa_rtpoll_item
;
98 static const char* const valid_modargs
[] = {
112 static int mmap_read(struct userdata
*u
) {
116 pa_source_assert_ref(u
->source
);
121 const snd_pcm_channel_area_t
*areas
;
122 snd_pcm_uframes_t offset
, frames
;
126 if ((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
129 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
131 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
137 pa_log("snd_pcm_avail_update: %s", snd_strerror(err
));
141 /* pa_log("Got request for %i samples", (int) n); */
148 if ((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0) {
151 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
153 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
159 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
163 /* Check these are multiples of 8 bit */
164 pa_assert((areas
[0].first
& 7) == 0);
165 pa_assert((areas
[0].step
& 7)== 0);
167 /* We assume a single interleaved memory buffer */
168 pa_assert((areas
[0].first
>> 3) == 0);
169 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
171 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
173 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
174 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
177 pa_source_post(u
->source
, &chunk
);
179 /* FIXME: Maybe we can do something to keep this memory block
180 * a little bit longer around? */
181 pa_memblock_unref_fixed(chunk
.memblock
);
183 if ((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0) {
186 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
188 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
194 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
200 /* pa_log("wrote %i samples", (int) frames); */
204 static int unix_read(struct userdata
*u
) {
205 snd_pcm_status_t
*status
;
208 snd_pcm_status_alloca(&status
);
211 pa_source_assert_ref(u
->source
);
215 snd_pcm_sframes_t t
, k
;
220 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0) {
221 pa_log("Failed to query DSP status data: %s", snd_strerror(err
));
225 if (snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
)
226 pa_log_debug("Buffer overrun!");
228 l
= snd_pcm_status_get_avail(status
) * u
->frame_size
;
233 chunk
.memblock
= pa_memblock_new(u
->core
->mempool
, (size_t) -1);
235 k
= pa_memblock_get_length(chunk
.memblock
);
240 k
= (k
/u
->frame_size
)*u
->frame_size
;
242 p
= pa_memblock_acquire(chunk
.memblock
);
243 t
= snd_pcm_readi(u
->pcm_handle
, (uint8_t*) p
, k
/ u
->frame_size
);
244 pa_memblock_release(chunk
.memblock
);
246 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
251 pa_memblock_unref(chunk
.memblock
);
253 if ((t
= snd_pcm_recover(u
->pcm_handle
, t
, 1)) == 0)
257 pa_log_debug("EAGAIN");
260 pa_log("Failed to read data from DSP: %s", snd_strerror(t
));
266 chunk
.length
= t
* u
->frame_size
;
268 pa_source_post(u
->source
, &chunk
);
269 pa_memblock_unref(chunk
.memblock
);
273 if (t
* u
->frame_size
>= (unsigned) l
)
278 static pa_usec_t
source_get_latency(struct userdata
*u
) {
280 snd_pcm_status_t
*status
;
281 snd_pcm_sframes_t frames
= 0;
284 snd_pcm_status_alloca(&status
);
287 pa_assert(u
->pcm_handle
);
289 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)
290 pa_log("Failed to get delay: %s", snd_strerror(err
));
292 frames
= snd_pcm_status_get_delay(status
);
295 r
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->source
->sample_spec
);
300 static int build_pollfd(struct userdata
*u
) {
302 struct pollfd
*pollfd
;
306 pa_assert(u
->pcm_handle
);
308 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
309 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
313 if (u
->alsa_rtpoll_item
)
314 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
316 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, n
);
317 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
319 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
320 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
327 static int suspend(struct userdata
*u
) {
329 pa_assert(u
->pcm_handle
);
332 snd_pcm_close(u
->pcm_handle
);
333 u
->pcm_handle
= NULL
;
335 if (u
->alsa_rtpoll_item
) {
336 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
337 u
->alsa_rtpoll_item
= NULL
;
340 pa_log_info("Device suspended...");
345 static int unsuspend(struct userdata
*u
) {
350 snd_pcm_uframes_t period_size
;
353 pa_assert(!u
->pcm_handle
);
355 pa_log_info("Trying resume...");
357 snd_config_update_free_global();
358 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
)) < 0) {
359 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
363 ss
= u
->source
->sample_spec
;
364 nfrags
= u
->nfragments
;
365 period_size
= u
->fragment_size
/ u
->frame_size
;
368 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
, TRUE
)) < 0) {
369 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
373 if (b
!= u
->use_mmap
) {
374 pa_log_warn("Resume failed, couldn't get original access mode.");
378 if (!pa_sample_spec_equal(&ss
, &u
->source
->sample_spec
)) {
379 pa_log_warn("Resume failed, couldn't restore original sample settings.");
383 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
384 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
388 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
389 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
393 if (build_pollfd(u
) < 0)
396 snd_pcm_start(u
->pcm_handle
);
398 /* FIXME: We need to reload the volume somehow */
400 pa_log_info("Resumed successfully...");
406 snd_pcm_close(u
->pcm_handle
);
407 u
->pcm_handle
= NULL
;
413 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
414 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
418 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
422 r
= source_get_latency(u
);
424 *((pa_usec_t
*) data
) = r
;
429 case PA_SOURCE_MESSAGE_SET_STATE
:
431 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
433 case PA_SOURCE_SUSPENDED
:
434 pa_assert(PA_SOURCE_OPENED(u
->source
->thread_info
.state
));
442 case PA_SOURCE_RUNNING
:
444 if (u
->source
->thread_info
.state
== PA_SOURCE_INIT
) {
445 if (build_pollfd(u
) < 0)
448 snd_pcm_start(u
->pcm_handle
);
451 if (u
->source
->thread_info
.state
== PA_SOURCE_SUSPENDED
) {
452 if (unsuspend(u
) < 0)
458 case PA_SOURCE_UNLINKED
:
466 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
469 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
470 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
473 pa_assert(u
->mixer_handle
);
475 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
478 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
479 pa_source_get_volume(u
->source
);
480 pa_source_get_mute(u
->source
);
486 static int source_get_volume_cb(pa_source
*s
) {
487 struct userdata
*u
= s
->userdata
;
492 pa_assert(u
->mixer_elem
);
494 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
497 pa_assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
499 if ((err
= snd_mixer_selem_get_capture_volume(u
->mixer_elem
, i
, &vol
)) < 0)
502 set_vol
= (long) roundf(((float) s
->volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
504 /* Try to avoid superfluous volume changes */
506 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
));
512 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
514 s
->get_volume
= NULL
;
515 s
->set_volume
= NULL
;
519 static int source_set_volume_cb(pa_source
*s
) {
520 struct userdata
*u
= s
->userdata
;
525 pa_assert(u
->mixer_elem
);
527 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
531 pa_assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
533 vol
= s
->volume
.values
[i
];
535 if (vol
> PA_VOLUME_NORM
)
536 vol
= PA_VOLUME_NORM
;
538 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
540 if ((err
= snd_mixer_selem_set_capture_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
547 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
549 s
->get_volume
= NULL
;
550 s
->set_volume
= NULL
;
554 static int source_get_mute_cb(pa_source
*s
) {
555 struct userdata
*u
= s
->userdata
;
559 pa_assert(u
->mixer_elem
);
561 if ((err
= snd_mixer_selem_get_capture_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
562 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
574 static int source_set_mute_cb(pa_source
*s
) {
575 struct userdata
*u
= s
->userdata
;
579 pa_assert(u
->mixer_elem
);
581 if ((err
= snd_mixer_selem_set_capture_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
582 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
592 static void thread_func(void *userdata
) {
593 struct userdata
*u
= userdata
;
597 pa_log_debug("Thread starting up");
599 if (u
->core
->realtime_scheduling
)
600 pa_make_realtime(u
->core
->realtime_priority
);
602 pa_thread_mq_install(&u
->thread_mq
);
603 pa_rtpoll_install(u
->rtpoll
);
608 /* Read some data and pass it to the sources */
609 if (PA_SOURCE_OPENED(u
->source
->thread_info
.state
)) {
612 if (mmap_read(u
) < 0)
616 if (unix_read(u
) < 0)
621 /* Hmm, nothing to do. Let's sleep */
622 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
628 /* Tell ALSA about this and process its response */
629 if (PA_SOURCE_OPENED(u
->source
->thread_info
.state
)) {
630 struct pollfd
*pollfd
;
631 unsigned short revents
= 0;
635 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
637 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
638 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
642 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
644 if (revents
& POLLERR
)
645 pa_log_warn("Got POLLERR from ALSA");
646 if (revents
& POLLNVAL
)
647 pa_log_warn("Got POLLNVAL from ALSA");
648 if (revents
& POLLHUP
)
649 pa_log_warn("Got POLLHUP from ALSA");
651 /* Try to recover from this error */
653 switch (snd_pcm_state(u
->pcm_handle
)) {
655 case SND_PCM_STATE_XRUN
:
656 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
657 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
662 case SND_PCM_STATE_SUSPENDED
:
663 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
664 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
671 snd_pcm_drop(u
->pcm_handle
);
673 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
674 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
684 /* If this was no regular exit from the loop we have to continue
685 * processing messages until we received PA_MESSAGE_SHUTDOWN */
686 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
687 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
690 pa_log_debug("Thread shutting down");
693 int pa__init(pa_module
*m
) {
695 pa_modargs
*ma
= NULL
;
696 struct userdata
*u
= NULL
;
700 uint32_t nfrags
, frag_size
;
701 snd_pcm_uframes_t period_size
;
703 snd_pcm_info_t
*pcm_info
= NULL
;
707 char *name_buf
= NULL
;
709 pa_bool_t use_mmap
= TRUE
, b
;
711 snd_pcm_info_alloca(&pcm_info
);
715 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
716 pa_log("Failed to parse module arguments");
720 ss
= m
->core
->default_sample_spec
;
721 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
722 pa_log("Failed to parse sample specification");
726 frame_size
= pa_frame_size(&ss
);
728 nfrags
= m
->core
->default_n_fragments
;
729 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
731 frag_size
= frame_size
;
733 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0) {
734 pa_log("Failed to parse buffer metrics");
737 period_size
= frag_size
/frame_size
;
739 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
740 pa_log("Failed to parse mmap argument.");
744 u
= pa_xnew0(struct userdata
, 1);
748 u
->use_mmap
= use_mmap
;
749 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
750 u
->rtpoll
= pa_rtpoll_new();
751 u
->alsa_rtpoll_item
= NULL
;
752 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
754 snd_config_update_free_global();
756 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
758 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
762 SND_PCM_STREAM_CAPTURE
,
763 &nfrags
, &period_size
,
769 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
770 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
773 SND_PCM_STREAM_CAPTURE
,
774 &nfrags
, &period_size
,
779 pa_assert(u
->device_name
);
780 pa_log_info("Successfully opened device %s.", u
->device_name
);
782 if (use_mmap
&& !b
) {
783 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
784 u
->use_mmap
= use_mmap
= b
;
788 pa_log_info("Successfully enabled mmap() mode.");
790 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
791 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
795 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
796 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
800 /* ALSA might tweak the sample spec, so recalculate the frame size */
801 frame_size
= pa_frame_size(&ss
);
803 if (ss
.channels
!= map
.channels
)
804 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
805 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
807 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
808 pa_log("Error opening mixer: %s", snd_strerror(err
));
811 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) < 0) ||
812 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Capture", NULL
))) {
813 snd_mixer_close(u
->mixer_handle
);
814 u
->mixer_handle
= NULL
;
818 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
821 name
= name_buf
= pa_sprintf_malloc("alsa_input.%s", u
->device_name
);
825 u
->source
= pa_source_new(m
->core
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
829 pa_log("Failed to create source object");
833 u
->source
->parent
.process_msg
= source_process_msg
;
834 u
->source
->userdata
= u
;
836 pa_source_set_module(u
->source
, m
);
837 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
838 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
839 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc(
840 "ALSA PCM on %s (%s)%s",
842 snd_pcm_info_get_name(pcm_info
),
843 use_mmap
? " via DMA" : ""));
846 u
->source
->flags
= PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
|PA_SOURCE_HW_VOLUME_CTRL
;
848 u
->frame_size
= frame_size
;
849 u
->fragment_size
= frag_size
= period_size
* frame_size
;
850 u
->nfragments
= nfrags
;
851 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
853 pa_log_info("Using %u fragments of size %lu bytes.", nfrags
, (long unsigned) u
->fragment_size
);
855 if (u
->mixer_handle
) {
856 pa_assert(u
->mixer_elem
);
858 if (snd_mixer_selem_has_capture_volume(u
->mixer_elem
)) {
861 for (i
= 0;i
< ss
.channels
;i
++) {
862 if (!snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
))
866 if (i
== ss
.channels
) {
867 u
->source
->get_volume
= source_get_volume_cb
;
868 u
->source
->set_volume
= source_set_volume_cb
;
869 snd_mixer_selem_get_capture_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
873 if (snd_mixer_selem_has_capture_switch(u
->mixer_elem
)) {
874 u
->source
->get_mute
= source_get_mute_cb
;
875 u
->source
->set_mute
= source_set_mute_cb
;
878 u
->mixer_fdl
= pa_alsa_fdlist_new();
880 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
881 pa_log("failed to initialise file descriptor monitoring");
885 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
886 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
890 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
891 pa_log("Failed to create thread.");
894 /* Get initial mixer settings */
895 if (u
->source
->get_volume
)
896 u
->source
->get_volume(u
->source
);
897 if (u
->source
->get_mute
)
898 u
->source
->get_mute(u
->source
);
900 pa_source_put(u
->source
);
916 void pa__done(pa_module
*m
) {
921 if (!(u
= m
->userdata
))
925 pa_source_unlink(u
->source
);
928 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
929 pa_thread_free(u
->thread
);
932 pa_thread_mq_done(&u
->thread_mq
);
935 pa_source_unref(u
->source
);
937 if (u
->alsa_rtpoll_item
)
938 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
941 pa_rtpoll_free(u
->rtpoll
);
944 pa_alsa_fdlist_free(u
->mixer_fdl
);
947 snd_mixer_close(u
->mixer_handle
);
950 snd_pcm_drop(u
->pcm_handle
);
951 snd_pcm_close(u
->pcm_handle
);
954 pa_xfree(u
->device_name
);
957 snd_config_update_free_global();