123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- #include "libmediaplayer.h"
- #include "player.h"
- #include <unistd.h>
- class libmediaplayer_impl
- {
- private:
- CMediaHostApi* m_pHostApi;
- CMediaPlayer* m_Player;
- CMediaPlayConfig m_stPlayConfig;
- bool m_bisplaying;
- public:
- libmediaplayer_impl(CMediaHostApi* pHostApi) {
- m_pHostApi = pHostApi;
- m_Player = new CMediaPlayer(pHostApi);
- if (NULL == m_Player) {
- pHostApi->Debug(MEDIA_LOG_DEBUG, "new MediaPlayer failed!");
- }
- memset(&m_stPlayConfig, 0, sizeof(CMediaPlayConfig));
- m_bisplaying = false;
- }
- ~libmediaplayer_impl()
- {
- m_pHostApi = NULL;
- delete m_Player;
- m_Player = NULL;
- m_bisplaying = false;
- }
- bool isStop() { return !m_bisplaying; }
- static void __cb_play_finished(void* user_data)
- {
- libmediaplayer_impl* pthis = static_cast<libmediaplayer_impl*>(user_data);
- if (NULL != pthis){
- pthis->PlayMediaFinished();
- pthis->m_bisplaying = false;
- }
- }
- int StartPlayVideo(const char* pVideoDir, const char* pNamePrefix = NULL, int nVideoCount = 1)
- {
- int iRet = -1;
- if (NULL == pVideoDir || NULL == pNamePrefix){
- return iRet;
- }
- play_media_callback_t cb;
- cb.cb_play_media_finished = &__cb_play_finished;
- cb.user_data = this;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "audio root path is %s.", pVideoDir);
- char strVideoName[MAX_PATH] = { 0 };
- snprintf(strVideoName, MAX_PATH, "%s/%s", pVideoDir, pNamePrefix);
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "video full path is %s.", strVideoName);
- if (!IsFileExist(strVideoName)) {
- return iRet;
- }
- rvc_media_player_param_t t_param = { 0 };
- t_param.p_input_file = strVideoName;
- t_param.cb = &cb;
- t_param.eType = eVideo_Type;
- t_param.eWindType = eVideoSize_Type;
- t_param.udisplaycx = SDL_WINDOWPOS_UNDEFINED;
- t_param.udisplaycy = SDL_WINDOWPOS_UNDEFINED;
- memcpy(t_param.strPlayLists[0], strVideoName, strlen(strVideoName));
- t_param.uFilesCount = 1;
- if (0 == m_Player->Init(&t_param))
- {
- m_bisplaying = true;
- m_Player->StartMediaPlay();
- iRet = 0;
- }
- return iRet;
- }
- int GetLocalAudioPlayingParams(rvc_media_player_param_t* pParam, const char* pAudioNames)
- {
- int iRet = -1;
- if (NULL == pParam || NULL == pAudioNames) {
- return iRet;
- }
-
- pParam->eType = eAudio_Type;
- pParam->eWindType = eVideoSize_Type;
- // 切分音频文件名
- size_t uLen = strlen(pAudioNames);
- char* Tmp = new char[uLen + 1];
- memset(Tmp, 0, uLen + 1);
- memcpy(Tmp, pAudioNames, uLen);
- char* Result[MAX_FILECOUNT] = { NULL };
- CStringSplit(Tmp, Result, "|");
- int FileCount = 0;
- char** pStr = Result;
- while (*pStr != NULL) {
- ++pStr;
- ++FileCount;
- }
- m_stPlayConfig.bPrimMonitor = true;
- m_stPlayConfig.nFileCnt = FileCount;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "wmp pAudioNames = %s!", pAudioNames);
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "wmp config.nFileCnt = %d!", FileCount);
- size_t uValidCount = 0;
- for (int i = 0; i < FileCount && i < MAX_FILECOUNT; i++) {
- char strFileName[MAX_PATH] = { 0 };
- snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, Result[i]);
- if (IsFileExist(strFileName)) {
- memcpy(pParam->strPlayLists[i], strFileName, strlen(strFileName));
- uValidCount++;
- }
- else {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "File %s is not exist.", strFileName);
- continue;
- }
- }
- delete[] Tmp;
- Tmp = NULL;
- pParam->uFilesCount = uValidCount;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "pParam uFilesCount = %d", pParam->uFilesCount);
- if (uValidCount > 0) {
- iRet = 0;
- }
-
- return iRet;
- }
- int StartPlayLocalAudio(const char* pAudioNames)
- {
- int iRet = -1;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "StartPlayLocalAudio %s.", pAudioNames);
- m_stPlayConfig.eMode = LOCALAUDIO;
- iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
- m_stPlayConfig.eMode = LOCALAUDIO;
- if (0 != iRet){
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration failed while play local audio!");
- return iRet;
- }
- else{
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration succeeded while play local audio!");
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
- }
- rvc_media_player_param_t t_param = { 0 };
- if (0 != GetLocalAudioPlayingParams(&t_param, pAudioNames)){
- return iRet;
- }
- play_media_callback_t cb;
- cb.cb_play_media_finished = &__cb_play_finished;
- cb.user_data = this;
- t_param.cb = &cb;
- if (0 == m_Player->Init(&t_param)){
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init success!");
- m_bisplaying = true;
- m_Player->StartMediaPlay();
- iRet = 0;
- }
- else{
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init failed!");
- }
- return iRet;
- }
- int GetLocalVideoPlayingParams(rvc_media_player_param_t* pParam)
- {
- int iRet = -1;
- if (NULL == pParam) {
- return iRet;
- }
- pParam->eType = eVideo_Type;
- pParam->eWindType = eFullScreen_Type;
- size_t uValidCount = 0;
- for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
- char strFileName[MAX_PATH] = { 0 };
- snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
- if (IsFileExist(strFileName)) {
- memcpy(pParam->strPlayLists[i], strFileName, strlen(strFileName));
- uValidCount++;
- }
- else {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "File %s is not exist.", strFileName);
- continue;
- }
- }
- pParam->uFilesCount = uValidCount;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "pParam uFilesCount = %d", pParam->uFilesCount);
- if (uValidCount > 0){
- iRet = 0;
- }
- return iRet;
- }
- int StartPlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- int iRet = -1;
- m_stPlayConfig.eMode = LOCALVIDEO;
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "nCfgInx=%d, nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
- iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
- m_stPlayConfig.eMode = LOCALVIDEO;
- m_stPlayConfig.nWndX = nWndX;
- m_stPlayConfig.nWndY = nWndY;
- m_stPlayConfig.nWndWidth = nWndWidth;
- m_stPlayConfig.nWndHeight = nWndHeight;
- if (0 != iRet){
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration failed while play local video!");
- return iRet;
- }
- else{
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration succeeded while play local video!");
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s,m_stPlayConfig.nFileCnt:%d", m_stPlayConfig.strRootPath, m_stPlayConfig.nFileCnt);
- }
- // 判断当前时间是否允许播放
- struct tm* ptm = NULL;
- time_t t = time(NULL);
- ptm = localtime(&t);
- char strNow[TIME_LEN] = { 0 };
- sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
- if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
- {
- //m_pHostApi->Debug("Now is %s, video play start time is %s, video play stop time is %s, play video rejected!", strNow, m_stPlayConfig.strVideoRunTime_S, m_stPlayConfig.strVideoRunTime_E);
- iRet = 0;
- return iRet;
- }
- rvc_media_player_param_t t_param = {0};
- if (0 != GetLocalVideoPlayingParams(&t_param)) {
- return iRet;
- }
- t_param.idisplaycx = SDL_WINDOWPOS_UNDEFINED;
- t_param.idisplaycy = SDL_WINDOWPOS_UNDEFINED;
- t_param.bvicemonitor = true;
- play_media_callback_t cb;
- cb.cb_play_media_finished = &__cb_play_finished;
- cb.user_data = this;
- t_param.cb = &cb;
- if (0 == m_Player->Init(&t_param)){
- m_bisplaying = true;
- m_Player->StartMediaPlay();
- iRet = 0;
- }
- else{
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "player init failed!");
- }
- return iRet;
- }
- bool checkIsPlay()
- {
- return m_bisplaying;
- }
- bool checkIsStop()
- {
- return !m_bisplaying;
- }
- int StartPlayMedia(CMediaPlayConfig& config)
- {
- int iRet = -1;
- memcpy(&m_stPlayConfig, &config, sizeof(CMediaPlayConfig));
- // 判断当前时间是否允许播放
- struct tm* ptm = NULL;
- time_t t = time(NULL);
- ptm = localtime(&t);
- char strNow[TIME_LEN] = { 0 };
- sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
- if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
- {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Now is %s, video play start time is %s, video play stop time is %s, play video rejected!", strNow, m_stPlayConfig.strVideoRunTime_S, m_stPlayConfig.strVideoRunTime_E);
- iRet = 0;
- return iRet;
- }
- rvc_media_player_param_t t_param = { 0 };
- //if (0 != GetLocalVideoPlayingParams(&t_param)) {
- // return iRet;
- //}
- t_param.eType = eVideo_Type;
- t_param.eWindType = eFullScreen_Type;
- t_param.bvicemonitor = true;
- char strFileName[MAX_PATH] = { 0 };
- snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
- if (IsFileExist(strFileName)) {
- memcpy(t_param.strPlayLists[0], strFileName, strlen(strFileName));
- t_param.uFilesCount = 1;
- }
- else {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG,"File %s is not exist.", strFileName);
- t_param.uFilesCount = 0;
- }
-
- m_pHostApi->Debug(MEDIA_LOG_DEBUG,"pParam uFilesCount = %d", t_param.uFilesCount);
- play_media_callback_t cb;
- cb.cb_play_media_finished = &__cb_play_finished;
- cb.user_data = this;
- t_param.cb = &cb;
- m_Player->SetVolume(config.nVolume);
- if (0 == m_Player->Init(&t_param)) {
- m_bisplaying = true;
- m_Player->StartMediaPlay();
- iRet = 0;
- }
- else {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init failed!");
- }
- return iRet;
- }
- bool StopPlay()
- {
- if (m_Player->GetPlayingFlag())
- {
- m_Player->StopMediaPlay();
- }
-
- return true;
- }
- void SetVolume(int nVolume)
- {
- //if (m_Player->GetPlayingFlag())
- {
- m_Player->SetVolume(nVolume);
- }
- }
- void StartPlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char* pVideoDir, const char* pNamePrefix = NULL, int nVideoCount = 1)
- {
- }
- void PlayMediaFinished()
- {
- m_pHostApi->Debug(MEDIA_LOG_DEBUG, "PlayMediaFinished!");
- m_pHostApi->AudioPlayFinished();
- }
- CMediaHostApi* GetHostApi()
- {
- return m_pHostApi;
- }
- bool IsFileExist(const char* pFilePath)
- {
- bool bRet = false;
- if (NULL != pFilePath){
- if (0 == access(pFilePath, F_OK)) {
- bRet = true;
- }
- }
- return bRet;
- }
- // 按分隔符分隔字符串
- void CStringSplit(char* str, char** result, const char* del)
- {
- char* p = strtok(str, del);
- while (p != NULL)
- {
- *result++ = p;
- p = strtok(NULL, del);
- }
- }
- };
- Clibmediaplayer::Clibmediaplayer(CMediaHostApi* pHostApi)
- {
- m_pImpl = new libmediaplayer_impl(pHostApi);
- return;
- }
- Clibmediaplayer::~Clibmediaplayer()
- {
- delete m_pImpl;
- m_pImpl = NULL;
- }
- int Clibmediaplayer::PlayVideo(const char* pVideoDir, const char* pNamePrefix, int nVideoCount)
- {
- return m_pImpl->StartPlayVideo(pVideoDir, pNamePrefix, nVideoCount);
- }
- int Clibmediaplayer::PlayLocalAudio(const char* pAudioNames)
- {
- m_pImpl->GetHostApi()->Debug(MEDIA_LOG_DEBUG, "Clibmediaplayer StartPlayLocalAudio");
- return m_pImpl->StartPlayLocalAudio(pAudioNames);
- }
- int Clibmediaplayer::PlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
- {
- return m_pImpl->StartPlayLocalVideo(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
- }
- bool Clibmediaplayer::checkIsPlay()
- {
- return m_pImpl->checkIsPlay();
- }
- bool Clibmediaplayer::checkIsStop()
- {
- return m_pImpl->isStop();
- }
- void Clibmediaplayer::PlayMedia(CMediaPlayConfig& config)
- {
- m_pImpl->StartPlayMedia(config);
- }
- void Clibmediaplayer::Close()
- {
- m_pImpl->StopPlay();
- }
- void Clibmediaplayer::SetVolume(int nVolume)
- {
- m_pImpl->SetVolume(nVolume);
- }
- void Clibmediaplayer::PlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char* pVideoDir, const char* pNamePrefix, int nVideoCount)
- {
- m_pImpl->StartPlaySalesRecordVideo(nWndX, nWndY, nWndWidth, nWndHeight, pVideoDir, pNamePrefix, nVideoCount);
- }
|