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