]> git.sur5r.net Git - minitube/blob - src/video.cpp
New upstream version 3.6
[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     QObject *reply = HttpUtils::yt().get(thumbnailUrl);
88     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
89 }
90
91 void Video::setDuration(int value) {
92     duration = value;
93     formattedDuration = DataUtils::formatDuration(duration);
94 }
95
96 void Video::setViewCount(int value) {
97     viewCount = value;
98     formattedViewCount = DataUtils::formatCount(viewCount);
99 }
100
101 void Video::setPublished(const QDateTime &value) {
102     published = value;
103     formattedPublished = DataUtils::formatDateTime(published);
104 }
105
106 void Video::setThumbnail(const QByteArray &bytes) {
107     qreal ratio = qApp->devicePixelRatio();
108     thumbnail.loadFromData(bytes);
109     thumbnail.setDevicePixelRatio(ratio);
110     const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
111     if (thumbnail.width() > thumbWidth)
112         thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
113     emit gotThumbnail();
114     loadingThumbnail = false;
115 }
116
117 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
118     qDebug() << "Streams loaded";
119     this->streamUrl = streamUrl;
120     emit gotStreamUrl(streamUrl, audioUrl);
121     if (ytVideo) {
122         definitionCode = ytVideo->getDefinitionCode();
123         ytVideo->deleteLater();
124         ytVideo = nullptr;
125     }
126     if (ytjsVideo) {
127         definitionCode = ytjsVideo->getDefinitionCode();
128         ytjsVideo->deleteLater();
129         ytjsVideo = nullptr;
130     }
131 }
132
133 void Video::loadStreamUrlJS() {
134     if (ytjsVideo) {
135         qDebug() << "Already loading" << id;
136         return;
137     }
138     ytjsVideo = new YTJSVideo(id, this);
139     connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
140     connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
141         emit errorStreamUrl(msg);
142         ytjsVideo->deleteLater();
143         ytjsVideo = nullptr;
144     });
145     ytjsVideo->loadStreamUrl();
146 }
147
148 void Video::loadStreamUrl() {
149     loadStreamUrlJS();
150     return;
151     if (ytVideo) {
152         qDebug() << "Already loading" << id;
153         return;
154     }
155     ytVideo = new YTVideo(id, this);
156     connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
157     connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
158         emit errorStreamUrl(msg);
159         ytVideo->deleteLater();
160         ytVideo = nullptr;
161     });
162     ytVideo->loadStreamUrl();
163 }
164
165 void Video::abortLoadStreamUrl() {
166     if (ytVideo) {
167         ytVideo->disconnect(this);
168         ytVideo->deleteLater();
169         ytVideo = nullptr;
170     }
171 }