audio: Try to keep callbacks firing at normal pace when device is lost.

--HG--
extra : histedit_source : 6af6e582c33b800c543fd1f4d73e99fe4cbba824
This commit is contained in:
Ryan C. Gordon 2017-02-26 00:39:22 -05:00
parent 844a3da31c
commit ca264fd374

View file

@ -688,13 +688,12 @@ SDL_RunAudio(void *devicep)
while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) { while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) {
data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL; data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL;
if (data == NULL) { const int got = SDL_AudioStreamGet(device->stream, data ? data : device->work_buffer, device->spec.size);
SDL_AudioStreamClear(device->stream);
SDL_Delay(delay);
break;
} else {
const int got = SDL_AudioStreamGet(device->stream, data, device->spec.size);
SDL_assert((got < 0) || (got == device->spec.size)); SDL_assert((got < 0) || (got == device->spec.size));
if (data == NULL) { /* device is having issues... */
SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */
} else {
if (got != device->spec.size) { if (got != device->spec.size) {
SDL_memset(data, device->spec.silence, device->spec.size); SDL_memset(data, device->spec.silence, device->spec.size);
} }
@ -770,7 +769,10 @@ SDL_CaptureAudio(void *devicep)
and block when there isn't data so this thread isn't eating CPU. and block when there isn't data so this thread isn't eating CPU.
But we don't process it further or call the app's callback. */ But we don't process it further or call the app's callback. */
while (SDL_AtomicGet(&device->enabled) && (still_need > 0)) { if (!SDL_AtomicGet(&device->enabled)) {
SDL_Delay(delay); /* try to keep callback firing at normal pace. */
} else {
while (still_need > 0) {
const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need); const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
SDL_assert(rc <= still_need); /* device should not overflow buffer. :) */ SDL_assert(rc <= still_need); /* device should not overflow buffer. :) */
if (rc > 0) { if (rc > 0) {
@ -781,6 +783,7 @@ SDL_CaptureAudio(void *devicep)
break; break;
} }
} }
}
if (still_need > 0) { if (still_need > 0) {
/* Keep any data we already read, silence the rest. */ /* Keep any data we already read, silence the rest. */