2016-06-19 03:56:49 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2023-11-18 04:07:41 -05:00
|
|
|
// Copyright (C) 2016-2021 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
|
2016-06-19 03:56:49 -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 //
|
2019-04-11 00:57:41 -04:00
|
|
|
// (at your option) any later version. //
|
2016-06-19 03:56:49 -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 <QUdpSocket>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include "dsp/dspcommands.h"
|
|
|
|
#include "dsp/dspengine.h"
|
2019-05-08 16:11:53 -04:00
|
|
|
#include "device/deviceapi.h"
|
2017-06-09 07:58:27 -04:00
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
#include "remoteinputudphandler.h"
|
|
|
|
#include "remoteinput.h"
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2021-12-26 05:56:37 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(RemoteInputUDPHandler::MsgReportMetaDataChange, Message)
|
2020-08-26 12:52:36 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(RemoteInputUDPHandler::MsgUDPAddressAndPort, Message)
|
2020-06-28 00:56:21 -04:00
|
|
|
|
2019-05-08 16:11:53 -04:00
|
|
|
RemoteInputUDPHandler::RemoteInputUDPHandler(SampleSinkFifo *sampleFifo, DeviceAPI *deviceAPI) :
|
2017-12-25 20:18:30 -05:00
|
|
|
m_deviceAPI(deviceAPI),
|
|
|
|
m_masterTimer(deviceAPI->getMasterTimer()),
|
|
|
|
m_masterTimerConnected(false),
|
|
|
|
m_running(false),
|
2019-02-02 18:26:26 -05:00
|
|
|
m_rateDivider(1000/REMOTEINPUT_THROTTLE_MS),
|
2021-12-04 15:36:12 -05:00
|
|
|
m_dataSocket(nullptr),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataAddress(QHostAddress::LocalHost),
|
|
|
|
m_remoteAddress(QHostAddress::LocalHost),
|
|
|
|
m_dataPort(9090),
|
2020-08-26 12:52:36 -04:00
|
|
|
m_multicastAddress(QStringLiteral("224.0.0.1")),
|
|
|
|
m_multicast(false),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataConnected(false),
|
2021-12-04 15:36:12 -05:00
|
|
|
m_udpBuf(nullptr),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_udpReadBytes(0),
|
|
|
|
m_sampleFifo(sampleFifo),
|
|
|
|
m_samplerate(0),
|
|
|
|
m_centerFrequency(0),
|
2018-11-13 07:45:55 -05:00
|
|
|
m_tv_msec(0),
|
2020-06-28 00:56:21 -04:00
|
|
|
m_messageQueueToGUI(0),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_tickCount(0),
|
|
|
|
m_samplesCount(0),
|
|
|
|
m_timer(0),
|
2019-02-02 18:26:26 -05:00
|
|
|
m_throttlems(REMOTEINPUT_THROTTLE_MS),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_readLengthSamples(0),
|
|
|
|
m_readLength(0),
|
2021-12-26 05:56:37 -05:00
|
|
|
m_converterBuffer(nullptr),
|
2018-01-24 02:49:18 -05:00
|
|
|
m_converterBufferNbSamples(0),
|
2016-06-19 03:56:49 -04:00
|
|
|
m_throttleToggle(false),
|
2016-07-12 02:52:38 -04:00
|
|
|
m_autoCorrBuffer(true)
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2019-02-02 16:58:42 -05:00
|
|
|
m_udpBuf = new char[RemoteUdpSize];
|
2017-12-25 20:18:30 -05:00
|
|
|
|
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
#warning "Uses internal timer"
|
|
|
|
m_timer = new QTimer();
|
|
|
|
m_timer->start(50);
|
|
|
|
m_throttlems = m_timer->interval();
|
|
|
|
#else
|
|
|
|
m_throttlems = m_masterTimer.interval();
|
|
|
|
#endif
|
|
|
|
m_rateDivider = 1000 / m_throttlems;
|
2020-08-26 12:52:36 -04:00
|
|
|
|
|
|
|
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages()));
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
RemoteInputUDPHandler::~RemoteInputUDPHandler()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
delete[] m_udpBuf;
|
2018-01-24 02:49:18 -05:00
|
|
|
if (m_converterBuffer) { delete[] m_converterBuffer; }
|
2016-06-19 03:56:49 -04:00
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
if (m_timer) {
|
|
|
|
delete m_timer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::start()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qDebug("RemoteInputUDPHandler::start");
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2017-12-25 20:18:30 -05:00
|
|
|
if (m_running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:36:12 -05:00
|
|
|
if (!m_dataSocket)
|
|
|
|
{
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataSocket = new QUdpSocket(this);
|
2021-12-04 15:36:12 -05:00
|
|
|
m_dataSocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, getDataSocketBufferSize());
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 02:20:53 -05:00
|
|
|
if (!m_dataConnected)
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2020-08-26 12:52:36 -04:00
|
|
|
if (m_dataSocket->bind(m_multicast ? QHostAddress::AnyIPv4 : m_dataAddress, m_dataPort, QUdpSocket::ShareAddress))
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qDebug("RemoteInputUDPHandler::start: bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort);
|
2020-08-26 12:52:36 -04:00
|
|
|
|
|
|
|
if (m_multicast)
|
|
|
|
{
|
|
|
|
if (m_dataSocket->joinMulticastGroup(m_multicastAddress)) {
|
|
|
|
qDebug("RemoteInputUDPHandler::start: joined multicast group %s", qPrintable(m_multicastAddress.toString()));
|
|
|
|
} else {
|
|
|
|
qDebug("RemoteInputUDPHandler::start: failed joining multicast group %s", qPrintable(m_multicastAddress.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead())); //, Qt::QueuedConnection);
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataConnected = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qWarning("RemoteInputUDPHandler::start: cannot bind data port %d", m_dataPort);
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataConnected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_elapsedTimer.start();
|
2017-12-25 20:18:30 -05:00
|
|
|
m_running = true;
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::stop()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qDebug("RemoteInputUDPHandler::stop");
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2017-12-25 20:18:30 -05:00
|
|
|
if (!m_running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectTimer();
|
|
|
|
|
2017-02-03 02:20:53 -05:00
|
|
|
if (m_dataConnected)
|
|
|
|
{
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataConnected = false;
|
2017-02-03 02:20:53 -05:00
|
|
|
disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dataSocket)
|
|
|
|
{
|
|
|
|
delete m_dataSocket;
|
2021-12-26 05:56:37 -05:00
|
|
|
m_dataSocket = nullptr;
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
2017-12-25 03:10:19 -05:00
|
|
|
|
2017-12-25 20:18:30 -05:00
|
|
|
m_centerFrequency = 0;
|
|
|
|
m_samplerate = 0;
|
|
|
|
m_running = false;
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2020-08-26 12:52:36 -04:00
|
|
|
void RemoteInputUDPHandler::configureUDPLink(const QString& address, quint16 port, const QString& multicastAddress, bool multicastJoin)
|
|
|
|
{
|
|
|
|
Message* msg = MsgUDPAddressAndPort::create(address, port, multicastAddress, multicastJoin);
|
|
|
|
m_inputMessageQueue.push(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteInputUDPHandler::applyUDPLink(const QString& address, quint16 port, const QString& multicastAddress, bool multicastJoin)
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2020-08-26 12:52:36 -04:00
|
|
|
qDebug() << "RemoteInputUDPHandler::applyUDPLink: "
|
|
|
|
<< " address: " << address
|
|
|
|
<< " port: " << port
|
|
|
|
<< " multicastAddress: " << multicastAddress
|
2021-12-04 15:36:12 -05:00
|
|
|
<< " multicastJoin: " << multicastJoin;
|
2020-08-26 12:52:36 -04:00
|
|
|
|
2016-06-19 03:56:49 -04:00
|
|
|
bool addressOK = m_dataAddress.setAddress(address);
|
|
|
|
|
|
|
|
if (!addressOK)
|
|
|
|
{
|
2020-08-26 12:52:36 -04:00
|
|
|
qWarning("RemoteInputUDPHandler::applyUDPLink: invalid address %s. Set to localhost.", address.toStdString().c_str());
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataAddress = QHostAddress::LocalHost;
|
|
|
|
}
|
|
|
|
|
2020-08-26 12:52:36 -04:00
|
|
|
m_multicast = multicastJoin;
|
|
|
|
addressOK = m_multicastAddress.setAddress(multicastAddress);
|
|
|
|
|
|
|
|
if (!addressOK)
|
|
|
|
{
|
|
|
|
qWarning("RemoteInputUDPHandler::applyUDPLink: invalid multicast address %s. disabling multicast.", address.toStdString().c_str());
|
|
|
|
m_multicast = false;
|
|
|
|
}
|
|
|
|
|
2016-06-19 03:56:49 -04:00
|
|
|
m_dataPort = port;
|
2017-12-25 20:18:30 -05:00
|
|
|
stop();
|
2016-06-19 03:56:49 -04:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::dataReadyRead()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2016-07-11 22:44:44 -04:00
|
|
|
m_udpReadBytes = 0;
|
|
|
|
|
2016-07-29 02:21:27 -04:00
|
|
|
while (m_dataSocket->hasPendingDatagrams() && m_dataConnected)
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
|
|
|
qint64 pendingDataSize = m_dataSocket->pendingDatagramSize();
|
2016-07-11 22:44:44 -04:00
|
|
|
m_udpReadBytes += m_dataSocket->readDatagram(&m_udpBuf[m_udpReadBytes], pendingDataSize, &m_remoteAddress, 0);
|
2016-06-19 18:45:24 -04:00
|
|
|
|
2019-02-02 16:58:42 -05:00
|
|
|
if (m_udpReadBytes == RemoteUdpSize) {
|
2016-06-19 18:45:24 -04:00
|
|
|
processData();
|
2016-07-11 22:44:44 -04:00
|
|
|
m_udpReadBytes = 0;
|
2016-06-19 18:45:24 -04:00
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::processData()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
m_remoteInputBuffer.writeData(m_udpBuf);
|
|
|
|
const RemoteMetaDataFEC& metaData = m_remoteInputBuffer.getCurrentMeta();
|
2021-12-26 05:56:37 -05:00
|
|
|
|
|
|
|
if (!(m_currentMeta == metaData))
|
|
|
|
{
|
|
|
|
m_currentMeta = metaData;
|
|
|
|
|
|
|
|
if (m_messageQueueToInput)
|
|
|
|
{
|
|
|
|
MsgReportMetaDataChange *msg = MsgReportMetaDataChange::create(m_currentMeta);
|
|
|
|
m_messageQueueToInput->push(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 03:07:37 -04:00
|
|
|
bool change = false;
|
2017-12-25 20:18:30 -05:00
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
m_tv_msec = m_remoteInputBuffer.getTVOutMSec();
|
2016-06-20 03:07:37 -04:00
|
|
|
|
|
|
|
if (m_centerFrequency != metaData.m_centerFrequency)
|
|
|
|
{
|
|
|
|
m_centerFrequency = metaData.m_centerFrequency;
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_samplerate != metaData.m_sampleRate)
|
|
|
|
{
|
2020-06-28 02:17:39 -04:00
|
|
|
disconnectTimer();
|
|
|
|
adjustNbDecoderSlots(metaData);
|
2016-06-20 03:07:37 -04:00
|
|
|
m_samplerate = metaData.m_sampleRate;
|
|
|
|
change = true;
|
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2018-09-13 08:58:35 -04:00
|
|
|
if (change && (m_samplerate != 0))
|
2016-06-20 03:07:37 -04:00
|
|
|
{
|
2019-04-25 21:07:50 -04:00
|
|
|
qDebug("RemoteInputUDPHandler::processData: m_samplerate: %u S/s m_centerFrequency: %lu Hz", m_samplerate, m_centerFrequency);
|
2017-12-25 03:10:19 -05:00
|
|
|
|
2019-04-25 21:07:50 -04:00
|
|
|
DSPSignalNotification *notif = new DSPSignalNotification(m_samplerate, m_centerFrequency); // Frequency in Hz for the DSP engine
|
2017-12-25 20:18:30 -05:00
|
|
|
m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif);
|
|
|
|
|
2020-06-28 00:56:21 -04:00
|
|
|
if (m_messageQueueToGUI)
|
2017-12-25 03:10:19 -05:00
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
RemoteInput::MsgReportRemoteInputStreamData *report = RemoteInput::MsgReportRemoteInputStreamData::create(
|
2017-12-25 03:10:19 -05:00
|
|
|
m_samplerate,
|
2019-04-25 21:07:50 -04:00
|
|
|
m_centerFrequency, // Frequency in Hz for the GUI
|
2018-11-13 07:45:55 -05:00
|
|
|
m_tv_msec);
|
2017-12-25 03:10:19 -05:00
|
|
|
|
2020-06-28 00:56:21 -04:00
|
|
|
m_messageQueueToGUI->push(report);
|
2017-12-25 03:10:19 -05:00
|
|
|
}
|
2017-12-25 20:18:30 -05:00
|
|
|
|
2021-12-04 15:36:12 -05:00
|
|
|
m_dataSocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, getDataSocketBufferSize());
|
2021-12-26 05:56:37 -05:00
|
|
|
m_elapsedTimer.restart();
|
|
|
|
m_throttlems = 0;
|
2017-12-25 20:18:30 -05:00
|
|
|
connectTimer();
|
2016-06-20 03:07:37 -04:00
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2020-06-28 02:17:39 -04:00
|
|
|
void RemoteInputUDPHandler::adjustNbDecoderSlots(const RemoteMetaDataFEC& metaData)
|
|
|
|
{
|
|
|
|
int sampleRate = metaData.m_sampleRate;
|
|
|
|
int sampleBytes = metaData.m_sampleBytes;
|
|
|
|
int bufferFrameSize = RemoteInputBuffer::getBufferFrameSize();
|
|
|
|
float fNbDecoderSlots = (float) (4 * sampleBytes * sampleRate) / (float) bufferFrameSize;
|
|
|
|
int rawNbDecoderSlots = ((((int) ceil(fNbDecoderSlots)) / 2) * 2) + 2; // next multiple of 2
|
|
|
|
qDebug("RemoteInputUDPHandler::adjustNbDecoderSlots: rawNbDecoderSlots: %d", rawNbDecoderSlots);
|
|
|
|
m_remoteInputBuffer.setNbDecoderSlots(rawNbDecoderSlots < 4 ? 4 : rawNbDecoderSlots);
|
|
|
|
m_remoteInputBuffer.setBufferLenSec(metaData);
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::connectTimer()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2017-12-25 20:18:30 -05:00
|
|
|
if (!m_masterTimerConnected)
|
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qDebug() << "RemoteInputUDPHandler::connectTimer";
|
2016-06-19 03:56:49 -04:00
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
#warning "Uses internal timer"
|
2017-12-25 20:18:30 -05:00
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(tick()));
|
2016-06-19 03:56:49 -04:00
|
|
|
#else
|
2017-12-25 20:18:30 -05:00
|
|
|
connect(&m_masterTimer, SIGNAL(timeout()), this, SLOT(tick()));
|
2016-06-19 03:56:49 -04:00
|
|
|
#endif
|
2017-12-25 20:18:30 -05:00
|
|
|
m_masterTimerConnected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::disconnectTimer()
|
2017-12-25 20:18:30 -05:00
|
|
|
{
|
|
|
|
if (m_masterTimerConnected)
|
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
qDebug() << "RemoteInputUDPHandler::disconnectTimer";
|
2017-12-25 20:18:30 -05:00
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
#warning "Uses internal timer"
|
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(tick()));
|
|
|
|
#else
|
|
|
|
disconnect(&m_masterTimer, SIGNAL(timeout()), this, SLOT(tick()));
|
|
|
|
#endif
|
|
|
|
m_masterTimerConnected = false;
|
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
void RemoteInputUDPHandler::tick()
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
|
|
|
// auto throttling
|
|
|
|
int throttlems = m_elapsedTimer.restart();
|
|
|
|
|
|
|
|
if (throttlems != m_throttlems)
|
|
|
|
{
|
|
|
|
m_throttlems = throttlems;
|
2021-12-26 05:56:37 -05:00
|
|
|
m_readLengthSamples = (m_currentMeta.m_sampleRate * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000;
|
2016-06-19 03:56:49 -04:00
|
|
|
m_throttleToggle = !m_throttleToggle;
|
|
|
|
}
|
|
|
|
|
2019-04-30 02:43:55 -04:00
|
|
|
if (m_autoCorrBuffer)
|
|
|
|
{
|
2019-02-02 18:26:26 -05:00
|
|
|
m_readLengthSamples += m_remoteInputBuffer.getRWBalanceCorrection();
|
2019-04-30 02:43:55 -04:00
|
|
|
// Eliminate negative or excessively high values
|
|
|
|
m_readLengthSamples = m_readLengthSamples < 0 ?
|
2021-12-26 05:56:37 -05:00
|
|
|
0 : m_readLengthSamples > (int) m_currentMeta.m_sampleRate/5 ?
|
2019-04-30 02:43:55 -04:00
|
|
|
m_remoteInputBuffer.getCurrentMeta().m_sampleRate/5 : m_readLengthSamples;
|
2016-07-12 21:31:19 -04:00
|
|
|
}
|
|
|
|
|
2021-12-26 05:56:37 -05:00
|
|
|
m_readLength = m_readLengthSamples * (m_currentMeta.m_sampleBytes & 0xF) * 2;
|
2016-07-12 21:31:19 -04:00
|
|
|
|
2021-12-26 05:56:37 -05:00
|
|
|
if (m_currentMeta.m_sampleBits == SDR_RX_SAMP_SZ) // no conversion
|
2021-12-19 07:15:17 -05:00
|
|
|
{
|
|
|
|
// read samples directly feeding the SampleFifo (no callback)
|
|
|
|
m_sampleFifo->write(reinterpret_cast<quint8*>(m_remoteInputBuffer.readData(m_readLength)), m_readLength);
|
|
|
|
m_samplesCount += m_readLengthSamples;
|
|
|
|
}
|
2021-12-26 05:56:37 -05:00
|
|
|
else if ((m_currentMeta.m_sampleBits == 8) && (SDR_RX_SAMP_SZ == 16)) // 8 -> 16
|
2021-12-19 07:15:17 -05:00
|
|
|
{
|
|
|
|
if (m_readLengthSamples > (int) m_converterBufferNbSamples)
|
|
|
|
{
|
|
|
|
if (m_converterBuffer) { delete[] m_converterBuffer; }
|
|
|
|
m_converterBuffer = new int32_t[m_readLengthSamples];
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:30:43 -05:00
|
|
|
int8_t *buf = (int8_t*) m_remoteInputBuffer.readData(m_readLength);
|
2021-12-19 07:15:17 -05:00
|
|
|
|
|
|
|
for (int is = 0; is < m_readLengthSamples; is++)
|
|
|
|
{
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[is] = buf[2*is+1] * (1<<8); // Q -> MSB
|
2021-12-19 07:15:17 -05:00
|
|
|
m_converterBuffer[is] <<= 16;
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[is] += buf[2*is] * (1<<8); // I -> LSB
|
2021-12-19 07:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-26 05:56:37 -05:00
|
|
|
else if ((m_currentMeta.m_sampleBits == 8) && (SDR_RX_SAMP_SZ == 24)) // 8 -> 24
|
2021-12-19 07:15:17 -05:00
|
|
|
{
|
|
|
|
if (m_readLengthSamples > (int) m_converterBufferNbSamples)
|
|
|
|
{
|
|
|
|
if (m_converterBuffer) { delete[] m_converterBuffer; }
|
|
|
|
m_converterBuffer = new int32_t[m_readLengthSamples*2];
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:30:43 -05:00
|
|
|
int8_t *buf = (int8_t*) m_remoteInputBuffer.readData(m_readLength);
|
2021-12-19 07:15:17 -05:00
|
|
|
|
|
|
|
for (int is = 0; is < m_readLengthSamples; is++)
|
|
|
|
{
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[2*is] = buf[2*is] * (1<<16); // I
|
|
|
|
m_converterBuffer[2*is+1] = buf[2*is+1] * (1<<16); // Q
|
2021-12-19 07:15:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m_sampleFifo->write(reinterpret_cast<quint8*>(m_converterBuffer), m_readLengthSamples*sizeof(Sample));
|
|
|
|
}
|
2021-12-26 05:56:37 -05:00
|
|
|
else if (m_currentMeta.m_sampleBits == 16) // 16 -> 24
|
2018-01-24 02:49:18 -05:00
|
|
|
{
|
2019-04-30 02:43:55 -04:00
|
|
|
if (m_readLengthSamples > (int) m_converterBufferNbSamples)
|
2018-01-24 02:49:18 -05:00
|
|
|
{
|
|
|
|
if (m_converterBuffer) { delete[] m_converterBuffer; }
|
|
|
|
m_converterBuffer = new int32_t[m_readLengthSamples*2];
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:30:43 -05:00
|
|
|
int16_t *buf = (int16_t*) m_remoteInputBuffer.readData(m_readLength);
|
2018-01-24 02:49:18 -05:00
|
|
|
|
2019-04-30 02:43:55 -04:00
|
|
|
for (int is = 0; is < m_readLengthSamples; is++)
|
2018-01-24 02:49:18 -05:00
|
|
|
{
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[2*is] = buf[2*is] * (1<<8); // I
|
|
|
|
m_converterBuffer[2*is+1] = buf[2*is+1] * (1<<8); // Q
|
2018-01-24 02:49:18 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 12:39:04 -05:00
|
|
|
m_sampleFifo->write(reinterpret_cast<quint8*>(m_converterBuffer), m_readLengthSamples*sizeof(Sample));
|
2018-01-24 02:49:18 -05:00
|
|
|
}
|
2021-12-26 05:56:37 -05:00
|
|
|
else if (m_currentMeta.m_sampleBits == 24) // 24 -> 16
|
2018-09-09 13:28:44 -04:00
|
|
|
{
|
2019-04-30 02:43:55 -04:00
|
|
|
if (m_readLengthSamples > (int) m_converterBufferNbSamples)
|
2018-09-09 13:28:44 -04:00
|
|
|
{
|
|
|
|
if (m_converterBuffer) { delete[] m_converterBuffer; }
|
|
|
|
m_converterBuffer = new int32_t[m_readLengthSamples];
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:30:43 -05:00
|
|
|
int32_t *buf = (int32_t*) m_remoteInputBuffer.readData(m_readLength);
|
2018-09-09 13:28:44 -04:00
|
|
|
|
2019-04-30 02:43:55 -04:00
|
|
|
for (int is = 0; is < m_readLengthSamples; is++)
|
2018-09-09 13:28:44 -04:00
|
|
|
{
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[is] = buf[2*is+1] / (1<<8); // Q -> MSB
|
2021-12-19 07:15:17 -05:00
|
|
|
m_converterBuffer[is] <<= 16;
|
2021-12-20 16:30:43 -05:00
|
|
|
m_converterBuffer[is] += buf[2*is] / (1<<8); // I -> LSB
|
2018-09-09 13:28:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_sampleFifo->write(reinterpret_cast<quint8*>(m_converterBuffer), m_readLengthSamples*sizeof(Sample));
|
|
|
|
}
|
2018-11-18 03:39:22 -05:00
|
|
|
else // invalid size
|
2018-09-09 13:28:44 -04:00
|
|
|
{
|
2021-12-26 05:56:37 -05:00
|
|
|
qWarning("RemoteInputUDPHandler::tick: unexpected sample size in stream: %d bits", (int) m_currentMeta.m_sampleBits);
|
2018-09-09 13:28:44 -04:00
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
|
|
|
|
if (m_tickCount < m_rateDivider)
|
|
|
|
{
|
|
|
|
m_tickCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_tickCount = 0;
|
2016-07-24 05:09:11 -04:00
|
|
|
|
2020-06-28 00:56:21 -04:00
|
|
|
if (m_messageQueueToGUI)
|
2017-12-24 13:58:26 -05:00
|
|
|
{
|
2017-12-25 20:18:30 -05:00
|
|
|
int framesDecodingStatus;
|
2019-02-02 18:26:26 -05:00
|
|
|
int minNbBlocks = m_remoteInputBuffer.getMinNbBlocks();
|
|
|
|
int minNbOriginalBlocks = m_remoteInputBuffer.getMinOriginalBlocks();
|
|
|
|
int nbOriginalBlocks = m_remoteInputBuffer.getCurrentMeta().m_nbOriginalBlocks;
|
|
|
|
int nbFECblocks = m_remoteInputBuffer.getCurrentMeta().m_nbFECBlocks;
|
|
|
|
int sampleBits = m_remoteInputBuffer.getCurrentMeta().m_sampleBits;
|
|
|
|
int sampleBytes = m_remoteInputBuffer.getCurrentMeta().m_sampleBytes;
|
2017-12-25 20:18:30 -05:00
|
|
|
|
|
|
|
//framesDecodingStatus = (minNbOriginalBlocks == nbOriginalBlocks ? 2 : (minNbOriginalBlocks < nbOriginalBlocks - nbFECblocks ? 0 : 1));
|
|
|
|
if (minNbBlocks < nbOriginalBlocks) {
|
|
|
|
framesDecodingStatus = 0;
|
|
|
|
} else if (minNbBlocks < nbOriginalBlocks + nbFECblocks) {
|
|
|
|
framesDecodingStatus = 1;
|
|
|
|
} else {
|
|
|
|
framesDecodingStatus = 2;
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:26:26 -05:00
|
|
|
RemoteInput::MsgReportRemoteInputStreamTiming *report = RemoteInput::MsgReportRemoteInputStreamTiming::create(
|
2018-11-13 07:45:55 -05:00
|
|
|
m_tv_msec,
|
2019-02-02 18:26:26 -05:00
|
|
|
m_remoteInputBuffer.getBufferLengthInSecs(),
|
|
|
|
m_remoteInputBuffer.getBufferGauge(),
|
2017-12-24 13:58:26 -05:00
|
|
|
framesDecodingStatus,
|
|
|
|
minNbBlocks == nbOriginalBlocks + nbFECblocks,
|
|
|
|
minNbBlocks,
|
|
|
|
minNbOriginalBlocks,
|
2019-02-02 18:26:26 -05:00
|
|
|
m_remoteInputBuffer.getMaxNbRecovery(),
|
|
|
|
m_remoteInputBuffer.getAvgNbBlocks(),
|
|
|
|
m_remoteInputBuffer.getAvgOriginalBlocks(),
|
|
|
|
m_remoteInputBuffer.getAvgNbRecovery(),
|
2017-12-24 13:58:26 -05:00
|
|
|
nbOriginalBlocks,
|
2018-09-09 11:39:36 -04:00
|
|
|
nbFECblocks,
|
2018-09-09 15:26:47 -04:00
|
|
|
sampleBits,
|
|
|
|
sampleBytes);
|
2017-12-24 13:58:26 -05:00
|
|
|
|
2020-06-28 00:56:21 -04:00
|
|
|
m_messageQueueToGUI->push(report);
|
2017-12-24 13:58:26 -05:00
|
|
|
}
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-26 12:52:36 -04:00
|
|
|
|
|
|
|
void RemoteInputUDPHandler::handleMessages()
|
|
|
|
{
|
|
|
|
Message* message;
|
|
|
|
|
|
|
|
while ((message = m_inputMessageQueue.pop()) != 0)
|
|
|
|
{
|
|
|
|
if (handleMessage(*message)) {
|
|
|
|
delete message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoteInputUDPHandler::handleMessage(const Message& cmd)
|
|
|
|
{
|
2021-12-04 15:36:12 -05:00
|
|
|
if (MsgUDPAddressAndPort::match(cmd))
|
2020-08-26 12:52:36 -04:00
|
|
|
{
|
2021-12-04 15:36:12 -05:00
|
|
|
MsgUDPAddressAndPort& notif = (MsgUDPAddressAndPort&) cmd;
|
2020-08-26 12:52:36 -04:00
|
|
|
applyUDPLink(notif.getAddress(), notif.getPort(), notif.getMulticastAddress(), notif.getMulticastJoin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 15:36:12 -05:00
|
|
|
|
|
|
|
int RemoteInputUDPHandler::getDataSocketBufferSize()
|
|
|
|
{
|
2021-12-18 00:24:47 -05:00
|
|
|
// set a floor value at 96 kS/s
|
|
|
|
uint32_t samplerate = m_samplerate < 96000 ? 96000 : m_samplerate;
|
2021-12-04 15:36:12 -05:00
|
|
|
// 250 ms (1/4s) at current sample rate
|
|
|
|
int bufferSize = (samplerate * 2 * (SDR_RX_SAMP_SZ == 16 ? 2 : 4)) / 4;
|
|
|
|
qDebug("RemoteInputUDPHandler::getDataSocketBufferSize: %d bytes", bufferSize);
|
|
|
|
return bufferSize;
|
|
|
|
}
|