|
@@ -1,14 +1,4 @@
|
|
|
-/*******************************************************************************
|
|
|
- * player.c
|
|
|
- *
|
|
|
- * details:
|
|
|
- * A simple ffmpeg player.
|
|
|
- *
|
|
|
- * refrence:
|
|
|
- * ffplay.c in FFmpeg 4.1 project.
|
|
|
- *******************************************************************************/
|
|
|
-
|
|
|
-#include <stdio.h>
|
|
|
+#include <stdio.h>
|
|
|
#include <stdbool.h>
|
|
|
#include <assert.h>
|
|
|
|
|
@@ -20,8 +10,6 @@
|
|
|
#include "audio.h"
|
|
|
|
|
|
|
|
|
-static int player_deinit(player_stat_t *is);
|
|
|
-
|
|
|
// 返回值:返回上一帧的pts更新值(上一帧pts+流逝的时间)
|
|
|
double get_clock(play_clock_t *c)
|
|
|
{
|
|
@@ -82,81 +70,6 @@ static void sync_play_clock_to_slave(play_clock_t *c, play_clock_t *slave)
|
|
|
set_clock(c, slave_clock, slave->serial);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-static int player_deinit(player_stat_t *is)
|
|
|
-{
|
|
|
- /* XXX: use a special url_shutdown call to abort parse cleanly */
|
|
|
- if (NULL == is){
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- is->abort_request = 1;
|
|
|
-
|
|
|
- SDL_WaitThread(is->read_tid, NULL);
|
|
|
- is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "demux thread finished.");
|
|
|
-
|
|
|
- for (int index = 0; index < is->uFilesCount; index++){
|
|
|
- if (NULL != is->p_fmt_ctx[index]){
|
|
|
- avformat_close_input(&is->p_fmt_ctx[index]);
|
|
|
- }
|
|
|
-
|
|
|
- if (NULL != is->p_acodec_ctx[index]){
|
|
|
- avcodec_free_context(&is->p_acodec_ctx[index]);
|
|
|
- }
|
|
|
-
|
|
|
- if (NULL != is->p_vcodec_ctx[index]) {
|
|
|
- avcodec_free_context(&is->p_vcodec_ctx[index]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- packet_queue_abort(&is->video_pkt_queue, is->rvc_hostapi);
|
|
|
- packet_queue_abort(&is->audio_pkt_queue, is->rvc_hostapi);
|
|
|
-
|
|
|
- frame_queue_signal(&is->video_frm_queue);
|
|
|
- frame_queue_signal(&is->audio_frm_queue);
|
|
|
-
|
|
|
- SDL_DestroyCond(is->continue_read_thread);
|
|
|
-
|
|
|
- if (is->eMType == eVideo_Type) {
|
|
|
- for (int index = 0; index < is->uFilesCount; index++) {
|
|
|
- is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d is->img_convert_ctx[%d] = 0x%0x", __FUNCTION__, __LINE__, index, is->img_convert_ctx[index]);
|
|
|
- if (NULL != is->img_convert_ctx[index]) {
|
|
|
- sws_freeContext(is->img_convert_ctx[index]);
|
|
|
- is->img_convert_ctx[index] = NULL;
|
|
|
- }
|
|
|
- if (NULL != is->p_frm_yuv[index]){
|
|
|
- av_frame_free(&is->p_frm_yuv[index]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (is->sdl_video.texture) {
|
|
|
- SDL_DestroyTexture(is->sdl_video.texture);
|
|
|
- }
|
|
|
-
|
|
|
- //packet_queue_destroy(&is->video_pkt_queue, is->rvc_hostapi);
|
|
|
- //packet_queue_destroy(&is->audio_pkt_queue, is->rvc_hostapi);
|
|
|
-
|
|
|
- ///* free all pictures */
|
|
|
- //frame_queue_destory(&is->video_frm_queue);
|
|
|
- //frame_queue_destory(&is->audio_frm_queue);
|
|
|
-
|
|
|
- is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "av_free player_stat_t.");
|
|
|
-
|
|
|
- swr_free(&is->audio_swr_ctx);
|
|
|
-
|
|
|
- is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d", __FUNCTION__, __LINE__);
|
|
|
- if (NULL != is->audio_frm_rwr) {
|
|
|
- av_free(is->audio_frm_rwr);
|
|
|
- is->audio_frm_rwr = NULL;
|
|
|
- }
|
|
|
-
|
|
|
- av_freep(is);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
/* pause or resume the video */
|
|
|
static void stream_toggle_pause(player_stat_t *is)
|
|
|
{
|
|
@@ -227,8 +140,7 @@ CMediaPlayer::CMediaPlayer(CMediaHostApi* pHostApi)
|
|
|
CMediaPlayer::~CMediaPlayer()
|
|
|
{
|
|
|
if (NULL != m_player_stat) {
|
|
|
- player_deinit(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
}
|
|
|
|
|
|
if (NULL != m_piconpath) {
|
|
@@ -284,7 +196,7 @@ int CMediaPlayer::Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player
|
|
|
}
|
|
|
|
|
|
if (NULL != m_player_stat) {
|
|
|
- player_deinit(m_player_stat);
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
}
|
|
|
|
|
|
m_player_stat = (player_stat_t*)av_mallocz(sizeof(player_stat_t));
|
|
@@ -319,8 +231,7 @@ int CMediaPlayer::Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player
|
|
|
memcpy(m_player_stat->strPlayLists[i], pMedia_Player->strPlayLists[i], strlen(pMedia_Player->strPlayLists[i]));
|
|
|
}
|
|
|
if (0 == pMedia_Player->uFilesCount || NULL == m_player_stat->rvc_hostapi || NULL == m_player_stat->prvc_cb || NULL == m_player_stat->piconpath) {
|
|
|
- av_free(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
return iRet;
|
|
|
}
|
|
|
else{
|
|
@@ -331,6 +242,106 @@ int CMediaPlayer::Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player
|
|
|
}
|
|
|
|
|
|
|
|
|
+int CMediaPlayer::UnInitialize_Player_Stat()
|
|
|
+{
|
|
|
+ /* XXX: use a special url_shutdown call to abort parse cleanly */
|
|
|
+ if (NULL == m_player_stat) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ m_player_stat->abort_request = 1;
|
|
|
+
|
|
|
+ SDL_WaitThread(m_player_stat->read_tid, NULL);
|
|
|
+ m_hostapi->Debug(MEDIA_LOG_DEBUG, "demux thread finished.");
|
|
|
+
|
|
|
+ for (int index = 0; index < m_player_stat->uFilesCount; index++) {
|
|
|
+ if (NULL != m_player_stat->p_fmt_ctx[index]) {
|
|
|
+ avformat_close_input(&m_player_stat->p_fmt_ctx[index]);
|
|
|
+ m_player_stat->p_fmt_ctx[index] = NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (NULL != m_player_stat->p_acodec_ctx[index]) {
|
|
|
+ avcodec_free_context(&m_player_stat->p_acodec_ctx[index]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (NULL != m_player_stat->p_vcodec_ctx[index]) {
|
|
|
+ avcodec_free_context(&m_player_stat->p_vcodec_ctx[index]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ packet_queue_abort(&m_player_stat->video_pkt_queue, m_hostapi);
|
|
|
+ packet_queue_abort(&m_player_stat->audio_pkt_queue, m_hostapi);
|
|
|
+
|
|
|
+ frame_queue_signal(&m_player_stat->video_frm_queue);
|
|
|
+ frame_queue_signal(&m_player_stat->audio_frm_queue);
|
|
|
+
|
|
|
+ SDL_DestroyCond(m_player_stat->continue_read_thread);
|
|
|
+
|
|
|
+ if (NULL != m_player_stat->audio_decode_tid){
|
|
|
+ SDL_WaitThread(m_player_stat->audio_decode_tid, NULL);
|
|
|
+ }
|
|
|
+ if (m_player_stat->eMType == eVideo_Type) {
|
|
|
+ if (NULL != m_player_stat->video_decode_tid){
|
|
|
+ SDL_WaitThread(m_player_stat->video_decode_tid, NULL);
|
|
|
+ m_hostapi->Debug(MEDIA_LOG_DEBUG, "video decode thread finished.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (NULL != m_player_stat->video_playing_tid) {
|
|
|
+ SDL_WaitThread(m_player_stat->video_playing_tid, NULL);
|
|
|
+ m_hostapi->Debug(MEDIA_LOG_DEBUG, "video playing thread finished.");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int index = 0; index < m_player_stat->uFilesCount; index++) {
|
|
|
+ //m_player_stat->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d m_player_stat->img_convert_ctx[%d] = 0x%0x", __FUNCTION__, __LINE__, index, m_player_stat->img_convert_ctx[index]);
|
|
|
+ if (NULL != m_player_stat->img_convert_ctx[index]) {
|
|
|
+ sws_freeContext(m_player_stat->img_convert_ctx[index]);
|
|
|
+ m_player_stat->img_convert_ctx[index] = NULL;
|
|
|
+ }
|
|
|
+ //m_player_stat->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d m_player_stat->p_frm_yuv[%d] = 0x%0x", __FUNCTION__, __LINE__, index, m_player_stat->p_frm_yuv[index]);
|
|
|
+ if (NULL != m_player_stat->p_frm_yuv[index]) {
|
|
|
+ av_frame_free(&m_player_stat->p_frm_yuv[index]);
|
|
|
+ av_frame_unref(m_player_stat->p_frm_yuv[index]);
|
|
|
+ }
|
|
|
+ //m_player_stat->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s:%d m_player_stat->p_video_buffer[%d] = 0x%0x", __FUNCTION__, __LINE__, index, m_player_stat->p_video_buffer[index]);
|
|
|
+ if (NULL != m_player_stat->p_video_buffer[index]) {
|
|
|
+ av_free(m_player_stat->p_video_buffer[index]);
|
|
|
+ m_player_stat->p_video_buffer[index] = NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ packet_queue_destroy(&m_player_stat->video_pkt_queue, m_hostapi);
|
|
|
+ packet_queue_destroy(&m_player_stat->audio_pkt_queue, m_hostapi);
|
|
|
+
|
|
|
+ ///* free all pictures */
|
|
|
+ frame_queue_destory(&m_player_stat->video_frm_queue);
|
|
|
+ frame_queue_destory(&m_player_stat->audio_frm_queue);
|
|
|
+
|
|
|
+ m_hostapi->Debug(MEDIA_LOG_DEBUG, "av_free player_stat_t.");
|
|
|
+
|
|
|
+ swr_free(&m_player_stat->audio_swr_ctx);
|
|
|
+
|
|
|
+ if (NULL != m_player_stat->audio_frm_rwr) {
|
|
|
+ av_free(m_player_stat->audio_frm_rwr);
|
|
|
+ m_player_stat->audio_frm_rwr = NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_player_stat->piconpath){
|
|
|
+ av_free(m_player_stat->piconpath);
|
|
|
+ m_player_stat->piconpath = NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_player_stat->paudiodev) {
|
|
|
+ av_free(m_player_stat->paudiodev);
|
|
|
+ m_player_stat->paudiodev = NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ av_free(m_player_stat);
|
|
|
+ m_player_stat = NULL;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
int CMediaPlayer::GetViceVideoDisplayInfo(int* icx, int* icy, int* iwidth, int* iheight)
|
|
|
{
|
|
|
int iRet = -1;
|
|
@@ -368,12 +379,10 @@ int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
|
|
|
frame_queue_init(&m_player_stat->audio_frm_queue, &m_player_stat->audio_pkt_queue, SAMPLE_QUEUE_SIZE, 1) < 0)
|
|
|
{
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "media frame queue init failed!");
|
|
|
- player_deinit(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
return iRet;
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
+ else{
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "frame queue init success!");
|
|
|
}
|
|
|
|
|
@@ -382,12 +391,10 @@ int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
|
|
|
packet_queue_init(&m_player_stat->audio_pkt_queue, m_hostapi) < 0)
|
|
|
{
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "media packet queue init failed!");
|
|
|
- player_deinit(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
return iRet;
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
+ else{
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "packet queue init success!");
|
|
|
}
|
|
|
|
|
@@ -397,11 +404,9 @@ int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
|
|
|
packet_queue_put(&m_player_stat->video_pkt_queue, &flush_pkt, m_hostapi);
|
|
|
packet_queue_put(&m_player_stat->audio_pkt_queue, &flush_pkt, m_hostapi);
|
|
|
|
|
|
- if (!(m_player_stat->continue_read_thread = SDL_CreateCond()))
|
|
|
- {
|
|
|
+ if (!(m_player_stat->continue_read_thread = SDL_CreateCond())){
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "SDL_CreateCond(): %s.", SDL_GetError());
|
|
|
- player_deinit(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
return iRet;
|
|
|
}
|
|
|
|
|
@@ -414,7 +419,6 @@ int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
|
|
|
if (0 == GetViceVideoDisplayInfo(&m_player_stat->iDisplayCx, &m_player_stat->iDisplayCy, &m_player_stat->iDisplayWidth, &m_player_stat->iDisplayHeight)){
|
|
|
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);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
iRet = 0;
|
|
@@ -488,7 +492,6 @@ int CMediaPlayer::StopMediaPlay()
|
|
|
m_player_stat->abort_request = 1;
|
|
|
if (m_bplaying) {
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "user stop audio play set SDL_PauseAudioDevice param to 1.");
|
|
|
- //SDL_PauseAudio(1);
|
|
|
SDL_PauseAudioDevice(audio_dev, 1);
|
|
|
}
|
|
|
|
|
@@ -512,6 +515,10 @@ int CMediaPlayer::ExitMediaPlayingThread()
|
|
|
m_hostapi->Debug(MEDIA_LOG_DEBUG, "ExitMediaPlayingThread called.");
|
|
|
|
|
|
if(eVideo_Type == m_player_stat->eMType){
|
|
|
+ if (m_player_stat->sdl_video.texture) {
|
|
|
+ SDL_DestroyTexture(m_player_stat->sdl_video.texture);
|
|
|
+ }
|
|
|
+
|
|
|
if (m_player_stat->sdl_video.renderer){
|
|
|
SDL_DestroyRenderer(m_player_stat->sdl_video.renderer);
|
|
|
}
|
|
@@ -526,13 +533,9 @@ int CMediaPlayer::ExitMediaPlayingThread()
|
|
|
m_player_stat->prvc_cb->cb_play_media_finished(m_player_stat->prvc_cb->user_data);
|
|
|
}
|
|
|
|
|
|
- player_deinit(m_player_stat);
|
|
|
- m_player_stat = NULL;
|
|
|
- m_bplaying = false;
|
|
|
-
|
|
|
- //avformat_network_deinit();
|
|
|
+ UnInitialize_Player_Stat();
|
|
|
|
|
|
- //SDL_Quit();
|
|
|
+ m_bplaying = false;
|
|
|
|
|
|
iRet = 0;
|
|
|
|