-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathSource.cpp
More file actions
443 lines (378 loc) · 9.05 KB
/
Copy pathSource.cpp
File metadata and controls
443 lines (378 loc) · 9.05 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Source.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Source.h"
#include "VideoImpl.h"
#include "VideoPlayerImpl.h"
#include "CameraImpl.h"
#include <iostream>
#include <QFileIconProvider>
#include <QFileInfo>
#include <QImageReader>
#include <QSettings>
namespace mmp {
UidAllocator Source::allocator;
QVector<GLuint> Texture::_orphanedTextures;
QMutex Texture::_orphanedTexturesMutex;
void Texture::orphanTexture(GLuint id)
{
QMutexLocker locker(&_orphanedTexturesMutex);
_orphanedTextures.append(id);
}
void Texture::deleteOrphanedTextures()
{
QMutexLocker locker(&_orphanedTexturesMutex);
if (_orphanedTextures.isEmpty())
return;
glDeleteTextures((GLsizei) _orphanedTextures.size(), _orphanedTextures.constData());
_orphanedTextures.clear();
}
void Texture::update()
{
if (textureId == 0)
{
glGenTextures(1, &textureId);
}
}
void Texture::read(const QJsonObject& obj)
{
Source::read(obj);
if (obj.contains("x"))
setX(obj["x"].toDouble());
if (obj.contains("y"))
setY(obj["y"].toDouble());
}
void Texture::write(QJsonObject& obj)
{
Source::write(obj);
obj["x"] = getX();
obj["y"] = getY();
}
Source::Source(uid id)
: Element(id, &allocator),
_isPlaying(false)
{
}
Source::~Source()
{
allocator.free(getId());
}
Image::Image(int id)
: Texture(id),
_rate(0),
_currentFrame(0),
_currentFrameReal(0.0),
_prevTime(0),
_bits(0)
{
setRate(1.0);
}
Image::Image(const QString uri_, uid id)
: Texture(id),
_rate(0),
_currentFrame(-1),
_currentFrameReal(0.0),
_prevTime(0),
_bits(0)
{
setUri(uri_);
setRate(1.0);
}
bool Image::setUri(const QString &uri)
{
if (uri != _uri)
{
_uri = uri;
build();
_emitPropertyChanged("uri");
}
return !_images.isEmpty();
}
void Image::build()
{
// Read all images and convert to RGBA for direct upload to OpenGL.
QImageReader reader(_uri);
_images.clear();
for (int i = 0; i < reader.imageCount(); i++) {
QImage raw = reader.read().convertToFormat(QImage::Format_RGBA8888);
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
_images.push_back(raw.mirrored(true, false).transformed(QTransform().rotate(180)));
QT_WARNING_POP
}
rewind();
}
void Image::update()
{
if (isAnimation() && isPlaying())
{
// Compute the interval of time since last call to update().
qreal currentTime = _elapsedTime();
qreal diffTime = currentTime - _prevTime;
// Update next frame.
_currentFrameReal += diffTime * _rate * MM::DEFAULT_FRAMES_PER_SECOND;
_currentFrameReal = wrapAround(_currentFrameReal, (qreal)_images.size());
uint nextFrame = (int)_currentFrameReal;
// If frame changed, update image bits pointer.
if (nextFrame != _currentFrame)
{
_currentFrame = nextFrame;
_bits = _images[_currentFrame].bits();
bitsChanged = true;
}
// Reset previous time.
_prevTime = currentTime;
}
}
void Image::rewind()
{
// Reset/restart everything.
if (isAnimation())
{
_currentFrame = 0;
_currentFrameReal = 0.0;
_prevTime = 0;
_timer.start();
}
_bits = _images.isEmpty() ? 0 : _images[0].bits();
bitsChanged = true;
}
const uchar* Image::getBits() {
return _bits;
}
QIcon Image::getIcon() const
{
static QFileIconProvider provider;
if (!_images.isEmpty())
// Create icon from image.
return QIcon(QPixmap::fromImage(_images[0]).scaled(MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE,
Qt::IgnoreAspectRatio));
else
// Return default icon from filesystem.
return provider.icon(QFileInfo(_uri));
}
void Image::setRate(double rate)
{
_rate = rate;
}
void Image::_doPlay()
{
_prevTime = _elapsedTime();
}
/* Implementation of the Video class */
Video::Video(int id) : Texture(id),
_uri(""),
_impl(nullptr)
{
_impl = new VideoPlayerImpl();
setRate(1);
setVolume(1);
}
Video::Video(const QString uri_, VideoType type, double rate, uid id):
Texture(id),
_uri(""),
_videoType(type),
_impl(nullptr)
{
switch (type) {
case VIDEO_URI:
_impl = new VideoPlayerImpl();
break;
case VIDEO_WEBCAM:
_impl = new CameraImpl();
break;
default:
_impl = new VideoPlayerImpl();
break;
}
setRate(rate);
setVolume(1);
setUri(uri_);
_videoType = type;
}
// vertigo
Video::~Video()
{
delete _impl;
}
void Video::build()
{
this->_impl->build();
}
int Video::getWidth() const
{
return this->_impl->getWidth();
}
int Video::getHeight() const
{
return this->_impl->getHeight();
}
void Video::update() {
_impl->update();
Texture::update();
}
void Video::rewind()
{
_impl->resetMovie();
}
void Video::releaseResources()
{
// Release the capture device / media player but keep the impl object, so the
// device is freed as soon as the source is removed (e.g. to let the same
// camera be re-opened) without invalidating the source. See reacquireResources().
if (_impl)
_impl->unloadMovie();
}
void Video::reacquireResources()
{
// Re-open using the stored URI/device id (set by loadMovie()).
if (_impl)
_impl->build();
}
void Video::lockMutex() {
_impl->lockMutex();
}
void Video::unlockMutex() {
_impl->unlockMutex();
}
const uchar* Video::getBits()
{
return this->_impl->getBits();
}
bool Video::bitsHaveChanged() const
{
return this->_impl->bitsHaveChanged();
}
void Video::setRate(double rate)
{
if (rate != _impl->getRate())
{
_impl->setRate(rate);
_emitPropertyChanged("rate");
}
}
double Video::getRate() const
{
return _impl->getRate();
}
void Video::setVolume(double volume)
{
if (volume != _impl->getVolume())
{
_impl->setVolume(volume);
_emitPropertyChanged("volume");
}
}
double Video::getVolume() const
{
return _impl->getVolume();
}
bool Video::hasVideoSupport()
{
return VideoImpl::hasVideoSupport();
}
bool Video::setUri(const QString &uri)
{
QSettings settings;
bool sameMediaSourceOSC = settings.value("oscSameMediaSource").toBool();
// Check if we're actually changing the uri.
// In some case with OSC message the user may need to allow
// the same media source (uri)
if (sameMediaSourceOSC || uri != _uri)
{
// Try to load movie.
if (!_impl->loadMovie(uri))
{
qDebug() << "Cannot load movie " << uri << "." << Qt::endl;
return false;
}
// Set uri.
_uri = uri;
// Try to get thumbnail.
// Wait for the first samples to be available to make sure we are ready.
if (!_impl->waitForNextBits(ICON_TIMEOUT))
{
qDebug() << "No bits coming" << Qt::endl;
return false;
}
if (_videoType != VIDEO_WEBCAM) { // Generated thumbnail if source type is not camera
if (!_generateThumbnail())
qDebug() << "Could not generate thumbnail for " << uri << ": using generic icon." << Qt::endl;
}
_emitPropertyChanged("uri");
// Return success.
return true;
}
return false;
}
void Video::_doPlay()
{
_impl->setPlayState(true);
}
void Video::_doPause()
{
_impl->setPlayState(false);
}
bool Video::_generateThumbnail()
{
static QFileIconProvider provider;
// Default (in case seeking and loading don't work).
_icon = provider.icon(QFileInfo(_uri));
if (_icon.isNull()) {
if (_uri.startsWith(QString("/dev/video"))) {
_icon = QIcon(":/add-camera");
}
else {
_icon = QIcon(":/add-video");
}
}
// Try seeking to the middle of the movie.
if (!_impl->seekTo(0.5))
{
_impl->resetMovie();
return false;
}
// Try to get a sample from the current position.
// NOTE: There is no guarantee the sample has yet been acquired.
const uchar* bits;
if (!_impl->waitForNextBits(ICON_TIMEOUT, &bits))
{
qDebug() << "Second waiting wrong..." << Qt::endl;
return false;
}
// Copy bits into thumbnail QImage.
QImage thumbnail(getWidth(), getHeight(), QImage::Format_ARGB32);
for (int y=0; y<getHeight(); y++)
for (int x=0; x<getWidth(); x++)
{
// Transfer RGBA to ARGB.
uint r = *bits++;
uint b = *bits++;
uint g = *bits++;
bits++; // skip alpha
thumbnail.setPixel(x, y, qRgb(r, g, b));
}
// Generate icon.
_icon = QIcon(QPixmap::fromImage(thumbnail).scaled(MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE,
Qt::IgnoreAspectRatio));
// Reset movie.
_impl->resetMovie();
return true;
}
}