plugins/multimedia/gstreamer/qgstreamerbushelper.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 <QMap>
       
    43 #include <QTimer>
       
    44 #include <QMutex>
       
    45 
       
    46 #include "qgstreamerbushelper.h"
       
    47 
       
    48 
       
    49 #ifndef QT_NO_GLIB
       
    50 class QGstreamerBusHelperPrivate : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 
       
    54 public:
       
    55     void addWatch(GstBus* bus, QGstreamerBusHelper* helper)
       
    56     {
       
    57         setParent(helper);
       
    58         m_tag = gst_bus_add_watch_full(bus, 0, busCallback, this, NULL);
       
    59         m_helper = helper;
       
    60         filter = 0;
       
    61     }
       
    62 
       
    63     void removeWatch(QGstreamerBusHelper* helper)
       
    64     {
       
    65         Q_UNUSED(helper);
       
    66         g_source_remove(m_tag);
       
    67     }
       
    68 
       
    69     static QGstreamerBusHelperPrivate* instance()
       
    70     {
       
    71         return new QGstreamerBusHelperPrivate;
       
    72     }
       
    73 
       
    74 private:
       
    75     void processMessage(GstBus* bus, GstMessage* message)
       
    76     {
       
    77         Q_UNUSED(bus);
       
    78         emit m_helper->message(message);
       
    79     }
       
    80 
       
    81     static gboolean busCallback(GstBus *bus, GstMessage *message, gpointer data)
       
    82     {
       
    83         reinterpret_cast<QGstreamerBusHelperPrivate*>(data)->processMessage(bus, message);
       
    84         return TRUE;
       
    85     }
       
    86 
       
    87     guint       m_tag;
       
    88     QGstreamerBusHelper*  m_helper;
       
    89 
       
    90 public:
       
    91     GstBus* bus;
       
    92     QGstreamerSyncEventFilter *filter;
       
    93     QMutex filterMutex;
       
    94 };
       
    95 
       
    96 #else
       
    97 
       
    98 class QGstreamerBusHelperPrivate : public QObject
       
    99 {
       
   100     Q_OBJECT
       
   101     typedef QMap<QGstreamerBusHelper*, GstBus*>   HelperMap;
       
   102 
       
   103 public:
       
   104     void addWatch(GstBus* bus, QGstreamerBusHelper* helper)
       
   105     {
       
   106         m_helperMap.insert(helper, bus);
       
   107 
       
   108         if (m_helperMap.size() == 1)
       
   109             m_intervalTimer->start();
       
   110     }
       
   111 
       
   112     void removeWatch(QGstreamerBusHelper* helper)
       
   113     {
       
   114         m_helperMap.remove(helper);
       
   115 
       
   116         if (m_helperMap.size() == 0)
       
   117             m_intervalTimer->stop();
       
   118     }
       
   119 
       
   120     static QGstreamerBusHelperPrivate* instance()
       
   121     {
       
   122         static QGstreamerBusHelperPrivate self;
       
   123 
       
   124         return &self;
       
   125     }
       
   126 
       
   127 private slots:
       
   128     void interval()
       
   129     {
       
   130         for (HelperMap::iterator it = m_helperMap.begin(); it != m_helperMap.end(); ++it) {
       
   131             GstMessage* message;
       
   132 
       
   133             while ((message = gst_bus_poll(it.value(), GST_MESSAGE_ANY, 0)) != 0) {
       
   134                 emit it.key()->message(message);
       
   135                 gst_message_unref(message);
       
   136             }
       
   137 
       
   138             emit it.key()->message(QGstreamerMessage());
       
   139         }
       
   140     }
       
   141 
       
   142 private:
       
   143     QGstreamerBusHelperPrivate()
       
   144     {
       
   145         m_intervalTimer = new QTimer(this);
       
   146         m_intervalTimer->setInterval(250);
       
   147 
       
   148         connect(m_intervalTimer, SIGNAL(timeout()), SLOT(interval()));
       
   149     }
       
   150 
       
   151     HelperMap   m_helperMap;
       
   152     QTimer*     m_intervalTimer;
       
   153 
       
   154 public:
       
   155     GstBus* bus;
       
   156     QGstreamerSyncEventFilter *filter;
       
   157     QMutex filterMutex;
       
   158 };
       
   159 #endif
       
   160 
       
   161 
       
   162 static GstBusSyncReply syncGstBusFilter(GstBus* bus, GstMessage* message, QGstreamerBusHelperPrivate *d)
       
   163 {
       
   164     Q_UNUSED(bus);
       
   165     QMutexLocker lock(&d->filterMutex);
       
   166 
       
   167     bool res = false;
       
   168 
       
   169     if (d->filter)
       
   170         res = d->filter->processSyncMessage(QGstreamerMessage(message));
       
   171 
       
   172     return res ? GST_BUS_DROP : GST_BUS_PASS;
       
   173 }
       
   174 
       
   175 
       
   176 /*!
       
   177     \class gstreamer::QGstreamerBusHelper
       
   178     \internal
       
   179 */
       
   180 
       
   181 QGstreamerBusHelper::QGstreamerBusHelper(GstBus* bus, QObject* parent):
       
   182     QObject(parent),
       
   183     d(QGstreamerBusHelperPrivate::instance())
       
   184 {
       
   185     d->bus = bus;
       
   186     d->addWatch(bus, this);
       
   187 
       
   188     gst_bus_set_sync_handler(bus, (GstBusSyncHandler)syncGstBusFilter, d);
       
   189 }
       
   190 
       
   191 QGstreamerBusHelper::~QGstreamerBusHelper()
       
   192 {
       
   193     d->removeWatch(this);
       
   194     gst_bus_set_sync_handler(d->bus,0,0);
       
   195 }
       
   196 
       
   197 void QGstreamerBusHelper::installSyncEventFilter(QGstreamerSyncEventFilter *filter)
       
   198 {
       
   199     QMutexLocker lock(&d->filterMutex);
       
   200     d->filter = filter;
       
   201 }
       
   202 
       
   203 #include "qgstreamerbushelper.moc"