Completely rework ALSA device selection code: choose the device to open depending...
[pulseaudio.git] / src / modules / module-alsa-sink.c
blobe62b8a067c15c50945756b26a33ae7d3e95a4a2b
1 /* $Id$ */
3 /***
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
22 USA.
23 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdio.h>
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);
57 PA_MODULE_USAGE(
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> "
63 "rate=<sample rate> "
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"
71 struct userdata {
72 pa_core *core;
73 pa_module *module;
74 pa_sink *sink;
76 pa_thread *thread;
77 pa_thread_mq thread_mq;
78 pa_rtpoll *rtpoll;
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;
88 unsigned nfragments;
89 pa_memchunk memchunk;
91 char *device_name;
93 pa_bool_t use_mmap;
95 pa_bool_t first;
97 pa_rtpoll_item *alsa_rtpoll_item;
100 static const char* const valid_modargs[] = {
101 "device",
102 "device_id",
103 "sink_name",
104 "format",
105 "channels",
106 "rate",
107 "fragments",
108 "fragment_size",
109 "channel_map",
110 "mmap",
111 NULL
114 static int mmap_write(struct userdata *u) {
115 int work_done = 0;
117 pa_assert(u);
118 pa_sink_assert_ref(u->sink);
120 for (;;) {
121 pa_memchunk chunk;
122 void *p;
123 snd_pcm_sframes_t n;
124 int err;
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) {
130 if (n == -EPIPE) {
131 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
132 u->first = TRUE;
135 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
136 continue;
138 if (err == -EAGAIN)
139 return work_done;
141 pa_log("snd_pcm_avail_update: %s", snd_strerror(err));
142 return -1;
145 /* pa_log("Got request for %i samples", (int) n); */
147 if (n <= 0)
148 return work_done;
150 frames = n;
152 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
154 if (err == -EPIPE) {
155 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
156 u->first = TRUE;
159 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
160 continue;
162 if (err == -EAGAIN)
163 return work_done;
165 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
166 return -1;
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);
181 chunk.index = 0;
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) {
191 if (err == -EPIPE) {
192 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
193 u->first = TRUE;
196 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
197 continue;
199 if (err == -EAGAIN)
200 return work_done;
202 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
203 return -1;
206 work_done = 1;
208 if (frames >= (snd_pcm_uframes_t) n)
209 return work_done;
211 /* pa_log("wrote %i samples", (int) frames); */
215 static int unix_write(struct userdata *u) {
216 snd_pcm_status_t *status;
217 int work_done = 0;
219 snd_pcm_status_alloca(&status);
221 pa_assert(u);
222 pa_sink_assert_ref(u->sink);
224 for (;;) {
225 void *p;
226 snd_pcm_sframes_t t;
227 ssize_t l;
228 int err;
230 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
231 pa_log("Failed to query DSP status data: %s", snd_strerror(err));
232 return -1;
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); */
242 if (l <= 0)
243 return work_done;
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); */
256 pa_assert(t != 0);
258 if (t < 0) {
260 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
261 continue;
263 if (t == -EAGAIN) {
264 pa_log_debug("EAGAIN");
265 return work_done;
266 } else {
267 pa_log("Failed to write data to DSP: %s", snd_strerror(t));
268 return -1;
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);
280 work_done = 1;
282 if (t * u->frame_size >= (unsigned) l)
283 return work_done;
287 static pa_usec_t sink_get_latency(struct userdata *u) {
288 pa_usec_t r = 0;
289 snd_pcm_status_t *status;
290 snd_pcm_sframes_t frames = 0;
291 int err;
293 snd_pcm_status_alloca(&status);
295 pa_assert(u);
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));
300 else
301 frames = snd_pcm_status_get_delay(status);
303 if (frames > 0)
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);
309 return r;
312 static int build_pollfd(struct userdata *u) {
313 int err;
314 struct pollfd *pollfd;
315 int n;
317 pa_assert(u);
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));
322 return -1;
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));
333 return -1;
336 return 0;
339 static int suspend(struct userdata *u) {
340 pa_assert(u);
341 pa_assert(u->pcm_handle);
343 /* Let's suspend */
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...");
355 return 0;
358 static int unsuspend(struct userdata *u) {
359 pa_sample_spec ss;
360 int err;
361 pa_bool_t b;
362 unsigned nfrags;
363 snd_pcm_uframes_t period_size;
365 pa_assert(u);
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));
373 goto fail;
376 ss = u->sink->sample_spec;
377 nfrags = u->nfragments;
378 period_size = u->fragment_size / u->frame_size;
379 b = u->use_mmap;
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));
383 goto fail;
386 if (b != u->use_mmap) {
387 pa_log_warn("Resume failed, couldn't get original access mode.");
388 goto fail;
391 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
392 pa_log_warn("Resume failed, couldn't restore original sample settings.");
393 goto fail;
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.");
398 goto fail;
401 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
402 pa_log("Failed to set software parameters: %s", snd_strerror(err));
403 goto fail;
406 if (build_pollfd(u) < 0)
407 goto fail;
409 /* FIXME: We need to reload the volume somehow */
411 u->first = TRUE;
413 pa_log_info("Resumed successfully...");
415 return 0;
417 fail:
418 if (u->pcm_handle) {
419 snd_pcm_close(u->pcm_handle);
420 u->pcm_handle = NULL;
423 return -1;
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;
429 switch (code) {
431 case PA_SINK_MESSAGE_GET_LATENCY: {
432 pa_usec_t r = 0;
434 if (u->pcm_handle)
435 r = sink_get_latency(u);
437 *((pa_usec_t*) data) = r;
439 return 0;
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));
449 if (suspend(u) < 0)
450 return -1;
452 break;
454 case PA_SINK_IDLE:
455 case PA_SINK_RUNNING:
457 if (u->sink->thread_info.state == PA_SINK_INIT) {
458 if (build_pollfd(u) < 0)
459 return -1;
462 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
463 if (unsuspend(u) < 0)
464 return -1;
467 break;
469 case PA_SINK_UNLINKED:
470 case PA_SINK_INIT:
474 break;
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);
483 pa_assert(u);
484 pa_assert(u->mixer_handle);
486 if (mask == SND_CTL_EVENT_MASK_REMOVE)
487 return 0;
489 if (mask & SND_CTL_EVENT_MASK_VALUE) {
490 pa_sink_get_volume(u->sink);
491 pa_sink_get_mute(u->sink);
494 return 0;
497 static int sink_get_volume_cb(pa_sink *s) {
498 struct userdata *u = s->userdata;
499 int err;
500 int i;
502 pa_assert(u);
503 pa_assert(u->mixer_elem);
505 for (i = 0; i < s->sample_spec.channels; i++) {
506 long set_vol, vol;
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)
511 goto fail;
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 */
516 if (set_vol != vol)
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));
520 return 0;
522 fail:
523 pa_log_error("Unable to read volume: %s", snd_strerror(err));
525 s->get_volume = NULL;
526 s->set_volume = NULL;
527 return -1;
530 static int sink_set_volume_cb(pa_sink *s) {
531 struct userdata *u = s->userdata;
532 int err;
533 int i;
535 pa_assert(u);
536 pa_assert(u->mixer_elem);
538 for (i = 0; i < s->sample_spec.channels; i++) {
539 long alsa_vol;
540 pa_volume_t vol;
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)
552 goto fail;
555 return 0;
557 fail:
558 pa_log_error("Unable to set volume: %s", snd_strerror(err));
560 s->get_volume = NULL;
561 s->set_volume = NULL;
562 return -1;
565 static int sink_get_mute_cb(pa_sink *s) {
566 struct userdata *u = s->userdata;
567 int err, sw;
569 pa_assert(u);
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));
575 s->get_mute = NULL;
576 s->set_mute = NULL;
577 return -1;
580 s->muted = !sw;
582 return 0;
585 static int sink_set_mute_cb(pa_sink *s) {
586 struct userdata *u = s->userdata;
587 int err;
589 pa_assert(u);
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));
595 s->get_mute = NULL;
596 s->set_mute = NULL;
597 return -1;
600 return 0;
603 static void thread_func(void *userdata) {
604 struct userdata *u = userdata;
606 pa_assert(u);
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);
616 for (;;) {
617 int ret;
619 /* Render some data and write it to the dsp */
620 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
621 int work_done = 0;
623 if (u->use_mmap) {
624 if ((work_done = mmap_write(u)) < 0)
625 goto fail;
626 } else {
627 if ((work_done = unix_write(u)) < 0)
628 goto fail;
631 if (work_done && u->first) {
632 pa_log_info("Starting playback.");
633 snd_pcm_start(u->pcm_handle);
634 u->first = FALSE;
635 continue;
639 /* Hmm, nothing to do. Let's sleep */
640 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
641 goto fail;
643 if (ret == 0)
644 goto finish;
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;
650 int err;
651 unsigned n;
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));
657 goto fail;
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));
676 goto fail;
678 break;
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));
683 goto fail;
685 break;
687 default:
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));
693 goto fail;
695 break;
701 fail:
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);
707 finish:
708 pa_log_debug("Thread shutting down");
711 int pa__init(pa_module*m) {
713 pa_modargs *ma = NULL;
714 struct userdata *u = NULL;
715 const char *dev_id;
716 pa_sample_spec ss;
717 pa_channel_map map;
718 uint32_t nfrags, frag_size;
719 snd_pcm_uframes_t period_size;
720 size_t frame_size;
721 snd_pcm_info_t *pcm_info = NULL;
722 int err;
723 char *t;
724 const char *name;
725 char *name_buf = NULL;
726 int namereg_fail;
727 pa_bool_t use_mmap = TRUE, b;
729 snd_pcm_info_alloca(&pcm_info);
731 pa_assert(m);
733 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
734 pa_log("Failed to parse module arguments");
735 goto fail;
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");
741 goto fail;
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);
748 if (frag_size <= 0)
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");
753 goto fail;
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.");
759 goto fail;
762 u = pa_xnew0(struct userdata, 1);
763 u->core = m->core;
764 u->module = m;
765 m->userdata = u;
766 u->use_mmap = use_mmap;
767 u->first = TRUE;
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();
775 b = use_mmap;
777 if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
779 if (!(u->pcm_handle = pa_alsa_open_by_device_id(
780 dev_id,
781 &u->device_name,
782 &ss, &map,
783 SND_PCM_STREAM_PLAYBACK,
784 &nfrags, &period_size,
785 &b)))
787 goto fail;
789 } else {
791 if (!(u->pcm_handle = pa_alsa_open_by_device_string(
792 pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
793 &u->device_name,
794 &ss, &map,
795 SND_PCM_STREAM_PLAYBACK,
796 &nfrags, &period_size,
797 &b)))
798 goto fail;
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;
810 if (u->use_mmap)
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));
815 goto fail;
818 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
819 pa_log("Failed to set software parameters: %s", snd_strerror(err));
820 goto fail;
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));
828 else {
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)))
839 namereg_fail = 1;
840 else {
841 name = name_buf = pa_sprintf_malloc("alsa_output.%s", u->device_name);
842 namereg_fail = 0;
845 u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
846 pa_xfree(name_buf);
848 if (!u->sink) {
849 pa_log("Failed to create sink object");
850 goto fail;
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",
861 u->device_name,
862 snd_pcm_info_get_name(pcm_info),
863 use_mmap ? " via DMA" : ""));
864 pa_xfree(t);
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)) {
883 int i;
885 for (i = 0; i < ss.channels; i++)
886 if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
887 break;
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);
894 } else
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");
907 goto fail;
910 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
911 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
912 } else
913 u->mixer_fdl = NULL;
915 if (!(u->thread = pa_thread_new(thread_func, u))) {
916 pa_log("Failed to create thread.");
917 goto fail;
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);
928 pa_modargs_free(ma);
930 return 0;
932 fail:
934 if (ma)
935 pa_modargs_free(ma);
937 pa__done(m);
939 return -1;
942 void pa__done(pa_module*m) {
943 struct userdata *u;
945 pa_assert(m);
947 if (!(u = m->userdata))
948 return;
950 if (u->sink)
951 pa_sink_unlink(u->sink);
953 if (u->thread) {
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);
960 if (u->sink)
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);
969 if (u->rtpoll)
970 pa_rtpoll_free(u->rtpoll);
972 if (u->mixer_fdl)
973 pa_alsa_fdlist_free(u->mixer_fdl);
975 if (u->mixer_handle)
976 snd_mixer_close(u->mixer_handle);
978 if (u->pcm_handle) {
979 snd_pcm_drop(u->pcm_handle);
980 snd_pcm_close(u->pcm_handle);
983 pa_xfree(u->device_name);
984 pa_xfree(u);
986 snd_config_update_free_global();