Add a missing pa_xfree.
[pulseaudio.git] / src / modules / module-alsa-sink.c
blob14aef7c90fd73596847d8389167ed123ed4f3745
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;
99 snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
102 static const char* const valid_modargs[] = {
103 "device",
104 "device_id",
105 "sink_name",
106 "format",
107 "channels",
108 "rate",
109 "fragments",
110 "fragment_size",
111 "channel_map",
112 "mmap",
113 NULL
116 static int mmap_write(struct userdata *u) {
117 int work_done = 0;
119 pa_assert(u);
120 pa_sink_assert_ref(u->sink);
122 for (;;) {
123 pa_memchunk chunk;
124 void *p;
125 snd_pcm_sframes_t n;
126 int err;
127 const snd_pcm_channel_area_t *areas;
128 snd_pcm_uframes_t offset, frames;
130 if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
132 if (n == -EPIPE) {
133 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
134 u->first = TRUE;
137 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
138 continue;
140 if (err == -EAGAIN)
141 return work_done;
143 pa_log("snd_pcm_avail_update: %s", snd_strerror(err));
144 return -1;
147 /* pa_log("Got request for %i samples", (int) n); */
149 if (n <= 0)
150 return work_done;
152 frames = n;
154 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
156 if (err == -EPIPE) {
157 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
158 u->first = TRUE;
161 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
162 continue;
164 if (err == -EAGAIN)
165 return work_done;
167 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
168 return -1;
171 /* Check these are multiples of 8 bit */
172 pa_assert((areas[0].first & 7) == 0);
173 pa_assert((areas[0].step & 7)== 0);
175 /* We assume a single interleaved memory buffer */
176 pa_assert((areas[0].first >> 3) == 0);
177 pa_assert((areas[0].step >> 3) == u->frame_size);
179 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
181 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
182 chunk.length = pa_memblock_get_length(chunk.memblock);
183 chunk.index = 0;
185 pa_sink_render_into_full(u->sink, &chunk);
187 /* FIXME: Maybe we can do something to keep this memory block
188 * a little bit longer around? */
189 pa_memblock_unref_fixed(chunk.memblock);
191 if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
193 if (err == -EPIPE) {
194 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
195 u->first = TRUE;
198 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
199 continue;
201 if (err == -EAGAIN)
202 return work_done;
204 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
205 return -1;
208 work_done = 1;
210 if (frames >= (snd_pcm_uframes_t) n)
211 return work_done;
213 /* pa_log("wrote %i samples", (int) frames); */
217 static int unix_write(struct userdata *u) {
218 snd_pcm_status_t *status;
219 int work_done = 0;
221 snd_pcm_status_alloca(&status);
223 pa_assert(u);
224 pa_sink_assert_ref(u->sink);
226 for (;;) {
227 void *p;
228 snd_pcm_sframes_t t;
229 ssize_t l;
230 int err;
232 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
233 pa_log("Failed to query DSP status data: %s", snd_strerror(err));
234 return -1;
237 if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
238 pa_log_debug("Buffer underrun!");
240 l = snd_pcm_status_get_avail(status) * u->frame_size;
242 /* pa_log("%u bytes to write", l); */
244 if (l <= 0)
245 return work_done;
247 if (u->memchunk.length <= 0)
248 pa_sink_render(u->sink, l, &u->memchunk);
250 pa_assert(u->memchunk.length > 0);
252 p = pa_memblock_acquire(u->memchunk.memblock);
253 t = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, u->memchunk.length / u->frame_size);
254 pa_memblock_release(u->memchunk.memblock);
256 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
258 pa_assert(t != 0);
260 if (t < 0) {
262 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
263 continue;
265 if (t == -EAGAIN) {
266 pa_log_debug("EAGAIN");
267 return work_done;
268 } else {
269 pa_log("Failed to write data to DSP: %s", snd_strerror(t));
270 return -1;
274 u->memchunk.index += t * u->frame_size;
275 u->memchunk.length -= t * u->frame_size;
277 if (u->memchunk.length <= 0) {
278 pa_memblock_unref(u->memchunk.memblock);
279 pa_memchunk_reset(&u->memchunk);
282 work_done = 1;
284 if (t * u->frame_size >= (unsigned) l)
285 return work_done;
289 static pa_usec_t sink_get_latency(struct userdata *u) {
290 pa_usec_t r = 0;
291 snd_pcm_status_t *status;
292 snd_pcm_sframes_t frames = 0;
293 int err;
295 snd_pcm_status_alloca(&status);
297 pa_assert(u);
298 pa_assert(u->pcm_handle);
300 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
301 pa_log("Failed to get delay: %s", snd_strerror(err));
302 else
303 frames = snd_pcm_status_get_delay(status);
305 if (frames > 0)
306 r = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
308 if (u->memchunk.memblock)
309 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
311 return r;
314 static int build_pollfd(struct userdata *u) {
315 int err;
316 struct pollfd *pollfd;
317 int n;
319 pa_assert(u);
320 pa_assert(u->pcm_handle);
322 if ((n = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
323 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
324 return -1;
327 if (u->alsa_rtpoll_item)
328 pa_rtpoll_item_free(u->alsa_rtpoll_item);
330 u->alsa_rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, n);
331 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, NULL);
333 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd, n)) < 0) {
334 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
335 return -1;
338 return 0;
341 static int suspend(struct userdata *u) {
342 pa_assert(u);
343 pa_assert(u->pcm_handle);
345 /* Let's suspend */
346 snd_pcm_drain(u->pcm_handle);
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_info("Device suspended...");
357 return 0;
360 static int unsuspend(struct userdata *u) {
361 pa_sample_spec ss;
362 int err;
363 pa_bool_t b;
364 unsigned nfrags;
365 snd_pcm_uframes_t period_size;
367 pa_assert(u);
368 pa_assert(!u->pcm_handle);
370 pa_log_info("Trying resume...");
372 snd_config_update_free_global();
373 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
374 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
375 goto fail;
378 ss = u->sink->sample_spec;
379 nfrags = u->nfragments;
380 period_size = u->fragment_size / u->frame_size;
381 b = u->use_mmap;
383 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b, TRUE)) < 0) {
384 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
385 goto fail;
388 if (b != u->use_mmap) {
389 pa_log_warn("Resume failed, couldn't get original access mode.");
390 goto fail;
393 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
394 pa_log_warn("Resume failed, couldn't restore original sample settings.");
395 goto fail;
398 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
399 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
400 goto fail;
403 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
404 pa_log("Failed to set software parameters: %s", snd_strerror(err));
405 goto fail;
408 if (build_pollfd(u) < 0)
409 goto fail;
411 /* FIXME: We need to reload the volume somehow */
413 u->first = TRUE;
415 pa_log_info("Resumed successfully...");
417 return 0;
419 fail:
420 if (u->pcm_handle) {
421 snd_pcm_close(u->pcm_handle);
422 u->pcm_handle = NULL;
425 return -1;
428 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
429 struct userdata *u = PA_SINK(o)->userdata;
431 switch (code) {
433 case PA_SINK_MESSAGE_GET_LATENCY: {
434 pa_usec_t r = 0;
436 if (u->pcm_handle)
437 r = sink_get_latency(u);
439 *((pa_usec_t*) data) = r;
441 return 0;
444 case PA_SINK_MESSAGE_SET_STATE:
446 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
448 case PA_SINK_SUSPENDED:
449 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
451 if (suspend(u) < 0)
452 return -1;
454 break;
456 case PA_SINK_IDLE:
457 case PA_SINK_RUNNING:
459 if (u->sink->thread_info.state == PA_SINK_INIT) {
460 if (build_pollfd(u) < 0)
461 return -1;
464 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
465 if (unsuspend(u) < 0)
466 return -1;
469 break;
471 case PA_SINK_UNLINKED:
472 case PA_SINK_INIT:
476 break;
479 return pa_sink_process_msg(o, code, data, offset, chunk);
482 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
483 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
485 pa_assert(u);
486 pa_assert(u->mixer_handle);
488 if (mask == SND_CTL_EVENT_MASK_REMOVE)
489 return 0;
491 if (mask & SND_CTL_EVENT_MASK_VALUE) {
492 pa_sink_get_volume(u->sink);
493 pa_sink_get_mute(u->sink);
496 return 0;
499 static int sink_get_volume_cb(pa_sink *s) {
500 struct userdata *u = s->userdata;
501 int err;
502 int i;
504 pa_assert(u);
505 pa_assert(u->mixer_elem);
507 for (i = 0; i < s->sample_spec.channels; i++) {
508 long set_vol, vol;
510 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
512 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &vol)) < 0)
513 goto fail;
515 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
517 /* Try to avoid superfluous volume changes */
518 if (set_vol != vol)
519 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));
522 return 0;
524 fail:
525 pa_log_error("Unable to read volume: %s", snd_strerror(err));
527 s->get_volume = NULL;
528 s->set_volume = NULL;
529 return -1;
532 static int sink_set_volume_cb(pa_sink *s) {
533 struct userdata *u = s->userdata;
534 int err;
535 int i;
537 pa_assert(u);
538 pa_assert(u->mixer_elem);
540 for (i = 0; i < s->sample_spec.channels; i++) {
541 long alsa_vol;
542 pa_volume_t vol;
544 pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
546 vol = s->volume.values[i];
548 if (vol > PA_VOLUME_NORM)
549 vol = PA_VOLUME_NORM;
551 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
553 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
554 goto fail;
557 return 0;
559 fail:
560 pa_log_error("Unable to set volume: %s", snd_strerror(err));
562 s->get_volume = NULL;
563 s->set_volume = NULL;
564 return -1;
567 static int sink_get_mute_cb(pa_sink *s) {
568 struct userdata *u = s->userdata;
569 int err, sw;
571 pa_assert(u);
572 pa_assert(u->mixer_elem);
574 if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
575 pa_log_error("Unable to get switch: %s", snd_strerror(err));
577 s->get_mute = NULL;
578 s->set_mute = NULL;
579 return -1;
582 s->muted = !sw;
584 return 0;
587 static int sink_set_mute_cb(pa_sink *s) {
588 struct userdata *u = s->userdata;
589 int err;
591 pa_assert(u);
592 pa_assert(u->mixer_elem);
594 if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
595 pa_log_error("Unable to set switch: %s", snd_strerror(err));
597 s->get_mute = NULL;
598 s->set_mute = NULL;
599 return -1;
602 return 0;
605 static void thread_func(void *userdata) {
606 struct userdata *u = userdata;
608 pa_assert(u);
610 pa_log_debug("Thread starting up");
612 if (u->core->realtime_scheduling)
613 pa_make_realtime(u->core->realtime_priority);
615 pa_thread_mq_install(&u->thread_mq);
616 pa_rtpoll_install(u->rtpoll);
618 for (;;) {
619 int ret;
621 /* Render some data and write it to the dsp */
622 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
623 int work_done = 0;
625 if (u->use_mmap) {
626 if ((work_done = mmap_write(u)) < 0)
627 goto fail;
628 } else {
629 if ((work_done = unix_write(u)) < 0)
630 goto fail;
633 if (work_done && u->first) {
634 pa_log_info("Starting playback.");
635 snd_pcm_start(u->pcm_handle);
636 u->first = FALSE;
637 continue;
641 /* Hmm, nothing to do. Let's sleep */
642 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
643 goto fail;
645 if (ret == 0)
646 goto finish;
648 /* Tell ALSA about this and process its response */
649 if (PA_SINK_OPENED(u->sink->thread_info.state)) {
650 struct pollfd *pollfd;
651 unsigned short revents = 0;
652 int err;
653 unsigned n;
655 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
657 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
658 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
659 goto fail;
662 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
664 if (revents & POLLERR)
665 pa_log_warn("Got POLLERR from ALSA");
666 if (revents & POLLNVAL)
667 pa_log_warn("Got POLLNVAL from ALSA");
668 if (revents & POLLHUP)
669 pa_log_warn("Got POLLHUP from ALSA");
671 /* Try to recover from this error */
673 switch (snd_pcm_state(u->pcm_handle)) {
675 case SND_PCM_STATE_XRUN:
676 if ((err = snd_pcm_recover(u->pcm_handle, -EPIPE, 1)) != 0) {
677 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
678 goto fail;
680 break;
682 case SND_PCM_STATE_SUSPENDED:
683 if ((err = snd_pcm_recover(u->pcm_handle, -ESTRPIPE, 1)) != 0) {
684 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
685 goto fail;
687 break;
689 default:
691 snd_pcm_drop(u->pcm_handle);
693 if ((err = snd_pcm_prepare(u->pcm_handle)) < 0) {
694 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
695 goto fail;
697 break;
703 fail:
704 /* If this was no regular exit from the loop we have to continue
705 * processing messages until we received PA_MESSAGE_SHUTDOWN */
706 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
707 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
709 finish:
710 pa_log_debug("Thread shutting down");
713 int pa__init(pa_module*m) {
715 pa_modargs *ma = NULL;
716 struct userdata *u = NULL;
717 const char *dev_id;
718 pa_sample_spec ss;
719 pa_channel_map map;
720 uint32_t nfrags, frag_size;
721 snd_pcm_uframes_t period_size;
722 size_t frame_size;
723 snd_pcm_info_t *pcm_info = NULL;
724 int err;
725 char *t;
726 const char *name;
727 char *name_buf = NULL;
728 int namereg_fail;
729 pa_bool_t use_mmap = TRUE, b;
731 snd_pcm_info_alloca(&pcm_info);
733 pa_assert(m);
735 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
736 pa_log("Failed to parse module arguments");
737 goto fail;
740 ss = m->core->default_sample_spec;
741 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
742 pa_log("Failed to parse sample specification and channel map");
743 goto fail;
746 frame_size = pa_frame_size(&ss);
748 nfrags = m->core->default_n_fragments;
749 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
750 if (frag_size <= 0)
751 frag_size = frame_size;
753 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
754 pa_log("Failed to parse buffer metrics");
755 goto fail;
757 period_size = frag_size/frame_size;
759 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
760 pa_log("Failed to parse mmap argument.");
761 goto fail;
764 u = pa_xnew0(struct userdata, 1);
765 u->core = m->core;
766 u->module = m;
767 m->userdata = u;
768 u->use_mmap = use_mmap;
769 u->first = TRUE;
770 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
771 u->rtpoll = pa_rtpoll_new();
772 u->alsa_rtpoll_item = NULL;
773 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
775 snd_config_update_free_global();
777 b = use_mmap;
779 if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
781 if (!(u->pcm_handle = pa_alsa_open_by_device_id(
782 dev_id,
783 &u->device_name,
784 &ss, &map,
785 SND_PCM_STREAM_PLAYBACK,
786 &nfrags, &period_size,
787 &b)))
789 goto fail;
791 } else {
793 if (!(u->pcm_handle = pa_alsa_open_by_device_string(
794 pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
795 &u->device_name,
796 &ss, &map,
797 SND_PCM_STREAM_PLAYBACK,
798 &nfrags, &period_size,
799 &b)))
800 goto fail;
804 pa_assert(u->device_name);
805 pa_log_info("Successfully opened device %s.", u->device_name);
807 if (use_mmap && !b) {
808 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
809 u->use_mmap = use_mmap = b;
812 if (u->use_mmap)
813 pa_log_info("Successfully enabled mmap() mode.");
815 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
816 pa_log("Error fetching PCM info: %s", snd_strerror(err));
817 goto fail;
820 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
821 pa_log("Failed to set software parameters: %s", snd_strerror(err));
822 goto fail;
825 /* ALSA might tweak the sample spec, so recalculate the frame size */
826 frame_size = pa_frame_size(&ss);
828 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
829 pa_log_warn("Error opening mixer: %s", snd_strerror(err));
830 else {
831 pa_bool_t found = FALSE;
833 if (pa_alsa_prepare_mixer(u->mixer_handle, u->device_name) >= 0)
834 found = TRUE;
835 else {
836 char *md = pa_sprintf_malloc("hw:%s", dev_id);
838 if (strcmp(u->device_name, md))
839 if (pa_alsa_prepare_mixer(u->mixer_handle, md) >= 0)
840 found = TRUE;
842 pa_xfree(md);
845 if (found)
846 if (!(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Master", "PCM")))
847 found = FALSE;
849 if (!found) {
850 snd_mixer_close(u->mixer_handle);
851 u->mixer_handle = NULL;
855 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
856 namereg_fail = 1;
857 else {
858 name = name_buf = pa_sprintf_malloc("alsa_output.%s", u->device_name);
859 namereg_fail = 0;
862 u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
863 pa_xfree(name_buf);
865 if (!u->sink) {
866 pa_log("Failed to create sink object");
867 goto fail;
870 u->sink->parent.process_msg = sink_process_msg;
871 u->sink->userdata = u;
873 pa_sink_set_module(u->sink, m);
874 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
875 pa_sink_set_rtpoll(u->sink, u->rtpoll);
876 pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
877 "ALSA PCM on %s (%s)%s",
878 u->device_name,
879 snd_pcm_info_get_name(pcm_info),
880 use_mmap ? " via DMA" : ""));
881 pa_xfree(t);
883 u->sink->flags = PA_SINK_HARDWARE|PA_SINK_LATENCY;
885 u->frame_size = frame_size;
886 u->fragment_size = frag_size = period_size * frame_size;
887 u->nfragments = nfrags;
888 u->hwbuf_size = u->fragment_size * nfrags;
890 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
892 pa_memchunk_reset(&u->memchunk);
894 if (u->mixer_handle) {
895 pa_assert(u->mixer_elem);
897 if (snd_mixer_selem_has_playback_volume(u->mixer_elem))
898 if (pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, TRUE) >= 0) {
899 u->sink->get_volume = sink_get_volume_cb;
900 u->sink->set_volume = sink_set_volume_cb;
901 snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
902 u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
905 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
906 u->sink->get_mute = sink_get_mute_cb;
907 u->sink->set_mute = sink_set_mute_cb;
908 u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
911 u->mixer_fdl = pa_alsa_fdlist_new();
913 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
914 pa_log("Failed to initialize file descriptor monitoring");
915 goto fail;
918 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
919 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
920 } else
921 u->mixer_fdl = NULL;
923 if (!(u->thread = pa_thread_new(thread_func, u))) {
924 pa_log("Failed to create thread.");
925 goto fail;
928 /* Get initial mixer settings */
929 if (u->sink->get_volume)
930 u->sink->get_volume(u->sink);
931 if (u->sink->get_mute)
932 u->sink->get_mute(u->sink);
934 pa_sink_put(u->sink);
936 pa_modargs_free(ma);
938 return 0;
940 fail:
942 if (ma)
943 pa_modargs_free(ma);
945 pa__done(m);
947 return -1;
950 void pa__done(pa_module*m) {
951 struct userdata *u;
953 pa_assert(m);
955 if (!(u = m->userdata))
956 return;
958 if (u->sink)
959 pa_sink_unlink(u->sink);
961 if (u->thread) {
962 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
963 pa_thread_free(u->thread);
966 pa_thread_mq_done(&u->thread_mq);
968 if (u->sink)
969 pa_sink_unref(u->sink);
971 if (u->memchunk.memblock)
972 pa_memblock_unref(u->memchunk.memblock);
974 if (u->alsa_rtpoll_item)
975 pa_rtpoll_item_free(u->alsa_rtpoll_item);
977 if (u->rtpoll)
978 pa_rtpoll_free(u->rtpoll);
980 if (u->mixer_fdl)
981 pa_alsa_fdlist_free(u->mixer_fdl);
983 if (u->mixer_handle)
984 snd_mixer_close(u->mixer_handle);
986 if (u->pcm_handle) {
987 snd_pcm_drop(u->pcm_handle);
988 snd_pcm_close(u->pcm_handle);
991 pa_xfree(u->device_name);
992 pa_xfree(u);
994 snd_config_update_free_global();