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 {
78 if (xStreamBuffer ==
nullptr || data ==
nullptr || len == 0)
return 0;
81 xHigherPriorityTaskWoken = pdFALSE;
82 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (
void *)data,
84 &xHigherPriorityTaskWoken);
88 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
90 return result /
sizeof(T);
92 return xStreamBufferReceive(xStreamBuffer, (
void *)data,
sizeof(T) * len,
93 readWait) /
sizeof(T);
98 LOGD(
"%s: %d", LOG_METHOD, len);
99 if (xStreamBuffer ==
nullptr || data ==
nullptr || len == 0)
return 0;
101 if (write_from_isr) {
102 xHigherPriorityTaskWoken = pdFALSE;
104 xStreamBufferSendFromISR(xStreamBuffer, (
void *)data,
sizeof(T) * len,
105 &xHigherPriorityTaskWoken);
107 portYIELD_FROM_ISR();
109 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
111 return result /
sizeof(T);
113 return xStreamBufferSend(xStreamBuffer, (
void *)data,
sizeof(T) * len,
114 writeWait) /
sizeof(T);
119 bool peek(T &result)
override {
120 LOGE(
"peek not implemented");
126 if (xStreamBuffer ==
nullptr)
return true;
127 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
131 if (xStreamBuffer ==
nullptr)
return true;
132 return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE;
143 if (xStreamBuffer ==
nullptr)
return;
144 xStreamBufferReset(xStreamBuffer);
149 if (xStreamBuffer ==
nullptr)
return 0;
150 return xStreamBufferBytesAvailable(xStreamBuffer) /
sizeof(T);
155 if (xStreamBuffer ==
nullptr)
return 0;
156 return xStreamBufferSpacesAvailable(xStreamBuffer) /
sizeof(T);
161 LOGE(
"address() not implemented");
165 size_t size() {
return current_size_bytes /
sizeof(T); }
167 operator bool() {
return xStreamBuffer !=
nullptr && size()>0;}
170 StreamBufferHandle_t xStreamBuffer =
nullptr;
171 StaticStreamBuffer_t static_stream_buffer;
172 uint8_t *p_data =
nullptr;
173 Allocator *p_allocator =
nullptr;
174 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
175 int readWait = portMAX_DELAY;
176 int writeWait = portMAX_DELAY;
177 bool read_from_isr =
false;
178 bool write_from_isr =
false;
179 size_t current_size_bytes = 0;
180 size_t trigger_level = 0;
185 if (current_size_bytes == 0)
return true;
188 int size = (current_size_bytes + 1) *
sizeof(T);
189 if (p_data ==
nullptr) {
190 p_data = (uint8_t *)p_allocator->allocate(size);
192 if (p_data ==
nullptr) {
193 LOGE(
"allocate falied for %d bytes", size)
200 if (xStreamBuffer ==
nullptr) {
201 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
202 p_data, &static_stream_buffer);
204 if (xStreamBuffer ==
nullptr) {
205 LOGE(
"xStreamBufferCreateStatic failed");
215 if (xStreamBuffer !=
nullptr) vStreamBufferDelete(xStreamBuffer);
216 if (p_data !=
nullptr) p_allocator->free(p_data);
217 current_size_bytes = 0;
219 xStreamBuffer =
nullptr;