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