diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 5353df41..91918ef0 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -611,7 +611,10 @@ void Game::init(bool initForPreviewOnly) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //sounds + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(); + soundRenderer= SoundRenderer::getInstance(); Tileset *tileset= world.getTileset(); AmbientSounds *ambientSounds= tileset->getAmbientSounds(); diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index d82a7e97..88efece3 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -301,7 +301,7 @@ void Program::loopWorker() { if(prevState == this->programState) { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(soundThreadManager == NULL) { + if(soundThreadManager == NULL || soundThreadManager->isThreadExecutionLagging()) { SoundRenderer::getInstance().update(); if(chronoUpdateLoop.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] SoundRenderer::getInstance().update() took msecs: %lld, updateCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chronoUpdateLoop.getMillis(),updateCount); if(chronoUpdateLoop.getMillis() > 0) chronoUpdateLoop.start(); @@ -553,10 +553,11 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ } // Run sound streaming in a background thread if enabled - if(config.getBool("ThreadedSoundStream","false") == true) { - BaseThread::shutdownAndWait(soundThreadManager); - delete soundThreadManager; - soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50); + if(SoundRenderer::getInstance().runningThreaded() == true) { + if(BaseThread::shutdownAndWait(soundThreadManager) == true) { + delete soundThreadManager; + } + soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,10); soundThreadManager->setUniqueID(__FILE__); soundThreadManager->start(); } diff --git a/source/glest_game/menu/main_menu.cpp b/source/glest_game/menu/main_menu.cpp index b809f3c3..071d2fdb 100644 --- a/source/glest_game/menu/main_menu.cpp +++ b/source/glest_game/menu/main_menu.cpp @@ -76,8 +76,8 @@ MainMenu::~MainMenu() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - soundRenderer.stopAllSounds(); + //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + //soundRenderer.stopAllSounds(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 4b187b17..ef7bd4f7 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -1361,6 +1361,10 @@ void MenuStateCustomGame::update() { Chrono chrono; chrono.start(); + // Test openal buffer underrun issue + //sleep(200); + // END + MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL)); try { diff --git a/source/glest_game/sound/sound_renderer.cpp b/source/glest_game/sound/sound_renderer.cpp index 9cefb158..e298ca06 100644 --- a/source/glest_game/sound/sound_renderer.cpp +++ b/source/glest_game/sound/sound_renderer.cpp @@ -37,8 +37,10 @@ SoundRenderer::SoundRenderer() { soundPlayer = NULL; loadConfig(); - runThreadSafe = false; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + Config &config= Config::getInstance(); + runThreadSafe = config.getBool("ThreadedSoundStream","true"); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] runThreadSafe = %d\n",__FILE__,__FUNCTION__,__LINE__,runThreadSafe); } bool SoundRenderer::init(Window *window) { @@ -46,14 +48,18 @@ bool SoundRenderer::init(Window *window) { SoundInterface &si= SoundInterface::getInstance(); FactoryRepository &fr= FactoryRepository::getInstance(); - Config &config= Config::getInstance(); - runThreadSafe = config.getBool("ThreadedSoundStream","false"); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + Config &config= Config::getInstance(); si.setFactory(fr.getSoundFactory(config.getString("FactorySound"))); stopAllSounds(); + MutexSafeWrapper safeMutex(NULL); + if(runThreadSafe == true) { + safeMutex.setMutex(&mutex); + } + soundPlayer= si.newSoundPlayer(); if(soundPlayer != NULL) { SoundPlayerParams soundPlayerParams; @@ -61,6 +67,7 @@ bool SoundRenderer::init(Window *window) { soundPlayerParams.strBufferCount= config.getInt("SoundStreamingBuffers"); soundPlayer->init(&soundPlayerParams); } + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -89,6 +96,10 @@ SoundRenderer::~SoundRenderer() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutex(NULL); + if(runThreadSafe == true) { + safeMutex.setMutex(&mutex); + } delete soundPlayer; soundPlayer = NULL; diff --git a/source/glest_game/sound/sound_renderer.h b/source/glest_game/sound/sound_renderer.h index ce4f2eb8..4250b293 100644 --- a/source/glest_game/sound/sound_renderer.h +++ b/source/glest_game/sound/sound_renderer.h @@ -79,6 +79,8 @@ public: void loadConfig(); bool wasInitOk() const; + + bool runningThreaded() const { return runThreadSafe; } }; }}//end namespace diff --git a/source/shared_lib/include/platform/common/simple_threads.h b/source/shared_lib/include/platform/common/simple_threads.h index 1c75b57e..b7eb5a61 100644 --- a/source/shared_lib/include/platform/common/simple_threads.h +++ b/source/shared_lib/include/platform/common/simple_threads.h @@ -63,6 +63,9 @@ protected: bool taskSignalled; bool needTaskSignal; + Mutex mutexLastExecuteTimestamp; + time_t lastExecuteTimestamp; + public: SimpleTaskThread(); SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface, @@ -74,6 +77,8 @@ public: void setTaskSignalled(bool value); bool getTaskSignalled(); + + bool isThreadExecutionLagging(); }; // ===================================================== diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index 5a425baa..fc1c027a 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -85,6 +85,18 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter this->millisecsBetweenExecutions = millisecsBetweenExecutions; this->needTaskSignal = needTaskSignal; setTaskSignalled(false); + + MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp); + lastExecuteTimestamp = time(NULL); +} + +bool SimpleTaskThread::isThreadExecutionLagging() { + bool result = false; + MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp); + result = (difftime(time(NULL),lastExecuteTimestamp) >= 5.0); + safeMutex.ReleaseLock(); + + return result; } bool SimpleTaskThread::canShutdown(bool deleteSelfIfShutdownDelayed) { @@ -124,8 +136,12 @@ void SimpleTaskThread::execute() { else if(runTask == true) { //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str()); if(getQuitStatus() == false) { - ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); this->simpleTaskInterface->simpleTask(this); + + MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp); + lastExecuteTimestamp = time(NULL); + safeMutex.ReleaseLock(); } //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str()); }