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 "format=<sample format> "
61 "channels=<number of channels> "
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"
76 pa_thread_mq thread_mq
;
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
;
96 pa_rtpoll_item
*alsa_rtpoll_item
;
99 static const char* const valid_modargs
[] = {
112 static int mmap_write(struct userdata
*u
) {
116 pa_sink_assert_ref(u
->sink
);
123 const snd_pcm_channel_area_t
*areas
;
124 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!");
133 if ((err
= snd_pcm_recover(u
->pcm_handle
, n
, 1)) == 0)
139 pa_log("snd_pcm_avail_update: %s", snd_strerror(err
));
143 /* pa_log("Got request for %i samples", (int) n); */
150 if ((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0) {
153 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
157 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
163 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
167 /* Check these are multiples of 8 bit */
168 pa_assert((areas
[0].first
& 7) == 0);
169 pa_assert((areas
[0].step
& 7)== 0);
171 /* We assume a single interleaved memory buffer */
172 pa_assert((areas
[0].first
>> 3) == 0);
173 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
175 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
177 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, 1);
178 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
181 pa_sink_render_into_full(u
->sink
, &chunk
);
183 /* FIXME: Maybe we can do something to keep this memory block
184 * a little bit longer around? */
185 pa_memblock_unref_fixed(chunk
.memblock
);
187 if ((err
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0) {
190 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
194 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0)
200 pa_log("Failed to write data to DSP: %s", snd_strerror(err
));
206 if (frames
>= (snd_pcm_uframes_t
) n
)
209 /* pa_log("wrote %i samples", (int) frames); */
213 static int unix_write(struct userdata
*u
) {
214 snd_pcm_status_t
*status
;
217 snd_pcm_status_alloca(&status
);
220 pa_sink_assert_ref(u
->sink
);
228 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0) {
229 pa_log("Failed to query DSP status data: %s", snd_strerror(err
));
233 if (snd_pcm_status_get_avail_max(status
)*u
->frame_size
>= u
->hwbuf_size
)
234 pa_log_debug("Buffer underrun!");
236 l
= snd_pcm_status_get_avail(status
) * u
->frame_size
;
238 /* pa_log("%u bytes to write", l); */
243 if (u
->memchunk
.length
<= 0)
244 pa_sink_render(u
->sink
, l
, &u
->memchunk
);
246 pa_assert(u
->memchunk
.length
> 0);
248 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
249 t
= snd_pcm_writei(u
->pcm_handle
, (const uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
);
250 pa_memblock_release(u
->memchunk
.memblock
);
252 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
258 if ((t
= snd_pcm_recover(u
->pcm_handle
, t
, 1)) == 0)
262 pa_log_debug("EAGAIN");
265 pa_log("Failed to write data to DSP: %s", snd_strerror(t
));
270 u
->memchunk
.index
+= t
* u
->frame_size
;
271 u
->memchunk
.length
-= t
* u
->frame_size
;
273 if (u
->memchunk
.length
<= 0) {
274 pa_memblock_unref(u
->memchunk
.memblock
);
275 pa_memchunk_reset(&u
->memchunk
);
280 if (t
* u
->frame_size
>= (unsigned) l
)
285 static pa_usec_t
sink_get_latency(struct userdata
*u
) {
287 snd_pcm_status_t
*status
;
288 snd_pcm_sframes_t frames
= 0;
291 snd_pcm_status_alloca(&status
);
294 pa_assert(u
->pcm_handle
);
296 if ((err
= snd_pcm_status(u
->pcm_handle
, status
)) < 0)
297 pa_log("Failed to get delay: %s", snd_strerror(err
));
299 frames
= snd_pcm_status_get_delay(status
);
302 r
= pa_bytes_to_usec(frames
* u
->frame_size
, &u
->sink
->sample_spec
);
304 if (u
->memchunk
.memblock
)
305 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
310 static int build_pollfd(struct userdata
*u
) {
312 struct pollfd
*pollfd
;
316 pa_assert(u
->pcm_handle
);
318 if ((n
= snd_pcm_poll_descriptors_count(u
->pcm_handle
)) < 0) {
319 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
323 if (u
->alsa_rtpoll_item
)
324 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
326 u
->alsa_rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, n
);
327 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, NULL
);
329 if ((err
= snd_pcm_poll_descriptors(u
->pcm_handle
, pollfd
, n
)) < 0) {
330 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
337 static int suspend(struct userdata
*u
) {
339 pa_assert(u
->pcm_handle
);
342 snd_pcm_drain(u
->pcm_handle
);
343 snd_pcm_close(u
->pcm_handle
);
344 u
->pcm_handle
= NULL
;
346 if (u
->alsa_rtpoll_item
) {
347 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
348 u
->alsa_rtpoll_item
= NULL
;
351 pa_log_info("Device suspended...");
356 static int unsuspend(struct userdata
*u
) {
360 snd_pcm_uframes_t period_size
;
363 pa_assert(!u
->pcm_handle
);
365 pa_log_info("Trying resume...");
367 snd_config_update_free_global();
368 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
369 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
373 ss
= u
->sink
->sample_spec
;
374 nfrags
= u
->nfragments
;
375 period_size
= u
->fragment_size
/ u
->frame_size
;
378 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
379 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
383 if (b
!= u
->use_mmap
) {
384 pa_log_warn("Resume failed, couldn't get original access mode.");
388 if (!pa_sample_spec_equal(&ss
, &u
->sink
->sample_spec
)) {
389 pa_log_warn("Resume failed, couldn't restore original sample settings.");
393 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
394 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
398 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
399 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
403 if (build_pollfd(u
) < 0)
406 /* FIXME: We need to reload the volume somehow */
410 pa_log_info("Resumed successfully...");
416 snd_pcm_close(u
->pcm_handle
);
417 u
->pcm_handle
= NULL
;
423 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
424 struct userdata
*u
= PA_SINK(o
)->userdata
;
428 case PA_SINK_MESSAGE_GET_LATENCY
: {
432 r
= sink_get_latency(u
);
434 *((pa_usec_t
*) data
) = r
;
439 case PA_SINK_MESSAGE_SET_STATE
:
441 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
443 case PA_SINK_SUSPENDED
:
444 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
452 case PA_SINK_RUNNING
:
454 if (u
->sink
->thread_info
.state
== PA_SINK_INIT
) {
455 if (build_pollfd(u
) < 0)
459 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
) {
460 if (unsuspend(u
) < 0)
466 case PA_SINK_UNLINKED
:
474 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
477 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
478 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
481 pa_assert(u
->mixer_handle
);
483 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
486 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
487 pa_sink_get_volume(u
->sink
);
488 pa_sink_get_mute(u
->sink
);
494 static int sink_get_volume_cb(pa_sink
*s
) {
495 struct userdata
*u
= s
->userdata
;
500 pa_assert(u
->mixer_elem
);
502 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
505 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
507 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
)) < 0)
510 set_vol
= (long) roundf(((float) s
->volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
512 /* Try to avoid superfluous volume changes */
514 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
));
520 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
522 s
->get_volume
= NULL
;
523 s
->set_volume
= NULL
;
527 static int sink_set_volume_cb(pa_sink
*s
) {
528 struct userdata
*u
= s
->userdata
;
533 pa_assert(u
->mixer_elem
);
535 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
539 pa_assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
541 vol
= s
->volume
.values
[i
];
543 if (vol
> PA_VOLUME_NORM
)
544 vol
= PA_VOLUME_NORM
;
546 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
548 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
555 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
557 s
->get_volume
= NULL
;
558 s
->set_volume
= NULL
;
562 static int sink_get_mute_cb(pa_sink
*s
) {
563 struct userdata
*u
= s
->userdata
;
567 pa_assert(u
->mixer_elem
);
569 if ((err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
570 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
582 static int sink_set_mute_cb(pa_sink
*s
) {
583 struct userdata
*u
= s
->userdata
;
587 pa_assert(u
->mixer_elem
);
589 if ((err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
590 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
600 static void thread_func(void *userdata
) {
601 struct userdata
*u
= userdata
;
605 pa_log_debug("Thread starting up");
607 if (u
->core
->realtime_scheduling
)
608 pa_make_realtime(u
->core
->realtime_priority
);
610 pa_thread_mq_install(&u
->thread_mq
);
611 pa_rtpoll_install(u
->rtpoll
);
616 /* Render some data and write it to the dsp */
617 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
621 if ((work_done
= mmap_write(u
)) < 0)
624 if ((work_done
= unix_write(u
)) < 0)
628 if (work_done
&& u
->first
) {
629 pa_log_info("Starting playback.");
630 snd_pcm_start(u
->pcm_handle
);
636 /* Hmm, nothing to do. Let's sleep */
637 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
643 /* Tell ALSA about this and process its response */
644 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
)) {
645 struct pollfd
*pollfd
;
646 unsigned short revents
= 0;
650 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
652 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
653 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
657 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
)) {
659 if (revents
& POLLERR
)
660 pa_log_warn("Got POLLERR from ALSA");
661 if (revents
& POLLNVAL
)
662 pa_log_warn("Got POLLNVAL from ALSA");
663 if (revents
& POLLHUP
)
664 pa_log_warn("Got POLLHUP from ALSA");
666 /* Try to recover from this error */
668 switch (snd_pcm_state(u
->pcm_handle
)) {
670 case SND_PCM_STATE_XRUN
:
671 if ((err
= snd_pcm_recover(u
->pcm_handle
, -EPIPE
, 1)) != 0) {
672 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
677 case SND_PCM_STATE_SUSPENDED
:
678 if ((err
= snd_pcm_recover(u
->pcm_handle
, -ESTRPIPE
, 1)) != 0) {
679 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
686 snd_pcm_drop(u
->pcm_handle
);
688 if ((err
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
689 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
699 /* If this was no regular exit from the loop we have to continue
700 * processing messages until we received PA_MESSAGE_SHUTDOWN */
701 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
702 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
705 pa_log_debug("Thread shutting down");
708 int pa__init(pa_module
*m
) {
710 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
;
726 snd_pcm_info_alloca(&pcm_info
);
730 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
731 pa_log("Failed to parse module arguments");
735 ss
= m
->core
->default_sample_spec
;
736 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
737 pa_log("Failed to parse sample specification and channel map");
741 frame_size
= pa_frame_size(&ss
);
743 nfrags
= m
->core
->default_n_fragments
;
744 frag_size
= pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*1000, &ss
);
746 frag_size
= frame_size
;
748 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0) {
749 pa_log("Failed to parse buffer metrics");
752 period_size
= frag_size
/frame_size
;
754 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
755 pa_log("Failed to parse mmap argument.");
759 u
= pa_xnew0(struct userdata
, 1);
763 u
->use_mmap
= use_mmap
;
765 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
766 u
->rtpoll
= pa_rtpoll_new();
767 u
->alsa_rtpoll_item
= NULL
;
768 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
770 snd_config_update_free_global();
772 dev
= pa_xstrdup(pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
));
776 if ((err
= snd_pcm_open(&u
->pcm_handle
, dev
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
)) < 0) {
777 pa_log("Error opening PCM device %s: %s", dev
, snd_strerror(err
));
783 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, &b
)) < 0) {
786 /* Hmm, some hw is very exotic, so we retry with plughw, if hw didn't work */
788 if (pa_startswith(dev
, "hw:")) {
789 char *d
= pa_sprintf_malloc("plughw:%s", dev
+3);
790 pa_log_debug("Opening the device as '%s' didn't work, retrying with '%s'.", dev
, d
);
794 snd_pcm_close(u
->pcm_handle
);
795 u
->pcm_handle
= NULL
;
800 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
808 u
->device_name
= dev
;
810 if (use_mmap
&& !b
) {
811 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
812 u
->use_mmap
= use_mmap
= b
;
816 pa_log_info("Successfully enabled mmap() mode.");
818 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
819 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
823 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
)) < 0) {
824 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
828 /* ALSA might tweak the sample spec, so recalculate the frame size */
829 frame_size
= pa_frame_size(&ss
);
831 if (ss
.channels
!= map
.channels
)
832 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
833 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
835 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
836 pa_log_warn("Error opening mixer: %s", snd_strerror(err
));
839 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
840 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Master", "PCM"))) {
842 snd_mixer_close(u
->mixer_handle
);
843 u
->mixer_handle
= NULL
;
847 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
850 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", dev
);
854 u
->sink
= pa_sink_new(m
->core
, __FILE__
, name
, namereg_fail
, &ss
, &map
);
858 pa_log("Failed to create sink object");
862 u
->sink
->parent
.process_msg
= sink_process_msg
;
863 u
->sink
->userdata
= u
;
865 pa_sink_set_module(u
->sink
, m
);
866 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
867 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
868 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc(
869 "ALSA PCM on %s (%s)%s",
871 snd_pcm_info_get_name(pcm_info
),
872 use_mmap
? " via DMA" : ""));
875 u
->sink
->flags
= PA_SINK_HARDWARE
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_LATENCY
;
877 u
->frame_size
= frame_size
;
878 u
->fragment_size
= frag_size
= period_size
* frame_size
;
879 u
->nfragments
= nfrags
;
880 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
882 pa_log_info("Using %u fragments of size %lu bytes.", nfrags
, (long unsigned) u
->fragment_size
);
884 pa_memchunk_reset(&u
->memchunk
);
886 if (u
->mixer_handle
) {
887 /* Initialize mixer code */
889 pa_assert(u
->mixer_elem
);
891 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
894 for (i
= 0; i
< ss
.channels
; i
++)
895 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
898 if (i
== ss
.channels
) {
899 pa_log_debug("ALSA device has separate volumes controls for all %u channels.", ss
.channels
);
900 u
->sink
->get_volume
= sink_get_volume_cb
;
901 u
->sink
->set_volume
= sink_set_volume_cb
;
902 snd_mixer_selem_get_playback_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
904 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);
907 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
908 u
->sink
->get_mute
= sink_get_mute_cb
;
909 u
->sink
->set_mute
= sink_set_mute_cb
;
912 u
->mixer_fdl
= pa_alsa_fdlist_new();
914 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
915 pa_log("failed to initialise file descriptor monitoring");
919 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
920 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
924 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
925 pa_log("Failed to create thread.");
929 /* Get initial mixer settings */
930 if (u
->sink
->get_volume
)
931 u
->sink
->get_volume(u
->sink
);
932 if (u
->sink
->get_mute
)
933 u
->sink
->get_mute(u
->sink
);
935 pa_sink_put(u
->sink
);
951 void pa__done(pa_module
*m
) {
956 if (!(u
= m
->userdata
))
960 pa_sink_unlink(u
->sink
);
963 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
964 pa_thread_free(u
->thread
);
967 pa_thread_mq_done(&u
->thread_mq
);
970 pa_sink_unref(u
->sink
);
972 if (u
->memchunk
.memblock
)
973 pa_memblock_unref(u
->memchunk
.memblock
);
975 if (u
->alsa_rtpoll_item
)
976 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
979 pa_rtpoll_free(u
->rtpoll
);
982 pa_alsa_fdlist_free(u
->mixer_fdl
);
985 snd_mixer_close(u
->mixer_handle
);
988 snd_pcm_drop(u
->pcm_handle
);
989 snd_pcm_close(u
->pcm_handle
);
992 pa_xfree(u
->device_name
);
995 snd_config_update_free_global();