Explorar o código

Clamp the audio drain delay to 100 ms

Fixes https://github.com/libsdl-org/SDL/issues/9829

(cherry picked from commit 08826230923dfb70e4558a41179ac0744e69fdb8)
Sam Lantinga hai 1 mes
pai
achega
6db23faa44
Modificáronse 2 ficheiros con 11 adicións e 3 borrados
  1. 8 3
      src/audio/SDL_audio.c
  2. 3 0
      src/audio/alsa/SDL_alsa_audio.c

+ 8 - 3
src/audio/SDL_audio.c

@@ -678,6 +678,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
     int data_len = 0;
     int data_len = 0;
     Uint8 *data;
     Uint8 *data;
     Uint8 *device_buf_keepsafe = NULL;
     Uint8 *device_buf_keepsafe = NULL;
+    Uint32 delay;
 
 
     SDL_assert(!device->iscapture);
     SDL_assert(!device->iscapture);
 
 
@@ -761,7 +762,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
                 SDL_assert((got <= 0) || (got == device->spec.size));
                 SDL_assert((got <= 0) || (got == device->spec.size));
 
 
                 if (data == NULL) { /* device is having issues... */
                 if (data == NULL) { /* device is having issues... */
-                    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
+                    delay = ((device->spec.samples * 1000) / device->spec.freq);
                     SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */
                     SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */
                 } else {
                 } else {
                     if (got != device->spec.size) {
                     if (got != device->spec.size) {
@@ -781,7 +782,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
             }
             }
         } else if (data == device->work_buffer) {
         } else if (data == device->work_buffer) {
             /* nothing to do; pause like we queued a buffer to play. */
             /* nothing to do; pause like we queued a buffer to play. */
-            const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
+            delay = ((device->spec.samples * 1000) / device->spec.freq);
             SDL_Delay(delay);
             SDL_Delay(delay);
         } else { /* writing directly to the device. */
         } else { /* writing directly to the device. */
             /* queue this buffer and wait for it to finish playing. */
             /* queue this buffer and wait for it to finish playing. */
@@ -791,7 +792,11 @@ static int SDLCALL SDL_RunAudio(void *userdata)
     }
     }
 
 
     /* Wait for the audio to drain. */
     /* Wait for the audio to drain. */
-    SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2);
+    delay = ((device->spec.samples * 1000) / device->spec.freq) * 2;
+    if (delay > 100) {
+        delay = 100;
+    }
+    SDL_Delay(delay);
 
 
     current_audio.impl.ThreadDeinit(device);
     current_audio.impl.ThreadDeinit(device);
 
 

+ 3 - 0
src/audio/alsa/SDL_alsa_audio.c

@@ -472,6 +472,9 @@ static void ALSA_CloseDevice(_THIS)
            ALSA_snd_pcm_drop() can hang, so don't use that.
            ALSA_snd_pcm_drop() can hang, so don't use that.
          */
          */
         Uint32 delay = ((this->spec.samples * 1000) / this->spec.freq) * 2;
         Uint32 delay = ((this->spec.samples * 1000) / this->spec.freq) * 2;
+        if (delay > 100) {
+            delay = 100;
+        }
         SDL_Delay(delay);
         SDL_Delay(delay);
 
 
         ALSA_snd_pcm_close(this->hidden->pcm_handle);
         ALSA_snd_pcm_close(this->hidden->pcm_handle);