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