snd_pcm_set_params faild on set_buffer_time_near and set_period_time_near
diff mbox

Message ID 55AE0831.2010206@streamunlimited.com
State New
Headers show

Commit Message

Martin Geier July 21, 2015, 8:52 a.m. UTC
Hallo,

I tried to configure alsa device with snd_pcm_set_params function, 
requested
parameters was:
channel 2, rate: 22050, soft_resample: 1, latency: 50000

Function snd_pcm_hw_params_set_buffer_time_near and 
snd_pcm_hw_param_set_near
is called with:
min: 50000, max: 50000, mindir: 0, maxdir: -1

and input params was:

ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: 22050
PERIOD_TIME: (362 743039)
PERIOD_SIZE: [8 16384]
PERIOD_BYTES: [32 65536]
PERIODS: [2 19]
BUFFER_TIME: (725 1486078)
BUFFER_SIZE: [16 32768]
BUFFER_BYTES: [64 131072]
TICK_TIME: ALL

unfortunately, this function fails, on function snd_pcm_hw_param_set_first:

ALSA ERROR hw_params: set_near (BUFFER_TIME)
            value = 50000 : Invalid argument
ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: 22050
PERIOD_TIME: (4580 5533)
PERIOD_SIZE: NONE
PERIOD_BYTES: [404 488]
PERIODS: 10
BUFFER_TIME: (50022 50023)
BUFFER_SIZE: 1103
BUFFER_BYTES: 4412
TICK_TIME: ALL

This shouldn't be problem because in snd_pcm_set_params is
snd_pcm_hw_params_set_period_time_near called.
This function should be called with same params as 
snd_pcm_hw_params_set_buffer_time_near
but the input params are:

snd_pcm_hw_param_set_near: min: 12500, max: 12500, mindir: 0, maxdir: -1
hw_params: snd_pcm_hw_param_set_near (PERIOD_TIME)
ACCESS:  RW_INTERLEAVED
FORMAT:  S16_LE
SUBFORMAT:  STD
SAMPLE_BITS: 16
FRAME_BITS: 32
CHANNELS: 2
RATE: 22050
PERIOD_TIME: (4580 5533)
PERIOD_SIZE: NONE
PERIOD_BYTES: [404 488]
PERIODS: 10
BUFFER_TIME: (50022 50023)
BUFFER_SIZE: 1103
BUFFER_BYTES: 4412
TICK_TIME: ALL

because in snd_pcm_hw_param_set_near is

if (last)
     err = snd_pcm_hw_param_set_last(pcm, params, var, val, dir);
else
     err = snd_pcm_hw_param_set_first(pcm, params, var, val, dir);
if (err < 0)
     dump_hw_params(params, "set_near", var, *val, err);
return err;

and in error part is only debug output and not restore original parameters.

Environment:
alsa-lib 1.0.27.1
TI AM335x soc

Simple fix is attached, but I am not sure if it is correct.

BR
Martin Geier

Comments

Raymond Yau July 23, 2015, 7:19 a.m. UTC | #1
>
> I tried to configure alsa device with snd_pcm_set_params function,
requested
> parameters was:
> channel 2, rate: 22050, soft_resample: 1, latency: 50000

The function snd_pcm_set_params does not always succed especially you are
using 22050Hz which is 20ms increment

Which soc driver ? It is quite difficult to find the soutce by chip name

https://git.kernel.org/cgit/linux/kernel/git/tiwai/sound.git/tree/sound/soc
Martin Geier July 23, 2015, 7:33 a.m. UTC | #2
I am using 'davinci' soc driver with board specific driver.

