Ticket #9828: alternative_solution.cpp

File alternative_solution.cpp, 3.2 KB (added by IsraelO, 4 years ago)

Temporary solution that solves hanled not been released

Line 
1//*******PRIVATE_DIRECT_SHOW_ENUM*********
2//This overload is uses as alternative to ffmpeg's that has memory/handles leaks)
3#include <dshow.h>
4#include <comdef.h>
5#include <map>
6#include <boost/locale.hpp>
7static std::vector< std::pair<std::string, std::vector<AVMediaType>>> EnumDShowDevices_Win32API()
8{
9 boost::locale::generator gen;
10 std::locale loc = gen.generate("");// encoding local to the computer.
11
12 CoInitialize(nullptr);
13
14 // Create the System Device Enumerator.
15 ICreateDevEnum *pDevEnum;
16 HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
17 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
18
19 std::vector< std::pair<std::string, std::vector<AVMediaType>>> vec;
20 std::map<std::string, std::vector<AVMediaType>> devMap;
21
22 if (SUCCEEDED(hr))
23 {
24 IEnumMoniker *pEnum = NULL;
25 //Create an enumerator for the audio category.
26 hr = pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pEnum, 0);
27 char msg[2000] = { '\0' };
28 _com_error error(hr);
29 sprintf_s(msg, sizeof(msg), "Error 0x%08x: %s", hr, error.ErrorMessage());
30
31 if (S_OK == hr)
32 {
33 IMoniker *pMoniker = NULL;
34 while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
35 {
36 IPropertyBag *pPropBag;
37 HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
38 if (FAILED(hr))
39 {
40 pMoniker->Release();
41 continue;
42 }
43
44 VARIANT var;
45 VariantInit(&var);
46
47 // Get description or friendly name.
48 hr = pPropBag->Read(L"Description", &var, 0);
49 if (FAILED(hr))
50 hr = pPropBag->Read(L"FriendlyName", &var, 0);
51 if (SUCCEEDED(hr))
52 {
53 const std::string devName = boost::locale::conv::from_utf(var.bstrVal, loc);
54 if (devMap.end() == devMap.find(devName))
55 devMap[devName] = std::vector<AVMediaType>();
56
57 devMap[devName].push_back(AVMEDIA_TYPE_AUDIO);
58 VariantClear(&var);
59 }
60 else {
61 VariantClear(&var);
62 }
63
64 pPropBag->Release();
65 pMoniker->Release();
66 }
67
68 pEnum->Release();
69 }
70 //Enumerate the video devices
71 hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
72 if (S_OK == hr)
73 {
74 IMoniker *pMoniker = NULL;
75 while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
76 {
77 IPropertyBag *pPropBag;
78 HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
79 if (FAILED(hr))
80 {
81 pMoniker->Release();
82 continue;
83 }
84
85 VARIANT var;
86 VariantInit(&var);
87
88 // Get description or friendly name.
89 hr = pPropBag->Read(L"Description", &var, 0);
90 if (FAILED(hr))
91 hr = pPropBag->Read(L"FriendlyName", &var, 0);
92 if (SUCCEEDED(hr))
93 {
94 const std::string devName = boost::locale::conv::from_utf(var.bstrVal, loc);
95 if (devMap.end() == devMap.find(devName))
96 devMap[devName] = std::vector<AVMediaType>();
97
98 devMap[devName].push_back(AVMEDIA_TYPE_VIDEO);
99 VariantClear(&var);
100 }
101 else {
102 VariantClear(&var);
103 }
104
105 pPropBag->Release();
106 pMoniker->Release();
107 }
108
109 pEnum->Release();
110 }
111
112 pDevEnum->Release();
113 }
114
115 CoUninitialize();
116
117 for (auto e : devMap)
118 vec.push_back(std::make_pair(e.first, e.second));
119
120 return vec;
121}