Skip to content

Commit

Permalink
Merge pull request #3105 from phire/dont_block
Browse files Browse the repository at this point in the history
ALSA: Don't block on Clear() call.
  • Loading branch information
shuffle2 committed Oct 4, 2015
2 parents 91d805f + a10a3ec commit 9878004
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
46 changes: 23 additions & 23 deletions Source/Core/AudioCommon/AlsaSoundStream.cpp
Expand Up @@ -48,21 +48,31 @@ void AlsaSound::Update()
void AlsaSound::SoundLoop()
{
Common::SetCurrentThreadName("Audio thread - alsa");
while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
while (m_thread_status.load() != ALSAThreadStatus::STOPPING)
{
std::unique_lock<std::mutex> lock(cv_m);
cv.wait(lock, [this]{return !m_muted || m_thread_status.load() != ALSAThreadStatus::RUNNING;});

m_mixer->Mix(mix_buffer, frames_to_deliver);
int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
if (rc == -EPIPE)
while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
{
// Underrun
snd_pcm_prepare(handle);
m_mixer->Mix(mix_buffer, frames_to_deliver);
int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
if (rc == -EPIPE)
{
// Underrun
snd_pcm_prepare(handle);
}
else if (rc < 0)
{
ERROR_LOG(AUDIO, "writei fail: %s", snd_strerror(rc));
}
}
else if (rc < 0)
if (m_thread_status.load() == ALSAThreadStatus::PAUSED)
{
ERROR_LOG(AUDIO, "writei fail: %s", snd_strerror(rc));
snd_pcm_drop(handle); // Stop sound output

// Block until thread status changes.
std::unique_lock<std::mutex> lock(cv_m);
cv.wait(lock, [this]{ return m_thread_status.load() != ALSAThreadStatus::PAUSED; });

snd_pcm_prepare(handle); // resume sound output
}
}
AlsaShutdown();
Expand All @@ -73,18 +83,8 @@ void AlsaSound::SoundLoop()
void AlsaSound::Clear(bool muted)
{
m_muted = muted;
if (m_muted)
{
std::lock_guard<std::mutex> lock(cv_m);
snd_pcm_drop(handle);
}
else
{
std::unique_lock<std::mutex> lock(cv_m);
snd_pcm_prepare(handle);
lock.unlock();
cv.notify_one();
}
m_thread_status.store(muted ? ALSAThreadStatus::PAUSED : ALSAThreadStatus::RUNNING);
cv.notify_one(); // Notify thread that status has changed
}

bool AlsaSound::AlsaInit()
Expand Down
1 change: 1 addition & 0 deletions Source/Core/AudioCommon/AlsaSoundStream.h
Expand Up @@ -46,6 +46,7 @@ class AlsaSound final : public SoundStream
enum class ALSAThreadStatus
{
RUNNING,
PAUSED,
STOPPING,
STOPPED,
};
Expand Down

0 comments on commit 9878004

Please sign in to comment.