当前位置: 代码迷 >> 多媒体/流媒体开发 >> directshow视频捕捉有关问题
  详细解决方案

directshow视频捕捉有关问题

热度:3208   发布时间:2013-02-26 00:00:00.0
directshow视频捕捉问题
各位高手们帮忙看看,两段代码和起来后我想能够捕获视频时还能预览,但运行后事与愿违,只能预览不能捕获,这是为什么?怎么修改?小弟我在这跪求大家。
//预览代码
pPreview = NULL;
CoCreateInstance(CLSID_VideoRenderer,0,CLSCTX_ALL,
IID_IBaseFilter,(void**)&pPreview);
if (pPreview != NULL)
{
pGraph->AddFilter(pPreview,L"preview");
//连接引脚
IPin * pSourceOut;
pSourceOut = FindPin(pSrc,PINDIR_OUTPUT);
IPin* pPreIn = FindPin(pPreview,PINDIR_INPUT);
pGraph->ConnectDirect(pSourceOut,pPreIn,NULL);
//获取预览窗口
IVideoWindow *pViewWnd= NULL;
pPreview->QueryInterface(IID_IVideoWindow,(void**)&pViewWnd);
if (pViewWnd)
{
//设置预览窗口的拥有者 
pViewWnd->put_Owner((long)m_hWndVideo);
pViewWnd->put_Left(1);
pViewWnd->put_Top(1);
//获取预览窗口风格
long style;
pViewWnd->get_WindowStyle(&style);
style = style & ~WS_CAPTION;
style = style & ~WS_DLGFRAME;
style = style & WS_CHILD;
pViewWnd->put_WindowStyle(style);
//设置预览窗口宽度和高度
pViewWnd->put_Height(Height);
  pViewWnd->put_Width(Width);
}
}
//视频捕捉代码
pGraph->AddFilter(pCompress,L"com");
  CoCreateInstance(CLSID_AviDest,NULL, CLSCTX_ALL, 
  IID_IBaseFilter,(void**)&pMux);
  pGraph->AddFilter(pMux,L"Mux");
  CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_ALL, 
  IID_IBaseFilter, (void **)&pWriter);
  pGraph->AddFilter(pWriter,L"Writer");
  pWriter->QueryInterface(IID_IFileSinkFilter2,(void**)&pSink);
  pSink->SetFileName(L"C:\\Example.avi",NULL);
  //压缩器输入\输出引脚
  IAMVideoCompression * pAMCompress ;
  IPin * pComOut,*pComIn ;
  pComIn = FindPin(pCompress,PINDIR_INPUT);
  pComOut = FindPin(pCompress,PINDIR_OUTPUT);
  pComOut->QueryInterface(IID_IAMVideoCompression,(void**)&pAMCompress);
  HRESULT hret;
  hret = pAMCompress->put_KeyFrameRate(8);
  IPin* pOutpin = FindPin(pSrc,PINDIR_OUTPUT); //pSrc的输出引脚
  IPin* pInpin,*pOut; //pMux的输入\输出引脚
  pInpin = FindPin(pMux,PINDIR_INPUT);
  pOut= FindPin(pMux,PINDIR_OUTPUT);
  IPin* pInpin1= FindPin(pWriter,PINDIR_INPUT);//pWriter的输入引脚  
  //连接引脚
  HRESULT result ;
  result = pGraph->ConnectDirect(pOutpin,pComIn,NULL);
  result = pGraph->ConnectDirect((IPin *)pComOut,pInpin,NULL);
  result = pGraph->ConnectDirect(pOut,pInpin1,NULL);
  pMediaControl->Run();
  pAMCompress->Release();

------解决方案--------------------------------------------------------
查找Pin的方式有问题,你需要去找一个未连接Pin.
HRESULT GetUnconnectedPin(
IBaseFilter *pFilter, // Pointer to the filter.
PIN_DIRECTION PinDir, // Direction of the pin to find.
IPin **ppPin) // Receives a pointer to the pin.
{
*ppPin = 0;
IEnumPins *pEnum = 0;
IPin *pPin = 0;
HRESULT hr = pFilter->EnumPins(&pEnum);
if (FAILED(hr))
{
return hr;
}
while (pEnum->Next(1, &pPin, NULL) == S_OK)
{
PIN_DIRECTION ThisPinDir;
pPin->QueryDirection(&ThisPinDir);
if (ThisPinDir == PinDir)
{
IPin *pTmp = 0;
hr = pPin->ConnectedTo(&pTmp);
if (SUCCEEDED(hr)) // Already connected, not the pin we want.
{
pTmp->Release();
}
else // Unconnected, this is the pin we want.
{
pEnum->Release();
  相关解决方案