Fixed bug 4710 - audio/alsa: avoid configuring hardware parameters with only a single period

Anthony Pesch

The previous code first configured the period size using snd_pcm_hw_par-
ams_set_period_size_near. Then, it further narrowed the configuration
space by calling snd_pcm_hw_params_set_buffer_size_near using a buffer
size of 2 times the _requested_ period size in order to try and get a
configuration with only 2 periods. If the configured period size was
larger than the requested size, the second call could inadvertently
narrow the configuration space to contain only a single period.

Rather than fixing the call to snd_pcm_hw_params_set_buffer_size_near
to use a size of 2 times the configured period size, the code has been
changed to use snd_pcm_hw_params_set_periods_min in order to more
clearly explain the intent.
This commit is contained in:
Sam Lantinga 2019-07-07 09:10:56 -07:00
parent 67bb882e13
commit 680e7937e0

View File

@ -72,7 +72,9 @@ static int (*ALSA_snd_pcm_hw_params_set_period_size_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *); (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
static int (*ALSA_snd_pcm_hw_params_get_period_size) static int (*ALSA_snd_pcm_hw_params_get_period_size)
(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *); (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
static int (*ALSA_snd_pcm_hw_params_set_periods_near) static int (*ALSA_snd_pcm_hw_params_set_periods_min)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*ALSA_snd_pcm_hw_params_set_periods_first)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *); (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*ALSA_snd_pcm_hw_params_get_periods) static int (*ALSA_snd_pcm_hw_params_get_periods)
(const snd_pcm_hw_params_t *, unsigned int *, int *); (const snd_pcm_hw_params_t *, unsigned int *, int *);
@ -148,7 +150,8 @@ load_alsa_syms(void)
SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near); SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near); SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size); SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_near); SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_min);
SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_first);
SDL_ALSA_SYM(snd_pcm_hw_params_get_periods); SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near); SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size); SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
@ -462,14 +465,14 @@ ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params)
{ {
int status; int status;
snd_pcm_hw_params_t *hwparams; snd_pcm_hw_params_t *hwparams;
snd_pcm_uframes_t bufsize;
snd_pcm_uframes_t persize; snd_pcm_uframes_t persize;
unsigned int periods;
/* Copy the hardware parameters for this setup */ /* Copy the hardware parameters for this setup */
snd_pcm_hw_params_alloca(&hwparams); snd_pcm_hw_params_alloca(&hwparams);
ALSA_snd_pcm_hw_params_copy(hwparams, params); ALSA_snd_pcm_hw_params_copy(hwparams, params);
/* Prioritize matching the period size to the requested buffer size */ /* Attempt to match the period size to the requested buffer size */
persize = this->spec.samples; persize = this->spec.samples;
status = ALSA_snd_pcm_hw_params_set_period_size_near( status = ALSA_snd_pcm_hw_params_set_period_size_near(
this->hidden->pcm_handle, hwparams, &persize, NULL); this->hidden->pcm_handle, hwparams, &persize, NULL);
@ -477,10 +480,16 @@ ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params)
return(-1); return(-1);
} }
/* Next try to restrict the parameters to having only two periods */ /* Need to at least double buffer */
bufsize = this->spec.samples * 2; periods = 2;
status = ALSA_snd_pcm_hw_params_set_buffer_size_near( status = ALSA_snd_pcm_hw_params_set_periods_min(
this->hidden->pcm_handle, hwparams, &bufsize); this->hidden->pcm_handle, hwparams, &periods, NULL);
if ( status < 0 ) {
return(-1);
}
status = ALSA_snd_pcm_hw_params_set_periods_first(
this->hidden->pcm_handle, hwparams, &periods, NULL);
if ( status < 0 ) { if ( status < 0 ) {
return(-1); return(-1);
} }
@ -495,9 +504,9 @@ ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params)
/* This is useful for debugging */ /* This is useful for debugging */
if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) { if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) {
unsigned int periods = 0; snd_pcm_uframes_t bufsize;
ALSA_snd_pcm_hw_params_get_periods(hwparams, &periods, NULL); ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
fprintf(stderr, fprintf(stderr,
"ALSA: period size = %ld, periods = %u, buffer size = %lu\n", "ALSA: period size = %ld, periods = %u, buffer size = %lu\n",