strawberry-audio-player-win.../src/qobuz/qobuzstreamurlrequest.cpp

237 lines
6.8 KiB
C++
Raw Normal View History

/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
*
* 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"
#include "core/shared_ptr.h"
2021-01-11 16:48:46 +01:00
#include "core/networkaccessmanager.h"
#include "core/song.h"
#include "utilities/timeconstants.h"
#include "qobuzservice.h"
#include "qobuzbaserequest.h"
#include "qobuzstreamurlrequest.h"
QobuzStreamURLRequest::QobuzStreamURLRequest(QobuzService *service, SharedPtr<NetworkAccessManager> network, const QUrl &media_url, const uint id, QObject *parent)
: 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) {}
QobuzStreamURLRequest::~QobuzStreamURLRequest() {
if (reply_) {
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply_, nullptr, this, nullptr);
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);
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."));
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."));
}
}
void QobuzStreamURLRequest::GetStreamURL() {
++tries_;
if (reply_) {
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply_, nullptr, this, nullptr);
if (reply_->isRunning()) reply_->abort();
reply_->deleteLater();
}
quint64 timestamp = QDateTime::currentDateTime().toSecsSinceEpoch();
ParamList params_to_sign = ParamList() << Param(QStringLiteral("format_id"), QString::number(format()))
<< Param(QStringLiteral("track_id"), QString::number(song_id_));
std::sort(params_to_sign.begin(), params_to_sign.end());
QString data_to_sign;
data_to_sign += QStringLiteral("trackgetFileUrl");
for (const Param &param : params_to_sign) {
data_to_sign += param.first + param.second;
}
data_to_sign += QString::number(timestamp);
data_to_sign += app_secret();
QByteArray const digest = QCryptographicHash::hash(data_to_sign.toUtf8(), QCryptographicHash::Md5);
const QString signature = QString::fromLatin1(digest.toHex()).rightJustified(32, QLatin1Char('0')).toLower();
ParamList params = params_to_sign;
params << Param(QStringLiteral("request_ts"), QString::number(timestamp));
params << Param(QStringLiteral("request_sig"), signature);
params << Param(QStringLiteral("user_auth_token"), user_auth_token());
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);
}
void QobuzStreamURLRequest::StreamURLReceived() {
if (!reply_) return;
QByteArray data = GetReplyData(reply_);
2021-01-26 16:48:04 +01:00
QObject::disconnect(reply_, nullptr, this, nullptr);
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());
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());
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());
return;
}
2024-04-09 23:20:26 +02:00
int track_id = json_obj[QStringLiteral("track_id")].toInt();
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());
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());
return;
}
2024-04-09 23:20:26 +02:00
QUrl url(json_obj[QStringLiteral("url")].toString());
QString mimetype = json_obj[QStringLiteral("mime_type")].toString();
2023-02-18 14:09:27 +01:00
Song::FileType filetype(Song::FileType::Unknown);
QMimeDatabase mimedb;
QStringList suffixes = mimedb.mimeTypeForName(mimetype).suffixes();
2021-03-21 04:55:00 +01:00
for (const QString &suffix : suffixes) {
filetype = Song::FiletypeByExtension(suffix);
2023-02-18 14:09:27 +01:00
if (filetype != Song::FileType::Unknown) break;
}
2023-02-18 14:09:27 +01:00
if (filetype == Song::FileType::Unknown) {
qLog(Debug) << "Qobuz: Unknown mimetype" << mimetype;
2023-02-18 14:09:27 +01:00
filetype = Song::FileType::Stream;
}
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());
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;
}
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;
}
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());
}
2023-04-21 16:20:00 +02:00
emit StreamURLSuccess(id_, media_url_, url, filetype, samplerate, bit_depth, duration);
}
void QobuzStreamURLRequest::Error(const QString &error, const QVariant &debug) {
if (!error.isEmpty()) {
qLog(Error) << "Qobuz:" << error;
errors_ << error;
}
if (debug.isValid()) qLog(Debug) << debug;
}