1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 08:04:49 -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
audioinputsettings.cpp
audioinputwebapiadapter.cpp
audioinputthread.cpp
audioinputworker.cpp
)
set(audioinput_HEADERS
@ -13,7 +13,7 @@ set(audioinput_HEADERS
audioinputplugin.h
audioinputsettings.h
audioinputwebapiadapter.h
audioinputthread.h
audioinputworker.h
)
include_directories(

View File

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

View File

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

View File

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

View File

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