123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- #include "libpictureplayer.h"
- #include "CPicturePlayer.h"
- #include <string.h>
- #ifdef _WIN32
- #include <process.h>
- #include <io.h>
- #else
- #include <unistd.h>
- #include <pthread.h>
- #endif
- #ifndef RVC_DEFAULT_PIC_WIDTH
- #define RVC_DEFAULT_PIC_WIDTH 1920
- #endif // !RVC_DEFAULT_PIC_WIDTH
- #ifndef RVC_DEFAULT_PIC_HEIGHT
- #define RVC_DEFAULT_PIC_HEIGHT 1080
- #endif // !RVC_DEFAULT_PIC_HEIGHT
- #ifdef _WIN32
- static unsigned int __stdcall PicturePlayingFunc(void* param)
- #else
- static void* PicturePlayingFunc(void* param)
- #endif
- {
- CPicturePlayer* is = (CPicturePlayer*)param;
- is->m_pHostApi->PicDebug(PIC_LOG_DEBUG, "enter PicturePlayingFunc");
- is->StartPicPlay();
- is->SetPicPlayingFlag(false);
- is->m_pHostApi->PicDebug(PIC_LOG_DEBUG, "leave PicturePlayingFunc");
- return 0;
- }
- class libpictureplayer_impl
- {
- public:
- CPicPlayConfig m_stPlayConfig;
- CPicHostApi* m_pHostApi;
- CPicturePlayer* m_Player;
- bool m_bIsPlay;
- #ifdef _WIN32
- HANDLE m_PlayThread;
- #else
- pthread_t m_PlayThreadId;
- #endif
- public:
- libpictureplayer_impl(CPicHostApi* pHostApi) {
- m_bIsPlay = false;
- m_pHostApi = pHostApi;
- memset(&m_stPlayConfig, 0, sizeof(m_stPlayConfig));
- m_Player = new CPicturePlayer(pHostApi);
- if (NULL == m_Player) {
- pHostApi->PicDebug(PIC_LOG_DEBUG, "new PicturePlayer failed!");
- }
- #ifdef _WIN32
- m_PlayThread = NULL;
- #else
- m_PlayThreadId = 0;
- #endif
- }
- ~libpictureplayer_impl()
- {
- m_pHostApi = NULL;
- delete m_Player;
- m_Player = NULL;
- m_bIsPlay = false;
- #ifdef _WIN32
- m_PlayThread = NULL;
- #else
- m_PlayThreadId = 0;
- #endif
- }
- bool isStop()
- {
- //return !m_bIsPlay;
- return !m_Player->GetPicPlayingFlag();
- }
- bool CheckIsPlay(void* pthreadid)
- {
- bool bret = false;
- #ifdef _WIN32
- HANDLE threadHandle = m_PlayThread;
- if (NULL != threadHandle)
- {
- bret = true;
- *(HANDLE*)pthreadid = threadHandle;
- }
- #else
- int64_t iThreadId = m_PlayThreadId;
- if (0 != iThreadId)
- {
- bret = true;
- *(int64_t*)pthreadid = iThreadId;
- }
- #endif
- return bret;
- }
- bool StartPlayMedia(CPicPlayConfig& config)
- {
- bool bRet = false;
- //if (m_bIsPlay){
- // return true;
- //}
- memcpy(&m_stPlayConfig, &config, sizeof(CPicPlayConfig));
- rvc_picture_player_param_t tplayer_param = { 0 };
- tplayer_param.icx = m_stPlayConfig.nWndX;
- tplayer_param.icy = m_stPlayConfig.nWndY;
- tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth > 0 ? m_stPlayConfig.nWndWidth : RVC_DEFAULT_PIC_WIDTH;
- tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight > 0 ? m_stPlayConfig.nWndHeight : RVC_DEFAULT_PIC_HEIGHT;
- tplayer_param.bfull_screen = m_stPlayConfig.bFullScreen;
- tplayer_param.nplay_cnt = m_stPlayConfig.nPlayCnt;
- tplayer_param.nplay_interval = m_stPlayConfig.nPlayInterval;
- char strFileName[MAX_PATH] = { 0 };
- #ifdef _WIN32
- _snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
- #else
- snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
- #endif
- if (IsFileExist(strFileName)) {
- memcpy(tplayer_param.strfile_names[0], strFileName, strlen(strFileName));
- tplayer_param.nfile_cnt = 1;
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "File %s is not exist.", strFileName);
- tplayer_param.nfile_cnt = 0;
- }
-
- //if (0 == GetPicturePlayingParams(&tplayer_param))
- {
- if (0 == m_Player->InitParam(&tplayer_param))
- {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "init picture player success and begin start picture play.");
- #ifdef _WIN32
- m_PlayThread = (HANDLE)_beginthreadex(NULL, 0, &PicturePlayingFunc, m_Player, 0, NULL);
- if (NULL != m_PlayThread) {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread success.");
- m_bIsPlay = true;
- bRet = true;
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play Success.");
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
- }
- #else
- int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
- if (0 == err) {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread[%u] success.", m_PlayThreadId);
- m_bIsPlay = true;
- bRet = true;
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play Success.");
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
- }
- #endif
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_ERROR, "init picture player failed!");
- }
- }
- //else {
- // m_pHostApi->PicDebug("get picture playing params failed!");
- //}
- return bRet;
- }
- int GetPicturePlayingParams(rvc_picture_player_param_t* pParam)
- {
- int iRet = -1;
- if (NULL == pParam) {
- return iRet;
- }
- pParam->bfull_screen = m_stPlayConfig.bFullScreen;
- pParam->nplay_cnt = m_stPlayConfig.nPlayCnt;
- pParam->nplay_interval = m_stPlayConfig.nPlayInterval;
- size_t uValidCount = 0;
- for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
- char strFileName[MAX_PATH] = { 0 };
- #ifdef _WIN32
- _snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
- #else
- snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
- #endif
- if (IsFileExist(strFileName)) {
- memcpy(pParam->strfile_names[i], strFileName, strlen(strFileName));
- uValidCount++;
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "File %s is not exist.", strFileName);
- continue;
- }
- }
- pParam->nfile_cnt = uValidCount;
- if (NULL != m_pHostApi) {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "pParam m_uFilesCount = %d", uValidCount);
- }
- if (uValidCount > 0) {
- iRet = 0;
- }
- return iRet;
- }
- bool StartPlay(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- bool bRet = false;
- if (true == m_Player->GetPicPlayingFlag()){
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "now is playing!");
- return true;
- }
- int iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
- if (0 != iRet){
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "Load ImgConfiguration failed!");
- return bRet;
- }
- else{
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "Load ImgConfiguration succeeded while play local image!");
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
- }
- m_stPlayConfig.nWndX = nWndX;
- m_stPlayConfig.nWndY = nWndY;
- m_stPlayConfig.nWndWidth = nWndWidth;
- m_stPlayConfig.nWndHeight = nWndHeight;
- rvc_picture_player_param_t tplayer_param = { 0 };
- tplayer_param.icx = m_stPlayConfig.nWndX;
- tplayer_param.icy = m_stPlayConfig.nWndY;
- tplayer_param.uwindow_width = m_stPlayConfig.nWndWidth;
- tplayer_param.uwindow_height = m_stPlayConfig.nWndHeight;
- if (0 == GetPicturePlayingParams(&tplayer_param)) {
- if (0 == m_Player->InitParam(&tplayer_param))
- {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "init picture player Success.");
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
- #ifdef _WIN32
- m_PlayThread = (HANDLE)_beginthreadex(NULL, 0, &PicturePlayingFunc, m_Player, 0, NULL);
- if (NULL != m_PlayThread) {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread success.");
- m_bIsPlay = true;
- bRet = true;
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
- }
- #else
- int err = pthread_create(&m_PlayThreadId, NULL, PicturePlayingFunc, m_Player);
- if (0 == err) {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread[%u] success.", m_PlayThreadId);
- m_bIsPlay = true;
- bRet = true;
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "start picture play success.");
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "create picture play thread failed.");
- }
- #endif
- }
- }
- else {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "get picture playing params failed!");
- }
- return bRet;
- }
- bool StopPlay()
- {
- if (NULL != m_Player)
- {
- if (m_Player->GetPicPlayingFlag())
- {
- m_Player->StopPicPlay();
- m_bIsPlay = false;
- }
- else
- {
- m_pHostApi->PicDebug(PIC_LOG_DEBUG, "picture playing already stopped!");
- }
- }
- return true;
- }
- bool IsFileExist(const char* pFilePath)
- {
- bool bRet = false;
- if (NULL != pFilePath) {
- #ifdef _WIN32
- if (0 == _access(pFilePath, 0)) {
- bRet = true;
- }
- #else
- if (0 == access(pFilePath, F_OK)) {
- bRet = true;
- }
- #endif
- }
- return bRet;
- }
- };
- Clibpictureplayer::Clibpictureplayer(CPicHostApi* pHostApi)
- {
- m_pImpl = new libpictureplayer_impl(pHostApi);
- }
- Clibpictureplayer::~Clibpictureplayer()
- {
- delete m_pImpl;
- m_pImpl = NULL;
- }
- void Clibpictureplayer::Play(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- m_pImpl->StartPlay(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
- }
- void Clibpictureplayer::PlayMedia(CPicPlayConfig& config)
- {
- m_pImpl->StartPlayMedia(config);
- }
- bool Clibpictureplayer::checkIsStop() {
- return m_pImpl->isStop();
- }
- bool Clibpictureplayer::checkIsPlay(void* pthreadid)
- {
- return m_pImpl->CheckIsPlay(pthreadid);
- }
- void Clibpictureplayer::Close()
- {
- m_pImpl->StopPlay();
- }
|