2015-12-12 17:01:46 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2015 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// 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 INCLUDE_UTIL_UDPSINK_H_
|
|
|
|
#define INCLUDE_UTIL_UDPSINK_H_
|
|
|
|
|
2017-05-26 04:35:29 -04:00
|
|
|
#include <stdint.h>
|
2015-12-12 17:01:46 -05:00
|
|
|
#include <QObject>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QHostAddress>
|
|
|
|
|
2016-04-03 05:29:11 -04:00
|
|
|
#include <cassert>
|
|
|
|
|
2015-12-12 17:01:46 -05:00
|
|
|
template<typename T>
|
|
|
|
class UDPSink
|
|
|
|
{
|
|
|
|
public:
|
2018-01-28 19:59:03 -05:00
|
|
|
UDPSink(QObject *parent, unsigned int udpSize) :
|
2015-12-12 17:01:46 -05:00
|
|
|
m_udpSize(udpSize),
|
2017-08-15 06:29:54 -04:00
|
|
|
m_udpSamples(udpSize/sizeof(T)),
|
2015-12-12 17:01:46 -05:00
|
|
|
m_address(QHostAddress::LocalHost),
|
2018-01-28 19:59:03 -05:00
|
|
|
m_port(9999),
|
2015-12-12 17:01:46 -05:00
|
|
|
m_sampleBufferIndex(0)
|
|
|
|
{
|
2017-08-15 06:29:54 -04:00
|
|
|
assert(m_udpSamples > 0);
|
|
|
|
m_sampleBuffer = new T[m_udpSamples];
|
2015-12-12 17:01:46 -05:00
|
|
|
m_socket = new QUdpSocket(parent);
|
|
|
|
}
|
|
|
|
|
2018-01-28 19:59:03 -05:00
|
|
|
UDPSink(QObject *parent, unsigned int udpSize, unsigned int port) :
|
|
|
|
m_udpSize(udpSize),
|
|
|
|
m_udpSamples(udpSize/sizeof(T)),
|
|
|
|
m_address(QHostAddress::LocalHost),
|
|
|
|
m_port(port),
|
|
|
|
m_sampleBufferIndex(0)
|
|
|
|
{
|
|
|
|
assert(m_udpSamples > 0);
|
|
|
|
m_sampleBuffer = new T[m_udpSamples];
|
|
|
|
m_socket = new QUdpSocket(parent);
|
|
|
|
}
|
|
|
|
|
2015-12-12 17:01:46 -05:00
|
|
|
UDPSink (QObject *parent, unsigned int udpSize, QHostAddress& address, unsigned int port) :
|
|
|
|
m_udpSize(udpSize),
|
2017-08-15 06:29:54 -04:00
|
|
|
m_udpSamples(udpSize/sizeof(T)),
|
2015-12-12 17:01:46 -05:00
|
|
|
m_address(address),
|
|
|
|
m_port(port),
|
|
|
|
m_sampleBufferIndex(0)
|
|
|
|
{
|
2017-08-15 06:29:54 -04:00
|
|
|
assert(m_udpSamples > 0);
|
|
|
|
m_sampleBuffer = new T[m_udpSamples];
|
2015-12-12 17:01:46 -05:00
|
|
|
m_socket = new QUdpSocket(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
~UDPSink()
|
|
|
|
{
|
|
|
|
delete[] m_sampleBuffer;
|
|
|
|
delete m_socket;
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:23:47 -05:00
|
|
|
void moveToThread(QThread *thread)
|
|
|
|
{
|
|
|
|
m_socket->moveToThread(thread);
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:29:11 -04:00
|
|
|
void setAddress(QString& address) { m_address.setAddress(address); }
|
|
|
|
void setPort(unsigned int port) { m_port = port; }
|
|
|
|
|
2018-01-28 19:59:03 -05:00
|
|
|
void setDestination(const QString& address, int port)
|
|
|
|
{
|
|
|
|
m_address.setAddress(const_cast<QString&>(address));
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
|
2017-08-24 05:49:47 -04:00
|
|
|
/**
|
|
|
|
* Write one sample
|
|
|
|
*/
|
2015-12-12 17:01:46 -05:00
|
|
|
void write(T sample)
|
|
|
|
{
|
2017-08-15 06:29:54 -04:00
|
|
|
if (m_sampleBufferIndex < m_udpSamples)
|
2015-12-12 17:01:46 -05:00
|
|
|
{
|
2016-04-03 05:29:11 -04:00
|
|
|
m_sampleBuffer[m_sampleBufferIndex] = sample;
|
2015-12-12 17:01:46 -05:00
|
|
|
m_sampleBufferIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-15 06:29:54 -04:00
|
|
|
m_socket->writeDatagram((const char*)&m_sampleBuffer[0], (qint64 ) m_udpSize, m_address, m_port);
|
2016-04-03 05:29:11 -04:00
|
|
|
m_sampleBuffer[0] = sample;
|
|
|
|
m_sampleBufferIndex = 1;
|
2015-12-12 17:01:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 05:49:47 -04:00
|
|
|
/**
|
|
|
|
* Write a bunch of samples
|
|
|
|
*/
|
|
|
|
void write(T *samples, int nbSamples)
|
|
|
|
{
|
|
|
|
int samplesIndex = 0;
|
|
|
|
|
|
|
|
if (m_sampleBufferIndex + nbSamples > m_udpSamples) // fill remainder of buffer and send it
|
|
|
|
{
|
|
|
|
memcpy(&m_sampleBuffer[m_sampleBufferIndex], &samples[samplesIndex], (m_udpSamples - m_sampleBufferIndex)*sizeof(T)); // fill remainder of buffer
|
|
|
|
m_socket->writeDatagram((const char*)&m_sampleBuffer[0], (qint64 ) m_udpSize, m_address, m_port); // send buffer
|
|
|
|
samplesIndex += (m_udpSamples - m_sampleBufferIndex);
|
|
|
|
nbSamples -= (m_udpSamples - m_sampleBufferIndex);
|
|
|
|
m_sampleBufferIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (nbSamples > m_udpSamples) // send directly from input without buffering
|
|
|
|
{
|
|
|
|
m_socket->writeDatagram((const char*)&samples[samplesIndex], (qint64 ) m_udpSize, m_address, m_port);
|
|
|
|
samplesIndex += m_udpSamples;
|
|
|
|
nbSamples -= m_udpSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&m_sampleBuffer[m_sampleBufferIndex], &samples[samplesIndex], nbSamples*sizeof(T)); // copy remainder of input to buffer
|
|
|
|
}
|
|
|
|
|
2015-12-12 17:01:46 -05:00
|
|
|
private:
|
2017-08-24 05:49:47 -04:00
|
|
|
int m_udpSize;
|
|
|
|
int m_udpSamples;
|
2015-12-12 17:01:46 -05:00
|
|
|
QHostAddress m_address;
|
|
|
|
unsigned int m_port;
|
|
|
|
QUdpSocket *m_socket;
|
|
|
|
T *m_sampleBuffer;;
|
2017-08-24 05:49:47 -04:00
|
|
|
int m_sampleBufferIndex;
|
2015-12-12 17:01:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* INCLUDE_UTIL_UDPSINK_H_ */
|