diff options
author | Chris Robinson <[email protected]> | 2018-02-01 02:21:14 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-02-01 02:24:44 -0800 |
commit | 7a538141c913d4cd0ebc292c651d399a40d5062d (patch) | |
tree | 2ae0f753fd88884fd87d9369f12cbebfa57a4d4c /examples/alffplay.cpp | |
parent | bcdc399029ee2dad87ec3dc608eaa44c9dd9614b (diff) |
Signal a condition variable when a buffer completes in alffplay
Diffstat (limited to 'examples/alffplay.cpp')
-rw-r--r-- | examples/alffplay.cpp | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 73c3e028..1c8d5697 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -237,6 +237,7 @@ struct AudioState { ALsizei mFrameSize{0}; std::mutex mSrcMutex; + std::condition_variable mSrcCond; ALuint mSource{0}; std::vector<ALuint> mBuffers; ALsizei mBufferIdx{0}; @@ -686,7 +687,11 @@ void AL_APIENTRY AudioState::EventCallback(ALenum eventType, ALuint object, ALui if(eventType == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT) { - /* TODO: Signal the audio handler to wake up and decode another buffer. */ + /* Temporarily lock the source mutex to ensure it's not between + * checking the processed count and going to sleep. + */ + std::unique_lock<std::mutex>(self->mSrcMutex).unlock(); + self->mSrcCond.notify_one(); return; } @@ -715,12 +720,14 @@ int AudioState::handler() AL_EVENT_TYPE_ERROR_SOFT, AL_EVENT_TYPE_PERFORMANCE_SOFT, AL_EVENT_TYPE_DEPRECATED_SOFT }}; std::unique_lock<std::mutex> lock(mSrcMutex); + milliseconds sleep_time = AudioBufferTime / 3; ALenum fmt; if(alEventControlSOFT) { alEventControlSOFT(types.size(), types.data(), AL_TRUE); alEventCallbackSOFT(EventCallback, this); + sleep_time = AudioBufferTotalTime; } /* Find a suitable format for OpenAL. */ @@ -941,9 +948,7 @@ int AudioState::handler() mMovie.mPlaying.load(std::memory_order_relaxed)) startPlayback(); - lock.unlock(); - SDL_Delay((AudioBufferTime/3).count()); - lock.lock(); + mSrcCond.wait_for(lock, sleep_time); } alSourceRewind(mSource); |