mod_localmediaplay.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. #include "stdafx.h"
  2. #include "mod_localmediaplay.h"
  3. #include "LocalMediaPlay_msg_g.h"
  4. #include "fileutil.h"
  5. #include "y2k_time.h"
  6. #define DEFAULT_SLEEP_TIME (60 * 1000)
  7. #define DEFAULT_ADVERT_TYPE 'A'
  8. bool IS_DEBUG = false;
  9. int scanTime = 600;
  10. int removeOldTime = 1800;
  11. #ifndef MAX_LOCAL_MEDIAS
  12. #define MAX_LOCAL_MEDIAS 128
  13. #endif // !MAX_LOCAL_MEDIAS
  14. #ifndef RVC_MIN_LOCALPLAYER_TIME
  15. #define RVC_MIN_LOCALPLAYER_TIME 6
  16. #endif
  17. //DWORD WINAPI clearOutdata(LPVOID lpv)
  18. //{
  19. // CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)lpv;
  20. // Sleep(removeOldTime * 1000);
  21. // curEntity->m_mediaManage.clearOutdataResource();
  22. // return 0;
  23. //}
  24. #ifdef RVC_OS_WIN
  25. DWORD WINAPI qryMedia(LPVOID lpv)
  26. {
  27. CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)lpv;
  28. Sleep(3000);
  29. while (true)
  30. {
  31. curEntity->m_mediaManage.InitResourceListByLocal();
  32. //curEntity->StartVideo(0, 0, 0, 0, 0);
  33. Sleep(3600 * 1000);
  34. }
  35. return 0;
  36. }
  37. #else
  38. void* queryMedia(void* param)
  39. {
  40. CLocalMediaPlayEntity* curEntity = (CLocalMediaPlayEntity*)param;
  41. sleep(3);
  42. while (false == curEntity->GetScanExitFlag())
  43. {
  44. curEntity->m_mediaManage.InitResourceListByLocal();
  45. sleep(3600);
  46. }
  47. return 0;
  48. }
  49. void sig_handler(int signum)
  50. {
  51. if (SIGTERM == signum){
  52. int ipid = getpid();
  53. kill(ipid, SIGKILL);
  54. }
  55. }
  56. #endif // RVC_OS_WIN
  57. CLocalMediaPlayEntity::CLocalMediaPlayEntity()
  58. : m_id_seq(0), m_connection(NULL)
  59. {
  60. //stopForDebug();
  61. m_defaultVolum = 50;
  62. #ifdef RVC_OS_WIN
  63. m_scanThread=NULL;
  64. m_pAudioPlayer = NULL;
  65. memset(m_pVideoPlayer, 0, sizeof(m_pVideoPlayer));
  66. memset(m_pImagePlayer, 0, sizeof(m_pImagePlayer));
  67. #else
  68. //add by clp 20201103
  69. m_scanThreadId = 0;
  70. m_pMediaAudioPlayer = NULL;
  71. memset(m_pMediaPlayer, 0, sizeof(m_pMediaPlayer));
  72. memset(m_pPicturePlayer, 0, sizeof(m_pPicturePlayer));
  73. m_uMediaPlayThreadId = 0;
  74. m_badplayflag = true;
  75. m_bgetflag = false;
  76. m_bgetico = false;
  77. #endif // RVC_OS_WIN
  78. ZeroMemory(&m_mediaParam, sizeof(MediaPlayParam));
  79. }
  80. CLocalMediaPlayEntity::~CLocalMediaPlayEntity()
  81. {
  82. SecureClientRelease();
  83. DWORD exitCode = 0;
  84. #ifdef RVC_OS_WIN
  85. if (m_scanThread)
  86. {
  87. TerminateThread(m_scanThread, exitCode);
  88. m_scanThread = NULL;
  89. }
  90. #else
  91. m_scanexitflag = true;
  92. if (0 == pthread_join(m_scanThreadId, NULL)) {
  93. Dbg("pthread join scanThreadId success.");
  94. }
  95. m_bgetflag = false;
  96. m_bgetico = false;
  97. #endif // RVC_OS_WIN
  98. }
  99. void CLocalMediaPlayEntity::SecureClientRelease()
  100. {
  101. if (NULL != m_connection)
  102. {
  103. if (m_connection->IsConnectionOK())
  104. m_connection->Close();
  105. m_connection->DecRefCount();
  106. m_connection = NULL;
  107. }
  108. }
  109. bool CLocalMediaPlayEntity::SecureClientConnect()
  110. {
  111. if (NULL != m_connection && m_connection->IsConnectionOK())
  112. return true;
  113. SecureClientRelease();
  114. m_connection = new CAdvertSyncConnection(this);
  115. return m_connection->ConnectFromCentralSetting();
  116. }
  117. void CLocalMediaPlayEntity::setMediaPath()
  118. {
  119. CSimpleStringA downloadPath, mediaPath;
  120. GetFunction()->GetPath("Download", downloadPath);
  121. GetFunction()->GetPath("Ad", mediaPath);
  122. m_mediaManage.setDefaultDownloadPath(downloadPath.GetData());
  123. m_mediaManage.setDefaultAddvertPath(mediaPath.GetData());
  124. }
  125. CServerSessionBase* CLocalMediaPlayEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass)
  126. {
  127. LOG_FUNCTION();
  128. LOG_TRACE("%s connected class = %s!", pszRemoteEntityName, pszClass);
  129. return new CLocalMediaPlaySession(this, m_id_seq++);
  130. }
  131. void CLocalMediaPlayEntity::loadDefaultMedia()
  132. {
  133. #ifdef RVC_OS_WIN
  134. m_defaultImg.clear();
  135. m_defaultAudio.clear();
  136. m_defaultVideo.clear();
  137. #else
  138. m_defaultPic.clear();
  139. m_Audios.clear();
  140. m_Videos.clear();
  141. #endif // RVC_OS_WIN
  142. // 读取LocalMediaPlay.ini中的配置
  143. CSimpleStringA strRootPath;
  144. ErrorCodeEnum Error = Error_Succeed;
  145. Error = GetFunction()->GetPath("ADData", strRootPath);
  146. if (Error != Error_Succeed) {
  147. Dbg("Fail to get local media root path!");
  148. return;
  149. }
  150. CSmartPointer<IConfigInfo> spConfig;
  151. CAutoArray<CSimpleStringA> sectionList;
  152. if (Error_Succeed == GetFunction()->OpenConfig(Config_Software, spConfig) && Error_Succeed == spConfig->ReadAllSections(sectionList))
  153. {
  154. for (int i = 0; i < sectionList.GetCount(); i++)
  155. {
  156. CSmartPointer<IConfigInfo> tempConfig = spConfig;
  157. SpIniMappingTable table;
  158. CSimpleStringA strFileNames;
  159. if (sectionList[i].IsStartWith("General"))
  160. {
  161. table.AddEntryBoolean(sectionList[i].GetData(), "runDebug", IS_DEBUG, false);
  162. table.AddEntryInt(sectionList[i].GetData(), "scanTime", scanTime, 600);
  163. table.AddEntryInt(sectionList[i].GetData(), "removeOldTime", removeOldTime, 1800);
  164. if (Error_Succeed == table.Load(tempConfig))
  165. Dbg("runDebug:%s, scanTime:%d", IS_DEBUG ? "true" : "false", scanTime);
  166. else
  167. Dbg("General load fail!");
  168. if (removeOldTime < 1800)
  169. {
  170. removeOldTime = 1800;
  171. }
  172. }
  173. else if (sectionList[i].IsStartWith("Image"))
  174. {
  175. // ����ͼƬ����
  176. #ifdef RVC_OS_WIN
  177. CImgPlayConfig curImg;
  178. ZeroMemory(&curImg, sizeof(CImgPlayConfig));
  179. #else
  180. CPicPlayConfig curImg;
  181. ZeroMemory(&curImg, sizeof(CPicPlayConfig));
  182. #endif // RVC_OS_WIN
  183. CSimpleStringA imgPath = strRootPath + SPLIT_SLASH_STR +"Image" + SPLIT_SLASH_STR;
  184. strncpy(curImg.strRootPath, (LPCSTR)imgPath, sizeof(curImg.strRootPath));
  185. Dbg("curSection: %s, config.strRootPath: %s", sectionList[i].GetData(), curImg.strRootPath);
  186. table.AddEntryBoolean(sectionList[i].GetData(), "FullScreen", curImg.bFullScreen, false);
  187. table.AddEntryBoolean(sectionList[i].GetData(), "PrimMonitor", curImg.bPrimMonitor, false);
  188. table.AddEntryInt(sectionList[i].GetData(), "PlayCount", curImg.nPlayCnt, 0);
  189. table.AddEntryInt(sectionList[i].GetData(), "PlayInterval", curImg.nPlayInterval, 0);
  190. table.AddEntryString(sectionList[i].GetData(), "ImageNames", strFileNames, "");
  191. if (Error_Succeed == table.Load(tempConfig))
  192. Dbg("Image Succeed to LoadConfig!");
  193. else
  194. Dbg("Image Fail to LoadConfig!");
  195. char *Tmp = new char[strFileNames.GetLength() + 1];
  196. strcpy(Tmp, (LPCSTR)strFileNames);
  197. char *Result[MAX_FILECOUNT] = { NULL };
  198. CStringSplit(Tmp, Result, "|");
  199. int FileCount = 0;
  200. char** pStr = Result;
  201. while (*pStr != NULL)
  202. {
  203. ++pStr; ++FileCount;
  204. }
  205. curImg.nFileCnt = FileCount;
  206. Dbg("Image config.nFileCnt = %d!", FileCount);
  207. for (int i = 0; i != FileCount; ++i)
  208. {
  209. strcpy(curImg.strFileNames[i], Result[i]);
  210. }
  211. delete[] Tmp; Tmp = NULL;
  212. #ifdef RVC_OS_WIN
  213. m_defaultImg.push_back(curImg);
  214. #else
  215. m_defaultPic.push_back(curImg);
  216. #endif // RVC_OS_WIN
  217. }
  218. else if (sectionList[i].IsStartWith("Video"))
  219. {
  220. #ifdef RVC_OS_WIN
  221. CWmpPlayConfig curVideo;
  222. ZeroMemory(&curVideo, sizeof(CWmpPlayConfig));
  223. #else
  224. CMediaPlayConfig curVideo = {0};
  225. #endif // RVC_OS_WIN
  226. CSimpleStringA videoPath = strRootPath + SPLIT_SLASH_STR +"Video" + SPLIT_SLASH_STR;
  227. strcpy(curVideo.strRootPath, (LPCSTR)videoPath);
  228. Dbg("config.strRootPath: %s", curVideo.strRootPath);
  229. CSimpleStringA strRunTime_S, strRunTime_E;
  230. table.AddEntryString("General", "VideoRunTime_Start", strRunTime_S, "");
  231. table.AddEntryString("General", "VideoRunTime_End", strRunTime_E, "");
  232. table.AddEntryBoolean(sectionList[i].GetData(), "FullScreen", curVideo.bFullScreen, false);
  233. table.AddEntryBoolean(sectionList[i].GetData(), "PrimMonitor", curVideo.bPrimMonitor, false);
  234. table.AddEntryBoolean(sectionList[i].GetData(), "SimpleMode", curVideo.bSimpleMode, true);
  235. table.AddEntryInt(sectionList[i].GetData(), "PlayCount", curVideo.nPlayCnt, 0);
  236. table.AddEntryInt(sectionList[i].GetData(), "PlayInterval", curVideo.nPlayInterval, 0);
  237. table.AddEntryString(sectionList[i].GetData(), "VideoNames", strFileNames, "");
  238. if (Error_Succeed == table.Load(tempConfig))
  239. Dbg("Succeed to LoadWmpConfig!");
  240. else
  241. Dbg("Fail to LoadWmpConfig!");
  242. curVideo.nVolume = 50;
  243. Error = GetFunction()->OpenConfig(Config_Run, tempConfig);
  244. if (Error == Error_Succeed)
  245. {
  246. Error = tempConfig->ReadConfigValueInt("LocalVideo", "Volume", curVideo.nVolume);
  247. if (Error != Error_Succeed || curVideo.nVolume < 0 || curVideo.nVolume > 100)
  248. curVideo.nVolume = 50;
  249. m_defaultVolum = curVideo.nVolume;
  250. }
  251. strcpy(curVideo.strVideoRunTime_S, (LPCSTR)strRunTime_S);
  252. Dbg("config.strVideoRunTime_S: %s", (LPCSTR)strRunTime_S);
  253. strcpy(curVideo.strVideoRunTime_E, (LPCSTR)strRunTime_E);
  254. Dbg("config.strVideoRunTime_E: %s", (LPCSTR)strRunTime_E);
  255. char *Tmp = new char[strFileNames.GetLength() + 1];
  256. strcpy(Tmp, (LPCSTR)strFileNames);
  257. char *Result[MAX_FILECOUNT] = { NULL };
  258. CStringSplit(Tmp, Result, "|");
  259. int FileCount = 0;
  260. char** pStr = Result;
  261. while (*pStr != NULL)
  262. {
  263. ++pStr; ++FileCount;
  264. }
  265. curVideo.nFileCnt = FileCount;
  266. Dbg("Wmp config.strFileNames = %s!", (LPCSTR)strFileNames);
  267. Dbg("Wmp config.nFileCnt = %d!", FileCount);
  268. for (int i = 0; i != FileCount; ++i)
  269. strcpy(curVideo.strFileNames[i], Result[i]);
  270. delete[] Tmp; Tmp = NULL;
  271. #ifdef RVC_OS_WIN
  272. m_defaultVideo.push_back(curVideo);
  273. #else
  274. m_Videos.push_back(curVideo);
  275. #endif // RVC_OS_WIN
  276. }
  277. else if (sectionList[i].IsStartWith("Audio"))
  278. {
  279. #ifdef RVC_OS_WIN
  280. CWmpPlayConfig curAudio;
  281. ZeroMemory(&curAudio, sizeof(CWmpPlayConfig));
  282. #else
  283. CMediaPlayConfig curAudio = { 0 };
  284. #endif // RVC_OS_WIN
  285. CSimpleStringA audioPath = strRootPath + SPLIT_SLASH_STR +"Audio" + SPLIT_SLASH_STR;
  286. strcpy(curAudio.strRootPath, (LPCSTR)audioPath);
  287. Dbg("config.strRootPath: %s", curAudio.strRootPath);
  288. table.AddEntryInt(sectionList[i], "PlayCount", curAudio.nPlayCnt, 0);
  289. table.AddEntryInt(sectionList[i], "PlayInterval", curAudio.nPlayInterval, 0);
  290. auto audioRet = GetLocalAudioVolume();
  291. if (audioRet.first)
  292. curAudio.nVolume = audioRet.second;
  293. else
  294. curAudio.nVolume = m_defaultVolum;
  295. if (Error_Succeed == table.Load(tempConfig))
  296. Dbg("Succeed to LoadWmpConfig!");
  297. else
  298. Dbg("Fail to LoadWmpConfig!");
  299. #ifdef RVC_OS_WIN
  300. m_defaultAudio.push_back(curAudio);
  301. #else
  302. m_Audios.push_back(curAudio);
  303. #endif // RVC_OS_WIN
  304. }
  305. }
  306. }
  307. }
  308. #ifdef RVC_OS_WIN
  309. BOOL CLocalMediaPlayEntity::LoadPlayConfig(CWmpPlayConfig& config, int CfgInx)
  310. {
  311. if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0)
  312. {
  313. Dbg("Invalid CfgInx while LoadConfig!");
  314. return FALSE;
  315. }
  316. if (config.eMode == LOCALAUDIO && CfgInx < m_defaultAudio.size())
  317. {
  318. memcpy(&config, &(m_defaultAudio[CfgInx]), sizeof(CWmpPlayConfig));
  319. }
  320. else if (config.eMode == LOCALVIDEO && CfgInx < m_defaultVideo.size())
  321. {
  322. memcpy(&config, &(m_defaultVideo[CfgInx]), sizeof(CWmpPlayConfig));
  323. }
  324. else {
  325. return FALSE;
  326. }
  327. return TRUE;
  328. }
  329. BOOL CLocalMediaPlayEntity::LoadPlayConfig(CImgPlayConfig& config, int CfgInx)
  330. {
  331. if (CfgInx >= m_defaultImg.size() || CfgInx < 0)
  332. {
  333. Dbg("Invalid CfgInx while LoadConfig!");
  334. return FALSE;
  335. }
  336. memcpy(&config, &(m_defaultImg[CfgInx]), sizeof(CImgPlayConfig));
  337. return TRUE;
  338. }
  339. void CLocalMediaPlayEntity::WmpDebug(const char* fmt, ...)
  340. {
  341. va_list arg;
  342. va_start(arg, fmt);
  343. vDbg(fmt, arg);
  344. va_end(arg);
  345. }
  346. void CLocalMediaPlayEntity::ImgDebug(const char* fmt, ...)
  347. {
  348. va_list arg;
  349. va_start(arg, fmt);
  350. vDbg(fmt, arg);
  351. va_end(arg);
  352. }
  353. #else
  354. int CLocalMediaPlayEntity::LoadPlayConfig(CMediaPlayConfig& config, int CfgInx)
  355. {
  356. if (CfgInx >= MAX_PLAY_CHANNELS || CfgInx < 0)
  357. {
  358. Dbg("Invalid CfgInx while LoadConfig!");
  359. return -1;
  360. }
  361. if (config.eMode == LOCALAUDIO && CfgInx < m_Audios.size())
  362. {
  363. memcpy(&config, &(m_Audios[CfgInx]), sizeof(CMediaPlayConfig));
  364. }
  365. else if (config.eMode == LOCALVIDEO && CfgInx < m_Videos.size())
  366. {
  367. memcpy(&config, &(m_Videos[CfgInx]), sizeof(CMediaPlayConfig));
  368. }
  369. else {
  370. return -1;
  371. }
  372. return 0;
  373. }
  374. int CLocalMediaPlayEntity::LoadPlayConfig(CPicPlayConfig& config, int CfgInx)
  375. {
  376. if (CfgInx >= m_defaultPic.size() || CfgInx < 0)
  377. {
  378. Dbg("Invalid CfgInx while LoadConfig!");
  379. return -1;
  380. }
  381. memcpy(&config, &(m_defaultPic[CfgInx]), sizeof(CPicPlayConfig));
  382. return 0;
  383. }
  384. void CLocalMediaPlayEntity::Debug(media_loglevel log_level, const char* fmt, ...)
  385. {
  386. if (log_level >= MEDIA_LOG_DEBUG){
  387. va_list arg;
  388. va_start(arg, fmt);
  389. vDbg(fmt, arg);
  390. va_end(arg);
  391. }
  392. }
  393. void CLocalMediaPlayEntity::PicDebug(pic_loglevel log_level, const char* fmt, ...)
  394. {
  395. if (log_level >= PIC_LOG_ERROR){
  396. va_list arg;
  397. va_start(arg, fmt);
  398. vDbg(fmt, arg);
  399. va_end(arg);
  400. }
  401. }
  402. void CLocalMediaPlayEntity::AudioPlayFinished()
  403. {
  404. AudioPlayRet ret;
  405. ret.AudioNames = m_lastPlayAudio.c_str();
  406. ret.ret = true;
  407. Dbg("stop play audio %s success", ret.AudioNames.GetData());
  408. SpSendBroadcast(GetFunction(), eMsg_AudioPlayRet, eMsgSig_AudioPlayRet, ret);
  409. }
  410. int CLocalMediaPlayEntity::GetMediaPlayerIcoPath(char* strPath, size_t uLen)
  411. {
  412. return GetPlayerIcoPath(strPath, uLen);
  413. }
  414. int CLocalMediaPlayEntity::GetAudioOutDevName(char* strDev, size_t uLen)
  415. {
  416. int iRet = -1;
  417. int idatalen = m_strAudioOutDev.GetLength();
  418. if (uLen > idatalen && idatalen > 0) {
  419. memcpy(strDev, m_strAudioOutDev.GetData(), idatalen);
  420. iRet = 0;
  421. if (false == m_bgetflag) {
  422. m_bgetflag = true;
  423. Dbg("%s:%d audio Out Device Name is %s.", __FUNCTION__, __LINE__, strDev);
  424. }
  425. }
  426. return iRet;
  427. }
  428. int CLocalMediaPlayEntity::GetPicPlayerIcoPath(char* strPath, size_t uLen)
  429. {
  430. return GetPlayerIcoPath(strPath, uLen);
  431. }
  432. bool CLocalMediaPlayEntity::GetPlayFlag()
  433. {
  434. return m_badplayflag;
  435. }
  436. bool CLocalMediaPlayEntity::GetScanExitFlag()
  437. {
  438. return m_scanexitflag;
  439. }
  440. int CLocalMediaPlayEntity::GetPlayerIcoPath(char* strPath, size_t uLen)
  441. {
  442. int iRet = -1;
  443. if (NULL == strPath) {
  444. return iRet;
  445. }
  446. CSimpleStringA csBinPath;
  447. ErrorCodeEnum eErrPath = GetFunction()->GetPath("Bin", csBinPath);
  448. if (eErrPath != Error_Succeed) {
  449. Dbg("GetBasePath failed (%d).", eErrPath);
  450. return iRet;
  451. }
  452. CSimpleStringA szIcoName("rvc_media_player_64px.bmp");
  453. szIcoName = csBinPath + SPLIT_SLASH_STR + szIcoName;
  454. if (m_bgetico == false) {
  455. Dbg("media player ico full path is %s.", szIcoName.GetData());
  456. m_bgetico = true;
  457. }
  458. if (uLen > szIcoName.GetLength()) {
  459. memcpy(strPath, szIcoName.GetData(), szIcoName.GetLength());
  460. iRet = 0;
  461. m_bgetico = true;
  462. }
  463. return iRet;
  464. }
  465. int CLocalMediaPlayEntity::GetAudioOutDev()
  466. {
  467. int iRet = -1;
  468. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  469. CSmartPointer<IConfigInfo> spRootConfig;
  470. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spRootConfig);
  471. if (Error == Error_Succeed) {
  472. SpIniMappingTable table;
  473. table.AddEntryString("audio", "handfree_out_dev", m_strAudioOutDev, "$");
  474. Error = table.Load(spRootConfig);
  475. if (Error == Error_Succeed) {
  476. Dbg("Audio Out Device name is %s.", m_strAudioOutDev.GetData());
  477. iRet = 0;
  478. }
  479. }
  480. return iRet;
  481. }
  482. #endif // RVC_OS_WIN
  483. void CLocalMediaPlayEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  484. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  485. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName, const char *pszMessage)
  486. {
  487. if (dwUserCode == LOG_EVT_SELFCHECK_IEBROWSER_IDLE)
  488. {
  489. Dbg("IEBrowser to idle, stop all media!");
  490. StopAll();
  491. }
  492. }
  493. void CLocalMediaPlayEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
  494. {
  495. LOG_FUNCTION();
  496. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  497. pTransactionContext->SendAnswer(Error);
  498. }
  499. ErrorCodeEnum CLocalMediaPlayEntity::__OnStart(ErrorCodeEnum preOperationError)
  500. {
  501. #ifdef RVC_OS_WIN
  502. //toolkit_setenv("SDL_AUDIODRIVER", "winmm");
  503. #else
  504. GetAudioOutDev();
  505. #endif // RVC_OS_WIN
  506. for (int i = 0; i != MAX_PLAY_CHANNELS; ++i)
  507. {
  508. #ifdef RVC_OS_WIN
  509. m_pVideoPlayer[i] = new Clibwmpplayer(this);
  510. m_pImagePlayer[i] = new Clibimgplayer(this);
  511. #else
  512. mediaplayer_init();
  513. m_pMediaPlayer[i] = new Clibmediaplayer(this);
  514. m_pPicturePlayer[i] = new Clibpictureplayer(this);
  515. #endif
  516. }
  517. #ifdef RVC_OS_WIN
  518. m_pAudioPlayer = new Clibwmpplayer(this);
  519. #else
  520. m_pMediaAudioPlayer = new Clibmediaplayer(this);
  521. signal(SIGTERM, sig_handler);
  522. #endif // RVC_OS_WIN
  523. if (!IsRunConfigExist())
  524. {
  525. SetLocalVideoVolume(0, 50);
  526. SetLocalAudioVolume(50);
  527. }
  528. //setMediaPath();
  529. loadDefaultMedia();
  530. GetFunction()->SubscribeLog(m_SubIDIEIdle, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_SELFCHECK_IEBROWSER_IDLE, NULL, false);
  531. #ifdef RVC_OS_WIN
  532. m_scanThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& qryMedia, this, 0, NULL);
  533. if (NULL != m_scanThread)
  534. Dbg("create qryMeia thread success, %d.", m_scanThread);
  535. #else
  536. int err = pthread_create(&m_scanThreadId, NULL, queryMedia, this);
  537. if (0 == err){
  538. Dbg("create queryMedia thread success, %u.", m_scanThreadId);
  539. }
  540. else{
  541. Dbg("create queryMedia thread failed.");
  542. }
  543. #endif // RVC_OS_WIN
  544. return Error_Succeed;
  545. }
  546. void CLocalMediaPlayEntity::OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
  547. {
  548. LOG_FUNCTION();
  549. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  550. pTransactionContext->SendAnswer(Error);
  551. }
  552. ErrorCodeEnum CLocalMediaPlayEntity::__OnClose(ErrorCodeEnum preOperationError)
  553. {
  554. for (int i = 0; i != MAX_PLAY_CHANNELS; ++i)
  555. {
  556. #ifdef RVC_OS_WIN
  557. delete m_pVideoPlayer[i]; m_pVideoPlayer[i] = NULL;
  558. delete m_pImagePlayer[i]; m_pImagePlayer[i] = NULL;
  559. #else
  560. delete m_pMediaPlayer[i]; m_pMediaPlayer[i] = NULL;
  561. delete m_pPicturePlayer[i]; m_pPicturePlayer[i] = NULL;
  562. #endif // RVC_OS_WIN
  563. }
  564. #ifdef RVC_OS_WIN
  565. delete m_pAudioPlayer; m_pAudioPlayer = NULL;
  566. #else
  567. delete m_pMediaAudioPlayer; m_pMediaAudioPlayer = NULL;
  568. mediaplayer_term();
  569. #endif // RVC_OS_WIN
  570. GetFunction()->UnsubscribeLog(m_SubIDIEIdle);
  571. return Error_Succeed;
  572. }
  573. #ifdef RVC_OS_WIN
  574. DWORD WINAPI CheckAudioThread(LPVOID param)
  575. {
  576. CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param;
  577. Dbg("Begin CheckAudioThread");
  578. HANDLE playThread = NULL;
  579. if (!entity->m_pAudioPlayer->checkIsPlay(playThread))
  580. {
  581. Dbg("Create play audio Media Thread Failed!");
  582. return 0;
  583. }
  584. WaitForSingleObject(playThread, INFINITE);
  585. entity->m_pAudioPlayer->Close();
  586. AudioPlayRet ret;
  587. ret.AudioNames = entity->m_lastPlayAudio.c_str();
  588. ret.ret = true;
  589. Dbg("stop play audio %s success", ret.AudioNames);
  590. SpSendBroadcast(entity->GetFunction(), eMsg_AudioPlayRet, eMsgSig_AudioPlayRet, ret);
  591. return 0;
  592. }
  593. #else
  594. void* StartAudioPlayingThreadFunc(void* param)
  595. {
  596. CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param;
  597. Dbg("Enter StartAudioPlayingThreadFunc");
  598. const char* pAudioNames = entity->m_lastPlayAudio.c_str();
  599. Dbg("Begin StartAudio, and Audio Info is %s.", pAudioNames);
  600. if (0 == entity->m_pMediaAudioPlayer->PlayLocalAudio(pAudioNames)) {
  601. Dbg("Succeed to StartAudio, and Audio Info is %s.", pAudioNames);
  602. }
  603. else{
  604. Dbg("StartAudio failed, for %s is not exsit.", pAudioNames);
  605. }
  606. Dbg("Leave StartAudioPlayingThreadFunc");
  607. return 0;
  608. }
  609. #endif // RVC_OS_WIN
  610. #ifdef RVC_OS_WIN
  611. DWORD WINAPI MediaPlayThread(LPVOID param)
  612. {
  613. CLocalMediaPlayEntity *entity = (CLocalMediaPlayEntity*)param;
  614. while (TRUE)
  615. {
  616. vector<ResourceParse> curParse;
  617. entity->m_mediaManage.GetPlayListByLocal(curParse);
  618. if (0 == curParse.size())
  619. {
  620. HANDLE playThread = NULL;
  621. CWmpPlayConfig config;
  622. ZeroMemory(&config, sizeof(CWmpPlayConfig));
  623. memcpy(&config, &(entity->m_defaultVideo[entity->m_mediaParam.nCfgInx]), sizeof(CWmpPlayConfig));
  624. config.nPlayCnt = 1; //can be change
  625. config.eMode = LOCALVIDEO;
  626. config.nWndX = entity->m_mediaParam.nWndX;
  627. config.nWndY = entity->m_mediaParam.nWndY;
  628. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  629. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  630. if (IS_DEBUG)
  631. {
  632. config.bFullScreen = false;
  633. config.bPrimMonitor = true;
  634. }
  635. // add by ly@2018/07/30
  636. if (!entity->GetLocalVideoVolume(0, config.nVolume))
  637. {
  638. config.nVolume = entity->m_defaultVolum;
  639. }
  640. Dbg("config.nVolume=%d while play local video.", config.nVolume);
  641. // �жϵ�ǰʱ���Ƿ���������
  642. SYSTEMTIME st;
  643. GetLocalTime(&st);
  644. TCHAR strNow[TIME_LEN];
  645. sprintf(strNow, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond);
  646. if (strcmp(strNow, config.strVideoRunTime_S) < 0 || strcmp(strNow, config.strVideoRunTime_E) >= 0)
  647. {
  648. Sleep(10000);
  649. continue;
  650. }
  651. entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config);
  652. if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread))
  653. {
  654. Dbg("Create play Video Media Thread Failed!");
  655. return 0;
  656. }
  657. WaitForSingleObject(playThread, INFINITE);
  658. if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop())
  659. {//ֹͣ����
  660. Dbg("stop play video");
  661. return 0;
  662. }
  663. else
  664. entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close();
  665. }
  666. for (vector<ResourceParse>::iterator i = curParse.begin(); i != curParse.end(); i++)
  667. {
  668. HANDLE playThread = NULL;
  669. Dbg("begin play extend %c:%s, %s", i->type, i->resourcePath.c_str(), i->videoNames.c_str());
  670. if ('V' == i->type)
  671. {//video
  672. CWmpPlayConfig config;
  673. ZeroMemory(&config, sizeof(CWmpPlayConfig));
  674. config.bFullScreen = IS_DEBUG ? false : i->fullScreen;
  675. config.bPrimMonitor = IS_DEBUG ? true : i->primMonitor;
  676. config.bSimpleMode = i->simpleMode;
  677. config.eMode = LOCALVIDEO;
  678. config.nFileCnt = 1;
  679. config.nPlayCnt = 1;
  680. config.nPlayInterval = i->playInterval;
  681. // add by ly@2018/07/30
  682. //config.nVolume = IS_DEBUG ? 0 : entity->m_defaultVolum;
  683. if (!entity->GetLocalVideoVolume(0, config.nVolume))
  684. {
  685. config.nVolume = entity->m_defaultVolum;
  686. }
  687. Dbg("config.nVolume=%d while play local video.", config.nVolume);
  688. config.nWndX = entity->m_mediaParam.nWndX;
  689. config.nWndY = entity->m_mediaParam.nWndY;
  690. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  691. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  692. strncpy_s(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath));
  693. strncpy_s(config.strFileNames[0], i->videoNames.c_str(), 256);
  694. strncpy_s(config.strVideoRunTime_S, "09:00:00", sizeof(config.strVideoRunTime_S));
  695. strncpy_s(config.strVideoRunTime_E, "17:30:00", sizeof(config.strVideoRunTime_E));
  696. entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config);
  697. if (!entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread))
  698. {
  699. Dbg("Create play Video Media Thread Failed!");
  700. return 0;
  701. }
  702. WaitForSingleObject(playThread, INFINITE);
  703. if (entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->checkIsStop())
  704. {//ֹͣ����
  705. Dbg("stop play video");
  706. return 0;
  707. }
  708. else
  709. entity->m_pVideoPlayer[entity->m_mediaParam.nCfgInx]->Close();
  710. }
  711. else if ('P' == i->type)
  712. {//play Image
  713. CImgPlayConfig config;
  714. ZeroMemory(&config, sizeof(CImgPlayConfig));
  715. config.bFullScreen = IS_DEBUG ? false : i->fullScreen;
  716. config.bPrimMonitor = IS_DEBUG ? true : i->primMonitor;
  717. config.nFileCnt = 1;
  718. config.nPlayCnt = 1;
  719. config.nPlayInterval = i->playInterval;
  720. config.nWndX = entity->m_mediaParam.nWndX;
  721. config.nWndY = entity->m_mediaParam.nWndY;
  722. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  723. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  724. strncpy_s(config.strRootPath, i->resourcePath.c_str(), sizeof(config.strRootPath));
  725. strncpy_s(config.strFileNames[0], i->videoNames.c_str(), 256);
  726. entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config);
  727. if (!entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(playThread))
  728. {
  729. Dbg("Create play Image Media Thread Failed!");
  730. return 0;
  731. }
  732. WaitForSingleObject(playThread, i->playInterval);
  733. if (entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop())
  734. {//ֹͣ����
  735. Dbg("stop play Image");
  736. return 0;
  737. }
  738. else
  739. entity->m_pImagePlayer[entity->m_mediaParam.nCfgInx]->Close();
  740. }
  741. }
  742. }
  743. return 0;
  744. }
  745. #else
  746. void* StartMediaPlayFunc(void* param)
  747. {
  748. int iRet = -1;
  749. if (NULL == param){
  750. return (void*)&iRet;
  751. }
  752. Dbg("Enter Media Play Function.");
  753. CLocalMediaPlayEntity* entity = (CLocalMediaPlayEntity*)param;
  754. while (entity->GetPlayFlag())
  755. {
  756. rvcResourceParse_t ResourceList[MAX_LOCAL_MEDIAS] = { 0 };
  757. size_t uCount = entity->m_mediaManage.GetPlayListByLocal(ResourceList, MAX_LOCAL_MEDIAS);
  758. unsigned int ustart_time = y2k_time_now();
  759. if (0 == uCount){
  760. int64_t playThreadId = 0;
  761. CMediaPlayConfig config = {0};
  762. memcpy(&config, &(entity->m_Videos[entity->m_mediaParam.nCfgInx]), sizeof(CMediaPlayConfig));
  763. config.nPlayCnt = 1; //can be change
  764. config.eMode = LOCALVIDEO;
  765. config.nWndX = entity->m_mediaParam.nWndX;
  766. config.nWndY = entity->m_mediaParam.nWndY;
  767. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  768. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  769. if (IS_DEBUG){
  770. config.bFullScreen = false;
  771. config.bPrimMonitor = true;
  772. }
  773. if (!entity->GetLocalVideoVolume(0, config.nVolume)){
  774. config.nVolume = entity->m_defaultVolum;
  775. }
  776. struct tm* ptm = NULL;
  777. time_t t = time(NULL);
  778. ptm = localtime(&t);
  779. TCHAR strNow[TIME_LEN] = {0};
  780. sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  781. if (strcmp(strNow, config.strVideoRunTime_S) < 0 || strcmp(strNow, config.strVideoRunTime_E) >= 0){
  782. Sleep(10000);
  783. continue;
  784. }
  785. if (-1 == entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config)){
  786. Sleep(30*DEFAULT_SLEEP_TIME);
  787. }
  788. Sleep(800);
  789. }
  790. for (int i = 0; i < uCount && i < MAX_LOCAL_MEDIAS && entity->GetPlayFlag(); i++)
  791. {
  792. //Dbg("ResourceList[%d] address is 0x%0x", i, ResourceList[i]);
  793. int64_t playThreadId = 0;
  794. rvcResourceParse_t item = ResourceList[i];
  795. //Dbg("begin play extend %c:%s, %s", item.type, item.strResourcePath, item.strvideoNames);
  796. if ('V' == item.type)
  797. {//video
  798. CMediaPlayConfig config = {0};
  799. config.bFullScreen = IS_DEBUG ? false : item.fullScreen;
  800. config.bPrimMonitor = IS_DEBUG ? true : item.primMonitor;
  801. config.bSimpleMode = item.simpleMode;
  802. config.eMode = LOCALVIDEO;
  803. config.nFileCnt = 1;
  804. config.nPlayCnt = 1;
  805. config.nPlayInterval = item.playInterval;
  806. // add by ly@2018/07/30
  807. //config.nVolume = IS_DEBUG ? 0 : entity->m_defaultVolum;
  808. if (!entity->GetLocalVideoVolume(0, config.nVolume)){
  809. config.nVolume = entity->m_defaultVolum;
  810. }
  811. //Dbg("config.nVolume=%d while play local video.", config.nVolume);
  812. config.nWndX = entity->m_mediaParam.nWndX;
  813. config.nWndY = entity->m_mediaParam.nWndY;
  814. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  815. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  816. strncpy(config.strRootPath, item.strResourcePath, strlen(item.strResourcePath));
  817. strncpy(config.strFileNames[0], item.strvideoNames, strlen(item.strvideoNames));
  818. strncpy(config.strVideoRunTime_S, "09:00:00", sizeof(config.strVideoRunTime_S));
  819. strncpy(config.strVideoRunTime_E, "17:30:00", sizeof(config.strVideoRunTime_E));
  820. int iPlayRet = entity->m_pMediaPlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config);
  821. if (-2 == iPlayRet){
  822. Sleep(DEFAULT_SLEEP_TIME);
  823. continue;
  824. }
  825. if (-3 == iPlayRet) {
  826. Sleep(DEFAULT_SLEEP_TIME/6);
  827. }
  828. Sleep(800);
  829. }
  830. else if ('P' == item.type)
  831. {//play Image
  832. Dbg("Begin new Image play, entity->m_mediaParam.nCfgInx = %d.", entity->m_mediaParam.nCfgInx);
  833. CPicPlayConfig config = {0};
  834. config.bFullScreen = IS_DEBUG ? false : item.fullScreen;
  835. config.bPrimMonitor = IS_DEBUG ? true : item.primMonitor;
  836. config.nFileCnt = 1;
  837. config.nPlayCnt = 1;
  838. config.nPlayInterval = item.playInterval;
  839. config.nWndX = entity->m_mediaParam.nWndX;
  840. config.nWndY = entity->m_mediaParam.nWndY;
  841. config.nWndWidth = entity->m_mediaParam.nWndWidth;
  842. config.nWndHeight = entity->m_mediaParam.nWndHeight;
  843. strncpy(config.strRootPath, item.strResourcePath, strlen(item.strResourcePath));
  844. strncpy(config.strFileNames[0], item.strvideoNames, strlen(item.strvideoNames));
  845. if (entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsStop()){
  846. entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->PlayMedia(config);
  847. if (!entity->m_pPicturePlayer[entity->m_mediaParam.nCfgInx]->checkIsPlay(&playThreadId)) {
  848. Dbg("Create play Image Media Thread Failed!");
  849. iRet = -1;
  850. return (void*)& iRet;
  851. }
  852. else {
  853. Dbg("Image Media Player Thread id is %u.", playThreadId);
  854. }
  855. if (0 == pthread_join(playThreadId, NULL)) {
  856. Dbg("pthread join thread id %u success.", playThreadId);
  857. iRet = 0;
  858. }
  859. else {
  860. Dbg("pthread join thread id %u failed for %s.", playThreadId, strerror(errno));
  861. iRet = -1;
  862. }
  863. }
  864. Sleep(100);
  865. }
  866. }
  867. unsigned int uend_time = y2k_time_now();
  868. //Dbg("uend_time - ustart_time = %u.", uend_time - ustart_time);
  869. if (uend_time - ustart_time < RVC_MIN_LOCALPLAYER_TIME) {
  870. Sleep(30*DEFAULT_SLEEP_TIME);
  871. }
  872. }
  873. Dbg("Leave Media Play Function.");
  874. return (void*)&iRet;
  875. }
  876. #endif // RVC_OS_WIN
  877. void CLocalMediaPlayEntity::StartVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  878. {
  879. if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0)
  880. {
  881. Dbg("Invalid CfgInx while StartVideo!");
  882. return;
  883. }
  884. m_mediaParam.nCfgInx = nCfgInx;
  885. m_mediaParam.nWndX = nWndX;
  886. m_mediaParam.nWndY = nWndY;
  887. m_mediaParam.nWndWidth = nWndWidth;
  888. m_mediaParam.nWndHeight = nWndHeight;
  889. #ifdef RVC_OS_WIN // RVC_OS_WIN
  890. if (NULL != m_playThread)
  891. {
  892. WaitForSingleObject(m_playThread, 5000);
  893. m_playThread = NULL;
  894. }
  895. if (NULL == m_playThread)
  896. m_playThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& MediaPlayThread, this, 0, NULL);
  897. #else
  898. m_badplayflag = true;
  899. int err = pthread_create(&m_uMediaPlayThreadId, NULL, StartMediaPlayFunc, this);
  900. if (0 == err) {
  901. Dbg("create media play thread[%u] success.", m_uMediaPlayThreadId);
  902. }
  903. else {
  904. Dbg("create media play thread failed.");
  905. }
  906. #endif
  907. }
  908. void CLocalMediaPlayEntity::StartAudio(const char *pAudioNames)
  909. {
  910. #ifdef RVC_OS_WIN
  911. static HANDLE audioPlayThread = NULL;
  912. if (audioPlayThread)//�ر����ڲ��ŵ���Ƶ
  913. {
  914. DWORD exitCode = 0;
  915. if (WAIT_TIMEOUT == WaitForSingleObject(audioPlayThread, 50))
  916. {
  917. TerminateThread(audioPlayThread, exitCode);
  918. m_pAudioPlayer->Close();
  919. }
  920. audioPlayThread = NULL;
  921. }
  922. Dbg("Begin StartAudio, and Audio Info is %s.", pAudioNames);
  923. m_pAudioPlayer->PlayLocalAudio(pAudioNames);
  924. Dbg("Succeed to StartAudio, and Audio Info is %s.", pAudioNames);
  925. auto audioRet = GetLocalAudioVolume();
  926. m_pAudioPlayer->SetVolume(audioRet.second);
  927. Dbg("Succeed to set audio volume %d!", audioRet.second);
  928. m_lastPlayAudio = pAudioNames;
  929. audioPlayThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)& CheckAudioThread, this, 0, NULL);
  930. #else
  931. if (m_pMediaAudioPlayer->checkIsPlay()) {
  932. Dbg("current is playing, close it.");
  933. if (0 != m_uMediaPlayThreadId){
  934. m_pMediaAudioPlayer->Close();
  935. m_uMediaPlayThreadId = 0;
  936. Sleep(800);
  937. }
  938. }
  939. m_lastPlayAudio = pAudioNames;
  940. int err = pthread_create(&m_uMediaPlayThreadId, NULL, StartAudioPlayingThreadFunc, this);
  941. if (0 == err) {
  942. Dbg("create audio play thread[%u] success.", m_uMediaPlayThreadId);
  943. }
  944. else {
  945. Dbg("create audio play thread failed.");
  946. }
  947. auto audioRet = GetLocalAudioVolume();
  948. m_pMediaAudioPlayer->SetVolume(audioRet.second);
  949. Dbg("Succeed to set audio volume %d!", audioRet.second);
  950. #endif // RVC_OS_WIN
  951. }
  952. void CLocalMediaPlayEntity::StartImage(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  953. {
  954. if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0)
  955. {
  956. Dbg("Invalid CfgInx while StartImage!");
  957. return;
  958. }
  959. #ifdef RVC_OS_WIN
  960. m_pImagePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  961. #else
  962. m_pPicturePlayer[nCfgInx]->Play(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  963. #endif // RVC_OS_WIN
  964. Dbg("Succeed to StartImage!");
  965. }
  966. void CLocalMediaPlayEntity::StopVideo(int nCfgInx)
  967. {
  968. if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0)
  969. {
  970. Dbg("Invalid CfgInx while StopVideo!");
  971. return;
  972. }
  973. #ifdef RVC_OS_WIN
  974. m_pVideoPlayer[nCfgInx]->Close();
  975. m_pImagePlayer[nCfgInx]->Close();
  976. if (m_playThread)
  977. {
  978. TerminateThread(m_playThread, -1);
  979. m_playThread = NULL;
  980. }
  981. #else
  982. m_pMediaPlayer[nCfgInx]->Close();
  983. m_pPicturePlayer[nCfgInx]->Close();
  984. m_badplayflag = false;
  985. #endif // RVC_OS_WIN
  986. Dbg("Succeed to StopVideo!");
  987. }
  988. void CLocalMediaPlayEntity::StopAudio()
  989. {
  990. #ifdef RVC_OS_WIN
  991. m_pAudioPlayer->Close();
  992. #else
  993. m_pMediaAudioPlayer->Close();
  994. #endif
  995. Dbg("Succeed to StopAudio!");
  996. }
  997. void CLocalMediaPlayEntity::StopImage(int nCfgInx)
  998. {
  999. if (nCfgInx >= MAX_PLAY_CHANNELS || nCfgInx < 0)
  1000. {
  1001. Dbg("Invalid CfgInx while StopImage!");
  1002. return;
  1003. }
  1004. #ifdef RVC_OS_WIN
  1005. m_pImagePlayer[nCfgInx]->Close();
  1006. #else
  1007. m_pPicturePlayer[nCfgInx]->Close();
  1008. #endif // RVC_OS_WIN
  1009. Dbg("Succeed to StopImage!");
  1010. }
  1011. void CLocalMediaPlayEntity::StopAll()
  1012. {
  1013. StopAudio(); // �ر���Ƶ
  1014. for (int i = 0; i < MAX_PLAY_CHANNELS; ++i)
  1015. {
  1016. StopVideo(i); // �ر���Ƶ
  1017. StopImage(i); // �ر�ͼ��
  1018. }
  1019. }
  1020. bool CLocalMediaPlayEntity::GetLocalVideoVolume(int nCfgInx, int &nVolume)
  1021. {
  1022. //Dbg("get local video volume req.");
  1023. CSmartPointer<IConfigInfo> spConfig;
  1024. ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig);
  1025. if (Error == Error_Succeed)
  1026. {
  1027. Error = spConfig->ReadConfigValueInt("LocalVideo", "Volume", nVolume);
  1028. if (Error == Error_Succeed && nVolume >= 0 && nVolume <= 100)
  1029. {
  1030. //Dbg("succeed to get local video volume with nCfgInx:%d, nVolume:%d", nCfgInx, nVolume);
  1031. return true;
  1032. }
  1033. }
  1034. Dbg("fail to get local video volume!");
  1035. return false;
  1036. }
  1037. std::pair<bool, int> CLocalMediaPlayEntity::GetLocalAudioVolume()
  1038. {
  1039. Dbg("get local audio volume req.");
  1040. CSmartPointer<IConfigInfo> spConfig;
  1041. int nVolume = 0;
  1042. ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig);
  1043. if (Error == Error_Succeed)
  1044. {
  1045. Error = spConfig->ReadConfigValueInt("LocalAudio", "Volume", nVolume);
  1046. //if (nVolume == 0)
  1047. //{
  1048. // nVolume = 50;
  1049. // spConfig->WriteConfigValueInt("LocalAudio", "Volume", nVolume);
  1050. // Dbg("read nVolume 0, set default 50");
  1051. //}
  1052. if (Error == Error_Succeed && nVolume >= 0 && nVolume <= 100)
  1053. {
  1054. Dbg("succeed to get local audio volume with nVolume:%d", nVolume);
  1055. return std::make_pair(true, nVolume);
  1056. }
  1057. }
  1058. Dbg("fail to get local audio volume!");
  1059. return std::make_pair(false, nVolume);
  1060. }
  1061. bool CLocalMediaPlayEntity::SetLocalAudioVolume(int nVolume)
  1062. {
  1063. Dbg("set local video audio req.");
  1064. if (nVolume < 0 || nVolume > 100)
  1065. {
  1066. return false;
  1067. }
  1068. CSmartPointer<IConfigInfo> spConfig;
  1069. ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig);
  1070. if (Error == Error_Succeed)
  1071. {
  1072. Error = spConfig->WriteConfigValueInt("LocalAudio", "Volume", nVolume);
  1073. if (Error == Error_Succeed)
  1074. {
  1075. Dbg("succeed to set local audio volume with nVolume:%d!", nVolume);
  1076. #ifdef RVC_OS_WIN
  1077. m_pAudioPlayer->SetVolume(nVolume);
  1078. #else
  1079. m_pMediaAudioPlayer->SetVolume(nVolume);
  1080. #endif // RVC_OS_WIN
  1081. return true;
  1082. }
  1083. }
  1084. Dbg("fail to set local video volume!");
  1085. return false;
  1086. }
  1087. bool CLocalMediaPlayEntity::SetLocalVideoVolume(int nCfgInx, int nVolume)
  1088. {
  1089. Dbg("set local video volume req.");
  1090. if (nVolume < 0 || nVolume > 100)
  1091. {
  1092. return false;
  1093. }
  1094. CSmartPointer<IConfigInfo> spConfig;
  1095. ErrorCodeEnum Error = GetFunction()->OpenConfig(Config_Run, spConfig);
  1096. if (Error == Error_Succeed)
  1097. {
  1098. Error = spConfig->WriteConfigValueInt("LocalVideo", "Volume", nVolume);
  1099. if (Error == Error_Succeed)
  1100. {
  1101. Dbg("succeed to set local video volume with nCfgInx:%d, nVolume:%d!", nCfgInx, nVolume);
  1102. for (int i = 0; i < MAX_PLAY_CHANNELS; ++i)
  1103. {
  1104. #ifdef RVC_OS_WIN
  1105. m_pVideoPlayer[i]->SetVolume(nVolume);
  1106. #else
  1107. m_pMediaPlayer[i]->SetVolume(nVolume);
  1108. #endif
  1109. }
  1110. return true;
  1111. }
  1112. }
  1113. Dbg("fail to set local video volume!");
  1114. return false;
  1115. }
  1116. bool CLocalMediaPlayEntity::IsRunConfigExist()
  1117. {
  1118. CSimpleStringA strPath;
  1119. ErrorCodeEnum eErr;
  1120. if ((eErr = GetFunction()->GetPath("RunInfo", strPath)) != Error_Succeed)
  1121. {
  1122. Dbg("get runinfo path failed(%d)", eErr);
  1123. return false;
  1124. }
  1125. #ifdef RVC_OS_WIN
  1126. CSimpleStringA strRuninfoFile;
  1127. strRuninfoFile = CSimpleStringA::Format("%s\\runcfg\\%s.ini", (LPCTSTR)strPath, GetEntityName());
  1128. WIN32_FIND_DATA findData;
  1129. if (FindFirstFileA((LPCTSTR)strRuninfoFile, &findData) != INVALID_HANDLE_VALUE) return true;
  1130. return false;
  1131. #else
  1132. CSimpleStringA strRuninfoFile;
  1133. strRuninfoFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "%s.ini", (LPCTSTR)strPath, GetEntityName());
  1134. if (ExistsFileA(strRuninfoFile)) {
  1135. return true;
  1136. }
  1137. else{
  1138. return false;
  1139. }
  1140. #endif // RVC_OS_WIN
  1141. }
  1142. void CLocalMediaPlayEntity::OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
  1143. {
  1144. if (Test_ShakeHand == eTestType)
  1145. {
  1146. pTransactionContext->SendAnswer(Error_Succeed);
  1147. }
  1148. }
  1149. void CLocalMediaPlayEntity::CStringSplit(char *str, char **result, const char *del)
  1150. {
  1151. char *p = strtok(str, del);
  1152. while (p != NULL)
  1153. {
  1154. *result++ = p;
  1155. p = strtok(NULL, del);
  1156. }
  1157. }
  1158. void CLocalMediaPlaySession::Handle_StartPlayVideo(SpReqAnsContext<PlayService_StartPlayVideo_Req, PlayService_StartPlayVideo_Ans>::Pointer ctx)
  1159. {
  1160. 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);
  1161. CSimpleStringA UpdateState = "";
  1162. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState);
  1163. if (Error == Error_Succeed && "1" == UpdateState)
  1164. {
  1165. Dbg("detect current is upgrade, do not play the media");
  1166. ctx->Answer(Error_Stoped);
  1167. m_pEntity->StopAll();
  1168. return;
  1169. }
  1170. m_pEntity->StartVideo(ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight);
  1171. ctx->Answer(Error_Succeed);
  1172. }
  1173. void CLocalMediaPlaySession::Handle_StartPlayAudio(SpReqAnsContext<PlayService_StartPlayAudio_Req, PlayService_StartPlayAudio_Ans>::Pointer ctx)
  1174. {
  1175. CSimpleStringA AudioNames = CSimpleStringW2A(ctx->Req.AudioNames);
  1176. CSimpleStringA UpdateState = "";
  1177. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSysVar("UpdateState", UpdateState);
  1178. if (Error == Error_Succeed && "1" == UpdateState)
  1179. {
  1180. Dbg("detect current is upgrade, do not play the media");
  1181. ctx->Answer(Error_Stoped);
  1182. m_pEntity->StopAll();
  1183. return;
  1184. }
  1185. m_pEntity->StartAudio(AudioNames.GetData());
  1186. ctx->Answer(Error_Succeed);
  1187. }
  1188. void CLocalMediaPlaySession::Handle_StartPlayImage(SpReqAnsContext<PlayService_StartPlayImage_Req, PlayService_StartPlayImage_Ans>::Pointer ctx)
  1189. {
  1190. m_pEntity->StartImage(ctx->Req.CfgInx, ctx->Req.WndX, ctx->Req.WndY, ctx->Req.WndWidth, ctx->Req.WndHeight);
  1191. ctx->Answer(Error_Succeed);
  1192. }
  1193. void CLocalMediaPlaySession::Handle_StopPlayVideo(SpReqAnsContext<PlayService_StopPlayVideo_Req, PlayService_StopPlayVideo_Ans>::Pointer ctx)
  1194. {
  1195. Dbg("stop play Video, Index:%d", ctx->Req.CfgInx);
  1196. m_pEntity->StopVideo(ctx->Req.CfgInx);
  1197. ctx->Answer(Error_Succeed);
  1198. }
  1199. void CLocalMediaPlaySession::Handle_StopPlayAudio(SpReqAnsContext<PlayService_StopPlayAudio_Req, PlayService_StopPlayAudio_Ans>::Pointer ctx)
  1200. {
  1201. m_pEntity->StopAudio();
  1202. ctx->Answer(Error_Succeed);
  1203. }
  1204. void CLocalMediaPlaySession::Handle_StopPlayImage(SpReqAnsContext<PlayService_StopPlayImage_Req, PlayService_StopPlayImage_Ans>::Pointer ctx)
  1205. {
  1206. m_pEntity->StopImage(ctx->Req.CfgInx);
  1207. ctx->Answer(Error_Succeed);
  1208. }
  1209. void CLocalMediaPlaySession::OnClose(ErrorCodeEnum)
  1210. {
  1211. LOG_FUNCTION();
  1212. }
  1213. void CLocalMediaPlaySession::Handle_GetLocalVideoVolume( SpReqAnsContext<PlayService_GetLocalVideoVolume_Req, PlayService_GetLocalVideoVolume_Ans>::Pointer ctx )
  1214. {
  1215. int volume = 0;
  1216. if (m_pEntity->GetLocalVideoVolume(ctx->Req.CfgInx, volume))
  1217. {
  1218. ctx->Ans.Volume = volume;
  1219. ctx->Answer(Error_Succeed);
  1220. }
  1221. else
  1222. {
  1223. ctx->Answer(Error_Unexpect);
  1224. }
  1225. }
  1226. void CLocalMediaPlaySession::Handle_SetLocalVideoVolume( SpReqAnsContext<PlayService_SetLocalVideoVolume_Req, PlayService_SetLocalVideoVolume_Ans>::Pointer ctx )
  1227. {
  1228. if (m_pEntity->SetLocalVideoVolume(ctx->Req.CfgInx, ctx->Req.Volume))
  1229. {
  1230. ctx->Answer(Error_Succeed);
  1231. }
  1232. else
  1233. {
  1234. ctx->Answer(Error_Unexpect);
  1235. }
  1236. }
  1237. void CLocalMediaPlaySession::Handle_GetLocalAudioVolume(SpReqAnsContext<PlayService_GetLocalAudioVolume_Req, PlayService_GetLocalAudioVolume_Ans>::Pointer ctx)
  1238. {
  1239. auto audioRet = m_pEntity->GetLocalAudioVolume();
  1240. if (audioRet.first)
  1241. {
  1242. ctx->Ans.Volume = audioRet.second;
  1243. ctx->Answer(Error_Succeed);
  1244. }
  1245. else
  1246. {
  1247. ctx->Answer(Error_Unexpect);
  1248. }
  1249. }
  1250. void CLocalMediaPlaySession::Handle_SetLocalAudioVolume(SpReqAnsContext<PlayService_SetLocalAudioVolume_Req, PlayService_SetLocalAudioVolume_Ans>::Pointer ctx)
  1251. {
  1252. if (m_pEntity->SetLocalAudioVolume(ctx->Req.Volume))
  1253. {
  1254. ctx->Answer(Error_Succeed);
  1255. }
  1256. else
  1257. {
  1258. ctx->Answer(Error_Unexpect);
  1259. }
  1260. }
  1261. SP_BEGIN_ENTITY_MAP()
  1262. SP_ENTITY(CLocalMediaPlayEntity)
  1263. SP_END_ENTITY_MAP()