2017-05-14 22:58:56 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2017-05-21 11:32:56 +00:00
|
|
|
// Copyright (C) 2017 Edouard Griffiths, F4EXB //
|
2017-05-14 22:58:56 +00:00
|
|
|
// //
|
|
|
|
// 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 <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "util/simpleserializer.h"
|
|
|
|
#include "dsp/dspcommands.h"
|
|
|
|
#include "dsp/dspengine.h"
|
|
|
|
#include "dsp/filerecord.h"
|
|
|
|
|
|
|
|
#include "device/devicesinkapi.h"
|
|
|
|
|
|
|
|
#include "sdrdaemonsinkgui.h"
|
|
|
|
#include "sdrdaemonsinkoutput.h"
|
|
|
|
#include "sdrdaemonsinkthread.h"
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgConfigureSDRdaemonSink, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgConfigureSDRdaemonSinkWork, Message)
|
2017-12-14 17:02:49 +00:00
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgStartStop, Message)
|
2017-05-21 02:19:12 +00:00
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgConfigureSDRdaemonSinkStreamTiming, Message)
|
2017-06-04 23:18:35 +00:00
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgConfigureSDRdaemonSinkChunkCorrection, Message)
|
2017-05-21 02:19:12 +00:00
|
|
|
MESSAGE_CLASS_DEFINITION(SDRdaemonSinkOutput::MsgReportSDRdaemonSinkStreamTiming, Message)
|
|
|
|
|
2017-09-14 11:55:57 +00:00
|
|
|
SDRdaemonSinkOutput::SDRdaemonSinkOutput(DeviceSinkAPI *deviceAPI) :
|
2017-05-14 22:58:56 +00:00
|
|
|
m_deviceAPI(deviceAPI),
|
|
|
|
m_settings(),
|
2017-05-25 18:13:34 +00:00
|
|
|
m_sdrDaemonSinkThread(0),
|
2017-05-21 10:58:57 +00:00
|
|
|
m_deviceDescription("SDRdaemonSink"),
|
2017-05-25 18:13:34 +00:00
|
|
|
m_startingTimeStamp(0),
|
2017-09-14 11:55:57 +00:00
|
|
|
m_masterTimer(deviceAPI->getMasterTimer())
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
SDRdaemonSinkOutput::~SDRdaemonSinkOutput()
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:34:25 +00:00
|
|
|
void SDRdaemonSinkOutput::destroy()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
bool SDRdaemonSinkOutput::start()
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2017-05-21 10:58:57 +00:00
|
|
|
qDebug() << "SDRdaemonSinkOutput::start";
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
if((m_sdrDaemonSinkThread = new SDRdaemonSinkThread(&m_sampleSourceFifo)) == 0)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
qFatal("out of memory");
|
|
|
|
stop();
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-22 23:41:30 +00:00
|
|
|
m_sdrDaemonSinkThread->setRemoteAddress(m_settings.m_address, m_settings.m_dataPort);
|
|
|
|
m_sdrDaemonSinkThread->setCenterFrequency(m_settings.m_centerFrequency);
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->setSamplerate(m_settings.m_sampleRate);
|
2017-05-22 23:41:30 +00:00
|
|
|
m_sdrDaemonSinkThread->setNbBlocksFEC(m_settings.m_nbFECBlocks);
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->connectTimer(m_masterTimer);
|
|
|
|
m_sdrDaemonSinkThread->startWork();
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-06-07 22:01:56 +00:00
|
|
|
double delay = ((127*127*m_settings.m_txDelay) / m_settings.m_sampleRate)/(128 + m_settings.m_nbFECBlocks);
|
|
|
|
m_sdrDaemonSinkThread->setTxDelay((int) (delay*1e6));
|
|
|
|
|
2017-05-14 22:58:56 +00:00
|
|
|
mutexLocker.unlock();
|
|
|
|
//applySettings(m_generalSettings, m_settings, true);
|
2017-05-21 10:58:57 +00:00
|
|
|
qDebug("SDRdaemonSinkOutput::start: started");
|
2017-05-14 22:58:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
void SDRdaemonSinkOutput::stop()
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
qDebug() << "SDRdaemonSinkOutput::stop";
|
2017-05-14 22:58:56 +00:00
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
if(m_sdrDaemonSinkThread != 0)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->stopWork();
|
|
|
|
delete m_sdrDaemonSinkThread;
|
|
|
|
m_sdrDaemonSinkThread = 0;
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
const QString& SDRdaemonSinkOutput::getDeviceDescription() const
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
return m_deviceDescription;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
int SDRdaemonSinkOutput::getSampleRate() const
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
return m_settings.m_sampleRate;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
quint64 SDRdaemonSinkOutput::getCenterFrequency() const
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
return m_settings.m_centerFrequency;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
std::time_t SDRdaemonSinkOutput::getStartingTimeStamp() const
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
return m_startingTimeStamp;
|
|
|
|
}
|
|
|
|
|
2017-05-21 02:19:12 +00:00
|
|
|
bool SDRdaemonSinkOutput::handleMessage(const Message& message)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-22 23:41:30 +00:00
|
|
|
|
|
|
|
if (MsgConfigureSDRdaemonSink::match(message))
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-22 23:41:30 +00:00
|
|
|
qDebug() << "SDRdaemonSinkOutput::handleMessage:" << message.getIdentifier();
|
2017-05-21 02:19:12 +00:00
|
|
|
MsgConfigureSDRdaemonSink& conf = (MsgConfigureSDRdaemonSink&) message;
|
2017-05-22 23:41:30 +00:00
|
|
|
applySettings(conf.getSettings(), conf.getForce());
|
2017-05-14 22:58:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-21 02:19:12 +00:00
|
|
|
else if (MsgConfigureSDRdaemonSinkWork::match(message))
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 02:19:12 +00:00
|
|
|
MsgConfigureSDRdaemonSinkWork& conf = (MsgConfigureSDRdaemonSinkWork&) message;
|
2017-05-14 22:58:56 +00:00
|
|
|
bool working = conf.isWorking();
|
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
if (working)
|
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->startWork();
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->stopWork();
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-14 17:02:49 +00:00
|
|
|
else if (MsgStartStop::match(message))
|
|
|
|
{
|
|
|
|
MsgStartStop& cmd = (MsgStartStop&) message;
|
|
|
|
qDebug() << "SDRdaemonSinkOutput::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop");
|
|
|
|
|
|
|
|
if (cmd.getStartStop())
|
|
|
|
{
|
|
|
|
if (m_deviceAPI->initGeneration())
|
|
|
|
{
|
|
|
|
m_deviceAPI->startGeneration();
|
|
|
|
DSPEngine::instance()->startAudioInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_deviceAPI->stopGeneration();
|
|
|
|
DSPEngine::instance()->stopAudioInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-21 02:19:12 +00:00
|
|
|
else if (MsgConfigureSDRdaemonSinkStreamTiming::match(message))
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 02:19:12 +00:00
|
|
|
MsgReportSDRdaemonSinkStreamTiming *report;
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-10-14 05:40:16 +00:00
|
|
|
if (m_sdrDaemonSinkThread != 0 && getMessageQueueToGUI())
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
report = MsgReportSDRdaemonSinkStreamTiming::create(m_sdrDaemonSinkThread->getSamplesCount());
|
2017-10-14 05:34:00 +00:00
|
|
|
getMessageQueueToGUI()->push(report);
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-04 23:18:35 +00:00
|
|
|
else if (MsgConfigureSDRdaemonSinkChunkCorrection::match(message))
|
|
|
|
{
|
|
|
|
MsgConfigureSDRdaemonSinkChunkCorrection& conf = (MsgConfigureSDRdaemonSinkChunkCorrection&) message;
|
|
|
|
|
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
|
|
|
{
|
|
|
|
m_sdrDaemonSinkThread->setChunkCorrection(conf.getChunkCorrection());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-14 22:58:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
void SDRdaemonSinkOutput::applySettings(const SDRdaemonSinkSettings& settings, bool force)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
bool forwardChange = false;
|
2017-06-07 22:01:56 +00:00
|
|
|
bool changeTxDelay = false;
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-05-21 18:13:17 +00:00
|
|
|
if (force || (m_settings.m_address != settings.m_address) || (m_settings.m_dataPort != settings.m_dataPort))
|
|
|
|
{
|
|
|
|
m_settings.m_address = settings.m_address;
|
|
|
|
m_settings.m_dataPort = settings.m_dataPort;
|
|
|
|
|
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
|
|
|
{
|
|
|
|
m_sdrDaemonSinkThread->setRemoteAddress(m_settings.m_address, m_settings.m_dataPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 22:58:56 +00:00
|
|
|
if (force || (m_settings.m_centerFrequency != settings.m_centerFrequency))
|
|
|
|
{
|
|
|
|
m_settings.m_centerFrequency = settings.m_centerFrequency;
|
2017-05-21 18:13:17 +00:00
|
|
|
|
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
|
|
|
{
|
|
|
|
m_sdrDaemonSinkThread->setCenterFrequency(m_settings.m_centerFrequency);
|
|
|
|
}
|
|
|
|
|
2017-05-14 22:58:56 +00:00
|
|
|
forwardChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force || (m_settings.m_sampleRate != settings.m_sampleRate))
|
|
|
|
{
|
|
|
|
m_settings.m_sampleRate = settings.m_sampleRate;
|
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-05-21 10:58:57 +00:00
|
|
|
m_sdrDaemonSinkThread->setSamplerate(m_settings.m_sampleRate);
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forwardChange = true;
|
2017-06-07 22:01:56 +00:00
|
|
|
changeTxDelay = true;
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (force || (m_settings.m_log2Interp != settings.m_log2Interp))
|
|
|
|
{
|
|
|
|
m_settings.m_log2Interp = settings.m_log2Interp;
|
2017-05-21 18:13:17 +00:00
|
|
|
forwardChange = true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 22:01:56 +00:00
|
|
|
if (force || (m_settings.m_nbFECBlocks != settings.m_nbFECBlocks))
|
2017-05-21 18:13:17 +00:00
|
|
|
{
|
2017-06-07 22:01:56 +00:00
|
|
|
m_settings.m_nbFECBlocks = settings.m_nbFECBlocks;
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-05-21 10:58:57 +00:00
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
2017-05-14 22:58:56 +00:00
|
|
|
{
|
2017-06-07 22:01:56 +00:00
|
|
|
m_sdrDaemonSinkThread->setNbBlocksFEC(m_settings.m_nbFECBlocks);
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
2017-06-07 22:01:56 +00:00
|
|
|
|
|
|
|
changeTxDelay = true;
|
2017-05-21 18:13:17 +00:00
|
|
|
}
|
2017-05-14 22:58:56 +00:00
|
|
|
|
2017-06-07 22:01:56 +00:00
|
|
|
if (force || (m_settings.m_txDelay != settings.m_txDelay))
|
2017-05-21 18:13:17 +00:00
|
|
|
{
|
2017-06-07 22:01:56 +00:00
|
|
|
m_settings.m_txDelay = settings.m_txDelay;
|
|
|
|
changeTxDelay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changeTxDelay)
|
|
|
|
{
|
|
|
|
double delay = ((127*127*m_settings.m_txDelay) / m_settings.m_sampleRate)/(128 + m_settings.m_nbFECBlocks);
|
|
|
|
qDebug("SDRdaemonSinkOutput::applySettings: Tx delay: %f us", delay*1e6);
|
2017-05-21 18:13:17 +00:00
|
|
|
|
|
|
|
if (m_sdrDaemonSinkThread != 0)
|
|
|
|
{
|
2017-06-07 22:01:56 +00:00
|
|
|
// delay is calculated as a fraction of the nominal UDP block process time
|
|
|
|
// frame size: 127 * 127 samples
|
|
|
|
// divided by sample rate gives the frame process time
|
|
|
|
// divided by the number of actual blocks including FEC blocks gives the block (i.e. UDP block) process time
|
|
|
|
m_sdrDaemonSinkThread->setTxDelay((int) (delay*1e6));
|
2017-05-21 18:13:17 +00:00
|
|
|
}
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 18:13:17 +00:00
|
|
|
mutexLocker.unlock();
|
|
|
|
|
2017-06-07 22:01:56 +00:00
|
|
|
qDebug("SDRdaemonSinkOutput::applySettings: %s m_centerFrequency: %llu m_sampleRate: %llu m_log2Interp: %d m_txDelay: %f m_nbFECBlocks: %d",
|
2017-05-21 18:13:17 +00:00
|
|
|
forwardChange ? "forward change" : "",
|
|
|
|
m_settings.m_centerFrequency,
|
|
|
|
m_settings.m_sampleRate,
|
|
|
|
m_settings.m_log2Interp,
|
|
|
|
m_settings.m_txDelay,
|
|
|
|
m_settings.m_nbFECBlocks);
|
2017-05-21 10:58:57 +00:00
|
|
|
|
2017-05-14 22:58:56 +00:00
|
|
|
if (forwardChange)
|
|
|
|
{
|
|
|
|
DSPSignalNotification *notif = new DSPSignalNotification(m_settings.m_sampleRate, m_settings.m_centerFrequency);
|
2017-09-13 21:42:28 +00:00
|
|
|
m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif);
|
2017-05-14 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|