HRESULT CVideoSource::AddVedioPreviewFilter(char* varDeviceName)
{
HRESULT hr = NULL;
IMoniker *pMoniker = NULL;
IPropertyBag *pPropBag = NULL;
BOOL bFind = false;
VARIANT varDescriptName;
VARIANT varFriendName;
VariantInit(&varDescriptName);
VariantInit(&varFriendName);
// Create the System Device Enumerator.
hr = CoCreateInstance( CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
reinterpret_cast<void**>(&m_pDevEnum)
);
if (FAILED(hr))
{
return hr;
}
// Create an enumerator for the video capture category.
hr = m_pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&m_pEnumMoniker, 0);
//if (!SUCCEEDED(hr)||pEnum)
if (hr == S_FALSE)
{
//there is not video device
return hr;
}
while (m_pEnumMoniker->Next(1, &pMoniker, NULL) == S_OK)
{
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag));
if (FAILED(hr))
{
pMoniker->Release();
continue; // Skip this one, maybe the next one will work.
}
// Find the description or friendly name.
hr = pPropBag->Read(L"Description", &varDescriptName, 0);
if (FAILED(hr))
{
}
hr = pPropBag->Read(L"FriendlyName", &varFriendName, 0);
CString str(varFriendName.bstrVal);
int iret = strcmp(varDeviceName,str);
if (iret==0)
{
bFind = true;
break;
}
VariantClear(&varDescriptName);
VariantClear(&varFriendName);
pPropBag->Release();
pMoniker->Release();
}
if (!bFind)
{
ErrMsg(TEXT("没有定位到视频采集设备"));
return NULL;
}
else
{
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&m_pVideoCap);
if (SUCCEEDED(hr))
{
hr = m_pGrapher->AddFilter(m_pVideoCap, varFriendName.bstrVal);
}
m_DeviceDescriptName = varDescriptName;
m_DeviceFriendName = varFriendName;
pPropBag->Release();
pMoniker->Release();
VariantClear(&varDescriptName);
VariantClear(&varFriendName);
//
//hr = m_pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Interleaved, m_pVideoCap,IID_IAMVideoCompression, (void **)&m_pAMVideoCompress);
//if(hr != S_OK)
//{
// hr = m_pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video, m_pVideoCap,IID_IAMVideoCompression, (void **)&m_pAMVideoCompress);
//}
// !!! What if this interface isn't supported?
// we use this interface to set the frame rate and get the capture size
hr = m_pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Interleaved,m_pVideoCap, IID_IAMStreamConfig, (void **)&m_pAMStreamConfig);
if(hr != NOERROR)
{
hr = m_pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video, m_pVideoCap,IID_IAMStreamConfig, (void **)&m_pAMStreamConfig);
if(hr != NOERROR)
{
// this means we can't set frame rate (non-DV only)
ErrMsg(TEXT("Error %x: Cannot find VCapture:IAMStreamConfig"), hr);
}
}
}
//添加Smart tee到Filter graph后,连接到videocapter
hr = m_pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,m_pVideoCap, NULL,m_pSmartTee);
//hr = m_pBuilder->RenderStream(NULL, &MEDIATYPE_Interleaved, m_pVideoCap, NULL,m_pSmartTee);
//hr = m_pBuilder->RenderStream(NULL, NULL, m_pVideoCap, NULL,m_pSmartTee);
return hr;
}