summaryrefslogtreecommitdiffstats
path: root/plugins/pluginDirectShow/internals/DSCaptureUtils.cxx
blob: d95f9968ae41f52d8eabf0ab71fb86a44f3fa550 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Copyright (C) 2011-2013 Doubango Telecom <http://www.doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*/
#include "internals/DSUtils.h"
#include "internals/DSCaptureUtils.h"
#include <amvideo.h>
#include <uuids.h>
#include <mtype.h>

#if defined (_WIN32_WCE)
#include <atlbase.h>
#include <atlstr.h>
#else
#include <atlconv.h>
#endif
#include <iostream>
#include <assert.h>

#include "tsk_debug.h"

#if defined (_WIN32_WCE)
#	include "internals/wince/cpropertybag.h"
#endif

HRESULT enumerateCaptureDevices(const std::string &prefix, std::vector<VideoGrabberName> *names)
{
	HRESULT hr = S_OK;

#ifdef _WIN32_WCE

	// FIXME: use FindNextDevice to query all devices
	HANDLE	handle = NULL;
	DEVMGR_DEVICE_INFORMATION di;

	TCHAR pwzName[MAX_PATH]; memset(pwzName,NULL,MAX_PATH);
	
	GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A,
                      0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86 }; // http://msdn.microsoft.com/en-us/library/aa918757.aspx

	di.dwSize = sizeof(di);
	
	for( int i=0; ; i++)
	{
		if(0 == i)
		{	/* 1st time */
			handle = FindFirstDevice( DeviceSearchByGuid, &guidCamera, &di );
			if(!handle || !di.hDevice)
			{
				hr = ( HRESULT_FROM_WIN32( GetLastError() ));
				goto bail;
			}
		}
		else if(handle)
		{	/* 2nd or 3rd time */ 
			BOOL ret = FindNextDevice(handle, &di);
			if(!ret || !di.hDevice)
			{
				/* No 2nd or 3rd camera ==> do not return error*/
				goto bail;
			}
		}
		else assert(0);
		
		StringCchCopy( pwzName, MAX_PATH, di.szDeviceName );

		/* from LPWSTR to LPSTR */
		char mbstr_name[MAX_PATH]; memset(mbstr_name,NULL,MAX_PATH);
		wcstombs(mbstr_name, pwzName, MAX_PATH);

		VideoGrabberName grabberName(std::string((const char*)mbstr_name), std::string((const char*)mbstr_name));
		names->push_back(grabberName);
	}
	
bail:
	/* close */
	if(handle) FindClose( handle );
	
#else
	ICreateDevEnum *deviceEnum;
	IEnumMoniker *enumerator;
	IMoniker *moniker;

	// Create the System Device Enumerator
	hr = COCREATE(CLSID_SystemDeviceEnum, IID_ICreateDevEnum, deviceEnum);
	if (FAILED(hr)) goto bail;

	// Ask for a device enumerator
	hr = deviceEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &enumerator, INCLUDE_CATEGORY_FLAG);
	if (FAILED(hr)) goto bail;

	// hr = S_FALSE and enumerator is NULL if there is no device to enumerate
	if (!enumerator) goto bail;

	USES_CONVERSION;

	while (enumerator->Next(1, &moniker, NULL) == S_OK)
	{
		// Get the properties bag for each device
		IPropertyBag *propBag;
		hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, reinterpret_cast<void**>(&propBag));
		if (FAILED(hr))
		{
			SAFE_RELEASE(moniker);
			continue;
		}

		std::string name;
		std::string description;

		VARIANT varName;
		VariantInit(&varName);
		VARIANT varDescription;
		VariantInit(&varDescription);

		// Find the device path (uniqueness is guaranteed)
		hr = propBag->Read(L"DevicePath", &varName, 0);
		if (SUCCEEDED(hr)) 
		{
			if (prefix != "") name = prefix + ":";
			name = name + std::string(W2A(varName.bstrVal));
		}

		// Find friendly name or the description
		hr = propBag->Read(L"FriendlyName", &varDescription, 0);
		if (SUCCEEDED(hr))
		{
			description = std::string(W2A(varDescription.bstrVal));
		}
		else
		{
			hr = propBag->Read(L"Description", &varDescription, 0);
			if (SUCCEEDED(hr)) description = std::string(W2A(varDescription.bstrVal));
		}

		hr = VariantClear(&varName);
		hr = VariantClear(&varDescription);

		SAFE_RELEASE(propBag);
		SAFE_RELEASE(moniker);

		// Add it to the list
		if (name != "")
		{
			VideoGrabberName grabberName(name, description);
			names->push_back(grabberName);
		}
	}

bail:
	SAFE_RELEASE(enumerator);
	SAFE_RELEASE(deviceEnum);
#endif

	return hr;
}

