| 
									
										
										
										
											2017-05-21 04:19:12 +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.                                           //
 | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +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
										 |  |  | #ifndef PLUGINS_SAMPLESINK_REMOTEOUTPUT_UDPSINKFEC_H_
 | 
					
						
							|  |  |  | #define PLUGINS_SAMPLESINK_REMOTEOUTPUT_UDPSINKFEC_H_
 | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | #include <channel/remotedatablock.h>
 | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <cstddef>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QObject>
 | 
					
						
							|  |  |  | #include <QHostAddress>
 | 
					
						
							|  |  |  | #include <QString>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							|  |  |  | #include "util/CRC64.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 18:24:50 +01:00
										 |  |  | class QThread; | 
					
						
							|  |  |  | class RemoteOutputSender; | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UDPSinkFEC : public QObject | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Q_OBJECT | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     static const uint32_t m_udpSize = 512;          //!< Size of UDP block in number of bytes
 | 
					
						
							|  |  |  |     static const uint32_t m_nbOriginalBlocks = 128; //!< Number of original blocks in a protected block sequence
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Construct UDP sink | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     UDPSinkFEC(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Destroy UDP sink */ | 
					
						
							|  |  |  |     ~UDPSinkFEC(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |     void init(); | 
					
						
							| 
									
										
										
										
											2019-12-12 18:24:50 +01:00
										 |  |  |     void startSender(); | 
					
						
							|  |  |  |     void stopSender(); | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Write IQ samples | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |     void write(const SampleVector::iterator& begin, uint32_t sampleChunkSize, bool isTx); | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Return the last error, or return an empty string if there is no error. */ | 
					
						
							|  |  |  |     std::string error() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::string ret(m_error); | 
					
						
							|  |  |  |         m_error.clear(); | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:27:19 +01:00
										 |  |  |     void setDeviceIndex(uint32_t deviceIndex) { m_deviceIndex = deviceIndex; } | 
					
						
							| 
									
										
										
										
											2018-09-11 01:01:43 +02:00
										 |  |  |     /** Set sample rate given in S/s */ | 
					
						
							|  |  |  |     void setSampleRate(uint32_t sampleRate); | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void setNbBlocksFEC(uint32_t nbBlocksFEC); | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |     void setNbTxBytes(uint32_t nbTxBytes) { m_nbTxBytes = nbTxBytes; } | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  |     void setRemoteAddress(const QString& address, uint16_t port); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Return true if the stream is OK, return false if there is an error. */ | 
					
						
							|  |  |  |     operator bool() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_error.empty(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     std::string  m_error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint32_t     m_sampleRate;        //!< sample rate in Hz
 | 
					
						
							|  |  |  |     uint32_t     m_nbSamples;         //!< total number of samples sent int the last frame
 | 
					
						
							|  |  |  |     QHostAddress m_ownAddress; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CRC64        m_crc64; | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     RemoteMetaDataFEC m_currentMetaFEC;  //!< Meta data for current frame
 | 
					
						
							| 
									
										
										
										
											2018-09-13 00:31:49 +02:00
										 |  |  |     uint32_t m_nbBlocksFEC;                 //!< Variable number of FEC blocks
 | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |     uint32_t m_nbTxBytes; | 
					
						
							| 
									
										
										
										
											2018-09-13 00:31:49 +02:00
										 |  |  |     float m_txDelayRatio;                   //!< Delay in ratio of nominal frame period
 | 
					
						
							| 
									
										
										
										
											2021-12-12 10:44:58 +01:00
										 |  |  |     RemoteDataFrame *m_dataFrame; | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  |     RemoteSuperBlock m_superBlock;       //!< current super block being built
 | 
					
						
							| 
									
										
										
										
											2018-09-13 00:31:49 +02:00
										 |  |  |     int m_txBlockIndex;                     //!< Current index in blocks to transmit in the Tx row
 | 
					
						
							|  |  |  |     uint16_t m_frameCount;                  //!< transmission frame count
 | 
					
						
							|  |  |  |     int m_sampleIndex;                      //!< Current sample index in protected block data
 | 
					
						
							| 
									
										
										
										
											2021-12-23 16:27:19 +01:00
										 |  |  |     uint32_t m_deviceIndex;              //!< Index of current device set
 | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 18:24:50 +01:00
										 |  |  |     RemoteOutputSender *m_remoteOutputSender; | 
					
						
							|  |  |  |     QThread *m_senderThread; | 
					
						
							| 
									
										
										
										
											2018-09-17 03:33:18 +02:00
										 |  |  |     QString m_remoteAddress; | 
					
						
							|  |  |  |     uint16_t m_remotePort; | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     uint32_t getNbSampleBits(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |     inline void convertSampleToData(const SampleVector::iterator& begin, int nbSamples, bool isTx) | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (sizeof(Sample) == m_nbTxBytes * 2) // 16 -> 16 or 24 ->24: direct copy
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             memcpy((void *) &m_superBlock.m_protectedBlock.buf[m_sampleIndex*m_nbTxBytes*2], | 
					
						
							|  |  |  |                     (const void *) &(*(begin)), | 
					
						
							|  |  |  |                     nbSamples * sizeof(Sample)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |         else if (isTx) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (m_nbTxBytes == 4) // just convert type int16_t -> int32_t (always 16 bit wide)
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     *((int32_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (begin+i)->m_real; | 
					
						
							|  |  |  |                     *((int32_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (begin+i)->m_imag; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (m_nbTxBytes == 2) //just convert type int32_t -> int16_t (always 16 bit wide)
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     *((int16_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (begin+i)->m_real; | 
					
						
							|  |  |  |                     *((int16_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (begin+i)->m_imag; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |             else if (m_nbTxBytes == 1) // 16 or 24 -> 8
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |                     *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (int8_t) ((begin+i)->m_real / (1<<8)); | 
					
						
							|  |  |  |                     *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (int8_t) ((begin+i)->m_imag / (1<<8)); | 
					
						
							| 
									
										
										
										
											2021-12-19 11:30:48 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (m_nbTxBytes == 4) // 16 -> 24
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |                     *((int32_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (begin+i)->m_real * (1<<8); | 
					
						
							|  |  |  |                     *((int32_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (begin+i)->m_imag * (1<<8); | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (m_nbTxBytes == 2) // 24 -> 16
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |                     *((int16_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (begin+i)->m_real / (1<<8); | 
					
						
							|  |  |  |                     *((int16_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (begin+i)->m_imag / (1<<8); | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |             else if ((m_nbTxBytes == 1) && (sizeof(Sample) == 4)) // 16 -> 8
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                    *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (int8_t) ((begin+i)->m_real / (1<<8)); | 
					
						
							|  |  |  |                    *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (int8_t) ((begin+i)->m_imag / (1<<8)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if ((m_nbTxBytes == 1) && (sizeof(Sample) == 4)) // 24 -> 8
 | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |             { | 
					
						
							|  |  |  |                 for (int i = 0; i < nbSamples; i++) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2021-12-20 22:30:43 +01:00
										 |  |  |                     *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2]) = (int8_t) ((begin+i)->m_real / (1<<16)); | 
					
						
							|  |  |  |                     *((int8_t*) &m_superBlock.m_protectedBlock.buf[(m_sampleIndex+ i)*m_nbTxBytes*2 + m_nbTxBytes]) = (int8_t) ((begin+i)->m_imag / (1<<16)); | 
					
						
							| 
									
										
										
										
											2021-12-18 22:49:05 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-21 04:19:12 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 22:58:42 +01:00
										 |  |  | #endif /* PLUGINS_SAMPLESINK_REMOTEOUTPUT_UDPSINKFEC_H_ */
 |