#include "stdafx.h" #include "mod_localmediaplay.h" #include "LocalMediaPlay_msg_g.h" #include "fileutil.h" #include "y2k_time.h" #define DEFAULT_SLEEP_TIME (60 * 1000) #define DEFAULT_ADVERT_TYPE 'A' bool IS_DEBUG = false; int scanTime = 600; int removeOldTime = 1800; #ifndef MAX_LOCAL_MEDIAS #define MAX_LOCAL_MEDIAS 128 #endif // !MAX_LOCAL_MEDIAS #ifndef RVC_MIN_LOCALPLAYER_TIME #define RVC_MIN_LOCALPLAYER_TIME 6 #endif //DWORD WINAPI clearOutdata(LPVOID lpv) //{ // CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)lpv; // Sleep(removeOldTime * 1000); // curEntity->m_mediaManage.clearOutdataResource(); // return 0; //} #ifdef RVC_OS_WIN DWORD WINAPI qryMedia(LPVOID lpv) { CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)lpv; Sleep(3000); while (true) { curEntity->m_mediaManage.InitResourceListByLocal(); //curEntity->StartVideo(0, 0, 0, 0, 0); Sleep(3600 * 1000); } return 0; } #else void* queryMedia(void* param) { CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)param; sleep(3); while (false == curEntity->GetScanExitFlag()) { curEntity->m_mediaManage.InitResourceListByLocal(); sleep(3600); } return 0; } void sig_handler(int signum) { if (SIGTERM == signum){ int ipid = getpid(); kill(ipid, SIGKILL); } } #endif // RVC_OS_WIN CLocalMediaPlayEntity::CLocalMediaPlayEntity() : m_id_seq(0), m_connection(NULL) { //stopForDebug(); m_defaultVolum = 50; #ifdef RVC_OS_WIN m_scanThread=NULL; m_pAudioPlayer = NULL; memset(m_pVideoPlayer, 0, sizeof(m_pVideoPlayer)); memset(m_pImagePlayer, 0, sizeof(m_pImagePlayer)); #else //add by clp 20201103 m_scanThreadId = 0; m_pMediaAudioPlayer = NULL; memset(m_pMediaPlayer, 0, sizeof(m_pMediaPlayer)); memset(m_pPicturePlayer, 0, sizeof(m_pPicturePlayer)); m_uMediaPlayThreadId = 0; m_badplayflag = true; m_bgetflag = false; m_bgetico = false; #endif // RVC_OS_WIN ZeroMemory(&m_mediaParam, sizeof(MediaPlayParam)); } CLocalMediaPlayEntity::~CLocalMediaPlayEntity() { SecureClientRelease(); DWORD exitCode = 0; #ifdef RVC_OS_WIN if (m_scanThread) { TerminateThread(m_scanThread, exitCode); m_scanThread = NULL; } #else m_scanexitflag = true; if (0 == pthread_join(m_scanThreadId, NULL)) { Dbg("pthread join scanThreadId success."); } m_bgetflag = false; m_bgetico = false; #endif // RVC_OS_WIN } void CLocalMediaPlayEntity::SecureClientRelease() { if (NULL != m_connection) { if (m_connection->IsConnectionOK()) m_connection->Close(); m_connection->DecRefCount(); m_connection = NULL; } } bool CLocalMediaPlayEntity::SecureClientConnect() { if (NULL != m_connection && m_connection->IsConnectionOK()) return true; SecureClientRelease(); m_connection = new CAdvertSyncConnection(this); return m_connection->ConnectFromCentralSetting(); } void CLocalMediaPlayEntity::setMediaPath() { CSimpleStringA downloadPath, mediaPath; GetFunction()->GetPath("Download", downloadPath); GetFunction()->GetPath("Ad", mediaPath); m_mediaManage.setDefaultDownloadPath(downloadPath.GetData()); m_mediaManage.setDefaultAddvertPath(mediaPath.GetData()); } CServerSessionBase* CLocalMediaPlayEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass) { LOG_FUNCTION(); LOG_TRACE("%s connected class = %s!", pszRemoteEntityName, pszClass); return new CLocalMediaPlaySession(this, m_id_seq++); } void CLocalMediaPlayEntity::loadDefaultMedia() { #ifdef RVC_OS_WIN m_defaultImg.clear(); m_defaultAudio.clear(); m_defaultVideo.clear(); #else m_defaultPic.clear(); m_Audios.clear(); m_Videos.clear(); #endif // RVC_OS_WIN // 读取LocalMediaPlay.ini中的配置 CSimpleStringA strRootPath; ErrorCodeEnum Error = Error_Succeed; Error = GetFunction()->GetPath("ADData", strRootPath); if (Error != Error_Succeed) { Dbg("Fail to get local media root path!"); return; } CSmartPointer spConfig; CAutoArray sectionList; if (Error_Succeed == GetFunction()->OpenConfig(Config_Software, spConfig) && Error_Succeed == spConfig->ReadAllSections(sectionList)) { for (int i = 0; i < sectionList.GetCount(); i++) { CSmartPointer tempConfig = spConfig; SpIniMappingTable table; CSimpleStringA strFileNames; if (sectionList[i].IsStartWith("General")) { table.AddEntryBoolean(sectionList[i].GetData(), "runDebug", IS_DEBUG, false); table.AddEntryInt(sectionList[i].GetData(), "scanTime", scanTime, 600); table.AddEntryInt(sectionList[i].GetData(), "removeOldTime", removeOldTime, 1800); if (Error_Succeed == table.Load(tempConfig)) Dbg("runDebug:%s, scanTime:%d", IS_DEBUG ? "true" : "false", scanTime); else Dbg("General load fail!"); if (removeOldTime < 1800) { removeOldTime = 1800; } } else if (sectionList[i].IsStartWith("Image")) { #ifdef RVC_OS_WIN CImgPlayConfig curImg; ZeroMemory(&curImg, sizeof(CImgPlayConfig)); #else CPicPlayConfig curImg; ZeroMemory(&curImg, sizeof(CPicPlayConfig)); #endif // RVC_OS_WIN CSimpleStringA imgPath = strRootPath + SPLIT_SLASH_STR +"Image" + SPLIT_SLASH_STR; strncpy(curImg.strRootPath, (LPCSTR)imgPath, sizeof(curImg.strRootPath)); Dbg("curSection: %s, config.strRootPath: %s", sectionList[i].GetData(), curImg.strRootPath); table.AddEntryBoolean(sectionList[i].GetData(), "FullScreen", curImg.bFullScreen, false); table.AddEntryBoolean(sectionList[i].GetData(), "PrimMonitor", curImg.bPrimMonitor, false); table.AddEntryInt(sectionList[i].GetData(), "PlayCount", curImg.nPlayCnt, 0); table.AddEntryInt(sectionList[i].GetData(), "PlayInterval", curImg.nPlayInterval, 0); table.AddEntryString(sectionList[i].GetData(), "ImageNames", strFileNames, ""); if (Error_Succeed == table.Load(tempConfig)) Dbg("Image Succeed to LoadConfig!"); else Dbg("Image Fail to LoadConfig!"); char *Tmp = new char[strFileNames.GetLength() + 1]; strcpy(Tmp, (LPCSTR)strFileNames); char *Result[MAX_FILECOUNT] = { NULL }; CStringSplit(Tmp, Result, "|"); int FileCount = 0; char** pStr = Result; while (*pStr != NULL) { ++pStr; ++FileCount; } curImg.nFileCnt = FileCount; Dbg("Image config.nFileCnt = %d!", FileCount); for (int i = 0; i != FileCount; ++i) { strcpy(curImg.strFileNames[i], Result[i]); } delete[] Tmp; Tmp = NULL; #ifdef RVC_OS_WIN m_defaultImg.push_back(curImg); #else m_defaultPic.push_back(curImg); #endif // RVC_OS_WIN } else if (sectionList[i].IsStartWith("Video")) { #ifdef RVC_OS_WIN CWmpPlayConfig curVideo; ZeroMemory(&curVideo, sizeof(CWmpPlayConfig)); #else CMediaPlayConfig curVideo = {0}; #endif // RVC_OS_WIN CSimpleStringA videoPath = strRootPath + SPLIT_SLASH_STR +"Video" + SPLIT_SLASH_STR; strcpy(curVideo.strRootPath, (LPCSTR)videoPath); Dbg("config.strRootPath: %s", curVideo.strRootPath); CSimpleStringA strRunTime_S, strRunTime_E; table.AddEntryString("General", "VideoRunTime_Start", strRunTime_S, ""); table.AddEntryString("General", "VideoRunTime_End", strRunTime_E, ""); table.AddEntryBoolean(sectionList[i].GetData(), "FullScreen", curVideo.bFullScreen, false); table.AddEntryBoolean(sectionList[i].GetData(), "PrimMonitor", curVideo.bPrimMonitor, false); table.AddEntryBoolean(sectionList[i].GetData(), "SimpleMode", curVideo.bSimpleMode, true); table.AddEntryInt(sectionList[i].GetData(), "PlayCount", curVideo.nPlayCnt, 0); table.AddEntryInt(sectionList[i].GetData(), "PlayInterval", curVideo.nPlayInterval, 0); table.AddEntryString(sectionList[i].GetData(), "VideoNames", strFileNames, ""); if (Error_Succeed == table.Load(tempConfig)) Dbg("Succeed to LoadWmpConfig!"); else Dbg("Fail to LoadWmpConfig!"); curVideo.nVolume = 50; Error = GetFunction()->OpenConfig(Config_Run, tempConfig); if (Error == Error_Succeed) { Error = tempConfig->ReadConfigValueInt("LocalVideo", "Volume", curVideo.nVolume); if (Error != Error_Succeed || curVideo.nVolume < 0 || curVideo.nVolume > 100) curVideo.nVolume = 50; m_defaultVolum = curVideo.nVolume; } strcpy(curVideo.strVideoRunTime_S, (LPCSTR)strRunTime_S); Dbg("config.strVideoRunTime_S: %s", (LPCSTR)strRunTime_S); strcpy(curVideo.strVideoRunTime_E, (LPCSTR)strRunTime_E); Dbg("config.strVideoRunTime_E: %s", (LPCSTR)strRunTime_E); char *Tmp = new char[strFileNames.GetLength() + 1]; strcpy(Tmp, (LPCSTR)strFileNames); char *Result[MAX_FILECOUNT] = { NULL }; CStringSplit(Tmp, Result, "|"); int FileCount = 0; char** pStr = Result; while (*pStr != NULL) { ++pStr; ++FileCount; } curVideo.nFileCnt = FileCount; Dbg("Wmp config.strFileNames = %s!", (LPCSTR)strFileNames); Dbg("Wmp config.nFileCnt = %d!", FileCount); for (int i = 0; i != FileCount; ++i) strcpy(curVideo.strFileNames[i], Result[i]); delete[] Tmp; Tmp = NULL; #ifdef RVC_OS_WIN m_defaultVideo.push_back(curVideo); #else m_Videos.push_back(curVideo); #endif // RVC_OS_WIN } else if (sectionList[i].IsStartWith("Audio")) { #ifdef RVC_OS_WIN CWmpPlayConfig curAudio; ZeroMemory(&curAudio, sizeof(CWmpPlayConfig)); #else CMediaPlayConfig curAudio = { 0 }; #endif // RVC_OS_WIN CSimpleStringA audioPath = strRootPath + SPLIT_SLASH_STR +"Audio" + SPLIT_SLASH_STR; strcpy(curAudio.strRootPath, (LPCSTR)audioPath); Dbg("config.strRootPath: %s", curAudio.strRootPath); table.AddEntryInt(sectionList[i], "PlayCount", curAudio.nPlayCnt, 0); table.AddEntryInt(sectionList[i], "PlayInterval", curAudio.nPlayInterval, 0); auto audioRet = GetLocalAudioVolume(); if (audioRet.first) curAudio.nVolume = audioRet.second; else curAudio.nVolume = m_defaultVolum; if (Error_Succeed == table.Load(tempConfig)) Dbg("Succeed to LoadWmpConfig!"); else Dbg("Fail to LoadWmpConfig!"); #ifdef RVC_OS_WIN m_defaultAudio.push_back(curAudio); #else m_Audios.push_back(curAudio); #endif // RVC_OS_WIN } } } } #ifdef RVC_OS_WIN BOOL CLocalMediaPlayEntity::LoadPlayConfig(CWmpPlayConfig& config, int CfgInx) { if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0) { Dbg("Invalid CfgInx while LoadConfig!"); return FALSE; } if (config.eMode == LOCALAUDIO && CfgInx < m_defaultAudio.size()) { memcpy(&config, &(m_defaultAudio[CfgInx]), sizeof(CWmpPlayConfig)); } else if (config.eMode == LOCALVIDEO && CfgInx < m_defaultVideo.size()) { memcpy(&config, &(m_defaultVideo[CfgInx]), sizeof(CWmpPlayConfig)); } else { return FALSE; } return TRUE; } BOOL CLocalMediaPlayEntity::LoadPlayConfig(CImgPlayConfig& config, int CfgInx) { if (CfgInx >= m_defaultImg.size() || CfgInx < 0) { Dbg("Invalid CfgInx while LoadConfig!"); return FALSE; } memcpy(&config, &(m_defaultImg[CfgInx]), sizeof(CImgPlayConfig)); return TRUE; } void CLocalMediaPlayEntity::WmpDebug(const char* fmt, ...) { va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } void CLocalMediaPlayEntity::ImgDebug(const char* fmt, ...) { va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } #else int CLocalMediaPlayEntity::LoadPlayConfig(CMediaPlayConfig& config, int CfgInx) { if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0) { Dbg("Invalid CfgInx while LoadConfig!"); return -1; } if (config.eMode == LOCALAUDIO && CfgInx < m_Audios.size()) { memcpy(&config, &(m_Audios[CfgInx]), sizeof(CMediaPlayConfig)); } else if (config.eMode == LOCALVIDEO && CfgInx < m_Videos.size()) { memcpy(&config, &(m_Videos[CfgInx]), sizeof(CMediaPlayConfig)); } else { return -1; } return 0; } int CLocalMediaPlayEntity::LoadPlayConfig(CPicPlayConfig& config, int CfgInx) { if (CfgInx >= m_defaultPic.size() || CfgInx < 0) { Dbg("Invalid CfgInx while LoadConfig!"); return -1; } memcpy(&config, &(m_defaultPic[CfgInx]), sizeof(CPicPlayConfig)); return 0; } void CLocalMediaPlayEntity::Debug(media_loglevel log_level, const char* fmt, ...) { if (log_level >= MEDIA_LOG_DEBUG){ va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } } void CLocalMediaPlayEntity::PicDebug(pic_loglevel log_level, const char* fmt, ...) { if (log_level >= PIC_LOG_ERROR){ va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } } void CLocalMediaPlayEntity::AudioPlayFinished() { AudioPlayRet ret; ret.AudioNames = m_lastPlayAudio.c_str(); ret.ret = true; Dbg("stop play audio %s success", ret.AudioNames.GetData()); SpSendBroadcast(GetFunction(), eMsg_AudioPlayRet, eMsgSig_AudioPlayRet, ret); } int CLocalMediaPlayEntity::GetMediaPlayerIcoPath(char* strPath, size_t uLen) { return GetPlayerIcoPath(strPath, uLen); } int CLocalMediaPlayEntity::GetAudioOutDevName(char* strDev, size_t uLen) { int iRet = -1; int idatalen = m_strAudioOutDev.GetLength(); if (uLen > idatalen && idatalen > 0) { memcpy(strDev, m_strAudioOutDev.GetData(), idatalen); iRet = 0; if (false == m_bgetflag) { m_bgetflag = true; Dbg("%s:%d audio Out Device Name is %s.", __FUNCTION__, __LINE__, strDev); } } return iRet; } int CLocalMediaPlayEntity::GetPicPlayerIcoPath(char* strPath, size_t uLen) { return GetPlayerIcoPath(strPath, uLen); } bool CLocalMediaPlayEntity::GetPlayFlag() { return m_badplayflag; } bool CLocalMediaPlayEntity::GetScanExitFlag() { return m_scanexitflag; } int CLocalMediaPlayEntity::GetPlayerIcoPath(char* strPath, size_t uLen) { int iRet = -1; if (NULL == strPath) { return iRet; } CSimpleStringA csBinPath; ErrorCodeEnum eErrPath = GetFunction()->GetPath("Bin", csBinPath); if (eErrPath != Error_Succeed) { Dbg("GetBasePath failed (%d).", eErrPath); return iRet; } CSimpleStringA szIcoName("rvc_media_player_64px.bmp"); szIcoName = csBinPath + SPLIT_SLASH_STR + szIcoName; if (m_bgetico == false) { Dbg("media player ico full path is %s.", szIcoName.GetData()); m_bgetico = true; } if (uLen > szIcoName.GetLength()) { memcpy(strPath, szIcoName.GetData(), szIcoName.GetLength()); iRet = 0; m_bgetico = true; } return iRet; } int CLocalMediaPlayEntity::GetAudioOutDev() { int iRet = -1; CSmartPointer spFunction = GetFunction(); CSmartPointer spRootConfig; ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spRootConfig); if (Error == Error_Succeed) { SpIniMappingTable table; table.AddEntryString("audio", "handfree_out_dev", m_strAudioOutDev, "$"); Error = table.Load(spRootConfig); if (Error == Error_Succeed) { Dbg("Audio Out Device name is %s.", m_strAudioOutDev.GetData()); iRet = 0; } } return iRet; } #endif // RVC_OS_WIN void CLocalMediaPlayEntity::OnLog(const CAutoArray &SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray &Param, const char *pszEntityName, const char *pszModuleName, const char *pszMessage) { if (dwUserCode == LOG_EVT_SELFCHECK_IEBROWSER_IDLE) { Dbg("IEBrowser to idle, stop all media!"); StopAll(); } } void CLocalMediaPlayEntity::OnPreStart(CAutoArray strArgs, CSmartPointer pTransactionContext) { LOG_FUNCTION(); ErrorCodeEnum Error = __OnStart(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum CLocalMediaPlayEntity::__OnStart(ErrorCodeEnum preOperationError) { #ifdef RVC_OS_WIN //toolkit_setenv("SDL_AUDIODRIVER", "winmm"); #else GetAudioOutDev(); #endif // RVC_OS_WIN for (int i = 0; i != MAX_PLAY_CHANNELS; ++i) { #ifdef RVC_OS_WIN m_pVideoPlayer[i] = new Clibwmpplayer(this); m_pImagePlayer[i] = new Clibimgplayer(this); #else mediaplayer_init(); m_pMediaPlayer[i] = new Clibmediaplayer(this); m_pPicturePlayer[i] = new Clibpictureplayer(this); #endif } #ifdef RVC_OS_WIN m_pAudioPlayer = new Clibwmpplayer(this); #else m_pMediaAudioPlayer = new Clibmediaplayer(this); signal(SIGTERM, sig_handler); #endif // RVC_OS_WIN if (!IsRunConfigExist()) { SetLocalVideoVolume(0, 50); SetLocalAudioVolume(50); } //setMediaPath(); loadDefaultMedia(); GetFunction()->SubscribeLog(m_SubIDIEIdle, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_SELFCHECK_IEBROWSER_IDLE, NULL, false); #ifdef RVC_OS_WIN m_scanThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& qryMedia, this, 0, NULL); if (NULL != m_scanThread) Dbg("create qryMeia thread success, %d.", m_scanThread); #else int err = pthread_create(&m_scanThreadId, NULL, queryMedia, this); if (0 == err){ Dbg("create queryMedia thread success, %u.", m_scanThreadId); } else{ Dbg("create queryMedia thread failed."); } #endif // RVC_OS_WIN return Error_Succeed; } void CLocalMediaPlayEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer pTransactionContext) { LOG_FUNCTION(); ErrorCodeEnum Error = __OnClose(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum CLocalMediaPlayEntity::__OnClose(ErrorCodeEnum preOperationError) { for (int i = 0; i != MAX_PLAY_CHANNELS; ++i) { #ifdef RVC_OS_WIN delete m_pVideoPlayer[i]; m_pVideoPlayer[i] = NULL; delete m_pImagePlayer[i]; m_pImagePlayer[i] = NULL; #else delete m_pMediaPlayer[i]; m_pMediaPlayer[i] = NULL; delete m_pPicturePlayer[i]; m_pPicturePlayer[i] = NULL; #endif // RVC_OS_WIN } #ifdef RVC_OS_WIN delete m_pAudioPlayer; m_pAudioPlayer = NULL; #else delete m_pMediaAudioPlayer; m_pMediaAudioPlayer = NULL; mediaplayer_term(); #endif // RVC_OS_WIN GetFunction()->UnsubscribeLog(m_SubIDIEIdle); return Error_Succeed; } #ifdef RVC_OS_WIN DWORD WINAPI CheckAudioThread(LPVOID param) { CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param; Dbg("Begin CheckAudioThread"); HANDLE playThread = NULL; if (!entity->m_pAudioPlayer->checkIsPlay(playThread)) { Dbg("Create play audio Media Thread Failed!"); return 0; } WaitForSingleObject(playThread, INFINITE); entity->m_pAudioPlayer->Close(); AudioPlayRet ret; ret.AudioNames = entity->m_lastPlayAudio.c_str(); ret.ret = true; Dbg("stop play audio %s success", ret.AudioNames); SpSendBroadcast(entity->GetFunction(), eMsg_AudioPlayRet, eMsgSig_AudioPlayRet, ret); return 0; } #else void* StartAudioPlayingThreadFunc(void* param) { CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; Dbg("Enter StartAudioPlayingThreadFunc"); const char* pAudioNames = entity->m_lastPlayAudio.c_str(); Dbg("Begin StartAudio, and Audio Info is %s.", pAudioNames); if (0 == entity->m_pMediaAudioPlayer->PlayLocalAudio(pAudioNames)) { Dbg("Succeed to StartAudio, and Audio Info is %s.", pAudioNames); } else{ Dbg("StartAudio failed, for %s is not exsit.", pAudioNames); } Dbg("Leave StartAudioPlayingThreadFunc"); return 0; } #endif // RVC_OS_WIN #ifdef RVC_OS_WIN DWORD WINAPI MediaPlayThread(LPVOID param) { CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param; while (TRUE) { vector curParse; entity->m_mediaManage.GetPlayListByLocal(curParse); if (0 == curParse.size()) { HANDLE playThread = NULL; CWmpPlayConfig config; ZeroMemory(&config, sizeof(CWmpPlayConfig)); memcpy(&config, &(entity->m_defaultVideo[entity->m_mediaParam.nCfgInx]), sizeof(CWmpPlayConfig)); config.nPlayCnt = 1; //can be change config.eMode = LOCALVIDEO; config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; if (IS_DEBUG) { config.bFullScreen = false; config.bPrimMonitor = true; } // add by ly@2018/07/30 if (!entity->GetLocalVideoVolume(0, config.nVolume)) { config.nVolume = entity->m_defaultVolum; } Dbg("config.nVolume=%d while play local video.", config.nVolume); // �жϵ�ǰʱ���Ƿ��������� SYSTEMTIME st; GetLocalTime(&st); TCHAR strNow[TIME_LEN]; sprintf(strNow, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond); if (strcmp(strNow, config.strVideoRunTime_S) < 0 || strcmp(strNow, config.strVideoRunTime_E) >= 0) { Sleep(10000); continue; } entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)) { Dbg("Create play Video Media Thread Failed!"); return 0; } WaitForSingleObject(playThread, INFINITE); if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//ֹͣ���� Dbg("stop play video"); return 0; } else entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close(); } for (vector::iterator i = curParse.begin(); i != curParse.end(); i++) { HANDLE playThread = NULL; Dbg("begin play extend %c:%s, %s", i->type, i->resourcePath.c_str(), i->videoNames.c_str()); if ('V' == i->type) {//video CWmpPlayConfig config; ZeroMemory(&config, sizeof(CWmpPlayConfig)); config.bFullScreen = IS_DEBUG ? false : i->fullScreen; config.bPrimMonitor = IS_DEBUG ? true : i->primMonitor; config.bSimpleMode = i->simpleMode; config.eMode = LOCALVIDEO; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = i->playInterval; // add by ly@2018/07/30 //config.nVolume = IS_DEBUG ? 0 : entity->m_defaultVolum; if (!entity->GetLocalVideoVolume(0, config.nVolume)) { config.nVolume = entity->m_defaultVolum; } Dbg("config.nVolume=%d while play local video.", config.nVolume); config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; strncpy_s(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath)); strncpy_s(config.strFileNames[0], i->videoNames.c_str(), 256); strncpy_s(config.strVideoRunTime_S, "09:00:00", sizeof(config.strVideoRunTime_S)); strncpy_s(config.strVideoRunTime_E, "17:30:00", sizeof(config.strVideoRunTime_E)); entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)) { Dbg("Create play Video Media Thread Failed!"); return 0; } WaitForSingleObject(playThread, INFINITE); if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//ֹͣ���� Dbg("stop play video"); return 0; } else entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close(); } else if ('P' == i->type) {//play Image CImgPlayConfig config; ZeroMemory(&config, sizeof(CImgPlayConfig)); config.bFullScreen = IS_DEBUG ? false : i->fullScreen; config.bPrimMonitor = IS_DEBUG ? true : i->primMonitor; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = i->playInterval; config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; strncpy_s(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath)); strncpy_s(config.strFileNames[0], i->videoNames.c_str(), 256); entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)) { Dbg("Create play Image Media Thread Failed!"); return 0; } WaitForSingleObject(playThread, i->playInterval); if (entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//ֹͣ���� Dbg("stop play Image"); return 0; } else entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->Close(); } } } return 0; } #else void* StartMediaPlayFunc(void* param) { int iRet = -1; if (NULL == param){ return (void*)&iRet; } Dbg("Enter Media Play Function."); CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; while (entity->GetPlayFlag()) { rvcResourceParse_t ResourceList[MAX_LOCAL_MEDIAS] = { 0 }; size_t uCount = entity->m_mediaManage.GetPlayListByLocal(ResourceList, MAX_LOCAL_MEDIAS); unsigned int ustart_time = y2k_time_now(); if (0 == uCount){ int64_t playThreadId = 0; CMediaPlayConfig config = {0}; memcpy(&config, &(entity->m_Videos[entity->m_mediaParam.nCfgInx]), sizeof(CMediaPlayConfig)); config.nPlayCnt = 1; //can be change config.eMode = LOCALVIDEO; config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; if (IS_DEBUG){ config.bFullScreen = false; config.bPrimMonitor = true; } if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } struct tm* ptm = NULL; time_t t = time(NULL); ptm = localtime(&t); TCHAR strNow[TIME_LEN] = {0}; sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); if (strcmp(strNow, config.strVideoRunTime_S) < 0 || strcmp(strNow, config.strVideoRunTime_E) >= 0){ Sleep(10000); continue; } if (-1 == entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config)){ Sleep(30*DEFAULT_SLEEP_TIME); } Sleep(800); } for (int i = 0; i < uCount && i < MAX_LOCAL_MEDIAS && entity->GetPlayFlag(); i++) { //Dbg("ResourceList[%d] address is 0x%0x", i, ResourceList[i]); int64_t playThreadId = 0; rvcResourceParse_t item = ResourceList[i]; //Dbg("begin play extend %c:%s, %s", item.type, item.strResourcePath, item.strvideoNames); if ('V' == item.type) {//video CMediaPlayConfig config = {0}; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : item.primMonitor; config.bSimpleMode = item.simpleMode; config.eMode = LOCALVIDEO; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = item.playInterval; // add by ly@2018/07/30 //config.nVolume = IS_DEBUG ? 0 : entity->m_defaultVolum; if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } //Dbg("config.nVolume=%d while play local video.", config.nVolume); config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; strncpy(config.strRootPath, item.strResourcePath, strlen(item.strResourcePath)); strncpy(config.strFileNames[0], item.strvideoNames, strlen(item.strvideoNames)); strncpy(config.strVideoRunTime_S, "09:00:00", sizeof(config.strVideoRunTime_S)); strncpy(config.strVideoRunTime_E, "17:30:00", sizeof(config.strVideoRunTime_E)); int iPlayRet = entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (-2 == iPlayRet){ Sleep(DEFAULT_SLEEP_TIME); continue; } if (-3 == iPlayRet) { Sleep(DEFAULT_SLEEP_TIME/6); } Sleep(800); } else if ('P' == item.type) {//play Image Dbg("Begin new Image play, entity->m_mediaParam.nCfgInx = %d.", entity->m_mediaParam.nCfgInx); CPicPlayConfig config = {0}; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : item.primMonitor; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = item.playInterval; config.nWndX = entity->m_mediaParam.nWndX; config.nWndY = entity->m_mediaParam.nWndY; config.nWndWidth = entity->m_mediaParam.nWndWidth; config.nWndHeight = entity->m_mediaParam.nWndHeight; strncpy(config.strRootPath, item.strResourcePath, strlen(item.strResourcePath)); strncpy(config.strFileNames[0], item.strvideoNames, strlen(item.strvideoNames)); if (entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){ entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(&playThreadId)) { Dbg("Create play Image Media Thread Failed!"); iRet = -1; return (void*)& iRet; } else { Dbg("Image Media Player Thread id is %u.", playThreadId); } if (0 == pthread_join(playThreadId, NULL)) { Dbg("pthread join thread id %u success.", playThreadId); iRet = 0; } else { Dbg("pthread join thread id %u failed for %s.", playThreadId, strerror(errno)); iRet = -1; } } Sleep(100); } } unsigned int uend_time = y2k_time_now(); //Dbg("uend_time - ustart_time = %u.", uend_time - ustart_time); if (uend_time - ustart_time < RVC_MIN_LOCALPLAYER_TIME) { Sleep(30*DEFAULT_SLEEP_TIME); } } Dbg("Leave Media Play Function."); return (void*)&iRet; } #endif // RVC_OS_WIN void CLocalMediaPlayEntity::StartVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight) { if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0) { Dbg("Invalid CfgInx while StartVideo!"); return; } m_mediaParam.nCfgInx = nCfgInx; m_mediaParam.nWndX = nWndX; m_mediaParam.nWndY = nWndY; m_mediaParam.nWndWidth = nWndWidth; m_mediaParam.nWndHeight = nWndHeight; #ifdef RVC_OS_WIN // RVC_OS_WIN if (NULL != m_playThread) { WaitForSingleObject(m_playThread, 5000); m_playThread = NULL; } if (NULL == m_playThread) m_playThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& MediaPlayThread, this, 0, NULL); #else m_badplayflag = true; int err = pthread_create(&m_uMediaPlayThreadId, NULL, StartMediaPlayFunc, this); if (0 == err) { Dbg("create media play thread[%u] success.", m_uMediaPlayThreadId); } else { Dbg("create media play thread failed."); } #endif } void CLocalMediaPlayEntity::StartAudio(const char *pAudioNames) { #ifdef RVC_OS_WIN static HANDLE audioPlayThread = NULL; if (audioPlayThread)//�ر����ڲ��ŵ���Ƶ { DWORD exitCode = 0; if (WAIT_TIMEOUT == WaitForSingleObject(audioPlayThread, 50)) { TerminateThread(audioPlayThread, exitCode); m_pAudioPlayer->Close(); } audioPlayThread = NULL; } Dbg("Begin StartAudio, and Audio Info is %s.", pAudioNames); m_pAudioPlayer->PlayLocalAudio(pAudioNames); Dbg("Succeed to StartAudio, and Audio Info is %s.", pAudioNames); auto audioRet = GetLocalAudioVolume(); m_pAudioPlayer->SetVolume(audioRet.second); Dbg("Succeed to set audio volume %d!", audioRet.second); m_lastPlayAudio = pAudioNames; audioPlayThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& CheckAudioThread, this, 0, NULL); #else if (m_pMediaAudioPlayer->checkIsPlay()) { Dbg("current is playing, close it."); if (0 != m_uMediaPlayThreadId){ m_pMediaAudioPlayer->Close(); m_uMediaPlayThreadId = 0; Sleep(800); } } m_lastPlayAudio = pAudioNames; int err = pthread_create(&m_uMediaPlayThreadId, NULL, StartAudioPlayingThreadFunc, this); if (0 == err) { Dbg("create audio play thread[%u] success.", m_uMediaPlayThreadId); } else { Dbg("create audio play thread failed."); } auto audioRet = GetLocalAudioVolume(); m_pMediaAudioPlayer->SetVolume(audioRet.second); Dbg("Succeed to set audio volume %d!", audioRet.second); #endif // RVC_OS_WIN } void CLocalMediaPlayEntity::StartImage(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight) { if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0) { Dbg("Invalid CfgInx while StartImage!"); return; } #ifdef RVC_OS_WIN m_pImagePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); #else m_pPicturePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); #endif // RVC_OS_WIN Dbg("Succeed to StartImage!"); } void CLocalMediaPlayEntity::StopVideo(int nCfgInx) { if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0) { Dbg("Invalid CfgInx while StopVideo!"); return; } #ifdef RVC_OS_WIN m_pVideoPlayer[nCfgInx]->Close(); m_pImagePlayer[nCfgInx]->Close(); if (m_playThread) { TerminateThread(m_playThread, -1); m_playThread = NULL; } #else m_pMediaPlayer[nCfgInx]->Close(); m_pPicturePlayer[nCfgInx]->Close(); m_badplayflag = false; #endif // RVC_OS_WIN Dbg("Succeed to StopVideo!"); } void CLocalMediaPlayEntity::StopAudio() { #ifdef RVC_OS_WIN m_pAudioPlayer->Close(); #else m_pMediaAudioPlayer->Close(); #endif Dbg("Succeed to StopAudio!"); } void CLocalMediaPlayEntity::StopImage(int nCfgInx) { if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0) { Dbg("Invalid CfgInx while StopImage!"); return; } #ifdef RVC_OS_WIN m_pImagePlayer[nCfgInx]->Close(); #else m_pPicturePlayer[nCfgInx]->Close(); #endif // RVC_OS_WIN Dbg("Succeed to StopImage!"); } void CLocalMediaPlayEntity::StopAll() { StopAudio(); // �ر���Ƶ for (int i = 0; i < MAX_PLAY_CHANNELS; ++i) { StopVideo(i); // �ر���Ƶ StopImage(i); // �ر�ͼ�� } } bool CLocalMediaPlayEntity::GetLocalVideoVolume(int nCfgInx, int &nVolume) { //Dbg("get local video volume req."); CSmartPointer spConfig; ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { Error = spConfig->ReadConfigValueInt("LocalVideo", "Volume", nVolume); if (Error == Error_Succeed && nVolume >= 0 && nVolume <= 100) { //Dbg("succeed to get local video volume with nCfgInx:%d, nVolume:%d", nCfgInx, nVolume); return true; } } Dbg("fail to get local video volume!"); return false; } std::pair CLocalMediaPlayEntity::GetLocalAudioVolume() { Dbg("get local audio volume req."); CSmartPointer spConfig; int nVolume = 0; ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { Error = spConfig->ReadConfigValueInt("LocalAudio", "Volume", nVolume); //if (nVolume == 0) //{ // nVolume = 50; // spConfig->WriteConfigValueInt("LocalAudio", "Volume", nVolume); // Dbg("read nVolume 0, set default 50"); //} if (Error == Error_Succeed && nVolume >= 0 && nVolume <= 100) { Dbg("succeed to get local audio volume with nVolume:%d", nVolume); return std::make_pair(true, nVolume); } } Dbg("fail to get local audio volume!"); return std::make_pair(false, nVolume); } bool CLocalMediaPlayEntity::SetLocalAudioVolume(int nVolume) { Dbg("set local video audio req."); if (nVolume < 0 || nVolume > 100) { return false; } CSmartPointer spConfig; ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { Error = spConfig->WriteConfigValueInt("LocalAudio", "Volume", nVolume); if (Error == Error_Succeed) { Dbg("succeed to set local audio volume with nVolume:%d!", nVolume); #ifdef RVC_OS_WIN m_pAudioPlayer->SetVolume(nVolume); #else m_pMediaAudioPlayer->SetVolume(nVolume); #endif // RVC_OS_WIN return true; } } Dbg("fail to set local video volume!"); return false; } bool CLocalMediaPlayEntity::SetLocalVideoVolume(int nCfgInx, int nVolume) { Dbg("set local video volume req."); if (nVolume < 0 || nVolume > 100) { return false; } CSmartPointer spConfig; ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig); if (Error == Error_Succeed) { Error = spConfig->WriteConfigValueInt("LocalVideo", "Volume", nVolume); if (Error == Error_Succeed) { Dbg("succeed to set local video volume with nCfgInx:%d, nVolume:%d!", nCfgInx, nVolume); for (int i = 0; i < MAX_PLAY_CHANNELS; ++i) { #ifdef RVC_OS_WIN m_pVideoPlayer[i]->SetVolume(nVolume); #else m_pMediaPlayer[i]->SetVolume(nVolume); #endif } return true; } } Dbg("fail to set local video volume!"); return false; } bool CLocalMediaPlayEntity::IsRunConfigExist() { CSimpleStringA strPath; ErrorCodeEnum eErr; if ((eErr = GetFunction()->GetPath("RunInfo", strPath)) != Error_Succeed) { Dbg("get runinfo path failed(%d)", eErr); return false; } #ifdef RVC_OS_WIN CSimpleStringA strRuninfoFile; strRuninfoFile = CSimpleStringA::Format("%s\\runcfg\\%s.ini", (LPCTSTR)strPath, GetEntityName()); WIN32_FIND_DATA findData; if (FindFirstFileA((LPCTSTR)strRuninfoFile, &findData) != INVALID_HANDLE_VALUE) return true; return false; #else CSimpleStringA strRuninfoFile; strRuninfoFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "%s.ini", (LPCTSTR)strPath, GetEntityName()); if (ExistsFileA(strRuninfoFile)) { return true; } else{ return false; } #endif // RVC_OS_WIN } void CLocalMediaPlayEntity::OnSelfTest(EntityTestEnum eTestType, CSmartPointer pTransactionContext) { if (Test_ShakeHand == eTestType) { pTransactionContext->SendAnswer(Error_Succeed); } } void CLocalMediaPlayEntity::CStringSplit(char *str, char **result, const char *del) { char *p = strtok(str, del); while (p != NULL) { *result++ = p; p = strtok(NULL, del); } } void CLocalMediaPlaySession::Handle_StartPlayVideo(SpReqAnsContext::Pointer ctx) { Dbg("start play Video, Index:%d, WndX:%d, WndY:%d, WndWidth:%d, WndWidth:%d", ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight); CSimpleStringA UpdateState = ""; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState); if (Error == Error_Succeed && "1" == UpdateState) { Dbg("detect current is upgrade, do not play the media"); ctx->Answer(Error_Stoped); m_pEntity->StopAll(); return; } m_pEntity->StartVideo(ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StartPlayAudio(SpReqAnsContext::Pointer ctx) { CSimpleStringA AudioNames = CSimpleStringW2A(ctx->Req.AudioNames); CSimpleStringA UpdateState = ""; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState); if (Error == Error_Succeed && "1" == UpdateState) { Dbg("detect current is upgrade, do not play the media"); ctx->Answer(Error_Stoped); m_pEntity->StopAll(); return; } m_pEntity->StartAudio(AudioNames.GetData()); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StartPlayImage(SpReqAnsContext::Pointer ctx) { m_pEntity->StartImage(ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StopPlayVideo(SpReqAnsContext::Pointer ctx) { Dbg("stop play Video, Index:%d", ctx->Req.CfgInx); m_pEntity->StopVideo(ctx->Req.CfgInx); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StopPlayAudio(SpReqAnsContext::Pointer ctx) { m_pEntity->StopAudio(); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StopPlayImage(SpReqAnsContext::Pointer ctx) { m_pEntity->StopImage(ctx->Req.CfgInx); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::OnClose(ErrorCodeEnum) { LOG_FUNCTION(); } void CLocalMediaPlaySession::Handle_GetLocalVideoVolume( SpReqAnsContext::Pointer ctx ) { int volume = 0; if (m_pEntity->GetLocalVideoVolume(ctx->Req.CfgInx, volume)) { ctx->Ans.Volume = volume; ctx->Answer(Error_Succeed); } else { ctx->Answer(Error_Unexpect); } } void CLocalMediaPlaySession::Handle_SetLocalVideoVolume( SpReqAnsContext::Pointer ctx ) { if (m_pEntity->SetLocalVideoVolume(ctx->Req.CfgInx, ctx->Req.Volume)) { ctx->Answer(Error_Succeed); } else { ctx->Answer(Error_Unexpect); } } void CLocalMediaPlaySession::Handle_GetLocalAudioVolume(SpReqAnsContext::Pointer ctx) { auto audioRet = m_pEntity->GetLocalAudioVolume(); if (audioRet.first) { ctx->Ans.Volume = audioRet.second; ctx->Answer(Error_Succeed); } else { ctx->Answer(Error_Unexpect); } } void CLocalMediaPlaySession::Handle_SetLocalAudioVolume(SpReqAnsContext::Pointer ctx) { if (m_pEntity->SetLocalAudioVolume(ctx->Req.Volume)) { ctx->Answer(Error_Succeed); } else { ctx->Answer(Error_Unexpect); } } SP_BEGIN_ENTITY_MAP() SP_ENTITY(CLocalMediaPlayEntity) SP_END_ENTITY_MAP()