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.
Classes | Public Slots | Public Member Functions | Static Public Attributes | Private Slots | Private Member Functions | Private Attributes | List of all members
UDPSourceUDPHandler Class Reference

#include <udpsourceudphandler.h>

Inherits QObject.

+ Collaboration diagram for UDPSourceUDPHandler:

Classes

class  MsgUDPAddressAndPort
 

Public Slots

void dataReadyRead ()
 

Public Member Functions

 UDPSourceUDPHandler ()
 
virtual ~UDPSourceUDPHandler ()
 
void start ()
 
void stop ()
 
void configureUDPLink (const QString &address, quint16 port)
 
void resetReadIndex ()
 
void resizeBuffer (float sampleRate)
 
void readSample (qint16 &t)
 audio mono More...
 
void readSample (AudioSample &a)
 audio stereo More...
 
void readSample (Sample &s)
 I/Q stream. More...
 
void setAutoRWBalance (bool autoRWBalance)
 
void setFeedbackMessageQueue (MessageQueue *messageQueue)
 
int32_t getBufferGauge () const
 

Static Public Attributes

static const int m_udpBlockSize = 512
 
static const int m_minNbUDPFrames = 256
 

Private Slots

void handleMessages ()
 

Private Member Functions

typedef char (udpBlk_t)[m_udpBlockSize]
 
void moveData (char *blk)
 
void advanceReadPointer (int nbBytes)
 
void applyUDPLink (const QString &address, quint16 port)
 
bool handleMessage (const Message &message)
 

Private Attributes

QUdpSocket * m_dataSocket
 
QHostAddress m_dataAddress
 
QHostAddress m_remoteAddress
 
quint16 m_dataPort
 
quint16 m_remotePort
 
bool m_dataConnected
 
udpBlk_t * m_udpBuf
 
char m_udpDump [m_udpBlockSize+8192]
 
int m_udpDumpIndex
 
int m_nbUDPFrames
 
int m_nbAllocatedUDPFrames
 
int m_writeFrameIndex
 
int m_readFrameIndex
 
int m_readIndex
 
int m_rwDelta
 
float m_d
 
bool m_autoRWBalance
 
MessageQueuem_feedbackMessageQueue
 
MessageQueue m_inputMessageQueue
 

Detailed Description

Definition at line 31 of file udpsourceudphandler.h.

Constructor & Destructor Documentation

◆ UDPSourceUDPHandler()

UDPSourceUDPHandler::UDPSourceUDPHandler ( )

Definition at line 28 of file udpsourceudphandler.cpp.

28  :
29  m_dataSocket(0),
30  m_dataAddress(QHostAddress::LocalHost),
31  m_remoteAddress(QHostAddress::LocalHost),
32  m_dataPort(9999),
33  m_remotePort(0),
34  m_dataConnected(false),
35  m_udpDumpIndex(0),
40  m_readIndex(0),
42  m_d(0),
43  m_autoRWBalance(true),
45 {
46  m_udpBuf = new udpBlk_t[m_minNbUDPFrames];
47  std::fill(m_udpDump, m_udpDump + m_udpBlockSize + 8192, 0);
48  connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages()));
49 }
static const int m_minNbUDPFrames
char m_udpDump[m_udpBlockSize+8192]
MessageQueue m_inputMessageQueue
static const int m_udpBlockSize
MessageQueue * m_feedbackMessageQueue

◆ ~UDPSourceUDPHandler()

UDPSourceUDPHandler::~UDPSourceUDPHandler ( )
virtual

Definition at line 51 of file udpsourceudphandler.cpp.

References m_udpBuf.

52 {
53  delete[] m_udpBuf;
54 }

Member Function Documentation

◆ advanceReadPointer()

void UDPSourceUDPHandler::advanceReadPointer ( int  nbBytes)
private

Definition at line 184 of file udpsourceudphandler.cpp.

References UDPSourceMessages::MsgSampleRateCorrection::create(), m_autoRWBalance, m_d, m_feedbackMessageQueue, m_nbUDPFrames, m_readFrameIndex, m_readIndex, m_rwDelta, m_udpBlockSize, m_writeFrameIndex, MessageQueue::push(), and resetReadIndex().

Referenced by UDPSourceUDPHandler::MsgUDPAddressAndPort::MsgUDPAddressAndPort(), and readSample().

