1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-02 22:14:45 -04:00

Remote output: threading refactoring

This commit is contained in:
f4exb
2019-12-12 18:24:50 +01:00
parent 1a8028a4eb
commit 30ddab77e3
9 changed files with 470 additions and 82 deletions
+63 -59
View File
@@ -17,6 +17,7 @@
#include "udpsinkfec.h"
#include <QThread>
#include <QDebug>
#include <boost/crc.hpp>
@@ -24,8 +25,7 @@
#include "util/timeutil.h"
#include "udpsinkfecworker.h"
#include "remoteoutputsender.h"
UDPSinkFEC::UDPSinkFEC() :
m_sampleRate(48000),
@@ -33,42 +33,42 @@ UDPSinkFEC::UDPSinkFEC() :
m_nbBlocksFEC(0),
m_txDelayRatio(0.0),
m_txDelay(0),
m_dataBlock(nullptr),
m_txBlockIndex(0),
m_txBlocksIndex(0),
m_frameCount(0),
m_sampleIndex(0),
m_udpWorker(0),
m_remoteOutputSender(nullptr),
m_senderThread(nullptr),
m_remoteAddress("127.0.0.1"),
m_remotePort(9090)
{
memset((char *) m_txBlocks, 0, 4*256*sizeof(RemoteSuperBlock));
memset((char *) &m_superBlock, 0, sizeof(RemoteSuperBlock));
m_currentMetaFEC.init();
m_bufMeta = new uint8_t[m_udpSize];
m_buf = new uint8_t[m_udpSize];
m_senderThread = new QThread(this);
m_remoteOutputSender = new RemoteOutputSender();
m_remoteOutputSender->moveToThread(m_senderThread);
}
UDPSinkFEC::~UDPSinkFEC()
{
delete[] m_buf;
delete[] m_bufMeta;
delete m_remoteOutputSender;
delete m_senderThread;
}
void UDPSinkFEC::start()
void UDPSinkFEC::startSender()
{
m_udpWorker = new UDPSinkFECWorker();
m_udpWorker->setRemoteAddress(m_remoteAddress, m_remotePort);
m_udpWorker->startStop(true);
qDebug("UDPSinkFEC::startSender");
m_remoteOutputSender->setDestination(m_remoteAddress, m_remotePort);
m_senderThread->start();
}
void UDPSinkFEC::stop()
void UDPSinkFEC::stopSender()
{
if (m_udpWorker)
{
m_udpWorker->startStop(false);
m_udpWorker->deleteLater();
m_udpWorker = 0;
}
qDebug("UDPSinkFEC::stopSender");
m_senderThread->exit();
m_senderThread->wait();
}
void UDPSinkFEC::setTxDelay(float txDelayRatio)
@@ -78,10 +78,14 @@ void UDPSinkFEC::setTxDelay(float txDelayRatio)
// divided by sample rate gives the frame process time
// divided by the number of actual blocks including FEC blocks gives the block (i.e. UDP block) process time
m_txDelayRatio = txDelayRatio;
int samplesPerBlock = RemoteNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8);
double delay = ((127*samplesPerBlock*txDelayRatio) / m_sampleRate)/(128 + m_nbBlocksFEC);
int samplesPerBlock = RemoteNbBytesPerBlock / sizeof(Sample);
double delay = m_sampleRate == 0 ? 1.0 : (127*samplesPerBlock*txDelayRatio) / m_sampleRate;
delay /= 128 + m_nbBlocksFEC;
m_txDelay = delay * 1e6;
qDebug() << "UDPSinkFEC::setTxDelay: txDelay: " << txDelayRatio << " m_txDelay: " << m_txDelay << " us";
qDebug() << "UDPSinkFEC::setTxDelay:"
<< "txDelay:" << txDelayRatio
<< "m_txDelay:" << m_txDelay << " us"
<< "m_sampleRate:" << m_sampleRate;
}
void UDPSinkFEC::setNbBlocksFEC(uint32_t nbBlocksFEC)
@@ -103,49 +107,49 @@ void UDPSinkFEC::setRemoteAddress(const QString& address, uint16_t port)
qDebug() << "UDPSinkFEC::setRemoteAddress: address: " << address << " port: " << port;
m_remoteAddress = address;
m_remotePort = port;
if (m_udpWorker) {
m_udpWorker->setRemoteAddress(m_remoteAddress, m_remotePort);
}
m_remoteOutputSender->setDestination(m_remoteAddress, m_remotePort);
}
void UDPSinkFEC::write(const SampleVector::iterator& begin, uint32_t sampleChunkSize)
{
const SampleVector::iterator end = begin + sampleChunkSize;
SampleVector::iterator it = begin;
SampleVector::const_iterator it = begin;
while (it != end)
{
int inSamplesIndex = it - begin;
int inRemainingSamples = end - it;
if (m_txBlockIndex == 0) // Tx block index 0 is a block with only meta data
if (m_txBlockIndex == 0)
{
RemoteMetaDataFEC metaData;
uint64_t ts_usecs = TimeUtil::nowus();
uint64_t nowus = TimeUtil::nowus();
metaData.m_centerFrequency = 0; // frequency not set by stream
metaData.m_sampleRate = m_sampleRate;
metaData.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
metaData.m_sampleBits = SDR_RX_SAMP_SZ;
metaData.m_nbOriginalBlocks = m_nbOriginalBlocks;
metaData.m_nbOriginalBlocks = RemoteNbOrginalBlocks;
metaData.m_nbFECBlocks = m_nbBlocksFEC;
metaData.m_tv_sec = ts_usecs / 1000000UL;
metaData.m_tv_usec = ts_usecs % 1000000UL;
metaData.m_tv_sec = nowus / 1000000UL; // tv.tv_sec;
metaData.m_tv_usec = nowus % 1000000UL; // tv.tv_usec;
if (!m_dataBlock) { // on the very first cycle there is no data block allocated
m_dataBlock = m_remoteOutputSender->getDataBlock(); // ask a new block to sender
}
boost::crc_32_type crc32;
crc32.process_bytes(&metaData, sizeof(RemoteMetaDataFEC)-4);
metaData.m_crc32 = crc32.checksum();
RemoteSuperBlock& superBlock = m_dataBlock->m_superBlocks[0]; // first block
superBlock.init();
superBlock.m_header.m_frameIndex = m_frameCount;
superBlock.m_header.m_blockIndex = m_txBlockIndex;
superBlock.m_header.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
superBlock.m_header.m_sampleBits = SDR_RX_SAMP_SZ;
memset((char *) &m_superBlock, 0, sizeof(m_superBlock));
m_superBlock.m_header.m_frameIndex = m_frameCount;
m_superBlock.m_header.m_blockIndex = m_txBlockIndex;
m_superBlock.m_header.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
m_superBlock.m_header.m_sampleBits = SDR_RX_SAMP_SZ;
RemoteMetaDataFEC *destMeta = (RemoteMetaDataFEC *) &m_superBlock.m_protectedBlock;
RemoteMetaDataFEC *destMeta = (RemoteMetaDataFEC *) &superBlock.m_protectedBlock;
*destMeta = metaData;
if (!(metaData == m_currentMetaFEC))
@@ -158,30 +162,28 @@ void UDPSinkFEC::write(const SampleVector::iterator& begin, uint32_t sampleChunk
<< "|" << (int) metaData.m_nbOriginalBlocks
<< ":" << (int) metaData.m_nbFECBlocks
<< "|" << metaData.m_tv_sec
<< ":" << metaData.m_tv_usec
<< "|";
<< ":" << metaData.m_tv_usec;
m_currentMetaFEC = metaData;
}
m_txBlocks[m_txBlocksIndex][0] = m_superBlock;
m_txBlockIndex = 1; // next Tx block with data
}
} // block zero
// handle different sample sizes...
int samplesPerBlock = RemoteNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8); // two I or Q samples
if (m_sampleIndex + inRemainingSamples < samplesPerBlock) // there is still room in the current super block
{
memcpy((char *) &m_superBlock.m_protectedBlock.buf[m_sampleIndex*sizeof(Sample)],
(const char *) &(*it),
memcpy((void *) &m_superBlock.m_protectedBlock.buf[m_sampleIndex*sizeof(Sample)],
(const void *) &(*(begin+inSamplesIndex)),
inRemainingSamples * sizeof(Sample));
m_sampleIndex += inRemainingSamples;
it = end; // all input samples are consumed
}
else // complete super block and initiate the next if not end of frame
{
memcpy((char *) &m_superBlock.m_protectedBlock.buf[m_sampleIndex*sizeof(Sample)],
(const char *) &(*it),
memcpy((void *) &m_superBlock.m_protectedBlock.buf[m_sampleIndex*sizeof(Sample)],
(const void *) &(*(begin+inSamplesIndex)),
(samplesPerBlock - m_sampleIndex) * sizeof(Sample));
it += samplesPerBlock - m_sampleIndex;
m_sampleIndex = 0;
@@ -190,18 +192,20 @@ void UDPSinkFEC::write(const SampleVector::iterator& begin, uint32_t sampleChunk
m_superBlock.m_header.m_blockIndex = m_txBlockIndex;
m_superBlock.m_header.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
m_superBlock.m_header.m_sampleBits = SDR_RX_SAMP_SZ;
m_txBlocks[m_txBlocksIndex][m_txBlockIndex] = m_superBlock;
m_dataBlock->m_superBlocks[m_txBlockIndex] = m_superBlock;
if (m_txBlockIndex == m_nbOriginalBlocks - 1) // frame complete
if (m_txBlockIndex == RemoteNbOrginalBlocks - 1) // frame complete
{
int nbBlocksFEC = m_nbBlocksFEC;
int txDelay = m_txDelay;
m_dataBlock->m_txControlBlock.m_frameIndex = m_frameCount;
m_dataBlock->m_txControlBlock.m_processed = false;
m_dataBlock->m_txControlBlock.m_complete = true;
m_dataBlock->m_txControlBlock.m_nbBlocksFEC = m_nbBlocksFEC;
m_dataBlock->m_txControlBlock.m_txDelay = m_txDelay;
m_dataBlock->m_txControlBlock.m_dataAddress = m_remoteAddress;
m_dataBlock->m_txControlBlock.m_dataPort = m_remotePort;
if (m_udpWorker) {
m_udpWorker->pushTxFrame(m_txBlocks[m_txBlocksIndex], nbBlocksFEC, txDelay, m_frameCount);
}
m_dataBlock = m_remoteOutputSender->getDataBlock(); // ask a new block to sender
m_txBlocksIndex = (m_txBlocksIndex + 1) % 4;
m_txBlockIndex = 0;
m_frameCount++;
}