1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 10:05:46 -05:00

Audio input: Update threading model. Part of #1346

This commit is contained in:
f4exb 2022-10-08 10:15:56 +02:00
parent 2bdc456b7a
commit 997a7da292
2 changed files with 30 additions and 13 deletions

View File

@ -22,6 +22,7 @@
#include <QDebug>
#include <QNetworkReply>
#include <QBuffer>
#include <QThread>
#include "SWGDeviceSettings.h"
#include "SWGDeviceState.h"
@ -40,6 +41,7 @@ AudioInput::AudioInput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_settings(),
m_worker(nullptr),
m_workerThread(nullptr),
m_deviceDescription("AudioInput"),
m_running(false),
m_centerFrequency(0)
@ -116,10 +118,10 @@ void AudioInput::init()
bool AudioInput::start()
{
qDebug() << "AudioInput::start";
QMutexLocker mutexLocker(&m_mutex);
if (m_running) {
stop();
return true;
}
if(!m_sampleFifo.setSize(96000*4))
@ -128,14 +130,21 @@ bool AudioInput::start()
return false;
}
qDebug() << "AudioInput::start";
applySettings(m_settings, true, true);
m_workerThread = new QThread();
m_worker = new AudioInputWorker(&m_sampleFifo, &m_fifo);
m_worker->moveToThread(&m_workerThread);
m_worker->moveToThread(m_workerThread);
QObject::connect(m_workerThread, &QThread::started, m_worker, &AudioInputWorker::startWork);
QObject::connect(m_workerThread, &QThread::finished, m_worker, &QObject::deleteLater);
QObject::connect(m_workerThread, &QThread::finished, m_workerThread, &QThread::deleteLater);
m_worker->setLog2Decimation(m_settings.m_log2Decim);
m_worker->setIQMapping(m_settings.m_iqMapping);
m_worker->startWork();
m_workerThread.start();
m_workerThread->start();
qDebug("AudioInput::started");
m_running = true;
@ -151,12 +160,19 @@ void AudioInput::closeDevice()
void AudioInput::stop()
{
if (m_worker)
QMutexLocker mutexLocker(&m_mutex);
if (!m_running) {
return;
}
qDebug("AudioInput::stop");
if (m_workerThread)
{
m_worker->stopWork();
m_workerThread.quit();
m_workerThread.wait();
delete m_worker;
m_workerThread->quit();
m_workerThread->wait();
m_workerThread = nullptr;
m_worker = nullptr;
}
@ -286,7 +302,7 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
reverseAPIKeys.append("log2Decim");
forwardChange = true;
if (m_worker)
if (m_running)
{
m_worker->setLog2Decimation(settings.m_log2Decim);
qDebug() << "AudioInput::applySettings: set decimation to " << (1<<settings.m_log2Decim);
@ -298,7 +314,7 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
reverseAPIKeys.append("iqMapping");
forwardChange = true;
if (m_worker) {
if (m_running) {
m_worker->setIQMapping(settings.m_iqMapping);
}
}

View File

@ -24,7 +24,7 @@
#include <QString>
#include <QByteArray>
#include <QNetworkRequest>
#include <QThread>
#include <QMutex>
#include "dsp/devicesamplesource.h"
#include "audio/audioinputdevice.h"
@ -34,6 +34,7 @@
class QNetworkAccessManager;
class QNetworkReply;
class QThread;
class DeviceAPI;
class AudioInputWorker;
@ -137,7 +138,7 @@ private:
QMutex m_mutex;
AudioInputSettings m_settings;
AudioInputWorker* m_worker;
QThread m_workerThread;
QThread *m_workerThread;
QString m_deviceDescription;
bool m_running;
int m_sampleRate;