From 1626408838eb480cbb53c8789711d7cdc56b495c Mon Sep 17 00:00:00 2001 From: Luo Jinghua Date: Fri, 21 Dec 2012 15:03:17 +0800 Subject: if no buffer is queued, then wait for one --- src/device_mixer.cpp | 21 +++++++++++++++++---- src/device_mixer.h | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/device_mixer.cpp b/src/device_mixer.cpp index ed10ab5..0a446c2 100644 --- a/src/device_mixer.cpp +++ b/src/device_mixer.cpp @@ -348,7 +348,7 @@ namespace audiere { return 0; while (m_free_list.empty()) { - m_buffer_cond.wait(m_buffer_mutex, 10); + m_buffer_cond.wait(m_buffer_mutex, 1); if (m_shutdown) return 0; } @@ -376,23 +376,35 @@ namespace audiere { if (m_shutdown) return false; + bool donotify = m_queue.empty(); while (m_queue.size() >= size_t(m_max_queue_len)) { - m_cond.wait(m_mutex, 10); + m_cond.wait(m_mutex, 1); if (m_shutdown) return false; } m_queue.push_back(buffer); + if (donotify) + m_cond.notify(); return true; } - AudioBuffer* AudioQueue::pop() + AudioBuffer* AudioQueue::pop(bool wait) { SYNCHRONIZED(m_mutex); - if (m_shutdown || m_queue.empty()) + if (m_shutdown) + return 0; + + if (!wait && m_queue.empty()) return 0; + while (m_queue.empty()) { + m_cond.wait(m_mutex, 1); + if (m_shutdown) + return 0; + } + bool donotify = m_queue.size() >= size_t(m_max_queue_len); AudioBuffer* buf = m_queue.front(); @@ -408,6 +420,7 @@ namespace audiere { m_shutdown = true; m_cond.notify(); + m_buffer_cond.notify(); } ThreadedMixerDevice::ThreadedMixerDevice(int rate, int block_size, int queue_len) diff --git a/src/device_mixer.h b/src/device_mixer.h index 942bccc..43c5c4f 100644 --- a/src/device_mixer.h +++ b/src/device_mixer.h @@ -120,7 +120,7 @@ namespace audiere { void put_buffer(AudioBuffer* buffer); bool push(AudioBuffer* buffer); - AudioBuffer* pop(); + AudioBuffer* pop(bool wait = true); void shutdown(); -- cgit v1.2.3