-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnityMicLister.cpp
63 lines (51 loc) · 1.62 KB
/
UnityMicLister.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include "windows.h"
#include "Mmsystem.h"
#include "UnityMicLister.h"
#include <Mmdeviceapi.h>
#include <Functiondiscoverykeys_devpkey.h>
#pragma comment(lib, "strmiids")
#define EXIT_ON_ERROR(hr) \
if (FAILED(hr)) { goto Exit; }
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
UNITYMICLISTER_API HRESULT GetDefaultMicrophoneName(LPWSTR *data)
{
HRESULT hr = CoInitializeEx(NULL, COINIT::COINIT_MULTITHREADED);
bool destroyCom = SUCCEEDED(hr);
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDevice *pDevice = NULL;
IPropertyStore *pProps = NULL;
EDataFlow flow = EDataFlow::eCapture;
ERole role = ERole::eConsole;
// Get the enumerator for the audio endpoint devices on this system.
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
EXIT_ON_ERROR(hr);
// Get the audio endpoint device with the specified data-flow
// direction (eRender or eCapture) and device role.
hr = pEnumerator->GetDefaultAudioEndpoint(flow, role, &pDevice);
EXIT_ON_ERROR(hr);
hr = pDevice->OpenPropertyStore(STGM_READ, &pProps);
EXIT_ON_ERROR(hr);
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
EXIT_ON_ERROR(hr)
// This will probably leak, should fix at some point
*data = varName.pwszVal;
Exit:
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProps);
SAFE_RELEASE(pEnumerator);
if (destroyCom)
{
CoUninitialize();
}
return hr;
}