SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
remoteinputbuffer.h
Go to the documentation of this file.
1 // Copyright (C) 2016 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #ifndef PLUGINS_SAMPLESOURCE_REMOTEINPUT_REMOTEINPUTBUFFER_H_
19 #define PLUGINS_SAMPLESOURCE_REMOTEINPUT_REMOTEINPUTBUFFER_H_
20 
22 #include <QString>
23 #include <QDebug>
24 #include <cstdlib>
25 #include "cm256cc/cm256.h"
26 #include "util/movingaverage.h"
27 
28 
29 #define REMOTEINPUT_UDPSIZE 512 // UDP payload size
30 #define REMOTEINPUT_NBORIGINALBLOCKS 128 // number of sample blocks per frame excluding FEC blocks
31 #define REMOTEINPUT_NBDECODERSLOTS 16 // power of two sub multiple of uint16_t size. A too large one is superfluous.
32 
34 {
35 public:
38 
39  // R/W operations
40  void writeData(char *array);
41  uint8_t *readData(int32_t length);
42 
43  // meta data
44  const RemoteMetaDataFEC& getCurrentMeta() const { return m_currentMeta; }
45 
46  // samples timestamp
47  uint32_t getTVOutSec() const { return m_tvOut_sec; }
48  uint32_t getTVOutUsec() const { return m_tvOut_usec; }
49  uint64_t getTVOutMSec() const { return (m_tvOut_sec * 1000LL) + (m_tvOut_usec/ 1000LL); }
50 
51  // stats
52 
53  int getCurNbBlocks() const { return m_curNbBlocks; }
54  int getCurOriginalBlocks() const { return m_curOriginalBlocks; }
55  int getCurNbRecovery() const { return m_curNbRecovery; }
56  float getAvgNbBlocks() const { return m_avgNbBlocks; }
57  float getAvgOriginalBlocks() const { return m_avgOrigBlocks; }
58  float getAvgNbRecovery() const { return m_avgNbRecovery; }
59 
61  {
62  int minNbBlocks = m_minNbBlocks;
63  m_minNbBlocks = 256;
64  return minNbBlocks;
65  }
66 
68  {
69  int minOriginalBlocks = m_minOriginalBlocks;
70  m_minOriginalBlocks = 128;
71  return minOriginalBlocks;
72  }
73 
75  {
76  int maxNbRecovery = m_maxNbRecovery;
77  m_maxNbRecovery = 0;
78  return maxNbRecovery;
79  }
80 
82  {
83  bool framesDecoded = m_framesDecoded;
84  m_framesDecoded = true;
85  return framesDecoded;
86  }
87 
88  float getBufferLengthInSecs() const { return m_bufferLenSec; }
90 
95  inline int32_t getBufferGauge() const
96  {
97  if (m_framesNbBytes)
98  {
100  int32_t ret = val < 0 ? -val - 50 : 50 -val;
101  return ret;
102  }
103  else
104  {
105  return 0; // default position
106  }
107  }
108 
109  static const int framesSize = REMOTEINPUT_NBDECODERSLOTS * (RemoteNbOrginalBlocks - 1) * RemoteNbBytesPerBlock;
110 
111 private:
113 
114 #pragma pack(push, 1)
115  struct BufferFrame
116  {
117  RemoteProtectedBlock m_blocks[RemoteNbOrginalBlocks - 1];
118  };
119 #pragma pack(pop)
120 
121  struct DecoderSlot
122  {
124  RemoteProtectedBlock m_originalBlocks[RemoteNbOrginalBlocks];
125  RemoteProtectedBlock m_recoveryBlocks[RemoteNbOrginalBlocks];
126  CM256::cm256_block m_cm256DescriptorBlocks[RemoteNbOrginalBlocks];
130  bool m_decoded;
132  };
133 
135  CM256::cm256_encoder_params m_paramsCM256;
156 
159 
161 
162  int m_nbReads;
166  CM256 m_cm256;
167  bool m_cm256_OK;
168 
169  inline RemoteProtectedBlock* storeOriginalBlock(int slotIndex, int blockIndex, const RemoteProtectedBlock& protectedBlock)
170  {
171  if (blockIndex == 0) {
172  // m_decoderSlots[slotIndex].m_originalBlocks[0] = protectedBlock;
173  // return &m_decoderSlots[slotIndex].m_originalBlocks[0];
174  m_decoderSlots[slotIndex].m_blockZero = protectedBlock;
175  return &m_decoderSlots[slotIndex].m_blockZero;
176  } else {
177  // m_decoderSlots[slotIndex].m_originalBlocks[blockIndex] = protectedBlock;
178  // return &m_decoderSlots[slotIndex].m_originalBlocks[blockIndex];
179  m_frames[slotIndex].m_blocks[blockIndex - 1] = protectedBlock;
180  return &m_frames[slotIndex].m_blocks[blockIndex - 1];
181  }
182  }
183 
184  inline RemoteProtectedBlock& getOriginalBlock(int slotIndex, int blockIndex)
185  {
186  if (blockIndex == 0) {
187  // return m_decoderSlots[slotIndex].m_originalBlocks[0];
188  return m_decoderSlots[slotIndex].m_blockZero;
189  } else {
190  // return m_decoderSlots[slotIndex].m_originalBlocks[blockIndex];
191  return m_frames[slotIndex].m_blocks[blockIndex - 1];
192  }
193  }
194 
195  inline RemoteMetaDataFEC *getMetaData(int slotIndex)
196  {
197  // return (MetaDataFEC *) &m_decoderSlots[slotIndex].m_originalBlocks[0];
198  return (RemoteMetaDataFEC *) &m_decoderSlots[slotIndex].m_blockZero;
199  }
200 
201  inline void resetOriginalBlocks(int slotIndex)
202  {
203  // memset((void *) m_decoderSlots[slotIndex].m_originalBlocks, 0, m_nbOriginalBlocks * sizeof(ProtectedBlock));
204  memset((void *) &m_decoderSlots[slotIndex].m_blockZero, 0, sizeof(RemoteProtectedBlock));
205  memset((void *) m_frames[slotIndex].m_blocks, 0, (RemoteNbOrginalBlocks - 1) * sizeof(RemoteProtectedBlock));
206  }
207 
208  void initDecodeAllSlots();
209  void initReadIndex();
210  void rwCorrectionEstimate(int slotIndex);
211  void checkSlotData(int slotIndex);
212  void initDecodeSlot(int slotIndex);
213 
214  static void printMeta(const QString& header, RemoteMetaDataFEC *metaData);
215 };
216 
217 
218 
219 #endif /* PLUGINS_SAMPLESOURCE_REMOTEINPUT_REMOTEINPUTBUFFER_H_ */
RemoteProtectedBlock m_blockZero
First block of a frame. Has meta data.
int m_readNbBytes
Nominal number of bytes per read (50ms)
int m_readIndex
current byte read index in frames buffer
RemoteMetaDataFEC m_currentMeta
Stored current meta data.
static void printMeta(const QString &header, RemoteMetaDataFEC *metaData)
int getCurNbBlocks() const
int m_minOriginalBlocks
(stats) minimum number of original blocks received since last poll
uint64_t getTVOutMSec() const
int getCurNbRecovery() const
void resetOriginalBlocks(int slotIndex)
int getCurOriginalBlocks() const
uint8_t * readData(int32_t length)
Read data from buffer.
uint32_t getTVOutUsec() const
int m_maxNbRecovery
(stats) maximum number of recovery blocks used since last poll
static const int nbDecoderSlots
int m_blockCount
number of blocks received for this frame
MovingAverageUtil< int, int, 10 > m_avgNbBlocks
(stats) average number of blocks received
RemoteProtectedBlock * storeOriginalBlock(int slotIndex, int blockIndex, const RemoteProtectedBlock &protectedBlock)
int m_balCorrection
R/W balance correction in number of samples.
CM256 m_cm256
CM256 library.
int m_curNbRecovery
(stats) instantaneous number of recovery blocks used
float getAvgNbRecovery() const
int m_curNbBlocks
(stats) instantaneous number of blocks received
uint32_t getTVOutSec() const
const RemoteMetaDataFEC & getCurrentMeta() const
unsigned int uint32_t
Definition: rtptypes_win.h:46
#define REMOTEINPUT_NBDECODERSLOTS
float getAvgNbBlocks() const
bool m_framesDecoded
[stats] true if all frames were decoded since last poll
uint8_t * m_readBuffer
Read buffer to hold samples when looping back to beginning of raw buffer.
int m_framesNbBytes
Number of bytes in samples buffer.
int m_recoveryCount
number of recovery blocks received
unsigned char uint8_t
Definition: rtptypes_win.h:42
int m_originalCount
number of original blocks received
static const int framesSize
int m_readSize
Read buffer size.
void initDecodeSlot(int slotIndex)
int32_t getRWBalanceCorrection() const
int m_nbWrites
Number of buffer writes since start of auto R/W balance correction period.
int m_balCorrLimit
Correction absolute value limit in number of samples.
float getBufferLengthInSecs() const
MovingAverageUtil< int, int, 10 > m_avgOrigBlocks
(stats) average number of original blocks received
RemoteProtectedBlock & getOriginalBlock(int slotIndex, int blockIndex)
int int32_t
Definition: rtptypes_win.h:45
float getAvgOriginalBlocks() const
CM256::cm256_encoder_params m_paramsCM256
CM256 decoder parameters block.
int m_frameHead
index of the current head frame sent
BufferFrame m_frames[nbDecoderSlots]
Samples buffer.
RemoteMetaDataFEC * getMetaData(int slotIndex)
uint32_t m_tvOut_usec
Estimated returned samples timestamp (microseconds)
RemoteProtectedBlock m_blocks[RemoteNbOrginalBlocks - 1]
uint32_t m_tvOut_sec
Estimated returned samples timestamp (seconds)
int m_nbReads
Number of buffer reads since start of auto R/W balance correction period.
void writeData(char *array)
Write data into buffer.
void rwCorrectionEstimate(int slotIndex)
int m_minNbBlocks
(stats) minimum number of blocks received since last poll
void checkSlotData(int slotIndex)
int m_decoderIndexHead
index of the current head frame slot in decoding slots
bool m_metaRetrieved
true if meta data (block zero) was retrieved
DecoderSlot m_decoderSlots[nbDecoderSlots]
CM256 decoding control/buffer slots.
bool m_cm256_OK
CM256 library initialized OK.
int32_t getBufferGauge() const
int m_curOriginalBlocks
(stats) instantanous number of original blocks received
int m_wrDeltaEstimate
Sampled estimate of write to read indexes difference.
MovingAverageUtil< int, int, 10 > m_avgNbRecovery
(stats) average number of recovery blocks used
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48