libimgplayer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. {
  31. // 表示是执行OnCancel或OnClose或StopPlay关闭的
  32. CloseHandle(m_hPlayThread);
  33. m_hPlayThread = NULL;
  34. }
  35. }
  36. static unsigned int __stdcall ImgPlayThread(LPVOID param)
  37. {
  38. int i = 0;
  39. CoInitialize(NULL);
  40. libimgplayer_impl *p = (libimgplayer_impl *)param;
  41. p->PlayDlg();
  42. CoUninitialize();
  43. return 0;
  44. }
  45. public:
  46. libimgplayer_impl(CImgHostApi *pHostApi)
  47. : m_pDlg(NULL), m_hPlayThread(NULL), m_nPlaythreadId(0), m_bIsPlay(FALSE)
  48. {
  49. m_pHostApi = pHostApi;
  50. memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
  51. }
  52. ~libimgplayer_impl()
  53. {
  54. m_pHostApi = NULL;
  55. }
  56. BOOL isStop(){ return !m_bIsPlay; }
  57. BOOL CheckIsPlay(HANDLE &playThread)
  58. {
  59. if (NULL == m_hPlayThread)
  60. return FALSE;
  61. DWORD exitCode = 0;
  62. if (GetExitCodeThread(m_hPlayThread, &exitCode) && STILL_ACTIVE == exitCode)
  63. {
  64. playThread = m_hPlayThread;
  65. return TRUE;
  66. }
  67. else
  68. return FALSE;
  69. }
  70. BOOL StartPlayMedia(CImgPlayConfig &config)
  71. {
  72. if (m_bIsPlay)
  73. {
  74. if (m_pDlg != NULL)
  75. return TRUE;
  76. }
  77. else
  78. m_bIsPlay = TRUE;
  79. memcpy(&m_stPlayConfig, &config, sizeof(CImgPlayConfig));
  80. m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, ImgPlayThread, (LPVOID)this, 0, (unsigned int*)&m_nPlaythreadId);
  81. return TRUE;
  82. }
  83. BOOL StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  84. {
  85. if (m_bIsPlay)
  86. {
  87. if (m_pDlg != NULL)
  88. return TRUE;
  89. }
  90. else
  91. m_bIsPlay = TRUE;
  92. m_stPlayConfig.nWndX = nWndX;
  93. m_stPlayConfig.nWndY = nWndY;
  94. m_stPlayConfig.nWndWidth = nWndWidth;
  95. m_stPlayConfig.nWndHeight = nWndHeight;
  96. BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  97. if (!bRet)
  98. {
  99. m_pHostApi->ImgDebug("Load ImgConfiguration failed!");
  100. return FALSE;
  101. }
  102. else
  103. {
  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. {
  114. if (m_hPlayThread != NULL)
  115. {
  116. ::PostMessage(m_pDlg->GetSafeHwnd(), WM_CLOSE, NULL, NULL);
  117. WaitForSingleObject(m_hPlayThread, INFINITE);
  118. }
  119. m_bIsPlay = FALSE;
  120. }
  121. return TRUE;
  122. }
  123. };
  124. Clibimgplayer::Clibimgplayer(CImgHostApi *pHostApi)
  125. {
  126. m_pImpl = new libimgplayer_impl(pHostApi);
  127. return;
  128. }
  129. Clibimgplayer::~Clibimgplayer()
  130. {
  131. delete m_pImpl;
  132. m_pImpl = NULL;
  133. }
  134. VOID Clibimgplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  135. {
  136. m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  137. }
  138. VOID Clibimgplayer::PlayMedia(CImgPlayConfig &config)
  139. {
  140. m_pImpl->StartPlayMedia(config);
  141. }
  142. BOOL Clibimgplayer::checkIsStop(){
  143. return m_pImpl->isStop();
  144. }
  145. BOOL Clibimgplayer::checkIsPlay(HANDLE &curThread)
  146. {
  147. return m_pImpl->CheckIsPlay(curThread);
  148. }
  149. VOID Clibimgplayer::Close()
  150. {
  151. m_pImpl->StopPlay();
  152. }