修改 \DXSDK\Samples\C++\DirectShow\Editing\StillCap 中的 CStillCapDlg::InitCaptureGraph 函数如下 :HRESULT CStillCapDlg::InitCaptureGraph( TCHAR * pFilename )
{ HRESULT hr; // make a filter graph
//
m_pGraph.CoCreateInstance( CLSID_FilterGraph );
if( !m_pGraph )
{
Error(TEXT("Could not create filter graph"));
return E_FAIL;
} // get whatever capture device exists
//
CComPtr< IBaseFilter > pCap;
GetDefaultCapDevice( &pCap );
if( !pCap )
{
Error( TEXT("No video capture device was detected on your system.\r\n\r\n")
TEXT("This sample requires a functional video capture device, such\r\n")
TEXT("as a USB web camera. Video capture will be disabled.") );
return E_FAIL;
} // add the capture filter to the graph
//
hr = m_pGraph->AddFilter( pCap, L"Cap" );
if( FAILED( hr ) )
{
Error( TEXT("Could not put capture device in graph"));
return hr;
}
// make a capture builder graph (for connecting help)
//
CComPtr< ICaptureGraphBuilder2 > pBuilder;
hr = pBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder2 );
if( !pBuilder )
{
Error( TEXT("Could not create capture graph builder2"));
return hr;
} hr = pBuilder->SetFiltergraph( m_pGraph );
if( FAILED( hr ) )
{
Error( TEXT("Could not set filtergraph on capture graph builder2"));
return hr;
} CComPtr< IBaseFilter > pMux;
CComPtr< IFileSinkFilter > pSink;
USES_CONVERSION; // The floging will be changed to asf , save as a asf file
hr = pBuilder->SetOutputFileName( &MEDIASUBTYPE_Asf, T2W( pFilename ),
&pMux, &pSink );
if( FAILED( hr ) )
{
Error( TEXT("Could not create/hookup mux and writer"));
return hr;
}
// The flowing for configuring pConfigAsfWriter
//
IConfigAsfWriter *pConfigAsfWriter = 0 ;
hr = pMux->QueryInterface(IID_IConfigAsfWriter, (void **)&pConfigAsfWriter) ;
if (FAILED(hr))
{
return hr ;
} // Convert the numerical profile index into a IWMProfile interface pointer
CComPtr<IWMProfile> pProfile;
hr = MapProfileIdToProfile(3, &pProfile); // 见下
if(FAILED(hr))
{
return hr ;
} hr = pConfigAsfWriter->ConfigureFilterUsingProfile(pProfile);
if(FAILED(hr))
{
return hr ;
}
hr = pConfigAsfWriter->SetIndexMode(FALSE);
if(FAILED(hr))
{
return false ;
} pConfigAsfWriter->Release() ;
//
// end configure
hr = pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE , &MEDIATYPE_Video , pCap , 0 , pMux);
if( FAILED( hr ) )
{
Error( TEXT("Could not connect capture pin Video"));
return hr;
} // for preview when capturing
hr = pBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
pCap, NULL, NULL );
if( FAILED( hr ) )
{
Error( TEXT("Could not render capture pin"));
return hr;
}
if( hr == VFW_S_NOPREVIEWPIN )
{
// preview was faked up using the capture pin, so we can't
// turn capture on and off at will.
hr = 0;
} // find the video window and stuff it in our window
//
CComQIPtr< IVideoWindow, &IID_IVideoWindow > pWindow = m_pGraph;
if( !pWindow )
{
Error( TEXT("Could not get video window interface"));
return hr;
}
// set up the preview window to be in our dialog
// instead of floating popup
//
HWND hwndPreview = NULL;
GetDlgItem( IDC_PREVIEW, &hwndPreview );
RECT rc;
::GetWindowRect( hwndPreview, &rc ); hr = pWindow->put_Owner( (OAHWND) hwndPreview );
hr = pWindow->put_Left( 0 );
hr = pWindow->put_Top( 0 );
hr = pWindow->put_Width( rc.right - rc.left );
hr = pWindow->put_Height( rc.bottom - rc.top );
hr = pWindow->put_Visible( OATRUE );
hr = pWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS ); // run the graph
// wrong ! 这一步总会出错
CComQIPtr< IMediaControl, &IID_IMediaControl > pControl = m_pGraph;
hr = pControl->Run( );
if( FAILED( hr ) )
{
OAFilterState p;
pControl->GetState(1000,&p);
Error( TEXT("Could not run graph"));
return hr;
} UpdateStatus(_T("Capturing Video To Disk"));
return 0;
}HRESULT MapProfileIdToProfile(int iProfile, IWMProfile **ppProfile)
{
DWORD cProfiles; if (!ppProfile)
return E_POINTER;
*ppProfile = 0;
CComPtr <IWMProfileManager> pIWMProfileManager; HRESULT hr = WMCreateProfileManager( &pIWMProfileManager );
if(FAILED(hr))
{
return hr;
} CComQIPtr<IWMProfileManager2, &IID_IWMProfileManager2> pIPM2(pIWMProfileManager); if(!pIPM2)
{
return E_UNEXPECTED;
} // Set the system profile version (4, 7, 8)
hr = pIPM2->SetSystemProfileVersion( g_WMTVersion );
if(FAILED(hr))
{
return hr;
} // Read back the current version to verify
//hr = pIPM2->GetSystemProfileVersion( &g_WMTVersion );
if(FAILED(hr))
{
return hr;
} // How many profiles exist for this version?
hr = pIWMProfileManager->GetSystemProfileCount( &cProfiles );
if(FAILED(hr))
{
return hr;
} // Invalid profile requested?
if( (DWORD)iProfile >= cProfiles )
{
return E_INVALIDARG;
} // Load the requested systme profile and return its interface
// in the ppProfile parameter
return (pIWMProfileManager->LoadSystemProfile( iProfile, ppProfile ));}graph 不能 run , 是不是 Asf Writer filter 的属性设置不对或不全 ? 谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货