Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncImageProvider: Retrieve Cover Art from database instead of file system #4674

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 61 additions & 25 deletions src/qml/asyncimageprovider.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#include "qml/asyncimageprovider.h"

#include "library/coverartcache.h"
#include "track/track.h"

namespace {
const QString kCoverArtPrefix = QStringLiteral("coverart/");
}

namespace mixxx {

namespace qml {

AsyncImageResponse::AsyncImageResponse(const QString& id, const QSize& requestedSize)
: m_id(id), m_requestedSize(requestedSize) {
AsyncImageResponse::AsyncImageResponse(
QString id,
QSize requestedSize,
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager)
: m_id(std::move(id)),
m_requestedSize(requestedSize),
m_pTrackCollectionManager(std::move(pTrackCollectionManager)) {
setAutoDelete(false);
}

Expand All @@ -19,39 +26,68 @@ QQuickTextureFactory* AsyncImageResponse::textureFactory() const {
}

void AsyncImageResponse::run() {
if (m_id.startsWith(kCoverArtPrefix)) {
QString trackLocation = AsyncImageProvider::coverArtUrlIdToTrackLocation(m_id);

// TODO: This code does not allow to override embedded cover art with
// a custom image, which is possible in Mixxx. We need to access the
// actual CoverInfo of the track instead of constructing a default
// instance on the fly.
//
// Unfortunately, TrackCollectionManager::getTrackByRef will not work
// when called from another thread (like this one). We need a solution
// for that.
CoverInfo coverInfo(CoverInfoRelative(), trackLocation);
coverInfo.type = CoverInfoRelative::METADATA;
CoverInfo::LoadedImage loadedImage = coverInfo.loadImage();
if (loadedImage.result != CoverInfo::LoadedImage::Result::Ok) {
coverInfo.type = CoverInfoRelative::FILE;
loadedImage = coverInfo.loadImage();
}
m_image = loadedImage.image;
if (!m_id.startsWith(kCoverArtPrefix)) {
qWarning() << "ImageProvider: Unsupported ID" << m_id;
emit finished();
return;
}
const QString trackLocation = AsyncImageProvider::coverArtUrlIdToTrackLocation(m_id);
const auto trackRef = TrackRef::fromFilePath(trackLocation);

// TODO: Only load CoverInfo from TrackCollectionManager
// instead of the entire, stateful track object.
TrackPointer pTrack;
if (QThread::currentThread() != m_pTrackCollectionManager->thread()) {
QMetaObject::invokeMethod(
m_pTrackCollectionManager.get(),
[this, trackRef] {
return m_pTrackCollectionManager->getTrackByRef(trackRef);
},
// This invocation will block the current thread!
Qt::BlockingQueuedConnection,
&pTrack);
} else {
qWarning() << "ImageProvider: Unknown ID " << m_id;
pTrack = m_pTrackCollectionManager->getTrackByRef(trackRef);
}
if (!pTrack) {
qWarning() << "ImageProvider: Failed to load track" << trackRef;
emit finished();
return;
}
const auto coverInfo = CoverInfo(pTrack->getCoverInfo(), trackLocation);
// Release the track reference asap, i.e. before loading the image
pTrack.reset();

if (!m_image.isNull() && m_requestedSize.isValid()) {
m_image = m_image.scaled(m_requestedSize);
const CoverInfo::LoadedImage loadedImage = coverInfo.loadImage();
switch (loadedImage.result) {
case CoverInfo::LoadedImage::Result::NoImage:
break;
case CoverInfo::LoadedImage::Result::Ok:
DEBUG_ASSERT(!loadedImage.image.isNull());
if (m_requestedSize.isValid()) {
m_image = loadedImage.image.scaled(m_requestedSize);
} else {
m_image = loadedImage.image;
}
break;
default:
qWarning() << "ImageProvider: Failed to load cover art" << trackRef;
break;
}

emit finished();
}

AsyncImageProvider::AsyncImageProvider(
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager)
: QQuickAsyncImageProvider(),
m_pTrackCollectionManager(pTrackCollectionManager) {
}

QQuickImageResponse* AsyncImageProvider::requestImageResponse(
const QString& id, const QSize& requestedSize) {
AsyncImageResponse* response = new AsyncImageResponse(id, requestedSize);
AsyncImageResponse* response = new AsyncImageResponse(
id, requestedSize, m_pTrackCollectionManager);
pool.start(response);
return response;
}
Expand Down
15 changes: 14 additions & 1 deletion src/qml/asyncimageprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,38 @@
#include <QSize>
#include <QString>
#include <QThreadPool>
#include <memory>

#include "library/coverart.h"
#include "library/trackcollectionmanager.h"

namespace mixxx {
namespace qml {

class AsyncImageResponse : public QQuickImageResponse, public QRunnable {
Q_OBJECT
public:
AsyncImageResponse(const QString& id, const QSize& requestedSize);
AsyncImageResponse(
QString id,
QSize requestedSize,
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager);

QQuickTextureFactory* textureFactory() const override;

void run() override;

private:
QString m_id;
QSize m_requestedSize;
std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;

QImage m_image;
};

class AsyncImageProvider : public QQuickAsyncImageProvider {
public:
AsyncImageProvider(std::shared_ptr<TrackCollectionManager> pTrackCollectionManager);

QQuickImageResponse* requestImageResponse(
const QString& id, const QSize& requestedSize) override;

Expand All @@ -33,6 +45,7 @@ class AsyncImageProvider : public QQuickAsyncImageProvider {

private:
QThreadPool pool;
std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;
};

} // namespace qml
Expand Down
3 changes: 2 additions & 1 deletion src/qml/qmlapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void QmlApplication::loadQml(const QString& path) {
m_pAppEngine->addImportPath(QStringLiteral(":/mixxx.org/imports"));

// No memory leak here, the QQmlEngine takes ownership of the provider
QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider();
QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider(
m_pCoreServices->getTrackCollectionManager());
m_pAppEngine->addImageProvider(AsyncImageProvider::kProviderName, pImageProvider);

m_pAppEngine->load(path);
Expand Down