3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioTypes.h"
5 #include "AudioTools/CoreAudio/Buffers.h"
6 #include "AudioTools/CoreAudio/AudioLogger.h"
9 # include "freertos/FreeRTOS.h"
10 # include "AudioTools/Concurrency/QueueRTOS.h"
11 # if ESP_IDF_VERSION_MAJOR >= 4
12 # include <freertos/stream_buffer.h>
15 # include "stream_buffer.h"
18 #include "LockGuard.h"
37 is_sync_available = syncAvailable;
44 return p_buffer->read();
52 for (
int j = 0; j < lenResult; j++) {
53 data[j] = p_buffer->read();
59 LOGD(
"%s: %d", LOG_METHOD, len);
62 for (
int j = 0; j < len; j++) {
63 if (p_buffer->write(data[j]) == 0) {
75 return p_buffer->peek();
79 bool isFull()
override {
return p_buffer->isFull(); }
81 bool isEmpty() {
return available() == 0; }
87 return p_buffer->write(data);
100 if (is_sync_available)
LockGuard guard(p_mutex);
101 return p_buffer->available();
107 if (is_sync_available)
LockGuard guard(p_mutex);
108 return p_buffer->availableForWrite();
114 return p_buffer->address();
118 return p_buffer->size();
122 BaseBuffer<T> *p_buffer =
nullptr;
123 Mutex *p_mutex =
nullptr;
124 bool is_sync_available =
false;
134 template <
typename T>
137 SynchronizedNBuffer(
int bufferSize,
int bufferCount,
int writeMaxWait=portMAX_DELAY,
int readMaxWait=portMAX_DELAY) {
139 read_max_wait = readMaxWait;
140 write_max_wait = writeMaxWait;
141 resize(bufferSize, bufferCount);
147 void resize(
int bufferSize,
int bufferCount) {
149 if (buffer_size == bufferSize && buffer_count == bufferCount){
153 max_size = bufferSize * bufferCount;
158 available_buffers.resize(bufferCount);
159 filled_buffers.resize(bufferCount);
161 setReadMaxWait(read_max_wait);
162 setWriteMaxWait(write_max_wait);
165 for (
int j = 0; j < bufferCount; j++) {
167 if (tmp !=
nullptr) {
168 available_buffers.enqueue(tmp);
170 LOGE(
"Not Enough Memory for buffer %d", j);
175 void setReadMaxWait(TickType_t ticks){
176 available_buffers.setReadMaxWait(ticks);
177 filled_buffers.setReadMaxWait(ticks);
180 void setWriteMaxWait(TickType_t ticks){
181 available_buffers.setWriteMaxWait(ticks);
182 filled_buffers.setWriteMaxWait(ticks);
189 int bufferCountFilled() {
190 return filled_buffers.size();
193 int bufferCountEmpty() {
194 return available_buffers.size();
201 size_t read_max_wait, write_max_wait;
202 int buffer_size = 0, buffer_count = 0;
208 while (available_buffers.dequeue(buffer)){
211 while (filled_buffers.dequeue(buffer)){
219 return available_buffers.dequeue(result) ? result :
nullptr;
222 bool addAvailableBuffer(BaseBuffer<T> *buffer) {
224 return available_buffers.enqueue(buffer);
227 BaseBuffer<T> *getNextFilledBuffer() {
229 BaseBuffer<T>* result;
230 return filled_buffers.dequeue(result) ? result :
nullptr;
233 bool addFilledBuffer(BaseBuffer<T> *buffer) {
235 return filled_buffers.enqueue(buffer);