]> git.sur5r.net Git - minitube/blob - src/video.cpp
New upstream version 3.8
[minitube] / src / video.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "video.h"
22 #include "datautils.h"
23 #include "http.h"
24 #include "httputils.h"
25 #include "jsfunctions.h"
26 #include "playlistitemdelegate.h"
27 #include "videodefinition.h"
28
29 #include "ytjsvideo.h"
30 #include "ytvideo.h"
31
32 Video::Video()
33     : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
34       loadingThumbnail(false), ytVideo(nullptr), ytjsVideo(nullptr) {}
35
36 Video::~Video() {
37     qDebug() << "Deleting" << id;
38 }
39
40 Video *Video::clone() {
41     Video *clone = new Video();
42     clone->title = title;
43     clone->description = description;
44     clone->channelTitle = channelTitle;
45     clone->channelId = channelId;
46     clone->webpage = webpage;
47     clone->streamUrl = streamUrl;
48     clone->thumbnail = thumbnail;
49     clone->thumbnailUrl = thumbnailUrl;
50     clone->mediumThumbnailUrl = mediumThumbnailUrl;
51     clone->duration = duration;
52     clone->formattedDuration = formattedDuration;
53     clone->published = published;
54     clone->formattedPublished = formattedPublished;
55     clone->viewCount = viewCount;
56     clone->formattedViewCount = formattedViewCount;
57     clone->id = id;
58     clone->definitionCode = definitionCode;
59     return clone;
60 }
61
62 const QString &Video::getWebpage() {
63     if (webpage.isEmpty() && !id.isEmpty())
64         webpage.append("https://www.youtube.com/watch?v=").append(id);
65     return webpage;
66 }
67
68 void Video::setWebpage(const QString &value) {
69     webpage = value;
70
71     // Get Video ID
72     if (id.isEmpty()) {
73         QRegExp re(JsFunctions::instance()->videoIdRE());
74         if (re.indexIn(webpage) == -1) {
75             qWarning() << QString("Cannot get video id for %1").arg(webpage);
76             // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
77             // loadingStreamUrl = false;
78             return;
79         }
80         id = re.cap(1);
81     }
82 }
83
84 void Video::loadThumbnail() {
85     if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
86     loadingThumbnail = true;
87     auto reply = HttpUtils::yt().get(thumbnailUrl);
88     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
89     connect(reply, &HttpReply::error, this, [this](auto &msg) {
90         qWarning() << msg;
91         loadingThumbnail = false;
92     });
93 }
94
95 void Video::setDuration(int value) {
96     duration = value;
97     formattedDuration = DataUtils::formatDuration(duration);
98 }
99
100 void Video::setViewCount(int value) {
101     viewCount = value;
102     formattedViewCount = DataUtils::formatCount(viewCount);
103 }
104
105 void Video::setPublished(const QDateTime &value) {
106     published = value;
107     formattedPublished = DataUtils::formatDateTime(published);
108 }
109
110 void Video::setThumbnail(const QByteArray &bytes) {
111     qreal ratio = qApp->devicePixelRatio();
112     thumbnail.loadFromData(bytes);
113     thumbnail.setDevicePixelRatio(ratio);
114     const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
115     if (thumbnail.width() > thumbWidth)
116         thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
117     emit gotThumbnail();
118     loadingThumbnail = false;
119 }
120
121 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
122     qDebug() << "Streams loaded";
123     this->streamUrl = streamUrl;
124     emit gotStreamUrl(streamUrl, audioUrl);
125     if (ytVideo) {
126         definitionCode = ytVideo->getDefinitionCode();
127         ytVideo->deleteLater();
128         ytVideo = nullptr;
129     }
130     if (ytjsVideo) {
131         definitionCode = ytjsVideo->getDefinitionCode();
132         ytjsVideo->deleteLater();
133         ytjsVideo = nullptr;
134     }
135 }
136
137 void Video::loadStreamUrlJS() {
138     if (ytjsVideo) {
139         qDebug() << "Already loading" << id;
140         return;
141     }
142     ytjsVideo = new YTJSVideo(id, this);
143     connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
144     connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
145         qDebug() << msg;
146         ytjsVideo->deleteLater();
147         ytjsVideo = nullptr;
148         loadStreamUrlYT();
149     });
150     ytjsVideo->loadStreamUrl();
151 }
152
153 void Video::loadStreamUrlYT() {
154     if (ytVideo) {
155         qDebug() << "Already loading" << id;
156         return;
157     }
158     ytVideo = new YTVideo(id, this);
159     connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
160     connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
161         qDebug() << msg;
162         emit errorStreamUrl(msg);
163         ytVideo->deleteLater();
164         ytVideo = nullptr;
165     });
166     ytVideo->loadStreamUrl();
167 }
168
169 void Video::loadStreamUrl() {
170     loadStreamUrlJS();
171 }
172
173 void Video::abortLoadStreamUrl() {
174     if (ytVideo) {
175         ytVideo->disconnect(this);
176         ytVideo->deleteLater();
177         ytVideo = nullptr;
178     }
179 }