libmediaplayer.cpp 12 KB

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