2019-04-18 15:03:01 +02:00
|
|
|
/* This file was part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Strawberry is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "moodbarloader.h"
|
|
|
|
|
|
|
|
#include <memory>
|
2021-06-20 19:04:08 +02:00
|
|
|
#include <chrono>
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QtGlobal>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QDir>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QFile>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QFileInfo>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QAbstractNetworkCache>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QNetworkDiskCache>
|
|
|
|
#include <QTimer>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QByteArray>
|
2019-04-18 15:03:01 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QSettings>
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
#include "moodbarpipeline.h"
|
|
|
|
|
|
|
|
#include "settings/moodbarsettingspage.h"
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2019-04-18 15:03:01 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
MoodbarLoader::MoodbarLoader(Application *app, QObject *parent)
|
2019-04-18 15:03:01 +02:00
|
|
|
: QObject(parent),
|
|
|
|
cache_(new QNetworkDiskCache(this)),
|
|
|
|
thread_(new QThread(this)),
|
|
|
|
kMaxActiveRequests(qMax(1, QThread::idealThreadCount() / 2)),
|
|
|
|
save_(false) {
|
|
|
|
|
|
|
|
cache_->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/moodbar");
|
|
|
|
cache_->setMaximumCacheSize(60 * 1024 * 1024); // 60MB - enough for 20,000 moodbars
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(app, &Application::SettingsChanged, this, &MoodbarLoader::ReloadSettings);
|
2019-04-18 15:03:01 +02:00
|
|
|
ReloadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MoodbarLoader::~MoodbarLoader() {
|
|
|
|
thread_->quit();
|
|
|
|
thread_->wait(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarLoader::ReloadSettings() {
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(MoodbarSettingsPage::kSettingsGroup);
|
|
|
|
save_ = s.value("save", false).toBool();
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
MaybeTakeNextRequest();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QStringList MoodbarLoader::MoodFilenames(const QString &song_filename) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
const QFileInfo file_info(song_filename);
|
|
|
|
const QString dir_path(file_info.dir().path());
|
2020-09-29 17:30:21 +02:00
|
|
|
const QString mood_filename = file_info.completeBaseName() + ".mood";
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
return QStringList() << dir_path + "/." + mood_filename << dir_path + "/" + mood_filename;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-05 18:58:02 +01:00
|
|
|
MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, QByteArray *data, MoodbarPipeline **async_pipeline) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
2022-01-05 18:58:02 +01:00
|
|
|
if (!url.isLocalFile() || has_cue) {
|
2023-02-18 14:09:27 +01:00
|
|
|
return Result::CannotLoad;
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Are we in the middle of loading this moodbar already?
|
|
|
|
if (requests_.contains(url)) {
|
|
|
|
*async_pipeline = requests_[url];
|
2023-02-18 14:09:27 +01:00
|
|
|
return Result::WillLoadAsync;
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a mood file exists for this file already
|
|
|
|
const QString filename(url.toLocalFile());
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
for (const QString &possible_mood_file : MoodFilenames(filename)) {
|
2019-04-18 15:03:01 +02:00
|
|
|
QFile f(possible_mood_file);
|
2021-08-09 23:32:26 +02:00
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
|
|
|
|
*data = f.readAll();
|
|
|
|
f.close();
|
2023-02-18 14:09:27 +01:00
|
|
|
return Result::Loaded;
|
2021-08-09 23:32:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Error) << "Failed to load moodbar data from" << possible_mood_file << f.errorString();
|
|
|
|
}
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe it exists in the cache?
|
|
|
|
std::unique_ptr<QIODevice> cache_device(cache_->data(url));
|
|
|
|
if (cache_device) {
|
|
|
|
qLog(Info) << "Loading cached moodbar data for" << filename;
|
|
|
|
*data = cache_device->readAll();
|
|
|
|
if (!data->isEmpty()) {
|
2023-02-18 14:09:27 +01:00
|
|
|
return Result::Loaded;
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!thread_->isRunning()) thread_->start(QThread::IdlePriority);
|
|
|
|
|
|
|
|
// There was no existing file, analyze the audio file and create one.
|
2021-01-26 16:48:04 +01:00
|
|
|
MoodbarPipeline *pipeline = new MoodbarPipeline(url);
|
2019-04-18 15:03:01 +02:00
|
|
|
pipeline->moveToThread(thread_);
|
2021-01-29 18:47:50 +01:00
|
|
|
QObject::connect(pipeline, &MoodbarPipeline::Finished, this, [this, pipeline, url]() { RequestFinished(pipeline, url); });
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
requests_[url] = pipeline;
|
|
|
|
queued_requests_ << url;
|
|
|
|
|
|
|
|
MaybeTakeNextRequest();
|
|
|
|
|
|
|
|
*async_pipeline = pipeline;
|
2023-02-18 14:09:27 +01:00
|
|
|
return Result::WillLoadAsync;
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoodbarLoader::MaybeTakeNextRequest() {
|
|
|
|
|
|
|
|
Q_ASSERT(QThread::currentThread() == qApp->thread());
|
|
|
|
|
2021-01-25 22:35:56 +01:00
|
|
|
if (active_requests_.count() >= kMaxActiveRequests || queued_requests_.isEmpty()) {
|
2019-04-18 15:03:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUrl url = queued_requests_.takeFirst();
|
|
|
|
active_requests_ << url;
|
|
|
|
|
|
|
|
qLog(Info) << "Creating moodbar data for" << url.toLocalFile();
|
|
|
|
QMetaObject::invokeMethod(requests_[url], "Start", Qt::QueuedConnection);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-14 18:58:24 +02:00
|
|
|
void MoodbarLoader::RequestFinished(MoodbarPipeline *request, const QUrl &url) {
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
Q_ASSERT(QThread::currentThread() == qApp->thread());
|
|
|
|
|
|
|
|
if (request->success()) {
|
|
|
|
qLog(Info) << "Moodbar data generated successfully for" << url.toLocalFile();
|
|
|
|
|
|
|
|
// Save the data in the cache
|
|
|
|
QNetworkCacheMetaData metadata;
|
|
|
|
metadata.setUrl(url);
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QIODevice *cache_file = cache_->prepare(metadata);
|
2019-04-18 15:03:01 +02:00
|
|
|
if (cache_file) {
|
2021-08-09 23:32:26 +02:00
|
|
|
if (cache_file->write(request->data()) > 0) {
|
|
|
|
cache_->insert(cache_file);
|
|
|
|
}
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the data alongside the original as well if we're configured to.
|
|
|
|
if (save_) {
|
2021-03-21 04:47:11 +01:00
|
|
|
QList<QString> mood_filenames = MoodFilenames(url.toLocalFile());
|
|
|
|
const QString mood_filename(mood_filenames[0]);
|
2019-04-18 15:03:01 +02:00
|
|
|
QFile mood_file(mood_filename);
|
|
|
|
if (mood_file.open(QIODevice::WriteOnly)) {
|
2021-08-09 23:32:26 +02:00
|
|
|
if (mood_file.write(request->data()) <= 0) {
|
|
|
|
qLog(Error) << "Error writing to mood file" << mood_filename << mood_file.errorString();
|
|
|
|
}
|
2021-07-14 20:52:57 +02:00
|
|
|
mood_file.close();
|
2019-04-18 15:03:01 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
2021-08-09 23:32:09 +02:00
|
|
|
if (!SetFileAttributes(reinterpret_cast<LPCTSTR>(mood_filename.utf16()), FILE_ATTRIBUTE_HIDDEN)) {
|
2019-04-18 15:03:01 +02:00
|
|
|
qLog(Warning) << "Error setting hidden attribute for file" << mood_filename;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2021-08-09 23:32:26 +02:00
|
|
|
qLog(Error) << "Error opening mood file" << mood_filename << "for writing:" << mood_file.errorString();
|
2019-04-18 15:03:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the request from the active list and delete it
|
|
|
|
requests_.remove(url);
|
|
|
|
active_requests_.remove(url);
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
QTimer::singleShot(1s, request, &MoodbarLoader::deleteLater);
|
2019-04-18 15:03:01 +02:00
|
|
|
|
|
|
|
MaybeTakeNextRequest();
|
|
|
|
|
|
|
|
}
|