2020-09-17 17:50:17 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2020-09-17 17:50:17 +02:00
|
|
|
*
|
|
|
|
* 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 "config.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QChar>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2020-09-17 17:50:17 +02:00
|
|
|
#include "core/song.h"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/timeconstants.h"
|
2020-09-17 17:50:17 +02:00
|
|
|
#include "qobuzservice.h"
|
|
|
|
#include "qobuzbaserequest.h"
|
|
|
|
#include "qobuzstreamurlrequest.h"
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
QobuzStreamURLRequest::QobuzStreamURLRequest(QobuzService *service, SharedPtr<NetworkAccessManager> network, const QUrl &media_url, const uint id, QObject *parent)
|
2020-09-17 17:50:17 +02:00
|
|
|
: QobuzBaseRequest(service, network, parent),
|
2021-07-11 07:40:57 +02:00
|
|
|
service_(service),
|
|
|
|
reply_(nullptr),
|
2023-04-21 16:20:00 +02:00
|
|
|
media_url_(media_url),
|
2021-07-11 07:40:57 +02:00
|
|
|
id_(id),
|
2023-04-21 16:20:00 +02:00
|
|
|
song_id_(media_url.path().toInt()),
|
2021-07-11 07:40:57 +02:00
|
|
|
tries_(0),
|
|
|
|
need_login_(false) {}
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
QobuzStreamURLRequest::~QobuzStreamURLRequest() {
|
|
|
|
|
|
|
|
if (reply_) {
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply_, nullptr, this, nullptr);
|
2020-09-17 17:50:17 +02:00
|
|
|
if (reply_->isRunning()) reply_->abort();
|
|
|
|
reply_->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::LoginComplete(const bool success, const QString &error) {
|
|
|
|
|
|
|
|
if (!need_login_) return;
|
|
|
|
need_login_ = false;
|
|
|
|
|
|
|
|
if (!success) {
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, error);
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Process();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::Process() {
|
|
|
|
|
|
|
|
if (app_id().isEmpty() || app_secret().isEmpty()) {
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, tr("Missing Qobuz app ID or secret."));
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!authenticated()) {
|
|
|
|
need_login_ = true;
|
|
|
|
emit TryLogin();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GetStreamURL();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::Cancel() {
|
|
|
|
|
|
|
|
if (reply_ && reply_->isRunning()) {
|
|
|
|
reply_->abort();
|
|
|
|
}
|
|
|
|
else {
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, tr("Cancelled."));
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::GetStreamURL() {
|
|
|
|
|
|
|
|
++tries_;
|
|
|
|
|
|
|
|
if (reply_) {
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply_, nullptr, this, nullptr);
|
2020-09-17 17:50:17 +02:00
|
|
|
if (reply_->isRunning()) reply_->abort();
|
|
|
|
reply_->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 timestamp = QDateTime::currentDateTime().toSecsSinceEpoch();
|
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
ParamList params_to_sign = ParamList() << Param(QStringLiteral("format_id"), QString::number(format()))
|
|
|
|
<< Param(QStringLiteral("track_id"), QString::number(song_id_));
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
std::sort(params_to_sign.begin(), params_to_sign.end());
|
|
|
|
|
|
|
|
QString data_to_sign;
|
2024-04-11 02:56:01 +02:00
|
|
|
data_to_sign += QStringLiteral("trackgetFileUrl");
|
2020-09-17 17:50:17 +02:00
|
|
|
for (const Param ¶m : params_to_sign) {
|
|
|
|
data_to_sign += param.first + param.second;
|
|
|
|
}
|
|
|
|
data_to_sign += QString::number(timestamp);
|
2024-04-11 02:56:01 +02:00
|
|
|
data_to_sign += app_secret();
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
QByteArray const digest = QCryptographicHash::hash(data_to_sign.toUtf8(), QCryptographicHash::Md5);
|
2024-04-11 02:56:01 +02:00
|
|
|
const QString signature = QString::fromLatin1(digest.toHex()).rightJustified(32, QLatin1Char('0')).toLower();
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
ParamList params = params_to_sign;
|
2024-04-11 02:56:01 +02:00
|
|
|
params << Param(QStringLiteral("request_ts"), QString::number(timestamp));
|
|
|
|
params << Param(QStringLiteral("request_sig"), signature);
|
|
|
|
params << Param(QStringLiteral("user_auth_token"), user_auth_token());
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
std::sort(params.begin(), params.end());
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
reply_ = CreateRequest(QStringLiteral("track/getFileUrl"), params);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(reply_, &QNetworkReply::finished, this, &QobuzStreamURLRequest::StreamURLReceived);
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::StreamURLReceived() {
|
|
|
|
|
|
|
|
if (!reply_) return;
|
|
|
|
|
|
|
|
QByteArray data = GetReplyData(reply_);
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(reply_, nullptr, this, nullptr);
|
2020-09-17 17:50:17 +02:00
|
|
|
reply_->deleteLater();
|
|
|
|
reply_ = nullptr;
|
|
|
|
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
if (!authenticated() && login_sent() && tries_ <= 1) {
|
|
|
|
need_login_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = ExtractJsonObj(data);
|
|
|
|
if (json_obj.isEmpty()) {
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!json_obj.contains(QStringLiteral("track_id"))) {
|
|
|
|
Error(QStringLiteral("Invalid Json reply, stream url is missing track_id."), json_obj);
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
int track_id = json_obj[QStringLiteral("track_id")].toInt();
|
2020-09-17 17:50:17 +02:00
|
|
|
if (track_id != song_id_) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Incorrect track ID returned."), json_obj);
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
if (!json_obj.contains(QStringLiteral("mime_type")) || !json_obj.contains(QStringLiteral("url"))) {
|
|
|
|
Error(QStringLiteral("Invalid Json reply, stream url is missing url or mime_type."), json_obj);
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:20:26 +02:00
|
|
|
QUrl url(json_obj[QStringLiteral("url")].toString());
|
|
|
|
QString mimetype = json_obj[QStringLiteral("mime_type")].toString();
|
2020-09-17 17:50:17 +02:00
|
|
|
|
2023-02-18 14:09:27 +01:00
|
|
|
Song::FileType filetype(Song::FileType::Unknown);
|
2020-09-17 17:50:17 +02:00
|
|
|
QMimeDatabase mimedb;
|
2024-04-11 02:56:01 +02:00
|
|
|
QStringList suffixes = mimedb.mimeTypeForName(mimetype).suffixes();
|
2021-03-21 04:55:00 +01:00
|
|
|
for (const QString &suffix : suffixes) {
|
2020-09-17 17:50:17 +02:00
|
|
|
filetype = Song::FiletypeByExtension(suffix);
|
2023-02-18 14:09:27 +01:00
|
|
|
if (filetype != Song::FileType::Unknown) break;
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
2023-02-18 14:09:27 +01:00
|
|
|
if (filetype == Song::FileType::Unknown) {
|
2020-09-17 17:50:17 +02:00
|
|
|
qLog(Debug) << "Qobuz: Unknown mimetype" << mimetype;
|
2023-02-18 14:09:27 +01:00
|
|
|
filetype = Song::FileType::Stream;
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!url.isValid()) {
|
2024-04-09 23:20:26 +02:00
|
|
|
Error(QStringLiteral("Returned stream url is invalid."), json_obj);
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLFailure(id_, media_url_, errors_.first());
|
2020-09-17 17:50:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 duration = -1;
|
2024-04-09 23:20:26 +02:00
|
|
|
if (json_obj.contains(QStringLiteral("duration"))) {
|
|
|
|
duration = json_obj[QStringLiteral("duration")].toInt() * kNsecPerSec;
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
|
|
|
int samplerate = -1;
|
2024-04-09 23:20:26 +02:00
|
|
|
if (json_obj.contains(QStringLiteral("sampling_rate"))) {
|
|
|
|
samplerate = static_cast<int>(json_obj[QStringLiteral("sampling_rate")].toDouble()) * 1000;
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
|
|
|
int bit_depth = -1;
|
2024-04-09 23:20:26 +02:00
|
|
|
if (json_obj.contains(QStringLiteral("bit_depth"))) {
|
|
|
|
bit_depth = static_cast<int>(json_obj[QStringLiteral("bit_depth")].toDouble());
|
2020-09-17 17:50:17 +02:00
|
|
|
}
|
|
|
|
|
2023-04-21 16:20:00 +02:00
|
|
|
emit StreamURLSuccess(id_, media_url_, url, filetype, samplerate, bit_depth, duration);
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QobuzStreamURLRequest::Error(const QString &error, const QVariant &debug) {
|
|
|
|
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
qLog(Error) << "Qobuz:" << error;
|
|
|
|
errors_ << error;
|
|
|
|
}
|
|
|
|
if (debug.isValid()) qLog(Debug) << debug;
|
|
|
|
|
|
|
|
}
|