mac os 获取音频设备列表

最近在研究mac下音视频采集原理,首先需要的是枚举设备:

#include <CoreFoundation/CFString.h>

// macos 音频头文件
#include <CoreAudio/CoreAudio.h>

#include <util/apple/cfstring-utils.h>

#include "audio-device-enum.h"

/* ugh, because mac has no means of capturing output, we have to basically
 * mark soundflower, wavtap and sound siphon as output devices. */
// 判断该设备是否是输入设备
static inline bool device_is_input(char *device)
{
	return astrstri(device, "soundflower") == NULL &&
	       astrstri(device, "wavtap")      == NULL &&
	       astrstri(device, "soundsiphon") == NULL;
}

// 判断是否枚举成功
static inline bool enum_success(OSStatus stat, const char *msg)
{
	if (stat != noErr) {
		blog(LOG_WARNING, "[coreaudio_enum_devices] %s failed: %d",
				msg, (int)stat);
		return false;
	}

	return true;
}

// 类型重定义 定义一种函数指针类型 enum_device_proc_t
typedef bool (*enum_device_proc_t)(void *param, CFStringRef cf_name,
		CFStringRef cf_uid, AudioDeviceID id);

// 由设备id获取设备uid和设备name
static bool coreaudio_enum_device(enum_device_proc_t proc, void *param,
		AudioDeviceID id)
{
	UInt32      size      = 0;
	CFStringRef cf_name   = NULL;
	CFStringRef cf_uid    = NULL;
	bool        enum_next = true;
	OSStatus    stat;

	AudioObjectPropertyAddress addr = {
		kAudioDevicePropertyStreams,
		kAudioDevicePropertyScopeInput,
		kAudioObjectPropertyElementMaster
	};

	/* check to see if it's a mac input device */
	AudioObjectGetPropertyDataSize(id, &addr, 0, NULL, &size);
	if (!size)
		return true;

	size = sizeof(CFStringRef);

	addr.mSelector = kAudioDevicePropertyDeviceUID;
	stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_uid);
	if (!enum_success(stat, "get audio device UID"))
		return true;

	addr.mSelector = kAudioDevicePropertyDeviceNameCFString;
	stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_name);
	if (!enum_success(stat, "get audio device name"))
		goto fail;

	enum_next = proc(param, cf_name, cf_uid, id);

fail:
	if (cf_name)
		CFRelease(cf_name);
	if (cf_uid)
		CFRelease(cf_uid);
	return enum_next;
}

// 枚举设备 
static void enum_devices(enum_device_proc_t proc, void *param)
{
	AudioObjectPropertyAddress addr = {
		kAudioHardwarePropertyDevices,
		kAudioObjectPropertyScopeGlobal,
		kAudioObjectPropertyElementMaster
	};

	UInt32        size = 0;
	UInt32        count;
	OSStatus      stat;
	AudioDeviceID *ids;

	stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
			0, NULL, &size);
	if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
		return;

	ids   = bmalloc(size);
	count = size / sizeof(AudioDeviceID);

	stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
			0, NULL, &size, ids);

	if (enum_success(stat, "get kAudioObjectSystemObject data"))
		for (UInt32 i = 0; i < count; i++)
			if (!coreaudio_enum_device(proc, param, ids[i]))
				break;

	bfree(ids);
}

// 设备列表结构体
struct add_data {
	struct device_list *list;
	bool               input;
};

// 向音频设备列表中添加设备信息
static bool coreaudio_enum_add_device(void *param, CFStringRef cf_name,
		CFStringRef cf_uid, AudioDeviceID id)
{
	struct add_data    *data = param;
	struct device_item item;

	memset(&item, 0, sizeof(item));

	if (!cfstr_copy_dstr(cf_name, kCFStringEncodingUTF8, &item.name))
		goto fail;
	if (!cfstr_copy_dstr(cf_uid,  kCFStringEncodingUTF8, &item.value))
		goto fail;

	if (data->input || !device_is_input(item.value.array))
		device_list_add(data->list, &item);

fail:
	device_item_free(&item);

	UNUSED_PARAMETER(id);
	return true;
}

// 根据input,枚举设备,获取音频设备列表
void coreaudio_enum_devices(struct device_list *list, bool input)
{
	struct add_data data = {list, input};
	enum_devices(coreaudio_enum_add_device, &data);
}

// 设备uid和deviceid结构体
struct device_id_data {
	CFStringRef   uid;
	AudioDeviceID *id;
	bool          found;
};

// 判断当前deviceid是需要找的deviceid
static bool get_device_id(void *param, CFStringRef cf_name, CFStringRef cf_uid,
		AudioDeviceID id)
{
	struct device_id_data *data = param;

	if (CFStringCompare(cf_uid, data->uid, 0) == 0) {
		*data->id   = id;
		data->found = true;
		return false;
	}

	UNUSED_PARAMETER(cf_name);
	return true;
}

// 由设备uid获取设备id
bool coreaudio_get_device_id(CFStringRef uid, AudioDeviceID *id)
{
	struct device_id_data data = {uid, id, false};
	enum_devices(get_device_id, &data);
	return data.found;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
虽然简单,但是实用 HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i,j; int ret=0; hDevInfo = SetupDiGetClassDevs((LPGUID) &GUID;_DEVCLASS_MOUSE, 0, 0,DIGCF_PRESENT);//DIGCF_PROFILE);// /*   GUID_DEVCLASS_FDC软盘控制器   GUID_DEVCLASS_DISPLAY显示卡   GUID_DEVCLASS_CDROM光驱   GUID_DEVCLASS_KEYBOARD键盘   GUID_DEVCLASS_COMPUTER计算机   GUID_DEVCLASS_SYSTEM系统   GUID_DEVCLASS_DISKDRIVE磁盘驱动器   GUID_DEVCLASS_MEDIA声音、视频和游戏控制器   GUID_DEVCLASS_MODEMMODEM   GUID_DEVCLASS_MOUSE鼠标和其他指针设备   GUID_DEVCLASS_NET网络设备器   GUID_DEVCLASS_USB通用串行总线控制器   GUID_DEVCLASS_FLOPPYDISK软盘驱动器   GUID_DEVCLASS_UNKNOWN未知设备   GUID_DEVCLASS_SCSIADAPTERSCSI 和 RAID 控制器   GUID_DEVCLASS_HDCIDE ATA/ATAPI 控制器   GUID_DEVCLASS_PORTS端口(COM 和 LPT)   GUID_DEVCLASS_MONITOR监视器   */ if (hDevInfo == INVALID_HANDLE_VALUE){ // Insert error handling here. // return ; } // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData;);i++) { DWORD DataT; //LPTSTR buffer = NULL; char buffer[2048]; DWORD buffersize =sizeof(buffer); while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData;, SPDRP_FRIENDLYNAME, &DataT;, (PBYTE)buffer, buffersize, &buffersize;)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. //if (buffer) LocalFree(buffer); //buffer = (PSP_INF_INFORMATION)LocalAlloc(LPTR,buffersize); // <!--[if !supportEmptyParas]--><!--[endif]--> } else { // Insert error handling here. break; } } if (buffer != NULL && i == 0) { // temp.Format(""); // str += temp; } // temp.Format("%s",buffer); // str += temp; if (buffer) LocalFree(buffer); } if (i != 0) { // temp.Format(""); // str += temp; } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { return ; } // <!--[if !supportEmptyParas]--><!--[endif]--> // Cleanup SetupDiDestroyDeviceInfoList(hDevInfo);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术爱生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值