diff --git a/plugins/channel/CMakeLists.txt b/plugins/channel/CMakeLists.txt index 489d11b4d..00011db34 100644 --- a/plugins/channel/CMakeLists.txt +++ b/plugins/channel/CMakeLists.txt @@ -5,6 +5,7 @@ add_subdirectory(am) add_subdirectory(nfm) add_subdirectory(ssb) add_subdirectory(tcpsrc) +add_subdirectory(udpsrc) add_subdirectory(wfm) add_subdirectory(chanalyzer) diff --git a/plugins/channel/udpsrc/CMakeLists.txt b/plugins/channel/udpsrc/CMakeLists.txt new file mode 100644 index 000000000..c5a79b75f --- /dev/null +++ b/plugins/channel/udpsrc/CMakeLists.txt @@ -0,0 +1,47 @@ +project(udpsrc) + +set(udpsrc_SOURCES + udpsrc.cpp + udpsrcgui.cpp + udpsrcplugin.cpp +) + +set(udpsrc_HEADERS + udpsrc.h + udpsrcgui.h + udpsrcplugin.h +) + +set(udpsrc_FORMS + udpsrcgui.ui +) + +include_directories( + . + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/include-gpl + ${OPENGL_INCLUDE_DIR} +) + +#include(${QT_USE_FILE}) +add_definitions(${QT_DEFINITIONS}) +add_definitions(-DQT_PLUGIN) +add_definitions(-DQT_SHARED) + +#qt5_wrap_cpp(udpsrc_HEADERS_MOC ${udpsrc_HEADERS}) +qt5_wrap_ui(udpsrc_FORMS_HEADERS ${udpsrc_FORMS}) + +add_library(demodudpsrc SHARED + ${udpsrc_SOURCES} + ${udpsrc_HEADERS_MOC} + ${udpsrc_FORMS_HEADERS} +) + +target_link_libraries(demodudpsrc + ${QT_LIBRARIES} + ${OPENGL_LIBRARIES} + sdrbase +) + +qt5_use_modules(demodudpsrc Core Widgets OpenGL Network) diff --git a/plugins/channel/udpsrc/udpsrc.cpp b/plugins/channel/udpsrc/udpsrc.cpp new file mode 100644 index 000000000..14d563b56 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrc.cpp @@ -0,0 +1,368 @@ +// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // +// (C) 2015 John Greb // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "udpsrc.h" + +#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* tcpSrcGUI, SampleSink* spectrum) : + m_settingsMutex(QMutex::Recursive) +{ + setObjectName("TCPSrc"); + + m_inputSampleRate = 96000; + m_sampleFormat = FormatSSB; + m_outputSampleRate = 48000; + m_rfBandwidth = 32000; + m_tcpPort = 9999; + m_nco.setFreq(0, m_inputSampleRate); + m_interpolator.create(16, m_inputSampleRate, m_rfBandwidth / 2.0); + m_sampleDistanceRemain = m_inputSampleRate / m_outputSampleRate; + m_uiMessageQueue = uiMessageQueue; + m_tcpSrcGUI = tcpSrcGUI; + m_spectrum = spectrum; + m_spectrumEnabled = false; + m_nextSSBId = 0; + m_nextS16leId = 0; + + m_last = 0; + m_this = 0; + m_scale = 0; + m_boost = 0; + m_magsq = 0; + m_sampleBufferSSB.resize(tcpFftLen); + TCPFilter = new fftfilt(0.3 / 48.0, 16.0 / 48.0, tcpFftLen); + // if (!TCPFilter) segfault; +} + +UDPSrc::~UDPSrc() +{ + if (TCPFilter) delete TCPFilter; +} + +void UDPSrc::configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int tcpPort, int boost) +{ + Message* cmd = MsgUDPSrcConfigure::create(sampleFormat, outputSampleRate, rfBandwidth, tcpPort, boost); + messageQueue->push(cmd); +} + +void UDPSrc::setSpectrum(MessageQueue* messageQueue, bool enabled) +{ + Message* cmd = MsgUDPSrcSpectrum::create(enabled); + messageQueue->push(cmd); +} + +void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) +{ + Complex ci; + fftfilt::cmplx* sideband; + Real l, r; + + m_sampleBuffer.clear(); + + m_settingsMutex.lock(); + + // Rtl-Sdr uses full 16-bit scale; FCDPP does not + //int rescale = 32768 * (1 << m_boost); + int rescale = (1 << m_boost); + + for(SampleVector::const_iterator it = begin; it < end; ++it) { + //Complex c(it->real() / 32768.0f, it->imag() / 32768.0f); + Complex c(it->real(), it->imag()); + c *= m_nco.nextIQ(); + + if(m_interpolator.interpolate(&m_sampleDistanceRemain, c, &ci)) + { + m_magsq = ((ci.real()*ci.real() + ci.imag()*ci.imag())*rescale*rescale) / (1<<30); + m_sampleBuffer.push_back(Sample(ci.real() * rescale, ci.imag() * rescale)); + m_sampleDistanceRemain += m_inputSampleRate / m_outputSampleRate; + } + } + + if((m_spectrum != 0) && (m_spectrumEnabled)) + { + m_spectrum->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), positiveOnly); + } + + for(int i = 0; i < m_s16leSockets.count(); i++) + { + 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) { + //Complex cj(it->real() / 30000.0, it->imag() / 30000.0); + Complex cj(it->real(), it->imag()); + int n_out = TCPFilter->runSSB(cj, &sideband, true); + 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_sampleBufferSSB.clear(); + } + } + } + + if((m_sampleFormat == FormatNFM) && (m_ssbSockets.count() > 0)) { + 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 = TCPFilter->runFilt(cj, &sideband); + if (n_out) { + Real sum = 1.0; + 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]; + r = m_last.real() * (m_this.imag() - sideband[i+1].imag()) + - m_last.imag() * (m_this.real() - sideband[i+1].real()); + m_this = sideband[i+1]; + m_sampleBufferSSB.push_back(Sample(l * m_scale, r * m_scale)); + sum += m_this.real() * m_this.real() + m_this.imag() * m_this.imag(); + } + // TODO: correct levels + m_scale = 24000 * tcpFftLen / sum; + for(int i = 0; i < m_ssbSockets.count(); i++) + m_ssbSockets[i].socket->write((const char*)&m_sampleBufferSSB[0], n_out * 2); + m_sampleBufferSSB.clear(); + } + } + } + + m_settingsMutex.unlock(); +} + +void UDPSrc::start() +{ + m_tcpServer = new QTcpServer(); + connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); + connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(onTcpServerError(QAbstractSocket::SocketError))); + m_tcpServer->listen(QHostAddress::Any, m_tcpPort); +} + +void UDPSrc::stop() +{ + closeAllSockets(&m_ssbSockets); + closeAllSockets(&m_s16leSockets); + + if(m_tcpServer->isListening()) + m_tcpServer->close(); + delete m_tcpServer; +} + +bool UDPSrc::handleMessage(const Message& cmd) +{ + qDebug() << "UDPSrc::handleMessage"; + + if (Channelizer::MsgChannelizerNotification::match(cmd)) + { + Channelizer::MsgChannelizerNotification& notif = (Channelizer::MsgChannelizerNotification&) cmd; + + m_settingsMutex.lock(); + + m_inputSampleRate = notif.getSampleRate(); + m_nco.setFreq(-notif.getFrequencyOffset(), m_inputSampleRate); + m_interpolator.create(16, m_inputSampleRate, m_rfBandwidth / 2.0); + m_sampleDistanceRemain = m_inputSampleRate / m_outputSampleRate; + + m_settingsMutex.unlock(); + + qDebug() << "TCPSrc::handleMessage: MsgChannelizerNotification: m_inputSampleRate: " << m_inputSampleRate + << " frequencyOffset: " << notif.getFrequencyOffset(); + + return true; + } + else if (MsgUDPSrcConfigure::match(cmd)) + { + MsgUDPSrcConfigure& cfg = (MsgUDPSrcConfigure&) cmd; + + m_settingsMutex.lock(); + + m_sampleFormat = cfg.getSampleFormat(); + m_outputSampleRate = cfg.getOutputSampleRate(); + m_rfBandwidth = cfg.getRFBandwidth(); + + if (cfg.getTCPPort() != m_tcpPort) + { + m_tcpPort = cfg.getTCPPort(); + + if(m_tcpServer->isListening()) + { + m_tcpServer->close(); + } + + m_tcpServer->listen(QHostAddress::Any, m_tcpPort); + } + + m_boost = cfg.getBoost(); + m_interpolator.create(16, m_inputSampleRate, m_rfBandwidth / 2.0); + m_sampleDistanceRemain = m_inputSampleRate / m_outputSampleRate; + + if (m_sampleFormat == FormatSSB) + { + TCPFilter->create_filter(0.3 / 48.0, m_rfBandwidth / 2.0 / m_outputSampleRate); + } + else + { + TCPFilter->create_filter(0.0, m_rfBandwidth / 2.0 / m_outputSampleRate); + } + + m_settingsMutex.unlock(); + + qDebug() << " - MsgTCPSrcConfigure: m_sampleFormat: " << m_sampleFormat + << " m_outputSampleRate: " << m_outputSampleRate + << " m_rfBandwidth: " << m_rfBandwidth + << " m_boost: " << m_boost; + + return true; + } + else if (MsgUDPSrcSpectrum::match(cmd)) + { + MsgUDPSrcSpectrum& spc = (MsgUDPSrcSpectrum&) cmd; + + m_spectrumEnabled = spc.getEnabled(); + + qDebug() << " - MsgTCPSrcSpectrum: m_spectrumEnabled: " << m_spectrumEnabled; + + return true; + } + else + { + if(m_spectrum != 0) + { + return m_spectrum->handleMessage(cmd); + } + else + { + return false; + } + } +} + +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_tcpServer->hasPendingConnections()) + { + qDebug("UDPSrc::onNewConnection: has a pending connection"); + QTcpSocket* connection = m_tcpServer->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::onTcpServerError(QAbstractSocket::SocketError socketError) +{ + qDebug("UDPSrc::onTcpServerError: %s", qPrintable(m_tcpServer->errorString())); +} diff --git a/plugins/channel/udpsrc/udpsrc.h b/plugins/channel/udpsrc/udpsrc.h new file mode 100644 index 000000000..124c8ea14 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrc.h @@ -0,0 +1,172 @@ +#ifndef INCLUDE_TCPSRC_H +#define INCLUDE_TCPSRC_H + +#include +#include +#include "dsp/samplesink.h" +#include "dsp/nco.h" +#include "dsp/fftfilt.h" +#include "dsp/interpolator.h" +#include "util/message.h" + +#define tcpFftLen 2048 + +class QTcpServer; +class QTcpSocket; +class UDPSrcGUI; + +class UDPSrc : public SampleSink { + Q_OBJECT + +public: + enum SampleFormat { + FormatSSB, + FormatNFM, + FormatS16LE, + FormatNone + }; + + UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, SampleSink* spectrum); + virtual ~UDPSrc(); + + void configure(MessageQueue* messageQueue, SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int tcpPort, int boost); + void setSpectrum(MessageQueue* messageQueue, bool enabled); + Real getMagSq() const { return m_magsq; } + + virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly); + virtual void start(); + 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 + + public: + SampleFormat getSampleFormat() const { return m_sampleFormat; } + Real getOutputSampleRate() const { return m_outputSampleRate; } + Real getRFBandwidth() const { return m_rfBandwidth; } + int getTCPPort() const { return m_tcpPort; } + int getBoost() const { return m_boost; } + + static MsgUDPSrcConfigure* create(SampleFormat sampleFormat, Real sampleRate, Real rfBandwidth, int tcpPort, int boost) + { + return new MsgUDPSrcConfigure(sampleFormat, sampleRate, rfBandwidth, tcpPort, boost); + } + + private: + SampleFormat m_sampleFormat; + Real m_outputSampleRate; + Real m_rfBandwidth; + int m_tcpPort; + int m_boost; + + MsgUDPSrcConfigure(SampleFormat sampleFormat, Real outputSampleRate, Real rfBandwidth, int tcpPort, int boost) : + Message(), + m_sampleFormat(sampleFormat), + m_outputSampleRate(outputSampleRate), + m_rfBandwidth(rfBandwidth), + m_tcpPort(tcpPort), + m_boost(boost) + { } + }; + class MsgUDPSrcSpectrum : public Message { + MESSAGE_CLASS_DECLARATION + + public: + bool getEnabled() const { return m_enabled; } + + static MsgUDPSrcSpectrum* create(bool enabled) + { + return new MsgUDPSrcSpectrum(enabled); + } + + private: + bool m_enabled; + + MsgUDPSrcSpectrum(bool enabled) : + Message(), + m_enabled(enabled) + { } + }; + + MessageQueue* m_uiMessageQueue; + UDPSrcGUI* m_tcpSrcGUI; + + int m_inputSampleRate; + + int m_sampleFormat; + Real m_outputSampleRate; + Real m_rfBandwidth; + int m_tcpPort; + int m_boost; + Real m_magsq; + + Real m_scale; + Complex m_last, m_this; + + NCO m_nco; + Interpolator m_interpolator; + Real m_sampleDistanceRemain; + fftfilt* TCPFilter; + + SampleVector m_sampleBuffer; + SampleVector m_sampleBufferSSB; + SampleSink* m_spectrum; + bool m_spectrumEnabled; + + QTcpServer* m_tcpServer; + 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 onTcpServerError(QAbstractSocket::SocketError socketError); +}; + +#endif // INCLUDE_TCPSRC_H diff --git a/plugins/channel/udpsrc/udpsrcgui.cpp b/plugins/channel/udpsrc/udpsrcgui.cpp new file mode 100644 index 000000000..270cd1419 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrcgui.cpp @@ -0,0 +1,416 @@ +#include "udpsrcgui.h" + +#include "plugin/pluginapi.h" +#include "dsp/threadedsamplesink.h" +#include "dsp/channelizer.h" +#include "dsp/spectrumvis.h" +#include "dsp/dspengine.h" +#include "util/simpleserializer.h" +#include "util/db.h" +#include "gui/basicchannelsettingswidget.h" +#include "ui_udpsrcgui.h" +#include "mainwindow.h" +#include "udpsrc.h" + +UDPSrcGUI* UDPSrcGUI::create(PluginAPI* pluginAPI) +{ + UDPSrcGUI* gui = new UDPSrcGUI(pluginAPI); + return gui; +} + +void UDPSrcGUI::destroy() +{ + delete this; +} + +void UDPSrcGUI::setName(const QString& name) +{ + setObjectName(name); +} + +qint64 UDPSrcGUI::getCenterFrequency() const +{ + return m_channelMarker.getCenterFrequency(); +} + +void UDPSrcGUI::setCenterFrequency(qint64 centerFrequency) +{ + m_channelMarker.setCenterFrequency(centerFrequency); + applySettings(); +} + +QString UDPSrcGUI::getName() const +{ + return objectName(); +} + +void UDPSrcGUI::resetToDefaults() +{ + blockApplySettings(true); + + ui->sampleFormat->setCurrentIndex(0); + ui->sampleRate->setText("48000"); + ui->rfBandwidth->setText("32000"); + ui->udpPort->setText("9999"); + ui->spectrumGUI->resetToDefaults(); + ui->boost->setValue(1); + + blockApplySettings(false); + applySettings(); +} + +QByteArray UDPSrcGUI::serialize() const +{ + SimpleSerializer s(1); + s.writeBlob(1, saveState()); + s.writeS32(2, m_channelMarker.getCenterFrequency()); + s.writeS32(3, m_sampleFormat); + s.writeReal(4, m_outputSampleRate); + s.writeReal(5, m_rfBandwidth); + s.writeS32(6, m_tcpPort); + s.writeBlob(7, ui->spectrumGUI->serialize()); + s.writeS32(8, (qint32)m_boost); + s.writeS32(9, m_channelMarker.getCenterFrequency()); + return s.final(); +} + +bool UDPSrcGUI::deserialize(const QByteArray& data) +{ + SimpleDeserializer d(data); + + if (!d.isValid()) + { + resetToDefaults(); + return false; + } + + if (d.getVersion() == 1) + { + QByteArray bytetmp; + qint32 s32tmp; + Real realtmp; + + blockApplySettings(true); + m_channelMarker.blockSignals(true); + + d.readBlob(1, &bytetmp); + restoreState(bytetmp); + d.readS32(2, &s32tmp, 0); + m_channelMarker.setCenterFrequency(s32tmp); + d.readS32(3, &s32tmp, UDPSrc::FormatSSB); + switch(s32tmp) { + case UDPSrc::FormatSSB: + ui->sampleFormat->setCurrentIndex(0); + break; + case UDPSrc::FormatNFM: + ui->sampleFormat->setCurrentIndex(1); + break; + case UDPSrc::FormatS16LE: + ui->sampleFormat->setCurrentIndex(2); + break; + default: + ui->sampleFormat->setCurrentIndex(0); + break; + } + d.readReal(4, &realtmp, 48000); + ui->sampleRate->setText(QString("%1").arg(realtmp, 0)); + d.readReal(5, &realtmp, 32000); + ui->rfBandwidth->setText(QString("%1").arg(realtmp, 0)); + d.readS32(6, &s32tmp, 9999); + ui->udpPort->setText(QString("%1").arg(s32tmp)); + d.readBlob(7, &bytetmp); + ui->spectrumGUI->deserialize(bytetmp); + d.readS32(8, &s32tmp, 1); + ui->boost->setValue(s32tmp); + d.readS32(9, &s32tmp, 0); + m_channelMarker.setCenterFrequency(s32tmp); + + blockApplySettings(false); + m_channelMarker.blockSignals(false); + + applySettings(); + return true; + } + else + { + resetToDefaults(); + return false; + } +} + +bool UDPSrcGUI::handleMessage(const Message& message) +{ + qDebug() << "TCPSrcGUI::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: TCPSrc::MsgTCPSrcConnection: " << con.getConnect() + << " ID: " << con.getID() + << " peerAddress: " << con.getPeerAddress() + << " peerPort: " << con.getPeerPort(); + + return true; + } + else + { + return false; + } +} + +void UDPSrcGUI::channelMarkerChanged() +{ + applySettings(); +} + +void UDPSrcGUI::tick() +{ + Real powDb = CalcDb::dbPower(m_tcpSrc->getMagSq()); + m_channelPowerDbAvg.feed(powDb); + ui->channelPower->setText(QString::number(m_channelPowerDbAvg.average(), 'f', 1)); +} + +UDPSrcGUI::UDPSrcGUI(PluginAPI* pluginAPI, QWidget* parent) : + RollupWidget(parent), + ui(new Ui::UDPSrcGUI), + m_pluginAPI(pluginAPI), + m_tcpSrc(0), + m_channelMarker(this), + m_channelPowerDbAvg(40,0), + m_basicSettingsShown(false), + m_doApplySettings(true) +{ + ui->setupUi(this); + ui->connectedClientsBox->hide(); + connect(this, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool))); + connect(this, SIGNAL(menuDoubleClickEvent()), this, SLOT(onMenuDoubleClicked())); + setAttribute(Qt::WA_DeleteOnClose, true); + + m_spectrumVis = new SpectrumVis(ui->glSpectrum); + m_tcpSrc = new UDPSrc(m_pluginAPI->getMainWindowMessageQueue(), this, m_spectrumVis); + m_channelizer = new Channelizer(m_tcpSrc); + m_threadedChannelizer = new ThreadedSampleSink(m_channelizer, this); + DSPEngine::instance()->addThreadedSink(m_threadedChannelizer); + + ui->deltaFrequency->setColorMapper(ColorMapper(ColorMapper::ReverseGold)); + ui->deltaFrequency->setValueRange(7, 0U, 9999999U); + + ui->glSpectrum->setCenterFrequency(0); + ui->glSpectrum->setSampleRate(ui->sampleRate->text().toInt()); + ui->glSpectrum->setDisplayWaterfall(true); + ui->glSpectrum->setDisplayMaxHold(true); + m_spectrumVis->configure(m_spectrumVis->getInputMessageQueue(), 64, 10, FFTWindow::BlackmanHarris); + + ui->glSpectrum->connectTimer(m_pluginAPI->getMainWindow()->getMasterTimer()); + connect(&m_pluginAPI->getMainWindow()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick())); + + //m_channelMarker = new ChannelMarker(this); + m_channelMarker.setBandwidth(16000); + m_channelMarker.setCenterFrequency(0); + m_channelMarker.setColor(Qt::green); + m_channelMarker.setVisible(true); + connect(&m_channelMarker, SIGNAL(changed()), this, SLOT(channelMarkerChanged())); + m_pluginAPI->addChannelMarker(&m_channelMarker); + + ui->spectrumGUI->setBuddies(m_spectrumVis->getInputMessageQueue(), m_spectrumVis, ui->glSpectrum); + + applySettings(); +} + +UDPSrcGUI::~UDPSrcGUI() +{ + m_pluginAPI->removeChannelInstance(this); + DSPEngine::instance()->removeThreadedSink(m_threadedChannelizer); + delete m_threadedChannelizer; + delete m_channelizer; + delete m_tcpSrc; + delete m_spectrumVis; + //delete m_channelMarker; + delete ui; +} + +void UDPSrcGUI::blockApplySettings(bool block) +{ + m_doApplySettings = !block; +} + +void UDPSrcGUI::applySettings() +{ + if (m_doApplySettings) + { + bool ok; + + Real outputSampleRate = ui->sampleRate->text().toDouble(&ok); + + if((!ok) || (outputSampleRate < 1000)) + { + outputSampleRate = 48000; + } + + Real rfBandwidth = ui->rfBandwidth->text().toDouble(&ok); + + if((!ok) || (rfBandwidth > outputSampleRate)) + { + rfBandwidth = outputSampleRate; + } + + int tcpPort = ui->udpPort->text().toInt(&ok); + + if((!ok) || (tcpPort < 1) || (tcpPort > 65535)) + { + tcpPort = 9999; + } + + int boost = ui->boost->value(); + + setTitleColor(m_channelMarker.getColor()); + ui->deltaFrequency->setValue(abs(m_channelMarker.getCenterFrequency())); + ui->deltaMinus->setChecked(m_channelMarker.getCenterFrequency() < 0); + ui->sampleRate->setText(QString("%1").arg(outputSampleRate, 0)); + ui->rfBandwidth->setText(QString("%1").arg(rfBandwidth, 0)); + ui->udpPort->setText(QString("%1").arg(tcpPort)); + ui->boost->setValue(boost); + m_channelMarker.disconnect(this, SLOT(channelMarkerChanged())); + m_channelMarker.setBandwidth((int)rfBandwidth); + connect(&m_channelMarker, SIGNAL(changed()), this, SLOT(channelMarkerChanged())); + ui->glSpectrum->setSampleRate(outputSampleRate); + + m_channelizer->configure(m_channelizer->getInputMessageQueue(), + outputSampleRate, + m_channelMarker.getCenterFrequency()); + + UDPSrc::SampleFormat sampleFormat; + + switch(ui->sampleFormat->currentIndex()) + { + case 0: + sampleFormat = UDPSrc::FormatSSB; + break; + case 1: + sampleFormat = UDPSrc::FormatNFM; + break; + case 2: + sampleFormat = UDPSrc::FormatS16LE; + break; + default: + sampleFormat = UDPSrc::FormatSSB; + break; + } + + m_sampleFormat = sampleFormat; + m_outputSampleRate = outputSampleRate; + m_rfBandwidth = rfBandwidth; + m_tcpPort = tcpPort; + m_boost = boost; + + m_tcpSrc->configure(m_tcpSrc->getInputMessageQueue(), + sampleFormat, + outputSampleRate, + rfBandwidth, + tcpPort, + boost); + + ui->applyBtn->setEnabled(false); + } +} + +void UDPSrcGUI::on_deltaMinus_toggled(bool minus) +{ + int deltaFrequency = m_channelMarker.getCenterFrequency(); + bool minusDelta = (deltaFrequency < 0); + + if (minus ^ minusDelta) // sign change + { + m_channelMarker.setCenterFrequency(-deltaFrequency); + } +} + +void UDPSrcGUI::on_deltaFrequency_changed(quint64 value) +{ + if (ui->deltaMinus->isChecked()) { + m_channelMarker.setCenterFrequency(-value); + } else { + m_channelMarker.setCenterFrequency(value); + } +} + +void UDPSrcGUI::on_sampleFormat_currentIndexChanged(int index) +{ + ui->applyBtn->setEnabled(true); +} + +void UDPSrcGUI::on_sampleRate_textEdited(const QString& arg1) +{ + ui->applyBtn->setEnabled(true); +} + +void UDPSrcGUI::on_rfBandwidth_textEdited(const QString& arg1) +{ + ui->applyBtn->setEnabled(true); +} + +void UDPSrcGUI::on_udpPort_textEdited(const QString& arg1) +{ + ui->applyBtn->setEnabled(true); +} + +void UDPSrcGUI::on_applyBtn_clicked() +{ + applySettings(); +} + +void UDPSrcGUI::on_boost_valueChanged(int value) +{ + ui->boost->setValue(value); + ui->boostText->setText(QString("%1").arg(value)); + ui->applyBtn->setEnabled(true); +} + +void UDPSrcGUI::onWidgetRolled(QWidget* widget, bool rollDown) +{ + if ((widget == ui->spectrumBox) && (m_tcpSrc != 0)) + { + m_tcpSrc->setSpectrum(m_tcpSrc->getInputMessageQueue(), rollDown); + } +} + +void UDPSrcGUI::onMenuDoubleClicked() +{ + if (!m_basicSettingsShown) + { + m_basicSettingsShown = true; + BasicChannelSettingsWidget* bcsw = new BasicChannelSettingsWidget(&m_channelMarker, this); + 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 new file mode 100644 index 000000000..a5dcf25a9 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrcgui.h @@ -0,0 +1,85 @@ +#ifndef INCLUDE_TCPSRCGUI_H +#define INCLUDE_TCPSRCGUI_H + +#include +#include "gui/rollupwidget.h" +#include "plugin/plugingui.h" +#include "dsp/channelmarker.h" +#include "dsp/movingaverage.h" + +#include "udpsrc.h" + +class PluginAPI; +class ThreadedSampleSink; +class Channelizer; +class UDPSrc; +class SpectrumVis; + +namespace Ui { + class UDPSrcGUI; +} + +class UDPSrcGUI : public RollupWidget, public PluginGUI { + Q_OBJECT + +public: + static UDPSrcGUI* create(PluginAPI* pluginAPI); + void destroy(); + + void setName(const QString& name); + QString getName() const; + virtual qint64 getCenterFrequency() const; + virtual void setCenterFrequency(qint64 centerFrequency); + + void resetToDefaults(); + QByteArray serialize() const; + bool deserialize(const QByteArray& data); + + virtual bool handleMessage(const Message& message); + +private slots: + void channelMarkerChanged(); + void on_deltaFrequency_changed(quint64 value); + void on_deltaMinus_toggled(bool minus); + void on_sampleFormat_currentIndexChanged(int index); + void on_sampleRate_textEdited(const QString& arg1); + void on_rfBandwidth_textEdited(const QString& arg1); + void on_udpPort_textEdited(const QString& arg1); + void on_applyBtn_clicked(); + void onWidgetRolled(QWidget* widget, bool rollDown); + void onMenuDoubleClicked(); + void on_boost_valueChanged(int value); + void tick(); + +private: + Ui::UDPSrcGUI* ui; + PluginAPI* m_pluginAPI; + UDPSrc* m_tcpSrc; + ChannelMarker m_channelMarker; + MovingAverage m_channelPowerDbAvg; + + // settings + UDPSrc::SampleFormat m_sampleFormat; + Real m_outputSampleRate; + Real m_rfBandwidth; + int m_boost; + int m_tcpPort; + bool m_basicSettingsShown; + bool m_doApplySettings; + + // RF path + ThreadedSampleSink* m_threadedChannelizer; + Channelizer* m_channelizer; + SpectrumVis* m_spectrumVis; + + explicit UDPSrcGUI(PluginAPI* pluginAPI, QWidget* parent = 0); + virtual ~UDPSrcGUI(); + + void blockApplySettings(bool block); + void applySettings(); + + void addConnection(quint32 id, const QHostAddress& peerAddress, int peerPort); + void delConnection(quint32 id); +}; + +#endif // INCLUDE_TCPSRCGUI_H diff --git a/plugins/channel/udpsrc/udpsrcgui.ui b/plugins/channel/udpsrc/udpsrcgui.ui new file mode 100644 index 000000000..3c4fde88e --- /dev/null +++ b/plugins/channel/udpsrc/udpsrcgui.ui @@ -0,0 +1,454 @@ + + + UDPSrcGUI + + + + 0 + 0 + 400 + 443 + + + + UDP Sample Source + + + + + 10 + 5 + 201 + 142 + + + + Settings + + + + 2 + + + 2 + + + 2 + + + 2 + + + 3 + + + + + Sample Format + + + + + + + 2 + + + + S16LE SSB + + + + + S16LE NFM + + + + + S16LE I/Q + + + + + + + + 32000 + + + + + + + RF Bandwidth (Hz) + + + + + + + Samplerate (Hz) + + + + + + + UDP Port + + + + + + + 48000 + + + + + + + 9999 + + + + + + + false + + + Apply + + + + + + + + + ... + + + + :/plus.png + :/minus.png + + + + true + + + false + + + + + + + + 0 + 0 + + + + + 32 + 16 + + + + + Monospace + 10 + + + + SizeVerCursor + + + Qt::StrongFocus + + + Demod shift frequency from center in Hz + + + + + + + + + + + + 26 + 26 + 26 + + + + + + + 255 + 255 + 255 + + + + + + + + + 26 + 26 + 26 + + + + + + + 255 + 255 + 255 + + + + + + + + + 118 + 118 + 117 + + + + + + + 255 + 255 + 255 + + + + + + + + + 8 + + + + Hz + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Channel power + + + Qt::LeftToRight + + + 0.0 + + + + + + + dB + + + + + + + + + + + Boost + + + + + + + 3 + + + 1 + + + Qt::Horizontal + + + + + + + 0 + + + + + + + + + + + 15 + 160 + 231 + 156 + + + + Channel Spectrum + + + + 3 + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + + + + + + + + 15 + 330 + 274 + 101 + + + + Connected Clients (0) + + + + 3 + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + 400 + 100 + + + + false + + + false + + + false + + + + IP:Port + + + + + + + + + + ValueDial + QWidget +
gui/valuedial.h
+ 1 +
+ + GLSpectrum + QWidget +
gui/glspectrum.h
+ 1 +
+ + GLSpectrumGUI + QWidget +
gui/glspectrumgui.h
+ 1 +
+ + RollupWidget + QWidget +
gui/rollupwidget.h
+ 1 +
+
+ + sampleFormat + udpPort + sampleRate + rfBandwidth + applyBtn + connections + + + + + +
diff --git a/plugins/channel/udpsrc/udpsrcplugin.cpp b/plugins/channel/udpsrc/udpsrcplugin.cpp new file mode 100644 index 000000000..60a529a02 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrcplugin.cpp @@ -0,0 +1,55 @@ +#include "udpsrcplugin.h" + +#include +#include +#include "plugin/pluginapi.h" + +#include "udpsrcgui.h" + +const PluginDescriptor UDPSrcPlugin::m_pluginDescriptor = { + QString("UDP Channel Source"), + QString("---"), + QString("(c) Edouard Griffiths, F4EXB"), + QString("https://github.com/f4exb/sdrangel"), + true, + QString("https://github.com/f4exb/sdrangel") +}; + +UDPSrcPlugin::UDPSrcPlugin(QObject* parent) : + QObject(parent) +{ +} + +const PluginDescriptor& UDPSrcPlugin::getPluginDescriptor() const +{ + return m_pluginDescriptor; +} + +void UDPSrcPlugin::initPlugin(PluginAPI* pluginAPI) +{ + m_pluginAPI = pluginAPI; + + // register TCP Channel Source + QAction* action = new QAction(tr("&UDP Source"), this); + connect(action, SIGNAL(triggered()), this, SLOT(createInstanceUDPSrc())); + m_pluginAPI->registerChannel("sdrangel.channel.udpsrc", this, action); +} + +PluginGUI* UDPSrcPlugin::createChannel(const QString& channelName) +{ + if(channelName == "sdrangel.channel.udpsrc") { + UDPSrcGUI* gui = UDPSrcGUI::create(m_pluginAPI); + m_pluginAPI->registerChannelInstance("sdrangel.channel.udpsrc", gui); + m_pluginAPI->addChannelRollup(gui); + return gui; + } else { + return 0; + } +} + +void UDPSrcPlugin::createInstanceUDPSrc() +{ + UDPSrcGUI* gui = UDPSrcGUI::create(m_pluginAPI); + m_pluginAPI->registerChannelInstance("sdrangel.channel.udpsrc", gui); + m_pluginAPI->addChannelRollup(gui); +} diff --git a/plugins/channel/udpsrc/udpsrcplugin.h b/plugins/channel/udpsrc/udpsrcplugin.h new file mode 100644 index 000000000..5da0e9ad9 --- /dev/null +++ b/plugins/channel/udpsrc/udpsrcplugin.h @@ -0,0 +1,29 @@ +#ifndef INCLUDE_UDPSRCPLUGIN_H +#define INCLUDE_UDPSRCPLUGIN_H + +#include +#include "plugin/plugininterface.h" + +class UDPSrcPlugin : public QObject, PluginInterface { + Q_OBJECT + Q_INTERFACES(PluginInterface) + Q_PLUGIN_METADATA(IID "sdrangel.demod.udpsrc") + +public: + explicit UDPSrcPlugin(QObject* parent = 0); + + const PluginDescriptor& getPluginDescriptor() const; + void initPlugin(PluginAPI* pluginAPI); + + PluginGUI* createChannel(const QString& channelName); + +private: + static const PluginDescriptor m_pluginDescriptor; + + PluginAPI* m_pluginAPI; + +private slots: + void createInstanceUDPSrc(); +}; + +#endif // INCLUDE_UDPSRCPLUGIN_H