qtmobility/plugins/multimedia/directshow/dsserviceplugin.cpp
changeset 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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 <QtCore/qstring.h>
       
    43 #include <QtCore/qdebug.h>
       
    44 #include <QtCore/QFile>
       
    45 
       
    46 #include "dsserviceplugin.h"
       
    47 
       
    48 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
    49 #include "dscameraservice.h"
       
    50 #endif
       
    51 
       
    52 #ifdef QMEDIA_DIRECTSHOW_PLAYER
       
    53 #include "directshowplayerservice.h"
       
    54 #endif
       
    55 
       
    56 #include <qmediaserviceprovider.h>
       
    57 
       
    58 
       
    59 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
    60 #ifndef _STRSAFE_H_INCLUDED_
       
    61 #include <tchar.h>
       
    62 #endif
       
    63 #include <dshow.h>
       
    64 #include <objbase.h>
       
    65 #include <initguid.h>
       
    66 #pragma comment(lib, "strmiids.lib")
       
    67 #pragma comment(lib, "ole32.lib")
       
    68 #include <windows.h>
       
    69 #endif
       
    70 
       
    71 QStringList DSServicePlugin::keys() const
       
    72 {
       
    73     return QStringList()
       
    74 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
    75             << QLatin1String(Q_MEDIASERVICE_CAMERA)
       
    76 #endif
       
    77 #ifdef QMEDIA_DIRECTSHOW_PLAYER
       
    78             << QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER)
       
    79 #endif
       
    80             ;
       
    81 }
       
    82 
       
    83 QMediaService* DSServicePlugin::create(QString const& key)
       
    84 {
       
    85 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
    86     if (key == QLatin1String(Q_MEDIASERVICE_CAMERA))
       
    87         return new DSCameraService;
       
    88 #endif
       
    89 #ifdef QMEDIA_DIRECTSHOW_PLAYER
       
    90     if (key == QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER))
       
    91         return new DirectShowPlayerService;
       
    92 #endif
       
    93 
       
    94     //qDebug() << "unsupported key:" << key;
       
    95     return 0;
       
    96 }
       
    97 
       
    98 void DSServicePlugin::release(QMediaService *service)
       
    99 {
       
   100     delete service;
       
   101 }
       
   102 
       
   103 QList<QByteArray> DSServicePlugin::devices(const QByteArray &service) const
       
   104 {
       
   105 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
   106     if (service == Q_MEDIASERVICE_CAMERA) {
       
   107         if (m_cameraDevices.isEmpty())
       
   108             updateDevices();
       
   109 
       
   110         return m_cameraDevices;
       
   111     }
       
   112 #endif
       
   113 
       
   114     return QList<QByteArray>();
       
   115 }
       
   116 
       
   117 QString DSServicePlugin::deviceDescription(const QByteArray &service, const QByteArray &device)
       
   118 {
       
   119 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
   120     if (service == Q_MEDIASERVICE_CAMERA) {
       
   121         if (m_cameraDevices.isEmpty())
       
   122             updateDevices();
       
   123 
       
   124         for (int i=0; i<m_cameraDevices.count(); i++)
       
   125             if (m_cameraDevices[i] == device)
       
   126                 return m_cameraDescriptions[i];
       
   127     }
       
   128 #endif
       
   129     return QString();
       
   130 }
       
   131 
       
   132 #ifdef QMEDIA_DIRECTSHOW_CAMERA
       
   133 void DSServicePlugin::updateDevices() const
       
   134 {
       
   135     m_cameraDevices.clear();
       
   136     m_cameraDescriptions.clear();
       
   137 
       
   138     CoInitialize(NULL);
       
   139     ICreateDevEnum* pDevEnum = NULL;
       
   140     IEnumMoniker* pEnum = NULL;
       
   141     // Create the System device enumerator
       
   142     HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
       
   143                  CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
       
   144 		 reinterpret_cast<void**>(&pDevEnum));
       
   145     if(SUCCEEDED(hr)) {
       
   146         // Create the enumerator for the video capture category
       
   147 	hr = pDevEnum->CreateClassEnumerator(
       
   148 	     CLSID_VideoInputDeviceCategory, &pEnum, 0);
       
   149 	pEnum->Reset();
       
   150 	// go through and find all video capture devices
       
   151 	IMoniker* pMoniker = NULL;
       
   152 	while(pEnum->Next(1, &pMoniker, NULL) == S_OK) {
       
   153 	    IPropertyBag *pPropBag;
       
   154 	    hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag,
       
   155 	         (void**)(&pPropBag));
       
   156             if(FAILED(hr)) {
       
   157 	        pMoniker->Release();
       
   158 		continue; // skip this one
       
   159 	    }
       
   160 	    // Find the description
       
   161 	    WCHAR str[120];
       
   162 	    VARIANT varName;
       
   163 	    varName.vt = VT_BSTR;
       
   164             hr = pPropBag->Read(L"FriendlyName", &varName, 0);
       
   165 	    if(SUCCEEDED(hr)) {
       
   166 	        StringCchCopyW(str,sizeof(str)/sizeof(str[0]),varName.bstrVal);
       
   167 		QString temp(QString::fromUtf16((unsigned short*)str));
       
   168 		m_cameraDevices.append(QString("ds:%1").arg(temp).toLocal8Bit().constData());
       
   169 	        hr = pPropBag->Read(L"Description", &varName, 0);
       
   170 	        StringCchCopyW(str,sizeof(str)/sizeof(str[0]),varName.bstrVal);
       
   171 		QString temp2(QString::fromUtf16((unsigned short*)str));
       
   172 		m_cameraDescriptions.append(temp2);
       
   173 	    }
       
   174 	    pPropBag->Release();
       
   175 	    pMoniker->Release();
       
   176 	}
       
   177     }
       
   178     CoUninitialize();
       
   179 }
       
   180 #endif
       
   181 
       
   182 Q_EXPORT_PLUGIN2(dsengine, DSServicePlugin);
       
   183