1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-23 16:38:37 -05:00
sdrangel/plugins/samplesink/fileoutput/fileoutput.cpp

360 lines
9.8 KiB
C++
Raw Normal View History

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>
#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"
2020-08-03 18:29:15 -04:00
#include "fileoutput.h"
#include "fileoutputworker.h"
MESSAGE_CLASS_DEFINITION(FileOutput::MsgConfigureFileOutput, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgStartStop, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgConfigureFileOutputName, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgConfigureFileOutputWork, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgConfigureFileOutputStreamTiming, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgReportFileOutputGeneration, Message)
MESSAGE_CLASS_DEFINITION(FileOutput::MsgReportFileOutputStreamTiming, Message)
FileOutput::FileOutput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
2016-10-19 12:42:57 -04:00
m_settings(),
2020-08-03 18:29:15 -04:00
m_fileOutputWorker(nullptr),
m_deviceDescription("FileOutput"),
m_fileName("./test.sdriq"),
2016-10-19 12:42:57 -04:00
m_startingTimeStamp(0),
m_masterTimer(deviceAPI->getMasterTimer())
2016-10-19 12:42:57 -04:00
{
m_deviceAPI->setNbSinkStreams(1);
2016-10-19 12:42:57 -04:00
}
2020-08-03 18:29:15 -04:00
FileOutput::~FileOutput()
2016-10-19 12:42:57 -04:00
{
stop();
}
2020-08-03 18:29:15 -04:00
void FileOutput::destroy()
{
delete this;
}
2020-08-03 18:29:15 -04:00
void FileOutput::openFileStream()
2016-10-19 12:42:57 -04:00
{
if (m_ofstream.is_open()) {
m_ofstream.close();
}
m_ofstream.open(m_fileName.toStdString().c_str(), std::ios::binary);
FileRecord::Header header;
int actualSampleRate = m_settings.m_sampleRate * (1<<m_settings.m_log2Interp);
header.sampleRate = actualSampleRate;
header.centerFrequency = m_settings.m_centerFrequency;
m_startingTimeStamp = time(0);
header.startTimeStamp = m_startingTimeStamp;
header.sampleSize = SDR_RX_SAMP_SZ;
FileRecord::writeHeader(m_ofstream, header);
2016-10-19 12:42:57 -04:00
2020-08-03 18:29:15 -04:00
qDebug() << "FileOutput::openFileStream: " << m_fileName.toStdString().c_str();
2016-10-19 12:42:57 -04:00
}
2020-08-03 18:29:15 -04:00
void FileOutput::init()
{
applySettings(m_settings, true);
}
2020-08-03 18:29:15 -04:00
bool FileOutput::start()
2016-10-19 12:42:57 -04:00
{
QMutexLocker mutexLocker(&m_mutex);
2020-08-03 18:29:15 -04:00
qDebug() << "FileOutput::start";
2016-10-19 12:42:57 -04:00
openFileStream();
2016-10-19 12:42:57 -04:00
2020-08-03 18:29:15 -04:00
m_fileOutputWorker = new FileOutputWorker(&m_ofstream, &m_sampleSourceFifo);
m_fileOutputWorker->moveToThread(&m_fileOutputWorkerThread);
m_fileOutputWorker->setSamplerate(m_settings.m_sampleRate);
m_fileOutputWorker->setLog2Interpolation(m_settings.m_log2Interp);
m_fileOutputWorker->connectTimer(m_masterTimer);
startWorker();
2016-10-19 12:42:57 -04:00
mutexLocker.unlock();
//applySettings(m_generalSettings, m_settings, true);
2020-08-03 18:29:15 -04:00
qDebug("FileOutput::start: started");
2016-10-19 12:42:57 -04:00
if (getMessageQueueToGUI())
{
2020-08-03 18:29:15 -04:00
MsgReportFileOutputGeneration *report = MsgReportFileOutputGeneration::create(true); // acquisition on
getMessageQueueToGUI()->push(report);
}
2016-10-19 12:42:57 -04:00
return true;
}
2020-08-03 18:29:15 -04:00
void FileOutput::stop()
2016-10-19 12:42:57 -04:00
{
qDebug() << "FileSourceInput::stop";
QMutexLocker mutexLocker(&m_mutex);
2020-08-03 18:29:15 -04:00
if (m_fileOutputWorker)
2016-10-19 12:42:57 -04:00
{
stopWorker();
2020-08-03 18:29:15 -04:00
delete m_fileOutputWorker;
m_fileOutputWorker = nullptr;
2016-10-19 12:42:57 -04:00
}
if (m_ofstream.is_open()) {
m_ofstream.close();
}
if (getMessageQueueToGUI())
{
2020-08-03 18:29:15 -04:00
MsgReportFileOutputGeneration *report = MsgReportFileOutputGeneration::create(false); // acquisition off
getMessageQueueToGUI()->push(report);
}
2016-10-19 12:42:57 -04:00
}
2020-08-03 18:29:15 -04:00
void FileOutput::startWorker()
2020-07-12 06:51:38 -04:00
{
2020-08-03 18:29:15 -04:00
m_fileOutputWorker->startWork();
m_fileOutputWorkerThread.start();
2020-07-12 06:51:38 -04:00
}
2020-08-03 18:29:15 -04:00
void FileOutput::stopWorker()
2020-07-12 06:51:38 -04:00
{
2020-08-03 18:29:15 -04:00
m_fileOutputWorker->stopWork();
m_fileOutputWorkerThread.quit();
m_fileOutputWorkerThread.wait();
2020-07-12 06:51:38 -04:00
}
2020-08-03 18:29:15 -04:00
QByteArray FileOutput::serialize() const
{
return m_settings.serialize();
}
2020-08-03 18:29:15 -04:00
bool FileOutput::deserialize(const QByteArray& data)
{
bool success = true;
if (!m_settings.deserialize(data))
{
m_settings.resetToDefaults();
success = false;
}
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutput* message = MsgConfigureFileOutput::create(m_settings, true);
m_inputMessageQueue.push(message);
if (m_guiMessageQueue)
{
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutput* messageToGUI = MsgConfigureFileOutput::create(m_settings, true);
m_guiMessageQueue->push(messageToGUI);
}
return success;
}
2020-08-03 18:29:15 -04:00
const QString& FileOutput::getDeviceDescription() const
2016-10-19 12:42:57 -04:00
{
return m_deviceDescription;
}
2020-08-03 18:29:15 -04:00
int FileOutput::getSampleRate() const
2016-10-19 12:42:57 -04:00
{
return m_settings.m_sampleRate;
2016-10-19 12:42:57 -04:00
}
2020-08-03 18:29:15 -04:00
quint64 FileOutput::getCenterFrequency() const
2016-10-19 12:42:57 -04:00
{
return m_settings.m_centerFrequency;
2016-10-19 12:42:57 -04:00
}
2020-08-03 18:29:15 -04:00
void FileOutput::setCenterFrequency(qint64 centerFrequency)
{
2020-08-03 18:29:15 -04:00
FileOutputSettings settings = m_settings;
settings.m_centerFrequency = centerFrequency;
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutput* message = MsgConfigureFileOutput::create(settings, false);
m_inputMessageQueue.push(message);
if (m_guiMessageQueue)
{
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutput* messageToGUI = MsgConfigureFileOutput::create(settings, false);
m_guiMessageQueue->push(messageToGUI);
}
}
2020-08-03 18:29:15 -04:00
std::time_t FileOutput::getStartingTimeStamp() const
2016-10-19 12:42:57 -04:00
{
return m_startingTimeStamp;
}
2020-08-03 18:29:15 -04:00
bool FileOutput::handleMessage(const Message& message)
2016-10-19 12:42:57 -04:00
{
2020-08-03 18:29:15 -04:00
if (MsgConfigureFileOutputName::match(message))
2016-10-19 12:42:57 -04:00
{
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutputName& conf = (MsgConfigureFileOutputName&) message;
2016-10-19 12:42:57 -04:00
m_fileName = conf.getFileName();
openFileStream();
return true;
}
else if (MsgStartStop::match(message))
{
MsgStartStop& cmd = (MsgStartStop&) message;
2020-08-03 18:29:15 -04:00
qDebug() << "FileOutput::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop");
if (cmd.getStartStop())
{
2019-05-08 16:11:53 -04:00
if (m_deviceAPI->initDeviceEngine())
{
2019-05-08 16:11:53 -04:00
m_deviceAPI->startDeviceEngine();
}
}
else
{
2019-05-08 16:11:53 -04:00
m_deviceAPI->stopDeviceEngine();
}
return true;
}
2020-08-03 18:29:15 -04:00
else if (MsgConfigureFileOutput::match(message))
{
2020-08-03 18:29:15 -04:00
qDebug() << "FileOutput::handleMessage: MsgConfigureFileOutput";
MsgConfigureFileOutput& conf = (MsgConfigureFileOutput&) message;
applySettings(conf.getSettings(), conf.getForce());
return true;
}
2020-08-03 18:29:15 -04:00
else if (MsgConfigureFileOutputWork::match(message))
2016-10-19 12:42:57 -04:00
{
2020-08-03 18:29:15 -04:00
MsgConfigureFileOutputWork& conf = (MsgConfigureFileOutputWork&) message;
2016-10-19 12:42:57 -04:00
bool working = conf.isWorking();
2020-08-03 18:29:15 -04:00
if (m_fileOutputWorker != 0)
2016-10-19 12:42:57 -04:00
{
if (working) {
startWorker();
} else {
stopWorker();
2016-10-19 12:42:57 -04:00
}
}
return true;
}
2020-08-03 18:29:15 -04:00
else if (MsgConfigureFileOutputStreamTiming::match(message))
2016-10-19 12:42:57 -04:00
{
2020-08-03 18:29:15 -04:00
MsgReportFileOutputStreamTiming *report;
2016-10-19 12:42:57 -04:00
2020-08-03 18:29:15 -04:00
if (m_fileOutputWorker != 0 && getMessageQueueToGUI())
2016-10-19 12:42:57 -04:00
{
2020-08-03 18:29:15 -04:00
report = MsgReportFileOutputStreamTiming::create(m_fileOutputWorker->getSamplesCount());
getMessageQueueToGUI()->push(report);
2016-10-19 12:42:57 -04:00
}
return true;
}
else
{
return false;
}
}
2020-08-03 18:29:15 -04:00
void FileOutput::applySettings(const FileOutputSettings& settings, bool force)
{
QMutexLocker mutexLocker(&m_mutex);
bool forwardChange = false;
if (force || (m_settings.m_centerFrequency != settings.m_centerFrequency))
{
m_settings.m_centerFrequency = settings.m_centerFrequency;
forwardChange = true;
}
if (force || (m_settings.m_sampleRate != settings.m_sampleRate))
{
m_settings.m_sampleRate = settings.m_sampleRate;
2020-08-03 18:29:15 -04:00
if (m_fileOutputWorker != 0)
{
2020-08-03 18:29:15 -04:00
m_fileOutputWorker->setSamplerate(m_settings.m_sampleRate);
}
forwardChange = true;
}
if (force || (m_settings.m_log2Interp != settings.m_log2Interp))
{
m_settings.m_log2Interp = settings.m_log2Interp;
2020-08-03 18:29:15 -04:00
if (m_fileOutputWorker != 0)
{
2020-08-03 18:29:15 -04:00
m_fileOutputWorker->setLog2Interpolation(m_settings.m_log2Interp);
}
forwardChange = true;
}
if (forwardChange)
{
2020-08-03 18:29:15 -04:00
qDebug("FileOutput::applySettings: forward: m_centerFrequency: %llu m_sampleRate: %llu m_log2Interp: %d",
m_settings.m_centerFrequency,
m_settings.m_sampleRate,
m_settings.m_log2Interp);
DSPSignalNotification *notif = new DSPSignalNotification(m_settings.m_sampleRate, m_settings.m_centerFrequency);
m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif);
}
}
2020-08-03 18:29:15 -04:00
int FileOutput::webapiRunGet(
SWGSDRangel::SWGDeviceState& response,
QString& errorMessage)
{
(void) errorMessage;
m_deviceAPI->getDeviceEngineStateStr(*response.getState());
return 200;
}
2020-08-03 18:29:15 -04:00
int FileOutput::webapiRun(
bool run,
SWGSDRangel::SWGDeviceState& response,
QString& errorMessage)
{
(void) errorMessage;
m_deviceAPI->getDeviceEngineStateStr(*response.getState());
MsgStartStop *message = MsgStartStop::create(run);
m_inputMessageQueue.push(message);
if (m_guiMessageQueue)
{
MsgStartStop *messagetoGui = MsgStartStop::create(run);
m_guiMessageQueue->push(messagetoGui);
}
return 200;
}