AlbumCoverLoader: Process tasks using timer

Helps reduce memory growth.
This commit is contained in:
Jonas Kvinge 2024-09-03 22:01:13 +02:00
parent 82079fcf70
commit 155485173b
2 changed files with 28 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Strawberry Music Player * Strawberry Music Player
* Copyright 2019-2023, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
* *
* Strawberry is free software: you can redistribute it and/or modify * Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -18,6 +18,7 @@
*/ */
#include <memory> #include <memory>
#include <chrono>
#include <QtGlobal> #include <QtGlobal>
#include <QObject> #include <QObject>
@ -31,6 +32,7 @@
#include <QUrl> #include <QUrl>
#include <QFile> #include <QFile>
#include <QImage> #include <QImage>
#include <QTimer>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
@ -44,6 +46,7 @@
#include "albumcoverloaderresult.h" #include "albumcoverloaderresult.h"
#include "albumcoverimageresult.h" #include "albumcoverimageresult.h"
using namespace std::literals::chrono_literals;
using std::make_shared; using std::make_shared;
namespace { namespace {
@ -53,6 +56,7 @@ constexpr int kMaxRedirects = 3;
AlbumCoverLoader::AlbumCoverLoader(QObject *parent) AlbumCoverLoader::AlbumCoverLoader(QObject *parent)
: QObject(parent), : QObject(parent),
network_(new NetworkAccessManager(this)), network_(new NetworkAccessManager(this)),
timer_process_tasks_(new QTimer(this)),
stop_requested_(false), stop_requested_(false),
load_image_async_id_(1), load_image_async_id_(1),
original_thread_(nullptr) { original_thread_(nullptr) {
@ -61,6 +65,10 @@ AlbumCoverLoader::AlbumCoverLoader(QObject *parent)
original_thread_ = thread(); 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() { void AlbumCoverLoader::ExitAsync() {
@ -165,18 +173,31 @@ quint64 AlbumCoverLoader::EnqueueTask(TaskPtr task) {
tasks_.enqueue(task); tasks_.enqueue(task);
} }
QMetaObject::invokeMethod(this, &AlbumCoverLoader::ProcessTasks, Qt::QueuedConnection); QMetaObject::invokeMethod(this, &AlbumCoverLoader::StartProcessTasks, Qt::QueuedConnection);
return task->id; return task->id;
} }
void AlbumCoverLoader::StartProcessTasks() {
if (!timer_process_tasks_->isActive()) {
timer_process_tasks_->start();
}
}
void AlbumCoverLoader::ProcessTasks() { void AlbumCoverLoader::ProcessTasks() {
TaskPtr task; TaskPtr task;
{ {
QMutexLocker l(&mutex_load_image_async_); 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(); task = tasks_.dequeue();
} }

View File

@ -1,6 +1,6 @@
/* /*
* Strawberry Music Player * Strawberry Music Player
* Copyright 2019-2023, Jonas Kvinge <jonas@jkvinge.net> * Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
* *
* Strawberry is free software: you can redistribute it and/or modify * Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -39,6 +39,7 @@
#include "albumcoverimageresult.h" #include "albumcoverimageresult.h"
class QThread; class QThread;
class QTimer;
class QNetworkReply; class QNetworkReply;
class NetworkAccessManager; class NetworkAccessManager;
@ -119,11 +120,13 @@ class AlbumCoverLoader : public QObject {
private Q_SLOTS: private Q_SLOTS:
void Exit(); void Exit();
void StartProcessTasks();
void ProcessTasks(); void ProcessTasks();
void LoadRemoteImageFinished(QNetworkReply *reply, AlbumCoverLoader::TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url); void LoadRemoteImageFinished(QNetworkReply *reply, AlbumCoverLoader::TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
private: private:
SharedPtr<NetworkAccessManager> network_; SharedPtr<NetworkAccessManager> network_;
QTimer *timer_process_tasks_;
bool stop_requested_; bool stop_requested_;
QMutex mutex_load_image_async_; QMutex mutex_load_image_async_;
QQueue<TaskPtr> tasks_; QQueue<TaskPtr> tasks_;