qtmobility/plugins/multimedia/gstreamer/mediacapture/maemo/qgstreamercaptureservice_maemo.cpp
changeset 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 "qgstreamercaptureservice_maemo.h"
       
    43 #include "qgstreamercapturesession_maemo.h"
       
    44 #include "qgstreamerrecordercontrol_maemo.h"
       
    45 #include "qgstreamermediacontainercontrol_maemo.h"
       
    46 #include "qgstreameraudioencode_maemo.h"
       
    47 #include "qgstreamervideoencode_maemo.h"
       
    48 #include "qgstreamerbushelper.h"
       
    49 #include "qgstreamercapturemetadatacontrol_maemo.h"
       
    50 
       
    51 #include "qgstreamervideooutputcontrol.h"
       
    52 #include "qgstreameraudioinputendpointselector.h"
       
    53 #include "qgstreamervideoinputdevicecontrol.h"
       
    54 
       
    55 #include "qgstreamervideooverlay.h"
       
    56 #include "qgstreamervideorenderer.h"
       
    57 
       
    58 #include "qgstreamervideowidget.h"
       
    59 
       
    60 #include <qmediaserviceprovider.h>
       
    61 
       
    62 #include <QtCore/qdebug.h>
       
    63 
       
    64 class QGstreamerVideoRendererWrapper : public QGstreamerElementFactory
       
    65 {
       
    66 public:
       
    67     QGstreamerVideoRendererWrapper(QGstreamerVideoRendererInterface *videoRenderer)
       
    68         :m_videoRenderer(videoRenderer),
       
    69          m_bin(0),
       
    70          m_element(0),
       
    71          m_colorspace(0)
       
    72     {
       
    73     }
       
    74 
       
    75     virtual ~QGstreamerVideoRendererWrapper()
       
    76     {
       
    77         if (m_bin)
       
    78             gst_object_unref(GST_OBJECT(m_bin));
       
    79     }
       
    80 
       
    81     GstElement *buildElement()
       
    82     {
       
    83 #ifdef Q_WS_MAEMO_5
       
    84         return m_element = m_videoRenderer->videoSink();
       
    85 #endif
       
    86         if (m_bin == NULL) {
       
    87             GstBin * bin = GST_BIN(gst_bin_new(NULL));
       
    88 
       
    89             m_colorspace = gst_element_factory_make("ffmpegcolorspace", NULL);
       
    90             m_element = m_videoRenderer->videoSink();
       
    91 
       
    92             gst_bin_add(bin, m_colorspace);
       
    93             gst_bin_add(bin, m_element);
       
    94             gst_element_link(m_colorspace, m_element);
       
    95 
       
    96             // add ghostpads
       
    97             GstPad *pad = gst_element_get_static_pad(m_colorspace, "sink");
       
    98             gst_element_add_pad(GST_ELEMENT(bin), gst_ghost_pad_new("sink", pad));
       
    99             gst_object_unref(GST_OBJECT(pad));
       
   100 
       
   101             m_bin = GST_ELEMENT(bin);
       
   102         }
       
   103 
       
   104         m_videoRenderer->precessNewStream();
       
   105 
       
   106         gst_object_ref(GST_OBJECT(m_bin));
       
   107         return m_bin;
       
   108     }
       
   109 
       
   110     void prepareWinId()
       
   111     {
       
   112         m_videoRenderer->precessNewStream();
       
   113     }
       
   114 
       
   115 private:
       
   116     QGstreamerVideoRendererInterface *m_videoRenderer;
       
   117 
       
   118     GstElement *m_bin;
       
   119     GstElement *m_element;
       
   120     GstElement *m_colorspace;
       
   121 };
       
   122 
       
   123 
       
   124 QGstreamerCaptureService::QGstreamerCaptureService(const QString &service, QObject *parent):
       
   125     QMediaService(parent)
       
   126 {
       
   127     static bool initialized = false;
       
   128     if (!initialized) {
       
   129         initialized = true;
       
   130         gst_init(NULL, NULL);
       
   131     }
       
   132 
       
   133     m_captureSession = 0;
       
   134     m_cameraControl = 0;
       
   135     m_metaDataControl = 0;
       
   136 
       
   137     m_audioInputEndpointSelector = 0;
       
   138     m_videoInputDevice = 0;
       
   139 
       
   140     m_videoOutput = 0;
       
   141     m_videoRenderer = 0;
       
   142     m_videoRendererFactory = 0;
       
   143     m_videoWindow = 0;
       
   144     m_videoWindowFactory = 0;
       
   145     m_videoWidgetControl = 0;
       
   146     m_videoWidgetFactory = 0;
       
   147     m_imageCaptureControl = 0;
       
   148 
       
   149 
       
   150     if (service == Q_MEDIASERVICE_AUDIOSOURCE) {
       
   151         m_captureSession = new QGstreamerCaptureSession(QGstreamerCaptureSession::Audio, this);
       
   152     }
       
   153 
       
   154     bool captureVideo = false;
       
   155     
       
   156     if (captureVideo) {
       
   157         m_captureSession = new QGstreamerCaptureSession(QGstreamerCaptureSession::AudioAndVideo, this);
       
   158         //TODO:m_captureSession->setVideoInput(m_cameraControl);
       
   159         m_videoInputDevice = new QGstreamerVideoInputDeviceControl(m_captureSession);
       
   160 
       
   161         //TODO:connect(m_videoInputDevice, SIGNAL(selectedDeviceChanged(QString)),
       
   162         //        m_cameraControl, SLOT(setDevice(QString)));
       
   163 
       
   164         //TODO:if (m_videoInputDevice->deviceCount())
       
   165         //    m_cameraControl->setDevice(m_videoInputDevice->deviceName(m_videoInputDevice->selectedDevice()));
       
   166 
       
   167         m_videoOutput = new QGstreamerVideoOutputControl(this);
       
   168         connect(m_videoOutput, SIGNAL(outputChanged(QVideoOutputControl::Output)),
       
   169                 this, SLOT(videoOutputChanged(QVideoOutputControl::Output)));
       
   170 
       
   171         m_videoRenderer = new QGstreamerVideoRenderer(this);
       
   172         m_videoRendererFactory = new QGstreamerVideoRendererWrapper(m_videoRenderer);
       
   173         m_videoWindow = new QGstreamerVideoOverlay(this);
       
   174         m_videoWindowFactory = new QGstreamerVideoRendererWrapper(m_videoWindow);
       
   175 
       
   176         m_videoWidgetControl = new QGstreamerVideoWidgetControl(this);
       
   177         m_videoWidgetFactory = new QGstreamerVideoRendererWrapper(m_videoWidgetControl);
       
   178 
       
   179         m_videoOutput->setAvailableOutputs(QList<QVideoOutputControl::Output>()
       
   180                                            << QVideoOutputControl::RendererOutput
       
   181                                            << QVideoOutputControl::WindowOutput
       
   182                                            << QVideoOutputControl::WidgetOutput);
       
   183     }
       
   184     
       
   185     if (!m_captureSession) {
       
   186         qWarning() << "Service type is not supported:" << service;
       
   187         return;
       
   188     }
       
   189 
       
   190     m_audioInputEndpointSelector = new QGstreamerAudioInputEndpointSelector(this);
       
   191     connect(m_audioInputEndpointSelector, SIGNAL(activeEndpointChanged(QString)), m_captureSession, SLOT(setCaptureDevice(QString)));
       
   192 
       
   193     if (m_captureSession && m_audioInputEndpointSelector->availableEndpoints().size() > 0)
       
   194         m_captureSession->setCaptureDevice(m_audioInputEndpointSelector->defaultEndpoint());
       
   195 
       
   196     m_metaDataControl = new QGstreamerCaptureMetaDataControl(this);
       
   197     connect(m_metaDataControl, SIGNAL(metaDataChanged(QMap<QByteArray,QVariant>)),
       
   198             m_captureSession, SLOT(setMetaData(QMap<QByteArray,QVariant>)));
       
   199 }
       
   200 
       
   201 QGstreamerCaptureService::~QGstreamerCaptureService()
       
   202 {
       
   203 }
       
   204 
       
   205 QMediaControl *QGstreamerCaptureService::control(const char *name) const
       
   206 {
       
   207     if (qstrcmp(name, QVideoOutputControl_iid) == 0)
       
   208         return m_videoOutput;
       
   209 
       
   210     if (qstrcmp(name, QVideoRendererControl_iid) == 0)
       
   211         return m_videoRenderer;
       
   212 
       
   213     if (qstrcmp(name, QVideoWindowControl_iid) == 0)
       
   214         return m_videoWindow;
       
   215 
       
   216     if (qstrcmp(name, QVideoWidgetControl_iid) == 0)
       
   217         return m_videoWidgetControl;
       
   218 
       
   219     if (qstrcmp(name,QAudioEndpointSelector_iid) == 0)
       
   220         return m_audioInputEndpointSelector;
       
   221 
       
   222     if (qstrcmp(name,QVideoDeviceControl_iid) == 0)
       
   223         return m_videoInputDevice;
       
   224 
       
   225     if (qstrcmp(name,QMediaRecorderControl_iid) == 0)
       
   226         return m_captureSession->recorderControl();
       
   227 
       
   228     if (qstrcmp(name,QAudioEncoderControl_iid) == 0)
       
   229         return m_captureSession->audioEncodeControl();
       
   230 
       
   231     if (qstrcmp(name,QVideoEncoderControl_iid) == 0)
       
   232         return m_captureSession->videoEncodeControl();
       
   233 
       
   234     if (qstrcmp(name,QMediaContainerControl_iid) == 0)
       
   235         return m_captureSession->mediaContainerControl();
       
   236 
       
   237     if (qstrcmp(name,QMetaDataControl_iid) == 0)
       
   238         return m_metaDataControl;
       
   239 
       
   240     return 0;
       
   241 }
       
   242 
       
   243 void QGstreamerCaptureService::videoOutputChanged(QVideoOutputControl::Output output)
       
   244 {
       
   245     switch (output) {
       
   246     case QVideoOutputControl::NoOutput:
       
   247         m_captureSession->setVideoPreview(0);
       
   248         break;
       
   249     case QVideoOutputControl::RendererOutput:
       
   250         m_captureSession->setVideoPreview(m_videoRendererFactory);
       
   251         break;
       
   252     case QVideoOutputControl::WindowOutput:
       
   253         m_captureSession->setVideoPreview(m_videoWindowFactory);
       
   254         break;
       
   255     case QVideoOutputControl::WidgetOutput:
       
   256         m_captureSession->setVideoPreview(m_videoWidgetFactory);
       
   257         break;
       
   258     default:
       
   259         qWarning("Invalid video output selection");
       
   260         m_captureSession->setVideoPreview(0);
       
   261         break;
       
   262     }
       
   263 }
       
   264