Ring Daemon 16.0.0
Loading...
Searching...
No Matches
ringbufferpool.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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 static const char* const DEFAULT_ID;
37
40
41 int getInternalSamplingRate() const { return internalAudioFormat_.sample_rate; }
42
43 AudioFormat getInternalAudioFormat() const { return internalAudioFormat_; }
44
45 void setInternalSamplingRate(unsigned sr);
46
48
54 void bindRingBuffers(const std::string &ringbufferId1, const std::string &ringbufferId2);
55
59 void unbindRingBuffers(const std::string &ringbufferId1, const std::string &ringbufferId2);
60
67 void bindHalfDuplexOut(const std::string &readerBufferId, const std::string &sourceBufferId);
68
75 void unBindHalfDuplexOut(const std::string &readerBufferId, const std::string &sourceBufferId);
76
82 void unBindAllHalfDuplexOut(const std::string &ringbufferId);
83
88 void unBindAllHalfDuplexIn(const std::string &sourceBufferId);
89
90 void unBindAll(const std::string& ringbufferId);
91
92 bool waitForDataAvailable(const std::string& ringbufferId,
93 const std::chrono::microseconds& max_wait) const;
94
95 std::shared_ptr<AudioFrame> getData(const std::string& ringbufferId);
96
97 std::shared_ptr<AudioFrame> getAvailableData(const std::string& ringbufferId);
98
99 size_t availableForGet(const std::string& ringbufferId) const;
100
101 size_t discard(size_t toDiscard, const std::string& ringbufferId);
102
103 void flush(const std::string& ringbufferId);
104
105 void flushAllBuffers();
106
112 std::shared_ptr<RingBuffer> createRingBuffer(const std::string& id);
113
120 std::shared_ptr<RingBuffer> getRingBuffer(const std::string& id);
121
125 std::shared_ptr<RingBuffer> getRingBuffer(const std::string& id) const;
126
127 bool isAudioMeterActive(const std::string& id);
128 void setAudioMeterState(const std::string& id, bool state);
129
130private:
132
133 // A set of RingBuffers readable by a call
134 using ReadBindings
135 = std::set<std::shared_ptr<RingBuffer>, std::owner_less<std::shared_ptr<RingBuffer>>>;
136
137 const ReadBindings* getReadBindings(const std::string& ringbufferId) const;
138 ReadBindings* getReadBindings(const std::string& ringbufferId);
139
140 void removeReadBindings(const std::string& ringbufferId);
141
148 void addReaderToRingBuffer(const std::shared_ptr<RingBuffer> &sourceBuffer,
149 const std::string &readerBufferId);
150
157 void removeReaderFromRingBuffer(const std::shared_ptr<RingBuffer> &sourceBuffer,
158 const std::string &readerBufferId);
159
160 // A cache of created RingBuffers listed by IDs.
161 std::map<std::string, std::weak_ptr<RingBuffer>> ringBufferMap_ {};
162
163 // A map of which RingBuffers a call has some ReadOffsets
164 std::map<std::string, ReadBindings> readBindingsMap_ {};
165
166 mutable std::recursive_mutex stateLock_ {};
167
168 AudioFormat internalAudioFormat_ {AudioFormat::DEFAULT()};
169
170 std::shared_ptr<RingBuffer> defaultRingBuffer_;
171};
172
173} // namespace jami
AudioFormat getInternalAudioFormat() const
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
void unBindAllHalfDuplexIn(const std::string &sourceBufferId)
Detaches a source from all its readers.
size_t discard(size_t toDiscard, const std::string &ringbufferId)
bool waitForDataAvailable(const std::string &ringbufferId, const std::chrono::microseconds &max_wait) const
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)
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)
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 ring_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()