]> git.sur5r.net Git - minitube/blobdiff - src/video.cpp
Update upstream source from tag 'upstream/3.8'
[minitube] / src / video.cpp
index 52432b8d0efcf950566c4eb27f941d2448240f22..fdf7a22f11ce09e681f99b36666c5dc3547ae93a 100644 (file)
@@ -23,12 +23,15 @@ $END_LICENSE */
 #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;
@@ -50,6 +53,7 @@ Video *Video::clone() {
     clone->published = published;
     clone->formattedPublished = formattedPublished;
     clone->viewCount = viewCount;
+    clone->formattedViewCount = formattedViewCount;
     clone->id = id;
     clone->definitionCode = definitionCode;
     return clone;
@@ -80,8 +84,12 @@ void Video::setWebpage(const QString &value) {
 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) {
@@ -89,6 +97,11 @@ 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);
@@ -98,29 +111,69 @@ void Video::setThumbnail(const QByteArray &bytes) {
     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;
+    }
+}