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
,
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 clear_up(struct userdata
*u
) {
102 pa_sink_disconnect(u
->sink
);
103 pa_sink_unref(u
->sink
);
108 pa_alsa_fdlist_free(u
->pcm_fdl
);
110 pa_alsa_fdlist_free(u
->mixer_fdl
);
112 u
->pcm_fdl
= u
->mixer_fdl
= NULL
;
114 if (u
->mixer_handle
) {
115 snd_mixer_close(u
->mixer_handle
);
116 u
->mixer_handle
= NULL
;
120 snd_pcm_drop(u
->pcm_handle
);
121 snd_pcm_close(u
->pcm_handle
);
122 u
->pcm_handle
= NULL
;
126 static int xrun_recovery(struct userdata
*u
) {
130 pa_log_info(__FILE__
": *** ALSA-XRUN (playback) ***");
132 if ((ret
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
133 pa_log(__FILE__
": snd_pcm_prepare() failed: %s", snd_strerror(-ret
));
136 pa_module_unload_request(u
->module
);
143 static void do_write(struct userdata
*u
) {
149 pa_memchunk
*memchunk
= NULL
;
150 snd_pcm_sframes_t frames
;
152 if (u
->memchunk
.memblock
)
153 memchunk
= &u
->memchunk
;
155 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
156 memchunk
= &u
->silence
;
158 memchunk
= &u
->memchunk
;
161 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
163 if ((frames
= snd_pcm_writei(u
->pcm_handle
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
164 if (frames
== -EAGAIN
)
167 if (frames
== -EPIPE
) {
168 if (xrun_recovery(u
) < 0)
174 pa_log(__FILE__
": snd_pcm_writei() failed: %s", snd_strerror(-frames
));
177 pa_module_unload_request(u
->module
);
181 if (memchunk
== &u
->memchunk
) {
182 size_t l
= frames
* u
->frame_size
;
183 memchunk
->index
+= l
;
184 memchunk
->length
-= l
;
186 if (memchunk
->length
== 0) {
187 pa_memblock_unref(memchunk
->memblock
);
188 memchunk
->memblock
= NULL
;
189 memchunk
->index
= memchunk
->length
= 0;
197 static void fdl_callback(void *userdata
) {
198 struct userdata
*u
= userdata
;
201 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
202 if (xrun_recovery(u
) < 0)
208 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
209 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
211 assert(u
&& u
->mixer_handle
);
213 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
216 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
217 if (u
->sink
->get_hw_volume
)
218 u
->sink
->get_hw_volume(u
->sink
);
219 if (u
->sink
->get_hw_mute
)
220 u
->sink
->get_hw_mute(u
->sink
);
221 pa_subscription_post(u
->sink
->core
,
222 PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
,
229 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
231 struct userdata
*u
= s
->userdata
;
232 snd_pcm_sframes_t frames
;
235 assert(s
&& u
&& u
->sink
);
237 if ((err
= snd_pcm_delay(u
->pcm_handle
, &frames
)) < 0) {
238 pa_log(__FILE__
": failed to get delay: %s", snd_strerror(err
));
239 s
->get_latency
= NULL
;
246 r
+= pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
248 if (u
->memchunk
.memblock
)
249 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
254 static int sink_get_hw_volume_cb(pa_sink
*s
) {
255 struct userdata
*u
= s
->userdata
;
260 assert(u
->mixer_elem
);
262 for (i
= 0; i
< s
->hw_volume
.channels
; i
++) {
265 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
267 if ((err
= snd_mixer_selem_get_playback_volume(u
->mixer_elem
, i
, &vol
)) < 0)
270 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
;
272 /* Try to avoid superfluous volume changes */
274 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
));
280 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
281 s
->get_hw_volume
= NULL
;
282 s
->set_hw_volume
= NULL
;
286 static int sink_set_hw_volume_cb(pa_sink
*s
) {
287 struct userdata
*u
= s
->userdata
;
293 assert(u
->mixer_elem
);
295 for (i
= 0; i
< s
->hw_volume
.channels
; i
++) {
298 assert(snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
));
300 vol
= s
->hw_volume
.values
[i
];
302 if (vol
> PA_VOLUME_NORM
)
303 vol
= PA_VOLUME_NORM
;
305 alsa_vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
307 if ((err
= snd_mixer_selem_set_playback_volume(u
->mixer_elem
, i
, alsa_vol
)) < 0)
314 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
315 s
->get_hw_volume
= NULL
;
316 s
->set_hw_volume
= NULL
;
320 static int sink_get_hw_mute_cb(pa_sink
*s
) {
321 struct userdata
*u
= s
->userdata
;
324 assert(u
&& u
->mixer_elem
);
326 err
= snd_mixer_selem_get_playback_switch(u
->mixer_elem
, 0, &sw
);
328 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
329 s
->get_hw_mute
= NULL
;
330 s
->set_hw_mute
= NULL
;
339 static int sink_set_hw_mute_cb(pa_sink
*s
) {
340 struct userdata
*u
= s
->userdata
;
343 assert(u
&& u
->mixer_elem
);
345 err
= snd_mixer_selem_set_playback_switch_all(u
->mixer_elem
, !s
->hw_muted
);
347 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
348 s
->get_hw_mute
= NULL
;
349 s
->set_hw_mute
= NULL
;
356 int pa__init(pa_core
*c
, pa_module
*m
) {
357 pa_modargs
*ma
= NULL
;
359 struct userdata
*u
= NULL
;
363 uint32_t periods
, fragsize
;
364 snd_pcm_uframes_t period_size
;
366 snd_pcm_info_t
*pcm_info
= NULL
;
370 char *name_buf
= NULL
;
373 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
374 pa_log(__FILE__
": failed to parse module arguments");
378 ss
= c
->default_sample_spec
;
379 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
380 pa_log(__FILE__
": failed to parse sample specification and channel map");
384 frame_size
= pa_frame_size(&ss
);
386 /* Fix latency to 100ms */
388 fragsize
= pa_bytes_per_second(&ss
)/128;
390 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
391 pa_log(__FILE__
": failed to parse buffer metrics");
394 period_size
= fragsize
/frame_size
;
396 u
= pa_xnew0(struct userdata
, 1);
400 snd_config_update_free_global();
401 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) {
402 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
406 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
407 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
408 pa_log(__FILE__
": Error fetching PCM info: %s", snd_strerror(err
));
412 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
413 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
417 if (ss
.channels
!= map
.channels
)
418 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
419 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
421 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
422 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
426 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
427 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "PCM", "Master"))) {
428 snd_mixer_close(u
->mixer_handle
);
429 u
->mixer_handle
= NULL
;
432 if ((name
= pa_modargs_get_value(ma
, "sink_name", NULL
)))
435 name
= name_buf
= pa_sprintf_malloc("alsa_output.%s", dev
);
439 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, name
, namereg_fail
, &ss
, &map
))) {
440 pa_log(__FILE__
": Failed to create sink object");
444 u
->sink
->is_hardware
= 1;
445 u
->sink
->get_latency
= sink_get_latency_cb
;
446 if (u
->mixer_handle
) {
447 assert(u
->mixer_elem
);
448 if (snd_mixer_selem_has_playback_volume(u
->mixer_elem
)) {
451 for (i
= 0;i
< ss
.channels
;i
++) {
452 if (!snd_mixer_selem_has_playback_channel(u
->mixer_elem
, i
))
456 if (i
== ss
.channels
) {
457 u
->sink
->get_hw_volume
= sink_get_hw_volume_cb
;
458 u
->sink
->set_hw_volume
= sink_set_hw_volume_cb
;
459 snd_mixer_selem_get_playback_volume_range(
460 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
463 if (snd_mixer_selem_has_playback_switch(u
->mixer_elem
)) {
464 u
->sink
->get_hw_mute
= sink_get_hw_mute_cb
;
465 u
->sink
->set_hw_mute
= sink_set_hw_mute_cb
;
468 u
->sink
->userdata
= u
;
469 pa_sink_set_owner(u
->sink
, m
);
470 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("ALSA PCM on %s (%s)", dev
, snd_pcm_info_get_name(pcm_info
)));
473 u
->pcm_fdl
= pa_alsa_fdlist_new();
475 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
476 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
480 if (u
->mixer_handle
) {
481 u
->mixer_fdl
= pa_alsa_fdlist_new();
482 assert(u
->mixer_fdl
);
483 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
484 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
487 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
488 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
492 u
->frame_size
= frame_size
;
493 u
->fragment_size
= period_size
* frame_size
;
495 pa_log_info(__FILE__
": using %u fragments of size %lu bytes.", periods
, (long unsigned)u
->fragment_size
);
497 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
, c
->memblock_stat
);
498 assert(u
->silence
.memblock
);
499 pa_silence_memblock(u
->silence
.memblock
, &ss
);
500 u
->silence
.index
= 0;
502 u
->memchunk
.memblock
= NULL
;
503 u
->memchunk
.index
= u
->memchunk
.length
= 0;
507 /* Get initial mixer settings */
508 if (u
->sink
->get_hw_volume
)
509 u
->sink
->get_hw_volume(u
->sink
);
510 if (u
->sink
->get_hw_mute
)
511 u
->sink
->get_hw_mute(u
->sink
);
521 snd_pcm_info_free(pcm_info
);
533 void pa__done(pa_core
*c
, pa_module
*m
) {
537 if (!(u
= m
->userdata
))
542 if (u
->memchunk
.memblock
)
543 pa_memblock_unref(u
->memchunk
.memblock
);
544 if (u
->silence
.memblock
)
545 pa_memblock_unref(u
->silence
.memblock
);