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.

456 lines
14 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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: VideoCapture.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of VideoCapture.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. //#include "chicago.h"
  16. #include "VideoCapture.h"
  17. #include "VideoCaptureDevice.h"
  18. #include "CameraPTZ.h"
  19. //#include "VideoWnd.h"
  20. #include "SensorLGS.h"
  21. #include "..\..\LgLib\Version.h"
  22. #include "SensorArray.h"
  23. //#include "GenericArchive.h"
  24. #include "..\Chicago\Report.h"
  25. //#define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2)
  26. #ifdef _DEBUG
  27. #undef THIS_FILE
  28. static char THIS_FILE[]=__FILE__;
  29. #define new DEBUG_NEW
  30. #endif
  31. /*
  32. void RELEASE_TRACE( LPCTSTR pFormat, ... )
  33. {
  34. TCHAR pBuffer[1024];
  35. va_list pMarker;
  36. va_start( pMarker, pFormat );
  37. _vsntprintf( pBuffer, 1024, pFormat, pMarker );
  38. va_end( pMarker );
  39. TCHAR pTimeBuffer[1024];
  40. SYSTEMTIME st;
  41. ::GetLocalTime( &st );
  42. _sntprintf( pTimeBuffer, 1024, _T("[%02d:%02d:%02d:%03d] %s"), st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, pBuffer );
  43. ::OutputDebugString( pTimeBuffer );
  44. }
  45. */
  46. CVideoCapture::CVideoCapture()
  47. {
  48. m_pSensorArray = NULL;
  49. m_pReport = NULL;
  50. // m_pVideoCaptureDevice = NULL;
  51. ::ZeroMemory( &m_Render, sizeof( m_Render ) );
  52. HWND hWnd = ::GetDesktopWindow();
  53. HDC hDC = ::GetDC( hWnd );
  54. m_Render.hDstDC = ::CreateCompatibleDC( hDC );
  55. m_Render.hSrcDC = ::CreateCompatibleDC( hDC );
  56. ::ReleaseDC( hWnd, hDC );
  57. const cBitCount = 24;
  58. m_Render.nDstBmiSize = sizeof(BITMAPINFOHEADER) + ( ( cBitCount <= 8 ) ? sizeof(RGBQUAD) * ( 1 << cBitCount ) : 0 );
  59. m_Render.pDstBmi = (LPBITMAPINFO) new BYTE[ m_Render.nDstBmiSize ];
  60. m_Render.pDstBmi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
  61. m_Render.pDstBmi->bmiHeader.biWidth = 320;
  62. m_Render.pDstBmi->bmiHeader.biHeight = 240;
  63. m_Render.pDstBmi->bmiHeader.biPlanes = 1;
  64. m_Render.pDstBmi->bmiHeader.biBitCount = cBitCount;
  65. m_Render.pDstBmi->bmiHeader.biCompression = 0;
  66. m_Render.pDstBmi->bmiHeader.biSizeImage =
  67. WIDTHBYTES( m_Render.pDstBmi->bmiHeader.biWidth *
  68. m_Render.pDstBmi->bmiHeader.biBitCount ) *
  69. m_Render.pDstBmi->bmiHeader.biHeight;
  70. m_Render.pDstBmi->bmiHeader.biClrImportant = 0;
  71. m_Render.pDstBmi->bmiHeader.biClrUsed = 0;
  72. m_Render.pDstBmi->bmiHeader.biXPelsPerMeter = 0;
  73. m_Render.pDstBmi->bmiHeader.biYPelsPerMeter = 0;
  74. /*
  75. for( int i = 0; i < 256; i++ )
  76. {
  77. m_Render.pDstBmi->bmiColors[i].rgbRed = i;
  78. m_Render.pDstBmi->bmiColors[i].rgbGreen = i;
  79. m_Render.pDstBmi->bmiColors[i].rgbBlue = i;
  80. m_Render.pDstBmi->bmiColors[i].rgbReserved = 0;
  81. }
  82. */
  83. m_Render.pDstBuffer = NULL;
  84. m_Render.hDstBitmap = ::CreateDIBSection( m_Render.hDstDC, m_Render.pDstBmi, DIB_RGB_COLORS,
  85. (void**)&m_Render.pDstBuffer, NULL, 0UL );
  86. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CVideoCapture") );
  87. }
  88. CVideoCapture::~CVideoCapture()
  89. {
  90. ::DeleteObject( m_Render.hDstBitmap );
  91. delete [] (LPBYTE)m_Render.pDstBmi;
  92. ::DeleteDC( m_Render.hDstDC );
  93. ::DeleteDC( m_Render.hSrcDC );
  94. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::~CVideoCapture") );
  95. }
  96. void CVideoCapture::Capture( CVideoCamera *pVideoCamera, LARGE_INTEGER liOffset )
  97. {
  98. //find connected video input
  99. CVideoPin *pOutputPin = pVideoCamera->GetOutput(0);
  100. if( !pOutputPin )
  101. {
  102. // camera already exist in capturing jobs list
  103. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "camera %p has no output pin", pVideoCamera );
  104. return;
  105. }
  106. // search for video input device
  107. while( pOutputPin )
  108. {
  109. // get conneted pin
  110. CVideoPin *pInputPin = pOutputPin->GetCPin();
  111. if( !pInputPin )
  112. {
  113. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "camera %p has output pin that don't conneted to other pin", pVideoCamera );
  114. return;
  115. }
  116. CVideoDevice *pVideoDevice = NULL;
  117. INT nInputPin = 0;
  118. pInputPin->GetVideoDevice( &pVideoDevice, &nInputPin );
  119. if( !pVideoDevice )
  120. {
  121. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "input pin don't belong to any device" );
  122. return;
  123. }
  124. // if it is video capture device
  125. for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ )
  126. {
  127. CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice(i);
  128. if( pVideoDevice == pVideoCaptureDevice )
  129. {
  130. // actual capturing
  131. LPBITMAPINFO pBmi = NULL;
  132. LPBYTE pBytes = NULL;
  133. HBITMAP hBitmap = NULL;
  134. pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap );
  135. // image storing
  136. CAP_DATA cd;
  137. memset( &cd, 0, sizeof(cd) );
  138. cd.liOffset = liOffset;
  139. cd.pCamera = pVideoCamera;
  140. StoreImage( pBmi, pBytes, cd );
  141. // send image to main queue
  142. m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap );
  143. // capture done
  144. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p manual capture done", pVideoCamera );
  145. return;
  146. }
  147. }
  148. // get associated pin
  149. pOutputPin = pInputPin->GetAPin();
  150. }
  151. // capture done
  152. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p manual capture can't be done", pVideoCamera );
  153. }
  154. void CVideoCapture::Capture( CSensor* pSensor, CVideoCamera *pVideoCamera, BOOL bSnapShot, LARGE_INTEGER liOffset, double pan, double tilt, double zoom )
  155. {
  156. // if there are any capture input and capture window
  157. if( m_pSensorArray->GetVideoCaptureDeviceCount() )
  158. {
  159. if( bSnapShot )
  160. {
  161. Capture( pVideoCamera, liOffset );
  162. return;
  163. }
  164. if( pVideoCamera->GetType() == VC_PTZ )
  165. {
  166. POSITION pos = m_List.GetHeadPosition();
  167. while( pos )
  168. {
  169. CAP_DATA cd = m_List.GetNext( pos );
  170. if( cd.pCamera == pVideoCamera )
  171. {
  172. // camera already exist in capturing jobs list
  173. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p already busy with capture request", pVideoCamera );
  174. return;
  175. }
  176. }
  177. }
  178. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "capture job for camera %p added to list", pVideoCamera );
  179. CAP_DATA cd;
  180. cd.bSnapShot = bSnapShot;
  181. cd.pCamera = pVideoCamera;
  182. cd.pSensor = pSensor;
  183. // data for ptz camera
  184. cd.pan = pan;
  185. cd.tilt = tilt;
  186. cd.zoom = zoom;
  187. // data for store image
  188. cd.liOffset = liOffset;
  189. // start time
  190. cd.dwTime = ::GetTickCount();
  191. // assume that we don't own camera yet
  192. cd.bOwn = FALSE;
  193. m_List.AddTail( cd );
  194. }
  195. }
  196. void CVideoCapture::CheckState()
  197. {
  198. POSITION pos = m_List.GetHeadPosition();
  199. while( pos )
  200. {
  201. CAP_DATA cd = m_List.GetAt( pos );
  202. DWORD dwTime = ::GetTickCount();
  203. // check capture time limitation
  204. if( ( dwTime - cd.dwTime ) > MAX_CAPTURE_TIME )
  205. {
  206. if( cd.bOwn )
  207. {
  208. // release camera owning
  209. cd.pCamera->Unlock();
  210. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("Camera unlocked due remove capture job for camera %p by timeout"), cd.pCamera );
  211. }
  212. // log information
  213. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("capture job for camera %p removed by timeout"), cd.pCamera );
  214. POSITION temp = pos;
  215. // goto next capture job
  216. m_List.GetNext( pos );
  217. // remove capture job
  218. m_List.RemoveAt( temp );
  219. }
  220. else
  221. if( ( cd.pCamera->GetType() == VC_FIX ) && ( ( dwTime - cd.dwTime ) < ( cd.pSensor->GetImageCount() * 500UL / 2UL ) ) ) // MAX_FIXED_WAIT
  222. {
  223. // skip capturing for fixed camera, we will capture only after 2 sec upon request for capture appears
  224. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "skip job for fixed camera" );
  225. // goto next capture job
  226. m_List.GetNext( pos );
  227. }
  228. else
  229. {
  230. CVideoCameraPTZ *pCameraPTZ;
  231. if( !cd.bOwn )
  232. {
  233. if( !cd.pCamera->IsLock() )
  234. {
  235. // log information
  236. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p locked", cd.pCamera );
  237. // we need to prevent from using camera by someone else
  238. cd.pCamera->Lock();
  239. // now we are owner of camera
  240. cd.bOwn = TRUE;
  241. // we MUST update data in list, because "cd" just a copy
  242. m_List.GetAt( pos ).bOwn = TRUE;
  243. // during snap shot camera already in position
  244. if( !cd.bSnapShot )
  245. {
  246. switch( cd.pCamera->GetType() )
  247. {
  248. case VC_FIX:
  249. break;
  250. case VC_PTZ:
  251. // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("%0.0f,%0.0f,%0.0f"), cd.pan, cd.tilt, cd.zoom );
  252. pCameraPTZ = static_cast<CVideoCameraPTZ*>(cd.pCamera);
  253. if( !pCameraPTZ->CmdSetPTZ( cd.pan, cd.tilt, cd.zoom ) )
  254. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p busy", cd.pCamera );
  255. // log information
  256. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p issued CmdSetPTZ", cd.pCamera );
  257. break;
  258. default:
  259. // camera MUST has known type
  260. ASSERT( FALSE );
  261. // log information
  262. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::CheckState", "camera %p has unknown type %d", cd.pCamera, cd.pCamera->GetType() );
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. if( cd.bOwn )
  269. {
  270. BOOL bCameraReady = TRUE;
  271. // during snap shot camera already in position
  272. if( !cd.bSnapShot )
  273. {
  274. // camera is in position?
  275. switch( cd.pCamera->GetType() )
  276. {
  277. case VC_FIX:
  278. break;
  279. case VC_PTZ:
  280. pCameraPTZ = static_cast<CVideoCameraPTZ*>(cd.pCamera);
  281. bCameraReady = pCameraPTZ->IsReadyPtz();
  282. if( !bCameraReady )
  283. {
  284. // pCameraPTZ->CmdGetPTZ();
  285. // RELEASE_TRACE( "CAMERA NOT READY\n" );
  286. }
  287. break;
  288. default:
  289. // camera MUST has known type
  290. ASSERT( bCameraReady = FALSE );
  291. // log information
  292. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::CheckState", "camera %p has unknown type %d", cd.pCamera, cd.pCamera->GetType() );
  293. break;
  294. }
  295. }
  296. if( bCameraReady )
  297. {
  298. // find availible pin index
  299. for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ )
  300. {
  301. CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice(i);
  302. int nInputPin = pVideoCaptureDevice->GetAvailiblePin( cd.pCamera->GetOutput(0) );
  303. if( nInputPin >= 0 )
  304. {
  305. // force video connection
  306. if( cd.pCamera->Connect( pVideoCaptureDevice->GetInput( nInputPin ), FALSE ) )
  307. {
  308. // assign current video camera to computer video input
  309. pVideoCaptureDevice->AssignVideoCamera( nInputPin, cd.pCamera );
  310. LPBITMAPINFO pBmi = NULL;
  311. LPBYTE pBytes = NULL;
  312. HBITMAP hBitmap = NULL;
  313. if( cd.pCamera->GetType() == VC_FIX )
  314. {
  315. for( INT nImage = cd.pSensor->GetImageCount() - 1; nImage >= 0; nImage-- )
  316. {
  317. // actual capturing & image storing
  318. if( !pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap, nImage * 500UL ) )
  319. continue;
  320. // store event
  321. StoreImage( pBmi, pBytes, cd );
  322. // send image to main queue
  323. m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap );
  324. }
  325. }
  326. else
  327. {
  328. // actual capturing & image storing
  329. if( pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap ) )
  330. {
  331. // store event
  332. StoreImage( pBmi, pBytes, cd );
  333. // send image to main queue
  334. m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap );
  335. }
  336. }
  337. // release camera owning
  338. cd.pCamera->Unlock();
  339. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "Camera %p unlocked after successefull capture", cd.pCamera );
  340. // release video lines owning
  341. cd.pCamera->Disconnect();
  342. // we need to update time in list,
  343. // so next time this node will be deleted
  344. m_List.GetAt( pos ).dwTime = dwTime - MAX_CAPTURE_TIME;
  345. // now we are not an owner of camera
  346. cd.bOwn = FALSE;
  347. // we MUST update data in list, because "cd" just a copy
  348. m_List.GetAt( pos ).bOwn = FALSE;
  349. // log information
  350. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "capture done for camera %d", cd.pCamera );
  351. break;
  352. }
  353. else
  354. // log information
  355. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %d could not connect to video input", cd.pCamera );
  356. }
  357. else
  358. // log information
  359. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "video capture device % has no availible video inputs", pVideoCaptureDevice );
  360. }
  361. }
  362. }
  363. // goto next capture job
  364. m_List.GetNext( pos );
  365. }
  366. }
  367. }
  368. void CVideoCapture::StoreImage( LPBITMAPINFO pSrcBmi, LPBYTE pSrcBytes, CVideoCapture::CAP_DATA &cd )
  369. {
  370. if( !pSrcBmi || !pSrcBytes )
  371. {
  372. // log information
  373. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoCapture::StoreImage"), _T("can't store - bitmap properties is invalid (%p%p)"), pSrcBmi, pSrcBytes );
  374. return;
  375. }
  376. ASSERT( m_pReport );
  377. if( m_ImageJpeg.Create( pSrcBmi, pSrcBytes ) )
  378. m_pReport->StoreImage( m_ImageJpeg, &cd.liOffset );
  379. }