1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
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 |
|
---|
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 |
|
---|
112 | VideoPlayer::~VideoPlayer()
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
184 | void VideoPlayer::setPosition(int frame)
|
---|
185 | {
|
---|
186 | movie.jumpToFrame(frame);
|
---|
187 | }
|
---|
188 |
|
---|
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 |
|
---|
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 | }
|
---|