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,7 +5,7 @@
#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))
{ {
@ -14,7 +14,6 @@ CRingBuffer::CRingBuffer() : m_BufferSize(sizeof(m_aBuffer) / sizeof(*m_aBuffer)
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())
@ -40,12 +39,14 @@ bool CRingBuffer::Pop(int16_t *pBuffer, size_t Samples)
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)
{ {
assert(!(m_WriteIndex + Samples > m_BufferSize));
int16_t *pBuffer = &m_aBuffer[m_WriteIndex]; int16_t *pBuffer = &m_aBuffer[m_WriteIndex];
while(Samples--) while(Samples--)
{ {
@ -64,15 +65,15 @@ void CRingBuffer::Mix(int16_t *pData, size_t Samples)
} }
} }
// 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)
@ -98,6 +99,7 @@ bool CRingBuffer::Push(int16_t *pData, size_t Samples)
Samples -= ToMix; Samples -= ToMix;
} }
//
if(!Samples) if(!Samples)
return true; return true;
@ -121,10 +123,10 @@ bool CRingBuffer::Push(int16_t *pData, size_t Samples)
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;
@ -158,5 +160,5 @@ size_t CRingBuffer::GetWriteIndex()
void CRingBuffer::SetWriteIndex(size_t WriteIndex) void CRingBuffer::SetWriteIndex(size_t WriteIndex)
{ {
m_WriteIndex = WriteIndex % m_BufferSize; m_WriteIndex = WriteIndex;
} }