Fixed bug 4710 - audio/alsa: avoid configuring hardware parameters with only a single period
authorSam Lantinga <slouken@libsdl.org>
Sun, 07 Jul 2019 09:10:56 -0700
changeset 129283c4a4b1077cd
parent 12927 abb47c384db3
child 12929 d6c5eb7a0afb
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.
src/audio/alsa/SDL_alsa_audio.c
     1.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Wed Jul 03 15:57:55 2019 -0700
     1.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Sun Jul 07 09:10:56 2019 -0700
     1.3 @@ -72,7 +72,9 @@
     1.4    (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
     1.5  static int (*ALSA_snd_pcm_hw_params_get_period_size)
     1.6    (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
     1.7 -static int (*ALSA_snd_pcm_hw_params_set_periods_near)
     1.8 +static int (*ALSA_snd_pcm_hw_params_set_periods_min)
     1.9 +  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
    1.10 +static int (*ALSA_snd_pcm_hw_params_set_periods_first)
    1.11    (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
    1.12  static int (*ALSA_snd_pcm_hw_params_get_periods)
    1.13    (const snd_pcm_hw_params_t *, unsigned int *, int *);
    1.14 @@ -148,7 +150,8 @@
    1.15      SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
    1.16      SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
    1.17      SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
    1.18 -    SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_near);
    1.19 +    SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_min);
    1.20 +    SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_first);
    1.21      SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
    1.22      SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
    1.23      SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
    1.24 @@ -462,14 +465,14 @@
    1.25  {
    1.26      int status;
    1.27      snd_pcm_hw_params_t *hwparams;
    1.28 -    snd_pcm_uframes_t bufsize;
    1.29      snd_pcm_uframes_t persize;
    1.30 +    unsigned int periods;
    1.31  
    1.32      /* Copy the hardware parameters for this setup */
    1.33      snd_pcm_hw_params_alloca(&hwparams);
    1.34      ALSA_snd_pcm_hw_params_copy(hwparams, params);
    1.35  
    1.36 -    /* Prioritize matching the period size to the requested buffer size */
    1.37 +    /* Attempt to match the period size to the requested buffer size */
    1.38      persize = this->spec.samples;
    1.39      status = ALSA_snd_pcm_hw_params_set_period_size_near(
    1.40                  this->hidden->pcm_handle, hwparams, &persize, NULL);
    1.41 @@ -477,10 +480,16 @@
    1.42          return(-1);
    1.43      }
    1.44  
    1.45 -    /* Next try to restrict the parameters to having only two periods */
    1.46 -    bufsize = this->spec.samples * 2;
    1.47 -    status = ALSA_snd_pcm_hw_params_set_buffer_size_near(
    1.48 -                    this->hidden->pcm_handle, hwparams, &bufsize);
    1.49 +    /* Need to at least double buffer */
    1.50 +    periods = 2;
    1.51 +    status = ALSA_snd_pcm_hw_params_set_periods_min(
    1.52 +                this->hidden->pcm_handle, hwparams, &periods, NULL);
    1.53 +    if ( status < 0 ) {
    1.54 +        return(-1);
    1.55 +    }
    1.56 +
    1.57 +    status = ALSA_snd_pcm_hw_params_set_periods_first(
    1.58 +                this->hidden->pcm_handle, hwparams, &periods, NULL);
    1.59      if ( status < 0 ) {
    1.60          return(-1);
    1.61      }
    1.62 @@ -495,9 +504,9 @@
    1.63  
    1.64      /* This is useful for debugging */
    1.65      if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) {
    1.66 -        unsigned int periods = 0;
    1.67 +        snd_pcm_uframes_t bufsize;
    1.68  
    1.69 -        ALSA_snd_pcm_hw_params_get_periods(hwparams, &periods, NULL);
    1.70 +        ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
    1.71  
    1.72          fprintf(stderr,
    1.73              "ALSA: period size = %ld, periods = %u, buffer size = %lu\n",