libpictureplayer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "libpictureplayer.h"
  2. #include "CPicturePlayer.h"
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <pthread.h>
  6. void* PicturePlayingFunc(void* param);
  7. class libpictureplayer_impl
  8. {
  9. public:
  10. CPicPlayConfig m_stPlayConfig;
  11. CPicHostApi* m_pHostApi;
  12. CPicturePlayer* m_Player;
  13. bool m_bIsPlay;
  14. pthread_t m_PlayThreadId;
  15. public:
  16. libpictureplayer_impl(CPicHostApi* pHostApi) {
  17. m_bIsPlay = false;
  18. m_pHostApi = pHostApi;
  19. memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
  20. m_Player = new CPicturePlayer(pHostApi);
  21. if (NULL == m_Player) {
  22. pHostApi->PicDebug("new PicturePlayer failed!");
  23. }
  24. m_PlayThreadId = 0;
  25. }
  26. ~libpictureplayer_impl()
  27. {
  28. m_pHostApi = NULL;
  29. delete m_Player;
  30. m_Player = NULL;
  31. m_bIsPlay = false;
  32. m_PlayThreadId = 0;
  33. }
  34. bool isStop()
  35. {
  36. return !m_bIsPlay;
  37. }
  38. bool CheckIsPlay(void* pthreadid)
  39. {
  40. bool bret = false;
  41. int64_t iThreadId = m_PlayThreadId;
  42. if (0 != iThreadId)
  43. {
  44. bret = true;
  45. *(int64_t*)pthreadid = iThreadId;
  46. }
  47. return bret;
  48. }
  49. bool StartPlayMedia(CPicPlayConfig& config)
  50. {
  51. bool bRet = false;
  52. if (m_bIsPlay){
  53. return true;
  54. }
  55. memcpy(&m_stPlayConfig, &config, sizeof(CPicPlayConfig));
  56. rvc_picture_player_param_t tplayer_param = { 0 };
  57. tplayer_param.icx = m_stPlayConfig.nWndX;
  58. tplayer_param.icy = m_stPlayConfig.nWndY;
  59. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
  60. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
  61. if (0 == GetPicturePlayingParams(&tplayer_param)) {
  62. if (0 == m_Player->Init(&tplayer_param))
  63. {
  64. m_pHostApi->PicDebug("init picture player success and begin start picture play.");
  65. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  66. if (0 == err) {
  67. m_pHostApi->PicDebug("create picture play thread[%u] success.", m_PlayThreadId);
  68. m_bIsPlay = true;
  69. bRet = true;
  70. m_pHostApi->PicDebug("Start Picture Play Success.");
  71. }
  72. else {
  73. m_pHostApi->PicDebug("create picture play thread failed.");
  74. }
  75. }
  76. else {
  77. m_pHostApi->PicDebug("init picture player failed!");
  78. }
  79. }
  80. else {
  81. m_pHostApi->PicDebug("Get Picture Playing Params failed!");
  82. }
  83. return bRet;
  84. }
  85. int GetPicturePlayingParams(rvc_picture_player_param_t* pParam)
  86. {
  87. int iRet = -1;
  88. if (NULL == pParam) {
  89. return iRet;
  90. }
  91. pParam->bfull_screen = m_stPlayConfig.bFullScreen;
  92. pParam->nplay_cnt = m_stPlayConfig.nPlayCnt;
  93. pParam->nplay_interval = m_stPlayConfig.nPlayInterval;
  94. size_t uValidCount = 0;
  95. for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
  96. char strFileName[MAX_PATH] = { 0 };
  97. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
  98. if (IsFileExist(strFileName)) {
  99. memcpy(pParam->strfile_names[i], strFileName, strlen(strFileName));
  100. uValidCount++;
  101. }
  102. else {
  103. m_pHostApi->PicDebug("File %s is not exsit.", strFileName);
  104. continue;
  105. }
  106. }
  107. pParam->nfile_cnt = uValidCount;
  108. if (NULL != m_pHostApi) {
  109. m_pHostApi->PicDebug("pParam uFilesCount = %d", uValidCount);
  110. }
  111. if (uValidCount > 0) {
  112. iRet = 0;
  113. }
  114. return iRet;
  115. }
  116. bool StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  117. {
  118. bool bRet = false;
  119. int iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  120. if (0 != iRet){
  121. m_pHostApi->PicDebug("Load ImgConfiguration failed!");
  122. return bRet;
  123. }
  124. else{
  125. m_pHostApi->PicDebug("Load ImgConfiguration succeeded while play local image!");
  126. m_pHostApi->PicDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  127. }
  128. m_stPlayConfig.nWndX = nWndX;
  129. m_stPlayConfig.nWndY = nWndY;
  130. m_stPlayConfig.nWndWidth = nWndWidth;
  131. m_stPlayConfig.nWndHeight = nWndHeight;
  132. rvc_picture_player_param_t tplayer_param = { 0 };
  133. tplayer_param.icx = m_stPlayConfig.nWndX;
  134. tplayer_param.icy = m_stPlayConfig.nWndY;
  135. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
  136. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
  137. if (0 == GetPicturePlayingParams(&tplayer_param)) {
  138. if (0 == m_Player->Init(&tplayer_param))
  139. {
  140. m_pHostApi->PicDebug("Init Picture Player Success.");
  141. m_pHostApi->PicDebug("Start Picture Play Success.");
  142. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  143. if (0 == err) {
  144. m_pHostApi->PicDebug("create picture play thread[%u] success.", m_PlayThreadId);
  145. m_bIsPlay = true;
  146. bRet = true;
  147. m_pHostApi->PicDebug("Start Picture Play Success.");
  148. }
  149. else {
  150. m_pHostApi->PicDebug("create picture play thread failed.");
  151. }
  152. }
  153. }
  154. else {
  155. m_pHostApi->PicDebug("Get Picture Playing Params failed!");
  156. }
  157. return bRet;
  158. }
  159. bool StopPlay()
  160. {
  161. if (NULL != m_Player)
  162. {
  163. if (m_Player->GetPicPlayingFlag())
  164. {
  165. m_Player->StopPicPlay();
  166. m_bIsPlay = false;
  167. }
  168. else
  169. {
  170. m_pHostApi->PicDebug("picture playing already stopped!");
  171. }
  172. }
  173. return true;
  174. }
  175. bool IsFileExist(const char* pFilePath)
  176. {
  177. bool bRet = false;
  178. if (NULL != pFilePath) {
  179. if (0 == access(pFilePath, F_OK)) {
  180. bRet = true;
  181. }
  182. }
  183. return bRet;
  184. }
  185. };
  186. Clibpictureplayer::Clibpictureplayer(CPicHostApi* pHostApi)
  187. {
  188. m_pImpl = new libpictureplayer_impl(pHostApi);
  189. }
  190. Clibpictureplayer::~Clibpictureplayer()
  191. {
  192. delete m_pImpl;
  193. m_pImpl = NULL;
  194. }
  195. void Clibpictureplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  196. {
  197. m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  198. }
  199. void Clibpictureplayer::PlayMedia(CPicPlayConfig& config)
  200. {
  201. m_pImpl->StartPlayMedia(config);
  202. }
  203. bool Clibpictureplayer::checkIsStop() {
  204. return m_pImpl->isStop();
  205. }
  206. bool Clibpictureplayer::checkIsPlay(void* pthreadid)
  207. {
  208. return m_pImpl->CheckIsPlay(pthreadid);
  209. }
  210. void Clibpictureplayer::Close()
  211. {
  212. m_pImpl->StopPlay();
  213. }
  214. void* PicturePlayingFunc(void* param)
  215. {
  216. CPicturePlayer* is = (CPicturePlayer*)param;
  217. is->m_pHostApi->PicDebug("enter PicturePlayingFunc");
  218. is->StartPicPlay();
  219. is->SetPicPlayingFlag(false);
  220. is->m_pHostApi->PicDebug("leave PicturePlayingFunc");
  221. return 0;
  222. }