Prechádzať zdrojové kódy

修复文件播放完后卡死的问题

huihui 3 mesiacov pred
rodič
commit
1da675b92b

+ 12 - 1
module/VideoPlayer/src/VideoPlayer/VideoPlayer.cpp

@@ -155,6 +155,7 @@ bool VideoPlayer::stop(bool isWait)
 
     m_state = VideoPlayer::Stop;
     mIsQuit = true;
+    mIsPause = false;
 
     ///唤醒等待中的线程
     m_cond_video.notify_all();
@@ -735,7 +736,13 @@ std::cout<<" video:"<<pFormatCtx->streams[videoStream]->duration<<" "<<pFormatCt
         {
             mIsReadFinished = true;
             mIsReadError = true;
-            printf("%s av_read_frame failed \n", __FUNCTION__);
+
+            ///唤醒等待中的线程
+            m_cond_video.notify_all();
+            m_cond_audio.notify_all();
+
+            // printf("%s av_read_frame failed %s mIsVideoThreadFinished=%d mIsAudioThreadFinished=%d mIsQuit=%d m_video_pkt_list.size()=%d m_audio_pkt_list.size()=%d \n", 
+            // __FUNCTION__, file_path, mIsVideoThreadFinished, mIsAudioThreadFinished, mIsQuit, m_video_pkt_list.size(), m_audio_pkt_list.size());
 //            if (mIsQuit)
 //            {
 //                break; //解码线程也执行完了 可以退出了
@@ -744,6 +751,10 @@ std::cout<<" video:"<<pFormatCtx->streams[videoStream]->duration<<" "<<pFormatCt
             mSleep(10);
             continue;
         }
+        else
+        {
+            mIsReadFinished = false;
+        }
 // qDebug("%s mIsQuit=%d mIsPause=%d packet.stream_index=%d \n", __FUNCTION__, mIsQuit, mIsPause, packet.stream_index);
 // fprintf(stderr, "%s mIsQuit=%d mIsPause=%d packet.stream_index=%d videoStream=%d audioStream=%d \n", __FUNCTION__, mIsQuit, mIsPause, packet.stream_index, videoStream, audioStream);
         if (packet.stream_index == videoStream)

+ 11 - 11
module/VideoPlayer/src/VideoPlayer/VideoPlayer.h

@@ -169,28 +169,28 @@ private:
     ///音视频同步相关
     uint64_t mVideoStartTime; //开始播放视频的时间
     uint64_t mPauseStartTime; //暂停开始的时间
-    int64_t audio_clock; ///音频时钟(秒-小数)
+    int64_t audio_clock; ///音频时钟毫秒
     int64_t video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame
-    AVStream *mVideoStream; //视频流
-    AVStream *mAudioStream; //音频流
+    AVStream *mVideoStream = nullptr; //视频流
+    AVStream *mAudioStream = nullptr; //音频流
     // std::mutex m_mutex_audio_clk;
     uint64_t getAudioClock();
 
     ///视频相关
-    AVFormatContext *pFormatCtx;
-    AVCodecContext *pCodecCtx;
-    AVCodec *pCodec;
+    AVFormatContext *pFormatCtx = nullptr;
+    AVCodecContext *pCodecCtx = nullptr;
+    AVCodec *pCodec = nullptr;
 
     ///音频相关
-    AVCodecContext *aCodecCtx;
-    AVCodec *aCodec;
-    AVFrame *aFrame;
+    AVCodecContext *aCodecCtx = nullptr;
+    AVCodec *aCodec = nullptr;
+    AVFrame *aFrame = nullptr;
 
     ///以下变量用于音频重采样
     /// 由于ffmpeg解码出来后的pcm数据有可能是带平面的pcm,因此这里统一做重采样处理,
     /// 重采样成44100的16 bits 双声道数据(AV_SAMPLE_FMT_S16)
-    AVFrame *aFrame_ReSample;
-    SwrContext *swrCtx;
+    AVFrame *aFrame_ReSample = nullptr;
+    SwrContext *swrCtx = nullptr;
 
     enum AVSampleFormat in_sample_fmt; //输入的采样格式
     enum AVSampleFormat out_sample_fmt;//输出的采样格式 16bit PCM

+ 2 - 2
module/VideoPlayer/src/VideoPlayer/VideoPlayer_AudioThread.cpp

@@ -74,7 +74,7 @@ void VideoPlayer::decodeAudioThread()
         }
 
         std::unique_lock<std::mutex> lck(m_mutex_audio);
-        while (!mIsQuit && m_audio_pkt_list.empty())
+        while (!mIsQuit && !mIsReadFinished && m_audio_pkt_list.empty())
         {
             m_cond_audio.wait(lck);
         }
@@ -139,7 +139,7 @@ void VideoPlayer::decodeAudioThread()
 
                 AACFramePtr aac_frame = std::make_shared<AACFrame>();
                 aac_frame->setFrameBuffer(adtsBuffer, 7, packet->data, packet->size);
-
+                aac_frame->setPts(audio_clock);
                 m_event_handle->onAudioBuffer(aac_frame);
             }
             else if(mAudioStream->codecpar->codec_id == AV_CODEC_ID_PCM_MULAW)

+ 2 - 4
module/VideoPlayer/src/VideoPlayer/VideoPlayer_VideoThread.cpp

@@ -217,7 +217,7 @@ fail:
 
 void VideoPlayer::decodeVideoThread()
 {
-    fprintf(stderr, "%s start \n", __FUNCTION__);
+    fprintf(stderr, "%s start pointer=%d file_path=%s \n", __FUNCTION__, this, m_file_path.c_str());
 
     mIsVideoThreadFinished = false;
 
@@ -324,7 +324,7 @@ void VideoPlayer::decodeVideoThread()
         }
 
         std::unique_lock<std::mutex> lck(m_mutex_video);
-        while (!mIsQuit && m_video_pkt_list.empty())
+        while (!mIsQuit && !mIsReadFinished && m_video_pkt_list.empty())
         {
             m_cond_video.wait(lck);
         }
@@ -620,6 +620,4 @@ void VideoPlayer::decodeVideoThread()
     mIsVideoThreadFinished = true;
 
     fprintf(stderr, "%s finished \n", __FUNCTION__);
-
-    return;
 }