CPicturePlayer.cpp 5.6 KB

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