From 746bb9c8cab04d44774ea1338c14f903cbaff3ae Mon Sep 17 00:00:00 2001 From: Cobaltergeist Date: Sat, 24 Sep 2016 13:52:41 -0700 Subject: [PATCH] Changed fillAndPushBuffer to use a retry loop. --- include/SFML/Audio/SoundStream.hpp | 3 +- src/SFML/Audio/SoundStream.cpp | 47 +++++++++++++++++------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index 6fbbb6db2..6dd0707ab 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -282,7 +282,8 @@ private: enum { - BufferCount = 3 ///< Number of audio buffers used by the streaming loop + BufferCount = 3, ///< Number of audio buffers used by the streaming loop + BufferRetries = 2 ///< Number of retries (excluding initial try) for onGetData() }; //////////////////////////////////////////////////////////// diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index ae6224933..f6eeeb8dd 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -399,37 +399,37 @@ bool SoundStream::fillAndPushBuffer(unsigned int bufferNum, bool immediateLoop) { bool requestStop = false; - // Acquire audio data + // Acquire audio data, also address EOF and error cases if they occur Chunk data = {NULL, 0}; - if (!onGetData(data)) + for (Uint32 retryCount = 0; !onGetData(data) && (retryCount < BufferRetries); ++retryCount) { // Mark the buffer as the last one (so that we know when to reset the playing position) m_endBuffers[bufferNum] = true; // Check if the stream must loop or stop - if (m_loop) - { - // Return to the beginning of the stream source - onSeek(Time::Zero); - - // If we previously had no data, try to fill the buffer once again - if (!data.samples || (data.sampleCount == 0)) - { - // If immediateLoop is specified, we have to immediately adjust the sample count - if (immediateLoop) - { - // We just tried to begin preloading at EOF: reset the sample count - m_samplesProcessed = 0; - m_endBuffers[bufferNum] = false; - } - return fillAndPushBuffer(bufferNum); - } - } - else + if (!m_loop) { // Not looping: request stop requestStop = true; + break; } + + // Return to the beginning of the stream source + onSeek(Time::Zero); + + // If we got data, break and process it, else try to fill the buffer once again + if (data.samples && data.sampleCount) + break; + + // If immediateLoop is specified, we have to immediately adjust the sample count + if (immediateLoop) + { + // We just tried to begin preloading at EOF: reset the sample count + m_samplesProcessed = 0; + m_endBuffers[bufferNum] = false; + } + + // We're a looping sound that got no data, so we retry onGetData() } // Fill the buffer if some data was returned @@ -444,6 +444,11 @@ bool SoundStream::fillAndPushBuffer(unsigned int bufferNum, bool immediateLoop) // Push it into the sound queue alCheck(alSourceQueueBuffers(m_source, 1, &buffer)); } + else + { + // If we get here, we most likely ran out of retries + requestStop = true; + } return requestStop; }