From 9ed6b40ba5e7aec0f193374339ef45adfea4521e Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 2 Dec 2015 02:28:31 +0100 Subject: [PATCH] UDP Source: compiles --- plugins/channel/udpsrc/udpsrc.cpp | 179 +++++--------------- plugins/channel/udpsrc/udpsrc.h | 66 ++------ plugins/channel/udpsrc/udpsrcgui.cpp | 55 +------ plugins/channel/udpsrc/udpsrcgui.h | 5 +- plugins/channel/udpsrc/udpsrcgui.ui | 236 +++++++++++++++------------ 5 files changed, 189 insertions(+), 352 deletions(-) diff --git a/plugins/channel/udpsrc/udpsrc.cpp b/plugins/channel/udpsrc/udpsrc.cpp index ab49daaaa..008cdb2d6 100644 --- a/plugins/channel/udpsrc/udpsrc.cpp +++ b/plugins/channel/udpsrc/udpsrc.cpp @@ -16,14 +16,12 @@ #include "udpsrc.h" -#include -#include +#include #include #include "dsp/channelizer.h" #include "udpsrcgui.h" MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcConfigure, Message) -MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcConnection, Message) MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcSpectrum, Message) UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, SampleSink* spectrum) : @@ -31,6 +29,8 @@ UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, SampleSink* s { setObjectName("UDPSrc"); + m_socket = new QUdpSocket(this); + m_inputSampleRate = 96000; m_sampleFormat = FormatSSB; m_outputSampleRate = 48000; @@ -58,12 +58,13 @@ UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, SampleSink* s UDPSrc::~UDPSrc() { + delete m_socket; if (UDPFilter) delete UDPFilter; } -void UDPSrc::configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int udpPort, int boost) +void UDPSrc::configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, QString& udpAddress, int udpPort, int boost) { - Message* cmd = MsgUDPSrcConfigure::create(sampleFormat, outputSampleRate, rfBandwidth, udpPort, boost); + Message* cmd = MsgUDPSrcConfigure::create(sampleFormat, outputSampleRate, rfBandwidth, udpAddress, udpPort, boost); messageQueue->push(cmd); } @@ -105,39 +106,43 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector: m_spectrum->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), positiveOnly); } - for(int i = 0; i < m_s16leSockets.count(); i++) + if (m_sampleFormat == FormatSSB) { - m_s16leSockets[i].socket->write((const char*)&m_sampleBuffer[0], m_sampleBuffer.size() * 4); - } - - if((m_sampleFormat == FormatSSB) && (m_ssbSockets.count() > 0)) { - for(SampleVector::const_iterator it = m_sampleBuffer.begin(); it != m_sampleBuffer.end(); ++it) { + for(SampleVector::const_iterator it = m_sampleBuffer.begin(); it != m_sampleBuffer.end(); ++it) + { //Complex cj(it->real() / 30000.0, it->imag() / 30000.0); Complex cj(it->real(), it->imag()); int n_out = UDPFilter->runSSB(cj, &sideband, true); - if (n_out) { - for (int i = 0; i < n_out; i+=2) { + + if (n_out) + { + for (int i = 0; i < n_out; i+=2) + { //l = (sideband[i].real() + sideband[i].imag()) * 0.7 * 32000.0; //r = (sideband[i+1].real() + sideband[i+1].imag()) * 0.7 * 32000.0; l = (sideband[i].real() + sideband[i].imag()) * 0.7; r = (sideband[i+1].real() + sideband[i+1].imag()) * 0.7; m_sampleBufferSSB.push_back(Sample(l, r)); } - for(int i = 0; i < m_ssbSockets.count(); i++) - m_ssbSockets[i].socket->write((const char*)&m_sampleBufferSSB[0], n_out * 2); + + m_socket->writeDatagram((const char*)&m_sampleBufferSSB[0], (qint64 ) (n_out * 2), m_udpAddress, m_udpPort); m_sampleBufferSSB.clear(); } } } - - if((m_sampleFormat == FormatNFM) && (m_ssbSockets.count() > 0)) { - for(SampleVector::const_iterator it = m_sampleBuffer.begin(); it != m_sampleBuffer.end(); ++it) { + else if (m_sampleFormat == FormatNFM) + { + for(SampleVector::const_iterator it = m_sampleBuffer.begin(); it != m_sampleBuffer.end(); ++it) + { Complex cj(it->real() / 32768.0f, it->imag() / 32768.0f); // An FFT filter here is overkill, but was already set up for SSB int n_out = UDPFilter->runFilt(cj, &sideband); - if (n_out) { + + if (n_out) + { Real sum = 1.0; - for (int i = 0; i < n_out; i+=2) { + for (int i = 0; i < n_out; i+=2) + { l = m_this.real() * (m_last.imag() - sideband[i].imag()) - m_this.imag() * (m_last.real() - sideband[i].real()); m_last = sideband[i]; @@ -149,32 +154,25 @@ void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector: } // TODO: correct levels m_scale = 24000 * udpFftLen / sum; - for(int i = 0; i < m_ssbSockets.count(); i++) - m_ssbSockets[i].socket->write((const char*)&m_sampleBufferSSB[0], n_out * 2); + m_socket->writeDatagram((const char*)&m_sampleBufferSSB[0], (qint64 ) (n_out * 2), m_udpAddress, m_udpPort); m_sampleBufferSSB.clear(); } } } + else + { + m_socket->writeDatagram((const char*)&m_sampleBuffer[0], (qint64 ) (m_sampleBuffer.size() * 4), m_udpAddress, m_udpPort); + } m_settingsMutex.unlock(); } void UDPSrc::start() { - m_udpServer = new QTcpServer(); - connect(m_udpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); - connect(m_udpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onUdpServerError(QAbstractSocket::SocketError))); - m_udpServer->listen(QHostAddress::Any, m_udpPort); } void UDPSrc::stop() { - closeAllSockets(&m_ssbSockets); - closeAllSockets(&m_s16leSockets); - - if(m_udpServer->isListening()) - m_udpServer->close(); - delete m_udpServer; } bool UDPSrc::handleMessage(const Message& cmd) @@ -209,16 +207,14 @@ bool UDPSrc::handleMessage(const Message& cmd) m_outputSampleRate = cfg.getOutputSampleRate(); m_rfBandwidth = cfg.getRFBandwidth(); + if (cfg.getUDPAddress() != m_udpAddress.toString()) + { + m_udpAddress.setAddress(cfg.getUDPAddress()); + } + if (cfg.getUDPPort() != m_udpPort) { m_udpPort = cfg.getUDPPort(); - - if(m_udpServer->isListening()) - { - m_udpServer->close(); - } - - m_udpServer->listen(QHostAddress::Any, m_udpPort); } m_boost = cfg.getBoost(); @@ -239,7 +235,9 @@ bool UDPSrc::handleMessage(const Message& cmd) qDebug() << " - MsgUDPSrcConfigure: m_sampleFormat: " << m_sampleFormat << " m_outputSampleRate: " << m_outputSampleRate << " m_rfBandwidth: " << m_rfBandwidth - << " m_boost: " << m_boost; + << " m_boost: " << m_boost + << " m_udpAddress: " << cfg.getUDPAddress() + << " m_udpPort: " << m_udpPort; return true; } @@ -265,104 +263,3 @@ bool UDPSrc::handleMessage(const Message& cmd) } } } - -void UDPSrc::closeAllSockets(Sockets* sockets) -{ - for(int i = 0; i < sockets->count(); ++i) - { - MsgUDPSrcConnection* msg = MsgUDPSrcConnection::create(false, sockets->at(i).id, QHostAddress(), 0); - m_uiMessageQueue->push(msg); - sockets->at(i).socket->close(); - } -} - -void UDPSrc::onNewConnection() -{ - qDebug("UDPSrc::onNewConnection"); - - while(m_udpServer->hasPendingConnections()) - { - qDebug("UDPSrc::onNewConnection: has a pending connection"); - QTcpSocket* connection = m_udpServer->nextPendingConnection(); - connection->setSocketOption(QAbstractSocket:: KeepAliveOption, 1); - connect(connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); - - switch(m_sampleFormat) { - - case FormatNFM: - case FormatSSB: - { - quint32 id = (FormatSSB << 24) | m_nextSSBId; - MsgUDPSrcConnection* msg = MsgUDPSrcConnection::create(true, id, connection->peerAddress(), connection->peerPort()); - m_nextSSBId = (m_nextSSBId + 1) & 0xffffff; - m_ssbSockets.push_back(Socket(id, connection)); - m_uiMessageQueue->push(msg); - break; - } - - case FormatS16LE: - { - qDebug("UDPSrc::onNewConnection: establish new S16LE connection"); - quint32 id = (FormatS16LE << 24) | m_nextS16leId; - MsgUDPSrcConnection* msg = MsgUDPSrcConnection::create(true, id, connection->peerAddress(), connection->peerPort()); - m_nextS16leId = (m_nextS16leId + 1) & 0xffffff; - m_s16leSockets.push_back(Socket(id, connection)); - m_uiMessageQueue->push(msg); - break; - } - - default: - delete connection; - break; - } - } -} - -void UDPSrc::onDisconnected() -{ - quint32 id; - QTcpSocket* socket = 0; - - qDebug("UDPSrc::onDisconnected"); - - for(int i = 0; i < m_ssbSockets.count(); i++) - { - if(m_ssbSockets[i].socket == sender()) - { - id = m_ssbSockets[i].id; - socket = m_ssbSockets[i].socket; - socket->close(); - m_ssbSockets.removeAt(i); - break; - } - } - - if(socket == 0) - { - for(int i = 0; i < m_s16leSockets.count(); i++) - { - if(m_s16leSockets[i].socket == sender()) - { - qDebug("UDPSrc::onDisconnected: remove S16LE socket #%d", i); - - id = m_s16leSockets[i].id; - socket = m_s16leSockets[i].socket; - socket->close(); - m_s16leSockets.removeAt(i); - break; - } - } - } - - if(socket != 0) - { - MsgUDPSrcConnection* msg = MsgUDPSrcConnection::create(false, id, QHostAddress(), 0); - m_uiMessageQueue->push(msg); - socket->deleteLater(); - } -} - -void UDPSrc::onUdpServerError(QAbstractSocket::SocketError socketError) -{ - qDebug("UDPSrc::onUdpServerError: %s", qPrintable(m_udpServer->errorString())); -} diff --git a/plugins/channel/udpsrc/udpsrc.h b/plugins/channel/udpsrc/udpsrc.h index 29f2c6b0e..ddb1e9e57 100644 --- a/plugins/channel/udpsrc/udpsrc.h +++ b/plugins/channel/udpsrc/udpsrc.h @@ -11,8 +11,7 @@ #define udpFftLen 2048 -class QTcpServer; -class QTcpSocket; +class QUdpSocket; class UDPSrcGUI; class UDPSrc : public SampleSink { @@ -29,7 +28,7 @@ public: UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, SampleSink* spectrum); virtual ~UDPSrc(); - void configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int udpPort, int boost); + void configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, QString& udpAddress, int udpPort, int boost); void setSpectrum(MessageQueue* messageQueue, bool enabled); Real getMagSq() const { return m_magsq; } @@ -38,35 +37,6 @@ public: virtual void stop(); virtual bool handleMessage(const Message& cmd); - class MsgUDPSrcConnection : public Message { - MESSAGE_CLASS_DECLARATION - - public: - bool getConnect() const { return m_connect; } - quint32 getID() const { return m_id; } - const QHostAddress& getPeerAddress() const { return m_peerAddress; } - int getPeerPort() const { return m_peerPort; } - - static MsgUDPSrcConnection* create(bool connect, quint32 id, const QHostAddress& peerAddress, int peerPort) - { - return new MsgUDPSrcConnection(connect, id, peerAddress, peerPort); - } - - private: - bool m_connect; - quint32 m_id; - QHostAddress m_peerAddress; - int m_peerPort; - - MsgUDPSrcConnection(bool connect, quint32 id, const QHostAddress& peerAddress, int peerPort) : - Message(), - m_connect(connect), - m_id(id), - m_peerAddress(peerAddress), - m_peerPort(peerPort) - { } - }; - protected: class MsgUDPSrcConfigure : public Message { MESSAGE_CLASS_DECLARATION @@ -75,26 +45,29 @@ protected: SampleFormat getSampleFormat() const { return m_sampleFormat; } Real getOutputSampleRate() const { return m_outputSampleRate; } Real getRFBandwidth() const { return m_rfBandwidth; } + const QString& getUDPAddress() const { return m_udpAddress; } int getUDPPort() const { return m_udpPort; } int getBoost() const { return m_boost; } - static MsgUDPSrcConfigure* create(SampleFormat sampleFormat, Real sampleRate, Real rfBandwidth, int udpPort, int boost) + static MsgUDPSrcConfigure* create(SampleFormat sampleFormat, Real sampleRate, Real rfBandwidth, QString& udpAddress, int udpPort, int boost) { - return new MsgUDPSrcConfigure(sampleFormat, sampleRate, rfBandwidth, udpPort, boost); + return new MsgUDPSrcConfigure(sampleFormat, sampleRate, rfBandwidth, udpAddress, udpPort, boost); } private: SampleFormat m_sampleFormat; Real m_outputSampleRate; Real m_rfBandwidth; + QString m_udpAddress; int m_udpPort; int m_boost; - MsgUDPSrcConfigure(SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int udpPort, int boost) : + MsgUDPSrcConfigure(SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, QString& udpAddress, int udpPort, int boost) : Message(), m_sampleFormat(sampleFormat), m_outputSampleRate(outputSampleRate), m_rfBandwidth(rfBandwidth), + m_udpAddress(udpAddress), m_udpPort(udpPort), m_boost(boost) { } @@ -121,13 +94,15 @@ protected: MessageQueue* m_uiMessageQueue; UDPSrcGUI* m_udpSrcGUI; + QUdpSocket *m_socket; int m_inputSampleRate; int m_sampleFormat; Real m_outputSampleRate; Real m_rfBandwidth; - int m_udpPort; + QHostAddress m_udpAddress; + quint16 m_udpPort; int m_boost; Real m_magsq; @@ -144,29 +119,10 @@ protected: SampleSink* m_spectrum; bool m_spectrumEnabled; - QTcpServer* m_udpServer; - struct Socket { - quint32 id; - QTcpSocket* socket; - Socket(quint32 _id, QTcpSocket* _socket) : - id(_id), - socket(_socket) - { } - }; - typedef QList Sockets; - Sockets m_ssbSockets; - Sockets m_s16leSockets; quint32 m_nextSSBId; quint32 m_nextS16leId; QMutex m_settingsMutex; - - void closeAllSockets(Sockets* sockets); - -protected slots: - void onNewConnection(); - void onDisconnected(); - void onUdpServerError(QAbstractSocket::SocketError socketError); }; #endif // INCLUDE_UDPSRC_H diff --git a/plugins/channel/udpsrc/udpsrcgui.cpp b/plugins/channel/udpsrc/udpsrcgui.cpp index c28590fac..b84e9d7f7 100644 --- a/plugins/channel/udpsrc/udpsrcgui.cpp +++ b/plugins/channel/udpsrc/udpsrcgui.cpp @@ -51,6 +51,7 @@ void UDPSrcGUI::resetToDefaults() ui->sampleFormat->setCurrentIndex(0); ui->sampleRate->setText("48000"); ui->rfBandwidth->setText("32000"); + ui->udpAddress->setText("127.0.0.1"); ui->udpPort->setText("9999"); ui->spectrumGUI->resetToDefaults(); ui->boost->setValue(1); @@ -71,6 +72,7 @@ QByteArray UDPSrcGUI::serialize() const s.writeBlob(7, ui->spectrumGUI->serialize()); s.writeS32(8, (qint32)m_boost); s.writeS32(9, m_channelMarker.getCenterFrequency()); + s.writeString(10, m_udpAddress); return s.final(); } @@ -87,6 +89,7 @@ bool UDPSrcGUI::deserialize(const QByteArray& data) if (d.getVersion() == 1) { QByteArray bytetmp; + QString strtmp; qint32 s32tmp; Real realtmp; @@ -124,6 +127,8 @@ bool UDPSrcGUI::deserialize(const QByteArray& data) ui->boost->setValue(s32tmp); d.readS32(9, &s32tmp, 0); m_channelMarker.setCenterFrequency(s32tmp); + d.readString(10, &strtmp, "127.0.0.1"); + ui->udpAddress->setText(strtmp); blockApplySettings(false); m_channelMarker.blockSignals(false); @@ -141,31 +146,7 @@ bool UDPSrcGUI::deserialize(const QByteArray& data) bool UDPSrcGUI::handleMessage(const Message& message) { qDebug() << "UDPSrcGUI::handleMessage"; - - if (UDPSrc::MsgUDPSrcConnection::match(message)) - { - UDPSrc::MsgUDPSrcConnection& con = (UDPSrc::MsgUDPSrcConnection&) message; - - if(con.getConnect()) - { - addConnection(con.getID(), con.getPeerAddress(), con.getPeerPort()); - } - else - { - delConnection(con.getID()); - } - - qDebug() << "UDPSrcGUI::handleMessage: UDPSrc::MsgUDPSrcConnection: " << con.getConnect() - << " ID: " << con.getID() - << " peerAddress: " << con.getPeerAddress() - << " peerPort: " << con.getPeerPort(); - - return true; - } - else - { - return false; - } + return false; } void UDPSrcGUI::channelMarkerChanged() @@ -264,6 +245,7 @@ void UDPSrcGUI::applySettings() rfBandwidth = outputSampleRate; } + m_udpAddress = ui->udpAddress->text(); int udpPort = ui->udpPort->text().toInt(&ok); if((!ok) || (udpPort < 1) || (udpPort > 65535)) @@ -278,6 +260,7 @@ void UDPSrcGUI::applySettings() ui->deltaMinus->setChecked(m_channelMarker.getCenterFrequency() < 0); ui->sampleRate->setText(QString("%1").arg(outputSampleRate, 0)); ui->rfBandwidth->setText(QString("%1").arg(rfBandwidth, 0)); + //ui->udpAddress->setText(m_udpAddress); ui->udpPort->setText(QString("%1").arg(udpPort)); ui->boost->setValue(boost); m_channelMarker.disconnect(this, SLOT(channelMarkerChanged())); @@ -317,6 +300,7 @@ void UDPSrcGUI::applySettings() sampleFormat, outputSampleRate, rfBandwidth, + m_udpAddress, udpPort, boost); @@ -393,24 +377,3 @@ void UDPSrcGUI::onMenuDoubleClicked() bcsw->show(); } } - -void UDPSrcGUI::addConnection(quint32 id, const QHostAddress& peerAddress, int peerPort) -{ - QStringList l; - l.append(QString("%1:%2").arg(peerAddress.toString()).arg(peerPort)); - new QTreeWidgetItem(ui->connections, l, id); - ui->connectedClientsBox->setWindowTitle(tr("Connected Clients (%1)").arg(ui->connections->topLevelItemCount())); -} - -void UDPSrcGUI::delConnection(quint32 id) -{ - for(int i = 0; i < ui->connections->topLevelItemCount(); i++) - { - if(ui->connections->topLevelItem(i)->type() == (int)id) - { - delete ui->connections->topLevelItem(i); - ui->connectedClientsBox->setWindowTitle(tr("Connected Clients (%1)").arg(ui->connections->topLevelItemCount())); - return; - } - } -} diff --git a/plugins/channel/udpsrc/udpsrcgui.h b/plugins/channel/udpsrc/udpsrcgui.h index 9619f27e4..0f9a1c586 100644 --- a/plugins/channel/udpsrc/udpsrcgui.h +++ b/plugins/channel/udpsrc/udpsrcgui.h @@ -44,6 +44,7 @@ private slots: void on_sampleFormat_currentIndexChanged(int index); void on_sampleRate_textEdited(const QString& arg1); void on_rfBandwidth_textEdited(const QString& arg1); + void on_udpAddress_textEdited(const QString& arg1); void on_udpPort_textEdited(const QString& arg1); void on_applyBtn_clicked(); void onWidgetRolled(QWidget* widget, bool rollDown); @@ -63,6 +64,7 @@ private: Real m_outputSampleRate; Real m_rfBandwidth; int m_boost; + QString m_udpAddress; int m_udpPort; bool m_basicSettingsShown; bool m_doApplySettings; @@ -77,9 +79,6 @@ private: void blockApplySettings(bool block); void applySettings(); - - void addConnection(quint32 id, const QHostAddress& peerAddress, int peerPort); - void delConnection(quint32 id); }; #endif // INCLUDE_UDPSRCGUI_H diff --git a/plugins/channel/udpsrc/udpsrcgui.ui b/plugins/channel/udpsrc/udpsrcgui.ui index 3c4fde88e..5185da66d 100644 --- a/plugins/channel/udpsrc/udpsrcgui.ui +++ b/plugins/channel/udpsrc/udpsrcgui.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 443 + 294 + 434 @@ -18,7 +18,7 @@ 10 5 - 201 + 241 142 @@ -44,83 +44,84 @@ - Sample Format + Format - - - - 2 - + + - - S16LE SSB - + + + Qt::Horizontal + + + + 40 + 20 + + + - - S16LE NFM - + + + Channel power + + + Qt::LeftToRight + + + 0.0 + + - - S16LE I/Q - + + + dB + + - - - - - - 32000 - - - - - - - RF Bandwidth (Hz) - - + - Samplerate (Hz) + Rate (Hz) - - - - UDP Port - - - - - - - 48000 - - - - - - - 9999 - - - - - - - false - - - Apply - - + + + + + + Boost + + + + + + + 3 + + + 1 + + + Qt::Horizontal + + + + + + + 0 + + + + @@ -252,69 +253,90 @@ - - + + - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Channel power - - - Qt::LeftToRight - + - 0.0 + Port - + - dB + 9999 - + + + 32000 + + + + + + + false + + + Apply + + + + + + + RF BW (Hz) + + + + + + + 48000 + + + + + + + 2 + - + + S16LE SSB + + + + + S16LE NFM + + + + + S16LE I/Q + + + + + + + + - Boost + Addr - - - 3 - - - 1 - - - Qt::Horizontal - - - - - + - 0 + 127.0.0.1