diff --git a/src/covermanager/albumcoverloader.cpp b/src/covermanager/albumcoverloader.cpp index 1e7233233..d6664075b 100644 --- a/src/covermanager/albumcoverloader.cpp +++ b/src/covermanager/albumcoverloader.cpp @@ -1,6 +1,6 @@ /* * Strawberry Music Player - * Copyright 2019-2023, Jonas Kvinge + * Copyright 2019-2024, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -31,6 +32,7 @@ #include #include #include +#include #include #include @@ -44,6 +46,7 @@ #include "albumcoverloaderresult.h" #include "albumcoverimageresult.h" +using namespace std::literals::chrono_literals; using std::make_shared; namespace { @@ -53,6 +56,7 @@ constexpr int kMaxRedirects = 3; AlbumCoverLoader::AlbumCoverLoader(QObject *parent) : QObject(parent), network_(new NetworkAccessManager(this)), + timer_process_tasks_(new QTimer(this)), stop_requested_(false), load_image_async_id_(1), original_thread_(nullptr) { @@ -61,6 +65,10 @@ AlbumCoverLoader::AlbumCoverLoader(QObject *parent) original_thread_ = thread(); + timer_process_tasks_->setSingleShot(false); + timer_process_tasks_->setInterval(10ms); + QObject::connect(timer_process_tasks_, &QTimer::timeout, this, &AlbumCoverLoader::ProcessTasks); + } void AlbumCoverLoader::ExitAsync() { @@ -165,18 +173,31 @@ quint64 AlbumCoverLoader::EnqueueTask(TaskPtr task) { tasks_.enqueue(task); } - QMetaObject::invokeMethod(this, &AlbumCoverLoader::ProcessTasks, Qt::QueuedConnection); + QMetaObject::invokeMethod(this, &AlbumCoverLoader::StartProcessTasks, Qt::QueuedConnection); return task->id; } +void AlbumCoverLoader::StartProcessTasks() { + + if (!timer_process_tasks_->isActive()) { + timer_process_tasks_->start(); + } + +} + void AlbumCoverLoader::ProcessTasks() { TaskPtr task; { QMutexLocker l(&mutex_load_image_async_); - if (tasks_.isEmpty()) return; + if (tasks_.isEmpty()) { + if (timer_process_tasks_->isActive()) { + timer_process_tasks_->stop(); + } + return; + } task = tasks_.dequeue(); } diff --git a/src/covermanager/albumcoverloader.h b/src/covermanager/albumcoverloader.h index cda6d847c..6ea2117b5 100644 --- a/src/covermanager/albumcoverloader.h +++ b/src/covermanager/albumcoverloader.h @@ -1,6 +1,6 @@ /* * Strawberry Music Player - * Copyright 2019-2023, Jonas Kvinge + * Copyright 2019-2024, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +39,7 @@ #include "albumcoverimageresult.h" class QThread; +class QTimer; class QNetworkReply; class NetworkAccessManager; @@ -119,11 +120,13 @@ class AlbumCoverLoader : public QObject { private Q_SLOTS: void Exit(); + void StartProcessTasks(); void ProcessTasks(); void LoadRemoteImageFinished(QNetworkReply *reply, AlbumCoverLoader::TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url); private: SharedPtr network_; + QTimer *timer_process_tasks_; bool stop_requested_; QMutex mutex_load_image_async_; QQueue tasks_;