libwmpplayer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include "stdafx.h"
  2. #include "libwmpplayer.h"
  3. #include "CPlayerDlg.h"
  4. #include <Windows.h>
  5. class libwmpplayer_impl
  6. {
  7. private:
  8. CPlayerDlg *m_pDlg;
  9. HANDLE m_hPlayThread;
  10. UINT m_nPlaythreadId;
  11. CWmpPlayConfig m_stPlayConfig;
  12. CWmpHostApi *m_pHostApi;
  13. BOOL m_bIsPlay;
  14. // add by ly
  15. HANDLE h_WMPCreateEvent; // 播放器创建事件
  16. HANDLE h_WMPPlayEndEvent;
  17. bool m_bWMPCreating; // 是否播放器正在创建中
  18. VOID PlayDlg()
  19. {
  20. // 参数检查
  21. if (!_stricmp(m_stPlayConfig.strRootPath, ""))
  22. return;
  23. switch (m_stPlayConfig.eMode)
  24. {
  25. case SINGLE:
  26. break;
  27. case SALESRECORD:
  28. if (!_stricmp(m_stPlayConfig.strNamePrefix, "") || m_stPlayConfig.nFileCnt <= 0)
  29. return;
  30. break;
  31. case LOCALAUDIO:
  32. if (m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0
  33. || m_stPlayConfig.nFileCnt <= 0 || m_stPlayConfig.nFileCnt > MAX_FILECOUNT)
  34. return;
  35. break;
  36. case LOCALVIDEO:
  37. if (m_stPlayConfig.nWndX < 0 || m_stPlayConfig.nWndY < 0 || m_stPlayConfig.nWndWidth < 0
  38. || m_stPlayConfig.nWndHeight < 0 || m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0
  39. || m_stPlayConfig.nFileCnt <= 0 || m_stPlayConfig.nFileCnt > MAX_FILECOUNT)
  40. return;
  41. break;
  42. case THRIDSALESRECORD:
  43. if (m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0 || m_stPlayConfig.nFileCnt <= 0
  44. || m_stPlayConfig.nFileCnt > MAX_FILECOUNT || m_stPlayConfig.nWndX < 0 || m_stPlayConfig.nWndY < 0
  45. || m_stPlayConfig.nWndWidth < 0 || m_stPlayConfig.nWndHeight < 0 )
  46. return;
  47. break;
  48. }
  49. if (m_pDlg != NULL)
  50. return;
  51. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  52. m_pDlg = new CPlayerDlg(&m_stPlayConfig);
  53. m_pHostApi->WmpDebug("Ok to initialize CPlayerDlg!");
  54. INT_PTR nResponse = m_pDlg->DoModal();
  55. delete m_pDlg;
  56. m_pDlg = NULL;
  57. if (nResponse == IDCANCEL)
  58. {
  59. // 表示是执行OnCancel或OnClose或StopPlay关闭的
  60. CloseHandle(m_hPlayThread);
  61. m_hPlayThread = NULL;
  62. }
  63. }
  64. static unsigned int __stdcall VideoPlayThread(LPVOID param)
  65. {
  66. int i = 0;
  67. CoInitialize(NULL);
  68. libwmpplayer_impl *p = (libwmpplayer_impl *)param;
  69. p->PlayDlg();
  70. CoUninitialize();
  71. return 0;
  72. }
  73. public:
  74. libwmpplayer_impl(CWmpHostApi *pHostApi)
  75. : m_pDlg(NULL), m_hPlayThread(NULL), m_nPlaythreadId(0), m_bIsPlay(FALSE), m_bWMPCreating(false), h_WMPPlayEndEvent(NULL)
  76. {
  77. m_pHostApi = pHostApi;
  78. memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
  79. }
  80. ~libwmpplayer_impl()
  81. {
  82. m_pHostApi = NULL;
  83. }
  84. BOOL isStop(){ return !m_bIsPlay; }
  85. BOOL CheckIsPlay(HANDLE &playThread)
  86. {
  87. if (NULL == m_hPlayThread)
  88. return FALSE;
  89. DWORD exitCode = 0;
  90. if (GetExitCodeThread(m_hPlayThread, &exitCode) && STILL_ACTIVE == exitCode)
  91. {
  92. playThread = h_WMPPlayEndEvent;
  93. return TRUE;
  94. }
  95. else
  96. return FALSE;
  97. }
  98. // 按分隔符分隔字符串
  99. void CStringSplit(char *str, char **result, const char *del)
  100. {
  101. char *p = strtok(str, del);
  102. while(p != NULL)
  103. {
  104. *result++ = p;
  105. p = strtok(NULL, del);
  106. }
  107. }
  108. BOOL StartPlayVideo(const char *pVideoDir, const char *pNamePrefix = NULL, int nVideoCount = 1)
  109. {
  110. if (pVideoDir == NULL)
  111. return FALSE;
  112. if (m_bIsPlay)
  113. {
  114. if (m_pDlg != NULL)
  115. return TRUE;
  116. }
  117. else
  118. {
  119. m_bIsPlay = TRUE;
  120. m_pHostApi->WmpDebug("StartPlayVideo set m_bIsPlay is TRUE.");
  121. }
  122. strcpy(m_stPlayConfig.strRootPath, pVideoDir);
  123. if (pNamePrefix != NULL)
  124. {
  125. strcpy(m_stPlayConfig.strNamePrefix, pNamePrefix);
  126. m_stPlayConfig.eMode = SALESRECORD;
  127. }
  128. else
  129. {
  130. strcpy(m_stPlayConfig.strNamePrefix, "");
  131. m_stPlayConfig.eMode = SINGLE;
  132. }
  133. m_stPlayConfig.nFileCnt = nVideoCount;
  134. m_stPlayConfig.bPrimMonitor = true;
  135. return CreateWMPDialog();
  136. }
  137. BOOL StartPlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoDir, const char *pNamePrefix = NULL, int nVideoCount = 1)
  138. {
  139. if (pVideoDir == NULL)
  140. return FALSE;
  141. if (m_bIsPlay)
  142. {
  143. if (m_pDlg != NULL)
  144. return TRUE;
  145. }
  146. else
  147. {
  148. m_bIsPlay = TRUE;
  149. m_pHostApi->WmpDebug("StartPlayVideo set m_bIsPlay is TRUE.");
  150. }
  151. m_pHostApi->WmpDebug("nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nWndX,nWndY,nWndWidth,nWndHeight);
  152. BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
  153. m_stPlayConfig.nWndX = nWndX;
  154. m_stPlayConfig.nWndY = nWndY;
  155. m_stPlayConfig.nWndWidth = nWndWidth;
  156. m_stPlayConfig.nWndHeight = nWndHeight;
  157. if (!bRet)
  158. {
  159. m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local video!");
  160. return FALSE;
  161. }
  162. else
  163. {
  164. m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local video!");
  165. m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  166. }
  167. strcpy(m_stPlayConfig.strRootPath, pVideoDir);
  168. if (pNamePrefix != NULL)
  169. {
  170. strcpy(m_stPlayConfig.strNamePrefix, pNamePrefix);
  171. m_stPlayConfig.eMode = THRIDSALESRECORD;
  172. }
  173. else
  174. {
  175. strcpy(m_stPlayConfig.strNamePrefix, "");
  176. m_stPlayConfig.eMode = SINGLE;
  177. }
  178. m_stPlayConfig.nFileCnt = nVideoCount;
  179. m_stPlayConfig.bPrimMonitor = true;
  180. return CreateWMPDialog();
  181. }
  182. BOOL StartPlayLocalAudio(const char *pAudioNames)
  183. {
  184. if (pAudioNames == NULL)
  185. return FALSE;
  186. if (m_bIsPlay)
  187. {
  188. if (m_pDlg != NULL)
  189. return TRUE;
  190. }
  191. else
  192. m_bIsPlay = TRUE;
  193. m_stPlayConfig.eMode = LOCALAUDIO;
  194. BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
  195. m_stPlayConfig.eMode = LOCALAUDIO;
  196. if (!bRet)
  197. {
  198. m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local audio!");
  199. return FALSE;
  200. }
  201. else
  202. {
  203. m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local audio!");
  204. m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  205. }
  206. // 切分音频文件名
  207. char *Tmp = new char[strlen(pAudioNames)+1];
  208. strcpy(Tmp, pAudioNames);
  209. char *Result[MAX_FILECOUNT] = { NULL };
  210. CStringSplit(Tmp, Result, "|");
  211. int FileCount = 0;
  212. char** pStr = Result;
  213. while (*pStr != NULL)
  214. {
  215. ++pStr; ++FileCount;
  216. }
  217. m_stPlayConfig.bPrimMonitor = true;
  218. m_stPlayConfig.nFileCnt = FileCount;
  219. m_pHostApi->WmpDebug("Wmp pAudioNames = %s!", pAudioNames);
  220. m_pHostApi->WmpDebug("Wmp config.nFileCnt = %d!", FileCount);
  221. for (int i = 0; i != FileCount; ++i)
  222. {
  223. strcpy(m_stPlayConfig.strFileNames[i], Result[i]);
  224. }
  225. delete[] Tmp; Tmp = NULL;
  226. return CreateWMPDialog();
  227. }
  228. BOOL StartPlayMedia(CWmpPlayConfig &config)
  229. {
  230. if (m_bIsPlay)
  231. {
  232. if (m_pDlg != NULL)
  233. return TRUE;
  234. }
  235. else
  236. m_bIsPlay = TRUE;
  237. memcpy(&m_stPlayConfig, &config, sizeof(CWmpPlayConfig));
  238. return CreateWMPDialog();
  239. }
  240. BOOL StartPlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  241. {
  242. if (m_bIsPlay)
  243. {
  244. if (m_pDlg != NULL)
  245. return TRUE;
  246. }
  247. else
  248. m_bIsPlay = TRUE;
  249. m_stPlayConfig.eMode = LOCALVIDEO;
  250. m_pHostApi->WmpDebug("nCfgInx=%d, nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nCfgInx,nWndX,nWndY,nWndWidth,nWndHeight);
  251. BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  252. m_stPlayConfig.eMode = LOCALVIDEO;
  253. m_stPlayConfig.nWndX = nWndX;
  254. m_stPlayConfig.nWndY = nWndY;
  255. m_stPlayConfig.nWndWidth = nWndWidth;
  256. m_stPlayConfig.nWndHeight = nWndHeight;
  257. if (!bRet)
  258. {
  259. m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local video!");
  260. return FALSE;
  261. }
  262. else
  263. {
  264. m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local video!");
  265. m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  266. }
  267. // 判断当前时间是否允许播放
  268. SYSTEMTIME st;
  269. GetLocalTime(&st);
  270. TCHAR strNow[TIME_LEN];
  271. sprintf(strNow, "%02d:%02d:%02d", st.wHour,st.wMinute, st.wSecond);
  272. if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
  273. {
  274. m_pHostApi->WmpDebug("Now is %s, play video rejected!", strNow);
  275. return TRUE;
  276. }
  277. return CreateWMPDialog();
  278. }
  279. // add by ly
  280. BOOL CreateWMPDialog()
  281. {
  282. m_bWMPCreating = true;
  283. DWORD WaitInterval = 3000;
  284. if (h_WMPPlayEndEvent)
  285. {
  286. CloseHandle(h_WMPPlayEndEvent);
  287. h_WMPPlayEndEvent = NULL;
  288. }
  289. h_WMPPlayEndEvent = CreateEventA(NULL, FALSE, FALSE, "WMPPLAYENDEvent");
  290. m_stPlayConfig.h_WMPPlayEndEvent = h_WMPPlayEndEvent;
  291. h_WMPCreateEvent = CreateEventA(NULL, FALSE, FALSE, "WMPCreateEvent");
  292. m_stPlayConfig.h_WMPCreateEvent = h_WMPCreateEvent;
  293. m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, VideoPlayThread, (LPVOID)this, 0, &m_nPlaythreadId);
  294. DWORD ret = WaitForSingleObject(h_WMPCreateEvent, WaitInterval);
  295. CloseHandle(h_WMPCreateEvent);
  296. h_WMPCreateEvent = NULL;
  297. m_bWMPCreating = false;
  298. if (ret == WAIT_OBJECT_0) {
  299. return TRUE;
  300. }
  301. else if (ret == WAIT_TIMEOUT) {
  302. return FALSE;
  303. }
  304. else //if (ret == WAIT_FAILED)
  305. {
  306. return FALSE;
  307. }
  308. }
  309. BOOL StopPlay()
  310. {
  311. static CRITICAL_SECTION t_cs;
  312. static bool t_isInit = false;
  313. if (!t_isInit)
  314. {
  315. InitializeCriticalSection(&t_cs);
  316. t_isInit = true;
  317. }
  318. EnterCriticalSection(&t_cs);
  319. if (m_pDlg != NULL)
  320. {
  321. if (m_hPlayThread != NULL)
  322. {
  323. while (m_bWMPCreating) {
  324. Sleep(100); // 等待直到播放器创建完成
  325. }
  326. ::PostMessage(m_pDlg->GetSafeHwnd(), WM_CLOSE, NULL, NULL);
  327. WaitForSingleObject(m_hPlayThread, INFINITE);
  328. }
  329. m_bIsPlay = FALSE;
  330. }
  331. LeaveCriticalSection(&t_cs);
  332. return TRUE;
  333. }
  334. BOOL SetVolume(int nVolume)
  335. {
  336. if (m_bIsPlay && m_pDlg != NULL &&
  337. nVolume >= 0 && nVolume <= 100)
  338. {
  339. ::PostMessage(m_pDlg->GetSafeHwnd(), WM_SETVOLUME_MESSAGE, NULL, (LPARAM)nVolume);
  340. }
  341. return TRUE;
  342. }
  343. };
  344. Clibwmpplayer::Clibwmpplayer(CWmpHostApi *pHostApi)
  345. {
  346. m_pImpl = new libwmpplayer_impl(pHostApi);
  347. return;
  348. }
  349. Clibwmpplayer::~Clibwmpplayer()
  350. {
  351. delete m_pImpl;
  352. m_pImpl = NULL;
  353. }
  354. VOID Clibwmpplayer::PlayVideo(const char *pVideoDir, const char *pNamePrefix, int nVideoCount)
  355. {
  356. m_pImpl->StartPlayVideo(pVideoDir, pNamePrefix, nVideoCount);
  357. }
  358. VOID Clibwmpplayer::PlayLocalAudio(const char *pAudioNames)
  359. {
  360. m_pImpl->StartPlayLocalAudio(pAudioNames);
  361. }
  362. VOID Clibwmpplayer::PlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  363. {
  364. m_pImpl->StartPlayLocalVideo(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  365. }
  366. BOOL Clibwmpplayer::checkIsPlay(HANDLE &curThread)
  367. {
  368. return m_pImpl->CheckIsPlay(curThread);
  369. }
  370. BOOL Clibwmpplayer::checkIsStop()
  371. {
  372. return m_pImpl->isStop();
  373. }
  374. VOID Clibwmpplayer::PlayMedia(CWmpPlayConfig &config)
  375. {
  376. m_pImpl->StartPlayMedia(config);
  377. }
  378. VOID Clibwmpplayer::Close()
  379. {
  380. m_pImpl->StopPlay();
  381. }
  382. VOID Clibwmpplayer::SetVolume( int nVolume )
  383. {
  384. m_pImpl->SetVolume(nVolume);
  385. }
  386. VOID Clibwmpplayer::PlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoDir, const char *pNamePrefix, int nVideoCount)
  387. {
  388. m_pImpl->StartPlaySalesRecordVideo(nWndX, nWndY, nWndWidth, nWndHeight, pVideoDir, pNamePrefix, nVideoCount);
  389. }