libpictureplayer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "libpictureplayer.h"
  2. #include "CPicturePlayer.h"
  3. #include <string.h>
  4. #ifdef _WIN32
  5. #include <process.h>
  6. #include <io.h>
  7. #else
  8. #include <unistd.h>
  9. #include <pthread.h>
  10. #endif
  11. #ifndef RVC_DEFAULT_PIC_WIDTH
  12. #define RVC_DEFAULT_PIC_WIDTH 1920
  13. #endif // !RVC_DEFAULT_PIC_WIDTH
  14. #ifndef RVC_DEFAULT_PIC_HEIGHT
  15. #define RVC_DEFAULT_PIC_HEIGHT 1080
  16. #endif // !RVC_DEFAULT_PIC_HEIGHT
  17. #ifdef _WIN32
  18. static unsigned int __stdcall PicturePlayingFunc(void* param)
  19. #else
  20. static void* PicturePlayingFunc(void* param)
  21. #endif
  22. {
  23. CPicturePlayer* is = (CPicturePlayer*)param;
  24. is->m_pHostApi->PicDebug(PIC_LOG_DEBUG, "enter PicturePlayingFunc");
  25. is->StartPicPlay();
  26. is->SetPicPlayingFlag(false);
  27. is->m_pHostApi->PicDebug(PIC_LOG_DEBUG, "leave PicturePlayingFunc");
  28. return 0;
  29. }
  30. class libpictureplayer_impl
  31. {
  32. public:
  33. CPicPlayConfig m_stPlayConfig;
  34. CPicHostApi* m_pHostApi;
  35. CPicturePlayer* m_Player;
  36. bool m_bIsPlay;
  37. #ifdef _WIN32
  38. HANDLE m_PlayThread;
  39. #else
  40. pthread_t m_PlayThreadId;
  41. #endif
  42. public:
  43. libpictureplayer_impl(CPicHostApi* pHostApi) {
  44. m_bIsPlay = false;
  45. m_pHostApi = pHostApi;
  46. memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
  47. m_Player = new CPicturePlayer(pHostApi);
  48. if (NULL == m_Player) {
  49. pHostApi->PicDebug(PIC_LOG_DEBUG, "new PicturePlayer failed!");
  50. }
  51. #ifdef _WIN32
  52. m_PlayThread = NULL;
  53. #else
  54. m_PlayThreadId = 0;
  55. #endif
  56. }
  57. ~libpictureplayer_impl()
  58. {
  59. m_pHostApi = NULL;
  60. delete m_Player;
  61. m_Player = NULL;
  62. m_bIsPlay = false;
  63. #ifdef _WIN32
  64. m_PlayThread = NULL;
  65. #else
  66. m_PlayThreadId = 0;
  67. #endif
  68. }
  69. bool isStop()
  70. {
  71. //return !m_bIsPlay;
  72. return !m_Player->GetPicPlayingFlag();
  73. }
  74. bool CheckIsPlay(void* pthreadid)
  75. {
  76. bool bret = false;
  77. #ifdef _WIN32
  78. HANDLE threadHandle = m_PlayThread;
  79. if (NULL != threadHandle)
  80. {
  81. bret = true;
  82. *(HANDLE*)pthreadid = threadHandle;
  83. }
  84. #else
  85. int64_t iThreadId = m_PlayThreadId;
  86. if (0 != iThreadId)
  87. {
  88. bret = true;
  89. *(int64_t*)pthreadid = iThreadId;
  90. }
  91. #endif
  92. return bret;
  93. }
  94. bool StartPlayMedia(CPicPlayConfig& config)
  95. {
  96. bool bRet = false;
  97. //if (m_bIsPlay){
  98. // return true;
  99. //}
  100. memcpy(&m_stPlayConfig, &config, sizeof(CPicPlayConfig));
  101. rvc_picture_player_param_t tplayer_param = { 0 };
  102. tplayer_param.icx = m_stPlayConfig.nWndX;
  103. tplayer_param.icy = m_stPlayConfig.nWndY;
  104. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth > 0 ? m_stPlayConfig.nWndWidth : RVC_DEFAULT_PIC_WIDTH;
  105. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight > 0 ? m_stPlayConfig.nWndHeight : RVC_DEFAULT_PIC_HEIGHT;
  106. tplayer_param.bfull_screen = m_stPlayConfig.bFullScreen;
  107. tplayer_param.nplay_cnt = m_stPlayConfig.nPlayCnt;
  108. tplayer_param.nplay_interval = m_stPlayConfig.nPlayInterval;
  109. char strFileName[MAX_PATH] = { 0 };
  110. #ifdef _WIN32
  111. _snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
  112. #else
  113. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
  114. #endif
  115. if (IsFileExist(strFileName)) {
  116. memcpy(tplayer_param.strfile_names[0], strFileName, strlen(strFileName));
  117. tplayer_param.nfile_cnt = 1;
  118. }
  119. else {
  120. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "File %s is not exist.", strFileName);
  121. tplayer_param.nfile_cnt = 0;
  122. }
  123. //if (0 == GetPicturePlayingParams(&tplayer_param))
  124. {
  125. if (0 == m_Player->InitParam(&tplayer_param))
  126. {
  127. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "init picture player success and begin start picture play.");
  128. #ifdef _WIN32
  129. m_PlayThread = (HANDLE)_beginthreadex(NULL, 0, &PicturePlayingFunc, m_Player, 0, NULL);
  130. if (NULL != m_PlayThread) {
  131. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread success.");
  132. m_bIsPlay = true;
  133. bRet = true;
  134. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play Success.");
  135. }
  136. else {
  137. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
  138. }
  139. #else
  140. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  141. if (0 == err) {
  142. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread[%u] success.", m_PlayThreadId);
  143. m_bIsPlay = true;
  144. bRet = true;
  145. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play Success.");
  146. }
  147. else {
  148. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
  149. }
  150. #endif
  151. }
  152. else {
  153. m_pHostApi->PicDebug(PIC_LOG_ERROR, "init picture player failed!");
  154. }
  155. }
  156. //else {
  157. // m_pHostApi->PicDebug("get picture playing params failed!");
  158. //}
  159. return bRet;
  160. }
  161. int GetPicturePlayingParams(rvc_picture_player_param_t* pParam)
  162. {
  163. int iRet = -1;
  164. if (NULL == pParam) {
  165. return iRet;
  166. }
  167. pParam->bfull_screen = m_stPlayConfig.bFullScreen;
  168. pParam->nplay_cnt = m_stPlayConfig.nPlayCnt;
  169. pParam->nplay_interval = m_stPlayConfig.nPlayInterval;
  170. size_t uValidCount = 0;
  171. for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
  172. char strFileName[MAX_PATH] = { 0 };
  173. #ifdef _WIN32
  174. _snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
  175. #else
  176. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
  177. #endif
  178. if (IsFileExist(strFileName)) {
  179. memcpy(pParam->strfile_names[i], strFileName, strlen(strFileName));
  180. uValidCount++;
  181. }
  182. else {
  183. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "File %s is not exist.", strFileName);
  184. continue;
  185. }
  186. }
  187. pParam->nfile_cnt = uValidCount;
  188. if (NULL != m_pHostApi) {
  189. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "pParam m_uFilesCount = %d", uValidCount);
  190. }
  191. if (uValidCount > 0) {
  192. iRet = 0;
  193. }
  194. return iRet;
  195. }
  196. bool StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  197. {
  198. bool bRet = false;
  199. if (true == m_Player->GetPicPlayingFlag()){
  200. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "now is playing!");
  201. return true;
  202. }
  203. int iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  204. if (0 != iRet){
  205. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "Load ImgConfiguration failed!");
  206. return bRet;
  207. }
  208. else{
  209. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "Load ImgConfiguration succeeded while play local image!");
  210. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  211. }
  212. m_stPlayConfig.nWndX = nWndX;
  213. m_stPlayConfig.nWndY = nWndY;
  214. m_stPlayConfig.nWndWidth = nWndWidth;
  215. m_stPlayConfig.nWndHeight = nWndHeight;
  216. rvc_picture_player_param_t tplayer_param = { 0 };
  217. tplayer_param.icx = m_stPlayConfig.nWndX;
  218. tplayer_param.icy = m_stPlayConfig.nWndY;
  219. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
  220. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
  221. if (0 == GetPicturePlayingParams(&tplayer_param)) {
  222. if (0 == m_Player->InitParam(&tplayer_param))
  223. {
  224. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "init picture player Success.");
  225. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
  226. #ifdef _WIN32
  227. m_PlayThread = (HANDLE)_beginthreadex(NULL, 0, &PicturePlayingFunc, m_Player, 0, NULL);
  228. if (NULL != m_PlayThread) {
  229. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread success.");
  230. m_bIsPlay = true;
  231. bRet = true;
  232. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
  233. }
  234. else {
  235. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
  236. }
  237. #else
  238. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  239. if (0 == err) {
  240. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread[%u] success.", m_PlayThreadId);
  241. m_bIsPlay = true;
  242. bRet = true;
  243. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
  244. }
  245. else {
  246. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
  247. }
  248. #endif
  249. }
  250. }
  251. else {
  252. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "get picture playing params failed!");
  253. }
  254. return bRet;
  255. }
  256. bool StopPlay()
  257. {
  258. if (NULL != m_Player)
  259. {
  260. if (m_Player->GetPicPlayingFlag())
  261. {
  262. m_Player->StopPicPlay();
  263. m_bIsPlay = false;
  264. }
  265. else
  266. {
  267. m_pHostApi->PicDebug(PIC_LOG_DEBUG, "picture playing already stopped!");
  268. }
  269. }
  270. return true;
  271. }
  272. bool IsFileExist(const char* pFilePath)
  273. {
  274. bool bRet = false;
  275. if (NULL != pFilePath) {
  276. #ifdef _WIN32
  277. if (0 == _access(pFilePath, 0)) {
  278. bRet = true;
  279. }
  280. #else
  281. if (0 == access(pFilePath, F_OK)) {
  282. bRet = true;
  283. }
  284. #endif
  285. }
  286. return bRet;
  287. }
  288. };
  289. Clibpictureplayer::Clibpictureplayer(CPicHostApi* pHostApi)
  290. {
  291. m_pImpl = new libpictureplayer_impl(pHostApi);
  292. }
  293. Clibpictureplayer::~Clibpictureplayer()
  294. {
  295. delete m_pImpl;
  296. m_pImpl = NULL;
  297. }
  298. void Clibpictureplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  299. {
  300. m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  301. }
  302. void Clibpictureplayer::PlayMedia(CPicPlayConfig& config)
  303. {
  304. m_pImpl->StartPlayMedia(config);
  305. }
  306. bool Clibpictureplayer::checkIsStop() {
  307. return m_pImpl->isStop();
  308. }
  309. bool Clibpictureplayer::checkIsPlay(void* pthreadid)
  310. {
  311. return m_pImpl->CheckIsPlay(pthreadid);
  312. }
  313. void Clibpictureplayer::Close()
  314. {
  315. m_pImpl->StopPlay();
  316. }