// libimgplayer.cpp : 定义应用程序的类行为。 // #include "stdafx.h" #include "libimgplayer.h" #include "CImgPlayerDlg.h" class libimgplayer_impl { private: CImgPlayerDlg *m_pDlg; HANDLE m_hPlayThread; DWORD m_nPlaythreadId; BOOL m_bIsPlay; CImgPlayConfig m_stPlayConfig; CImgHostApi *m_pHostApi; VOID PlayDlg() { 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) return; if (m_pDlg != NULL) return; AFX_MANAGE_STATE(AfxGetStaticModuleState()); m_pDlg = new CImgPlayerDlg(&m_stPlayConfig); //m_pHostApi->ImgDebug("Ok to initialize CImgDlg!"); 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 ImgPlayThread(LPVOID param) { int i = 0; CoInitialize(NULL); libimgplayer_impl *p = (libimgplayer_impl *)param; p->PlayDlg(); CoUninitialize(); return 0; } public: libimgplayer_impl(CImgHostApi *pHostApi) : m_pDlg(NULL), m_hPlayThread(NULL), m_nPlaythreadId(0), m_bIsPlay(FALSE) { m_pHostApi = pHostApi; memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig)); } ~libimgplayer_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 = m_hPlayThread; return TRUE; } else { return FALSE; } } BOOL StartPlayMedia(CImgPlayConfig &config) { if (m_bIsPlay){ if (m_pDlg != NULL) { return TRUE; } } else { m_bIsPlay = TRUE; } memcpy(&m_stPlayConfig, &config, sizeof(CImgPlayConfig)); m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, ImgPlayThread, (LPVOID)this, 0, (unsigned int*)&m_nPlaythreadId); return TRUE; } BOOL StartPlay(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.nWndX = nWndX; m_stPlayConfig.nWndY = nWndY; m_stPlayConfig.nWndWidth = nWndWidth; m_stPlayConfig.nWndHeight = nWndHeight; BOOL bRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx); if (!bRet){ m_pHostApi->ImgDebug("Load ImgConfiguration failed!"); return FALSE; } else{ m_pHostApi->ImgDebug("Load ImgConfiguration succeeded while play local image!"); m_pHostApi->ImgDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath); } m_hPlayThread = (HANDLE)_beginthreadex(NULL, 0, ImgPlayThread, (LPVOID)this, 0, (unsigned int*)&m_nPlaythreadId); return TRUE; } BOOL StopPlay() { if (m_pDlg != NULL){ if (m_hPlayThread != NULL){ ::PostMessage(m_pDlg->GetSafeHwnd(), WM_CLOSE, NULL, NULL); WaitForSingleObject(m_hPlayThread, INFINITE); } } m_bIsPlay = FALSE; return TRUE; } }; Clibimgplayer::Clibimgplayer(CImgHostApi *pHostApi) { m_pImpl = new libimgplayer_impl(pHostApi); return; } Clibimgplayer::~Clibimgplayer() { delete m_pImpl; m_pImpl = NULL; } VOID Clibimgplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight) { m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); } VOID Clibimgplayer::PlayMedia(CImgPlayConfig &config) { m_pImpl->StartPlayMedia(config); } BOOL Clibimgplayer::checkIsStop(){ return m_pImpl->isStop(); } BOOL Clibimgplayer::checkIsPlay(HANDLE &curThread) { return m_pImpl->CheckIsPlay(curThread); } VOID Clibimgplayer::Close() { m_pImpl->StopPlay(); }