Moved audio to separate thread.

merge-requests/1/head
Elliott Liggett 2021-02-08 00:31:48 -08:00
rodzic 89fe648868
commit 6fee18df9a
5 zmienionych plików z 137 dodań i 17 usunięć

59
rxaudiohandler.cpp 100644
Wyświetl plik

@ -0,0 +1,59 @@
#include "rxaudiohandler.h"
rxAudioHandler::rxAudioHandler()
{
}
rxAudioHandler::~rxAudioHandler()
{
audio->stop();
delete audio;
delete buffer;
}
void rxAudioHandler::process()
{
qDebug() << "rxAudio Handler created.";
}
void rxAudioHandler::setup(const QAudioFormat format, const int bufferSize)
{
this->format = format;
this->bufferSize = bufferSize;
buffer = new QBuffer();
buffer->open(QIODevice::ReadWrite);
audio = new QAudioOutput(format);
audio->setBufferSize(bufferSize);
buffer->seek(0);
audio->start(buffer);
}
void rxAudioHandler::incomingAudio(const QByteArray data, const int size)
{
buffer->buffer().remove(0,buffer->pos());
buffer->seek(buffer->size());
buffer->write(data.constData(), size);
buffer->seek(0);
}
void rxAudioHandler::changeBufferSize(const int newSize)
{
// TODO: make a way to change the buffer size.
// possibly deleting the buffer and re-creating
audio->setBufferSize(newSize);
}
void rxAudioHandler::getBufferSize()
{
emit sendBufferSize(buffer->size());
}
void rxAudioHandler::getAudioBufferSize()
{
emit sendAudioBufferSize(audio->bufferSize());
}

44
rxaudiohandler.h 100644
Wyświetl plik

@ -0,0 +1,44 @@
#ifndef RXAUDIOHANDLER_H
#define RXAUDIOHANDLER_H
#include <QObject>
#include <QtMultimedia/QAudioOutput>
#include <QBuffer>
#include <QDebug>
class rxAudioHandler : public QObject
{
Q_OBJECT
public:
rxAudioHandler();
~rxAudioHandler();
public slots:
void process();
void setup(const QAudioFormat format, const int bufferSize);
void incomingAudio(const QByteArray data, const int size);
void changeBufferSize(const int newSize);
void getBufferSize();
void getAudioBufferSize();
signals:
void audioMessage(QString message);
void sendBufferSize(int newSize);
void sendAudioBufferSize(int newSize);
private:
QBuffer* buffer;
QAudioOutput* audio;
QAudioFormat format;
int bufferSize;
};
#endif // RXAUDIOHANDLER_H

Wyświetl plik

@ -576,12 +576,15 @@ udpAudio::udpAudio(QHostAddress local, QHostAddress ip, int aport)
qDebug() << "----- done with audio info -----";
}
buffer = new QBuffer();
buffer->open(QIODevice::ReadWrite);
audio = new QAudioOutput(format);
audio->setBufferSize(10000); // TODO: add preference, maybe UI too. 20210205: connection was wifi --> cellular --> internet --> rig
buffer->seek(0);
audio->start(buffer);
rxaudio = new rxAudioHandler();
rxAudioThread = new QThread(this);
rxaudio->moveToThread(rxAudioThread);
connect(this,SIGNAL(setupAudio(QAudioFormat,int)), rxaudio, SLOT(setup(QAudioFormat,int)));
connect(this, SIGNAL(haveAudioData(QByteArray,int)), rxaudio, SLOT(incomingAudio(QByteArray,int)));
rxaudio->setup(format, 10000);
}
@ -596,6 +599,14 @@ udpAudio::~udpAudio()
{
delete buffer;
}
if(rxaudio != Q_NULLPTR)
{
delete rxaudio;
}
if(rxAudioThread != Q_NULLPTR)
{
delete rxAudioThread;
}
}
void udpAudio::DataReceived()
@ -638,15 +649,7 @@ void udpAudio::DataReceived()
lastReceivedSeq = gotSeq;
//qDebug() << "Got Audio Sequence: (" << r.length() << ") " << gotSeq;
// Delete contents of buffer up to existing pos()
buffer->buffer().remove(0,buffer->pos());
// Seek to end of curent buffer
buffer->seek(buffer->size());
// Append to end of buffer
buffer->write(r.mid(24).constData(), r.mid(24).length());
// Seek to start of buffer.
buffer->seek(0);
emit haveAudioData(r.mid(24), r.mid(24).length());
}
break;
}

Wyświetl plik

@ -15,10 +15,14 @@
// Needed for audio
#include <QtMultimedia/QAudioOutput>
#include <QBuffer>
#include <QThread>
#include <QDebug>
#include "rxaudiohandler.h"
// Parent class that contains all common items.
class udpBase : public QObject
{
@ -118,6 +122,10 @@ public:
udpAudio(QHostAddress local, QHostAddress ip, int aport);
~udpAudio();
QAudioOutput* audio;
signals:
void haveAudioData(QByteArray data, int length);
void setupAudio(const QAudioFormat format, const int bufferSize);
private:
void DataReceived();
@ -128,6 +136,10 @@ private:
bool sentPacketConnect2 = false;
uint16_t sendAudioSeq = 0;
rxAudioHandler* rxaudio;
QThread* rxAudioThread;
};

Wyświetl plik

@ -80,7 +80,8 @@ SOURCES += main.cpp\
freqmemory.cpp \
rigidentities.cpp \
udphandler.cpp \
logcategories.cpp
logcategories.cpp \
rxaudiohandler.cpp
HEADERS += wfmain.h \
commhandler.h \
@ -88,7 +89,8 @@ HEADERS += wfmain.h \
freqmemory.h \
rigidentities.h \
udphandler.h \
logcategories.h
logcategories.h \
rxaudiohandler.h
FORMS += wfmain.ui