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.
This commit is contained in:
Sam Lantinga 2019-06-04 17:32:15 -07:00
parent b5d3b6fc25
commit 723d014336
3 changed files with 3 additions and 29 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;