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.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/memchunk.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/sample-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
50 #include "alsa-util.h"
51 #include "module-alsa-sink-symdef.h"
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("ALSA Sink");
55 PA_MODULE_VERSION(PACKAGE_VERSION
);
56 PA_MODULE_LOAD_ONCE(FALSE
);
58 "sink_name=<name for the sink> "
59 "device=<ALSA device> "
60 "device_id=<ALSA device id> "
61 "format=<sample format> "
62 "channels=<number of channels> "
64 "fragments=<number of fragments> "
65 "fragment_size=<fragment size> "
66 "channel_map=<channel map> "
67 "mmap=<enable memory mapping?>");
69 #define DEFAULT_DEVICE "default"
77 pa_thread_mq thread_mq
;
80 snd_pcm_t
*pcm_handle
;
82 pa_alsa_fdlist
*mixer_fdl
;
83 snd_mixer_t
*mixer_handle
;
84 snd_mixer_elem_t
*mixer_elem
;
85 long hw_volume_max
, hw_volume_min
;
87 size_t frame_size
, fragment_size
, hwbuf_size
;
97 pa_rtpoll_item
*alsa_rtpoll_item
;
100 static const char* const valid_modargs
[] = {
114 static int mmap_write(struct userdata
*u
) {
118 pa_sink_assert_ref(u
->sink
);
125 const snd_pcm_channel_area_t
*areas
;
126 snd_pcm_uframes_t offset
, frames
;
128 if ((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0) {
131 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
135 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
141 pa_log("snd_pcm_avail_update: %s", snd_strerror(err
));
145 /* pa_log("Got request for %i samples", (int) n); */
152 if ((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0) {
155 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
159 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
165 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
169 /* Check these are multiples of 8 bit */
170 pa_assert((areas
[0].first
& 7) == 0);
171 pa_assert((areas
[0].step
& 7)== 0);
173 /* We assume a single interleaved memory buffer */
174 pa_assert((areas
[0].first
>> 3) == 0);
175 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
177 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
179 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
180 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
183 pa_sink_render_into_full(u
->sink
, &chunk
);
185 /* FIXME: Maybe we can do something to keep this memory block
186 * a little bit longer around? */
187 pa_memblock_unref_fixed(chunk
.memblock
);
189 if ((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0) {
192 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
196 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
202 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
208 if (frames
>= (snd_pcm_uframes_t
) n
)
211 /* pa_log("wrote %i samples", (int) frames); */
215 static int unix_write(struct userdata
*u
) {
216 snd_pcm_status_t
*status
;
219 snd_pcm_status_alloca(&status
);
222 pa_sink_assert_ref(u
->sink
);
230 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0) {
231 pa_log("Failed to query DSP status data: %s", snd_strerror(err
));
235 if (snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
)
236 pa_log_debug("Buffer underrun!");
238 l
= snd_pcm_status_get_avail(status
) * u
->frame_size
;
240 /* pa_log("%u bytes to write", l); */
245 if (u
->memchunk
.length
<= 0)
246 pa_sink_render(u
->sink
, l
, &u
->memchunk
);
248 pa_assert(u
->memchunk
.length
> 0);
250 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
251 t
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
);
252 pa_memblock_release(u
->memchunk
.memblock
);
254 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
260 if ((t
= snd_pcm_recover(u
->pcm_handle
, t
, 1)) == 0)
264 pa_log_debug("EAGAIN");
267 pa_log("Failed to write data to DSP: %s", snd_strerror(t
));
272 u
->memchunk
.index
+= t
* u
->frame_size
;
273 u
->memchunk
.length
-= t
* u
->frame_size
;
275 if (u
->memchunk
.length
<= 0) {
276 pa_memblock_unref(u
->memchunk
.memblock
);
277 pa_memchunk_reset(&u
->memchunk
);
282 if (t
* u
->frame_size
>= (unsigned) l
)
287 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
289 snd_pcm_status_t
*status
;
290 snd_pcm_sframes_t frames
= 0;
293 snd_pcm_status_alloca(&status
);
296 pa_assert(u
->pcm_handle
);
298 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)
299 pa_log("Failed to get delay: %s", snd_strerror(err
));
301 frames
= snd_pcm_status_get_delay(status
);
304 r
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
306 if (u
->memchunk
.memblock
)
307 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
312 static int build_pollfd(struct userdata
*u
) {
314 struct pollfd
*pollfd
;
318 pa_assert(u
->pcm_handle
);
320 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
321 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
325 if (u
->alsa_rtpoll_item
)
326 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
328 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, n
);
329 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
331 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
332 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
339 static int suspend(struct userdata
*u
) {
341 pa_assert(u
->pcm_handle
);
344 snd_pcm_drain(u
->pcm_handle
);
345 snd_pcm_close(u
->pcm_handle
);
346 u
->pcm_handle
= NULL
;
348 if (u
->alsa_rtpoll_item
) {
349 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
350 u
->alsa_rtpoll_item
= NULL
;
353 pa_log_info("Device suspended...");
358 static int unsuspend(struct userdata
*u
) {
363 snd_pcm_uframes_t period_size
;
366 pa_assert(!u
->pcm_handle
);
368 pa_log_info("Trying resume...");
370 snd_config_update_free_global();
371 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
372 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
376 ss
= u
->sink
->sample_spec
;
377 nfrags
= u
->nfragments
;
378 period_size
= u
->fragment_size
/ u
->frame_size
;
381 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
, TRUE
)) < 0) {
382 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
386 if (b
!= u
->use_mmap
) {
387 pa_log_warn("Resume failed, couldn't get original access mode.");
391 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
392 pa_log_warn("Resume failed, couldn't restore original sample settings.");
396 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
397 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
401 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
402 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
406 if (build_pollfd(u
) < 0)
409 /* FIXME: We need to reload the volume somehow */
413 pa_log_info("Resumed successfully...");
419 snd_pcm_close(u
->pcm_handle
);
420 u
->pcm_handle
= NULL
;
426 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
427 struct userdata
*u
= PA_SINK(o
)->userdata
;
431 case PA_SINK_MESSAGE_GET_LATENCY
: {
435 r
= sink_get_latency(u
);
437 *((pa_usec_t
*) data
) = r
;
442 case PA_SINK_MESSAGE_SET_STATE
:
444 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
446 case PA_SINK_SUSPENDED
:
447 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
455 case PA_SINK_RUNNING
:
457 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
458 if (build_pollfd(u
) < 0)
462 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
463 if (unsuspend(u
) < 0)
469 case PA_SINK_UNLINKED
:
477 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
480 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
481 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
484 pa_assert(u
->mixer_handle
);
486 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
489 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
490 pa_sink_get_volume(u
->sink
);
491 pa_sink_get_mute(u
->sink
);
497 static int sink_get_volume_cb(pa_sink
*s
) {
498 struct userdata
*u
= s
->userdata
;
503 pa_assert(u
->mixer_elem
);
505 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
508 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
510 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
)) < 0)
513 set_vol
= (long) roundf(((float) s
->volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
515 /* Try to avoid superfluous volume changes */
517 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
));
523 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
525 s
->get_volume
= NULL
;
526 s
->set_volume
= NULL
;
530 static int sink_set_volume_cb(pa_sink
*s
) {
531 struct userdata
*u
= s
->userdata
;
536 pa_assert(u
->mixer_elem
);
538 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
542 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
544 vol
= s
->volume
.values
[i
];
546 if (vol
> PA_VOLUME_NORM
)
547 vol
= PA_VOLUME_NORM
;
549 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
551 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
558 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
560 s
->get_volume
= NULL
;
561 s
->set_volume
= NULL
;
565 static int sink_get_mute_cb(pa_sink
*s
) {
566 struct userdata
*u
= s
->userdata
;
570 pa_assert(u
->mixer_elem
);
572 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
573 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
585 static int sink_set_mute_cb(pa_sink
*s
) {
586 struct userdata
*u
= s
->userdata
;
590 pa_assert(u
->mixer_elem
);
592 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
593 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
603 static void thread_func(void *userdata
) {
604 struct userdata
*u
= userdata
;
608 pa_log_debug("Thread starting up");
610 if (u
->core
->realtime_scheduling
)
611 pa_make_realtime(u
->core
->realtime_priority
);
613 pa_thread_mq_install(&u
->thread_mq
);
614 pa_rtpoll_install(u
->rtpoll
);
619 /* Render some data and write it to the dsp */
620 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
624 if ((work_done
= mmap_write(u
)) < 0)
627 if ((work_done
= unix_write(u
)) < 0)
631 if (work_done
&& u
->first
) {
632 pa_log_info("Starting playback.");
633 snd_pcm_start(u
->pcm_handle
);
639 /* Hmm, nothing to do. Let's sleep */
640 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
646 /* Tell ALSA about this and process its response */
647 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
648 struct pollfd
*pollfd
;
649 unsigned short revents
= 0;
653 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
655 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
656 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
660 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
662 if (revents
& POLLERR
)
663 pa_log_warn("Got POLLERR from ALSA");
664 if (revents
& POLLNVAL
)
665 pa_log_warn("Got POLLNVAL from ALSA");
666 if (revents
& POLLHUP
)
667 pa_log_warn("Got POLLHUP from ALSA");
669 /* Try to recover from this error */
671 switch (snd_pcm_state(u
->pcm_handle
)) {
673 case SND_PCM_STATE_XRUN
:
674 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
675 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
680 case SND_PCM_STATE_SUSPENDED
:
681 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
682 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
689 snd_pcm_drop(u
->pcm_handle
);
691 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
692 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
702 /* If this was no regular exit from the loop we have to continue
703 * processing messages until we received PA_MESSAGE_SHUTDOWN */
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
;
714 struct userdata
*u
= NULL
;
718 uint32_t nfrags
, frag_size
;
719 snd_pcm_uframes_t period_size
;
721 snd_pcm_info_t
*pcm_info
= NULL
;
725 char *name_buf
= NULL
;
727 pa_bool_t use_mmap
= TRUE
, b
;
729 snd_pcm_info_alloca(&pcm_info
);
733 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
734 pa_log("Failed to parse module arguments");
738 ss
= m
->core
->default_sample_spec
;
739 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
740 pa_log("Failed to parse sample specification and channel map");
744 frame_size
= pa_frame_size(&ss
);
746 nfrags
= m
->core
->default_n_fragments
;
747 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
749 frag_size
= frame_size
;
751 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0) {
752 pa_log("Failed to parse buffer metrics");
755 period_size
= frag_size
/frame_size
;
757 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
758 pa_log("Failed to parse mmap argument.");
762 u
= pa_xnew0(struct userdata
, 1);
766 u
->use_mmap
= use_mmap
;
768 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
769 u
->rtpoll
= pa_rtpoll_new();
770 u
->alsa_rtpoll_item
= NULL
;
771 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
773 snd_config_update_free_global();
777 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
779 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
783 SND_PCM_STREAM_PLAYBACK
,
784 &nfrags
, &period_size
,
791 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
792 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
795 SND_PCM_STREAM_PLAYBACK
,
796 &nfrags
, &period_size
,
802 pa_assert(u
->device_name
);
803 pa_log_info("Successfully opened device %s.", u
->device_name
);
805 if (use_mmap
&& !b
) {
806 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
807 u
->use_mmap
= use_mmap
= b
;
811 pa_log_info("Successfully enabled mmap() mode.");
813 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
814 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
818 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
819 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
823 /* ALSA might tweak the sample spec, so recalculate the frame size */
824 frame_size
= pa_frame_size(&ss
);
826 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
827 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
830 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) < 0) ||
831 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM"))) {
833 snd_mixer_close(u
->mixer_handle
);
834 u
->mixer_handle
= NULL
;
838 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
841 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", u
->device_name
);
845 u
->sink
= pa_sink_new(m
->core
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
849 pa_log("Failed to create sink object");
853 u
->sink
->parent
.process_msg
= sink_process_msg
;
854 u
->sink
->userdata
= u
;
856 pa_sink_set_module(u
->sink
, m
);
857 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
858 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
859 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc(
860 "ALSA PCM on %s (%s)%s",
862 snd_pcm_info_get_name(pcm_info
),
863 use_mmap
? " via DMA" : ""));
866 u
->sink
->flags
= PA_SINK_HARDWARE
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_LATENCY
;
868 u
->frame_size
= frame_size
;
869 u
->fragment_size
= frag_size
= period_size
* frame_size
;
870 u
->nfragments
= nfrags
;
871 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
873 pa_log_info("Using %u fragments of size %lu bytes.", nfrags
, (long unsigned) u
->fragment_size
);
875 pa_memchunk_reset(&u
->memchunk
);
877 if (u
->mixer_handle
) {
878 /* Initialize mixer code */
880 pa_assert(u
->mixer_elem
);
882 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
885 for (i
= 0; i
< ss
.channels
; i
++)
886 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
889 if (i
== ss
.channels
) {
890 pa_log_debug("ALSA device has separate volumes controls for all %u channels.", ss
.channels
);
891 u
->sink
->get_volume
= sink_get_volume_cb
;
892 u
->sink
->set_volume
= sink_set_volume_cb
;
893 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
895 pa_log_info("ALSA device lacks separate volumes controls for all %u channels (%u available), falling back to software volume control.", ss
.channels
, i
+1);
898 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
899 u
->sink
->get_mute
= sink_get_mute_cb
;
900 u
->sink
->set_mute
= sink_set_mute_cb
;
903 u
->mixer_fdl
= pa_alsa_fdlist_new();
905 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
906 pa_log("failed to initialise file descriptor monitoring");
910 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
911 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
915 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
916 pa_log("Failed to create thread.");
920 /* Get initial mixer settings */
921 if (u
->sink
->get_volume
)
922 u
->sink
->get_volume(u
->sink
);
923 if (u
->sink
->get_mute
)
924 u
->sink
->get_mute(u
->sink
);
926 pa_sink_put(u
->sink
);
942 void pa__done(pa_module
*m
) {
947 if (!(u
= m
->userdata
))
951 pa_sink_unlink(u
->sink
);
954 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
955 pa_thread_free(u
->thread
);
958 pa_thread_mq_done(&u
->thread_mq
);
961 pa_sink_unref(u
->sink
);
963 if (u
->memchunk
.memblock
)
964 pa_memblock_unref(u
->memchunk
.memblock
);
966 if (u
->alsa_rtpoll_item
)
967 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
970 pa_rtpoll_free(u
->rtpoll
);
973 pa_alsa_fdlist_free(u
->mixer_fdl
);
976 snd_mixer_close(u
->mixer_handle
);
979 snd_pcm_drop(u
->pcm_handle
);
980 snd_pcm_close(u
->pcm_handle
);
983 pa_xfree(u
->device_name
);
986 snd_config_update_free_global();