#include "stdafx.h" #include "mod_localmediaplay.h" #include "LocalMediaPlay_msg_g.h" #include "y2k_time.h" #include "fileutil.h" #include "rvc_media_common.h" #include #include "../mod_interactivecontrol/Event.h" #include "Event.h" #ifndef RVC_MIN_LOCALPLAYER_TIME #define RVC_MIN_LOCALPLAYER_TIME 1 #endif #define DEFAULT_SLEEP_TIME 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 #ifndef RVC_VIDEOPLAY_START_TIME #define RVC_VIDEOPLAY_START_TIME "09:00:00" #endif // !RVC_VIDEOPLAY_START_TIME #ifndef RVC_VIDEOPLAY_END_TIME #define RVC_VIDEOPLAY_END_TIME "17:30:00" #endif // !RVC_VIDEOPLAY_END_TIME #ifndef rvc_snprintf #ifdef RVC_OS_WIN #define rvc_snprintf _snprintf #else #define rvc_snprintf snprintf #endif // RVC_OS_WIN #endif // rvc_snprintf static uint32_t ConvertStereo2Mono(char* pDstBuf, const uint32_t uDstLen, char* pSrcBuf, uint32_t uSrcLen, uint32_t uBitDeepth) { uint32_t uRet = 0; uint32_t uOneChannelLen = uSrcLen / 2; uint32_t i = 0; for (; i < uOneChannelLen / 2 && i < uDstLen / uBitDeepth; i++) { memcpy((uint16_t*)pDstBuf + i, ((uint32_t*)(pSrcBuf)) + i, uBitDeepth); } if (i == uOneChannelLen / 2) { uRet = uOneChannelLen; } return uRet; } static uint32_t Transform2Pcm8k(char* pdstbuf, const uint32_t udstlen, char* pcm_441kdata, int idata_size, int ichannels) { uint32_t nLen = 0; int nSkipByte = 5 * ichannels; int nSourcePos = 0; char* pcm_8k_data = (char*)pdstbuf; for (nLen = 0; nSourcePos + 1 < idata_size; nLen = nLen + 2) { pcm_8k_data[nLen] = pcm_441kdata[nSourcePos]; pcm_8k_data[nLen + 1] = pcm_441kdata[nSourcePos + 1]; if (nSkipByte == 5 * ichannels) { nSkipByte = 6 * ichannels; } else { nSkipByte = 5 * ichannels; } nSourcePos = nSourcePos + nSkipByte * 2; } return nLen; } static uint32_t Transform48k2Pcm8k(char* pdstbuf, const uint32_t udstlen, char* pcm_48kdata, int idata_size, int ichannels) { uint32_t nLen = 0; int nSkipByte = 6 * ichannels; int nSourcePos = 0; char* pcm_8k_data = (char*)pdstbuf; for (nLen = 0; nSourcePos + 1 < idata_size; nLen = nLen + 2) { pcm_8k_data[nLen] = pcm_48kdata[nSourcePos]; pcm_8k_data[nLen + 1] = pcm_48kdata[nSourcePos + 1]; nSourcePos = nSourcePos + nSkipByte * 2; } return nLen; } static uint32_t PcmConvert(char* pdstbuf, const uint32_t udstlen, char* pcmsrcdata, int isrclen, int isamplespersec, int ichannels) { uint32_t uLen = 0; if (2 == ichannels) { if (8000 == isamplespersec) { uLen = ConvertStereo2Mono(pdstbuf, udstlen, pcmsrcdata, isrclen, 2); } else if (44100 == isamplespersec) { uLen = Transform2Pcm8k(pdstbuf, udstlen, pcmsrcdata, isrclen, 2); } else if (48000 == isamplespersec) { uLen = Transform48k2Pcm8k(pdstbuf, udstlen, pcmsrcdata, isrclen, 2); } } else { if (44100 == isamplespersec) { uLen = Transform2Pcm8k(pdstbuf, udstlen, pcmsrcdata, isrclen, 1); } else if (48000 == isamplespersec) { uLen = Transform48k2Pcm8k(pdstbuf, udstlen, pcmsrcdata, isrclen, 1); } } return uLen; } static bool IsSupportAudioSamprate(int isamprate) { bool bret = false; if (8000 == isamprate || 44100 == isamprate || 48000 == isamprate) { bret = true; } return bret; } static void CStringSplit(char* str, char** result, const char* del) { char* pdata = NULL; char* p = NULL; #ifdef RVC_OS_WIN p = strtok_s(str, del, &pdata); #else p = strtok_r(str, del, &pdata); #endif // RVC_OS_WIN while (p != NULL) { *result++ = p; #ifdef RVC_OS_WIN p = strtok_s(NULL, del, &pdata); #else p = strtok_r(NULL, del, &pdata); #endif } } static bool IsValidPlayTime(const char* pstrStartTime, const char* pstrEndTime) { bool bRet = true; if (NULL == pstrStartTime || NULL == pstrEndTime) { return false; } #ifdef DEVOPS_ON_ST #else #ifdef RVC_OS_WIN SYSTEMTIME st; GetLocalTime(&st); char strNow[MAX_PATH] = { 0 }; _snprintf(strNow, MAX_PATH, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond); if (strcmp(strNow, pstrStartTime) < 0 || strcmp(strNow, pstrEndTime) >= 0) { bRet = false; } #else struct tm* ptm = NULL; time_t t = time(NULL); ptm = localtime(&t); char strNow[TIME_LEN] = { 0 }; snprintf(strNow, TIME_LEN, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); if (strcmp(strNow, pstrStartTime) < 0 || strcmp(strNow, pstrEndTime) >= 0) { bRet = false; } #endif // RVC_OS_WIN #endif return bRet; } static bool CheckFileExist(const char* pstrFolder, const char* pstrFileName, bool blog) { bool bret = false; if (NULL == pstrFolder || NULL == pstrFileName) { return bret; } CSimpleStringA strFolder(pstrFolder); CSimpleStringA strFileName(pstrFileName); CSimpleStringA strFullPath = strFolder + strFileName; bret = ExistsFileA(strFullPath.GetData()); if (false == bret) { if (blog) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_ADV_VIDEO_NOT_EXIST, CSimpleStringA::Format("adv file %s not exist!", strFullPath.GetData()).GetData()); } } return bret; } #ifdef RVC_OS_WIN DWORD WINAPI qryMedia(LPVOID lpv) { CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)lpv; Sleep(3000); while (true){ curEntity->m_mediaManage.InitResourceListByLocal(); 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_defaultVolum = 50; #ifdef RVC_OS_WIN m_pAudioPlayer = NULL; m_scanThread = NULL; m_playThread = NULL; memset(m_pVideoPlayer, 0, sizeof(m_pVideoPlayer)); memset(m_pImagePlayer, 0, sizeof(m_pImagePlayer)); #endif // RVC_OS_WIN ZeroMemory(&m_mediaParam, sizeof(MediaPlayParam)); m_pMediaAudioPlayer = NULL; memset(m_pMediaPlayer, 0, sizeof(m_pMediaPlayer)); memset(m_pPicturePlayer, 0, sizeof(m_pPicturePlayer)); #ifdef RVC_OS_WIN m_scanThreadId = NULL; m_uMediaPlayThreadId = NULL; m_uNoticePlayThreadId = NULL; m_uAudioPlayThreadId = NULL; #else m_scanThreadId = 0; m_uMediaPlayThreadId = 0; m_uNoticePlayThreadId = 0; m_uAudioPlayThreadId = 0; #endif // !RVC_OS_WIN m_scanexitflag = false; m_badplayflag = true; m_bgetflag = false; m_bgetico = false; m_remote_audio_queue = NULL; m_pFile = NULL; m_bRecordPCM = false; m_iPlayType = 0; m_bStartRecord = false; m_leftlen = 0; m_piclevel = PIC_LOG_INFO; m_medialevel = MEDIA_LOG_INFO; m_buserstopaudio = false; } CLocalMediaPlayEntity::~CLocalMediaPlayEntity() { #ifdef RVC_OS_WIN DWORD exitCode = 0; if (m_scanThread){ TerminateThread(m_scanThread, exitCode); m_scanThread = NULL; } #else m_scanexitflag = true; if (0 != pthread_join(m_scanThreadId, NULL)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pthread join scanThreadId failed."); } m_bgetflag = false; m_bgetico = false; #endif // RVC_OS_WIN } void CLocalMediaPlayEntity::setMediaPath() { CSimpleStringA downloadPath, mediaPath; GetFunction()->GetPath("Download", downloadPath); GetFunction()->GetPath("Ad", mediaPath); mediaPath += SPLIT_SLASH_STR; mediaPath += "Video"; m_mediaManage.setDefaultDownloadPath(downloadPath.GetData()); m_mediaManage.setDefaultAddvertPath(mediaPath.GetData()); } CServerSessionBase* CLocalMediaPlayEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass) { return new CLocalMediaPlaySession(this, m_id_seq++); } #ifdef RVC_OS_WIN bool CLocalMediaPlayEntity::LoadPlayConfig(CWmpPlayConfig &config, int CfgInx) { if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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 >= MAX_PLAY_CHANNELS || CfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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); int n = _vsnprintf(NULL, 0, fmt, arg); if (n >= 512) { char* buf = (char*)malloc((size_t)(n + 1)); _vsnprintf(buf, n + 1, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf); free(buf); } else{ char strlog[512] = {0}; _vsnprintf(strlog, 512, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog); } va_end(arg); } void CLocalMediaPlayEntity::WmpLogEvt(int ievent, const char* strmsg) { if (0 == ievent){ LogWarn(Severity_Low, Error_Debug, LOG_EVT_VICE_MONITOR_NOT_EXIST, strmsg); } else if(1 == ievent){ LogWarn(Severity_Low, Error_Debug, LOG_EVT_VICE_MONITOR_SET_ERROR, strmsg); } } void CLocalMediaPlayEntity::ImgDebug(const char *fmt, ...) { va_list arg; va_start(arg, fmt); int n = _vsnprintf(NULL, 0, fmt, arg); if (n >= 512) { char* buf = (char*)malloc((size_t)(n + 1)); _vsnprintf(buf, n + 1, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf); free(buf); } else{ char strlog[512] = {0}; _vsnprintf(strlog, 512, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog); } va_end(arg); } #endif // RVC_OS_WIN void CLocalMediaPlayEntity::loadDefaultMedia(bool fNewVersion) { if (!fNewVersion) { #ifdef RVC_OS_WIN m_defaultImg.clear(); m_defaultAudio.clear(); m_defaultVideo.clear(); #endif // RVC_OS_WIN } else { m_defaultPic.clear(); m_Audios.clear(); m_Videos.clear(); } // 获取本地媒体根目录 CSimpleStringA strRootPath(""); ErrorCodeEnum Error = Error_Succeed; Error = GetFunction()->GetPath("ADData", strRootPath); if (Error != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Fail to get local media root path!"); return; } CSmartPointer spConfigCen; if (Error_Succeed == GetFunction()->OpenConfig(Config_CenterSetting, spConfigCen)) { IS_DEBUG = false; do { int value(0); spConfigCen->ReadConfigValueInt(GetEntityName(), "runDebug", value); if (value > 0) { IS_DEBUG = true; } } while (false); scanTime = 600; removeOldTime = 1800; do { int value(0); spConfigCen->ReadConfigValueInt(GetEntityName(), "scanTime", value); if (value > 0) { scanTime = value; } } while (false); do { int value(0); spConfigCen->ReadConfigValueInt(GetEntityName(), "removeOldTime", value); if (value > 0) { removeOldTime = value; } } while (false); } CSimpleStringA imgPath = strRootPath + SPLIT_SLASH_STR + "Image" + SPLIT_SLASH_STR; //Image0 do { if (!fNewVersion) { #ifdef RVC_OS_WIN CImgPlayConfig curImg; ZeroMemory(&curImg, sizeof(CImgPlayConfig)); strncpy(curImg.strRootPath, imgPath.GetData(), sizeof(curImg.strRootPath)); curImg.bFullScreen = true; curImg.bPrimMonitor = false; curImg.nPlayCnt = 0; curImg.nPlayInterval = 5000; curImg.nFileCnt = 4; for (int i = 0; i < curImg.nFileCnt; ++i) { rvc_snprintf(curImg.strFileNames[i], MAX_PATH, "Ad0%s%d.jpg", SPLIT_SLASH_STR, i); } m_defaultImg.push_back(curImg); #endif } else { CPicPlayConfig curImg1; ZeroMemory(&curImg1, sizeof(CPicPlayConfig)); strncpy(curImg1.strRootPath, imgPath.GetData(), sizeof(curImg1.strRootPath)); curImg1.bFullScreen = true; curImg1.bPrimMonitor = false; curImg1.nPlayCnt = 0; curImg1.nPlayInterval = 5000; curImg1.nFileCnt = 4; for (int i = 0; i < curImg1.nFileCnt; ++i) { rvc_snprintf(curImg1.strFileNames[i], MAX_PATH, "Ad0%s%d.jpg", SPLIT_SLASH_STR, i); } m_defaultPic.push_back(curImg1); } } while (false); //Image1 do { if (!fNewVersion) { #ifdef RVC_OS_WIN CImgPlayConfig curImg; ZeroMemory(&curImg, sizeof(CImgPlayConfig)); strncpy(curImg.strRootPath, imgPath.GetData(), sizeof(curImg.strRootPath)); curImg.bFullScreen = false; curImg.bPrimMonitor = true; curImg.nPlayCnt = 6; curImg.nPlayInterval = 1000; curImg.nFileCnt = 4; for (int i = 0; i < curImg.nFileCnt; ++i) { rvc_snprintf(curImg.strFileNames[i], MAX_PATH, "Ad1%s%d.jpg", SPLIT_SLASH_STR, i); } m_defaultImg.push_back(curImg); #endif } else { CPicPlayConfig curImg1; ZeroMemory(&curImg1, sizeof(CPicPlayConfig)); strncpy(curImg1.strRootPath, imgPath.GetData(), sizeof(curImg1.strRootPath)); curImg1.bFullScreen = true; curImg1.bPrimMonitor = false; curImg1.nPlayCnt = 6; curImg1.nPlayInterval = 1000; curImg1.nFileCnt = 4; for (int i = 0; i < curImg1.nFileCnt; ++i) { rvc_snprintf(curImg1.strFileNames[i], MAX_PATH, "Ad1%s%d.jpg", SPLIT_SLASH_STR, i); } m_defaultPic.push_back(curImg1); } } while (false); CSimpleStringA videoPath = strRootPath + SPLIT_SLASH_STR + "Video" + SPLIT_SLASH_STR; m_VideoPath = videoPath; //Video0 do { if (!fNewVersion) { #ifdef RVC_OS_WIN CWmpPlayConfig curVideo; ZeroMemory(&curVideo, sizeof(CWmpPlayConfig)); strcpy(curVideo.strRootPath, videoPath.GetData()); //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("config.strRootPath: %s", curVideo.strRootPath); curVideo.bFullScreen = true; curVideo.bPrimMonitor = false; curVideo.bSimpleMode = true; curVideo.nPlayCnt = 1; curVideo.nPlayInterval = 2000; curVideo.nVolume = 50; CSmartPointer tempConfig; 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, RVC_VIDEOPLAY_START_TIME); strcpy(curVideo.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME); curVideo.nFileCnt = 1; strcpy(curVideo.strFileNames[0], "可视柜台欢迎界面.wmv"); m_defaultVideo.push_back(curVideo); #endif // RVC_OS_WIN } else { CMediaPlayConfig curVideo1 = { 0 }; strcpy(curVideo1.strRootPath, videoPath.GetData()); curVideo1.bFullScreen = true; curVideo1.bPrimMonitor = false; curVideo1.bSimpleMode = true; curVideo1.nPlayCnt = 1; curVideo1.nPlayInterval = 2000; curVideo1.nVolume = 50; curVideo1.nFileCnt = 1; strcpy(curVideo1.strFileNames[0], "可视柜台欢迎界面.wmv"); strcpy(curVideo1.strVideoRunTime_S, RVC_VIDEOPLAY_START_TIME); strcpy(curVideo1.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME); m_Videos.push_back(curVideo1); } } while (false); //Video1 do { if (!fNewVersion) { #ifdef RVC_OS_WIN CWmpPlayConfig curVideo; ZeroMemory(&curVideo, sizeof(CWmpPlayConfig)); strcpy(curVideo.strRootPath, videoPath.GetData()); curVideo.bFullScreen = true; curVideo.bPrimMonitor = false; curVideo.bSimpleMode = true; curVideo.nPlayCnt = 0; curVideo.nPlayInterval = 10000; curVideo.nVolume = 50; CSmartPointer tempConfig; 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, RVC_VIDEOPLAY_START_TIME); strcpy(curVideo.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME); curVideo.nFileCnt = 1; strcpy(curVideo.strFileNames[0], "存款保险标识0418.wmv"); m_defaultVideo.push_back(curVideo); #endif // RVC_OS_WIN } else { CMediaPlayConfig curVideo1 = { 0 }; strcpy(curVideo1.strRootPath, videoPath.GetData()); curVideo1.bFullScreen = true; curVideo1.bPrimMonitor = false; curVideo1.bSimpleMode = true; curVideo1.nPlayCnt = 0; curVideo1.nPlayInterval = 10000; curVideo1.nVolume = 50; curVideo1.nFileCnt = 1; strcpy(curVideo1.strFileNames[0], "存款保险标识0418.wmv"); strcpy(curVideo1.strVideoRunTime_S, RVC_VIDEOPLAY_START_TIME); strcpy(curVideo1.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME); m_Videos.push_back(curVideo1); } } while (false); CSimpleStringA audioPath = strRootPath + SPLIT_SLASH_STR + "Audio" + SPLIT_SLASH_STR; m_AudioPath = audioPath; //Audio do { if (!fNewVersion) { #ifdef RVC_OS_WIN CWmpPlayConfig curAudio; ZeroMemory(&curAudio, sizeof(CWmpPlayConfig)); strcpy(curAudio.strRootPath, audioPath.GetData()); //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("config.strRootPath: %s", curAudio.strRootPath); curAudio.nPlayCnt = 1; curAudio.nPlayInterval = 5000; auto audioRet = GetLocalAudioVolume(); if (audioRet.first) { curAudio.nVolume = audioRet.second; } else { curAudio.nVolume = m_defaultVolum; } m_defaultAudio.push_back(curAudio); #endif } else { CMediaPlayConfig curAudio1 = { 0 }; strcpy(curAudio1.strRootPath, audioPath.GetData()); curAudio1.nPlayCnt = 1; curAudio1.nPlayInterval = 5000; auto audioRet = GetLocalAudioVolume(); if (audioRet.first) { curAudio1.nVolume = audioRet.second; } else { curAudio1.nVolume = m_defaultVolum; } m_Audios.push_back(curAudio1); } } while (false); } int CLocalMediaPlayEntity::LoadPlayConfig(CMediaPlayConfig& config, int CfgInx) { if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CfgInx while LoadConfig!"); return -1; } if (config.eMode == MEDIA_LOCALAUDIO && CfgInx < m_Audios.size()){ memcpy(&config, &(m_Audios[CfgInx]), sizeof(CMediaPlayConfig)); } else if (config.eMode == MEDIA_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 >= MAX_PLAY_CHANNELS || CfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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 >= m_medialevel){ va_list arg; va_start(arg, fmt); int n = vsnprintf(NULL, 0, fmt, arg); if (n >= MAX_PATH) { char* buf = (char*)malloc((size_t)(n + 1)); vsnprintf(buf, n + 1, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf); free(buf); } else { char strlog[MAX_PATH] = { 0 }; vsnprintf(strlog, MAX_PATH, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog); } va_end(arg); } } void CLocalMediaPlayEntity::PicDebug(pic_loglevel log_level, const char* fmt, ...) { if (log_level >= m_piclevel){ va_list arg; va_start(arg, fmt); int n = vsnprintf(NULL, 0, fmt, arg); if (n >= MAX_PATH) { char* buf = (char*)malloc((size_t)(n + 1)); vsnprintf(buf, n + 1, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf); free(buf); } else { char strlog[MAX_PATH] = { 0 }; vsnprintf(strlog, MAX_PATH, fmt, arg); DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog); } va_end(arg); } } void CLocalMediaPlayEntity::MediaPlayFinished(int iMediaType) { if (0 == iMediaType) { AudioPlayRet ret; ret.AudioNames = m_lastPlayAudio.c_str(); ret.ret = true; //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop play audio %s success.", ret.AudioNames.GetData()); if (false == m_buserstopaudio) { SpSendBroadcast(GetFunction(), eMsg_AudioPlayRet, eMsgSig_AudioPlayRet, ret); //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpSendBroadcast AudioPlayRet and audio name is %s.", ret.AudioNames.GetData()); } } else { VideoPlayRet ret; ret.VideoNames = m_lastPlayVideo.c_str(); ret.ret = true; SpSendBroadcast(GetFunction(), eMsg_VideoPlayRet, eMsgSig_VideoPlayRet, ret); } } int CLocalMediaPlayEntity::GetMediaPlayerIcoPath(char* strPath, int iLen) { return GetPlayerIcoPath(strPath, iLen); } int CLocalMediaPlayEntity::GetAudioOutDevName(char* strDev, int iLen) { int iRet = -1; int idatalen = m_strAudioOutDev.GetLength(); if (iLen > idatalen && idatalen > 0) { memcpy(strDev, m_strAudioOutDev.GetData(), idatalen); iRet = 0; if (false == m_bgetflag) { m_bgetflag = true; } } return iRet; } int CLocalMediaPlayEntity::PlayingAudioDataCallback(audio_param_t* param, const void* input, unsigned long uaudiolen) { int iRet = -1; if (0 != m_iPlayType && m_bStartRecord) { if (false == m_bloged) { m_bloged = true; if (8000 != param->freq || 1 != param->channels) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_PLAYING_AUDIO_INFOS, CSimpleStringA::Format("audio param target (%d channels, %d Hz, channel_layout(%d), frame_size(%d), bytes_per_sec(%d), audio format(%d)).", param->channels, param->freq, param->channel_layout, param->frame_size, param->bytes_per_sec, param->fmt).GetData()); } if (!IsSupportAudioSamprate(param->freq)) { LogWarn(Severity_Middle, Error_Debug, LOG_EVT_NOTSUPPORT_AUDIOFORMAT, CSimpleStringA::Format("not support audio param (%d channels, %d Hz, channel_layout(%d), frame_size(%d), bytes_per_sec(%d), audio format(%d)).", param->channels, param->freq, param->channel_layout, param->frame_size, param->bytes_per_sec, param->fmt).GetData()); } } //AV_SAMPLE_FMT_S16 == 1 if (1 == param->fmt) { int isingle_audioframe_len = param->bytes_per_sec/50; int ioffset = 0; int icount = (uaudiolen + m_leftlen) / isingle_audioframe_len; //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d icount:%d, isingle_audioframe_len = %d.", __FUNCTION__, __LINE__, icount, isingle_audioframe_len); if (icount > 0) { for (int i = 0; i < icount; i++) { audio_frame frm; char* straudio = new char[isingle_audioframe_len]; if (m_leftlen > 0){ memcpy(straudio, m_audiobuffer, m_leftlen); memcpy(straudio + m_leftlen, (char*)input, isingle_audioframe_len - m_leftlen); ioffset += (isingle_audioframe_len - m_leftlen); m_leftlen = 0; } else{ memcpy(straudio, (char*)input + ioffset, isingle_audioframe_len); ioffset += isingle_audioframe_len; } frm.bitspersample = 16; frm.format = param->fmt; frm.data = straudio; frm.framesize = isingle_audioframe_len; frm.nchannels = param->channels; frm.samplespersec = param->freq; frm.iseriesnumber = m_iseriesnumber++; if (1 == frm.nchannels && 8000 == frm.samplespersec){ if (!m_remote_audio_queue->InsertAudio(&frm)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("InsertAudio failed! frameCount:%d", frm.framesize); } else { //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s:%d InsertAudio(series number = %d) success and framesize = %d.", __FUNCTION__, __LINE__, frm.iseriesnumber, frm.framesize); if (m_bRecordPCM && m_pFile){ fwrite(frm.data, frm.framesize, 1, m_pFile); } } } else{ char strsingle[RVC_AUDIO_LEN*2] = {0}; uint32_t uLen = PcmConvert(strsingle, RVC_AUDIO_LEN*2, straudio, isingle_audioframe_len, frm.samplespersec, frm.nchannels); frm.data = strsingle; frm.framesize = RVC_AUDIO_LEN; frm.nchannels = 1; if (!m_remote_audio_queue->InsertAudio(&frm)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("InsertAudio failed! frameCount:%d", frm.framesize); } else { //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s:%d InsertAudio(series number = %d) success and framesize = %d.", __FUNCTION__, __LINE__, frm.iseriesnumber, frm.framesize); if (m_bRecordPCM && m_pFile) { fwrite(frm.data, frm.framesize, 1, m_pFile); } } } delete []straudio; } memset(m_audiobuffer, 0, RVC_AUDIO_BUFFER_LEN); int ileftaudio = uaudiolen - ioffset; memcpy(m_audiobuffer, (char*)input + ioffset, ileftaudio); m_leftlen = ileftaudio; } else { memcpy(m_audiobuffer + m_leftlen, (char*)input , uaudiolen); m_leftlen += uaudiolen; } iRet = 0; } } return iRet; } int CLocalMediaPlayEntity::GetPicPlayerIcoPath(char* strPath, int uLen) { return GetPlayerIcoPath(strPath, uLen); } bool CLocalMediaPlayEntity::GetPlayFlag() { return m_badplayflag; } bool CLocalMediaPlayEntity::GetScanExitFlag() { return m_scanexitflag; } DeviceTypeEnum CLocalMediaPlayEntity::GetDeviceType() { return m_eDeviceType; } int CLocalMediaPlayEntity::GetPlayerIcoPath(char* strPath, int iLen) { int iRet = -1; if (NULL == strPath) { return iRet; } CSimpleStringA csBinPath; ErrorCodeEnum eErrPath = GetFunction()->GetPath("Bin", csBinPath); if (eErrPath != Error_Succeed) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("GetBasePath failed (%d).", eErrPath); return iRet; } CSimpleStringA szIcoName("rvc_media_player_64px.bmp"); szIcoName = csBinPath + SPLIT_SLASH_STR + szIcoName; if (m_bgetico == false) { m_bgetico = true; } if (iLen > 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) { //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Audio Out Device name is %s.", m_strAudioOutDev.GetData()); iRet = 0; } } return iRet; } void CLocalMediaPlayEntity::LogLocalMediaPlayVolumeInfo() { int iAudioVolume = 50; auto audioret = GetLocalAudioVolume(); if (audioret.first) { iAudioVolume = audioret.second; } int iVideoVolume = 50; GetLocalVideoVolume(0, iVideoVolume); CSimpleStringA strJson(""); strJson += CSimpleStringA::Format("\"%s\":\"%d\",\"%s\":\"%d\"", "LocalAudio", iAudioVolume, "LocalVideo", iVideoVolume); LogWarn(Severity_Low, Error_Debug, LOG_EVT_LOCALMEDIAPLAY_VOLUME, CSimpleStringA::Format("[{%s}]", strJson.GetData()).GetData()); } ErrorCodeEnum CLocalMediaPlayEntity::LoadEntityConfig() { SpIniMappingTable table; int iPlayType = 0; int iMediaLogLevel = (int)MEDIA_LOG_INFO; int iPicLogLevel = (int)PIC_LOG_INFO; bool bRecordPCM = false; table.AddEntryInt("LocalMediaPlay", "PlayType", iPlayType, 0); table.AddEntryInt("LocalMediaPlay", "MediaLevel", iMediaLogLevel, (int)MEDIA_LOG_INFO); table.AddEntryInt("LocalMediaPlay", "PicLevel", iPicLogLevel, (int)PIC_LOG_INFO); table.AddEntryBoolean("SalesRecorder", "IsRecordPCM", bRecordPCM, false); CSmartPointer spConfig; ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_CenterSetting, spConfig); if (Error == Error_Succeed) { Error = table.Load(spConfig); if (Error_Succeed == Error){ m_iPlayType = iPlayType; if (iMediaLogLevel <= MEDIA_LOG_ERROR && iMediaLogLevel > 0){ m_medialevel = (media_loglevel)iMediaLogLevel; } if (iPicLogLevel <= PIC_LOG_ERROR && iPicLogLevel > 0) { m_piclevel = (pic_loglevel)iPicLogLevel; } if (bRecordPCM) { m_bRecordPCM = true; } } } #ifdef RVC_OS_LINUX m_iPlayType = 1; #endif // RVC_OS_LINUX return Error; } 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, const linkContext &pLinkInfo) { if (LOG_EVT_HEALTHMANAGER_BROWSER_IDLE == dwUserCode){ //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("IEBrowser to idle, stop all media!"); StopAll(); } else if(LOG_EVT_UI_STARTREMOTERECORD == dwUserCode){ if (0 != m_iPlayType){ //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Start remote record."); HandleRemoteRecord(pszMessage); } } else if (LOG_EVT_UI_STOPREMOTERECORD == dwUserCode) { if (0 != m_iPlayType){ //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Stop remote record."); m_bStartRecord = false; m_iseriesnumber = 0; m_leftlen = 0; if (NULL != m_remote_audio_queue) { delete m_remote_audio_queue; m_remote_audio_queue = NULL; } if (m_pFile) { fclose(m_pFile); m_pFile = NULL; } } } } void CLocalMediaPlayEntity::OnPreStart(CAutoArray strArgs, CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnStart(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum CLocalMediaPlayEntity::__OnStart(ErrorCodeEnum preOperationError) { m_eDeviceType = RvcGetDeviceType(); if (Error_Succeed != LoadEntityConfig()){ DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("LoadEntityConfig failed."); } return Error_Succeed; } void CLocalMediaPlayEntity::OnStarted() { LogEvent(Severity_Middle, LOG_EVT_MOD_LOCALPLAYER_STARTED_SUCCESS, "local media play entity started successfully."); GetAudioOutDev(); 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); #endif mediaplayer_init(); m_pMediaPlayer[i] = new Clibmediaplayer(this); m_pPicturePlayer[i] = new Clibpictureplayer(this); } m_pMediaAudioPlayer = new Clibmediaplayer(this); #ifdef RVC_OS_WIN m_pAudioPlayer = new Clibwmpplayer(this); #else signal(SIGTERM, sig_handler); #endif // RVC_OS_WIN if (!IsRunConfigExist()){ SetLocalVideoVolume(0, 50); SetLocalAudioVolume(50); } else { LogLocalMediaPlayVolumeInfo(); } setMediaPath(); loadDefaultMedia(0 != m_iPlayType); GetFunction()->SubscribeLog(m_SubIDIEIdle, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_HEALTHMANAGER_BROWSER_IDLE, NULL, false); GetFunction()->SubscribeLog(m_SubIDStartRecord, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_STARTREMOTERECORD, NULL, false); GetFunction()->SubscribeLog(m_SubIDStopRecord, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_STOPREMOTERECORD, NULL, false); if (eStand2sType == m_eDeviceType) { #ifdef RVC_OS_WIN m_scanThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&qryMedia, this, 0, NULL); if (NULL == m_scanThread) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create queryMedia thread failed."); } #else int err = pthread_create(&m_scanThreadId, NULL, queryMedia, this); if (0 != err) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create queryMedia thread failed."); } #endif // RVC_OS_WIN } } void CLocalMediaPlayEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer pTransactionContext) { 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; #endif // RVC_OS_WIN delete m_pMediaPlayer[i]; m_pMediaPlayer[i] = NULL; delete m_pPicturePlayer[i]; m_pPicturePlayer[i] = NULL; } #ifdef RVC_OS_WIN delete m_pAudioPlayer; m_pAudioPlayer = NULL; #endif delete m_pMediaAudioPlayer; m_pMediaAudioPlayer = NULL; GetFunction()->UnsubscribeLog(m_SubIDIEIdle); GetFunction()->UnsubscribeLog(m_SubIDStartRecord); GetFunction()->UnsubscribeLog(m_SubIDStopRecord); // 释放播放对象 if (0 != m_iPlayType){ mediaplayer_term(); } return Error_Succeed; } ErrorCodeEnum CLocalMediaPlayEntity::HandleRemoteRecord(const char* pszMessage) { ErrorCodeEnum Error = Error_Succeed; m_bStartRecord = true; memset(m_audiobuffer, 0, RVC_AUDIO_LEN); m_leftlen = 0; m_remote_audio_queue = new Clibaudioqueue(REC_COMMON_REMOTEAUDIO_SHM_QUEUE); m_iseriesnumber = 0; m_bloged = false; if (true == m_bRecordPCM){ char strSwiftName[MAX_PATH] = { 0 }; if (NULL != pszMessage) { size_t ulen = strlen(pszMessage); char* tmp = new char[ulen + 1]; memset(tmp, 0, ulen + 1); memcpy(tmp, pszMessage, ulen); char* result[16] = { 0 }; auto arr1 = CSimpleStringA2W(tmp).Split('@'); auto arr2 = CAutoArray(arr1.GetCount()); for (int i = 0; i < arr1.GetCount(); ++i) { arr2[i] = CSimpleStringW2A(arr1[i]); result[i] = const_cast(arr2[i].GetData()); } if (arr1.GetCount() > 4) { #ifdef RVC_OS_WIN _snprintf(strSwiftName, MAX_PATH, "%s", result[4]); #else snprintf(strSwiftName, MAX_PATH, "%s", result[4]); #endif // RVC_OS_WIN } delete tmp; tmp = NULL; } CSimpleStringA strTempDir(""); CSimpleStringA strName(""); if (Error_Succeed == GetFunction()->GetPath("Temp", strTempDir)) { strName = CSimpleStringA::Format("%s/remote_audio_notice_%s.pcm", strTempDir.GetData(), strSwiftName); } else { strName = CSimpleStringA::Format("remote_audio_notice_%s.pcm", strSwiftName); } m_pFile = fopen(strName.GetData(), "wb+"); if (NULL == m_pFile) { DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("fopen %s failed.", strName.GetData()); } } return Error; } #ifdef RVC_OS_WIN DWORD WINAPI StartAudioPlayingThreadFunc(LPVOID param) { CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; const char* pAudioNames = entity->m_lastPlayAudio.c_str(); if (0 == entity->m_iPlayType) { HANDLE playEndEvent = NULL; entity->m_pAudioPlayer->PlayLocalAudio(pAudioNames); if (!entity->m_pAudioPlayer->checkIsPlay(playEndEvent)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get audio Play End Event Failed!"); } else { WaitForSingleObject(playEndEvent, INFINITE); if (entity->m_pAudioPlayer->checkIsStop()) { //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop audio play success."); } else { entity->m_pAudioPlayer->Close(); } } } else { if (0 != entity->m_pMediaAudioPlayer->PlayLocalAudio(pAudioNames)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("StartAudio failed, for %s is not exist.", pAudioNames); } } //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Leave StartAudioPlayingThreadFunc"); entity->m_uMediaPlayThreadId = NULL; return 0; } DWORD WINAPI NoticePlayThread(LPVOID param) { CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param; HANDLE playEndEvent = NULL; if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playEndEvent)){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get notice play end event failed!"); return 0; } bool bPlayFinished = FALSE; for (;;) { DWORD dwRet = WaitForSingleObject(playEndEvent, 100); if (dwRet == WAIT_OBJECT_0) { // exit bPlayFinished = true; break; } else { // timeout if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){ break; } } } entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close(); if (bPlayFinished){ VideoPlayRet ret; ret.VideoNames = entity->m_lastPlayVideo.c_str(); ret.ret = true; SpSendBroadcast(entity->GetFunction(), eMsg_VideoPlayRet, eMsgSig_VideoPlayRet, ret); //LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_PLAY_FINISHED, CSimpleStringA::Format("notice %s play finished.", ret.VideoNames.GetData()).GetData()); } else{ //LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_PLAY_EARLY_TERM, CSimpleStringA::Format("notice %s play early terminate.", entity->m_lastPlayVideo.c_str()).GetData()); } entity->m_lastPlayVideo.clear(); return 0; } DWORD WINAPI MediaPlayThread(LPVOID param) { CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param; static bool blog = true; while (entity->GetPlayFlag()) { vector curParse; entity->m_mediaManage.GetPlayListByLocal(curParse); unsigned int ustart_time = y2k_time_now(); 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; config.bPrimMonitor = false; if (IS_DEBUG){ config.bFullScreen = false; config.bPrimMonitor = true; } if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)){ break; } if (false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)){ if (WAIT_TIMEOUT == WaitForSingleObject(entity->m_hStopMediaPlayEvent, 30*DEFAULT_SLEEP_TIME)) { continue; } else { break; } } if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get Play End Event Failed!"); } else{ WaitForSingleObject(playThread, INFINITE); if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){//停止播放 //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop play video"); } else{ entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close(); } } } else { for (vector::iterator i = curParse.begin(); i != curParse.end() && entity->GetPlayFlag(); i++) { HANDLE playThread = NULL; if ('V' == i->type) {//video CWmpPlayConfig config; ZeroMemory(&config, sizeof(CWmpPlayConfig)); config.bFullScreen = IS_DEBUG ? false : i->fullScreen; config.bPrimMonitor = IS_DEBUG ? true : false; config.bSimpleMode = i->simpleMode; config.eMode = LOCALVIDEO; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = i->playInterval; if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } 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, RVC_VIDEOPLAY_START_TIME, sizeof(config.strVideoRunTime_S)); strncpy_s(config.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME, sizeof(config.strVideoRunTime_E)); if (false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)) { if (WAIT_TIMEOUT == WaitForSingleObject(entity->m_hStopMediaPlayEvent, 30 * DEFAULT_SLEEP_TIME)) { continue; } else { break; } } if(!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)){ continue; } entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get Play End Event Failed!"); } else{ WaitForSingleObject(playThread, INFINITE); if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){//停止播放 //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop play video"); } 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 : false; 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); if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { continue; } entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (!entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread)){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Create play Image Media Thread Failed!"); } else { WaitForSingleObject(playThread, i->playInterval); if (entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){//停止播放 //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop play Image"); } else{ entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->Close(); } } } } } unsigned int uend_time = y2k_time_now(); if (uend_time - ustart_time < RVC_MIN_LOCALPLAYER_TIME) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIAPLAY_COST_TIME_ERROR, "广告播放时长不足1秒"); break; } blog = false; } if (entity->m_badplayflag) { entity->m_badplayflag = false; } if (entity->m_playThread) { entity->m_playThread = NULL; } return 0; } DWORD WINAPI StartMediaPlayFunc(LPVOID param) { int iRet = -1; if (NULL == param){ return iRet; } CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; static bool blog = true; while (entity->GetPlayFlag()) { vector curParse; entity->m_mediaManage.GetPlayListByLocal(curParse); unsigned int ustart_time = y2k_time_now(); if (0 == curParse.size()){ CMediaPlayConfig config = {0}; memcpy(&config, &(entity->m_Videos[entity->m_mediaParam.nCfgInx]), sizeof(CMediaPlayConfig)); config.nPlayCnt = 1; //can be change config.eMode = MEDIA_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; config.bPrimMonitor = false; if (IS_DEBUG){ config.bFullScreen = false; config.bPrimMonitor = true; } if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } if(false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)){ if (WAIT_TIMEOUT == WaitForSingleObject(entity->m_hStopMediaPlayEvent, 30 * DEFAULT_SLEEP_TIME)) { continue; } else { break; } } if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { break; } if (-1 == entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config)){ break; } } for (int i = 0; i < curParse.size() && entity->GetPlayFlag(); i++) { HANDLE playThreadId = 0; ResourceParse item = curParse[i]; if ('V' == item.type) {//video CMediaPlayConfig config = {0}; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : false; config.bSimpleMode = item.simpleMode; config.eMode = MEDIA_LOCALVIDEO; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = item.playInterval; if (!entity->GetLocalVideoVolume(0, config.nVolume)){ config.nVolume = entity->m_defaultVolum; } 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.resourcePath.c_str(), item.resourcePath.length() < MAX_PATH ? item.resourcePath.length() : MAX_PATH); strncpy(config.strFileNames[0], item.videoNames.c_str(), item.videoNames.length() < MAX_PATH ? item.videoNames.length() : MAX_PATH); strncpy(config.strVideoRunTime_S, RVC_VIDEOPLAY_START_TIME, sizeof(config.strVideoRunTime_S)); strncpy(config.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME, sizeof(config.strVideoRunTime_E)); if (false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)){ if (WAIT_TIMEOUT == WaitForSingleObject(entity->m_hStopMediaPlayEvent, 30 * DEFAULT_SLEEP_TIME)) { continue; } else { break; } } if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { continue; } int iPlayRet = entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (-3 == iPlayRet) { if (WAIT_TIMEOUT == WaitForSingleObject(entity->m_hStopMediaPlayEvent, DEFAULT_SLEEP_TIME)) { continue; } else { break; } } } else if ('P' == item.type) {//play Image CPicPlayConfig config = {0}; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : false; 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.resourcePath.c_str(), item.resourcePath.length() < MAX_PATH ? item.resourcePath.length() : MAX_PATH); strncpy(config.strFileNames[0], item.videoNames.c_str(), item.videoNames.length() < MAX_PATH ? item.videoNames.length() : MAX_PATH); if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { continue; } 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)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Create play Image Media Thread Failed!"); iRet = -1; return iRet; } WaitForSingleObject(playThreadId, INFINITE); if (entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){//停止播放 } else{ entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->Close(); } } } } unsigned int uend_time = y2k_time_now(); if (uend_time - ustart_time < RVC_MIN_LOCALPLAYER_TIME) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIAPLAY_COST_TIME_ERROR, "广告播放时长不足1秒"); break; } blog = false; } if (entity->m_badplayflag) { entity->m_badplayflag = false; } if (0 != entity->m_uMediaPlayThreadId) { entity->m_uMediaPlayThreadId = 0; } return iRet; } #else void* StartAudioPlayingThreadFunc(void* param) { CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; const char* pAudioNames = entity->m_lastPlayAudio.c_str(); if (0 != entity->m_pMediaAudioPlayer->PlayLocalAudio(pAudioNames)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("StartAudio failed, for %s is not exsit.", pAudioNames); } entity->m_uAudioPlayThreadId = 0; return 0; } void* StartMediaPlayFunc(void* param) { int iRet = -1; if (NULL == param) { return (void*)&iRet; } CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; static bool blog = true; while (entity->GetPlayFlag()) { rvcResourceParse_t ResourceList[MAX_LOCAL_MEDIAS] = { 0 }; int iCount = entity->m_mediaManage.GetPlayListByLocal(ResourceList, MAX_LOCAL_MEDIAS); unsigned int ustart_time = y2k_time_now(); if (0 == iCount) { 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 = MEDIA_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; config.bPrimMonitor = false; if (IS_DEBUG) { config.bFullScreen = false; config.bPrimMonitor = true; } if (!entity->GetLocalVideoVolume(0, config.nVolume)) { config.nVolume = entity->m_defaultVolum; } if (false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)){ struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 30; int iresult = sem_timedwait(&entity->m_stop_mediaplay_semt, &ts); if (0 != iresult && (ETIMEDOUT == errno)) { continue; } else { break; } } if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { break; } if (-1 == entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config)) { break; } } for (int i = 0; i < iCount && i < MAX_LOCAL_MEDIAS && entity->GetPlayFlag(); i++) { int64_t playThreadId = 0; rvcResourceParse_t item = ResourceList[i]; if ('V' == item.type) {//video CMediaPlayConfig config = { 0 }; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : false; config.bSimpleMode = item.simpleMode; config.eMode = MEDIA_LOCALVIDEO; config.nFileCnt = 1; config.nPlayCnt = 1; config.nPlayInterval = item.playInterval; if (!entity->GetLocalVideoVolume(0, config.nVolume)) { config.nVolume = entity->m_defaultVolum; } 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, RVC_VIDEOPLAY_START_TIME, sizeof(config.strVideoRunTime_S)); strncpy(config.strVideoRunTime_E, RVC_VIDEOPLAY_END_TIME, sizeof(config.strVideoRunTime_E)); if (false == IsValidPlayTime(config.strVideoRunTime_S, config.strVideoRunTime_E)) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 30; if (0 != sem_timedwait(&entity->m_stop_mediaplay_semt, &ts) && (ETIMEDOUT == errno)) { continue; } else { break; } } if (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { continue; } int iPlayRet = entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config); if (-3 == iPlayRet) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 2; if (0 != sem_timedwait(&entity->m_stop_mediaplay_semt, &ts) && (ETIMEDOUT == errno)) { continue; } else { break; } } } else if ('P' == item.type) {//play Image CPicPlayConfig config = { 0 }; config.bFullScreen = IS_DEBUG ? false : item.fullScreen; config.bPrimMonitor = IS_DEBUG ? true : false; 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 (!CheckFileExist(config.strRootPath, config.strFileNames[0], blog)) { continue; } 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)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Create play Image Media Thread Failed!"); iRet = -1; return (void*)&iRet; } if (0 == pthread_join(playThreadId, NULL)) { iRet = 0; } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("pthread join thread id %u failed for %s.", playThreadId, strerror(errno)); iRet = -1; } } } } unsigned int uend_time = y2k_time_now(); if (uend_time - ustart_time < RVC_MIN_LOCALPLAYER_TIME) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_MEDIAPLAY_COST_TIME_ERROR, "广告播放时长不足1秒"); break; } blog = false; } entity->m_uMediaPlayThreadId = 0; return (void*)&iRet; } void* StartNoticePlayThreadFunc(void* param) { int iRet = -1; CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; const char* pVideoName = entity->m_lastPlayVideo.c_str(); Clibmediaplayer* player = entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]; player->PlayVideoNotice(entity->m_mediaParam.nWndX, entity->m_mediaParam.nWndY, entity->m_mediaParam.nWndWidth, entity->m_mediaParam.nWndHeight, pVideoName); entity->m_uNoticePlayThreadId = 0; iRet = 0; return (void*)&iRet; } #endif DeviceTypeEnum CLocalMediaPlayEntity::RvcGetDeviceType() { DeviceTypeEnum eType = eStand2sType; CSmartPointer spFunction = GetFunction(); CSystemStaticInfo stStaticinfo; spFunction->GetSystemStaticInfo(stStaticinfo); if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) { eType = eStand1SPlusType; } else if (_stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) { eType = eCardStore; } else { eType = eStand2sType; } if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) { DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]); } return eType; } void CLocalMediaPlayEntity::StartVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight) { if(eStand2sType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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 if (0 == m_iPlayType) { if (NULL != m_playThread) { StopVideo(nCfgInx); } if (NULL == m_playThread) { m_hStopMediaPlayEvent = CreateEventA(NULL, false, false, NULL); m_badplayflag = true; m_playThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&MediaPlayThread, this, 0, NULL); } } else { if (NULL != m_uMediaPlayThreadId) { StopVideo(nCfgInx); } m_hStopMediaPlayEvent = CreateEventA(NULL, false, false, NULL); m_badplayflag = true; m_uMediaPlayThreadId = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&StartMediaPlayFunc, this, 0, NULL); } #else sem_init(&m_stop_mediaplay_semt, 0, 0); m_badplayflag = true; int err = pthread_create(&m_uMediaPlayThreadId, NULL, StartMediaPlayFunc, this); if (0 != err) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create media play thread failed."); } #endif // RVC_OS_WIN } } void CLocalMediaPlayEntity::StartAudio(const char *pAudioNames) { if (NULL != pAudioNames) { char strAudios[MAX_PATH] = { 0 }; rvc_snprintf(strAudios, MAX_PATH, "%s", pAudioNames); const char* d = "|"; char* pName = NULL; char* pdata = NULL; #ifdef RVC_OS_WIN pName = strtok_s(strAudios, d, &pdata); #else pName = strtok_r(strAudios, d, &pdata); #endif while (pName) { CSimpleStringA strAudioFileName = pName; CSimpleStringA strAudioFullPath = m_AudioPath + strAudioFileName; if (!ExistsFileA(strAudioFullPath.GetData())) { LogWarn(Severity_Low, Error_Debug, LOG_EVT_ADV_AUDIO_NOT_EXIST, CSimpleStringA::Format("audio file %s not exist!", strAudioFullPath.GetData()).GetData()); return; } #ifdef RVC_OS_WIN pName = strtok_s(NULL, d, &pdata); #else pName = strtok_r(NULL, d, &pdata); #endif } } #ifdef RVC_OS_WIN if (0 == m_iPlayType) { if (!m_pAudioPlayer->checkIsStop()) { StopAudio(); } m_lastPlayAudio = pAudioNames; m_uAudioPlayThreadId = CreateThread(NULL, 0, StartAudioPlayingThreadFunc, this, 0, NULL); m_buserstopaudio = false; auto audioRet = GetLocalAudioVolume(); m_pMediaAudioPlayer->SetVolume(audioRet.second); } else { if (m_pMediaAudioPlayer->checkIsPlay()) { StopAudio(); } m_lastPlayAudio = pAudioNames; m_uAudioPlayThreadId = CreateThread(NULL, 0, StartAudioPlayingThreadFunc, this, 0, NULL); m_buserstopaudio = false; auto audioRet = GetLocalAudioVolume(); m_pMediaAudioPlayer->SetVolume(audioRet.second); } #else if (m_pMediaAudioPlayer->checkIsPlay()) { StopAudio(); } m_lastPlayAudio = pAudioNames; int err = pthread_create(&m_uAudioPlayThreadId, NULL, StartAudioPlayingThreadFunc, this); if (0 != err) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create audio play thread failed."); } m_buserstopaudio = false; auto audioRet = GetLocalAudioVolume(); m_pMediaAudioPlayer->SetVolume(audioRet.second); #endif } void CLocalMediaPlayEntity::StartImage(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight) { if(eStand2sType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CfgInx while StartImage!"); return; } #ifdef RVC_OS_WIN if (0 == m_iPlayType){ m_pImagePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); } else{ m_pPicturePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); } #else m_pPicturePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight); #endif //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Succeed to StartImage!"); } } void CLocalMediaPlayEntity::StopVideo(int nCfgInx) { if(eStand2sType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CfgInx while StopVideo!"); return; } m_badplayflag = false; #ifdef RVC_OS_WIN if (0 == m_iPlayType){ if (NULL != m_playThread) { m_pVideoPlayer[nCfgInx]->Close(); m_pImagePlayer[nCfgInx]->Close(); SetEvent(m_hStopMediaPlayEvent); WaitForSingleObject(m_playThread, INFINITE); m_playThread = NULL; CloseHandle(m_hStopMediaPlayEvent); } } else{ if (NULL != m_uMediaPlayThreadId) { m_pMediaPlayer[nCfgInx]->Close(); m_pPicturePlayer[nCfgInx]->Close(); SetEvent(m_hStopMediaPlayEvent); WaitForSingleObject(m_uMediaPlayThreadId, INFINITE); m_uMediaPlayThreadId = NULL; CloseHandle(m_hStopMediaPlayEvent); } } #else if (0 != m_uMediaPlayThreadId) { m_pMediaPlayer[nCfgInx]->Close(); m_pPicturePlayer[nCfgInx]->Close(); sem_post(&m_stop_mediaplay_semt); pthread_join(m_uMediaPlayThreadId, NULL); m_uMediaPlayThreadId = 0; sem_destroy(&m_stop_mediaplay_semt); } #endif } } void CLocalMediaPlayEntity::StopAudio() { #ifdef RVC_OS_WIN if (NULL != m_uAudioPlayThreadId) { if (0 == m_iPlayType) { m_pAudioPlayer->Close(); } else { m_pMediaAudioPlayer->Close(); } m_buserstopaudio = true; WaitForSingleObject(m_uAudioPlayThreadId, INFINITE); m_uAudioPlayThreadId = NULL; } #else if (0 != m_uAudioPlayThreadId) { m_pMediaAudioPlayer->Close(); m_buserstopaudio = true; pthread_join(m_uAudioPlayThreadId, NULL); m_uAudioPlayThreadId = 0; } #endif } void CLocalMediaPlayEntity::StopImage(int nCfgInx) { if(eStand2sType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CfgInx while StopImage!"); return; } #ifdef RVC_OS_WIN if (0 == m_iPlayType){ if (false == m_pImagePlayer[nCfgInx]->checkIsStop()) { m_pImagePlayer[nCfgInx]->Close(); } } else{ if (false == m_pPicturePlayer[nCfgInx]->checkIsStop()) { m_pPicturePlayer[nCfgInx]->Close(); } } #else if (false == m_pPicturePlayer[nCfgInx]->checkIsStop()) { m_pPicturePlayer[nCfgInx]->Close(); } #endif } } void CLocalMediaPlayEntity::StopAll() { StopAudio(); // 关闭音频 for (int i = 0; i < MAX_PLAY_CHANNELS; ++i){ StopVideo(i); // 关闭视频 StopImage(i); // 关闭图像 StopNotice(i); // 关闭视频提示 } } bool CLocalMediaPlayEntity::GetLocalVideoVolume(int nCfgInx, int &nVolume) { 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) { return true; } } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail to get local video volume!"); return false; } std::pair CLocalMediaPlayEntity::GetLocalAudioVolume() { 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); } if (Error == Error_Succeed && nVolume >= 0 && nVolume <= 100) { return std::make_pair(true, nVolume); } } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail to get local audio volume!"); return std::make_pair(false, nVolume); } bool CLocalMediaPlayEntity::SetLocalAudioVolume(int nVolume) { 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) { #ifdef RVC_OS_WIN if (0 == m_iPlayType) { m_pAudioPlayer->SetVolume(nVolume); } else { m_pMediaAudioPlayer->SetVolume(nVolume); } #else m_pMediaAudioPlayer->SetVolume(nVolume); #endif return true; } } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail to set local video volume!"); return false; } bool CLocalMediaPlayEntity::SetLocalVideoVolume(int nCfgInx, int nVolume) { 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) { for (int i = 0; i < MAX_PLAY_CHANNELS; ++i) { #ifdef RVC_OS_WIN if (0 == m_iPlayType){ m_pVideoPlayer[i]->SetVolume(nVolume); } else{ m_pMediaPlayer[i]->SetVolume(nVolume); } #else m_pMediaPlayer[i]->SetVolume(nVolume); #endif } return true; } } DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail to set local video volume!"); return false; } bool CLocalMediaPlayEntity::IsRunConfigExist() { CSimpleStringA strPath; ErrorCodeEnum eErr; if ((eErr = GetFunction()->GetPath("RunInfo", strPath)) != Error_Succeed){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get runinfo path failed(%d)", eErr); return false; } 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; } } ErrorCodeEnum CLocalMediaPlayEntity::StartMediaPlayerNotice(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoName) { #ifdef RVC_OS_WIN if (false == m_pAudioPlayer->checkIsStop()) { StopAudio(); } if (false == m_pVideoPlayer[nCfgInx]->checkIsStop()) { StopNotice(nCfgInx); } #endif // RVC_OS_WI m_mediaParam.nCfgInx = nCfgInx; m_mediaParam.nWndX = nWndX; m_mediaParam.nWndY = nWndY; m_mediaParam.nWndWidth = nWndWidth%2 ? nWndWidth+1 : nWndWidth; m_mediaParam.nWndHeight = nWndHeight%2 ? nWndHeight+1 : nWndHeight; CSimpleStringA strPath; ErrorCodeEnum Error = GetFunction()->GetPath("ADData", strPath); if (Error == Error_Succeed) { #ifdef RVC_OS_WIN CSimpleStringA VideoPath = strPath + SPLIT_SLASH_STR + "Video" + SPLIT_SLASH_STR; CSimpleStringA strNoticeFileName = VideoPath + pVideoName; if(ExistsFileA(strNoticeFileName.GetData())){ if (m_pMediaPlayer[nCfgInx]->IsFileValid(strNoticeFileName.GetData())){ int iVolume = 60; GetLocalVideoVolume(nCfgInx,iVolume); m_pVideoPlayer[nCfgInx]->SetVolume(iVolume); m_lastPlayVideo = pVideoName; m_uNoticePlayThreadId = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&StartNoticePlayThreadFunc, this, 0, NULL); //LogWarn(Severity_Low, Error_Debug, LOG_EVT_START_NOTICE_PLAY, CSimpleStringA::Format("start play notice %s", strNoticeFileName.GetData()).GetData()); } else{ Error = Error_InvalidState; LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_FILE_NOT_VALID, CSimpleStringA::Format("play notice failed for %s is not valid!", strNoticeFileName.GetData()).GetData()); } } else{ Error = Error_InvalidState; LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_FILE_NOT_EXIST, CSimpleStringA::Format("play notice failed for %s not exist!", strNoticeFileName.GetData()).GetData()); } #endif } return Error; } #ifdef RVC_OS_WIN DWORD WINAPI StartNoticePlayThreadFunc(LPVOID param) { int iRet = -1; CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param; const char* pVideoName = entity->m_lastPlayVideo.c_str(); int nCfgInx = entity->m_mediaParam.nCfgInx; int nWndX = entity->m_mediaParam.nWndX; int nWndY = entity->m_mediaParam.nWndY; int nWndWidth = entity->m_mediaParam.nWndWidth; int nWndHeight = entity->m_mediaParam.nWndHeight; if (0 == entity->m_iPlayType) { HANDLE playEndEvent = NULL; entity->m_pVideoPlayer[nCfgInx]->PlayVideoNotice(nWndX, nWndY, nWndWidth, nWndHeight, entity->m_VideoPath.GetData(), pVideoName); if (!entity->m_pVideoPlayer[nCfgInx]->checkIsPlay(playEndEvent)) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get notice Play End Event Failed!"); } else { WaitForSingleObject(playEndEvent, INFINITE); if (entity->m_pVideoPlayer[nCfgInx]->checkIsStop()) { //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("stop notice play success."); } else { entity->m_pVideoPlayer[nCfgInx]->Close(); } } } else { Clibmediaplayer* player = entity->m_pMediaPlayer[nCfgInx]; player->PlayVideoNotice(nWndX, nWndY, nWndWidth, nWndHeight, pVideoName); } entity->m_uNoticePlayThreadId = NULL; iRet = 0; return iRet; } #endif ErrorCodeEnum CLocalMediaPlayEntity::StartFFPlayerNotice(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoName) { ErrorCodeEnum Error = Error_Succeed; if (m_pMediaAudioPlayer->checkIsPlay()) { StopAudio(); } if (m_pMediaPlayer[nCfgInx]->checkIsPlay()) { StopNotice(nCfgInx); } m_mediaParam.nCfgInx = nCfgInx; m_mediaParam.nWndX = nWndX; m_mediaParam.nWndY = nWndY; m_mediaParam.nWndWidth = nWndWidth % 2 ? nWndWidth + 1 : nWndWidth; m_mediaParam.nWndHeight = nWndHeight % 2 ? nWndHeight + 1 : nWndHeight; CSimpleStringA strPath; Error = GetFunction()->GetPath("ADData", strPath); if (Error == Error_Succeed) { CSimpleStringA VideoPath = strPath + SPLIT_SLASH_STR + "Video" + SPLIT_SLASH_STR; CSimpleStringA strNoticeFileName = VideoPath + CSimpleStringA(pVideoName); BOOL bRet = ExistsFile(strNoticeFileName.GetData()); if (bRet) { if (m_pMediaPlayer[nCfgInx]->IsFileValid(strNoticeFileName.GetData())){ int iVolume = 60; GetLocalVideoVolume(nCfgInx, iVolume); m_pMediaPlayer[nCfgInx]->SetVolume(iVolume); m_lastPlayVideo = strNoticeFileName.GetData(); #ifdef RVC_OS_WIN m_uNoticePlayThreadId = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&StartNoticePlayThreadFunc, this, 0, NULL); if (NULL != m_uNoticePlayThreadId) { //LogWarn(Severity_Low, Error_Debug, LOG_EVT_START_NOTICE_PLAY, CSimpleStringA::Format("start play video notice %s", strNoticeFileName.GetData()).GetData()); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create notice play thread failed."); } #else int err = pthread_create(&m_uNoticePlayThreadId, NULL, StartNoticePlayThreadFunc, this); if (0 == err) { //LogWarn(Severity_Low, Error_Debug, LOG_EVT_START_NOTICE_PLAY, CSimpleStringA::Format("start play video notice %s", strNoticeFileName.GetData()).GetData()); } else { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create notice play thread failed."); } #endif } else{ Error = Error_InvalidState; LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_FILE_NOT_VALID, CSimpleStringA::Format("play notice failed for %s is not valid!", strNoticeFileName.GetData()).GetData()); } } else { Error = Error_InvalidState; LogWarn(Severity_Low, Error_Debug, LOG_EVT_NOTICE_FILE_NOT_EXIST, CSimpleStringA::Format("play notice failed for %s not exist!", strNoticeFileName.GetData()).GetData()); } } return Error; } ErrorCodeEnum CLocalMediaPlayEntity::StartNotice(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char *pVideoName) { ErrorCodeEnum Error = Error_Succeed; if (eStand2sType == m_eDeviceType || eStand1SPlusType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0 || NULL == pVideoName) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid param CfgInx or video name while StartNotice!"); return Error_Param; } if (0 == m_iPlayType){ Error = StartMediaPlayerNotice(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight, pVideoName); } else{ Error = StartFFPlayerNotice(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight, pVideoName); } } return Error; } ErrorCodeEnum CLocalMediaPlayEntity::StopNotice(int nCfgInx) { ErrorCodeEnum ErrorCode = Error_Succeed; if (eStand2sType == m_eDeviceType || eStand1SPlusType == m_eDeviceType){ if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid CfgInx while StopNotice!"); return Error_Param; } #ifdef RVC_OS_WIN if (NULL != m_uNoticePlayThreadId) { if (0 == m_iPlayType) { if (false == m_pVideoPlayer[nCfgInx]->checkIsStop()) { m_pVideoPlayer[nCfgInx]->Close(); } } else { if (false == m_pMediaPlayer[nCfgInx]->checkIsStop()) { m_pMediaPlayer[nCfgInx]->Close(); } } WaitForSingleObject(m_uNoticePlayThreadId, INFINITE); if (m_lastPlayVideo.length() > 0) { //LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOP_NOTICE_PLAY, CSimpleStringA::Format("success to stop play notice ", m_lastPlayVideo.c_str()).GetData()); } m_uNoticePlayThreadId = NULL; } #else m_pMediaPlayer[nCfgInx]->Close(); if (m_uNoticePlayThreadId > 0) { if (0 == pthread_join(m_uNoticePlayThreadId, NULL)) { if (m_lastPlayVideo.length() > 0) { //LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOP_NOTICE_PLAY, CSimpleStringA::Format("%s%s", "success to stop play notice ", m_lastPlayVideo.c_str()).GetData()); } } m_uNoticePlayThreadId = 0; } #endif } return ErrorCode; } ErrorCodeEnum CLocalMediaPlayEntity::StopPlayAllMedias() { ErrorCodeEnum ErrorCode = Error_Succeed; StopAll(); return ErrorCode; } void CLocalMediaPlayEntity::OnSelfTest(EntityTestEnum eTestType, CSmartPointer pTransactionContext) { if (Test_ShakeHand == eTestType){ pTransactionContext->SendAnswer(Error_Succeed); } } void CLocalMediaPlaySession::Handle_StartPlayVideo(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); CSimpleStringA UpdateState = ""; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState); if (Error == Error_Succeed && "1" == UpdateState) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("detect current is upgrade, do not play the media"); ctx->Answer(Error_Stoped); m_pEntity->StopAll(); return; } if (eStand2sType == m_pEntity->GetDeviceType()){ 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) { DbgToBeidou(ctx->link, __FUNCTION__)(); CSimpleStringA AudioNames = CSimpleStringW2A(ctx->Req.AudioNames); CSimpleStringA UpdateState = ""; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState); if (Error == Error_Succeed && "1" == UpdateState) { DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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) { DbgToBeidou(ctx->link, __FUNCTION__)(); 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) { DbgToBeidou(ctx->link, __FUNCTION__)(); m_pEntity->StopVideo(ctx->Req.CfgInx); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StopPlayAudio(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); m_pEntity->StopAudio(); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::Handle_StopPlayImage(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); m_pEntity->StopImage(ctx->Req.CfgInx); ctx->Answer(Error_Succeed); } void CLocalMediaPlaySession::OnClose(ErrorCodeEnum) { } void CLocalMediaPlaySession::Handle_GetLocalVideoVolume( SpReqAnsContext::Pointer ctx ) { DbgToBeidou(ctx->link, __FUNCTION__)(); 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 ) { DbgToBeidou(ctx->link, __FUNCTION__)(); 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) { DbgToBeidou(ctx->link, __FUNCTION__)(); 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) { DbgToBeidou(ctx->link, __FUNCTION__)(); if (m_pEntity->SetLocalAudioVolume(ctx->Req.Volume)){ ctx->Answer(Error_Succeed); } else{ ctx->Answer(Error_Unexpect); } } void CLocalMediaPlaySession::Handle_StartPlayNotice(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); CSimpleStringA UpdateState = ""; ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState); if (Error == Error_Succeed && "1" == UpdateState){ DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("detect current is upgrade, do not play the media"); ctx->Answer(Error_Stoped); m_pEntity->StopAll(); return; } CSimpleStringA VideoName = CSimpleStringW2A(ctx->Req.VideoName); Error = m_pEntity->StartNotice(ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight, VideoName.GetData()); ctx->Answer(Error); } void CLocalMediaPlaySession::Handle_StopPlayNotice(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); ErrorCodeEnum Error = m_pEntity->StopNotice(ctx->Req.CfgInx); ctx->Answer(Error); } void CLocalMediaPlaySession::Handle_StopPlayAllMedias(SpReqAnsContext::Pointer ctx) { DbgToBeidou(ctx->link, __FUNCTION__)(); ErrorCodeEnum Error = m_pEntity->StopPlayAllMedias(); ctx->Answer(Error); } SP_BEGIN_ENTITY_MAP() SP_ENTITY(CLocalMediaPlayEntity) SP_END_ENTITY_MAP()