1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-27 02:09:14 -05:00
sdrangel/plugins/channelrx/remotesink/remotesinksender.cpp

152 lines
5.8 KiB
C++
Raw Normal View History

2018-09-04 19:32:29 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018-2019 Edouard Griffiths, F4EXB. //
2018-09-04 19:32:29 -04:00
// //
// Remote sink channel (Rx) UDP sender thread //
2018-09-04 19:32:29 -04:00
// //
// SDRangel can work as a detached SDR front end. With this plugin it can //
// sends the I/Q samples stream to another SDRangel instance via UDP. //
// It is controlled via a Web REST API. //
2018-09-04 19:32:29 -04:00
// //
// 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. //
2018-09-04 19:32:29 -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-12-11 17:19:22 -05:00
#include <thread>
#include <chrono>
2018-09-04 19:32:29 -04:00
#include <QUdpSocket>
#include "cm256cc/cm256.h"
2018-09-04 19:32:29 -04:00
2019-12-11 17:19:22 -05:00
#include "channel/remotedatablock.h"
#include "remotesinksender.h"
2018-09-04 19:32:29 -04:00
2019-12-11 17:19:22 -05:00
RemoteSinkSender::RemoteSinkSender() :
m_fifo(20, this),
2018-09-04 19:32:29 -04:00
m_address(QHostAddress::LocalHost),
2019-12-11 17:19:22 -05:00
m_socket(nullptr)
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
qDebug("RemoteSinkSender::RemoteSinkSender");
m_cm256p = m_cm256.isInitialized() ? &m_cm256 : nullptr;
m_socket = new QUdpSocket(this);
QObject::connect(
&m_fifo,
&RemoteSinkFifo::dataBlockServed,
this,
&RemoteSinkSender::handleData,
Qt::QueuedConnection
);
2018-09-04 19:32:29 -04:00
}
2019-12-11 17:19:22 -05:00
RemoteSinkSender::~RemoteSinkSender()
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
qDebug("RemoteSinkSender::~RemoteSinkSender");
delete m_socket;
2018-09-04 19:32:29 -04:00
}
2019-12-11 17:19:22 -05:00
RemoteDataBlock *RemoteSinkSender::getDataBlock()
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
return m_fifo.getDataBlock();
2018-09-04 19:32:29 -04:00
}
2019-12-11 17:19:22 -05:00
void RemoteSinkSender::handleData()
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
RemoteDataBlock *dataBlock;
unsigned int remainder = m_fifo.getRemainder();
2018-09-04 19:32:29 -04:00
2019-12-11 17:19:22 -05:00
while (remainder != 0)
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
remainder = m_fifo.readDataBlock(&dataBlock);
2018-09-04 19:32:29 -04:00
2019-12-11 17:19:22 -05:00
if (dataBlock) {
sendDataBlock(dataBlock);
}
}
2018-09-05 23:21:43 -04:00
}
2019-12-11 17:19:22 -05:00
void RemoteSinkSender::sendDataBlock(RemoteDataBlock *dataBlock)
2018-09-04 19:32:29 -04:00
{
CM256::cm256_encoder_params cm256Params; //!< Main interface with CM256 encoder
CM256::cm256_block descriptorBlocks[256]; //!< Pointers to data for CM256 encoder
RemoteProtectedBlock fecBlocks[256]; //!< FEC data
2018-09-04 19:32:29 -04:00
2019-12-11 17:19:22 -05:00
uint16_t frameIndex = dataBlock->m_txControlBlock.m_frameIndex;
int nbBlocksFEC = dataBlock->m_txControlBlock.m_nbBlocksFEC;
int txDelay = dataBlock->m_txControlBlock.m_txDelay;
m_address.setAddress(dataBlock->m_txControlBlock.m_dataAddress);
uint16_t dataPort = dataBlock->m_txControlBlock.m_dataPort;
RemoteSuperBlock *txBlockx = dataBlock->m_superBlocks;
2018-09-04 19:32:29 -04:00
2018-09-05 22:36:56 -04:00
if ((nbBlocksFEC == 0) || !m_cm256p) // Do not FEC encode
2018-09-04 19:32:29 -04:00
{
if (m_socket)
{
for (int i = 0; i < RemoteNbOrginalBlocks; i++)
2018-09-04 19:32:29 -04:00
{
// send block via UDP
m_socket->writeDatagram((const char*)&txBlockx[i], (qint64 ) RemoteUdpSize, m_address, dataPort);
2019-12-11 17:19:22 -05:00
std::this_thread::sleep_for(std::chrono::microseconds(txDelay));
2018-09-04 19:32:29 -04:00
}
}
}
else
{
cm256Params.BlockBytes = sizeof(RemoteProtectedBlock);
cm256Params.OriginalCount = RemoteNbOrginalBlocks;
2018-09-04 19:32:29 -04:00
cm256Params.RecoveryCount = nbBlocksFEC;
// Fill pointers to data
for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; ++i)
{
if (i >= cm256Params.OriginalCount) {
memset((void *) &txBlockx[i].m_protectedBlock, 0, sizeof(RemoteProtectedBlock));
2018-09-04 19:32:29 -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;
2018-09-04 19:32:29 -04:00
descriptorBlocks[i].Block = (void *) &(txBlockx[i].m_protectedBlock);
descriptorBlocks[i].Index = txBlockx[i].m_header.m_blockIndex;
}
// Encode FEC blocks
2018-09-05 22:36:56 -04:00
if (m_cm256p->cm256_encode(cm256Params, descriptorBlocks, fecBlocks))
2018-09-04 19:32:29 -04:00
{
2019-12-11 17:19:22 -05:00
qWarning("RemoteSinkSender::handleDataBlock: CM256 encode failed. No transmission.");
2018-09-04 19:32:29 -04:00
// TODO: send without FEC changing meta data to set indication of no FEC
}
// Merge FEC with data to transmit
for (int i = 0; i < cm256Params.RecoveryCount; i++)
{
txBlockx[i + cm256Params.OriginalCount].m_protectedBlock = fecBlocks[i];
}
// Transmit all blocks
if (m_socket)
{
for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; i++)
{
// send block via UDP
m_socket->writeDatagram((const char*)&txBlockx[i], (qint64 ) RemoteUdpSize, m_address, dataPort);
2019-12-11 17:19:22 -05:00
std::this_thread::sleep_for(std::chrono::microseconds(txDelay));
2018-09-04 19:32:29 -04:00
}
}
}
2019-12-11 17:19:22 -05:00
dataBlock->m_txControlBlock.m_processed = true;
2018-09-04 19:32:29 -04:00
}