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