123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #include <stdio.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <string.h>
- #include "CPicturePlayer.h"
- static char* rvc_strdup(const char* strdata)
- {
- char* strbuffer = NULL;
- if (NULL == strdata)
- {
- return strbuffer;
- }
-
- uint8_t ulen = strlen(strdata);
- if (strbuffer = (char*)malloc(ulen + 1))
- {
- memset(strbuffer, 0, ulen + 1);
- memcpy(strbuffer, strdata, ulen);
- }
- return strbuffer;
- }
- static void rvc_strfree(char* strdata)
- {
- if (NULL != strdata)
- {
- free(strdata);
- strdata = NULL;
- }
- }
- CPicturePlayer::CPicturePlayer(CPicHostApi* pHostApi)
- {
- m_thid = NULL;
- m_bplaying = false;
-
- m_window = NULL;
- m_nfile_cnt = 0;
- m_nplay_cnt = 0;
- m_nplay_interval = 5000;
- m_stricopath = NULL;
- memset(m_strroot_path, 0, MAX_PATH);
- for (size_t i = 0; i < MAX_FILECOUNT; i++){
- memset(m_strfile_names[i], 0, MAX_PATH);
- }
- m_pHostApi = pHostApi;
- if (NULL != m_pHostApi){
- m_pHostApi->PicDebug("new CPicturePlayer success!");
- char strIcoPath[MAX_PATH] = { 0 };
- if (0 == pHostApi->GetPicPlayerIcoPath(strIcoPath, MAX_PATH)) {
- m_stricopath = rvc_strdup(strIcoPath);
- }
- }
- for (int inum = 0; inum < MAX_DISPLAYNUM; inum++){
- memset(&m_dispalymode[inum], 0, sizeof(SDL_DisplayMode));
- }
- m_show_width = 0;
- m_show_height = 0;
- m_busrstop = false;
- }
- CPicturePlayer::~CPicturePlayer()
- {
- DeInit();
- }
- size_t CPicturePlayer::GetVideoDisplayInfo()
- {
- size_t uCount = SDL_GetNumVideoDisplays();
- m_pHostApi->PicDebug("VideoDisplays Number is %d:", uCount);
- for (size_t i = 0; i < uCount && i < MAX_DISPLAYNUM; i++){
- SDL_GetDesktopDisplayMode(i, &m_dispalymode[i]);
- m_pHostApi->PicDebug("VideoDisplays{%d} format = %d", i, m_dispalymode[i].format);
- m_pHostApi->PicDebug("VideoDisplays{%d} w = %d", i, m_dispalymode[i].w);
- m_pHostApi->PicDebug("VideoDisplays{%d} h = %d", i, m_dispalymode[i].h);
- m_pHostApi->PicDebug("VideoDisplays{%d} refresh_rate = %d", i, m_dispalymode[i].refresh_rate);
- }
- return uCount;
- }
- int CPicturePlayer::Init(rvc_picture_player_param_t* tparam)
- {
- int iRet = -1;
- if (NULL == tparam) {
- return iRet;
- }
- if (0 != SDL_Init(SDL_INIT_VIDEO)) {
- m_pHostApi->PicDebug("Could not initialize SDL - %s", SDL_GetError());
- m_pHostApi->PicDebug("(Did you set the DISPLAY variable?)");
- return iRet;
- }
- m_nfile_cnt = tparam->nfile_cnt;
- m_nplay_cnt = tparam->nplay_cnt;
- m_nplay_interval = tparam->nplay_interval;
- snprintf(m_strroot_path, MAX_PATH, "%s", tparam->strroot_path);
- for (size_t index = 0; index < m_nfile_cnt; index++) {
- snprintf(m_strfile_names[index], MAX_PATH, "%s", tparam->strfile_names[index]);
- }
- Uint32 uflag = SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS;
- if (tparam->bfull_screen) {
- uflag += SDL_WINDOW_FULLSCREEN;
- }
- size_t uVideoPlayNum = GetVideoDisplayInfo();
- int idispalycx = SDL_WINDOWPOS_UNDEFINED;
- int idispalycy = SDL_WINDOWPOS_UNDEFINED;
- int idispaly_width = m_dispalymode[0].w;
- int idispaly_height = m_dispalymode[0].h;
- if (false == tparam->bprim_monitor && uVideoPlayNum > 1) {
- idispalycx = m_dispalymode[0].w;
- idispaly_width = m_dispalymode[1].w;
- idispaly_height = m_dispalymode[1].h;
- }
- m_show_width = idispaly_width;
- m_show_height = idispaly_height;
- //1.创建播放窗体
- m_window = SDL_CreateWindow("图片播放器",
- idispalycx,// 不关心窗口X坐标
- idispalycy,// 不关心窗口Y坐标
- idispaly_width,
- idispaly_height,
- uflag
- );
- if (NULL == m_window){
- m_pHostApi->PicDebug("SDL_CreateWindow() failed: %s.", SDL_GetError());
- return -1;
- }
- //2.设置播放器ico
- if (NULL != m_stricopath) {
- SDL_Surface* IconSurface = SDL_LoadBMP(m_stricopath);
- if (NULL == IconSurface) {
- m_pHostApi->PicDebug("SDL_LoadBMP(%s) failed: %s", m_stricopath, SDL_GetError());
- }
- else {
- SDL_SetWindowIcon(m_window, IconSurface);
- SDL_FreeSurface(IconSurface);
- }
- }
- iRet = 0;
- return iRet;
- }
- int CPicturePlayer::DeInit()
- {
- int iRet = -1;
- rvc_strfree(m_stricopath);
- m_stricopath = NULL;
- m_show_width = 0;
- m_show_height = 0;
- m_busrstop = false;
- if (NULL != m_window) {
- SDL_DestroyWindow(m_window);
- }
- SDL_Quit();
- iRet = 0;
- return iRet;
- }
- bool CPicturePlayer::StartPicPlay()
- {
- bool bRet = false;
- m_bplaying = true;
- m_pHostApi->PicDebug("set m_bplaying = true");
- SDL_Surface* surface = SDL_GetWindowSurface(m_window);
- int iPlayCount = 0;
- for (; iPlayCount < m_nplay_cnt; iPlayCount++)
- {
- if (m_busrstop) {
- break;
- }
- int index = 0;
- for (; index < m_nfile_cnt; index++)
- {
- if (m_busrstop) {
- break;
- }
- SDL_Surface* image = SDL_LoadBMP(m_strfile_names[index]);
- //SDL_Surface* image = IMG_Load(is->m_strfile_names[index]);
- if (NULL == image) {
- m_pHostApi->PicDebug("SDL_LoadBMP %s failed!", m_strfile_names[index]);
- continue;
- }
- else {
- m_pHostApi->PicDebug("SDL_LoadBMP %s success!", m_strfile_names[index]);
- }
- SDL_Rect dst;
- dst.x = 0;
- dst.y = 0;
- dst.w = m_show_width;
- dst.h = m_show_height;
- SDL_BlitSurface(image, NULL, surface, &dst);
- SDL_UpdateWindowSurface(m_window);
- SDL_FreeSurface(surface);
- SDL_FreeSurface(image);
- SDL_Delay(m_nplay_interval);
- }
- }
- if (iPlayCount == m_nplay_cnt) {
- m_pHostApi->PicDebug("%d times picture playing task finished, exit.", iPlayCount);
- }
- else
- {
- m_pHostApi->PicDebug("user stop picture playing task, exit");
- }
- SDL_Quit();
- m_pHostApi->PicDebug("set m_bplaying = false");
- m_bplaying = false;
- bRet = true;
- return bRet;
- }
- int CPicturePlayer::StopPicPlay()
- {
- int iRet = -1;
- m_busrstop = true;
- m_pHostApi->PicDebug("stop picture play, set usr stop flag true.");
- iRet = 0;
- return iRet;
- }
- bool CPicturePlayer::GetPicPlayingFlag()
- {
- m_pHostApi->PicDebug("m_bplaying flag is %s", m_bplaying ? "true" : "false");
- return m_bplaying;
- }
|