-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerWidget.cpp
365 lines (321 loc) · 7.62 KB
/
PlayerWidget.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
354
355
356
357
358
359
360
361
362
363
364
365
#include <QLabel>
#include <QPushButton>
#include <QMessageBox>
#include <QPainter>
#include "PlayerWidget.h"
#include "mainwindow.h"
/*! \brief Setup and connect all s&s
*
* Setup and connect all s&s
*
* @param parent parent
* @param mainwin pointer to the mainwindow so we can use s&s
* @param buff pointer to the images buffer
*/
PlayerWidget::PlayerWidget(
QWidget *parent,
QWidget *mainwin,
ImagesBuffer *buff
) : QWidget(parent), _bmng(buff)
{
playState = false;
playbackTimer = new QTimer(this);
connect(playbackTimer, SIGNAL(timeout()), this, SLOT(updateFrame()));
// S&S to mainwin
connect(this, SIGNAL(newFrame(QPixmap)), mainwin, SLOT(updateFrame(QPixmap)));
connect(this, SIGNAL(timeChanged(qint64)), mainwin, SLOT(updateTime(qint64)));
connect(this, SIGNAL(playPauseToggle(bool)), mainwin, SLOT(changePlayPause(bool)));
connect(this, SIGNAL(frameChanged()), mainwin, SLOT(updateSlider()));
connect(this, SIGNAL(endOfStream()), mainwin, SLOT(endOfStream()));
}
/*! \brief Destroyer
*
* Destroyer
*/
PlayerWidget::~PlayerWidget()
{}
/*! \brief display last loaded frame.
*
* Display last loaded frame.
*/
void PlayerWidget::displayFrame()
{
if (!_bmng->isVideoLoaded())
return;
emit newFrame(_actualFrame.img);
emit timeChanged(_actualFrame.time);
}
/*! \brief load and display the next frame
*
* Load and display the next frame and emit the signal frameChanged()
*/
void PlayerWidget::updateFrame()
{
if (!nextSingleFrame()) {
stopVideo(false);
emit endOfStream();
}
emit frameChanged();
}
/******************* PUBLIC METHODS ************/
/***************************************
********* FRAME ACTIONS *********
***************************************/
/*! \brief reload and display last frame.
*
* Reload last decoded frame, usefull when a resize event occurs.
*/
void PlayerWidget::reloadFrame()
{
// this is needed otherwise a "Load video first"
// error is thrown on app startup if used inside resizeEvent
if (_bmng->isVideoLoaded())
displayFrame();
}
/*! \brief displays next frame.
*
* Displays the next frame from the current player position.
* This won't update the buffer. This function must be called only for
* the playback of the video.
*
* @return success or not
*/
bool PlayerWidget::nextSingleFrame()
{
bool ok;
if (!(ok = _bmng->getSingleFrame(_actualFrame, _actualFrame.num + 1))) {
// end of stream?
QMessageBox::critical(NULL, "Error", "End of stream");
}
displayFrame();
return ok;
}
/*! \brief displays next frame.
*
* Displays the next frame from the current player position. This will update
* the buffer.
*
* @return success or not
* @see prevFrame()
*/
bool PlayerWidget::nextFrame(){
bool ok;
if (!(ok = _bmng->getFrame(_actualFrame, _actualFrame.num + 1))) {
// end of stream?
QMessageBox::critical(NULL, "Error", "End of stream");
}
displayFrame();
return ok;
}
/*! \brief displays previous frame.
*
* Displays the previous frame from the current player position. This will update
* the buffer.
*
* @return success or not
* @see nextFrame()
*/
bool PlayerWidget::prevFrame(){
if (!_bmng->getFrame(_actualFrame, _actualFrame.num - 1)) {
// QMessageBox::critical(NULL, "Error", "seekPrevFrame failed");
return false;
}
displayFrame();
return true;
}
/*! \brief seek to frame number
*
* Displays the given frame number
*
* @param num frame number
* @see seekToTime()
*/
void PlayerWidget::seekToFrame(const qint64 num){
if (!_bmng->getFrame(_actualFrame, num)) {
QMessageBox::critical(NULL, "Error", "seekToFrame failed");
return;
}
displayFrame();
return;
}
/*! \brief seek to given time
*
* Displays the frame near the given time
*
* @param ms time in milliseconds
* @see seekToFrame()
*/
void PlayerWidget::seekToTime(const qint64 ms){
if (!_bmng->getFrameByTime(_actualFrame, ms)) {
QMessageBox::critical(NULL, "Error", "seekToFrame failed");
return;
}
displayFrame();
return;
}
/*! \brief seek to given time percentage
*
* Displays the frame near the given percentage of the entire video length
*
* @param perc double value from 0 to 1
*/
void PlayerWidget::seekToTimePercentage(const double perc){
if (!_bmng->getFrameByTimePercentage(_actualFrame, perc)) {
QMessageBox::critical(NULL, "Error", "seekToFrame failed");
return;
}
displayFrame();
return;
}
/***************************************
********* VIDEO ACTIONS *********
***************************************/
/*! \brief load a video.
*
* Open and load a video by using ffmpeg's decoder.
*
* @param fileName path to the video
*/
void PlayerWidget::loadVideo(const QString fileName)
{
if (!_bmng->loadVideo(fileName)) {
QMessageBox::critical(NULL, "Error", "Error loading the video!");
return;
}
// Retrieve datas
frameMs = _bmng->getFrameMsec();
numFrames = _bmng->getNumFrames();
videoLength = _bmng->getVideoLengthMs();
_bmng->getMidFrame(_actualFrame);
displayFrame();
}
/*! \brief play/pause the video.
*
* Change the state of the player between play and pause.
* If the player isn't playing starts, otherwhise if it's
* paused it resumes the playback.
*/
void PlayerWidget::playPause()
{
if (!_bmng->isVideoLoaded())
return;
playState = !playState;
if (playState) {
playVideo();
} else {
pauseVideo();
}
emit playPauseToggle(playState);
}
/*! \brief play the video.
*
* Play the video by starting the timer.
* @see pauseVideo()
* @see stopVideo()
*/
bool PlayerWidget::playVideo()
{
if (!_bmng->isVideoLoaded())
return false;
playbackTimer->start(frameMs);
return true;
}
/*! \brief pause the video.
*
* Pause the video by stopping the timer.
*
* @see playVideo()
* @see stopVideo()
*/
bool PlayerWidget::pauseVideo()
{
if (!_bmng->isVideoLoaded())
return false;
playbackTimer->stop();
// do "another" getFrame because while in playback the buffer isn't updated
// (for performance and visualization reason).
if (!_bmng->getFrame(_actualFrame, _actualFrame.num)) {
QMessageBox::critical(NULL, "Error", "seekToFrame failed");
return false;
}
return true;
}
/*! \brief stop the video.
*
* Stop the video playback and seek to its start point.
*
* @param reset seek to frame 0 or not
* @see playVideo()
* @see pauseVideo()
*/
bool PlayerWidget::stopVideo(const bool reset)
{
if (!_bmng->isVideoLoaded())
return false;
// pause if playing
if (playState) {
playPause();
}
// reset
if (reset)
seekToFrame(0);
emit frameChanged();
return true;
}
/***************************************
********* GETTERS *********
***************************************/
/*! \brief the video is playing?
*
* Checks if the video is playing.
*/
bool PlayerWidget::isVideoPlaying()
{
return playState;
}
/*! \brief the video is playing?
*
* Checks if the video is playing.
*/
bool PlayerWidget::isVideoLoaded()
{
return _bmng->isVideoLoaded();
}
/*! \brief Get current frame number.
*
* This functions is used to get the current frame number.
*
* @return current frame number.
* @see currentFrameTime()
*/
qint64 PlayerWidget::currentFrameNumber() {
return _actualFrame.num;
}
/*! \brief Get current frame time.
*
* This functions is used to get the time of the current frame.
*
* @return current frame time.
* @see currentFrameNumber()
*/
qint64 PlayerWidget::currentFrameTime() {
return _actualFrame.time;
}
/*! \brief Get number of frames
*
* Retrieve the number of frames
*/
qint64 PlayerWidget::getNumFrames() {
return numFrames;
}
/*! \brief Percentage of time passed (0 to 1)
*
* Calculated the ratio (0 to 1) of the current frame time compared to the
* total video length.
*
* @return ratio percentage decimal (0 to 1)
* @see currentFrameTime()
*/
double PlayerWidget::currentTimePercentage() {
return _actualFrame.time / (double)videoLength;
}