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
|
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
/**
* \file ds_manager.cpp
* \brief DirectShow capture devices manager.
* \author Sebastien Vincent
* \date 2010
*/
#include <cstdlib>
#include "ds_manager.h"
#include "ds_capture_device.h"
#include "qedit.h"
/* initialization of static member variables */
DSManager* DSManager::m_instance = NULL;
DSManager* DSManager::getInstance()
{
return m_instance;
}
DSManager::DSManager()
{
DWORD ret = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if(ret != S_OK && ret != S_FALSE)
{
//seems to be a problem with COM initialization
/*
printf("problem\n");fflush(stdout);
if(ret == RPC_E_CHANGED_MODE)
{
printf("rpc\n");fflush(stdout);
}
else if(ret == E_INVALIDARG)
{
printf("invalid arg\n");fflush(stdout);
}
else if(ret == E_OUTOFMEMORY)
{
printf("outofmemory\n");fflush(stdout);
}
else if(ret == E_UNEXPECTED)
{
printf("unexpected\n");fflush(stdout);
}
*/
comInited = false;
return;
}
comInited = true;
initCaptureDevices();
}
DSManager::~DSManager()
{
for(std::list<DSCaptureDevice*>::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it)
{
delete *it;
}
m_devices.clear();
/* one CoUninitialize per CoInitialize */
if(comInited)
{
CoUninitialize();
}
}
bool DSManager::initialize()
{
if(!m_instance)
{
m_instance = new DSManager();
}
return m_instance != NULL;
}
void DSManager::destroy()
{
if(m_instance)
{
delete m_instance;
m_instance = NULL;
}
}
std::list<DSCaptureDevice*> DSManager::getDevices() const
{
return m_devices;
}
size_t DSManager::getDevicesCount()
{
return m_devices.size();
}
void DSManager::initCaptureDevices()
{
HRESULT ret = 0;
VARIANT name;
ICreateDevEnum* devEnum = NULL;
IEnumMoniker* monikerEnum = NULL;
IMoniker* moniker = NULL;
if(m_devices.size() > 0)
{
/* clean up our list in case of reinitialization */
for(std::list<DSCaptureDevice*>::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it)
{
delete *it;
}
m_devices.clear();
}
/* get the available devices list */
ret = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&devEnum);
if(FAILED(ret))
{
return;
}
ret = devEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&monikerEnum, NULL);
/* error or no devices */
if(FAILED(ret) || ret == S_FALSE)
{
devEnum->Release();
return;
}
/* loop and initialize all available capture devices */
while(monikerEnum->Next(1, &moniker, 0) == S_OK)
{
DSCaptureDevice* captureDevice = NULL;
IPropertyBag* propertyBag = NULL;
{
IBaseFilter* cp = NULL;
if(!FAILED(moniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&cp)))
{
IAMVfwCaptureDialogs* vfw = NULL;
if(!FAILED(
cp->QueryInterface(IID_IAMVfwCaptureDialogs, (void**)&vfw)))
{
if(vfw)
{
vfw->Release();
cp->Release();
continue;
}
}
}
}
/* get properties of the device */
ret = moniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyBag);
if(!FAILED(ret))
{
VariantInit(&name);
ret = propertyBag->Read(L"FriendlyName", &name, 0);
if(FAILED(ret))
{
VariantClear(&name);
propertyBag->Release();
moniker->Release();
continue;
}
/* create a new capture device */
captureDevice = new DSCaptureDevice(name.bstrVal);
/* wprintf(L"%ws\n", name.bstrVal); */
if(captureDevice && captureDevice->initDevice(moniker))
{
/* initialization success, add to the list */
m_devices.push_back(captureDevice);
}
else
{
/* printf("failed to initialize device\n"); */
delete captureDevice;
}
/* clean up */
VariantClear(&name);
propertyBag->Release();
}
moniker->Release();
}
/* cleanup */
monikerEnum->Release();
devEnum->Release();
}
|