Skip to content

Commit

Permalink
Separate decoding and analyzing pools. Fix crash on deleting analyzed…
Browse files Browse the repository at this point in the history
… object.
  • Loading branch information
skotopes committed Aug 9, 2021
1 parent 0d3e0d7 commit 167d3db
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
12 changes: 11 additions & 1 deletion arfariusapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <QFileOpenEvent>

ArfariusApplication::ArfariusApplication(int & argc, char ** argv) :
QApplication(argc, argv)
QApplication(argc, argv),
decoder_thread_pool(this),
analyze_thread_pool(this)
{
}

Expand All @@ -16,3 +18,11 @@ bool ArfariusApplication::event(QEvent *event)
return QApplication::event(event);
}
}

QThreadPool* ArfariusApplication::getDecoderThreadPool() {
return &decoder_thread_pool;
}

QThreadPool* ArfariusApplication::getAnalyzeThreadPool() {
return &analyze_thread_pool;
}
10 changes: 10 additions & 0 deletions arfariusapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
#define WMPAPPLICATION_H

#include <QApplication>
#include <QThreadPool>
#include <QUrl>

#define arfariusApp (static_cast<ArfariusApplication *>(QCoreApplication::instance()))

class ArfariusApplication : public QApplication
{
Q_OBJECT

QThreadPool decoder_thread_pool;
QThreadPool analyze_thread_pool;

public:
explicit ArfariusApplication(int &argc, char **argv);
bool event(QEvent *);

QThreadPool *getDecoderThreadPool();
QThreadPool *getAnalyzeThreadPool();

signals:
void fileDropped(QUrl);

Expand Down
37 changes: 22 additions & 15 deletions player.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "player.h"

#include "arfariusapplication.h"
#include "playlistmodel.h"
#include "playlistitem.h"

Expand Down Expand Up @@ -128,14 +129,17 @@ void Player::ejectFile()
if (file) {
file->cancelDecoding();

eject_future = QtConcurrent::run([this](){
av_sample_t *buffer = new av_sample_t[4096];
while (state != Player::PLAY && file) {
qDebug() << this << "ejectFile(): pumping samples";
pull(buffer, 4096);
eject_future = QtConcurrent::run(
arfariusApp->getDecoderThreadPool(),
[this](){
av_sample_t *buffer = new av_sample_t[4096];
while (state != Player::PLAY && file) {
qDebug() << this << "ejectFile(): pumping samples";
pull(buffer, 4096);
}
delete [] buffer ;
}
delete [] buffer ;
});
);
file_future.waitForFinished();
eject_future.waitForFinished();
}
Expand All @@ -161,15 +165,18 @@ void Player::updateItem(PlayListItem *item)
file->setSamplerate(_sample_rate);
file->setChannels(_channels);
file->connectOutput(this);
file_future = QtConcurrent::run([this](){
file->decode();
delete file; file = 0;
if (quiet) {
quiet = false;
} else {
emit trackEnded();
file_future = QtConcurrent::run(
arfariusApp->getDecoderThreadPool(),
[this](){
file->decode();
delete file; file = 0;
if (quiet) {
quiet = false;
} else {
emit trackEnded();
}
}
});
);

if (state != Player::PLAY) {
startStream();
Expand Down
13 changes: 11 additions & 2 deletions playlistitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <taglib/tstring.h>
#include <taglib/tag.h>

#include "arfariusapplication.h"
#include "avexception.h"
#include "avsplitter.h"
#include "avhistogram.h"
Expand Down Expand Up @@ -59,7 +60,7 @@ TagLib::String toString(QString str) {
*/

PlayListItem::PlayListItem(QUrl s) :
QObject(), source(s), artist(), title(), album(), duration(-1)
QObject(), source(s), artist(), title(), album(), duration(-1), busy(false)
{

}
Expand All @@ -68,6 +69,10 @@ PlayListItem::~PlayListItem()
{
}

bool PlayListItem::isBusy() {
return busy;
}

bool PlayListItem::isValid()
{
try {
Expand Down Expand Up @@ -235,7 +240,11 @@ bool PlayListItem::hasHistogram()
void PlayListItem::ensureHistogram()
{
if (!hasHistogram()) {
QtConcurrent::run([&]{ auto hack = this; analyze(); hack = nullptr; });
busy = true;
QtConcurrent::run(
arfariusApp->getAnalyzeThreadPool(),
[this]{ analyze(); busy = false; }
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions playlistitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PLAYLISTITEM_H

#include <QObject>
#include <QFuture>
#include <QUrl>

class AVFile;
Expand All @@ -13,6 +14,7 @@ class PlayListItem : public QObject
PlayListItem(QUrl s);
virtual ~PlayListItem();

bool isBusy();
bool isValid();
bool isLocalFile();

Expand Down Expand Up @@ -45,6 +47,8 @@ class PlayListItem : public QObject
QString album;
int duration;

bool busy;

QString getArtist();
QString getTitle();
QString getAlbum();
Expand Down
8 changes: 5 additions & 3 deletions playlistmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "playlistmodel.h"
#include "playlistitem.h"
#include "arfariusapplication.h"

#include <QProgressDialog>
#include <QDirIterator>
Expand Down Expand Up @@ -93,9 +94,10 @@ bool PlayListModel::removeRows(int row, int count, const QModelIndex &parent)
current_item = items[current];
}

for (int i = 0; i < count; ++i) {
items[row]->deleteLater();
items.removeAt(row);
for (int i = row; i < row+count; i++) {
while(items[i]->isBusy()) arfariusApp->processEvents(QEventLoop::AllEvents, 10);
items[i]->deleteLater();
items.removeAt(i);
}

if (current_item) {
Expand Down

0 comments on commit 167d3db

Please sign in to comment.