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 #ifdef HAVE_SYS_POLL_H
37 #include <asoundlib.h>
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
42 #include <pulsecore/core.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/memchunk.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/sample-util.h>
49 #include <pulsecore/log.h>
50 #include <pulsecore/macro.h>
51 #include <pulsecore/thread.h>
52 #include <pulsecore/core-error.h>
53 #include <pulsecore/thread-mq.h>
54 #include <pulsecore/rtpoll.h>
56 #include "alsa-util.h"
57 #include "module-alsa-sink-symdef.h"
59 PA_MODULE_AUTHOR("Lennart Poettering")
60 PA_MODULE_DESCRIPTION("ALSA Sink")
61 PA_MODULE_VERSION(PACKAGE_VERSION
)
63 "sink_name=<name for the sink> "
64 "device=<ALSA device> "
65 "format=<sample format> "
66 "channels=<number of channels> "
68 "fragments=<number of fragments> "
69 "fragment_size=<fragment size> "
70 "channel_map=<channel map> "
71 "mmap=<enable memory mapping?>")
73 #define DEFAULT_DEVICE "default"
81 pa_thread_mq thread_mq
;
84 snd_pcm_t
*pcm_handle
;
86 pa_alsa_fdlist
*mixer_fdl
;
87 snd_mixer_t
*mixer_handle
;
88 snd_mixer_elem_t
*mixer_elem
;
89 long hw_volume_max
, hw_volume_min
;
91 size_t frame_size
, fragment_size
, hwbuf_size
;
101 pa_rtpoll_item
*alsa_rtpoll_item
;
104 static const char* const valid_modargs
[] = {
117 static int mmap_write(struct userdata
*u
) {
121 pa_sink_assert_ref(u
->sink
);
128 const snd_pcm_channel_area_t
*areas
;
129 snd_pcm_uframes_t offset
, frames
;
131 if ((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
134 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
138 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
144 pa_log("snd_pcm_avail_update: %s", snd_strerror(n
));
148 /* pa_log("Got request for %i samples", (int) n); */
155 if ((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0) {
158 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
162 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
168 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
172 /* Check these are multiples of 8 bit */
173 pa_assert((areas
[0].first
& 7) == 0);
174 pa_assert((areas
[0].step
& 7)== 0);
176 /* We assume a single interleaved memory buffer */
177 pa_assert((areas
[0].first
>> 3) == 0);
178 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
180 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
182 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
183 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
186 pa_sink_render_into_full(u
->sink
, &chunk
);
188 /* FIXME: Maybe we can do something to keep this memory block
189 * a little bit longer around? */
190 pa_memblock_unref_fixed(chunk
.memblock
);
192 if ((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0) {
195 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
199 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
205 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
211 if (frames
>= (snd_pcm_uframes_t
) n
)
214 /* pa_log("wrote %i samples", (int) frames); */
218 static int unix_write(struct userdata
*u
) {
219 snd_pcm_status_t
*status
;
222 snd_pcm_status_alloca(&status
);
225 pa_sink_assert_ref(u
->sink
);
233 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0) {
234 pa_log("Failed to query DSP status data: %s", snd_strerror(t
));
238 if (snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
)
239 pa_log_debug("Buffer underrun!");
241 l
= snd_pcm_status_get_avail(status
) * u
->frame_size
;
243 /* pa_log("%u bytes to write", l); */
248 if (u
->memchunk
.length
<= 0)
249 pa_sink_render(u
->sink
, l
, &u
->memchunk
);
251 pa_assert(u
->memchunk
.length
> 0);
253 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
254 t
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
);
255 pa_memblock_release(u
->memchunk
.memblock
);
257 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
263 if ((t
= snd_pcm_recover(u
->pcm_handle
, t
, 1)) == 0)
267 pa_log_debug("EAGAIN");
270 pa_log("Failed to write data to DSP: %s", snd_strerror(t
));
275 u
->memchunk
.index
+= t
* u
->frame_size
;
276 u
->memchunk
.length
-= t
* u
->frame_size
;
278 if (u
->memchunk
.length
<= 0) {
279 pa_memblock_unref(u
->memchunk
.memblock
);
280 pa_memchunk_reset(&u
->memchunk
);
285 if (t
* u
->frame_size
>= (unsigned) l
)
290 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
292 snd_pcm_status_t
*status
;
293 snd_pcm_sframes_t frames
= 0;
296 snd_pcm_status_alloca(&status
);
299 pa_assert(u
->pcm_handle
);
301 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)
302 pa_log("Failed to get delay: %s", snd_strerror(err
));
304 frames
= snd_pcm_status_get_delay(status
);
307 r
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
309 if (u
->memchunk
.memblock
)
310 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
315 static int build_pollfd(struct userdata
*u
) {
317 struct pollfd
*pollfd
;
321 pa_assert(u
->pcm_handle
);
323 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
324 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
328 if (u
->alsa_rtpoll_item
)
329 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
331 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, n
);
332 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
334 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
335 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
342 static int suspend(struct userdata
*u
) {
344 pa_assert(u
->pcm_handle
);
346 snd_pcm_drain(u
->pcm_handle
); /* Let's suspend */
347 snd_pcm_close(u
->pcm_handle
);
348 u
->pcm_handle
= NULL
;
350 if (u
->alsa_rtpoll_item
) {
351 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
352 u
->alsa_rtpoll_item
= NULL
;
355 pa_log_debug("Device suspended...");
360 static int unsuspend(struct userdata
*u
) {
364 snd_pcm_uframes_t period_size
;
367 pa_assert(!u
->pcm_handle
);
369 pa_log_debug("Trying resume...");
371 snd_config_update_free_global();
372 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
373 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
377 ss
= u
->sink
->sample_spec
;
378 nfrags
= u
->nfragments
;
379 period_size
= u
->fragment_size
/ u
->frame_size
;
382 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
383 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
387 if (b
!= u
->use_mmap
) {
388 pa_log_warn("Resume failed, couldn't get original access mode.");
392 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
393 pa_log_warn("Resume failed, couldn't restore original sample settings.");
397 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
398 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
402 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
403 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
407 if (build_pollfd(u
) < 0)
410 /* FIXME: We need to reload the volume somehow */
414 pa_log_debug("Resumed successfully...");
420 snd_pcm_close(u
->pcm_handle
);
421 u
->pcm_handle
= NULL
;
427 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
428 struct userdata
*u
= PA_SINK(o
)->userdata
;
432 case PA_SINK_MESSAGE_GET_LATENCY
: {
436 r
= sink_get_latency(u
);
438 *((pa_usec_t
*) data
) = r
;
443 case PA_SINK_MESSAGE_SET_STATE
:
445 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
447 case PA_SINK_SUSPENDED
:
448 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
456 case PA_SINK_RUNNING
:
458 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
459 if (unsuspend(u
) < 0)
465 case PA_SINK_DISCONNECTED
:
472 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
475 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
476 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
479 pa_assert(u
->mixer_handle
);
481 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
484 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
485 pa_sink_get_volume(u
->sink
);
486 pa_sink_get_mute(u
->sink
);
492 static int sink_get_volume_cb(pa_sink
*s
) {
493 struct userdata
*u
= s
->userdata
;
498 pa_assert(u
->mixer_elem
);
500 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
503 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
505 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
)) < 0)
508 set_vol
= (long) roundf(((float) s
->volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
510 /* Try to avoid superfluous volume changes */
512 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
));
518 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
520 s
->get_volume
= NULL
;
521 s
->set_volume
= NULL
;
525 static int sink_set_volume_cb(pa_sink
*s
) {
526 struct userdata
*u
= s
->userdata
;
531 pa_assert(u
->mixer_elem
);
533 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
537 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
539 vol
= s
->volume
.values
[i
];
541 if (vol
> PA_VOLUME_NORM
)
542 vol
= PA_VOLUME_NORM
;
544 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
546 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
553 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
555 s
->get_volume
= NULL
;
556 s
->set_volume
= NULL
;
560 static int sink_get_mute_cb(pa_sink
*s
) {
561 struct userdata
*u
= s
->userdata
;
565 pa_assert(u
->mixer_elem
);
567 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
568 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
580 static int sink_set_mute_cb(pa_sink
*s
) {
581 struct userdata
*u
= s
->userdata
;
585 pa_assert(u
->mixer_elem
);
587 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
588 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
598 static void thread_func(void *userdata
) {
599 struct userdata
*u
= userdata
;
603 pa_log_debug("Thread starting up");
605 if (u
->core
->high_priority
)
608 pa_thread_mq_install(&u
->thread_mq
);
609 pa_rtpoll_install(u
->rtpoll
);
611 if (build_pollfd(u
) < 0)
615 pa_msgobject
*object
;
621 /* pa_log("loop"); */
623 /* Render some data and write it to the dsp */
624 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
627 pa_assert(u
->pcm_handle
);
630 if ((work_done
= mmap_write(u
)) < 0)
633 if ((work_done
= unix_write(u
)) < 0)
637 if (work_done
&& u
->first
) {
638 pa_log_info("Starting playback.");
639 snd_pcm_start(u
->pcm_handle
);
645 /* pa_log("loop2"); */
647 /* Check whether there is a message for us to process */
648 if (pa_asyncmsgq_get(u
->thread_mq
.inq
, &object
, &code
, &data
, &offset
, &chunk
, 0) == 0) {
651 /* pa_log("processing msg"); */
653 if (!object
&& code
== PA_MESSAGE_SHUTDOWN
) {
654 pa_asyncmsgq_done(u
->thread_mq
.inq
, 0);
658 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, offset
, &chunk
);
659 pa_asyncmsgq_done(u
->thread_mq
.inq
, ret
);
663 /* Hmm, nothing to do. Let's sleep */
664 if (pa_rtpoll_run(u
->rtpoll
) < 0) {
665 pa_log("poll() failed: %s", pa_cstrerror(errno
));
669 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
670 struct pollfd
*pollfd
;
671 unsigned short revents
= 0;
675 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
677 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
678 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
682 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
683 if (revents
& POLLERR
)
684 pa_log_warn("Got POLLERR from ALSA");
685 if (revents
& POLLNVAL
)
686 pa_log_warn("Got POLLNVAL from ALSA");
687 if (revents
& POLLHUP
)
688 pa_log_warn("Got POLLHUP from ALSA");
692 /* pa_log("got alsa event"); */
698 /* We have to continue processing messages until we receive the
699 * SHUTDOWN message */
700 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
701 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
704 pa_log_debug("Thread shutting down");
707 int pa__init(pa_module
*m
) {
709 pa_modargs
*ma
= NULL
;
711 struct userdata
*u
= NULL
;
715 uint32_t nfrags
, frag_size
;
716 snd_pcm_uframes_t period_size
;
718 snd_pcm_info_t
*pcm_info
= NULL
;
722 char *name_buf
= NULL
;
728 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
729 pa_log("Failed to parse module arguments");
733 ss
= m
->core
->default_sample_spec
;
734 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
735 pa_log("Failed to parse sample specification and channel map");
739 frame_size
= pa_frame_size(&ss
);
741 nfrags
= m
->core
->default_n_fragments
;
742 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
744 frag_size
= frame_size
;
746 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0) {
747 pa_log("Failed to parse buffer metrics");
750 period_size
= frag_size
/frame_size
;
752 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
753 pa_log("Failed to parse mmap argument.");
757 u
= pa_xnew0(struct userdata
, 1);
761 u
->use_mmap
= use_mmap
;
763 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
764 u
->rtpoll
= pa_rtpoll_new();
765 u
->alsa_rtpoll_item
= NULL
;
766 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, u
->thread_mq
.inq
);
768 snd_config_update_free_global();
769 if ((err
= snd_pcm_open(&u
->pcm_handle
, dev
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
770 pa_log("Error opening PCM device %s: %s", dev
, snd_strerror(err
));
774 u
->device_name
= pa_xstrdup(dev
);
776 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
777 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
778 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
783 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
784 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
788 if (use_mmap
&& !b
) {
789 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
790 u
->use_mmap
= use_mmap
= b
;
794 pa_log_info("Successfully enabled mmap() mode.");
796 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
797 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
801 /* ALSA might tweak the sample spec, so recalculate the frame size */
802 frame_size
= pa_frame_size(&ss
);
804 if (ss
.channels
!= map
.channels
)
805 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
806 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
808 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
809 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
812 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
813 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM"))) {
815 snd_mixer_close(u
->mixer_handle
);
816 u
->mixer_handle
= NULL
;
820 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
823 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", dev
);
827 u
->sink
= pa_sink_new(m
->core
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
831 pa_log("Failed to create sink object");
835 u
->sink
->parent
.process_msg
= sink_process_msg
;
836 u
->sink
->userdata
= u
;
838 pa_sink_set_module(u
->sink
, m
);
839 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
840 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc(
841 "ALSA PCM on %s (%s)%s",
843 snd_pcm_info_get_name(pcm_info
),
844 use_mmap
? " via DMA" : ""));
847 u
->sink
->is_hardware
= 1;
849 u
->frame_size
= frame_size
;
850 u
->fragment_size
= frag_size
= period_size
* frame_size
;
851 u
->nfragments
= nfrags
;
852 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
854 pa_log_info("Using %u fragments of size %lu bytes.", nfrags
, (long unsigned) u
->fragment_size
);
856 pa_memchunk_reset(&u
->memchunk
);
858 if (u
->mixer_handle
) {
859 /* Initialize mixer code */
861 pa_assert(u
->mixer_elem
);
863 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
866 for (i
= 0;i
< ss
.channels
; i
++) {
867 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
871 if (i
== ss
.channels
) {
872 u
->sink
->get_volume
= sink_get_volume_cb
;
873 u
->sink
->set_volume
= sink_set_volume_cb
;
874 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
878 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
879 u
->sink
->get_mute
= sink_get_mute_cb
;
880 u
->sink
->set_mute
= sink_set_mute_cb
;
883 u
->mixer_fdl
= pa_alsa_fdlist_new();
885 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
886 pa_log("failed to initialise file descriptor monitoring");
890 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
891 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
895 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
896 pa_log("Failed to create thread.");
900 /* Get initial mixer settings */
901 if (u
->sink
->get_volume
)
902 u
->sink
->get_volume(u
->sink
);
903 if (u
->sink
->get_mute
)
904 u
->sink
->get_mute(u
->sink
);
914 snd_pcm_info_free(pcm_info
);
926 void pa__done(pa_module
*m
) {
931 if (!(u
= m
->userdata
))
935 pa_sink_disconnect(u
->sink
);
938 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
939 pa_thread_free(u
->thread
);
942 pa_thread_mq_done(&u
->thread_mq
);
945 pa_sink_unref(u
->sink
);
947 if (u
->memchunk
.memblock
)
948 pa_memblock_unref(u
->memchunk
.memblock
);
950 if (u
->alsa_rtpoll_item
)
951 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
954 pa_rtpoll_free(u
->rtpoll
);
957 pa_alsa_fdlist_free(u
->mixer_fdl
);
960 snd_mixer_close(u
->mixer_handle
);
963 snd_pcm_drop(u
->pcm_handle
);
964 snd_pcm_close(u
->pcm_handle
);
967 pa_xfree(u
->device_name
);
970 snd_config_update_free_global();