qtmobility/plugins/multimedia/directshow/camera/dsvideodevicecontrol.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
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 <QDebug>
       
    43 #include <QFile>
       
    44 #include <QtGui/QIcon>
       
    45 #include "dsvideodevicecontrol.h"
       
    46 #include "dscamerasession.h"
       
    47 
       
    48 #include <tchar.h>
       
    49 #include <dshow.h>
       
    50 #include <objbase.h>
       
    51 #include <initguid.h>
       
    52 
       
    53 DSVideoDeviceControl::DSVideoDeviceControl(QObject *parent)
       
    54     : QVideoDeviceControl(parent)
       
    55 {
       
    56     m_session = qobject_cast<DSCameraSession*>(parent);
       
    57 
       
    58     devices.clear();
       
    59     descriptions.clear();
       
    60 
       
    61     CoInitialize(NULL);
       
    62     ICreateDevEnum* pDevEnum = NULL;
       
    63     IEnumMoniker* pEnum = NULL;
       
    64     // Create the System device enumerator
       
    65     HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
       
    66             CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
       
    67             reinterpret_cast<void**>(&pDevEnum));
       
    68     if(SUCCEEDED(hr)) {
       
    69         // Create the enumerator for the video capture category
       
    70         hr = pDevEnum->CreateClassEnumerator(
       
    71                 CLSID_VideoInputDeviceCategory, &pEnum, 0);
       
    72         pEnum->Reset();
       
    73         // go through and find all video capture devices
       
    74         IMoniker* pMoniker = NULL;
       
    75         while(pEnum->Next(1, &pMoniker, NULL) == S_OK) {
       
    76             IPropertyBag *pPropBag;
       
    77             hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag,
       
    78                     (void**)(&pPropBag));
       
    79             if(FAILED(hr)) {
       
    80                 pMoniker->Release();
       
    81                 continue; // skip this one
       
    82             }
       
    83             // Find the description
       
    84             WCHAR str[120];
       
    85             VARIANT varName;
       
    86             varName.vt = VT_BSTR;
       
    87             hr = pPropBag->Read(L"FriendlyName", &varName, 0);
       
    88             if(SUCCEEDED(hr)) {
       
    89                 StringCchCopyW(str,sizeof(str)/sizeof(str[0]),varName.bstrVal);
       
    90                 QString temp(QString::fromUtf16((unsigned short*)str));
       
    91                 devices.append(QString("ds:%1").arg(temp).toLocal8Bit().constData());
       
    92                 hr = pPropBag->Read(L"Description", &varName, 0);
       
    93                 StringCchCopyW(str,sizeof(str)/sizeof(str[0]),varName.bstrVal);
       
    94                 QString temp2(QString::fromUtf16((unsigned short*)str));
       
    95                 descriptions.append(temp2.toLocal8Bit().constData());
       
    96             }
       
    97             pPropBag->Release();
       
    98             pMoniker->Release();
       
    99         }
       
   100     }
       
   101     CoUninitialize();
       
   102 
       
   103     selected = 0;
       
   104 }
       
   105 
       
   106 int DSVideoDeviceControl::deviceCount() const
       
   107 {
       
   108     return devices.count();
       
   109 }
       
   110 
       
   111 QString DSVideoDeviceControl::deviceName(int index) const
       
   112 {
       
   113     if(index >= 0 && index <= devices.count())
       
   114         return devices.at(index);
       
   115 
       
   116     return QString();
       
   117 }
       
   118 
       
   119 QString DSVideoDeviceControl::deviceDescription(int index) const
       
   120 {
       
   121     if(index >= 0 && index <= descriptions.count())
       
   122         return descriptions.at(index);
       
   123 
       
   124     return QString();
       
   125 }
       
   126 
       
   127 QIcon DSVideoDeviceControl::deviceIcon(int index) const
       
   128 {
       
   129     Q_UNUSED(index)
       
   130 
       
   131     return QIcon();
       
   132 }
       
   133 
       
   134 int DSVideoDeviceControl::defaultDevice() const
       
   135 {
       
   136     return 0;
       
   137 }
       
   138 
       
   139 int DSVideoDeviceControl::selectedDevice() const
       
   140 {
       
   141     return selected;
       
   142 }
       
   143 
       
   144 void DSVideoDeviceControl::setSelectedDevice(int index)
       
   145 {
       
   146     if(index >= 0 && index <= devices.count()) {
       
   147         if (m_session) {
       
   148             QString device = devices.at(index);
       
   149             if (device.startsWith("ds:"))
       
   150                 device.remove(0,3);
       
   151             m_session->setDevice(device);
       
   152         }
       
   153         selected = index;
       
   154     }
       
   155 }
       
   156