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-source-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("ALSA Source")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 "source_name=<name for the source> "
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
;
77 static const char* const valid_modargs
[] = {
89 #define DEFAULT_SOURCE_NAME "alsa_input"
90 #define DEFAULT_DEVICE "default"
92 static void update_usage(struct userdata
*u
) {
93 pa_module_set_used(u
->module
,
94 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
97 static void xrun_recovery(struct userdata
*u
) {
100 pa_log(__FILE__
": *** ALSA-XRUN (capture) ***");
102 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
103 pa_log(__FILE__
": snd_pcm_prepare() failed");
106 static void do_read(struct userdata
*u
) {
112 pa_memchunk post_memchunk
;
113 snd_pcm_sframes_t frames
;
116 if (!u
->memchunk
.memblock
) {
117 u
->memchunk
.memblock
= pa_memblock_new(u
->memchunk
.length
= u
->fragment_size
, u
->source
->core
->memblock_stat
);
118 u
->memchunk
.index
= 0;
121 assert(u
->memchunk
.memblock
&& u
->memchunk
.memblock
->data
&& u
->memchunk
.length
&& u
->memchunk
.memblock
->length
&& (u
->memchunk
.length
% u
->frame_size
) == 0);
123 if ((frames
= snd_pcm_readi(u
->pcm_handle
, (uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
/ u
->frame_size
)) < 0) {
124 if (frames
== -EAGAIN
)
127 if (frames
== -EPIPE
) {
132 pa_log(__FILE__
": snd_pcm_readi() failed: %s", strerror(-frames
));
136 l
= frames
* u
->frame_size
;
138 post_memchunk
= u
->memchunk
;
139 post_memchunk
.length
= l
;
141 pa_source_post(u
->source
, &post_memchunk
);
143 u
->memchunk
.index
+= l
;
144 u
->memchunk
.length
-= l
;
146 if (u
->memchunk
.length
== 0) {
147 pa_memblock_unref(u
->memchunk
.memblock
);
148 u
->memchunk
.memblock
= NULL
;
149 u
->memchunk
.index
= u
->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_REMOVE
)
174 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
175 if (u
->source
->get_hw_volume
)
176 u
->source
->get_hw_volume(u
->source
);
177 if (u
->source
->get_hw_mute
)
178 u
->source
->get_hw_mute(u
->source
);
179 pa_subscription_post(u
->source
->core
,
180 PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
,
187 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
188 struct userdata
*u
= s
->userdata
;
189 snd_pcm_sframes_t frames
;
190 assert(s
&& u
&& u
->source
);
192 if (snd_pcm_delay(u
->pcm_handle
, &frames
) < 0) {
193 pa_log(__FILE__
": failed to get delay");
194 s
->get_latency
= NULL
;
198 return pa_bytes_to_usec(frames
* u
->frame_size
, &s
->sample_spec
);
201 static int source_get_hw_volume_cb(pa_source
*s
) {
202 struct userdata
*u
= s
->userdata
;
207 assert(u
&& u
->mixer_elem
);
209 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
210 assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
212 err
= snd_mixer_selem_get_capture_volume(u
->mixer_elem
, i
, &vol
);
215 s
->hw_volume
.values
[i
] =
216 (vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
/ (u
->hw_volume_max
- u
->hw_volume_min
);
222 pa_log_error(__FILE__
": Unable to read volume: %s", snd_strerror(err
));
223 s
->get_hw_volume
= NULL
;
224 s
->set_hw_volume
= NULL
;
228 static int source_set_hw_volume_cb(pa_source
*s
) {
229 struct userdata
*u
= s
->userdata
;
234 assert(u
&& u
->mixer_elem
);
236 for (i
= 0;i
< s
->hw_volume
.channels
;i
++) {
237 assert(snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
));
239 vol
= s
->hw_volume
.values
[i
];
241 if (vol
> PA_VOLUME_NORM
)
242 vol
= PA_VOLUME_NORM
;
244 vol
= vol
* (u
->hw_volume_max
- u
->hw_volume_min
) /
245 PA_VOLUME_NORM
+ u
->hw_volume_min
;
246 err
= snd_mixer_selem_set_capture_volume(u
->mixer_elem
, i
, vol
);
254 pa_log_error(__FILE__
": Unable to set volume: %s", snd_strerror(err
));
255 s
->get_hw_volume
= NULL
;
256 s
->set_hw_volume
= NULL
;
260 static int source_get_hw_mute_cb(pa_source
*s
) {
261 struct userdata
*u
= s
->userdata
;
264 assert(u
&& u
->mixer_elem
);
266 err
= snd_mixer_selem_get_capture_switch(u
->mixer_elem
, 0, &sw
);
268 pa_log_error(__FILE__
": Unable to get switch: %s", snd_strerror(err
));
269 s
->get_hw_mute
= NULL
;
270 s
->set_hw_mute
= NULL
;
279 static int source_set_hw_mute_cb(pa_source
*s
) {
280 struct userdata
*u
= s
->userdata
;
283 assert(u
&& u
->mixer_elem
);
285 err
= snd_mixer_selem_set_capture_switch_all(u
->mixer_elem
, !s
->hw_muted
);
287 pa_log_error(__FILE__
": Unable to set switch: %s", snd_strerror(err
));
288 s
->get_hw_mute
= NULL
;
289 s
->set_hw_mute
= NULL
;
296 int pa__init(pa_core
*c
, pa_module
*m
) {
297 pa_modargs
*ma
= NULL
;
299 struct userdata
*u
= NULL
;
303 unsigned periods
, fragsize
;
304 snd_pcm_uframes_t period_size
;
306 snd_pcm_info_t
*pcm_info
= NULL
;
309 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
310 pa_log(__FILE__
": failed to parse module arguments");
314 ss
= c
->default_sample_spec
;
315 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
316 pa_log(__FILE__
": failed to parse sample specification");
320 pa_alsa_channel_map_init_auto(&map
, ss
.channels
);
321 if ((pa_modargs_get_channel_map(ma
, &map
) < 0)) {
322 pa_log(__FILE__
": invalid channel map.");
326 if (ss
.channels
!= map
.channels
) {
327 pa_log(__FILE__
": channel map and sample specification don't match.");
331 frame_size
= pa_frame_size(&ss
);
335 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
336 pa_log(__FILE__
": failed to parse buffer metrics");
339 period_size
= fragsize
;
341 u
= pa_xmalloc0(sizeof(struct userdata
));
345 snd_config_update_free_global();
346 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) {
347 pa_log(__FILE__
": Error opening PCM device %s: %s", dev
, snd_strerror(err
));
351 if ((err
= snd_pcm_info_malloc(&pcm_info
)) < 0 ||
352 (err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
353 pa_log(__FILE__
": Error fetching PCM info: %s", snd_strerror(err
));
357 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &period_size
)) < 0) {
358 pa_log(__FILE__
": Failed to set hardware parameters: %s", snd_strerror(err
));
362 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0) {
363 pa_log(__FILE__
": Error opening mixer: %s", snd_strerror(err
));
367 if ((pa_alsa_prepare_mixer(u
->mixer_handle
, dev
) < 0) ||
368 !(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Capture"))) {
369 snd_mixer_close(u
->mixer_handle
);
370 u
->mixer_handle
= NULL
;
373 u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
);
376 u
->source
->userdata
= u
;
377 u
->source
->get_latency
= source_get_latency_cb
;
378 if (u
->mixer_handle
) {
379 assert(u
->mixer_elem
);
380 if (snd_mixer_selem_has_capture_volume(u
->mixer_elem
)) {
383 for (i
= 0;i
< ss
.channels
;i
++) {
384 if (!snd_mixer_selem_has_capture_channel(u
->mixer_elem
, i
))
388 if (i
== ss
.channels
) {
389 u
->source
->get_hw_volume
= source_get_hw_volume_cb
;
390 u
->source
->set_hw_volume
= source_set_hw_volume_cb
;
391 snd_mixer_selem_get_capture_volume_range(
392 u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
);
395 if (snd_mixer_selem_has_capture_switch(u
->mixer_elem
)) {
396 u
->source
->get_hw_mute
= source_get_hw_mute_cb
;
397 u
->source
->set_hw_mute
= source_set_hw_mute_cb
;
400 pa_source_set_owner(u
->source
, m
);
401 u
->source
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev
, snd_pcm_info_get_name(pcm_info
));
403 u
->pcm_fdl
= pa_alsa_fdlist_new();
405 if (pa_alsa_fdlist_init_pcm(u
->pcm_fdl
, u
->pcm_handle
, c
->mainloop
, fdl_callback
, u
) < 0) {
406 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
410 if (u
->mixer_handle
) {
411 u
->mixer_fdl
= pa_alsa_fdlist_new();
412 assert(u
->mixer_fdl
);
413 if (pa_alsa_fdlist_init_mixer(u
->mixer_fdl
, u
->mixer_handle
, c
->mainloop
) < 0) {
414 pa_log(__FILE__
": failed to initialise file descriptor monitoring");
417 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
418 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
421 u
->frame_size
= frame_size
;
422 u
->fragment_size
= period_size
;
424 pa_log(__FILE__
": using %u fragments of size %lu bytes.", periods
, (long unsigned)u
->fragment_size
);
426 u
->memchunk
.memblock
= NULL
;
427 u
->memchunk
.index
= u
->memchunk
.length
= 0;
429 snd_pcm_start(u
->pcm_handle
);
433 /* Get initial mixer settings */
434 if (u
->source
->get_hw_volume
)
435 u
->source
->get_hw_volume(u
->source
);
436 if (u
->source
->get_hw_mute
)
437 u
->source
->get_hw_mute(u
->source
);
444 snd_pcm_info_free(pcm_info
);
456 void pa__done(pa_core
*c
, pa_module
*m
) {
460 if (!(u
= m
->userdata
))
464 pa_source_disconnect(u
->source
);
465 pa_source_unref(u
->source
);
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
);