Abort looping in SoundStream::streamData if an OpenAL error occurs that would have caused it to never terminate. Fixes #1831

This commit is contained in:
binary1248 2022-02-19 03:23:25 +01:00 committed by Lukas Dürrenberger
parent c5f3aeca72
commit 8e95d1c73a
3 changed files with 38 additions and 0 deletions

View File

@ -38,6 +38,15 @@
#endif #endif
#endif #endif
namespace
{
// A nested named namespace is used here to allow unity builds of SFML.
namespace AlCheckImpl
{
thread_local ALenum lastError(AL_NO_ERROR);
}
}
namespace sf namespace sf
{ {
namespace priv namespace priv
@ -50,6 +59,8 @@ void alCheckError(const std::filesystem::path& file, unsigned int line, const ch
if (errorCode != AL_NO_ERROR) if (errorCode != AL_NO_ERROR)
{ {
AlCheckImpl::lastError = errorCode;
std::string error = "Unknown error"; std::string error = "Unknown error";
std::string description = "No description"; std::string description = "No description";
@ -101,6 +112,13 @@ void alCheckError(const std::filesystem::path& file, unsigned int line, const ch
} }
} }
////////////////////////////////////////////////////////////
ALenum alGetLastErrorImpl()
{
return std::exchange(AlCheckImpl::lastError, AL_NO_ERROR);
}
} // namespace priv } // namespace priv
} // namespace sf } // namespace sf

View File

@ -56,11 +56,13 @@ namespace priv
// If in debug mode, perform a test on every call // If in debug mode, perform a test on every call
// The do-while loop is needed so that alCheck can be used as a single statement in if/else branches // The do-while loop is needed so that alCheck can be used as a single statement in if/else branches
#define alCheck(expr) do { expr; sf::priv::alCheckError(__FILE__, __LINE__, #expr); } while (false) #define alCheck(expr) do { expr; sf::priv::alCheckError(__FILE__, __LINE__, #expr); } while (false)
#define alGetLastError sf::priv::alGetLastErrorImpl
#else #else
// Else, we don't add any overhead // Else, we don't add any overhead
#define alCheck(expr) (expr) #define alCheck(expr) (expr)
#define alGetLastError alGetError
#endif #endif
@ -75,6 +77,15 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void alCheckError(const std::filesystem::path& file, unsigned int line, const char* expression); void alCheckError(const std::filesystem::path& file, unsigned int line, const char* expression);
////////////////////////////////////////////////////////////
/// Get the last OpenAL error on this thread
///
/// \return The last OpenAL error on this thread
///
////////////////////////////////////////////////////////////
ALenum alGetLastErrorImpl();
} // namespace priv } // namespace priv
} // namespace sf } // namespace sf

View File

@ -385,6 +385,15 @@ void SoundStream::streamData()
} }
} }
// Check if any error has occurred
if (alGetLastError() != AL_NO_ERROR)
{
// Abort streaming (exit main loop)
std::scoped_lock lock(m_threadMutex);
m_isStreaming = false;
break;
}
// Leave some time for the other threads if the stream is still playing // Leave some time for the other threads if the stream is still playing
if (SoundSource::getStatus() != Stopped) if (SoundSource::getStatus() != Stopped)
sleep(m_processingInterval); sleep(m_processingInterval);