From b6400b6f8124f2425c8176f21f81baaff746e50a Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Thu, 12 Jul 2018 16:20:26 +0300 Subject: [PATCH] - add BufferQueue class to HL_Sync.* - minor cleanup in CoreAudio support --- src/engine/audio/Audio_CoreAudio.h | 6 ++--- src/engine/helper/HL_Sync.cpp | 36 ++++++++++++++++++++++++++++++ src/engine/helper/HL_Sync.h | 18 +++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/engine/audio/Audio_CoreAudio.h b/src/engine/audio/Audio_CoreAudio.h index 40cc6410..f787f532 100644 --- a/src/engine/audio/Audio_CoreAudio.h +++ b/src/engine/audio/Audio_CoreAudio.h @@ -13,9 +13,9 @@ #include "Audio_Helper.h" #include "Audio_Resampler.h" #include "Audio_DataWindow.h" -#include "../Helper/HL_Pointer.h" -#include "../Helper/HL_ByteBuffer.h" -#include "../Helper/HL_Exception.h" +#include "../helper/HL_Pointer.h" +#include "../helper/HL_ByteBuffer.h" +#include "../helper/HL_Exception.h" #include // Define CoreAudio buffer time length in milliseconds diff --git a/src/engine/helper/HL_Sync.cpp b/src/engine/helper/HL_Sync.cpp index 1ae41867..e2d5634b 100644 --- a/src/engine/helper/HL_Sync.cpp +++ b/src/engine/helper/HL_Sync.cpp @@ -84,3 +84,39 @@ uint32_t TimeHelper::getDelta(uint32_t later, uint32_t earlier) return 0; } + +// --------------- BufferQueue ----------------- +BufferQueue::BufferQueue() +{ + +} + +BufferQueue::~BufferQueue() +{ + +} + +void BufferQueue::push(const void* data, int bytes) +{ + std::unique_lock l(mMutex); + + Block b = std::make_shared>(); + b->resize(bytes); + memcpy(b->data(), data, bytes); + mBlockList.push_back(b); +} + +BufferQueue::Block BufferQueue::pull(std::chrono::milliseconds timeout) +{ + std::unique_lock l(mMutex); + std::cv_status status = std::cv_status::no_timeout; + + while (mBlockList.empty() && status == std::cv_status::no_timeout) + status = mSignal.wait_for(l, timeout); + + Block r; + if (status == std::cv_status::no_timeout) + r = mBlockList.front(); mBlockList.erase(mBlockList.begin()); + + return r; +} diff --git a/src/engine/helper/HL_Sync.h b/src/engine/helper/HL_Sync.h index d4e5726d..5b7fd637 100644 --- a/src/engine/helper/HL_Sync.h +++ b/src/engine/helper/HL_Sync.h @@ -10,6 +10,7 @@ #include #include #include +#include typedef std::recursive_mutex Mutex; typedef std::unique_lock Lock; @@ -77,4 +78,21 @@ public: static uint32_t getDelta(uint32_t later, uint32_t earlier); }; +class BufferQueue +{ +public: + BufferQueue(); + ~BufferQueue(); + + typedef std::shared_ptr> Block; + + void push(const void* data, int bytes); + Block pull(std::chrono::milliseconds timeout); + +protected: + std::mutex mMutex; + std::condition_variable mSignal; + std::vector mBlockList; +}; + #endif