void testFun()
{
chStringA strDevName;
chStringA strDevID;
chStringA useDevName = "WIN2 USB2.0 PC Camera";
chStringA useDevId = "\\\\?\\usb#vid_0ac8&pid_3420&mi_00#7&24c43346&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global";
chListStringA listName;
listName = wVideoHelper::GetVideoCaptureNameList();
chStringA listFirstName = listName.front();
chListStringA listIDs;
listIDs = wVideoHelper::GetVideoCaptureIDList();
chStringA listFirstID = listIDs.front();
strDevName = wVideoHelper::GetVideoCaptureNameByID(useDevId);
strDevID = wVideoHelper::GetVideoCaptureIDByName(useDevName);
int nCounts = wVideoHelper::GetVideoCaptureDevicesCounts();
chStringA strWinTitle = "yecy hello world";
DisplayCaptureSettingsDialogBox(useDevId, strWinTitle, NULL, 500, 1000);
return -1;
}
#ifndef __wVedioHelper_h__
#define __wVedioHelper_h__
#include "uiHelper.h"
#include <Dshow.h>
#pragma comment(lib, "Strmiids.lib")
//typedef chObjList_stack<chStringA> VideoCaptureDevNameList;
class UIHELPER_EXPORT wVideoHelper
{
public:
/************************vedio input***************/
static int GetVideoCaptureDevicesCounts();
static chStringA GetVideoCaptureIDByName(const chConstString& strDevName);
static chStringA GetVideoCaptureNameByID(const chConstString& strDevID);
static chListStringA GetVideoCaptureNameList();
static chListStringA GetVideoCaptureIDList();
public:
wVideoHelper(){};
~wVideoHelper(){};
};
int UIHELPER_EXPORT DisplayCaptureSettingsDialogBox(const chConstString& deviceUniqueIdUTF8,
const chConstString& dialogTitleUTF8,
void* parentWindow,
int positionX,
int positionY);
#endif
/////////////.h///////////
#include "ETLLib/ETLLib.hpp"
#include "wVedioHelper.h"
#define THIS_MODULE "wVedioHelper"
//#define RELEASE_AND_CLEAR(p) if (p) { (p) -> Release () ; (p) = NULL ; }
//int deviceUniqueIdUTF8Length = 256;
int wVideoHelper::GetVideoCaptureDevicesCounts()
{
int nResultDevCounts = 0;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) || _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched))
{
++nResultDevCounts;
}
if (pM != NULL)
{
pM->Release();
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return nResultDevCounts;
}
chStringA wVideoHelper::GetVideoCaptureIDByName(const chConstString& strDevName)
{// enumerate all video capture devices
chStringA strResultDeviceID;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) && _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL;bool deviceFound = false;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched) && !deviceFound)
{
IPropertyBag *pBag = NULL;
if (S_OK == pM->BindToStorage(0, 0, IID_IPropertyBag, (void **) &pBag))
{
VARIANT varName; VariantInit(&varName);
hr = pBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
chStringA strDeviceName = chW2UTF8(varName.bstrVal);
if (strDevName == strDeviceName)
{
// We have found the requested device
VARIANT varID;
VariantInit(&varID);
pBag->Read(L"DevicePath", &varID, 0);
strResultDeviceID = chW2UTF8(varID.bstrVal);
VariantClear(&varID);
deviceFound = true;
}
}
pBag->Release();
VariantClear(&varName);
pM->Release();
}
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return strResultDeviceID;
}
chStringA wVideoHelper::GetVideoCaptureNameByID(const chConstString& strDevID)
{// enumerate all video capture devices
chStringA strResultDevName;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) || _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL; bool deviceFound = false;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched) && !deviceFound)
{
IPropertyBag *pBag = NULL;
if (pM->BindToStorage(0, 0, IID_IPropertyBag, (void **) &pBag) == S_OK)
{
VARIANT varID; VariantInit(&varID);
hr = pBag->Read(L"DevicePath", &varID, 0);
if (SUCCEEDED(hr))
{
chStringA strDeviceID = chW2UTF8(varID.bstrVal);
if (strDevID == strDeviceID)
{
VARIANT varName;
VariantInit(&varName);
pBag->Read(L"FriendlyName", &varName, 0);
strResultDevName = chW2UTF8(varName.bstrVal);
VariantClear(&varName);
deviceFound = true;
}
}
VariantClear(&varID);
pBag->Release();
pM->Release();
}
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return strResultDevName;
}
chListStringA wVideoHelper::GetVideoCaptureNameList()
{
chListStringA listResultDevName;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) || _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL; bool deviceFound = false;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched) && !deviceFound)
{
IPropertyBag *pBag = NULL;
if (pM->BindToStorage(0, 0, IID_IPropertyBag, (void **) &pBag) == S_OK)
{
VARIANT varName; VariantInit(&varName);
hr = pBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
chStringA strDevName = chW2UTF8(varName.bstrVal);
listResultDevName.push_back(strDevName);
}
VariantClear(&varName);
pBag->Release();
pM->Release();
}
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return listResultDevName;
}
chListStringA wVideoHelper::GetVideoCaptureIDList()
{
chListStringA listResultDevID;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) || _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL; bool deviceFound = false;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched) && !deviceFound)
{
IPropertyBag *pBag = NULL;
if (pM->BindToStorage(0, 0, IID_IPropertyBag, (void **) &pBag) == S_OK)
{
VARIANT varID; VariantInit(&varID);
hr = pBag->Read(L"DevicePath", &varID, 0);
if (SUCCEEDED(hr))
{
chStringA strDevName = chW2UTF8(varID.bstrVal);
listResultDevID.push_back(strDevName);
}
VariantClear(&varID);
pBag->Release();
pM->Release();
}
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return listResultDevID;
}
static IBaseFilter * GetDeviceFilter(chConstString deviceUniqueIdUTF8)
{
IBaseFilter *captureFilter = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICreateDevEnum* _dsDevEnum;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &_dsDevEnum);
IEnumMoniker* _dsMonikerDevEnum = NULL;
HRESULT hr = _dsDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &_dsMonikerDevEnum, 0);
if (SUCCEEDED(hr) || _dsMonikerDevEnum != NULL)
{
_dsMonikerDevEnum->Reset();
ULONG cFetched = 0; IMoniker *pM = NULL; bool deviceFound = false;
while (S_OK == _dsMonikerDevEnum->Next(1, &pM, &cFetched) && !deviceFound)
{
IPropertyBag *pBag;
if (pM->BindToStorage(0, 0, IID_IPropertyBag, (void **) &pBag) == S_OK)
{
VARIANT varName; VariantInit(&varName);
hr = pBag->Read(L"DevicePath", &varName, 0);
if (SUCCEEDED(hr))
{
chStringA strDeviceID = chW2UTF8(varName.bstrVal);
if (deviceUniqueIdUTF8 == strDeviceID)
{
deviceFound = true;
hr = pM->BindToObject(0, 0, IID_IBaseFilter, (void**) &captureFilter);
}
}
VariantClear(&varName);
pBag->Release();
pM->Release();
}
}
_dsMonikerDevEnum->Release();
}
CoUninitialize();
return captureFilter;
}
int DisplayCaptureSettingsDialogBox(const chConstString& deviceUniqueIdUTF8,
const chConstString& dialogTitleUTF8,
void* parentWindow,
int positionX,
int positionY)
{
HWND window = (HWND) parentWindow;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
IBaseFilter* filter = GetDeviceFilter(deviceUniqueIdUTF8);
if (filter != NULL)
{
ISpecifyPropertyPages* pPages = NULL; HRESULT hr = S_OK;
hr = filter->QueryInterface(IID_ISpecifyPropertyPages, (LPVOID*) &pPages);
if (SUCCEEDED(hr))
{
CAUUID uuid;
hr = pPages->GetPages(&uuid);
if (SUCCEEDED(hr))
{
chStringW strDialogTitleW = chUTF82W(dialogTitleUTF8);
// Invoke a dialog box to display.
hr = OleCreatePropertyFrame(window, // You must create the parent window.
positionX, // Horizontal position for the dialog box.
positionY, // Vertical position for the dialog box.
strDialogTitleW.c_str(),// String used for the dialog box caption.
1, // Number of pointers passed in pPlugin.
(LPUNKNOWN*) &filter, // Pointer to the filter.
uuid.cElems, // Number of property pages.
uuid.pElems, // Array of property page CLSIDs.
LOCALE_USER_DEFAULT, // Locale ID for the dialog box.
0, NULL); // Reserved
// Release memory.
if (uuid.pElems)
{
CoTaskMemFree(uuid.pElems);
}
}
}
}
filter->Release();
CoUninitialize();
return 0;
}