From 723d01433665c9387c16e5d55f6ca08b7b8db988 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 4 Jun 2019 17:32:15 -0700 Subject: [PATCH] Fixed bug 4171 - SDL_GetQueuedAudioSize is broken with WASAPI Cameron Gutman I was trying to use SDL_GetQueuedAudioSize() to ensure my audio latency didn't get too high while streaming data in from the network. If I get more than N frames of audio queued, I know that the network is giving me more data than I can play and I need to drop some to keep latency low. This doesn't work well on WASAPI out of the box, due to the addition of GetPendingBytes() to the amount of queued data. As a terrible hack, I loop 100 times calling SDL_Delay(10) and SDL_GetQueuedAudioSize() before I ever call SDL_QueueAudio() to get a "baseline" amount that I then subtract from SDL_GetQueuedAudioSize() later. However, because this value isn't actually a constant, this hack can cause SDL_GetQueuedAudioSize() - baselineSize to be < 0. This means I have no accurate way of determining how much data is actually queued in SDL's audio buffer queue. The SDL_GetQueuedAudioSize() documentation says: "This is the number of bytes that have been queued for playback with SDL_QueueAudio(), but have not yet been sent to the hardware." Yet, SDL_GetQueuedAudioSize() returns > 0 value when SDL_QueueAudio() has never been called. Based on that documentation, I believe the current behavior contradicts the documented behavior of this function and should be changed in line with Boris's patch. I understand that exposing the IAudioClient::GetCurrentPadding() value is useful, but a solution there needs to take into account what of that data is silence inserted by SDL and what is actual data queued by the user with SDL_QueueAudio(). Until that happens, I think the best approach is to remove the GetPendingBytes() call until SDL is able to keep track of queued data to make sense of it. This would make SDL_GetQueuedAudioSize() possible to use accurately with WASAPI. --- src/audio/SDL_audio.c | 15 +++------------ src/audio/SDL_sysaudio.h | 1 - src/audio/wasapi/SDL_wasapi.c | 16 ---------------- 3 files changed, 3 insertions(+), 29 deletions(-) diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index ad010e44dc630..e8c6cfb19cddd 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -248,12 +248,6 @@ SDL_AudioPlayDevice_Default(_THIS) { /* no-op. */ } -static int -SDL_AudioGetPendingBytes_Default(_THIS) -{ - return 0; -} - static Uint8 * SDL_AudioGetDeviceBuf_Default(_THIS) { @@ -361,7 +355,6 @@ finish_audio_entry_points_init(void) FILL_STUB(BeginLoopIteration); FILL_STUB(WaitDevice); FILL_STUB(PlayDevice); - FILL_STUB(GetPendingBytes); FILL_STUB(GetDeviceBuf); FILL_STUB(CaptureFromDevice); FILL_STUB(FlushCapture); @@ -654,11 +647,9 @@ SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid) } /* Nothing to do unless we're set up for queueing. */ - if (device->callbackspec.callback == SDL_BufferQueueDrainCallback) { - current_audio.impl.LockDevice(device); - retval = ((Uint32) SDL_CountDataQueue(device->buffer_queue)) + current_audio.impl.GetPendingBytes(device); - current_audio.impl.UnlockDevice(device); - } else if (device->callbackspec.callback == SDL_BufferQueueFillCallback) { + if (device->callbackspec.callback == SDL_BufferQueueDrainCallback || + device->callbackspec.callback == SDL_BufferQueueFillCallback) + { current_audio.impl.LockDevice(device); retval = (Uint32) SDL_CountDataQueue(device->buffer_queue); current_audio.impl.UnlockDevice(device); diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h index 284b626c7b3f2..5a87a3b60eac6 100644 --- a/src/audio/SDL_sysaudio.h +++ b/src/audio/SDL_sysaudio.h @@ -71,7 +71,6 @@ typedef struct SDL_AudioDriverImpl void (*BeginLoopIteration)(_THIS); /* Called by audio thread at top of loop */ void (*WaitDevice) (_THIS); void (*PlayDevice) (_THIS); - int (*GetPendingBytes) (_THIS); Uint8 *(*GetDeviceBuf) (_THIS); int (*CaptureFromDevice) (_THIS, void *buffer, int buflen); void (*FlushCapture) (_THIS); diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c index 4011cb1aca489..67cbccf30212b 100644 --- a/src/audio/wasapi/SDL_wasapi.c +++ b/src/audio/wasapi/SDL_wasapi.c @@ -161,21 +161,6 @@ WASAPI_DetectDevices(void) WASAPI_EnumerateEndpoints(); } -static int -WASAPI_GetPendingBytes(_THIS) -{ - UINT32 frames = 0; - - /* it's okay to fail here; we'll deal with failures in the audio thread. */ - /* FIXME: need a lock around checking this->hidden->client */ - if (this->hidden->client != NULL) { /* definitely activated? */ - if (FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &frames))) { - return 0; /* oh well. */ - } - } - return ((int) frames) * this->hidden->framesize; -} - static SDL_INLINE SDL_bool WasapiFailed(_THIS, const HRESULT err) { @@ -765,7 +750,6 @@ WASAPI_Init(SDL_AudioDriverImpl * impl) impl->OpenDevice = WASAPI_OpenDevice; impl->PlayDevice = WASAPI_PlayDevice; impl->WaitDevice = WASAPI_WaitDevice; - impl->GetPendingBytes = WASAPI_GetPendingBytes; impl->GetDeviceBuf = WASAPI_GetDeviceBuf; impl->CaptureFromDevice = WASAPI_CaptureFromDevice; impl->FlushCapture = WASAPI_FlushCapture;