-
Notifications
You must be signed in to change notification settings - Fork 226
/
DeviceManager.cpp
156 lines (130 loc) · 4.58 KB
/
DeviceManager.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//--------------------------------------------------------------------------------------
// DeviceManager.cpp
//
// Wraps the underlying IMMNotificationClient to manage capture devices
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "DeviceManager.h"
using namespace Microsoft::WRL;
using namespace Windows::Media::Devices;
//A couple of example GUIDs
#define USER1HEADSETGUID L"{0.0.1.00000000}.{007F00A1-0014-004C-9E00-B80084006F00}"
#define KINECTGUID L"{0.0.1.00000000}.{001D0054-00E7-0013-4C00-4C000C00D400}"
DeviceManager::DeviceManager() :
_cRef(1),
m_CaptureIndex( 0 )
{
ComPtr<IMMDeviceCollection> deviceCollectionInterface;
CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&m_deviceEnum);
if (!InitializeCriticalSectionEx( &m_CritSec, 0, 0 ))
{
return;
}
UpdateCaptureDeviceList();
}
DeviceManager::~DeviceManager()
{
DeleteCriticalSection( &m_CritSec );
}
HRESULT STDMETHODCALLTYPE DeviceManager::OnDefaultDeviceChanged(
EDataFlow flow, ERole role,
LPCWSTR pwstrDeviceId)
{
UNREFERENCED_PARAMETER(flow);
UNREFERENCED_PARAMETER(role);
UNREFERENCED_PARAMETER(pwstrDeviceId);
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceManager::OnDeviceAdded(LPCWSTR pwstrDeviceId)
{
//Occurs when we plug in a new device
UNREFERENCED_PARAMETER(pwstrDeviceId);
UpdateCaptureDeviceList();
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceManager::OnDeviceRemoved(LPCWSTR pwstrDeviceId)
{
//Occurs when we unplug a device
UNREFERENCED_PARAMETER(pwstrDeviceId);
UpdateCaptureDeviceList();
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceManager::OnDeviceStateChanged(
LPCWSTR pwstrDeviceId,
DWORD dwNewState)
{
UNREFERENCED_PARAMETER(pwstrDeviceId);
UNREFERENCED_PARAMETER(dwNewState);
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceManager::OnPropertyValueChanged(
LPCWSTR pwstrDeviceId,
const PROPERTYKEY key)
{
UNREFERENCED_PARAMETER(pwstrDeviceId);
UNREFERENCED_PARAMETER(key);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Name: UpdateCaptureDeviceList
// Desc: Updates the list of capture devices
//--------------------------------------------------------------------------------------
void DeviceManager::UpdateCaptureDeviceList()
{
EnterCriticalSection( &m_CritSec );
ComPtr<IMMDeviceCollection> deviceCollectionInterface;
HRESULT hr = S_OK;
std::wstring currentID = L"";
//Save the ID of current capture device incase its index changes
if(m_CaptureDevices.size() > 0)
{
currentID = m_CaptureDevices.at(m_CaptureIndex);
}
if (m_deviceEnum)
{
hr = m_deviceEnum->EnumAudioEndpoints(eCapture, DEVICE_STATEMASK_ALL, &deviceCollectionInterface);
if (SUCCEEDED( hr ))
{
ComPtr<IMMDevice> deviceInterface;
UINT iDeviceCount = 0;
m_CaptureDevices.clear();
hr = deviceCollectionInterface->GetCount(&iDeviceCount);
for(UINT i=0; SUCCEEDED( hr ) && i<iDeviceCount; i++)
{
//Go through each device and save the ID
hr = deviceCollectionInterface->Item(i, &deviceInterface);
if (SUCCEEDED( hr ))
{
LPWSTR deviceId;
deviceInterface->GetId(&deviceId);
if(wcscmp(deviceId, KINECTGUID) == 0)
{
m_CaptureDevices.push_back(L"Kinect (16khz)");
}
else if(wcscmp(deviceId, USER1HEADSETGUID) == 0)
{
m_CaptureDevices.push_back(L"User 1 Headset (24khz)");
}
else
{
m_CaptureDevices.push_back(deviceId);
}
if(currentID.compare(deviceId) == 0)
{
//Make sure index is correct based on the ID string
m_CaptureIndex = i;
}
}
}
if(m_deviceChangeFunc)
{
//Report back that a change has been made to the device list
m_deviceChangeFunc(m_CaptureIndex);
}
}
}
LeaveCriticalSection( &m_CritSec );
}