给你2段我的代码吧,pGB,pCGB,
ConnectDevice(bool bOverlay, HWND hParent)//bOverlay是否用overlay播放
//hParent父窗口的句柄
{
// Create the Capture Graph Builder.
HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICaptureGraphBuilder2,
(void**)&pCGB);
if (SUCCEEDED(hr))
{
// Create the Filter Graph Manager.
hr = CoCreateInstance(CLSID_FilterGraph,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void **)&pGB);
if (SUCCEEDED(hr))
{
// Initialize the Capture Graph Builder.
pCGB->SetFiltergraph(pGB);
}
else
{
pGB ->Release();
pCGB->Release();
return hr;
}
}
else
{
return hr;
// Failed
}
IMediaControl *pControl;
hr = pGB->QueryInterface(IID_IMediaControl, (void **)&pControl);
pControl->Stop();
if(!SUCCEEDED(hr))
{
return hr;
}
ICreateDevEnum *pSysDevEnum = NULL;
//System Device Enumerator
// Create the System Device Enumerator.
hr = CoCreateInstance(CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
reinterpret_cast<void**>(&pSysDevEnum));
IEnumMoniker *pMonikerEnum = NULL;
//Moniker Enumerator
if (SUCCEEDED(hr))
{
// Create an enumerator for the video capture category.
hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&pMonikerEnum, 0);
}
else
{
return hr;
}
IMoniker *pMoniker = NULL;
//The Current Device
pMonikerEnum->Next(1, &pMoniker, NULL);//第一个捕捉设备
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSourceFilter);
if (SUCCEEDED(hr))
{
hr = pGB->AddFilter(pSourceFilter, L"Capture Filter");
}
//if use overlay
if (bOverlay==true)
{
IBaseFilter *pOverlay = NULL;
CoCreateInstance(CLSID_OverlayMixer,
NULL,
CLSCTX_INPROC,
IID_IBaseFilter,
(LPVOID*)&pOverlay);
pGB->AddFilter(pOverlay, L"Overaly Mixer");
hr = pCGB->RenderStream(&PIN_CATEGORY_PREVIEW,
&MEDIATYPE_Video,
pSourceFilter,
pOverlay'
NULL);
pOverlay->Release();
}
else
{
hr = pCGB->RenderStream(&PIN_CATEGORY_PREVIEW,
&MEDIATYPE_Video,
pSourceFilter,
NULL,
NULL);
}
//Set Output Format
IAMStreamConfig *pConfig = NULL;
pCGB->FindInterface(&PIN_CATEGORY_PREVIEW, // Preview pin.
0, // Any media type.
pSourceFilter, // Pointer to the capture filter.
IID_IAMStreamConfig,
(void**)&pConfig);
AM_MEDIA_TYPE *pmtConfig;
pConfig->GetFormat(&pmtConfig);
if(bOverlay==true)
{
pmtConfig->subtype = MEDIASUBTYPE_YUY2;
}
else
{
pmtConfig->subtype = MEDIASUBTYPE_RGB24;
}
pConfig->SetFormat(pmtConfig);
DeleteMediaType(pmtConfig);
//Set videowindow rect
RECT rc;
GetClientRect(hParent, &rc);
SetVideoWindow(0, hParent, rc);//自定义的
pControl->Run();
pMoniker->Release();
pMonikerEnum->Release();
pSysDevEnum->Release();
pControl->Release();
return hr;
}
SetVideoWindow(HWND hParent, RECT rc)
{ //Set videowindow owner
IMediaControl *pControl;
HRESULT hr = pGB->QueryInterface(IID_IMediaControl, (void **)&pControl);
pControl->Stop();
IVideoWindow *pVideoWindow = NULL;
hr = pGB->QueryInterface(IID_IVideoWindow, (void **)&pVideoWindow);
pVideoWindow->put_Visible(OAFALSE);
pVideoWindow->put_Owner(NULL);
pVideoWindow->put_Owner((OAHWND)hParent);
pVideoWindow->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
pVideoWindow->SetWindowPosition(0, 0, rc.right, rc.bottom);
pVideoWindow->put_Visible(OATRUE);
//Set Output Format
IAMStreamConfig *pConfig = NULL;
pCGB->FindInterface(
&PIN_CATEGORY_PREVIEW, // Preview pin.
0, // Any media type.
pSourceFilter, // Pointer to the capture filter.
IID_IAMStreamConfig, (void**)&pConfig);
AM_MEDIA_TYPE *pmtConfig;
pConfig->GetFormat(&pmtConfig);
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
pVih->bmiHeader.biWidth = rc.right;//分辨率的宽
pVih->bmiHeader.biHeight = rc.bottom;//分辨率的高
pVih->bmiHeader.biSizeImage = DIBSIZE(pVih->bmiHeader);
pConfig->SetFormat(pmtConfig);
MyDeleteMediaType(pmtConfig);
pControl->Run();
pConfig->Release();
pControl->Release();
pVideoWindow->Release();
return hr;
}
我试过了,没问题的,将按照你设置的大小播放视频,就是那个RECT rc参数