From bb820db4b5b9e928b4d7fe58bbab2f56b1897f64 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 30 May 2006 22:05:07 +0000 Subject: * if an ALSA device doesn't support the channel count requested, use what ALSA suggests instead * if an ALSA device doesn't support the sampling freq requested, use what ALSA suggests and resample if this deviates more than 10% from what we requested * fix segfault freeing an unitialized mixer_fdl field git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@992 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/alsa-util.c | 17 ++++++++++++++--- src/modules/alsa-util.h | 2 +- src/modules/module-alsa-sink.c | 13 ++++++++++--- src/modules/module-alsa-source.c | 15 +++++++++++---- 4 files changed, 36 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/modules/alsa-util.c b/src/modules/alsa-util.c index 122f4419..22e623f6 100644 --- a/src/modules/alsa-util.c +++ b/src/modules/alsa-util.c @@ -249,11 +249,12 @@ int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_han /* Set the hardware parameters of the given ALSA device. Returns the * selected fragment settings in *period and *period_size */ -int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, const pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) { +int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) { int ret = -1; snd_pcm_uframes_t buffer_size; snd_pcm_hw_params_t *hwparams = NULL; unsigned int r = ss->rate; + unsigned int c = ss->channels; static const snd_pcm_format_t format_trans[] = { [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8, @@ -273,14 +274,24 @@ int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, const pa_sample_spec *ss, uint3 (ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0 || (ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format])) < 0 || (ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0 || - (ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels)) < 0 || + (ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0 || (*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) || (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0) || (ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) goto finish; - if (ss->rate != r) + if (ss->rate != r) { pa_log_info(__FILE__": device doesn't support %u Hz, changed to %u Hz.", ss->rate, r); + + /* If the sample rate deviates too much, we need to resample */ + if (r < ss->rate*.9 || r > ss->rate*1.1) + ss->rate = r; + } + + if (ss->channels != c) { + pa_log_info(__FILE__": device doesn't support %u channels, changed to %u.", ss->channels, c); + ss->channels = c; + } if ((ret = snd_pcm_prepare(pcm_handle)) < 0) goto finish; diff --git a/src/modules/alsa-util.h b/src/modules/alsa-util.h index 69d4eddc..4bee8625 100644 --- a/src/modules/alsa-util.h +++ b/src/modules/alsa-util.h @@ -37,7 +37,7 @@ void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl); int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist *fdl, snd_pcm_t *pcm_handle, pa_mainloop_api* m, void (*cb)(void *userdata), void *userdata); int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m); -int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, const pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size); +int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size); int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev); snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name); diff --git a/src/modules/module-alsa-sink.c b/src/modules/module-alsa-sink.c index e768e16a..a0b6dc08 100644 --- a/src/modules/module-alsa-sink.c +++ b/src/modules/module-alsa-sink.c @@ -363,6 +363,10 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } + if (ss.channels != map.channels) + /* Seems ALSA didn't like the channel number, so let's fix the channel map */ + pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA); + if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) { pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err)); goto fail; @@ -374,8 +378,10 @@ int pa__init(pa_core *c, pa_module*m) { u->mixer_handle = NULL; } - u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map); - assert(u->sink); + if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) { + pa_log(__FILE__": Failed to create sink object"); + goto fail; + } u->sink->get_latency = sink_get_latency_cb; if (u->mixer_handle) { @@ -420,7 +426,8 @@ int pa__init(pa_core *c, pa_module*m) { } 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; u->frame_size = frame_size; u->fragment_size = period_size * frame_size; diff --git a/src/modules/module-alsa-source.c b/src/modules/module-alsa-source.c index 414efda8..10b98f75 100644 --- a/src/modules/module-alsa-source.c +++ b/src/modules/module-alsa-source.c @@ -335,7 +335,7 @@ int pa__init(pa_core *c, pa_module*m) { } period_size = fragsize/frame_size; - u = pa_xmalloc0(sizeof(struct userdata)); + u = pa_xnew(struct userdata, 1); m->userdata = u; u->module = m; @@ -356,6 +356,10 @@ int pa__init(pa_core *c, pa_module*m) { goto fail; } + if (ss.channels != map.channels) + /* Seems ALSA didn't like the channel number, so let's fix the channel map */ + pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA); + if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) { pa_log(__FILE__": Error opening mixer: %s", snd_strerror(err)); goto fail; @@ -367,8 +371,10 @@ int pa__init(pa_core *c, pa_module*m) { u->mixer_handle = NULL; } - u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map); - assert(u->source); + if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) { + pa_log(__FILE__": Failed to create source object"); + goto fail; + } u->source->userdata = u; u->source->get_latency = source_get_latency_cb; @@ -413,7 +419,8 @@ int pa__init(pa_core *c, pa_module*m) { } 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; u->frame_size = frame_size; u->fragment_size = period_size * frame_size; -- cgit