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