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 //
|
2019-04-11 00:39:30 -04:00
|
|
|
// (at your option) any later version. //
|
2017-05-20 22:19:12 -04:00
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
#include "udpsinkfec.h"
|
|
|
|
|
2017-05-20 22:19:12 -04:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <boost/crc.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
|
2018-11-13 08:21:36 -05:00
|
|
|
#include "util/timeutil.h"
|
2019-02-02 16:58:42 -05:00
|
|
|
|
2018-09-16 05:23:24 -04:00
|
|
|
#include "udpsinkfecworker.h"
|
2017-05-20 22:19:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
UDPSinkFEC::UDPSinkFEC() :
|
|
|
|
m_sampleRate(48000),
|
|
|
|
m_nbSamples(0),
|
|
|
|
m_nbBlocksFEC(0),
|
2018-09-10 19:01:43 -04:00
|
|
|
m_txDelayRatio(0.0),
|
2017-05-20 22:19:12 -04:00
|
|
|
m_txDelay(0),
|
|
|
|
m_txBlockIndex(0),
|
|
|
|
m_txBlocksIndex(0),
|
|
|
|
m_frameCount(0),
|
2018-09-16 21:33:18 -04:00
|
|
|
m_sampleIndex(0),
|
|
|
|
m_udpWorker(0),
|
|
|
|
m_remoteAddress("127.0.0.1"),
|
|
|
|
m_remotePort(9090)
|
2017-05-20 22:19:12 -04:00
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
memset((char *) m_txBlocks, 0, 4*256*sizeof(RemoteSuperBlock));
|
|
|
|
memset((char *) &m_superBlock, 0, sizeof(RemoteSuperBlock));
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSinkFEC::~UDPSinkFEC()
|
|
|
|
{
|
|
|
|
delete[] m_buf;
|
|
|
|
delete[] m_bufMeta;
|
2018-09-16 21:33:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSinkFEC::start()
|
|
|
|
{
|
|
|
|
m_udpWorker = new UDPSinkFECWorker();
|
|
|
|
m_udpWorker->setRemoteAddress(m_remoteAddress, m_remotePort);
|
|
|
|
m_udpWorker->startStop(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSinkFEC::stop()
|
|
|
|
{
|
|
|
|
if (m_udpWorker)
|
|
|
|
{
|
|
|
|
m_udpWorker->startStop(false);
|
|
|
|
m_udpWorker->deleteLater();
|
|
|
|
m_udpWorker = 0;
|
|
|
|
}
|
2017-05-20 22:19:12 -04:00
|
|
|
}
|
|
|
|
|
2018-09-10 19:01:43 -04:00
|
|
|
void UDPSinkFEC::setTxDelay(float txDelayRatio)
|
2017-05-20 22:19:12 -04:00
|
|
|
{
|
2018-09-10 19:01:43 -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;
|
2019-02-02 16:58:42 -05:00
|
|
|
int samplesPerBlock = RemoteNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8);
|
2018-09-10 19:01:43 -04:00
|
|
|
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;
|
2018-09-10 19:01:43 -04:00
|
|
|
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;
|
2018-09-16 21:33:18 -04:00
|
|
|
m_remoteAddress = address;
|
|
|
|
m_remotePort = port;
|
|
|
|
|
|
|
|
if (m_udpWorker) {
|
|
|
|
m_udpWorker->setRemoteAddress(m_remoteAddress, m_remotePort);
|
|
|
|
}
|
2017-05-20 22:19:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
RemoteMetaDataFEC metaData;
|
2017-05-20 22:19:12 -04:00
|
|
|
|
2018-11-13 08:21:36 -05:00
|
|
|
uint64_t ts_usecs = TimeUtil::nowus();
|
2017-05-20 22:19:12 -04:00
|
|
|
|
2018-09-06 18:58:09 -04:00
|
|
|
metaData.m_centerFrequency = 0; // frequency not set by stream
|
2017-05-20 22:19:12 -04:00
|
|
|
metaData.m_sampleRate = m_sampleRate;
|
2018-09-12 18:31:49 -04:00
|
|
|
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;
|
2018-11-13 08:21:36 -05:00
|
|
|
metaData.m_tv_sec = ts_usecs / 1000000UL;
|
|
|
|
metaData.m_tv_usec = ts_usecs % 1000000UL;
|
2017-05-20 22:19:12 -04:00
|
|
|
|
|
|
|
boost::crc_32_type crc32;
|
|
|
|
crc32.process_bytes(&metaData, 20);
|
|
|
|
|
|
|
|
metaData.m_crc32 = crc32.checksum();
|
|
|
|
|
2018-04-04 17:39:31 -04:00
|
|
|
memset((char *) &m_superBlock, 0, sizeof(m_superBlock));
|
2017-05-20 22:19:12 -04:00
|
|
|
|
2018-09-12 18:31:49 -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;
|
2018-11-18 05:06:41 -05:00
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
RemoteMetaDataFEC *destMeta = (RemoteMetaDataFEC *) &m_superBlock.m_protectedBlock;
|
2018-11-18 05:06:41 -05:00
|
|
|
*destMeta = metaData;
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
int samplesPerBlock = RemoteNbBytesPerBlock / (SDR_RX_SAMP_SZ <= 16 ? 4 : 8); // two I or Q samples
|
2018-09-10 18:24:18 -04:00
|
|
|
|
2017-05-20 22:19:12 -04:00
|
|
|
if (m_sampleIndex + inRemainingSamples < samplesPerBlock) // there is still room in the current super block
|
|
|
|
{
|
2018-09-12 18:31:49 -04:00
|
|
|
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
|
|
|
|
{
|
2018-09-12 18:31:49 -04:00
|
|
|
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;
|
|
|
|
|
2018-09-12 18:31:49 -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;
|
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;
|
|
|
|
|
2018-09-16 21:33:18 -04:00
|
|
|
if (m_udpWorker) {
|
|
|
|
m_udpWorker->pushTxFrame(m_txBlocks[m_txBlocksIndex], nbBlocksFEC, txDelay, m_frameCount);
|
|
|
|
}
|
2017-05-20 22:19:12 -04:00
|
|
|
|
|
|
|
m_txBlocksIndex = (m_txBlocksIndex + 1) % 4;
|
|
|
|
m_txBlockIndex = 0;
|
|
|
|
m_frameCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_txBlockIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|