Fix the "Could not lock audio buffer for filling" issue that has been plaguing Windows 7 users, and to some extent also other Windows versions when using the DSound2 audio player.

The actual issue was that sometimes a zero-length buffer was requested to be locked, and that always fails. On Windows 7 this was much more common, probably related to the timer coalescing introduced there causing two waits to end in a row.

Originally committed to SVN as r3740.
This commit is contained in:
Niels Martin Hansen 2009-10-27 16:54:14 +00:00
parent 3849ab0ebd
commit 5f5c5569b8
1 changed files with 33 additions and 15 deletions

View File

@ -127,7 +127,7 @@ class DirectSoundPlayer2Thread {
is_playing,
error_happened;
wxChar *error_message;
const wxChar *error_message;
double volume;
int64_t start_frame;
int64_t end_frame;
@ -386,14 +386,22 @@ do_fill_buffer:
int bytes_needed = (int)play_cursor - (int)buffer_offset;
if (bytes_needed < 0) bytes_needed += (int)bufSize;
// Requesting zero buffer makes Windows cry, and zero buffer seemed to be
// a common request on Windows 7. (Maybe related to the new timer coalescing?)
// We'll probably get non-zero bytes requested on the next iteration.
if (bytes_needed == 0)
break;
DWORD buf1sz, buf2sz;
void *buf1, *buf2;
assert(buffer_offset < bufSize);
assert(bytes_needed <= bufSize);
HRESULT res = bfr->Lock(buffer_offset, bytes_needed, &buf1, &buf1sz, &buf2, &buf2sz, 0);
while (FAILED(res)) // yes, while, so I can break out of it without a goto!
{
if (res == DSERR_BUFFERLOST)
switch (res)
{
case DSERR_BUFFERLOST:
// Try to regain the buffer
// When the buffer was lost the entire contents was lost too, so we have to start over
if (SUCCEEDED(bfr->Restore()) &&
@ -403,11 +411,21 @@ do_fill_buffer:
wxLogDebug(_T("DirectSoundPlayer2: Lost and restored buffer"));
break;
}
REPORT_ERROR("Lost buffer and could not restore it.")
}
REPORT_ERROR("Could not lock buffer for filling.")
case DSERR_INVALIDPARAM:
REPORT_ERROR("Invalid parameters to IDirectSoundBuffer8::Lock().")
case DSERR_INVALIDCALL:
REPORT_ERROR("Invalid call to IDirectSoundBuffer8::Lock().")
case DSERR_PRIOLEVELNEEDED:
REPORT_ERROR("Incorrect priority level set on DirectSoundBuffer8 object.")
default:
if (FAILED(res))
REPORT_ERROR("Could not lock audio buffer, unknown error.")
break;
}
DWORD bytes_filled = FillAndUnlockBuffers(buf1, buf1sz, buf2, buf2sz, next_input_frame, bfr.obj);