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.

228 lines
6.5 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: LgHWVideoSwitch.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgHWVideoSwitchDevice.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. CLgHWVideoSwitch::CLgHWVideoSwitch()
  22. {
  23. // initialize request counter
  24. m_nRequestCount = 0;
  25. }
  26. CLgHWVideoSwitch::~CLgHWVideoSwitch()
  27. {
  28. Destroy();
  29. }
  30. BOOL CLgHWVideoSwitch::Create( INT nInput, INT nOutput )
  31. {
  32. // initialize inputs
  33. for( INT i = 0; i < nInput; i++ )
  34. {
  35. CLgHWVideoPin *pInput = new CLgHWVideoPin;
  36. if( !pInput )
  37. {
  38. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::Create"), _T("new CLgHWVideoPin for input failed") );
  39. return FALSE;
  40. }
  41. // set pin index
  42. pInput->SetIndex( i );
  43. // link input to itself
  44. if( !SwitchToInputLinkP::Link( pInput ) )
  45. {
  46. delete pInput;
  47. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::Create"), _T("Link( pInput ) failed") );
  48. return FALSE;
  49. }
  50. }
  51. // initialize outputs
  52. for( i = 0; i < nOutput; i++ )
  53. {
  54. CLgHWVideoPin *pOutput = new CLgHWVideoPin;
  55. if( !pOutput )
  56. {
  57. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::Create"), _T("new CLgHWVideoPin for output failed") );
  58. return FALSE;
  59. }
  60. // set pin index
  61. pOutput->SetIndex( i );
  62. // link output to itself
  63. if( !SwitchToOutputLinkP::Link( pOutput ) )
  64. {
  65. delete pOutput;
  66. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::Create"), _T("Link( pOutput ) failed") );
  67. return FALSE;
  68. }
  69. }
  70. return TRUE;
  71. }
  72. void CLgHWVideoSwitch::Destroy()
  73. {
  74. // delete all pins
  75. CLgLocker LockInput( &SwitchToInputLinkP::GetLock() );
  76. while( SwitchToInputLinkP::GetCount() )
  77. delete *SwitchToInputLinkP::GetAt(0);
  78. CLgLocker LockOutput( &SwitchToOutputLinkP::GetLock() );
  79. while( SwitchToOutputLinkP::GetCount() )
  80. delete *SwitchToOutputLinkP::GetAt(0);
  81. SwitchToJobLinkP::Unlink();
  82. }
  83. BOOL CLgHWVideoSwitch::Link( CLgLink<CLgHWVideoJob,CLgHWVideoSwitch> *pJob )
  84. {
  85. if( !SwitchToJobLinkP::Link( pJob ) )
  86. return FALSE;
  87. return CheckJobArray();
  88. }
  89. // notification about switching
  90. void CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch( INT nInput, INT nOutput )
  91. {
  92. // protect from simultaneus access
  93. CLgLocker LockOutput( &SwitchToOutputLinkP::GetLock() );
  94. CLgLocker LockInput( &SwitchToInputLinkP::GetLock() );
  95. CLgArray< CLgLink<CLgHWVideoPin,CLgHWVideoSwitch,CLgHWVideoPinOutput>* >& arrOutput = *this;
  96. CLgArray< CLgLink<CLgHWVideoPin,CLgHWVideoSwitch,CLgHWVideoPinInput>* >& arrInput = *this;
  97. // validate input and output
  98. if( nInput < 0 || nInput >= arrInput.GetCount() || nOutput < 0 || nOutput >= arrOutput.GetCount() )
  99. {
  100. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch"), _T("Parameters are incorrect (%d,%d)"), nInput, nOutput );
  101. return;
  102. }
  103. // perform internal interconnection
  104. CLgHWVideoPin *pInputPin = *arrInput[nInput];
  105. CLgHWVideoPin *pOutputPin = *arrOutput[nOutput];
  106. // unlink previous associations
  107. pInputPin->PinToInsideLinkSP::Unlink();
  108. pOutputPin->PinToInsideLinkSP::Unlink();
  109. // establish new association
  110. if( !pInputPin->PinToInsideLinkSP::Link( pOutputPin ) )
  111. {
  112. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch"), _T("Link failed") );
  113. return;
  114. }
  115. // protect from simultaneus access ???
  116. CLgLocker LockJobOutput( &pInputPin->PinToJobOutputLinkSP::GetLock() );
  117. CLgLocker LockJobInput( &pInputPin->PinToJobInputLinkSP::GetLock() );
  118. CLgHWVideoJob *pJobInput = pInputPin->PinToJobInputLinkSP::GetLinkThis();
  119. CLgHWVideoJob *pJobOutput = pOutputPin->PinToJobOutputLinkSP::GetLinkThis();
  120. if( pJobInput != pJobOutput )
  121. {
  122. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch"), _T("Job input is not equal job output") );
  123. return;
  124. }
  125. // delete job due its performation
  126. delete pJobInput;
  127. /*
  128. CLgArray< CLgLink<CLgHWVideoJob,CLgHWVideoSwitch>* >& arrJob = *this;
  129. // update jobs array
  130. for( INT i = 0; i < arrJob.GetCount(); i++ )
  131. {
  132. CLgHWVideoJob *pJob = *arrJob[i];
  133. if( !pJob )
  134. {
  135. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch"), _T("Job is NULL") );
  136. return;
  137. }
  138. CLgHWVideoPin *pInput = *pJob;
  139. CLgHWVideoPin *pOutput = *pJob;
  140. if( !pInput || !pOutput )
  141. {
  142. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::OnHWVideoSwitchDeviceSwitch"), _T("Job has wrong input or output associations (%p,%p)"), pInput, pOutput );
  143. return;
  144. }
  145. // if job had been done
  146. if( pInput->GetIndex() == nInput && pOutput->GetIndex() == nOutput )
  147. {
  148. delete pJob;
  149. break;
  150. }
  151. }
  152. */
  153. // decrease number of issued requests
  154. m_nRequestCount--;
  155. // start next job
  156. CheckJobArray();
  157. }
  158. BOOL CLgHWVideoSwitch::CheckJobArray()
  159. {
  160. // protect from simultaneus access
  161. CLgLocker LockJob( &SwitchToJobLinkP::GetLock() );
  162. CLgArray< CLgLink<CLgHWVideoJob,CLgHWVideoSwitch>* >& arrJob = *this;
  163. // if all jobs had been done or there is outstanding request
  164. if( m_nRequestCount || !arrJob.GetCount() )
  165. return TRUE;
  166. // get next job
  167. CLgHWVideoJob *pJob = *arrJob[0];
  168. if( !pJob )
  169. {
  170. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::CheckJobArray"), _T("Job is NULL") );
  171. return FALSE;
  172. }
  173. // protect from simultaneus access
  174. CLgLocker LockJobOutput( &pJob->JobToOutputLinkSP::GetLock() );
  175. CLgLocker LockJobInput( &pJob->JobToInputLinkSP::GetLock() );
  176. // get job pins
  177. CLgHWVideoPin *pInput = pJob->JobToInputLinkSP::GetLinkThis();
  178. CLgHWVideoPin *pOutput = pJob->JobToOutputLinkSP::GetLinkThis();
  179. if( !pInput || !pOutput )
  180. {
  181. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgHWVideoSwitch::CheckJobArray"), _T("Job has wrong input or output associations (%p,%p)"), pInput, pOutput );
  182. return FALSE;
  183. }
  184. // start next job
  185. if( !HWVideoSwitchDeviceSwitch( pInput->GetIndex(), pOutput->GetIndex() ) )
  186. return FALSE;
  187. // increase number of issued requests
  188. m_nRequestCount++;
  189. return TRUE;
  190. }