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.

329 lines
8.7 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: LgHWVideoCamera.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgHWVideoCamera.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgHWVideoCamera.h"
  16. #include "LgHWVideoEngine.h"
  17. #include "LgHWVideoCapture.h"
  18. #include "LgHWVideoPin.h"
  19. #include "LgHWVideoSwitch.h"
  20. #include "LgHWVideoJob.h"
  21. CLgHWVideoCamera::CLgHWVideoCamera()
  22. {
  23. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgHWVideoCamera::CLgHWVideoCamera") );
  24. }
  25. CLgHWVideoCamera::~CLgHWVideoCamera()
  26. {
  27. Destroy();
  28. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgHWVideoCamera::~CLgHWVideoCamera") );
  29. }
  30. BOOL CLgHWVideoCamera::Create()
  31. {
  32. // create video device with one video output
  33. return CLgHWVideoSwitch::Create( 0, 1 );
  34. }
  35. void CLgHWVideoCamera::Destroy()
  36. {
  37. // unlock all locked pins
  38. CameraToPinLinkP::Unlink();
  39. // clear all outstanding jobs
  40. while( CameraToJobLinkP::GetCount() )
  41. delete *CameraToJobLinkP::GetAt(0);
  42. // nessesary to call for prevent pure functions call
  43. CLgHWVideoSwitch::Destroy();
  44. }
  45. BOOL CLgHWVideoCamera::HWVideoCameraActivate( BOOL bActivate )
  46. {
  47. if( bActivate )
  48. {
  49. // if already linked
  50. if( CameraToCaptureLinkSP::GetLinkThis() )
  51. {
  52. // notify about availibility
  53. OnHWVideoCameraActivate( bActivate );
  54. return TRUE;
  55. }
  56. CLgHWVideoEngine *pEngine = *this;
  57. if( !pEngine )
  58. return FALSE;
  59. // find availible video input
  60. CLgArray< CLgLink<CLgHWVideoCapture,CLgHWVideoEngine>* >& arrInput = *pEngine;
  61. for( INT i = 0; i < arrInput.GetCount(); i++ )
  62. {
  63. CLgHWVideoCapture *pCapture = *arrInput[i];
  64. if( !pCapture )
  65. {
  66. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_WARNING, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("No video capturers") );
  67. return FALSE;
  68. }
  69. // if linked to another camera
  70. if( pCapture->CaptureToCameraLinkSP::GetLinkThis() )
  71. continue;
  72. // prepare pointer to video input pin of video input
  73. CLgHWVideoPin *pPinInput = pCapture->GetVideoPinInput();
  74. if( !pPinInput )
  75. {
  76. // should be called CLgHWVideoInput::Create!!!
  77. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("CLgHWVideoInput::Create was not called") );
  78. return FALSE;
  79. }
  80. // prepare pointer to video input pin of video input
  81. CLgHWVideoPin *pPinOutput = GetVideoPinOutput();
  82. if( !pPinOutput )
  83. {
  84. // should be called CLgHWVideoCamera::Create!!!
  85. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("CLgHWVideoCamera::Create was not called") );
  86. return FALSE;
  87. }
  88. // clear graph (prepare for way calculation)
  89. if( !GraphClear( pPinInput, pPinOutput ) )
  90. {
  91. // no way availible
  92. continue;
  93. }
  94. // calculate way length
  95. if( !GraphSetCost( pPinInput, pPinOutput, 0 ) )
  96. {
  97. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("GraphSetCost failed") );
  98. return FALSE;
  99. }
  100. // lock way
  101. if( !GraphWayLock( pPinInput, pPinOutput ) )
  102. {
  103. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoCamera::HWVideoCameraActivate"), _T("GraphWayLock failed") );
  104. return FALSE;
  105. }
  106. // if there are no switching jobs, then line is ready
  107. if( !CameraToJobLinkP::GetCount() )
  108. OnHWVideoCameraActivate( bActivate );
  109. // lock video input
  110. CameraToCaptureLinkSP::Link( pCapture );
  111. break;
  112. }
  113. }
  114. else
  115. {
  116. // if already unlinked
  117. if( !CameraToCaptureLinkSP::GetLinkThis() )
  118. {
  119. // notify about availibility
  120. OnHWVideoCameraActivate( bActivate );
  121. return TRUE;
  122. }
  123. // unlock
  124. GraphWayUnlock();
  125. }
  126. return TRUE;
  127. }
  128. BOOL CLgHWVideoCamera::GraphClear( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput )
  129. {
  130. if( !pPinOutput || !pPinInput )
  131. return FALSE;
  132. // get hard linked pin
  133. CLgHWVideoPin *pDevicePinInput = pPinOutput->PinToOutsideLinkSP::GetLinkThis();
  134. if( !pDevicePinInput )
  135. return FALSE;
  136. // set way cost
  137. pPinOutput->SetCost( INT_MAX );
  138. // if pin reached
  139. if( pDevicePinInput == pPinInput )
  140. return TRUE;
  141. // get device
  142. CLgHWVideoSwitch *pDevice = pDevicePinInput->PinToSwitchInputLinkSP::GetLinkThis();
  143. if( !pDevice )
  144. return FALSE;
  145. // enumerate all outputs of device
  146. BOOL bWayFound = FALSE;
  147. CLgArray< CLgLink<CLgHWVideoPin,CLgHWVideoSwitch,CLgHWVideoPinOutput>* > &arrDeviceOutput = *pDevice;
  148. for( INT i = 0; i < arrDeviceOutput.GetCount(); i++ )
  149. {
  150. CLgHWVideoPin *pDevicePinOutput = *arrDeviceOutput[i];
  151. if( !pDevicePinOutput )
  152. return FALSE;
  153. if( GraphClear( pPinInput, pDevicePinOutput ) )
  154. bWayFound = TRUE;
  155. }
  156. return bWayFound;
  157. }
  158. BOOL CLgHWVideoCamera::GraphSetCost( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput, INT nCost )
  159. {
  160. if( !pPinOutput || !pPinInput )
  161. return FALSE;
  162. // get hard linked pin
  163. CLgHWVideoPin *pDevicePinInput = pPinOutput->PinToOutsideLinkSP::GetLinkThis();
  164. if( !pDevicePinInput )
  165. return FALSE;
  166. // set way cost
  167. pPinOutput->SetCost( nCost );
  168. // connection established
  169. if( pPinInput == pDevicePinInput )
  170. return TRUE;
  171. // get device
  172. CLgHWVideoSwitch *pDevice = pDevicePinInput->PinToSwitchInputLinkSP::GetLinkThis();
  173. if( !pDevice )
  174. return FALSE;
  175. // enumerate all outputs of device
  176. BOOL bInputFound = FALSE;
  177. CLgArray< CLgLink<CLgHWVideoPin,CLgHWVideoSwitch,CLgHWVideoPinOutput>* > &arrDeviceOutput = *pDevice;
  178. for( INT i = 0; i < arrDeviceOutput.GetCount(); i++ )
  179. {
  180. CLgHWVideoPin *pDevicePinOutput = *arrDeviceOutput[i];
  181. if( !pDevicePinOutput )
  182. return FALSE;
  183. // if pin already locked
  184. if( pDevicePinOutput->PinToCameraLinkSP::GetCount() )
  185. continue;
  186. // if cost already had been set
  187. if( pDevicePinOutput->GetCost() != INT_MAX )
  188. continue;
  189. // set cost in subgraph (true if pin is reached)
  190. if( GraphSetCost( pPinInput, pDevicePinOutput, nCost + 1 ) )
  191. bInputFound = TRUE;
  192. }
  193. // input pin was not reached
  194. return bInputFound;
  195. }
  196. BOOL CLgHWVideoCamera::GraphWayLock( CLgHWVideoPin *pPinInput, CLgHWVideoPin *pPinOutput )
  197. {
  198. if( !pPinOutput || !pPinInput )
  199. return FALSE;
  200. // lock input pin
  201. CameraToPinLinkP::Link( pPinInput );
  202. // get hard linked pin
  203. CLgHWVideoPin *pDevicePinOutput = pPinInput->PinToOutsideLinkSP::GetLinkThis();
  204. if( !pDevicePinOutput )
  205. return FALSE;
  206. // lock hard linked pin
  207. CameraToPinLinkP::Link( pDevicePinOutput );
  208. // connection established
  209. if( pPinOutput == pDevicePinOutput )
  210. return TRUE;
  211. // get device
  212. CLgHWVideoSwitch *pDevice = pDevicePinOutput->PinToSwitchOutputLinkSP::GetLinkThis();
  213. if( !pDevice )
  214. return FALSE;
  215. // choose appropriate input on device
  216. INT nMinWayCost = INT_MAX;
  217. CLgHWVideoPin *pDevicePinInputBest = NULL;
  218. CLgArray< CLgLink<CLgHWVideoPin,CLgHWVideoSwitch,CLgHWVideoPinInput>* > &arrDeviceInput = *pDevice;
  219. for( INT i = 0; i < arrDeviceInput.GetCount(); i++ )
  220. {
  221. CLgHWVideoPin *pDevicePinInput = *arrDeviceInput[i];
  222. if( !pDevicePinInput )
  223. return FALSE;
  224. // if pin already locked
  225. if( pDevicePinOutput->PinToCameraLinkSP::GetCount() )
  226. continue;
  227. if( pDevicePinInput->GetCost() < nMinWayCost )
  228. {
  229. nMinWayCost = pDevicePinInput->GetCost();
  230. pDevicePinInputBest = pDevicePinInput;
  231. }
  232. }
  233. // unexpected situation
  234. if( !pDevicePinInputBest )
  235. return FALSE;
  236. // create job
  237. CLgHWVideoJob *pJob = new CLgHWVideoJob;
  238. if( !pJob )
  239. return FALSE;
  240. // initialize job (order is very inportent!!!)
  241. pJob->JobToOutputLinkSP::Link( pDevicePinOutput );
  242. pJob->JobToInputLinkSP::Link( pDevicePinInputBest );
  243. pJob->JobToCameraLinkSP::Link( this );
  244. pJob->JobToSwitchLinkSP::Link( pDevice );
  245. // lock pin
  246. CameraToPinLinkP::Link( pDevicePinInputBest );
  247. // find remaining way
  248. return GraphWayLock( pDevicePinInputBest, pPinOutput );
  249. }
  250. BOOL CLgHWVideoCamera::GraphWayUnlock()
  251. {
  252. // unlock all switches
  253. CameraToPinLinkP::Unlink();
  254. // clear all jobs
  255. while( CameraToJobLinkP::GetCount() )
  256. delete *CameraToJobLinkP::GetAt(0);
  257. // unlink video input
  258. CameraToCaptureLinkSP::Unlink();
  259. // notify about availibility
  260. OnHWVideoCameraActivate( FALSE );
  261. return TRUE;
  262. }
  263. BOOL CLgHWVideoCamera::HWVideoCameraSampleGet( INT nSampleID )
  264. {
  265. CLgHWVideoCapture *pCapture = *this;
  266. if( !pCapture )
  267. return FALSE;
  268. // pass sample to connected camera
  269. return pCapture->HWVideoInputSampleGet( nSampleID );
  270. }