Skip to content

Commit e458305

Browse files
committed
Clamp the audio drain delay to 100 ms
Fixes #9829 (cherry picked from commit 0882623) (cherry picked from commit 6db23fa)
1 parent 3ff58bf commit e458305

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/audio/SDL_audio.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
678678
int data_len = 0;
679679
Uint8 *data;
680680
Uint8 *device_buf_keepsafe = NULL;
681+
Uint32 delay;
681682

682683
SDL_assert(!device->iscapture);
683684

@@ -761,7 +762,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
761762
SDL_assert((got <= 0) || (got == device->spec.size));
762763

763764
if (data == NULL) { /* device is having issues... */
764-
const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
765+
delay = ((device->spec.samples * 1000) / device->spec.freq);
765766
SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */
766767
} else {
767768
if (got != device->spec.size) {
@@ -781,7 +782,7 @@ static int SDLCALL SDL_RunAudio(void *userdata)
781782
}
782783
} else if (data == device->work_buffer) {
783784
/* nothing to do; pause like we queued a buffer to play. */
784-
const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
785+
delay = ((device->spec.samples * 1000) / device->spec.freq);
785786
SDL_Delay(delay);
786787
} else { /* writing directly to the device. */
787788
/* queue this buffer and wait for it to finish playing. */
@@ -791,7 +792,11 @@ static int SDLCALL SDL_RunAudio(void *userdata)
791792
}
792793

793794
/* Wait for the audio to drain. */
794-
SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2);
795+
delay = ((device->spec.samples * 1000) / device->spec.freq) * 2;
796+
if (delay > 100) {
797+
delay = 100;
798+
}
799+
SDL_Delay(delay);
795800

796801
current_audio.impl.ThreadDeinit(device);
797802

src/audio/alsa/SDL_alsa_audio.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ static void ALSA_CloseDevice(_THIS)
470470
ALSA_snd_pcm_drop() can hang, so don't use that.
471471
*/
472472
Uint32 delay = ((this->spec.samples * 1000) / this->spec.freq) * 2;
473+
if (delay > 100) {
474+
delay = 100;
475+
}
473476
SDL_Delay(delay);
474477

475478
ALSA_snd_pcm_close(this->hidden->pcm_handle);

0 commit comments

Comments
 (0)