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
32 #ifdef HAVE_SYS_POLL_H
38 #include <asoundlib.h>
40 #include <pulse/xmalloc.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>
51 #include "alsa-util.h"
52 #include "module-alsa-sink-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("ALSA Sink")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
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>")
68 snd_pcm_t *pcm_handle;
69 snd_mixer_t *mixer_handle;
70 snd_mixer_elem_t *mixer_elem;
72 struct pa_alsa_fdlist *pcm_fdl;
73 struct pa_alsa_fdlist *mixer_fdl;
74 long hw_volume_max, hw_volume_min;
76 size_t frame_size, fragment_size;
77 pa_memchunk memchunk, silence;
81 static const char* const valid_modargs[] = {
93 #define DEFAULT_DEVICE "default"
95 static void update_usage(struct userdata *u) {
96 pa_module_set_used(u->module, u->sink ? pa_sink_used_by(u->sink) : 0);
99 static void clear_up(struct userdata *u) {
103 pa_sink_disconnect(u->sink);
104 pa_sink_unref(u->sink);
109 pa_alsa_fdlist_free(u->pcm_fdl);
111 pa_alsa_fdlist_free(u->mixer_fdl);
113 u->pcm_fdl = u->mixer_fdl = NULL;
115 if (u->mixer_handle) {
116 snd_mixer_close(u->mixer_handle);
117 u->mixer_handle = NULL;
121 snd_pcm_drop(u->pcm_handle);
122 snd_pcm_close(u->pcm_handle);
123 u->pcm_handle = NULL;
127 static int xrun_recovery(struct userdata *u) {
131 pa_log_info("*** ALSA-XRUN (playback) ***");
133 if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0) {
134 pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
137 pa_module_unload_request(u->module);
144 static int suspend_recovery(struct userdata *u) {
148 pa_log_info("*** ALSA-SUSPEND (playback) ***");
150 if ((ret = snd_pcm_resume(u->pcm_handle)) < 0) {
155 pa_log("snd_pcm_resume() failed: %s", snd_strerror(-ret));
157 if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0)
158 pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
163 pa_module_unload_request(u->module);
171 static void do_write(struct userdata *u) {
178 pa_memchunk *memchunk = NULL;
179 snd_pcm_sframes_t frames;
181 if (u->memchunk.memblock)
182 memchunk = &u->memchunk;
184 if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
185 memchunk = &u->silence;
187 memchunk = &u->memchunk;
189 assert(memchunk->memblock);
190 assert(memchunk->length);
191 assert((memchunk->length % u->frame_size) == 0);
193 p = pa_memblock_acquire(memchunk->memblock);
195 if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) p + memchunk->index, memchunk->length / u->frame_size)) < 0) {
196 pa_memblock_release(memchunk->memblock);
198 if (frames == -EAGAIN)
201 if (frames == -EPIPE) {
202 if (xrun_recovery(u) < 0)
208 if (frames == -ESTRPIPE) {
209 if (suspend_recovery(u) < 0)
215 pa_log("snd_pcm_writei() failed: %s", snd_strerror(-frames));
218 pa_module_unload_request(u->module);
222 pa_memblock_release(memchunk->memblock);
224 if (memchunk == &u->memchunk) {
225 size_t l = frames * u->frame_size;
226 memchunk->index += l;
227 memchunk->length -= l;
229 if (memchunk->length == 0) {
230 pa_memblock_unref(memchunk->memblock);
231 memchunk->memblock = NULL;
232 memchunk->index = memchunk->length = 0;
240 static void fdl_callback(void *userdata) {
241 struct userdata *u = userdata;
244 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
245 if (xrun_recovery(u) < 0)
248 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_SUSPENDED)
249 if (suspend_recovery(u) < 0)
255 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
256 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
258 assert(u && u->mixer_handle);
260 if (mask == SND_CTL_EVENT_MASK_REMOVE)
263 if (mask & SND_CTL_EVENT_MASK_VALUE) {
264 if (u->sink->get_hw_volume)
265 u->sink->get_hw_volume(u->sink);
266 if (u->sink->get_hw_mute)
267 u->sink->get_hw_mute(u->sink);
268 pa_subscription_post(u->sink->core,
269 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
276 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
278 struct userdata *u = s->userdata;
279 snd_pcm_sframes_t frames;
282 assert(s && u && u->sink);
284 if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
285 pa_log("failed to get delay: %s", snd_strerror(err));
286 s->get_latency = NULL;
293 r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
295 if (u->memchunk.memblock)
296 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
301 static int sink_get_hw_volume_cb(pa_sink *s) {
302 struct userdata *u = s->userdata;
307 assert(u->mixer_elem);
309 for (i = 0; i < s->hw_volume.channels; i++) {
312 assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
314 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0)
317 set_vol = (long) roundf(((float) s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
319 /* Try to avoid superfluous volume changes */
321 s->hw_volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
327 pa_log_error("Unable to read volume: %s", snd_strerror(err));
328 s->get_hw_volume = NULL;
329 s->set_hw_volume = NULL;
333 static int sink_set_hw_volume_cb(pa_sink *s) {
334 struct userdata *u = s->userdata;
340 assert(u->mixer_elem);
342 for (i = 0; i < s->hw_volume.channels; i++) {
345 assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
347 vol = s->hw_volume.values[i];
349 if (vol > PA_VOLUME_NORM)
350 vol = PA_VOLUME_NORM;
352 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
354 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0)
361 pa_log_error("Unable to set volume: %s", snd_strerror(err));
362 s->get_hw_volume = NULL;
363 s->set_hw_volume = NULL;
367 static int sink_get_hw_mute_cb(pa_sink *s) {
368 struct userdata *u = s->userdata;
371 assert(u && u->mixer_elem);
373 err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
375 pa_log_error("Unable to get switch: %s", snd_strerror(err));
376 s->get_hw_mute = NULL;
377 s->set_hw_mute = NULL;
386 static int sink_set_hw_mute_cb(pa_sink *s) {
387 struct userdata *u = s->userdata;
390 assert(u && u->mixer_elem);
392 err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
394 pa_log_error("Unable to set switch: %s", snd_strerror(err));
395 s->get_hw_mute = NULL;
396 s->set_hw_mute = NULL;
403 int pa__init(pa_core *c, pa_module*m) {
404 pa_modargs *ma = NULL;
406 struct userdata *u = NULL;
410 uint32_t periods, fragsize;
411 snd_pcm_uframes_t period_size;
413 snd_pcm_info_t *pcm_info = NULL;
417 char *name_buf = NULL;
420 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
421 pa_log("failed to parse module arguments");
425 ss = c->default_sample_spec;
426 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
427 pa_log("failed to parse sample specification and channel map");
431 frame_size = pa_frame_size(&ss);
433 /* Fix latency to 100ms */
435 fragsize = pa_bytes_per_second(&ss)/128;
437 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
438 pa_log("failed to parse buffer metrics");
441 period_size = fragsize/frame_size;
443 u = pa_xnew0(struct userdata, 1);
447 snd_config_update_free_global();
448 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) {
449 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
453 if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
454 (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
455 pa_log("Error fetching PCM info: %s", snd_strerror(err));
459 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
460 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
464 /* ALSA might tweak the sample spec, so recalculate the frame size */
465 frame_size = pa_frame_size(&ss);
467 if (ss.channels != map.channels)
468 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
469 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
471 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
472 pa_log("Error opening mixer: %s", snd_strerror(err));
476 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
477 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM", "Master"))) {
478 snd_mixer_close(u->mixer_handle);
479 u->mixer_handle = NULL;
482 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
485 name = name_buf = pa_sprintf_malloc("alsa_output.%s", dev);
489 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map))) {
490 pa_log("Failed to create sink object");
494 u->sink->is_hardware = 1;
495 u->sink->get_latency = sink_get_latency_cb;
496 if (u->mixer_handle) {
497 assert(u->mixer_elem);
498 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
501 for (i = 0;i < ss.channels;i++) {
502 if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
506 if (i == ss.channels) {
507 u->sink->get_hw_volume = sink_get_hw_volume_cb;
508 u->sink->set_hw_volume = sink_set_hw_volume_cb;
509 snd_mixer_selem_get_playback_volume_range(
510 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
513 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
514 u->sink->get_hw_mute = sink_get_hw_mute_cb;
515 u->sink->set_hw_mute = sink_set_hw_mute_cb;
518 u->sink->userdata = u;
519 pa_sink_set_owner(u->sink, m);
520 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("ALSA PCM on %s (%s)", dev, snd_pcm_info_get_name(pcm_info)));
523 u->pcm_fdl = pa_alsa_fdlist_new();
525 if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
526 pa_log("failed to initialise file descriptor monitoring");
530 if (u->mixer_handle) {
531 u->mixer_fdl = pa_alsa_fdlist_new();
532 assert(u->mixer_fdl);
533 if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
534 pa_log("failed to initialise file descriptor monitoring");
537 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
538 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
542 u->frame_size = frame_size;
543 u->fragment_size = period_size * frame_size;
545 pa_log_info("using %u fragments of size %lu bytes.", periods, (long unsigned)u->fragment_size);
547 u->silence.memblock = pa_memblock_new(c->mempool, u->silence.length = u->fragment_size);
548 assert(u->silence.memblock);
549 pa_silence_memblock(u->silence.memblock, &ss);
550 u->silence.index = 0;
552 u->memchunk.memblock = NULL;
553 u->memchunk.index = u->memchunk.length = 0;
557 /* Get initial mixer settings */
558 if (u->sink->get_hw_volume)
559 u->sink->get_hw_volume(u->sink);
560 if (u->sink->get_hw_mute)
561 u->sink->get_hw_mute(u->sink);
571 snd_pcm_info_free(pcm_info);
583 void pa__done(pa_core *c, pa_module*m) {
587 if (!(u = m->userdata))
592 if (u->memchunk.memblock)
593 pa_memblock_unref(u->memchunk.memblock);
594 if (u->silence.memblock)
595 pa_memblock_unref(u->silence.memblock);