DirectShow 获取音视频输入设备列表

开发环境:Win10 + VS2015

本文介绍一个 "获取音频视频输入设备列表" 的示例代码。


效果图


代码下载

代码下载(VC2015):Github - DShow_simpleVideo


实现代码

DS_GetAudioVideoInputDevices.h

/* ----------------------------------------------------------
文件名称:DS_AudioVideoDevices.h

功能描述:
获取音频视频输入设备列表

接口函数:
DS_GetAudioVideoInputDevices
------------------------------------------------------------ */
#pragma once

#include <windows.h>
#include <vector>
#include <dshow.h>

#ifndef MACRO_GROUP_DEVICENAME
#define MACRO_GROUP_DEVICENAME

#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;
#endif

#ifdef __cplusplus
extern "C"
{
#endif

    /*
    功能:获取音视频输入设备列表
    参数说明:
    vectorDevices:用于存储返回的设备友好名及Moniker名
    guidValue:
    CLSID_AudioInputDeviceCategory:获取音频输入设备列表
    CLSID_VideoInputDeviceCategory:获取视频输入设备列表
    返回值:
    错误代码
    说明:
    基于DirectShow
    列表中的第一个设备为系统缺省设备
    capGetDriverDescription只能获得设备驱动名
    */
    HRESULT DS_GetAudioVideoInputDevices(std::vector<TDeviceName> &vectorDevices, REFGUID guidValue);

#ifdef __cplusplus
}
#endif


DS_GetAudioVideoInputDevices.cpp

#include "stdafx.h"
#include "DS_AudioVideoDevices.h"

// 用到的DirectShow SDK链接库
#pragma comment(lib, "Strmiids.lib")

// 获取音视频输入设备列表
HRESULT DS_GetAudioVideoInputDevices(std::vector<TDeviceName> &vectorDevices, REFGUID guidValue)
{
    TDeviceName name;
    HRESULT hr;

    // 初始化vector
    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))
                {
                    // 拷贝设备友好名到name.FriendlyName
                    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))
                        {
                            // 拷贝设备Moniker名到name.MonikerName
                            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;
}


DShow_getVideoDevices.cpp

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <dshow.h>
#include "DS_AudioVideoDevices.h"

// 用到的DirectShow SDK链接库
#pragma comment(lib,"strmiids.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<TDeviceName> vectorDevices;

    // 获取音视频输入设备列表
    DS_GetAudioVideoInputDevices(vectorDevices, CLSID_VideoInputDeviceCategory);

    // 打印"设备友好名"和"设备Moniker名"(摄像头)
    for (unsigned int i = 0; i < vectorDevices.size(); i++)
        std::wcout << vectorDevices[i].FriendlyName << " | " << vectorDevices[i].MonikerName << std::endl;

    return 0;
}


参考:

基于DirectShow获取音频视频输入设备列表

DirectShow 视频采集

1.使用DShow获取本机的视音频设备和自带编解码器列表


猜你喜欢

转载自www.cnblogs.com/linuxAndMcu/p/12058731.html