4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polypcore/core.h>
38 #include <polypcore/module.h>
39 #include <polypcore/memchunk.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/modargs.h>
42 #include <polypcore/util.h>
43 #include <polypcore/sample-util.h>
44 #include <polypcore/xmalloc.h>
45 #include <polypcore/log.h>
47 #include "alsa-util.h"
48 #include "module-alsa-sink-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("ALSA Sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 "sink_name=<name for the sink> "
55 "device=<ALSA device> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "fragments=<number of fragments> "
60 "fragment_size=<fragment size> "
61 "channel_map=<channel map>")
64 snd_pcm_t
*pcm_handle
;
65 snd_mixer_t
*mixer_handle
;
66 snd_mixer_elem_t
*mixer_elem
;
68 struct pa_alsa_fdlist
*pcm_fdl
;
69 struct pa_alsa_fdlist
*mixer_fdl
;
70 long hw_volume_max
, hw_volume_min
;
72 size_t frame_size
, fragment_size
;
73 pa_memchunk memchunk
, silence
;
77 static const char* const valid_modargs
[] = {
89 #define DEFAULT_SINK_NAME "alsa_output"
90 #define DEFAULT_DEVICE "default"
92 static void update_usage(struct userdata
*u
) {
93 pa_module_set_used(u
->module
,
94 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
95 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0));
98 static void xrun_recovery(struct userdata
*u
) {
101 pa_log(__FILE__
": *** ALSA-XRUN (playback) ***");
103 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
104 pa_log(__FILE__
": snd_pcm_prepare() failed");
107 static void do_write(struct userdata
*u
) {
113 pa_memchunk
*memchunk
= NULL
;
114 snd_pcm_sframes_t frames
;
116 if (u
->memchunk
.memblock
)
117 memchunk
= &u
->memchunk
;
119 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
120 memchunk
= &u
->silence
;
122 memchunk
= &u
->memchunk
;
125 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
127 if ((frames
= snd_pcm_writei(u
->pcm_handle
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
128 if (frames
== -EAGAIN
)
131 if (frames
== -EPIPE
) {
136 pa_log(__FILE__
": snd_pcm_writei() failed: %s", snd_strerror(frames
));
140 if (memchunk
== &u
->memchunk
) {
141 size_t l
= frames
* u
->frame_size
;
142 memchunk
->index
+= l
;
143 memchunk
->length
-= l
;
145 if (memchunk
->length
== 0) {
146 pa_memblock_unref(memchunk
->memblock
);
147 memchunk
->memblock
= NULL
;
148 memchunk
->index
= memchunk
->length
= 0;
156 static void fdl_callback(void *userdata
) {
157 struct userdata
*u
= userdata
;
160 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
166 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
167 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
169 assert(u
&& u
->mixer_handle
);
171 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
172 if (u
->sink
->get_hw_volume
)
173 u
->sink
->get_hw_volume(u
->sink
);
174 if (u
->sink
->get_hw_mute
)
175 u
->sink
->get_hw_mute(u
->sink
);
176 pa_subscription_post(u
->sink
->core
,
177 PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
,
184 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
186 struct userdata
*u
= s
->userdata
;
187 snd_pcm_sframes_t frames
;
190 assert(s
&& u
&& u
->sink
);
192 if ((err
= snd_pcm_delay(u
->pcm_handle
, &frames
)) < 0) {
193 pa_log(__FILE__
": failed to get delay: %s", snd_strerror(err
));
194 s
->get_latency
= NULL
;
201 r
+= pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
203 if (u
->memchunk
.memblock
)
204 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
209 static int sink_get_hw_volume_cb(pa_sink
*s
) {
210 struct userdata
*u
= s
->userdata
;
215 assert(u
&& u
->mixer_elem
);
217 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
218 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
220 err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
);
223 s
->hw_volume
.values
[i
] =
224 (vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
/ (u
->hw_volume_max
- u
->hw_volume_min
);
230 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
231 s
->get_hw_volume
= NULL
;
232 s
->set_hw_volume
= NULL
;
236 static int sink_set_hw_volume_cb(pa_sink
*s
) {
237 struct userdata
*u
= s
->userdata
;
242 assert(u
&& u
->mixer_elem
);
244 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
245 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
247 vol
= s
->hw_volume
.values
[i
];
249 if (vol
> PA_VOLUME_NORM
)
250 vol
= PA_VOLUME_NORM
;
252 vol
= (vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) /
253 PA_VOLUME_NORM
+ u
->hw_volume_min
;
255 err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, vol
);
263 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
264 s
->get_hw_volume
= NULL
;
265 s
->set_hw_volume
= NULL
;
269 static int sink_get_hw_mute_cb(pa_sink
*s
) {
270 struct userdata
*u
= s
->userdata
;
273 assert(u
&& u
->mixer_elem
);
275 err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
);
277 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
278 s
->get_hw_mute
= NULL
;
279 s
->set_hw_mute
= NULL
;
288 static int sink_set_hw_mute_cb(pa_sink
*s
) {
289 struct userdata
*u
= s
->userdata
;
292 assert(u
&& u
->mixer_elem
);
294 err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->hw_muted
);
296 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
297 s
->get_hw_mute
= NULL
;
298 s
->set_hw_mute
= NULL
;
305 int pa__init(pa_core
*c
, pa_module
*m
) {
306 pa_modargs
*ma
= NULL
;
308 struct userdata
*u
= NULL
;
312 uint32_t periods
, fragsize
;
313 snd_pcm_uframes_t period_size
;
315 snd_pcm_info_t
*pcm_info
= NULL
;
318 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
319 pa_log(__FILE__
": failed to parse module arguments");
323 ss
= c
->default_sample_spec
;
324 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
) < 0) {
325 pa_log(__FILE__
": failed to parse sample specification");
328 frame_size
= pa_frame_size(&ss
);
332 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
333 pa_log(__FILE__
": failed to parse buffer metrics");
336 period_size
= fragsize
;
338 u
= pa_xmalloc0(sizeof(struct userdata
));
342 snd_config_update_free_global();
343 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) {
344 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
348 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
349 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
350 pa_log(__FILE__
": Error fetching PCM info: %s", snd_strerror(err
));
354 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
355 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
359 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
360 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
364 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
365 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "PCM"))) {
366 snd_mixer_close(u
->mixer_handle
);
367 u
->mixer_handle
= NULL
;
370 u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
);
373 u
->sink
->get_latency
= sink_get_latency_cb
;
374 if (u
->mixer_handle
) {
375 assert(u
->mixer_elem
);
376 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
379 for (i
= 0;i
< ss
.channels
;i
++) {
380 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
384 if (i
== ss
.channels
) {
385 u
->sink
->get_hw_volume
= sink_get_hw_volume_cb
;
386 u
->sink
->set_hw_volume
= sink_set_hw_volume_cb
;
387 snd_mixer_selem_get_playback_volume_range(
388 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
391 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
392 u
->sink
->get_hw_mute
= sink_get_hw_mute_cb
;
393 u
->sink
->set_hw_mute
= sink_set_hw_mute_cb
;
396 u
->sink
->userdata
= u
;
397 pa_sink_set_owner(u
->sink
, m
);
398 u
->sink
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev
, snd_pcm_info_get_name(pcm_info
));
400 u
->pcm_fdl
= pa_alsa_fdlist_new();
402 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
403 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
407 if (u
->mixer_handle
) {
408 u
->mixer_fdl
= pa_alsa_fdlist_new();
409 assert(u
->mixer_fdl
);
410 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
411 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
414 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
415 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
418 u
->frame_size
= frame_size
;
419 u
->fragment_size
= period_size
;
421 pa_log_info(__FILE__
": using %u fragments of size %lu bytes.", periods
, (long unsigned)u
->fragment_size
);
423 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
, c
->memblock_stat
);
424 assert(u
->silence
.memblock
);
425 pa_silence_memblock(u
->silence
.memblock
, &ss
);
426 u
->silence
.index
= 0;
428 u
->memchunk
.memblock
= NULL
;
429 u
->memchunk
.index
= u
->memchunk
.length
= 0;
433 /* Get initial mixer settings */
434 if (u
->sink
->get_hw_volume
)
435 u
->sink
->get_hw_volume(u
->sink
);
436 if (u
->sink
->get_hw_mute
)
437 u
->sink
->get_hw_mute(u
->sink
);
444 snd_pcm_info_free(pcm_info
);
456 void pa__done(pa_core
*c
, pa_module
*m
) {
460 if (!(u
= m
->userdata
))
464 pa_sink_disconnect(u
->sink
);
465 pa_sink_unref(u
->sink
);
469 pa_alsa_fdlist_free(u
->pcm_fdl
);
471 pa_alsa_fdlist_free(u
->mixer_fdl
);
474 snd_mixer_close(u
->mixer_handle
);
477 snd_pcm_drop(u
->pcm_handle
);
478 snd_pcm_close(u
->pcm_handle
);
481 if (u
->memchunk
.memblock
)
482 pa_memblock_unref(u
->memchunk
.memblock
);
483 if (u
->silence
.memblock
)
484 pa_memblock_unref(u
->silence
.memblock
);