more multithreaded audio playback

This commit is contained in:
itsmattkc
2018-07-26 15:55:52 +01:00
parent e7a864da50
commit 2cf3ecffc4
2 changed files with 82 additions and 38 deletions
+65 -35
View File
@@ -29,11 +29,22 @@ ViewerWidget::ViewerWidget(QWidget *parent) : QOpenGLWidget(parent) {
format.setDepthBufferSize(24); format.setDepthBufferSize(24);
setFormat(format); setFormat(format);
// start audio sending thread
connect(&audio_sender_thread, SIGNAL(finished()), &audio_sender_thread, SLOT(deleteLater()));
audio_sender_thread.start();
// error handler - retries after 250ms if we couldn't get the entire image // error handler - retries after 250ms if we couldn't get the entire image
retry_timer.setInterval(250); retry_timer.setInterval(250);
connect(&retry_timer, SIGNAL(timeout()), this, SLOT(retry())); connect(&retry_timer, SIGNAL(timeout()), this, SLOT(retry()));
} }
ViewerWidget::~ViewerWidget() {
audio_sender_thread.close = true;
audio_sender_thread.cond.wakeAll();
audio_sender_thread.lock.lock();
audio_sender_thread.lock.unlock();
}
void ViewerWidget::deleteFunction() { void ViewerWidget::deleteFunction() {
// destroy all textures as well // destroy all textures as well
for (int i=0;i<sequence->clip_count();i++) { for (int i=0;i<sequence->clip_count();i++) {
@@ -193,7 +204,60 @@ void ViewerWidget::compose_sequence(Sequence *s, bool render_audio) {
} }
} }
int ViewerWidget::send_audio_to_output(int offset, int max) { void ViewerWidget::paintGL() {
bool loop = true;
while (loop) {
loop = false;
texture_failed = false;
if (multithreaded) retry_timer.stop();
glClear(GL_COLOR_BUFFER_BIT);
// compose video preview
compose_sequence(sequence, (panel_timeline->playing || force_audio));
// send audio to IO device
if (panel_timeline->playing) {
audio_sender_thread.cond.wakeAll();
}
if (texture_failed) {
if (multithreaded) {
retry_timer.start();
} else {
qDebug() << "[INFO] Texture failed - looping";
loop = true;
}
}
}
}
AudioSenderThread::AudioSenderThread() : close(false) {
lock.lock();
}
AudioSenderThread::~AudioSenderThread() {
lock.unlock();
}
void AudioSenderThread::run() {
while (true) {
cond.wait(&lock);
if (close) {
break;
} else {
int adjusted_read_index = audio_ibuffer_read%audio_ibuffer_size;
int max_write = audio_ibuffer_size - adjusted_read_index;
if (send_audio_to_output(adjusted_read_index, max_write) == max_write) {
// got all the bytes, write again
send_audio_to_output(0, audio_ibuffer_size);
}
}
}
}
int AudioSenderThread::send_audio_to_output(int offset, int max) {
// send audio to device // send audio to device
int actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max); int actual_write = audio_io_device->write((const char*) audio_ibuffer+offset, max);
audio_ibuffer_read += actual_write; audio_ibuffer_read += actual_write;
@@ -225,37 +289,3 @@ int ViewerWidget::send_audio_to_output(int offset, int max) {
return actual_write; return actual_write;
} }
void ViewerWidget::paintGL() {
bool loop = true;
while (loop) {
loop = false;
texture_failed = false;
if (multithreaded) retry_timer.stop();
glClear(GL_COLOR_BUFFER_BIT);
// compose video preview
compose_sequence(sequence, (panel_timeline->playing || force_audio));
// send audio to IO device
if (panel_timeline->playing) {
int adjusted_read_index = audio_ibuffer_read%audio_ibuffer_size;
int max_write = audio_ibuffer_size - adjusted_read_index;
if (send_audio_to_output(adjusted_read_index, max_write) == max_write) {
// got all the bytes, write again
send_audio_to_output(0, audio_ibuffer_size);
}
}
if (texture_failed) {
if (multithreaded) {
retry_timer.start();
} else {
qDebug() << "[INFO] Texture failed - looping";
loop = true;
}
}
}
}
+17 -3
View File
@@ -6,17 +6,32 @@
#include <QMatrix4x4> #include <QMatrix4x4>
#include <QOpenGLTexture> #include <QOpenGLTexture>
#include <QTimer> #include <QTimer>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
struct Clip; struct Clip;
struct Sequence; struct Sequence;
class Viewer; class AudioSenderThread : public QThread {
public:
AudioSenderThread();
~AudioSenderThread();
void run();
QWaitCondition cond;
bool close;
QMutex lock;
private:
QVector<qint16> samples;
int send_audio_to_output(int offset, int max);
};
class ViewerWidget : public QOpenGLWidget, public QOpenGLFunctions class ViewerWidget : public QOpenGLWidget, public QOpenGLFunctions
{ {
Q_OBJECT Q_OBJECT
public: public:
ViewerWidget(QWidget *parent = 0); ViewerWidget(QWidget *parent = 0);
~ViewerWidget();
bool multithreaded; bool multithreaded;
bool force_audio; bool force_audio;
@@ -29,12 +44,11 @@ protected:
// void resizeGL(int w, int h); // void resizeGL(int w, int h);
private: private:
QTimer retry_timer; QTimer retry_timer;
QVector<qint16> samples; AudioSenderThread audio_sender_thread;
private slots: private slots:
void retry(); void retry();
void deleteFunction(); void deleteFunction();
void compose_sequence(Sequence* s, bool render_audio); void compose_sequence(Sequence* s, bool render_audio);
int send_audio_to_output(int offset, int max);
}; };
#endif // VIEWERWIDGET_H #endif // VIEWERWIDGET_H