]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-source.c
extend rtpoll API to allow registration of arbitray functions to be executed in the...
[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_SUSPENDED) {
441 if (unsuspend(u) < 0)
442 return -1;
443 }
444
445 break;
446
447 case PA_SOURCE_UNLINKED:
448 case PA_SOURCE_INIT:
449 ;
450 }
451
452 break;
453 }
454
455 return pa_source_process_msg(o, code, data, offset, chunk);
456 }
457
458 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
459 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
460
461 pa_assert(u);
462 pa_assert(u->mixer_handle);
463
464 if (mask == SND_CTL_EVENT_MASK_REMOVE)
465 return 0;
466
467 if (mask & SND_CTL_EVENT_MASK_VALUE) {
468 pa_source_get_volume(u->source);
469 pa_source_get_mute(u->source);
470 }
471
472 return 0;
473 }
474
475 static int source_get_volume_cb(pa_source *s) {
476 struct userdata *u = s->userdata;
477 int err;
478 int i;
479
480 pa_assert(u);
481 pa_assert(u->mixer_elem);
482
483 for (i = 0; i < s->sample_spec.channels; i++) {
484 long set_vol, vol;
485
486 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
487
488 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, i, &vol)) < 0)
489 goto fail;
490
491 set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
492
493 /* Try to avoid superfluous volume changes */
494 if (set_vol != vol)
495 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));
496 }
497
498 return 0;
499
500 fail:
501 pa_log_error("Unable to read volume: %s", snd_strerror(err));
502
503 s->get_volume = NULL;
504 s->set_volume = NULL;
505 return -1;
506 }
507
508 static int source_set_volume_cb(pa_source *s) {
509 struct userdata *u = s->userdata;
510 int err;
511 int i;
512
513 pa_assert(u);
514 pa_assert(u->mixer_elem);
515
516 for (i = 0; i < s->sample_spec.channels; i++) {
517 long alsa_vol;
518 pa_volume_t vol;
519
520 pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
521
522 vol = s->volume.values[i];
523
524 if (vol > PA_VOLUME_NORM)
525 vol = PA_VOLUME_NORM;
526
527 alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
528
529 if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, i, alsa_vol)) < 0)
530 goto fail;
531 }
532
533 return 0;
534
535 fail:
536 pa_log_error("Unable to set volume: %s", snd_strerror(err));
537
538 s->get_volume = NULL;
539 s->set_volume = NULL;
540 return -1;
541 }
542
543 static int source_get_mute_cb(pa_source *s) {
544 struct userdata *u = s->userdata;
545 int err, sw;
546
547 pa_assert(u);
548 pa_assert(u->mixer_elem);
549
550 if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) {
551 pa_log_error("Unable to get switch: %s", snd_strerror(err));
552
553 s->get_mute = NULL;
554 s->set_mute = NULL;
555 return -1;
556 }
557
558 s->muted = !sw;
559
560 return 0;
561 }
562
563 static int source_set_mute_cb(pa_source *s) {
564 struct userdata *u = s->userdata;
565 int err;
566
567 pa_assert(u);
568 pa_assert(u->mixer_elem);
569
570 if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) {
571 pa_log_error("Unable to set switch: %s", snd_strerror(err));
572
573 s->get_mute = NULL;
574 s->set_mute = NULL;
575 return -1;
576 }
577
578 return 0;
579 }
580
581 static void thread_func(void *userdata) {
582 struct userdata *u = userdata;
583
584 pa_assert(u);
585
586 pa_log_debug("Thread starting up");
587
588 if (u->core->high_priority)
589 pa_make_realtime();
590
591 pa_thread_mq_install(&u->thread_mq);
592 pa_rtpoll_install(u->rtpoll);
593
594 if (build_pollfd(u) < 0)
595 goto fail;
596
597 snd_pcm_start(u->pcm_handle);
598
599 for (;;) {
600 int ret;
601
602 /* Read some data and pass it to the sources */
603 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
604
605 if (u->use_mmap) {
606 if (mmap_read(u) < 0)
607 goto fail;
608
609 } else {
610 if (unix_read(u) < 0)
611 goto fail;
612 }
613 }
614
615 /* Now give the source outputs some to time to process their data */
616 if ((ret = pa_source_process_outputs(u->source)) < 0)
617 goto fail;
618 if (ret > 0)
619 continue;
620
621 /* Check whether there is a message for us to process */
622 if ((ret = pa_thread_mq_process(&u->thread_mq) < 0))
623 goto finish;
624 if (ret > 0)
625 continue;
626
627 /* Hmm, nothing to do. Let's sleep */
628 if (pa_rtpoll_run(u->rtpoll, 1) < 0) {
629 pa_log("poll() failed: %s", pa_cstrerror(errno));
630 goto fail;
631 }
632
633 /* Tell ALSA about this and process its response */
634 if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
635 struct pollfd *pollfd;
636 unsigned short revents = 0;
637 int err;
638 unsigned n;
639
640 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
641
642 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
643 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
644 goto fail;
645 }
646
647 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
648 if (revents & POLLERR)
649 pa_log_warn("Got POLLERR from ALSA");
650 if (revents & POLLNVAL)
651 pa_log_warn("Got POLLNVAL from ALSA");
652 if (revents & POLLHUP)
653 pa_log_warn("Got POLLHUP from ALSA");
654
655 goto fail;
656 }
657 }
658 }
659
660 fail:
661 /* We have to continue processing messages until we receive the
662 * SHUTDOWN message */
663 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
664 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
665
666 finish:
667 pa_log_debug("Thread shutting down");
668 }
669
670 int pa__init(pa_module*m) {
671
672 pa_modargs *ma = NULL;
673 struct userdata *u = NULL;
674 const char *dev;
675 pa_sample_spec ss;
676 pa_channel_map map;
677 unsigned nfrags, frag_size;
678 snd_pcm_uframes_t period_size;
679 size_t frame_size;
680 snd_pcm_info_t *pcm_info = NULL;
681 int err;
682 char *t;
683 const char *name;
684 char *name_buf = NULL;
685 int namereg_fail;
686 int use_mmap = 1, b;
687
688 snd_pcm_info_alloca(&pcm_info);
689
690 pa_assert(m);
691
692 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
693 pa_log("Failed to parse module arguments");
694 goto fail;
695 }
696
697 ss = m->core->default_sample_spec;
698 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
699 pa_log("Failed to parse sample specification");
700 goto fail;
701 }
702
703 frame_size = pa_frame_size(&ss);
704
705 nfrags = m->core->default_n_fragments;
706 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
707 if (frag_size <= 0)
708 frag_size = frame_size;
709
710 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
711 pa_log("Failed to parse buffer metrics");
712 goto fail;
713 }
714 period_size = frag_size/frame_size;
715
716 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
717 pa_log("Failed to parse mmap argument.");
718 goto fail;
719 }
720
721 u = pa_xnew0(struct userdata, 1);
722 u->core = m->core;
723 u->module = m;
724 m->userdata = u;
725 u->use_mmap = use_mmap;
726 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
727 u->rtpoll = pa_rtpoll_new();
728 u->alsa_rtpoll_item = NULL;
729 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
730
731 snd_config_update_free_global();
732 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) {
733 pa_log("Error opening PCM device %s: %s", dev, snd_strerror(err));
734 goto fail;
735 }
736
737 u->device_name = pa_xstrdup(dev);
738
739 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
740 pa_log("Error fetching PCM info: %s", snd_strerror(err));
741 goto fail;
742 }
743
744 b = use_mmap;
745 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
746 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
747 goto fail;
748 }
749
750 if (use_mmap && !b) {
751 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
752 u->use_mmap = use_mmap = b;
753 }
754
755 if (u->use_mmap)
756 pa_log_info("Successfully enabled mmap() mode.");
757
758 if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
759 pa_log("Failed to set software parameters: %s", snd_strerror(err));
760 goto fail;
761 }
762
763 /* ALSA might tweak the sample spec, so recalculate the frame size */
764 frame_size = pa_frame_size(&ss);
765
766 if (ss.channels != map.channels)
767 /* Seems ALSA didn't like the channel number, so let's fix the channel map */
768 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
769
770 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
771 pa_log("Error opening mixer: %s", snd_strerror(err));
772 else {
773
774 if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
775 !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", NULL))) {
776 snd_mixer_close(u->mixer_handle);
777 u->mixer_handle = NULL;
778 }
779 }
780
781 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
782 namereg_fail = 1;
783 else {
784 name = name_buf = pa_sprintf_malloc("alsa_input.%s", dev);
785 namereg_fail = 0;
786 }
787
788 u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
789 pa_xfree(name_buf);
790
791 if (!u->source) {
792 pa_log("Failed to create source object");
793 goto fail;
794 }
795
796 u->source->parent.process_msg = source_process_msg;
797 u->source->userdata = u;
798
799 pa_source_set_module(u->source, m);
800 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
801 pa_source_set_rtpoll(u->source, u->rtpoll);
802 pa_source_set_description(u->source, t = pa_sprintf_malloc(
803 "ALSA PCM on %s (%s)%s",
804 dev,
805 snd_pcm_info_get_name(pcm_info),
806 use_mmap ? " via DMA" : ""));
807 pa_xfree(t);
808
809 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_CAN_SUSPEND|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL;
810
811 u->frame_size = frame_size;
812 u->fragment_size = frag_size = period_size * frame_size;
813 u->nfragments = nfrags;
814 u->hwbuf_size = u->fragment_size * nfrags;
815
816 pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
817
818 if (u->mixer_handle) {
819 pa_assert(u->mixer_elem);
820
821 if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
822 int i;
823
824 for (i = 0;i < ss.channels;i++) {
825 if (!snd_mixer_selem_has_capture_channel(u->mixer_elem, i))
826 break;
827 }
828
829 if (i == ss.channels) {
830 u->source->get_volume = source_get_volume_cb;
831 u->source->set_volume = source_set_volume_cb;
832 snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
833 }
834 }
835
836 if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
837 u->source->get_mute = source_get_mute_cb;
838 u->source->set_mute = source_set_mute_cb;
839 }
840
841 u->mixer_fdl = pa_alsa_fdlist_new();
842
843 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
844 pa_log("failed to initialise file descriptor monitoring");
845 goto fail;
846 }
847
848 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
849 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
850 } else
851 u->mixer_fdl = NULL;
852
853 if (!(u->thread = pa_thread_new(thread_func, u))) {
854 pa_log("Failed to create thread.");
855 goto fail;
856 }
857 /* Get initial mixer settings */
858 if (u->source->get_volume)
859 u->source->get_volume(u->source);
860 if (u->source->get_mute)
861 u->source->get_mute(u->source);
862
863 pa_source_put(u->source);
864
865 pa_modargs_free(ma);
866
867 return 0;
868
869 fail:
870
871 if (ma)
872 pa_modargs_free(ma);
873
874 pa__done(m);
875
876 return -1;
877 }
878
879 void pa__done(pa_module*m) {
880 struct userdata *u;
881
882 pa_assert(m);
883
884 if (!(u = m->userdata))
885 return;
886
887 if (u->source)
888 pa_source_unlink(u->source);
889
890 if (u->thread) {
891 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
892 pa_thread_free(u->thread);
893 }
894
895 pa_thread_mq_done(&u->thread_mq);
896
897 if (u->source)
898 pa_source_unref(u->source);
899
900 if (u->alsa_rtpoll_item)
901 pa_rtpoll_item_free(u->alsa_rtpoll_item);
902
903 if (u->rtpoll)
904 pa_rtpoll_free(u->rtpoll);
905
906 if (u->mixer_fdl)
907 pa_alsa_fdlist_free(u->mixer_fdl);
908
909 if (u->mixer_handle)
910 snd_mixer_close(u->mixer_handle);
911
912 if (u->pcm_handle) {
913 snd_pcm_drop(u->pcm_handle);
914 snd_pcm_close(u->pcm_handle);
915 }
916
917 pa_xfree(u->device_name);
918 pa_xfree(u);
919
920 snd_config_update_free_global();
921 }
922