1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-23 00:18:37 -05:00

Audio Input: implemented worker + thread

This commit is contained in:
f4exb 2020-11-13 16:22:29 +01:00
parent 4b397f0cc7
commit 6e3add76e3
5 changed files with 45 additions and 70 deletions

View File

@ -5,7 +5,7 @@ set(audioinput_SOURCES
audioinputplugin.cpp audioinputplugin.cpp
audioinputsettings.cpp audioinputsettings.cpp
audioinputwebapiadapter.cpp audioinputwebapiadapter.cpp
audioinputthread.cpp audioinputworker.cpp
) )
set(audioinput_HEADERS set(audioinput_HEADERS
@ -13,7 +13,7 @@ set(audioinput_HEADERS
audioinputplugin.h audioinputplugin.h
audioinputsettings.h audioinputsettings.h
audioinputwebapiadapter.h audioinputwebapiadapter.h
audioinputthread.h audioinputworker.h
) )
include_directories( include_directories(

View File

@ -31,7 +31,7 @@
#include "device/deviceapi.h" #include "device/deviceapi.h"
#include "audioinput.h" #include "audioinput.h"
#include "audioinputthread.h" #include "audioinputworker.h"
MESSAGE_CLASS_DEFINITION(AudioInput::MsgConfigureAudioInput, Message) MESSAGE_CLASS_DEFINITION(AudioInput::MsgConfigureAudioInput, Message)
MESSAGE_CLASS_DEFINITION(AudioInput::MsgStartStop, Message) MESSAGE_CLASS_DEFINITION(AudioInput::MsgStartStop, Message)
@ -39,11 +39,10 @@ MESSAGE_CLASS_DEFINITION(AudioInput::MsgStartStop, Message)
AudioInput::AudioInput(DeviceAPI *deviceAPI) : AudioInput::AudioInput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI), m_deviceAPI(deviceAPI),
m_settings(), m_settings(),
m_thread(nullptr),
m_deviceDescription("AudioInput"), m_deviceDescription("AudioInput"),
m_running(false) m_running(false)
{ {
m_fifo.setSize(20*AudioInputThread::m_convBufSamples); m_fifo.setSize(20*AudioInputWorker::m_convBufSamples);
openDevice(); openDevice();
m_deviceAPI->setNbSourceStreams(1); m_deviceAPI->setNbSourceStreams(1);
m_networkManager = new QNetworkAccessManager(); m_networkManager = new QNetworkAccessManager();
@ -106,7 +105,9 @@ bool AudioInput::start()
{ {
qDebug() << "AudioInput::start"; qDebug() << "AudioInput::start";
if (m_running) stop(); if (m_running) {
stop();
}
if(!m_sampleFifo.setSize(96000*4)) if(!m_sampleFifo.setSize(96000*4))
{ {
@ -116,11 +117,12 @@ bool AudioInput::start()
applySettings(m_settings, true, true); applySettings(m_settings, true, true);
m_thread = new AudioInputThread(&m_sampleFifo, &m_fifo); m_worker = new AudioInputWorker(&m_sampleFifo, &m_fifo);
m_thread->setLog2Decimation(m_settings.m_log2Decim); m_worker->moveToThread(&m_workerThread);
m_thread->setIQMapping(m_settings.m_iqMapping); m_worker->setLog2Decimation(m_settings.m_log2Decim);
m_thread->startWork(); m_worker->setIQMapping(m_settings.m_iqMapping);
m_worker->startWork();
m_workerThread.start();
qDebug("AudioInput::started"); qDebug("AudioInput::started");
m_running = true; m_running = true;
@ -136,12 +138,13 @@ void AudioInput::closeDevice()
void AudioInput::stop() void AudioInput::stop()
{ {
if (m_thread) if (m_worker)
{ {
m_thread->stopWork(); m_worker->stopWork();
// wait for thread to quit ? m_workerThread.quit();
delete m_thread; m_workerThread.wait();
m_thread = nullptr; delete m_worker;
m_worker = nullptr;
} }
m_running = false; m_running = false;
@ -274,9 +277,9 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
reverseAPIKeys.append("log2Decim"); reverseAPIKeys.append("log2Decim");
forwardChange = true; forwardChange = true;
if (m_thread) if (m_worker)
{ {
m_thread->setLog2Decimation(settings.m_log2Decim); m_worker->setLog2Decimation(settings.m_log2Decim);
qDebug() << "AudioInput::applySettings: set decimation to " << (1<<settings.m_log2Decim); qDebug() << "AudioInput::applySettings: set decimation to " << (1<<settings.m_log2Decim);
} }
} }
@ -284,8 +287,10 @@ void AudioInput::applySettings(const AudioInputSettings& settings, bool force, b
if ((m_settings.m_iqMapping != settings.m_iqMapping) || force) if ((m_settings.m_iqMapping != settings.m_iqMapping) || force)
{ {
reverseAPIKeys.append("iqMapping"); reverseAPIKeys.append("iqMapping");
if (m_thread)
m_thread->setIQMapping(settings.m_iqMapping); if (m_worker) {
m_worker->setIQMapping(settings.m_iqMapping);
}
} }
if (settings.m_useReverseAPI) if (settings.m_useReverseAPI)

View File

@ -24,6 +24,7 @@
#include <QString> #include <QString>
#include <QByteArray> #include <QByteArray>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QThread>
#include "dsp/devicesamplesource.h" #include "dsp/devicesamplesource.h"
#include "audio/audioinputdevice.h" #include "audio/audioinputdevice.h"
@ -34,7 +35,7 @@
class QNetworkAccessManager; class QNetworkAccessManager;
class QNetworkReply; class QNetworkReply;
class DeviceAPI; class DeviceAPI;
class AudioInputThread; class AudioInputWorker;
class AudioInput : public DeviceSampleSource { class AudioInput : public DeviceSampleSource {
Q_OBJECT Q_OBJECT
@ -135,7 +136,8 @@ private:
AudioFifo m_fifo; AudioFifo m_fifo;
QMutex m_mutex; QMutex m_mutex;
AudioInputSettings m_settings; AudioInputSettings m_settings;
AudioInputThread* m_thread; AudioInputWorker* m_worker;
QThread m_workerThread;
QString m_deviceDescription; QString m_deviceDescription;
bool m_running; bool m_running;
QNetworkAccessManager *m_networkManager; QNetworkAccessManager *m_networkManager;

View File

@ -20,16 +20,14 @@
#include <QDebug> #include <QDebug>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <chrono>
#include <thread>
#include "dsp/samplesinkfifo.h" #include "dsp/samplesinkfifo.h"
#include "audio/audiofifo.h" #include "audio/audiofifo.h"
#include "audioinputthread.h" #include "audioinputworker.h"
AudioInputThread::AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent) : AudioInputWorker::AudioInputWorker(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent) :
QThread(parent), QObject(parent),
m_fifo(fifo), m_fifo(fifo),
m_running(false), m_running(false),
m_log2Decim(0), m_log2Decim(0),
@ -37,50 +35,25 @@ AudioInputThread::AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo,
m_convertBuffer(m_convBufSamples), m_convertBuffer(m_convBufSamples),
m_sampleFifo(sampleFifo) m_sampleFifo(sampleFifo)
{ {
start();
} }
AudioInputThread::~AudioInputThread() AudioInputWorker::~AudioInputWorker()
{ {
} }
void AudioInputThread::startWork() void AudioInputWorker::startWork()
{ {
connect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio())); connect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio()));
m_startWaitMutex.lock(); m_running = true;
start();
while(!m_running)
{
m_startWaiter.wait(&m_startWaitMutex, 100);
}
m_startWaitMutex.unlock();
} }
void AudioInputThread::stopWork() void AudioInputWorker::stopWork()
{ {
disconnect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio())); disconnect(m_fifo, SIGNAL(dataReady()), this, SLOT(handleAudio()));
m_running = false; m_running = false;
wait();
} }
void AudioInputThread::run() void AudioInputWorker::workIQ(unsigned int nbRead)
{
m_running = true;
qDebug("AudioInputThread::run: start running loop");
while (m_running)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
qDebug("AudioInputThread::run: running loop stopped");
m_running = false;
}
void AudioInputThread::workIQ(unsigned int nbRead)
{ {
// Map between left and right audio channels and IQ channels // Map between left and right audio channels and IQ channels
if (m_iqMapping == AudioInputSettings::IQMapping::L) if (m_iqMapping == AudioInputSettings::IQMapping::L)
@ -138,7 +111,7 @@ void AudioInputThread::workIQ(unsigned int nbRead)
m_sampleFifo->write(m_convertBuffer.begin(), it); m_sampleFifo->write(m_convertBuffer.begin(), it);
} }
void AudioInputThread::handleAudio() void AudioInputWorker::handleAudio()
{ {
uint32_t nbRead; uint32_t nbRead;

View File

@ -16,12 +16,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // // along with this program. If not, see <http://www.gnu.org/licenses/>. //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_AUDIOINPUTTHREAD_H #ifndef INCLUDE_AUDIOINPUTWORKER_H
#define INCLUDE_AUDIOINPUTTHREAD_H #define INCLUDE_AUDIOINPUTWORKER_H
#include <QThread> #include <QObject>
#include <QMutex>
#include <QWaitCondition>
#include "dsp/samplesinkfifo.h" #include "dsp/samplesinkfifo.h"
#include "dsp/decimators.h" #include "dsp/decimators.h"
@ -29,12 +27,12 @@
class AudioFifo; class AudioFifo;
class AudioInputThread : public QThread { class AudioInputWorker : public QObject {
Q_OBJECT Q_OBJECT
public: public:
AudioInputThread(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent = nullptr); AudioInputWorker(SampleSinkFifo* sampleFifo, AudioFifo *fifo, QObject* parent = nullptr);
~AudioInputThread(); ~AudioInputWorker();
void startWork(); void startWork();
void stopWork(); void stopWork();
@ -45,8 +43,6 @@ public:
private: private:
AudioFifo* m_fifo; AudioFifo* m_fifo;
QMutex m_startWaitMutex;
QWaitCondition m_startWaiter;
bool m_running; bool m_running;
unsigned int m_log2Decim; unsigned int m_log2Decim;
AudioInputSettings::IQMapping m_iqMapping; AudioInputSettings::IQMapping m_iqMapping;
@ -56,11 +52,10 @@ private:
SampleSinkFifo* m_sampleFifo; SampleSinkFifo* m_sampleFifo;
Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 16, true> m_decimatorsIQ; Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 16, true> m_decimatorsIQ;
void run();
void workIQ(unsigned int nbRead); void workIQ(unsigned int nbRead);
private slots: private slots:
void handleAudio(); void handleAudio();
}; };
#endif // INCLUDE_AUDIOINPUTTHREAD_H #endif // INCLUDE_AUDIOINPUTWORKER_H