#include "stdafx.h" #include "mod_localmediaplay.h" #include "LocalMediaPlay_msg_g.h" #include "fileutil.h" #define DEFAULT_SLEEP_TIME (60 * 1000) #define DEFAULT_ADVERT_TYPE 'A' bool IS_DEBUG = false; int scanTime = 600; int removeOldTime = 1800; //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(3000); while (true) { curEntity->m_mediaManage.InitResourceListByLocal(); Sleep(3600 * 1000); } } #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)); #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 if (0 == pthread_kill(m_scanThreadId, 0)) { pthread_cancel(m_scanThreadId); } #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 // 获取本地媒体根目录 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; ZeroMemory(&curVideo, sizeof(CMediaPlayConfig)); #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!"); // 音量配置 add by ly at 2017/06/06 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; ZeroMemory(&curAudio, sizeof(CMediaPlayConfig)); #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(const char* fmt, ...) { va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } void CLocalMediaPlayEntity::PicDebug(const char* fmt, ...) { va_list arg; va_start(arg, fmt); vDbg(fmt, arg); va_end(arg); } #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) { LOG_FUNCTION(); #ifdef RVC_OS_WIN //toolkit_setenv("SDL_AUDIODRIVER", "winmm"); #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 Dbg("begin create mediaplayer and pictureplayer"); m_pMediaPlayer[i] = new Clibmediaplayer(this); m_pPicturePlayer[i] = new Clibpictureplayer(this); Dbg("create mediaplayer and pictureplayer success."); #endif } #ifdef RVC_OS_WIN m_pAudioPlayer = new Clibwmpplayer(this); #else m_pMediaAudioPlayer = new Clibmediaplayer(this); #endif // RVC_OS_WIN if (!IsRunConfigExist()) { SetLocalVideoVolume(0, 50); SetLocalAudioVolume(50); } //setMediaPath(); loadDefaultMedia(); // 订阅IEBrowser重启事件 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, %d.", m_scanThreadId); } else { Dbg("create queryMedia thread failed."); } #endif // RVC_OS_WIN return Error_Succeed; } void CLocalMediaPlayEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnClose(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum CLocalMediaPlayEntity::__OnClose(ErrorCodeEnum preOperationError) { LOG_FUNCTION(); 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; #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* CheckAudioPlayingThreadFunc(void* param) { CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; Dbg("Begin CheckAudioPlayingThreadFunc"); pthread_t playThreadId = 0; if (!entity->m_pMediaAudioPlayer->checkIsPlay(&playThreadId)) { Dbg("Create play audio Media Thread Failed!"); return 0; } pthread_join(playThreadId, NULL); 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; } #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 int StartMediaPlay(void* param) { int iRet = -1; if (NULL == param) { return iRet; } CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; while (TRUE) { vector curParse; entity->m_mediaManage.GetPlayListByLocal(curParse); if (0 == curParse.size()) { int64_t playThreadId = 0; CMediaPlayConfig config; ZeroMemory(&config, sizeof(CMediaPlayConfig)); 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; } // 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); // 判断当前时间是否允许播放 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; } entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(&playThreadId)) { Dbg("Create play Video Media Thread Failed!"); return iRet; } iRet = 0; pthread_join(playThreadId, NULL); if (entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//停止播放 Dbg("stop play video"); return iRet; } } for (vector::iterator i = curParse.begin(); i != curParse.end(); i++) { int64_t playThreadId = 0; Dbg("begin play extend %c:%s, %s", i->type, i->resourcePath.c_str(), i->videoNames.c_str()); if ('V' == i->type) {//video CMediaPlayConfig config; ZeroMemory(&config, sizeof(CMediaPlayConfig)); 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(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath)); strncpy(config.strFileNames[0], i->videoNames.c_str(), 256); strncpy(config.strVideoRunTime_S, "09:00:00", sizeof(config.strVideoRunTime_S)); strncpy(config.strVideoRunTime_E, "17:30:00", sizeof(config.strVideoRunTime_E)); entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(&playThreadId)) { Dbg("Create play Video Media Thread Failed!"); return 0; } pthread_join(playThreadId, NULL); if (entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//停止播放 Dbg("stop play video"); return 0; } } else if ('P' == i->type) {//play Image CPicPlayConfig config; ZeroMemory(&config, sizeof(CPicPlayConfig)); 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(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath)); strncpy(config.strFileNames[0], i->videoNames.c_str(), 256); 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!"); return -1; } //WaitForSingleObject(playThread, i->playInterval); struct timespec ts; { int rc = clock_gettime(CLOCK_REALTIME, &ts); if (rc) return -1; } ts.tv_nsec += (i->playInterval * 1000000); pthread_timedjoin_np(playThreadId, NULL, &ts); if (entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()) {//停止播放 Dbg("stop play Image"); return 0; } else { entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->Close(); } } } } return 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 StartMediaPlay(this); #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 static pthread_t audioPlayThreadId = 0; if (0 == pthread_kill(audioPlayThreadId, 0)) { } Dbg("Begin StartAudio, and Audio Info is %s.", pAudioNames); m_pMediaAudioPlayer->PlayLocalAudio(pAudioNames); Dbg("Succeed to StartAudio, and Audio Info is %s.", pAudioNames); auto audioRet = GetLocalAudioVolume(); m_pMediaAudioPlayer->SetVolume(audioRet.second); Dbg("Succeed to set audio volume %d!", audioRet.second); m_lastPlayAudio = pAudioNames; #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(); #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 audo 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); Dbg("start play audio, name:%s", 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((LPCTSTR)AudioNames); 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()