Skip to content

Commit 1b3df6a

Browse files
Fix buffer overflow bug
When 2 transmitters sending at the same time, a buffer overflow can happen. So, a check is added to flush the incoming samples in that case. Nice side effect is, that you can hear 2 transmitters when they are sending at the same time (distorted).
1 parent 451b871 commit 1b3df6a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/audio_output/src/OutputBuffer.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ class OutputBuffer
5353
void add_samples(const uint8_t *samples, int count)
5454
{
5555
xSemaphoreTake(m_semaphore, portMAX_DELAY);
56-
// copy the samples into the buffer wrapping around as needed
57-
for (int i = 0; i < count; i++)
56+
// check if there is still room in the buffer
57+
if (m_available_samples + count <= m_buffer_size)
5858
{
59-
m_buffer[m_write_head] = samples[i];
60-
m_write_head = (m_write_head + 1) % m_buffer_size;
59+
// copy the samples into the buffer wrapping around as needed
60+
for (int i = 0; i < count; i++)
61+
{
62+
m_buffer[m_write_head] = samples[i];
63+
m_write_head = (m_write_head + 1) % m_buffer_size;
64+
}
65+
m_available_samples += count;
6166
}
62-
m_available_samples += count;
6367
xSemaphoreGive(m_semaphore);
6468
}
6569

0 commit comments

Comments
 (0)