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:
parent
ae9824e26a
commit
6ea20336c2
@ -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) {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user