QtGStreamer  0.10.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
graphicsvideosurface.cpp
00001 /*
00002     Copyright (C) 2012 Collabora Ltd. <info@collabora.com>
00003       @author George Kiagiadakis <george.kiagiadakis@collabora.com>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as published
00007     by the Free Software Foundation; either version 2.1 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 #include "graphicsvideosurface_p.h"
00019 #include "../elementfactory.h"
00020 #include "../../QGlib/connect.h"
00021 
00022 #ifndef QTGSTREAMER_UI_NO_OPENGL
00023 # include <QtOpenGL/QGLWidget>
00024 #endif
00025 
00026 namespace QGst {
00027 namespace Ui {
00028 
00029 GraphicsVideoSurface::GraphicsVideoSurface(QGraphicsView *parent)
00030     : QObject(parent), d(new GraphicsVideoSurfacePrivate)
00031 {
00032     d->view = parent;
00033 }
00034 
00035 GraphicsVideoSurface::~GraphicsVideoSurface()
00036 {
00037     if (!d->videoSink.isNull()) {
00038         d->videoSink->setState(QGst::StateNull);
00039     }
00040 
00041     delete d;
00042 }
00043 
00044 ElementPtr GraphicsVideoSurface::videoSink() const
00045 {
00046     if (d->videoSink.isNull()) {
00047 #ifndef QTGSTREAMER_UI_NO_OPENGL
00048         //if the viewport is a QGLWidget, profit from it
00049         QGLWidget *glw = qobject_cast<QGLWidget*>(d->view->viewport());
00050         if (glw) {
00051             d->videoSink = QGst::ElementFactory::make("qtglvideosink");
00052 
00053             if (!d->videoSink.isNull()) {
00054                 glw->makeCurrent();
00055                 d->videoSink->setProperty("glcontext", (void*) QGLContext::currentContext());
00056                 glw->doneCurrent();
00057 
00058                 if (d->videoSink->setState(QGst::StateReady) != QGst::StateChangeSuccess) {
00059                     d->videoSink.clear();
00060                 }
00061             }
00062         }
00063 #endif
00064 
00065         if (d->videoSink.isNull()) {
00066             d->videoSink = QGst::ElementFactory::make("qtvideosink");
00067 
00068             if (d->videoSink.isNull()) {
00069                 qCritical("Failed to create qtvideosink. Make sure it is installed correctly");
00070                 return ElementPtr();
00071             }
00072         }
00073 
00074         QGlib::connect(d->videoSink, "update",
00075                        const_cast<GraphicsVideoSurface*>(this),
00076                        &GraphicsVideoSurface::onUpdate);
00077     }
00078 
00079     return d->videoSink;
00080 }
00081 
00082 void GraphicsVideoSurface::onUpdate()
00083 {
00084     Q_FOREACH(GraphicsVideoWidget *item, d->items) {
00085         item->update(item->rect());
00086     }
00087 }
00088 
00089 } // namespace Ui
00090 } // namespace QGst