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.

469 lines
12 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: 12/11/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: VideoBase.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of VideoBase.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "chicago.h"
  16. #include "VideoBase.h"
  17. #include "..\DLIB\VideoWnd.h"
  18. #include "VideoFrameWnd.h"
  19. #include "VideoListWnd.h"
  20. #include "..\dlib\Settings.h"
  21. #include "..\..\lglib\video\directshowstreamvideo.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27. CVideoBase::CVideoBase()
  28. {
  29. // m_pDSConfiguration = NULL;
  30. m_nVideo = 0;
  31. m_pVideoFrameWnd = NULL;
  32. m_pVideoListWnd = NULL;
  33. m_pCameraNavigateDlg = NULL;
  34. m_pSensorArray = NULL;
  35. m_bCameraSwitchLock = FALSE;
  36. m_nVideoFormat = VF_NONE;
  37. m_pSettings = NULL;
  38. m_nVideoFPS = 3;
  39. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoBase::CVideoBase") );
  40. }
  41. CVideoBase::~CVideoBase()
  42. {
  43. Destroy();
  44. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoBase::~CVideoBase") );
  45. }
  46. BOOL CVideoBase::InitDirectShow(CWnd *pBaseWnd, CSettings *pSettings )
  47. {
  48. // normal processing
  49. if( !m_DirectShowBase.Create() )
  50. return FALSE;
  51. if( !pSettings )
  52. return FALSE;
  53. m_pSettings = pSettings;
  54. m_nVideoFormat = VF_PAL;
  55. m_pSettings->Load( _T("VIDEO_STANDART"), &m_nVideoFormat );
  56. m_nVideoFPS = 0;
  57. m_pSettings->Load( _T("VIDEO_FPS"), &m_nVideoFPS );
  58. if( !pBaseWnd )
  59. return FALSE;
  60. for( int i = 0; i < m_DirectShowBase.GetDirectShowStreamCount(); i++ )
  61. {
  62. CDirectShowStreamVideo *pDirectShowStream = m_DirectShowBase.GetDirectShowStream( i );
  63. /* switch( m_nVideoFormat )
  64. {
  65. case VF_PAL: pDirectShowStream->SetVideoStandart( AnalogVideo_PAL_B ); break;
  66. case VF_NTSC: pDirectShowStream->SetVideoStandart( AnalogVideo_NTSC_M ); break;
  67. }
  68. */
  69. pDirectShowStream->SetVideoFPS( m_nVideoFPS );
  70. for( int j = 0; j < pDirectShowStream->GetVideoSourceCount(); j++ )
  71. {
  72. CDDVideoWnd *pDDVideoWnd = new CDDVideoWnd( m_pSensorArray );
  73. if( !pDDVideoWnd )
  74. break;
  75. if( !pDDVideoWnd->Create( pBaseWnd, -100, -100, 16, 12, NULL ) )
  76. {
  77. delete pDDVideoWnd;
  78. break;
  79. }
  80. m_ArrayDDVideoWnd.Add( pDDVideoWnd );
  81. }
  82. }
  83. return m_DirectShowBase.Start();
  84. }
  85. BOOL CVideoBase::InitVideoForWindows( CWnd* pBaseWnd, CSettings* )
  86. {
  87. /* CVideoForWindowsWnd *pVFW = new CVideoForWindowsWnd;
  88. if( !pVFW )
  89. return FALSE;
  90. if( pVFW->Create( pBaseWnd, -100, -100, 16, 12 ) )
  91. {
  92. m_ArrayDDVideoWnd.Add( pVFW );
  93. return TRUE;
  94. }
  95. delete pVFW;
  96. */
  97. return FALSE;
  98. }
  99. BOOL CVideoBase::Create( CWnd *pBaseWnd, CSettings *pSettings )
  100. {
  101. // return TRUE;
  102. Destroy();
  103. BOOL bError = FALSE;
  104. // if demo mode
  105. if( m_pSensorArray->IsDemo() )
  106. {
  107. TCHAR pName[][MAX_PATH] =
  108. {
  109. "demo1.avi",
  110. "demo2.avi",
  111. };
  112. for( INT i = 0; i < 2; i++ )
  113. {
  114. if( !m_DirectShowStreamMovie[i].Create( pName[i] ) )
  115. continue;
  116. CDDVideoWnd *pDDVideoWnd = new CDDVideoWnd( m_pSensorArray );
  117. if( !pDDVideoWnd )
  118. continue;
  119. if( !pDDVideoWnd->Create( pBaseWnd, -100, -100, 16, 12, NULL ) )
  120. {
  121. delete pDDVideoWnd;
  122. continue;
  123. }
  124. m_DirectShowStreamMovie[i].Link( pDDVideoWnd, 0 );
  125. pDDVideoWnd->SetVideoBase( this );
  126. m_ArrayDDVideoWnd.Add( pDDVideoWnd );
  127. }
  128. }
  129. else
  130. {
  131. if( !InitDirectShow( pBaseWnd, pSettings ))
  132. bError = TRUE;
  133. }
  134. if( !bError )
  135. {
  136. for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ )
  137. {
  138. CVideoCaptureDevice *pDevice = m_pSensorArray->GetVideoCaptureDevice(i);
  139. INT nVideoStandard = AnalogVideo_PAL_B;
  140. switch( pDevice ? pDevice->GetVideoStandard() : 0 )
  141. {
  142. case 0: nVideoStandard = AnalogVideo_PAL_B; break;
  143. case 1: nVideoStandard = AnalogVideo_NTSC_M; break;
  144. }
  145. for( INT j = 0; j < m_pSensorArray->GetVideoCaptureDevice(i)->GetInputCount(); j++ )
  146. {
  147. // if pin connected to any device
  148. if( pDevice->GetInput(j)->GetCPin() )
  149. {
  150. CVideoCaptureWnd *pDDVideoWnd = AllocVideoWnd( pDevice->GetVideoDeviceID() ? (pDevice->GetVideoDeviceID() - 1) : 0, j, nVideoStandard );
  151. if( !pDDVideoWnd )
  152. {
  153. bError = TRUE;
  154. break;
  155. }
  156. pDevice->AssignCaptureSource( j, pDDVideoWnd );
  157. }
  158. }
  159. }
  160. }
  161. return !bError;
  162. }
  163. void CVideoBase::Destroy()
  164. {
  165. if( m_pSettings )
  166. {
  167. m_pSettings->Save( _T("VIDEO_STANDART"), m_nVideoFormat );
  168. m_pSettings->Save( _T("VIDEO_FPS"), m_nVideoFPS );
  169. m_pSettings = NULL;
  170. }
  171. m_nVideoFormat = VF_NONE;
  172. m_DirectShowBase.Destroy();
  173. m_DirectShowStreamMovie[0].Destroy();
  174. m_DirectShowStreamMovie[1].Destroy();
  175. m_DirectShowStreamMovie[2].Destroy();
  176. m_DirectShowStreamMovie[3].Destroy();
  177. for( int i = 0; i < m_ArrayDDVideoWnd.GetCount(); i++ )
  178. {
  179. if( m_ArrayDDVideoWnd[i] )
  180. {
  181. m_ArrayDDVideoWnd[i]->DestroyWindow();
  182. delete m_ArrayDDVideoWnd[i];
  183. }
  184. }
  185. m_ArrayDDVideoWnd.RemoveAll();
  186. m_nVideo = 0;
  187. }
  188. CVideoCaptureWnd *CVideoBase::AllocVideoWnd( INT nBoard, INT nInput, INT nVideoStandard )
  189. {
  190. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoBase::AllocVideoWnd"), _T("Board %d, Input %d"), nBoard, nInput );
  191. // if demonstration mode
  192. if( m_pSensorArray->IsDemo() )
  193. {
  194. if( m_ArrayDDVideoWnd.GetCount() > m_nVideo )
  195. {
  196. m_ArrayDDVideoWnd[m_nVideo]->SetVideoBase( this );
  197. return m_ArrayDDVideoWnd[ m_nVideo++ ];
  198. }
  199. return NULL;
  200. }
  201. // normal processing
  202. for( INT i = 0; i < m_DirectShowBase.GetDirectShowStreamCount(); i++ )
  203. {
  204. CDirectShowStreamVideo *pDirectShowStream = m_DirectShowBase.GetDirectShowStream( i );
  205. for( int j = 0; j < pDirectShowStream->GetVideoSourceCount(); j++ )
  206. {
  207. if( i == nBoard && j == nInput )
  208. {
  209. pDirectShowStream->Link( static_cast<CDirectShowVideo*>( static_cast<CDDVideoWnd*>( m_ArrayDDVideoWnd[m_nVideo] ) ), j );
  210. pDirectShowStream->SetVideoStandart( nVideoStandard );
  211. m_ArrayDDVideoWnd[m_nVideo]->SetVideoBase( this );
  212. return m_ArrayDDVideoWnd[ m_nVideo++ ];
  213. }
  214. }
  215. }
  216. if( m_ArrayDDVideoWnd.GetCount() > m_nVideo )
  217. {
  218. m_ArrayDDVideoWnd[ m_nVideo ]->SetVideoBase( this );
  219. return m_ArrayDDVideoWnd[ m_nVideo++ ];
  220. }
  221. return NULL;
  222. }
  223. void CVideoBase::SetControlledVideo( CVideoCaptureWnd *pNewWnd )
  224. {
  225. if( m_bCameraSwitchLock )
  226. return;
  227. if( !m_pVideoFrameWnd )
  228. return;
  229. if( !m_pVideoListWnd )
  230. return;
  231. if( !m_pCameraNavigateDlg )
  232. return;
  233. if( !pNewWnd )
  234. return;
  235. // compare with current window
  236. CVideoCaptureWnd *pCurrentWnd = static_cast<CVideoCaptureWnd*>( m_pVideoFrameWnd->GetVideoWnd() );
  237. if( pCurrentWnd == pNewWnd )
  238. {
  239. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoBase::SetControlledVideo", "Video %p already is in main video wnd", pCurrentWnd );
  240. // IMPORTENT!!!: notify camera control dlg about video switching
  241. m_pCameraNavigateDlg->SetVideoSource( pNewWnd );
  242. return;
  243. }
  244. // remove new window from "video list" wnd
  245. if( !m_pVideoListWnd->Remove( pNewWnd ) )
  246. {
  247. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CVideoBase::SetControlledVideo", "Can't remove new video wnd from list" );
  248. return;
  249. }
  250. // add old video window to "video list"
  251. m_pVideoListWnd->Add( pCurrentWnd );
  252. // assign new video for main video viewport
  253. m_pVideoFrameWnd->SetVideoWnd( pNewWnd );
  254. // notify camera control dlg about video switching
  255. m_pCameraNavigateDlg->SetVideoSource( pNewWnd );
  256. // log information
  257. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoBase::SetControlledVideo", "Set video wnd %p", pNewWnd );
  258. }
  259. // NOTE: should be rewitten !!!
  260. void CVideoBase::Initialize()
  261. {
  262. // log info
  263. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoBase::Initialize", "Start..." );
  264. ASSERT( m_pSensorArray );
  265. for( int nCamera = 0; nCamera < m_pSensorArray->GetVideoCameraCount(); nCamera++ )
  266. {
  267. CVideoCamera *pVideoCamera = m_pSensorArray->GetVideoCamera( nCamera );
  268. ASSERT( pVideoCamera );
  269. for( int nVideoCaptureDevice = 0; nVideoCaptureDevice < m_pSensorArray->GetVideoCaptureDeviceCount(); nVideoCaptureDevice++ )
  270. {
  271. CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice( nVideoCaptureDevice );
  272. if( !pVideoCaptureDevice )
  273. continue;
  274. int nVideoInputPin = pVideoCaptureDevice->GetAvailiblePin( pVideoCamera->GetOutput(0) );
  275. if( nVideoInputPin < 0 )
  276. continue;
  277. CVideoCaptureSource *pVideoCaptureSource = pVideoCaptureDevice->GetCaptureSource( nVideoInputPin );
  278. if( !pVideoCaptureSource )
  279. continue;
  280. for( int nSensor = 0; nSensor < m_pSensorArray->GetSensorCount(); nSensor++ )
  281. {
  282. CSensor *pSensor = m_pSensorArray->GetSensor( nSensor );
  283. if( !pSensor )
  284. continue;
  285. // update maximum image count
  286. if( pVideoCamera == pSensor->GetVideoCamera() )
  287. pVideoCaptureSource->UpdateMaxPreviewImageCount( pSensor->GetImageCount() );
  288. }
  289. }
  290. }
  291. for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ )
  292. {
  293. CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice( i );
  294. ASSERT( pVideoCaptureDevice );
  295. for( int nInputPin = 0; nInputPin < m_ArrayDDVideoWnd.GetCount(); nInputPin++ )
  296. {
  297. for( int nCamera = 0; nCamera < m_pSensorArray->GetVideoCameraCount(); nCamera++ )
  298. {
  299. CVideoCamera *pVideoCamera = m_pSensorArray->GetVideoCamera( nCamera );
  300. ASSERT( pVideoCamera );
  301. if( !pVideoCamera->IsLock() )
  302. {
  303. int nWeight = pVideoCaptureDevice->GetInput( nInputPin )->TestConnection( pVideoCamera->GetOutput(0) );
  304. if( nWeight < INT_MAX )
  305. {
  306. pVideoCamera->Lock();
  307. pVideoCamera->Connect( pVideoCaptureDevice->GetInput( nInputPin ) );
  308. pVideoCamera->AssignCaptureSource( pVideoCaptureDevice->GetCaptureSource( nInputPin ) );
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. for( nCamera = 0; nCamera < m_pSensorArray->GetVideoCameraCount(); nCamera++ )
  316. {
  317. CVideoCamera *pVideoCamera = m_pSensorArray->GetVideoCamera( nCamera );
  318. ASSERT( pVideoCamera );
  319. if( pVideoCamera->IsLock() )
  320. pVideoCamera->Unlock();
  321. }
  322. // log info
  323. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoBase::Initialize", "Done." );
  324. }
  325. void CVideoBase::UpdateInfo(BOOL bOddUpdate)
  326. {
  327. }
  328. BOOL CVideoBase::SetVideoFPS( INT nFPS )
  329. {
  330. if( m_nVideoFPS == nFPS )
  331. return TRUE;
  332. m_nVideoFPS = nFPS;
  333. for( int i = 0; i < m_DirectShowBase.GetDirectShowStreamCount(); i++ )
  334. {
  335. CDirectShowStreamVideo *pDirectShowStream = m_DirectShowBase.GetDirectShowStream( i );
  336. pDirectShowStream->SetVideoFPS( m_nVideoFPS );
  337. }
  338. if( m_pSettings )
  339. m_pSettings->Save( _T("VIDEO_FPS"), m_nVideoFPS );
  340. return TRUE;
  341. }
  342. BOOL CVideoBase::SetVideoFormat( INT nFormat )
  343. {
  344. if( m_nVideoFormat == nFormat )
  345. return TRUE;
  346. m_nVideoFormat = nFormat;
  347. for( int i = 0; i < m_DirectShowBase.GetDirectShowStreamCount(); i++ )
  348. {
  349. CDirectShowStreamVideo *pDirectShowStream = m_DirectShowBase.GetDirectShowStream( i );
  350. switch( m_nVideoFormat )
  351. {
  352. case VF_PAL: pDirectShowStream->SetVideoStandart( AnalogVideo_PAL_B ); break;
  353. case VF_NTSC: pDirectShowStream->SetVideoStandart( AnalogVideo_NTSC_M ); break;
  354. }
  355. }
  356. // restart video engine
  357. m_DirectShowBase.Stop();
  358. m_DirectShowBase.Start();
  359. if( m_pSettings )
  360. m_pSettings->Save( _T("VIDEO_STANDART"), m_nVideoFormat );
  361. return FALSE;
  362. }
  363. void CVideoBase::UpdateDemo( CVideoCaptureWnd* pVideoWnd, double SampleTime )
  364. {
  365. if( m_pSensorArray->IsDemo() )
  366. {
  367. for( INT i = 0; i < m_ArrayDDVideoWnd.GetCount(); i++ )
  368. {
  369. if( m_ArrayDDVideoWnd[i] == pVideoWnd )
  370. {
  371. m_pSensorArray->UpdateDemo( i, SampleTime );
  372. return;
  373. }
  374. }
  375. }
  376. }
  377. void CVideoBase::OnChangeSettings()
  378. {
  379. for( INT i = 0; i < m_ArrayDDVideoWnd.GetCount(); i++ )
  380. static_cast<CDDVideoWnd*>(m_ArrayDDVideoWnd[i])->OnChangeSettings() ;
  381. }