player.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <assert.h>
  4. #include "player.h"
  5. #include "frame.h"
  6. #include "packet.h"
  7. #include "demux.h"
  8. #include "video.h"
  9. #include "audio.h"
  10. static int initialized = 0;
  11. // 返回值:返回上一帧的pts更新值(上一帧pts+流逝的时间)
  12. double get_clock(play_clock_t *c)
  13. {
  14. if (*c->queue_serial != c->serial)
  15. {
  16. return NAN;
  17. }
  18. if (c->paused)
  19. {
  20. return c->pts;
  21. }
  22. else
  23. {
  24. double time = av_gettime_relative() / 1000000.0;
  25. double ret = c->pts_drift + time; // 展开得: c->pts + (time - c->last_updated)
  26. return ret;
  27. }
  28. }
  29. void set_clock_at(play_clock_t *c, double pts, int serial, double time)
  30. {
  31. c->pts = pts;
  32. c->last_updated = time;
  33. c->pts_drift = c->pts - time;
  34. c->serial = serial;
  35. }
  36. void set_clock(play_clock_t *c, double pts, int serial)
  37. {
  38. double time = av_gettime_relative() / 1000000.0;
  39. set_clock_at(c, pts, serial, time);
  40. }
  41. static void set_clock_speed(play_clock_t *c, double speed)
  42. {
  43. set_clock(c, get_clock(c), c->serial);
  44. c->speed = speed;
  45. }
  46. void init_clock(play_clock_t *c, int *queue_serial)
  47. {
  48. c->speed = 1.0;
  49. c->paused = 0;
  50. c->queue_serial = queue_serial;
  51. set_clock(c, NAN, -1);
  52. }
  53. static void sync_play_clock_to_slave(play_clock_t *c, play_clock_t *slave)
  54. {
  55. double clock = get_clock(c);
  56. double slave_clock = get_clock(slave);
  57. if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
  58. set_clock(c, slave_clock, slave->serial);
  59. }
  60. /* pause or resume the video */
  61. static void stream_toggle_pause(player_stat_t *is)
  62. {
  63. if (is->paused)
  64. {
  65. // 这里表示当前是暂停状态,将切换到继续播放状态。在继续播放之前,先将暂停期间流逝的时间加到frame_timer中
  66. is->frame_timer += av_gettime_relative() / 1000000.0 - is->video_clk.last_updated;
  67. set_clock(&is->video_clk, get_clock(&is->video_clk), is->video_clk.serial);
  68. }
  69. is->paused = is->audio_clk.paused = is->video_clk.paused = !is->paused;
  70. }
  71. static void toggle_pause(player_stat_t *is)
  72. {
  73. stream_toggle_pause(is);
  74. is->step = 0;
  75. }
  76. static void toggle_full_screen(player_stat_t* is)
  77. {
  78. SDL_SetWindowFullscreen(is->sdl_video.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
  79. }
  80. CMediaPlayer::CMediaPlayer(CMediaHostApi* pHostApi)
  81. {
  82. m_hostapi = pHostApi;
  83. m_player_stat = NULL;
  84. m_uvolume = SDL_MIX_MAXVOLUME/2;
  85. m_bplaying = false;
  86. m_piconpath = NULL;
  87. m_paudiodev = NULL;
  88. if (NULL != m_hostapi){
  89. char str_iconpath[MAX_PATH] = {0};
  90. if (0 == pHostApi->GetMediaPlayerIcoPath(str_iconpath, MAX_PATH)){
  91. m_piconpath = av_strdup(str_iconpath);
  92. }
  93. char str_audiodev[MAX_PATH] = { 0 };
  94. if (0 == pHostApi->GetAudioOutDevName(str_audiodev, MAX_PATH)) {
  95. m_paudiodev = av_strdup(str_audiodev);
  96. }
  97. else {
  98. m_paudiodev = NULL;
  99. }
  100. }
  101. else {
  102. m_hostapi->Debug(MEDIA_LOG_DEBUG, "new CMediaPlayer failed!");
  103. }
  104. if (initialized){
  105. ++initialized;
  106. }
  107. else {
  108. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)){
  109. m_hostapi->Debug(MEDIA_LOG_ERROR, "Could not initialize SDL - %s", SDL_GetError());
  110. m_hostapi->Debug(MEDIA_LOG_ERROR, "(Did you set the DISPLAY variable?)");
  111. }
  112. else {
  113. initialized++;
  114. m_hostapi->Debug(MEDIA_LOG_DEBUG, "initialize SDL success");
  115. }
  116. }
  117. }
  118. CMediaPlayer::~CMediaPlayer()
  119. {
  120. if (NULL != m_player_stat) {
  121. UnInitialize_Player_Stat();
  122. }
  123. if (NULL != m_piconpath) {
  124. av_free(m_piconpath);
  125. m_piconpath = NULL;
  126. }
  127. if (NULL != m_paudiodev){
  128. av_free(m_paudiodev);
  129. m_paudiodev = NULL;
  130. }
  131. if (--initialized == 0){
  132. SDL_Quit();
  133. m_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_Quit");
  134. }
  135. }
  136. static int audio_volume_callback(int* audiovolume, void* userdata)
  137. {
  138. CMediaPlayer* player = (CMediaPlayer*)userdata;
  139. *audiovolume = player->GetVolume();
  140. return 0;
  141. }
  142. static int audio_play_finished_callback(void* userdata)
  143. {
  144. CMediaPlayer* player = (CMediaPlayer*)userdata;
  145. if (NULL != player){
  146. player->SetFinishedFlag();
  147. }
  148. return 0;
  149. }
  150. bool CMediaPlayer::SetFinishedFlag()
  151. {
  152. if (NULL != m_player_stat){
  153. m_player_stat->baudio_finished = true;
  154. if (NULL != m_hostapi) {
  155. m_hostapi->Debug(MEDIA_LOG_ERROR, "%s:%d set baudio_finished to true.", __FUNCTION__, __LINE__);
  156. }
  157. }
  158. else {
  159. if (NULL != m_hostapi) {
  160. m_hostapi->Debug(MEDIA_LOG_ERROR, "%s:%d m_player_stat is null and set audio finished flag failed.", __FUNCTION__, __LINE__);
  161. }
  162. }
  163. return true;
  164. }
  165. uint8_t CMediaPlayer::GetVolume()
  166. {
  167. return m_uvolume;
  168. }
  169. int CMediaPlayer::Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player)
  170. {
  171. int iRet = -1;
  172. if (NULL == pMedia_Player) {
  173. return iRet;
  174. }
  175. if (NULL != m_player_stat) {
  176. UnInitialize_Player_Stat();
  177. }
  178. m_player_stat = (player_stat_t*)av_mallocz(sizeof(player_stat_t));
  179. if (NULL == m_player_stat) {
  180. m_hostapi->Debug(MEDIA_LOG_DEBUG, "player_stat_t struct malloc failed!");
  181. return iRet;
  182. }
  183. m_player_stat->baudio_finished = false;
  184. m_player_stat->bvideo_finished = false;
  185. m_player_stat->bread_finished = false;
  186. m_player_stat->rvc_hostapi = m_hostapi;
  187. m_player_stat->prvc_cb = pMedia_Player->cb;
  188. m_player_stat->eMType = pMedia_Player->eType;
  189. m_player_stat->eWindType = pMedia_Player->eWindType;
  190. m_player_stat->bvice_monitor = pMedia_Player->bvicemonitor;
  191. m_player_stat->iDisplayCx = pMedia_Player->idisplaycx;
  192. m_player_stat->iDisplayCy = pMedia_Player->idisplaycy;
  193. m_player_stat->iDisplayWidth = pMedia_Player->idisplaywidth;
  194. m_player_stat->iDisplayHeight = pMedia_Player->idisplayheight;
  195. m_player_stat->on_audio_volume = &audio_volume_callback;
  196. m_player_stat->on_audio_play_finished = &audio_play_finished_callback;
  197. m_player_stat->user_data = this;
  198. if (NULL != m_piconpath){
  199. m_player_stat->piconpath = av_strdup(m_piconpath);
  200. }
  201. if (NULL != m_paudiodev){
  202. m_player_stat->paudiodev = av_strdup(m_paudiodev);
  203. }
  204. m_player_stat->uVolume = m_uvolume;
  205. m_player_stat->uFilesCount = pMedia_Player->uFilesCount;
  206. for (size_t i = 0; i < pMedia_Player->uFilesCount; i++){
  207. memcpy(m_player_stat->strPlayLists[i], pMedia_Player->strPlayLists[i], strlen(pMedia_Player->strPlayLists[i]));
  208. }
  209. if (0 == pMedia_Player->uFilesCount || NULL == m_player_stat->rvc_hostapi || NULL == m_player_stat->prvc_cb || NULL == m_player_stat->piconpath) {
  210. UnInitialize_Player_Stat();
  211. return iRet;
  212. }
  213. else{
  214. iRet = 0;
  215. }
  216. return iRet;
  217. }
  218. int CMediaPlayer::UnInitialize_Player_Stat()
  219. {
  220. /* XXX: use a special url_shutdown call to abort parse cleanly */
  221. if (NULL == m_player_stat) {
  222. return -1;
  223. }
  224. packet_queue_abort(&m_player_stat->video_pkt_queue, m_hostapi);
  225. packet_queue_abort(&m_player_stat->audio_pkt_queue, m_hostapi);
  226. frame_queue_signal(&m_player_stat->video_frm_queue);
  227. frame_queue_signal(&m_player_stat->audio_frm_queue);
  228. if (NULL != m_player_stat->audio_decode_tid) {
  229. SDL_WaitThread(m_player_stat->audio_decode_tid, NULL);
  230. m_player_stat->audio_decode_tid = NULL;
  231. m_hostapi->Debug(MEDIA_LOG_DEBUG, "audio decode thread finished.");
  232. }
  233. if (m_player_stat->eMType == eVideo_Type) {
  234. if (NULL != m_player_stat->video_decode_tid) {
  235. SDL_WaitThread(m_player_stat->video_decode_tid, NULL);
  236. m_player_stat->video_decode_tid = NULL;
  237. m_hostapi->Debug(MEDIA_LOG_DEBUG, "video decode thread finished.");
  238. }
  239. if (NULL != m_player_stat->video_playing_tid) {
  240. SDL_WaitThread(m_player_stat->video_playing_tid, NULL);
  241. m_player_stat->video_playing_tid = NULL;
  242. m_hostapi->Debug(MEDIA_LOG_DEBUG, "video playing thread finished.");
  243. }
  244. }
  245. if (NULL != m_player_stat->read_tid){
  246. SDL_WaitThread(m_player_stat->read_tid, NULL);
  247. m_player_stat->read_tid = NULL;
  248. m_hostapi->Debug(MEDIA_LOG_DEBUG, "demux thread finished.");
  249. }
  250. for (int index = 0; index < m_player_stat->uFilesCount; index++) {
  251. if (NULL != m_player_stat->p_fmt_ctx[index]) {
  252. avformat_close_input(&m_player_stat->p_fmt_ctx[index]);
  253. m_player_stat->p_fmt_ctx[index] = NULL;
  254. }
  255. if (NULL != m_player_stat->p_acodec_ctx[index]) {
  256. avcodec_free_context(&m_player_stat->p_acodec_ctx[index]);
  257. }
  258. if (NULL != m_player_stat->p_vcodec_ctx[index]) {
  259. avcodec_free_context(&m_player_stat->p_vcodec_ctx[index]);
  260. }
  261. }
  262. SDL_DestroyCond(m_player_stat->continue_read_thread);
  263. if (m_player_stat->eMType == eVideo_Type) {
  264. for (int index = 0; index < m_player_stat->uFilesCount; index++) {
  265. if (NULL != m_player_stat->img_convert_ctx[index]) {
  266. sws_freeContext(m_player_stat->img_convert_ctx[index]);
  267. m_player_stat->img_convert_ctx[index] = NULL;
  268. }
  269. if (NULL != m_player_stat->p_frm_yuv[index]) {
  270. av_frame_free(&m_player_stat->p_frm_yuv[index]);
  271. av_frame_unref(m_player_stat->p_frm_yuv[index]);
  272. }
  273. if (NULL != m_player_stat->p_video_buffer[index]) {
  274. av_free(m_player_stat->p_video_buffer[index]);
  275. m_player_stat->p_video_buffer[index] = NULL;
  276. }
  277. }
  278. }
  279. packet_queue_destroy(&m_player_stat->video_pkt_queue, m_hostapi);
  280. packet_queue_destroy(&m_player_stat->audio_pkt_queue, m_hostapi);
  281. ///* free all pictures */
  282. frame_queue_destory(&m_player_stat->video_frm_queue);
  283. frame_queue_destory(&m_player_stat->audio_frm_queue);
  284. m_hostapi->Debug(MEDIA_LOG_DEBUG, "av_free player_stat_t.");
  285. swr_free(&m_player_stat->audio_swr_ctx);
  286. if (NULL != m_player_stat->audio_frm_rwr) {
  287. av_free(m_player_stat->audio_frm_rwr);
  288. m_player_stat->audio_frm_rwr = NULL;
  289. }
  290. if (m_player_stat->piconpath){
  291. av_free(m_player_stat->piconpath);
  292. m_player_stat->piconpath = NULL;
  293. }
  294. if (m_player_stat->paudiodev) {
  295. av_free(m_player_stat->paudiodev);
  296. m_player_stat->paudiodev = NULL;
  297. }
  298. if (m_player_stat->straudiodev) {
  299. av_free(m_player_stat->straudiodev);
  300. m_player_stat->straudiodev = NULL;
  301. }
  302. av_free(m_player_stat);
  303. m_player_stat = NULL;
  304. return 0;
  305. }
  306. int CMediaPlayer::GetViceVideoDisplayInfo(int* icx, int* icy, int* iwidth, int* iheight)
  307. {
  308. int iRet = -1;
  309. size_t uCount = SDL_GetNumVideoDisplays();
  310. m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays Number is %d.", uCount);
  311. SDL_DisplayMode dispalymode[RVC_MAX_DISPLAYNUM] = { 0 };
  312. for (size_t i = 0; i < uCount && i < RVC_MAX_DISPLAYNUM; i++) {
  313. SDL_GetDesktopDisplayMode(i, &dispalymode[i]);
  314. m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} format = %d", i, dispalymode[i].format);
  315. m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} w = %d", i, dispalymode[i].w);
  316. m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} h = %d", i, dispalymode[i].h);
  317. m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} refresh_rate = %d", i, dispalymode[i].refresh_rate);
  318. }
  319. if (uCount > 1) {
  320. *icx = dispalymode[0].w;
  321. *icy = 0;
  322. *iwidth = dispalymode[1].w;
  323. *iheight = dispalymode[1].h;
  324. iRet = 0;
  325. }
  326. return iRet;
  327. }
  328. int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
  329. {
  330. int iRet = -1;
  331. if (0 != Initialize_Player_Stat(pMedia_Player)){
  332. return iRet;
  333. }
  334. m_hostapi->Debug(MEDIA_LOG_DEBUG, "initialize player stat success.");
  335. /* start video display */
  336. if (frame_queue_init(&m_player_stat->video_frm_queue, &m_player_stat->video_pkt_queue, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0 ||
  337. frame_queue_init(&m_player_stat->audio_frm_queue, &m_player_stat->audio_pkt_queue, SAMPLE_QUEUE_SIZE, 1) < 0)
  338. {
  339. m_hostapi->Debug(MEDIA_LOG_DEBUG, "media frame queue init failed!");
  340. UnInitialize_Player_Stat();
  341. return iRet;
  342. }
  343. else{
  344. m_hostapi->Debug(MEDIA_LOG_DEBUG, "frame queue init success!");
  345. }
  346. /* init packet queue */
  347. if (packet_queue_init(&m_player_stat->video_pkt_queue, m_hostapi) < 0 ||
  348. packet_queue_init(&m_player_stat->audio_pkt_queue, m_hostapi) < 0)
  349. {
  350. m_hostapi->Debug(MEDIA_LOG_DEBUG, "media packet queue init failed!");
  351. UnInitialize_Player_Stat();
  352. return iRet;
  353. }
  354. else{
  355. m_hostapi->Debug(MEDIA_LOG_DEBUG, "packet queue init success!");
  356. }
  357. /* put end pkt into packet queue */
  358. AVPacket flush_pkt = {0};
  359. flush_pkt.data = NULL;
  360. packet_queue_put(&m_player_stat->video_pkt_queue, &flush_pkt, m_hostapi);
  361. packet_queue_put(&m_player_stat->audio_pkt_queue, &flush_pkt, m_hostapi);
  362. if (!(m_player_stat->continue_read_thread = SDL_CreateCond())){
  363. m_hostapi->Debug(MEDIA_LOG_DEBUG, "SDL_CreateCond(): %s.", SDL_GetError());
  364. UnInitialize_Player_Stat();
  365. return iRet;
  366. }
  367. init_clock(&m_player_stat->video_clk, &m_player_stat->video_pkt_queue.serial);
  368. init_clock(&m_player_stat->audio_clk, &m_player_stat->audio_pkt_queue.serial);
  369. if (m_player_stat->bvice_monitor){
  370. if (0 == GetViceVideoDisplayInfo(&m_player_stat->iDisplayCx, &m_player_stat->iDisplayCy, &m_player_stat->iDisplayWidth, &m_player_stat->iDisplayHeight)){
  371. m_hostapi->Debug(MEDIA_LOG_DEBUG,"display cx is %d, cy is %d, width is %d, height is %d.", m_player_stat->iDisplayCx, m_player_stat->iDisplayCy, m_player_stat->iDisplayWidth, m_player_stat->iDisplayHeight);
  372. }
  373. }
  374. iRet = 0;
  375. return iRet;
  376. }
  377. int CMediaPlayer::SetVolume(uint8_t uVolume)
  378. {
  379. int iRet = -1;
  380. m_uvolume = uVolume * SDL_MIX_MAXVOLUME/100;
  381. m_hostapi->Debug(MEDIA_LOG_DEBUG, "set audio volume(%d) to %d.", uVolume, m_uvolume);
  382. iRet = 0;
  383. return iRet;
  384. }
  385. bool CMediaPlayer::GetPlayingFlag()
  386. {
  387. return m_bplaying;
  388. }
  389. int CMediaPlayer::StartMediaPlay()
  390. {
  391. int iRet = -1;
  392. if (0 == open_demux(m_player_stat)){
  393. m_hostapi->Debug(MEDIA_LOG_DEBUG, "open_demux function call success.");
  394. }
  395. else{
  396. m_hostapi->Debug(MEDIA_LOG_ERROR, "open_demux function call failed.");
  397. av_free(m_player_stat);
  398. m_player_stat = NULL;
  399. iRet = -3;
  400. return iRet;
  401. }
  402. m_hostapi->Debug(MEDIA_LOG_DEBUG, "set playing flag to true.");
  403. m_bplaying = true;
  404. if (eVideo_Type == m_player_stat->eMType) {
  405. open_video(m_player_stat);
  406. }
  407. else{
  408. m_player_stat->bvideo_finished = true;
  409. }
  410. open_audio(m_player_stat);
  411. m_hostapi->Debug(MEDIA_LOG_DEBUG, "after open_audio function.");
  412. while (false == m_player_stat->baudio_finished || false == m_player_stat->bvideo_finished ){
  413. SDL_Delay(100);
  414. }
  415. ExitMediaPlayingThread();
  416. iRet = 0;
  417. return iRet;
  418. }
  419. int CMediaPlayer::StopMediaPlay()
  420. {
  421. int iRet = -1;
  422. m_hostapi->Debug(MEDIA_LOG_DEBUG, "StopMediaPlay called.");
  423. if (NULL == m_player_stat) {
  424. return iRet;
  425. }
  426. m_hostapi->Debug(MEDIA_LOG_ERROR, "%s:%d set m_player_stat media finished flag to true.", __FUNCTION__, __LINE__);
  427. m_player_stat->baudio_finished = true;
  428. m_player_stat->bvideo_finished = true;
  429. if (m_bplaying) {
  430. m_hostapi->Debug(MEDIA_LOG_DEBUG, "user stop audio play set SDL_PauseAudioDevice param to 1.");
  431. if (0 != audio_dev){
  432. SDL_PauseAudioDevice(audio_dev, 1);
  433. }
  434. }
  435. iRet = 0;
  436. return iRet;
  437. }
  438. int CMediaPlayer::ExitMediaPlayingThread()
  439. {
  440. int iRet = -1;
  441. if (NULL == m_player_stat){
  442. return iRet;
  443. }
  444. m_hostapi->Debug(MEDIA_LOG_DEBUG, "enter ExitMediaPlayingThread call.");
  445. if(eVideo_Type == m_player_stat->eMType){
  446. if (m_player_stat->sdl_video.texture) {
  447. SDL_DestroyTexture(m_player_stat->sdl_video.texture);
  448. }
  449. if (m_player_stat->sdl_video.renderer){
  450. SDL_DestroyRenderer(m_player_stat->sdl_video.renderer);
  451. }
  452. if (m_player_stat->sdl_video.window){
  453. SDL_DestroyWindow(m_player_stat->sdl_video.window);
  454. m_hostapi->Debug(MEDIA_LOG_ERROR, "Destroy Window.");
  455. }
  456. }
  457. if (m_player_stat->prvc_cb){
  458. m_hostapi->Debug(MEDIA_LOG_DEBUG, "cb_play_media_finished callback.");
  459. if (eAudio_Type == m_player_stat->eMType) {
  460. m_player_stat->prvc_cb->cb_play_media_finished(m_player_stat->prvc_cb->user_data);
  461. }
  462. }
  463. UnInitialize_Player_Stat();
  464. m_bplaying = false;
  465. iRet = 0;
  466. m_hostapi->Debug(MEDIA_LOG_DEBUG, "Leave ExitMediaPlayingThread call.");
  467. return iRet;
  468. }
  469. int64_t CMediaPlayer::GetMediaPlayingThreadId()
  470. {
  471. int64_t iRet = 0;
  472. if (NULL != m_player_stat)
  473. {
  474. if (NULL != m_player_stat->read_tid)
  475. {
  476. iRet = SDL_GestureID(m_player_stat->read_tid);
  477. }
  478. }
  479. return iRet;
  480. }