From a66193f7fd6b7170b2105324a4dbf34fe269d21f Mon Sep 17 00:00:00 2001 From: f4exb Date: Thu, 17 Aug 2017 01:03:09 +0200 Subject: [PATCH] UDPSink plugin: UDP buffer dynamic allocation (still fixed value) --- plugins/channeltx/udpsink/udpsink.cpp | 12 +++---- .../channeltx/udpsink/udpsinkudphandler.cpp | 32 ++++++++++++++++--- plugins/channeltx/udpsink/udpsinkudphandler.h | 9 ++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/plugins/channeltx/udpsink/udpsink.cpp b/plugins/channeltx/udpsink/udpsink.cpp index d45c787c5..2303dfc62 100644 --- a/plugins/channeltx/udpsink/udpsink.cpp +++ b/plugins/channeltx/udpsink/udpsink.cpp @@ -259,12 +259,12 @@ bool UDPSink::handleMessage(const Message& cmd) m_sampleRateSum = 0.0; m_sampleRateAvgCounter = 0; } - else - { - qDebug("UDPSink::handleMessage: MsgSampleRateCorrection: corr: %+.6f new rate: %.0f", - cfg.getCorrectionFactor(), - m_actualInputSampleRate); - } +// else +// { +// qDebug("UDPSink::handleMessage: MsgSampleRateCorrection: corr: %+.6f new rate: %.0f", +// cfg.getCorrectionFactor(), +// m_actualInputSampleRate); +// } m_settingsMutex.lock(); m_interpolatorDistanceRemain = 0; diff --git a/plugins/channeltx/udpsink/udpsinkudphandler.cpp b/plugins/channeltx/udpsink/udpsinkudphandler.cpp index 5f9033eb2..af54c881f 100644 --- a/plugins/channeltx/udpsink/udpsinkudphandler.cpp +++ b/plugins/channeltx/udpsink/udpsinkudphandler.cpp @@ -27,6 +27,8 @@ UDPSinkUDPHandler::UDPSinkUDPHandler() : m_dataPort(9999), m_dataConnected(false), m_udpReadBytes(0), + m_nbUDPFrames(m_minNbUDPFrames), + m_nbAllocatedUDPFrames(m_minNbUDPFrames), m_writeIndex(0), m_readFrameIndex(m_minNbUDPFrames/2), m_readIndex(0), @@ -34,10 +36,12 @@ UDPSinkUDPHandler::UDPSinkUDPHandler() : m_d(0), m_feedbackMessageQueue(0) { + m_udpBuf = new udpBlk_t[m_minNbUDPFrames]; } UDPSinkUDPHandler::~UDPSinkUDPHandler() { + delete[] m_udpBuf; } void UDPSinkUDPHandler::start() @@ -102,7 +106,7 @@ void UDPSinkUDPHandler::moveData() { memcpy(m_udpBuf[m_writeIndex], m_udpTmpBuf, m_udpBlockSize); - if (m_writeIndex < m_minNbUDPFrames - 1) { + if (m_writeIndex < m_nbUDPFrames - 1) { m_writeIndex++; } else { m_writeIndex = 0; @@ -131,14 +135,14 @@ void UDPSinkUDPHandler::advanceReadPointer(int nbBytes) { m_readIndex = 0; - if (m_readFrameIndex < m_minNbUDPFrames - 1) + if (m_readFrameIndex < m_nbUDPFrames - 1) { m_readFrameIndex++; } else { m_rwDelta = m_writeIndex; // raw R/W delta estimate - float d = (m_rwDelta - (m_minNbUDPFrames/2))/(float) m_minNbUDPFrames; + float d = (m_rwDelta - (m_nbUDPFrames/2))/(float) m_nbUDPFrames; //qDebug("UDPSinkUDPHandler::advanceReadPointer: w: %02d d: %f", m_writeIndex, d); if ((d < -0.45) || (d > 0.45)) @@ -182,8 +186,26 @@ void UDPSinkUDPHandler::configureUDPLink(const QString& address, quint16 port) void UDPSinkUDPHandler::resetReadIndex() { - m_readFrameIndex = (m_writeIndex + (m_minNbUDPFrames/2)) % m_minNbUDPFrames; - m_rwDelta = m_minNbUDPFrames/2; + m_readFrameIndex = (m_writeIndex + (m_nbUDPFrames/2)) % m_nbUDPFrames; + m_rwDelta = m_nbUDPFrames/2; m_readIndex = 0; m_d = 0.0f; } + +void UDPSinkUDPHandler::resizeBuffer(float sampleRate) +{ + int halfNbFrames = std::max((sampleRate / 375.0), (m_minNbUDPFrames / 2.0)); + qDebug("UDPSinkUDPHandler::resizeBuffer: nb_frames: %d", 2*halfNbFrames); + + if (2*halfNbFrames > m_nbAllocatedUDPFrames) + { + delete[] m_udpBuf; + m_udpBuf = new udpBlk_t[2*halfNbFrames]; + m_nbAllocatedUDPFrames = 2*halfNbFrames; + } + + m_nbUDPFrames = 2*halfNbFrames; + m_writeIndex = 0; + + resetReadIndex(); +} diff --git a/plugins/channeltx/udpsink/udpsinkudphandler.h b/plugins/channeltx/udpsink/udpsinkudphandler.h index f573e8c8f..1ae8cd83b 100644 --- a/plugins/channeltx/udpsink/udpsinkudphandler.h +++ b/plugins/channeltx/udpsink/udpsinkudphandler.h @@ -37,6 +37,7 @@ public: void stop(); void configureUDPLink(const QString& address, quint16 port); void resetReadIndex(); + void resizeBuffer(float sampleRate); void readSample(Real &t); void readSample(Sample &s); @@ -49,8 +50,8 @@ public: */ inline int32_t getBufferGauge() const { - int32_t val = m_rwDelta - (m_minNbUDPFrames/2); - return (100*val) / m_minNbUDPFrames; + int32_t val = m_rwDelta - (m_nbUDPFrames/2); + return (100*val) / m_nbUDPFrames; } static const int m_udpBlockSize = 512; // UDP block size in number of bytes @@ -72,7 +73,9 @@ private: bool m_dataConnected; udpBlk_t m_udpTmpBuf; qint64 m_udpReadBytes; - udpBlk_t m_udpBuf[m_minNbUDPFrames]; + udpBlk_t *m_udpBuf; + int m_nbUDPFrames; + int m_nbAllocatedUDPFrames; int m_writeIndex; int m_readFrameIndex; int m_readIndex;