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