| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | // Copyright (C) 2018-2019 Edouard Griffiths, F4EXB.                             //
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | // Remote sink channel (Rx) UDP sender thread                                    //
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01: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-05 01:32:29 +02: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 06:39:30 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02: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-01-22 23:39:12 +01:00
										 |  |  | #include "remotesinkthread.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | #include <channel/remotedatablock.h>
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | #include <QUdpSocket>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-15 10:42:39 +02:00
										 |  |  | #include "cm256cc/cm256.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | MESSAGE_CLASS_DEFINITION(RemoteSinkThread::MsgStartStop, Message) | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | RemoteSinkThread::RemoteSinkThread(QObject* parent) : | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |     QThread(parent), | 
					
						
							|  |  |  |     m_running(false), | 
					
						
							|  |  |  |     m_address(QHostAddress::LocalHost), | 
					
						
							|  |  |  |     m_socket(0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-06 04:36:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_cm256p = m_cm256.isInitialized() ? &m_cm256 : 0; | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |     connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | RemoteSinkThread::~RemoteSinkThread() | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |     qDebug("RemoteSinkThread::~RemoteSinkThread"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | void RemoteSinkThread::startStop(bool start) | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     MsgStartStop *msg = MsgStartStop::create(start); | 
					
						
							|  |  |  |     m_inputMessageQueue.push(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | void RemoteSinkThread::startWork() | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |     qDebug("RemoteSinkThread::startWork"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 	m_startWaitMutex.lock(); | 
					
						
							|  |  |  | 	m_socket = new QUdpSocket(this); | 
					
						
							|  |  |  | 	start(); | 
					
						
							|  |  |  | 	while(!m_running) | 
					
						
							|  |  |  | 		m_startWaiter.wait(&m_startWaitMutex, 100); | 
					
						
							|  |  |  | 	m_startWaitMutex.unlock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | void RemoteSinkThread::stopWork() | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | 	qDebug("RemoteSinkThread::stopWork"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |     delete m_socket; | 
					
						
							|  |  |  |     m_socket = 0; | 
					
						
							|  |  |  | 	m_running = false; | 
					
						
							|  |  |  | 	wait(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | void RemoteSinkThread::run() | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |     qDebug("RemoteSinkThread::run: begin"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 	m_running = true; | 
					
						
							|  |  |  | 	m_startWaiter.wakeAll(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (m_running) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         sleep(1); // Do nothing as everything is in the data handler (dequeuer)
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_running = false; | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |     qDebug("RemoteSinkThread::run: end"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | void RemoteSinkThread::processDataBlock(RemoteDataBlock *dataBlock) | 
					
						
							| 
									
										
										
										
											2018-09-06 05:21:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     handleDataBlock(*dataBlock); | 
					
						
							|  |  |  |     delete dataBlock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | void RemoteSinkThread::handleDataBlock(RemoteDataBlock& dataBlock) | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	CM256::cm256_encoder_params cm256Params;  //!< Main interface with CM256 encoder
 | 
					
						
							|  |  |  | 	CM256::cm256_block descriptorBlocks[256]; //!< Pointers to data for CM256 encoder
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | 	RemoteProtectedBlock fecBlocks[256];   //!< FEC data
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02: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; | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     RemoteSuperBlock *txBlockx = dataBlock.m_superBlocks; | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 04:36:56 +02:00
										 |  |  |     if ((nbBlocksFEC == 0) || !m_cm256p) // Do not FEC encode
 | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (m_socket) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |             for (int i = 0; i < RemoteNbOrginalBlocks; i++) | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |             { | 
					
						
							|  |  |  |                 // send block via UDP
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |                 m_socket->writeDatagram((const char*)&txBlockx[i], (qint64 ) RemoteUdpSize, m_address, dataPort); | 
					
						
							| 
									
										
										
										
											2018-09-06 04:23:27 +02:00
										 |  |  |                 usleep(txDelay); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |         cm256Params.BlockBytes = sizeof(RemoteProtectedBlock); | 
					
						
							|  |  |  |         cm256Params.OriginalCount = RemoteNbOrginalBlocks; | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |         cm256Params.RecoveryCount = nbBlocksFEC; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Fill pointers to data
 | 
					
						
							|  |  |  |         for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; ++i) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (i >= cm256Params.OriginalCount) { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |                 memset((void *) &txBlockx[i].m_protectedBlock, 0, sizeof(RemoteProtectedBlock)); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             txBlockx[i].m_header.m_frameIndex = frameIndex; | 
					
						
							|  |  |  |             txBlockx[i].m_header.m_blockIndex = i; | 
					
						
							| 
									
										
										
										
											2018-09-13 00:31:49 +02:00
										 |  |  |             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-05 01:32:29 +02:00
										 |  |  |             descriptorBlocks[i].Block = (void *) &(txBlockx[i].m_protectedBlock); | 
					
						
							|  |  |  |             descriptorBlocks[i].Index = txBlockx[i].m_header.m_blockIndex; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Encode FEC blocks
 | 
					
						
							| 
									
										
										
										
											2018-09-06 04:36:56 +02:00
										 |  |  |         if (m_cm256p->cm256_encode(cm256Params, descriptorBlocks, fecBlocks)) | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |             qWarning("RemoteSinkThread::handleDataBlock: CM256 encode failed. No transmission."); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02: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
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |                 m_socket->writeDatagram((const char*)&txBlockx[i], (qint64 ) RemoteUdpSize, m_address, dataPort); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  |                 usleep(txDelay); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dataBlock.m_txControlBlock.m_processed = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  | void RemoteSinkThread::handleInputMessages() | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Message* message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((message = m_inputMessageQueue.pop()) != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (MsgStartStop::match(*message)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             MsgStartStop* notif = (MsgStartStop*) message; | 
					
						
							| 
									
										
										
										
											2019-01-22 23:39:12 +01:00
										 |  |  |             qDebug("RemoteSinkThread::handleInputMessages: MsgStartStop: %s", notif->getStartStop() ? "start" : "stop"); | 
					
						
							| 
									
										
										
										
											2018-09-05 01:32:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (notif->getStartStop()) { | 
					
						
							|  |  |  |                 startWork(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 stopWork(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             delete message; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |