44 putToBuffer(std::move(
frame));
47 JAMI_LOG(
"Create new RingBuffer {}",
id);
52 JAMI_LOG(
"Destroy RingBuffer {}",
id);
64 for (
auto& offset : readoffsets_)
65 offset.second.offset = endPos_;
70 for (
const auto &offset: readoffsets_) {
82 const size_t startPos = getSmallestReadOffset();
98 JAMI_DBG(
"Start=%zu; End=%zu; BufferSize=%zu", getSmallestReadOffset(), endPos_, buffer_.size());
102RingBuffer::getReadOffset(
const std::string&
ringbufferId)
const
105 return (
iter != readoffsets_.end()) ?
iter->second.offset : 0;
109RingBuffer::getSmallestReadOffset()
const
111 if (hasNoReadOffsets())
114 for (
auto const&
iter : readoffsets_)
120RingBuffer::storeReadOffset(
size_t offset,
const std::string&
ringbufferId)
124 if (
iter != readoffsets_.end())
125 iter->second.offset = offset;
133 std::lock_guard
l(lock_);
135 readoffsets_.emplace(
ringbufferId, ReadOffset {endPos_, {}});
141 std::lock_guard
l(lock_);
143 if (
iter != readoffsets_.end())
144 readoffsets_.erase(
iter);
148RingBuffer::hasThisReadOffset(
const std::string&
ringbufferId)
const
150 return readoffsets_.find(
ringbufferId) != readoffsets_.end();
154RingBuffer::hasNoReadOffsets()
const
156 return readoffsets_.empty();
166 std::lock_guard
l(writeLock_);
172RingBuffer::putToBuffer(std::shared_ptr<AudioFrame>&& data)
174 std::lock_guard
l(lock_);
183 size_t pos = endPos_;
185 buffer_[
pos] = std::move(data);
193 rmsLevel_ +=
newBuf->calcRMS();
201 for (
auto& offset : readoffsets_) {
202 if (offset.second.callback)
203 offset.second.callback(
newBuf);
206 not_empty_.notify_all();
220std::shared_ptr<AudioFrame>
223 std::lock_guard
l(lock_);
226 if (offset == readoffsets_.end())
233 size_t startPos = offset->second.offset;
246 std::unique_lock
l(lock_);
250 if (readoffsets_.find(
ringbufferId) == readoffsets_.end())
264 if (
deadline == time_point::max()) {
266 not_empty_.wait(
l,
check);
277 std::lock_guard
l(lock_);
284 if (offset == readoffsets_.end())
301 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.
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.
RingBuffer(const std::string &id, size_t size, AudioFormat format=AudioFormat::MONO())
Constructor.
#define JAMI_ERROR(formatstr,...)
#define JAMI_LOG(formatstr,...)
void emitSignal(Args... args)
static constexpr const int RMS_SIGNAL_INTERVAL
static const size_t MIN_BUFFER_SIZE