Skip to content

Commit 051ce0f

Browse files
sloukenicculus
authored andcommitted
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.
1 parent 5fcc83d commit 051ce0f

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/audio/alsa/SDL_alsa_audio.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,18 @@ static bool ALSA_WaitDevice(SDL_AudioDevice *device)
351351
const int sample_frames = device->sample_frames;
352352
const int fulldelay = (int) ((((Uint64) sample_frames) * 1000) / device->spec.freq);
353353
const int delay = SDL_clamp(fulldelay, 1, 5);
354-
int total_delays = 0;
355354

356-
SDL_assert(fulldelay > 0); // so the `fulldelay * 5` below produces a reasonable result.
357-
358-
while (!SDL_GetAtomicInt(&device->shutdown) && (ALSA_snd_pcm_avail(device->hidden->pcm) < sample_frames)) {
359-
if (total_delays >= (fulldelay * 5)) {
360-
// Hmm, not much we can do - probably disconnected, abort
361-
//SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA: hardware seems to have frozen, giving up on it.");
355+
while (!SDL_GetAtomicInt(&device->shutdown)) {
356+
const int rc = ALSA_snd_pcm_avail(device->hidden->pcm);
357+
if (rc < 0) {
358+
SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "ALSA wait failed (unrecoverable): %s", ALSA_snd_strerror(rc));
362359
return false;
363-
} else {
364-
SDL_Delay(delay);
365-
total_delays += delay; // THIS IS NOT EXACT, but just so we don't wait forever on problems...
366360
}
361+
if (rc >= sample_frames) {
362+
break;
363+
}
364+
SDL_Delay(delay);
367365
}
368-
369366
return true;
370367
}
371368

0 commit comments

Comments
 (0)