restoring ringbuffer to previous state. removing code that seems to leave no effect for now

This commit is contained in:
jenz 2025-10-12 21:18:36 +01:00
parent 678155ff57
commit cf86277e67
2 changed files with 99 additions and 127 deletions

View File

@ -300,8 +300,6 @@ bool CVoice::SDK_OnLoad(char *error, size_t maxlength, bool late)
//opus edit //opus edit
int err; int err;
//m_OpusEncoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
//m_OpusEncoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);I //content, broadcast, and applications requiring less than 15 ms of coding delay.
m_OpusEncoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err); m_OpusEncoder = opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, &err);
if (err<0) if (err<0)
{ {
@ -309,8 +307,8 @@ bool CVoice::SDK_OnLoad(char *error, size_t maxlength, bool late)
return false; return false;
} }
opus_encoder_ctl(m_OpusEncoder, OPUS_SET_BITRATE(48000)); opus_encoder_ctl(m_OpusEncoder, OPUS_SET_BITRATE(48000));
opus_encoder_ctl(m_OpusEncoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); // Force SILK mode opus_encoder_ctl(m_OpusEncoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND));
opus_encoder_ctl(m_OpusEncoder, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); opus_encoder_ctl(m_OpusEncoder, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND));
opus_encoder_ctl(m_OpusEncoder, OPUS_SET_COMPLEXITY(10)); opus_encoder_ctl(m_OpusEncoder, OPUS_SET_COMPLEXITY(10));
opus_encoder_ctl(m_OpusEncoder, OPUS_SET_VBR(0)); opus_encoder_ctl(m_OpusEncoder, OPUS_SET_VBR(0));
@ -762,10 +760,6 @@ void CVoice::HandleVoiceData()
{ {
int16_t aBuffer[TotalSamplesPerFrame]; int16_t aBuffer[TotalSamplesPerFrame];
size_t OldReadIdx = m_Buffer.m_ReadIndex;
size_t OldCurLength = m_Buffer.CurrentLength();
size_t OldTotalLength = m_Buffer.TotalLength();
if(!m_Buffer.Pop(aBuffer, TotalSamplesPerFrame)) if(!m_Buffer.Pop(aBuffer, TotalSamplesPerFrame))
{ {
smutils->LogError(myself, "Buffer pop failed!"); smutils->LogError(myself, "Buffer pop failed!");
@ -790,22 +784,6 @@ void CVoice::HandleVoiceData()
*pFrameSize = (uint16_t)nbBytes; *pFrameSize = (uint16_t)nbBytes;
*pTotalDataLength += sizeof(uint16_t) + nbBytes; *pTotalDataLength += sizeof(uint16_t) + nbBytes;
FinalSize += nbBytes; FinalSize += nbBytes;
// Buffer underrun check
for(int Client = 0; Client < MAX_CLIENTS; Client++)
{
CClient *pClient = &m_aClients[Client];
if(pClient->m_Socket == -1 || pClient->m_New == true)
continue;
m_Buffer.SetWriteIndex(pClient->m_BufferWriteIndex);
if(m_Buffer.CurrentLength() > pClient->m_LastLength)
{
pClient->m_BufferWriteIndex = m_Buffer.GetReadIndex();
m_Buffer.SetWriteIndex(pClient->m_BufferWriteIndex);
pClient->m_LastLength = m_Buffer.CurrentLength();
}
}
} }
// 8. Add CRC32 // 8. Add CRC32
@ -813,14 +791,6 @@ void CVoice::HandleVoiceData()
memcpy(&aFinal[FinalSize], &crc32_value, sizeof(uint32_t)); memcpy(&aFinal[FinalSize], &crc32_value, sizeof(uint32_t));
FinalSize += sizeof(uint32_t); FinalSize += sizeof(uint32_t);
/*
smutils->LogMessage(myself, "=== OPUS PACKET ===");
smutils->LogMessage(myself, "FinalSize: %d, Frames: %d", FinalSize, FramesAvailable);
smutils->LogMessage(myself, "Header bytes: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
aFinal[0], aFinal[1], aFinal[2], aFinal[3], aFinal[4], aFinal[5], aFinal[6],
aFinal[7], aFinal[8], aFinal[9], aFinal[10], aFinal[11], aFinal[12], aFinal[13]);
*/
BroadcastVoiceData(pClient, FinalSize, aFinal); BroadcastVoiceData(pClient, FinalSize, aFinal);
if (m_AvailableTime < getTime()) if (m_AvailableTime < getTime())

View File

@ -5,158 +5,160 @@
#undef NDEBUG #undef NDEBUG
#include <assert.h> #include <assert.h>
template <typename T> inline T min(T a, T b) { return a < b ? a : b; } template <typename T> inline T min(T a, T b) { return a<b?a:b; }
CRingBuffer::CRingBuffer() : m_BufferSize(sizeof(m_aBuffer) / sizeof(*m_aBuffer)) CRingBuffer::CRingBuffer() : m_BufferSize(sizeof(m_aBuffer) / sizeof(*m_aBuffer))
{ {
m_ReadIndex = 0; m_ReadIndex = 0;
m_WriteIndex = 0; m_WriteIndex = 0;
m_Length = 0; m_Length = 0;
} }
// Pop `Samples` int16_t values from buffer
bool CRingBuffer::Pop(int16_t *pBuffer, size_t Samples) bool CRingBuffer::Pop(int16_t *pBuffer, size_t Samples)
{ {
if(Samples > TotalLength()) if(Samples > TotalLength())
return false; return false;
if(m_ReadIndex + Samples > m_BufferSize) if(m_ReadIndex + Samples > m_BufferSize)
{ {
size_t TowardsEnd = m_BufferSize - m_ReadIndex; size_t TowardsEnd = m_BufferSize - m_ReadIndex;
memcpy(pBuffer, &m_aBuffer[m_ReadIndex], TowardsEnd * sizeof(*m_aBuffer)); memcpy(pBuffer, &m_aBuffer[m_ReadIndex], TowardsEnd * sizeof(*m_aBuffer));
m_ReadIndex = 0; m_ReadIndex = 0;
size_t Left = Samples - TowardsEnd; size_t Left = Samples - TowardsEnd;
memcpy(&pBuffer[TowardsEnd], m_aBuffer, Left * sizeof(*m_aBuffer)); memcpy(&pBuffer[TowardsEnd], m_aBuffer, Left * sizeof(*m_aBuffer));
m_ReadIndex = Left; m_ReadIndex = Left;
} }
else else
{ {
memcpy(pBuffer, &m_aBuffer[m_ReadIndex], Samples * sizeof(*m_aBuffer)); memcpy(pBuffer, &m_aBuffer[m_ReadIndex], Samples * sizeof(*m_aBuffer));
m_ReadIndex += Samples; m_ReadIndex += Samples;
} }
if(m_ReadIndex == m_BufferSize) if(m_ReadIndex == m_BufferSize)
m_ReadIndex = 0; m_ReadIndex = 0;
m_Length -= Samples; m_Length -= Samples;
return true;
return true;
} }
// Mix int16_t data into buffer at write index
void CRingBuffer::Mix(int16_t *pData, size_t Samples) void CRingBuffer::Mix(int16_t *pData, size_t Samples)
{ {
int16_t *pBuffer = &m_aBuffer[m_WriteIndex]; assert(!(m_WriteIndex + Samples > m_BufferSize));
while(Samples--)
{
int32_t Sample = *pBuffer;
Sample += *pData;
if(Sample > INT16_MAX) int16_t *pBuffer = &m_aBuffer[m_WriteIndex];
*pBuffer = INT16_MAX; while(Samples--)
else if(Sample < INT16_MIN) {
*pBuffer = INT16_MIN; int32_t Sample = *pBuffer;
else Sample += *pData;
*pBuffer = Sample;
pBuffer++; if(Sample > INT16_MAX)
pData++; *pBuffer = INT16_MAX;
} else if(Sample < INT16_MIN)
*pBuffer = INT16_MIN;
else
*pBuffer = Sample;
pBuffer++;
pData++;
}
} }
// Push `Samples` int16_t values into buffer
bool CRingBuffer::Push(int16_t *pData, size_t Samples) bool CRingBuffer::Push(int16_t *pData, size_t Samples)
{ {
if(Samples > CurrentFree()) if(Samples > CurrentFree())
return false; return false;
// Mix with existing data // Mix with data in front of us
if(CurrentLength() < TotalLength()) if(CurrentLength() < TotalLength())
{ {
size_t ToMix = min(Samples, TotalLength() - CurrentLength()); //
size_t ToMix = min(Samples, TotalLength() - CurrentLength());
if(m_WriteIndex + ToMix > m_BufferSize) if(m_WriteIndex + ToMix > m_BufferSize)
{ {
size_t TowardsEnd = m_BufferSize - m_WriteIndex; size_t TowardsEnd = m_BufferSize - m_WriteIndex;
Mix(pData, TowardsEnd); Mix(pData, TowardsEnd);
m_WriteIndex = 0; m_WriteIndex = 0;
size_t Left = ToMix - TowardsEnd; size_t Left = ToMix - TowardsEnd;
Mix(&pData[TowardsEnd], Left); Mix(&pData[TowardsEnd], Left);
m_WriteIndex = Left; m_WriteIndex = Left;
} }
else else
{ {
Mix(pData, ToMix); Mix(pData, ToMix);
m_WriteIndex += ToMix; m_WriteIndex += ToMix;
} }
if(m_WriteIndex == m_BufferSize) if(m_WriteIndex == m_BufferSize)
m_WriteIndex = 0; m_WriteIndex = 0;
pData += ToMix; pData += ToMix;
Samples -= ToMix; Samples -= ToMix;
} }
if(!Samples) //
return true; if(!Samples)
return true;
if(m_WriteIndex + Samples > m_BufferSize) if(m_WriteIndex + Samples > m_BufferSize)
{ {
size_t TowardsEnd = m_BufferSize - m_WriteIndex; size_t TowardsEnd = m_BufferSize - m_WriteIndex;
memcpy(&m_aBuffer[m_WriteIndex], pData, TowardsEnd * sizeof(*m_aBuffer)); memcpy(&m_aBuffer[m_WriteIndex], pData, TowardsEnd * sizeof(*m_aBuffer));
m_WriteIndex = 0; m_WriteIndex = 0;
size_t Left = Samples - TowardsEnd; size_t Left = Samples - TowardsEnd;
memcpy(m_aBuffer, &pData[TowardsEnd], Left * sizeof(*m_aBuffer)); memcpy(m_aBuffer, &pData[TowardsEnd], Left * sizeof(*m_aBuffer));
m_WriteIndex = Left; m_WriteIndex = Left;
} }
else else
{ {
memcpy(&m_aBuffer[m_WriteIndex], pData, Samples * sizeof(*m_aBuffer)); memcpy(&m_aBuffer[m_WriteIndex], pData, Samples * sizeof(*m_aBuffer));
m_WriteIndex += Samples; m_WriteIndex += Samples;
} }
if(m_WriteIndex == m_BufferSize) if(m_WriteIndex == m_BufferSize)
m_WriteIndex = 0; m_WriteIndex = 0;
m_Length += Samples; m_Length += Samples;
return true;
return true;
} }
// Helper functions
size_t CRingBuffer::TotalLength() size_t CRingBuffer::TotalLength()
{ {
return m_Length; return m_Length;
} }
size_t CRingBuffer::TotalFree() size_t CRingBuffer::TotalFree()
{ {
return m_BufferSize - m_Length - 1; return m_BufferSize - m_Length - 1;
} }
size_t CRingBuffer::CurrentLength() size_t CRingBuffer::CurrentLength()
{ {
return ((ssize_t)m_WriteIndex - (ssize_t)m_ReadIndex) % m_BufferSize; return ((ssize_t)m_WriteIndex - (ssize_t)m_ReadIndex) % m_BufferSize;
} }
size_t CRingBuffer::CurrentFree() size_t CRingBuffer::CurrentFree()
{ {
size_t BufferFree = ((ssize_t)m_ReadIndex - (ssize_t)m_WriteIndex) % m_BufferSize; size_t BufferFree = ((ssize_t)m_ReadIndex - (ssize_t)m_WriteIndex) % m_BufferSize;
return (BufferFree ? BufferFree : m_BufferSize) - 1; return (BufferFree ? BufferFree : m_BufferSize) - 1;
} }
size_t CRingBuffer::GetReadIndex() size_t CRingBuffer::GetReadIndex()
{ {
return m_ReadIndex; return m_ReadIndex;
} }
size_t CRingBuffer::GetWriteIndex() size_t CRingBuffer::GetWriteIndex()
{ {
return m_WriteIndex; return m_WriteIndex;
} }
void CRingBuffer::SetWriteIndex(size_t WriteIndex) void CRingBuffer::SetWriteIndex(size_t WriteIndex)
{ {
m_WriteIndex = WriteIndex % m_BufferSize; m_WriteIndex = WriteIndex;
} }