SDL: alsa: fixed disconnecting the microphone when opened

From 051ce0ff8954e75c2f0a1194a35656adadecc09b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 20 Jun 2025 15:14:58 -0700
Subject: [PATCH] alsa: fixed disconnecting the microphone when opened

Some devices take some time for data to become available, so we'll keep waiting as long as necessary for them to provide data.
---
 src/audio/alsa/SDL_alsa_audio.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index 199bff4dea6a1..9cc991299e968 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -351,21 +351,18 @@ static bool ALSA_WaitDevice(SDL_AudioDevice *device)
     const int sample_frames = device->sample_frames;
     const int fulldelay = (int) ((((Uint64) sample_frames) * 1000) / device->spec.freq);
     const int delay = SDL_clamp(fulldelay, 1, 5);
-    int total_delays = 0;
 
-    SDL_assert(fulldelay > 0);  // so the `fulldelay * 5` below produces a reasonable result.
-
-    while (!SDL_GetAtomicInt(&device->shutdown) && (ALSA_snd_pcm_avail(device->hidden->pcm) < sample_frames)) {
-        if (total_delays >= (fulldelay * 5)) {
-            // Hmm, not much we can do - probably disconnected, abort
-            //SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA: hardware seems to have frozen, giving up on it.");
+    while (!SDL_GetAtomicInt(&device->shutdown)) {
+        const int rc = ALSA_snd_pcm_avail(device->hidden->pcm);
+        if (rc < 0) {
+            SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA wait failed (unrecoverable): %s", ALSA_snd_strerror(rc));
             return false;
-        } else {
-            SDL_Delay(delay);
-            total_delays += delay;  // THIS IS NOT EXACT, but just so we don't wait forever on problems...
         }
+        if (rc >= sample_frames) {
+            break;
+        }
+        SDL_Delay(delay);
     }
-
     return true;
 }