1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-13 20:01:46 -05:00

Merge pull request #2136 from mxi-box/sigmf_fix

Fix Sigmf Record: overwriting sigmf-data or sigmf-meta
This commit is contained in:
Edouard Griffiths 2024-05-30 00:23:24 +02:00 committed by GitHub
commit c913b20a27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 40 deletions

View File

@ -83,11 +83,11 @@ SigMFFileRecord::~SigMFFileRecord()
stopRecording(); stopRecording();
if (m_metaFile.is_open()) { if (m_metaFile.isOpen()) {
m_metaFile.close(); m_metaFile.close();
} }
if (m_sampleFile.is_open()) { if (m_sampleFile.isOpen()) {
m_sampleFile.close(); m_sampleFile.close();
} }
@ -100,37 +100,33 @@ void SigMFFileRecord::setFileName(const QString& fileName)
{ {
qDebug("SigMFFileRecord::setFileName: %s", qPrintable(fileName)); qDebug("SigMFFileRecord::setFileName: %s", qPrintable(fileName));
if (m_metaFile.is_open()) { if (m_metaFile.isOpen()) {
m_metaFile.close(); m_metaFile.close();
} }
if (m_sampleFile.is_open()) { if (m_sampleFile.isOpen()) {
m_sampleFile.close(); m_sampleFile.close();
} }
m_fileName = fileName; m_fileName = fileName;
QString metaFileName = m_fileName + ".sigmf-meta";
m_metaFile.setFileName(metaFileName);
QString sampleFileName = m_fileName + ".sigmf-data";
m_sampleFile.setFileName(sampleFileName);
if (QFile::exists(fileName + ".sigmf-data") && QFile::exists(fileName + ".sigmf-meta")) if (QFile::exists(metaFileName) && QFile::exists(sampleFileName))
{ {
m_metaFileName = m_fileName + ".sigmf-meta"; QFile metaStream(metaFileName);
std::ifstream metaStream; metaStream.open(QIODevice::ReadOnly);
#ifdef Q_OS_WIN
metaStream.open(m_metaFileName.toStdWString().c_str());
#else
metaStream.open(m_metaFileName.toStdString().c_str());
#endif
std::ostringstream meta_buffer;
meta_buffer << metaStream.rdbuf();
try try
{ {
from_json(json::parse(meta_buffer.str()), *m_metaRecord); from_json(json::parse(metaStream.readAll().toStdString()), *m_metaRecord);
metaStream.close(); metaStream.close();
std::string sdrAngelVersion = m_metaRecord->global.access<sdrangel::GlobalT>().version; std::string sdrAngelVersion = m_metaRecord->global.access<sdrangel::GlobalT>().version;
if (sdrAngelVersion.size() != 0) if (sdrAngelVersion.size() != 0)
{ {
qDebug("SigMFFileRecord::setFileName: appending mode"); qDebug("SigMFFileRecord::setFileName: appending mode");
m_metaFile.open(m_metaFileName.toStdString().c_str(), std::ofstream::out);
m_initialMsCount = 0; m_initialMsCount = 0;
for (auto capture : m_metaRecord->captures) for (auto capture : m_metaRecord->captures)
@ -140,10 +136,18 @@ void SigMFFileRecord::setFileName(const QString& fileName)
m_initialMsCount += (length * 1000) / sampleRate; m_initialMsCount += (length * 1000) / sampleRate;
} }
m_sampleFileName = m_fileName + ".sigmf-data"; m_sampleFile.setFileName(m_fileName + ".sigmf-data");
m_sampleFile.open(m_sampleFileName.toStdString().c_str(), std::ios::binary & std::ios::app); if (!m_sampleFile.open(QIODevice::WriteOnly | QIODevice::Append))
m_initialBytesCount = (uint64_t) m_sampleFile.tellp(); {
qWarning() << "SigMFFileRecord::setFileName: failed to open file: " << m_sampleFile.fileName();
}
m_initialBytesCount = (uint64_t) m_sampleFile.size();
m_sampleStart = m_initialBytesCount / ((1<<m_log2RecordSampleSize)/4); // sizeof(Sample); m_sampleStart = m_initialBytesCount / ((1<<m_log2RecordSampleSize)/4); // sizeof(Sample);
if (!m_metaFile.open(QIODevice::WriteOnly | QIODevice::Append))
{
qWarning() << "SigMFFileRecord::setFileName: failed to open file: " << m_metaFile.fileName();
}
m_recordStart = false; m_recordStart = false;
} }
@ -186,18 +190,14 @@ bool SigMFFileRecord::startRecording()
qDebug("SigMFFileRecord::startRecording: new record %s", qPrintable(m_fileName)); qDebug("SigMFFileRecord::startRecording: new record %s", qPrintable(m_fileName));
clearMeta(); clearMeta();
m_sampleStart = 0; m_sampleStart = 0;
m_sampleFileName = m_fileName + ".sigmf-data"; if (!m_sampleFile.open(QIODevice::WriteOnly))
m_metaFileName = m_fileName + ".sigmf-meta";
m_sampleFile.open(m_sampleFileName.toStdString().c_str(), std::ios::binary);
if (!m_sampleFile.is_open())
{ {
qWarning() << "SigMFFileRecord::startRecording: failed to open file: " << m_sampleFileName; qWarning() << "SigMFFileRecord::startRecording: failed to open file: " << m_sampleFile.fileName();
success = false; success = false;
} }
m_metaFile.open(m_metaFileName.toStdString().c_str(), std::ofstream::out); if (!m_metaFile.open(QIODevice::WriteOnly | QIODevice::Append))
if (!m_metaFile.is_open())
{ {
qWarning() << "SigMFFileRecord::startRecording: failed to open file: " << m_metaFileName; qWarning() << "SigMFFileRecord::startRecording: failed to open file: " << m_metaFile.fileName();
success = false; success = false;
} }
makeHeader(); makeHeader();
@ -221,14 +221,14 @@ bool SigMFFileRecord::stopRecording()
qDebug("SigMFFileRecord::stopRecording: file previous capture"); qDebug("SigMFFileRecord::stopRecording: file previous capture");
makeCapture(); makeCapture();
m_recordOn = false; m_recordOn = false;
if (m_sampleFile.bad()) if (m_sampleFile.error())
{ {
qWarning() << "SigMFFileRecord::stopRecording: an error occurred while writing to " << m_sampleFileName; qWarning() << "SigMFFileRecord::stopRecording: an error occurred while writing to " << m_sampleFile.fileName();
return false; return false;
} }
if (m_metaFile.bad()) if (m_metaFile.error())
{ {
qWarning() << "SigMFFileRecord::stopRecording: an error occurred while writing to " << m_metaFileName; qWarning() << "SigMFFileRecord::stopRecording: an error occurred while writing to " << m_metaFile.fileName();
return false; return false;
} }
} }
@ -274,10 +274,11 @@ void SigMFFileRecord::makeCapture()
m_metaRecord->captures.emplace_back(recording_capture); m_metaRecord->captures.emplace_back(recording_capture);
m_sampleStart += m_sampleCount; m_sampleStart += m_sampleCount;
// Flush meta to disk // Flush meta to disk
m_metaFile.seekp(0); m_metaFile.seek(0);
std::string jsonRecord = json(*m_metaRecord).dump(2); std::string jsonRecord = json(*m_metaRecord).dump(2);
m_metaFile << jsonRecord; m_metaFile.write(jsonRecord.c_str(), jsonRecord.size());
m_metaFile.flush(); m_metaFile.flush();
m_metaFile.resize(m_metaFile.pos());
} }
else else
{ {

View File

@ -22,12 +22,10 @@
#ifndef INCLUDE_SIGMF_FILERECORD_H #ifndef INCLUDE_SIGMF_FILERECORD_H
#define INCLUDE_SIGMF_FILERECORD_H #define INCLUDE_SIGMF_FILERECORD_H
#include <string>
#include <iostream>
#include <fstream>
#include <ctime> #include <ctime>
#include <QDateTime> #include <QDateTime>
#include <QFile>
#include "dsp/sigmf_forward.h" #include "dsp/sigmf_forward.h"
#include "dsp/filerecordinterface.h" #include "dsp/filerecordinterface.h"
@ -77,16 +75,14 @@ private:
QString m_hardwareId; QString m_hardwareId;
QString m_fileName; QString m_fileName;
QString m_sampleFileName;
QString m_metaFileName;
quint32 m_sampleRate; quint32 m_sampleRate;
quint64 m_centerFrequency; quint64 m_centerFrequency;
qint64 m_msShift; qint64 m_msShift;
bool m_recordOn; bool m_recordOn;
bool m_recordStart; bool m_recordStart;
QDateTime m_captureStartDT; QDateTime m_captureStartDT;
std::ofstream m_metaFile; QFile m_metaFile;
std::ofstream m_sampleFile; QFile m_sampleFile;
quint64 m_sampleStart; quint64 m_sampleStart;
quint64 m_sampleCount; quint64 m_sampleCount;
quint64 m_initialMsCount; quint64 m_initialMsCount;