1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 12:22:00 -05:00
sdrangel/plugins/samplesource/usrpinput/usrpinputthread.cpp

259 lines
8.0 KiB
C++
Raw Normal View History

2020-10-02 17:45:28 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2020 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
2020-10-02 17:45:28 -04: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 //
// (at your option) any later version. //
// //
// 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 <algorithm>
#include <QDebug>
#include <uhd/types/stream_cmd.hpp>
#include "usrpinputthread.h"
2023-11-22 09:28:35 -05:00
USRPInputThread::USRPInputThread(uhd::rx_streamer::sptr stream, size_t bufSamples,
SampleSinkFifo* sampleFifo, ReplayBuffer<qint16> *replayBuffer, QObject* parent) :
2020-10-02 17:45:28 -04:00
QThread(parent),
m_running(false),
m_stream(stream),
m_bufSamples(bufSamples),
m_convertBuffer(bufSamples),
2020-10-02 17:45:28 -04:00
m_sampleFifo(sampleFifo),
2023-11-22 09:28:35 -05:00
m_replayBuffer(replayBuffer),
2020-10-02 17:45:28 -04:00
m_log2Decim(0)
{
// *2 as samples are I+Q
m_buf = new qint16[2*bufSamples];
std::fill(m_buf, m_buf + 2*bufSamples, 0);
2020-10-02 17:45:28 -04:00
}
USRPInputThread::~USRPInputThread()
{
stopWork();
delete m_buf;
}
void USRPInputThread::issueStreamCmd(bool start)
{
uhd::stream_cmd_t stream_cmd(start ? uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS : uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
stream_cmd.num_samps = size_t(0);
stream_cmd.stream_now = true;
stream_cmd.time_spec = uhd::time_spec_t();
2023-11-22 09:28:35 -05:00
if (m_stream)
{
m_stream->issue_stream_cmd(stream_cmd);
qDebug() << "USRPInputThread::issueStreamCmd " << (start ? "start" : "stop");
}
else
{
qDebug() << "USRPInputThread::issueStreamCmd m_stream is null";
}
2020-10-02 17:45:28 -04:00
}
void USRPInputThread::startWork()
{
if (m_running) return; // return if running already
try
{
// Start streaming
issueStreamCmd(true);
2020-10-02 17:45:28 -04:00
// Reset stats
m_packets = 0;
m_overflows = 0;
m_timeouts = 0;
qDebug("USRPInputThread::startWork: stream started");
}
catch (std::exception& e)
{
qDebug() << "USRPInputThread::startWork: exception: " << e.what();
}
m_startWaitMutex.lock();
start();
while(!m_running)
m_startWaiter.wait(&m_startWaitMutex, 100);
m_startWaitMutex.unlock();
}
void USRPInputThread::stopWork()
{
if (!m_running) return; // return if not running
m_running = false;
wait();
try
{
uhd::rx_metadata_t md;
// Stop streaming
issueStreamCmd(false);
2020-10-02 17:45:28 -04:00
// Clear out any data left in the stream, otherwise we'll get an
// exception 'recv buffer smaller than vrt packet offset' when restarting
md.end_of_burst = false;
md.error_code = uhd::rx_metadata_t::ERROR_CODE_NONE;
while (!md.end_of_burst && (md.error_code != uhd::rx_metadata_t::ERROR_CODE_TIMEOUT))
2020-10-02 17:45:28 -04:00
{
try
{
md.reset();
m_stream->recv(m_buf, m_bufSamples, md);
2020-10-02 17:45:28 -04:00
}
catch (std::exception& e)
{
qDebug() << "USRPInputThread::stopWork: exception ignored while flushing buffers: " << e.what();
}
}
qDebug("USRPInputThread::stopWork: stream stopped");
}
catch (std::exception& e)
{
qDebug() << "USRPInputThread::stopWork: exception: " << e.what();
}
}
void USRPInputThread::setLog2Decimation(unsigned int log2_decim)
{
m_log2Decim = log2_decim;
}
void USRPInputThread::run()
{
uhd::rx_metadata_t md;
m_running = true;
m_startWaiter.wakeAll();
try
{
while (m_running)
{
md.reset();
const size_t samples_received = m_stream->recv(m_buf, m_bufSamples, md);
2020-10-02 17:45:28 -04:00
m_packets++;
if (samples_received != m_bufSamples)
2020-10-02 17:45:28 -04:00
{
2020-10-26 10:50:00 -04:00
qDebug("USRPInputThread::run - received %ld/%ld samples", samples_received, m_bufSamples);
2020-10-02 17:45:28 -04:00
}
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT)
{
qDebug("USRPInputThread::run - timeout - ending thread");
m_timeouts++;
// Restart streaming
issueStreamCmd(false);
issueStreamCmd(true);
qDebug("USRPInputThread::run - timeout - restarting");
2020-10-02 17:45:28 -04:00
}
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW)
{
qDebug("USRPInputThread::run - overflow");
m_overflows++;
}
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND)
qDebug("USRPInputThread::run - late command error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN)
qDebug("USRPInputThread::run - broken chain error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_ALIGNMENT)
qDebug("USRPInputThread::run - alignment error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET)
qDebug("USRPInputThread::run - bad packet error");
if (samples_received > 0)
callbackIQ(m_buf, 2 * samples_received);
2020-10-02 17:45:28 -04:00
}
}
catch (std::exception& e)
{
qDebug() << "USRPInputThread::run: exception: " << e.what();
}
m_running = false;
}
// Decimate according to specified log2 (ex: log2=4 => decim=16)
2023-11-22 09:28:35 -05:00
void USRPInputThread::callbackIQ(const qint16* inBuf, qint32 len)
2020-10-02 17:45:28 -04:00
{
SampleVector::iterator it = m_convertBuffer.begin();
2023-11-22 09:28:35 -05:00
// Save data to replay buffer
2023-12-06 05:53:28 -05:00
m_replayBuffer->lock();
bool replayEnabled = m_replayBuffer->getSize() > 0;
if (replayEnabled) {
m_replayBuffer->write(inBuf, len);
}
2023-11-22 09:28:35 -05:00
2023-12-06 05:53:28 -05:00
const qint16* buf = inBuf;
qint32 remaining = len;
2023-11-22 09:28:35 -05:00
while (remaining > 0)
2023-12-06 05:53:28 -05:00
{
// Choose between live data or replayed data
if (replayEnabled && m_replayBuffer->useReplay()) {
len = m_replayBuffer->read(remaining, buf);
} else {
len = remaining;
}
remaining -= len;
2023-11-22 09:28:35 -05:00
switch (m_log2Decim)
{
case 0:
m_decimatorsIQ.decimate1(&it, buf, len);
break;
case 1:
m_decimatorsIQ.decimate2_cen(&it, buf, len);
break;
case 2:
m_decimatorsIQ.decimate4_cen(&it, buf, len);
break;
case 3:
m_decimatorsIQ.decimate8_cen(&it, buf, len);
break;
case 4:
m_decimatorsIQ.decimate16_cen(&it, buf, len);
break;
case 5:
m_decimatorsIQ.decimate32_cen(&it, buf, len);
break;
case 6:
m_decimatorsIQ.decimate64_cen(&it, buf, len);
break;
default:
break;
}
2020-10-02 17:45:28 -04:00
}
2023-11-22 09:28:35 -05:00
m_replayBuffer->unlock();
2020-10-02 17:45:28 -04:00
m_sampleFifo->write(m_convertBuffer.begin(), it);
}
void USRPInputThread::getStreamStatus(bool& active, quint32& overflows, quint32& timeouts)
{
//qDebug() << "USRPInputThread::getStreamStatus " << m_packets << " " << m_overflows << " " << m_timeouts;
active = m_packets > 0;
overflows = m_overflows;
timeouts = m_timeouts;
}