On 23.07.2015 09:19, Raymond Yau wrote:
>
>
> >
> > I tried to configure alsa device with snd_pcm_set_params function, 
> requested
> > parameters was:
> > channel 2, rate: 22050, soft_resample: 1, latency: 50000
>
> The function snd_pcm_set_params does not always succed especially you 
> are using 22050Hz which is 20ms increment
>
> Which soc driver ? It is quite difficult to find the soutce by chip name
>
> https://git.kernel.org/cgit/linux/kernel/git/tiwai/sound.git/tree/sound/soc
>
Takashi Iwai July 23, 2015, 8:11 a.m. UTC | #3
On Tue, 21 Jul 2015 10:52:01 +0200,
Martin Geier wrote:
> 
> Hallo,
> 
> I tried to configure alsa device with snd_pcm_set_params function, 
> requested
> parameters was:
> channel 2, rate: 22050, soft_resample: 1, latency: 50000
> 
> Function snd_pcm_hw_params_set_buffer_time_near and 
> snd_pcm_hw_param_set_near
> is called with:
> min: 50000, max: 50000, mindir: 0, maxdir: -1
> 
> and input params was:
> 
> ACCESS:  RW_INTERLEAVED
> FORMAT:  S16_LE
> SUBFORMAT:  STD
> SAMPLE_BITS: 16
> FRAME_BITS: 32
> CHANNELS: 2
> RATE: 22050
> PERIOD_TIME: (362 743039)
> PERIOD_SIZE: [8 16384]
> PERIOD_BYTES: [32 65536]
> PERIODS: [2 19]
> BUFFER_TIME: (725 1486078)
> BUFFER_SIZE: [16 32768]
> BUFFER_BYTES: [64 131072]
> TICK_TIME: ALL
> 
> unfortunately, this function fails, on function snd_pcm_hw_param_set_first:
> 
> ALSA ERROR hw_params: set_near (BUFFER_TIME)
>             value = 50000 : Invalid argument
> ACCESS:  RW_INTERLEAVED
> FORMAT:  S16_LE
> SUBFORMAT:  STD
> SAMPLE_BITS: 16
> FRAME_BITS: 32
> CHANNELS: 2
> RATE: 22050
> PERIOD_TIME: (4580 5533)
> PERIOD_SIZE: NONE
> PERIOD_BYTES: [404 488]
> PERIODS: 10
> BUFFER_TIME: (50022 50023)
> BUFFER_SIZE: 1103
> BUFFER_BYTES: 4412
> TICK_TIME: ALL
> 
> This shouldn't be problem because in snd_pcm_set_params is
> snd_pcm_hw_params_set_period_time_near called.
> This function should be called with same params as 
> snd_pcm_hw_params_set_buffer_time_near
> but the input params are:
> 
> snd_pcm_hw_param_set_near: min: 12500, max: 12500, mindir: 0, maxdir: -1
> hw_params: snd_pcm_hw_param_set_near (PERIOD_TIME)
> ACCESS:  RW_INTERLEAVED
> FORMAT:  S16_LE
> SUBFORMAT:  STD
> SAMPLE_BITS: 16
> FRAME_BITS: 32
> CHANNELS: 2
> RATE: 22050
> PERIOD_TIME: (4580 5533)
> PERIOD_SIZE: NONE
> PERIOD_BYTES: [404 488]
> PERIODS: 10
> BUFFER_TIME: (50022 50023)
> BUFFER_SIZE: 1103
> BUFFER_BYTES: 4412
> TICK_TIME: ALL
> 
> because in snd_pcm_hw_param_set_near is
> 
> if (last)
>      err = snd_pcm_hw_param_set_last(pcm, params, var, val, dir);
> else
>      err = snd_pcm_hw_param_set_first(pcm, params, var, val, dir);
> if (err < 0)
>      dump_hw_params(params, "set_near", var, *val, err);
> return err;
> 
> and in error part is only debug output and not restore original parameters.
> 
> Environment:
> alsa-lib 1.0.27.1
> TI AM335x soc
> 
> Simple fix is attached, but I am not sure if it is correct.

Thanks for analysis and patch.  It's however rather a bug in
snd_pcm_set_params().  There is no guarantee to keep the old value in
snd_pcm_set_*(), thus the caller needs to take care of it instead.


Takashi

Patch
diff mbox

From 6a31c2f854b255b407ce2d52d233db00588d2e40 Mon Sep 17 00:00:00 2001
From: Martin Geier <martin.geier@streamunlimited.com>
Date: Mon, 20 Jul 2015 08:59:54 +0200
Subject: [PATCH] restore params if snd_pcm_hw_param_set_near method failed

Signed-off-by: Martin Geier <martin.geier@streamunlimited.com>
---
 pcm_params.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/pcm/pcm_params.c b/src/pcm/pcm_params.c
index 0b66e8c..57d98d8 100644
--- a/src/pcm/pcm_params.c
+++ b/src/pcm/pcm_params.c
@@ -872,8 +872,10 @@  int snd_pcm_hw_param_set_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
 		err = snd_pcm_hw_param_set_last(pcm, params, var, val, dir);
 	else
 		err = snd_pcm_hw_param_set_first(pcm, params, var, val, dir);
-	if (err < 0)
+	if (err < 0) {
 		dump_hw_params(params, "set_near", var, *val, err);
+		*params = save;
+	}
 	return err;
 }
 
-- 
1.9.1