Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

612 lines
18 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 03/12/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgDirectShowCaptureGraph.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgDirectShowCaptureGraph.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgDirectShowCaptureGraph.h"
  16. #include "LgDirectShowVideoInputEnum.h"
  17. #ifdef REGISTER_FILTERGRAPH
  18. HRESULT CLgDirectShowCaptureGraph::AddGraphToRot( IUnknown *pUnkGraph, DWORD *pdwRegister )
  19. {
  20. IMoniker * pMoniker;
  21. IRunningObjectTable *pROT;
  22. WCHAR wsz[128];
  23. HRESULT hr;
  24. if (FAILED(GetRunningObjectTable(0, &pROT)))
  25. {
  26. return E_FAIL;
  27. }
  28. _snwprintf( wsz, 128, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
  29. GetCurrentProcessId());
  30. hr = CreateItemMoniker(L"!", wsz, &pMoniker);
  31. if (SUCCEEDED(hr))
  32. {
  33. hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister);
  34. pMoniker->Release();
  35. }
  36. pROT->Release();
  37. return hr;
  38. }
  39. void CLgDirectShowCaptureGraph::RemoveGraphFromRot( DWORD pdwRegister )
  40. {
  41. IRunningObjectTable *pROT;
  42. if (SUCCEEDED(GetRunningObjectTable(0, &pROT)))
  43. {
  44. pROT->Revoke(pdwRegister);
  45. pROT->Release();
  46. }
  47. }
  48. #endif
  49. CLgDirectShowCaptureGraph::CLgDirectShowCaptureGraph()
  50. {
  51. m_pGraphBuilder = NULL;
  52. m_pCaptureGraphBuilder = NULL;
  53. m_pMediaControl = NULL;
  54. #ifdef REGISTER_FILTERGRAPH
  55. m_dwGraphRegister = 0;
  56. #endif
  57. ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
  58. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::CLgDirectShowCaptureGraph") );
  59. }
  60. CLgDirectShowCaptureGraph::~CLgDirectShowCaptureGraph()
  61. {
  62. Destroy();
  63. ::CoUninitialize();
  64. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::~CLgDirectShowCaptureGraph") );
  65. }
  66. BOOL CLgDirectShowCaptureGraph::Create()
  67. {
  68. Destroy();
  69. // Create the filter graph.
  70. HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&m_pGraphBuilder );
  71. if( FAILED(hr) || !m_pGraphBuilder )
  72. {
  73. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("CLgDirectShowCaptureGraph::Create(IID_IGraphBuilder) failed") );
  74. return FALSE;
  75. }
  76. // Create the capture graph builder.
  77. hr = ::CoCreateInstance( CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&m_pCaptureGraphBuilder );
  78. if( FAILED(hr) || !m_pCaptureGraphBuilder )
  79. {
  80. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("CLgDirectShowCaptureGraph::Create(IID_ICaptureGraphBuilder2) failed") );
  81. return FALSE;
  82. }
  83. // connect filter graph with capture filter graph
  84. hr = m_pCaptureGraphBuilder->SetFiltergraph(m_pGraphBuilder);
  85. if( FAILED(hr) )
  86. {
  87. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("m_pCaptureGraphBuilder->SetFiltergraph failed") );
  88. return FALSE;
  89. }
  90. // Add our graph to the running object table, which will allow
  91. // the GraphEdit application to "spy" on our graph
  92. #ifdef REGISTER_FILTERGRAPH
  93. hr = AddGraphToRot( m_pGraphBuilder, &m_dwGraphRegister );
  94. if (FAILED(hr))
  95. m_dwGraphRegister = 0;
  96. #endif
  97. m_pGraphBuilder->QueryInterface( IID_IMediaControl, (void**)&m_pMediaControl );
  98. if( !m_pMediaControl )
  99. {
  100. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Create"), _T("m_pGraphBuilder->QueryInterface(IID_IMediaControl) failed") );
  101. return FALSE;
  102. }
  103. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::Create"), _T("Succeeded") );
  104. return TRUE;
  105. }
  106. void CLgDirectShowCaptureGraph::Destroy()
  107. {
  108. // log info
  109. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Destroy", "start..." );
  110. #ifdef REGISTER_FILTERGRAPH
  111. // Remove filter graph from the running object table
  112. if( m_dwGraphRegister )
  113. {
  114. RemoveGraphFromRot( m_dwGraphRegister );
  115. m_dwGraphRegister = FALSE;
  116. }
  117. #endif
  118. Stop();
  119. while( m_StreamArray.GetCount() )
  120. {
  121. m_StreamArray[0].pStream->Destroy();
  122. delete m_StreamArray[0].pStream;
  123. m_StreamArray.Remove(0);
  124. }
  125. if( m_pMediaControl )
  126. {
  127. m_pMediaControl->Release();
  128. m_pMediaControl = NULL;
  129. }
  130. if( m_pCaptureGraphBuilder )
  131. {
  132. m_pCaptureGraphBuilder->Release();
  133. m_pCaptureGraphBuilder = NULL;
  134. }
  135. if( m_pGraphBuilder )
  136. {
  137. m_pGraphBuilder->Release();
  138. m_pGraphBuilder = NULL;
  139. }
  140. // log info
  141. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Destroy", "done" );
  142. }
  143. CDirectShowStreamVideo *CLgDirectShowCaptureGraph::CreateStream( LPWSTR pDisplayName )
  144. {
  145. if( !m_pCaptureGraphBuilder || !m_pGraphBuilder || !m_pMediaControl )
  146. {
  147. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::CreateStream"), _T("Capture graph was not built yet") );
  148. return NULL;
  149. }
  150. for( INT i = 0; i < m_StreamArray.GetCount(); i++ )
  151. {
  152. if( !wcscmp( m_StreamArray[i].pDisplayName, pDisplayName ) )
  153. {
  154. return m_StreamArray[i].pStream;
  155. }
  156. }
  157. // Create capture filter
  158. IBaseFilter *pCaptureFilter = CLgDirectShowVideoInputEnum::CreateCaptureFilter( pDisplayName );
  159. if( !pCaptureFilter )
  160. return NULL;
  161. // add capture filter into graph
  162. WCHAR pName[MAX_PATH];
  163. static INT nStreamVideo = 0;
  164. _snwprintf( pName, MAX_PATH, L"Capture Source Filter %d", nStreamVideo );
  165. HRESULT hr = m_pGraphBuilder->AddFilter( pCaptureFilter, pName );
  166. if( FAILED(hr) )
  167. {
  168. pCaptureFilter->Release();
  169. return NULL;
  170. }
  171. // select best quality
  172. // SelectBestResolution( pCaptureFilter );
  173. // create grabber filter
  174. IBaseFilter *pGrabberFilter = NULL;
  175. hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&pGrabberFilter );
  176. if( FAILED(hr) || !pGrabberFilter )
  177. {
  178. pCaptureFilter->Release();
  179. return NULL;
  180. }
  181. // add grabber filter to filter graph
  182. _snwprintf( pName, MAX_PATH, L"Sample Grabber Filter %d", nStreamVideo );
  183. hr = m_pGraphBuilder->AddFilter( pGrabberFilter, pName );
  184. if( FAILED(hr) )
  185. {
  186. pCaptureFilter->Release();
  187. pGrabberFilter->Release();
  188. return NULL;
  189. }
  190. // create sample grabber
  191. ISampleGrabber *pSampleGrabber = NULL;
  192. hr = pGrabberFilter->QueryInterface( IID_ISampleGrabber, (void **)&pSampleGrabber );
  193. if( FAILED(hr) || !pSampleGrabber )
  194. {
  195. pCaptureFilter->Release();
  196. pGrabberFilter->Release();
  197. return NULL;
  198. }
  199. // set video capture format
  200. AM_MEDIA_TYPE mt;
  201. ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
  202. mt.majortype = MEDIATYPE_Video;
  203. mt.subtype = MEDIASUBTYPE_RGB24;
  204. mt.formattype = FORMAT_VideoInfo;
  205. hr = pSampleGrabber->SetMediaType(&mt);
  206. if( FAILED(hr) )
  207. {
  208. pCaptureFilter->Release();
  209. pGrabberFilter->Release();
  210. pSampleGrabber->Release();
  211. return FALSE;
  212. }
  213. pSampleGrabber->Release();
  214. // add crossbar
  215. IBaseFilter *pCrossbarFilter = NULL;
  216. IAMCrossbar *pCrossbar = NULL;
  217. hr = m_pCaptureGraphBuilder->FindInterface( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCaptureFilter, IID_IAMCrossbar, (void **)&pCrossbar );
  218. if( SUCCEEDED(hr) && pCrossbar )
  219. {
  220. hr = pCrossbar->QueryInterface( IID_IBaseFilter, (void**)&pCrossbarFilter );
  221. if( FAILED(hr) || !pCrossbarFilter )
  222. pCrossbarFilter = NULL;
  223. pCrossbar->Release();
  224. }
  225. // add null renderer
  226. IBaseFilter *pNullRenderer = NULL;
  227. hr = ::CoCreateInstance( CLSID_NullRenderer, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&pNullRenderer );
  228. if( SUCCEEDED(hr) || !pNullRenderer )
  229. {
  230. _snwprintf( pName, MAX_PATH, L"Null Renderer Filter %d", nStreamVideo++ );
  231. hr = m_pGraphBuilder->AddFilter( pNullRenderer, pName );
  232. if( FAILED(hr) )
  233. {
  234. pNullRenderer->Release();
  235. pNullRenderer = NULL;
  236. }
  237. }
  238. // render filter graph set
  239. hr = m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCaptureFilter, pGrabberFilter, pNullRenderer );
  240. if( pNullRenderer )
  241. pNullRenderer->Release();
  242. if( FAILED(hr) )
  243. {
  244. hr = m_pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pCaptureFilter, pGrabberFilter, pNullRenderer );
  245. if( FAILED(hr) )
  246. {
  247. if( pCrossbarFilter )
  248. pCrossbarFilter->Release();
  249. pCaptureFilter->Release();
  250. pGrabberFilter->Release();
  251. return NULL;
  252. }
  253. }
  254. // create video stream
  255. CDirectShowStreamVideo *pStream = new CDirectShowStreamVideo;
  256. if( pStream )
  257. {
  258. if( pStream->Create( pCrossbarFilter ? pCrossbarFilter : pCaptureFilter, pCaptureFilter ) )
  259. {
  260. VIDEO_STREAM_INFO vsi;
  261. vsi.pStream = pStream;
  262. wcsncpy( vsi.pDisplayName, pDisplayName, MAX_PATH );
  263. m_StreamArray.Add( vsi );
  264. }
  265. else
  266. {
  267. delete pStream;
  268. pStream = NULL;
  269. }
  270. }
  271. // relase everythink
  272. if( pCrossbarFilter )
  273. pCrossbarFilter->Release();
  274. pCaptureFilter->Release();
  275. pGrabberFilter->Release();
  276. return pStream;
  277. }
  278. BOOL CLgDirectShowCaptureGraph::CreateStreamAll()
  279. {
  280. CLgDirectShowVideoInputEnum VideoInputEnum;
  281. if( !VideoInputEnum.Create() )
  282. {
  283. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Stop"), _T("VideoInputEnum.Create failed") );
  284. return FALSE;
  285. }
  286. for( INT i = 0; i < VideoInputEnum.GetVideoInputDeviceCount(); i++ )
  287. // for( INT i = 4; i < 5; i++ )
  288. {
  289. CLgDirectShowVideoInputEnum::DSVID_INFO DeviceInfo;
  290. if( VideoInputEnum.GetVideoInputDeviceInfo( i, &DeviceInfo ) )
  291. CreateStream( DeviceInfo.pDisplayName );
  292. }
  293. return TRUE;
  294. }
  295. BOOL CLgDirectShowCaptureGraph::Stop()
  296. {
  297. if( !m_pMediaControl )
  298. return FALSE;
  299. // log info
  300. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "start..." );
  301. OAFilterState fs;
  302. while( true )
  303. {
  304. if( m_pMediaControl->Stop() != S_OK )
  305. {
  306. // log info
  307. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "can't stop" );
  308. return FALSE;
  309. }
  310. if( m_pMediaControl->GetState( 1000UL, &fs ) == S_OK )
  311. {
  312. if( fs == State_Stopped )
  313. break;
  314. }
  315. }
  316. // log info
  317. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Stop", "done" );
  318. return TRUE;
  319. }
  320. BOOL CLgDirectShowCaptureGraph::Start()
  321. {
  322. if( !m_pMediaControl )
  323. {
  324. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectShowCaptureGraph::Start"), _T("m_pMediaControl is NULL") );
  325. return FALSE;
  326. }
  327. HRESULT hr = m_pMediaControl->Run();
  328. if( FAILED( hr ) )
  329. {
  330. // log info
  331. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Start", "run failed" );
  332. return FALSE;
  333. }
  334. // log info
  335. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::Start" );
  336. return TRUE;
  337. }
  338. INT CLgDirectShowCaptureGraph::GetDirectShowStreamCount()
  339. {
  340. return m_StreamArray.GetCount();
  341. }
  342. CDirectShowStreamVideo* CLgDirectShowCaptureGraph::GetDirectShowStream( INT nStream )
  343. {
  344. return ( nStream >= 0 && nStream < m_StreamArray.GetCount() ) ? m_StreamArray[nStream].pStream : NULL;
  345. }
  346. BOOL CLgDirectShowCaptureGraph::SelectBestResolution( IBaseFilter *pCaptureFilter )
  347. {
  348. /*
  349. IEnumPins *pEnumPins = NULL;
  350. HRESULT hr = pCaptureFilter->EnumPins( &pEnumPins );
  351. if( SUCCEEDED(hr) && pEnumPins )
  352. {
  353. IPin *pPin = NULL;
  354. while( pEnumPins->Next( 1, &pPin, NULL ) == S_OK )
  355. {
  356. PIN_DIRECTION PinDir;
  357. hr = pPin->QueryDirection( &PinDir );
  358. if( FAILED(hr) || PinDir != PINDIR_OUTPUT )
  359. {
  360. pPin->Release();
  361. continue;
  362. }
  363. PIN_INFO PinInfo;
  364. memset( &PinInfo, 0, sizeof(PinInfo) );
  365. hr = pPin->QueryPinInfo( &PinInfo );
  366. if( PinInfo.pFilter )
  367. PinInfo.pFilter->Release();
  368. if( FAILED(hr) || PinInfo.dir != PINDIR_OUTPUT )
  369. {
  370. pPin->Release();
  371. continue;
  372. }
  373. TCHAR pPinName[MAX_PATH];
  374. WideCharToMultiByte( CP_OEMCP, 0, PinInfo.achName, -1, pPinName, MAX_PATH, NULL, NULL );
  375. // select best quality
  376. IAMStreamConfig *pConfig = NULL;
  377. hr = pPin->QueryInterface( IID_IAMStreamConfig, (void**)&pConfig );
  378. if( FAILED(hr) || !pConfig )
  379. {
  380. pPin->Release();
  381. continue;
  382. }
  383. int iCount = 0, iSize = 0;
  384. hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);
  385. // Check the size to make sure we pass in the correct structure.
  386. if( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) )
  387. {
  388. // Use the video capabilities structure.
  389. AM_MEDIA_TYPE *pmtBestConfig = NULL;
  390. VIDEO_STREAM_CONFIG_CAPS sccBestConfig;
  391. for (int iFormat = 0; iFormat < iCount; iFormat++)
  392. {
  393. VIDEO_STREAM_CONFIG_CAPS scc;
  394. AM_MEDIA_TYPE *pmtConfig;
  395. hr = pConfig->GetStreamCaps( iFormat, &pmtConfig, (BYTE*)&scc );
  396. if (SUCCEEDED(hr))
  397. {
  398. BOOL bChange = FALSE;
  399. // Examine the format, and possibly use it.
  400. if( (pmtConfig->majortype == MEDIATYPE_Video) &&
  401. (pmtConfig->subtype == MEDIASUBTYPE_RGB24) &&
  402. (pmtConfig->formattype == FORMAT_VideoInfo) &&
  403. (pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) &&
  404. (pmtConfig->pbFormat != NULL) )
  405. {
  406. VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
  407. if( pmtBestConfig )
  408. {
  409. VIDEOINFOHEADER *pBestVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
  410. if( pVih->bmiHeader.biWidth > pBestVih->bmiHeader.biWidth &&
  411. pVih->bmiHeader.biHeight > pBestVih->bmiHeader.biHeight )
  412. {
  413. bChange = TRUE;
  414. }
  415. }
  416. else
  417. bChange = TRUE;
  418. // pVih contains the detailed format information.
  419. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::CreateStream", "Found resolution %dx%dx%dx", pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight, pVih->bmiHeader.biBitCount );
  420. }
  421. if( bChange )
  422. {
  423. if( pmtBestConfig )
  424. {
  425. ::CoTaskMemFree( pmtBestConfig->pbFormat );
  426. ::CoTaskMemFree( pmtBestConfig );
  427. }
  428. pmtBestConfig = pmtConfig;
  429. sccBestConfig = scc;
  430. }
  431. else
  432. {
  433. // Delete the media type when you are done.
  434. ::CoTaskMemFree( pmtConfig->pbFormat );
  435. ::CoTaskMemFree( pmtConfig );
  436. }
  437. }
  438. }
  439. if( pmtBestConfig )
  440. {
  441. // VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
  442. // pVih->bmiHeader.biWidth = sccBestConfig.MaxOutputSize.cx;
  443. // pVih->bmiHeader.biHeight = sccBestConfig.MaxOutputSize.cy;
  444. // pVih->bmiHeader.biSizeImage = WIDTHBYTES( pVih->bmiHeader.biBitCount * sccBestConfig.MinOutputSize.cx ) * sccBestConfig.MinOutputSize.cx;
  445. hr = pConfig->SetFormat( pmtBestConfig );
  446. // Delete the media type when you are done.
  447. ::CoTaskMemFree( pmtBestConfig->pbFormat );
  448. ::CoTaskMemFree( pmtBestConfig );
  449. }
  450. }
  451. pConfig->Release();
  452. pPin->Release();
  453. }
  454. pEnumPins->Release();
  455. }
  456. */
  457. IAMStreamConfig *pConfig = NULL;
  458. HRESULT hr = m_pCaptureGraphBuilder->FindInterface( &PIN_CATEGORY_PREVIEW, 0, pCaptureFilter, IID_IAMStreamConfig, (void**)&pConfig );
  459. if( SUCCEEDED( hr ) )
  460. {
  461. int iCount = 0, iSize = 0;
  462. hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);
  463. // Check the size to make sure we pass in the correct structure.
  464. if( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) )
  465. {
  466. // Use the video capabilities structure.
  467. AM_MEDIA_TYPE *pmtBestConfig = NULL;
  468. VIDEO_STREAM_CONFIG_CAPS sccBestConfig;
  469. for (int iFormat = 0; iFormat < iCount; iFormat++)
  470. {
  471. VIDEO_STREAM_CONFIG_CAPS scc;
  472. AM_MEDIA_TYPE *pmtConfig;
  473. hr = pConfig->GetStreamCaps( iFormat, &pmtConfig, (BYTE*)&scc );
  474. if (SUCCEEDED(hr))
  475. {
  476. BOOL bChange = FALSE;
  477. // Examine the format, and possibly use it.
  478. if( (pmtConfig->majortype == MEDIATYPE_Video) &&
  479. (pmtConfig->subtype == MEDIASUBTYPE_RGB24) &&
  480. (pmtConfig->formattype == FORMAT_VideoInfo) &&
  481. (pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) &&
  482. (pmtConfig->pbFormat != NULL) )
  483. {
  484. VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
  485. if( pmtBestConfig )
  486. {
  487. VIDEOINFOHEADER *pBestVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
  488. if( pVih->bmiHeader.biWidth > pBestVih->bmiHeader.biWidth &&
  489. pVih->bmiHeader.biHeight > pBestVih->bmiHeader.biHeight )
  490. {
  491. bChange = TRUE;
  492. }
  493. }
  494. else
  495. bChange = TRUE;
  496. // pVih contains the detailed format information.
  497. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDirectShowCaptureGraph::CreateStream", "Found resolution %dx%dx%dx", pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight, pVih->bmiHeader.biBitCount );
  498. }
  499. if( bChange )
  500. {
  501. if( pmtBestConfig )
  502. {
  503. ::CoTaskMemFree( pmtBestConfig->pbFormat );
  504. ::CoTaskMemFree( pmtBestConfig );
  505. }
  506. pmtBestConfig = pmtConfig;
  507. sccBestConfig = scc;
  508. }
  509. else
  510. {
  511. // Delete the media type when you are done.
  512. ::CoTaskMemFree( pmtConfig->pbFormat );
  513. ::CoTaskMemFree( pmtConfig );
  514. }
  515. }
  516. }
  517. if( pmtBestConfig )
  518. {
  519. // VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtBestConfig->pbFormat;
  520. // pVih->bmiHeader.biWidth = sccBestConfig.MaxOutputSize.cx;
  521. // pVih->bmiHeader.biHeight = sccBestConfig.MaxOutputSize.cy;
  522. // pVih->bmiHeader.biSizeImage = WIDTHBYTES( pVih->bmiHeader.biBitCount * sccBestConfig.MinOutputSize.cx ) * sccBestConfig.MinOutputSize.cx;
  523. // pVih->dwBitRate *= 4;
  524. hr = pConfig->SetFormat( pmtBestConfig );
  525. // Delete the media type when you are done.
  526. ::CoTaskMemFree( pmtBestConfig->pbFormat );
  527. ::CoTaskMemFree( pmtBestConfig );
  528. }
  529. }
  530. pConfig->Release();
  531. }
  532. return TRUE;
  533. }