1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-24 10:50:29 -05:00

SDRdaemon plugin: code cleanup

This commit is contained in:
f4exb 2016-02-20 03:48:04 +01:00
parent 33a196e4aa
commit b59c28d4c7
5 changed files with 1 additions and 358 deletions

View File

@ -45,6 +45,7 @@ SDRdaemonBuffer::SDRdaemonBuffer(uint32_t rateDivider) :
m_readChunkIndex(0), m_readChunkIndex(0),
m_rawSize(0), m_rawSize(0),
m_rawBuffer(0), m_rawBuffer(0),
m_chunkSize(0),
m_bytesInBlock(0), m_bytesInBlock(0),
m_nbBlocks(0) m_nbBlocks(0)
{ {
@ -174,7 +175,6 @@ void SDRdaemonBuffer::writeDataLZ4(const char *array, uint32_t length)
} }
uint64_t crc64 = m_crc64.calculate_crc(m_lz4InBuffer, m_lz4InSize); uint64_t crc64 = m_crc64.calculate_crc(m_lz4InBuffer, m_lz4InSize);
//uint64_t crc64 = 0x0123456789ABCDEF;
if (memcmp(&crc64, &m_dataCRC, 8) == 0) if (memcmp(&crc64, &m_dataCRC, 8) == 0)
{ {

View File

@ -1,257 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Edouard Griffiths, F4EXB //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include "dsp/samplefifo.h"
#include "util/messagequeue.h"
#include "sdrdaemoninput.h"
#include <QUdpSocket>
#include <QDebug>
#include "sdrdaemonthread.h"
const int SDRdaemonThread::m_rateDivider = 1000/SDRDAEMON_THROTTLE_MS;
const int SDRdaemonThread::m_udpPayloadSize = 512;
SDRdaemonThread::SDRdaemonThread(SampleFifo* sampleFifo, MessageQueue *outputMessageQueueToGUI, QObject* parent) :
QThread(parent),
m_running(false),
m_dataSocket(0),
m_dataAddress(QHostAddress::LocalHost),
m_dataPort(9090),
m_dataConnected(false),
m_buf(0),
m_udpBuf(0),
m_bufsize(0),
m_chunksize(0),
m_sampleFifo(sampleFifo),
m_samplesCount(0),
m_sdrDaemonBuffer(m_udpPayloadSize),
m_samplerate(0),
m_centerFrequency(0),
m_outputMessageQueueToGUI(outputMessageQueueToGUI)
{
m_udpBuf = new char[m_udpPayloadSize];
m_dataSocket = new QUdpSocket(this);
if (m_dataSocket->bind(m_dataAddress, m_dataPort))
{
qDebug("SDRdaemonThread::SDRdaemonThread: bind data socket to port %d", m_dataPort);
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
m_dataConnected = true;
}
else
{
qWarning("SDRdaemonThread::SDRdaemonThread: cannot bind data port %d", m_dataPort);
m_dataConnected = false;
}
}
SDRdaemonThread::~SDRdaemonThread()
{
if (m_running) {
stopWork();
}
if (m_udpBuf != 0) {
free(m_udpBuf);
}
if (m_buf != 0) {
free(m_buf);
}
}
void SDRdaemonThread::startWork()
{
qDebug() << "SDRdaemonThread::startWork: ";
if (!m_dataConnected)
{
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
m_dataConnected = true;
}
m_startWaitMutex.lock();
start();
while(!m_running)
m_startWaiter.wait(&m_startWaitMutex, 100);
m_startWaitMutex.unlock();
}
void SDRdaemonThread::stopWork()
{
qDebug() << "SDRdaemonThread::stopWork";
if (m_dataConnected) {
disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
m_dataConnected = false;
}
m_running = false;
wait();
}
void SDRdaemonThread::setSamplerate(uint32_t samplerate)
{
bool wasRunning = m_running;
qDebug() << "SDRdaemonThread::setSamplerate:"
<< " new:" << samplerate
<< " old:" << m_samplerate;
if (m_running)
{
stopWork();
m_running = false;
}
m_samplerate = samplerate;
m_chunksize = (m_samplerate / m_rateDivider)*4; // TODO: implement FF and slow motion here. 4 corresponds to live. 2 is half speed, 8 is doulbe speed
m_bufsize = m_chunksize;
if (m_buf == 0) {
qDebug() << " - Allocate buffer";
m_buf = (quint8*) malloc(m_bufsize);
} else {
qDebug() << " - Re-allocate buffer";
m_buf = (quint8*) realloc((void*) m_buf, m_bufsize);
}
qDebug() << " - size: " << m_bufsize
<< " #samples: " << (m_bufsize/4);
if (wasRunning)
{
startWork();
m_running = true;
}
}
void SDRdaemonThread::updateLink(const QString& address, quint16 port)
{
if (m_dataSocket) {
delete m_dataSocket;
m_dataSocket = 0;
}
m_dataSocket = new QUdpSocket(this);
m_dataPort = port;
if (m_dataAddress.setAddress(address))
{
if (m_dataSocket->bind(m_dataAddress, m_dataPort))
{
qDebug("SDRdaemonThread::startWork: bind data socket to port %d", m_dataPort);
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
m_dataConnected = true;
}
else
{
qWarning("SDRdaemonThread::updateLink: cannot bind data port %d", m_dataPort);
m_dataConnected = false;
}
}
else
{
qWarning("SDRdaemonThread::updateLink: cannot set data address: %s", qPrintable(address));
m_dataConnected = false;
}
}
void SDRdaemonThread::run()
{
int res;
m_running = true;
m_startWaiter.wakeAll();
while(m_running) // actual work is in the tick() function
{
sleep(1);
}
m_running = false;
}
void SDRdaemonThread::connectTimer(const QTimer& timer)
{
qDebug() << "SDRdaemonThread::connectTimer";
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}
void SDRdaemonThread::tick()
{
if (m_running)
{
// read samples directly feeding the SampleFifo (no callback)
m_sampleFifo->write(reinterpret_cast<quint8*>(m_sdrDaemonBuffer.readData(m_chunksize)), m_chunksize);
m_samplesCount += m_chunksize / 4;
}
}
void SDRdaemonThread::dataReadyRead()
{
while (m_dataSocket->hasPendingDatagrams())
{
qint64 pendingDataSize = m_dataSocket->pendingDatagramSize();
qint64 readBytes = m_dataSocket->readDatagram(m_udpBuf, pendingDataSize, 0, 0);
if (readBytes < 0)
{
qDebug() << "SDRdaemonThread::dataReadyRead: read failed";
}
else if (readBytes > 0)
{
m_sdrDaemonBuffer.updateBlockCounts(readBytes);
if (m_sdrDaemonBuffer.readMeta(m_udpBuf, readBytes))
{
const SDRdaemonBuffer::MetaData& metaData = m_sdrDaemonBuffer.getCurrentMeta();
bool change = false;
if (m_samplerate != metaData.m_sampleRate)
{
setSamplerate(metaData.m_sampleRate);
m_samplerate = metaData.m_sampleRate;
change = true;
}
if (m_centerFrequency != metaData.m_centerFrequency)
{
m_centerFrequency = metaData.m_centerFrequency;
change = true;
}
if (change)
{
SDRdaemonInput::MsgReportSDRdaemonStreamData *report = SDRdaemonInput::MsgReportSDRdaemonStreamData::create(
metaData.m_sampleRate,
metaData.m_centerFrequency,
metaData.m_tv_sec,
metaData.m_tv_usec);
m_outputMessageQueueToGUI->push(report);
}
}
else if (m_sdrDaemonBuffer.isSync())
{
m_sdrDaemonBuffer.writeData(m_udpBuf, readBytes);
}
}
}
}

View File

@ -1,85 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB //
// //
// 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_SDRDAEMONTHREAD_H
#define INCLUDE_SDRDAEMONTHREAD_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QTimer>
#include <QHostAddress>
#include <iostream>
#include <cstdlib>
#include "dsp/samplefifo.h"
#include "dsp/inthalfbandfilter.h"
#include "sdrdaemonbuffer.h"
#define SDRDAEMON_THROTTLE_MS 50
class QUdpSocket;
class MessageQueue;
class SDRdaemonThread : public QThread {
Q_OBJECT
public:
SDRdaemonThread(SampleFifo* sampleFifo, MessageQueue *outputMessageQueueToGUI, QObject* parent = NULL);
~SDRdaemonThread();
void startWork();
void stopWork();
void updateLink(const QString& address, quint16 port);
bool isRunning() const { return m_running; }
std::size_t getSamplesCount() const { return m_samplesCount; }
void connectTimer(const QTimer& timer);
public slots:
void dataReadyRead();
private:
QMutex m_startWaitMutex;
QWaitCondition m_startWaiter;
bool m_running;
QUdpSocket *m_dataSocket;
QHostAddress m_dataAddress;
quint16 m_dataPort;
bool m_dataConnected;
quint8 *m_buf;
char *m_udpBuf;
std::size_t m_bufsize;
std::size_t m_chunksize;
SampleFifo* m_sampleFifo;
std::size_t m_samplesCount;
SDRdaemonBuffer m_sdrDaemonBuffer;
uint32_t m_samplerate;
uint32_t m_centerFrequency;
MessageQueue *m_outputMessageQueueToGUI;
static const int m_rateDivider;
static const int m_udpPayloadSize;
void setSamplerate(uint32_t samplerate);
void run();
private slots:
void tick();
};
#endif // INCLUDE_SDRDAEMONTHREAD_H

View File

@ -25,7 +25,6 @@
const int SDRdaemonUDPHandler::m_rateDivider = 1000/SDRDAEMON_THROTTLE_MS; const int SDRdaemonUDPHandler::m_rateDivider = 1000/SDRDAEMON_THROTTLE_MS;
SDRdaemonUDPHandler::SDRdaemonUDPHandler(SampleFifo *sampleFifo, MessageQueue *outputMessageQueueToGUI) : SDRdaemonUDPHandler::SDRdaemonUDPHandler(SampleFifo *sampleFifo, MessageQueue *outputMessageQueueToGUI) :
//m_mutex(QMutex::Recursive),
m_sdrDaemonBuffer(m_rateDivider), m_sdrDaemonBuffer(m_rateDivider),
m_dataSocket(0), m_dataSocket(0),
m_dataAddress(QHostAddress::LocalHost), m_dataAddress(QHostAddress::LocalHost),
@ -64,7 +63,6 @@ void SDRdaemonUDPHandler::start()
if (m_dataSocket->bind(m_dataAddress, m_dataPort)) if (m_dataSocket->bind(m_dataAddress, m_dataPort))
{ {
qDebug("SDRdaemonUDPHandler::start: bind data socket to port %d", m_dataPort); qDebug("SDRdaemonUDPHandler::start: bind data socket to port %d", m_dataPort);
//connect(this, SIGNAL(dataReady()), this, SLOT(processData()));
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()), Qt::QueuedConnection); // , Qt::QueuedConnection connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()), Qt::QueuedConnection); // , Qt::QueuedConnection
m_dataConnected = true; m_dataConnected = true;
} }
@ -82,7 +80,6 @@ void SDRdaemonUDPHandler::stop()
if (m_dataConnected) { if (m_dataConnected) {
disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
//disconnect(this, SIGNAL(dataReady()), this, SLOT(processData));
m_dataConnected = false; m_dataConnected = false;
} }
@ -95,13 +92,10 @@ void SDRdaemonUDPHandler::stop()
void SDRdaemonUDPHandler::dataReadyRead() void SDRdaemonUDPHandler::dataReadyRead()
{ {
//qDebug() << "SDRdaemonUDPHandler::dataReadyRead";
while (m_dataSocket->hasPendingDatagrams()) while (m_dataSocket->hasPendingDatagrams())
{ {
qint64 pendingDataSize = m_dataSocket->pendingDatagramSize(); qint64 pendingDataSize = m_dataSocket->pendingDatagramSize();
m_udpReadBytes = m_dataSocket->readDatagram(m_udpBuf, pendingDataSize, 0, 0); m_udpReadBytes = m_dataSocket->readDatagram(m_udpBuf, pendingDataSize, 0, 0);
//emit dataReady();
processData(); processData();
} }
} }
@ -114,8 +108,6 @@ void SDRdaemonUDPHandler::processData()
} }
else if (m_udpReadBytes > 0) else if (m_udpReadBytes > 0)
{ {
//QMutexLocker ml(&m_mutex);
m_sdrDaemonBuffer.updateBlockCounts(m_udpReadBytes); m_sdrDaemonBuffer.updateBlockCounts(m_udpReadBytes);
if (m_sdrDaemonBuffer.readMeta(m_udpBuf, m_udpReadBytes)) if (m_sdrDaemonBuffer.readMeta(m_udpBuf, m_udpReadBytes))
@ -163,8 +155,6 @@ void SDRdaemonUDPHandler::setSamplerate(uint32_t samplerate)
<< " new:" << samplerate << " new:" << samplerate
<< " old:" << m_samplerate; << " old:" << m_samplerate;
//QMutexLocker ml(&m_mutex);
m_samplerate = samplerate; m_samplerate = samplerate;
m_chunksize = (m_samplerate / m_rateDivider) * SDRdaemonBuffer::m_iqSampleSize; m_chunksize = (m_samplerate / m_rateDivider) * SDRdaemonBuffer::m_iqSampleSize;

View File

@ -41,13 +41,8 @@ public:
public slots: public slots:
void dataReadyRead(); void dataReadyRead();
// void processData();
//signals:
// void dataReady();
private: private:
//QMutex m_mutex;
SDRdaemonBuffer m_sdrDaemonBuffer; SDRdaemonBuffer m_sdrDaemonBuffer;
QUdpSocket *m_dataSocket; QUdpSocket *m_dataSocket;
QHostAddress m_dataAddress; QHostAddress m_dataAddress;