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_SINK_NAME "alsa_output"
91 #define DEFAULT_DEVICE "default"
93 static void update_usage(struct userdata
*u
) {
94 pa_module_set_used(u
->module
,
95 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
96 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0));
99 static void xrun_recovery(struct userdata
*u
) {
102 pa_log(__FILE__
": *** ALSA-XRUN (playback) ***");
104 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
105 pa_log(__FILE__
": snd_pcm_prepare() failed");
108 static void do_write(struct userdata
*u
) {
114 pa_memchunk
*memchunk
= NULL
;
115 snd_pcm_sframes_t frames
;
117 if (u
->memchunk
.memblock
)
118 memchunk
= &u
->memchunk
;
120 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
121 memchunk
= &u
->silence
;
123 memchunk
= &u
->memchunk
;
126 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
128 if ((frames
= snd_pcm_writei(u
->pcm_handle
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
129 if (frames
== -EAGAIN
)
132 if (frames
== -EPIPE
) {
137 pa_log(__FILE__
": snd_pcm_writei() failed: %s", snd_strerror(frames
));
141 if (memchunk
== &u
->memchunk
) {
142 size_t l
= frames
* u
->frame_size
;
143 memchunk
->index
+= l
;
144 memchunk
->length
-= l
;
146 if (memchunk
->length
== 0) {
147 pa_memblock_unref(memchunk
->memblock
);
148 memchunk
->memblock
= NULL
;
149 memchunk
->index
= memchunk
->length
= 0;
157 static void fdl_callback(void *userdata
) {
158 struct userdata
*u
= userdata
;
161 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
167 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
168 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
170 assert(u
&& u
->mixer_handle
);
172 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
175 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
176 if (u
->sink
->get_hw_volume
)
177 u
->sink
->get_hw_volume(u
->sink
);
178 if (u
->sink
->get_hw_mute
)
179 u
->sink
->get_hw_mute(u
->sink
);
180 pa_subscription_post(u
->sink
->core
,
181 PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
,
188 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
190 struct userdata
*u
= s
->userdata
;
191 snd_pcm_sframes_t frames
;
194 assert(s
&& u
&& u
->sink
);
196 if ((err
= snd_pcm_delay(u
->pcm_handle
, &frames
)) < 0) {
197 pa_log(__FILE__
": failed to get delay: %s", snd_strerror(err
));
198 s
->get_latency
= NULL
;
205 r
+= pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
207 if (u
->memchunk
.memblock
)
208 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
213 static int sink_get_hw_volume_cb(pa_sink
*s
) {
214 struct userdata
*u
= s
->userdata
;
219 assert(u
&& u
->mixer_elem
);
221 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
222 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
224 err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
);
227 s
->hw_volume
.values
[i
] =
228 (vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
/ (u
->hw_volume_max
- u
->hw_volume_min
);
234 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
235 s
->get_hw_volume
= NULL
;
236 s
->set_hw_volume
= NULL
;
240 static int sink_set_hw_volume_cb(pa_sink
*s
) {
241 struct userdata
*u
= s
->userdata
;
246 assert(u
&& u
->mixer_elem
);
248 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
249 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
251 vol
= s
->hw_volume
.values
[i
];
253 if (vol
> PA_VOLUME_NORM
)
254 vol
= PA_VOLUME_NORM
;
256 vol
= (vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) /
257 PA_VOLUME_NORM
+ u
->hw_volume_min
;
259 err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, vol
);
267 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
268 s
->get_hw_volume
= NULL
;
269 s
->set_hw_volume
= NULL
;
273 static int sink_get_hw_mute_cb(pa_sink
*s
) {
274 struct userdata
*u
= s
->userdata
;
277 assert(u
&& u
->mixer_elem
);
279 err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
);
281 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
282 s
->get_hw_mute
= NULL
;
283 s
->set_hw_mute
= NULL
;
292 static int sink_set_hw_mute_cb(pa_sink
*s
) {
293 struct userdata
*u
= s
->userdata
;
296 assert(u
&& u
->mixer_elem
);
298 err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->hw_muted
);
300 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
301 s
->get_hw_mute
= NULL
;
302 s
->set_hw_mute
= NULL
;
309 int pa__init(pa_core
*c
, pa_module
*m
) {
310 pa_modargs
*ma
= NULL
;
312 struct userdata
*u
= NULL
;
316 uint32_t periods
, fragsize
;
317 snd_pcm_uframes_t period_size
;
319 snd_pcm_info_t
*pcm_info
= NULL
;
322 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
323 pa_log(__FILE__
": failed to parse module arguments");
327 ss
= c
->default_sample_spec
;
328 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
329 pa_log(__FILE__
": failed to parse sample specification and channel map");
333 frame_size
= pa_frame_size(&ss
);
335 /* Fix latency to 100ms */
337 fragsize
= pa_bytes_per_second(&ss
)/128;
339 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
340 pa_log(__FILE__
": failed to parse buffer metrics");
343 period_size
= fragsize
/frame_size
;
345 u
= pa_xnew0(struct userdata
, 1);
349 snd_config_update_free_global();
350 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) {
351 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
355 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
356 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
357 pa_log(__FILE__
": Error fetching PCM info: %s", snd_strerror(err
));
361 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
362 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
366 if (ss
.channels
!= map
.channels
)
367 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
368 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
370 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
371 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
375 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
376 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "PCM", "Master"))) {
377 snd_mixer_close(u
->mixer_handle
);
378 u
->mixer_handle
= NULL
;
381 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
382 pa_log(__FILE__
": Failed to create sink object");
386 u
->sink
->is_hardware
= 1;
387 u
->sink
->get_latency
= sink_get_latency_cb
;
388 if (u
->mixer_handle
) {
389 assert(u
->mixer_elem
);
390 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
393 for (i
= 0;i
< ss
.channels
;i
++) {
394 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
398 if (i
== ss
.channels
) {
399 u
->sink
->get_hw_volume
= sink_get_hw_volume_cb
;
400 u
->sink
->set_hw_volume
= sink_set_hw_volume_cb
;
401 snd_mixer_selem_get_playback_volume_range(
402 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
405 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
406 u
->sink
->get_hw_mute
= sink_get_hw_mute_cb
;
407 u
->sink
->set_hw_mute
= sink_set_hw_mute_cb
;
410 u
->sink
->userdata
= u
;
411 pa_sink_set_owner(u
->sink
, m
);
412 u
->sink
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev
, snd_pcm_info_get_name(pcm_info
));
414 u
->pcm_fdl
= pa_alsa_fdlist_new();
416 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
417 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
421 if (u
->mixer_handle
) {
422 u
->mixer_fdl
= pa_alsa_fdlist_new();
423 assert(u
->mixer_fdl
);
424 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
425 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
428 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
429 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
433 u
->frame_size
= frame_size
;
434 u
->fragment_size
= period_size
* frame_size
;
436 pa_log_info(__FILE__
": using %u fragments of size %lu bytes.", periods
, (long unsigned)u
->fragment_size
);
438 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
, c
->memblock_stat
);
439 assert(u
->silence
.memblock
);
440 pa_silence_memblock(u
->silence
.memblock
, &ss
);
441 u
->silence
.index
= 0;
443 u
->memchunk
.memblock
= NULL
;
444 u
->memchunk
.index
= u
->memchunk
.length
= 0;
448 /* Get initial mixer settings */
449 if (u
->sink
->get_hw_volume
)
450 u
->sink
->get_hw_volume(u
->sink
);
451 if (u
->sink
->get_hw_mute
)
452 u
->sink
->get_hw_mute(u
->sink
);
459 snd_pcm_info_free(pcm_info
);
471 void pa__done(pa_core
*c
, pa_module
*m
) {
475 if (!(u
= m
->userdata
))
479 pa_sink_disconnect(u
->sink
);
480 pa_sink_unref(u
->sink
);
484 pa_alsa_fdlist_free(u
->pcm_fdl
);
486 pa_alsa_fdlist_free(u
->mixer_fdl
);
489 snd_mixer_close(u
->mixer_handle
);
492 snd_pcm_drop(u
->pcm_handle
);
493 snd_pcm_close(u
->pcm_handle
);
496 if (u
->memchunk
.memblock
)
497 pa_memblock_unref(u
->memchunk
.memblock
);
498 if (u
->silence
.memblock
)
499 pa_memblock_unref(u
->silence
.memblock
);