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"
12# include "stream_buffer.h"
31 BufferRTOS(
size_t streamBufferSize,
size_t xTriggerLevel = 1,
32 TickType_t writeMaxWait = portMAX_DELAY,
33 TickType_t readMaxWait = portMAX_DELAY,
36 readWait = readMaxWait;
37 writeWait = writeMaxWait;
38 current_size_bytes = (streamBufferSize+1) *
sizeof(T);
39 trigger_level = xTriggerLevel;
40 p_allocator = &allocator;
42 if (streamBufferSize > 0) {
52 int req_size_bytes = (size + 1)*
sizeof(T);
53 if (current_size_bytes != req_size_bytes) {
55 current_size_bytes = req_size_bytes;
61 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
63 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
65 void setWriteFromISR(
bool active) { write_from_isr = active; }
67 void setReadFromISR(
bool active) { read_from_isr = active; }
70 bool read(T &result)
override {
70 bool read(T &result)
override {
…}
78 xHigherPriorityTaskWoken = pdFALSE;
79 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (
void *)data,
81 &xHigherPriorityTaskWoken);
85 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
87 return result /
sizeof(T);
89 return xStreamBufferReceive(xStreamBuffer, (
void *)data,
sizeof(T) * len,
90 readWait) /
sizeof(T);
95 LOGD(
"%s: %d", LOG_METHOD, len);
97 xHigherPriorityTaskWoken = pdFALSE;
99 xStreamBufferSendFromISR(xStreamBuffer, (
void *)data,
sizeof(T) * len,
100 &xHigherPriorityTaskWoken);
102 portYIELD_FROM_ISR();
104 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
106 return result /
sizeof(T);
108 return xStreamBufferSend(xStreamBuffer, (
void *)data,
sizeof(T) * len,
109 writeWait) /
sizeof(T);
114 bool peek(T &result)
override {
115 LOGE(
"peek not implemented");
121 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
124 bool isEmpty() {
return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE; }
133 void reset()
override { xStreamBufferReset(xStreamBuffer); }
137 return xStreamBufferBytesAvailable(xStreamBuffer) /
sizeof(T);
142 return xStreamBufferSpacesAvailable(xStreamBuffer) /
sizeof(T);
147 LOGE(
"address() not implemented");
151 size_t size() {
return current_size_bytes /
sizeof(T); }
153 operator bool() {
return xStreamBuffer !=
nullptr && size()>0;}
156 StreamBufferHandle_t xStreamBuffer =
nullptr;
157 StaticStreamBuffer_t static_stream_buffer;
158 uint8_t *p_data =
nullptr;
159 Allocator *p_allocator =
nullptr;
160 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
161 int readWait = portMAX_DELAY;
162 int writeWait = portMAX_DELAY;
163 bool read_from_isr =
false;
164 bool write_from_isr =
false;
165 size_t current_size_bytes = 0;
166 size_t trigger_level = 0;
171 if (current_size_bytes == 0)
return true;
174 int size = (current_size_bytes + 1) *
sizeof(T);
175 if (p_data ==
nullptr) {
176 p_data = (uint8_t *)p_allocator->allocate(size);
178 if (p_data ==
nullptr) {
179 LOGE(
"allocate falied for %d bytes", size)
186 if (xStreamBuffer ==
nullptr) {
187 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
188 p_data, &static_stream_buffer);
190 if (xStreamBuffer ==
nullptr) {
191 LOGE(
"xStreamBufferCreateStatic failed");
201 if (xStreamBuffer !=
nullptr) vStreamBufferDelete(xStreamBuffer);
202 p_allocator->free(p_data);
203 current_size_bytes = 0;
205 xStreamBuffer =
nullptr;
211using SynchronizedBufferRTOS = BufferRTOS<T>;