123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- #include <stdio.h>
- #include <stdbool.h>
- #include <assert.h>
- #include "player.h"
- #include "frame.h"
- #include "packet.h"
- #include "demux.h"
- #include "video.h"
- #include "audio.h"
- static int initialized = 0;
- // 返回值:返回上一帧的pts更新值(上一帧pts+流逝的时间)
- double get_clock(play_clock_t *c)
- {
- if (*c->queue_serial != c->serial)
- {
- return NAN;
- }
- if (c->paused)
- {
- return c->pts;
- }
- else
- {
- double time = av_gettime_relative() / 1000000.0;
- double ret = c->pts_drift + time; // 展开得: c->pts + (time - c->last_updated)
- return ret;
- }
- }
- void set_clock_at(play_clock_t *c, double pts, int serial, double time)
- {
- c->pts = pts;
- c->last_updated = time;
- c->pts_drift = c->pts - time;
- c->serial = serial;
- }
- void set_clock(play_clock_t *c, double pts, int serial)
- {
- double time = av_gettime_relative() / 1000000.0;
- set_clock_at(c, pts, serial, time);
- }
- static void set_clock_speed(play_clock_t *c, double speed)
- {
- set_clock(c, get_clock(c), c->serial);
- c->speed = speed;
- }
- void init_clock(play_clock_t *c, int *queue_serial)
- {
- c->speed = 1.0;
- c->paused = 0;
- c->queue_serial = queue_serial;
- set_clock(c, NAN, -1);
- }
- static void sync_play_clock_to_slave(play_clock_t *c, play_clock_t *slave)
- {
- double clock = get_clock(c);
- double slave_clock = get_clock(slave);
- if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
- set_clock(c, slave_clock, slave->serial);
- }
- /* pause or resume the video */
- static void stream_toggle_pause(player_stat_t *is)
- {
- if (is->paused)
- {
- // 这里表示当前是暂停状态,将切换到继续播放状态。在继续播放之前,先将暂停期间流逝的时间加到frame_timer中
- is->frame_timer += av_gettime_relative() / 1000000.0 - is->video_clk.last_updated;
- set_clock(&is->video_clk, get_clock(&is->video_clk), is->video_clk.serial);
- }
- is->paused = is->audio_clk.paused = is->video_clk.paused = !is->paused;
- }
- static void toggle_pause(player_stat_t *is)
- {
- stream_toggle_pause(is);
- is->step = 0;
- }
- static void toggle_full_screen(player_stat_t* is)
- {
- SDL_SetWindowFullscreen(is->sdl_video.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
- }
- CMediaPlayer::CMediaPlayer(CMediaHostApi* pHostApi)
- {
- m_hostapi = pHostApi;
- m_player_stat = NULL;
- m_uvolume = SDL_MIX_MAXVOLUME/2;
- m_bplaying = false;
- m_piconpath = NULL;
- m_paudiodev = NULL;
-
- if (NULL != m_hostapi){
- char str_iconpath[MAX_PATH] = {0};
- if (0 == pHostApi->GetMediaPlayerIcoPath(str_iconpath, MAX_PATH)){
- m_piconpath = av_strdup(str_iconpath);
- }
- char str_audiodev[MAX_PATH] = { 0 };
- if (0 == pHostApi->GetAudioOutDevName(str_audiodev, MAX_PATH)) {
- m_paudiodev = av_strdup(str_audiodev);
- }
- else {
- m_paudiodev = NULL;
- }
- }
- else {
- m_hostapi->Debug(MEDIA_LOG_ERROR, "new CMediaPlayer failed!");
- }
- if (initialized){
- ++initialized;
- }
- else {
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)){
- m_hostapi->Debug(MEDIA_LOG_ERROR, "Could not initialize SDL - %s", SDL_GetError());
- m_hostapi->Debug(MEDIA_LOG_ERROR, "(Did you set the DISPLAY variable?)");
- }
- else {
- initialized++;
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "initialize SDL success");
- }
- }
- }
- CMediaPlayer::~CMediaPlayer()
- {
- if (NULL != m_player_stat) {
- UnInitialize_Player_Stat();
- }
- if (NULL != m_piconpath) {
- av_free(m_piconpath);
- m_piconpath = NULL;
- }
- if (NULL != m_paudiodev){
- av_free(m_paudiodev);
- m_paudiodev = NULL;
- }
- if (--initialized == 0){
- SDL_Quit();
- m_hostapi->Debug(MEDIA_LOG_INFO, "SDL_Quit");
- }
- }
- static int audio_volume_callback(int* audiovolume, void* userdata)
- {
- CMediaPlayer* player = (CMediaPlayer*)userdata;
- *audiovolume = player->GetVolume();
- return 0;
- }
- static int audio_play_finished_callback(void* userdata)
- {
- CMediaPlayer* player = (CMediaPlayer*)userdata;
- if (NULL != player){
- player->SetFinishedFlag();
- }
- return 0;
- }
- bool CMediaPlayer::SetFinishedFlag()
- {
- if (NULL != m_player_stat){
- //m_player_stat->baudio_finished = true;
- if (NULL != m_hostapi) {
- m_hostapi->Debug(MEDIA_LOG_INFO, "%s:%d set baudio_finished to true.", __FUNCTION__, __LINE__);
- }
- }
- else {
- if (NULL != m_hostapi) {
- m_hostapi->Debug(MEDIA_LOG_INFO, "%s:%d m_player_stat is null and set audio finished flag failed.", __FUNCTION__, __LINE__);
- }
- }
-
- return true;
- }
- uint8_t CMediaPlayer::GetVolume()
- {
- return m_uvolume;
- }
- eMediaType_t CMediaPlayer::GetPlayingMediaType()
- {
- return m_player_stat->eMType;
- }
- int CMediaPlayer::Initialize_Player_Stat(rvc_media_player_param_t* pMedia_Player)
- {
- int iRet = -1;
- if (NULL == pMedia_Player) {
- return iRet;
- }
- if (NULL != m_player_stat) {
- UnInitialize_Player_Stat();
- }
- m_player_stat = (player_stat_t*)av_mallocz(sizeof(player_stat_t));
- if (NULL == m_player_stat) {
- m_hostapi->Debug(MEDIA_LOG_ERROR, "player_stat_t struct malloc failed!");
- return iRet;
- }
- m_player_stat->buser_stop = false;
- m_player_stat->rvc_hostapi = m_hostapi;
- m_player_stat->prvc_cb = pMedia_Player->cb;
- m_player_stat->eMType = pMedia_Player->eType;
- m_player_stat->eWindType = pMedia_Player->eWindType;
- m_player_stat->bvice_monitor = pMedia_Player->bvicemonitor;
- m_player_stat->iDisplayCx = pMedia_Player->idisplaycx;
- m_player_stat->iDisplayCy = pMedia_Player->idisplaycy;
- m_player_stat->iDisplayWidth = pMedia_Player->idisplaywidth;
- m_player_stat->iDisplayHeight = pMedia_Player->idisplayheight;
- m_player_stat->on_audio_volume = &audio_volume_callback;
- m_player_stat->on_audio_play_finished = &audio_play_finished_callback;
- m_player_stat->user_data = this;
- if (NULL != m_piconpath){
- m_player_stat->piconpath = av_strdup(m_piconpath);
- }
- if (NULL != m_paudiodev){
- m_player_stat->paudiodev = av_strdup(m_paudiodev);
- }
- m_player_stat->uVolume = m_uvolume;
- m_player_stat->uFilesCount = pMedia_Player->uFilesCount;
- m_player_stat->icurrent_index = 0;
- m_player_stat->iaudio_dec_index = 0;
- for (size_t i = 0; i < pMedia_Player->uFilesCount; i++){
- 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) {
- UnInitialize_Player_Stat();
- return iRet;
- }
- else{
- iRet = 0;
- }
- return iRet;
- }
- int CMediaPlayer::UnInitialize_Player_Stat()
- {
- /* XXX: use a special url_shutdown call to abort parse cleanly */
- if (NULL == m_player_stat) {
- return -1;
- }
- if (NULL != m_player_stat->read_tid){
- SDL_WaitThread(m_player_stat->read_tid, NULL);
- 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]);
- }
- }
-
- SDL_DestroyCond(m_player_stat->continue_read_thread);
- SDL_DestroyCond(m_player_stat->audio_play_cond);
- SDL_DestroyMutex(m_player_stat->audio_play_wait_mutex);
- if (m_player_stat->eMType == eVideo_Type) {
- for (int index = 0; index < m_player_stat->uFilesCount; 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;
- }
- 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]);
- }
- 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;
- }
- if (m_player_stat->straudiodev) {
- av_free(m_player_stat->straudiodev);
- m_player_stat->straudiodev = 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;
- size_t uCount = SDL_GetNumVideoDisplays();
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays Number is %d.", uCount);
- SDL_DisplayMode dispalymode[RVC_MAX_DISPLAYNUM] = { 0 };
- for (size_t i = 0; i < uCount && i < RVC_MAX_DISPLAYNUM; i++) {
- SDL_GetDesktopDisplayMode(i, &dispalymode[i]);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} format = %d", i, dispalymode[i].format);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} w = %d", i, dispalymode[i].w);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} h = %d", i, dispalymode[i].h);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "VideoDisplays{%d} refresh_rate = %d", i, dispalymode[i].refresh_rate);
- }
- if (uCount > 1) {
- *icx = dispalymode[0].w;
- *icy = 0;
- *iwidth = dispalymode[1].w;
- *iheight = dispalymode[1].h;
- iRet = 0;
- }
- return iRet;
- }
- int CMediaPlayer::InitParam(rvc_media_player_param_t* pMedia_Player)
- {
- int iRet = -1;
- if (0 != Initialize_Player_Stat(pMedia_Player)){
- return iRet;
- }
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "initialize player stat success.");
- /* start video display */
- if (frame_queue_init(&m_player_stat->video_frm_queue, &m_player_stat->video_pkt_queue, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0 ||
- 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!");
- UnInitialize_Player_Stat();
- return iRet;
- }
- else{
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "frame queue init success!");
- }
- /* init packet queue */
- if (packet_queue_init(&m_player_stat->video_pkt_queue, m_hostapi) < 0 ||
- packet_queue_init(&m_player_stat->audio_pkt_queue, m_hostapi) < 0)
- {
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "media packet queue init failed!");
- UnInitialize_Player_Stat();
- return iRet;
- }
- else{
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "packet queue init success!");
- }
- /* put end pkt into packet queue */
- AVPacket flush_pkt = {0};
- flush_pkt.data = NULL;
- 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()) || !(m_player_stat->audio_play_cond = SDL_CreateCond())){
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "SDL_CreateCond(): %s.", SDL_GetError());
- UnInitialize_Player_Stat();
- return iRet;
- }
- m_player_stat->audio_play_wait_mutex = SDL_CreateMutex();
- init_clock(&m_player_stat->video_clk, &m_player_stat->video_pkt_queue.serial);
- init_clock(&m_player_stat->audio_clk, &m_player_stat->audio_pkt_queue.serial);
- if (m_player_stat->bvice_monitor){
- 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, "Get Vice Video Display Info Success.");
- }
- }
-
- 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;
- return iRet;
- }
- int CMediaPlayer::SetVolume(uint8_t uVolume)
- {
- int iRet = -1;
-
- m_uvolume = uVolume * SDL_MIX_MAXVOLUME/100;
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "set audio volume(%d) to %d.", uVolume, m_uvolume);
- iRet = 0;
- return iRet;
- }
- bool CMediaPlayer::GetPlayingFlag()
- {
- return m_bplaying;
- }
- int CMediaPlayer::StartMediaPlay()
- {
- int iRet = -1;
- if (0 == open_demux(m_player_stat)){
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "open_demux function call success.");
- }
- else{
- m_hostapi->Debug(MEDIA_LOG_ERROR, "open_demux function call failed.");
- av_free(m_player_stat);
- m_player_stat = NULL;
- iRet = -3;
- return iRet;
- }
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "set playing flag to true.");
- m_bplaying = true;
- if (eVideo_Type == m_player_stat->eMType) {
- open_video(m_player_stat);
- }
- open_audio(m_player_stat);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "after open_audio function.");
- //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);
- if (NULL != m_player_stat->audio_decode_tid) {
- SDL_WaitThread(m_player_stat->audio_decode_tid, NULL);
- m_player_stat->audio_decode_tid = NULL;
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "audio decode thread finished.");
- }
- 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_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_player_stat->video_playing_tid = NULL;
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "video playing thread finished.");
- }
- }
- ExitMediaPlayingThread();
- iRet = 0;
-
- return iRet;
- }
- int CMediaPlayer::StopMediaPlay()
- {
- int iRet = -1;
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "StopMediaPlay called.");
- if (NULL == m_player_stat) {
- return iRet;
- }
- 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);
- m_hostapi->Debug(MEDIA_LOG_INFO, "%s:%d set m_player_stat media finished flag to true.", __FUNCTION__, __LINE__);
- m_player_stat->buser_stop = true;
- iRet = 0;
- return iRet;
- }
- int CMediaPlayer::ExitMediaPlayingThread()
- {
- int iRet = -1;
- if (NULL == m_player_stat){
- return iRet;
- }
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "enter ExitMediaPlayingThread call.");
- 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);
- }
-
- if (m_player_stat->sdl_video.window){
- SDL_DestroyWindow(m_player_stat->sdl_video.window);
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "Destroy Window.");
- }
- }
- if (m_player_stat->prvc_cb){
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "cb_play_media_finished callback.");
- m_player_stat->prvc_cb->cb_play_media_finished(m_player_stat->prvc_cb->user_data);
- }
- UnInitialize_Player_Stat();
- m_bplaying = false;
- iRet = 0;
-
- m_hostapi->Debug(MEDIA_LOG_DEBUG, "Leave ExitMediaPlayingThread call.");
- return iRet;
- }
- int64_t CMediaPlayer::GetMediaPlayingThreadId()
- {
- int64_t iRet = 0;
- if (NULL != m_player_stat)
- {
- if (NULL != m_player_stat->read_tid)
- {
- iRet = SDL_GestureID(m_player_stat->read_tid);
- }
- }
- return iRet;
- }
|