185 {
186  if (m_readIndex < m_udpBlockSize - 2*nbBytes)
187  {
188  m_readIndex += nbBytes;
189  }
190  else
191  {
192  m_readIndex = 0;
193 
195  {
197  }
198  else
199  {
200  m_rwDelta = m_writeFrameIndex; // raw R/W delta estimate
201  int nbUDPFrames2 = m_nbUDPFrames/2;
202  float d = (m_rwDelta - nbUDPFrames2)/(float) m_nbUDPFrames;
203  //qDebug("UDPSourceUDPHandler::advanceReadPointer: w: %02d d: %f", m_writeIndex, d);
204 
205  if ((d < -0.45) || (d > 0.45))
206  {
207  resetReadIndex();
208  }
209  else
210  {
211  float dd = d - m_d; // derivative
212  float c = (d / 15.0) + (dd / 20.0); // damping and scaling
213  c = c < -0.05 ? -0.05 : c > 0.05 ? 0.05 : c; // limit
215 
218  }
219 
220  m_readFrameIndex = 0;
221  m_d = d;
222  }
223  }
224  }
225 }
void push(Message *message, bool emitSignal=true)
Push message onto queue.
static MsgSampleRateCorrection * create(float correctionFactor, float rawDeltaRatio)
Definition: udpsourcemsg.h:36
static const int m_udpBlockSize
MessageQueue * m_feedbackMessageQueue
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ applyUDPLink()

void UDPSourceUDPHandler::applyUDPLink ( const QString &  address,
quint16  port 
)
private

Definition at line 233 of file udpsourceudphandler.cpp.

References m_dataAddress, m_dataPort, resetReadIndex(), start(), and stop().

Referenced by handleMessage(), and UDPSourceUDPHandler::MsgUDPAddressAndPort::MsgUDPAddressAndPort().

