DSCaptureUtils.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Copyright (C) 2011-2013 Doubango Telecom <http://www.doubango.org>
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. */
  18. #include "internals/DSUtils.h"
  19. #include "internals/DSCaptureUtils.h"
  20. #include <amvideo.h>
  21. #include <uuids.h>
  22. #include <mtype.h>
  23. #if defined (_WIN32_WCE)
  24. #include <atlbase.h>
  25. #include <atlstr.h>
  26. #else
  27. #include <atlconv.h>
  28. #endif
  29. #include <iostream>
  30. #include <assert.h>
  31. #include "tsk_debug.h"
  32. #if defined (_WIN32_WCE)
  33. # include "internals/wince/cpropertybag.h"
  34. #endif
  35. HRESULT enumerateCaptureDevices(const std::string &prefix, std::vector<VideoGrabberName> *names)
  36. {
  37. HRESULT hr = S_OK;
  38. #ifdef _WIN32_WCE
  39. // FIXME: use FindNextDevice to query all devices
  40. HANDLE handle = NULL;
  41. DEVMGR_DEVICE_INFORMATION di;
  42. TCHAR pwzName[MAX_PATH];
  43. memset(pwzName,NULL,MAX_PATH);
  44. GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A,
  45. 0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86
  46. }; // http://msdn.microsoft.com/en-us/library/aa918757.aspx
  47. di.dwSize = sizeof(di);
  48. for( int i=0; ; i++) {
  49. if(0 == i) {
  50. /* 1st time */
  51. handle = FindFirstDevice( DeviceSearchByGuid, &guidCamera, &di );
  52. if(!handle || !di.hDevice) {
  53. hr = ( HRESULT_FROM_WIN32( GetLastError() ));
  54. goto bail;
  55. }
  56. }
  57. else if(handle) {
  58. /* 2nd or 3rd time */
  59. BOOL ret = FindNextDevice(handle, &di);
  60. if(!ret || !di.hDevice) {
  61. /* No 2nd or 3rd camera ==> do not return error*/
  62. goto bail;
  63. }
  64. }
  65. else {
  66. assert(0);
  67. }
  68. StringCchCopy( pwzName, MAX_PATH, di.szDeviceName );
  69. /* from LPWSTR to LPSTR */
  70. char mbstr_name[MAX_PATH];
  71. memset(mbstr_name,NULL,MAX_PATH);
  72. wcstombs(mbstr_name, pwzName, MAX_PATH);
  73. VideoGrabberName grabberName(std::string((const char*)mbstr_name), std::string((const char*)mbstr_name));
  74. names->push_back(grabberName);
  75. }
  76. bail:
  77. /* close */
  78. if(handle) {
  79. FindClose( handle );
  80. }
  81. #else
  82. ICreateDevEnum *deviceEnum;
  83. IEnumMoniker *enumerator;
  84. IMoniker *moniker;
  85. // Create the System Device Enumerator
  86. hr = COCREATE(CLSID_SystemDeviceEnum, IID_ICreateDevEnum, deviceEnum);
  87. if (FAILED(hr)) {
  88. goto bail;
  89. }
  90. // Ask for a device enumerator
  91. hr = deviceEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &enumerator, INCLUDE_CATEGORY_FLAG);
  92. if (FAILED(hr)) {
  93. goto bail;
  94. }
  95. // hr = S_FALSE and enumerator is NULL if there is no device to enumerate
  96. if (!enumerator) {
  97. goto bail;
  98. }
  99. USES_CONVERSION;
  100. while (enumerator->Next(1, &moniker, NULL) == S_OK) {
  101. // Get the properties bag for each device
  102. IPropertyBag *propBag;
  103. hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, reinterpret_cast<void**>(&propBag));
  104. if (FAILED(hr)) {
  105. SAFE_RELEASE(moniker);
  106. continue;
  107. }
  108. std::string name;
  109. std::string description;
  110. VARIANT varName;
  111. VariantInit(&varName);
  112. VARIANT varDescription;
  113. VariantInit(&varDescription);
  114. // Find the device path (uniqueness is guaranteed)
  115. hr = propBag->Read(L"DevicePath", &varName, 0);
  116. if (SUCCEEDED(hr)) {
  117. if (prefix != "") {
  118. name = prefix + ":";
  119. }
  120. name = name + std::string(W2A(varName.bstrVal));
  121. }
  122. // Find friendly name or the description
  123. hr = propBag->Read(L"FriendlyName", &varDescription, 0);
  124. if (SUCCEEDED(hr)) {
  125. description = std::string(W2A(varDescription.bstrVal));
  126. }
  127. else {
  128. hr = propBag->Read(L"Description", &varDescription, 0);
  129. if (SUCCEEDED(hr)) {
  130. description = std::string(W2A(varDescription.bstrVal));
  131. }
  132. }
  133. hr = VariantClear(&varName);
  134. hr = VariantClear(&varDescription);
  135. SAFE_RELEASE(propBag);
  136. SAFE_RELEASE(moniker);
  137. // Add it to the list
  138. if (name != "") {
  139. VideoGrabberName grabberName(name, description);
  140. names->push_back(grabberName);
  141. }
  142. }
  143. bail:
  144. SAFE_RELEASE(enumerator);
  145. SAFE_RELEASE(deviceEnum);
  146. #endif
  147. return hr;
  148. }
  149. HRESULT createSourceFilter(std::string *devicePath, IBaseFilter **sourceFilter)
  150. {
  151. HRESULT hr;
  152. IEnumMoniker *enumerator = NULL;
  153. IMoniker *moniker = NULL;
  154. bool found = false;
  155. // Set sourceFilter to null
  156. SAFE_RELEASE((*sourceFilter));
  157. #if defined( _WIN32_WCE)
  158. CPropertyBag pBag;
  159. HANDLE handle = NULL;
  160. DEVMGR_DEVICE_INFORMATION di;
  161. TCHAR pwzName[MAX_PATH];
  162. CComVariant varCamName;
  163. IPersistPropertyBag *propBag = NULL;
  164. GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A,
  165. 0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86
  166. }; // http://msdn.microsoft.com/en-us/library/aa918757.aspx
  167. di.dwSize = sizeof(di);
  168. for( int i=0; ; i++) {
  169. if(0 == i) {
  170. /* 1st time */
  171. handle = FindFirstDevice( DeviceSearchByGuid, &guidCamera, &di );
  172. if(!handle || !di.hDevice) {
  173. hr = ( HRESULT_FROM_WIN32( GetLastError() ));
  174. goto bail;
  175. }
  176. }
  177. else if(handle) {
  178. /* 2nd or 3rd time */
  179. BOOL ret = FindNextDevice(handle, &di);
  180. if(!ret || !di.hDevice) {
  181. /* No 2nd or 3rd camera ==> do not return error*/
  182. goto bail;
  183. }
  184. }
  185. else {
  186. assert(0);
  187. }
  188. StringCchCopy( pwzName, MAX_PATH, di.szDeviceName );
  189. /* from LPWSTR to LPSTR */
  190. char mbstr_name[MAX_PATH];
  191. memset(mbstr_name,NULL,MAX_PATH);
  192. wcstombs(mbstr_name, pwzName, MAX_PATH);
  193. if((std::string((const char*)mbstr_name) == (*devicePath)) || ("0" == (*devicePath))) {
  194. varCamName = pwzName;
  195. if( varCamName.vt != VT_BSTR ) {
  196. hr = E_OUTOFMEMORY;
  197. goto bail;
  198. }
  199. // Create Source filter
  200. hr = COCREATE(CLSID_VideoCapture, IID_IBaseFilter, *sourceFilter);
  201. if(FAILED(hr)) {
  202. goto bail;
  203. }
  204. // Query PropertyBag
  205. hr = QUERY((*sourceFilter), IID_IPersistPropertyBag, propBag);
  206. if(FAILED(hr)) {
  207. goto bail;
  208. }
  209. hr = pBag.Write( L"VCapName", &varCamName );
  210. if(FAILED(hr)) {
  211. goto bail;
  212. }
  213. hr = propBag->Load( &pBag, NULL );
  214. if(FAILED(hr)) {
  215. goto bail;
  216. }
  217. }
  218. }
  219. #else
  220. ICreateDevEnum *deviceEnum = NULL;
  221. IPropertyBag *propBag = NULL;
  222. // Create the System Device Enumerator
  223. hr = COCREATE(CLSID_SystemDeviceEnum, IID_ICreateDevEnum, deviceEnum);
  224. if (FAILED(hr)) {
  225. goto bail;
  226. }
  227. // Ask for a device enumerator
  228. hr = deviceEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &enumerator, INCLUDE_CATEGORY_FLAG);
  229. if(FAILED(hr)) {
  230. goto bail;
  231. }
  232. // hr = S_FALSE and enumerator is NULL if there is no device to enumerate
  233. if(!enumerator) {
  234. goto bail;
  235. }
  236. USES_CONVERSION;
  237. while (!found && (enumerator->Next(1, &moniker, NULL) == S_OK)) {
  238. // Get the properties bag for each device
  239. hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, reinterpret_cast<void**>(&propBag));
  240. if (FAILED(hr)) {
  241. SAFE_RELEASE(moniker);
  242. continue;
  243. }
  244. std::string name;
  245. VARIANT varName;
  246. VariantInit(&varName);
  247. // Find the device path (uniqueness is guaranteed)
  248. hr = propBag->Read(L"DevicePath", &varName, 0);
  249. if (SUCCEEDED(hr)) {
  250. name = std::string(W2A(varName.bstrVal));
  251. }
  252. // Check for device path
  253. // "Null" means first found
  254. if ((name == (*devicePath)) ||
  255. ("Null" == (*devicePath))) {
  256. hr = moniker->BindToObject(0, 0, IID_IBaseFilter, reinterpret_cast<void**>(&(*sourceFilter)));
  257. if (SUCCEEDED(hr)) {
  258. (*devicePath) = name;
  259. found = true;
  260. }
  261. }
  262. hr = VariantClear(&varName);
  263. SAFE_RELEASE(propBag);
  264. SAFE_RELEASE(moniker);
  265. }
  266. #endif
  267. bail:
  268. #ifdef _WIN32_WCE
  269. if(handle) {
  270. FindClose(handle);
  271. }
  272. #else
  273. SAFE_RELEASE(deviceEnum);
  274. #endif
  275. SAFE_RELEASE(moniker);
  276. SAFE_RELEASE(enumerator);
  277. SAFE_RELEASE(propBag);
  278. return hr;
  279. }
  280. HRESULT getSupportedFormats(IBaseFilter *sourceFilter, std::vector<DSCaptureFormat> *formats)
  281. {
  282. HRESULT hr = E_FAIL;
  283. IPin *pinOut = NULL;
  284. IAMStreamConfig *streamConfig = NULL;
  285. AM_MEDIA_TYPE *mediaType = NULL;
  286. int count, size;
  287. // Check source filter pointer
  288. if (!sourceFilter) {
  289. goto bail;
  290. }
  291. pinOut = GetPin(sourceFilter, PINDIR_OUTPUT);
  292. if(!pinOut) {
  293. goto bail;
  294. }
  295. // Retrieve the stream config interface
  296. hr = QUERY(pinOut, IID_IAMStreamConfig, streamConfig);
  297. if (FAILED(hr)) {
  298. goto bail;
  299. }
  300. // Get the number of capabilities
  301. hr = streamConfig->GetNumberOfCapabilities(&count, &size);
  302. if (FAILED(hr)) {
  303. goto bail;
  304. }
  305. hr = streamConfig->GetFormat(&mediaType);
  306. if (FAILED(hr)) {
  307. goto bail;
  308. }
  309. // Iterate through the formats
  310. for (int i = 0; i < count; i++) {
  311. VIDEO_STREAM_CONFIG_CAPS streamConfigCaps;
  312. hr = streamConfig->GetStreamCaps(i, &mediaType, reinterpret_cast<BYTE*>(&streamConfigCaps));
  313. if (FAILED(hr)) {
  314. TSK_DEBUG_ERROR("Failed to get Stream caps");
  315. break;
  316. }
  317. if (streamConfigCaps.guid == FORMAT_VideoInfo) {
  318. VIDEOINFOHEADER* vih = reinterpret_cast<VIDEOINFOHEADER*>(mediaType->pbFormat);
  319. BITMAPINFOHEADER* bih = &vih->bmiHeader;
  320. int width = abs(bih->biWidth);
  321. int height = abs(bih->biHeight);
  322. int fps = (int) ((float)(vih->AvgTimePerFrame)/10000.f);
  323. GUID chroma = mediaType->subtype;
  324. // Add format to the list
  325. DSCaptureFormat format(width, height, fps, chroma);
  326. formats->push_back(format);
  327. }
  328. DeleteMediaType(mediaType);
  329. }
  330. bail:
  331. SAFE_RELEASE(streamConfig);
  332. SAFE_RELEASE(pinOut);
  333. return hr;
  334. }