struct StuResolution
{
int width;
int height;
};
QVector<StuResolution> ResolutionList;
//cpp
//摄像头
QString MyDevice::getVideoDeviceName()
{
char sName[256] = { 0 };
QString capture = "";
bool bRet = false;
::CoInitialize(NULL);
ICreateDevEnum* pCreateDevEnum;//enumrate all video capture devices
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
(void**)&pCreateDevEnum);
IEnumMoniker* pEm;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0);
if (hr != NOERROR)
{
::CoUninitialize();
return "";
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
while (hr = pEm->Next(1, &pM, &cFetched), hr == S_OK)
{
IPropertyBag* pBag = NULL;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pBag);
if (SUCCEEDED(hr))
{
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);//还有其他属性,像描述信息等等
if (hr == NOERROR)
{
//获取设备名称
WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, sName, 256, "", NULL);
capture = QString::fromLocal8Bit(sName);
bRet = true;
GetResolution(pM);
SysFreeString(var.bstrVal);
}
pBag->Release();
}
pM->Release();
}
pCreateDevEnum = NULL;
pEm = NULL;
::CoUninitialize();
return capture;
}
//分辨率
void MyDevice::GetResolution(IMoniker *pMoniker)
{
USES_CONVERSION;
HRESULT hr = NULL;
IBaseFilter *pFilter;
hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)&pFilter);
if (!pFilter)
{
return;
}
IEnumPins * pinEnum = NULL;
IPin * pin = NULL;
if (FAILED(pFilter->EnumPins(&pinEnum)))
{
pinEnum->Release();
return;
}
pinEnum->Reset();
ULONG pinFetched = 0;
while (SUCCEEDED(pinEnum->Next(1, &pin, &pinFetched)) && pinFetched)
{
if (!pin)
{
continue;
}
PIN_INFO pinInfo;
if (FAILED(pin->QueryPinInfo(&pinInfo)))
{
continue;
}
if (pinInfo.dir != PINDIR_OUTPUT)
{
continue;
}
IEnumMediaTypes *mtEnum = NULL;
AM_MEDIA_TYPE *mt = NULL;
if (FAILED(pin->EnumMediaTypes(&mtEnum)))
{
break;
}
mtEnum->Reset();
ULONG mtFetched = 0;
while (SUCCEEDED(mtEnum->Next(1, &mt, &mtFetched)) && mtFetched)
{
BITMAPINFOHEADER* bmi = NULL;
int avgTime;
if (mt->formattype == FORMAT_VideoInfo)
{
if (mt->cbFormat >= sizeof(VIDEOINFOHEADER))
{
VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(mt->pbFormat);
bmi = &(pVih->bmiHeader);
avgTime = pVih->AvgTimePerFrame;
}
}
else if (mt->formattype == FORMAT_VideoInfo2)
{
if (mt->cbFormat >= sizeof(VIDEOINFOHEADER2))
{
VIDEOINFOHEADER2* pVih = reinterpret_cast<VIDEOINFOHEADER2*>(mt->pbFormat);
bmi = &(pVih->bmiHeader);
avgTime = pVih->AvgTimePerFrame;
}
}
if (bmi)
{
int echo = false;
for (int i = 0; i < ResolutionList.size(); i++)
{
if (ResolutionList[i].width == bmi->biWidth && ResolutionList[i].height == bmi->biHeight)
{
echo = true;
break;
}
}
if (!echo)
{
StuResolution stuResolution;
stuResolution.width = bmi->biWidth;
stuResolution.height = bmi->biHeight;
ResolutionList.append(stuResolution);
qDebug() << "llt cam Resolution:" << bmi->biWidth << "x" << bmi->biHeight;
}
}
}
pin->Release();
}
return;
}
//麦克风
QString MyDirectShow::getAudioDeviceName()
{
char sName[256] = { 0 };
QString capture = "";
bool bRet = false;
::CoInitialize(NULL);
ICreateDevEnum* pCreateDevEnum;//enumrate all audio capture devices
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
(void**)&pCreateDevEnum);
IEnumMoniker* pEm;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pEm, 0);
if (hr != NOERROR)
{
::CoUninitialize();
return "";
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
while (hr = pEm->Next(1, &pM, &cFetched), hr == S_OK)
{
IPropertyBag* pBag = NULL;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pBag);
if (SUCCEEDED(hr))
{
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);//还有其他属性,像描述信息等等
if (hr == NOERROR)
{
//获取设备名称
WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, sName, 256, "", NULL);
capture = QString::fromLocal8Bit(sName);
SysFreeString(var.bstrVal);
}
pBag->Release();
}
pM->Release();
bRet = true;
}
pCreateDevEnum = NULL;
pEm = NULL;
::CoUninitialize();
return capture;
}
//扬声器
QString MyDevice::getSpeakerDeviceName()
{
char sName[256] = { 0 };
QString speaker = "";
bool bRet = false;
::CoInitialize(NULL);
ICreateDevEnum* pCreateDevEnum;//enumrate all speaker devices
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
(void**)&pCreateDevEnum);
IEnumMoniker* pEm;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_AudioRendererCategory, &pEm, 0);
if (hr != NOERROR)
{
::CoUninitialize();
return "";
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
while (hr = pEm->Next(1, &pM, &cFetched), hr == S_OK)
{
IPropertyBag* pBag = NULL;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pBag);
if (SUCCEEDED(hr))
{
VARIANT var;
var.vt = VT_BSTR;
hr = pBag->Read(L"FriendlyName", &var, NULL);//还有其他属性,像描述信息等等
if (hr == NOERROR)
{
//获取设备名称
WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, sName, 256, "", NULL);
speaker = QString::fromLocal8Bit(sName);
SysFreeString(var.bstrVal);
}
pBag->Release();
}
pM->Release();
bRet = true;
}
pCreateDevEnum = NULL;
pEm = NULL;
::CoUninitialize();
return speaker;
}