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.

162 lines
4.1 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: DirectShowStream.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DirectShowStream.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "DirectShowStream.h"
  17. #include "DirectShowVideo.h"
  18. #include "DirectShowSampleGrabberCB.h"
  19. CDirectShowStream::CDirectShowStream()
  20. {
  21. m_pSampleGrabber = NULL;
  22. m_pDirectShowSampleGrabberCB = NULL;
  23. m_pBitmapInfoHeader = NULL;
  24. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStream::CDirectShowStream" );
  25. }
  26. CDirectShowStream::~CDirectShowStream()
  27. {
  28. Destroy();
  29. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CDirectShowStream::~CDirectShowStream" );
  30. }
  31. BOOL CDirectShowStream::Create( ISampleGrabber *pSampleGrabber )
  32. {
  33. Destroy();
  34. if( !pSampleGrabber )
  35. return FALSE;
  36. HRESULT hr = pSampleGrabber->QueryInterface( IID_ISampleGrabber, (void**)&m_pSampleGrabber );
  37. if( FAILED(hr) )
  38. return FALSE;
  39. return InitCapturer();
  40. }
  41. void CDirectShowStream::Destroy()
  42. {
  43. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CDirectShowStream::Destroy"), _T("Started...") );
  44. // unlink all video
  45. CLgLinkP<CDirectShowStream,CDirectShowVideo>::Unlink();
  46. // unlink sample grabber
  47. CLgLinkSP<CDirectShowStream,CDirectShowSampleGrabberCB>::Unlink();
  48. if( m_pSampleGrabber )
  49. {
  50. m_pSampleGrabber->SetCallback( NULL, 1 );
  51. m_pSampleGrabber->Release();
  52. m_pSampleGrabber = NULL;
  53. }
  54. if( m_pDirectShowSampleGrabberCB )
  55. {
  56. m_pDirectShowSampleGrabberCB->Release();
  57. m_pDirectShowSampleGrabberCB = NULL;
  58. }
  59. if( m_pBitmapInfoHeader )
  60. {
  61. delete m_pBitmapInfoHeader;
  62. m_pBitmapInfoHeader = NULL;
  63. }
  64. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CDirectShowStream::Destroy"), _T("Done") );
  65. }
  66. BOOL CDirectShowStream::InitCapturer()
  67. {
  68. if( !m_pSampleGrabber )
  69. return FALSE;
  70. HRESULT hr = m_pSampleGrabber->SetOneShot( FALSE );
  71. if( FAILED( hr ) )
  72. return FALSE;
  73. hr = m_pSampleGrabber->SetBufferSamples( TRUE );
  74. if( FAILED( hr ) )
  75. return FALSE;
  76. // retrieve media type
  77. AM_MEDIA_TYPE mt;
  78. if( FAILED( m_pSampleGrabber->GetConnectedMediaType(&mt) ) )
  79. return FALSE;
  80. if( mt.formattype != FORMAT_VideoInfo )
  81. return FALSE;
  82. // store video format
  83. VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat;
  84. if( !pVideoHeader )
  85. return FALSE;
  86. // store video format
  87. m_pBitmapInfoHeader = new BITMAPINFOHEADER;
  88. if( !m_pBitmapInfoHeader )
  89. return FALSE;
  90. memcpy( m_pBitmapInfoHeader, &pVideoHeader->bmiHeader, sizeof(BITMAPINFOHEADER) );
  91. // release video format
  92. ::CoTaskMemFree( mt.pbFormat );
  93. // create ISampleGrabberCB object
  94. m_pDirectShowSampleGrabberCB = new CDirectShowSampleGrabberCB();
  95. if( !m_pDirectShowSampleGrabberCB )
  96. return FALSE;
  97. // link to itself
  98. m_pDirectShowSampleGrabberCB->Link( this );
  99. // install callback interface
  100. hr = m_pSampleGrabber->SetCallback( m_pDirectShowSampleGrabberCB, 1 );
  101. if( FAILED( hr ) )
  102. return FALSE;
  103. return TRUE;
  104. }
  105. BOOL CDirectShowStream::Link( CDirectShowVideo *pDirectShowVideo, INT nSource )
  106. {
  107. if( !pDirectShowVideo )
  108. return FALSE;
  109. // protect
  110. CLgLocker Lock( &CLgLinkP<CDirectShowStream,CDirectShowVideo>::GetLock() );
  111. // make reference
  112. CLgArray< CLgLink<CDirectShowVideo,CDirectShowStream>* >& arrVideo = *this;
  113. // check for pin avalibility
  114. for( INT i = 0; i < arrVideo.GetCount(); i++ )
  115. {
  116. CDirectShowVideo *pTempDirectShowVideo = *arrVideo[i];
  117. if( pTempDirectShowVideo->GetVideoInput() == nSource )
  118. return pTempDirectShowVideo == pDirectShowVideo;
  119. }
  120. // try to connect
  121. if( !CLgLinkP<CDirectShowStream,CDirectShowVideo>::Link( pDirectShowVideo ) )
  122. return FALSE;
  123. // store video input index
  124. pDirectShowVideo->SetVideoInput( nSource );
  125. return TRUE;
  126. }