CddaSongLoader: Only run one song loading thread at once.

- CddaSongLoader destructor waits for thread to end.
- Added flag to interrupt LoadSongsFromCdda
- Only start song loading if not already running
- Removed (now obsolete) mutex
This commit is contained in:
Lukas Prediger 2021-06-01 19:26:13 +03:00
parent a5853a1243
commit c3f8c010ec
No known key found for this signature in database
GPG Key ID: DF7C5DA2E98A3428
2 changed files with 21 additions and 7 deletions

View File

@ -27,12 +27,18 @@
#include "core/timeconstants.h"
CddaSongLoader::CddaSongLoader(const QUrl& url, QObject* parent)
: QObject(parent), url_(url), cdda_(nullptr) {
: QObject(parent), url_(url), cdda_(nullptr), may_load_(true) {
connect(this, SIGNAL(MusicBrainzDiscIdLoaded(const QString&)),
SLOT(LoadAudioCDTags(const QString&)));
}
CddaSongLoader::~CddaSongLoader() {}
CddaSongLoader::~CddaSongLoader() {
// The LoadSongsFromCdda methods runs concurrently in a thread and we need to
// wait for it to terminate. There's no guarantee that it has terminated when
// destructor is invoked.
may_load_ = false;
loading_future_.waitForFinished();
}
QUrl CddaSongLoader::GetUrlFromTrack(int track_number) const {
QString track;
@ -45,11 +51,15 @@ QUrl CddaSongLoader::GetUrlFromTrack(int track_number) const {
}
void CddaSongLoader::LoadSongs() {
QtConcurrent::run(this, &CddaSongLoader::LoadSongsFromCdda);
// only dispatch a new thread for loading tracks if not already running.
if (!loading_future_.isRunning()) {
loading_future_ =
QtConcurrent::run(this, &CddaSongLoader::LoadSongsFromCdda);
}
}
void CddaSongLoader::LoadSongsFromCdda() {
QMutexLocker locker(&mutex_load_);
if (!may_load_) return;
// Create gstreamer cdda element
GError* error = nullptr;
@ -118,7 +128,8 @@ void CddaSongLoader::LoadSongsFromCdda() {
GstMessage* msg = nullptr;
GstMessageType msg_filter =
static_cast<GstMessageType>(GST_MESSAGE_TOC | GST_MESSAGE_TAG);
while (msg_filter &&
QString musicbrainz_discid;
while (may_load_ && msg_filter &&
(msg = gst_bus_timed_pop_filtered(GST_ELEMENT_BUS(pipeline),
10 * GST_SECOND, msg_filter))) {
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_TOC) {

View File

@ -18,13 +18,15 @@
#ifndef CDDASONGLOADER_H
#define CDDASONGLOADER_H
#include <QMutex>
#include <QFuture>
#include <QObject>
#include <QUrl>
// These must come after Qt includes (issue 3247)
#include <gst/audio/gstaudiocdsrc.h>
#include <atomic>
#include "core/song.h"
#include "musicbrainz/musicbrainzclient.h"
@ -61,7 +63,8 @@ class CddaSongLoader : public QObject {
QUrl url_;
GstElement* cdda_;
QMutex mutex_load_;
QFuture<void> loading_future_;
std::atomic<bool> may_load_;
};
#endif // CDDASONGLOADER_H