Directshow获取系统默认设备

本文介绍使用DirectShow API来枚举系统中可用的音频输入(如麦克风)、音频输出(如扬声器)及视频输入(如摄像头)设备的方法。通过创建设备枚举器并获取设备类别枚举器,可以进一步读取每个设备的友好名称和唯一标识。
摘要由CSDN通过智能技术生成
下面函数为通过 Directshow  API 获取设备列表的函数, pEnumCat->Next的第0个设备为系统默认设备

REFGUID guidValue :  CLSID_AudioInputDeviceCategory(麦克风);  CLSID_AudioRendererCategory(扬声器);  CLSID_VideoInputDeviceCategory(Camera)

HRESULT GetAVDevices( std::list<av_device> &list_Devices, REFGUID guidValue )
{
    TDeviceName name;   
    av_device avname; 
    HRESULT hr;  

    // 初始化  
    //list_Devices.clear();  

    // 初始化COM  
    hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );  
    if (FAILED(hr))  
    {  
        return hr;  
    }  

    // 创建系统设备枚举器实例  
    ICreateDevEnum *pSysDevEnum = NULL;  
    hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum );  
    if (FAILED(hr))  
    {  
        CoUninitialize();  
        return hr;  
    }  

    // 获取设备类枚举器  
    IEnumMoniker *pEnumCat = NULL;  
    int i =0;
    hr = pSysDevEnum->CreateClassEnumerator( guidValue, &pEnumCat, 0 );  
    if (hr == S_OK)   
    {  
        // 枚举设备名称  
        IMoniker *pMoniker = NULL;  
        ULONG cFetched;  
        while(pEnumCat->Next( 1, &pMoniker, &cFetched ) == S_OK)  
        {  
            IPropertyBag *pPropBag;  
            hr = pMoniker->BindToStorage( NULL, NULL, IID_IPropertyBag, (void **)&pPropBag );  
            if (SUCCEEDED(hr))  
            {  
                // 获取设备友好名  
                VARIANT varName;  
                VariantInit( &varName );  

                hr = pPropBag->Read( L"FriendlyName", &varName, NULL );  
                if (SUCCEEDED(hr))  
                {  
                    avname.device_name = Win_Utility::UnicodeToUtf8(varName.bstrVal);
                    avname.device_name_display = Win_Utility::UnicodeToANSI(varName.bstrVal);
                    // 获取设备Moniker名  
                    LPOLESTR pOleDisplayName = reinterpret_cast<LPOLESTR>(CoTaskMemAlloc(MAX_MONIKER_NAME_LENGTH * 2));  
                    if (pOleDisplayName != NULL)  
                    {  
                        hr = pMoniker->GetDisplayName( NULL, NULL, &pOleDisplayName );  

                        if (SUCCEEDED(hr))  
                        {  
                            avname.unique_deviceid = Win_Utility::UnicodeToANSI(pOleDisplayName);
                            if(CLSID_AudioInputDeviceCategory==guidValue)
                            {
                                avname.device_type=AUDIO_CHANNEL;
                            }
                            else if(CLSID_VideoInputDeviceCategory==guidValue)
                            {
                                avname.device_type=VIDEO_CHANNEL;
                            }

                            if(i==0)
                            {
                                avname.windows_default=1;
                            }
                            else
                            {
                                avname.windows_default=0;
                            }
                            avname.user_default = 0;
                            list_Devices.push_back( avname ); 
                            i++;
                        }  

                        CoTaskMemFree( pOleDisplayName );  
                    }  
                }  

                VariantClear( &varName );  
                pPropBag->Release();                       
            }  

            pMoniker->Release();  
        } // End for While  

        pEnumCat->Release();  
    }  

    pSysDevEnum->Release();  
    CoUninitialize();  
    return hr;  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值