[pulseaudio-discuss] [PATCH v4] alsa-sink/source, sink, source: Consider sample format for avoid-resampling/passthrough

Tanu Kaskinen tanuk at iki.fi
Wed Nov 14 09:37:50 UTC 2018


On Wed, 2018-10-31 at 23:48 +0900, Sangchul Lee wrote:
> Sample format(e.g. 16 bit, 24 bit) was not considered even if the
> avoid-resampling option is set or the passthrough mode is used.
> This patch checks both sample format and rate of a stream to
> determine whether to avoid resampling in case of the option is set.
> In other word, it is possble to use the stream's original sample
> format and rate without resampling as long as these are supported
> by the device.
> 
> pa_sink_input_update_rate() and pa_source_output_update_rate() are
> renamed to pa_sink_input_update_resampler() and pa_source_output
> _update_resampler() respectively.
> 
> functions are added as below.
>  pa_sink_set_sample_format(), pa_sink_set_sample_rate(),
>  pa_source_set_sample_format(), pa_source_set_sample_rate()
> 
> Signed-off-by: Sangchul Lee <sc11.lee at samsung.com>
> ---

> @@ -1674,33 +1717,41 @@ static bool sink_set_formats(pa_sink *s, pa_idxset *formats) {
>  static int sink_reconfigure_cb(pa_sink *s, pa_sample_spec *spec, bool passthrough) {
>      struct userdata *u = s->userdata;
>      int i;
> -    bool supported = false;
> -
> -    /* FIXME: we only update rate for now */
> +    bool format_supported = false;
> +    bool rate_supported = false;
>  
>      pa_assert(u);
>  
> -    for (i = 0; u->supported_rates[i]; i++) {
> -        if (u->supported_rates[i] == spec->rate) {
> -            supported = true;
> +    for (i = 0; u->supported_formats[i] != PA_SAMPLE_MAX; i++) {
> +        if (u->supported_formats[i] == spec->format) {
> +            pa_sink_set_sample_format(u->sink, spec->format);
> +            format_supported = true;
>              break;
>          }
>      }
>  
> -    if (!supported) {
> -        pa_log_info("Sink does not support sample rate of %d Hz", spec->rate);
> -        return -1;
> +    if (!format_supported) {
> +        pa_log_info("Sink does not support sample format of %s, set it to default value",
> +                    pa_sample_format_to_string(spec->format));
> +        pa_sink_set_sample_format(u->sink, u->core->default_sample_spec.format);

This is still not right. We can't assume that u->core-
>default_sample_spec.format is supported by the hardware.

When the sink is created, it opens the device for the first time, using
the default format. If that format isn't supported, some other format
is selected instead (or otherwise the whole sink initialization fails).
I think this format should be used as the default here, because it's
known to work.

>      }
>  
> -    if (!PA_SINK_IS_OPENED(s->state)) {
> -        pa_log_info("Updating rate for device %s, new rate is %d", u->device_name, spec->rate);
> -        u->sink->sample_spec.rate = spec->rate;
> -        return 0;
> +    for (i = 0; u->supported_rates[i]; i++) {
> +        if (u->supported_rates[i] == spec->rate) {
> +            pa_sink_set_sample_rate(u->sink, spec->rate);
> +            rate_supported = true;
> +            break;
> +        }
> +    }
> +
> +    if (!rate_supported) {
> +        pa_log_info("Sink does not support sample rate of %u, set it to default value", spec->rate);
> +        pa_sink_set_sample_rate(u->sink, u->core->default_sample_spec.rate);

Same problem here with the rate as with the format earlier.

>      }
>  
>      /* Passthrough status change is handled during unsuspend */
>  
> -    return -1;
> +    return 0;

The function now reports success always. This made me have a look at
the places that check for errors from reconfigure(). There are just two
places that currently care about the reconfigure() return values:
pa_sink_reconfigure(), when it reconfigures a monitor sink, and
pa_source_reconfigure(), when it reconfigures a monitored sink. To me
it looks like those can be safely changed to just assume that the
reconfiguration always succeeds. We should change the reconfigure()
callback return types to void.

-- 
Tanu

https://www.patreon.com/tanuk
https://liberapay.com/tanuk



More information about the pulseaudio-discuss mailing list