]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-source.c
move modules to ${libdir}/polypaudio-${PA_MAJORMINOR}/modules/
[pulseaudio] / src / modules / module-alsa-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdio.h>
28
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #else
32 #include "poll.h"
33 #endif
34
35 #include <asoundlib.h>
36
37 #include <polyp/error.h>
38 #include <polyp/xmalloc.h>
39
40 #include <polypcore/core.h>
41 #include <polypcore/module.h>
42 #include <polypcore/memchunk.h>
43 #include <polypcore/sink.h>
44 #include <polypcore/modargs.h>
45 #include <polypcore/core-util.h>
46 #include <polypcore/sample-util.h>
47 #include <polypcore/log.h>
48
49 #include "alsa-util.h"
50 #include "module-alsa-source-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("ALSA Source")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
55 PA_MODULE_USAGE(
56 "source_name=<name for the source> "
57 "device=<ALSA device> "
58 "format=<sample format> "
59 "channels=<number of channels> "
60 "rate=<sample rate> "
61 "fragments=<number of fragments> "
62 "fragment_size=<fragment size> "
63 "channel_map=<channel map>")
64
65 struct userdata {
66 snd_pcm_t *pcm_handle;
67 snd_mixer_t *mixer_handle;
68 snd_mixer_elem_t *mixer_elem;
69 pa_source *source;
70 struct pa_alsa_fdlist *pcm_fdl;
71 struct pa_alsa_fdlist *mixer_fdl;
72 long hw_volume_max, hw_volume_min;
73
74 size_t frame_size, fragment_size;
75 pa_memchunk memchunk;
76 pa_module *module;
77 };
78
79 static const char* const valid_modargs[] = {
80 "device",
81 "source_name",
82 "channels",
83 "rate",
84 "format",
85 "fragments",
86 "fragment_size",
87 "channel_map",
88 NULL
89 };
90
91 #define DEFAULT_SOURCE_NAME "alsa_input"
92 #define DEFAULT_DEVICE "default"
93
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));
97 }
98
99 static void xrun_recovery(struct userdata *u) {
100 assert(u);
101
102 pa_log(__FILE__": *** ALSA-XRUN (capture) ***");
103
104 if (snd_pcm_prepare(u->pcm_handle) < 0)
105 pa_log(__FILE__": snd_pcm_prepare() failed");
106 }
107
108 static void do_read(struct userdata *u) {
109 assert(u);
110
111 update_usage(u);
112
113 for (;;) {
114 pa_memchunk post_memchunk;
115 snd_pcm_sframes_t frames;
116 size_t l;
117
118 if (!u->memchunk.memblock) {
119 u->memchunk.memblock = pa_memblock_new(u->memchunk.length = u->fragment_size, u->source->core->memblock_stat);
120 u->memchunk.index = 0;
121 }
122
123 assert(u->memchunk.memblock);
124 assert(u->memchunk.length);
125 assert(u->memchunk.memblock->data);
126 assert(u->memchunk.memblock->length);
127 assert(u->memchunk.length % u->frame_size == 0);
128
129 if ((frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length / u->frame_size)) < 0) {
130 if (frames == -EAGAIN)
131 return;
132
133 if (frames == -EPIPE) {
134 xrun_recovery(u);
135 continue;
136 }
137
138 pa_log(__FILE__": snd_pcm_readi() failed: %s", pa_cstrerror(-frames));
139 return;
140 }
141
142 l = frames * u->frame_size;
143
144 post_memchunk = u->memchunk;
145 post_memchunk.length = l;
146
147 pa_source_post(u->source, &post_memchunk);
148
149 u->memchunk.index += l;
150 u->memchunk.length -= l;
151
152 if (u->memchunk.length == 0) {
153 pa_memblock_unref(u->memchunk.memblock);
154 u->memchunk.memblock = NULL;
155 u->memchunk.index = u->memchunk.length = 0;
156 }
157
158 break;
159 }
160 }
161
162 static void fdl_callback(void *userdata) {
163 struct userdata *u = userdata;
164 assert(u);
165
166 if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
167 xrun_recovery(u);
168
169 do_read(u);
170 }
171
172 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
173 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
174
175 assert(u && u->mixer_handle);
176
177 if (mask == SND_CTL_EVENT_MASK_REMOVE)
178 return 0;
179
180 if (mask & SND_CTL_EVENT_MASK_VALUE) {
181 if (u->source->get_hw_volume)
182 u->source->get_hw_volume(u->source);
183 if (u->source->get_hw_mute)
184 u->source->get_hw_mute(u->source);
185 pa_subscription_post(u->source->core,
186 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
187 u->source->index);
188 }
189
190 return 0;
191 }
192
193 static pa_usec_t source_get_latency_cb(pa_source *s) {
194 struct userdata *u = s->userdata;
195 snd_pcm_sframes_t frames;
196 assert(s && u && u->source);
197
198 if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
199 pa_log(__FILE__": failed to get delay");
200 s->get_latency = NULL;
201 return 0;
202 }
203
204 return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
205 }
206
207 static int source_get_hw_volume_cb(pa_source *s) {
208 struct userdata *u = s->userdata;
209 long vol;
210 int err;
211 int i;
212
213 assert(u && u->mixer_elem);
214
215 for (i = 0;i < s->hw_volume.channels;i++) {
216 assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
217
218 err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol);
219 if (err < 0)
220 goto fail;
221 s->hw_volume.values[i] =
222 (vol - u->hw_volume_min) * PA_VOLUME_NORM / (u->hw_volume_max - u->hw_volume_min);
223 }
224
225 return 0;
226
227 fail:
228 pa_log_error(__FILE__": Unable to read volume: %s", snd_strerror(err));
229 s->get_hw_volume = NULL;
230 s->set_hw_volume = NULL;
231 return -1;
232 }
233
234 static int source_set_hw_volume_cb(pa_source *s) {
235 struct userdata *u = s->userdata;
236 int err;
237 pa_volume_t vol;
238 int i;
239
240 assert(u && u->mixer_elem);
241
242 for (i = 0;i < s->hw_volume.channels;i++) {
243 assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
244
245 vol = s->hw_volume.values[i];
246
247 if (vol > PA_VOLUME_NORM)
248 vol = PA_VOLUME_NORM;
249
250 vol = vol * (u->hw_volume_max - u->hw_volume_min) /
251 PA_VOLUME_NORM + u->hw_volume_min;
252 err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, vol);
253 if (err < 0)
254 goto fail;
255 }
256
257 return 0;
258
259 fail:
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;
263 return -1;
264 }
265
266 static int source_get_hw_mute_cb(pa_source *s) {
267 struct userdata *u = s->userdata;
268 int err, sw;
269
270 assert(u && u->mixer_elem);
271
272 err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
273 if (err) {
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;
277 return -1;
278 }
279
280 s->hw_muted = !sw;
281
282 return 0;
283 }
284
285 static int source_set_hw_mute_cb(pa_source *s) {
286 struct userdata *u = s->userdata;
287 int err;
288
289 assert(u && u->mixer_elem);
290
291 err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_muted);
292 if (err) {
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;
296 return -1;
297 }
298
299 return 0;
300 }
301
302 int pa__init(pa_core *c, pa_module*m) {
303 pa_modargs *ma = NULL;
304 int ret = -1;
305 struct userdata *u = NULL;
306 const char *dev;
307 pa_sample_spec ss;
308 pa_channel_map map;
309 unsigned periods, fragsize;
310 snd_pcm_uframes_t period_size;
311 size_t frame_size;
312 snd_pcm_info_t *pcm_info = NULL;
313 int err;
314
315 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
316 pa_log(__FILE__": failed to parse module arguments");
317 goto fail;
318 }
319
320 ss = c->default_sample_spec;
321 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
322 pa_log(__FILE__": failed to parse sample specification");
323 goto fail;
324 }
325
326 frame_size = pa_frame_size(&ss);
327
328 /* Fix latency to 100ms */
329 periods = 12;
330 fragsize = pa_bytes_per_second(&ss)/128;
331
332 if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
333 pa_log(__FILE__": failed to parse buffer metrics");
334 goto fail;
335 }
336 period_size = fragsize/frame_size;
337
338 u = pa_xmalloc0(sizeof(struct userdata));
339 m->userdata = u;
340 u->module = m;
341
342 snd_config_update_free_global();
343 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) {
344 pa_log(__FILE__": Error opening PCM device %s: %s", dev, snd_strerror(err));
345 goto fail;
346 }
347
348 if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
349 (err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
350 pa_log(__FILE__": Error fetching PCM info: %s", snd_strerror(err));
351 goto fail;
352 }
353
354 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
355 pa_log(__FILE__": Failed to set hardware parameters: %s", snd_strerror(err));
356 goto fail;
357 }
358
359 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
360 pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err));
361 goto fail;
362 }
363
364 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
365 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture"))) {
366 snd_mixer_close(u->mixer_handle);
367 u->mixer_handle = NULL;
368 }
369
370 u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
371 assert(u->source);
372
373 u->source->userdata = u;
374 u->source->get_latency = source_get_latency_cb;
375 if (u->mixer_handle) {
376 assert(u->mixer_elem);
377 if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
378 int i;
379
380 for (i = 0;i < ss.channels;i++) {
381 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
382 break;
383 }
384
385 if (i == ss.channels) {
386 u->source->get_hw_volume = source_get_hw_volume_cb;
387 u->source->set_hw_volume = source_set_hw_volume_cb;
388 snd_mixer_selem_get_capture_volume_range(
389 u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
390 }
391 }
392 if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
393 u->source->get_hw_mute = source_get_hw_mute_cb;
394 u->source->set_hw_mute = source_set_hw_mute_cb;
395 }
396 }
397 pa_source_set_owner(u->source, m);
398 u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s' (%s)", dev, snd_pcm_info_get_name(pcm_info));
399
400 u->pcm_fdl = pa_alsa_fdlist_new();
401 assert(u->pcm_fdl);
402 if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
403 pa_log(__FILE__": failed to initialise file descriptor monitoring");
404 goto fail;
405 }
406
407 if (u->mixer_handle) {
408 u->mixer_fdl = pa_alsa_fdlist_new();
409 assert(u->mixer_fdl);
410 if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
411 pa_log(__FILE__": failed to initialise file descriptor monitoring");
412 goto fail;
413 }
414 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
415 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
416 }
417
418 u->frame_size = frame_size;
419 u->fragment_size = period_size * frame_size;
420
421 pa_log_info(__FILE__": using %u fragments of size %lu bytes.", periods, (long unsigned) u->fragment_size);
422
423 u->memchunk.memblock = NULL;
424 u->memchunk.index = u->memchunk.length = 0;
425
426 snd_pcm_start(u->pcm_handle);
427
428 ret = 0;
429
430 /* Get initial mixer settings */
431 if (u->source->get_hw_volume)
432 u->source->get_hw_volume(u->source);
433 if (u->source->get_hw_mute)
434 u->source->get_hw_mute(u->source);
435
436 finish:
437 if (ma)
438 pa_modargs_free(ma);
439
440 if (pcm_info)
441 snd_pcm_info_free(pcm_info);
442
443 return ret;
444
445 fail:
446
447 if (u)
448 pa__done(c, m);
449
450 goto finish;
451 }
452
453 void pa__done(pa_core *c, pa_module*m) {
454 struct userdata *u;
455 assert(c && m);
456
457 if (!(u = m->userdata))
458 return;
459
460 if (u->source) {
461 pa_source_disconnect(u->source);
462 pa_source_unref(u->source);
463 }
464
465 if (u->pcm_fdl)
466 pa_alsa_fdlist_free(u->pcm_fdl);
467 if (u->mixer_fdl)
468 pa_alsa_fdlist_free(u->mixer_fdl);
469
470 if (u->mixer_handle)
471 snd_mixer_close(u->mixer_handle);
472
473 if (u->pcm_handle) {
474 snd_pcm_drop(u->pcm_handle);
475 snd_pcm_close(u->pcm_handle);
476 }
477
478 if (u->memchunk.memblock)
479 pa_memblock_unref(u->memchunk.memblock);
480
481 pa_xfree(u);
482 }
483