-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvideoplayer.cpp
353 lines (293 loc) · 9.9 KB
/
videoplayer.cpp
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
#include "videoplayer.h"
#include "ui_videoplayer.h"
VideoPlayer::VideoPlayer(QWidget *parent) :
MediaSourceBase(parent),
ui(new Ui::VideoPlayer)
{
ui->setupUi(this);
MediaSourceBase::init(ui->AudioMeterSliderL, ui->AudioMeterSliderR, ui->audioLabel, ui->GOButton, ui->VideoBox, ui->opacitySlider, ui->fadeCheck, ui->MonitorPushButton, ui->volumeSlider, ui->volSync);
id = "VideoPlayer";
ui->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
ui->playButton->setIconSize(QSize(32, 32));
connect(ui->playButton, SIGNAL(clicked()), this, SLOT(play()));
ui->pauseButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
ui->pauseButton->setIconSize(QSize(32, 32));
connect(ui->pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
ui->stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
ui->stopButton->setIconSize(QSize(32, 32));
connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stop()));
ui->dirSelectButton->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
ui->dirSelectButton->setIconSize(QSize(32, 32));
connect(ui->dirSelectButton, SIGNAL(clicked()), this, SLOT(selectFolder()));
connect(ui->fileList, SIGNAL(currentTextChanged(QString)), this, SLOT(newFileSelected(QString)));
connect(ui->positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(setPosition(int)));
connect(ui->loopCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(loopChanged(bool)));
connect(&videoSink, SIGNAL(newPrerollImage(QImage)), this, SLOT(newVideoFrameFromSink(QImage)));
connect(&positionTimer, SIGNAL(timeout()), this, SLOT(onPositionChanged()));
}
VideoPlayer::~VideoPlayer()
{
delete ui;
}
bool VideoPlayer::getLoop()
{
return ui->loopCheckBox->isChecked();
}
void VideoPlayer::setLoop(bool newState)
{
ui->loopCheckBox->setChecked(newState);
}
void VideoPlayer::init(QString videocaps, QString audiocaps)
{
this->videocaps = videocaps;
this->audiocaps = audiocaps;
}
void VideoPlayer::setPosition(const QTime & pos)
{
QGst::SeekEventPtr evt = QGst::SeekEvent::create(
1.0, QGst::FormatTime, QGst::SeekFlagFlush,
QGst::SeekTypeSet, QGst::ClockTime::fromTime(pos),
QGst::SeekTypeNone, QGst::ClockTime::None
);
pipeline->sendEvent(evt);
updatePositionLabel(length(), pos);
}
void VideoPlayer::selectFolder()
{
currentDir = QFileDialog::getExistingDirectory(this, "Select folder for videoplayer");
QDir dirInfo(currentDir);
ui->fileList->clear();
foreach (QString item, dirInfo.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::Readable, QDir::Name))
{
ui->fileList->addItem(item);
}
}
void VideoPlayer::newFileSelected(QString newFile)
{
qDebug() << "NEW FILE:" << currentDir << newFile;
if (!pipeline.isNull())
{
pipeline->setState(QGst::StateNull);
pipeline.clear();
}
onPositionChanged();
QString desc = QString(" filesrc location=\"%1\" ! decodebin name=decode !"
" queue ! videorate ! videoscale ! videoconvert ! "
" gamma name=gamma ! videobalance name=balance ! videoflip name=flip ! "
" appsink name=videosink caps=\"%2\""
" decode. ! queue ! audioresample ! audioconvert ! appsink name=audiosink caps=\"%3\"")
.arg(QString("%1/%2").arg(currentDir).arg(newFile))
.arg(videocaps)
.arg(audiocaps);
qDebug() << "Pipeline:" << desc;
pipeline = QGst::Parse::launch(desc).dynamicCast<QGst::Pipeline>();
QGlib::connect(pipeline->bus(), "message", this, &VideoPlayer::onBusMessage);
pipeline->bus()->addSignalWatch();
videoSink.setElement(pipeline->getElementByName("videosink"));
audioSink.setElement(pipeline->getElementByName("audiosink"));
gammaElement = pipeline->getElementByName("gamma");
videoBalanceElement = pipeline->getElementByName("balance");
videoFlipElement = pipeline->getElementByName("flip");
// Let the pipeline preroll to check for errors and such
pipeline->setState(QGst::StatePaused);
updateBackground();
}
void VideoPlayer::goButtonClicked()
{
emit fadeMeIn(false);
}
void VideoPlayer::onBusMessage(const QGst::MessagePtr &message)
{
qDebug() << "VIDEOPLAYER MESSAGE" << message->type() << message->typeName();
switch (message->type()) {
case QGst::MessageEos:
if (ui->loopCheckBox->isChecked())
{
setPosition(QTime(0, 0));
}
else
{
pause();
}
break;
case QGst::MessageError: //Some error occurred.
qCritical() << message.staticCast<QGst::ErrorMessage>()->error();
sourceOffline();
break;
case QGst::MessageStateChanged:
if (message->source() == pipeline) {
qDebug() << "VIDEOPLAYER NEWSTATE" << pipeline->currentState();
handlePipelineStateChange(message.staticCast<QGst::StateChangedMessage>());
}
break;
default:
break;
}
}
void VideoPlayer::handlePipelineStateChange(const QGst::StateChangedMessagePtr & scm)
{
switch (scm->newState()) {
case QGst::StatePlaying:
//start the timer when the pipeline starts playing
positionTimer.start(100);
if (scm->oldState() == QGst::StateNull)
{
sourceOnline();
}
break;
case QGst::StatePaused:
//stop the timer when the pipeline pauses
if(scm->oldState() == QGst::StatePlaying) {
positionTimer.stop();
}
break;
default:
break;
}
onStateChanged();
}
void VideoPlayer::onStateChanged()
{
QGst::State newState = QGst::StateNull;
if (!pipeline.isNull())
{
newState = pipeline->currentState();
}
ui->playButton->setEnabled(newState != QGst::StatePlaying);
ui->pauseButton->setEnabled(newState == QGst::StatePlaying);
ui->stopButton->setEnabled(newState != QGst::StateNull);
ui->positionSlider->setEnabled(newState != QGst::StateNull);
ui->opacitySlider->setEnabled((newState == QGst::StatePlaying) || (newState == QGst::StatePaused));
ui->volumeSlider->setEnabled((newState == QGst::StatePlaying) || (newState == QGst::StatePaused));
//if we are in Null state, call onPositionChanged() to restore
//the position of the slider and the text on the label
if (newState == QGst::StateNull) {
onPositionChanged();
ui->playButton->setEnabled(false);
}
updateBackground();
emit pipelineNewState(newState);
updatePositionLabel(length(), QTime(0, 0));
}
QTime VideoPlayer::length() const
{
if (!pipeline.isNull()) {
//here we query the pipeline about the content's duration
//and we request that the result is returned in time format
QGst::DurationQueryPtr query = QGst::DurationQuery::create(QGst::FormatTime);
pipeline->query(query);
return QGst::ClockTime(query->duration()).toTime();
} else {
return QTime(0,0);
}
}
QTime VideoPlayer::position() const
{
if (!pipeline.isNull()) {
//here we query the pipeline about its position
//and we request that the result is returned in time format
QGst::PositionQueryPtr query = QGst::PositionQuery::create(QGst::FormatTime);
pipeline->query(query);
return QGst::ClockTime(query->position()).toTime();
} else {
return QTime(0,0);
}
}
void VideoPlayer::onPositionChanged()
{
QTime len(0,0);
QTime curpos(0,0);
QGst::State state = QGst::StateNull;
if (!pipeline.isNull())
{
state = pipeline->currentState();
}
if ((state != QGst::StateReady) && (state != QGst::StateNull))
{
len = length();
curpos = position();
}
updatePositionLabel(len, curpos);
if (len != QTime(0,0)) {
ui->positionSlider->setValue(curpos.msecsTo(QTime(0,0)) * 1000 / len.msecsTo(QTime(0,0)));
} else {
ui->positionSlider->setValue(0);
}
if (curpos != QTime(0,0)) {
ui->positionLabel->setEnabled(true);
ui->positionSlider->setEnabled(true);
}
}
void VideoPlayer::updatePositionLabel(QTime len, QTime pos)
{
QTime remaining = QTime(0, 0);
remaining = remaining.addMSecs(pos.msecsTo(len));
ui->positionLabel->setText(QString("Position: %1 Duration: %2 Remaining: -%3").arg(pos.toString("hh:mm:ss.zzz")).arg(len.toString("hh:mm:ss.zzz")).arg(remaining.toString("hh:mm:ss.zzz")));
}
/* Called when the user changes the slider's position */
void VideoPlayer::setPosition(int value)
{
uint len = -length().msecsTo(QTime(0,0));
if (len != 0 && value > 0) {
QTime pos(0,0);
pos = pos.addMSecs(len * (value / 1000.0));
setPosition(pos);
}
}
void VideoPlayer::play()
{
if (pipeline) {
pipeline->setState(QGst::StatePlaying);
sourceOnline();
if (ui->goOnPlaycheckBox->isChecked())
{
setVideoOpacity(1.0);
}
}
}
void VideoPlayer::pause()
{
if (pipeline) {
pipeline->setState(QGst::StatePaused);
}
if (ui->unggoOnPauseCheckBox->isChecked())
{
setVideoOpacity(0.0);
}
}
void VideoPlayer::playOrPause()
{
if (!pipeline)
{
return;
}
if (pipeline->currentState() == QGst::StatePlaying)
{
pause();
}
else
{
play();
}
}
void VideoPlayer::stop()
{
sourceOffline();
ui->fileList->clearSelection();
updateBackground();
if (!pipeline.isNull()) {
pipeline->setState(QGst::StateNull);
//once the pipeline stops, the bus is flushed so we will
//not receive any StateChangedMessage about this.
//so, to inform the ui, we have to emit this signal manually.
onStateChanged();
}
}
void VideoPlayer::prevFile()
{
ui->fileList->setCurrentRow(ui->fileList->currentRow() - 1);
}
void VideoPlayer::nextFile()
{
ui->fileList->setCurrentRow(ui->fileList->currentRow() + 1);
}