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
)
53 PA_MODULE_USAGE("sink_name=<name for the sink> device=<ALSA device> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
56 snd_pcm_t
*pcm_handle
;
57 snd_mixer_t
*mixer_handle
;
58 snd_mixer_elem_t
*mixer_elem
;
60 struct pa_alsa_fdlist
*pcm_fdl
;
61 struct pa_alsa_fdlist
*mixer_fdl
;
62 long hw_volume_max
, hw_volume_min
;
64 size_t frame_size
, fragment_size
;
65 pa_memchunk memchunk
, silence
;
69 static const char* const valid_modargs
[] = {
80 #define DEFAULT_SINK_NAME "alsa_output"
81 #define DEFAULT_DEVICE "default"
83 static void update_usage(struct userdata
*u
) {
84 pa_module_set_used(u
->module
,
85 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
86 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0));
89 static void xrun_recovery(struct userdata
*u
) {
92 pa_log(__FILE__
": *** ALSA-XRUN (playback) ***");
94 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
95 pa_log(__FILE__
": snd_pcm_prepare() failed");
98 static void do_write(struct userdata
*u
) {
104 pa_memchunk
*memchunk
= NULL
;
105 snd_pcm_sframes_t frames
;
107 if (u
->memchunk
.memblock
)
108 memchunk
= &u
->memchunk
;
110 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
111 memchunk
= &u
->silence
;
113 memchunk
= &u
->memchunk
;
116 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
118 if ((frames
= snd_pcm_writei(u
->pcm_handle
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
119 if (frames
== -EAGAIN
)
122 if (frames
== -EPIPE
) {
127 pa_log(__FILE__
": snd_pcm_writei() failed");
131 if (memchunk
== &u
->memchunk
) {
132 size_t l
= frames
* u
->frame_size
;
133 memchunk
->index
+= l
;
134 memchunk
->length
-= l
;
136 if (memchunk
->length
== 0) {
137 pa_memblock_unref(memchunk
->memblock
);
138 memchunk
->memblock
= NULL
;
139 memchunk
->index
= memchunk
->length
= 0;
147 static void fdl_callback(void *userdata
) {
148 struct userdata
*u
= userdata
;
151 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
157 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
158 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
160 assert(u
&& u
->mixer_handle
);
162 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
163 if (u
->sink
->get_hw_volume
)
164 u
->sink
->get_hw_volume(u
->sink
);
165 if (u
->sink
->get_hw_mute
)
166 u
->sink
->get_hw_mute(u
->sink
);
167 pa_subscription_post(u
->sink
->core
,
168 PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
,
175 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
177 struct userdata
*u
= s
->userdata
;
178 snd_pcm_sframes_t frames
;
179 assert(s
&& u
&& u
->sink
);
181 if (snd_pcm_delay(u
->pcm_handle
, &frames
) < 0) {
182 pa_log(__FILE__
": failed to get delay");
183 s
->get_latency
= NULL
;
190 r
+= pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
192 if (u
->memchunk
.memblock
)
193 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
198 static int sink_get_hw_volume_cb(pa_sink
*s
) {
199 struct userdata
*u
= s
->userdata
;
203 assert(u
&& u
->mixer_elem
);
205 if (snd_mixer_selem_has_playback_volume_joined(u
->mixer_elem
)) {
206 err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, 0, &vol
);
209 pa_cvolume_set(&s
->hw_volume
, s
->hw_volume
.channels
,
210 (vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
/ (u
->hw_volume_max
- u
->hw_volume_min
));
214 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
215 err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
);
218 s
->hw_volume
.values
[i
] =
219 (vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
/ (u
->hw_volume_max
- u
->hw_volume_min
);
226 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
227 s
->get_hw_volume
= NULL
;
228 s
->set_hw_volume
= NULL
;
232 static int sink_set_hw_volume_cb(pa_sink
*s
) {
233 struct userdata
*u
= s
->userdata
;
237 assert(u
&& u
->mixer_elem
);
239 if (snd_mixer_selem_has_playback_volume_joined(u
->mixer_elem
)) {
240 vol
= pa_cvolume_avg(&s
->hw_volume
) * (u
->hw_volume_max
- u
->hw_volume_min
) /
241 PA_VOLUME_NORM
+ u
->hw_volume_min
;
242 err
= snd_mixer_selem_set_playback_volume_all(u
->mixer_elem
, vol
);
248 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
249 vol
= s
->hw_volume
.values
[i
] * (u
->hw_volume_max
- u
->hw_volume_min
) /
250 PA_VOLUME_NORM
+ u
->hw_volume_min
;
251 err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, vol
);
260 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
261 s
->get_hw_volume
= NULL
;
262 s
->set_hw_volume
= NULL
;
266 static int sink_get_hw_mute_cb(pa_sink
*s
) {
267 struct userdata
*u
= s
->userdata
;
270 assert(u
&& u
->mixer_elem
);
272 err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
);
274 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
275 s
->get_hw_mute
= NULL
;
276 s
->set_hw_mute
= NULL
;
285 static int sink_set_hw_mute_cb(pa_sink
*s
) {
286 struct userdata
*u
= s
->userdata
;
289 assert(u
&& u
->mixer_elem
);
291 err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->hw_muted
);
293 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
294 s
->get_hw_mute
= NULL
;
295 s
->set_hw_mute
= NULL
;
302 int pa__init(pa_core
*c
, pa_module
*m
) {
303 pa_modargs
*ma
= NULL
;
305 struct userdata
*u
= NULL
;
308 uint32_t periods
, fragsize
;
309 snd_pcm_uframes_t period_size
;
313 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
314 pa_log(__FILE__
": failed to parse module arguments");
318 ss
= c
->default_sample_spec
;
319 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
320 pa_log(__FILE__
": failed to parse sample specification");
323 frame_size
= pa_frame_size(&ss
);
327 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
328 pa_log(__FILE__
": failed to parse buffer metrics");
331 period_size
= fragsize
;
333 u
= pa_xmalloc0(sizeof(struct userdata
));
337 snd_config_update_free_global();
338 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) {
339 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
343 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
344 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
348 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
349 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
353 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
354 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "PCM"))) {
355 snd_mixer_close(u
->mixer_handle
);
356 u
->mixer_handle
= NULL
;
359 u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, NULL
);
362 u
->sink
->get_latency
= sink_get_latency_cb
;
363 if (u
->mixer_handle
) {
364 assert(u
->mixer_elem
);
365 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
366 u
->sink
->get_hw_volume
= sink_get_hw_volume_cb
;
367 u
->sink
->set_hw_volume
= sink_set_hw_volume_cb
;
368 snd_mixer_selem_get_playback_volume_range(
369 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
371 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
372 u
->sink
->get_hw_mute
= sink_get_hw_mute_cb
;
373 u
->sink
->set_hw_mute
= sink_set_hw_mute_cb
;
376 u
->sink
->userdata
= u
;
377 pa_sink_set_owner(u
->sink
, m
);
378 u
->sink
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev
);
380 u
->pcm_fdl
= pa_alsa_fdlist_new();
382 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
383 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
387 if (u
->mixer_handle
) {
388 u
->mixer_fdl
= pa_alsa_fdlist_new();
389 assert(u
->mixer_fdl
);
390 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
391 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
394 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
395 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
398 u
->frame_size
= frame_size
;
399 u
->fragment_size
= period_size
;
401 pa_log_info(__FILE__
": using %u fragments of size %u bytes.", periods
, u
->fragment_size
);
403 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
, c
->memblock_stat
);
404 assert(u
->silence
.memblock
);
405 pa_silence_memblock(u
->silence
.memblock
, &ss
);
406 u
->silence
.index
= 0;
408 u
->memchunk
.memblock
= NULL
;
409 u
->memchunk
.index
= u
->memchunk
.length
= 0;
413 /* Get initial mixer settings */
414 if (u
->sink
->get_hw_volume
)
415 u
->sink
->get_hw_volume(u
->sink
);
416 if (u
->sink
->get_hw_mute
)
417 u
->sink
->get_hw_mute(u
->sink
);
433 void pa__done(pa_core
*c
, pa_module
*m
) {
437 if (!(u
= m
->userdata
))
441 pa_sink_disconnect(u
->sink
);
442 pa_sink_unref(u
->sink
);
446 pa_alsa_fdlist_free(u
->pcm_fdl
);
448 pa_alsa_fdlist_free(u
->mixer_fdl
);
451 snd_mixer_close(u
->mixer_handle
);
454 snd_pcm_drop(u
->pcm_handle
);
455 snd_pcm_close(u
->pcm_handle
);
458 if (u
->memchunk
.memblock
)
459 pa_memblock_unref(u
->memchunk
.memblock
);
460 if (u
->silence
.memblock
)
461 pa_memblock_unref(u
->silence
.memblock
);