소스 검색

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.
Sam Lantinga 10 달 전
부모
커밋
051ce0ff89
1개의 변경된 파일8개의 추가작업 그리고 11개의 파일을 삭제
  1. 8 11
      src/audio/alsa/SDL_alsa_audio.c

+ 8 - 11
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;
 }