Ring Daemon
Loading...
Searching...
No Matches
ringbufferpool.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "audio_format.h"
20#include "media_buffer.h"
21#include "noncopyable.h"
22
23#include <map>
24#include <set>
25#include <string>
26#include <mutex>
27#include <memory>
28
29namespace jami {
30
31class RingBuffer;
32
34{
35public:
36 using clock = std::chrono::steady_clock;
37 using time_point = clock::time_point;
38 using duration = clock::duration;
39 static const char* const DEFAULT_ID;
40
43
44 int getInternalSamplingRate() const { return static_cast<int>(internalAudioFormat_.sample_rate); }
45
46 AudioFormat getInternalAudioFormat() const { return internalAudioFormat_; }
47
48 void setInternalSamplingRate(unsigned sr);
49
51
57 void bindRingBuffers(const std::string& ringbufferId1, const std::string& ringbufferId2);
58
62 void unbindRingBuffers(const std::string& ringbufferId1, const std::string& ringbufferId2);
63
70 void bindHalfDuplexOut(const std::string& readerBufferId, const std::string& sourceBufferId);
71
78 void unBindHalfDuplexOut(const std::string& readerBufferId, const std::string& sourceBufferId);
79
85 void unBindAllHalfDuplexOut(const std::string& ringbufferId);
86
91 void unBindAllHalfDuplexIn(const std::string& sourceBufferId);
92
93 void unBindAll(const std::string& ringbufferId);
94
95 bool waitForDataAvailable(const std::string& ringbufferId, const duration& max_wait) const;
96 bool waitForDataAvailable(const std::string& ringbufferId, const time_point& deadline) const;
97
98 std::shared_ptr<AudioFrame> getData(const std::string& ringbufferId);
99
100 std::shared_ptr<AudioFrame> getAvailableData(const std::string& ringbufferId);
101
102 size_t availableForGet(const std::string& ringbufferId) const;
103
104 size_t discard(size_t toDiscard, const std::string& ringbufferId);
105
106 void flush(const std::string& ringbufferId);
107
108 void flushAllBuffers();
109
115 std::shared_ptr<RingBuffer> createRingBuffer(const std::string& id);
116
123 std::shared_ptr<RingBuffer> getRingBuffer(const std::string& id);
124
128 std::shared_ptr<RingBuffer> getRingBuffer(const std::string& id) const;
129
130 bool isAudioMeterActive(const std::string& id);
131 void setAudioMeterState(const std::string& id, bool state);
132
133private:
135
136 // A set of RingBuffers readable by a call
137 using ReadBindings = std::set<std::shared_ptr<RingBuffer>, std::owner_less<std::shared_ptr<RingBuffer>>>;
138
139 const ReadBindings* getReadBindings(const std::string& ringbufferId) const;
140 ReadBindings* getReadBindings(const std::string& ringbufferId);
141
142 void removeReadBindings(const std::string& ringbufferId);
143
148 std::shared_ptr<RingBuffer> getRingBufferLocked(const std::string& id);
149 std::shared_ptr<RingBuffer> getRingBufferLocked(const std::string& id) const;
150 void flushAllBuffersLocked();
151
158 void addReaderToRingBuffer(const std::shared_ptr<RingBuffer>& sourceBuffer, const std::string& readerBufferId);
159
166 void removeReaderFromRingBuffer(const std::shared_ptr<RingBuffer>& sourceBuffer, const std::string& readerBufferId);
167
168 // A cache of created RingBuffers listed by IDs.
169 std::map<std::string, std::weak_ptr<RingBuffer>> ringBufferMap_ {};
170
171 // A map of which RingBuffers a call has some ReadOffsets
172 std::map<std::string, ReadBindings> readBindingsMap_ {};
173
174 mutable std::mutex stateLock_ {};
175
176 AudioFormat internalAudioFormat_ {AudioFormat::DEFAULT()};
177
178 std::shared_ptr<RingBuffer> defaultRingBuffer_;
179};
180
181} // namespace jami
AudioFormat getInternalAudioFormat() const
clock::time_point time_point
std::shared_ptr< RingBuffer > createRingBuffer(const std::string &id)
Create a new ringbuffer with a default readoffset.
std::shared_ptr< RingBuffer > getRingBuffer(const std::string &id)
Obtain a shared pointer on a RingBuffer given by its ID.
size_t availableForGet(const std::string &ringbufferId) const
bool waitForDataAvailable(const std::string &ringbufferId, const duration &max_wait) const
void unBindAllHalfDuplexIn(const std::string &sourceBufferId)
Detaches a source from all its readers.
size_t discard(size_t toDiscard, const std::string &ringbufferId)
void setAudioMeterState(const std::string &id, bool state)
void bindHalfDuplexOut(const std::string &readerBufferId, const std::string &sourceBufferId)
Attaches a reader the specified source.
std::shared_ptr< AudioFrame > getAvailableData(const std::string &ringbufferId)
clock::duration duration
void unBindAllHalfDuplexOut(const std::string &ringbufferId)
Detaches a reader from all his sources.
void setInternalSamplingRate(unsigned sr)
bool isAudioMeterActive(const std::string &id)
void bindRingBuffers(const std::string &ringbufferId1, const std::string &ringbufferId2)
Bind two RingBuffer together (full duplex).
std::shared_ptr< AudioFrame > getData(const std::string &ringbufferId)
static const char *const DEFAULT_ID
void unbindRingBuffers(const std::string &ringbufferId1, const std::string &ringbufferId2)
Unbind two RingBuffer (full duplex).
void unBindAll(const std::string &ringbufferId)
std::chrono::steady_clock clock
int getInternalSamplingRate() const
void flush(const std::string &ringbufferId)
void unBindHalfDuplexOut(const std::string &readerBufferId, const std::string &sourceBufferId)
Detaches a reader from the specified source.
void setInternalAudioFormat(AudioFormat format)
void emitSignal(Args... args)
Definition jami_signal.h:64
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
Structure to hold sample rate and channel number associated with audio data.
static const constexpr AudioFormat DEFAULT()