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.

545 lines
14 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 24/03/2000
  5. // Last modification: 04/03/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Video camera showing and captiring window
  8. // FILE: VideoDirectShow.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file for implementation of class CVideoDirectShow
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "VideoDirectShow.h"
  17. //#include "Resource.h"
  18. const GUID IID_ICaptureDIB = { 0xfd5010a3, 0x8ebe, 0x11ce, 0x81, 0x83, 0x00, 0xaa, 0x00, 0x57, 0x7d, 0xa1 };
  19. BOOL CVideoDirectShow::Create( LPCTSTR pFileName )
  20. {
  21. HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **) &m_pGraph );
  22. if( SUCCEEDED( hr ) )
  23. {
  24. hr = m_pGraph->QueryInterface( IID_IMediaControl, (void**)&m_pMediaControl );
  25. if( SUCCEEDED( hr ) )
  26. {
  27. WCHAR pwFileName[ MAX_PATH ] = L"";
  28. ::MultiByteToWideChar( CP_ACP, 0, pFileName, pFileName ? strlen( pFileName ) : 0, pwFileName, MAX_PATH );
  29. hr = m_pGraph->RenderFile( pwFileName, NULL );
  30. if( SUCCEEDED( hr ) )
  31. {
  32. FindVideos();
  33. m_pMediaControl->Run();
  34. /*
  35. m_pMediaControl->Stop();
  36. OAFilterState fs = 1;
  37. m_pMediaControl->GetState( INFINITE, &fs );
  38. m_pMediaControl->Run();
  39. */
  40. return TRUE;
  41. }
  42. }
  43. }
  44. return FALSE;
  45. }
  46. CVideoDirectShow::CVideoDirectShow()
  47. {
  48. m_pGraph = NULL;
  49. m_pMediaControl = NULL;
  50. m_nCount = 0;
  51. ::ZeroMemory( m_ppVideoWindow, sizeof(m_ppVideoWindow) );
  52. ::ZeroMemory( m_ppCaptureDIB, sizeof(m_ppCaptureDIB) );
  53. ::ZeroMemory( m_phWnd, sizeof(m_phWnd) );
  54. ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
  55. }
  56. CVideoDirectShow::~CVideoDirectShow()
  57. {
  58. if( m_pMediaControl )
  59. {
  60. m_pMediaControl->Stop();
  61. OAFilterState fs = 1;
  62. m_pMediaControl->GetState( INFINITE, &fs );
  63. m_pMediaControl->Release();
  64. m_pMediaControl = NULL;
  65. }
  66. for( int i = 0; i < m_nCount; i++ )
  67. {
  68. if( m_ppCaptureDIB[i] )
  69. {
  70. m_ppCaptureDIB[i]->Release();
  71. m_ppCaptureDIB[i] = NULL;
  72. }
  73. if( m_ppVideoWindow[i] )
  74. {
  75. m_ppVideoWindow[i]->Release();
  76. m_ppVideoWindow[i] = NULL;
  77. }
  78. }
  79. if( m_pGraph )
  80. {
  81. m_pGraph->Release();
  82. m_pGraph = NULL;
  83. }
  84. ::CoUninitialize();
  85. }
  86. BOOL CVideoDirectShow::Capture( int n, LPBITMAPINFO pBmi, LPBYTE pBytes )
  87. {
  88. if( n < m_nCount && m_ppCaptureDIB[n] )
  89. {
  90. LPBITMAPINFOHEADER pBmih;
  91. LPBYTE pBuffer;
  92. HRESULT hr = m_ppCaptureDIB[n]->get_DIB( &pBmih, &pBuffer );
  93. if( SUCCEEDED(hr) )
  94. {
  95. LPBYTE pDstBytes = NULL;
  96. HBITMAP hDstBitmap = ::CreateDIBSection( 0UL, pBmi, DIB_RGB_COLORS, (void**)&pDstBytes, NULL, 0UL );
  97. if( hDstBitmap )
  98. {
  99. HWND hWnd = ::GetDesktopWindow();
  100. HDC hDC = ::GetDC( hWnd );
  101. HDC hCopyDC = ::CreateCompatibleDC( hDC );
  102. ::ReleaseDC( hWnd, hDC );
  103. HBITMAP hOldBitmap = (HBITMAP)::SelectObject( hCopyDC, hDstBitmap );
  104. int nOldMode = ::SetStretchBltMode( hCopyDC, COLORONCOLOR );
  105. ::StretchDIBits( hCopyDC, 0, 0, pBmi->bmiHeader.biWidth, pBmi->bmiHeader.biHeight,
  106. 0, 0, pBmih->biWidth, pBmih->biHeight,
  107. pBuffer, (LPBITMAPINFO)pBmih, DIB_RGB_COLORS, SRCCOPY );
  108. ::SetStretchBltMode( hCopyDC, nOldMode );
  109. ::SelectObject( hCopyDC, hOldBitmap );
  110. ::CopyMemory( pBytes, pDstBytes, pBmi->bmiHeader.biSizeImage );
  111. ::DeleteDC( hCopyDC );
  112. ::DeleteObject( hDstBitmap );
  113. }
  114. /*
  115. if( m_phWnd[n]->OpenClipboard() )
  116. {
  117. HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, pBmih->biSizeImage + sizeof(BITMAPINFOHEADER) );
  118. LPBYTE pMem = (LPBYTE)::GlobalLock( hMem );
  119. ::CopyMemory( pMem, pBmih, sizeof(BITMAPINFOHEADER) );
  120. ::CopyMemory( pMem + sizeof(BITMAPINFOHEADER), pBuffer, pBmih->biSizeImage );
  121. ::GlobalUnlock( hMem );
  122. ::SetClipboardData( CF_DIB, hMem );
  123. ::CloseClipboard();
  124. }
  125. */
  126. ::CoTaskMemFree( pBmih );
  127. ::CoTaskMemFree( pBuffer );
  128. }
  129. return TRUE;
  130. }
  131. return FALSE;
  132. }
  133. BOOL CVideoDirectShow::FindVideos()
  134. {
  135. IEnumFilters *pEnum = NULL;
  136. IBaseFilter *pFilter = NULL;
  137. ULONG cFetched;
  138. HRESULT hr;
  139. m_pGraph->EnumFilters(&pEnum);
  140. while( pEnum->Next(1, &pFilter, &cFetched) == S_OK )
  141. {
  142. IVideoWindow *pVideoWindow = NULL;
  143. hr = pFilter->QueryInterface( IID_IVideoWindow, (void**)&pVideoWindow );
  144. if( SUCCEEDED( hr ) )
  145. {
  146. CGuidList list;
  147. m_ppCaptureDIB[m_nCount] = FindCaptureDIB( pFilter, list );
  148. if( m_ppCaptureDIB[m_nCount] )
  149. {
  150. m_ppCaptureDIB[m_nCount]->put_Scale( 1.0, 1.7 );
  151. m_ppCaptureDIB[m_nCount]->put_Shift( 0.0, 0.0 );
  152. m_ppVideoWindow[m_nCount] = pVideoWindow;
  153. // m_ppVideoWindow[m_nCount]->put_AutoShow( OAFALSE );
  154. // m_ppVideoWindow[m_nCount]->put_Visible( OAFALSE );
  155. m_nCount++;
  156. }
  157. pVideoWindow->put_AutoShow( OAFALSE );
  158. pVideoWindow->put_Visible( OAFALSE );
  159. }
  160. pFilter->Release();
  161. }
  162. pEnum->Release();
  163. return TRUE;
  164. }
  165. ICaptureDIB *CVideoDirectShow::FindCaptureDIB( IBaseFilter* pFilter, CGuidList &list )
  166. {
  167. IEnumPins *pEnumPins = NULL;
  168. IPin *pPinIn = NULL;
  169. IPin *pPinOut = NULL;
  170. ICaptureDIB *pCaptureDIB = NULL;
  171. IPersist *pPersist = NULL;
  172. ULONG cFetched;
  173. HRESULT hr;
  174. hr = pFilter->EnumPins( &pEnumPins );
  175. if( SUCCEEDED(hr) )
  176. {
  177. while( pEnumPins->Next( 1, &pPinIn, &cFetched ) == S_OK && !pCaptureDIB )
  178. {
  179. hr = pPinIn->ConnectedTo( &pPinOut );
  180. if( SUCCEEDED( hr ) )
  181. {
  182. PIN_INFO PinInfo;
  183. hr = pPinOut->QueryPinInfo( &PinInfo );
  184. if( SUCCEEDED( hr ) )
  185. {
  186. /* if( PinInfo.dir == PINDIR_INPUT )
  187. {
  188. if( PinInfo.pFilter )
  189. PinInfo.pFilter->Release();
  190. }
  191. else
  192. */ if( PinInfo.pFilter )
  193. {
  194. hr = PinInfo.pFilter->QueryInterface( IID_IPersist, (void**)&pPersist );
  195. if( SUCCEEDED( hr ) )
  196. {
  197. CLSID clsid;
  198. hr = pPersist->GetClassID( &clsid );
  199. if( SUCCEEDED( hr ) )
  200. {
  201. if( !list.Find( clsid ) )
  202. {
  203. list.AddTail( clsid );
  204. // if( !IsCompatible( PinInfo.pFilter, IID_IOverlay ) )
  205. {
  206. hr = PinInfo.pFilter->QueryInterface( IID_ICaptureDIB, (void**)&pCaptureDIB );
  207. if( FAILED( hr ) )
  208. pCaptureDIB = FindCaptureDIB( PinInfo.pFilter, list );
  209. }
  210. }
  211. PinInfo.pFilter->Release();
  212. }
  213. }
  214. }
  215. }
  216. pPinOut->Release();
  217. }
  218. pPinIn->Release();
  219. }
  220. pEnumPins->Release();
  221. }
  222. return pCaptureDIB;
  223. }
  224. bool CVideoDirectShow::IsCompatible( IBaseFilter *pFilter, REFIID IID )
  225. {
  226. HRESULT hr;
  227. if( !pFilter )
  228. return false;
  229. IUnknown *pUnknown = NULL;
  230. hr = pFilter->QueryInterface( IID, (void**)&pUnknown );
  231. if( FAILED( hr ) )
  232. {
  233. return false;
  234. }
  235. pUnknown->Release();
  236. return true;
  237. }
  238. BOOL CVideoDirectShow::SetupFilters()
  239. {
  240. IEnumFilters *pEnum = NULL;
  241. IBaseFilter *pFilter = NULL;
  242. ULONG cFetched;
  243. m_pGraph->EnumFilters(&pEnum);
  244. while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)
  245. {
  246. ISpecifyPropertyPages *pSpecify;
  247. HRESULT hr = pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pSpecify);
  248. if (SUCCEEDED(hr)) {
  249. FILTER_INFO FilterInfo;
  250. pFilter->QueryFilterInfo(&FilterInfo);
  251. CAUUID caGUID;
  252. pSpecify->GetPages(&caGUID);
  253. pSpecify->Release();
  254. OleCreatePropertyFrame(
  255. NULL, // Parent window
  256. 0, // x (Reserved)
  257. 0, // y (Reserved)
  258. FilterInfo.achName, // Caption for the dialog box
  259. 1, // Number of filters
  260. (IUnknown **)&pFilter, // Pointer to the filter
  261. caGUID.cElems, // Number of property pages
  262. caGUID.pElems, // Pointer to property page CLSIDs
  263. 0, // Locale identifier
  264. 0, // Reserved
  265. NULL // Reserved
  266. );
  267. CoTaskMemFree(caGUID.pElems);
  268. FilterInfo.pGraph->Release();
  269. }
  270. pFilter->Release();
  271. }
  272. pEnum->Release();
  273. return TRUE;
  274. }
  275. BOOL CVideoDirectShow::ConnectWnd(int n, HWND hWnd, int x, int y, int cx, int cy)
  276. {
  277. if( n < m_nCount && m_ppVideoWindow[n] )
  278. {
  279. m_phWnd[n] = hWnd;
  280. m_ppVideoWindow[n]->put_Owner( (OAHWND) m_phWnd[n] );
  281. m_ppVideoWindow[n]->put_WindowStyle( WS_CHILD | WS_CLIPCHILDREN );
  282. m_ppVideoWindow[n]->SetWindowPosition( x, y, cx, cy );
  283. m_ppVideoWindow[n]->put_AutoShow( OATRUE );
  284. m_ppVideoWindow[n]->put_Visible( OATRUE );
  285. return TRUE;
  286. }
  287. return FALSE;
  288. }
  289. void CVideoDirectShow::SetWindowPosition( int n, int x, int y, int cx, int cy )
  290. {
  291. if( n < m_nCount && m_ppVideoWindow[n] )
  292. {
  293. m_ppVideoWindow[n]->SetWindowPosition( x, y, cx, cy+180 );
  294. m_pMediaControl->Run();
  295. }
  296. }
  297. void CVideoDirectShow::ShowWindow( int n, int nShow )
  298. {
  299. if( n < m_nCount )
  300. {
  301. if( nShow == SW_SHOW )
  302. {
  303. // m_pMediaControl->Run();
  304. m_ppVideoWindow[n]->put_Visible( OATRUE );
  305. }
  306. else
  307. {
  308. m_ppVideoWindow[n]->put_Visible( OAFALSE );
  309. }
  310. }
  311. }
  312. /*
  313. BOOL CVideoDirectShow::SetupDialog( CWnd *pWnd, int n )
  314. {
  315. if( n < m_nCount )
  316. {
  317. HWND hWndOwner = pWnd->GetSafeHwnd();
  318. HINSTANCE hinst = (HINSTANCE)GetWindowLong( hWndOwner, GWL_HINSTANCE );
  319. IBaseFilter *pFilter = NULL;
  320. m_ppVideoWindow[n]->QueryInterface( IID_IBaseFilter, (void**)&pFilter );
  321. if( pFilter )
  322. {
  323. CGuidList gList;
  324. EnumFilters( pFilter, gList, m_SpecifyList );
  325. pFilter->Release();
  326. ::DialogBoxParam( hinst, (LPCTSTR)IDD_VIDEO_SETUP_DLG, hWndOwner, (DLGPROC) DialogProcSetup, (LPARAM)this );
  327. POSITION pos = m_SpecifyList.GetHeadPosition();
  328. while( pos )
  329. {
  330. PROPERTY prop = m_SpecifyList.GetNext( pos );
  331. prop.pSpecify->Release();
  332. prop.pUnknown->Release();
  333. }
  334. m_SpecifyList.RemoveAll();
  335. }
  336. }
  337. return TRUE;
  338. }
  339. BOOL CVideoDirectShow::DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  340. {
  341. switch( uMsg )
  342. {
  343. case WM_INITDIALOG:
  344. {
  345. HINSTANCE hInst = (HINSTANCE)::GetWindowLong( hWnd, GWL_HINSTANCE );
  346. HFONT hFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
  347. int i = 0;
  348. POSITION pos = m_SpecifyList.GetHeadPosition();
  349. CRect bRect;
  350. while( pos )
  351. {
  352. PROPERTY prop = m_SpecifyList.GetNext( pos );
  353. CRect rect( 2, 2 + i * 14, 102, 14 + i * 14 );
  354. CRect fRect( &rect );
  355. fRect.bottom += 4;
  356. fRect.right += 4;
  357. ::MapDialogRect( hWnd, &rect );
  358. ::MapDialogRect( hWnd, &fRect );
  359. if( i ) bRect |= fRect;
  360. else bRect = fRect;
  361. CHAR pBuffer[MAX_FILTER_NAME];
  362. ::WideCharToMultiByte( CP_ACP, 0, prop.pName, -1, pBuffer, MAX_FILTER_NAME, NULL, NULL );
  363. CRect *pRect = &rect;
  364. HWND hChildWnd = ::CreateWindow( "BUTTON", pBuffer, WS_CHILD | WS_VISIBLE, pRect->left, pRect->top, pRect->Width(), pRect->Height(), hWnd, NULL, hInst, 0UL );
  365. ::SendMessage( hChildWnd, WM_SETFONT, (WPARAM)hFont, TRUE );
  366. ::SetWindowLong( hChildWnd, GWL_ID, i + 20 );
  367. i++;
  368. }
  369. CRect cRect, wRect;
  370. GetClientRect( hWnd, &cRect );
  371. GetWindowRect( hWnd, &wRect );
  372. int cx = wRect.Width() - cRect.Width();
  373. int cy = wRect.Height() - cRect.Height();
  374. ::MoveWindow( hWnd, wRect.left, wRect.top, cx + bRect.Width(), cy + bRect.Height(), TRUE );
  375. }
  376. return TRUE;
  377. case WM_COMMAND:
  378. {
  379. int i = 20;
  380. POSITION pos = m_SpecifyList.GetHeadPosition();
  381. while( pos )
  382. {
  383. PROPERTY prop = m_SpecifyList.GetNext( pos );
  384. if( i == LOWORD( wParam ) )
  385. {
  386. CAUUID caGUID;
  387. prop.pSpecify->GetPages(&caGUID);
  388. OleCreatePropertyFrame(
  389. hWnd, // Parent window
  390. 0, // x (Reserved)
  391. 0, // y (Reserved)
  392. prop.pName, // Caption for the dialog box
  393. 1, // Number of filters
  394. (IUnknown **)&prop.pUnknown, // Pointer to the filter
  395. caGUID.cElems, // Number of property pages
  396. caGUID.pElems, // Pointer to property page CLSIDs
  397. 0, // Locale identifier
  398. 0, // Reserved
  399. NULL // Reserved
  400. );
  401. CoTaskMemFree(caGUID.pElems);
  402. break;
  403. }
  404. i++;
  405. }
  406. }
  407. return TRUE;
  408. case WM_CLOSE:
  409. return ::EndDialog( hWnd, IDCANCEL );
  410. }
  411. return FALSE;
  412. }
  413. */
  414. void CVideoDirectShow::EnumFilters( IBaseFilter* pFilter, CGuidList &gList, CSpecifyList &sList )
  415. {
  416. IEnumPins *pEnumPins = NULL;
  417. IPin *pPinIn = NULL;
  418. IPin *pPinOut = NULL;
  419. IPersist *pPersist = NULL;
  420. ULONG cFetched;
  421. HRESULT hr;
  422. hr = pFilter->EnumPins( &pEnumPins );
  423. if( SUCCEEDED(hr) )
  424. {
  425. while( pEnumPins->Next( 1, &pPinIn, &cFetched ) == S_OK )
  426. {
  427. hr = pPinIn->ConnectedTo( &pPinOut );
  428. if( SUCCEEDED( hr ) )
  429. {
  430. PIN_INFO PinInfo;
  431. hr = pPinOut->QueryPinInfo( &PinInfo );
  432. if( SUCCEEDED( hr ) )
  433. {
  434. if( PinInfo.pFilter )
  435. {
  436. hr = PinInfo.pFilter->QueryInterface( IID_IPersist, (void**)&pPersist );
  437. if( SUCCEEDED( hr ) )
  438. {
  439. CLSID clsid;
  440. hr = pPersist->GetClassID( &clsid );
  441. if( SUCCEEDED( hr ) )
  442. {
  443. if( !gList.Find( clsid ) )
  444. {
  445. gList.AddTail( clsid );
  446. PROPERTY prop;
  447. hr = PinInfo.pFilter->QueryInterface( IID_ISpecifyPropertyPages, (void**)&prop.pSpecify );
  448. if( SUCCEEDED( hr ) )
  449. {
  450. hr = PinInfo.pFilter->QueryInterface( IID_IUnknown, (void**)&prop.pUnknown );
  451. FILTER_INFO FilterInfo;
  452. hr = PinInfo.pFilter->QueryFilterInfo(&FilterInfo);
  453. if( SUCCEEDED( hr ) )
  454. {
  455. wcscpy( prop.pName, FilterInfo.achName );
  456. if( FilterInfo.pGraph )
  457. FilterInfo.pGraph->Release();
  458. }
  459. else
  460. wcscpy( prop.pName, L"Unknown" );
  461. sList.AddTail( prop );
  462. }
  463. EnumFilters( PinInfo.pFilter, gList, sList );
  464. }
  465. PinInfo.pFilter->Release();
  466. }
  467. }
  468. }
  469. }
  470. pPinOut->Release();
  471. }
  472. pPinIn->Release();
  473. }
  474. pEnumPins->Release();
  475. }
  476. }