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-error.h>
40 #include <pulsecore/core.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/memchunk.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/sample-util.h>
47 #include <pulsecore/log.h>
49 #include "alsa-util.h"
50 #include "module-alsa-source-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("ALSA Source")
54 PA_MODULE_VERSION(PACKAGE_VERSION
)
56 "source_name=<name for the source> "
57 "device=<ALSA device> "
58 "format=<sample format> "
59 "channels=<number of channels> "
61 "fragments=<number of fragments> "
62 "fragment_size=<fragment size> "
63 "channel_map=<channel map>")
66 snd_pcm_t
*pcm_handle
;
67 snd_mixer_t
*mixer_handle
;
68 snd_mixer_elem_t
*mixer_elem
;
70 struct pa_alsa_fdlist
*pcm_fdl
;
71 struct pa_alsa_fdlist
*mixer_fdl
;
72 long hw_volume_max
, hw_volume_min
;
74 size_t frame_size
, fragment_size
;
79 static const char* const valid_modargs
[] = {
91 #define DEFAULT_SOURCE_NAME "alsa_input"
92 #define DEFAULT_DEVICE "default"
94 static void update_usage(struct userdata
*u
) {
95 pa_module_set_used(u
->module
,
96 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
99 static void clear_up(struct userdata
*u
) {
103 pa_source_disconnect(u
->source
);
104 pa_source_unref(u
->source
);
109 pa_alsa_fdlist_free(u
->pcm_fdl
);
111 pa_alsa_fdlist_free(u
->mixer_fdl
);
113 u
->pcm_fdl
= u
->mixer_fdl
= NULL
;
115 if (u
->mixer_handle
) {
116 snd_mixer_close(u
->mixer_handle
);
117 u
->mixer_handle
= NULL
;
121 snd_pcm_drop(u
->pcm_handle
);
122 snd_pcm_close(u
->pcm_handle
);
123 u
->pcm_handle
= NULL
;
127 static int xrun_recovery(struct userdata
*u
) {
131 pa_log_info(__FILE__
": *** ALSA-XRUN (capture) ***");
133 if ((ret
= snd_pcm_prepare(u
->pcm_handle
)) < 0) {
134 pa_log(__FILE__
": snd_pcm_prepare() failed: %s", snd_strerror(-ret
));
137 pa_module_unload_request(u
->module
);
145 static void do_read(struct userdata
*u
) {
151 pa_memchunk post_memchunk
;
152 snd_pcm_sframes_t frames
;
155 if (!u
->memchunk
.memblock
) {
156 u
->memchunk
.memblock
= pa_memblock_new(u
->memchunk
.length
= u
->fragment_size
, u
->source
->core
->memblock_stat
);
157 u
->memchunk
.index
= 0;
160 assert(u
->memchunk
.memblock
);
161 assert(u
->memchunk
.length
);
162 assert(u
->memchunk
.memblock
->data
);
163 assert(u
->memchunk
.memblock
->length
);
164 assert(u
->memchunk
.length
% u
->frame_size
== 0);
166 if ((frames
= snd_pcm_readi(u
->pcm_handle
, (uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
)) < 0) {
167 if (frames
== -EAGAIN
)
170 if (frames
== -EPIPE
) {
171 if (xrun_recovery(u
) < 0)
177 pa_log(__FILE__
": snd_pcm_readi() failed: %s", snd_strerror(-frames
));
180 pa_module_unload_request(u
->module
);
184 l
= frames
* u
->frame_size
;
186 post_memchunk
= u
->memchunk
;
187 post_memchunk
.length
= l
;
189 pa_source_post(u
->source
, &post_memchunk
);
191 u
->memchunk
.index
+= l
;
192 u
->memchunk
.length
-= l
;
194 if (u
->memchunk
.length
== 0) {
195 pa_memblock_unref(u
->memchunk
.memblock
);
196 u
->memchunk
.memblock
= NULL
;
197 u
->memchunk
.index
= u
->memchunk
.length
= 0;
204 static void fdl_callback(void *userdata
) {
205 struct userdata
*u
= userdata
;
208 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
209 if (xrun_recovery(u
) < 0)
215 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
216 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
218 assert(u
&& u
->mixer_handle
);
220 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
223 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
224 if (u
->source
->get_hw_volume
)
225 u
->source
->get_hw_volume(u
->source
);
226 if (u
->source
->get_hw_mute
)
227 u
->source
->get_hw_mute(u
->source
);
229 pa_subscription_post(u
->source
->core
,
230 PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
,
237 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
238 struct userdata
*u
= s
->userdata
;
239 snd_pcm_sframes_t frames
;
240 assert(s
&& u
&& u
->source
);
242 if (snd_pcm_delay(u
->pcm_handle
, &frames
) < 0) {
243 pa_log(__FILE__
": failed to get delay");
244 s
->get_latency
= NULL
;
248 return pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
251 static int source_get_hw_volume_cb(pa_source
*s
) {
252 struct userdata
*u
= s
->userdata
;
257 assert(u
&& u
->mixer_elem
);
259 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
262 assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
264 if ((err
= snd_mixer_selem_get_capture_volume(u
->mixer_elem
, i
, &vol
)) < 0)
267 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
;
269 /* Try to avoid superfluous volume changes */
271 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
));
277 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
278 s
->get_hw_volume
= NULL
;
279 s
->set_hw_volume
= NULL
;
283 static int source_set_hw_volume_cb(pa_source
*s
) {
284 struct userdata
*u
= s
->userdata
;
289 assert(u
&& u
->mixer_elem
);
291 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
292 assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
294 vol
= s
->hw_volume
.values
[i
];
296 if (vol
> PA_VOLUME_NORM
)
297 vol
= PA_VOLUME_NORM
;
299 vol
= (long) roundf(((float) vol
* (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
301 if ((err
= snd_mixer_selem_set_capture_volume(u
->mixer_elem
, i
, vol
)) < 0)
308 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
309 s
->get_hw_volume
= NULL
;
310 s
->set_hw_volume
= NULL
;
314 static int source_get_hw_mute_cb(pa_source
*s
) {
315 struct userdata
*u
= s
->userdata
;
318 assert(u
&& u
->mixer_elem
);
320 err
= snd_mixer_selem_get_capture_switch(u
->mixer_elem
, 0, &sw
);
322 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
323 s
->get_hw_mute
= NULL
;
324 s
->set_hw_mute
= NULL
;
333 static int source_set_hw_mute_cb(pa_source
*s
) {
334 struct userdata
*u
= s
->userdata
;
337 assert(u
&& u
->mixer_elem
);
339 err
= snd_mixer_selem_set_capture_switch_all(u
->mixer_elem
, !s
->hw_muted
);
341 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
342 s
->get_hw_mute
= NULL
;
343 s
->set_hw_mute
= NULL
;
350 int pa__init(pa_core
*c
, pa_module
*m
) {
351 pa_modargs
*ma
= NULL
;
353 struct userdata
*u
= NULL
;
357 unsigned periods
, fragsize
;
358 snd_pcm_uframes_t period_size
;
360 snd_pcm_info_t
*pcm_info
= NULL
;
363 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
364 pa_log(__FILE__
": failed to parse module arguments");
368 ss
= c
->default_sample_spec
;
369 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
370 pa_log(__FILE__
": failed to parse sample specification");
374 frame_size
= pa_frame_size(&ss
);
376 /* Fix latency to 100ms */
378 fragsize
= pa_bytes_per_second(&ss
)/128;
380 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
381 pa_log(__FILE__
": failed to parse buffer metrics");
384 period_size
= fragsize
/frame_size
;
386 u
= pa_xnew0(struct userdata
, 1);
390 snd_config_update_free_global();
391 if ((err
= snd_pcm_open(&u
->pcm_handle
, dev
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
)) < 0) {
392 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
396 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
397 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
398 pa_log(__FILE__
": Error fetching PCM info: %s", snd_strerror(err
));
402 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
403 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
407 if (ss
.channels
!= map
.channels
)
408 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
409 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_ALSA
);
411 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
412 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
416 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
417 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Capture", "Mic"))) {
418 snd_mixer_close(u
->mixer_handle
);
419 u
->mixer_handle
= NULL
;
422 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
))) {
423 pa_log(__FILE__
": Failed to create source object");
427 u
->source
->is_hardware
= 1;
428 u
->source
->userdata
= u
;
429 u
->source
->get_latency
= source_get_latency_cb
;
430 if (u
->mixer_handle
) {
431 assert(u
->mixer_elem
);
432 if (snd_mixer_selem_has_capture_volume(u
->mixer_elem
)) {
435 for (i
= 0;i
< ss
.channels
;i
++) {
436 if (!snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
))
440 if (i
== ss
.channels
) {
441 u
->source
->get_hw_volume
= source_get_hw_volume_cb
;
442 u
->source
->set_hw_volume
= source_set_hw_volume_cb
;
443 snd_mixer_selem_get_capture_volume_range(
444 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
447 if (snd_mixer_selem_has_capture_switch(u
->mixer_elem
)) {
448 u
->source
->get_hw_mute
= source_get_hw_mute_cb
;
449 u
->source
->set_hw_mute
= source_set_hw_mute_cb
;
452 pa_source_set_owner(u
->source
, m
);
453 u
->source
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev
, snd_pcm_info_get_name(pcm_info
));
455 u
->pcm_fdl
= pa_alsa_fdlist_new();
457 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
458 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
462 if (u
->mixer_handle
) {
463 u
->mixer_fdl
= pa_alsa_fdlist_new();
464 assert(u
->mixer_fdl
);
465 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
466 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
469 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
470 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
474 u
->frame_size
= frame_size
;
475 u
->fragment_size
= period_size
* frame_size
;
477 pa_log_info(__FILE__
": using %u fragments of size %lu bytes.", periods
, (long unsigned) u
->fragment_size
);
479 u
->memchunk
.memblock
= NULL
;
480 u
->memchunk
.index
= u
->memchunk
.length
= 0;
482 snd_pcm_start(u
->pcm_handle
);
486 /* Get initial mixer settings */
487 if (u
->source
->get_hw_volume
)
488 u
->source
->get_hw_volume(u
->source
);
489 if (u
->source
->get_hw_mute
)
490 u
->source
->get_hw_mute(u
->source
);
497 snd_pcm_info_free(pcm_info
);
509 void pa__done(pa_core
*c
, pa_module
*m
) {
513 if (!(u
= m
->userdata
))
518 if (u
->memchunk
.memblock
)
519 pa_memblock_unref(u
->memchunk
.memblock
);