2#include "AudioToolsConfig.h"
3#include "AudioTools/CoreAudio/Buffers.h"
4#include "AudioTools/CoreAudio/AudioLogger.h"
5#include "AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h"
8# include <freertos/stream_buffer.h>
9# include "freertos/FreeRTOS.h"
10#elif defined(__linux__)
13# include "stream_buffer.h"
32 BufferRTOS(
size_t streamBufferSize,
size_t xTriggerLevel = 1,
33 TickType_t writeMaxWait = portMAX_DELAY,
34 TickType_t readMaxWait = portMAX_DELAY,
37 readWait = readMaxWait;
38 writeWait = writeMaxWait;
39 current_size_bytes = (streamBufferSize+1) *
sizeof(T);
40 trigger_level = xTriggerLevel;
41 p_allocator = &allocator;
43 if (streamBufferSize > 0) {
53 int req_size_bytes = (size + 1)*
sizeof(T);
54 if (current_size_bytes != req_size_bytes) {
56 current_size_bytes = req_size_bytes;
62 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
64 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
66 void setWriteFromISR(
bool active) { write_from_isr = active; }
68 void setReadFromISR(
bool active) { read_from_isr = active; }
71 bool read(T &result)
override {
79 xHigherPriorityTaskWoken = pdFALSE;
80 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (
void *)data,
82 &xHigherPriorityTaskWoken);
86 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
88 return result /
sizeof(T);
90 return xStreamBufferReceive(xStreamBuffer, (
void *)data,
sizeof(T) * len,
91 readWait) /
sizeof(T);
96 LOGD(
"%s: %d", LOG_METHOD, len);
98 xHigherPriorityTaskWoken = pdFALSE;
100 xStreamBufferSendFromISR(xStreamBuffer, (
void *)data,
sizeof(T) * len,
101 &xHigherPriorityTaskWoken);
103 portYIELD_FROM_ISR();
105 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
107 return result /
sizeof(T);
109 return xStreamBufferSend(xStreamBuffer, (
void *)data,
sizeof(T) * len,
110 writeWait) /
sizeof(T);
115 bool peek(T &result)
override {
116 LOGE(
"peek not implemented");
122 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
125 bool isEmpty() {
return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE; }
134 void reset()
override { xStreamBufferReset(xStreamBuffer); }
138 return xStreamBufferBytesAvailable(xStreamBuffer) /
sizeof(T);
143 return xStreamBufferSpacesAvailable(xStreamBuffer) /
sizeof(T);
148 LOGE(
"address() not implemented");
152 size_t size() {
return current_size_bytes /
sizeof(T); }
154 operator bool() {
return xStreamBuffer !=
nullptr && size()>0;}
157 StreamBufferHandle_t xStreamBuffer =
nullptr;
158 StaticStreamBuffer_t static_stream_buffer;
159 uint8_t *p_data =
nullptr;
160 Allocator *p_allocator =
nullptr;
161 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
162 int readWait = portMAX_DELAY;
163 int writeWait = portMAX_DELAY;
164 bool read_from_isr =
false;
165 bool write_from_isr =
false;
166 size_t current_size_bytes = 0;
167 size_t trigger_level = 0;
172 if (current_size_bytes == 0)
return true;
175 int size = (current_size_bytes + 1) *
sizeof(T);
176 if (p_data ==
nullptr) {
177 p_data = (uint8_t *)p_allocator->allocate(size);
179 if (p_data ==
nullptr) {
180 LOGE(
"allocate falied for %d bytes", size)
187 if (xStreamBuffer ==
nullptr) {
188 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
189 p_data, &static_stream_buffer);
191 if (xStreamBuffer ==
nullptr) {
192 LOGE(
"xStreamBufferCreateStatic failed");
202 if (xStreamBuffer !=
nullptr) vStreamBufferDelete(xStreamBuffer);
203 p_allocator->free(p_data);
204 current_size_bytes = 0;
206 xStreamBuffer =
nullptr;
212using SynchronizedBufferRTOS = BufferRTOS<T>;