Skip to content

Commit

Permalink
fix buffer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jstahlbaum-fibernetics committed Jul 3, 2024
1 parent 6e14ecc commit 34759cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class AudioPipe
// Stores data coming from the external socket server
uint8_t *m_audio_buffer_in;
size_t m_audio_buffer_in_max_len;
size_t m_audio_buffer_in_read_offset;
size_t m_audio_buffer_in_write_offset;
size_t m_audio_buffer_in_len;

Expand Down
30 changes: 17 additions & 13 deletions components/freeswitch/src/mod/mod_twilio_stream/src/audio_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ AudioPipe::AudioPipe(const char *uuid,
m_audio_buffer_out_min_freespace(minFreespace),
m_audio_buffer_out_max_len(bufLen),
m_audio_buffer_in_max_len(bufInLen),
m_audio_buffer_in_read_offset(0),
m_audio_buffer_in_write_offset(0),
m_audio_buffer_in_len(0),
m_gracefulShutdown(false),
Expand Down Expand Up @@ -633,40 +634,42 @@ void AudioPipe::do_graceful_shutdown()
}
void AudioPipe::binaryReadPush(uint8_t *data, size_t len)
{
auto avail = m_audio_buffer_in_max_len - m_audio_buffer_in_write_offset;
auto ptr = m_audio_buffer_in + m_audio_buffer_in_write_offset;
uint32_t avail = m_audio_buffer_in_max_len - m_audio_buffer_in_write_offset;
uint8_t *ptr = m_audio_buffer_in + m_audio_buffer_in_write_offset;

if (len <= avail)
{
memcpy(ptr, data, len);
m_audio_buffer_in_write_offset += len;
m_audio_buffer_in_write_offset = (m_audio_buffer_in_write_offset + len) % m_audio_buffer_in_max_len;
}
else
{
// Wrapping case
memcpy(ptr, data, avail);
memcpy(m_audio_buffer_in, &data[avail], len - avail);
m_audio_buffer_in_write_offset = len - avail;
m_audio_buffer_in_write_offset = (len - avail) % m_audio_buffer_in_max_len;
}
m_audio_buffer_in_len += len;
}

size_t AudioPipe::binaryReadPop(uint8_t *data, size_t data_len)
{
size_t len = std::min(data_len, m_audio_buffer_in_len);
if (len > 0)
{
auto ptr = m_audio_buffer_in + m_audio_buffer_in_write_offset;

if (m_audio_buffer_in_write_offset >= len)
uint32_t avail = m_audio_buffer_in_max_len - m_audio_buffer_in_read_offset;
uint8_t *ptr = m_audio_buffer_in + m_audio_buffer_in_read_offset;
if (len <= avail)
{
memcpy(data, ptr - len, len);
m_audio_buffer_in_write_offset -= len;
memcpy(data, ptr, len);
m_audio_buffer_in_read_offset = (m_audio_buffer_in_read_offset + len) % m_audio_buffer_in_max_len;
}
else
{
auto end_segment = len - m_audio_buffer_in_write_offset;
memcpy(&data[end_segment], m_audio_buffer_in, m_audio_buffer_in_write_offset);
memcpy(data, m_audio_buffer_in + m_audio_buffer_in_max_len - end_segment, end_segment);
m_audio_buffer_in_write_offset = m_audio_buffer_in_max_len - end_segment;
// Wrapping case
memcpy(data, ptr, avail);
memcpy(&data[avail], m_audio_buffer_in, len - avail);
m_audio_buffer_in_read_offset = (len - avail) % m_audio_buffer_in_max_len;
}
m_audio_buffer_in_len -= len;

Expand All @@ -682,6 +685,7 @@ size_t AudioPipe::binaryReadPop(uint8_t *data, size_t data_len)

void AudioPipe::binaryReadClear()
{
m_audio_buffer_in_read_offset = 0;
m_audio_buffer_in_write_offset = 0;
m_audio_buffer_in_len = 0;
for (int i = 0; i < m_marks.size(); i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ namespace
tech_pvt->graceful_shutdown = 0;

size_t buflen = LWS_PRE + (FRAME_SIZE_8000 * desiredSampling / 8000 * channels * 1000 / RTP_PACKETIZATION_PERIOD * nAudioBufferSecs);
size_t bufInlen = LWS_PRE + (FRAME_SIZE_8000 * channels * nAudioInBufferSecs);
size_t bufInlen = (desiredSampling * channels * nAudioInBufferSecs);

AudioPipe *ap = new AudioPipe(tech_pvt->sessionId, host, port, path, sslFlags,
buflen, bufInlen, read_impl.decoded_bytes_per_packet, username, password, bugname, eventCallback);
if (!ap)
Expand Down Expand Up @@ -745,7 +745,7 @@ extern "C"
tech_pvt->id,
available, minBuffer);
return SWITCH_TRUE;
}
}

if (switch_mutex_trylock(tech_pvt->mutex) == SWITCH_STATUS_SUCCESS)
{
Expand Down Expand Up @@ -780,9 +780,6 @@ extern "C"
{

auto marks = pAudioPipe->clearExpiredMarks();
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%u) buffer clearExpiredMarks (%d) \n",
tech_pvt->id,
marks.size());
for (int i = 0; i < marks.size(); i++)
{
pTwilioHelper->mark(pAudioPipe, marks[i]);
Expand Down

0 comments on commit 34759cf

Please sign in to comment.