2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
29 #include <asoundlib.h>
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
37 #include <pulse/timeval.h>
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>
56 #include "alsa-util.h"
57 #include "module-alsa-source-symdef.h"
59 PA_MODULE_AUTHOR("Lennart Poettering");
60 PA_MODULE_DESCRIPTION("ALSA Source");
61 PA_MODULE_VERSION(PACKAGE_VERSION
);
62 PA_MODULE_LOAD_ONCE(FALSE
);
64 "source_name=<name for the source> "
65 "device=<ALSA device> "
66 "device_id=<ALSA card index> "
67 "format=<sample format> "
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>");
78 static const char* const valid_modargs
[] = {
91 "tsched_buffer_watermark",
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 */
107 pa_thread_mq thread_mq
;
110 snd_pcm_t
*pcm_handle
;
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
;
120 pa_cvolume hardware_volume
;
122 size_t frame_size
, fragment_size
, hwbuf_size
, tsched_watermark
;
127 pa_bool_t use_mmap
, use_tsched
;
129 pa_rtpoll_item
*alsa_rtpoll_item
;
131 snd_mixer_selem_channel_id_t mixer_map
[SND_MIXER_SCHN_LAST
];
133 pa_smoother
*smoother
;
136 snd_pcm_sframes_t hwbuf_unused_frames
;
139 static void fix_tsched_watermark(struct userdata
*u
) {
141 size_t min_sleep
, min_wakeup
;
144 max_use
= u
->hwbuf_size
- (size_t) u
->hwbuf_unused_frames
* u
->frame_size
;
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
);
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
;
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
;
159 if (u
->tsched_watermark
> max_use
-min_sleep
)
160 u
->tsched_watermark
= max_use
-min_sleep
;
162 if (u
->tsched_watermark
< min_wakeup
)
163 u
->tsched_watermark
= min_wakeup
;
166 static pa_usec_t
hw_sleep_time(struct userdata
*u
, pa_usec_t
*sleep_usec
, pa_usec_t
*process_usec
) {
171 usec
= pa_source_get_requested_latency_within_thread(u
->source
);
173 if (usec
== (pa_usec_t
) -1)
174 usec
= pa_bytes_to_usec(u
->hwbuf_size
, &u
->source
->sample_spec
);
176 /* pa_log_debug("hw buffer time: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC)); */
178 wm
= pa_bytes_to_usec(u
->tsched_watermark
, &u
->source
->sample_spec
);
181 *sleep_usec
= usec
- wm
;
184 *process_usec
= *sleep_usec
= usec
/= 2;
186 /* pa_log_debug("after watermark: %u ms", (unsigned) (*sleep_usec / PA_USEC_PER_MSEC)); */
191 static int try_recover(struct userdata
*u
, const char *call
, int err
) {
196 pa_log_debug("%s: %s", call
, snd_strerror(err
));
198 pa_assert(err
!= -EAGAIN
);
201 pa_log_debug("%s: Buffer overrun!", call
);
203 if ((err
= snd_pcm_recover(u
->pcm_handle
, err
, 1)) == 0) {
204 snd_pcm_start(u
->pcm_handle
);
208 pa_log("%s: %s", call
, snd_strerror(err
));
212 static size_t check_left_to_record(struct userdata
*u
, snd_pcm_sframes_t n
) {
213 size_t left_to_record
;
215 if ((size_t) n
*u
->frame_size
< u
->hwbuf_size
)
216 left_to_record
= u
->hwbuf_size
- ((size_t) n
*u
->frame_size
);
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); */
223 pa_log_info("Overrun!");
226 size_t old_watermark
= u
->tsched_watermark
;
228 u
->tsched_watermark
*= 2;
229 fix_tsched_watermark(u
);
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
);
237 return left_to_record
;
240 static int mmap_read(struct userdata
*u
, pa_usec_t
*sleep_usec
) {
242 pa_usec_t max_sleep_usec
= 0, process_usec
= 0;
243 size_t left_to_record
;
246 pa_source_assert_ref(u
->source
);
249 hw_sleep_time(u
, &max_sleep_usec
, &process_usec
);
255 snd_pcm_hwsync(u
->pcm_handle
);
257 if (PA_UNLIKELY((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0)) {
259 if ((r
= try_recover(u
, "snd_pcm_avail_update", (int) n
)) == 0)
265 left_to_record
= check_left_to_record(u
, n
);
268 if (pa_bytes_to_usec(left_to_record
, &u
->source
->sample_spec
) > process_usec
+max_sleep_usec
/2)
271 if (PA_UNLIKELY(n
<= 0))
276 const snd_pcm_channel_area_t
*areas
;
277 snd_pcm_uframes_t offset
, frames
= (snd_pcm_uframes_t
) n
;
280 snd_pcm_sframes_t sframes
;
282 /* pa_log_debug("%lu frames to read", (unsigned long) frames); */
284 if (PA_UNLIKELY((err
= snd_pcm_mmap_begin(u
->pcm_handle
, &areas
, &offset
, &frames
)) < 0)) {
286 if ((r
= try_recover(u
, "snd_pcm_mmap_begin", err
)) == 0)
292 /* Make sure that if these memblocks need to be copied they will fit into one slot */
293 if (frames
> pa_mempool_block_size_max(u
->source
->core
->mempool
)/u
->frame_size
)
294 frames
= pa_mempool_block_size_max(u
->source
->core
->mempool
)/u
->frame_size
;
296 /* Check these are multiples of 8 bit */
297 pa_assert((areas
[0].first
& 7) == 0);
298 pa_assert((areas
[0].step
& 7)== 0);
300 /* We assume a single interleaved memory buffer */
301 pa_assert((areas
[0].first
>> 3) == 0);
302 pa_assert((areas
[0].step
>> 3) == u
->frame_size
);
304 p
= (uint8_t*) areas
[0].addr
+ (offset
* u
->frame_size
);
306 chunk
.memblock
= pa_memblock_new_fixed(u
->core
->mempool
, p
, frames
* u
->frame_size
, TRUE
);
307 chunk
.length
= pa_memblock_get_length(chunk
.memblock
);
310 pa_source_post(u
->source
, &chunk
);
311 pa_memblock_unref_fixed(chunk
.memblock
);
313 if (PA_UNLIKELY((sframes
= snd_pcm_mmap_commit(u
->pcm_handle
, offset
, frames
)) < 0)) {
315 if ((r
= try_recover(u
, "snd_pcm_mmap_commit", (int) sframes
)) == 0)
323 u
->frame_index
+= (int64_t) frames
;
325 /* pa_log_debug("read %lu frames", (unsigned long) frames); */
327 if (frames
>= (snd_pcm_uframes_t
) n
)
330 n
-= (snd_pcm_sframes_t
) frames
;
334 *sleep_usec
= pa_bytes_to_usec(left_to_record
, &u
->source
->sample_spec
) - process_usec
;
338 static int unix_read(struct userdata
*u
, pa_usec_t
*sleep_usec
) {
340 pa_usec_t max_sleep_usec
= 0, process_usec
= 0;
341 size_t left_to_record
;
344 pa_source_assert_ref(u
->source
);
347 hw_sleep_time(u
, &max_sleep_usec
, &process_usec
);
353 snd_pcm_hwsync(u
->pcm_handle
);
355 if (PA_UNLIKELY((n
= snd_pcm_avail_update(u
->pcm_handle
)) < 0)) {
357 if ((r
= try_recover(u
, "snd_pcm_avail_update", (int) n
)) == 0)
363 left_to_record
= check_left_to_record(u
, n
);
366 if (pa_bytes_to_usec(left_to_record
, &u
->source
->sample_spec
) > process_usec
+max_sleep_usec
/2)
369 if (PA_UNLIKELY(n
<= 0))
374 snd_pcm_sframes_t frames
;
377 chunk
.memblock
= pa_memblock_new(u
->core
->mempool
, (size_t) -1);
379 frames
= (snd_pcm_sframes_t
) (pa_memblock_get_length(chunk
.memblock
) / u
->frame_size
);
384 /* pa_log_debug("%lu frames to read", (unsigned long) n); */
386 p
= pa_memblock_acquire(chunk
.memblock
);
387 frames
= snd_pcm_readi(u
->pcm_handle
, (uint8_t*) p
, (snd_pcm_uframes_t
) frames
);
388 pa_memblock_release(chunk
.memblock
);
390 pa_assert(frames
!= 0);
392 if (PA_UNLIKELY(frames
< 0)) {
393 pa_memblock_unref(chunk
.memblock
);
395 if ((r
= try_recover(u
, "snd_pcm_readi", (int) (frames
))) == 0)
402 chunk
.length
= (size_t) frames
* u
->frame_size
;
404 pa_source_post(u
->source
, &chunk
);
405 pa_memblock_unref(chunk
.memblock
);
409 u
->frame_index
+= frames
;
411 /* pa_log_debug("read %lu frames", (unsigned long) frames); */
420 *sleep_usec
= pa_bytes_to_usec(left_to_record
, &u
->source
->sample_spec
) - process_usec
;
424 static void update_smoother(struct userdata
*u
) {
425 snd_pcm_sframes_t delay
= 0;
428 pa_usec_t now1
, now2
;
431 pa_assert(u
->pcm_handle
);
433 /* Let's update the time smoother */
435 snd_pcm_hwsync(u
->pcm_handle
);
436 snd_pcm_avail_update(u
->pcm_handle
);
438 if (PA_UNLIKELY((err
= snd_pcm_delay(u
->pcm_handle
, &delay
)) < 0)) {
439 pa_log_warn("Failed to get delay: %s", snd_strerror(err
));
443 frames
= u
->frame_index
+ delay
;
445 now1
= pa_rtclock_usec();
446 now2
= pa_bytes_to_usec((uint64_t) frames
* u
->frame_size
, &u
->source
->sample_spec
);
448 pa_smoother_put(u
->smoother
, now1
, now2
);
451 static pa_usec_t
source_get_latency(struct userdata
*u
) {
454 pa_usec_t now1
, now2
;
458 now1
= pa_rtclock_usec();
459 now2
= pa_smoother_get(u
->smoother
, now1
);
461 delay
= (int64_t) now2
- (int64_t) pa_bytes_to_usec((uint64_t) u
->frame_index
* u
->frame_size
, &u
->source
->sample_spec
);
464 r
= (pa_usec_t
) delay
;
469 static int build_pollfd(struct userdata
*u
) {
471 pa_assert(u
->pcm_handle
);
473 if (u
->alsa_rtpoll_item
)
474 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
476 if (!(u
->alsa_rtpoll_item
= pa_alsa_build_pollfd(u
->pcm_handle
, u
->rtpoll
)))
482 static int suspend(struct userdata
*u
) {
484 pa_assert(u
->pcm_handle
);
486 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
489 snd_pcm_close(u
->pcm_handle
);
490 u
->pcm_handle
= NULL
;
492 if (u
->alsa_rtpoll_item
) {
493 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
494 u
->alsa_rtpoll_item
= NULL
;
497 pa_log_info("Device suspended...");
502 static int update_sw_params(struct userdata
*u
) {
503 snd_pcm_uframes_t avail_min
;
508 /* Use the full buffer if noone asked us for anything specific */
509 u
->hwbuf_unused_frames
= 0;
514 if ((latency
= pa_source_get_requested_latency_within_thread(u
->source
)) != (pa_usec_t
) -1) {
517 pa_log_debug("latency set to %0.2f", (double) latency
/ PA_USEC_PER_MSEC
);
519 b
= pa_usec_to_bytes(latency
, &u
->source
->sample_spec
);
521 /* We need at least one sample in our buffer */
523 if (PA_UNLIKELY(b
< u
->frame_size
))
526 u
->hwbuf_unused_frames
= (snd_pcm_sframes_t
)
527 (PA_LIKELY(b
< u
->hwbuf_size
) ?
528 ((u
->hwbuf_size
- b
) / u
->frame_size
) : 0);
530 fix_tsched_watermark(u
);
534 pa_log_debug("hwbuf_unused_frames=%lu", (unsigned long) u
->hwbuf_unused_frames
);
539 pa_usec_t sleep_usec
, process_usec
;
541 hw_sleep_time(u
, &sleep_usec
, &process_usec
);
542 avail_min
+= pa_usec_to_bytes(sleep_usec
, &u
->source
->sample_spec
);
545 pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min
);
547 if ((err
= pa_alsa_set_sw_params(u
->pcm_handle
, avail_min
)) < 0) {
548 pa_log("Failed to set software parameters: %s", snd_strerror(err
));
555 static int unsuspend(struct userdata
*u
) {
560 snd_pcm_uframes_t period_size
;
563 pa_assert(!u
->pcm_handle
);
565 pa_log_info("Trying resume...");
567 snd_config_update_free_global();
569 if ((err
= snd_pcm_open(&u
->pcm_handle
, u
->device_name
, SND_PCM_STREAM_CAPTURE
,
570 /*SND_PCM_NONBLOCK|*/
571 SND_PCM_NO_AUTO_RESAMPLE
|
572 SND_PCM_NO_AUTO_CHANNELS
|
573 SND_PCM_NO_AUTO_FORMAT
)) < 0) {
574 pa_log("Error opening PCM device %s: %s", u
->device_name
, snd_strerror(err
));
578 ss
= u
->source
->sample_spec
;
579 nfrags
= u
->nfragments
;
580 period_size
= u
->fragment_size
/ u
->frame_size
;
584 if ((err
= pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &nfrags
, &period_size
, u
->hwbuf_size
/ u
->frame_size
, &b
, &d
, TRUE
)) < 0) {
585 pa_log("Failed to set hardware parameters: %s", snd_strerror(err
));
589 if (b
!= u
->use_mmap
|| d
!= u
->use_tsched
) {
590 pa_log_warn("Resume failed, couldn't get original access mode.");
594 if (!pa_sample_spec_equal(&ss
, &u
->source
->sample_spec
)) {
595 pa_log_warn("Resume failed, couldn't restore original sample settings.");
599 if (nfrags
!= u
->nfragments
|| period_size
*u
->frame_size
!= u
->fragment_size
) {
600 pa_log_warn("Resume failed, couldn't restore original fragment settings. (Old: %lu*%lu, New %lu*%lu)",
601 (unsigned long) u
->nfragments
, (unsigned long) u
->fragment_size
,
602 (unsigned long) nfrags
, period_size
* u
->frame_size
);
606 if (update_sw_params(u
) < 0)
609 if (build_pollfd(u
) < 0)
612 /* FIXME: We need to reload the volume somehow */
614 snd_pcm_start(u
->pcm_handle
);
615 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
617 pa_log_info("Resumed successfully...");
623 snd_pcm_close(u
->pcm_handle
);
624 u
->pcm_handle
= NULL
;
630 static int source_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
631 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
635 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
639 r
= source_get_latency(u
);
641 *((pa_usec_t
*) data
) = r
;
646 case PA_SOURCE_MESSAGE_SET_STATE
:
648 switch ((pa_source_state_t
) PA_PTR_TO_UINT(data
)) {
650 case PA_SOURCE_SUSPENDED
:
651 pa_assert(PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
));
659 case PA_SOURCE_RUNNING
:
661 if (u
->source
->thread_info
.state
== PA_SOURCE_INIT
) {
662 if (build_pollfd(u
) < 0)
665 snd_pcm_start(u
->pcm_handle
);
668 if (u
->source
->thread_info
.state
== PA_SOURCE_SUSPENDED
) {
669 if (unsuspend(u
) < 0)
675 case PA_SOURCE_UNLINKED
:
683 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
686 static int mixer_callback(snd_mixer_elem_t
*elem
, unsigned int mask
) {
687 struct userdata
*u
= snd_mixer_elem_get_callback_private(elem
);
690 pa_assert(u
->mixer_handle
);
692 if (mask
== SND_CTL_EVENT_MASK_REMOVE
)
695 if (mask
& SND_CTL_EVENT_MASK_VALUE
) {
696 pa_source_get_volume(u
->source
, TRUE
);
697 pa_source_get_mute(u
->source
, TRUE
);
703 static int source_get_volume_cb(pa_source
*s
) {
704 struct userdata
*u
= s
->userdata
;
708 char t
[PA_CVOLUME_SNPRINT_MAX
];
711 pa_assert(u
->mixer_elem
);
713 if (u
->mixer_seperate_channels
) {
715 r
.channels
= s
->sample_spec
.channels
;
717 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
720 if (u
->hw_dB_supported
) {
722 if ((err
= snd_mixer_selem_get_capture_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
725 #ifdef HAVE_VALGRIND_MEMCHECK_H
726 VALGRIND_MAKE_MEM_DEFINED(&alsa_vol
, sizeof(alsa_vol
));
729 r
.values
[i
] = pa_sw_volume_from_dB((double) alsa_vol
/ 100.0);
732 if ((err
= snd_mixer_selem_get_capture_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
735 r
.values
[i
] = (pa_volume_t
) round(((double) (alsa_vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
) / (double) (u
->hw_volume_max
- u
->hw_volume_min
));
742 pa_assert(u
->hw_dB_supported
);
744 if ((err
= snd_mixer_selem_get_capture_dB(u
->mixer_elem
, SND_MIXER_SCHN_MONO
, &alsa_vol
)) < 0)
747 #ifdef HAVE_VALGRIND_MEMCHECK_H
748 VALGRIND_MAKE_MEM_DEFINED(&alsa_vol
, sizeof(alsa_vol
));
751 pa_cvolume_set(&r
, s
->sample_spec
.channels
, pa_sw_volume_from_dB((double) alsa_vol
/ 100.0));
754 pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(t
, sizeof(t
), &r
));
756 if (!pa_cvolume_equal(&u
->hardware_volume
, &r
)) {
758 u
->hardware_volume
= s
->volume
= r
;
760 if (u
->hw_dB_supported
) {
763 /* Hmm, so the hardware volume changed, let's reset our software volume */
765 pa_cvolume_reset(&reset
, s
->sample_spec
.channels
);
766 pa_source_set_soft_volume(s
, &reset
);
773 pa_log_error("Unable to read volume: %s", snd_strerror(err
));
778 static int source_set_volume_cb(pa_source
*s
) {
779 struct userdata
*u
= s
->userdata
;
785 pa_assert(u
->mixer_elem
);
787 if (u
->mixer_seperate_channels
) {
789 r
.channels
= s
->sample_spec
.channels
;
791 for (i
= 0; i
< s
->sample_spec
.channels
; i
++) {
795 vol
= s
->volume
.values
[i
];
797 if (u
->hw_dB_supported
) {
799 alsa_vol
= (long) (pa_sw_volume_to_dB(vol
) * 100);
800 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_dB_min
, u
->hw_dB_max
);
802 if ((err
= snd_mixer_selem_set_capture_dB(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
, 1)) < 0)
805 if ((err
= snd_mixer_selem_get_capture_dB(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
808 r
.values
[i
] = pa_sw_volume_from_dB((double) alsa_vol
/ 100.0);
811 alsa_vol
= (long) round(((double) vol
* (double) (u
->hw_volume_max
- u
->hw_volume_min
)) / PA_VOLUME_NORM
) + u
->hw_volume_min
;
812 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_volume_min
, u
->hw_volume_max
);
814 if ((err
= snd_mixer_selem_set_capture_volume(u
->mixer_elem
, u
->mixer_map
[i
], alsa_vol
)) < 0)
817 if ((err
= snd_mixer_selem_get_capture_volume(u
->mixer_elem
, u
->mixer_map
[i
], &alsa_vol
)) < 0)
820 r
.values
[i
] = (pa_volume_t
) round(((double) (alsa_vol
- u
->hw_volume_min
) * PA_VOLUME_NORM
) / (double) (u
->hw_volume_max
- u
->hw_volume_min
));
828 pa_assert(u
->hw_dB_supported
);
830 vol
= pa_cvolume_max(&s
->volume
);
832 alsa_vol
= (long) (pa_sw_volume_to_dB(vol
) * 100);
833 alsa_vol
= PA_CLAMP_UNLIKELY(alsa_vol
, u
->hw_dB_min
, u
->hw_dB_max
);
835 if ((err
= snd_mixer_selem_set_capture_dB_all(u
->mixer_elem
, alsa_vol
, 1)) < 0)
838 if ((err
= snd_mixer_selem_get_capture_dB(u
->mixer_elem
, SND_MIXER_SCHN_MONO
, &alsa_vol
)) < 0)
841 pa_cvolume_set(&r
, s
->volume
.channels
, pa_sw_volume_from_dB((double) alsa_vol
/ 100.0));
844 u
->hardware_volume
= r
;
846 if (u
->hw_dB_supported
) {
847 char t
[PA_CVOLUME_SNPRINT_MAX
];
849 /* Match exactly what the user requested by software */
851 pa_alsa_volume_divide(&r
, &s
->volume
);
852 pa_source_set_soft_volume(s
, &r
);
854 pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t
, sizeof(t
), &s
->volume
));
855 pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t
, sizeof(t
), &u
->hardware_volume
));
856 pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t
, sizeof(t
), &r
));
860 /* We can't match exactly what the user requested, hence let's
861 * at least tell the user about it */
868 pa_log_error("Unable to set volume: %s", snd_strerror(err
));
873 static int source_get_mute_cb(pa_source
*s
) {
874 struct userdata
*u
= s
->userdata
;
878 pa_assert(u
->mixer_elem
);
880 if ((err
= snd_mixer_selem_get_capture_switch(u
->mixer_elem
, 0, &sw
)) < 0) {
881 pa_log_error("Unable to get switch: %s", snd_strerror(err
));
890 static int source_set_mute_cb(pa_source
*s
) {
891 struct userdata
*u
= s
->userdata
;
895 pa_assert(u
->mixer_elem
);
897 if ((err
= snd_mixer_selem_set_capture_switch_all(u
->mixer_elem
, !s
->muted
)) < 0) {
898 pa_log_error("Unable to set switch: %s", snd_strerror(err
));
905 static void source_update_requested_latency_cb(pa_source
*s
) {
906 struct userdata
*u
= s
->userdata
;
915 static void thread_func(void *userdata
) {
916 struct userdata
*u
= userdata
;
920 pa_log_debug("Thread starting up");
922 if (u
->core
->realtime_scheduling
)
923 pa_make_realtime(u
->core
->realtime_priority
);
925 pa_thread_mq_install(&u
->thread_mq
);
926 pa_rtpoll_install(u
->rtpoll
);
931 /* pa_log_debug("loop"); */
933 /* Read some data and pass it to the sources */
934 if (PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
936 pa_usec_t sleep_usec
= 0;
939 work_done
= mmap_read(u
, &sleep_usec
);
941 work_done
= unix_read(u
, &sleep_usec
);
946 /* pa_log_debug("work_done = %i", work_done); */
954 /* OK, the capture buffer is now empty, let's
955 * calculate when to wake up next */
957 /* pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) sleep_usec / PA_USEC_PER_MSEC); */
959 /* Convert from the sound card time domain to the
960 * system time domain */
961 cusec
= pa_smoother_translate(u
->smoother
, pa_rtclock_usec(), sleep_usec
);
963 /* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */
965 /* We don't trust the conversion, so we wake up whatever comes first */
966 pa_rtpoll_set_timer_relative(u
->rtpoll
, PA_MIN(sleep_usec
, cusec
));
968 } else if (u
->use_tsched
)
970 /* OK, we're in an invalid state, let's disable our timers */
971 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
973 /* Hmm, nothing to do. Let's sleep */
974 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
980 /* Tell ALSA about this and process its response */
981 if (PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
982 struct pollfd
*pollfd
;
983 unsigned short revents
= 0;
987 pollfd
= pa_rtpoll_item_get_pollfd(u
->alsa_rtpoll_item
, &n
);
989 if ((err
= snd_pcm_poll_descriptors_revents(u
->pcm_handle
, pollfd
, n
, &revents
)) < 0) {
990 pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err
));
994 if (revents
& (POLLERR
|POLLNVAL
|POLLHUP
|POLLPRI
)) {
995 if (pa_alsa_recover_from_poll(u
->pcm_handle
, revents
) < 0)
998 snd_pcm_start(u
->pcm_handle
);
1001 if (revents
&& u
->use_tsched
)
1002 pa_log_debug("Wakeup from ALSA!%s%s", (revents
& POLLIN
) ? " INPUT" : "", (revents
& POLLOUT
) ? " OUTPUT" : "");
1007 /* If this was no regular exit from the loop we have to continue
1008 * processing messages until we received PA_MESSAGE_SHUTDOWN */
1009 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
1010 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
1013 pa_log_debug("Thread shutting down");
1016 int pa__init(pa_module
*m
) {
1018 pa_modargs
*ma
= NULL
;
1019 struct userdata
*u
= NULL
;
1023 uint32_t nfrags
, hwbuf_size
, frag_size
, tsched_size
, tsched_watermark
;
1024 snd_pcm_uframes_t period_frames
, tsched_frames
;
1026 snd_pcm_info_t
*pcm_info
= NULL
;
1029 char *name_buf
= NULL
;
1030 pa_bool_t namereg_fail
;
1031 pa_bool_t use_mmap
= TRUE
, b
, use_tsched
= TRUE
, d
;
1032 pa_source_new_data data
;
1034 snd_pcm_info_alloca(&pcm_info
);
1038 pa_alsa_redirect_errors_inc();
1040 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1041 pa_log("Failed to parse module arguments");
1045 ss
= m
->core
->default_sample_spec
;
1046 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_ALSA
) < 0) {
1047 pa_log("Failed to parse sample specification");
1051 frame_size
= pa_frame_size(&ss
);
1053 nfrags
= m
->core
->default_n_fragments
;
1054 frag_size
= (uint32_t) pa_usec_to_bytes(m
->core
->default_fragment_size_msec
*PA_USEC_PER_MSEC
, &ss
);
1056 frag_size
= (uint32_t) frame_size
;
1057 tsched_size
= (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC
, &ss
);
1058 tsched_watermark
= (uint32_t) pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC
, &ss
);
1060 if (pa_modargs_get_value_u32(ma
, "fragments", &nfrags
) < 0 ||
1061 pa_modargs_get_value_u32(ma
, "fragment_size", &frag_size
) < 0 ||
1062 pa_modargs_get_value_u32(ma
, "tsched_buffer_size", &tsched_size
) < 0 ||
1063 pa_modargs_get_value_u32(ma
, "tsched_buffer_watermark", &tsched_watermark
) < 0) {
1064 pa_log("Failed to parse buffer metrics");
1068 hwbuf_size
= frag_size
* nfrags
;
1069 period_frames
= frag_size
/frame_size
;
1070 tsched_frames
= tsched_size
/frame_size
;
1072 if (pa_modargs_get_value_boolean(ma
, "mmap", &use_mmap
) < 0) {
1073 pa_log("Failed to parse mmap argument.");
1077 if (pa_modargs_get_value_boolean(ma
, "tsched", &use_tsched
) < 0) {
1078 pa_log("Failed to parse timer_scheduling argument.");
1082 if (use_tsched
&& !pa_rtclock_hrtimer()) {
1083 pa_log_notice("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
1087 u
= pa_xnew0(struct userdata
, 1);
1091 u
->use_mmap
= use_mmap
;
1092 u
->use_tsched
= use_tsched
;
1093 u
->rtpoll
= pa_rtpoll_new();
1094 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
1095 u
->alsa_rtpoll_item
= NULL
;
1097 u
->smoother
= pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC
, DEFAULT_TSCHED_WATERMARK_USEC
, TRUE
, 5);
1098 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
1100 snd_config_update_free_global();
1105 if ((dev_id
= pa_modargs_get_value(ma
, "device_id", NULL
))) {
1107 if (!(u
->pcm_handle
= pa_alsa_open_by_device_id(
1111 SND_PCM_STREAM_CAPTURE
,
1112 &nfrags
, &period_frames
, tsched_frames
,
1118 if (!(u
->pcm_handle
= pa_alsa_open_by_device_string(
1119 pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
),
1122 SND_PCM_STREAM_CAPTURE
,
1123 &nfrags
, &period_frames
, tsched_frames
,
1128 pa_assert(u
->device_name
);
1129 pa_log_info("Successfully opened device %s.", u
->device_name
);
1131 if (use_mmap
&& !b
) {
1132 pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
1133 u
->use_mmap
= use_mmap
= FALSE
;
1136 if (use_tsched
&& (!b
|| !d
)) {
1137 pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
1138 u
->use_tsched
= use_tsched
= FALSE
;
1142 pa_log_info("Successfully enabled mmap() mode.");
1145 pa_log_info("Successfully enabled timer-based scheduling mode.");
1147 if ((err
= snd_pcm_info(u
->pcm_handle
, pcm_info
)) < 0) {
1148 pa_log("Error fetching PCM info: %s", snd_strerror(err
));
1152 /* ALSA might tweak the sample spec, so recalculate the frame size */
1153 frame_size
= pa_frame_size(&ss
);
1155 if ((err
= snd_mixer_open(&u
->mixer_handle
, 0)) < 0)
1156 pa_log("Error opening mixer: %s", snd_strerror(err
));
1158 pa_bool_t found
= FALSE
;
1160 if (pa_alsa_prepare_mixer(u
->mixer_handle
, u
->device_name
) >= 0)
1163 snd_pcm_info_t
* info
;
1165 snd_pcm_info_alloca(&info
);
1167 if (snd_pcm_info(u
->pcm_handle
, info
) >= 0) {
1171 if ((card
= snd_pcm_info_get_card(info
)) >= 0) {
1173 md
= pa_sprintf_malloc("hw:%i", card
);
1175 if (strcmp(u
->device_name
, md
))
1176 if (pa_alsa_prepare_mixer(u
->mixer_handle
, md
) >= 0)
1184 if (!(u
->mixer_elem
= pa_alsa_find_elem(u
->mixer_handle
, "Capture", "Mic")))
1188 snd_mixer_close(u
->mixer_handle
);
1189 u
->mixer_handle
= NULL
;
1193 if ((name
= pa_modargs_get_value(ma
, "source_name", NULL
)))
1194 namereg_fail
= TRUE
;
1196 name
= name_buf
= pa_sprintf_malloc("alsa_input.%s", u
->device_name
);
1197 namereg_fail
= FALSE
;
1200 pa_source_new_data_init(&data
);
1201 data
.driver
= __FILE__
;
1203 pa_source_new_data_set_name(&data
, name
);
1204 data
.namereg_fail
= namereg_fail
;
1205 pa_source_new_data_set_sample_spec(&data
, &ss
);
1206 pa_source_new_data_set_channel_map(&data
, &map
);
1208 pa_alsa_init_proplist(data
.proplist
, pcm_info
);
1209 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->device_name
);
1210 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE
, "%lu", (unsigned long) (period_frames
* frame_size
* nfrags
));
1211 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE
, "%lu", (unsigned long) (period_frames
* frame_size
));
1212 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_ACCESS_MODE
, u
->use_tsched
? "mmap+timer" : (u
->use_mmap
? "mmap" : "serial"));
1214 u
->source
= pa_source_new(m
->core
, &data
, PA_SOURCE_HARDWARE
|PA_SOURCE_LATENCY
);
1215 pa_source_new_data_done(&data
);
1219 pa_log("Failed to create source object");
1223 u
->source
->parent
.process_msg
= source_process_msg
;
1224 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1225 u
->source
->userdata
= u
;
1227 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
1228 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
1230 u
->frame_size
= frame_size
;
1231 u
->fragment_size
= frag_size
= (uint32_t) (period_frames
* frame_size
);
1232 u
->nfragments
= nfrags
;
1233 u
->hwbuf_size
= u
->fragment_size
* nfrags
;
1234 u
->hwbuf_unused_frames
= 0;
1235 u
->tsched_watermark
= tsched_watermark
;
1237 u
->hw_dB_supported
= FALSE
;
1238 u
->hw_dB_min
= u
->hw_dB_max
= 0;
1239 u
->hw_volume_min
= u
->hw_volume_max
= 0;
1240 u
->mixer_seperate_channels
= FALSE
;
1241 pa_cvolume_mute(&u
->hardware_volume
, u
->source
->sample_spec
.channels
);
1244 fix_tsched_watermark(u
);
1246 pa_source_set_latency_range(u
->source
,
1247 !use_tsched
? pa_bytes_to_usec(u
->hwbuf_size
, &ss
) : (pa_usec_t
) -1,
1248 pa_bytes_to_usec(u
->hwbuf_size
, &ss
));
1250 pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
1251 nfrags
, (long unsigned) u
->fragment_size
,
1252 (double) pa_bytes_to_usec(u
->hwbuf_size
, &ss
) / PA_USEC_PER_MSEC
);
1255 pa_log_info("Time scheduling watermark is %0.2fms",
1256 (double) pa_bytes_to_usec(u
->tsched_watermark
, &ss
) / PA_USEC_PER_MSEC
);
1258 if (update_sw_params(u
) < 0)
1261 if (u
->mixer_handle
) {
1262 pa_assert(u
->mixer_elem
);
1264 if (snd_mixer_selem_has_capture_volume(u
->mixer_elem
)) {
1265 pa_bool_t suitable
= TRUE
;
1267 if (snd_mixer_selem_get_capture_volume_range(u
->mixer_elem
, &u
->hw_volume_min
, &u
->hw_volume_max
) < 0) {
1268 pa_log_info("Failed to get volume range. Falling back to software volume control.");
1271 pa_log_info("Volume ranges from %li to %li.", u
->hw_volume_min
, u
->hw_volume_max
);
1272 pa_assert(u
->hw_volume_min
< u
->hw_volume_max
);
1275 if (snd_mixer_selem_get_capture_dB_range(u
->mixer_elem
, &u
->hw_dB_min
, &u
->hw_dB_max
) < 0)
1276 pa_log_info("Mixer doesn't support dB information.");
1278 #ifdef HAVE_VALGRIND_MEMCHECK_H
1279 VALGRIND_MAKE_MEM_DEFINED(&u
->hw_dB_min
, sizeof(u
->hw_dB_min
));
1280 VALGRIND_MAKE_MEM_DEFINED(&u
->hw_dB_max
, sizeof(u
->hw_dB_max
));
1283 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);
1284 pa_assert(u
->hw_dB_min
< u
->hw_dB_max
);
1285 u
->hw_dB_supported
= TRUE
;
1289 !u
->hw_dB_supported
&&
1290 u
->hw_volume_max
- u
->hw_volume_min
< 3) {
1292 pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
1298 u
->mixer_seperate_channels
= pa_alsa_calc_mixer_map(u
->mixer_elem
, &map
, u
->mixer_map
, FALSE
) >= 0;
1300 u
->source
->get_volume
= source_get_volume_cb
;
1301 u
->source
->set_volume
= source_set_volume_cb
;
1302 u
->source
->flags
|= PA_SOURCE_HW_VOLUME_CTRL
| (u
->hw_dB_supported
? PA_SOURCE_DECIBEL_VOLUME
: 0);
1303 pa_log_info("Using hardware volume control. Hardware dB scale %s.", u
->hw_dB_supported
? "supported" : "not supported");
1305 pa_log_info("Using software volume control.");
1308 if (snd_mixer_selem_has_capture_switch(u
->mixer_elem
)) {
1309 u
->source
->get_mute
= source_get_mute_cb
;
1310 u
->source
->set_mute
= source_set_mute_cb
;
1311 u
->source
->flags
|= PA_SOURCE_HW_MUTE_CTRL
;
1313 pa_log_info("Using software mute control.");
1315 u
->mixer_fdl
= pa_alsa_fdlist_new();
1317 if (pa_alsa_fdlist_set_mixer(u
->mixer_fdl
, u
->mixer_handle
, m
->core
->mainloop
) < 0) {
1318 pa_log("Failed to initialize file descriptor monitoring");
1322 snd_mixer_elem_set_callback(u
->mixer_elem
, mixer_callback
);
1323 snd_mixer_elem_set_callback_private(u
->mixer_elem
, u
);
1325 u
->mixer_fdl
= NULL
;
1327 pa_alsa_dump(u
->pcm_handle
);
1329 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1330 pa_log("Failed to create thread.");
1333 /* Get initial mixer settings */
1334 if (data
.volume_is_set
) {
1335 if (u
->source
->set_volume
)
1336 u
->source
->set_volume(u
->source
);
1338 if (u
->source
->get_volume
)
1339 u
->source
->get_volume(u
->source
);
1342 if (data
.muted_is_set
) {
1343 if (u
->source
->set_mute
)
1344 u
->source
->set_mute(u
->source
);
1346 if (u
->source
->get_mute
)
1347 u
->source
->get_mute(u
->source
);
1350 pa_source_put(u
->source
);
1352 pa_modargs_free(ma
);
1359 pa_modargs_free(ma
);
1366 void pa__done(pa_module
*m
) {
1371 if (!(u
= m
->userdata
)) {
1372 pa_alsa_redirect_errors_dec();
1377 pa_source_unlink(u
->source
);
1380 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1381 pa_thread_free(u
->thread
);
1384 pa_thread_mq_done(&u
->thread_mq
);
1387 pa_source_unref(u
->source
);
1389 if (u
->alsa_rtpoll_item
)
1390 pa_rtpoll_item_free(u
->alsa_rtpoll_item
);
1393 pa_rtpoll_free(u
->rtpoll
);
1396 pa_alsa_fdlist_free(u
->mixer_fdl
);
1398 if (u
->mixer_handle
)
1399 snd_mixer_close(u
->mixer_handle
);
1401 if (u
->pcm_handle
) {
1402 snd_pcm_drop(u
->pcm_handle
);
1403 snd_pcm_close(u
->pcm_handle
);
1407 pa_smoother_free(u
->smoother
);
1409 pa_xfree(u
->device_name
);
1412 snd_config_update_free_global();
1413 pa_alsa_redirect_errors_dec();