2016-10-19 12:42:57 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 //
|
2019-04-11 00:39:30 -04:00
|
|
|
// (at your option) any later version. //
|
2016-10-19 12:42:57 -04:00
|
|
|
// //
|
|
|
|
// 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 <errno.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2017-12-14 12:22:45 -05:00
|
|
|
#include "SWGDeviceSettings.h"
|
|
|
|
#include "SWGDeviceState.h"
|
|
|
|
|
2016-10-19 12:42:57 -04:00
|
|
|
#include "util/simpleserializer.h"
|
|
|
|
#include "dsp/dspcommands.h"
|
|
|
|
#include "dsp/dspengine.h"
|
|
|
|
#include "dsp/filerecord.h"
|
|
|
|
|
2019-05-08 16:11:53 -04:00
|
|
|
#include "device/deviceapi.h"
|
2016-10-22 15:47:59 -04:00
|
|
|
|
2016-10-19 12:42:57 -04:00
|
|
|
#include "filesinkoutput.h"
|
2020-07-12 04:22:24 -04:00
|
|
|
#include "filesinkworker.h"
|
2016-10-19 12:42:57 -04:00
|
|
|
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgConfigureFileSink, Message)
|
2017-12-13 18:38:22 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgStartStop, Message)
|
2016-10-19 12:42:57 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgConfigureFileSinkName, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgConfigureFileSinkWork, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgConfigureFileSinkStreamTiming, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgReportFileSinkGeneration, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(FileSinkOutput::MsgReportFileSinkStreamTiming, Message)
|
|
|
|
|
2019-05-08 16:11:53 -04:00
|
|
|
FileSinkOutput::FileSinkOutput(DeviceAPI *deviceAPI) :
|
2016-10-22 15:47:59 -04:00
|
|
|
m_deviceAPI(deviceAPI),
|
2016-10-19 12:42:57 -04:00
|
|
|
m_settings(),
|
2020-07-12 04:22:24 -04:00
|
|
|
m_fileSinkWorker(nullptr),
|
2016-10-23 04:38:44 -04:00
|
|
|
m_deviceDescription("FileSink"),
|
|
|
|
m_fileName("./test.sdriq"),
|
2016-10-19 12:42:57 -04:00
|
|
|
m_startingTimeStamp(0),
|
2017-09-14 07:55:57 -04:00
|
|
|
m_masterTimer(deviceAPI->getMasterTimer())
|
2016-10-19 12:42:57 -04:00
|
|
|
{
|
2019-05-20 18:27:08 -04:00
|
|
|
m_deviceAPI->setNbSinkStreams(1);
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSinkOutput::~FileSinkOutput()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2017-09-16 05:34:25 -04:00
|
|
|
void FileSinkOutput::destroy()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:42:57 -04:00
|
|
|
void FileSinkOutput::openFileStream()
|
|
|
|
{
|
|
|
|
if (m_ofstream.is_open()) {
|
|
|
|
m_ofstream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ofstream.open(m_fileName.toStdString().c_str(), std::ios::binary);
|
|
|
|
|
2018-10-10 08:05:21 -04:00
|
|
|
FileRecord::Header header;
|
2017-01-01 21:14:46 -05:00
|
|
|
int actualSampleRate = m_settings.m_sampleRate * (1<<m_settings.m_log2Interp);
|
2018-10-10 08:05:21 -04:00
|
|
|
header.sampleRate = actualSampleRate;
|
|
|
|
header.centerFrequency = m_settings.m_centerFrequency;
|
2016-10-20 19:21:17 -04:00
|
|
|
m_startingTimeStamp = time(0);
|
2018-10-10 08:05:21 -04:00
|
|
|
header.startTimeStamp = m_startingTimeStamp;
|
|
|
|
header.sampleSize = SDR_RX_SAMP_SZ;
|
|
|
|
|
|
|
|
FileRecord::writeHeader(m_ofstream, header);
|
2016-10-19 12:42:57 -04:00
|
|
|
|
|
|
|
qDebug() << "FileSinkOutput::openFileStream: " << m_fileName.toStdString().c_str();
|
|
|
|
}
|
|
|
|
|
2017-12-25 06:59:44 -05:00
|
|
|
void FileSinkOutput::init()
|
|
|
|
{
|
|
|
|
applySettings(m_settings, true);
|
|
|
|
}
|
|
|
|
|
2017-04-13 21:44:49 -04:00
|
|
|
bool FileSinkOutput::start()
|
2016-10-19 12:42:57 -04:00
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
qDebug() << "FileSinkOutput::start";
|
|
|
|
|
2016-10-23 04:54:54 -04:00
|
|
|
openFileStream();
|
2016-10-19 12:42:57 -04:00
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
m_fileSinkWorker = new FileSinkWorker(&m_ofstream, &m_sampleSourceFifo);
|
|
|
|
m_fileSinkWorker->moveToThread(&m_fileSinkWorkerThread);
|
|
|
|
m_fileSinkWorker->setSamplerate(m_settings.m_sampleRate);
|
|
|
|
m_fileSinkWorker->setLog2Interpolation(m_settings.m_log2Interp);
|
|
|
|
m_fileSinkWorker->connectTimer(m_masterTimer);
|
|
|
|
startWorker();
|
2016-10-19 12:42:57 -04:00
|
|
|
|
|
|
|
mutexLocker.unlock();
|
|
|
|
//applySettings(m_generalSettings, m_settings, true);
|
|
|
|
qDebug("FileSinkOutput::start: started");
|
|
|
|
|
2017-10-14 01:40:16 -04:00
|
|
|
if (getMessageQueueToGUI())
|
|
|
|
{
|
|
|
|
MsgReportFileSinkGeneration *report = MsgReportFileSinkGeneration::create(true); // acquisition on
|
|
|
|
getMessageQueueToGUI()->push(report);
|
|
|
|
}
|
2016-10-19 12:42:57 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSinkOutput::stop()
|
|
|
|
{
|
|
|
|
qDebug() << "FileSourceInput::stop";
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
if (m_fileSinkWorker)
|
2016-10-19 12:42:57 -04:00
|
|
|
{
|
2020-07-12 04:22:24 -04:00
|
|
|
stopWorker();
|
|
|
|
delete m_fileSinkWorker;
|
|
|
|
m_fileSinkWorker = nullptr;
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
2016-10-23 04:54:54 -04:00
|
|
|
if (m_ofstream.is_open()) {
|
|
|
|
m_ofstream.close();
|
|
|
|
}
|
|
|
|
|
2017-10-14 01:40:16 -04:00
|
|
|
if (getMessageQueueToGUI())
|
|
|
|
{
|
|
|
|
MsgReportFileSinkGeneration *report = MsgReportFileSinkGeneration::create(false); // acquisition off
|
|
|
|
getMessageQueueToGUI()->push(report);
|
|
|
|
}
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-12 06:51:38 -04:00
|
|
|
void FileSinkOutput::startWorker()
|
|
|
|
{
|
|
|
|
m_fileSinkWorker->startWork();
|
|
|
|
m_fileSinkWorkerThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileSinkOutput::stopWorker()
|
|
|
|
{
|
|
|
|
m_fileSinkWorker->stopWork();
|
|
|
|
m_fileSinkWorkerThread.quit();
|
|
|
|
m_fileSinkWorkerThread.wait();
|
|
|
|
}
|
|
|
|
|
2017-12-27 22:04:50 -05:00
|
|
|
QByteArray FileSinkOutput::serialize() const
|
|
|
|
{
|
|
|
|
return m_settings.serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSinkOutput::deserialize(const QByteArray& data)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
if (!m_settings.deserialize(data))
|
|
|
|
{
|
|
|
|
m_settings.resetToDefaults();
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MsgConfigureFileSink* message = MsgConfigureFileSink::create(m_settings, true);
|
|
|
|
m_inputMessageQueue.push(message);
|
|
|
|
|
|
|
|
if (m_guiMessageQueue)
|
|
|
|
{
|
|
|
|
MsgConfigureFileSink* messageToGUI = MsgConfigureFileSink::create(m_settings, true);
|
|
|
|
m_guiMessageQueue->push(messageToGUI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:42:57 -04:00
|
|
|
const QString& FileSinkOutput::getDeviceDescription() const
|
|
|
|
{
|
|
|
|
return m_deviceDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileSinkOutput::getSampleRate() const
|
|
|
|
{
|
2016-10-23 04:38:44 -04:00
|
|
|
return m_settings.m_sampleRate;
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
quint64 FileSinkOutput::getCenterFrequency() const
|
|
|
|
{
|
2016-10-23 04:38:44 -04:00
|
|
|
return m_settings.m_centerFrequency;
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:04:50 -05:00
|
|
|
void FileSinkOutput::setCenterFrequency(qint64 centerFrequency)
|
|
|
|
{
|
|
|
|
FileSinkSettings settings = m_settings;
|
|
|
|
settings.m_centerFrequency = centerFrequency;
|
|
|
|
|
|
|
|
MsgConfigureFileSink* message = MsgConfigureFileSink::create(settings, false);
|
|
|
|
m_inputMessageQueue.push(message);
|
|
|
|
|
|
|
|
if (m_guiMessageQueue)
|
|
|
|
{
|
|
|
|
MsgConfigureFileSink* messageToGUI = MsgConfigureFileSink::create(settings, false);
|
|
|
|
m_guiMessageQueue->push(messageToGUI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:42:57 -04:00
|
|
|
std::time_t FileSinkOutput::getStartingTimeStamp() const
|
|
|
|
{
|
|
|
|
return m_startingTimeStamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSinkOutput::handleMessage(const Message& message)
|
|
|
|
{
|
|
|
|
if (MsgConfigureFileSinkName::match(message))
|
|
|
|
{
|
|
|
|
MsgConfigureFileSinkName& conf = (MsgConfigureFileSinkName&) message;
|
|
|
|
m_fileName = conf.getFileName();
|
|
|
|
openFileStream();
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-13 18:38:22 -05:00
|
|
|
else if (MsgStartStop::match(message))
|
|
|
|
{
|
|
|
|
MsgStartStop& cmd = (MsgStartStop&) message;
|
|
|
|
qDebug() << "FileSinkOutput::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop");
|
|
|
|
|
|
|
|
if (cmd.getStartStop())
|
|
|
|
{
|
2019-05-08 16:11:53 -04:00
|
|
|
if (m_deviceAPI->initDeviceEngine())
|
2017-12-13 18:38:22 -05:00
|
|
|
{
|
2019-05-08 16:11:53 -04:00
|
|
|
m_deviceAPI->startDeviceEngine();
|
2017-12-13 18:38:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-08 16:11:53 -04:00
|
|
|
m_deviceAPI->stopDeviceEngine();
|
2017-12-13 18:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-20 19:21:17 -04:00
|
|
|
else if (MsgConfigureFileSink::match(message))
|
|
|
|
{
|
2016-10-22 20:22:00 -04:00
|
|
|
qDebug() << "FileSinkOutput::handleMessage: MsgConfigureFileSink";
|
2016-10-20 19:21:17 -04:00
|
|
|
MsgConfigureFileSink& conf = (MsgConfigureFileSink&) message;
|
2017-10-14 01:28:57 -04:00
|
|
|
applySettings(conf.getSettings(), conf.getForce());
|
2016-10-20 19:21:17 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-19 12:42:57 -04:00
|
|
|
else if (MsgConfigureFileSinkWork::match(message))
|
|
|
|
{
|
|
|
|
MsgConfigureFileSinkWork& conf = (MsgConfigureFileSinkWork&) message;
|
|
|
|
bool working = conf.isWorking();
|
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
if (m_fileSinkWorker != 0)
|
2016-10-19 12:42:57 -04:00
|
|
|
{
|
2020-07-12 04:22:24 -04:00
|
|
|
if (working) {
|
|
|
|
startWorker();
|
|
|
|
} else {
|
|
|
|
stopWorker();
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (MsgConfigureFileSinkStreamTiming::match(message))
|
|
|
|
{
|
2016-10-23 06:22:52 -04:00
|
|
|
MsgReportFileSinkStreamTiming *report;
|
2016-10-19 12:42:57 -04:00
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
if (m_fileSinkWorker != 0 && getMessageQueueToGUI())
|
2016-10-19 12:42:57 -04:00
|
|
|
{
|
2020-07-12 04:22:24 -04:00
|
|
|
report = MsgReportFileSinkStreamTiming::create(m_fileSinkWorker->getSamplesCount());
|
2017-10-14 01:34:00 -04:00
|
|
|
getMessageQueueToGUI()->push(report);
|
2016-10-19 12:42:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 19:21:17 -04:00
|
|
|
|
|
|
|
void FileSinkOutput::applySettings(const FileSinkSettings& settings, bool force)
|
|
|
|
{
|
|
|
|
QMutexLocker mutexLocker(&m_mutex);
|
2016-10-22 15:47:59 -04:00
|
|
|
bool forwardChange = false;
|
2016-10-20 19:21:17 -04:00
|
|
|
|
|
|
|
if (force || (m_settings.m_centerFrequency != settings.m_centerFrequency))
|
|
|
|
{
|
|
|
|
m_settings.m_centerFrequency = settings.m_centerFrequency;
|
2016-10-22 15:47:59 -04:00
|
|
|
forwardChange = true;
|
2016-10-20 19:21:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (force || (m_settings.m_sampleRate != settings.m_sampleRate))
|
|
|
|
{
|
|
|
|
m_settings.m_sampleRate = settings.m_sampleRate;
|
2016-10-23 04:38:44 -04:00
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
if (m_fileSinkWorker != 0)
|
2016-10-23 04:38:44 -04:00
|
|
|
{
|
2020-07-12 04:22:24 -04:00
|
|
|
m_fileSinkWorker->setSamplerate(m_settings.m_sampleRate);
|
2016-10-23 04:38:44 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 15:47:59 -04:00
|
|
|
forwardChange = true;
|
2016-10-20 19:21:17 -04:00
|
|
|
}
|
2016-10-22 15:47:59 -04:00
|
|
|
|
2017-01-01 21:14:46 -05:00
|
|
|
if (force || (m_settings.m_log2Interp != settings.m_log2Interp))
|
|
|
|
{
|
|
|
|
m_settings.m_log2Interp = settings.m_log2Interp;
|
|
|
|
|
2020-07-12 04:22:24 -04:00
|
|
|
if (m_fileSinkWorker != 0)
|
2017-01-01 21:14:46 -05:00
|
|
|
{
|
2020-07-12 04:22:24 -04:00
|
|
|
m_fileSinkWorker->setLog2Interpolation(m_settings.m_log2Interp);
|
2017-01-01 21:14:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
forwardChange = true;
|
|
|
|
}
|
|
|
|
|
2016-10-22 15:47:59 -04:00
|
|
|
if (forwardChange)
|
|
|
|
{
|
2017-04-06 14:13:31 -04:00
|
|
|
qDebug("FileSinkOutput::applySettings: forward: m_centerFrequency: %llu m_sampleRate: %llu m_log2Interp: %d",
|
2016-10-22 20:22:00 -04:00
|
|
|
m_settings.m_centerFrequency,
|
2017-01-01 21:14:46 -05:00
|
|
|
m_settings.m_sampleRate,
|
|
|
|
m_settings.m_log2Interp);
|
2016-10-22 15:47:59 -04:00
|
|
|
DSPSignalNotification *notif = new DSPSignalNotification(m_settings.m_sampleRate, m_settings.m_centerFrequency);
|
2017-09-13 17:42:28 -04:00
|
|
|
m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif);
|
2016-10-22 15:47:59 -04:00
|
|
|
}
|
|
|
|
|
2016-10-20 19:21:17 -04:00
|
|
|
}
|
2017-12-14 12:22:45 -05:00
|
|
|
|
|
|
|
int FileSinkOutput::webapiRunGet(
|
|
|
|
SWGSDRangel::SWGDeviceState& response,
|
2018-11-13 02:51:14 -05:00
|
|
|
QString& errorMessage)
|
2017-12-14 12:22:45 -05:00
|
|
|
{
|
2018-11-13 02:51:14 -05:00
|
|
|
(void) errorMessage;
|
2017-12-14 12:22:45 -05:00
|
|
|
m_deviceAPI->getDeviceEngineStateStr(*response.getState());
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileSinkOutput::webapiRun(
|
|
|
|
bool run,
|
|
|
|
SWGSDRangel::SWGDeviceState& response,
|
2018-11-13 02:51:14 -05:00
|
|
|
QString& errorMessage)
|
2017-12-14 12:22:45 -05:00
|
|
|
{
|
2018-11-13 02:51:14 -05:00
|
|
|
(void) errorMessage;
|
2017-12-14 17:29:12 -05:00
|
|
|
m_deviceAPI->getDeviceEngineStateStr(*response.getState());
|
2017-12-14 12:22:45 -05:00
|
|
|
MsgStartStop *message = MsgStartStop::create(run);
|
|
|
|
m_inputMessageQueue.push(message);
|
|
|
|
|
|
|
|
if (m_guiMessageQueue)
|
|
|
|
{
|
|
|
|
MsgStartStop *messagetoGui = MsgStartStop::create(run);
|
|
|
|
m_guiMessageQueue->push(messagetoGui);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
|