DSCaptureGraph.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/DSCaptureGraph.h"
  19. #include "internals/DSUtils.h"
  20. #include "internals/DSCaptureUtils.h"
  21. #include "tsk_debug.h"
  22. #include <iostream>
  23. using namespace std;
  24. #ifdef _WIN32_WCE
  25. DSCaptureGraph::DSCaptureGraph(DSISampleGrabberCB* callback, HRESULT *hr)
  26. : DSBaseCaptureGraph(callback, hr)
  27. #else
  28. DSCaptureGraph::DSCaptureGraph(ISampleGrabberCB* callback, HRESULT *hr)
  29. : DSBaseCaptureGraph(callback, hr)
  30. #endif
  31. {
  32. this->grabberCallback = callback;
  33. this->captureFormat = NULL;
  34. this->captureGraphBuilder = NULL;
  35. this->graphBuilder = NULL;
  36. this->sourceFilter = NULL;
  37. this->sampleGrabberFilter = NULL;
  38. #ifdef _WIN32_WCE
  39. this->colorConvertor565 = NULL;
  40. #else
  41. this->frameRateFilter = NULL;
  42. #endif
  43. this->nullRendererFilter = NULL;
  44. this->grabberController = NULL;
  45. this->mediaController = NULL;
  46. this->mediaEventController = NULL;
  47. this->streamConfiguration = NULL;
  48. this->running = FALSE;
  49. this->paused = FALSE;
  50. this->deviceId = "";
  51. *hr = this->createCaptureGraph();
  52. }
  53. DSCaptureGraph::~DSCaptureGraph()
  54. {
  55. SAFE_RELEASE(this->streamConfiguration);
  56. SAFE_RELEASE(this->mediaEventController);
  57. SAFE_RELEASE(this->mediaController);
  58. SAFE_RELEASE(this->grabberController);
  59. #if defined(_WIN32_WCE)
  60. SAFE_RELEASE(this->colorConvertor565);
  61. #else
  62. #endif
  63. SAFE_RELEASE(this->nullRendererFilter);
  64. SAFE_RELEASE(this->sampleGrabberFilter);
  65. SAFE_RELEASE(this->sourceFilter);
  66. SAFE_RELEASE(this->graphBuilder);
  67. SAFE_RELEASE(this->captureGraphBuilder);
  68. }
  69. HRESULT DSCaptureGraph::setSource(const std::string &devicePath)
  70. {
  71. HRESULT hr = E_FAIL;
  72. if (this->sourceFilter) {
  73. this->graphBuilder->RemoveFilter(this->sourceFilter);
  74. }
  75. SAFE_RELEASE(this->streamConfiguration);
  76. SAFE_RELEASE(this->sourceFilter);
  77. // Create the filter
  78. this->deviceId = devicePath;
  79. hr = createSourceFilter(&this->deviceId, &this->sourceFilter);
  80. if (this->sourceFilter) {
  81. // Gets the supported formats
  82. this->supportedFormats.clear();
  83. getSupportedFormats(this->sourceFilter, &this->supportedFormats);
  84. // Query for video stream config
  85. hr = this->captureGraphBuilder->FindInterface(
  86. &PIN_CATEGORY_CAPTURE,
  87. &MEDIATYPE_Video,
  88. this->sourceFilter,
  89. IID_IAMStreamConfig,
  90. reinterpret_cast<void**>(&this->streamConfiguration));
  91. hr = this->graphBuilder->AddFilter(this->sourceFilter, FILTER_WEBCAM);
  92. }
  93. return hr;
  94. }
  95. HRESULT DSCaptureGraph::setParameters(DSCaptureFormat *format, int framerate)
  96. {
  97. HRESULT hr = E_FAIL;
  98. AM_MEDIA_TYPE *mediaType = NULL;
  99. if (!this->streamConfiguration) {
  100. goto bail;
  101. }
  102. hr = this->streamConfiguration->GetFormat(&mediaType);
  103. if (FAILED(hr)) {
  104. goto bail;
  105. }
  106. VIDEOINFOHEADER* vih = reinterpret_cast<VIDEOINFOHEADER*>(mediaType->pbFormat);
  107. BITMAPINFOHEADER* bih = &vih->bmiHeader;
  108. int w = format->getWidth();
  109. int h = format->getHeight();
  110. bool wN = (bih->biWidth<0);
  111. bool hN = (bih->biHeight<0);
  112. // DIBS are DWORD aligned
  113. int data_size = h * ((w * bih->biBitCount + 31) / 32) * 4;
  114. bih->biSize = sizeof(BITMAPINFOHEADER);
  115. bih->biWidth = w*(wN?-1:1);
  116. bih->biHeight = h*(hN?-1:1);
  117. bih->biSizeImage = data_size;
  118. //vih->dwBitRate = framerate * data_size;
  119. //vih->AvgTimePerFrame = SECONDS_TO_100NS(framerate);
  120. mediaType->cbFormat = sizeof(VIDEOINFOHEADER);
  121. //mediaType->lSampleSize = data_size;
  122. mediaType->subtype = format->getChroma();
  123. hr = this->streamConfiguration->SetFormat(mediaType);
  124. if (FAILED(hr)) {
  125. goto bail;
  126. }
  127. #if defined(_WIN32_WCE)
  128. hr = this->grabberController->SetFps((int) DS_SECONDS_FROM_100NS(vih->AvgTimePerFrame)/*format->getFramerate()*/, framerate);
  129. if (FAILED(hr)) {
  130. goto bail;
  131. }
  132. hr = this->grabberController->SetSize(w,h);
  133. #else
  134. // Set fps using tdshow filter
  135. hr = this->frameRateFilter->SetFps((int) ((float)vih->AvgTimePerFrame/10000.f)/*format->getFramerate()*/, framerate);
  136. #endif
  137. if (FAILED(hr)) {
  138. goto bail;
  139. }
  140. this->captureFormat = format;
  141. bail:
  142. DeleteMediaType(mediaType);
  143. return hr;
  144. }
  145. #if defined(_WIN32_WCE)
  146. # include "internals/wince/DSNullFilter.h"
  147. #endif
  148. HRESULT DSCaptureGraph::connect()
  149. {
  150. HRESULT hr;
  151. if (!this->sourceFilter) {
  152. TSK_DEBUG_ERROR("Invalid source filter");
  153. return E_FAIL;
  154. }
  155. if (!this->captureFormat) {
  156. TSK_DEBUG_ERROR("Invalid capture format");
  157. return E_FAIL;
  158. }
  159. if (!this->graphBuilder) {
  160. TSK_DEBUG_ERROR("Invalid grash builder");
  161. return E_FAIL;
  162. }
  163. if (this->captureFormat->isRGB()) {
  164. #if defined(_WIN32_WCE)
  165. hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->colorConvertor565) ;
  166. if(FAILED(hr)) {
  167. TSK_DEBUG_ERROR("ConnectFilters failed");
  168. return hr;
  169. }
  170. hr = ConnectFilters(this->graphBuilder, this->colorConvertor565, this->sampleGrabberFilter) ;
  171. if(FAILED(hr)) {
  172. TSK_DEBUG_ERROR("ConnectFilters failed");
  173. return hr;
  174. }
  175. hr = ConnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  176. if(FAILED(hr)) {
  177. TSK_DEBUG_ERROR("ConnectFilters failed");
  178. return hr;
  179. }
  180. #else
  181. hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->frameRateFilter);
  182. if(FAILED(hr)) {
  183. TSK_DEBUG_ERROR("ConnectFilters failed");
  184. return hr;
  185. }
  186. hr = ConnectFilters(this->graphBuilder, this->frameRateFilter, this->sampleGrabberFilter);
  187. if(FAILED(hr)) {
  188. TSK_DEBUG_ERROR("ConnectFilters failed");
  189. return hr;
  190. }
  191. hr = ConnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  192. if(FAILED(hr)) {
  193. TSK_DEBUG_ERROR("ConnectFilters failed");
  194. return hr;
  195. }
  196. #endif
  197. }
  198. else {
  199. #if defined(_WIN32_WCE)
  200. hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->colorConvertor565) ;
  201. if(FAILED(hr)) {
  202. return hr;
  203. }
  204. hr = ConnectFilters(this->graphBuilder, this->colorConvertor565, this->sampleGrabberFilter) ;
  205. if(FAILED(hr)) {
  206. return hr;
  207. }
  208. hr = ConnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  209. if(FAILED(hr)) {
  210. return hr;
  211. }
  212. #else
  213. // No convertor needed
  214. // AVI Decompressor Filter is automatically by the Filter Graph Manager when needed
  215. hr = ConnectFilters(this->graphBuilder, this->sourceFilter, this->frameRateFilter);
  216. if(FAILED(hr)) {
  217. TSK_DEBUG_ERROR("ConnectFilters failed");
  218. return hr;
  219. }
  220. hr = ConnectFilters(this->graphBuilder, this->frameRateFilter, this->sampleGrabberFilter);
  221. if(FAILED(hr)) {
  222. TSK_DEBUG_ERROR("ConnectFilters failed");
  223. return hr;
  224. }
  225. hr = ConnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  226. if(FAILED(hr)) {
  227. TSK_DEBUG_ERROR("ConnectFilters failed");
  228. return hr;
  229. }
  230. #endif
  231. }
  232. return hr;
  233. }
  234. HRESULT DSCaptureGraph::disconnect()
  235. {
  236. HRESULT hr;
  237. if (!this->sourceFilter) {
  238. return E_FAIL;
  239. }
  240. if (!this->captureFormat) {
  241. return E_FAIL;
  242. }
  243. if (this->captureFormat->isRGB()) {
  244. #if defined(_WIN32_WCE)
  245. hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->colorConvertor565);
  246. hr = DisconnectFilters(this->graphBuilder, this->colorConvertor565, this->sampleGrabberFilter);
  247. hr = DisconnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  248. #else
  249. hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->frameRateFilter);
  250. hr = DisconnectFilters(this->graphBuilder, this->frameRateFilter, this->sampleGrabberFilter);
  251. hr = DisconnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  252. #endif
  253. }
  254. else {
  255. #if defined(_WIN32_WCE)
  256. hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->colorConvertor565);
  257. if(FAILED(hr)) {
  258. return hr;
  259. }
  260. hr = DisconnectFilters(this->graphBuilder, this->colorConvertor565, this->sampleGrabberFilter);
  261. if(FAILED(hr)) {
  262. return hr;
  263. }
  264. hr = DisconnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  265. if(FAILED(hr)) {
  266. return hr;
  267. }
  268. #else
  269. hr = DisconnectFilters(this->graphBuilder, this->sourceFilter, this->frameRateFilter);
  270. hr = DisconnectFilters(this->graphBuilder, this->frameRateFilter, this->sampleGrabberFilter);
  271. hr = DisconnectFilters(this->graphBuilder, this->sampleGrabberFilter, this->nullRendererFilter);
  272. #endif
  273. }
  274. return hr;
  275. }
  276. HRESULT DSCaptureGraph::start()
  277. {
  278. HRESULT hr;
  279. if (isRunning() && !isPaused()) {
  280. return S_OK;
  281. }
  282. //this->mediaController->Stop();
  283. hr = this->mediaController ? this->mediaController->Run() : E_POINTER;
  284. /*if (hr == S_FALSE)
  285. {
  286. cerr << "DSCaptureGraph::mediaController->Start() has failed with " << hr << ". Waiting for transition." << endl;
  287. FILTER_STATE pfs;
  288. hr = this->mediaController->GetState(2500, (OAFilterState*) &pfs);
  289. hr = this->mediaController->Run();
  290. }*/
  291. if (!SUCCEEDED(hr)) {
  292. #if defined(_WIN32_WCE)
  293. MessageBox(NULL, _T("Starting DirectShow Graph Failed"), _T("Failure"), MB_OK);
  294. //assert(1==15);
  295. #endif
  296. TSK_DEBUG_ERROR("DSCaptureGraph::mediaController->Run() has failed with %ld", hr);
  297. return hr;
  298. }
  299. this->running = true;
  300. return hr;
  301. }
  302. HRESULT DSCaptureGraph::pause()
  303. {
  304. HRESULT hr = S_OK;
  305. if (isRunning()) {
  306. hr = this->mediaController->Pause();
  307. if (SUCCEEDED(hr)) {
  308. this->paused = TRUE;
  309. }
  310. }
  311. return hr;
  312. }
  313. HRESULT DSCaptureGraph::stop()
  314. {
  315. HRESULT hr;
  316. #if 0 // Must not
  317. hr = this->mediaController->Pause();
  318. if (hr == S_FALSE) {
  319. TSK_DEBUG_ERROR("DSCaptureGraph::mediaController->Pause() has failed with %ld. Waiting for transition.", hr);
  320. FILTER_STATE pfs;
  321. hr = this->mediaController->GetState(2500, (OAFilterState*) &pfs);
  322. }
  323. #endif
  324. hr = this->mediaController->Stop();
  325. if (!SUCCEEDED(hr)) {
  326. TSK_DEBUG_ERROR("DSCaptureGraph::mediaController->Stop() has failed with %ld", hr);
  327. }
  328. this->running = false;
  329. this->paused = false;
  330. return hr;
  331. }
  332. bool DSCaptureGraph::isRunning()
  333. {
  334. return this->running;
  335. }
  336. bool DSCaptureGraph::isPaused()
  337. {
  338. return this->paused;
  339. }
  340. HRESULT DSCaptureGraph::getConnectedMediaType(AM_MEDIA_TYPE *mediaType)
  341. {
  342. #if defined(_WIN32_WCE)
  343. memmove(mediaType, &this->grabberController->GetMediaType(), sizeof(AM_MEDIA_TYPE));
  344. return S_OK;
  345. #else
  346. return this->grabberController->GetConnectedMediaType(mediaType);
  347. #endif
  348. }
  349. HRESULT DSCaptureGraph::createCaptureGraph()
  350. {
  351. HRESULT hr;
  352. #if defined(_WIN32_WCE)
  353. // Create capture graph builder
  354. CHECK_HR(hr = COCREATE(CLSID_CaptureGraphBuilder, IID_ICaptureGraphBuilder2, this->captureGraphBuilder));
  355. CHECK_HR(hr = COCREATE(CLSID_FilterGraph, IID_IGraphBuilder, this->graphBuilder));
  356. CHECK_HR(hr = this->captureGraphBuilder->SetFiltergraph(this->graphBuilder));
  357. // Create filters
  358. LPUNKNOWN pUnk1 = NULL, pUnk2 = NULL;
  359. CHECK_HR(hr = COCREATE(CLSID_Colour, IID_IBaseFilter, this->colorConvertor565));
  360. this->sampleGrabberFilter = new DSSampleGrabber(FITLER_SAMPLE_GRABBER, pUnk1, &hr);
  361. CHECK_HR(hr);
  362. this->nullRendererFilter = new DSNullFilter(/*FILTER_NULL_RENDERER,*/ pUnk2, &hr);
  363. CHECK_HR(hr);
  364. this->grabberController = (DSSampleGrabber*)(this->sampleGrabberFilter);
  365. if (!this->grabberController) {
  366. CHECK_HR(E_FAIL);
  367. }
  368. // Add Filters
  369. CHECK_HR(hr = this->graphBuilder->AddFilter(this->colorConvertor565, FILTER_COLOR_CONVERTOR_565));
  370. CHECK_HR(hr = this->graphBuilder->AddFilter(this->sampleGrabberFilter, FITLER_SAMPLE_GRABBER));
  371. CHECK_HR(hr = this->graphBuilder->AddFilter(this->nullRendererFilter, FILTER_NULL_RENDERER));
  372. // Find media control
  373. CHECK_HR(hr = QUERY(this->graphBuilder, IID_IMediaControl, this->mediaController));
  374. // Set callback
  375. CHECK_HR(hr = this->grabberController->SetCallback(this->grabberCallback));
  376. #else
  377. // Create capture graph builder
  378. CHECK_HR(hr = COCREATE(CLSID_CaptureGraphBuilder2, IID_ICaptureGraphBuilder2, this->captureGraphBuilder));
  379. // Create the graph builder
  380. CHECK_HR(hr = COCREATE(CLSID_FilterGraph, IID_IGraphBuilder, this->graphBuilder));
  381. // Initialize the Capture Graph Builder.
  382. CHECK_HR(hr = this->captureGraphBuilder->SetFiltergraph(this->graphBuilder));
  383. // Create the sample grabber filter
  384. CHECK_HR(hr = COCREATE(CLSID_SampleGrabber, IID_IBaseFilter, this->sampleGrabberFilter));
  385. // Create tdshow filter
  386. LPUNKNOWN pUnk = NULL;
  387. this->frameRateFilter = new DSFrameRateFilter(FILTER_FRAMERATE, pUnk, &hr);
  388. CHECK_HR(hr);
  389. if (!this->frameRateFilter == NULL) {
  390. CHECK_HR(E_FAIL);
  391. }
  392. // Create the NULL renderer
  393. CHECK_HR(hr = COCREATE(CLSID_NullRenderer, IID_IBaseFilter, this->nullRendererFilter));
  394. // Add sample grabber to the graph
  395. CHECK_HR(hr = this->graphBuilder->AddFilter(this->sampleGrabberFilter, FITLER_SAMPLE_GRABBER));
  396. // Add null renderer to the graph
  397. CHECK_HR(hr = this->graphBuilder->AddFilter(this->nullRendererFilter, FILTER_NULL_RENDERER));
  398. // Add tdshow filter
  399. CHECK_HR(hr = this->graphBuilder->AddFilter(this->frameRateFilter, FILTER_FRAMERATE));
  400. // Find media control
  401. CHECK_HR(hr = QUERY(this->graphBuilder, IID_IMediaControl, this->mediaController));
  402. // Create the sample grabber
  403. CHECK_HR(hr = QUERY(this->sampleGrabberFilter, IID_ISampleGrabber, this->grabberController));
  404. // Set the sample grabber media type (RGB24)
  405. // TODO : CHECK
  406. AM_MEDIA_TYPE mt;
  407. ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
  408. mt.majortype = MEDIATYPE_Video;
  409. mt.subtype = MEDIASUBTYPE_RGB24;
  410. mt.formattype = FORMAT_VideoInfo;
  411. CHECK_HR(hr = this->grabberController->SetMediaType(&mt));
  412. // Set sample grabber media type
  413. this->grabberController->SetOneShot(FALSE);
  414. this->grabberController->SetBufferSamples(FALSE);
  415. CHECK_HR(hr = this->grabberController->SetCallback(this->grabberCallback, 1));
  416. #endif
  417. bail:
  418. return hr;
  419. }