libpictureplayer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. pthreadid = &iThreadId;
  46. }
  47. }
  48. bool StartPlayMedia(CPicPlayConfig& config)
  49. {
  50. bool bRet = false;
  51. if (m_bIsPlay){
  52. return true;
  53. }
  54. memcpy(&m_stPlayConfig, &config, sizeof(CPicPlayConfig));
  55. rvc_picture_player_param_t tplayer_param = { 0 };
  56. tplayer_param.icx = m_stPlayConfig.nWndX;
  57. tplayer_param.icy = m_stPlayConfig.nWndY;
  58. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
  59. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
  60. if (0 == GetPicturePlayingParams(&tplayer_param)) {
  61. if (0 == m_Player->Init(&tplayer_param))
  62. {
  63. m_pHostApi->PicDebug("Init Picture Player Success.");
  64. m_pHostApi->PicDebug("Start Picture Play Success.");
  65. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  66. if (0 == err) {
  67. m_pHostApi->PicDebug("create picture play thread[%lu] 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. }
  77. else {
  78. m_pHostApi->PicDebug("Get Picture Playing Params failed!");
  79. }
  80. return bRet;
  81. }
  82. int GetPicturePlayingParams(rvc_picture_player_param_t* pParam)
  83. {
  84. int iRet = -1;
  85. if (NULL == pParam) {
  86. return iRet;
  87. }
  88. pParam->bfull_screen = m_stPlayConfig.bFullScreen;
  89. pParam->nplay_cnt = m_stPlayConfig.nPlayCnt;
  90. pParam->nplay_interval = m_stPlayConfig.nPlayInterval;
  91. size_t uValidCount = 0;
  92. for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
  93. char strFileName[MAX_PATH] = { 0 };
  94. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
  95. if (IsFileExist(strFileName)) {
  96. memcpy(pParam->strfile_names[i], strFileName, strlen(strFileName));
  97. uValidCount++;
  98. }
  99. else {
  100. m_pHostApi->PicDebug("File %s is not exsit.", strFileName);
  101. continue;
  102. }
  103. }
  104. pParam->nfile_cnt = uValidCount;
  105. if (NULL != m_pHostApi) {
  106. m_pHostApi->PicDebug("pParam uFilesCount = %d", uValidCount);
  107. }
  108. if (uValidCount > 0) {
  109. iRet = 0;
  110. }
  111. return iRet;
  112. }
  113. bool StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  114. {
  115. bool bRet = false;
  116. int iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  117. if (0 != iRet){
  118. m_pHostApi->PicDebug("Load ImgConfiguration failed!");
  119. return bRet;
  120. }
  121. else{
  122. m_pHostApi->PicDebug("Load ImgConfiguration succeeded while play local image!");
  123. m_pHostApi->PicDebug("m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  124. }
  125. m_stPlayConfig.nWndX = nWndX;
  126. m_stPlayConfig.nWndY = nWndY;
  127. m_stPlayConfig.nWndWidth = nWndWidth;
  128. m_stPlayConfig.nWndHeight = nWndHeight;
  129. rvc_picture_player_param_t tplayer_param = { 0 };
  130. tplayer_param.icx = m_stPlayConfig.nWndX;
  131. tplayer_param.icy = m_stPlayConfig.nWndY;
  132. tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
  133. tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
  134. if (0 == GetPicturePlayingParams(&tplayer_param)) {
  135. if (0 == m_Player->Init(&tplayer_param))
  136. {
  137. m_pHostApi->PicDebug("Init Picture Player Success.");
  138. m_pHostApi->PicDebug("Start Picture Play Success.");
  139. int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
  140. if (0 == err) {
  141. m_pHostApi->PicDebug("create picture play thread[%lu] success.", m_PlayThreadId);
  142. m_bIsPlay = true;
  143. bRet = true;
  144. m_pHostApi->PicDebug("Start Picture Play Success.");
  145. }
  146. else {
  147. m_pHostApi->PicDebug("create picture play thread failed.");
  148. }
  149. }
  150. }
  151. else {
  152. m_pHostApi->PicDebug("Get Picture Playing Params failed!");
  153. }
  154. return bRet;
  155. }
  156. bool StopPlay()
  157. {
  158. if (NULL != m_Player)
  159. {
  160. if (m_Player->GetPicPlayingFlag())
  161. {
  162. m_Player->StopPicPlay();
  163. m_bIsPlay = false;
  164. }
  165. else
  166. {
  167. m_pHostApi->PicDebug("picture playing already stopped!");
  168. }
  169. }
  170. return true;
  171. }
  172. bool IsFileExist(const char* pFilePath)
  173. {
  174. bool bRet = false;
  175. if (NULL != pFilePath) {
  176. if (0 == access(pFilePath, F_OK)) {
  177. bRet = true;
  178. }
  179. }
  180. return bRet;
  181. }
  182. };
  183. Clibpictureplayer::Clibpictureplayer(CPicHostApi* pHostApi)
  184. {
  185. m_pImpl = new libpictureplayer_impl(pHostApi);
  186. }
  187. Clibpictureplayer::~Clibpictureplayer()
  188. {
  189. delete m_pImpl;
  190. m_pImpl = NULL;
  191. }
  192. void Clibpictureplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  193. {
  194. m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  195. }
  196. void Clibpictureplayer::PlayMedia(CPicPlayConfig& config)
  197. {
  198. m_pImpl->StartPlayMedia(config);
  199. }
  200. bool Clibpictureplayer::checkIsStop() {
  201. return m_pImpl->isStop();
  202. }
  203. bool Clibpictureplayer::checkIsPlay(void* pthreadid)
  204. {
  205. return m_pImpl->CheckIsPlay(pthreadid);
  206. }
  207. void Clibpictureplayer::Close()
  208. {
  209. m_pImpl->StopPlay();
  210. }
  211. void* PicturePlayingFunc(void* param)
  212. {
  213. CPicturePlayer* is = (CPicturePlayer*)param;
  214. is->m_pHostApi->PicDebug("enter PicturePlayingFunc");
  215. is->StartPicPlay();
  216. is->m_pHostApi->PicDebug("leave PicturePlayingFunc");
  217. return 0;
  218. }