234 {
235  qDebug("UDPSourceUDPHandler::configureUDPLink: %s:%d", address.toStdString().c_str(), port);
236  bool addressOK = m_dataAddress.setAddress(address);
237 
238  if (!addressOK)
239  {
240  qWarning("UDPSourceUDPHandler::configureUDPLink: invalid address %s. Set to localhost.", address.toStdString().c_str());
241  m_dataAddress = QHostAddress::LocalHost;
242  }
243 
244  stop();
245  m_dataPort = port;
246  resetReadIndex();
247  start();
248 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ char()

typedef UDPSourceUDPHandler::char ( udpBlk_t  )
private

Referenced by UDPSourceUDPHandler::MsgUDPAddressAndPort::MsgUDPAddressAndPort().

+ Here is the caller graph for this function:

◆ configureUDPLink()

void UDPSourceUDPHandler::configureUDPLink ( const QString &  address,
quint16  port 
)

Definition at line 227 of file udpsourceudphandler.cpp.

References UDPSourceUDPHandler::MsgUDPAddressAndPort::create(), m_inputMessageQueue, and MessageQueue::push().

Referenced by UDPSource::applySettings().

228 {
229  Message* msg = MsgUDPAddressAndPort::create(address, port);
231 }
void push(Message *message, bool emitSignal=true)
Push message onto queue.
static MsgUDPAddressAndPort * create(QString address, quint16 port)
MessageQueue m_inputMessageQueue
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dataReadyRead

void UDPSourceUDPHandler::dataReadyRead ( )
slot

Definition at line 99 of file udpsourceudphandler.cpp.

References m_dataConnected, m_dataSocket, m_remoteAddress, m_remotePort, m_udpBlockSize, m_udpDump, m_udpDumpIndex, and moveData().

Referenced by start(), and stop().

100 {
101  while (m_dataSocket->hasPendingDatagrams() && m_dataConnected)
102  {
103  qint64 pendingDataSize = m_dataSocket->pendingDatagramSize();
104  qint64 bytesRead = m_dataSocket->readDatagram(&m_udpDump[m_udpDumpIndex], pendingDataSize, &m_remoteAddress, &m_remotePort);
105 
106  if (bytesRead < 0)
107  {
108  qWarning("UDPSourceUDPHandler::dataReadyRead: UDP read error");
109  }
110  else
111  {
112  int udpDumpSize = m_udpDumpIndex + bytesRead;
113  int udpDumpPtr = 0;
114 
115  while (udpDumpSize >= m_udpBlockSize)
116  {
117  moveData(&m_udpDump[udpDumpPtr]);
118  udpDumpPtr += m_udpBlockSize;
119  udpDumpSize -= m_udpBlockSize;
120  }
121 
122  if (udpDumpSize > 0)
123  {
124  memcpy(m_udpDump, &m_udpDump[udpDumpPtr], udpDumpSize);
125  }
126 
127  m_udpDumpIndex = udpDumpSize;
128  }
129  }
130 }
char m_udpDump[m_udpBlockSize+8192]
static const int m_udpBlockSize
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBufferGauge()

int32_t UDPSourceUDPHandler::getBufferGauge ( ) const
inline

Get buffer gauge value in % of buffer size ([-50:50]) [-50:0] : write leads or read lags [0:50] : read leads or write lags

Definition at line 55 of file udpsourceudphandler.h.

References m_nbUDPFrames, and m_rwDelta.

Referenced by UDPSource::getBufferGauge().

56  {
57  int32_t val = m_rwDelta - (m_nbUDPFrames/2);
58  return (100*val) / m_nbUDPFrames;
59  }
int int32_t
Definition: rtptypes_win.h:45
+ Here is the caller graph for this function:

◆ handleMessage()

bool UDPSourceUDPHandler::handleMessage ( const Message message)
private

Definition at line 289 of file udpsourceudphandler.cpp.

References applyUDPLink(), UDPSourceUDPHandler::MsgUDPAddressAndPort::getAddress(), UDPSourceUDPHandler::MsgUDPAddressAndPort::getPort(), and Message::match().

Referenced by handleMessages(), and UDPSourceUDPHandler::MsgUDPAddressAndPort::MsgUDPAddressAndPort().

290 {
292  {
294  applyUDPLink(notif.getAddress(), notif.getPort());
295  return true;
296  }
297  else
298  {
299  return false;
300  }
301 }
void applyUDPLink(const QString &address, quint16 port)
static bool match(const Message *message)
Definition: message.cpp:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleMessages

void UDPSourceUDPHandler::handleMessages ( )
privateslot

Definition at line 276 of file udpsourceudphandler.cpp.

References handleMessage(), m_inputMessageQueue, and MessageQueue::pop().

277 {
278  Message* message;
279 
280  while ((message = m_inputMessageQueue.pop()) != 0)
281  {
282  if (handleMessage(*message))
283  {
284  delete message;
285  }
286  }
287 }
Message * pop()
Pop message from queue.
MessageQueue m_inputMessageQueue
bool handleMessage(const Message &message)
+ Here is the call graph for this function:

◆ moveData()

void UDPSourceUDPHandler::moveData ( char blk)
private

Definition at line 132 of file udpsourceudphandler.cpp.

References m_nbUDPFrames, m_udpBlockSize, m_udpBuf, and m_writeFrameIndex.

Referenced by dataReadyRead(), and UDPSourceUDPHandler::MsgUDPAddressAndPort::MsgUDPAddressAndPort().

133 {
135 
136  if (m_writeFrameIndex < m_nbUDPFrames - 1) {
138  } else {
139  m_writeFrameIndex = 0;
140  }
141 }
static const int m_udpBlockSize
+ Here is the caller graph for this function:

◆ readSample() [1/3]

void UDPSourceUDPHandler::readSample ( qint16 &  t)

audio mono

Definition at line 143 of file udpsourceudphandler.cpp.

References advanceReadPointer(), m_readFrameIndex, m_readIndex, m_udpBuf, and m_writeFrameIndex.

Referenced by UDPSource::modulateSample(), and UDPSource::readMonoSample().

144 {
145  if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
146  {
147  t = 0;
148  }
149  else
150  {
151  memcpy(&t, &m_udpBuf[m_readFrameIndex][m_readIndex], sizeof(qint16));
152  advanceReadPointer((int) sizeof(qint16));
153  }
154 }
void advanceReadPointer(int nbBytes)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readSample() [2/3]

void UDPSourceUDPHandler::readSample ( AudioSample a)

audio stereo

Definition at line 156 of file udpsourceudphandler.cpp.

References advanceReadPointer(), AudioSample::l, m_readFrameIndex, m_readIndex, m_udpBuf, m_writeFrameIndex, and AudioSample::r.

157 {
158  if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
159  {
160  a.l = 0;
161  a.r = 0;
162  }
163  else
164  {
165  memcpy(&a, &m_udpBuf[m_readFrameIndex][m_readIndex], sizeof(AudioSample));
166  advanceReadPointer((int) sizeof(AudioSample));
167  }
168 }
void advanceReadPointer(int nbBytes)
qint16 r
Definition: dsptypes.h:92
qint16 l
Definition: dsptypes.h:91
+ Here is the call graph for this function:

◆ readSample() [3/3]

void UDPSourceUDPHandler::readSample ( Sample s)

I/Q stream.

Definition at line 170 of file udpsourceudphandler.cpp.

References advanceReadPointer(), Sample::m_imag, m_readFrameIndex, m_readIndex, Sample::m_real, m_udpBuf, and m_writeFrameIndex.

171 {
172  if (m_readFrameIndex == m_writeFrameIndex) // block until more writes
173  {
174  s.m_real = 0;
175  s.m_imag = 0;
176  }
177  else
178  {
179  memcpy(&s, &m_udpBuf[m_readFrameIndex][m_readIndex], sizeof(Sample));
180  advanceReadPointer((int) sizeof(Sample));
181  }
182 }
void advanceReadPointer(int nbBytes)
FixReal m_real
Definition: dsptypes.h:64
FixReal m_imag
Definition: dsptypes.h:65
+ Here is the call graph for this function:

◆ resetReadIndex()

void UDPSourceUDPHandler::resetReadIndex ( )

◆ resizeBuffer()

void UDPSourceUDPHandler::resizeBuffer ( float  sampleRate)

Definition at line 258 of file udpsourceudphandler.cpp.

References m_minNbUDPFrames, m_nbAllocatedUDPFrames, m_nbUDPFrames, m_udpBuf, m_writeFrameIndex, leansdr::max(), and resetReadIndex().

Referenced by UDPSource::applySettings().

259 {
260  int halfNbFrames = std::max((sampleRate / 375.0), (m_minNbUDPFrames / 2.0));
261  qDebug("UDPSourceUDPHandler::resizeBuffer: nb_frames: %d", 2*halfNbFrames);
262 
263  if (2*halfNbFrames > m_nbAllocatedUDPFrames)
264  {
265  delete[] m_udpBuf;
266  m_udpBuf = new udpBlk_t[2*halfNbFrames];
267  m_nbAllocatedUDPFrames = 2*halfNbFrames;
268  }
269 
270  m_nbUDPFrames = 2*halfNbFrames;
271  m_writeFrameIndex = 0;
272 
273  resetReadIndex();
274 }
static const int m_minNbUDPFrames
T max(const T &x, const T &y)
Definition: framework.h:446
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAutoRWBalance()

void UDPSourceUDPHandler::setAutoRWBalance ( bool  autoRWBalance)
inline

Definition at line 48 of file udpsourceudphandler.h.

References m_autoRWBalance.

Referenced by UDPSource::applySettings().

48 { m_autoRWBalance = autoRWBalance; }
+ Here is the caller graph for this function:

◆ setFeedbackMessageQueue()

void UDPSourceUDPHandler::setFeedbackMessageQueue ( MessageQueue messageQueue)
inline

Definition at line 49 of file udpsourceudphandler.h.

References m_feedbackMessageQueue.

Referenced by UDPSource::UDPSource().

49 { m_feedbackMessageQueue = messageQueue; }
MessageQueue * m_feedbackMessageQueue
+ Here is the caller graph for this function:

◆ start()

void UDPSourceUDPHandler::start ( )

Definition at line 56 of file udpsourceudphandler.cpp.

References dataReadyRead(), m_dataAddress, m_dataConnected, m_dataPort, and m_dataSocket.

Referenced by applyUDPLink(), and UDPSource::start().

57 {
58  qDebug("UDPSourceUDPHandler::start");
59 
60  if (!m_dataSocket)
61  {
62  m_dataSocket = new QUdpSocket(this);
63  }
64 
65  if (!m_dataConnected)
66  {
67 
69  {
70  qDebug("UDPSourceUDPHandler::start: bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort);
71  connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); // , Qt::QueuedConnection gets stuck since Qt 5.8.0
72  m_dataConnected = true;
73  }
74  else
75  {
76  qWarning("UDPSourceUDPHandler::start: cannot bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort);
77  m_dataConnected = false;
78  }
79  }
80 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stop()

void UDPSourceUDPHandler::stop ( )

Definition at line 82 of file udpsourceudphandler.cpp.

References dataReadyRead(), m_dataConnected, and m_dataSocket.

Referenced by applyUDPLink(), and UDPSource::stop().

83 {
84  qDebug("UDPSourceUDPHandler::stop");
85 
86  if (m_dataConnected)
87  {
88  m_dataConnected = false;
89  disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
90  }
91 
92  if (m_dataSocket)
93  {
94  delete m_dataSocket;
95  m_dataSocket = 0;
96  }
97 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_autoRWBalance

bool UDPSourceUDPHandler::m_autoRWBalance
private

Definition at line 114 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), and setAutoRWBalance().

◆ m_d

float UDPSourceUDPHandler::m_d
private

Definition at line 113 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), and resetReadIndex().

◆ m_dataAddress

QHostAddress UDPSourceUDPHandler::m_dataAddress
private

Definition at line 99 of file udpsourceudphandler.h.

Referenced by applyUDPLink(), and start().

◆ m_dataConnected

bool UDPSourceUDPHandler::m_dataConnected
private

Definition at line 103 of file udpsourceudphandler.h.

Referenced by dataReadyRead(), start(), and stop().

◆ m_dataPort

quint16 UDPSourceUDPHandler::m_dataPort
private

Definition at line 101 of file udpsourceudphandler.h.

Referenced by applyUDPLink(), and start().

◆ m_dataSocket

QUdpSocket* UDPSourceUDPHandler::m_dataSocket
private

Definition at line 98 of file udpsourceudphandler.h.

Referenced by dataReadyRead(), start(), and stop().

◆ m_feedbackMessageQueue

MessageQueue* UDPSourceUDPHandler::m_feedbackMessageQueue
private

Definition at line 115 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), and setFeedbackMessageQueue().

◆ m_inputMessageQueue

MessageQueue UDPSourceUDPHandler::m_inputMessageQueue
private

Definition at line 116 of file udpsourceudphandler.h.

Referenced by configureUDPLink(), and handleMessages().

◆ m_minNbUDPFrames

const int UDPSourceUDPHandler::m_minNbUDPFrames = 256
static

Definition at line 62 of file udpsourceudphandler.h.

Referenced by resizeBuffer().

◆ m_nbAllocatedUDPFrames

int UDPSourceUDPHandler::m_nbAllocatedUDPFrames
private

Definition at line 108 of file udpsourceudphandler.h.

Referenced by resizeBuffer().

◆ m_nbUDPFrames

int UDPSourceUDPHandler::m_nbUDPFrames
private

◆ m_readFrameIndex

int UDPSourceUDPHandler::m_readFrameIndex
private

Definition at line 110 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), readSample(), and resetReadIndex().