HRESULT createSourceFilter(std::string *devicePath, IBaseFilter **sourceFilter)
{
	HRESULT hr;
	
	IEnumMoniker *enumerator = NULL;
	IMoniker *moniker = NULL;
	bool found = false;

	// Set sourceFilter to null
	SAFE_RELEASE((*sourceFilter));

#if defined( _WIN32_WCE)
	CPropertyBag  pBag;
	HANDLE	handle = NULL;
	DEVMGR_DEVICE_INFORMATION di;
	TCHAR pwzName[MAX_PATH];
	CComVariant varCamName;
	IPersistPropertyBag *propBag = NULL;
	GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A,
                      0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86 }; // http://msdn.microsoft.com/en-us/library/aa918757.aspx

	di.dwSize = sizeof(di);

	for( int i=0; ; i++)
	{
		if(0 == i)
		{	/* 1st time */
			handle = FindFirstDevice( DeviceSearchByGuid, &guidCamera, &di );
			if(!handle || !di.hDevice)
			{
				hr = ( HRESULT_FROM_WIN32( GetLastError() ));
				goto bail;
			}
		}
		else if(handle)
		{	/* 2nd or 3rd time */ 
			BOOL ret = FindNextDevice(handle, &di);
			if(!ret || !di.hDevice)
			{
				/* No 2nd or 3rd camera ==> do not return error*/
				goto bail;
			}
		}
		else assert(0);
		
		StringCchCopy( pwzName, MAX_PATH, di.szDeviceName );

		/* from LPWSTR to LPSTR */
		char mbstr_name[MAX_PATH];
		memset(mbstr_name,NULL,MAX_PATH); 
		wcstombs(mbstr_name, pwzName, MAX_PATH);

		if((std::string((const char*)mbstr_name) == (*devicePath)) || ("0" == (*devicePath)))
		{
			varCamName = pwzName;
			if( varCamName.vt != VT_BSTR )
			{
				hr = E_OUTOFMEMORY;
				goto bail;
			}
			
			// Create Source filter
			hr = COCREATE(CLSID_VideoCapture, IID_IBaseFilter, *sourceFilter);
			if(FAILED(hr)) goto bail;
			
			// Query PropertyBag
			hr = QUERY((*sourceFilter), IID_IPersistPropertyBag, propBag);
			if(FAILED(hr)) goto bail;
			
			hr = pBag.Write( L"VCapName", &varCamName );
			if(FAILED(hr)) goto bail;
			
			hr = propBag->Load( &pBag, NULL );
			if(FAILED(hr)) goto bail;
		}
	}
#else
	ICreateDevEnum *deviceEnum = NULL;
	IPropertyBag *propBag = NULL;

	// Create the System Device Enumerator
	hr = COCREATE(CLSID_SystemDeviceEnum, IID_ICreateDevEnum, deviceEnum);
	if (FAILED(hr)){
		goto bail;
	}

	// Ask for a device enumerator
	hr = deviceEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &enumerator, INCLUDE_CATEGORY_FLAG);
	if(FAILED(hr)){
		goto bail;
	}

	// hr = S_FALSE and enumerator is NULL if there is no device to enumerate
	if(!enumerator){
		goto bail;
	}

	USES_CONVERSION;

	while (!found && (enumerator->Next(1, &moniker, NULL) == S_OK)){
		// Get the properties bag for each device
		hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, reinterpret_cast<void**>(&propBag));
		if (FAILED(hr)){
			SAFE_RELEASE(moniker);
			continue;
		}

		std::string name;

		VARIANT varName;
		VariantInit(&varName);

		// Find the device path (uniqueness is guaranteed)
		hr = propBag->Read(L"DevicePath", &varName, 0);
		if (SUCCEEDED(hr)) name = std::string(W2A(varName.bstrVal));

		// Check for device path
		// "Null" means first found
		if ((name == (*devicePath)) ||
			("Null" == (*devicePath)))
		{
			hr = moniker->BindToObject(0, 0, IID_IBaseFilter, reinterpret_cast<void**>(&(*sourceFilter)));
			if (SUCCEEDED(hr)){
				(*devicePath) = name;
				found = true;
			}
		}

		hr = VariantClear(&varName);

		SAFE_RELEASE(propBag);
		SAFE_RELEASE(moniker);
	}
#endif

bail:
#ifdef _WIN32_WCE
	if(handle) FindClose(handle);
#else
	SAFE_RELEASE(deviceEnum);
#endif
	SAFE_RELEASE(moniker);
	SAFE_RELEASE(enumerator);
	SAFE_RELEASE(propBag);

	return hr;
}

HRESULT getSupportedFormats(IBaseFilter *sourceFilter, std::vector<DSCaptureFormat> *formats)
{
	HRESULT hr = E_FAIL;
	IPin *pinOut = NULL;
	IAMStreamConfig *streamConfig = NULL;
	AM_MEDIA_TYPE *mediaType = NULL;
	int count, size;

	// Check source filter pointer
	if (!sourceFilter) goto bail;

	pinOut = GetPin(sourceFilter, PINDIR_OUTPUT);
	if(!pinOut) goto bail;

	// Retrieve the stream config interface
	hr = QUERY(pinOut, IID_IAMStreamConfig, streamConfig);
	if (FAILED(hr)) goto bail;

	// Get the number of capabilities
	hr = streamConfig->GetNumberOfCapabilities(&count, &size);
	if (FAILED(hr)) goto bail;

	hr = streamConfig->GetFormat(&mediaType);
	if (FAILED(hr)) goto bail;

	// Iterate through the formats
	for (int i = 0; i < count; i++){
		VIDEO_STREAM_CONFIG_CAPS streamConfigCaps;

		hr = streamConfig->GetStreamCaps(i, &mediaType, reinterpret_cast<BYTE*>(&streamConfigCaps));

		if (FAILED(hr)){
			TSK_DEBUG_ERROR("Failed to get Stream caps");
			break;
		}

		if (streamConfigCaps.guid == FORMAT_VideoInfo){
			VIDEOINFOHEADER* vih = reinterpret_cast<VIDEOINFOHEADER*>(mediaType->pbFormat);
			BITMAPINFOHEADER* bih = &vih->bmiHeader;

			int width = abs(bih->biWidth);
			int height = abs(bih->biHeight);
			int fps = (int) ((float)(vih->AvgTimePerFrame)/10000.f);
			GUID chroma = mediaType->subtype;

			// Add format to the list
			DSCaptureFormat format(width, height, fps, chroma);
			formats->push_back(format);
		}

		DeleteMediaType(mediaType);
	}

bail:
	SAFE_RELEASE(streamConfig);
	SAFE_RELEASE(pinOut);

	return hr;
}
OpenPOWER on IntegriCloud