zhuizhu пре 8 месеци
родитељ
комит
f86f235ae9

+ 0 - 6
AvPlayer2/audio_decode_thread.cpp

@@ -21,12 +21,6 @@ AudioDecodeThread::~AudioDecodeThread()
         << "[AudioDecodeThread] ~AudioDecodeThread, m_pState:" << (void*) m_pState;
 }
 
-void AudioDecodeThread::stop()
-{
-    *m_exit = true;
-    m_cv.notify_all();
-}
-
 void AudioDecodeThread::run()
 {
     qCDebug(playerControllerAudioDecodeThread) << "[AudioDecodeThread] run start, m_pState:" << (void*) m_pState;

+ 0 - 2
AvPlayer2/audio_decode_thread.h

@@ -12,8 +12,6 @@ public:
     explicit AudioDecodeThread(VideoState* pState = nullptr);
     ~AudioDecodeThread();
 
-    void stop() override;
-
 protected:
     void run() override;
 

+ 0 - 5
AvPlayer2/audio_play_thread.cpp

@@ -401,8 +401,3 @@ void AudioPlayThread::final_resample_param()
     swr_free(&m_audioResample.swrCtx);
 }
 
-void AudioPlayThread::stop_thread()
-{
-    *m_exit = true;
-    // 如有条件变量/阻塞等待,这里唤醒
-}

+ 1 - 5
AvPlayer2/audio_play_thread.h

@@ -40,10 +40,6 @@ class AudioPlayThread : public ThreadBase
 public:
     explicit AudioPlayThread(VideoState* pState = nullptr);
     virtual ~AudioPlayThread();
-    AudioPlayThread(const AudioPlayThread&) = delete;
-    AudioPlayThread& operator=(const AudioPlayThread&) = delete;
-    AudioPlayThread(AudioPlayThread&&) = delete;
-    AudioPlayThread& operator=(AudioPlayThread&&) = delete;
 
 public:
     void print_device() const;
@@ -56,7 +52,7 @@ public:
     float get_device_volume() const;
     void set_device_volume(float volume);
     void send_visual_open(bool bSend = true) { m_bSendToVisual = bSend; };
-    void stop_thread();
+
     void setOnUpdatePlayTime(std::function<void()> cb) { m_onUpdatePlayTime = std::move(cb); }
     void setOnDataVisualReady(std::function<void(const AudioData&)> cb) { m_onDataVisualReady = std::move(cb); }
 

+ 18 - 11
AvPlayer2/playercontroller.cpp

@@ -231,15 +231,12 @@ void PlayerController::stopPlay()
     qCDebug(playerControllerLog) << "Stopping playback...";
     m_state = PlayerState::Stopping;
 
-    if (m_videoState) {
-        m_videoState->delete_video_state();
-    }
-    // 清理视频状态
-    m_videoState.reset();
-
     // 停止并重置所有线程
     stopAndResetThreads();
 
+    // 清理视频状态
+    m_videoState.reset();
+
     m_currentFile.clear();
     m_state = PlayerState::Idle;
 
@@ -491,20 +488,30 @@ void PlayerController::stopAndResetThreads()
                 }
                 std::this_thread::sleep_for(std::chrono::milliseconds(10));
             }
-            threadPtr->join();
-            qCDebug(playerControllerLog) << "[stopAndReset] [" << threadName << "] thread joined and will reset.";
+            // 只有线程已停止才join
+            if (!threadPtr->isRunning()) {
+                threadPtr->join();
+                qCDebug(playerControllerLog)
+                    << "[stopAndReset] [" << threadName << "] thread joined and will reset.";
+            }
             threadPtr.reset();
         }
     };
 
     // 按依赖顺序停止线程
     stopAndReset(m_beforePlayThread, "BeforePlay");
-    stopAndReset(m_packetReadThread, "PacketRead");
+    stopAndReset(m_videoPlayThread, "VideoPlay");
+    stopAndReset(m_audioPlayThread, "AudioPlay");
+
+    // 解码前先 关闭流 不然会卡死异常
+    if (m_videoState) {
+        m_videoState->delete_video_state();
+    }
+
     stopAndReset(m_decodeVideoThread, "DecodeVideo");
     stopAndReset(m_decodeAudioThread, "DecodeAudio");
     stopAndReset(m_decodeSubtitleThread, "DecodeSubtitle");
-    stopAndReset(m_videoPlayThread, "VideoPlay");
-    stopAndReset(m_audioPlayThread, "AudioPlay");
+    stopAndReset(m_packetReadThread, "PacketRead");
 }
 
 bool PlayerController::areAllThreadsStopped() const

+ 0 - 5
AvPlayer2/subtitle_decode_thread.cpp

@@ -19,11 +19,6 @@ SubtitleDecodeThread::~SubtitleDecodeThread()
 {
 }
 
-void SubtitleDecodeThread::stop()
-{
-    *m_exit = true;
-    m_cv.notify_all();
-}
 
 void SubtitleDecodeThread::run()
 {

+ 0 - 3
AvPlayer2/subtitle_decode_thread.h

@@ -5,7 +5,6 @@
 
 #include "ThreadBase.h"
 #include "packets_sync.h"
-#include <atomic>
 
 class SubtitleDecodeThread : public ThreadBase
 {
@@ -13,8 +12,6 @@ public:
     explicit SubtitleDecodeThread(VideoState* pState = nullptr);
     ~SubtitleDecodeThread();
 
-    void stop() override;
-
 protected:
     void run() override;
 

+ 0 - 6
AvPlayer2/video_decode_thread.cpp

@@ -18,12 +18,6 @@ VideoDecodeThread::VideoDecodeThread(VideoState* pState)
 
 VideoDecodeThread::~VideoDecodeThread() {}
 
-void VideoDecodeThread::stop()
-{
-    *m_exit = true;
-    m_cv.notify_all();
-}
-
 void VideoDecodeThread::run()
 {
     qCDebug(playerControllerVideoDecodeThread)

+ 0 - 2
AvPlayer2/video_decode_thread.h

@@ -12,8 +12,6 @@ public:
     explicit VideoDecodeThread(VideoState* pState = nullptr);
     ~VideoDecodeThread();
 
-    void stop() override;
-
 protected:
     void run() override;
 

+ 0 - 6
AvPlayer2/video_play_thread.cpp

@@ -32,12 +32,6 @@ VideoPlayThread::~VideoPlayThread()
     final_resample_param();
 }
 
-void VideoPlayThread::stop()
-{
-    *m_exit = true;
-    m_cv.notify_all();
-}
-
 void VideoPlayThread::run()
 {
     qCDebug(playerControllerVideoPlayThread) << "[VideoPlayThread] run start, m_pState:" << (void*)m_pState;

+ 0 - 1
AvPlayer2/video_play_thread.h

@@ -24,7 +24,6 @@ public:
     ~VideoPlayThread();
 
     bool init_resample_param(AVCodecContext* pVideo, bool bHardware = false);
-    void stop() override;
 
     void setOnFrameReady(std::function<void(AVFrame*)> cb) { m_onFrameReady = std::move(cb); }
     void setOnSubtitleReady(std::function<void(const QString&)> cb) { m_onSubtitleReady = std::move(cb); }

+ 1 - 1
AvPlayer2/video_state.cpp

@@ -406,7 +406,7 @@ void VideoStateData::stream_close(VideoState* is)
     if (is->subtitle_stream >= 0)
         stream_component_close(is, is->subtitle_stream);
 
-    threads_exit_wait(is); // 这里插入线程关闭 ?
+    //threads_exit_wait(is); // 这里插入线程关闭 ?
 
     avformat_close_input(&is->ic);