1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "videoplayer.h"
42 #include "videoitem.h"
43
44 #include <QtMultimedia>
45
46 #ifndef QT_NO_OPENGL
47 # include <QtOpenGL/QGLWidget>
48 #endif
49
VideoPlayer(QWidget * parent,Qt::WindowFlags flags)50 VideoPlayer::VideoPlayer(QWidget *parent, Qt::WindowFlags flags)
51 : QWidget(parent, flags)
52 , videoItem(0)
53 , playButton(0)
54 , positionSlider(0)
55 {
56 connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
57 this, SLOT(movieStateChanged(QMovie::MovieState)));
58 connect(&movie, SIGNAL(frameChanged(int)),
59 this, SLOT(frameChanged(int)));
60
61 videoItem = new VideoItem;
62
63 QGraphicsScene *scene = new QGraphicsScene(this);
64 QGraphicsView *graphicsView = new QGraphicsView(scene);
65
66 #ifndef QT_NO_OPENGL
67 graphicsView->setViewport(new QGLWidget);
68 #endif
69
70 scene->addItem(videoItem);
71
72 QSlider *rotateSlider = new QSlider(Qt::Horizontal);
73 rotateSlider->setRange(-180, 180);
74 rotateSlider->setValue(0);
75
76 connect(rotateSlider, SIGNAL(valueChanged(int)),
77 this, SLOT(rotateVideo(int)));
78
79 QAbstractButton *openButton = new QPushButton(tr("Open..."));
80 connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
81
82 playButton = new QPushButton;
83 playButton->setEnabled(false);
84 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
85
86 connect(playButton, SIGNAL(clicked()),
87 this, SLOT(play()));
88
89 positionSlider = new QSlider(Qt::Horizontal);
90 positionSlider->setRange(0, 0);
91
92 connect(positionSlider, SIGNAL(sliderMoved(int)),
93 this, SLOT(setPosition(int)));
94
95 connect(&movie, SIGNAL(frameChanged(int)),
96 positionSlider, SLOT(setValue(int)));
97
98 QBoxLayout *controlLayout = new QHBoxLayout;
99 controlLayout->setMargin(0);
100 controlLayout->addWidget(openButton);
101 controlLayout->addWidget(playButton);
102 controlLayout->addWidget(positionSlider);
103
104 QBoxLayout *layout = new QVBoxLayout;
105 layout->addWidget(graphicsView);
106 layout->addWidget(rotateSlider);
107 layout->addLayout(controlLayout);
108
109 setLayout(layout);
110 }
111
~VideoPlayer()112 VideoPlayer::~VideoPlayer()
113 {
114 }
115
openFile()116 void VideoPlayer::openFile()
117 {
118 QStringList supportedFormats;
119 foreach (QString fmt, QMovie::supportedFormats())
120 supportedFormats << fmt;
121 foreach (QString fmt, QImageReader::supportedImageFormats())
122 supportedFormats << fmt;
123
124 QString filter = "Images (";
125 foreach ( QString fmt, supportedFormats) {
126 filter.append(QString("*.%1 ").arg(fmt));
127 }
128 filter.append(")");
129
130 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),
131 QDir::homePath(), filter);
132
133 if (!fileName.isEmpty()) {
134 videoItem->stop();
135
136 movie.setFileName(fileName);
137
138 playButton->setEnabled(true);
139 positionSlider->setMaximum(movie.frameCount());
140
141 movie.jumpToFrame(0);
142 }
143 }
144
play()145 void VideoPlayer::play()
146 {
147 switch(movie.state()) {
148 case QMovie::NotRunning:
149 movie.start();
150 break;
151 case QMovie::Paused:
152 movie.setPaused(false);
153 break;
154 case QMovie::Running:
155 movie.setPaused(true);
156 break;
157 }
158 }
159
movieStateChanged(QMovie::MovieState state)160 void VideoPlayer::movieStateChanged(QMovie::MovieState state)
161 {
162 switch(state) {
163 case QMovie::NotRunning:
164 case QMovie::Paused:
165 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
166 break;
167 case QMovie::Running:
168 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
169 break;
170 }
171 }
172
frameChanged(int frame)173 void VideoPlayer::frameChanged(int frame)
174 {
175 if (!presentImage(movie.currentImage())) {
176 movie.stop();
177 playButton->setEnabled(false);
178 positionSlider->setMaximum(0);
179 } else {
180 positionSlider->setValue(frame);
181 }
182 }
183
setPosition(int frame)184 void VideoPlayer::setPosition(int frame)
185 {
186 movie.jumpToFrame(frame);
187 }
188
rotateVideo(int angle)189 void VideoPlayer::rotateVideo(int angle)
190 {
191 //rotate around the center of video element
192 qreal x = videoItem->boundingRect().width() / 2.0;
193 qreal y = videoItem->boundingRect().height() / 2.0;
194 videoItem->setTransform(QTransform().translate(x, y).rotate(angle).translate(-x, -y));
195 }
196
presentImage(const QImage & image)197 bool VideoPlayer::presentImage(const QImage &image)
198 {
199 QVideoFrame frame(image);
200
201 if (!frame.isValid())
202 return false;
203
204 QVideoSurfaceFormat currentFormat = videoItem->surfaceFormat();
205
206 if (frame.pixelFormat() != currentFormat.pixelFormat()
207 || frame.size() != currentFormat.frameSize()) {
208 QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
209
210 if (!videoItem->start(format))
211 return false;
212 }
213
214 if (!videoItem->present(frame)) {
215 videoItem->stop();
216
217 return false;
218 } else {
219 return true;
220 }
221 }
222