HRESULT CCaptureVideo::Open(int iDeviceID,int iPress) { HRESULT hr; hr = InitCaptureGraphBuilder(); if (FAILED(hr)){ return hr; } // Bind Device Filter. We know the device because the id was passed in if(!BindVideoFilter(iDeviceID, &m_pBF)) return S_FALSE; hr = m_pGB->AddFilter(m_pBF, L"Capture Filter"); // create a sample grabber hr = CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_ISampleGrabber, (void**)&m_pGrabber ); if(FAILED(hr)){ return hr; } CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber );//设置视频格式 AM_MEDIA_TYPE mt; ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE)); mt.majortype = MEDIATYPE_Video; mt.subtype = MEDIASUBTYPE_RGB24; // MEDIASUBTYPE_RGB24 ; hr = m_pGrabber->SetMediaType(&mt); if( FAILED( hr ) ){ return hr; } hr = m_pGB->AddFilter( pGrabBase, L"Grabber" ); if( FAILED( hr ) ){ return hr; }// try to render preview/capture pin hr = m_pCapture->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,m_pBF,pGrabBase,NULL); if( FAILED( hr ) ) hr = m_pCapture->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,m_pBF,pGrabBase,NULL); if( FAILED( hr ) ){ return hr; } hr = m_pGrabber->GetConnectedMediaType( &mt ); if ( FAILED( hr) ){ return hr; }VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat; mCB.lWidth = vih->bmiHeader.biWidth; mCB.lHeight = vih->bmiHeader.biHeight; mCB.bGrabVideo = FALSE ; //mCB.frame_handler = NULL ; FreeMediaType(mt); hr = m_pGrabber->SetBufferSamples( FALSE ); hr = m_pGrabber->SetOneShot( FALSE ); //设置视频捕获回调函数 也就是如果有视频数据时就会调用这个类的BufferCB函数 //返回OnTimer hr = m_pGrabber->SetCallback( &mCB, 1 ); //设置视频捕捉窗口 m_hWnd = CreateWindow("#32770", /* Dialog */ "", WS_POPUP, 0, 0, 0, 0, NULL, NULL, NULL, NULL); SetupVideoWindow(); hr = m_pMC->Run();//开始视频捕捉 if(FAILED(hr)){ //AfxMessageBox("Couldn’t run the graph!");return hr; } return S_OK; }
BOOL CDXVideoGrabber::PlayFile(const char* inFile, HWND hWnd) { RemoveAllFilters(); HRESULT hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber ); if( !m_pGrabber ) return FALSE; CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber ); // force it to connect to video, 24 bit // CMediaType VideoType; VideoType.SetType( &MEDIATYPE_Video ); VideoType.SetSubtype( &MEDIASUBTYPE_RGB24 ); hr = m_pGrabber->SetMediaType( &VideoType ); // shouldn't fail if( FAILED( hr ) ) return FALSE; // add the grabber to the graph // hr = m_pIGraphBuilder->AddFilter( pGrabBase, L"Grabber" ); if( FAILED( hr ) ) return FALSE; RenderFile(inFile); AM_MEDIA_TYPE mt; hr = m_pGrabber->GetConnectedMediaType( &mt ); if ( FAILED( hr) ) { return FALSE; } VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat; if(m_pSGCB) { // m_pSGCB->SetOwner(GetParent(hWnd)); m_pSGCB->SetWidth(vih->bmiHeader.biWidth); m_pSGCB->SetHeight(vih->bmiHeader.biHeight); } FreeMediaType( mt ); // don't buffer the samples as they pass through // hr = m_pGrabber->SetBufferSamples( FALSE ); // only grab one at a time, stop stream after // grabbing one sample // hr = m_pGrabber->SetOneShot( FALSE ); // set the callback, so we can grab the one sample // hr = m_pGrabber->SetCallback( m_pSGCB, 1 ); if(!SetDisplayWindow(hWnd)) { return FALSE; } return Run(); }
BOOL CDXCamara::PlayFile(const char* inFile, HWND hWnd) { HRESULT hr; RemoveAllFilters(); // get whatever capture device exists // CComPtr< IBaseFilter > pCap; GetDefaultCapDevice( &pCap ); if( !pCap ) return FALSE; // add the capture filter to the graph // hr = m_pIGraphBuilder->AddFilter( pCap, L"Cap" ); if( FAILED( hr ) ) return FALSE; hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber ); if( !m_pGrabber ) return FALSE; CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber ); // force it to connect to video, 24 bit // CMediaType VideoType; VideoType.SetType( &MEDIATYPE_Video ); VideoType.SetSubtype( &MEDIASUBTYPE_RGB24 ); hr = m_pGrabber->SetMediaType( &VideoType ); // shouldn't fail if( FAILED( hr ) ) return FALSE; // add the grabber to the graph // hr = m_pIGraphBuilder->AddFilter( pGrabBase, L"Grabber" ); if( FAILED( hr ) ) return FALSE; // make a capture builder graph (for connecting help) // CComPtr< ICaptureGraphBuilder2 > pBuilder; hr = pBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder2 ); if( !pBuilder ) return FALSE; hr = pBuilder->SetFiltergraph( m_pIGraphBuilder ); if( FAILED( hr ) ) return FALSE; // If there is a VP pin present on the video device, then put the // renderer on CLSID_NullRenderer CComPtr<IPin> pVPPin; hr = pBuilder->FindPin( pCap, PINDIR_OUTPUT, &PIN_CATEGORY_VIDEOPORT, NULL, FALSE, 0, &pVPPin); // If there is a VP pin, put the renderer on NULL Renderer CComPtr<IBaseFilter> pRenderer; if (S_OK == hr) { hr = pRenderer.CoCreateInstance(CLSID_NullRenderer); if (FAILED (hr)) { return FALSE; } hr = m_pIGraphBuilder->AddFilter(pRenderer, L"NULL renderer"); if (FAILED (hr)) { return FALSE; } } hr = pBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Interleaved, pCap, pGrabBase, pRenderer); if (FAILED (hr)) { // try to render preview pin hr = pBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, pGrabBase, pRenderer); // try to render capture pin if( FAILED( hr ) ) { hr = pBuilder->RenderStream( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCap, pGrabBase, pRenderer); } } if( FAILED( hr ) ) { return FALSE; } // ask for the connection media type so we know how big // it is, so we can write out bitmaps // AM_MEDIA_TYPE mt; hr = m_pGrabber->GetConnectedMediaType( &mt ); if ( FAILED( hr) ) { return FALSE; } VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat; if(m_pSGCB) { // m_pSGCB->SetOwner(GetParent(hWnd)); m_pSGCB->SetWidth(vih->bmiHeader.biWidth); m_pSGCB->SetHeight(vih->bmiHeader.biHeight); } FreeMediaType( mt ); // don't buffer the samples as they pass through // hr = m_pGrabber->SetBufferSamples( FALSE ); // only grab one at a time, stop stream after // grabbing one sample // hr = m_pGrabber->SetOneShot( FALSE ); // set the callback, so we can grab the one sample // hr = m_pGrabber->SetCallback( m_pSGCB, 1 ); // CComPtr< IBaseFilter > pMux; // CComPtr< IFileSinkFilter > pSink; // USES_CONVERSION; // // hr = pBuilder->SetOutputFileName( &MEDIASUBTYPE_Avi, T2W( inFile ), // &pMux, &pSink ); // if( FAILED( hr ) ) // return FALSE; // // hr = pBuilder->RenderStream( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, // pCap, NULL,pMux ); // if( FAILED( hr ) ) // return FALSE; // hr = pBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, // pCap, NULL, NULL ); // if( FAILED( hr ) ) // return FALSE; if(!SetDisplayWindow(hWnd)) { return FALSE; } return Run(); }
HRESULT CCaptureVideo::Init(int iDeviceID, HWND hWnd) { HRESULT hr; hr = InitCaptureGraphBuilder(); if (FAILED(hr)) { IBA_LOG0(_T("Failed to get video interfaces!")); return hr; } // Bind Device Filter. We know the device because the id was passed in if(!BindFilter(iDeviceID, &m_pBF)) return S_FALSE; hr = m_pGB->AddFilter(m_pBF, L"Capture Filter"); m_pGrabber = NULL; hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber ); if( !m_pGrabber ) { IBA_LOG0(_T("Fail to create SampleGrabber, maybe qedit.dll is not registered?")); return hr; } CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber ); //设置视频格式 AM_MEDIA_TYPE mt; ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE)); mt.majortype = MEDIATYPE_Video; mt.subtype = MEDIASUBTYPE_RGB24; hr = m_pGrabber->SetMediaType(&mt); if( FAILED( hr ) ) { IBA_LOG0(_T("Fail to set media type!")); return hr; } hr = m_pGB->AddFilter( pGrabBase, L"Grabber" ); if( FAILED( hr ) ) { IBA_LOG0(_T("Fail to put sample grabber in graph")); return hr; } // try to render preview/capture pin hr = m_pCapture->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,m_pBF,pGrabBase,NULL); if( FAILED( hr ) ) hr = m_pCapture->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,m_pBF,pGrabBase,NULL); if( FAILED( hr ) ) { // OutputEx(ToString("Can’t build the graph")); return hr; } hr = m_pGrabber->GetConnectedMediaType( &mt ); if ( FAILED( hr) ) { IBA_LOG0(_T("Failt to read the connected media type")); return hr; } VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat; m_CB.m_lWidth = vih->bmiHeader.biWidth; m_CB.m_lHeight = vih->bmiHeader.biHeight; FreeMediaType(mt); hr = m_pGrabber->SetBufferSamples( FALSE ); hr = m_pGrabber->SetOneShot( FALSE ); hr = m_pGrabber->SetCallback( &m_CB, 1 ); //设置视频捕捉窗口 m_hWnd = hWnd ; SetupVideoWindow(); hr = m_pMC->Run();//开始视频捕捉 if(FAILED(hr)) { IBA_LOG0(_T("Couldn’t run the graph!")); return hr; } m_bOpened = true; return S_OK; }