libmediaplayer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #include "libmediaplayer.h"
  2. #include "player.h"
  3. #include <unistd.h>
  4. class libmediaplayer_impl
  5. {
  6. private:
  7. CMediaHostApi* m_pHostApi;
  8. CMediaPlayer* m_Player;
  9. CMediaPlayConfig m_stPlayConfig;
  10. bool m_bisplaying;
  11. public:
  12. libmediaplayer_impl(CMediaHostApi* pHostApi) {
  13. m_pHostApi = pHostApi;
  14. m_Player = new CMediaPlayer(pHostApi);
  15. if (NULL == m_Player) {
  16. pHostApi->Debug(MEDIA_LOG_DEBUG, "new MediaPlayer failed!");
  17. }
  18. memset(&m_stPlayConfig, 0, sizeof(CMediaPlayConfig));
  19. m_bisplaying = false;
  20. }
  21. ~libmediaplayer_impl()
  22. {
  23. m_pHostApi = NULL;
  24. delete m_Player;
  25. m_Player = NULL;
  26. m_bisplaying = false;
  27. }
  28. bool isStop() { return !m_bisplaying; }
  29. static void __cb_play_finished(void* user_data)
  30. {
  31. libmediaplayer_impl* pthis = static_cast<libmediaplayer_impl*>(user_data);
  32. if (NULL != pthis){
  33. pthis->PlayMediaFinished();
  34. pthis->m_bisplaying = false;
  35. }
  36. }
  37. int StartPlayVideo(const char* pVideoDir, const char* pNamePrefix = NULL, int nVideoCount = 1)
  38. {
  39. int iRet = -1;
  40. if (NULL == pVideoDir || NULL == pNamePrefix){
  41. return iRet;
  42. }
  43. play_media_callback_t cb;
  44. cb.cb_play_media_finished = &__cb_play_finished;
  45. cb.user_data = this;
  46. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "audio root path is %s.", pVideoDir);
  47. char strVideoName[MAX_PATH] = { 0 };
  48. snprintf(strVideoName, MAX_PATH, "%s/%s", pVideoDir, pNamePrefix);
  49. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "video full path is %s.", strVideoName);
  50. if (!IsFileExist(strVideoName)) {
  51. return iRet;
  52. }
  53. rvc_media_player_param_t t_param = { 0 };
  54. t_param.p_input_file = strVideoName;
  55. t_param.cb = &cb;
  56. t_param.eType = eVideo_Type;
  57. t_param.eWindType = eVideoSize_Type;
  58. t_param.udisplaycx = SDL_WINDOWPOS_UNDEFINED;
  59. t_param.udisplaycy = SDL_WINDOWPOS_UNDEFINED;
  60. memcpy(t_param.strPlayLists[0], strVideoName, strlen(strVideoName));
  61. t_param.uFilesCount = 1;
  62. if (0 == m_Player->Init(&t_param))
  63. {
  64. m_bisplaying = true;
  65. m_Player->StartMediaPlay();
  66. iRet = 0;
  67. }
  68. return iRet;
  69. }
  70. int GetLocalAudioPlayingParams(rvc_media_player_param_t* pParam, const char* pAudioNames)
  71. {
  72. int iRet = -1;
  73. if (NULL == pParam || NULL == pAudioNames) {
  74. return iRet;
  75. }
  76. pParam->eType = eAudio_Type;
  77. pParam->eWindType = eVideoSize_Type;
  78. // 切分音频文件名
  79. size_t uLen = strlen(pAudioNames);
  80. char* Tmp = new char[uLen + 1];
  81. memset(Tmp, 0, uLen + 1);
  82. memcpy(Tmp, pAudioNames, uLen);
  83. char* Result[MAX_FILECOUNT] = { NULL };
  84. CStringSplit(Tmp, Result, "|");
  85. int FileCount = 0;
  86. char** pStr = Result;
  87. while (*pStr != NULL) {
  88. ++pStr;
  89. ++FileCount;
  90. }
  91. m_stPlayConfig.bPrimMonitor = true;
  92. m_stPlayConfig.nFileCnt = FileCount;
  93. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "wmp pAudioNames = %s!", pAudioNames);
  94. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "wmp config.nFileCnt = %d!", FileCount);
  95. size_t uValidCount = 0;
  96. for (int i = 0; i < FileCount && i < MAX_FILECOUNT; i++) {
  97. char strFileName[MAX_PATH] = { 0 };
  98. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, Result[i]);
  99. if (IsFileExist(strFileName)) {
  100. memcpy(pParam->strPlayLists[i], strFileName, strlen(strFileName));
  101. uValidCount++;
  102. }
  103. else {
  104. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "File %s is not exist.", strFileName);
  105. continue;
  106. }
  107. }
  108. delete[] Tmp;
  109. Tmp = NULL;
  110. pParam->uFilesCount = uValidCount;
  111. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "pParam uFilesCount = %d", pParam->uFilesCount);
  112. if (uValidCount > 0) {
  113. iRet = 0;
  114. }
  115. return iRet;
  116. }
  117. int StartPlayLocalAudio(const char* pAudioNames)
  118. {
  119. int iRet = -1;
  120. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "StartPlayLocalAudio %s.", pAudioNames);
  121. m_stPlayConfig.eMode = LOCALAUDIO;
  122. iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig);
  123. m_stPlayConfig.eMode = LOCALAUDIO;
  124. if (0 != iRet){
  125. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration failed while play local audio!");
  126. return iRet;
  127. }
  128. else{
  129. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration succeeded while play local audio!");
  130. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s", m_stPlayConfig.strRootPath);
  131. }
  132. rvc_media_player_param_t t_param = { 0 };
  133. if (0 != GetLocalAudioPlayingParams(&t_param, pAudioNames)){
  134. return iRet;
  135. }
  136. play_media_callback_t cb;
  137. cb.cb_play_media_finished = &__cb_play_finished;
  138. cb.user_data = this;
  139. t_param.cb = &cb;
  140. if (0 == m_Player->Init(&t_param)){
  141. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init success!");
  142. m_bisplaying = true;
  143. m_Player->StartMediaPlay();
  144. iRet = 0;
  145. }
  146. else{
  147. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init failed!");
  148. }
  149. return iRet;
  150. }
  151. int GetLocalVideoPlayingParams(rvc_media_player_param_t* pParam)
  152. {
  153. int iRet = -1;
  154. if (NULL == pParam) {
  155. return iRet;
  156. }
  157. pParam->eType = eVideo_Type;
  158. pParam->eWindType = eFullScreen_Type;
  159. size_t uValidCount = 0;
  160. for (int i = 0; i < m_stPlayConfig.nFileCnt && i < MAX_FILECOUNT; i++) {
  161. char strFileName[MAX_PATH] = { 0 };
  162. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[i]);
  163. if (IsFileExist(strFileName)) {
  164. memcpy(pParam->strPlayLists[i], strFileName, strlen(strFileName));
  165. uValidCount++;
  166. }
  167. else {
  168. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "File %s is not exist.", strFileName);
  169. continue;
  170. }
  171. }
  172. pParam->uFilesCount = uValidCount;
  173. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "pParam uFilesCount = %d", pParam->uFilesCount);
  174. if (uValidCount > 0){
  175. iRet = 0;
  176. }
  177. return iRet;
  178. }
  179. int StartPlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  180. {
  181. int iRet = -1;
  182. m_stPlayConfig.eMode = LOCALVIDEO;
  183. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "nCfgInx=%d, nWndX=%d, nWndY=%d, nWndWidth=%d, nWndHeight=%d!", nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  184. iRet = m_pHostApi->LoadPlayConfig(m_stPlayConfig, nCfgInx);
  185. m_stPlayConfig.eMode = LOCALVIDEO;
  186. m_stPlayConfig.nWndX = nWndX;
  187. m_stPlayConfig.nWndY = nWndY;
  188. m_stPlayConfig.nWndWidth = nWndWidth;
  189. m_stPlayConfig.nWndHeight = nWndHeight;
  190. if (0 != iRet){
  191. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration failed while play local video!");
  192. return iRet;
  193. }
  194. else{
  195. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Load WmpConfiguration succeeded while play local video!");
  196. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "m_stPlayConfig.strRootPath: %s,m_stPlayConfig.nFileCnt:%d", m_stPlayConfig.strRootPath, m_stPlayConfig.nFileCnt);
  197. }
  198. // 判断当前时间是否允许播放
  199. struct tm* ptm = NULL;
  200. time_t t = time(NULL);
  201. ptm = localtime(&t);
  202. char strNow[TIME_LEN] = { 0 };
  203. sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  204. if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
  205. {
  206. //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);
  207. iRet = 0;
  208. return iRet;
  209. }
  210. rvc_media_player_param_t t_param = {0};
  211. if (0 != GetLocalVideoPlayingParams(&t_param)) {
  212. return iRet;
  213. }
  214. t_param.idisplaycx = SDL_WINDOWPOS_UNDEFINED;
  215. t_param.idisplaycy = SDL_WINDOWPOS_UNDEFINED;
  216. t_param.bvicemonitor = true;
  217. play_media_callback_t cb;
  218. cb.cb_play_media_finished = &__cb_play_finished;
  219. cb.user_data = this;
  220. t_param.cb = &cb;
  221. if (0 == m_Player->Init(&t_param)){
  222. m_bisplaying = true;
  223. m_Player->StartMediaPlay();
  224. iRet = 0;
  225. }
  226. else{
  227. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "player init failed!");
  228. }
  229. return iRet;
  230. }
  231. bool checkIsPlay()
  232. {
  233. return m_bisplaying;
  234. }
  235. bool checkIsStop()
  236. {
  237. return !m_bisplaying;
  238. }
  239. int StartPlayMedia(CMediaPlayConfig& config)
  240. {
  241. int iRet = -1;
  242. memcpy(&m_stPlayConfig, &config, sizeof(CMediaPlayConfig));
  243. // 判断当前时间是否允许播放
  244. struct tm* ptm = NULL;
  245. time_t t = time(NULL);
  246. ptm = localtime(&t);
  247. char strNow[TIME_LEN] = { 0 };
  248. sprintf(strNow, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  249. if (strcmp(strNow, m_stPlayConfig.strVideoRunTime_S) < 0 || strcmp(strNow, m_stPlayConfig.strVideoRunTime_E) >= 0)
  250. {
  251. 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);
  252. iRet = 0;
  253. return iRet;
  254. }
  255. rvc_media_player_param_t t_param = { 0 };
  256. //if (0 != GetLocalVideoPlayingParams(&t_param)) {
  257. // return iRet;
  258. //}
  259. t_param.eType = eVideo_Type;
  260. t_param.eWindType = eFullScreen_Type;
  261. t_param.bvicemonitor = true;
  262. char strFileName[MAX_PATH] = { 0 };
  263. snprintf(strFileName, MAX_PATH, "%s%s", m_stPlayConfig.strRootPath, m_stPlayConfig.strFileNames[0]);
  264. if (IsFileExist(strFileName)) {
  265. memcpy(t_param.strPlayLists[0], strFileName, strlen(strFileName));
  266. t_param.uFilesCount = 1;
  267. }
  268. else {
  269. m_pHostApi->Debug(MEDIA_LOG_DEBUG,"File %s is not exist.", strFileName);
  270. t_param.uFilesCount = 0;
  271. }
  272. m_pHostApi->Debug(MEDIA_LOG_DEBUG,"pParam uFilesCount = %d", t_param.uFilesCount);
  273. play_media_callback_t cb;
  274. cb.cb_play_media_finished = &__cb_play_finished;
  275. cb.user_data = this;
  276. t_param.cb = &cb;
  277. m_Player->SetVolume(config.nVolume);
  278. if (0 == m_Player->Init(&t_param)) {
  279. m_bisplaying = true;
  280. m_Player->StartMediaPlay();
  281. iRet = 0;
  282. }
  283. else {
  284. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "Player Init failed!");
  285. }
  286. return iRet;
  287. }
  288. bool StopPlay()
  289. {
  290. if (m_Player->GetPlayingFlag())
  291. {
  292. m_Player->StopMediaPlay();
  293. }
  294. return true;
  295. }
  296. void SetVolume(int nVolume)
  297. {
  298. //if (m_Player->GetPlayingFlag())
  299. {
  300. m_Player->SetVolume(nVolume);
  301. }
  302. }
  303. void StartPlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char* pVideoDir, const char* pNamePrefix = NULL, int nVideoCount = 1)
  304. {
  305. }
  306. void PlayMediaFinished()
  307. {
  308. m_pHostApi->Debug(MEDIA_LOG_DEBUG, "PlayMediaFinished!");
  309. m_pHostApi->AudioPlayFinished();
  310. }
  311. CMediaHostApi* GetHostApi()
  312. {
  313. return m_pHostApi;
  314. }
  315. bool IsFileExist(const char* pFilePath)
  316. {
  317. bool bRet = false;
  318. if (NULL != pFilePath){
  319. if (0 == access(pFilePath, F_OK)) {
  320. bRet = true;
  321. }
  322. }
  323. return bRet;
  324. }
  325. // 按分隔符分隔字符串
  326. void CStringSplit(char* str, char** result, const char* del)
  327. {
  328. char* p = strtok(str, del);
  329. while (p != NULL)
  330. {
  331. *result++ = p;
  332. p = strtok(NULL, del);
  333. }
  334. }
  335. };
  336. Clibmediaplayer::Clibmediaplayer(CMediaHostApi* pHostApi)
  337. {
  338. m_pImpl = new libmediaplayer_impl(pHostApi);
  339. return;
  340. }
  341. Clibmediaplayer::~Clibmediaplayer()
  342. {
  343. delete m_pImpl;
  344. m_pImpl = NULL;
  345. }
  346. int Clibmediaplayer::PlayVideo(const char* pVideoDir, const char* pNamePrefix, int nVideoCount)
  347. {
  348. return m_pImpl->StartPlayVideo(pVideoDir, pNamePrefix, nVideoCount);
  349. }
  350. int Clibmediaplayer::PlayLocalAudio(const char* pAudioNames)
  351. {
  352. m_pImpl->GetHostApi()->Debug(MEDIA_LOG_DEBUG, "Clibmediaplayer StartPlayLocalAudio");
  353. return m_pImpl->StartPlayLocalAudio(pAudioNames);
  354. }
  355. int Clibmediaplayer::PlayLocalVideo(int nCfgInx, int nWndX, int nWndY, int nWndWidth, int nWndHeight)
  356. {
  357. return m_pImpl->StartPlayLocalVideo(nCfgInx, nWndX, nWndY, nWndWidth, nWndHeight);
  358. }
  359. bool Clibmediaplayer::checkIsPlay()
  360. {
  361. return m_pImpl->checkIsPlay();
  362. }
  363. bool Clibmediaplayer::checkIsStop()
  364. {
  365. return m_pImpl->isStop();
  366. }
  367. void Clibmediaplayer::PlayMedia(CMediaPlayConfig& config)
  368. {
  369. m_pImpl->StartPlayMedia(config);
  370. }
  371. void Clibmediaplayer::Close()
  372. {
  373. m_pImpl->StopPlay();
  374. }
  375. void Clibmediaplayer::SetVolume(int nVolume)
  376. {
  377. m_pImpl->SetVolume(nVolume);
  378. }
  379. void Clibmediaplayer::PlaySalesRecordVideo(int nWndX, int nWndY, int nWndWidth, int nWndHeight, const char* pVideoDir, const char* pNamePrefix, int nVideoCount)
  380. {
  381. m_pImpl->StartPlaySalesRecordVideo(nWndX, nWndY, nWndWidth, nWndHeight, pVideoDir, pNamePrefix, nVideoCount);
  382. }