| 
									
										
										
										
											2018-09-16 11:23:24 +02: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 06:39:30 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +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-02-02 22:58:42 +01:00
										 |  |  | #include "udpsinkfecworker.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:42:08 +02:00
										 |  |  | #include <QUdpSocket>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(UDPSinkFECWorker::MsgUDPFECEncodeAndSend, Message) | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(UDPSinkFECWorker::MsgConfigureRemoteAddress, Message) | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | MESSAGE_CLASS_DEFINITION(UDPSinkFECWorker::MsgStartStop, Message) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | UDPSinkFECWorker::UDPSinkFECWorker() : | 
					
						
							|  |  |  |         m_running(false), | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |         m_udpSocket(0), | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |         m_remotePort(9090) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_cm256Valid = m_cm256.isInitialized(); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:42:08 +02:00
										 |  |  |     connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UDPSinkFECWorker::~UDPSinkFECWorker() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | void UDPSinkFECWorker::startStop(bool start) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     MsgStartStop *msg = MsgStartStop::create(start); | 
					
						
							|  |  |  |     m_inputMessageQueue.push(msg); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | void UDPSinkFECWorker::startWork() | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     qDebug("UDPSinkFECWorker::startWork"); | 
					
						
							|  |  |  |     m_startWaitMutex.lock(); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:42:08 +02:00
										 |  |  |     m_udpSocket = new QUdpSocket(this); | 
					
						
							| 
									
										
										
										
											2018-11-18 09:39:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     start(); | 
					
						
							| 
									
										
										
										
											2018-11-18 09:39:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while(!m_running) { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |         m_startWaiter.wait(&m_startWaitMutex, 100); | 
					
						
							| 
									
										
										
										
											2018-11-18 09:39:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     m_startWaitMutex.unlock(); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | void UDPSinkFECWorker::stopWork() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qDebug("UDPSinkFECWorker::stopWork"); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:42:08 +02:00
										 |  |  |     delete m_udpSocket; | 
					
						
							|  |  |  |     m_udpSocket = 0; | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     m_running = false; | 
					
						
							|  |  |  |     wait(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UDPSinkFECWorker::run() | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_running  = true; | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     m_startWaiter.wakeAll(); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qDebug("UDPSinkFECWorker::process: started"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (m_running) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |         sleep(1); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     m_running = false; | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qDebug("UDPSinkFECWorker::process: stopped"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | void UDPSinkFECWorker::pushTxFrame(RemoteSuperBlock *txBlocks, | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     uint32_t nbBlocksFEC, | 
					
						
							|  |  |  |     uint32_t txDelay, | 
					
						
							|  |  |  |     uint16_t frameIndex) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     //qDebug("UDPSinkFECWorker::pushTxFrame. %d", m_inputMessageQueue.size());
 | 
					
						
							|  |  |  |     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)); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UDPSinkFECWorker::handleInputMessages() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Message* message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while ((message = m_inputMessageQueue.pop()) != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (MsgUDPFECEncodeAndSend::match(*message)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             MsgUDPFECEncodeAndSend *sendMsg = (MsgUDPFECEncodeAndSend *) message; | 
					
						
							|  |  |  |             encodeAndTransmit(sendMsg->getTxBlocks(), sendMsg->getFrameIndex(), sendMsg->getNbBlocsFEC(), sendMsg->getTxDelay()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (MsgConfigureRemoteAddress::match(*message)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("UDPSinkFECWorker::handleInputMessages: %s", message->getIdentifier()); | 
					
						
							|  |  |  |             MsgConfigureRemoteAddress *addressMsg = (MsgConfigureRemoteAddress *) message; | 
					
						
							|  |  |  |             m_remoteAddress = addressMsg->getAddress(); | 
					
						
							|  |  |  |             m_remotePort = addressMsg->getPort(); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:42:08 +02:00
										 |  |  |             m_remoteHostAddress.setAddress(addressMsg->getAddress()); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |         else if (MsgStartStop::match(*message)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             MsgStartStop* notif = (MsgStartStop*) message; | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |             qDebug("UDPSinkFECWorker::handleInputMessages: MsgStartStop: %s", notif->getStartStop() ? "start" : "stop"); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (notif->getStartStop()) { | 
					
						
							|  |  |  |                 startWork(); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 stopWork(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         delete message; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | void UDPSinkFECWorker::encodeAndTransmit(RemoteSuperBlock *txBlockx, uint16_t frameIndex, uint32_t nbBlocksFEC, uint32_t txDelay) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +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-16 11:23:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if ((nbBlocksFEC == 0) || !m_cm256Valid) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |         if (m_udpSocket) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |             for (unsigned int i = 0; i < RemoteNbOrginalBlocks; i++) | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |                 m_udpSocket->writeDatagram((const char *) &txBlockx[i], RemoteUdpSize, m_remoteHostAddress, m_remotePort); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |                 usleep(txDelay); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |         cm256Params.BlockBytes = sizeof(RemoteProtectedBlock); | 
					
						
							|  |  |  |         cm256Params.OriginalCount = RemoteNbOrginalBlocks; | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +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((char *) &txBlockx[i].m_protectedBlock, 0, sizeof(RemoteProtectedBlock)); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02: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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Encode FEC blocks
 | 
					
						
							|  |  |  |         if (m_cm256.cm256_encode(cm256Params, descriptorBlocks, fecBlocks)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("UDPSinkFECWorker::encodeAndTransmit: CM256 encode failed. No transmission."); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 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
 | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |         if (m_udpSocket) | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |             for (int i = 0; i < cm256Params.OriginalCount + cm256Params.RecoveryCount; i++) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     #ifdef REMOTE_PUNCTURE
 | 
					
						
							|  |  |  |                 if (i == REMOTE_PUNCTURE) { | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |     #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |                 m_udpSocket->writeDatagram((const char *) &txBlockx[i], RemoteUdpSize, m_remoteHostAddress, m_remotePort); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:53:46 +02:00
										 |  |  |                 usleep(txDelay); | 
					
						
							| 
									
										
										
										
											2018-09-16 11:23:24 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |