split out mixer setup into seperate functions to make things more readable

This commit is contained in:
Lennart Poettering 2009-02-19 04:04:42 +01:00
parent e1608d5db2
commit 205b0ba08e
2 changed files with 177 additions and 163 deletions

View file

@ -1227,6 +1227,91 @@ static void set_sink_name(pa_sink_new_data *data, pa_modargs *ma, const char *de
pa_xfree(t); pa_xfree(t);
} }
static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) {
pa_assert(u);
if (!u->mixer_handle)
return 0;
pa_assert(u->mixer_elem);
if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
pa_bool_t suitable = FALSE;
if (snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0)
pa_log_info("Failed to get volume range. Falling back to software volume control.");
else if (u->hw_volume_min >= u->hw_volume_max)
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);
else {
pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
suitable = TRUE;
}
if (suitable) {
if (ignore_dB || snd_mixer_selem_get_playback_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0)
pa_log_info("Mixer doesn't support dB information or data is ignored.");
else {
#ifdef HAVE_VALGRIND_MEMCHECK_H
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min));
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max));
#endif
if (u->hw_dB_min >= u->hw_dB_max)
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);
else {
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);
u->hw_dB_supported = TRUE;
if (u->hw_dB_max > 0) {
u->sink->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0);
pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->sink->base_volume));
} else
pa_log_info("No particular base volume set, fixing to 0 dB");
}
}
if (!u->hw_dB_supported &&
u->hw_volume_max - u->hw_volume_min < 3) {
pa_log_info("Device doesn't do dB volume and has less than 4 volume levels. Falling back to software volume control.");
suitable = FALSE;
}
}
if (suitable) {
u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &u->sink->channel_map, u->mixer_map, TRUE) >= 0;
u->sink->get_volume = sink_get_volume_cb;
u->sink->set_volume = sink_set_volume_cb;
u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SINK_DECIBEL_VOLUME : 0);
pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
if (!u->hw_dB_supported)
u->sink->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1;
} else
pa_log_info("Using software volume control.");
}
if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
u->sink->get_mute = sink_get_mute_cb;
u->sink->set_mute = sink_set_mute_cb;
u->sink->flags |= PA_SINK_HW_MUTE_CTRL;
} else
pa_log_info("Using software mute control.");
u->mixer_fdl = pa_alsa_fdlist_new();
if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, u->core->mainloop) < 0) {
pa_log("Failed to initialize file descriptor monitoring");
return -1;
}
snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
snd_mixer_elem_set_callback_private(u->mixer_elem, u);
return 0;
}
pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) { pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) {
struct userdata *u = NULL; struct userdata *u = NULL;
@ -1236,7 +1321,6 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca
uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark; uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
snd_pcm_uframes_t period_frames, tsched_frames; snd_pcm_uframes_t period_frames, tsched_frames;
size_t frame_size; size_t frame_size;
int err;
pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE; pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE;
pa_usec_t usec; pa_usec_t usec;
pa_sink_new_data data; pa_sink_new_data data;
@ -1365,7 +1449,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca
} }
if (use_tsched && (!b || !d)) { if (use_tsched && (!b || !d)) {
pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling."); pa_log_info("Cannot enable timer-based scheduling, falling back to sound IRQ scheduling.");
u->use_tsched = use_tsched = FALSE; u->use_tsched = use_tsched = FALSE;
} }
@ -1448,86 +1532,8 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca
if (update_sw_params(u) < 0) if (update_sw_params(u) < 0)
goto fail; goto fail;
pa_memchunk_reset(&u->memchunk); if (setup_mixer(u, ignore_dB) < 0)
goto fail;
if (u->mixer_handle) {
pa_assert(u->mixer_elem);
if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
pa_bool_t suitable = FALSE;
if (snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0)
pa_log_info("Failed to get volume range. Falling back to software volume control.");
else if (u->hw_volume_min >= u->hw_volume_max)
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);
else {
pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
suitable = TRUE;
}
if (suitable) {
if (ignore_dB || snd_mixer_selem_get_playback_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0)
pa_log_info("Mixer doesn't support dB information or data is ignored.");
else {
#ifdef HAVE_VALGRIND_MEMCHECK_H
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min));
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max));
#endif
if (u->hw_dB_min >= u->hw_dB_max)
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);
else {
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);
u->hw_dB_supported = TRUE;
if (u->hw_dB_max > 0) {
u->sink->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0);
pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->sink->base_volume));
} else
pa_log_info("No particular base volume set, fixing to 0 dB");
}
}
if (!u->hw_dB_supported &&
u->hw_volume_max - u->hw_volume_min < 3) {
pa_log_info("Device doesn't do dB volume and has less than 4 volume levels. Falling back to software volume control.");
suitable = FALSE;
}
}
if (suitable) {
u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, TRUE) >= 0;
u->sink->get_volume = sink_get_volume_cb;
u->sink->set_volume = sink_set_volume_cb;
u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SINK_DECIBEL_VOLUME : 0);
pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
if (!u->hw_dB_supported)
u->sink->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1;
} else
pa_log_info("Using software volume control.");
}
if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
u->sink->get_mute = sink_get_mute_cb;
u->sink->set_mute = sink_set_mute_cb;
u->sink->flags |= PA_SINK_HW_MUTE_CTRL;
} else
pa_log_info("Using software mute control.");
u->mixer_fdl = pa_alsa_fdlist_new();
if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
pa_log("Failed to initialize file descriptor monitoring");
goto fail;
}
snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
snd_mixer_elem_set_callback_private(u->mixer_elem, u);
} else
u->mixer_fdl = NULL;
pa_alsa_dump(u->pcm_handle); pa_alsa_dump(u->pcm_handle);

View file

@ -1062,6 +1062,91 @@ static void set_source_name(pa_source_new_data *data, pa_modargs *ma, const char
pa_xfree(t); pa_xfree(t);
} }
static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) {
pa_assert(u);
if (!u->mixer_handle)
return 0;
pa_assert(u->mixer_elem);
if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
pa_bool_t suitable = FALSE;
if (snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0)
pa_log_info("Failed to get volume range. Falling back to software volume control.");
else if (u->hw_volume_min >= u->hw_volume_max)
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);
else {
pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
suitable = TRUE;
}
if (suitable) {
if (ignore_dB || snd_mixer_selem_get_capture_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0)
pa_log_info("Mixer doesn't support dB information or data is ignored.");
else {
#ifdef HAVE_VALGRIND_MEMCHECK_H
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min));
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max));
#endif
if (u->hw_dB_min >= u->hw_dB_max)
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);
else {
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);
u->hw_dB_supported = TRUE;
if (u->hw_dB_max > 0) {
u->source->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0);
pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->source->base_volume));
} else
pa_log_info("No particular base volume set, fixing to 0 dB");
}
}
if (!u->hw_dB_supported &&
u->hw_volume_max - u->hw_volume_min < 3) {
pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
suitable = FALSE;
}
}
if (suitable) {
u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &u->source->channel_map, u->mixer_map, FALSE) >= 0;
u->source->get_volume = source_get_volume_cb;
u->source->set_volume = source_set_volume_cb;
u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SOURCE_DECIBEL_VOLUME : 0);
pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
if (!u->hw_dB_supported)
u->source->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1;
} else
pa_log_info("Using software volume control.");
}
if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
u->source->get_mute = source_get_mute_cb;
u->source->set_mute = source_set_mute_cb;
u->source->flags |= PA_SOURCE_HW_MUTE_CTRL;
} else
pa_log_info("Using software mute control.");
u->mixer_fdl = pa_alsa_fdlist_new();
if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, u->core->mainloop) < 0) {
pa_log("Failed to initialize file descriptor monitoring");
return -1;
}
snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
snd_mixer_elem_set_callback_private(u->mixer_elem, u);
return 0;
}
pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) { pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) {
struct userdata *u = NULL; struct userdata *u = NULL;
@ -1071,11 +1156,11 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p
uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark; uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
snd_pcm_uframes_t period_frames, tsched_frames; snd_pcm_uframes_t period_frames, tsched_frames;
size_t frame_size; size_t frame_size;
int err;
pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE; pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE;
pa_source_new_data data; pa_source_new_data data;
pa_assert(m); pa_assert(m);
pa_assert(ma);
ss = m->core->default_sample_spec; ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) { if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_ALSA) < 0) {
@ -1190,7 +1275,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p
} }
if (use_tsched && (!b || !d)) { if (use_tsched && (!b || !d)) {
pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling."); pa_log_info("Cannot enable timer-based scheduling, falling back to sound IRQ scheduling.");
u->use_tsched = use_tsched = FALSE; u->use_tsched = use_tsched = FALSE;
} }
@ -1270,85 +1355,8 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p
if (update_sw_params(u) < 0) if (update_sw_params(u) < 0)
goto fail; goto fail;
if (u->mixer_handle) { if (setup_mixer(u, ignore_dB) < 0)
pa_assert(u->mixer_elem); goto fail;
if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
pa_bool_t suitable = FALSE;
if (snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0)
pa_log_info("Failed to get volume range. Falling back to software volume control.");
else if (u->hw_volume_min >= u->hw_volume_max)
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);
else {
pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
suitable = TRUE;
}
if (suitable) {
if (ignore_dB || snd_mixer_selem_get_capture_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0)
pa_log_info("Mixer doesn't support dB information or data is ignored.");
else {
#ifdef HAVE_VALGRIND_MEMCHECK_H
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min));
VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max));
#endif
if (u->hw_dB_min >= u->hw_dB_max)
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);
else {
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);
u->hw_dB_supported = TRUE;
if (u->hw_dB_max > 0) {
u->source->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0);
pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->source->base_volume));
} else
pa_log_info("No particular base volume set, fixing to 0 dB");
}
}
if (!u->hw_dB_supported &&
u->hw_volume_max - u->hw_volume_min < 3) {
pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
suitable = FALSE;
}
}
if (suitable) {
u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, FALSE) >= 0;
u->source->get_volume = source_get_volume_cb;
u->source->set_volume = source_set_volume_cb;
u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SOURCE_DECIBEL_VOLUME : 0);
pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported");
if (!u->hw_dB_supported)
u->source->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1;
} else
pa_log_info("Using software volume control.");
}
if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
u->source->get_mute = source_get_mute_cb;
u->source->set_mute = source_set_mute_cb;
u->source->flags |= PA_SOURCE_HW_MUTE_CTRL;
} else
pa_log_info("Using software mute control.");
u->mixer_fdl = pa_alsa_fdlist_new();
if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, m->core->mainloop) < 0) {
pa_log("Failed to initialize file descriptor monitoring");
goto fail;
}
snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
snd_mixer_elem_set_callback_private(u->mixer_elem, u);
} else
u->mixer_fdl = NULL;
pa_alsa_dump(u->pcm_handle); pa_alsa_dump(u->pcm_handle);