◆ m_readIndex

int UDPSourceUDPHandler::m_readIndex
private

Definition at line 111 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), readSample(), and resetReadIndex().

◆ m_remoteAddress

QHostAddress UDPSourceUDPHandler::m_remoteAddress
private

Definition at line 100 of file udpsourceudphandler.h.

Referenced by dataReadyRead().

◆ m_remotePort

quint16 UDPSourceUDPHandler::m_remotePort
private

Definition at line 102 of file udpsourceudphandler.h.

Referenced by dataReadyRead().

◆ m_rwDelta

int UDPSourceUDPHandler::m_rwDelta
private

Definition at line 112 of file udpsourceudphandler.h.

Referenced by advanceReadPointer(), getBufferGauge(), and resetReadIndex().

◆ m_udpBlockSize

const int UDPSourceUDPHandler::m_udpBlockSize = 512
static

◆ m_udpBuf

udpBlk_t* UDPSourceUDPHandler::m_udpBuf
private

Definition at line 104 of file udpsourceudphandler.h.

Referenced by moveData(), readSample(), resizeBuffer(), and ~UDPSourceUDPHandler().

◆ m_udpDump

char UDPSourceUDPHandler::m_udpDump[m_udpBlockSize+8192]
private

Definition at line 105 of file udpsourceudphandler.h.

Referenced by dataReadyRead().

◆ m_udpDumpIndex

int UDPSourceUDPHandler::m_udpDumpIndex
private

Definition at line 106 of file udpsourceudphandler.h.

Referenced by dataReadyRead().

◆ m_writeFrameIndex

int UDPSourceUDPHandler::m_writeFrameIndex
private

The documentation for this class was generated from the following files: