diff options
author | erorcun <erorcunerorcun@hotmail.com.tr> | 2021-06-27 14:42:52 +0300 |
---|---|---|
committer | erorcun <erorcunerorcun@hotmail.com.tr> | 2021-07-10 23:14:08 +0300 |
commit | db4ae18e5d7a582c01c759409ba909b24d640e40 (patch) | |
tree | e855cea2d903e71a09c6303d92fd53739e1aa354 | |
parent | 5458632c405fd81e76e625ba9dfabe8831509d1b (diff) |
Remove waiting for stream closure in multi-thread audio
-rw-r--r-- | src/audio/oal/stream.cpp | 40 | ||||
-rw-r--r-- | src/audio/oal/stream.h | 2 | ||||
-rw-r--r-- | src/audio/sampman_oal.cpp | 1 |
3 files changed, 23 insertions, 20 deletions
diff --git a/src/audio/oal/stream.cpp b/src/audio/oal/stream.cpp index 8b627e2a..0209202a 100644 --- a/src/audio/oal/stream.cpp +++ b/src/audio/oal/stream.cpp @@ -36,6 +36,7 @@ std::mutex gAudioThreadQueueMutex; std::condition_variable gAudioThreadCv; bool gAudioThreadTerm = false; std::queue<CStream*> gStreamsToProcess; // values are not unique, we will handle that ourself +std::queue<std::pair<IDecoder*, void*>> gStreamsToClose; #else #include "stream.h" #endif @@ -1012,7 +1013,11 @@ CStream::FlagAsToBeProcessed(bool close) return; gAudioThreadQueueMutex.lock(); - gStreamsToProcess.push(this); + if (close) + gStreamsToClose.push(std::pair<IDecoder*, void*>(m_pSoundFile ? m_pSoundFile : nil, m_pBuffer ? m_pBuffer : nil)); + else + gStreamsToProcess.push(this); + gAudioThreadQueueMutex.unlock(); gAudioThreadCv.notify_one(); @@ -1026,10 +1031,22 @@ void audioFileOpsThread() { // Just a semaphore std::unique_lock<std::mutex> queueMutex(gAudioThreadQueueMutex); - gAudioThreadCv.wait(queueMutex, [] { return gStreamsToProcess.size() > 0 || gAudioThreadTerm; }); + gAudioThreadCv.wait(queueMutex, [] { return gStreamsToProcess.size() > 0 || gStreamsToClose.size() > 0 || gAudioThreadTerm; }); if (gAudioThreadTerm) return; + if (!gStreamsToClose.empty()) { + auto streamToClose = gStreamsToClose.front(); + gStreamsToClose.pop(); + if (streamToClose.first) { // pSoundFile + delete streamToClose.first; + } + + if (streamToClose.second) { // pBuffer + free(streamToClose.second); + } + } + if (!gStreamsToProcess.empty()) { stream = gStreamsToProcess.front(); gStreamsToProcess.pop(); @@ -1044,18 +1061,6 @@ void audioFileOpsThread() do { if (!stream->IsOpened()) { - // We MUST do that here, because we release mutex for m_pSoundFile->Seek() and m_pSoundFile->Decode() since they're costly - if (stream->m_pSoundFile) { - delete stream->m_pSoundFile; - stream->m_pSoundFile = nil; - } - - if (stream->m_pBuffer) { - free(stream->m_pBuffer); - stream->m_pBuffer = nil; - } - lock.unlock(); - stream->m_closeCv.notify_one(); break; } @@ -1166,10 +1171,6 @@ bool CStream::Open(const char* filename, uint32 overrideSampleRate) #ifdef MULTITHREADED_AUDIO std::unique_lock<std::mutex> lock(m_mutex); - CStream *stream = this; - // Wait for thread to close old one. We can't close it here, because the thread might be running Decode() or Seek(), while mutex is released - m_closeCv.wait(lock, [this] { return m_pSoundFile == nil && m_pBuffer == nil; }); - m_bDoSeek = false; m_SeekPos = 0; #endif @@ -1255,7 +1256,8 @@ void CStream::Close() Stop(); ClearBuffers(); m_bIExist = false; - // clearing buffer queues are not needed. after m_bIExist is cleared, this stream is ded + std::queue<std::pair<ALuint, ALuint>>().swap(m_fillBuffers); + tsQueue<std::pair<ALuint, ALuint>>().swapNts(m_queueBuffers); // TSness not required, mutex is acquired } FlagAsToBeProcessed(true); diff --git a/src/audio/oal/stream.h b/src/audio/oal/stream.h index 10b595c1..f0456925 100644 --- a/src/audio/oal/stream.h +++ b/src/audio/oal/stream.h @@ -127,7 +127,7 @@ public: std::mutex m_mutex; std::queue<std::pair<ALuint, ALuint>> m_fillBuffers; // left and right buffer tsQueue<std::pair<ALuint, ALuint>> m_queueBuffers; - std::condition_variable m_closeCv; +// std::condition_variable m_closeCv; bool m_bDoSeek; uint32 m_SeekPos; bool m_bIExist; diff --git a/src/audio/sampman_oal.cpp b/src/audio/sampman_oal.cpp index 6c25cf79..b96df8c4 100644 --- a/src/audio/sampman_oal.cpp +++ b/src/audio/sampman_oal.cpp @@ -526,6 +526,7 @@ _FindMP3s(void) { total_ms = aStream[0]->GetLengthMS(); aStream[0]->Close(); + OutputDebugString(fd.cFileName); _pMP3List = new tMP3Entry; |