-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcstreamloader.cpp
More file actions
308 lines (266 loc) · 8.59 KB
/
cstreamloader.cpp
File metadata and controls
308 lines (266 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*=============================================================================\
| $HeadURL$
|
| Author: Jo2003
|
| last changed by: $Author$
|
| Begin: 4.12.2010 / 18:55
|
| $Id$
|
\=============================================================================*/
#include "cstreamloader.h"
#include "externals_inc.h"
/*-----------------------------------------------------------------------------\
| Function: CStreamLoader / constructor
|
| Author: Jo2003
|
| Begin: Monday, January 04, 2010 16:14:52
|
| Description: construct stream downloader class, init values
|
| Parameters: --
|
\-----------------------------------------------------------------------------*/
CStreamLoader::CStreamLoader() :QHttp()
{
iReq = -1;
iCache = -1;
sHost = "";
bUseTimerRec = false;
m_pTmpFile = NULL;
m_iSize = 0;
m_bSpeedTest = false;
// set timer interval for file check ...
tFileCheck.setInterval(2000); // 2 sec ...
connect(this, SIGNAL(requestFinished(int, bool)), this, SLOT(handleEndRequest(int, bool)));
connect(&tFileCheck, SIGNAL(timeout()), this, SLOT(slotStreamDataAvailable()));
}
/*-----------------------------------------------------------------------------\
| Function: ~CStreamLoader / destructor
|
| Author: Jo2003
|
| Begin: Monday, January 04, 2010 16:14:52
|
| Description: constructs a CKartinaClnt object to communicate with
| kartina.tv
| Parameters: host, username, password, erotic allowed (true / false)
|
\-----------------------------------------------------------------------------*/
CStreamLoader::~CStreamLoader()
{
tFileCheck.stop();
abort();
if (fStream.isOpen ())
{
fStream.close ();
}
}
/* -----------------------------------------------------------------\
| Method: downloadStream [slot]
| Begin: 13.12.2010 / 16:32:33
| Author: Jo2003
| Description: download stream to disk
|
| Parameters: stream url, file name, optional cache time
|
| Returns: --
\----------------------------------------------------------------- */
void CStreamLoader::downloadStream (const QString &sUrl, const QString &sFileName, int iCacheTime, bool bTimerRec)
{
QString tmpUrl = sUrl;
bUseTimerRec = bTimerRec;
m_bSpeedTest = false;
tFileCheck.setInterval(2000); // 2 sec ...
mInfo(tr("Archive URL:\n --> %1").arg(sUrl));
// patch stream prefix to allow the usage of QUrl ...
tmpUrl.replace ("http/ts", "http");
QUrl url(tmpUrl);
// print out host ...
mInfo(tr("Download Stream from http://%1:%2\n --> to %3")
.arg(url.host()).arg((url.port() == -1) ? 80 : url.port())
.arg(sFileName));
// save cache time ...
iCache = iCacheTime;
// set file name for download file ...
fStream.setFileName (sFileName);
// unbuffered to make sure that data will be written down to file as soon as possible ...
if (fStream.open (QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
{
setHost (url.host(), QHttp::ConnectionModeHttp, (url.port() == -1) ? 80 : url.port());
iReq = get (tmpUrl, &fStream);
mInfo(tr("Request #%1 sent ...").arg(iReq));
// start file check timer ...
tFileCheck.start();
emit sigStreamRequested(iReq);
}
}
//---------------------------------------------------------------------------
//! \brief start speed test
//
//! \author Jo2003
//! \date 16.09.2015
//
//! \param sUrl [in] (onst QString&) download url
//! \param iSize [in] (int) size to download in MB (10 ... 100).
//---------------------------------------------------------------------------
void CStreamLoader::speedTest(const QString &sUrl, int iSize)
{
QUrl url(sUrl);
m_iSize = (iSize <= 0) ? 10 : ((iSize > 100) ? 100 : iSize);
m_pTmpFile = new QTemporaryFile();
m_bSpeedTest = true;
if (m_pTmpFile)
{
if (m_pTmpFile->open())
{
tFileCheck.setInterval(500);
setHost (url.host(), QHttp::ConnectionModeHttp, (url.port() == -1) ? 80 : url.port());
iReq = get (url.toString(), m_pTmpFile);
mInfo(tr("Request #%1 (%2) sent ...").arg(iReq).arg(sUrl));
// start file check timer ...
tFileCheck.start();
m_tmDwn.start();
}
}
}
//---------------------------------------------------------------------------
//! \brief end speed test
//
//! \author Jo2003
//! \date 16.09.2015
//
//---------------------------------------------------------------------------
void CStreamLoader::endSpeedTest()
{
abort();
tFileCheck.stop();
if (m_pTmpFile)
{
emit sigDwnSpeed(m_tmDwn.elapsed(), m_pTmpFile->size());
emit sigSpeedTestEnd();
delete m_pTmpFile;
m_pTmpFile = NULL;
}
}
/* -----------------------------------------------------------------\
| Method: stopDownload
| Begin: 13.12.2010 / 16:32:33
| Author: Jo2003
| Description: stop stream download
|
| Parameters: request id
|
| Returns: --
\----------------------------------------------------------------- */
void CStreamLoader::stopDownload(int id)
{
// we should abort only the download ...
if (id == iReq)
{
// make sure we don't check filesize anymore ...
tFileCheck.stop();
// quit download ...
abort();
// close stream file ...
fStream.close();
}
}
/* -----------------------------------------------------------------\
| Method: slotStreamDataAvailable
| Begin: 14.12.2010 / 11:35
| Author: Jo2003
| Description: check if we have already downloaded a part of
| the stream
|
| Parameters: --
|
| Returns: --
\----------------------------------------------------------------- */
void CStreamLoader::slotStreamDataAvailable()
{
if (m_bSpeedTest)
{
if (m_pTmpFile)
{
emit sigDwnSpeed(m_tmDwn.elapsed(), m_pTmpFile->size());
if (m_pTmpFile->size() >= (m_iSize * 1024 * 1024))
{
endSpeedTest();
}
}
}
else
{
// Make sure downloaded part of video is "big" enough
// so libVLC will not reach the end when reading it.
// Assume a good value up to 720p (HD Ready).
// 4.5Mbit/s -> 562.5kB/s
// wait until file is filled with cache size ...
int iSize = (iCache / 1000) * 562500;
iSize = (iSize < MIN_CACHE_SIZE) ? MIN_CACHE_SIZE : iSize;
if (fStream.size() >= iSize)
{
// check no more needed ...
tFileCheck.stop();
if (!bUseTimerRec)
{
emit sigStreamDownload(iReq, fStream.fileName());
}
else
{
emit sigStreamDwnTimer(iReq, fStream.fileName());
}
emit sigBufferPercent(100);
}
else
{
emit sigBufferPercent((int)((100 * fStream.size()) / iSize));
}
}
}
/*-----------------------------------------------------------------------------\
| Function: handleEndRequest (slot)
|
| Author: Jo2003
|
| Begin: Monday, January 04, 2010 16:14:52
|
| Description: sends messages if a request has finished ...
|
| Parameters: request id (unused), error inidicator
|
| Returns: --
\-----------------------------------------------------------------------------*/
void CStreamLoader::handleEndRequest(int id, bool err)
{
if (!m_bSpeedTest)
{
// is this our request ... ?
if (id == iReq)
{
if (!err)
{
mInfo(tr("Request #%1 done!").arg(id));
// This shouldn't happen. The download should end only
// on request. In case it ends, we will close the
// file to which data were downloaded ...
fStream.close();
}
else
{
mErr(tr("Error in Request: %1!").arg(errorString()));
// send error signal ...
emit sigError(errorString());
}
}
}
}
/*=============================================================================\
| History:
| ---------------------------------------------------------------------------
| 14.12.2010: - a download class for streams sent from kartina.tv
\=============================================================================*/