src/3rdparty/phonon/gstreamer/widgetrenderer.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /*  This file is part of the KDE project.
       
     2 
       
     3     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 
       
     5     This library is free software: you can redistribute it and/or modify
       
     6     it under the terms of the GNU Lesser General Public License as published by
       
     7     the Free Software Foundation, either version 2.1 or 3 of the License.
       
     8 
       
     9     This library is distributed in the hope that it will be useful,
       
    10     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12     GNU Lesser General Public License for more details.
       
    13 
       
    14     You should have received a copy of the GNU Lesser General Public License
       
    15     along with this library.  If not, see <http://www.gnu.org/licenses/>.
       
    16 */
       
    17 
       
    18 #include <QtGui/QPainter>
       
    19 #include <gst/gst.h>
       
    20 #include "common.h"
       
    21 #include "message.h"
       
    22 #include "mediaobject.h"
       
    23 #include "qwidgetvideosink.h"
       
    24 #include "widgetrenderer.h"
       
    25 #include "qrgb.h"
       
    26 
       
    27 // support old OpenGL installations (1.2)
       
    28 // assume that if TEXTURE0 isn't defined, none are
       
    29 #ifndef GL_TEXTURE0
       
    30 # define GL_TEXTURE0    0x84C0
       
    31 # define GL_TEXTURE1    0x84C1
       
    32 # define GL_TEXTURE2    0x84C2
       
    33 #endif
       
    34 
       
    35 QT_BEGIN_NAMESPACE
       
    36 
       
    37 static void frameRendered()
       
    38 {
       
    39     static QString displayFps = qgetenv("PHONON_GST_FPS");
       
    40     if (displayFps.isEmpty())
       
    41         return;
       
    42 
       
    43     static int frames = 0;
       
    44     static QTime lastTime = QTime::currentTime();
       
    45     QTime time = QTime::currentTime();
       
    46 
       
    47     int delta = lastTime.msecsTo(time);
       
    48     if (delta > 2000) {
       
    49         printf("FPS: %f\n", 1000.0 * frames / qreal(delta));
       
    50         lastTime = time;
       
    51         frames = 0;
       
    52     }
       
    53 
       
    54     ++frames;
       
    55 }
       
    56 
       
    57 namespace Phonon
       
    58 {
       
    59 namespace Gstreamer
       
    60 {
       
    61 
       
    62 WidgetRenderer::WidgetRenderer(VideoWidget *videoWidget)
       
    63         : AbstractRenderer(videoWidget)
       
    64         , m_width(0)
       
    65         , m_height(0)
       
    66 {
       
    67     videoWidget->backend()->logMessage("Creating QWidget renderer");
       
    68     if ((m_videoSink = GST_ELEMENT(g_object_new(get_type_RGB(), NULL)))) {
       
    69         gst_object_ref (GST_OBJECT (m_videoSink)); //Take ownership
       
    70         gst_object_sink (GST_OBJECT (m_videoSink));
       
    71         
       
    72         QWidgetVideoSinkBase*  sink = reinterpret_cast<QWidgetVideoSinkBase*>(m_videoSink);
       
    73         // Let the videosink know which widget to direct frame updates to
       
    74         sink->renderWidget = videoWidget;
       
    75     }
       
    76 
       
    77     // Clear the background with black by default
       
    78     QPalette palette;
       
    79     palette.setColor(QPalette::Background, Qt::black);
       
    80     m_videoWidget->setPalette(palette);
       
    81     m_videoWidget->setAutoFillBackground(true);
       
    82     m_videoWidget->setAttribute(Qt::WA_NoSystemBackground, false);
       
    83     m_videoWidget->setAttribute(Qt::WA_PaintOnScreen, false);
       
    84 }
       
    85 
       
    86 void WidgetRenderer::setNextFrame(const QByteArray &array, int w, int h)
       
    87 {
       
    88     if (m_videoWidget->root()->state() == Phonon::LoadingState)
       
    89         return;
       
    90 
       
    91     m_frame = QImage(); 
       
    92     {
       
    93         m_frame = QImage((uchar *)array.constData(), w, h, QImage::Format_RGB32);
       
    94     }
       
    95 
       
    96     m_array = array;
       
    97     m_width = w;
       
    98     m_height = h;
       
    99 
       
   100     m_videoWidget->update();
       
   101 }
       
   102 
       
   103 void WidgetRenderer::handleMediaNodeEvent(const MediaNodeEvent *event)
       
   104 {
       
   105     switch (event->type()) {
       
   106     case MediaNodeEvent::SourceChanged:
       
   107     {
       
   108         clearFrame();
       
   109         break;
       
   110     }
       
   111     default:
       
   112         break;
       
   113     }
       
   114 }
       
   115 
       
   116 void WidgetRenderer::clearFrame()
       
   117 {
       
   118     m_frame = QImage();
       
   119     m_array = QByteArray();
       
   120     m_videoWidget->update();
       
   121 }
       
   122 
       
   123 const QImage &WidgetRenderer::currentFrame() const
       
   124 {
       
   125     return m_frame;
       
   126 }
       
   127 
       
   128 void WidgetRenderer::handlePaint(QPaintEvent *event)
       
   129 {
       
   130     Q_UNUSED(event);
       
   131     QPainter painter(m_videoWidget);
       
   132     m_drawFrameRect = m_videoWidget->calculateDrawFrameRect();
       
   133     painter.drawImage(drawFrameRect(), currentFrame());
       
   134     frameRendered();
       
   135 }
       
   136 
       
   137 bool WidgetRenderer::eventFilter(QEvent * event)
       
   138 {
       
   139     if (event->type() == QEvent::User) {
       
   140         NewFrameEvent *frameEvent= static_cast <NewFrameEvent *>(event);
       
   141         setNextFrame(frameEvent->frame, frameEvent->width, frameEvent->height);
       
   142         return true;
       
   143     }
       
   144     return false;
       
   145 }
       
   146 
       
   147 }
       
   148 } //namespace Phonon::Gstreamer
       
   149 
       
   150 QT_END_NAMESPACE