-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvideolist.h
226 lines (182 loc) · 5.42 KB
/
videolist.h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include <windows.h>
#include <vector>
#include <dshow.h>
#include <string>
#pragma comment(lib, "Strmiids.lib")
using namespace std;
const int MAX_FRIENDLY_NAME_LENGTH = 128;
const int MAX_MONIKER_NAME_LENGTH = 256;
typedef struct _TDeviceName
{
WCHAR FriendlyName[MAX_FRIENDLY_NAME_LENGTH];
WCHAR MonikerName[MAX_MONIKER_NAME_LENGTH];
} TDeviceName;
inline std::wstring Utf82Unicode(const std::string& utf8string)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[0]);
}
//unicode 转为 ascii
inline std::string WideByte2Acsi(std::wstring& wstrcode)
{
int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (asciisize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(asciisize);
int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
if (convresult != asciisize)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[0]);
}
inline std::string UTF_82ASCII(std::string& strUtf8Code)
{
std::string strRet("");
//先把 utf8 转为 unicode
std::wstring wstr = Utf82Unicode(strUtf8Code);
//最后把 unicode 转为 ascii
strRet = WideByte2Acsi(wstr);
return strRet;
}
//ascii 转 Unicode
inline std::wstring Acsi2WideByte(std::string& strascii)
{
int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[0]);
}
//Unicode 转 Utf8
inline std::string Unicode2Utf8(const std::wstring& widestring)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
if (utf8size == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(utf8size);
int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
if (convresult != utf8size)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[0]);
}
//ascii 转 Utf8
inline std::string ASCII2UTF_8(std::string& strAsciiCode)
{
std::string strRet("");
//先把 ascii 转为 unicode
std::wstring wstr = Acsi2WideByte(strAsciiCode);
//最后把 unicode 转为 utf8
strRet = Unicode2Utf8(wstr);
return strRet;
}
static 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(摄像头)
static 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;
}