Skip to content

Commit f02f54a

Browse files
committed
A2DP check allocation
1 parent f7ae557 commit f02f54a

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/AudioLibs/A2DPStream.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ class A2DPStream : public AudioStream, public VolumeSupport {
132132
this->config = cfg;
133133
bool result = false;
134134
LOGI("Connecting to %s",cfg.name);
135-
a2dp_buffer.resize(cfg.buffer_size);
135+
136+
if (!a2dp_buffer.resize(cfg.buffer_size)){
137+
LOGE("a2dp_buffer resize failed");
138+
return false;
139+
}
136140

137141
// initialize a2dp_silence_timeout
138142
if (config.silence_on_nodata){
@@ -224,9 +228,11 @@ class A2DPStream : public AudioStream, public VolumeSupport {
224228
}
225229

226230
// blocking write: if buffer is full we wait
227-
while(len > 0.9f * a2dp_buffer.availableForWrite() * 100 / 90){
228-
LOGD("Waiting for buffer to be available");
231+
size_t free = a2dp_buffer.availableForWrite();
232+
while(len > free){
233+
LOGD("Waiting for buffer to be available: %d < %d", (int) len, (int) free);
229234
delay(5);
235+
free = a2dp_buffer.availableForWrite();
230236
}
231237
}
232238

src/Concurrency/BufferRTOS.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ class BufferRTOS : public BaseBuffer<T> {
4747
~BufferRTOS() { end(); }
4848

4949
/// Re-Allocats the memory and the queue
50-
void resize(size_t size) {
50+
bool resize(size_t size) {
51+
bool result = true;
5152
if (current_size != size) {
5253
end();
5354
current_size = size;
54-
setup();
55+
result = setup();
5556
}
57+
return result;
5658
}
5759

5860
void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
@@ -167,17 +169,26 @@ class BufferRTOS : public BaseBuffer<T> {
167169
if (current_size == 0) return true;
168170

169171
// allocate data if necessary
172+
int size = (current_size + 1) * sizeof(T);
170173
if (p_data == nullptr) {
171-
p_data = (uint8_t *)p_allocator->allocate((current_size + 1) * sizeof(T));
174+
p_data = (uint8_t *)p_allocator->allocate(size);
175+
// check allocation
176+
if (p_data == nullptr) {
177+
LOGE("allocate falied for %d bytes", size)
178+
return false;
179+
}
172180
}
173-
if (p_data == nullptr) return false;
181+
174182

175183
// create stream buffer if necessary
176184
if (xStreamBuffer == nullptr) {
177185
xStreamBuffer = xStreamBufferCreateStatic(current_size, trigger_level,
178186
p_data, &static_stream_buffer);
179187
}
180-
if (xStreamBuffer == nullptr) return false;
188+
if (xStreamBuffer == nullptr) {
189+
LOGE("xStreamBufferCreateStatic failed");
190+
return false;
191+
}
181192
// make sure that the data is empty
182193
reset();
183194
return true;

0 commit comments

Comments
 (0)