123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- #include "stdafx.h"
- #include "libwmpplayer.h"
- #include "CPlayerDlg.h"
- #include <Windows.h>
- class libwmpplayer_impl
- {
- private:
- CPlayerDlg *m_pDlg;
- HANDLE m_hPlayThread;
- UINT m_nPlaythreadId;
- CWmpPlayConfig m_stPlayConfig;
- CWmpHostApi *m_pHostApi;
- BOOL m_bIsPlay;
- // add by ly
- HANDLE h_WMPCreateEvent; // 播放器创建事件
- HANDLE h_WMPPlayEndEvent;
- bool m_bWMPCreating; // 是否播放器正在创建中
- VOID PlayDlg()
- {
- // 参数检查
- if (!_stricmp(m_stPlayConfig.strRootPath, ""))
- return;
- switch (m_stPlayConfig.eMode)
- {
- case SINGLE:
- break;
- case SALESRECORD:
- if (!_stricmp(m_stPlayConfig.strNamePrefix, "") || m_stPlayConfig.nFileCnt <= 0)
- return;
- break;
- case LOCALAUDIO:
- if (m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0
- || m_stPlayConfig.nFileCnt <= 0 || m_stPlayConfig.nFileCnt > MAX_FILECOUNT)
- return;
- break;
- case LOCALVIDEO:
- if (m_stPlayConfig.nWndX < 0 || m_stPlayConfig.nWndY < 0 || m_stPlayConfig.nWndWidth < 0
- || m_stPlayConfig.nWndHeight < 0 || m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0
- || m_stPlayConfig.nFileCnt <= 0 || m_stPlayConfig.nFileCnt > MAX_FILECOUNT)
- return;
- break;
- case THRIDSALESRECORD:
- if (m_stPlayConfig.nPlayCnt < 0 || m_stPlayConfig.nPlayInterval < 0 || m_stPlayConfig.nFileCnt <= 0
- || m_stPlayConfig.nFileCnt > MAX_FILECOUNT || m_stPlayConfig.nWndX < 0 || m_stPlayConfig.nWndY < 0
- || m_stPlayConfig.nWndWidth < 0 || m_stPlayConfig.nWndHeight < 0 )
- return;
- break;
- }
-
- if (m_pDlg != NULL)
- return;
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- m_pDlg = new CPlayerDlg(&m_stPlayConfig);
- m_pHostApi->WmpDebug("Ok to initialize CPlayerDlg!");
- INT_PTR nResponse = m_pDlg->DoModal();
- delete m_pDlg;
- m_pDlg = NULL;
- if (nResponse == IDCANCEL)
- {
- // 表示是执行OnCancel或OnClose或StopPlay关闭的
- CloseHandle(m_hPlayThread);
- m_hPlayThread = NULL;
- }
- }
- static unsigned int __stdcall VideoPlayThread(LPVOID param)
- {
- int i = 0;
- CoInitialize(NULL);
- libwmpplayer_impl *p = (libwmpplayer_impl *)param;
- p->PlayDlg();
- CoUninitialize();
- return 0;
- }
- public:
- libwmpplayer_impl(CWmpHostApi *pHostApi)
- : m_pDlg(NULL), m_hPlayThread(NULL), m_nPlaythreadId(0), m_bIsPlay(FALSE), m_bWMPCreating(false), h_WMPPlayEndEvent(NULL)
- {
- m_pHostApi = pHostApi;
- memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
- }
- ~libwmpplayer_impl()
- {
- m_pHostApi = NULL;
- }
-
- BOOL isStop(){ return !m_bIsPlay; }
- BOOL CheckIsPlay(HANDLE &playThread)
- {
- if (NULL == m_hPlayThread)
- return FALSE;
- DWORD exitCode = 0;
- if (GetExitCodeThread(m_hPlayThread, &exitCode) && STILL_ACTIVE == exitCode)
- {
- playThread = h_WMPPlayEndEvent;
- return TRUE;
- }
- else
- return FALSE;
- }
- // 按分隔符分隔字符串
- void CStringSplit(char *str, char **result, const char *del)
- {
- char *p = strtok(str, del);
- while(p != NULL)
- {
- *result++ = p;
- p = strtok(NULL, del);
- }
- }
- BOOL StartPlayVideo(const char *pVideoDir, const char *pNamePrefix = NULL, int nVideoCount = 1)
- {
- if (pVideoDir == NULL)
- return FALSE;
- if (m_bIsPlay)
- {
- if (m_pDlg != NULL)
- return TRUE;
- }
- else
- {
- m_bIsPlay = TRUE;
- m_pHostApi->WmpDebug("StartPlayVideo set m_bIsPlay is TRUE.");
- }
-
- strcpy(m_stPlayConfig.strRootPath, pVideoDir);
- if (pNamePrefix != NULL)
- {
- strcpy(m_stPlayConfig.strNamePrefix, pNamePrefix);
- m_stPlayConfig.eMode = SALESRECORD;
- }
- else
- {
- strcpy(m_stPlayConfig.strNamePrefix, "");
- m_stPlayConfig.eMode = SINGLE;
- }
- m_stPlayConfig.nFileCnt = nVideoCount;
- m_stPlayConfig.bPrimMonitor = true;
- return CreateWMPDialog();
- }
- BOOL StartPlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoDir, const char *pNamePrefix = NULL, int nVideoCount = 1)
- {
- if (pVideoDir == NULL)
- return FALSE;
- if (m_bIsPlay)
- {
- if (m_pDlg != NULL)
- return TRUE;
- }
- else
- {
- m_bIsPlay = TRUE;
- m_pHostApi->WmpDebug("StartPlayVideo set m_bIsPlay is TRUE.");
- }
- m_pHostApi->WmpDebug("nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nWndX,nWndY,nWndWidth,nWndHeight);
- BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
- m_stPlayConfig.nWndX = nWndX;
- m_stPlayConfig.nWndY = nWndY;
- m_stPlayConfig.nWndWidth = nWndWidth;
- m_stPlayConfig.nWndHeight = nWndHeight;
- if (!bRet)
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local video!");
- return FALSE;
- }
- else
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local video!");
- m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
- }
- strcpy(m_stPlayConfig.strRootPath, pVideoDir);
- if (pNamePrefix != NULL)
- {
- strcpy(m_stPlayConfig.strNamePrefix, pNamePrefix);
- m_stPlayConfig.eMode = THRIDSALESRECORD;
- }
- else
- {
- strcpy(m_stPlayConfig.strNamePrefix, "");
- m_stPlayConfig.eMode = SINGLE;
- }
- m_stPlayConfig.nFileCnt = nVideoCount;
- m_stPlayConfig.bPrimMonitor = true;
- return CreateWMPDialog();
- }
- BOOL StartPlayLocalAudio(const char *pAudioNames)
- {
- if (pAudioNames == NULL)
- return FALSE;
- if (m_bIsPlay)
- {
- if (m_pDlg != NULL)
- return TRUE;
- }
- else
- m_bIsPlay = TRUE;
-
- m_stPlayConfig.eMode = LOCALAUDIO;
- BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
- m_stPlayConfig.eMode = LOCALAUDIO;
- if (!bRet)
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local audio!");
- return FALSE;
- }
- else
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local audio!");
- m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
- }
- // 切分音频文件名
- char *Tmp = new char[strlen(pAudioNames)+1];
- strcpy(Tmp, pAudioNames);
- char *Result[MAX_FILECOUNT] = { NULL };
- CStringSplit(Tmp, Result, "|");
- int FileCount = 0;
- char** pStr = Result;
- while (*pStr != NULL)
- {
- ++pStr; ++FileCount;
- }
- m_stPlayConfig.bPrimMonitor = true;
- m_stPlayConfig.nFileCnt = FileCount;
- m_pHostApi->WmpDebug("Wmp pAudioNames = %s!", pAudioNames);
- m_pHostApi->WmpDebug("Wmp config.nFileCnt = %d!", FileCount);
- for (int i = 0; i != FileCount; ++i)
- {
- strcpy(m_stPlayConfig.strFileNames[i], Result[i]);
- }
- delete[] Tmp; Tmp = NULL;
- return CreateWMPDialog();
- }
- BOOL StartPlayMedia(CWmpPlayConfig &config)
- {
- if (m_bIsPlay)
- {
- if (m_pDlg != NULL)
- return TRUE;
- }
- else
- m_bIsPlay = TRUE;
- memcpy(&m_stPlayConfig, &config, sizeof(CWmpPlayConfig));
- return CreateWMPDialog();
- }
- BOOL StartPlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- if (m_bIsPlay)
- {
- if (m_pDlg != NULL)
- return TRUE;
- }
- else
- m_bIsPlay = TRUE;
-
- m_stPlayConfig.eMode = LOCALVIDEO;
- m_pHostApi->WmpDebug("nCfgInx=%d, nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nCfgInx,nWndX,nWndY,nWndWidth,nWndHeight);
- BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
- m_stPlayConfig.eMode = LOCALVIDEO;
- m_stPlayConfig.nWndX = nWndX;
- m_stPlayConfig.nWndY = nWndY;
- m_stPlayConfig.nWndWidth = nWndWidth;
- m_stPlayConfig.nWndHeight = nWndHeight;
- if (!bRet)
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration failed while play local video!");
- return FALSE;
- }
- else
- {
- m_pHostApi->WmpDebug("Load WmpConfiguration succeeded while play local video!");
- m_pHostApi->WmpDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
- }
- // 判断当前时间是否允许播放
- SYSTEMTIME st;
- GetLocalTime(&st);
- TCHAR strNow[TIME_LEN];
- sprintf(strNow, "%02d:%02d:%02d", st.wHour,st.wMinute, st.wSecond);
- if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
- {
- m_pHostApi->WmpDebug("Now is %s, play video rejected!", strNow);
- return TRUE;
- }
- return CreateWMPDialog();
- }
- // add by ly
- BOOL CreateWMPDialog()
- {
- m_bWMPCreating = true;
- DWORD WaitInterval = 3000;
- if (h_WMPPlayEndEvent)
- {
- CloseHandle(h_WMPPlayEndEvent);
- h_WMPPlayEndEvent = NULL;
- }
- h_WMPPlayEndEvent = CreateEventA(NULL, FALSE, FALSE, "WMPPLAYENDEvent");
- m_stPlayConfig.h_WMPPlayEndEvent = h_WMPPlayEndEvent;
- h_WMPCreateEvent = CreateEventA(NULL, FALSE, FALSE, "WMPCreateEvent");
- m_stPlayConfig.h_WMPCreateEvent = h_WMPCreateEvent;
- m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, VideoPlayThread, (LPVOID)this, 0, &m_nPlaythreadId);
- DWORD ret = WaitForSingleObject(h_WMPCreateEvent, WaitInterval);
- CloseHandle(h_WMPCreateEvent);
- h_WMPCreateEvent = NULL;
- m_bWMPCreating = false;
- if (ret == WAIT_OBJECT_0) {
- return TRUE;
- }
- else if (ret == WAIT_TIMEOUT) {
- return FALSE;
- }
- else //if (ret == WAIT_FAILED)
- {
- return FALSE;
- }
- }
- BOOL StopPlay()
- {
- static CRITICAL_SECTION t_cs;
- static bool t_isInit = false;
- if (!t_isInit)
- {
- InitializeCriticalSection(&t_cs);
- t_isInit = true;
- }
- EnterCriticalSection(&t_cs);
- if (m_pDlg != NULL)
- {
- if (m_hPlayThread != NULL)
- {
- while (m_bWMPCreating) {
- Sleep(100); // 等待直到播放器创建完成
- }
- ::PostMessage(m_pDlg->GetSafeHwnd(), WM_CLOSE, NULL, NULL);
- WaitForSingleObject(m_hPlayThread, INFINITE);
- }
- m_bIsPlay = FALSE;
- }
- LeaveCriticalSection(&t_cs);
- return TRUE;
- }
- BOOL SetVolume(int nVolume)
- {
- if (m_bIsPlay && m_pDlg != NULL &&
- nVolume >= 0 && nVolume <= 100)
- {
- ::PostMessage(m_pDlg->GetSafeHwnd(), WM_SETVOLUME_MESSAGE, NULL, (LPARAM)nVolume);
- }
- return TRUE;
- }
- };
- Clibwmpplayer::Clibwmpplayer(CWmpHostApi *pHostApi)
- {
- m_pImpl = new libwmpplayer_impl(pHostApi);
- return;
- }
- Clibwmpplayer::~Clibwmpplayer()
- {
- delete m_pImpl;
- m_pImpl = NULL;
- }
- VOID Clibwmpplayer::PlayVideo(const char *pVideoDir, const char *pNamePrefix, int nVideoCount)
- {
- m_pImpl->StartPlayVideo(pVideoDir, pNamePrefix, nVideoCount);
- }
- VOID Clibwmpplayer::PlayLocalAudio(const char *pAudioNames)
- {
- m_pImpl->StartPlayLocalAudio(pAudioNames);
- }
- VOID Clibwmpplayer::PlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- m_pImpl->StartPlayLocalVideo(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
- }
- BOOL Clibwmpplayer::checkIsPlay(HANDLE &curThread)
- {
- return m_pImpl->CheckIsPlay(curThread);
- }
- BOOL Clibwmpplayer::checkIsStop()
- {
- return m_pImpl->isStop();
- }
- VOID Clibwmpplayer::PlayMedia(CWmpPlayConfig &config)
- {
- m_pImpl->StartPlayMedia(config);
- }
- VOID Clibwmpplayer::Close()
- {
- m_pImpl->StopPlay();
- }
- VOID Clibwmpplayer::SetVolume( int nVolume )
- {
- m_pImpl->SetVolume(nVolume);
- }
- VOID Clibwmpplayer::PlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoDir, const char *pNamePrefix, int nVideoCount)
- {
- m_pImpl->StartPlaySalesRecordVideo(nWndX, nWndY, nWndWidth, nWndHeight, pVideoDir, pNamePrefix, nVideoCount);
- }
|