XP下采用DirectShow采集摄像头

欢迎大家提出意见,一起讨论!

需要示例源码的请独自联系我.

前提: 摄像头能正常工作、摄像头有创建directshow filter

 

大家可以对比我的另一篇文章学习:    wince系统下DirectShow采集摄像头

一、初始化工作
1、DirctShow环境初始化
  1. bool  
  2. uEye_DirectShow_Demo_Dlg::DirectShow_Init()  
  3. {  
  4.     // initialize the COM library on the current thread  
  5.     HRESULT err= CoInitialize(NULL);  
  6.   
  7.     if( FAILED(err))  
  8.     {  
  9.         MessageBoxEx( NULL, "Initializing COM library failed!", __FUNCTION__, MB_ICONERROR, 0);  
  10.     }  
  11.   
  12.     return err == S_OK;  
  13. }  
bool
uEye_DirectShow_Demo_Dlg::DirectShow_Init()
{
    // initialize the COM library on the current thread
    HRESULT err= CoInitialize(NULL);

    if( FAILED(err))
    {
        MessageBoxEx( NULL, "Initializing COM library failed!", __FUNCTION__, MB_ICONERROR, 0);
    }

    return err == S_OK;
}

2、搜索Video源

如果没有设备接入,那么CreateClassEnumerator会返回失败

  1. bool  
  2. uEye_DirectShow_Demo_Dlg::VideoSourcesList_Fill()  
  3. {  
  4.     HRESULT status= S_OK;  
  5.   
  6.     // create System Device Enumerator  
  7.     ICreateDevEnum *pSystemDeviceEnumerator= NULL;  
  8.     status= CoCreateInstance(  CLSID_SystemDeviceEnum,  
  9.                                 NULL,  
  10.                                 CLSCTX_INPROC,  
  11.                                 IID_ICreateDevEnum,  
  12.                                 (void**)&pSystemDeviceEnumerator);  
  13.     if( FAILED(status))  
  14.     {  
  15.         MessageBoxEx( NULL, "Creating System Device Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);  
  16.         return false;  
  17.     }  
  18.   
  19.     // create Class Enumerator that lists alls video input devices among the system devices  
  20.     IEnumMoniker *pVideoInputDeviceEnumerator= NULL;  
  21.     status= pSystemDeviceEnumerator->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,  
  22.                                                             &pVideoInputDeviceEnumerator,  
  23.                                                             0);  
  24.   
  25.     // release the System Device Enumerator which is not needed anymore  
  26.     pSystemDeviceEnumerator->Release();  
  27.     pSystemDeviceEnumerator= NULL;  
  28.   
  29.     if( status != S_OK)  
  30.     {  
  31.         MessageBoxEx( NULL, "Creating Class Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);  
  32.         return false;  
  33.     }  
  34.   
  35.     // add entry '[no device selected]' to list  
  36.     m_comboVideoSources.AddString( "[no device selected]");  
  37.     m_comboVideoSources.SetItemDataPtr( 0, NULL);  
  38.   
  39.     // for each enumerated video input device: add it to the list  
  40.     IMoniker *pMoniker= NULL;  
  41.     while( pVideoInputDeviceEnumerator->Next( 1, &pMoniker, NULL) == S_OK )  
  42.     {  
  43.         VARIANT var;  
  44.         VariantInit(&var);  
  45.   
  46.         // make filters properties accessible  
  47.         IPropertyBag *pPropBag= NULL;  
  48.         status= pMoniker->BindToStorage( 0, 0, IID_IPropertyBag, (void**)&pPropBag);  
  49.         if( FAILED(status))  
  50.         {  
  51.             pPropBag= NULL;  
  52.             MessageBoxEx( NULL, "Accessing filter properties failed!", __FUNCTION__, MB_ICONERROR, 0);  
  53.             // continue with the next filter  
  54.         }  
  55.         else  
  56.         {  
  57.             // add a reference to the storage object  
  58.             pPropBag->AddRef();  
  59.   
  60.             // get the name of this filter  
  61.             status= pPropBag->Read( L"FriendlyName", &var, 0);  
  62.             if( FAILED(status))  
  63.             {  
  64.                 MessageBoxEx( NULL, "Reading filter name failed!", __FUNCTION__, MB_ICONERROR, 0);  
  65.                 // continue with the next filter  
  66.             }  
  67.             else  
  68.             {  
  69.                 // if uEye Capture Device:  
  70.                 // add filtername to the list and link the moniker pointer to the list entry  
  71.                 CString sTemp(var.bstrVal);  
  72. #if (0) /* jma [04/08/2010] add devices named UI... too */  
  73.                 if( sTemp.Find( "uEye Capture Device", 0) != -1)  
  74. #endif  
  75.                 if ((sTemp.Find( "uEye Capture Device", 0) != -1) || (sTemp.Find( "UI", 0) != -1))  
  76.                 {  
  77.                     int index = m_comboVideoSources.AddString( sTemp);  
  78.                     // dont forget to release the moniker later!  
  79.                     m_comboVideoSources.SetItemDataPtr( index, pMoniker);  
  80.                 }  
  81.                 else  
  82.                 {  
  83.                     pMoniker->Release();  
  84.                     pMoniker= NULL;  
  85.                 }  
  86.             }  
  87.   
  88.             // release the reference to the storage object  
  89.             pPropBag->Release();  
  90.             pPropBag= NULL;  
  91.         }  
  92.   
  93.         VariantClear(&var);  
  94.     }  
  95.   
  96.     // release the class enumerator  
  97.     pVideoInputDeviceEnumerator->Release();  
  98.     pVideoInputDeviceEnumerator= NULL;  
  99.   
  100.     // select first list entry  
  101.     m_comboVideoSources.SetCurSel( 0);  
  102.   
  103.     return false;  
  104. }  
bool
uEye_DirectShow_Demo_Dlg::VideoSourcesList_Fill()
{
    HRESULT status= S_OK;

    // create System Device Enumerator
    ICreateDevEnum *pSystemDeviceEnumerator= NULL;
    status= CoCreateInstance(  CLSID_SystemDeviceEnum,
                                NULL,
                                CLSCTX_INPROC,
                                IID_ICreateDevEnum,
                                (void**)&pSystemDeviceEnumerator);
    if( FAILED(status))
    {
        MessageBoxEx( NULL, "Creating System Device Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    // create Class Enumerator that lists alls video input devices among the system devices
    IEnumMoniker *pVideoInputDeviceEnumerator= NULL;
    status= pSystemDeviceEnumerator->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
                                                            &pVideoInputDeviceEnumerator,
                                                            0);

    // release the System Device Enumerator which is not needed anymore
    pSystemDeviceEnumerator->Release();
    pSystemDeviceEnumerator= NULL;

    if( status != S_OK)
    {
        MessageBoxEx( NULL, "Creating Class Enumerator failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    // add entry '[no device selected]' to list
    m_comboVideoSources.AddString( "[no device selected]");
    m_comboVideoSources.SetItemDataPtr( 0, NULL);

    // for each enumerated video input device: add it to the list
    IMoniker *pMoniker= NULL;
    while( pVideoInputDeviceEnumerator->Next( 1, &pMoniker, NULL) == S_OK )
    {
        VARIANT var;
        VariantInit(&var);

        // make filters properties accessible
        IPropertyBag *pPropBag= NULL;
        status= pMoniker->BindToStorage( 0, 0, IID_IPropertyBag, (void**)&pPropBag);
        if( FAILED(status))
        {
            pPropBag= NULL;
            MessageBoxEx( NULL, "Accessing filter properties failed!", __FUNCTION__, MB_ICONERROR, 0);
            // continue with the next filter
        }
        else
        {
            // add a reference to the storage object
            pPropBag->AddRef();

            // get the name of this filter
            status= pPropBag->Read( L"FriendlyName", &var, 0);
            if( FAILED(status))
            {
                MessageBoxEx( NULL, "Reading filter name failed!", __FUNCTION__, MB_ICONERROR, 0);
                // continue with the next filter
            }
            else
            {
                // if uEye Capture Device:
                // add filtername to the list and link the moniker pointer to the list entry
                CString sTemp(var.bstrVal);
#if (0) /* jma [04/08/2010] add devices named UI... too */
                if( sTemp.Find( "uEye Capture Device", 0) != -1)
#endif
                if ((sTemp.Find( "uEye Capture Device", 0) != -1) || (sTemp.Find( "UI", 0) != -1))
                {
                    int index = m_comboVideoSources.AddString( sTemp);
                    // dont forget to release the moniker later!
                    m_comboVideoSources.SetItemDataPtr( index, pMoniker);
                }
                else
                {
                    pMoniker->Release();
                    pMoniker= NULL;
                }
            }

            // release the reference to the storage object
            pPropBag->Release();
            pPropBag= NULL;
        }

        VariantClear(&var);
    }

    // release the class enumerator
    pVideoInputDeviceEnumerator->Release();
    pVideoInputDeviceEnumerator= NULL;

    // select first list entry
    m_comboVideoSources.SetCurSel( 0);

    return false;
}

二、选择某个摄像设备
1、先停止原有的Media
  1. bool  
  2. uEye_DirectShow_Demo_Dlg::FilterGraph_Stop()  
  3. {  
  4.     Invalidate();  
  5.   
  6.     // proceed only if filter graph manager object present  
  7.     if( m_pActiveFilterGraphManager == NULL)  
  8.     {  
  9.         return true;  
  10.     }  
  11.   
  12.     HRESULT status= S_OK;  
  13.   
  14.     // get the MediaControl interface of the graph  
  15.     IMediaControl* pMediaControl= NULL;  
  16.     status= m_pActiveFilterGraphManager->QueryInterface( IID_IMediaControl, (void**)&pMediaControl);  
  17.     if( FAILED(status) || pMediaControl == NULL)  
  18.     {  
  19.         MessageBoxEx( NULL, "Querying MediaControl interface of the filter graph manager failed!", __FUNCTION__, MB_ICONERROR, 0);  
  20.         return false;  
  21.     }  
  22.   
  23.     //// check for graph to be executing before allowing to stop  
  24.   
  25.     //OAFilterState filterState= 0;   // OAFilterState is actually a long  
  26.     //status= pMediaControl->GetState( 100, &filterState);  
  27.     //if( FAILED(status))  
  28.     //{  
  29.     //    // cleanup  
  30.   
  31.     //    // release the MediaControl interface object  
  32.     //    pMediaControl->Release();  
  33.     //    pMediaControl= NULL;  
  34.   
  35.     //    MessageBoxEx( NULL, "Querying graph state failed!", __FUNCTION__, MB_ICONERROR, 0);  
  36.     //    return false;  
  37.     //}  
  38.   
  39.     //if( filterState != State_Stopped)  
  40.     {  
  41.         // stop the execution of the graph  
  42.         status= pMediaControl->Stop();  
  43.         if( FAILED(status))  
  44.         {  
  45.             // cleanup  
  46.   
  47.             // release the MediaControl interface object  
  48.             pMediaControl->Release();  
  49.             pMediaControl= NULL;  
  50.   
  51.             MessageBoxEx( NULL, "Stopping the graph failed!", __FUNCTION__, MB_ICONERROR, 0);  
  52.             return false;  
  53.         }  
  54.   
  55.         // update the graph state view  
  56.         UpdateGraphState( State_Stopped);  
  57.     }  
  58.   
  59.     // release the MediaControl interface object  
  60.     pMediaControl->Release();  
  61.     pMediaControl= NULL;  
  62.   
  63.     return true;  
  64. }  
bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Stop()
{
    Invalidate();

    // proceed only if filter graph manager object present
    if( m_pActiveFilterGraphManager == NULL)
    {
        return true;
    }

    HRESULT status= S_OK;

    // get the MediaControl interface of the graph
    IMediaControl* pMediaControl= NULL;
    status= m_pActiveFilterGraphManager->QueryInterface( IID_IMediaControl, (void**)&pMediaControl);
    if( FAILED(status) || pMediaControl == NULL)
    {
        MessageBoxEx( NULL, "Querying MediaControl interface of the filter graph manager failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    //// check for graph to be executing before allowing to stop

    //OAFilterState filterState= 0;   // OAFilterState is actually a long
    //status= pMediaControl->GetState( 100, &filterState);
    //if( FAILED(status))
    //{
    //    // cleanup

    //    // release the MediaControl interface object
    //    pMediaControl->Release();
    //    pMediaControl= NULL;

    //    MessageBoxEx( NULL, "Querying graph state failed!", __FUNCTION__, MB_ICONERROR, 0);
    //    return false;
    //}

    //if( filterState != State_Stopped)
    {
        // stop the execution of the graph
        status= pMediaControl->Stop();
        if( FAILED(status))
        {
            // cleanup

            // release the MediaControl interface object
            pMediaControl->Release();
            pMediaControl= NULL;

            MessageBoxEx( NULL, "Stopping the graph failed!", __FUNCTION__, MB_ICONERROR, 0);
            return false;
        }

        // update the graph state view
        UpdateGraphState( State_Stopped);
    }

    // release the MediaControl interface object
    pMediaControl->Release();
    pMediaControl= NULL;

    return true;
}


2、删除Graph
  1. bool  
  2. uEye_DirectShow_Demo_Dlg::FilterGraph_Destroy()  
  3. {  
  4.     // proceed only if filter graph manager object present  
  5.     if( m_pActiveFilterGraphManager == NULL)  
  6.     {  
  7.         return true;  
  8.     }  
  9.   
  10.     if( !FilterGraph_Stop())  
  11.     {  
  12.         return false;  
  13.     }  
  14.   
  15.     // disable 'dump graph' button as long as no graph present  
  16.     m_bnDumpGraph.EnableWindow( FALSE);  
  17.   
  18.     // delete the capture filter  
  19.     m_pActiveVideoSource->Release();  
  20.     m_pActiveVideoSource= NULL;  
  21.   
  22.     // delete the graph  
  23.     m_pActiveFilterGraphManager->Release();  
  24.     m_pActiveFilterGraphManager= NULL;  
  25.   
  26.     // update the graph state view  
  27.     UpdateGraphState( -1);  
  28.   
  29.     return true;  
  30. }  
bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Destroy()
{
    // proceed only if filter graph manager object present
    if( m_pActiveFilterGraphManager == NULL)
    {
        return true;
    }

    if( !FilterGraph_Stop())
    {
        return false;
    }

    // disable 'dump graph' button as long as no graph present
    m_bnDumpGraph.EnableWindow( FALSE);

    // delete the capture filter
    m_pActiveVideoSource->Release();
    m_pActiveVideoSource= NULL;

    // delete the graph
    m_pActiveFilterGraphManager->Release();
    m_pActiveFilterGraphManager= NULL;

    // update the graph state view
    UpdateGraphState( -1);

    return true;
}


 

3、根据选择的设备的moniker来创建Graph

分为:

(1)为选择的设备创建capture filter

 status= pMoniker->BindToObject( 0, 0, IID_IBaseFilter, (void**)&m_pActiveVideoSource);

(2) 创建一个capture Graph Builder对象

ICaptureGraphBuilder2* pCaptureGraphBuilder= NULL;
    status= CoCreateInstance(   CLSID_CaptureGraphBuilder2,
                                NULL,
                                CLSCTX_INPROC,
                                IID_ICaptureGraphBuilder2,
                                (void**)&pCaptureGraphBuilder);

(3) 创建 Filter Graph Manager

  // create the Filter Graph Manager
    status= CoCreateInstance(   CLSID_FilterGraph,
                                NULL,
                                CLSCTX_INPROC,
                                IID_IGraphBuilder,
                                (void **)&m_pActiveFilterGraphManager);

(4) 初始化Capture Graph Builder 对象,把graph和builder 连接起来

   status= pCaptureGraphBuilder->SetFiltergraph( m_pActiveFilterGraphManager);

(5) 增加Capture到graph中

  status= m_pActiveFilterGraphManager->AddFilter( m_pActiveVideoSource, L"Video Capture");

(6) render 视频的capture pin, 把caputre filter和render连接起来

  status= pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE,
                                                &MEDIATYPE_Video,
                                                m_pActiveVideoSource,
                                                NULL,
                                                NULL);

(7) 查询filter graph manager的VideoWindow接口

 IVideoWindow* pVideoWindow= NULL;
    status= m_pActiveFilterGraphManager->QueryInterface( IID_IVideoWindow, (void**)&pVideoWindow);

(8) 把video view连接到graph

 // connect the dialogs video view to the graph
    CWnd* pwnd= GetDlgItem( IDC_STATIC_VIDEOVIEW);
    pVideoWindow->put_Owner( (OAHWND)pwnd->m_hWnd);  // put_Owner always returns NOERROR

// we dont want a title bar on our video view
    long pWindowStyle = 0;
    pVideoWindow->get_WindowStyle(&pWindowStyle);
    pWindowStyle &= ~WS_CAPTION;
    pVideoWindow->put_WindowStyle(pWindowStyle);

    // adjust graphs video geometry
    CRect rc( 0, 0, 0, 0);
    pwnd->GetClientRect( &rc);
    pVideoWindow->SetWindowPosition( rc.left, rc.top, rc.Width(), rc.Height());

    // release the VideoWindow interface object, we do not need it anymore
    pVideoWindow->Release();

四、  开始播放
  1. bool  
  2. uEye_DirectShow_Demo_Dlg::FilterGraph_Start()  
  3. {  
  4.     // proceed only if filter graph manager object present  
  5.     if( m_pActiveFilterGraphManager == NULL)  
  6.     {  
  7.         return true;  
  8.     }  
  9.   
  10.     HRESULT status= S_OK;  
  11.   
  12.     // get the MediaControl interface of the graph  
  13.     IMediaControl* pMediaControl= NULL;  
  14.     status= m_pActiveFilterGraphManager->QueryInterface( IID_IMediaControl, (void**)&pMediaControl);  
  15.     if( FAILED(status) || pMediaControl == NULL)  
  16.     {  
  17.         MessageBoxEx( NULL, "Querying MediaControl interface of the filter graph manager failed!", __FUNCTION__, MB_ICONERROR, 0);  
  18.         return false;  
  19.     }  
  20.   
  21.     //// check for graph to be stopped before allowing to start  
  22.   
  23.     //OAFilterState filterState= 0;   // OAFilterState is actually a long  
  24.     //status= pMediaControl->GetState( 100, &filterState);  
  25.     //if( FAILED(status))  
  26.     //{  
  27.     //    // cleanup  
  28.   
  29.     //    // release the MediaControl interface object  
  30.     //    pMediaControl->Release();  
  31.     //    pMediaControl= NULL;  
  32.   
  33.     //    MessageBoxEx( NULL, "Querying graph state failed!", __FUNCTION__, MB_ICONERROR, 0);  
  34.     //    return false;  
  35.     //}  
  36.   
  37.     //if( filterState == State_Stopped)  
  38.     {  
  39.         // start the execution of the graph  
  40.         status= pMediaControl->Run();  
  41.         if( FAILED(status))  
  42.         {  
  43.             // cleanup  
  44.   
  45.             // release the MediaControl interface object  
  46.             pMediaControl->Release();  
  47.             pMediaControl= NULL;  
  48.   
  49.             MessageBoxEx( NULL, "Starting the graph failed!", __FUNCTION__, MB_ICONERROR, 0);  
  50.             return false;  
  51.         }  
  52.   
  53.         // update the graph state view  
  54.         UpdateGraphState( State_Running);  
  55.     }  
  56.   
  57.     // release the MediaControl interface object  
  58.     pMediaControl->Release();  
  59.     pMediaControl= NULL;  
  60.   
  61.     return true;  
  62. }  
bool
uEye_DirectShow_Demo_Dlg::FilterGraph_Start()
{
    // proceed only if filter graph manager object present
    if( m_pActiveFilterGraphManager == NULL)
    {
        return true;
    }

    HRESULT status= S_OK;

    // get the MediaControl interface of the graph
    IMediaControl* pMediaControl= NULL;
    status= m_pActiveFilterGraphManager->QueryInterface( IID_IMediaControl, (void**)&pMediaControl);
    if( FAILED(status) || pMediaControl == NULL)
    {
        MessageBoxEx( NULL, "Querying MediaControl interface of the filter graph manager failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    //// check for graph to be stopped before allowing to start

    //OAFilterState filterState= 0;   // OAFilterState is actually a long
    //status= pMediaControl->GetState( 100, &filterState);
    //if( FAILED(status))
    //{
    //    // cleanup

    //    // release the MediaControl interface object
    //    pMediaControl->Release();
    //    pMediaControl= NULL;

    //    MessageBoxEx( NULL, "Querying graph state failed!", __FUNCTION__, MB_ICONERROR, 0);
    //    return false;
    //}

    //if( filterState == State_Stopped)
    {
        // start the execution of the graph
        status= pMediaControl->Run();
        if( FAILED(status))
        {
            // cleanup

            // release the MediaControl interface object
            pMediaControl->Release();
            pMediaControl= NULL;

            MessageBoxEx( NULL, "Starting the graph failed!", __FUNCTION__, MB_ICONERROR, 0);
            return false;
        }

        // update the graph state view
        UpdateGraphState( State_Running);
    }

    // release the MediaControl interface object
    pMediaControl->Release();
    pMediaControl= NULL;

    return true;
}

五、 pin 特征的查看

如果pin里有自己的查看特征接口, 那么就直接调用接口

  1. bool  
  2. uEye_DirectShow_Demo_Dlg::ShowPinProperties()  
  3. {  
  4.     // proceed only if video source object present  
  5.     if( m_pActiveVideoSource == NULL)  
  6.     {  
  7.         return true;  
  8.     }  
  9.   
  10.     HRESULT status= S_OK;  
  11.   
  12.     IPin* pPin= GetPin( m_pActiveVideoSource, PINDIR_OUTPUT, &MEDIATYPE_Video, &PIN_CATEGORY_CAPTURE);  
  13.     if( pPin == NULL)  
  14.     {  
  15.         MessageBoxEx( NULL, "Pin not available!", __FUNCTION__, MB_ICONERROR, 0);  
  16.         return false;  
  17.     }  
  18.   
  19.     IUnknown* pFilterUnk= NULL;  
  20.     status= pPin->QueryInterface( IID_IUnknown, (void**)&pFilterUnk);  
  21.     if( FAILED(status))  
  22.     {  
  23.         // cleanup  
  24.   
  25.         pPin->Release();  
  26.         pPin= NULL;  
  27.   
  28.         MessageBoxEx( NULL, "Querying pin's IAMStreamConfig interface failed!", __FUNCTION__, MB_ICONERROR, 0);  
  29.         return false;  
  30.     }  
  31.   
  32.     ISpecifyPropertyPages* pSpecifyPropertyPages= NULL;  
  33.     status= pFilterUnk->QueryInterface( IID_ISpecifyPropertyPages, (void**)&pSpecifyPropertyPages);  
  34.     if( FAILED(status))  
  35.     {  
  36.         // cleanup  
  37.   
  38.         pFilterUnk->Release();  
  39.         pFilterUnk= NULL;  
  40.   
  41.         pPin->Release();  
  42.         pPin= NULL;  
  43.   
  44.         MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);  
  45.         return false;  
  46.     }  
  47.   
  48.     CAUUID cauuid= { 0, NULL};  
  49.     status= pSpecifyPropertyPages->GetPages( &cauuid);  
  50.     if( FAILED(status))  
  51.     {  
  52.         // cleanup  
  53.   
  54.         pSpecifyPropertyPages->Release();  
  55.         pSpecifyPropertyPages->Release();  
  56.         pSpecifyPropertyPages= NULL;  
  57.   
  58.         pFilterUnk->Release();  
  59.         pFilterUnk= NULL;  
  60.   
  61.         pPin->Release();  
  62.         pPin= NULL;  
  63.   
  64.         MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);  
  65.         return false;  
  66.     }  
  67.     pSpecifyPropertyPages->Release();  
  68.     pSpecifyPropertyPages->Release();  
  69.     pSpecifyPropertyPages= NULL;  
  70.   
  71.     status= OleCreatePropertyFrame( GetSafeHwnd(),//*this,  
  72.                                     0,  
  73.                                     0,  
  74.                                     OLESTR("uEye Capture Device Pin"),  
  75.                                     1,  
  76.                                     (IUnknown**)&pFilterUnk,  
  77.                                     cauuid.cElems,  
  78.                                     (GUID*)cauuid.pElems,  
  79.                                     0,  
  80.                                     0,  
  81.                                     NULL);  
  82.   
  83.     if( FAILED(status))  
  84.     {  
  85.         // cleanup  
  86.   
  87.         CoTaskMemFree( cauuid.pElems);  
  88.         cauuid.pElems= NULL;  
  89.         cauuid.cElems= 0;  
  90.   
  91.         pFilterUnk->Release();  
  92.         pFilterUnk= NULL;  
  93.   
  94.         pPin->Release();  
  95.         pPin= NULL;  
  96.   
  97.         MessageBoxEx( NULL, "OleCreatePropertyFrame failed!", __FUNCTION__, MB_ICONERROR, 0);  
  98.         return false;  
  99.     }  
  100.   
  101.     // cleanup  
  102.   
  103.     CoTaskMemFree( cauuid.pElems);  
  104.     cauuid.pElems= NULL;  
  105.     cauuid.cElems= 0;  
  106.   
  107.     pFilterUnk->Release();  
  108.     pFilterUnk= NULL;  
  109.   
  110.     pPin->Release();  
  111.     pPin= NULL;  
  112.   
  113.     return true;  
  114. }  
bool
uEye_DirectShow_Demo_Dlg::ShowPinProperties()
{
    // proceed only if video source object present
    if( m_pActiveVideoSource == NULL)
    {
        return true;
    }

    HRESULT status= S_OK;

    IPin* pPin= GetPin( m_pActiveVideoSource, PINDIR_OUTPUT, &MEDIATYPE_Video, &PIN_CATEGORY_CAPTURE);
    if( pPin == NULL)
    {
        MessageBoxEx( NULL, "Pin not available!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    IUnknown* pFilterUnk= NULL;
    status= pPin->QueryInterface( IID_IUnknown, (void**)&pFilterUnk);
    if( FAILED(status))
    {
        // cleanup

        pPin->Release();
        pPin= NULL;

        MessageBoxEx( NULL, "Querying pin's IAMStreamConfig interface failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    ISpecifyPropertyPages* pSpecifyPropertyPages= NULL;
    status= pFilterUnk->QueryInterface( IID_ISpecifyPropertyPages, (void**)&pSpecifyPropertyPages);
    if( FAILED(status))
    {
        // cleanup

        pFilterUnk->Release();
        pFilterUnk= NULL;

        pPin->Release();
        pPin= NULL;

        MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    CAUUID cauuid= { 0, NULL};
    status= pSpecifyPropertyPages->GetPages( &cauuid);
    if( FAILED(status))
    {
        // cleanup

        pSpecifyPropertyPages->Release();
        pSpecifyPropertyPages->Release();
        pSpecifyPropertyPages= NULL;

        pFilterUnk->Release();
        pFilterUnk= NULL;

        pPin->Release();
        pPin= NULL;

        MessageBoxEx( NULL, "Querying pin's ISpecifyPropertyPages interface failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }
    pSpecifyPropertyPages->Release();
    pSpecifyPropertyPages->Release();
    pSpecifyPropertyPages= NULL;

    status= OleCreatePropertyFrame( GetSafeHwnd(),//*this,
                                    0,
                                    0,
                                    OLESTR("uEye Capture Device Pin"),
                                    1,
                                    (IUnknown**)&pFilterUnk,
                                    cauuid.cElems,
                                    (GUID*)cauuid.pElems,
                                    0,
                                    0,
                                    NULL);

    if( FAILED(status))
    {
        // cleanup

        CoTaskMemFree( cauuid.pElems);
        cauuid.pElems= NULL;
        cauuid.cElems= 0;

        pFilterUnk->Release();
        pFilterUnk= NULL;

        pPin->Release();
        pPin= NULL;

        MessageBoxEx( NULL, "OleCreatePropertyFrame failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    // cleanup

    CoTaskMemFree( cauuid.pElems);
    cauuid.pElems= NULL;
    cauuid.cElems= 0;

    pFilterUnk->Release();
    pFilterUnk= NULL;

    pPin->Release();
    pPin= NULL;

    return true;
}

六、 查询包含的 filter信息
  1. bool  
  2. uEye_DirectShow_Demo_Dlg::EnumerateFilters()   
  3. {  
  4.     // proceed only if filter graph builder object present  
  5.     if( m_pActiveFilterGraphManager == NULL)  
  6.     {  
  7.         return false;  
  8.     }  
  9.   
  10.     IEnumFilters *pEnum = NULL;  
  11.     IBaseFilter *pFilter;  
  12.     ULONG cFetched;  
  13.   
  14.     HRESULT hr = m_pActiveFilterGraphManager->EnumFilters(&pEnum);  
  15.     if (FAILED(hr))  
  16.     {  
  17.         MessageBoxEx( NULL, "Enumerating filters failed!", __FUNCTION__, MB_ICONERROR, 0);  
  18.         return false;  
  19.     }  
  20.   
  21.     CString sInfo( "Filters in the Graph:\n\n");  
  22.     int i= 0;  
  23.   
  24.     while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)  
  25.     {  
  26.         i++;  
  27.   
  28.         FILTER_INFO FilterInfo;  
  29.         hr = pFilter->QueryFilterInfo(&FilterInfo);  
  30.         if (FAILED(hr))  
  31.         {  
  32.             MessageBoxEx( NULL, "Could not get the filter info", __FUNCTION__, MB_ICONERROR, 0);  
  33.             continue;  // Maybe the next one will work.  
  34.         }  
  35.   
  36.         LPWSTR pVendorInfo= NULL;  
  37.         hr= pFilter->QueryVendorInfo( &pVendorInfo);  
  38.         if( FAILED(hr))  
  39.         {  
  40.             pVendorInfo= NULL;  
  41.         }  
  42.   
  43.   
  44.         if( pVendorInfo != NULL)  
  45.         {  
  46.             sInfo.AppendFormat( "%d: %S (by %S)\n", i, FilterInfo.achName, pVendorInfo);  
  47.             CoTaskMemFree( pVendorInfo);  
  48.             pVendorInfo= NULL;  
  49.         }  
  50.         else  
  51.         {  
  52.             sInfo.AppendFormat( "%d: %S\n", i, FilterInfo.achName);  
  53.         }  
  54.   
  55.         // The FILTER_INFO structure holds a pointer to the Filter Graph  
  56.         // Manager, with a reference count that must be released.  
  57.         if (FilterInfo.pGraph != NULL)  
  58.         {  
  59.             FilterInfo.pGraph->Release();  
  60.         }  
  61.         pFilter->Release();  
  62.     }  
  63.   
  64.     pEnum->Release();  
  65.   
  66.     MessageBoxEx( NULL, sInfo.GetBuffer(), __FUNCTION__, MB_OK, 0);  
  67.   
  68.     return true;  
  69. }  
bool
uEye_DirectShow_Demo_Dlg::EnumerateFilters() 
{
    // proceed only if filter graph builder object present
    if( m_pActiveFilterGraphManager == NULL)
    {
        return false;
    }

    IEnumFilters *pEnum = NULL;
    IBaseFilter *pFilter;
    ULONG cFetched;

    HRESULT hr = m_pActiveFilterGraphManager->EnumFilters(&pEnum);
    if (FAILED(hr))
    {
        MessageBoxEx( NULL, "Enumerating filters failed!", __FUNCTION__, MB_ICONERROR, 0);
        return false;
    }

    CString sInfo( "Filters in the Graph:\n\n");
    int i= 0;

    while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)
    {
        i++;

        FILTER_INFO FilterInfo;
        hr = pFilter->QueryFilterInfo(&FilterInfo);
        if (FAILED(hr))
        {
            MessageBoxEx( NULL, "Could not get the filter info", __FUNCTION__, MB_ICONERROR, 0);
            continue;  // Maybe the next one will work.
        }

        LPWSTR pVendorInfo= NULL;
        hr= pFilter->QueryVendorInfo( &pVendorInfo);
        if( FAILED(hr))
        {
            pVendorInfo= NULL;
        }


        if( pVendorInfo != NULL)
        {
            sInfo.AppendFormat( "%d: %S (by %S)\n", i, FilterInfo.achName, pVendorInfo);
            CoTaskMemFree( pVendorInfo);
            pVendorInfo= NULL;
        }
        else
        {
            sInfo.AppendFormat( "%d: %S\n", i, FilterInfo.achName);
        }

        // The FILTER_INFO structure holds a pointer to the Filter Graph
        // Manager, with a reference count that must be released.
        if (FilterInfo.pGraph != NULL)
        {
            FilterInfo.pGraph->Release();
        }
        pFilter->Release();
    }

    pEnum->Release();

    MessageBoxEx( NULL, sInfo.GetBuffer(), __FUNCTION__, MB_OK, 0);

    return true;
}


 

//指定视频采集设备的友好名字,为它创建一个Filter IBaseFilter * CTestPreviewDlg::CreateVideoDevice(const char * inFriendlyName) { return CreateHardwareFilter(CLSID_VideoInputDeviceCategory,inFriendlyName); } //根据设备的友好名字,创建一个代表该设备的Filter IBaseFilter * CTestPreviewDlg::CreateHardwareFilter(GUID inCategory,const char * inFriendlyName) { //创建一个系统枚举组件对象 ICreateDevEnum * enumHardware = NULL; HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_ALL, IID_ICreateDevEnum,(void**)&enumHardware); if(FAILED(hr)) { return NULL; } IBaseFilter * hardwareFilter = NULL; IEnumMoniker * enumMoniker = NULL; //为指定的目录创建枚举器 hr = enumHardware->CreateClassEnumerator(inCategory,&enumMoniker,0); if(enumMoniker) { enumMoniker->Reset(); ULONG fetched = 0; IMoniker * moniker = NULL; char friendlyName[256]; //枚举得到该目录下所有的设备,逐个进行名字匹配 while(!hardwareFilter && SUCCEEDED(enumMoniker->Next(1,&moniker, &fetched)) && fetched) { if(moniker) { IPropertyBag * propertyBag = NULL; VARIANT name; friendlyName[0] = 0; hr = moniker->BindToStorage(0,0,IID_IPropertyBag,(void**)&propertyBag); //读取设备的友好名字 if(SUCCEEDED(hr)) { name.vt = VT_BSTR; hr = propertyBag->Read(L"Friendlyname",&name,NULL); } if(SUCCEEDED(hr)) { WideCharToMultiByte(CP_ACP,0,name.bstrVal,-1, friendlyName,256,NULL,NULL); //如果当前设备的友好名字与用户指定的设备名字相同, //则将当前设备标识绑定为Filter形式 if(strcmp(friendlyName,inFriendlyName) == 0) { moniker->BindToObject(0,0,IID_IBaseFilter, (void**)&hardwareFilter); } } //释放使用过的接口 if(propertyBag) { propertyBag->Release(); propertyBag = NULL; } moniker->Release(); } } enumMoniker->Release(); } enumHardware->Release(); return hardwareFilter; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值