From 446f2a2f34748808548d75e5c63b90aee43a459a Mon Sep 17 00:00:00 2001 From: badaix Date: Tue, 18 Aug 2015 22:01:08 +0200 Subject: [PATCH] stream notifies about new chunks --- client/alsaPlayer.cpp | 6 ++++-- client/stream.cpp | 10 ++++++++++ client/stream.h | 7 ++++++- common/queue.h | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/client/alsaPlayer.cpp b/client/alsaPlayer.cpp index 8765f72e..42e44978 100644 --- a/client/alsaPlayer.cpp +++ b/client/alsaPlayer.cpp @@ -201,13 +201,15 @@ void Player::worker() else { logO << "Failed to get chunk\n"; - usleep(100*1000); + while (active_ && !stream_->waitForChunk(100)) + logD << "Waiting for chunk\n"; } } } -vector Player::pcm_list(void) { +vector Player::pcm_list(void) +{ void **hints, **n; char *name, *descr, *io; vector result; diff --git a/client/stream.cpp b/client/stream.cpp index 11b3e54b..0c78d79c 100644 --- a/client/stream.cpp +++ b/client/stream.cpp @@ -74,10 +74,20 @@ void Stream::addChunk(msg::PcmChunk* chunk) while (chunks_.size() * chunk->duration().count() > 10000) chunks_.pop(); chunks_.push(shared_ptr(chunk)); + std::unique_lock lck(cvMutex_); + cv_.notify_one(); // logD << "new chunk: " << chunk_->getDuration() << ", Chunks: " << chunks_.size() << "\n"; } +bool Stream::waitForChunk(size_t ms) const +{ + std::unique_lock lck(cvMutex_); + cv_.wait_for(lck, std::chrono::milliseconds(ms)); + return !chunks_.empty(); +} + + cs::time_point_hrc Stream::getSilentPlayerChunk(void* outputBuffer, unsigned long framesPerBuffer) { diff --git a/client/stream.h b/client/stream.h index a9b73df8..088d9c93 100644 --- a/client/stream.h +++ b/client/stream.h @@ -54,12 +54,14 @@ public: /// "Server buffer": playout latency, e.g. 1000ms void setBufferLen(size_t bufferLenMs); - + const msg::SampleFormat& getFormat() const { return format_; } + bool waitForChunk(size_t ms) const; + private: chronos::time_point_hrc getNextPlayerChunk(void* outputBuffer, const chronos::usec& timeout, unsigned long framesPerBuffer); chronos::time_point_hrc getNextPlayerChunk(void* outputBuffer, const chronos::usec& timeout, unsigned long framesPerBuffer, long framesCorrection); @@ -88,6 +90,9 @@ private: unsigned long playedFrames_; long correctAfterXFrames_; chronos::msec bufferMs_; + + mutable std::condition_variable cv_; + mutable std::mutex cvMutex_; }; diff --git a/common/queue.h b/common/queue.h index 802844a6..3869a3dd 100644 --- a/common/queue.h +++ b/common/queue.h @@ -101,6 +101,11 @@ public: return queue_.size(); } + bool empty() const + { + return (size() == 0); + } + Queue()=default; Queue(const Queue&) = delete; // disable copying Queue& operator=(const Queue&) = delete; // disable assignment