Use shared pointers for moodbar pipelines

Possible fix for #1633
This commit is contained in:
Jonas Kvinge
2025-01-05 18:28:41 +01:00
parent 36a8ab49a0
commit 8302a95bc1
8 changed files with 102 additions and 68 deletions

View File

@@ -2,7 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, David Sansome <me@davidsansome.com>
* Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2019-2025, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
#include <QByteArray>
#include <QUrl>
#include "includes/shared_ptr.h"
#include "core/song.h"
#include "core/settings.h"
#include "core/player.h"
@@ -33,6 +34,8 @@
#include "moodbarloader.h"
#include "moodbarpipeline.h"
using std::make_shared;
MoodbarController::MoodbarController(const SharedPtr<Player> player, const SharedPtr<MoodbarLoader> moodbar_loader, QObject *parent)
: QObject(parent),
player_(player),
@@ -56,25 +59,27 @@ void MoodbarController::CurrentSongChanged(const Song &song) {
if (!enabled_) return;
QByteArray data;
MoodbarPipeline *pipeline = nullptr;
const MoodbarLoader::Result result = moodbar_loader_->Load(song.url(), song.has_cue(), &data, &pipeline);
switch (result) {
case MoodbarLoader::Result::CannotLoad:
const MoodbarLoader::LoadResult load_result = moodbar_loader_->Load(song.url(), song.has_cue());
switch (load_result.status) {
case MoodbarLoader::LoadStatus::CannotLoad:
Q_EMIT CurrentMoodbarDataChanged(QByteArray());
break;
case MoodbarLoader::Result::Loaded:
Q_EMIT CurrentMoodbarDataChanged(data);
case MoodbarLoader::LoadStatus::Loaded:
Q_EMIT CurrentMoodbarDataChanged(load_result.data);
break;
case MoodbarLoader::Result::WillLoadAsync:
// Emit an empty array for now so the GUI reverts to a normal progress
// bar. Our slot will be called when the data is actually loaded.
case MoodbarLoader::LoadStatus::WillLoadAsync:
// Emit an empty array for now so the GUI reverts to a normal progressbar. Our slot will be called when the data is actually loaded.
Q_EMIT CurrentMoodbarDataChanged(QByteArray());
QObject::connect(pipeline, &MoodbarPipeline::Finished, this, [this, pipeline, song]() { AsyncLoadComplete(pipeline, song.url()); });
MoodbarPipelinePtr pipeline = load_result.pipeline;
Q_ASSERT(pipeline);
SharedPtr<QMetaObject::Connection> connection = make_shared<QMetaObject::Connection>();
*connection = QObject::connect(&*pipeline, &MoodbarPipeline::Finished, this, [this, connection, pipeline, song]() {
AsyncLoadComplete(pipeline, song.url());
QObject::disconnect(*connection);
});
break;
}
@@ -88,7 +93,7 @@ void MoodbarController::PlaybackStopped() {
}
void MoodbarController::AsyncLoadComplete(MoodbarPipeline *pipeline, const QUrl &url) {
void MoodbarController::AsyncLoadComplete(MoodbarPipelinePtr pipeline, const QUrl &url) {
// Is this song still playing?
PlaylistItemPtr current_item = player_->GetCurrentItem();