1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-16 18:06:35 -04:00
sdrangel/plugins/samplesink/sdrdaemonsink/udpsinkfec.cpp

338 lines
12 KiB
C++
Raw Normal View History

2017-05-20 22:19:12 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Edouard Griffiths, F4EXB //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <sys/time.h>
#include <unistd.h>
#include <boost/crc.hpp>
#include <boost/cstdint.hpp>
#include "udpsinkfec.h"
MESSAGE_CLASS_DEFINITION(UDPSinkFECWorker::MsgUDPFECEncodeAndSend, Message)
MESSAGE_CLASS_DEFINITION(UDPSinkFECWorker::MsgConfigureRemoteAddress, Message)
UDPSinkFEC::UDPSinkFEC() :
m_sampleRate(48000),
m_nbSamples(0),
m_nbBlocksFEC(0),
m_txDelayRatio(0.0),
2017-05-20 22:19:12 -04:00
m_txDelay(0),
m_txBlockIndex(0),
m_txBlocksIndex(0),
m_frameCount(0),
m_sampleIndex(0)
{
memset((char *) m_txBlocks, 0, 4*256*sizeof(SDRDaemonSuperBlock));
memset((char *) &m_superBlock, 0, sizeof(SDRDaemonSuperBlock));
2017-05-20 22:19:12 -04:00
m_currentMetaFEC.init();
m_bufMeta = new uint8_t[m_udpSize];
m_buf = new uint8_t[m_udpSize];
2017-05-22 19:41:30 -04:00
m_udpThread = new QThread();
2017-05-20 22:19:12 -04:00
m_udpWorker = new UDPSinkFECWorker();
2017-05-22 19:41:30 -04:00
m_udpWorker->moveToThread(m_udpThread);
connect(m_udpThread, SIGNAL(started()), m_udpWorker, SLOT(process()));
connect(m_udpWorker, SIGNAL(finished()), m_udpThread, SLOT(quit()));
m_udpThread->start();
2017-05-20 22:19:12 -04:00
}
UDPSinkFEC::~UDPSinkFEC()
{
2017-05-22 19:41:30 -04:00
m_udpWorker->stop();
m_udpThread->wait();
2017-05-20 22:19:12 -04:00
delete[] m_buf;
delete[] m_bufMeta;
delete m_udpWorker;
2017-05-22 19:41:30 -04:00
delete m_udpThread;
2017-05-20 22:19:12 -04:00
}
void UDPSinkFEC::setTxDelay(float txDelayRatio)
2017-05-20 22:19:12 -04:00
{
// delay is calculated from the fraction of the nominal UDP block process time
// frame size: 127 * (126 or 63 samples depending on I or Q sample bytes of 2 or 4 bytes respectively)
// 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 = SDRDaemonNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8);
double delay = ((127*samplesPerBlock*txDelayRatio) / m_sampleRate)/(128 + m_nbBlocksFEC);
m_txDelay = delay * 1e6;
qDebug() << "UDPSinkFEC::setTxDelay: txDelay: " << txDelayRatio << " m_txDelay: " << m_txDelay << " us";
2017-05-20 22:19:12 -04:00
}
void UDPSinkFEC::setNbBlocksFEC(uint32_t nbBlocksFEC)
{
qDebug() << "UDPSinkFEC::setNbBlocksFEC: nbBlocksFEC: " << nbBlocksFEC;
m_nbBlocksFEC = nbBlocksFEC;
setTxDelay(m_txDelayRatio);
}
void UDPSinkFEC::setSampleRate(uint32_t sampleRate)
{
qDebug() << "UDPSinkFEC::setSampleRate: sampleRate: " << sampleRate;
m_sampleRate = sampleRate;
setTxDelay(m_txDelayRatio);
2017-05-20 22:19:12 -04:00
}
void UDPSinkFEC::setRemoteAddress(const QString& address, uint16_t port)
{
qDebug() << "UDPSinkFEC::setRemoteAddress: address: " << address << " port: " << port;
m_udpWorker->setRemoteAddress(address, port);
}
void UDPSinkFEC::write(const SampleVector::iterator& begin, uint32_t sampleChunkSize)
{
const SampleVector::iterator end = begin + sampleChunkSize;
SampleVector::iterator it = begin;
while (it != end)
{
int inRemainingSamples = end - it;
if (m_txBlockIndex == 0) // Tx block index 0 is a block with only meta data
{
struct timeval tv;
SDRDaemonMetaDataFEC metaData;
2017-05-20 22:19:12 -04:00
gettimeofday(&tv, 0);
metaData.m_centerFrequency = 0; // frequency not set by stream
2017-05-20 22:19:12 -04:00
metaData.m_sampleRate = m_sampleRate;
metaData.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
metaData.m_sampleBits = SDR_RX_SAMP_SZ;
2017-05-20 22:19:12 -04:00
metaData.m_nbOriginalBlocks = m_nbOriginalBlocks;
metaData.m_nbFECBlocks = m_nbBlocksFEC;
metaData.m_tv_sec = tv.tv_sec;
metaData.m_tv_usec = tv.tv_usec;
boost::crc_32_type crc32;
crc32.process_bytes(&metaData, 20);
metaData.m_crc32 = crc32.checksum();
memset((char *) &m_superBlock, 0, sizeof(m_superBlock));
2017-05-20 22:19:12 -04:00
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;
memcpy((char *) &m_superBlock.m_protectedBlock, (const char *) &metaData, sizeof(SDRDaemonMetaDataFEC));
2017-05-20 22:19:12 -04:00
if (!(metaData == m_currentMetaFEC))
{
qDebug() << "UDPSinkFEC::write: meta: "
<< "|" << metaData.m_centerFrequency
<< ":" << metaData.m_sampleRate
<< ":" << (int) (metaData.m_sampleBytes & 0xF)
<< ":" << (int) metaData.m_sampleBits
<< "|" << (int) metaData.m_nbOriginalBlocks
<< ":" << (int) metaData.m_nbFECBlocks
<< "|" << metaData.m_tv_sec
<< ":" << metaData.m_tv_usec
<< "|";
m_currentMetaFEC = metaData;
}
m_txBlocks[m_txBlocksIndex][0] = m_superBlock;
m_txBlockIndex = 1; // next Tx block with data
}
int samplesPerBlock = SDRDaemonNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8); // two I or Q samples
2017-05-20 22:19:12 -04:00
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),
inRemainingSamples * sizeof(Sample));
2017-05-20 22:19:12 -04:00
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),
(samplesPerBlock - m_sampleIndex) * sizeof(Sample));
2017-05-20 22:19:12 -04:00
it += samplesPerBlock - m_sampleIndex;
m_sampleIndex = 0;
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;
2017-05-20 22:19:12 -04:00
m_txBlocks[m_txBlocksIndex][m_txBlockIndex] = m_superBlock;
if (m_txBlockIndex == m_nbOriginalBlocks - 1) // frame complete
{
int nbBlocksFEC = m_nbBlocksFEC;
int txDelay = m_txDelay;
m_udpWorker->pushTxFrame(m_txBlocks[m_txBlocksIndex], nbBlocksFEC, txDelay, m_frameCount);
m_txBlocksIndex = (m_txBlocksIndex + 1) % 4;
m_txBlockIndex = 0;
m_frameCount++;
}
else
{
m_txBlockIndex++;
}
}
}
}
2017-05-22 19:41:30 -04:00
UDPSinkFECWorker::UDPSinkFECWorker() :
m_running(false),
m_remotePort(9090)
2017-05-20 22:19:12 -04:00
{
m_cm256Valid = m_cm256.isInitialized();
2017-05-22 19:41:30 -04:00
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::DirectConnection);
2017-05-20 22:19:12 -04:00
}
UDPSinkFECWorker::~UDPSinkFECWorker()
{
2017-05-22 19:41:30 -04:00
disconnect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
m_inputMessageQueue.clear();
2017-05-20 22:19:12 -04:00
}
void UDPSinkFECWorker::pushTxFrame(SDRDaemonSuperBlock *txBlocks,
2017-05-20 22:19:12 -04:00
uint32_t nbBlocksFEC,
uint32_t txDelay,
uint16_t frameIndex)
{
2017-05-22 19:41:30 -04:00
//qDebug("UDPSinkFECWorker::pushTxFrame. %d", m_inputMessageQueue.size());
2017-05-20 22:19:12 -04:00
m_inputMessageQueue.push(MsgUDPFECEncodeAndSend::create(txBlocks, nbBlocksFEC, txDelay, frameIndex));
}
void UDPSinkFECWorker::setRemoteAddress(const QString& address, uint16_t port)
{
m_inputMessageQueue.push(MsgConfigureRemoteAddress::create(address, port));
}
2017-05-22 19:41:30 -04:00
void UDPSinkFECWorker::process()
{
m_running = true;
qDebug("UDPSinkFECWorker::process: started");
while (m_running)
{
usleep(250000);
}
qDebug("UDPSinkFECWorker::process: stopped");
emit finished();
}
void UDPSinkFECWorker::stop()
{
m_running = false;
}
2017-05-20 22:19:12 -04:00
void UDPSinkFECWorker::handleInputMessages()
{
Message* message;
while ((message = m_inputMessageQueue.pop()) != 0)
{
if (MsgUDPFECEncodeAndSend::match(*message))
{
MsgUDPFECEncodeAndSend *sendMsg = (MsgUDPFECEncodeAndSend *) message;
2017-05-22 19:41:30 -04:00
encodeAndTransmit(sendMsg->getTxBlocks(), sendMsg->getFrameIndex(), sendMsg->getNbBlocsFEC(), sendMsg->getTxDelay());
2017-05-20 22:19:12 -04:00
}
else if (MsgConfigureRemoteAddress::match(*message))
{
2017-05-22 19:41:30 -04:00
qDebug("UDPSinkFECWorker::handleInputMessages: %s", message->getIdentifier());
2017-05-20 22:19:12 -04:00
MsgConfigureRemoteAddress *addressMsg = (MsgConfigureRemoteAddress *) message;
2017-05-22 19:41:30 -04:00
m_remoteAddress = addressMsg->getAddress();
2017-05-20 22:19:12 -04:00
m_remotePort = addressMsg->getPort();
}
delete message;
}
}
void UDPSinkFECWorker::encodeAndTransmit(SDRDaemonSuperBlock *txBlockx, uint16_t frameIndex, uint32_t nbBlocksFEC, uint32_t txDelay)
2017-05-20 22:19:12 -04:00
{
CM256::cm256_encoder_params cm256Params; //!< Main interface with CM256 encoder
CM256::cm256_block descriptorBlocks[256]; //!< Pointers to data for CM256 encoder
SDRDaemonProtectedBlock fecBlocks[256]; //!< FEC data
2017-05-20 22:19:12 -04:00
if ((nbBlocksFEC == 0) || !m_cm256Valid)
{
2017-05-25 14:13:34 -04:00
for (unsigned int i = 0; i < UDPSinkFEC::m_nbOriginalBlocks; i++)
2017-05-20 22:19:12 -04:00
{
2017-05-22 19:41:30 -04:00
m_socket.SendDataGram((const void *) &txBlockx[i], (int) UDPSinkFEC::m_udpSize, m_remoteAddress.toStdString(), (uint32_t) m_remotePort);
//m_udpSocket->writeDatagram((const char *) &txBlockx[i], (int) UDPSinkFEC::m_udpSize, m_remoteAddress, m_remotePort);
2017-05-20 22:19:12 -04:00
usleep(txDelay);
}
}
else
{
cm256Params.BlockBytes = sizeof(SDRDaemonProtectedBlock);
2017-05-20 22:19:12 -04:00
cm256Params.OriginalCount = UDPSinkFEC::m_nbOriginalBlocks;
cm256Params.RecoveryCount = nbBlocksFEC;
// Fill pointers to data
for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; ++i)
{
if (i >= cm256Params.OriginalCount) {
memset((char *) &txBlockx[i].m_protectedBlock, 0, sizeof(SDRDaemonProtectedBlock));
2017-05-20 22:19:12 -04:00
}
txBlockx[i].m_header.m_frameIndex = frameIndex;
txBlockx[i].m_header.m_blockIndex = i;
txBlockx[i].m_header.m_sampleBytes = (SDR_RX_SAMP_SZ <= 16 ? 2 : 4);
txBlockx[i].m_header.m_sampleBits = SDR_RX_SAMP_SZ;
descriptorBlocks[i].Block = (void *) &(txBlockx[i].m_protectedBlock);
descriptorBlocks[i].Index = txBlockx[i].m_header.m_blockIndex;
2017-05-20 22:19:12 -04:00
}
// Encode FEC blocks
if (m_cm256.cm256_encode(cm256Params, descriptorBlocks, fecBlocks))
{
2017-05-22 19:41:30 -04:00
qDebug("UDPSinkFECWorker::encodeAndTransmit: CM256 encode failed. No transmission.");
2017-05-20 22:19:12 -04:00
return;
}
// Merge FEC with data to transmit
for (int i = 0; i < cm256Params.RecoveryCount; i++)
{
txBlockx[i + cm256Params.OriginalCount].m_protectedBlock = fecBlocks[i];
2017-05-20 22:19:12 -04:00
}
// Transmit all blocks
2017-05-22 19:41:30 -04:00
2017-05-20 22:19:12 -04:00
for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; i++)
{
#ifdef SDRDAEMON_PUNCTURE
if (i == SDRDAEMON_PUNCTURE) {
continue;
}
#endif
m_socket.SendDataGram((const void *) &txBlockx[i], (int) UDPSinkFEC::m_udpSize, m_remoteAddress.toStdString(), (uint32_t) m_remotePort);
2017-05-20 22:19:12 -04:00
usleep(txDelay);
}
}
}