libimgplayer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // libimgplayer.cpp : 定义应用程序的类行为。
  2. //
  3. #include "stdafx.h"
  4. #include "libimgplayer.h"
  5. #include "CImgPlayerDlg.h"
  6. class libimgplayer_impl
  7. {
  8. private:
  9. CImgPlayerDlg *m_pDlg;
  10. HANDLE m_hPlayThread;
  11. DWORD m_nPlaythreadId;
  12. BOOL m_bIsPlay;
  13. CImgPlayConfig m_stPlayConfig;
  14. CImgHostApi *m_pHostApi;
  15. VOID PlayDlg()
  16. {
  17. if (m_stPlayConfig.nWndX < 0 || m_stPlayConfig.nWndY < 0 || m_stPlayConfig.nWndWidth < 0
  18. || m_stPlayConfig.nWndHeight < 0 || m_stPlayConfig.nPlayCnt < 0
  19. || m_stPlayConfig.nPlayInterval < 0|| m_stPlayConfig.nFileCnt <= 0)
  20. return;
  21. if (m_pDlg != NULL)
  22. return;
  23. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  24. m_pDlg = new CImgPlayerDlg(&m_stPlayConfig);
  25. //m_pHostApi->ImgDebug("Ok to initialize CImgDlg!");
  26. INT_PTR nResponse = m_pDlg->DoModal();
  27. delete m_pDlg;
  28. m_pDlg = NULL;
  29. if (nResponse == IDCANCEL){
  30. // 表示是执行OnCancel或OnClose或StopPlay关闭的
  31. CloseHandle(m_hPlayThread);
  32. m_hPlayThread = NULL;
  33. }
  34. }
  35. static unsigned int __stdcall ImgPlayThread(LPVOID param)
  36. {
  37. int i = 0;
  38. CoInitialize(NULL);
  39. libimgplayer_impl *p = (libimgplayer_impl *)param;
  40. p->PlayDlg();
  41. CoUninitialize();
  42. return 0;
  43. }
  44. public:
  45. libimgplayer_impl(CImgHostApi *pHostApi)
  46. : m_pDlg(NULL), m_hPlayThread(NULL), m_nPlaythreadId(0), m_bIsPlay(FALSE)
  47. {
  48. m_pHostApi = pHostApi;
  49. memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
  50. }
  51. ~libimgplayer_impl()
  52. {
  53. m_pHostApi = NULL;
  54. }
  55. BOOL isStop(){ return !m_bIsPlay; }
  56. BOOL CheckIsPlay(HANDLE &playThread)
  57. {
  58. if (NULL == m_hPlayThread) {
  59. return FALSE;
  60. }
  61. DWORD exitCode = 0;
  62. if (GetExitCodeThread(m_hPlayThread, &exitCode) && STILL_ACTIVE == exitCode){
  63. playThread = m_hPlayThread;
  64. return TRUE;
  65. }
  66. else {
  67. return FALSE;
  68. }
  69. }
  70. BOOL StartPlayMedia(CImgPlayConfig &config)
  71. {
  72. if (m_bIsPlay){
  73. if (m_pDlg != NULL) {
  74. return TRUE;
  75. }
  76. }
  77. else {
  78. m_bIsPlay = TRUE;
  79. }
  80. memcpy(&m_stPlayConfig, &config, sizeof(CImgPlayConfig));
  81. m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, ImgPlayThread, (LPVOID)this, 0, (unsigned int*)&m_nPlaythreadId);
  82. return TRUE;
  83. }
  84. BOOL StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  85. {
  86. if (m_bIsPlay){
  87. if (m_pDlg != NULL) {
  88. return TRUE;
  89. }
  90. }
  91. else {
  92. m_bIsPlay = TRUE;
  93. }
  94. m_stPlayConfig.nWndX = nWndX;
  95. m_stPlayConfig.nWndY = nWndY;
  96. m_stPlayConfig.nWndWidth = nWndWidth;
  97. m_stPlayConfig.nWndHeight = nWndHeight;
  98. BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  99. if (!bRet){
  100. m_pHostApi->ImgDebug("Load ImgConfiguration failed!");
  101. return FALSE;
  102. }
  103. else{
  104. m_pHostApi->ImgDebug("Load ImgConfiguration succeeded while play local image!");
  105. m_pHostApi->ImgDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  106. }
  107. m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, ImgPlayThread, (LPVOID)this, 0, (unsigned int*)&m_nPlaythreadId);
  108. return TRUE;
  109. }
  110. BOOL StopPlay()
  111. {
  112. if (m_pDlg != NULL){
  113. if (m_hPlayThread != NULL){
  114. ::PostMessage(m_pDlg->GetSafeHwnd(), WM_CLOSE, NULL, NULL);
  115. WaitForSingleObject(m_hPlayThread, INFINITE);
  116. }
  117. }
  118. m_bIsPlay = FALSE;
  119. return TRUE;
  120. }
  121. };
  122. Clibimgplayer::Clibimgplayer(CImgHostApi *pHostApi)
  123. {
  124. m_pImpl = new libimgplayer_impl(pHostApi);
  125. return;
  126. }
  127. Clibimgplayer::~Clibimgplayer()
  128. {
  129. delete m_pImpl;
  130. m_pImpl = NULL;
  131. }
  132. VOID Clibimgplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  133. {
  134. m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  135. }
  136. VOID Clibimgplayer::PlayMedia(CImgPlayConfig &config)
  137. {
  138. m_pImpl->StartPlayMedia(config);
  139. }
  140. BOOL Clibimgplayer::checkIsStop(){
  141. return m_pImpl->isStop();
  142. }
  143. BOOL Clibimgplayer::checkIsPlay(HANDLE &curThread)
  144. {
  145. return m_pImpl->CheckIsPlay(curThread);
  146. }
  147. VOID Clibimgplayer::Close()
  148. {
  149. m_pImpl->StopPlay();
  150. }