MPEG1Player.cpp
上传用户:weiliangfb
上传日期:2007-01-23
资源大小:142k
文件大小:3k
源码类别:

多媒体

开发平台:

Visual C++

  1. // MPEG1Player.cpp: implementation of the CMPEG1Player class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Cient.h"
  6. #include "MPEG1Player.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. CMPEG1Player::CMPEG1Player()
  13. {
  14. HRESULT hr;
  15. m_pFilter = CAsyncFilter::CreateInstance( &hr);
  16. m_pGBuilder = NULL;
  17. m_pFGraph = NULL;
  18. m_bInitOk= FALSE;
  19. m_bHaveGet = FALSE;
  20. }
  21. CMPEG1Player::~CMPEG1Player()
  22. {
  23. if( m_pGBuilder)
  24. m_pGBuilder->Release();
  25. if(m_pFGraph)
  26. m_pFGraph->Release();
  27. //m_pFilter->Release();//Do No Release it;
  28. }
  29. BOOL CMPEG1Player::InitPlayer(LONGLONG llMediaLen,PlayerEvent * pEvenSink,HWND hWnd)
  30. {
  31. if(m_bInitOk)
  32. return TRUE;
  33. m_pFilter->SetMediaLength(llMediaLen);
  34. m_pFilter->Advise(pEvenSink);
  35. HRESULT hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IFilterGraph,(LPVOID *)&m_pFGraph);
  36. if(FAILED(hr))
  37. return FALSE;
  38. hr = m_pFGraph->AddFilter(m_pFilter,L"Source");
  39. if(FAILED(hr))
  40. return FALSE;
  41. hr = m_pFGraph->QueryInterface(IID_IGraphBuilder,(LPVOID *)&m_pGBuilder);
  42. if(FAILED(hr))
  43. return FALSE;
  44.     IEnumPins  *pEnum;
  45.     IPin       *pPin;
  46.     m_pFilter->EnumPins(&pEnum);
  47.     if(pEnum->Next(1, &pPin, 0) == S_OK)
  48.     {
  49. hr = m_pGBuilder->Render(pPin);
  50.          pPin->Release();
  51. if(FAILED(hr))
  52. return FALSE;
  53.    }
  54. else 
  55. {
  56.     pEnum->Release();
  57. return FALSE;
  58. }
  59.     pEnum->Release();
  60. CComQIPtr<IVideoWindow, &IID_IVideoWindow> spVideoWindow(m_pGBuilder);
  61. if(hWnd)
  62. {
  63. spVideoWindow->put_Owner((OAHWND)hWnd);
  64. spVideoWindow->put_WindowStyle(WS_CHILD);
  65. RECT rect;
  66. GetClientRect(hWnd,&rect);
  67. spVideoWindow->SetWindowPosition(rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top);
  68. }
  69. m_bInitOk = TRUE;
  70. return TRUE;
  71. }
  72. HRESULT CMPEG1Player::Run()
  73. {
  74. if(!m_bInitOk)
  75. return E_FAIL;
  76. CComQIPtr<IMediaControl, &IID_IMediaControl> spMediaCtrol(m_pGBuilder);
  77. HRESULT hr = spMediaCtrol->Run();
  78. return hr;
  79. }
  80. HRESULT CMPEG1Player::Pause()
  81. {
  82. if(!m_bInitOk)
  83. return E_FAIL;
  84. CComQIPtr<IMediaControl, &IID_IMediaControl> spMediaCtrol(m_pGBuilder);
  85. HRESULT hr = spMediaCtrol->Pause();
  86. return hr;
  87. }
  88. HRESULT CMPEG1Player::SetPlayRate(double dblFps)
  89. {
  90. if(!m_bInitOk)
  91. return E_FAIL;
  92. if(dblFps==0)
  93. return E_FAIL;
  94. if(!m_bHaveGet)
  95. {
  96. CComQIPtr<IBasicVideo , &IID_IBasicVideo > spBaseVideo(m_pGBuilder);
  97. spBaseVideo->get_AvgTimePerFrame(&m_oldRefTime);
  98. m_bHaveGet = TRUE;
  99. }
  100. CComQIPtr<IMediaSeeking , &IID_IMediaSeeking > spMediaCSeeking(m_pGBuilder);
  101. double dblNewRat = 1./(1./dblFps/m_oldRefTime);
  102. return spMediaCSeeking->SetRate(dblNewRat);
  103. }