美文网首页win
Directshow 获取 设备信息(麦克风,扬声器,摄像头)

Directshow 获取 设备信息(麦克风,扬声器,摄像头)

作者: zjjcc | 来源:发表于2018-08-19 14:03 被阅读146次

通过DirectShow Api获取系统麦克风、扬声器、摄像头信息

#include <windows.h>

#include <vector>

#include <dshow.h>

#pragma comment(lib, "Strmiids.lib")

#define MAX_FRIENDLY_NAME_LENGTH    128

#define MAX_MONIKER_NAME_LENGTH     256

typedef struct _TDeviceName

{

    WCHAR FriendlyName[MAX_FRIENDLY_NAME_LENGTH];   // 设备友好名

    WCHAR MonikerName[MAX_MONIKER_NAME_LENGTH];     // 设备Moniker名

} TDeviceName;

//WCHAR  转换为 Char

char* WCharToChar(WCHAR *s)

{

 int w_nlen = WideCharToMultiByte(CP_ACP, 0, s, -1, NULL, 0, NULL, false);

 char *ret = new char[w_nlen];

 memset(ret, 0, w_nlen);

 WideCharToMultiByte(CP_ACP, 0, s, -1, ret, w_nlen, NULL, false);

return ret;

}

//REFGUID guidValue : CLSID_AudioInputDeviceCategory(麦克风);  CLSID_AudioRendererCategory(扬声器); //CLSID_VideoInputDeviceCategory(摄像头)

HRESULT GetAudioVideoInputDevices(std::vector<TDeviceName> &vectorDevices, REFGUID guidValue)

{

    TDeviceName name;

    HRESULT hr;

    // 初始化

    vectorDevices.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;

    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))

                {

                    StringCchCopy(name.FriendlyName, MAX_FRIENDLY_NAME_LENGTH, 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))

                        {

                            StringCchCopy(name.MonikerName, MAX_MONIKER_NAME_LENGTH, pOleDisplayName);

                            vectorDevices.push_back(name);

                        }

                          CoTaskMemFree(pOleDisplayName);

                    }

                }

                VariantClear(&varName);

                pPropBag->Release();

            }

            pMoniker->Release();

        } // End for While

        pEnumCat->Release();

    }

    pSysDevEnum->Release();

    CoUninitialize();

    return hr;

}

int _tmain(int argc, _TCHAR* argv[])

{

    std::vector<TDeviceName> vectorDevices;

    vectorDevices.clear();

//获取摄像头信息

    GetAudioVideoInputDevices(vectorDevices, CLSID_VideoInputDeviceCategory);

    for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)

    {

        printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));

    }

    vectorDevices.clear();

//获取麦克风设备信息

    GetAudioVideoInputDevices(vectorDevices, CLSID_AudioInputDeviceCategory);

    for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)

    {

        //wprintf(L"FriendlyName = %ws, MonikerName = %ws\n", itor->FriendlyName, itor->MonikerName);

        printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));

    }

    vectorDevices.clear();

//获取扬声器设备信息

    GetAudioVideoInputDevices(vectorDevices, CLSID_AudioRendererCategory);

    for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)

    {

        //wprintf(L"FriendlyName = %ws, MonikerName = %ws\n", itor->FriendlyName, itor->MonikerName);

        printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));

    }

    getchar();

    return 0;

}

相关文章

  • Directshow 获取 设备信息(麦克风,扬声器,摄像头)

    通过DirectShow Api获取系统麦克风、扬声器、摄像头信息 #include #i...

  • AVCaptureDevice

    判断设备是否存在某一类型设备 获取捕获设备 目前iPhone 上只能获取以下设备:前、后摄像头 及麦克风 a 获...

  • 罗技CC4000e:女生也能轻松玩转的4K视频会议系统

    提到视频协作办公系统,相信大家都会想到显示屏、摄像头、麦克风、扬声器、调制解调器、编译码器、图像处理设备、控制切换...

  • 视频开发基础概念知识二

    一、基本概念 AVCaptureSession 苹果为了管理从摄像头、麦克风等设备捕获到的信息,写了一个AVCap...

  • IOS自定义相机

    //捕获设备,通常是前置摄像头,后置摄像头,麦克风(音频输入) @property(nonatomic, stro...

  • iOS自定义相机

    1、首先声明以下对象 #import//捕获设备,通常是前置摄像头,后置摄像头,麦克风(音频输入)@propert...

  • 美颜摄像头获取

    美颜摄像头获取 关于摄像头获取的,简单的方法,就是使用OBS上面的“视频捕获设备(studio)/视频捕捉设备(c...

  • AVAudioSession之currentRoute

    原型 currentRoute用于控制当前app的输入/输出设备,比如麦克风、扬声器或耳机。这个属性完全由系统控制...

  • 五、捕捉媒体

    先上一个媒体捕捉类关系图。 捕捉设备 AVCaptureDevice: 为摄像头、麦克风等物理设备定义接口。最常...

  • FFmpeg-iOS获取摄像头麦克风

    今天咱来讲讲在iOS 平台上利用ffmpeg获取到摄像头和麦克风,代码很少,后面再加上iOS 自带的获取摄像头的例...

网友评论

    本文标题:Directshow 获取 设备信息(麦克风,扬声器,摄像头)

    本文链接:https://www.haomeiwen.com/subject/pzsciftx.html