void CXunLeiPlayDlg::OnCapstart()//开始录制
{
CString str;
CFileDialog FileDlg(FALSE,_T("AVI"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("视频文件|*.avi|"),this);
if (FileDlg.DoModal()==IDOK)
{
str = FileDlg.GetPathName();
}
if (str.IsEmpty())
{
MessageBox(_T("请选择或输入文件"));
return;
}
ICaptureGraphBuilder2 * pBuilder = NULL;
pGraph = NULL;
pMediaControl = NULL;
IBaseFilter *pMux =NULL;
IBaseFilter *pWriter=NULL;
IBaseFilter *pCompress=NULL;
IFileSinkFilter2 *pSink;
//枚举视频设备
ICreateDevEnum *pDevEnum = NULL;
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void **)&pDevEnum);
IEnumMoniker *pClassEnum = NULL;
pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
ULONG cFetched;
if (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
pMoniker->Release();
}
pClassEnum->Release();
//列举视频压缩设备
pDevEnum->CreateClassEnumerator(CLSID_VideoCompressorCategory, &pClassEnum, 0);
while (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag* pProp= NULL;
pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pProp);
VARIANT varName;
varName.vt = VT_BSTR;
pProp->Read(L"FriendlyName", &varName,0);
CString str = varName.bstrVal;
if (str.Find(_T("Microsoft Video 1"),0)!= -1)
{
pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pCompress);
pMoniker->Release();
break;
}
VariantClear(&varName);
}
pClassEnum->Release();
if (pCompress==NULL)
{
MessageBox(_T("没有发现Microsoft Video 1压缩器!"),_T("提示"),MB_ICONASTERISK);
return;
}
CoCreateInstance(CLSID_CaptureGraphBuilder2,0,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void**)&pBuilder);
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
pBuilder->SetFiltergraph(pGraph);
pGraph->QueryInterface(IID_IMediaControl,(void**)&pMediaControl);
pGraph->AddFilter(pSrc,L"avi");
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(str.AllocSysString(),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();
m_IsRecorded = TRUE;
}