CPicturePlayer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #include "CPicturePlayer.h"
  6. static char* rvc_strdup(const char* strdata)
  7. {
  8. char* strbuffer = NULL;
  9. if (NULL == strdata)
  10. {
  11. return strbuffer;
  12. }
  13. uint8_t ulen = strlen(strdata);
  14. if (strbuffer = (char*)malloc(ulen + 1))
  15. {
  16. memset(strbuffer, 0, ulen + 1);
  17. memcpy(strbuffer, strdata, ulen);
  18. }
  19. return strbuffer;
  20. }
  21. static void rvc_strfree(char* strdata)
  22. {
  23. if (NULL != strdata)
  24. {
  25. free(strdata);
  26. strdata = NULL;
  27. }
  28. }
  29. CPicturePlayer::CPicturePlayer(CPicHostApi* pHostApi)
  30. {
  31. m_thid = NULL;
  32. m_bplaying = false;
  33. m_window = NULL;
  34. m_nfile_cnt = 0;
  35. m_nplay_cnt = 0;
  36. m_nplay_interval = 5000;
  37. m_stricopath = NULL;
  38. memset(m_strroot_path, 0, MAX_PATH);
  39. for (size_t i = 0; i < MAX_FILECOUNT; i++){
  40. memset(m_strfile_names[i], 0, MAX_PATH);
  41. }
  42. m_pHostApi = pHostApi;
  43. if (NULL != m_pHostApi){
  44. m_pHostApi->PicDebug("new CPicturePlayer success!");
  45. char strIcoPath[MAX_PATH] = { 0 };
  46. if (0 == pHostApi->GetPicPlayerIcoPath(strIcoPath, MAX_PATH)) {
  47. m_stricopath = rvc_strdup(strIcoPath);
  48. }
  49. }
  50. for (int inum = 0; inum < MAX_DISPLAYNUM; inum++){
  51. memset(&m_dispalymode[inum], 0, sizeof(SDL_DisplayMode));
  52. }
  53. m_show_width = 0;
  54. m_show_height = 0;
  55. m_busrstop = false;
  56. }
  57. CPicturePlayer::~CPicturePlayer()
  58. {
  59. DeInit();
  60. }
  61. size_t CPicturePlayer::GetVideoDisplayInfo()
  62. {
  63. size_t uCount = SDL_GetNumVideoDisplays();
  64. m_pHostApi->PicDebug("VideoDisplays Number is %d:", uCount);
  65. for (size_t i = 0; i < uCount && i < MAX_DISPLAYNUM; i++){
  66. SDL_GetDesktopDisplayMode(i, &m_dispalymode[i]);
  67. m_pHostApi->PicDebug("VideoDisplays{%d} format = %d", i, m_dispalymode[i].format);
  68. m_pHostApi->PicDebug("VideoDisplays{%d} w = %d", i, m_dispalymode[i].w);
  69. m_pHostApi->PicDebug("VideoDisplays{%d} h = %d", i, m_dispalymode[i].h);
  70. m_pHostApi->PicDebug("VideoDisplays{%d} refresh_rate = %d", i, m_dispalymode[i].refresh_rate);
  71. }
  72. return uCount;
  73. }
  74. int CPicturePlayer::Init(rvc_picture_player_param_t* tparam)
  75. {
  76. int iRet = -1;
  77. if (NULL == tparam) {
  78. return iRet;
  79. }
  80. if (0 != SDL_Init(SDL_INIT_VIDEO)) {
  81. m_pHostApi->PicDebug("Could not initialize SDL - %s", SDL_GetError());
  82. m_pHostApi->PicDebug("(Did you set the DISPLAY variable?)");
  83. return iRet;
  84. }
  85. m_nfile_cnt = tparam->nfile_cnt;
  86. m_nplay_cnt = tparam->nplay_cnt;
  87. m_nplay_interval = tparam->nplay_interval;
  88. snprintf(m_strroot_path, MAX_PATH, "%s", tparam->strroot_path);
  89. for (size_t index = 0; index < m_nfile_cnt; index++) {
  90. snprintf(m_strfile_names[index], MAX_PATH, "%s", tparam->strfile_names[index]);
  91. }
  92. Uint32 uflag = SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS;
  93. if (tparam->bfull_screen) {
  94. uflag += SDL_WINDOW_FULLSCREEN;
  95. }
  96. size_t uVideoPlayNum = GetVideoDisplayInfo();
  97. int idispalycx = SDL_WINDOWPOS_UNDEFINED;
  98. int idispalycy = SDL_WINDOWPOS_UNDEFINED;
  99. int idispaly_width = m_dispalymode[0].w;
  100. int idispaly_height = m_dispalymode[0].h;
  101. if (false == tparam->bprim_monitor && uVideoPlayNum > 1) {
  102. idispalycx = m_dispalymode[0].w;
  103. idispaly_width = m_dispalymode[1].w;
  104. idispaly_height = m_dispalymode[1].h;
  105. }
  106. m_show_width = idispaly_width;
  107. m_show_height = idispaly_height;
  108. //1.创建播放窗体
  109. m_window = SDL_CreateWindow("图片播放器",
  110. idispalycx,// 不关心窗口X坐标
  111. idispalycy,// 不关心窗口Y坐标
  112. idispaly_width,
  113. idispaly_height,
  114. uflag
  115. );
  116. if (NULL == m_window){
  117. m_pHostApi->PicDebug("SDL_CreateWindow() failed: %s.", SDL_GetError());
  118. return -1;
  119. }
  120. //2.设置播放器ico
  121. if (NULL != m_stricopath) {
  122. SDL_Surface* IconSurface = SDL_LoadBMP(m_stricopath);
  123. if (NULL == IconSurface) {
  124. m_pHostApi->PicDebug("SDL_LoadBMP(%s) failed: %s", m_stricopath, SDL_GetError());
  125. }
  126. else {
  127. SDL_SetWindowIcon(m_window, IconSurface);
  128. SDL_FreeSurface(IconSurface);
  129. }
  130. }
  131. iRet = 0;
  132. return iRet;
  133. }
  134. int CPicturePlayer::DeInit()
  135. {
  136. int iRet = -1;
  137. rvc_strfree(m_stricopath);
  138. m_stricopath = NULL;
  139. m_show_width = 0;
  140. m_show_height = 0;
  141. m_busrstop = false;
  142. if (NULL != m_window) {
  143. SDL_DestroyWindow(m_window);
  144. }
  145. SDL_Quit();
  146. iRet = 0;
  147. return iRet;
  148. }
  149. bool CPicturePlayer::StartPicPlay()
  150. {
  151. bool bRet = false;
  152. m_bplaying = true;
  153. m_pHostApi->PicDebug("set m_bplaying = true");
  154. SDL_Surface* surface = SDL_GetWindowSurface(m_window);
  155. int iPlayCount = 0;
  156. for (; iPlayCount < m_nplay_cnt; iPlayCount++)
  157. {
  158. if (m_busrstop) {
  159. break;
  160. }
  161. int index = 0;
  162. for (; index < m_nfile_cnt; index++)
  163. {
  164. if (m_busrstop) {
  165. break;
  166. }
  167. SDL_Surface* image = SDL_LoadBMP(m_strfile_names[index]);
  168. //SDL_Surface* image = IMG_Load(is->m_strfile_names[index]);
  169. if (NULL == image) {
  170. m_pHostApi->PicDebug("SDL_LoadBMP %s failed!", m_strfile_names[index]);
  171. continue;
  172. }
  173. else {
  174. m_pHostApi->PicDebug("SDL_LoadBMP %s success!", m_strfile_names[index]);
  175. }
  176. SDL_Rect dst;
  177. dst.x = 0;
  178. dst.y = 0;
  179. dst.w = m_show_width;
  180. dst.h = m_show_height;
  181. SDL_BlitSurface(image, NULL, surface, &dst);
  182. SDL_UpdateWindowSurface(m_window);
  183. SDL_FreeSurface(surface);
  184. SDL_FreeSurface(image);
  185. SDL_Delay(m_nplay_interval);
  186. }
  187. }
  188. if (iPlayCount == m_nplay_cnt) {
  189. m_pHostApi->PicDebug("%d times picture playing task finished, exit.", iPlayCount);
  190. }
  191. else
  192. {
  193. m_pHostApi->PicDebug("user stop picture playing task, exit");
  194. }
  195. SDL_Quit();
  196. m_pHostApi->PicDebug("set m_bplaying = false");
  197. m_bplaying = false;
  198. bRet = true;
  199. return bRet;
  200. }
  201. int CPicturePlayer::StopPicPlay()
  202. {
  203. int iRet = -1;
  204. m_busrstop = true;
  205. m_pHostApi->PicDebug("stop picture play, set usr stop flag true.");
  206. iRet = 0;
  207. return iRet;
  208. }
  209. bool CPicturePlayer::GetPicPlayingFlag()
  210. {
  211. m_pHostApi->PicDebug("m_bplaying flag is %s", m_bplaying ? "true" : "false");
  212. return m_bplaying;
  213. }