#include "http.h"
#include "httputils.h"
#include "jsfunctions.h"
+#include "playlistitemdelegate.h"
#include "videodefinition.h"
+
+#include "ytjsvideo.h"
#include "ytvideo.h"
Video::Video()
: duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
- loadingThumbnail(false), ytVideo(0) {}
+ loadingThumbnail(false), ytVideo(nullptr), ytjsVideo(nullptr) {}
Video::~Video() {
qDebug() << "Deleting" << id;
clone->published = published;
clone->formattedPublished = formattedPublished;
clone->viewCount = viewCount;
+ clone->formattedViewCount = formattedViewCount;
clone->id = id;
clone->definitionCode = definitionCode;
return clone;
void Video::loadThumbnail() {
if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
loadingThumbnail = true;
- QObject *reply = HttpUtils::yt().get(thumbnailUrl);
+ auto reply = HttpUtils::yt().get(thumbnailUrl);
connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
+ connect(reply, &HttpReply::error, this, [this](auto &msg) {
+ qWarning() << msg;
+ loadingThumbnail = false;
+ });
}
void Video::setDuration(int value) {
formattedDuration = DataUtils::formatDuration(duration);
}
+void Video::setViewCount(int value) {
+ viewCount = value;
+ formattedViewCount = DataUtils::formatCount(viewCount);
+}
+
void Video::setPublished(const QDateTime &value) {
published = value;
formattedPublished = DataUtils::formatDateTime(published);
qreal ratio = qApp->devicePixelRatio();
thumbnail.loadFromData(bytes);
thumbnail.setDevicePixelRatio(ratio);
- const int thumbWidth = 160 * ratio;
+ const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
if (thumbnail.width() > thumbWidth)
thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
emit gotThumbnail();
loadingThumbnail = false;
}
-void Video::streamUrlLoaded(const QUrl &streamUrl) {
- definitionCode = ytVideo->getDefinitionCode();
+void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
+ qDebug() << "Streams loaded";
this->streamUrl = streamUrl;
- emit gotStreamUrl(this->streamUrl);
- delete ytVideo;
- ytVideo = 0;
+ emit gotStreamUrl(streamUrl, audioUrl);
+ if (ytVideo) {
+ definitionCode = ytVideo->getDefinitionCode();
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
+ }
+ if (ytjsVideo) {
+ definitionCode = ytjsVideo->getDefinitionCode();
+ ytjsVideo->deleteLater();
+ ytjsVideo = nullptr;
+ }
}
-void Video::loadStreamUrl() {
+void Video::loadStreamUrlJS() {
+ if (ytjsVideo) {
+ qDebug() << "Already loading" << id;
+ return;
+ }
+ ytjsVideo = new YTJSVideo(id, this);
+ connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
+ connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
+ qDebug() << msg;
+ ytjsVideo->deleteLater();
+ ytjsVideo = nullptr;
+ loadStreamUrlYT();
+ });
+ ytjsVideo->loadStreamUrl();
+}
+
+void Video::loadStreamUrlYT() {
if (ytVideo) {
qDebug() << "Already loading" << id;
return;
}
ytVideo = new YTVideo(id, this);
connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
- connect(ytVideo, &YTVideo::errorStreamUrl, this, &Video::errorStreamUrl);
- connect(ytVideo, &YTVideo::errorStreamUrl, ytVideo, &QObject::deleteLater);
+ connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
+ qDebug() << msg;
+ emit errorStreamUrl(msg);
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
+ });
ytVideo->loadStreamUrl();
}
+
+void Video::loadStreamUrl() {
+ loadStreamUrlJS();
+}
+
+void Video::abortLoadStreamUrl() {
+ if (ytVideo) {
+ ytVideo->disconnect(this);
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
+ }
+}