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>
55 #include "alsa-util.h"
56 #include "module-alsa-sink-symdef.h"
58 PA_MODULE_AUTHOR("Lennart Poettering")
59 PA_MODULE_DESCRIPTION("ALSA Sink")
60 PA_MODULE_VERSION(PACKAGE_VERSION
)
62 "sink_name=<name for the sink> "
63 "device=<ALSA device> "
64 "format=<sample format> "
65 "channels=<number of channels> "
67 "fragments=<number of fragments> "
68 "fragment_size=<fragment size> "
69 "channel_map=<channel map> "
70 "mmap=<enable memory mapping?>")
72 #define DEFAULT_DEVICE "default"
79 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
;
98 struct pollfd
*pollfd
;
107 static const char* const valid_modargs
[] = {
120 static int mmap_write(struct userdata
*u
) {
123 const snd_pcm_channel_area_t
*areas
;
124 snd_pcm_uframes_t offset
, frames
;
134 if ((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
137 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
141 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
147 pa_log("snd_pcm_avail_update: %s", snd_strerror(n
));
151 /* pa_log("Got request for %i samples", (int) n); */
158 if ((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0) {
161 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
165 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
171 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
175 /* Check these are multiples of 8 bit */
176 pa_assert((areas
[0].first
& 7) == 0);
177 pa_assert((areas
[0].step
& 7)== 0);
179 /* We assume a single interleaved memory buffer */
180 pa_assert((areas
[0].first
>> 3) == 0);
181 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
183 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
185 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
186 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
189 pa_sink_render_into_full(u
->sink
, &chunk
);
191 /* FIXME: Maybe we can do something to keep this memory block
192 * a little bit longer around? */
193 pa_memblock_unref_fixed(chunk
.memblock
);
195 if ((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0) {
198 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
202 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
208 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
214 /* pa_log("wrote %i samples", (int) frames); */
218 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
220 snd_pcm_status_t
*status
;
221 snd_pcm_sframes_t frames
= 0;
224 snd_pcm_status_alloca(&status
);
228 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)
229 pa_log("Failed to get delay: %s", snd_strerror(err
));
231 frames
= snd_pcm_status_get_delay(status
);
234 r
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
236 if (u
->memchunk
.memblock
)
237 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
242 static int build_pollfd(struct userdata
*u
) {
246 pa_assert(u
->pcm_handle
);
248 if ((u
->n_alsa_fds
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
249 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(u
->n_alsa_fds
));
254 u
->pollfd
= pa_xnew0(struct pollfd
, POLLFD_ALSA_BASE
+ u
->n_alsa_fds
);
256 u
->pollfd
[POLLFD_ASYNCQ
].fd
= pa_asyncmsgq_get_fd(u
->thread_mq
.inq
);
257 u
->pollfd
[POLLFD_ASYNCQ
].events
= POLLIN
;
259 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, u
->pollfd
+POLLFD_ALSA_BASE
, u
->n_alsa_fds
)) < 0) {
260 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
267 static int suspend(struct userdata
*u
) {
269 pa_assert(u
->pcm_handle
);
271 snd_pcm_drain(u
->pcm_handle
); /* Let's suspend */
272 snd_pcm_close(u
->pcm_handle
);
273 u
->pcm_handle
= NULL
;
275 pa_log_debug("Device suspended...");
280 static int unsuspend(struct userdata
*u
) {
284 snd_pcm_uframes_t period_size
;
287 pa_assert(!u
->pcm_handle
);
289 pa_log_debug("Trying resume...");
291 snd_config_update_free_global();
292 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
293 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
297 ss
= u
->sink
->sample_spec
;
298 nfrags
= u
->nfragments
;
299 period_size
= u
->fragment_size
/ u
->frame_size
;
302 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
303 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
307 if (b
!= u
->use_mmap
) {
308 pa_log_warn("Resume failed, couldn't get original access mode.");
312 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
313 pa_log_warn("Resume failed, couldn't restore original sample settings.");
317 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
318 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
322 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
323 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
327 if (build_pollfd(u
) < 0)
330 /* FIXME: We need to reload the volume somehow */
334 pa_log_debug("Resumed successfully...");
340 snd_pcm_close(u
->pcm_handle
);
341 u
->pcm_handle
= NULL
;
347 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
348 struct userdata
*u
= PA_SINK(o
)->userdata
;
352 case PA_SINK_MESSAGE_GET_LATENCY
: {
356 r
= sink_get_latency(u
);
358 *((pa_usec_t
*) data
) = r
;
363 case PA_SINK_MESSAGE_SET_STATE
:
365 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
367 case PA_SINK_SUSPENDED
:
368 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
376 case PA_SINK_RUNNING
:
378 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
379 if (unsuspend(u
) < 0)
385 case PA_SINK_DISCONNECTED
:
392 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
395 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
396 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
399 pa_assert(u
->mixer_handle
);
401 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
404 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
405 pa_sink_get_volume(u
->sink
);
406 pa_sink_get_mute(u
->sink
);
412 static int sink_get_volume_cb(pa_sink
*s
) {
413 struct userdata
*u
= s
->userdata
;
418 pa_assert(u
->mixer_elem
);
420 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
423 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
425 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
)) < 0)
428 set_vol
= (long) roundf(((float) s
->volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
430 /* Try to avoid superfluous volume changes */
432 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
));
438 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
440 s
->get_volume
= NULL
;
441 s
->set_volume
= NULL
;
445 static int sink_set_volume_cb(pa_sink
*s
) {
446 struct userdata
*u
= s
->userdata
;
451 pa_assert(u
->mixer_elem
);
453 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
457 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
459 vol
= s
->volume
.values
[i
];
461 if (vol
> PA_VOLUME_NORM
)
462 vol
= PA_VOLUME_NORM
;
464 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
466 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
473 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
475 s
->get_volume
= NULL
;
476 s
->set_volume
= NULL
;
480 static int sink_get_mute_cb(pa_sink
*s
) {
481 struct userdata
*u
= s
->userdata
;
485 pa_assert(u
->mixer_elem
);
487 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
488 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
500 static int sink_set_mute_cb(pa_sink
*s
) {
501 struct userdata
*u
= s
->userdata
;
505 pa_assert(u
->mixer_elem
);
507 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
508 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
518 static void thread_func(void *userdata
) {
520 struct userdata
*u
= userdata
;
521 unsigned short revents
= 0;
522 snd_pcm_status_t
*status
;
526 snd_pcm_status_alloca(&status
);
528 pa_log_debug("Thread starting up");
530 if (u
->core
->high_priority
)
533 pa_thread_mq_install(&u
->thread_mq
);
535 if (build_pollfd(u
) < 0)
539 pa_msgobject
*object
;
546 /* pa_log("loop"); */
548 /* Check whether there is a message for us to process */
549 if (pa_asyncmsgq_get(u
->thread_mq
.inq
, &object
, &code
, &data
, &offset
, &chunk
, 0) == 0) {
552 /* pa_log("processing msg"); */
554 if (!object
&& code
== PA_MESSAGE_SHUTDOWN
) {
555 pa_asyncmsgq_done(u
->thread_mq
.inq
, 0);
559 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, offset
, &chunk
);
560 pa_asyncmsgq_done(u
->thread_mq
.inq
, ret
);
564 /* pa_log("loop2"); */
566 /* Render some data and write it to the dsp */
568 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
) && ((revents
& POLLOUT
) || u
->first
== 1)) {
570 pa_assert(u
->pcm_handle
);
574 if ((work_done
= mmap_write(u
)) < 0)
584 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0) {
585 pa_log("Failed to query DSP status data: %s", snd_strerror(t
));
589 if (snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
)
590 pa_log_debug("Buffer underrun!");
592 l
= snd_pcm_status_get_avail(status
) * u
->frame_size
;
594 /* pa_log("%u bytes to write", l); */
599 if (u
->memchunk
.length
<= 0)
600 pa_sink_render(u
->sink
, l
, &u
->memchunk
);
602 pa_assert(u
->memchunk
.length
> 0);
604 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
605 t
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
);
606 pa_memblock_release(u
->memchunk
.memblock
);
608 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
614 if ((t
= snd_pcm_recover(u
->pcm_handle
, t
, 1)) == 0)
618 pa_log_debug("EAGAIN");
621 pa_log("Failed to write data to DSP: %s", snd_strerror(t
));
626 u
->memchunk
.index
+= t
* u
->frame_size
;
627 u
->memchunk
.length
-= t
* u
->frame_size
;
629 if (u
->memchunk
.length
<= 0) {
630 pa_memblock_unref(u
->memchunk
.memblock
);
631 pa_memchunk_reset(&u
->memchunk
);
636 if (t
* u
->frame_size
>= (unsigned) l
)
646 pa_log_info("Starting playback.");
647 snd_pcm_start(u
->pcm_handle
);
655 /* Hmm, nothing to do. Let's sleep */
656 if (pa_asyncmsgq_before_poll(u
->thread_mq
.inq
) < 0)
659 /* pa_log("polling for %i", POLLFD_ALSA_BASE + (PA_SINK_OPENED(u->sink->thread_info.state) ? u->n_alsa_fds : 0)); */
660 r
= poll(u
->pollfd
, POLLFD_ALSA_BASE
+ (PA_SINK_OPENED(u
->sink
->thread_info
.state
) ? u
->n_alsa_fds
: 0), -1);
661 /* pa_log("poll end"); */
663 pa_asyncmsgq_after_poll(u
->thread_mq
.inq
);
666 if (errno
== EINTR
) {
667 u
->pollfd
[POLLFD_ASYNCQ
].revents
= 0;
672 pa_log("poll() failed: %s", pa_cstrerror(errno
));
678 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
679 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, u
->pollfd
+ POLLFD_ALSA_BASE
, u
->n_alsa_fds
, &revents
)) < 0) {
680 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
684 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
685 if (revents
& POLLERR
)
686 pa_log_warn("Got POLLERR from ALSA");
687 if (revents
& POLLNVAL
)
688 pa_log_warn("Got POLLNVAL from ALSA");
689 if (revents
& POLLHUP
)
690 pa_log_warn("Got POLLHUP from ALSA");
694 /* pa_log("got alsa event"); */
698 pa_assert((u
->pollfd
[POLLFD_ASYNCQ
].revents
& ~POLLIN
) == 0);
702 /* We have to continue processing messages until we receive the
703 * SHUTDOWN message */
704 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
705 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
708 pa_log_debug("Thread shutting down");
711 int pa__init(pa_module
*m
) {
713 pa_modargs
*ma
= NULL
;
715 struct userdata
*u
= NULL
;
719 uint32_t nfrags
, frag_size
;
720 snd_pcm_uframes_t period_size
;
722 snd_pcm_info_t
*pcm_info
= NULL
;
726 char *name_buf
= NULL
;
732 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
733 pa_log("Failed to parse module arguments");
737 ss
= m
->core
->default_sample_spec
;
738 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
739 pa_log("Failed to parse sample specification and channel map");
743 frame_size
= pa_frame_size(&ss
);
745 nfrags
= m
->core
->default_n_fragments
;
746 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
748 frag_size
= frame_size
;
750 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0) {
751 pa_log("Failed to parse buffer metrics");
754 period_size
= frag_size
/frame_size
;
756 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
757 pa_log("Failed to parse mmap argument.");
761 u
= pa_xnew0(struct userdata
, 1);
765 u
->use_mmap
= use_mmap
;
769 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
771 snd_config_update_free_global();
772 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) {
773 pa_log("Error opening PCM device %s: %s", dev
, snd_strerror(err
));
777 u
->device_name
= pa_xstrdup(dev
);
779 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
780 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
781 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
786 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
787 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
791 if (use_mmap
&& !b
) {
792 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
793 u
->use_mmap
= use_mmap
= b
;
797 pa_log_info("Successfully enabled mmap() mode.");
799 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
800 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
804 /* ALSA might tweak the sample spec, so recalculate the frame size */
805 frame_size
= pa_frame_size(&ss
);
807 if (ss
.channels
!= map
.channels
)
808 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
809 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
811 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
812 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
815 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
816 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM"))) {
818 snd_mixer_close(u
->mixer_handle
);
819 u
->mixer_handle
= NULL
;
823 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
826 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", dev
);
830 u
->sink
= pa_sink_new(m
->core
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
834 pa_log("Failed to create sink object");
838 u
->sink
->parent
.process_msg
= sink_process_msg
;
839 u
->sink
->userdata
= u
;
841 pa_sink_set_module(u
->sink
, m
);
842 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
843 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc(
844 "ALSA PCM on %s (%s)%s",
846 snd_pcm_info_get_name(pcm_info
),
847 use_mmap
? " via DMA" : ""));
850 u
->sink
->is_hardware
= 1;
852 u
->frame_size
= frame_size
;
853 u
->fragment_size
= frag_size
= period_size
* frame_size
;
854 u
->nfragments
= nfrags
;
855 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
857 pa_log_info("Using %u fragments of size %lu bytes.", nfrags
, (long unsigned) u
->fragment_size
);
859 pa_memchunk_reset(&u
->memchunk
);
861 if (u
->mixer_handle
) {
862 /* Initialize mixer code */
864 pa_assert(u
->mixer_elem
);
866 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
869 for (i
= 0;i
< ss
.channels
; i
++) {
870 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
874 if (i
== ss
.channels
) {
875 u
->sink
->get_volume
= sink_get_volume_cb
;
876 u
->sink
->set_volume
= sink_set_volume_cb
;
877 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
881 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
882 u
->sink
->get_mute
= sink_get_mute_cb
;
883 u
->sink
->set_mute
= sink_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");
893 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
894 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
898 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
899 pa_log("Failed to create thread.");
903 /* Get initial mixer settings */
904 if (u
->sink
->get_volume
)
905 u
->sink
->get_volume(u
->sink
);
906 if (u
->sink
->get_mute
)
907 u
->sink
->get_mute(u
->sink
);
917 snd_pcm_info_free(pcm_info
);
929 void pa__done(pa_module
*m
) {
934 if (!(u
= m
->userdata
))
938 pa_sink_disconnect(u
->sink
);
941 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
942 pa_thread_free(u
->thread
);
945 pa_thread_mq_done(&u
->thread_mq
);
948 pa_sink_unref(u
->sink
);
950 if (u
->memchunk
.memblock
)
951 pa_memblock_unref(u
->memchunk
.memblock
);
954 pa_alsa_fdlist_free(u
->mixer_fdl
);
957 snd_mixer_close(u
->mixer_handle
);
960 snd_pcm_drop(u
->pcm_handle
);
961 snd_pcm_close(u
->pcm_handle
);
965 pa_xfree(u
->device_name
);
968 snd_config_update_free_global();