|
|
@@ -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()
|
|
|
{
|
|
|
// 启动所有创建的线程
|