]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-source.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-alsa-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30
31 #include <asoundlib.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/core.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/memchunk.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50
51 #include "alsa-util.h"
52 #include "module-alsa-source-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("ALSA Source")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE(
58 "source_name=<name for the source> "
59 "device=<ALSA device> "
60 "format=<sample format> "
61 "channels=<number of channels> "
62 "rate=<sample rate> "
63 "fragments=<number of fragments> "
64 "fragment_size=<fragment size> "
65 "channel_map=<channel map> "
66 "mmap=<enable memory mapping?>")
67
68 #define DEFAULT_DEVICE "default"
69
70 struct userdata {
71 pa_core *core;
72 pa_module *module;
73 pa_source *source;
74
75 pa_thread *thread;
76 pa_thread_mq thread_mq;
77 pa_rtpoll *rtpoll;
78
79 snd_pcm_t *pcm_handle;
80
81 pa_alsa_fdlist *mixer_fdl;
82 snd_mixer_t *mixer_handle;
83 snd_mixer_elem_t *mixer_elem;
84 long hw_volume_max, hw_volume_min;
85
86 size_t frame_size, fragment_size, hwbuf_size;
87 unsigned nfragments;
88
89 char *device_name;
90
91 int use_mmap;
92
93 pa_rtpoll_item *alsa_rtpoll_item;
94 };
95
96 static const char* const valid_modargs[] = {
97 "device",
98 "source_name",
99 "channels",
100 "rate",
101 "format",
102 "fragments",
103 "fragment_size",
104 "channel_map",
105 "mmap",
106 NULL
107 };
108
109 static int mmap_read(struct userdata *u) {
110 int work_done = 0;
111
112 pa_assert(u);
113 pa_source_assert_ref(u->source);
114
115 for (;;) {
116 snd_pcm_sframes_t n;
117 int err;
118 const snd_pcm_channel_area_t *areas;
119 snd_pcm_uframes_t offset, frames;
120 pa_memchunk chunk;
121 void *p;
122
123 if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
124
125 if (n == -EPIPE)
126 pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
127
128 if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
129 continue;
130
131 if (err == -EAGAIN)
132 return work_done;
133
134 pa_log("snd_pcm_avail_update: %s", snd_strerror(err));
135 return -1;
136 }
137
138 /* pa_log("Got request for %i samples", (int) n); */
139
140 if (n <= 0)
141 return work_done;
142
143 frames = n;
144
145 if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
146
147 if (err == -EPIPE)
148 pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
149
150 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
151 continue;
152
153 if (err == -EAGAIN)
154 return work_done;
155
156 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
157 return -1;
158 }
159
160 /* Check these are multiples of 8 bit */
161 pa_assert((areas[0].first & 7) == 0);
162 pa_assert((areas[0].step & 7)== 0);
163
164 /* We assume a single interleaved memory buffer */
165 pa_assert((areas[0].first >> 3) == 0);
166 pa_assert((areas[0].step >> 3) == u->frame_size);
167
168 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
169
170 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
171 chunk.length = pa_memblock_get_length(chunk.memblock);
172 chunk.index = 0;
173
174 pa_source_post(u->source, &chunk);
175
176 /* FIXME: Maybe we can do something to keep this memory block
177 * a little bit longer around? */
178 pa_memblock_unref_fixed(chunk.memblock);
179
180 if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
181
182 if (err == -EPIPE)
183 pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
184
185 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
186 continue;
187
188 if (err == -EAGAIN)
189 return work_done;
190
191 pa_log("Failed to write data to DSP: %s", snd_strerror(err));
192 return -1;
193 }
194
195 work_done = 1;
196
197 /* pa_log("wrote %i samples", (int) frames); */
198 }
199 }
200
201 static int unix_read(struct userdata *u) {
202 snd_pcm_status_t *status;
203 int work_done = 0;
204
205 snd_pcm_status_alloca(&status);
206
207 pa_assert(u);
208 pa_source_assert_ref(u->source);
209
210 for (;;) {
211 void *p;
212 snd_pcm_sframes_t t, k;
213 ssize_t l;
214 int err;
215 pa_memchunk chunk;
216
217 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
218 pa_log("Failed to query DSP status data: %s", snd_strerror(err));
219 return -1;
220 }
221
222 if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
223 pa_log_debug("Buffer overrun!");
224
225 l = snd_pcm_status_get_avail(status) * u->frame_size;
226
227 if (l <= 0)
228 return work_done;
229
230 chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
231
232 k = pa_memblock_get_length(chunk.memblock);
233
234 if (k > l)
235 k = l;
236
237 k = (k/u->frame_size)*u->frame_size;
238
239 p = pa_memblock_acquire(chunk.memblock);
240 t = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, k / u->frame_size);
241 pa_memblock_release(chunk.memblock);
242
243 /* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
244
245 pa_assert(t != 0);
246
247 if (t < 0) {
248 pa_memblock_unref(chunk.memblock);
249
250 if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
251 continue;
252
253 if (t == -EAGAIN) {
254 pa_log_debug("EAGAIN");
255 return work_done;
256 } else {
257 pa_log("Failed to read data from DSP: %s", snd_strerror(t));
258 return -1;
259 }
260 }
261
262 chunk.index = 0;
263 chunk.length = t * u->frame_size;
264
265 pa_source_post(u->source, &chunk);
266 pa_memblock_unref(chunk.memblock);
267
268 work_done = 1;
269
270 if (t * u->frame_size >= (unsigned) l)
271 return work_done;
272 }
273 }
274
275 static pa_usec_t source_get_latency(struct userdata *u) {
276 pa_usec_t r = 0;
277 snd_pcm_status_t *status;
278 snd_pcm_sframes_t frames = 0;
279 int err;
280
281 snd_pcm_status_alloca(&status);
282
283 pa_assert(u);
284 pa_assert(u->pcm_handle);
285
286 if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
287 pa_log("Failed to get delay: %s", snd_strerror(err));
288 else
289 frames = snd_pcm_status_get_delay(status);
290
291 if (frames > 0)
292 r = pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec);
293
294 return r;
295 }
296
297 static int build_pollfd(struct userdata *u) {
298 int err;
299 struct pollfd *pollfd;
300 int n;
301
302 pa_assert(u);
303 pa_assert(u->pcm_handle);
304
305 if ((n = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
306 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
307 return -1;
308 }
309
310 if (u->alsa_rtpoll_item)
311 pa_rtpoll_item_free(u->alsa_rtpoll_item);
312
313 u->alsa_rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, n);
314 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, NULL);
315
316 if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd, n)) < 0) {
317 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
318 return -1;
319 }
320
321 return 0;
322 }
323
324 static int suspend(struct userdata *u) {
325 pa_assert(u);
326 pa_assert(u->pcm_handle);
327
328 /* Let's suspend */
329 snd_pcm_close(u->pcm_handle);
330 u->pcm_handle = NULL;
331
332 if (u->alsa_rtpoll_item) {
333 pa_rtpoll_item_free(u->alsa_rtpoll_item);
334 u->alsa_rtpoll_item = NULL;
335 }
336
337 pa_log_info("Device suspended...");
338
339 return 0;
340 }
341
342 static int unsuspend(struct userdata *u) {
343 pa_sample_spec ss;
344 int err, b;
345 unsigned nfrags;
346 snd_pcm_uframes_t period_size;
347
348 pa_assert(u);
349 pa_assert(!u->pcm_handle);
350
351 pa_log_info("Trying resume...");
352
353 snd_config_update_free_global();
354 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
355 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
356 goto fail;
357 }
358
359 ss = u->source->sample_spec;
360 nfrags = u->nfragments;
361 period_size = u->fragment_size / u->frame_size;
362 b = u->use_mmap;
363
364 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
365 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
366 goto fail;
367 }
368
369 if (b != u->use_mmap) {
370 pa_log_warn("Resume failed, couldn't get original access mode.");
371 goto fail;
372 }
373
374 if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
375 pa_log_warn("Resume failed, couldn't restore original sample settings.");
376 goto fail;
377 }
378
379 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
380 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
381 goto fail;
382 }
383
384 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
385 pa_log("Failed to set software parameters: %s", snd_strerror(err));
386 goto fail;
387 }
388
389 if (build_pollfd(u) < 0)
390 goto fail;
391
392 snd_pcm_start(u->pcm_handle);
393
394 /* FIXME: We need to reload the volume somehow */
395
396 pa_log_info("Resumed successfully...");
397
398 return 0;
399
400 fail:
401 if (u->pcm_handle) {
402 snd_pcm_close(u->pcm_handle);
403 u->pcm_handle = NULL;
404 }
405
406 return -1;
407 }
408
409 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
410 struct userdata *u = PA_SOURCE(o)->userdata;
411
412 switch (code) {
413
414 case PA_SOURCE_MESSAGE_GET_LATENCY: {
415 pa_usec_t r = 0;
416
417 if (u->pcm_handle)
418 r = source_get_latency(u);
419
420 *((pa_usec_t*) data) = r;
421
422 return 0;
423 }
424
425 case PA_SOURCE_MESSAGE_SET_STATE:
426
427 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
428
429 case PA_SOURCE_SUSPENDED:
430 pa_assert(PA_SOURCE_OPENED(u->source->thread_info.state));
431
432 if (suspend(u) < 0)
433 return -1;
434
435 break;
436
437 case PA_SOURCE_IDLE:
438 case PA_SOURCE_RUNNING:
439
440 if (u->source->thread_info.state == PA_SOURCE_INIT) {
441 if (build_pollfd(u) < 0)
442 return -1;
443
444 snd_pcm_start(u->pcm_handle);
445 }
446
447 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
448 if (unsuspend(u) < 0)
449 return -1;
450 }
451
452 break;
453
454 case PA_SOURCE_UNLINKED:
455 case PA_SOURCE_INIT:
456 ;
457 }
458
459 break;
460 }
461
462 return pa_source_process_msg(o, code, data, offset, chunk);
463 }
464
465 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
466 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
467
468 pa_assert(u);
469 pa_assert(u->mixer_handle);
470
471 if (mask == SND_CTL_EVENT_MASK_REMOVE)
472 return 0;
473
474 if (mask & SND_CTL_EVENT_MASK_VALUE) {
475 pa_source_get_volume(u->source);
476 pa_source_get_mute(u->source);
477 }
478
479 return 0;
480 }
481
482 static int source_get_volume_cb(pa_source *s) {
483 struct userdata *u = s->userdata;
484 int err;
485 int i;
486
487 pa_assert(u);
488 pa_assert(u->mixer_elem);
489
490 for (i = 0; i < s->sample_spec.channels; i++) {
491 long set_vol, vol;
492
493 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
494
495 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol)) < 0)
496 goto fail;
497
498 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
499
500 /* Try to avoid superfluous volume changes */
501 if (set_vol != vol)
502 s->volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
503 }
504
505 return 0;
506
507 fail:
508 pa_log_error("Unable to read volume: %s", snd_strerror(err));
509
510 s->get_volume = NULL;
511 s->set_volume = NULL;
512 return -1;
513 }
514
515 static int source_set_volume_cb(pa_source *s) {
516 struct userdata *u = s->userdata;
517 int err;
518 int i;
519
520 pa_assert(u);
521 pa_assert(u->mixer_elem);
522
523 for (i = 0; i < s->sample_spec.channels; i++) {
524 long alsa_vol;
525 pa_volume_t vol;
526
527 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
528
529 vol = s->volume.values[i];
530
531 if (vol > PA_VOLUME_NORM)
532 vol = PA_VOLUME_NORM;
533
534 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
535
536 if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, alsa_vol)) < 0)
537 goto fail;
538 }
539
540 return 0;
541
542 fail:
543 pa_log_error("Unable to set volume: %s", snd_strerror(err));
544
545 s->get_volume = NULL;
546 s->set_volume = NULL;
547 return -1;
548 }
549
550 static int source_get_mute_cb(pa_source *s) {
551 struct userdata *u = s->userdata;
552 int err, sw;
553
554 pa_assert(u);
555 pa_assert(u->mixer_elem);
556
557 if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) {
558 pa_log_error("Unable to get switch: %s", snd_strerror(err));
559
560 s->get_mute = NULL;
561 s->set_mute = NULL;
562 return -1;
563 }
564
565 s->muted = !sw;
566
567 return 0;
568 }
569
570 static int source_set_mute_cb(pa_source *s) {
571 struct userdata *u = s->userdata;
572 int err;
573
574 pa_assert(u);
575 pa_assert(u->mixer_elem);
576
577 if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) {
578 pa_log_error("Unable to set switch: %s", snd_strerror(err));
579
580 s->get_mute = NULL;
581 s->set_mute = NULL;
582 return -1;
583 }
584
585 return 0;
586 }
587
588 static void thread_func(void *userdata) {
589 struct userdata *u = userdata;
590
591 pa_assert(u);
592
593 pa_log_debug("Thread starting up");
594
595 if (u->core->high_priority)
596 pa_make_realtime();
597
598 pa_thread_mq_install(&u->thread_mq);
599 pa_rtpoll_install(u->rtpoll);
600
601 for (;;) {
602 int ret;
603
604 /* Read some data and pass it to the sources */
605 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
606
607 if (u->use_mmap) {
608 if (mmap_read(u) < 0)
609 goto fail;
610
611 } else {
612 if (unix_read(u) < 0)
613 goto fail;
614 }
615 }
616
617 /* Hmm, nothing to do. Let's sleep */
618 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
619 goto fail;
620
621 if (ret == 0)
622 goto finish;
623
624 /* Tell ALSA about this and process its response */
625 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
626 struct pollfd *pollfd;
627 unsigned short revents = 0;
628 int err;
629 unsigned n;
630
631 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
632
633 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
634 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
635 goto fail;
636 }
637
638 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
639
640 if (revents & POLLERR)
641 pa_log_warn("Got POLLERR from ALSA");
642 if (revents & POLLNVAL)
643 pa_log_warn("Got POLLNVAL from ALSA");
644 if (revents & POLLHUP)
645 pa_log_warn("Got POLLHUP from ALSA");
646
647 /* Try to recover from this error */
648
649 switch (snd_pcm_state(u->pcm_handle)) {
650
651 case SND_PCM_STATE_XRUN:
652 if ((err = snd_pcm_recover(u->pcm_handle, -EPIPE, 1)) != 0) {
653 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
654 goto fail;
655 }
656 break;
657
658 case SND_PCM_STATE_SUSPENDED:
659 if ((err = snd_pcm_recover(u->pcm_handle, -ESTRPIPE, 1)) != 0) {
660 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
661 goto fail;
662 }
663 break;
664
665 default:
666
667 snd_pcm_drop(u->pcm_handle);
668
669 if ((err = snd_pcm_prepare(u->pcm_handle)) < 0) {
670 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
671 goto fail;
672 }
673 break;
674 }
675 }
676 }
677 }
678
679 fail:
680 /* If this was no regular exit from the loop we have to continue
681 * processing messages until we received PA_MESSAGE_SHUTDOWN */
682 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
683 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
684
685 finish:
686 pa_log_debug("Thread shutting down");
687 }
688
689 int pa__init(pa_module*m) {
690
691 pa_modargs *ma = NULL;
692 struct userdata *u = NULL;
693 char *dev;
694 pa_sample_spec ss;
695 pa_channel_map map;
696 unsigned nfrags, frag_size;
697 snd_pcm_uframes_t period_size;
698 size_t frame_size;
699 snd_pcm_info_t *pcm_info = NULL;
700 int err;
701 char *t;
702 const char *name;
703 char *name_buf = NULL;
704 int namereg_fail;
705 int use_mmap = 1, b;
706
707 snd_pcm_info_alloca(&pcm_info);
708
709 pa_assert(m);
710
711 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
712 pa_log("Failed to parse module arguments");
713 goto fail;
714 }
715
716 ss = m->core->default_sample_spec;
717 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
718 pa_log("Failed to parse sample specification");
719 goto fail;
720 }
721
722 frame_size = pa_frame_size(&ss);
723
724 nfrags = m->core->default_n_fragments;
725 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
726 if (frag_size <= 0)
727 frag_size = frame_size;
728
729 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
730 pa_log("Failed to parse buffer metrics");
731 goto fail;
732 }
733 period_size = frag_size/frame_size;
734
735 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
736 pa_log("Failed to parse mmap argument.");
737 goto fail;
738 }
739
740 u = pa_xnew0(struct userdata, 1);
741 u->core = m->core;
742 u->module = m;
743 m->userdata = u;
744 u->use_mmap = use_mmap;
745 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
746 u->rtpoll = pa_rtpoll_new();
747 u->alsa_rtpoll_item = NULL;
748 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
749
750 snd_config_update_free_global();
751
752 dev = pa_xstrdup(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE));
753
754 for (;;) {
755
756 if ((err = snd_pcm_open(&u->pcm_handle, dev, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
757 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
758 pa_xfree(dev);
759 goto fail;
760 }
761
762 b = use_mmap;
763 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
764
765 if (err == -EPERM) {
766 /* Hmm, some hw is very exotic, so we retry with plughw, if hw didn't work */
767
768 if (pa_startswith(dev, "hw:")) {
769 char *d = pa_sprintf_malloc("plughw:%s", dev+3);
770 pa_log_debug("Opening the device as '%s' didn't work, retrying with '%s'.", dev, d);
771 pa_xfree(dev);
772 dev = d;
773
774 snd_pcm_close(u->pcm_handle);
775 u->pcm_handle = NULL;
776 continue;
777 }
778 }
779
780 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
781 pa_xfree(dev);
782 goto fail;
783 }
784
785 break;
786 }
787
788 u->device_name = dev;
789
790 if (use_mmap && !b) {
791 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
792 u->use_mmap = use_mmap = b;
793 }
794
795 if (u->use_mmap)
796 pa_log_info("Successfully enabled mmap() mode.");
797
798 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
799 pa_log("Error fetching PCM info: %s", snd_strerror(err));
800 goto fail;
801 }
802
803 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
804 pa_log("Failed to set software parameters: %s", snd_strerror(err));
805 goto fail;
806 }
807
808 /* ALSA might tweak the sample spec, so recalculate the frame size */
809 frame_size = pa_frame_size(&ss);
810
811 if (ss.channels != map.channels)
812 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
813 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
814
815 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
816 pa_log("Error opening mixer: %s", snd_strerror(err));
817 else {
818
819 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
820 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", NULL))) {
821 snd_mixer_close(u->mixer_handle);
822 u->mixer_handle = NULL;
823 }
824 }
825
826 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
827 namereg_fail = 1;
828 else {
829 name = name_buf = pa_sprintf_malloc("alsa_input.%s", dev);
830 namereg_fail = 0;
831 }
832
833 u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
834 pa_xfree(name_buf);
835
836 if (!u->source) {
837 pa_log("Failed to create source object");
838 goto fail;
839 }
840
841 u->source->parent.process_msg = source_process_msg;
842 u->source->userdata = u;
843
844 pa_source_set_module(u->source, m);
845 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
846 pa_source_set_rtpoll(u->source, u->rtpoll);
847 pa_source_set_description(u->source, t = pa_sprintf_malloc(
848 "ALSA PCM on %s (%s)%s",
849 dev,
850 snd_pcm_info_get_name(pcm_info),
851 use_mmap ? " via DMA" : ""));
852 pa_xfree(t);
853
854 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL;
855
856 u->frame_size = frame_size;
857 u->fragment_size = frag_size = period_size * frame_size;
858 u->nfragments = nfrags;
859 u->hwbuf_size = u->fragment_size * nfrags;
860
861 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
862
863 if (u->mixer_handle) {
864 pa_assert(u->mixer_elem);
865
866 if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
867 int i;
868
869 for (i = 0;i < ss.channels;i++) {
870 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
871 break;
872 }
873
874 if (i == ss.channels) {
875 u->source->get_volume = source_get_volume_cb;
876 u->source->set_volume = source_set_volume_cb;
877 snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
878 }
879 }
880
881 if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
882 u->source->get_mute = source_get_mute_cb;
883 u->source->set_mute = source_set_mute_cb;
884 }
885
886 u->mixer_fdl = pa_alsa_fdlist_new();
887
888 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
889 pa_log("failed to initialise file descriptor monitoring");
890 goto fail;
891 }
892
893 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
894 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
895 } else
896 u->mixer_fdl = NULL;
897
898 if (!(u->thread = pa_thread_new(thread_func, u))) {
899 pa_log("Failed to create thread.");
900 goto fail;
901 }
902 /* Get initial mixer settings */
903 if (u->source->get_volume)
904 u->source->get_volume(u->source);
905 if (u->source->get_mute)
906 u->source->get_mute(u->source);
907
908 pa_source_put(u->source);
909
910 pa_modargs_free(ma);
911
912 return 0;
913
914 fail:
915
916 if (ma)
917 pa_modargs_free(ma);
918
919 pa__done(m);
920
921 return -1;
922 }
923
924 void pa__done(pa_module*m) {
925 struct userdata *u;
926
927 pa_assert(m);
928
929 if (!(u = m->userdata))
930 return;
931
932 if (u->source)
933 pa_source_unlink(u->source);
934
935 if (u->thread) {
936 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
937 pa_thread_free(u->thread);
938 }
939
940 pa_thread_mq_done(&u->thread_mq);
941
942 if (u->source)
943 pa_source_unref(u->source);
944
945 if (u->alsa_rtpoll_item)
946 pa_rtpoll_item_free(u->alsa_rtpoll_item);
947
948 if (u->rtpoll)
949 pa_rtpoll_free(u->rtpoll);
950
951 if (u->mixer_fdl)
952 pa_alsa_fdlist_free(u->mixer_fdl);
953
954 if (u->mixer_handle)
955 snd_mixer_close(u->mixer_handle);
956
957 if (u->pcm_handle) {
958 snd_pcm_drop(u->pcm_handle);
959 snd_pcm_close(u->pcm_handle);
960 }
961
962 pa_xfree(u->device_name);
963 pa_xfree(u);
964
965 snd_config_update_free_global();
966 }