|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qgraphicsvideoitem.h" |
|
43 |
|
44 #include <qmediaobject.h> |
|
45 #include <qmediaservice.h> |
|
46 #include <qpaintervideosurface_p.h> |
|
47 #include <qvideorenderercontrol.h> |
|
48 |
|
49 #include <QtCore/qcoreevent.h> |
|
50 #include <QtCore/qpointer.h> |
|
51 |
|
52 #include <qvideosurfaceformat.h> |
|
53 |
|
54 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1) |
|
55 #include <QtOpenGL/qgl.h> |
|
56 #endif |
|
57 |
|
58 QT_BEGIN_NAMESPACE |
|
59 |
|
60 class QGraphicsVideoItemPrivate |
|
61 { |
|
62 public: |
|
63 QGraphicsVideoItemPrivate() |
|
64 : q_ptr(0) |
|
65 , surface(0) |
|
66 , mediaObject(0) |
|
67 , service(0) |
|
68 , rendererControl(0) |
|
69 , aspectRatioMode(Qt::KeepAspectRatio) |
|
70 , updatePaintDevice(true) |
|
71 , rect(0.0, 0.0, 320, 240) |
|
72 { |
|
73 } |
|
74 |
|
75 QGraphicsVideoItem *q_ptr; |
|
76 |
|
77 QPainterVideoSurface *surface; |
|
78 QPointer<QMediaObject> mediaObject; |
|
79 QMediaService *service; |
|
80 QVideoRendererControl *rendererControl; |
|
81 Qt::AspectRatioMode aspectRatioMode; |
|
82 bool updatePaintDevice; |
|
83 QRectF rect; |
|
84 QRectF boundingRect; |
|
85 QRectF sourceRect; |
|
86 QSizeF nativeSize; |
|
87 |
|
88 void clearService(); |
|
89 void updateRects(); |
|
90 |
|
91 void _q_present(); |
|
92 void _q_formatChanged(const QVideoSurfaceFormat &format); |
|
93 void _q_serviceDestroyed(); |
|
94 }; |
|
95 |
|
96 void QGraphicsVideoItemPrivate::clearService() |
|
97 { |
|
98 if (rendererControl) { |
|
99 surface->stop(); |
|
100 rendererControl->setSurface(0); |
|
101 service->releaseControl(rendererControl); |
|
102 rendererControl = 0; |
|
103 } |
|
104 if (service) { |
|
105 QObject::disconnect(service, SIGNAL(destroyed()), q_ptr, SLOT(_q_serviceDestroyed())); |
|
106 service = 0; |
|
107 } |
|
108 } |
|
109 |
|
110 void QGraphicsVideoItemPrivate::updateRects() |
|
111 { |
|
112 q_ptr->prepareGeometryChange(); |
|
113 |
|
114 if (nativeSize.isEmpty()) { |
|
115 boundingRect = QRectF(); |
|
116 } else if (aspectRatioMode == Qt::IgnoreAspectRatio) { |
|
117 boundingRect = rect; |
|
118 sourceRect = QRectF(0, 0, 1, 1); |
|
119 } else if (aspectRatioMode == Qt::KeepAspectRatio) { |
|
120 QSizeF size = nativeSize; |
|
121 size.scale(rect.size(), Qt::KeepAspectRatio); |
|
122 |
|
123 boundingRect = QRectF(0, 0, size.width(), size.height()); |
|
124 boundingRect.moveCenter(rect.center()); |
|
125 |
|
126 sourceRect = QRectF(0, 0, 1, 1); |
|
127 } else if (aspectRatioMode == Qt::KeepAspectRatioByExpanding) { |
|
128 boundingRect = rect; |
|
129 |
|
130 QSizeF size = rect.size(); |
|
131 size.scale(nativeSize, Qt::KeepAspectRatio); |
|
132 |
|
133 sourceRect = QRectF( |
|
134 0, 0, size.width() / nativeSize.width(), size.height() / nativeSize.height()); |
|
135 sourceRect.moveCenter(QPointF(0.5, 0.5)); |
|
136 } |
|
137 } |
|
138 |
|
139 void QGraphicsVideoItemPrivate::_q_present() |
|
140 { |
|
141 if (q_ptr->isObscured()) { |
|
142 q_ptr->update(boundingRect); |
|
143 surface->setReady(true); |
|
144 } else { |
|
145 q_ptr->update(boundingRect); |
|
146 } |
|
147 } |
|
148 |
|
149 void QGraphicsVideoItemPrivate::_q_formatChanged(const QVideoSurfaceFormat &format) |
|
150 { |
|
151 nativeSize = format.sizeHint(); |
|
152 |
|
153 updateRects(); |
|
154 |
|
155 emit q_ptr->nativeSizeChanged(nativeSize); |
|
156 } |
|
157 |
|
158 void QGraphicsVideoItemPrivate::_q_serviceDestroyed() |
|
159 { |
|
160 rendererControl = 0; |
|
161 service = 0; |
|
162 |
|
163 surface->stop(); |
|
164 } |
|
165 |
|
166 |
|
167 /*! |
|
168 \class QGraphicsVideoItem |
|
169 |
|
170 \brief The QGraphicsVideoItem class provides a graphics item which display video produced by a QMediaObject. |
|
171 |
|
172 \ingroup multimedia |
|
173 |
|
174 Attaching a QGraphicsVideoItem to a QMediaObject allows it to display |
|
175 the video or image output of that media object. A QGraphicsVideoItem |
|
176 is attached to a media object by passing a pointer to the QMediaObject |
|
177 to the setMediaObject() function. |
|
178 |
|
179 \code |
|
180 player = new QMediaPlayer(this); |
|
181 |
|
182 QGraphicsVideoItem *item = new QGraphicsVideoItem; |
|
183 player->setVideoOutput(item); |
|
184 graphicsView->scene()->addItem(item); |
|
185 graphicsView->show(); |
|
186 |
|
187 player->setMedia(video); |
|
188 player->play(); |
|
189 \endcode |
|
190 |
|
191 \bold {Note}: Only a single display output can be attached to a media |
|
192 object at one time. |
|
193 |
|
194 \sa QMediaObject, QMediaPlayer, QVideoWidget |
|
195 */ |
|
196 |
|
197 /*! |
|
198 Constructs a graphics item that displays video. |
|
199 |
|
200 The \a parent is passed to QGraphicsItem. |
|
201 */ |
|
202 QGraphicsVideoItem::QGraphicsVideoItem(QGraphicsItem *parent) |
|
203 : QGraphicsObject(parent) |
|
204 , d_ptr(new QGraphicsVideoItemPrivate) |
|
205 { |
|
206 d_ptr->q_ptr = this; |
|
207 d_ptr->surface = new QPainterVideoSurface; |
|
208 |
|
209 connect(d_ptr->surface, SIGNAL(frameChanged()), this, SLOT(_q_present())); |
|
210 connect(d_ptr->surface, SIGNAL(surfaceFormatChanged(QVideoSurfaceFormat)), |
|
211 this, SLOT(_q_formatChanged(QVideoSurfaceFormat))); |
|
212 } |
|
213 |
|
214 /*! |
|
215 Destroys a video graphics item. |
|
216 */ |
|
217 QGraphicsVideoItem::~QGraphicsVideoItem() |
|
218 { |
|
219 if (d_ptr->rendererControl) { |
|
220 d_ptr->rendererControl->setSurface(0); |
|
221 d_ptr->service->releaseControl(d_ptr->rendererControl); |
|
222 } |
|
223 |
|
224 delete d_ptr->surface; |
|
225 delete d_ptr; |
|
226 } |
|
227 |
|
228 /*! |
|
229 \property QGraphicsVideoItem::mediaObject |
|
230 \brief the media object which provides the video displayed by a graphics |
|
231 item. |
|
232 */ |
|
233 |
|
234 QMediaObject *QGraphicsVideoItem::mediaObject() const |
|
235 { |
|
236 return d_func()->mediaObject; |
|
237 } |
|
238 |
|
239 /*! |
|
240 \internal |
|
241 */ |
|
242 bool QGraphicsVideoItem::setMediaObject(QMediaObject *object) |
|
243 { |
|
244 Q_D(QGraphicsVideoItem); |
|
245 |
|
246 if (object == d->mediaObject) |
|
247 return true; |
|
248 |
|
249 d->clearService(); |
|
250 |
|
251 d->mediaObject = object; |
|
252 |
|
253 if (d->mediaObject) { |
|
254 d->service = d->mediaObject->service(); |
|
255 |
|
256 if (d->service) { |
|
257 QMediaControl *control = d->service->requestControl(QVideoRendererControl_iid); |
|
258 if (control) { |
|
259 d->rendererControl = qobject_cast<QVideoRendererControl *>(control); |
|
260 |
|
261 if (d->rendererControl) { |
|
262 d->rendererControl->setSurface(d->surface); |
|
263 |
|
264 connect(d->service, SIGNAL(destroyed()), this, SLOT(_q_serviceDestroyed())); |
|
265 |
|
266 return true; |
|
267 } |
|
268 if (control) |
|
269 d->service->releaseControl(control); |
|
270 } |
|
271 } |
|
272 } |
|
273 |
|
274 d->mediaObject = 0; |
|
275 return false; |
|
276 } |
|
277 |
|
278 /*! |
|
279 \property QGraphicsVideoItem::aspectRatioMode |
|
280 \brief how a video is scaled to fit the graphics item's size. |
|
281 */ |
|
282 |
|
283 Qt::AspectRatioMode QGraphicsVideoItem::aspectRatioMode() const |
|
284 { |
|
285 return d_func()->aspectRatioMode; |
|
286 } |
|
287 |
|
288 void QGraphicsVideoItem::setAspectRatioMode(Qt::AspectRatioMode mode) |
|
289 { |
|
290 Q_D(QGraphicsVideoItem); |
|
291 |
|
292 d->aspectRatioMode = mode; |
|
293 d->updateRects(); |
|
294 } |
|
295 |
|
296 /*! |
|
297 \property QGraphicsVideoItem::offset |
|
298 \brief the video item's offset. |
|
299 |
|
300 QGraphicsVideoItem will draw video using the offset for its top left |
|
301 corner. |
|
302 */ |
|
303 |
|
304 QPointF QGraphicsVideoItem::offset() const |
|
305 { |
|
306 return d_func()->rect.topLeft(); |
|
307 } |
|
308 |
|
309 void QGraphicsVideoItem::setOffset(const QPointF &offset) |
|
310 { |
|
311 Q_D(QGraphicsVideoItem); |
|
312 |
|
313 d->rect.moveTo(offset); |
|
314 d->updateRects(); |
|
315 } |
|
316 |
|
317 /*! |
|
318 \property QGraphicsVideoItem::size |
|
319 \brief the video item's size. |
|
320 |
|
321 QGraphicsVideoItem will draw video scaled to fit size according to its |
|
322 fillMode. |
|
323 */ |
|
324 |
|
325 QSizeF QGraphicsVideoItem::size() const |
|
326 { |
|
327 return d_func()->rect.size(); |
|
328 } |
|
329 |
|
330 void QGraphicsVideoItem::setSize(const QSizeF &size) |
|
331 { |
|
332 Q_D(QGraphicsVideoItem); |
|
333 |
|
334 d->rect.setSize(size.isValid() ? size : QSizeF(0, 0)); |
|
335 d->updateRects(); |
|
336 } |
|
337 |
|
338 /*! |
|
339 \property QGraphicsVideoItem::nativeSize |
|
340 \brief the native size of the video. |
|
341 */ |
|
342 |
|
343 QSizeF QGraphicsVideoItem::nativeSize() const |
|
344 { |
|
345 return d_func()->nativeSize; |
|
346 } |
|
347 |
|
348 /*! |
|
349 \fn QGraphicsVideoItem::nativeSizeChanged(const QSizeF &size) |
|
350 |
|
351 Signals that the native \a size of the video has changed. |
|
352 */ |
|
353 |
|
354 /*! |
|
355 \reimp |
|
356 */ |
|
357 QRectF QGraphicsVideoItem::boundingRect() const |
|
358 { |
|
359 return d_func()->boundingRect; |
|
360 } |
|
361 |
|
362 /*! |
|
363 \reimp |
|
364 */ |
|
365 void QGraphicsVideoItem::paint( |
|
366 QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
367 { |
|
368 Q_D(QGraphicsVideoItem); |
|
369 |
|
370 Q_UNUSED(option); |
|
371 Q_UNUSED(widget); |
|
372 |
|
373 if (d->surface && d->surface->isActive()) { |
|
374 d->surface->paint(painter, d->boundingRect, d->sourceRect); |
|
375 d->surface->setReady(true); |
|
376 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1) |
|
377 } else if (d->updatePaintDevice && (painter->paintEngine()->type() == QPaintEngine::OpenGL |
|
378 || painter->paintEngine()->type() == QPaintEngine::OpenGL2)) { |
|
379 d->updatePaintDevice = false; |
|
380 |
|
381 if (widget) |
|
382 connect(widget, SIGNAL(destroyed()), d->surface, SLOT(viewportDestroyed())); |
|
383 |
|
384 d->surface->setGLContext(const_cast<QGLContext *>(QGLContext::currentContext())); |
|
385 if (d->surface->supportedShaderTypes() & QPainterVideoSurface::GlslShader) { |
|
386 d->surface->setShaderType(QPainterVideoSurface::GlslShader); |
|
387 } else { |
|
388 d->surface->setShaderType(QPainterVideoSurface::FragmentProgramShader); |
|
389 } |
|
390 #endif |
|
391 } |
|
392 } |
|
393 |
|
394 /*! |
|
395 \reimp |
|
396 |
|
397 \internal |
|
398 */ |
|
399 QVariant QGraphicsVideoItem::itemChange(GraphicsItemChange change, const QVariant &value) |
|
400 { |
|
401 return QGraphicsItem::itemChange(change, value); |
|
402 } |
|
403 |
|
404 /*! |
|
405 \internal |
|
406 */ |
|
407 void QGraphicsVideoItem::timerEvent(QTimerEvent *event) |
|
408 { |
|
409 QGraphicsObject::timerEvent(event); |
|
410 } |
|
411 |
|
412 #include "moc_qgraphicsvideoitem.cpp" |
|
413 QT_END_NAMESPACE |