zhuizhu 8 kuukautta sitten
vanhempi
säilyke
238ae13b83
2 muutettua tiedostoa jossa 32 lisäystä ja 28 poistoa
  1. 32 26
      AvPlayer2/playercontroller.cpp
  2. 0 2
      AvPlayer2/playercontroller.h

+ 32 - 26
AvPlayer2/playercontroller.cpp

@@ -231,11 +231,15 @@ 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;
 
@@ -466,26 +470,41 @@ void PlayerController::videoSeek(double position, double increment)
 // 线程管理辅助方法
 void PlayerController::stopAndResetThreads()
 {
-    auto stopAndReset = [](auto& threadPtr) {
+    auto stopAndReset = [](auto& threadPtr, const QString& threadName) {
         if (threadPtr) {
             qCDebug(playerControllerLog)
-                << "[stopAndReset] try stop/join thread, isRunning=" << threadPtr->isRunning();
+                << "[stopAndReset] [" << threadName << "] try stop/join thread, isRunning=" << threadPtr->isRunning();
             threadPtr->stop();
+
+            // 添加超时等待机制
+            const int MAX_WAIT_MS = 500; // 最多等待500毫秒
+            auto startTime = std::chrono::steady_clock::now();
+
+            while (threadPtr->isRunning()) {
+                auto now = std::chrono::steady_clock::now();
+                auto elapsed
+                    = std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
+                if (elapsed > MAX_WAIT_MS) {
+                    qCWarning(playerControllerLog)
+                        << "[stopAndReset] [" << threadName << "] Thread stop timeout after" << elapsed << "ms";
+                    break;
+                }
+                std::this_thread::sleep_for(std::chrono::milliseconds(10));
+            }
             threadPtr->join();
-            qCDebug(playerControllerLog) << "[stopAndReset] thread joined and will reset.";
+            qCDebug(playerControllerLog) << "[stopAndReset] [" << threadName << "] thread joined and will reset.";
             threadPtr.reset();
         }
     };
 
     // 按依赖顺序停止线程
-    stopAndReset(m_stopPlayWaitingThread);
-    stopAndReset(m_beforePlayThread);
-    stopAndReset(m_packetReadThread);
-    stopAndReset(m_decodeVideoThread);
-    stopAndReset(m_decodeAudioThread);
-    stopAndReset(m_decodeSubtitleThread);
-    stopAndReset(m_videoPlayThread);
-    stopAndReset(m_audioPlayThread);
+    stopAndReset(m_beforePlayThread, "BeforePlay");
+    stopAndReset(m_packetReadThread, "PacketRead");
+    stopAndReset(m_decodeVideoThread, "DecodeVideo");
+    stopAndReset(m_decodeAudioThread, "DecodeAudio");
+    stopAndReset(m_decodeSubtitleThread, "DecodeSubtitle");
+    stopAndReset(m_videoPlayThread, "VideoPlay");
+    stopAndReset(m_audioPlayThread, "AudioPlay");
 }
 
 bool PlayerController::areAllThreadsStopped() const
@@ -499,19 +518,6 @@ bool PlayerController::areAllThreadsStopped() const
            && (!m_decodeSubtitleThread || !m_decodeSubtitleThread->isRunning());
 }
 
-bool PlayerController::waitStopPlay(const QString& file)
-{
-    m_stopPlayWaitingThread = std::make_unique<StopWaitingThread>(this, file.toStdString());
-
-    m_stopPlayWaitingThread->setOnFinished([this]() {
-        // 可根据需要添加额外处理
-        //m_stopPlayWaitingThread.reset();
-    });
-    m_stopPlayWaitingThread->start();
-    qCDebug(playerControllerLog) << "++++++++++ StopPlay waiting thread started";
-    return true;
-}
-
 void PlayerController::allThreadStart()
 {
     // 启动所有创建的线程

+ 0 - 2
AvPlayer2/playercontroller.h

@@ -253,7 +253,6 @@ private:
     //--------------------------------------------------------------------------
     // 核心播放逻辑
     //--------------------------------------------------------------------------
-    bool waitStopPlay(const QString& file);
     void allThreadStart();
 
     // 线程管理辅助方法
@@ -314,7 +313,6 @@ private:
     //--------------------------------------------------------------------------
     std::unique_ptr<VideoStateData> m_videoState;              ///< 视频状态数据
     std::unique_ptr<StartPlayThread> m_beforePlayThread;       ///< 播放前准备线程
-    std::unique_ptr<StopWaitingThread> m_stopPlayWaitingThread; ///< 停止等待线程
     std::thread m_eventThread;                                 ///< 事件处理线程
     QString m_currentFile;                                     ///< 当前播放文件路径
 };