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.

432 lines
11 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: 04/03/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: DirectShowMovieStream.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DirectShowMovieStream.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include <Qedit.h>
  17. #include <Tchar.h>
  18. #include <stdio.h>
  19. #include "DirectShowStreamMovie.h"
  20. #include "DirectShowSampleGrabberCB.h"
  21. #include "DirectShowVideo.h"
  22. CDirectShowStreamMovie::CDirectShowStreamMovie()
  23. {
  24. m_pGraphBuilder = NULL;
  25. m_pMediaControl = NULL;
  26. m_pMediaEvent = NULL;
  27. m_pVideoWindow = NULL;
  28. m_pMediaSeeking = NULL;
  29. m_pSampleGrabber = NULL;
  30. m_pBitmapInfoHeader = NULL;
  31. ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
  32. }
  33. CDirectShowStreamMovie::~CDirectShowStreamMovie()
  34. {
  35. Destroy();
  36. ::CoUninitialize();
  37. }
  38. BOOL CDirectShowStreamMovie::Create( LPCTSTR pFileName )
  39. {
  40. Destroy();
  41. // Create the filter graph.
  42. HRESULT hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&m_pGraphBuilder );
  43. if( FAILED(hr) )
  44. return FALSE;
  45. // TCHAR pName[MAX_PATH];
  46. // ::WideCharToMultiByte( CP_ACP, 0, PinInfo.achName, -1, pName, MAX_PATH, 0, 0 );
  47. WCHAR wFileName[MAX_PATH];
  48. ::MultiByteToWideChar( CP_ACP, 0, pFileName, -1, wFileName, MAX_PATH );
  49. // Have the graph builder construct its the appropriate graph automatically
  50. hr = m_pGraphBuilder->RenderFile( wFileName, NULL );
  51. if( FAILED(hr) )
  52. return FALSE;
  53. // QueryInterface for DirectShow interfaces
  54. hr = m_pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl);
  55. if( FAILED(hr) )
  56. return FALSE;
  57. hr = m_pGraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&m_pMediaEvent);
  58. if( FAILED(hr) )
  59. return FALSE;
  60. hr = m_pGraphBuilder->QueryInterface(IID_IMediaSeeking, (void **)&m_pMediaSeeking);
  61. if( FAILED(hr) )
  62. return FALSE;
  63. /*
  64. hr = m_pGraphBuilder->QueryInterface(IID_IMediaPosition, (void **)&pMP);
  65. if( FAILED(hr) )
  66. return FALSE;
  67. */
  68. // create grabber filter
  69. IBaseFilter *pGrabberFilter = NULL;
  70. hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&pGrabberFilter );
  71. if( FAILED(hr) )
  72. return FALSE;
  73. // create sample grabber
  74. ISampleGrabber *pSampleGrabber = NULL;
  75. hr = pGrabberFilter->QueryInterface( IID_ISampleGrabber, (void **)&pSampleGrabber );
  76. if( FAILED(hr) )
  77. {
  78. pGrabberFilter->Release();
  79. return FALSE;
  80. }
  81. // add grabber filter to filter graph
  82. hr = m_pGraphBuilder->AddFilter( pGrabberFilter, L"Grabber" );
  83. if( FAILED(hr) )
  84. {
  85. pGrabberFilter->Release();
  86. pSampleGrabber->Release();
  87. return FALSE;
  88. }
  89. // set video capture format
  90. AM_MEDIA_TYPE mt;
  91. ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
  92. mt.majortype = MEDIATYPE_Video;
  93. mt.subtype = MEDIASUBTYPE_RGB24;
  94. mt.formattype = FORMAT_VideoInfo;
  95. hr = pSampleGrabber->SetMediaType(&mt);
  96. if( FAILED(hr) )
  97. {
  98. pGrabberFilter->Release();
  99. pSampleGrabber->Release();
  100. return FALSE;
  101. }
  102. // Query for video interfaces
  103. IVideoWindow *pVW = NULL;
  104. hr = m_pGraphBuilder->QueryInterface(IID_IVideoWindow, (void **)&pVW);
  105. if( FAILED(hr) )
  106. return FALSE;
  107. pVW->put_Visible( OAFALSE );
  108. pVW->put_AutoShow( OAFALSE );
  109. pVW->Release();
  110. // insert grabber filter
  111. IBaseFilter *pVideoRenderer = FindVideoRenderer( m_pGraphBuilder );
  112. if( !pVideoRenderer )
  113. return FALSE;
  114. AppendGrabber( pVideoRenderer, pGrabberFilter );
  115. pVideoRenderer->Release();
  116. pGrabberFilter->Release();
  117. // Run the graph to play the media file
  118. hr = m_pMediaControl->Run();
  119. if( FAILED(hr) )
  120. return FALSE;
  121. return CThread::Start( FALSE );
  122. }
  123. void CDirectShowStreamMovie::Destroy()
  124. {
  125. CThread::Stop( TRUE );
  126. }
  127. BOOL CDirectShowStreamMovie::AppendGrabber( IBaseFilter *pVideoRenderer, IBaseFilter *pGrabberFilter )
  128. {
  129. if( !pVideoRenderer || !pGrabberFilter )
  130. return FALSE;
  131. // get pins enumeration
  132. IEnumPins *pEnumPins = NULL;
  133. HRESULT hr = pVideoRenderer->EnumPins( &pEnumPins );
  134. if( FAILED(hr) )
  135. return FALSE;
  136. IPin *pPinIn = NULL;
  137. while( pEnumPins->Next( 1, &pPinIn, NULL ) == S_OK )
  138. {
  139. // get pin info
  140. PIN_INFO PinInfo;
  141. hr = pPinIn->QueryPinInfo( &PinInfo );
  142. if( FAILED( hr ) )
  143. {
  144. pPinIn->Release();
  145. pPinIn = NULL;
  146. continue;
  147. }
  148. // check if it is output pin
  149. if( PinInfo.dir == PINDIR_OUTPUT )
  150. {
  151. pPinIn->Release();
  152. pPinIn = NULL;
  153. continue;
  154. }
  155. // get connected pin
  156. IPin *pPinOut = NULL;
  157. hr = pPinIn->ConnectedTo( &pPinOut );
  158. if( FAILED( hr ) )
  159. {
  160. pPinIn->Release();
  161. pPinIn = NULL;
  162. continue;
  163. }
  164. // get pin info
  165. hr = pPinIn->QueryPinInfo( &PinInfo );
  166. if( FAILED( hr ) )
  167. {
  168. pPinIn->Release();
  169. pPinIn = NULL;
  170. pPinOut->Release();
  171. continue;
  172. }
  173. // disconnect pins
  174. if( SUCCEEDED( m_pGraphBuilder->Disconnect( pPinOut ) ) &&
  175. SUCCEEDED( m_pGraphBuilder->Disconnect( pPinIn ) ) )
  176. {
  177. IEnumPins *pEnumGrabberPins = NULL;
  178. hr = pGrabberFilter->EnumPins( &pEnumGrabberPins );
  179. if( SUCCEEDED(hr) )
  180. {
  181. IPin *pGrabberPin = NULL;
  182. while( pEnumGrabberPins->Next( 1, &pGrabberPin, NULL ) == S_OK )
  183. {
  184. // get pin info
  185. PIN_INFO GrabberPinInfo;
  186. hr = pGrabberPin->QueryPinInfo( &GrabberPinInfo );
  187. if( FAILED( hr ) )
  188. {
  189. pGrabberPin->Release();
  190. pGrabberPin = NULL;
  191. continue;
  192. }
  193. // check if it is output pin
  194. if( GrabberPinInfo.dir == PINDIR_INPUT )
  195. {
  196. hr = m_pGraphBuilder->Connect( pPinOut, pGrabberPin );
  197. }
  198. else
  199. {
  200. hr = m_pGraphBuilder->Connect( pGrabberPin, pPinIn );
  201. }
  202. GrabberPinInfo.pFilter->Release();
  203. pGrabberPin->Release();
  204. }
  205. }
  206. }
  207. PinInfo.pFilter->Release();
  208. pPinIn->Release();
  209. pPinOut->Release();
  210. }
  211. pEnumPins->Release();
  212. return TRUE;
  213. }
  214. DWORD CDirectShowStreamMovie::ThreadProc()
  215. {
  216. HANDLE hMediaEvent;
  217. m_pMediaEvent->GetEventHandle( (OAEVENT*) &hMediaEvent );
  218. HANDLE pEvents[] = { CThread::GetThreadStopEvent(), hMediaEvent };
  219. BOOL bRun = TRUE;
  220. while( bRun )
  221. {
  222. switch( ::WaitForMultipleObjects( 2, pEvents, FALSE, INFINITE ) )
  223. {
  224. case WAIT_OBJECT_0:
  225. case WAIT_FAILED:
  226. default:
  227. bRun = FALSE;
  228. continue;
  229. case WAIT_OBJECT_0+1:
  230. break;
  231. }
  232. LONG evCode, evParam1, evParam2;
  233. // Process all queued events
  234. while(SUCCEEDED(m_pMediaEvent->GetEvent(&evCode, (LONG_PTR *) &evParam1,
  235. (LONG_PTR *) &evParam2, 0)))
  236. {
  237. // Free memory associated with callback, since we're not using it
  238. HRESULT hr = m_pMediaEvent->FreeEventParams(evCode, evParam1, evParam2);
  239. // If this is the end of the clip, reset to beginning
  240. if(EC_COMPLETE == evCode)
  241. {
  242. LONGLONG pos=0;
  243. // Reset to first frame of movie
  244. hr = m_pMediaSeeking->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
  245. NULL, AM_SEEKING_NoPositioning);
  246. if (FAILED(hr))
  247. {
  248. // Some custom filters (like the Windows CE MIDI filter)
  249. // may not implement seeking interfaces (IMediaSeeking)
  250. // to allow seeking to the start. In that case, just stop
  251. // and restart for the same effect. This should not be
  252. // necessary in most cases.
  253. if (FAILED(hr = m_pMediaControl->Stop()))
  254. {
  255. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CDirectShowStreamMovie::ThreadProc"), _T("Failed(0x%08lx) to stop media clip"), hr );
  256. break;
  257. }
  258. if (FAILED(hr = m_pMediaControl->Run()))
  259. {
  260. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CDirectShowStreamMovie::ThreadProc"), _T("Failed(0x%08lx) to reset media clip"), hr);
  261. break;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. return 0UL;
  268. }
  269. IBaseFilter *CDirectShowStreamMovie::FindVideoRenderer(IGraphBuilder *pGraphBuilder)
  270. {
  271. if( !pGraphBuilder )
  272. return FALSE;
  273. // get filter graph
  274. IFilterGraph *pFilterGraph = NULL;
  275. HRESULT hr = pGraphBuilder->QueryInterface( IID_IFilterGraph, (void**)&pFilterGraph );
  276. if( FAILED(hr) )
  277. return FALSE;
  278. // enumerate all filters
  279. IEnumFilters *pEnumFilters = NULL;
  280. hr = pFilterGraph->EnumFilters( &pEnumFilters );
  281. if( FAILED(hr) )
  282. {
  283. pFilterGraph->Release();
  284. return FALSE;
  285. }
  286. // free filter graph
  287. pFilterGraph->Release();
  288. // step throught all filters
  289. IBaseFilter *pFilter = NULL;
  290. while( pEnumFilters->Next( 1, &pFilter, NULL ) == S_OK )
  291. {
  292. IVideoWindow *pVideoWindow = NULL;
  293. hr = pFilter->QueryInterface( IID_IBasicVideo, (void**)pVideoWindow );
  294. if( SUCCEEDED( hr ) )
  295. {
  296. pVideoWindow->Release();
  297. pEnumFilters->Release();
  298. return pFilter;
  299. }
  300. FILTER_INFO fi;
  301. memset( &fi, 0, sizeof(fi) );
  302. hr = pFilter->QueryFilterInfo( &fi );
  303. if( FAILED(hr) )
  304. {
  305. pFilter->Release();
  306. if( fi.pGraph )
  307. fi.pGraph->Release();
  308. continue;
  309. }
  310. TCHAR pName[MAX_PATH];
  311. ::WideCharToMultiByte( CP_ACP, 0, fi.achName, -1, pName, MAX_PATH, 0, 0 );
  312. if( !_tcscmp( pName, _T("Video Renderer") ) )
  313. {
  314. pEnumFilters->Release();
  315. if( fi.pGraph )
  316. fi.pGraph->Release();
  317. return pFilter;
  318. }
  319. pFilter->Release();
  320. if( fi.pGraph )
  321. fi.pGraph->Release();
  322. }
  323. return FALSE;
  324. }
  325. BOOL CDirectShowStreamMovie::InitCapturer()
  326. {
  327. if( !m_pSampleGrabber )
  328. return FALSE;
  329. HRESULT hr = m_pSampleGrabber->SetOneShot( FALSE );
  330. if( FAILED( hr ) )
  331. return FALSE;
  332. hr = m_pSampleGrabber->SetBufferSamples( TRUE );
  333. if( FAILED( hr ) )
  334. return FALSE;
  335. // retrieve media type
  336. AM_MEDIA_TYPE mt;
  337. if( FAILED( m_pSampleGrabber->GetConnectedMediaType(&mt) ) )
  338. return FALSE;
  339. if( mt.formattype != FORMAT_VideoInfo )
  340. return FALSE;
  341. // store video format
  342. VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat;
  343. if( !pVideoHeader )
  344. return FALSE;
  345. // store video format
  346. m_pBitmapInfoHeader = new BITMAPINFOHEADER;
  347. if( !m_pBitmapInfoHeader )
  348. return FALSE;
  349. memcpy( m_pBitmapInfoHeader, &pVideoHeader->bmiHeader, sizeof(BITMAPINFOHEADER) );
  350. // release video format
  351. ::CoTaskMemFree( mt.pbFormat );
  352. // create ISampleGrabberCB object
  353. m_pDirectShowSampleGrabberCB = new CDirectShowSampleGrabberCB();
  354. if( !m_pDirectShowSampleGrabberCB )
  355. return FALSE;
  356. // link to itself
  357. m_pDirectShowSampleGrabberCB->SetDirectShowStream( this );
  358. // install callback interface
  359. hr = m_pSampleGrabber->SetCallback( m_pDirectShowSampleGrabberCB, 1 );
  360. if( FAILED( hr ) )
  361. return FALSE;
  362. return TRUE;
  363. }