| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2016 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                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_
 | 
					
						
							|  |  |  | #define PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | #include <QString>
 | 
					
						
							|  |  |  | #include <cstdlib>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | #include "util/CRC64.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SDRdaemonBuffer | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | #pragma pack(push, 1)
 | 
					
						
							|  |  |  | 	struct MetaData | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  |         // critical data
 | 
					
						
							| 
									
										
										
										
											2016-02-14 20:01:46 +01:00
										 |  |  | 		uint32_t m_centerFrequency;   //!< center frequency in kHz
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 		uint32_t m_sampleRate;        //!< sample rate in Hz
 | 
					
						
							|  |  |  | 		uint8_t  m_sampleBytes;       //!< MSB(4): indicators, LSB(4) number of bytes per sample
 | 
					
						
							|  |  |  | 		uint8_t  m_sampleBits;        //!< number of effective bits per sample
 | 
					
						
							|  |  |  | 		uint16_t m_blockSize;         //!< payload size
 | 
					
						
							|  |  |  | 		uint32_t m_nbSamples;         //!< number of samples in a hardware block
 | 
					
						
							|  |  |  |         // end of critical data
 | 
					
						
							|  |  |  | 		uint16_t m_nbBlocks;          //!< number of hardware blocks in the frame
 | 
					
						
							|  |  |  | 		uint32_t m_nbBytes;           //!< total number of bytes in the frame
 | 
					
						
							|  |  |  | 		uint32_t m_tv_sec;            //!< seconds of timestamp at start time of frame processing
 | 
					
						
							|  |  |  | 		uint32_t m_tv_usec;           //!< microseconds of timestamp at start time of frame processing
 | 
					
						
							|  |  |  | 		uint64_t m_crc;               //!< 64 bit CRC of the above
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bool operator==(const MetaData& rhs) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		    return (memcmp((const void *) this, (const void *) &rhs, 20) == 0); // Only the 20 first bytes are relevant (critical)
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void init() | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			memset((void *) this, 0, sizeof(MetaData)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void operator=(const MetaData& rhs) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			memcpy((void *) this, (const void *) &rhs, sizeof(MetaData)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | #pragma pack(pop)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	SDRdaemonBuffer(uint32_t throttlems); | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 	~SDRdaemonBuffer(); | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  | 	bool readMeta(char *array);                   //!< Attempt to read meta. Returns true if meta block
 | 
					
						
							| 
									
										
										
										
											2016-01-28 04:54:16 +01:00
										 |  |  | 	void writeData(char *array, uint32_t length); //!< Write data into buffer.
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	uint8_t *readData(int32_t length); | 
					
						
							|  |  |  | 	void updateBlockCounts(uint32_t nbBytesReceived); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 	const MetaData& getCurrentMeta() const { return m_currentMeta; } | 
					
						
							| 
									
										
										
										
											2016-02-22 15:03:16 +01:00
										 |  |  | 	uint32_t getSampleRateStream() const { return m_sampleRateStream; } | 
					
						
							| 
									
										
										
										
											2016-02-22 12:10:13 +01:00
										 |  |  | 	uint32_t getSampleRate() const { return m_sampleRate; } | 
					
						
							| 
									
										
										
										
											2016-01-26 23:48:52 +01:00
										 |  |  | 	bool isSync() const { return m_sync; } | 
					
						
							| 
									
										
										
										
											2016-02-21 11:01:43 +01:00
										 |  |  | 	bool isSyncLocked() const { return m_syncLock; } | 
					
						
							| 
									
										
										
										
											2016-02-23 18:09:20 +01:00
										 |  |  | 	uint32_t getFrameSize() const { return m_frameSize; } | 
					
						
							| 
									
										
										
										
											2016-02-23 18:19:35 +01:00
										 |  |  | 	bool isLz4Compressed() const { return m_lz4; } | 
					
						
							| 
									
										
										
										
											2016-02-23 19:27:47 +01:00
										 |  |  | 	float getCompressionRatio() const { return (m_frameSize > 0 ? (float) m_lz4InSize / (float) m_frameSize : 1.0); } | 
					
						
							|  |  |  | 	uint32_t getLz4DataCRCOK() const { return m_nbLastLz4CRCOK; } | 
					
						
							|  |  |  | 	uint32_t getLz4SuccessfulDecodes() const { return m_nbLastLz4SuccessfulDecodes; } | 
					
						
							| 
									
										
										
										
											2016-03-20 14:40:40 +01:00
										 |  |  | 	float getBufferLengthInSecs() const { return m_bufferLenSec; } | 
					
						
							| 
									
										
										
										
											2016-03-11 05:25:25 +01:00
										 |  |  | 	void setAutoFollowRate(bool autoFollowRate) { m_autoFollowRate = autoFollowRate; } | 
					
						
							| 
									
										
										
										
											2016-03-18 14:03:34 +01:00
										 |  |  |     void setAutoCorrBuffer(bool autoCorrBuffer) { m_autoCorrBuffer = autoCorrBuffer; } | 
					
						
							|  |  |  |     void setResetIndexes() { m_resetIndexes = true; } | 
					
						
							|  |  |  |     int32_t getRWBalanceCorrection() const { return m_balCorrection; } | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 15:41:48 +01:00
										 |  |  |     /** Get buffer gauge value in % of buffer size ([-50:50])
 | 
					
						
							|  |  |  |      *  [-50:0] : write leads or read lags | 
					
						
							|  |  |  |      *  [0:50]  : read leads or write lags | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     inline int32_t getBufferGauge() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_rawSize) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2016-03-18 10:52:35 +01:00
										 |  |  |             int32_t val = ((m_writeIndex - m_readIndex) * 100) / (int32_t) m_rawSize; | 
					
						
							| 
									
										
										
										
											2016-03-17 15:41:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 08:45:01 +01:00
										 |  |  |             if (val < -50) { | 
					
						
							| 
									
										
										
										
											2016-03-18 10:52:35 +01:00
										 |  |  |                 return val + 100; // read leads (positive)
 | 
					
						
							|  |  |  |             } else if (val < 50) { | 
					
						
							|  |  |  |                 return val;       // read leads (positive) or write leads (negative)
 | 
					
						
							| 
									
										
										
										
											2016-03-17 15:41:48 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2016-03-18 10:52:35 +01:00
										 |  |  |                 return val - 100; // write leads (negative)
 | 
					
						
							| 
									
										
										
										
											2016-03-17 15:41:48 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2016-03-18 08:45:01 +01:00
										 |  |  |             return -50; // default position
 | 
					
						
							| 
									
										
										
										
											2016-03-17 15:41:48 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-20 03:41:20 +01:00
										 |  |  | 	static const int m_udpPayloadSize; | 
					
						
							|  |  |  | 	static const int m_sampleSize; | 
					
						
							|  |  |  | 	static const int m_iqSampleSize; | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	static const int m_rawBufferLengthSeconds; | 
					
						
							| 
									
										
										
										
											2016-03-20 12:59:55 +01:00
										 |  |  | 	static const int m_rawBufferMinNbFrames; //!< Minimum number of frames for the length of buffer
 | 
					
						
							| 
									
										
										
										
											2016-02-20 03:41:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	void updateBufferSize(uint32_t sampleRate); | 
					
						
							| 
									
										
										
										
											2016-01-27 08:24:00 +01:00
										 |  |  | 	void updateLZ4Sizes(uint32_t frameSize); | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	void updateReadBufferSize(uint32_t length); | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  | 	void writeDataLZ4(const char *array, int length); | 
					
						
							| 
									
										
										
										
											2016-02-23 01:29:03 +01:00
										 |  |  | 	void writeToRawBufferLZ4(); | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  | 	void writeToRawBufferUncompressed(const char *array, int length); | 
					
						
							| 
									
										
										
										
											2016-03-15 18:56:15 +01:00
										 |  |  |     void resetIndexes(); | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  |     static void printMeta(const QString& header, MetaData *metaData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t m_throttlemsNominal;  //!< Initial throttle in ms
 | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  | 	int      m_rawSize;            //!< Size of the raw samples buffer in bytes
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  |     uint8_t  *m_rawBuffer;         //!< Buffer for raw samples obtained from UDP (I/Q not in a formal I/Q structure)
 | 
					
						
							|  |  |  |     uint32_t m_sampleRateStream;   //!< Current sample rate from the stream meta data
 | 
					
						
							|  |  |  |     uint32_t m_sampleRate;         //!< Current actual sample rate in Hz
 | 
					
						
							|  |  |  | 	uint8_t  m_sampleBytes;        //!< Current number of bytes per I or Q sample
 | 
					
						
							|  |  |  | 	uint8_t  m_sampleBits;         //!< Current number of effective bits per sample
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-21 11:01:43 +01:00
										 |  |  | 	bool m_sync;             //!< Meta data acquired
 | 
					
						
							|  |  |  | 	bool m_syncLock;         //!< Meta data expected (Stream synchronized)
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | 	bool m_lz4;              //!< Stream is compressed with LZ4
 | 
					
						
							|  |  |  | 	MetaData m_currentMeta;  //!< Stored current meta data
 | 
					
						
							|  |  |  | 	CRC64 m_crc64;           //!< CRC64 calculator
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  |     uint32_t m_nbBlocks;     //!< Number of UDP blocks received in the current frame
 | 
					
						
							|  |  |  |     uint32_t m_bytesInBlock; //!< Number of bytes received in the current UDP block
 | 
					
						
							|  |  |  |     uint64_t m_dataCRC;      //!< CRC64 of the data block
 | 
					
						
							| 
									
										
										
										
											2016-01-27 08:40:54 +01:00
										 |  |  | 	uint32_t m_inCount;      //!< Current position of uncompressed input
 | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  |     int      m_lz4InCount;   //!< Current position in LZ4 input buffer
 | 
					
						
							|  |  |  |     int      m_lz4InSize;    //!< Size in bytes of the LZ4 input data
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  |     uint8_t *m_lz4InBuffer;  //!< Buffer for LZ4 compressed input
 | 
					
						
							| 
									
										
										
										
											2016-01-26 08:27:45 +01:00
										 |  |  |     uint8_t *m_lz4OutBuffer; //!< Buffer for LZ4 uncompressed output
 | 
					
						
							| 
									
										
										
										
											2016-01-27 08:24:00 +01:00
										 |  |  |     uint32_t m_frameSize;    //!< Size in bytes of one uncompressed frame
 | 
					
						
							| 
									
										
										
										
											2016-03-20 14:40:40 +01:00
										 |  |  |     float    m_bufferLenSec; //!< Raw buffer length in seconds
 | 
					
						
							| 
									
										
										
										
											2016-02-20 23:02:34 +01:00
										 |  |  |     uint32_t m_nbLz4Decodes; | 
					
						
							|  |  |  |     uint32_t m_nbLz4SuccessfulDecodes; | 
					
						
							|  |  |  |     uint32_t m_nbLz4CRCOK; | 
					
						
							| 
									
										
										
										
											2016-02-23 19:27:47 +01:00
										 |  |  |     uint32_t m_nbLastLz4SuccessfulDecodes; | 
					
						
							|  |  |  |     uint32_t m_nbLastLz4CRCOK; | 
					
						
							| 
									
										
										
										
											2016-02-22 12:10:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	int32_t  m_writeIndex;   //!< Current write position in the raw samples buffer
 | 
					
						
							|  |  |  | 	int32_t  m_readIndex;    //!< Current read position in the raw samples buffer
 | 
					
						
							| 
									
										
										
										
											2017-05-25 20:13:34 +02:00
										 |  |  | 	int      m_readSize;     //!< Read buffer size
 | 
					
						
							| 
									
										
										
										
											2016-03-15 06:13:52 +01:00
										 |  |  | 	uint8_t  *m_readBuffer;  //!< Read buffer to hold samples when looping back to beginning of raw buffer
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool     m_autoFollowRate; //!< Auto follow stream sample rate else stick with meta data sample rate
 | 
					
						
							| 
									
										
										
										
											2016-03-18 14:03:34 +01:00
										 |  |  |     bool     m_autoCorrBuffer; //!< Auto correct buffer read / write balance (attempt to ...)
 | 
					
						
							| 
									
										
										
										
											2016-03-15 18:56:15 +01:00
										 |  |  |     bool     m_skewTest; | 
					
						
							|  |  |  |     bool     m_skewCorrection; //!< Do a skew rate correction at next meta data reception
 | 
					
						
							| 
									
										
										
										
											2016-03-18 14:03:34 +01:00
										 |  |  |     bool     m_resetIndexes;   //!< Do a reset indexes at next meta data reception
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int64_t  m_readCount;    //!< Number of bytes read for auto skew compensation
 | 
					
						
							|  |  |  |     int64_t  m_writeCount;   //!< Number of bytes written for auto skew compensation
 | 
					
						
							|  |  |  |     uint32_t m_nbCycles;     //!< Number of buffer cycles since start of auto skew compensation byte counting
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 03:49:38 +01:00
										 |  |  |     uint32_t m_nbReads;      //!< Number of buffer reads since start of auto R/W balance correction period
 | 
					
						
							| 
									
										
										
										
											2016-03-18 14:03:34 +01:00
										 |  |  |     int32_t  m_balCorrection; //!< R/W balance correction in number of samples
 | 
					
						
							| 
									
										
										
										
											2016-03-19 18:54:09 +01:00
										 |  |  |     int32_t  m_balCorrLimit; //!< Correction absolute value limit in number of samples
 | 
					
						
							| 
									
										
										
										
											2016-01-24 23:38:55 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_ */
 |