From 5f5c5569b8f170ce0921e5a1c2ba72114e36dcbd Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Tue, 27 Oct 2009 16:54:14 +0000 Subject: [PATCH] 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. --- aegisub/src/audio_player_dsound2.cpp | 48 +++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/aegisub/src/audio_player_dsound2.cpp b/aegisub/src/audio_player_dsound2.cpp index 605243488..c336c287b 100644 --- a/aegisub/src/audio_player_dsound2.cpp +++ b/aegisub/src/audio_player_dsound2.cpp @@ -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,28 +386,46 @@ 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! + switch (res) { - if (res == DSERR_BUFFERLOST) + 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()) && + SUCCEEDED(bfr->Lock(0, bufSize, &buf1, &buf1sz, &buf2, &buf2sz, 0)) && + SUCCEEDED(bfr->Play(0, 0, DSBPLAY_LOOPING))) { - // 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()) && - SUCCEEDED(bfr->Lock(0, bufSize, &buf1, &buf1sz, &buf2, &buf2sz, 0)) && - SUCCEEDED(bfr->Play(0, 0, DSBPLAY_LOOPING))) - { - wxLogDebug(_T("DirectSoundPlayer2: Lost and restored buffer")); - break; - } - - REPORT_ERROR("Lost buffer and could not restore it.") + 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);