]> git.sur5r.net Git - minitube/blobdiff - src/video.cpp
Update upstream source from tag 'upstream/3.8'
[minitube] / src / video.cpp
index 05d9761b58eb52ea899706c959587dd49114d466..fdf7a22f11ce09e681f99b36666c5dc3547ae93a 100644 (file)
@@ -25,11 +25,13 @@ $END_LICENSE */
 #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(nullptr) {}
+      loadingThumbnail(false), ytVideo(nullptr), ytjsVideo(nullptr) {}
 
 Video::~Video() {
     qDebug() << "Deleting" << id;
@@ -82,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) {
@@ -114,14 +120,37 @@ void Video::setThumbnail(const QByteArray &bytes) {
 
 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
     qDebug() << "Streams loaded";
-    definitionCode = ytVideo->getDefinitionCode();
     this->streamUrl = streamUrl;
     emit gotStreamUrl(streamUrl, audioUrl);
-    ytVideo->deleteLater();
-    ytVideo = nullptr;
+    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;
@@ -129,6 +158,7 @@ void Video::loadStreamUrl() {
     ytVideo = new YTVideo(id, this);
     connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
     connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
+        qDebug() << msg;
         emit errorStreamUrl(msg);
         ytVideo->deleteLater();
         ytVideo = nullptr;
@@ -136,6 +166,10 @@ void Video::loadStreamUrl() {
     ytVideo->loadStreamUrl();
 }
 
+void Video::loadStreamUrl() {
+    loadStreamUrlJS();
+}
+
 void Video::abortLoadStreamUrl() {
     if (ytVideo) {
         ytVideo->disconnect(this);