4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #ifdef HAVE_SYS_POLL_H
35 #include <asoundlib.h>
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/core.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/memchunk.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/sample-util.h>
46 #include <pulsecore/log.h>
48 #include "alsa-util.h"
49 #include "module-alsa-sink-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("ALSA Sink")
53 PA_MODULE_VERSION(PACKAGE_VERSION)
55 "sink_name=<name for the sink> "
56 "device=<ALSA device> "
57 "format=<sample format> "
58 "channels=<number of channels> "
60 "fragments=<number of fragments> "
61 "fragment_size=<fragment size> "
62 "channel_map=<channel map>")
65 snd_pcm_t *pcm_handle;
66 snd_mixer_t *mixer_handle;
67 snd_mixer_elem_t *mixer_elem;
69 struct pa_alsa_fdlist *pcm_fdl;
70 struct pa_alsa_fdlist *mixer_fdl;
71 long hw_volume_max, hw_volume_min;
73 size_t frame_size, fragment_size;
74 pa_memchunk memchunk, silence;
78 static const char* const valid_modargs[] = {
90 #define DEFAULT_DEVICE "default"
92 static void update_usage(struct userdata *u) {
93 pa_module_set_used(u->module, u->sink ? pa_sink_used_by(u->sink) : 0);
96 static void clear_up(struct userdata *u) {
100 pa_sink_disconnect(u->sink);
101 pa_sink_unref(u->sink);
106 pa_alsa_fdlist_free(u->pcm_fdl);
108 pa_alsa_fdlist_free(u->mixer_fdl);
110 u->pcm_fdl = u->mixer_fdl = NULL;
112 if (u->mixer_handle) {
113 snd_mixer_close(u->mixer_handle);
114 u->mixer_handle = NULL;
118 snd_pcm_drop(u->pcm_handle);
119 snd_pcm_close(u->pcm_handle);
120 u->pcm_handle = NULL;
124 static int xrun_recovery(struct userdata *u) {
128 pa_log_info("*** ALSA-XRUN (playback) ***");
130 if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0) {
131 pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
134 pa_module_unload_request(u->module);
141 static void do_write(struct userdata *u) {
147 pa_memchunk *memchunk = NULL;
148 snd_pcm_sframes_t frames;
150 if (u->memchunk.memblock)
151 memchunk = &u->memchunk;
153 if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
154 memchunk = &u->silence;
156 memchunk = &u->memchunk;
159 assert(memchunk->memblock && memchunk->memblock->data && memchunk->length && memchunk->memblock->length && (memchunk->length % u->frame_size) == 0);
161 if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)) < 0) {
162 if (frames == -EAGAIN)
165 if (frames == -EPIPE) {
166 if (xrun_recovery(u) < 0)
172 pa_log("snd_pcm_writei() failed: %s", snd_strerror(-frames));
175 pa_module_unload_request(u->module);
179 if (memchunk == &u->memchunk) {
180 size_t l = frames * u->frame_size;
181 memchunk->index += l;
182 memchunk->length -= l;
184 if (memchunk->length == 0) {
185 pa_memblock_unref(memchunk->memblock);
186 memchunk->memblock = NULL;
187 memchunk->index = memchunk->length = 0;
195 static void fdl_callback(void *userdata) {
196 struct userdata *u = userdata;
199 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
200 if (xrun_recovery(u) < 0)
206 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
207 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
209 assert(u && u->mixer_handle);
211 if (mask == SND_CTL_EVENT_MASK_REMOVE)
214 if (mask & SND_CTL_EVENT_MASK_VALUE) {
215 if (u->sink->get_hw_volume)
216 u->sink->get_hw_volume(u->sink);
217 if (u->sink->get_hw_mute)
218 u->sink->get_hw_mute(u->sink);
219 pa_subscription_post(u->sink->core,
220 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
227 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
229 struct userdata *u = s->userdata;
230 snd_pcm_sframes_t frames;
233 assert(s && u && u->sink);
235 if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
236 pa_log("failed to get delay: %s", snd_strerror(err));
237 s->get_latency = NULL;
244 r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
246 if (u->memchunk.memblock)
247 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
252 static int sink_get_hw_volume_cb(pa_sink *s) {
253 struct userdata *u = s->userdata;
258 assert(u->mixer_elem);
260 for (i = 0; i < s->hw_volume.channels; i++) {
263 assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
265 if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0)
268 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;
270 /* Try to avoid superfluous volume changes */
272 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));
278 pa_log_error("Unable to read volume: %s", snd_strerror(err));
279 s->get_hw_volume = NULL;
280 s->set_hw_volume = NULL;
284 static int sink_set_hw_volume_cb(pa_sink *s) {
285 struct userdata *u = s->userdata;
291 assert(u->mixer_elem);
293 for (i = 0; i < s->hw_volume.channels; i++) {
296 assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
298 vol = s->hw_volume.values[i];
300 if (vol > PA_VOLUME_NORM)
301 vol = PA_VOLUME_NORM;
303 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
305 if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0)
312 pa_log_error("Unable to set volume: %s", snd_strerror(err));
313 s->get_hw_volume = NULL;
314 s->set_hw_volume = NULL;
318 static int sink_get_hw_mute_cb(pa_sink *s) {
319 struct userdata *u = s->userdata;
322 assert(u && u->mixer_elem);
324 err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
326 pa_log_error("Unable to get switch: %s", snd_strerror(err));
327 s->get_hw_mute = NULL;
328 s->set_hw_mute = NULL;
337 static int sink_set_hw_mute_cb(pa_sink *s) {
338 struct userdata *u = s->userdata;
341 assert(u && u->mixer_elem);
343 err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
345 pa_log_error("Unable to set switch: %s", snd_strerror(err));
346 s->get_hw_mute = NULL;
347 s->set_hw_mute = NULL;
354 int pa__init(pa_core *c, pa_module*m) {
355 pa_modargs *ma = NULL;
357 struct userdata *u = NULL;
361 uint32_t periods, fragsize;
362 snd_pcm_uframes_t period_size;
364 snd_pcm_info_t *pcm_info = NULL;
368 char *name_buf = NULL;
371 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
372 pa_log("failed to parse module arguments");
376 ss = c->default_sample_spec;
377 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
378 pa_log("failed to parse sample specification and channel map");
382 frame_size = pa_frame_size(&ss);
384 /* Fix latency to 100ms */
386 fragsize = pa_bytes_per_second(&ss)/128;
388 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
389 pa_log("failed to parse buffer metrics");
392 period_size = fragsize/frame_size;
394 u = pa_xnew0(struct userdata, 1);
398 snd_config_update_free_global();
399 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) {
400 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
404 if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
405 (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
406 pa_log("Error fetching PCM info: %s", snd_strerror(err));
410 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
411 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
415 if (ss.channels != map.channels)
416 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
417 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
419 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
420 pa_log("Error opening mixer: %s", snd_strerror(err));
424 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
425 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM", "Master"))) {
426 snd_mixer_close(u->mixer_handle);
427 u->mixer_handle = NULL;
430 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
433 name = name_buf = pa_sprintf_malloc("alsa_output.%s", dev);
437 if (!(u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map))) {
438 pa_log("Failed to create sink object");
442 u->sink->is_hardware = 1;
443 u->sink->get_latency = sink_get_latency_cb;
444 if (u->mixer_handle) {
445 assert(u->mixer_elem);
446 if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
449 for (i = 0;i < ss.channels;i++) {
450 if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
454 if (i == ss.channels) {
455 u->sink->get_hw_volume = sink_get_hw_volume_cb;
456 u->sink->set_hw_volume = sink_set_hw_volume_cb;
457 snd_mixer_selem_get_playback_volume_range(
458 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
461 if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
462 u->sink->get_hw_mute = sink_get_hw_mute_cb;
463 u->sink->set_hw_mute = sink_set_hw_mute_cb;
466 u->sink->userdata = u;
467 pa_sink_set_owner(u->sink, m);
468 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("ALSA PCM on %s (%s)", dev, snd_pcm_info_get_name(pcm_info)));
471 u->pcm_fdl = pa_alsa_fdlist_new();
473 if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
474 pa_log("failed to initialise file descriptor monitoring");
478 if (u->mixer_handle) {
479 u->mixer_fdl = pa_alsa_fdlist_new();
480 assert(u->mixer_fdl);
481 if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
482 pa_log("failed to initialise file descriptor monitoring");
485 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
486 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
490 u->frame_size = frame_size;
491 u->fragment_size = period_size * frame_size;
493 pa_log_info("using %u fragments of size %lu bytes.", periods, (long unsigned)u->fragment_size);
495 u->silence.memblock = pa_memblock_new(c->mempool, u->silence.length = u->fragment_size);
496 assert(u->silence.memblock);
497 pa_silence_memblock(u->silence.memblock, &ss);
498 u->silence.index = 0;
500 u->memchunk.memblock = NULL;
501 u->memchunk.index = u->memchunk.length = 0;
505 /* Get initial mixer settings */
506 if (u->sink->get_hw_volume)
507 u->sink->get_hw_volume(u->sink);
508 if (u->sink->get_hw_mute)
509 u->sink->get_hw_mute(u->sink);
519 snd_pcm_info_free(pcm_info);
531 void pa__done(pa_core *c, pa_module*m) {
535 if (!(u = m->userdata))
540 if (u->memchunk.memblock)
541 pa_memblock_unref(u->memchunk.memblock);
542 if (u->silence.memblock)
543 pa_memblock_unref(u->silence.memblock);