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; }
void CCaptureVideo::Init() { m_hWnd = CreateWindow("#32770", /* Dialog */ "", WS_POPUP, 0, 0, 0, 0, NULL, NULL, NULL, NULL); HRESULT hr; hr = InitCaptureGraphBuilder(); if (FAILED(hr)) { //AfxMessageBox("Failed to get video interfaces!"); return ; } SetupVideoWindow(); }
HRESULT CAccessSys::StartCapture(void) { HRESULT hr; IBaseFilter *pSrcFilter = NULL; IFilterGraph*pGraph = NULL; if (e_psCurrent == Running){ StopPreview(); } hr = BuildCapture(); if (FAILED(hr)) { Msg(TEXT("Couldn't BuildCapture ! hr=0x%x"), hr); return hr; } // Start previewing video & audio data if (h_wnd){ hr = SetupVideoWindow(h_wnd); if (FAILED(hr)) { Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr); return hr; } } hr = p_control->Run(); if (FAILED(hr)) { Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr); return hr; } // Remember current state e_psCurrent = Running; //m_remote = new CRemoteSys(p_loop); //Msg(TEXT("StartCapture success")); return S_OK; }
HRESULT CaptureVideo() { HRESULT hr; IBaseFilter *pSrcFilter=NULL; // Get DirectShow interfaces hr = GetInterfaces(); if (FAILED(hr)) { Msg(TEXT("Failed to get video interfaces! hr=0x%x"), hr); return hr; } // Attach the filter graph to the capture graph hr = g_pCapture->SetFiltergraph(g_pGraph); if (FAILED(hr)) { Msg(TEXT("Failed to set capture filter graph! hr=0x%x"), hr); return hr; } // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. hr = FindCaptureDevice(&pSrcFilter); if (FAILED(hr)) { // Don't display a message because FindCaptureDevice will handle it return hr; } // Add Capture filter to our graph. hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture"); if (FAILED(hr)) { Msg(TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n") TEXT("If you have a working video capture device, please make sure\r\n") TEXT("that it is connected and is not being used by another application.\r\n\r\n") TEXT("The sample will now close."), hr); pSrcFilter->Release(); return hr; } // Render the preview pin on the video capture filter // Use this instead of g_pGraph->RenderFile hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter, NULL, NULL); if (FAILED(hr)) { Msg(TEXT("Couldn't render the video capture stream. hr=0x%x\r\n") TEXT("The capture device may already be in use by another application.\r\n\r\n") TEXT("The sample will now close."), hr); pSrcFilter->Release(); return hr; } // Now that the filter has been added to the graph and we have // rendered its stream, we can release this reference to the filter. pSrcFilter->Release(); // Set video window style and position hr = SetupVideoWindow(); if (FAILED(hr)) { Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr); return hr; } #ifdef REGISTER_FILTERGRAPH // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister); if (FAILED(hr)) { Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr); g_dwGraphRegister = 0; } #endif // Start previewing video data hr = g_pMC->Run(); if (FAILED(hr)) { Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr); return hr; } // Remember current state g_psCurrent = Running; return S_OK; }
HRESULT CAccessSys::StartPreview(HWND h) { HRESULT hr; IBaseFilter *pSrcFilter = NULL; if (e_psCurrent == Running){ StopPreview(); } hr = BuildPreview(); if (FAILED(hr)){ return hr; } //// Get DirectShow interfaces //hr = GetInterfaces(); //if (FAILED(hr)) //{ // Msg(TEXT("Failed to get video interfaces! hr=0x%x"), hr); // return hr; //} //// Attach the filter graph to the capture graph //hr = p_capture_graph_builder2->SetFiltergraph(p_graph); //if (FAILED(hr)) //{ // Msg(TEXT("Failed to set capture filter graph! hr=0x%x"), hr); // return hr; //} //// Use the system device enumerator and class enumerator to find //// a video capture/preview device, such as a desktop USB video camera. //hr = FindCaptureDevice(); //if (FAILED(hr)) //{ // // Don't display a message because FindCaptureDevice will handle it // return hr; //} //pSrcFilter = p_streams[0].p_device_filter; //// Add Capture filter to our graph. //hr = p_graph->AddFilter(pSrcFilter, L"Video Source"); //if (FAILED(hr)) //{ // Msg(TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n") // TEXT("If you have a working video capture device, please make sure\r\n") // TEXT("that it is connected and is not being used by another application.\r\n\r\n") // TEXT("The sample will now close."), hr); // pSrcFilter->Release(); // return hr; //} // Render the preview pin on the video capture filter // Use this instead of g_pGraph->RenderFile //pSrcFilter = p_streams[0].p_device_filter; //hr = p_capture_graph_builder2->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, // pSrcFilter, NULL, NULL); //if (FAILED(hr)) //{ // Msg(TEXT("Couldn't render the video capture stream. hr=0x%x\r\n") // TEXT("The capture device may already be in use by another application.\r\n\r\n") // TEXT("The sample will now close."), hr); // pSrcFilter->Release(); // return hr; //} //// Now that the filter has been added to the graph and we have //// rendered its stream, we can release this reference to the filter. //pSrcFilter->Release(); //pSrcFilter = p_streams[1].p_device_filter; //// Add Capture filter to our graph. //hr = p_graph->AddFilter(pSrcFilter, L"Audio Source"); //if (FAILED(hr)) //{ // Msg(TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n") // TEXT("If you have a working video capture device, please make sure\r\n") // TEXT("that it is connected and is not being used by another application.\r\n\r\n") // TEXT("The sample will now close."), hr); // pSrcFilter->Release(); // return hr; //} // Render the preview pin on the audio capture filter // Use this instead of g_pGraph->RenderFile //pSrcFilter = p_streams[1].p_device_filter; //hr = p_capture_graph_builder2->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Audio, // pSrcFilter, NULL, NULL); //if (FAILED(hr)) //{ // Msg(TEXT("Couldn't render the video capture stream. hr=0x%x\r\n") // TEXT("The capture device may already be in use by another application.\r\n\r\n") // TEXT("The sample will now close."), hr); // pSrcFilter->Release(); // return hr; //} //// Now that the filter has been added to the graph and we have //// rendered its stream, we can release this reference to the filter. //pSrcFilter->Release(); // Set video window style and position if (h){ h_wnd = h; hr = SetupVideoWindow(h_wnd); if (FAILED(hr)) { Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr); return hr; } } #ifdef REGISTER_FILTERGRAPH // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister); if (FAILED(hr)) { Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr); g_dwGraphRegister = 0; } #endif // Start previewing video & audio data hr = p_control->Run(); if (FAILED(hr)) { Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr); return hr; } DWORD err = GetLastError(); // Remember current state e_psCurrent = Running; //ConnectToServer(m_userinfo.ip, m_userinfo.port); return S_OK; }
HRESULT CaptureVideo() { HRESULT hr; IBaseFilter *pSrcFilter=NULL; // Get DirectShow interfaces hr = GetInterfaces(); if (FAILED(hr)) { Msg(TEXT("Failed to get video interfaces! hr=0x%x"), hr); return hr; } // Attach the filter graph to the capture graph hr = g_pCapture->SetFiltergraph(g_pGraph); if (FAILED(hr)) { Msg(TEXT("Failed to set capture filter graph! hr=0x%x"), hr); return hr; } // Use the system device enumerator and class enumerator to find // a video capture/preview device, such as a desktop USB video camera. hr = FindCaptureDevice(&pSrcFilter); if (FAILED(hr)) { // Don't display a message because FindCaptureDevice will handle it return hr; } // Add Capture filter to our graph. hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture"); if (FAILED(hr)) { Msg(TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n") TEXT("If you have a working video capture device, please make sure\r\n") TEXT("that it is connected and is not being used by another application.\r\n\r\n") TEXT("The sample will now close."), hr); pSrcFilter->Release(); return hr; } // Copied code //======================================== IAMStreamConfig *pSC; hr = g_pCapture->FindInterface(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Interleaved, pSrcFilter, IID_IAMStreamConfig, (void **)&pSC); if(FAILED(hr)) hr = g_pCapture->FindInterface(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter, IID_IAMStreamConfig, (void **)&pSC); if (!pSC) { return hr; } int iCount = 0, iSize = 0; hr = pSC->GetNumberOfCapabilities(&iCount, &iSize); // Check the size to make sure we pass in the correct structure. if (iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS)) { // Use the video capabilities structure. int i = 0; for (int iFormat = 0; iFormat < iCount; iFormat++) { VIDEO_STREAM_CONFIG_CAPS scc; AM_MEDIA_TYPE *pmtConfig; hr = pSC->GetFormat(&pmtConfig); VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)pmtConfig->pbFormat; double fps = 30; pvi->AvgTimePerFrame = (LONGLONG)(10000000/fps); pvi->bmiHeader.biSizeImage = DIBSIZE(pvi->bmiHeader); pvi->bmiHeader.biWidth = 1920; pvi->bmiHeader.biHeight = 1080; hr = pSC->SetFormat(pmtConfig); //hr = pSC->GetStreamCaps(iFormat, &pmtConfig, (BYTE*)&scc); //if (SUCCEEDED(hr)) //{ // /* Examine the format, and possibly use it. */ // if (pmtConfig->formattype == FORMAT_VideoInfo) { // long width = HEADER(pmtConfig->pbFormat)->biWidth; // long height = HEADER(pmtConfig->pbFormat)->biHeight; // // if (width == 1920 && height == 1080) { // VIDEOINFOHEADER *info = (VIDEOINFOHEADER *)pmtConfig->pbFormat; // if (i == 0) { // pSC->SetFormat(pmtConfig); // DeleteMediaType(pmtConfig); // break; // } // i++; // } // } // // Delete the media type when you are done. // DeleteMediaType(pmtConfig); //} } } if(SUCCEEDED(hr)) { pSC->Release(); } //======================================== // Render the preview pin on the video capture filter // Use this instead of g_pGraph->RenderFile hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter, NULL, NULL); if (FAILED(hr)) { Msg(TEXT("Couldn't render the video capture stream. hr=0x%x\r\n") TEXT("The capture device may already be in use by another application.\r\n\r\n") TEXT("The sample will now close."), hr); pSrcFilter->Release(); return hr; } // Now that the filter has been added to the graph and we have // rendered its stream, we can release this reference to the filter. pSrcFilter->Release(); // Set video window style and position hr = SetupVideoWindow(); if (FAILED(hr)) { Msg(TEXT("Couldn't initialize video window! hr=0x%x"), hr); return hr; } #ifdef REGISTER_FILTERGRAPH // Add our graph to the running object table, which will allow // the GraphEdit application to "spy" on our graph hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister); if (FAILED(hr)) { Msg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr); g_dwGraphRegister = 0; } #endif // Start previewing video data hr = g_pMC->Run(); if (FAILED(hr)) { Msg(TEXT("Couldn't run the graph! hr=0x%x"), hr); return hr; } // Remember current state g_psCurrent = Running; return S_OK; }
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; }