]> code.delx.au - pulseaudio/blob - src/modules/module-alsa-source.c
Merge commit 'flameeyes/autoconf-2.62'
[pulseaudio] / src / modules / module-alsa-source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #include <asoundlib.h>
30
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
33 #endif
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
37 #include <pulse/timeval.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/core.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/memchunk.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/sample-util.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/macro.h>
49 #include <pulsecore/thread.h>
50 #include <pulsecore/core-error.h>
51 #include <pulsecore/thread-mq.h>
52 #include <pulsecore/rtpoll.h>
53 #include <pulsecore/time-smoother.h>
54 #include <pulsecore/rtclock.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_LOAD_ONCE(FALSE);
63 PA_MODULE_USAGE(
64 "source_name=<name for the source> "
65 "device=<ALSA device> "
66 "device_id=<ALSA card index> "
67 "format=<sample format> "
68 "rate=<sample rate> "
69 "channels=<number of channels> "
70 "channel_map=<channel map> "
71 "fragments=<number of fragments> "
72 "fragment_size=<fragment size> "
73 "mmap=<enable memory mapping?> "
74 "tsched=<enable system timer based scheduling mode?> "
75 "tsched_buffer_size=<buffer size when using timer based scheduling> "
76 "tsched_buffer_watermark=<upper fill watermark>");
77
78 static const char* const valid_modargs[] = {
79 "source_name",
80 "device",
81 "device_id",
82 "format",
83 "rate",
84 "channels",
85 "channel_map",
86 "fragments",
87 "fragment_size",
88 "mmap",
89 "tsched",
90 "tsched_buffer_size",
91 "tsched_buffer_watermark",
92 NULL
93 };
94
95 #define DEFAULT_DEVICE "default"
96 #define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC) /* 2s */
97 #define DEFAULT_TSCHED_WATERMARK_USEC (20*PA_USEC_PER_MSEC) /* 20ms */
98 #define TSCHED_MIN_SLEEP_USEC (3*PA_USEC_PER_MSEC) /* 3ms */
99 #define TSCHED_MIN_WAKEUP_USEC (3*PA_USEC_PER_MSEC) /* 3ms */
100
101 struct userdata {
102 pa_core *core;
103 pa_module *module;
104 pa_source *source;
105
106 pa_thread *thread;
107 pa_thread_mq thread_mq;
108 pa_rtpoll *rtpoll;
109
110 snd_pcm_t *pcm_handle;
111
112 pa_alsa_fdlist *mixer_fdl;
113 snd_mixer_t *mixer_handle;
114 snd_mixer_elem_t *mixer_elem;
115 long hw_volume_max, hw_volume_min;
116 long hw_dB_max, hw_dB_min;
117 pa_bool_t hw_dB_supported;
118 pa_bool_t mixer_seperate_channels;
119
120 pa_cvolume hardware_volume;
121
122 size_t frame_size, fragment_size, hwbuf_size, tsched_watermark;
123 unsigned nfragments;
124
125 char *device_name;
126
127 pa_bool_t use_mmap, use_tsched;
128
129 pa_rtpoll_item *alsa_rtpoll_item;
130
131 snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
132
133 pa_smoother *smoother;
134 int64_t frame_index;
135
136 snd_pcm_sframes_t hwbuf_unused_frames;
137 };
138
139 static void fix_tsched_watermark(struct userdata *u) {
140 size_t max_use;
141 size_t min_sleep, min_wakeup;
142 pa_assert(u);
143
144 max_use = u->hwbuf_size - u->hwbuf_unused_frames * u->frame_size;
145
146 min_sleep = pa_usec_to_bytes(TSCHED_MIN_SLEEP_USEC, &u->source->sample_spec);
147 min_wakeup = pa_usec_to_bytes(TSCHED_MIN_WAKEUP_USEC, &u->source->sample_spec);
148
149 if (min_sleep > max_use/2)
150 min_sleep = pa_frame_align(max_use/2, &u->source->sample_spec);
151 if (min_sleep < u->frame_size)
152 min_sleep = u->frame_size;
153
154 if (min_wakeup > max_use/2)
155 min_wakeup = pa_frame_align(max_use/2, &u->source->sample_spec);
156 if (min_wakeup < u->frame_size)
157 min_wakeup = u->frame_size;
158
159 if (u->tsched_watermark > max_use-min_sleep)
160 u->tsched_watermark = max_use-min_sleep;
161
162 if (u->tsched_watermark < min_wakeup)
163 u->tsched_watermark = min_wakeup;
164 }
165
166 static pa_usec_t hw_sleep_time(struct userdata *u, pa_usec_t *sleep_usec, pa_usec_t*process_usec) {
167 pa_usec_t wm, usec;
168
169 pa_assert(u);
170
171 usec = pa_source_get_requested_latency_within_thread(u->source);
172
173 if (usec == (pa_usec_t) -1)
174 usec = pa_bytes_to_usec(u->hwbuf_size, &u->source->sample_spec);
175
176 /* pa_log_debug("hw buffer time: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC)); */
177
178 wm = pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec);
179
180 if (usec >= wm) {
181 *sleep_usec = usec - wm;
182 *process_usec = wm;
183 } else
184 *process_usec = *sleep_usec = usec /= 2;
185
186 /* pa_log_debug("after watermark: %u ms", (unsigned) (*sleep_usec / PA_USEC_PER_MSEC)); */
187
188 return usec;
189 }
190
191 static int try_recover(struct userdata *u, const char *call, int err) {
192 pa_assert(u);
193 pa_assert(call);
194 pa_assert(err < 0);
195
196 pa_log_debug("%s: %s", call, snd_strerror(err));
197
198 pa_assert(err != -EAGAIN);
199
200 if (err == -EPIPE)
201 pa_log_debug("%s: Buffer overrun!", call);
202
203 if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0) {
204 snd_pcm_start(u->pcm_handle);
205 return 0;
206 }
207
208 pa_log("%s: %s", call, snd_strerror(err));
209 return -1;
210 }
211
212 static size_t check_left_to_record(struct userdata *u, snd_pcm_sframes_t n) {
213 size_t left_to_record;
214
215 if (n*u->frame_size < u->hwbuf_size)
216 left_to_record = u->hwbuf_size - (n*u->frame_size);
217 else
218 left_to_record = 0;
219
220 if (left_to_record > 0) {
221 /* pa_log_debug("%0.2f ms left to record", (double) pa_bytes_to_usec(left_to_record, &u->source->sample_spec) / PA_USEC_PER_MSEC); */
222 } else {
223 pa_log_info("Overrun!");
224
225 if (u->use_tsched) {
226 size_t old_watermark = u->tsched_watermark;
227
228 u->tsched_watermark *= 2;
229 fix_tsched_watermark(u);
230
231 if (old_watermark != u->tsched_watermark)
232 pa_log_notice("Increasing wakeup watermark to %0.2f ms",
233 (double) pa_bytes_to_usec(u->tsched_watermark, &u->source->sample_spec) / PA_USEC_PER_MSEC);
234 }
235 }
236
237 return left_to_record;
238 }
239
240 static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec) {
241 int work_done = 0;
242 pa_usec_t max_sleep_usec = 0, process_usec = 0;
243 size_t left_to_record;
244
245 pa_assert(u);
246 pa_source_assert_ref(u->source);
247
248 if (u->use_tsched)
249 hw_sleep_time(u, &max_sleep_usec, &process_usec);
250
251 for (;;) {
252 snd_pcm_sframes_t n;
253 int r;
254
255 snd_pcm_hwsync(u->pcm_handle);
256
257 if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
258
259 if ((r = try_recover(u, "snd_pcm_avail_update", n)) == 0)
260 continue;
261
262 return r;
263 }
264
265 left_to_record = check_left_to_record(u, n);
266
267 if (u->use_tsched)
268 if (pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > process_usec+max_sleep_usec/2)
269 break;
270
271 if (PA_UNLIKELY(n <= 0))
272 break;
273
274 for (;;) {
275 int err;
276 const snd_pcm_channel_area_t *areas;
277 snd_pcm_uframes_t offset, frames = (snd_pcm_uframes_t) n;
278 pa_memchunk chunk;
279 void *p;
280
281 /* pa_log_debug("%lu frames to read", (unsigned long) frames); */
282
283 if (PA_UNLIKELY((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0)) {
284
285 if ((r = try_recover(u, "snd_pcm_mmap_begin", err)) == 0)
286 continue;
287
288 return r;
289 }
290
291 /* Make sure that if these memblocks need to be copied they will fit into one slot */
292 if (frames > pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size)
293 frames = pa_mempool_block_size_max(u->source->core->mempool)/u->frame_size;
294
295 /* Check these are multiples of 8 bit */
296 pa_assert((areas[0].first & 7) == 0);
297 pa_assert((areas[0].step & 7)== 0);
298
299 /* We assume a single interleaved memory buffer */
300 pa_assert((areas[0].first >> 3) == 0);
301 pa_assert((areas[0].step >> 3) == u->frame_size);
302
303 p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
304
305 chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, TRUE);
306 chunk.length = pa_memblock_get_length(chunk.memblock);
307 chunk.index = 0;
308
309 pa_source_post(u->source, &chunk);
310 pa_memblock_unref_fixed(chunk.memblock);
311
312 if (PA_UNLIKELY((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
313
314 if ((r = try_recover(u, "snd_pcm_mmap_commit", err)) == 0)
315 continue;
316
317 return r;
318 }
319
320 work_done = 1;
321
322 u->frame_index += frames;
323
324 /* pa_log_debug("read %lu frames", (unsigned long) frames); */
325
326 if (frames >= (snd_pcm_uframes_t) n)
327 break;
328
329 n -= frames;
330 }
331 }
332
333 *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec;
334 return work_done;
335 }
336
337 static int unix_read(struct userdata *u, pa_usec_t *sleep_usec) {
338 int work_done = 0;
339 pa_usec_t max_sleep_usec = 0, process_usec = 0;
340 size_t left_to_record;
341
342 pa_assert(u);
343 pa_source_assert_ref(u->source);
344
345 if (u->use_tsched)
346 hw_sleep_time(u, &max_sleep_usec, &process_usec);
347
348 for (;;) {
349 snd_pcm_sframes_t n;
350 int r;
351
352 snd_pcm_hwsync(u->pcm_handle);
353
354 if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
355
356 if ((r = try_recover(u, "snd_pcm_avail_update", n)) == 0)
357 continue;
358
359 return r;
360 }
361
362 left_to_record = check_left_to_record(u, n);
363
364 if (u->use_tsched)
365 if (pa_bytes_to_usec(left_to_record, &u->source->sample_spec) > process_usec+max_sleep_usec/2)
366 break;
367
368 if (PA_UNLIKELY(n <= 0))
369 return work_done;
370
371 for (;;) {
372 void *p;
373 snd_pcm_sframes_t frames;
374 pa_memchunk chunk;
375
376 chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
377
378 frames = pa_memblock_get_length(chunk.memblock) / u->frame_size;
379
380 if (frames > n)
381 frames = n;
382
383 /* pa_log_debug("%lu frames to read", (unsigned long) n); */
384
385 p = pa_memblock_acquire(chunk.memblock);
386 frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, frames);
387 pa_memblock_release(chunk.memblock);
388
389 pa_assert(frames != 0);
390
391 if (PA_UNLIKELY(frames < 0)) {
392 pa_memblock_unref(chunk.memblock);
393
394 if ((r = try_recover(u, "snd_pcm_readi", n)) == 0)
395 continue;
396
397 return r;
398 }
399
400 chunk.index = 0;
401 chunk.length = frames * u->frame_size;
402
403 pa_source_post(u->source, &chunk);
404 pa_memblock_unref(chunk.memblock);
405
406 work_done = 1;
407
408 u->frame_index += frames;
409
410 /* pa_log_debug("read %lu frames", (unsigned long) frames); */
411
412 if (frames >= n)
413 break;
414
415 n -= frames;
416 }
417 }
418
419 *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec;
420 return work_done;
421 }
422
423 static void update_smoother(struct userdata *u) {
424 snd_pcm_sframes_t delay = 0;
425 int64_t frames;
426 int err;
427 pa_usec_t now1, now2;
428
429 pa_assert(u);
430 pa_assert(u->pcm_handle);
431
432 /* Let's update the time smoother */
433
434 snd_pcm_hwsync(u->pcm_handle);
435 snd_pcm_avail_update(u->pcm_handle);
436
437 if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) {
438 pa_log_warn("Failed to get delay: %s", snd_strerror(err));
439 return;
440 }
441
442 frames = u->frame_index + delay;
443
444 now1 = pa_rtclock_usec();
445 now2 = pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec);
446
447 pa_smoother_put(u->smoother, now1, now2);
448 }
449
450 static pa_usec_t source_get_latency(struct userdata *u) {
451 pa_usec_t r = 0;
452 int64_t delay;
453 pa_usec_t now1, now2;
454
455 pa_assert(u);
456
457 now1 = pa_rtclock_usec();
458 now2 = pa_smoother_get(u->smoother, now1);
459
460 delay = (int64_t) now2 - pa_bytes_to_usec(u->frame_index * u->frame_size, &u->source->sample_spec);
461
462 if (delay > 0)
463 r = (pa_usec_t) delay;
464
465 return r;
466 }
467
468 static int build_pollfd(struct userdata *u) {
469 pa_assert(u);
470 pa_assert(u->pcm_handle);
471
472 if (u->alsa_rtpoll_item)
473 pa_rtpoll_item_free(u->alsa_rtpoll_item);
474
475 if (!(u->alsa_rtpoll_item = pa_alsa_build_pollfd(u->pcm_handle, u->rtpoll)))
476 return -1;
477
478 return 0;
479 }
480
481 static int suspend(struct userdata *u) {
482 pa_assert(u);
483 pa_assert(u->pcm_handle);
484
485 pa_smoother_pause(u->smoother, pa_rtclock_usec());
486
487 /* Let's suspend */
488 snd_pcm_close(u->pcm_handle);
489 u->pcm_handle = NULL;
490
491 if (u->alsa_rtpoll_item) {
492 pa_rtpoll_item_free(u->alsa_rtpoll_item);
493 u->alsa_rtpoll_item = NULL;
494 }
495
496 pa_log_info("Device suspended...");
497
498 return 0;
499 }
500
501 static int update_sw_params(struct userdata *u) {
502 snd_pcm_uframes_t avail_min;
503 int err;
504
505 pa_assert(u);
506
507 /* Use the full buffer if noone asked us for anything specific */
508 u->hwbuf_unused_frames = 0;
509
510 if (u->use_tsched) {
511 pa_usec_t latency;
512
513 if ((latency = pa_source_get_requested_latency_within_thread(u->source)) != (pa_usec_t) -1) {
514 size_t b;
515
516 pa_log_debug("latency set to %0.2f", (double) latency / PA_USEC_PER_MSEC);
517
518 b = pa_usec_to_bytes(latency, &u->source->sample_spec);
519
520 /* We need at least one sample in our buffer */
521
522 if (PA_UNLIKELY(b < u->frame_size))
523 b = u->frame_size;
524
525 u->hwbuf_unused_frames =
526 PA_LIKELY(b < u->hwbuf_size) ?
527 ((u->hwbuf_size - b) / u->frame_size) : 0;
528
529 fix_tsched_watermark(u);
530 }
531 }
532
533 pa_log_debug("hwbuf_unused_frames=%lu", (unsigned long) u->hwbuf_unused_frames);
534
535 avail_min = 1;
536
537 if (u->use_tsched) {
538 pa_usec_t sleep_usec, process_usec;
539
540 hw_sleep_time(u, &sleep_usec, &process_usec);
541 avail_min += pa_usec_to_bytes(sleep_usec, &u->source->sample_spec);
542 }
543
544 pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min);
545
546 if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
547 pa_log("Failed to set software parameters: %s", snd_strerror(err));
548 return err;
549 }
550
551 return 0;
552 }
553
554 static int unsuspend(struct userdata *u) {
555 pa_sample_spec ss;
556 int err;
557 pa_bool_t b, d;
558 unsigned nfrags;
559 snd_pcm_uframes_t period_size;
560
561 pa_assert(u);
562 pa_assert(!u->pcm_handle);
563
564 pa_log_info("Trying resume...");
565
566 snd_config_update_free_global();
567 if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
568 pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
569 goto fail;
570 }
571
572 ss = u->source->sample_spec;
573 nfrags = u->nfragments;
574 period_size = u->fragment_size / u->frame_size;
575 b = u->use_mmap;
576 d = u->use_tsched;
577
578 if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
579 pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
580 goto fail;
581 }
582
583 if (b != u->use_mmap || d != u->use_tsched) {
584 pa_log_warn("Resume failed, couldn't get original access mode.");
585 goto fail;
586 }
587
588 if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
589 pa_log_warn("Resume failed, couldn't restore original sample settings.");
590 goto fail;
591 }
592
593 if (nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
594 pa_log_warn("Resume failed, couldn't restore original fragment settings.");
595 goto fail;
596 }
597
598 if (update_sw_params(u) < 0)
599 goto fail;
600
601 if (build_pollfd(u) < 0)
602 goto fail;
603
604 /* FIXME: We need to reload the volume somehow */
605
606 snd_pcm_start(u->pcm_handle);
607 pa_smoother_resume(u->smoother, pa_rtclock_usec());
608
609 pa_log_info("Resumed successfully...");
610
611 return 0;
612
613 fail:
614 if (u->pcm_handle) {
615 snd_pcm_close(u->pcm_handle);
616 u->pcm_handle = NULL;
617 }
618
619 return -1;
620 }
621
622 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
623 struct userdata *u = PA_SOURCE(o)->userdata;
624
625 switch (code) {
626
627 case PA_SOURCE_MESSAGE_GET_LATENCY: {
628 pa_usec_t r = 0;
629
630 if (u->pcm_handle)
631 r = source_get_latency(u);
632
633 *((pa_usec_t*) data) = r;
634
635 return 0;
636 }
637
638 case PA_SOURCE_MESSAGE_SET_STATE:
639
640 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
641
642 case PA_SOURCE_SUSPENDED:
643 pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
644
645 if (suspend(u) < 0)
646 return -1;
647
648 break;
649
650 case PA_SOURCE_IDLE:
651 case PA_SOURCE_RUNNING:
652
653 if (u->source->thread_info.state == PA_SOURCE_INIT) {
654 if (build_pollfd(u) < 0)
655 return -1;
656
657 snd_pcm_start(u->pcm_handle);
658 }
659
660 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
661 if (unsuspend(u) < 0)
662 return -1;
663 }
664
665 break;
666
667 case PA_SOURCE_UNLINKED:
668 case PA_SOURCE_INIT:
669 ;
670 }
671
672 break;
673 }
674
675 return pa_source_process_msg(o, code, data, offset, chunk);
676 }
677
678 static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
679 struct userdata *u = snd_mixer_elem_get_callback_private(elem);
680
681 pa_assert(u);
682 pa_assert(u->mixer_handle);
683
684 if (mask == SND_CTL_EVENT_MASK_REMOVE)
685 return 0;
686
687 if (mask & SND_CTL_EVENT_MASK_VALUE) {
688 pa_source_get_volume(u->source, TRUE);
689 pa_source_get_mute(u->source, TRUE);
690 }
691
692 return 0;
693 }
694
695 static int source_get_volume_cb(pa_source *s) {
696 struct userdata *u = s->userdata;
697 int err;
698 unsigned i;
699 pa_cvolume r;
700 char t[PA_CVOLUME_SNPRINT_MAX];
701
702 pa_assert(u);
703 pa_assert(u->mixer_elem);
704
705 if (u->mixer_seperate_channels) {
706
707 r.channels = s->sample_spec.channels;
708
709 for (i = 0; i < s->sample_spec.channels; i++) {
710 long alsa_vol;
711
712 if (u->hw_dB_supported) {
713
714 if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
715 goto fail;
716
717 #ifdef HAVE_VALGRIND_MEMCHECK_H
718 VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol));
719 #endif
720
721 r.values[i] = pa_sw_volume_from_dB((double) alsa_vol / 100.0);
722 } else {
723
724 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
725 goto fail;
726
727 r.values[i] = (pa_volume_t) round(((double) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
728 }
729 }
730
731 } else {
732 long alsa_vol;
733
734 pa_assert(u->hw_dB_supported);
735
736 if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0)
737 goto fail;
738
739 #ifdef HAVE_VALGRIND_MEMCHECK_H
740 VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol));
741 #endif
742
743 pa_cvolume_set(&r, s->sample_spec.channels, pa_sw_volume_from_dB((double) alsa_vol / 100.0));
744 }
745
746 pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r));
747
748 if (!pa_cvolume_equal(&u->hardware_volume, &r)) {
749
750 u->hardware_volume = s->volume = r;
751
752 if (u->hw_dB_supported) {
753 pa_cvolume reset;
754
755 /* Hmm, so the hardware volume changed, let's reset our software volume */
756
757 pa_cvolume_reset(&reset, s->sample_spec.channels);
758 pa_source_set_soft_volume(s, &reset);
759 }
760 }
761
762 return 0;
763
764 fail:
765 pa_log_error("Unable to read volume: %s", snd_strerror(err));
766
767 return -1;
768 }
769
770 static int source_set_volume_cb(pa_source *s) {
771 struct userdata *u = s->userdata;
772 int err;
773 unsigned i;
774 pa_cvolume r;
775
776 pa_assert(u);
777 pa_assert(u->mixer_elem);
778
779 if (u->mixer_seperate_channels) {
780
781 r.channels = s->sample_spec.channels;
782
783 for (i = 0; i < s->sample_spec.channels; i++) {
784 long alsa_vol;
785 pa_volume_t vol;
786
787 vol = s->volume.values[i];
788
789 if (u->hw_dB_supported) {
790
791 alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
792 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
793
794 if ((err = snd_mixer_selem_set_capture_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, 1)) < 0)
795 goto fail;
796
797 if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
798 goto fail;
799
800 r.values[i] = pa_sw_volume_from_dB((double) alsa_vol / 100.0);
801 } else {
802
803 alsa_vol = (long) round(((double) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
804 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max);
805
806 if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
807 goto fail;
808
809 if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
810 goto fail;
811
812 r.values[i] = (pa_volume_t) round(((double) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
813 }
814 }
815
816 } else {
817 pa_volume_t vol;
818 long alsa_vol;
819
820 pa_assert(u->hw_dB_supported);
821
822 vol = pa_cvolume_max(&s->volume);
823
824 alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
825 alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
826
827 if ((err = snd_mixer_selem_set_capture_dB_all(u->mixer_elem, alsa_vol, 1)) < 0)
828 goto fail;
829
830 if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0)
831 goto fail;
832
833 pa_cvolume_set(&r, s->volume.channels, pa_sw_volume_from_dB((double) alsa_vol / 100.0));
834 }
835
836 u->hardware_volume = r;
837
838 if (u->hw_dB_supported) {
839 char t[PA_CVOLUME_SNPRINT_MAX];
840
841 /* Match exactly what the user requested by software */
842
843 pa_alsa_volume_divide(&r, &s->volume);
844 pa_source_set_soft_volume(s, &r);
845
846 pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->volume));
847 pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &u->hardware_volume));
848 pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &r));
849
850 } else
851
852 /* We can't match exactly what the user requested, hence let's
853 * at least tell the user about it */
854
855 s->volume = r;
856
857 return 0;
858
859 fail:
860 pa_log_error("Unable to set volume: %s", snd_strerror(err));
861
862 return -1;
863 }
864
865 static int source_get_mute_cb(pa_source *s) {
866 struct userdata *u = s->userdata;
867 int err, sw;
868
869 pa_assert(u);
870 pa_assert(u->mixer_elem);
871
872 if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) {
873 pa_log_error("Unable to get switch: %s", snd_strerror(err));
874 return -1;
875 }
876
877 s->muted = !sw;
878
879 return 0;
880 }
881
882 static int source_set_mute_cb(pa_source *s) {
883 struct userdata *u = s->userdata;
884 int err;
885
886 pa_assert(u);
887 pa_assert(u->mixer_elem);
888
889 if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) {
890 pa_log_error("Unable to set switch: %s", snd_strerror(err));
891 return -1;
892 }
893
894 return 0;
895 }
896
897 static void source_update_requested_latency_cb(pa_source *s) {
898 struct userdata *u = s->userdata;
899 pa_assert(u);
900
901 if (!u->pcm_handle)
902 return;
903
904 update_sw_params(u);
905 }
906
907 static void thread_func(void *userdata) {
908 struct userdata *u = userdata;
909
910 pa_assert(u);
911
912 pa_log_debug("Thread starting up");
913
914 if (u->core->realtime_scheduling)
915 pa_make_realtime(u->core->realtime_priority);
916
917 pa_thread_mq_install(&u->thread_mq);
918 pa_rtpoll_install(u->rtpoll);
919
920 for (;;) {
921 int ret;
922
923 /* pa_log_debug("loop"); */
924
925 /* Read some data and pass it to the sources */
926 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
927 int work_done = 0;
928 pa_usec_t sleep_usec = 0;
929
930 if (u->use_mmap)
931 work_done = mmap_read(u, &sleep_usec);
932 else
933 work_done = unix_read(u, &sleep_usec);
934
935 if (work_done < 0)
936 goto fail;
937
938 /* pa_log_debug("work_done = %i", work_done); */
939
940 if (work_done)
941 update_smoother(u);
942
943 if (u->use_tsched) {
944 pa_usec_t cusec;
945
946 /* OK, the capture buffer is now empty, let's
947 * calculate when to wake up next */
948
949 /* pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) sleep_usec / PA_USEC_PER_MSEC); */
950
951 /* Convert from the sound card time domain to the
952 * system time domain */
953 cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), sleep_usec);
954
955 /* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */
956
957 /* We don't trust the conversion, so we wake up whatever comes first */
958 pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(sleep_usec, cusec));
959 }
960 } else if (u->use_tsched)
961
962 /* OK, we're in an invalid state, let's disable our timers */
963 pa_rtpoll_set_timer_disabled(u->rtpoll);
964
965 /* Hmm, nothing to do. Let's sleep */
966 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
967 goto fail;
968
969 if (ret == 0)
970 goto finish;
971
972 /* Tell ALSA about this and process its response */
973 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
974 struct pollfd *pollfd;
975 unsigned short revents = 0;
976 int err;
977 unsigned n;
978
979 pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n);
980
981 if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) {
982 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
983 goto fail;
984 }
985
986 if (revents & (POLLERR|POLLNVAL|POLLHUP)) {
987 if (pa_alsa_recover_from_poll(u->pcm_handle, revents) < 0)
988 goto fail;
989
990 snd_pcm_start(u->pcm_handle);
991 }
992
993 if (revents && u->use_tsched)
994 pa_log_debug("Wakeup from ALSA! (%i)", revents);
995 }
996 }
997
998 fail:
999 /* If this was no regular exit from the loop we have to continue
1000 * processing messages until we received PA_MESSAGE_SHUTDOWN */
1001 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1002 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1003
1004 finish:
1005 pa_log_debug("Thread shutting down");
1006 }
1007
1008 int pa__init(pa_module*m) {
1009
1010 pa_modargs *ma = NULL;
1011 struct userdata *u = NULL;
1012 const char *dev_id;
1013 pa_sample_spec ss;
1014 pa_channel_map map;
1015 uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
1016 snd_pcm_uframes_t period_frames, tsched_frames;
1017 size_t frame_size;
1018 snd_pcm_info_t *pcm_info = NULL;
1019 int err;
1020 const char *name;
1021 char *name_buf = NULL;
1022 pa_bool_t namereg_fail;
1023 pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d;
1024 pa_source_new_data data;
1025
1026 snd_pcm_info_alloca(&pcm_info);
1027
1028 pa_assert(m);
1029
1030 pa_alsa_redirect_errors_inc();
1031
1032 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1033 pa_log("Failed to parse module arguments");
1034 goto fail;
1035 }
1036
1037 ss = m->core->default_sample_spec;
1038 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
1039 pa_log("Failed to parse sample specification");
1040 goto fail;
1041 }
1042
1043 frame_size = pa_frame_size(&ss);
1044
1045 nfrags = m->core->default_n_fragments;
1046 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
1047 if (frag_size <= 0)
1048 frag_size = frame_size;
1049 tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
1050 tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
1051
1052 if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
1053 pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
1054 pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
1055 pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
1056 pa_log("Failed to parse buffer metrics");
1057 goto fail;
1058 }
1059
1060 hwbuf_size = frag_size * nfrags;
1061 period_frames = frag_size/frame_size;
1062 tsched_frames = tsched_size/frame_size;
1063
1064 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1065 pa_log("Failed to parse mmap argument.");
1066 goto fail;
1067 }
1068
1069 if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
1070 pa_log("Failed to parse timer_scheduling argument.");
1071 goto fail;
1072 }
1073
1074 if (use_tsched && !pa_rtclock_hrtimer()) {
1075 pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1076 use_tsched = FALSE;
1077 }
1078
1079 u = pa_xnew0(struct userdata, 1);
1080 u->core = m->core;
1081 u->module = m;
1082 m->userdata = u;
1083 u->use_mmap = use_mmap;
1084 u->use_tsched = use_tsched;
1085 u->rtpoll = pa_rtpoll_new();
1086 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1087 u->alsa_rtpoll_item = NULL;
1088
1089 u->smoother = pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC, DEFAULT_TSCHED_WATERMARK_USEC, TRUE, 5);
1090 pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
1091
1092 snd_config_update_free_global();
1093
1094 b = use_mmap;
1095 d = use_tsched;
1096
1097 if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
1098
1099 if (!(u->pcm_handle = pa_alsa_open_by_device_id(
1100 dev_id,
1101 &u->device_name,
1102 &ss, &map,
1103 SND_PCM_STREAM_CAPTURE,
1104 &nfrags, &period_frames, tsched_frames,
1105 &b, &d)))
1106 goto fail;
1107
1108 } else {
1109
1110 if (!(u->pcm_handle = pa_alsa_open_by_device_string(
1111 pa_modargs_get_value(ma, "device", DEFAULT_DEVICE),
1112 &u->device_name,
1113 &ss, &map,
1114 SND_PCM_STREAM_CAPTURE,
1115 &nfrags, &period_frames, tsched_frames,
1116 &b, &d)))
1117 goto fail;
1118 }
1119
1120 pa_assert(u->device_name);
1121 pa_log_info("Successfully opened device %s.", u->device_name);
1122
1123 if (use_mmap && !b) {
1124 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1125 u->use_mmap = use_mmap = FALSE;
1126 }
1127
1128 if (use_tsched && (!b || !d)) {
1129 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1130 u->use_tsched = use_tsched = FALSE;
1131 }
1132
1133 if (u->use_mmap)
1134 pa_log_info("Successfully enabled mmap() mode.");
1135
1136 if (u->use_tsched)
1137 pa_log_info("Successfully enabled timer-based scheduling mode.");
1138
1139 if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
1140 pa_log("Error fetching PCM info: %s", snd_strerror(err));
1141 goto fail;
1142 }
1143
1144 /* ALSA might tweak the sample spec, so recalculate the frame size */
1145 frame_size = pa_frame_size(&ss);
1146
1147 if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0)
1148 pa_log("Error opening mixer: %s", snd_strerror(err));
1149 else {
1150 pa_bool_t found = FALSE;
1151
1152 if (pa_alsa_prepare_mixer(u->mixer_handle, u->device_name) >= 0)
1153 found = TRUE;
1154 else {
1155 snd_pcm_info_t* info;
1156
1157 snd_pcm_info_alloca(&info);
1158
1159 if (snd_pcm_info(u->pcm_handle, info) >= 0) {
1160 char *md;
1161 int card;
1162
1163 if ((card = snd_pcm_info_get_card(info)) >= 0) {
1164
1165 md = pa_sprintf_malloc("hw:%i", card);
1166
1167 if (strcmp(u->device_name, md))
1168 if (pa_alsa_prepare_mixer(u->mixer_handle, md) >= 0)
1169 found = TRUE;
1170 pa_xfree(md);
1171 }
1172 }
1173 }
1174
1175 if (found)
1176 if (!(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "Capture", "Mic")))
1177 found = FALSE;
1178
1179 if (!found) {
1180 snd_mixer_close(u->mixer_handle);
1181 u->mixer_handle = NULL;
1182 }
1183 }
1184
1185 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
1186 namereg_fail = TRUE;
1187 else {
1188 name = name_buf = pa_sprintf_malloc("alsa_input.%s", u->device_name);
1189 namereg_fail = FALSE;
1190 }
1191
1192 pa_source_new_data_init(&data);
1193 data.driver = __FILE__;
1194 data.module = m;
1195 pa_source_new_data_set_name(&data, name);
1196 data.namereg_fail = namereg_fail;
1197 pa_source_new_data_set_sample_spec(&data, &ss);
1198 pa_source_new_data_set_channel_map(&data, &map);
1199
1200 pa_alsa_init_proplist(data.proplist, pcm_info);
1201 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
1202 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (period_frames * frame_size * nfrags));
1203 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size));
1204 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial"));
1205
1206 u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
1207 pa_source_new_data_done(&data);
1208 pa_xfree(name_buf);
1209
1210 if (!u->source) {
1211 pa_log("Failed to create source object");
1212 goto fail;
1213 }
1214
1215 u->source->parent.process_msg = source_process_msg;
1216 u->source->update_requested_latency = source_update_requested_latency_cb;
1217 u->source->userdata = u;
1218
1219 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1220 pa_source_set_rtpoll(u->source, u->rtpoll);
1221
1222 u->frame_size = frame_size;
1223 u->fragment_size = frag_size = period_frames * frame_size;
1224 u->nfragments = nfrags;
1225 u->hwbuf_size = u->fragment_size * nfrags;
1226 u->hwbuf_unused_frames = 0;
1227 u->tsched_watermark = tsched_watermark;
1228 u->frame_index = 0;
1229 u->hw_dB_supported = FALSE;
1230 u->hw_dB_min = u->hw_dB_max = 0;
1231 u->hw_volume_min = u->hw_volume_max = 0;
1232 u->mixer_seperate_channels = FALSE;
1233 pa_cvolume_mute(&u->hardware_volume, u->source->sample_spec.channels);
1234
1235 if (use_tsched)
1236 fix_tsched_watermark(u);
1237
1238 pa_source_set_latency_range(u->source,
1239 !use_tsched ? pa_bytes_to_usec(u->hwbuf_size, &ss) : (pa_usec_t) -1,
1240 pa_bytes_to_usec(u->hwbuf_size, &ss));
1241
1242 pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1243 nfrags, (long unsigned) u->fragment_size,
1244 (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
1245
1246 if (use_tsched)
1247 pa_log_info("Time scheduling watermark is %0.2fms",
1248 (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
1249
1250 if (update_sw_params(u) < 0)
1251 goto fail;
1252
1253 if (u->mixer_handle) {
1254 pa_assert(u->mixer_elem);
1255
1256 if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
1257 pa_bool_t suitable = TRUE;
1258
1259 if (snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0) {
1260 pa_log_info("Failed to get volume range. Falling back to software volume control.");
1261 suitable = FALSE;
1262 } else {
1263 pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
1264 pa_assert(u->hw_volume_min < u->hw_volume_max);
1265 }
1266
1267 if (snd_mixer_selem_get_capture_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0)
1268 pa_log_info("Mixer doesn't support dB information.");
1269 else {
1270 #ifdef HAVE_VALGRIND_MEMCHECK_H
1271 VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min));
1272 VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max));
1273 #endif
1274
1275 pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", u->hw_dB_min/100.0, u->hw_dB_max/100.0);
1276 pa_assert(u->hw_dB_min < u->hw_dB_max);
1277 u->hw_dB_supported = TRUE;
1278 }
1279
1280 if (suitable &&
1281 !u->hw_dB_supported &&
1282 u->hw_volume_max - u->hw_volume_min < 3) {
1283
1284 pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1285 suitable = FALSE;
1286 }
1287
1288
1289 if (suitable) {
1290 u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, FALSE) >= 0;
1291
1292 u->source->get_volume = source_get_volume_cb;
1293 u->source->set_volume = source_set_volume_cb;
1294 u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SOURCE_DECIBEL_VOLUME : 0);
1295 pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
1296 } else
1297 pa_log_info("Using software volume control.");
1298 }
1299
1300 if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
1301 u->source->get_mute = source_get_mute_cb;
1302 u->source->set_mute = source_set_mute_cb;
1303 u->source->flags |= PA_SOURCE_HW_MUTE_CTRL;
1304 } else
1305 pa_log_info("Using software mute control.");
1306
1307 u->mixer_fdl = pa_alsa_fdlist_new();
1308
1309 if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
1310 pa_log("Failed to initialize file descriptor monitoring");
1311 goto fail;
1312 }
1313
1314 snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
1315 snd_mixer_elem_set_callback_private(u->mixer_elem, u);
1316 } else
1317 u->mixer_fdl = NULL;
1318
1319 pa_alsa_dump(u->pcm_handle);
1320
1321 if (!(u->thread = pa_thread_new(thread_func, u))) {
1322 pa_log("Failed to create thread.");
1323 goto fail;
1324 }
1325 /* Get initial mixer settings */
1326 if (data.volume_is_set) {
1327 if (u->source->set_volume)
1328 u->source->set_volume(u->source);
1329 } else {
1330 if (u->source->get_volume)
1331 u->source->get_volume(u->source);
1332 }
1333
1334 if (data.muted_is_set) {
1335 if (u->source->set_mute)
1336 u->source->set_mute(u->source);
1337 } else {
1338 if (u->source->get_mute)
1339 u->source->get_mute(u->source);
1340 }
1341
1342 pa_source_put(u->source);
1343
1344 pa_modargs_free(ma);
1345
1346 return 0;
1347
1348 fail:
1349
1350 if (ma)
1351 pa_modargs_free(ma);
1352
1353 pa__done(m);
1354
1355 return -1;
1356 }
1357
1358 void pa__done(pa_module*m) {
1359 struct userdata *u;
1360
1361 pa_assert(m);
1362
1363 if (!(u = m->userdata)) {
1364 pa_alsa_redirect_errors_dec();
1365 return;
1366 }
1367
1368 if (u->source)
1369 pa_source_unlink(u->source);
1370
1371 if (u->thread) {
1372 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1373 pa_thread_free(u->thread);
1374 }
1375
1376 pa_thread_mq_done(&u->thread_mq);
1377
1378 if (u->source)
1379 pa_source_unref(u->source);
1380
1381 if (u->alsa_rtpoll_item)
1382 pa_rtpoll_item_free(u->alsa_rtpoll_item);
1383
1384 if (u->rtpoll)
1385 pa_rtpoll_free(u->rtpoll);
1386
1387 if (u->mixer_fdl)
1388 pa_alsa_fdlist_free(u->mixer_fdl);
1389
1390 if (u->mixer_handle)
1391 snd_mixer_close(u->mixer_handle);
1392
1393 if (u->pcm_handle) {
1394 snd_pcm_drop(u->pcm_handle);
1395 snd_pcm_close(u->pcm_handle);
1396 }
1397
1398 if (u->smoother)
1399 pa_smoother_free(u->smoother);
1400
1401 pa_xfree(u->device_name);
1402 pa_xfree(u);
1403
1404 snd_config_update_free_global();
1405 pa_alsa_redirect_errors_dec();
1406 }