41 putToBuffer(std::move(
frame));
44 JAMI_LOG(
"Create new RingBuffer {}",
id);
49 JAMI_LOG(
"Destroy RingBuffer {}",
id);
61 for (
auto& offset : readoffsets_)
62 offset.second.offset = endPos_;
65std::vector<std::string>
69 for (
const auto& offset : readoffsets_) {
81 const size_t startPos = getSmallestReadOffset();
97 JAMI_DBG(
"Start=%zu; End=%zu; BufferSize=%zu", getSmallestReadOffset(), endPos_, buffer_.size());
101RingBuffer::getReadOffset(
const std::string&
ringbufferId)
const
104 return (
iter != readoffsets_.end()) ?
iter->second.offset : 0;
108RingBuffer::getSmallestReadOffset()
const
110 if (hasNoReadOffsets())
113 for (
auto const&
iter : readoffsets_)
119RingBuffer::storeReadOffset(
size_t offset,
const std::string&
ringbufferId)
123 if (
iter != readoffsets_.end())
124 iter->second.offset = offset;
132 std::lock_guard
l(lock_);
134 readoffsets_.emplace(
ringbufferId, ReadOffset {endPos_, {}});
140 std::lock_guard
l(lock_);
142 if (
iter != readoffsets_.end())
143 readoffsets_.erase(
iter);
147RingBuffer::hasThisReadOffset(
const std::string&
ringbufferId)
const
149 return readoffsets_.find(
ringbufferId) != readoffsets_.end();
153RingBuffer::hasNoReadOffsets()
const
155 return readoffsets_.empty();
165 std::lock_guard
l(writeLock_);
171RingBuffer::putToBuffer(std::shared_ptr<AudioFrame>&& data)
173 std::lock_guard
l(lock_);
182 size_t pos = endPos_;
184 buffer_[
pos] = std::move(data);
192 rmsLevel_ +=
newBuf->calcRMS();
200 for (
auto& offset : readoffsets_) {
201 if (offset.second.callback)
202 offset.second.callback(
newBuf);
205 not_empty_.notify_all();
219std::shared_ptr<AudioFrame>
222 std::lock_guard
l(lock_);
225 if (offset == readoffsets_.end())
232 size_t startPos = offset->second.offset;
245 std::unique_lock
l(lock_);
249 if (readoffsets_.find(
ringbufferId) == readoffsets_.end())
263 if (
deadline == time_point::max()) {
265 not_empty_.wait(
l,
check);
276 std::lock_guard
l(lock_);
283 if (offset == readoffsets_.end())
300 for (
auto& r : readoffsets_) {
void enqueue(std::shared_ptr< AudioFrame > &&frame)
Write @frame's data to the queue.
int resample(const AVFrame *input, AVFrame *output)
Resample a frame.
void debug()
Debug function print mEnd, mStart, mBufferSize.
RingBuffer(const std::string &id, AudioFormat format=AudioFormat::MONO())
Constructor.
clock::time_point time_point
void removeReadOffset(const std::string &ringbufferId)
Remove a readoffset for this ringbuffer.
void flush(const std::string &ringbufferId)
Reset the counters to 0 for this read offset.
void createReadOffset(const std::string &ringbufferId)
Add a new readoffset for this ringbuffer.
size_t availableForGet(const std::string &ringbufferId) const
To get how much samples are available in the buffer to read in.
std::shared_ptr< AudioFrame > get(const std::string &ringbufferId)
Get data in the ring buffer.
std::vector< std::string > getSubscribers()
Return the list of subscribers (Ring buffers Id that are reading this ring buffer).
size_t getLength(const std::string &ringbufferId) const
size_t discard(size_t toDiscard, const std::string &ringbufferId)
Discard data from the buffer.
void put(std::shared_ptr< AudioFrame > &&data)
Write data in the ring buffer.
size_t putLength() const
Total length of the ring buffer which is available for "putting".
size_t waitForDataAvailable(const std::string &ringbufferId, const time_point &deadline=time_point::max()) const
Blocks until min_data_length samples of data is available, or until deadline has passed.
#define JAMI_ERROR(formatstr,...)
#define JAMI_LOG(formatstr,...)
void emitSignal(Args... args)
static constexpr const int RMS_SIGNAL_INTERVAL