2011-04-02 15:34:06 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-26 13:34:53 +01:00
|
|
|
Copyright 2011, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "albumcoverfetchersearch.h"
|
2012-10-12 12:38:12 +02:00
|
|
|
|
|
|
|
#include <cmath>
|
2011-04-02 15:34:06 +02:00
|
|
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QNetworkReply>
|
2011-05-28 18:00:46 +02:00
|
|
|
#include <QTimer>
|
2011-04-17 17:25:32 +02:00
|
|
|
#include <QtDebug>
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2012-10-12 12:38:12 +02:00
|
|
|
#include "albumcoverfetcher.h"
|
|
|
|
#include "coverprovider.h"
|
|
|
|
#include "coverproviders.h"
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
2011-06-26 17:07:19 +02:00
|
|
|
|
|
|
|
const int AlbumCoverFetcherSearch::kSearchTimeoutMs = 10000;
|
|
|
|
const int AlbumCoverFetcherSearch::kImageLoadTimeoutMs = 2500;
|
|
|
|
const int AlbumCoverFetcherSearch::kTargetSize = 500;
|
|
|
|
const float AlbumCoverFetcherSearch::kGoodScore = 1.85;
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
AlbumCoverFetcherSearch::AlbumCoverFetcherSearch(
|
|
|
|
const CoverSearchRequest& request, QNetworkAccessManager* network,
|
|
|
|
QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
request_(request),
|
|
|
|
image_load_timeout_(new NetworkTimeouts(kImageLoadTimeoutMs, this)),
|
|
|
|
network_(network),
|
|
|
|
cancel_requested_(false) {
|
|
|
|
// we will terminate the search after kSearchTimeoutMs miliseconds if we are
|
|
|
|
// not
|
2011-04-02 17:31:28 +02:00
|
|
|
// able to find all of the results before that point in time
|
2011-06-26 17:07:19 +02:00
|
|
|
QTimer::singleShot(kSearchTimeoutMs, this, SLOT(TerminateSearch()));
|
2011-04-02 17:31:28 +02:00
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-04-02 17:31:28 +02:00
|
|
|
void AlbumCoverFetcherSearch::TerminateSearch() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (int id : pending_requests_.keys()) {
|
2011-06-20 01:15:51 +02:00
|
|
|
pending_requests_.take(id)->CancelSearch(id);
|
2011-05-28 18:00:46 +02:00
|
|
|
}
|
|
|
|
|
2011-06-26 17:07:19 +02:00
|
|
|
AllProvidersFinished();
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 20:34:41 +02:00
|
|
|
void AlbumCoverFetcherSearch::Start(CoverProviders* cover_providers) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (CoverProvider* provider : cover_providers->List()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(provider, SIGNAL(SearchFinished(int, QList<CoverSearchResult>)),
|
|
|
|
SLOT(ProviderSearchFinished(int, QList<CoverSearchResult>)));
|
2011-07-23 20:34:41 +02:00
|
|
|
const int id = cover_providers->NextId();
|
2014-02-07 16:34:20 +01:00
|
|
|
const bool success =
|
|
|
|
provider->StartSearch(request_.artist, request_.album, id);
|
2011-06-20 01:15:51 +02:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
pending_requests_[id] = provider;
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.network_requests_made_++;
|
2011-04-02 17:31:28 +02:00
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
2011-05-28 18:00:46 +02:00
|
|
|
|
2011-06-22 21:07:15 +02:00
|
|
|
// end this search before it even began if there are no providers...
|
2014-02-07 16:34:20 +01:00
|
|
|
if (pending_requests_.isEmpty()) {
|
2011-05-28 18:00:46 +02:00
|
|
|
TerminateSearch();
|
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2011-06-26 17:07:48 +02:00
|
|
|
static bool CompareProviders(const CoverSearchResult& a,
|
|
|
|
const CoverSearchResult& b) {
|
|
|
|
return a.provider < b.provider;
|
2011-06-26 17:07:19 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
void AlbumCoverFetcherSearch::ProviderSearchFinished(
|
|
|
|
int id, const QList<CoverSearchResult>& results) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!pending_requests_.contains(id)) return;
|
2011-05-28 18:00:46 +02:00
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
CoverProvider* provider = pending_requests_.take(id);
|
2011-06-20 01:15:29 +02:00
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
CoverSearchResults results_copy(results);
|
2011-06-26 17:07:48 +02:00
|
|
|
// Set categories on the results
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < results_copy.count(); ++i) {
|
2011-06-26 17:07:48 +02:00
|
|
|
results_copy[i].provider = provider->name();
|
2011-05-28 18:00:46 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 01:15:51 +02:00
|
|
|
// Add results from the current provider to our pool
|
|
|
|
results_.append(results_copy);
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.total_images_by_provider_[provider->name()]++;
|
2011-06-20 01:15:51 +02:00
|
|
|
|
2011-05-28 18:00:46 +02:00
|
|
|
// do we have more providers left?
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!pending_requests_.isEmpty()) {
|
2011-05-28 18:00:46 +02:00
|
|
|
return;
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2011-06-26 17:07:19 +02:00
|
|
|
AllProvidersFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::AllProvidersFinished() {
|
2011-06-26 17:07:29 +02:00
|
|
|
if (cancel_requested_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:34:06 +02:00
|
|
|
// if we only wanted to do the search then we're done
|
|
|
|
if (request_.search) {
|
|
|
|
emit SearchFinished(request_.id, results_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no results?
|
|
|
|
if (results_.isEmpty()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.missing_images_++;
|
2011-04-02 15:34:06 +02:00
|
|
|
emit AlbumCoverFetched(request_.id, QImage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-26 17:07:19 +02:00
|
|
|
// Now we have to load some images and figure out which one is the best.
|
|
|
|
// We'll sort the list of results by category, then load the first few images
|
|
|
|
// from each category and use some heuristics to score them. If no images
|
|
|
|
// are good enough we'll keep loading more images until we find one that is
|
|
|
|
// or we run out of results.
|
2011-06-26 17:07:48 +02:00
|
|
|
qStableSort(results_.begin(), results_.end(), CompareProviders);
|
2011-06-26 17:07:19 +02:00
|
|
|
FetchMoreImages();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::FetchMoreImages() {
|
|
|
|
// Try the first one in each category.
|
2011-06-26 17:07:48 +02:00
|
|
|
QString last_provider;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < results_.count(); ++i) {
|
2011-06-26 17:07:48 +02:00
|
|
|
if (results_[i].provider == last_provider) {
|
2011-06-26 17:07:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoverSearchResult result = results_.takeAt(i--);
|
2011-06-26 17:07:48 +02:00
|
|
|
last_provider = result.provider;
|
2011-06-26 17:07:19 +02:00
|
|
|
|
2011-06-26 17:07:48 +02:00
|
|
|
qLog(Debug) << "Loading" << result.image_url << "from" << result.provider;
|
2011-06-26 17:07:19 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
RedirectFollower* image_reply =
|
|
|
|
new RedirectFollower(network_->get(QNetworkRequest(result.image_url)));
|
2012-10-12 12:38:12 +02:00
|
|
|
NewClosure(image_reply, SIGNAL(finished()), this,
|
2014-02-07 16:34:20 +01:00
|
|
|
SLOT(ProviderCoverFetchFinished(RedirectFollower*)),
|
|
|
|
image_reply);
|
2011-06-26 17:07:48 +02:00
|
|
|
pending_image_loads_[image_reply] = result.provider;
|
2011-06-26 17:07:19 +02:00
|
|
|
image_load_timeout_->AddReply(image_reply);
|
2011-06-26 17:07:48 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.network_requests_made_++;
|
2011-06-26 17:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_image_loads_.isEmpty()) {
|
|
|
|
// There were no more results? Time to give up.
|
|
|
|
SendBestImage();
|
|
|
|
}
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverFetcherSearch::ProviderCoverFetchFinished(
|
|
|
|
RedirectFollower* reply) {
|
2011-04-02 15:34:06 +02:00
|
|
|
reply->deleteLater();
|
2011-06-26 17:07:48 +02:00
|
|
|
const QString provider = pending_image_loads_.take(reply);
|
|
|
|
|
|
|
|
statistics_.bytes_transferred_ += reply->bytesAvailable();
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-06-26 17:07:29 +02:00
|
|
|
if (cancel_requested_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:34:06 +02:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2011-06-26 17:07:19 +02:00
|
|
|
qLog(Info) << "Error requesting" << reply->url() << reply->errorString();
|
2011-04-02 15:34:06 +02:00
|
|
|
} else {
|
|
|
|
QImage image;
|
2011-06-26 17:07:19 +02:00
|
|
|
if (!image.loadFromData(reply->readAll())) {
|
|
|
|
qLog(Info) << "Error decoding image data from" << reply->url();
|
|
|
|
} else {
|
|
|
|
const float score = ScoreImage(image);
|
2011-06-26 17:07:48 +02:00
|
|
|
candidate_images_.insertMulti(score, CandidateImage(provider, image));
|
2011-06-26 17:07:19 +02:00
|
|
|
|
|
|
|
qLog(Debug) << reply->url() << "scored" << score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_image_loads_.isEmpty()) {
|
|
|
|
// We've fetched everything we wanted to fetch for now, check if we have an
|
|
|
|
// image that's good enough.
|
|
|
|
float best_score = 0.0;
|
|
|
|
|
|
|
|
if (!candidate_images_.isEmpty()) {
|
|
|
|
best_score = candidate_images_.keys().last();
|
|
|
|
}
|
|
|
|
|
|
|
|
qLog(Debug) << "Best image so far has a score of" << best_score;
|
|
|
|
if (best_score >= kGoodScore) {
|
|
|
|
SendBestImage();
|
|
|
|
} else {
|
|
|
|
FetchMoreImages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AlbumCoverFetcherSearch::ScoreImage(const QImage& image) const {
|
|
|
|
// Invalid images score nothing
|
|
|
|
if (image.isNull()) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A 500x500px image scores 1.0, bigger scores higher
|
2014-02-07 16:34:20 +01:00
|
|
|
const float size_score =
|
2014-12-26 13:34:53 +01:00
|
|
|
std::sqrt(static_cast<float>(image.width() * image.height())) / kTargetSize;
|
2011-04-02 15:34:06 +02:00
|
|
|
|
2011-06-26 17:07:19 +02:00
|
|
|
// A 1:1 image scores 1.0, anything else scores less
|
2014-12-26 13:34:53 +01:00
|
|
|
const float aspect_score = 1.0 - static_cast<float>(image.height() - image.width()) /
|
2014-02-07 16:34:20 +01:00
|
|
|
std::max(image.height(), image.width());
|
2011-06-26 17:07:19 +02:00
|
|
|
|
|
|
|
return size_score + aspect_score;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::SendBestImage() {
|
|
|
|
QImage image;
|
|
|
|
|
|
|
|
if (!candidate_images_.isEmpty()) {
|
2011-06-26 17:07:48 +02:00
|
|
|
const CandidateImage best_image = candidate_images_.values().back();
|
|
|
|
image = best_image.second;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.chosen_images_by_provider_[best_image.first]++;
|
|
|
|
statistics_.chosen_images_++;
|
2011-06-26 17:07:48 +02:00
|
|
|
statistics_.chosen_width_ += image.width();
|
|
|
|
statistics_.chosen_height_ += image.height();
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
statistics_.missing_images_++;
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
2011-06-26 17:07:19 +02:00
|
|
|
|
|
|
|
emit AlbumCoverFetched(request_.id, image);
|
2011-04-02 15:34:06 +02:00
|
|
|
}
|
2011-06-26 17:07:29 +02:00
|
|
|
|
|
|
|
void AlbumCoverFetcherSearch::Cancel() {
|
|
|
|
cancel_requested_ = true;
|
|
|
|
|
|
|
|
if (!pending_requests_.isEmpty()) {
|
|
|
|
TerminateSearch();
|
|
|
|
} else if (!pending_image_loads_.isEmpty()) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (RedirectFollower* reply : pending_image_loads_.keys()) {
|
2011-06-26 17:07:29 +02:00
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
pending_image_loads_.clear();
|
|
|
|
}
|
|
|
|
}
|