#include "playercontroller.h" #include "common.h" #include #include #include #include #include #include "ffmpeg_init.h" #include "start_play_thread.h" #include "video_state.h" #include "audio_decode_thread.h" #include "audio_effect_helper.h" #include "audio_play_thread.h" #include "play_control_window.h" #include "read_thread.h" #include "start_play_thread.h" #include "stopplay_waiting_thread.h" #include "subtitle_decode_thread.h" #include "video_decode_thread.h" #include "video_play_thread.h" #include "video_state.h" Q_LOGGING_CATEGORY(playerControllerLog, "player.controller") PlayerController::PlayerController(QWidget* parent) : QObject(parent) { ffmpeg_init(); m_state = PlayerState::Idle; } PlayerController::~PlayerController() { if (m_initThread.joinable()) { m_initThread.join(); } stopPlay(); } void PlayerController::startToPlay(const QString& file) { std::lock_guard lock(m_stopMutex); qCDebug(playerControllerLog) << "[PlayerController] m_state" << (int) m_state.load(); qDebug() << "[PlayerController] checkAndResetState: " << "m_packetReadThread:" << (bool) m_packetReadThread << "m_decodeVideoThread:" << (bool) m_decodeVideoThread << "m_decodeAudioThread:" << (bool) m_decodeAudioThread << "m_audioPlayThread:" << (bool) m_audioPlayThread << "m_videoPlayThread:" << (bool) m_videoPlayThread << "m_decodeSubtitleThread:" << (bool) m_decodeSubtitleThread << Qt::endl << "m_packetReadThread:" << (m_packetReadThread ? m_packetReadThread->isRunning() : false) << "m_decodeVideoThread:" << (m_decodeVideoThread ? m_decodeVideoThread->isRunning() : false) << "m_decodeAudioThread:" << (m_decodeAudioThread ? m_decodeAudioThread->isRunning() : false) << "m_audioPlayThread:" << (m_audioPlayThread ? m_audioPlayThread->isRunning() : false) << "m_videoPlayThread:" << (m_videoPlayThread ? m_videoPlayThread->isRunning() : false) << "m_decodeSubtitleThread:" << (m_decodeSubtitleThread ? m_decodeSubtitleThread->isRunning() : false); // 自愈:如果状态为Playing但所有线程都已退出,强制Idle if (m_state == PlayerState::Playing) { // 检查线程指针是否存在并且是否正在运行 bool allThreadsStopped = (!m_packetReadThread || !m_packetReadThread->isRunning()) && (!m_decodeVideoThread || !m_decodeVideoThread->isRunning()) && (!m_decodeAudioThread || !m_decodeAudioThread->isRunning()) && (!m_audioPlayThread || !m_audioPlayThread->isRunning()) && (!m_videoPlayThread || !m_videoPlayThread->isRunning()) && (!m_decodeSubtitleThread || !m_decodeSubtitleThread->isRunning()); if (allThreadsStopped) { auto stopAndReset = [](auto& threadPtr) { if (threadPtr) { qCDebug(playerControllerLog) << "[stopAndReset] try stop/join thread, isRunning=" << threadPtr->isRunning(); threadPtr->stop(); threadPtr->join(); qCDebug(playerControllerLog) << "[stopAndReset] 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); m_currentFile.clear(); m_state = PlayerState::Idle; qCDebug(playerControllerLog) << "[PlayerController] All threads stopped, force reset to Idle."; } } if (m_state == PlayerState::Initializing) { qCDebug(playerControllerLog) << "Player is initializing. Ignoring request."; return; } if (m_state == PlayerState::Playing) { if (m_currentFile == file) { qCDebug(playerControllerLog) << "Already playing the same file. Ignoring request."; return; } else { qCDebug(playerControllerLog) << "Player is busy with another file, stopping and switching to:" << file; stopPlay(); // 这里直接 fallthrough 到 Idle 状态 } } if (m_state == PlayerState::Idle) { bool allThreadsStopped = (!m_packetReadThread || !m_packetReadThread->isRunning()) && (!m_decodeVideoThread || !m_decodeVideoThread->isRunning()) && (!m_decodeAudioThread || !m_decodeAudioThread->isRunning()) && (!m_audioPlayThread || !m_audioPlayThread->isRunning()) && (!m_videoPlayThread || !m_videoPlayThread->isRunning()) && (!m_decodeSubtitleThread || !m_decodeSubtitleThread->isRunning()); if (allThreadsStopped) { auto stopAndReset = [](auto& threadPtr) { if (threadPtr) { qCDebug(playerControllerLog) << "[stopAndReset] try stop/join thread, isRunning=" << threadPtr->isRunning(); threadPtr->stop(); threadPtr->join(); qCDebug(playerControllerLog) << "[stopAndReset] thread joined and will reset."; threadPtr.reset(); } }; m_videoState.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); m_currentFile.clear(); m_state = PlayerState::Idle; qCDebug(playerControllerLog) << "[PlayerController] All threads stopped, force reset to Idle."; } qCDebug(playerControllerLog) << "Player is idle. Starting playback for:" << file; m_state = PlayerState::Initializing; m_currentFile = file; if (m_initThread.joinable()) { m_initThread.join(); } m_initThread = std::thread(&PlayerController::asyncInit, this, file); } } void PlayerController::asyncInit(const QString& file) { bool success = false; QElapsedTimer timer; timer.start(); qCDebug(playerControllerLog) << "[Init] asyncInit started"; if (file.isEmpty()) { qCWarning(playerControllerLog) << "Filename is invalid. Please select a valid media file."; success = false; } else { qCInfo(playerControllerLog) << "Check file: " << toNativePath(file); success = true; } m_initSuccess = success; onAsyncInitFinished(file, success); qCDebug(playerControllerLog) << "[Init] asyncInit finished in " << timer.elapsed() << " ms"; } void PlayerController::onAsyncInitFinished(const QString& file, bool success) { std::lock_guard lock(m_stopMutex); if (!success) { playFailed(m_currentFile); m_state = PlayerState::Idle; return; } qCDebug(playerControllerLog) << "[Init] createVideoState..."; if (!createVideoState(m_currentFile)) { qCWarning(playerControllerLog) << "Video state creation failed"; readPacketStopped(); playFailed(m_currentFile); m_state = PlayerState::Idle; return; } assert(m_videoState); qCDebug(playerControllerLog) << "[Init] createReadThread..."; if (!createReadThread()) { qCWarning(playerControllerLog) << "Packet read thread creation failed"; playFailed(m_currentFile); m_state = PlayerState::Idle; return; } if (!m_videoState) { qCWarning(playerControllerLog) << "Video state initialization error"; playFailed(m_currentFile); m_state = PlayerState::Idle; return; } m_packetReadThread->set_video_state(m_videoState->get_state()); const bool hasVideo = playingHasVideo(); const bool hasAudio = playingHasAudio(); const bool hasSubtitle = playingHasSubtitle(); if (hasVideo) { if (!createDecodeVideoThread() || !createVideoPlayThread()) { qCWarning(playerControllerLog) << "Video processing setup failed"; playFailed(m_currentFile); stopPlay(); m_state = PlayerState::Idle; return; } } if (hasAudio) { if (!createDecodeAudioThread() || !createAudioPlayThread()) { qCWarning(playerControllerLog) << "Audio processing setup failed"; playFailed(m_currentFile); stopPlay(); m_state = PlayerState::Idle; return; } } if (hasSubtitle && !createDecodeSubtitleThread()) { qCWarning(playerControllerLog) << "Subtitle processing setup failed"; playFailed(m_currentFile); stopPlay(); m_state = PlayerState::Idle; return; } if (hasAudio) { startPlayThread(); } else { playStarted(); } emit startToPlaySignal(); m_state = PlayerState::Playing; } void PlayerController::stopPlay() { std::lock_guard lock(m_stopMutex); if (m_state == PlayerState::Idle) return; m_state = PlayerState::Stopping; m_videoState.reset(); m_currentFile.clear(); m_state = PlayerState::Idle; } void PlayerController::pausePlay() { if (!m_videoState) return; if (auto state = m_videoState->get_state()) toggle_pause(state, !state->paused); emit updatePlayControlStatus(); } void PlayerController::playMute(bool mute) { if (!m_videoState) return; if (auto state = m_videoState->get_state()) toggle_mute(state, mute); } void PlayerController::playStartSeek() { emit playSeek(); pausePlay(); } void PlayerController::playSeekPre() { videoSeekInc(-2); } void PlayerController::playSeekNext() { videoSeekInc(2); } void PlayerController::setVolume(int volume, int maxValue) { if (!m_audioPlayThread) return; const float vol = static_cast(volume) / maxValue; m_audioPlayThread->set_device_volume(vol); } void PlayerController::setPlaySpeed(double speed) { if (m_videoState) { if (auto state = m_videoState->get_state()) { #if USE_AVFILTER_AUDIO set_audio_playspeed(state, speed); #endif } } } // 状态访问接口 QString PlayerController::playingFile() const { return isPlaying() ? m_currentFile : QString(); } bool PlayerController::isPlaying() const { return m_state == PlayerState::Playing; } bool PlayerController::playingHasVideo() { return m_videoState ? m_videoState->has_video() : false; } bool PlayerController::playingHasAudio() { return m_videoState ? m_videoState->has_audio() : false; } bool PlayerController::playingHasSubtitle() { return m_videoState ? m_videoState->has_subtitle() : false; } VideoState* PlayerController::state() { return m_videoState ? m_videoState->get_state() : nullptr; } float PlayerController::deviceVolume() const { return m_audioPlayThread ? m_audioPlayThread->get_device_volume() : 0.0f; } void PlayerController::setDeviceVolume(float volume) { if (m_audioPlayThread) m_audioPlayThread->set_device_volume(volume); } // 播放状态回调槽函数 void PlayerController::playStarted(bool success) { if (!success) { qCWarning(playerControllerLog) << "Audio device initialization failed!"; return; } allThreadStart(); setThreads(); } void PlayerController::playFailed(const QString& file) { emit showMessage(QString("Playback failed: %1").arg(toNativePath(file)), "Warning", ""); } // 线程 finished 槽函数只做日志和信号 void PlayerController::readPacketStopped() { qCDebug(playerControllerLog) << "************* Read packets thread stopped signal received."; //m_packetReadThread.reset(); checkAndResetState(); // stopPlay(); if (m_videoState) { m_videoState->delete_video_state(); } emit audioStopped(); } void PlayerController::decodeVideoStopped() { qCDebug(playerControllerLog) << "************* Video decode thread stopped."; //m_decodeVideoThread.reset(); checkAndResetState(); } void PlayerController::decodeAudioStopped() { qCDebug(playerControllerLog) << "************* Audio decode thread stopped."; //m_decodeAudioThread.reset(); checkAndResetState(); } void PlayerController::decodeSubtitleStopped() { qCDebug(playerControllerLog) << "************* Subtitle decode thread stopped."; //m_decodeSubtitleThread.reset(); checkAndResetState(); } void PlayerController::audioPlayStopped() { qCDebug(playerControllerLog) << "************* Audio play thread stopped."; emit audioStopped(); //m_audioPlayThread.reset(); checkAndResetState(); } void PlayerController::videoPlayStopped() { qCDebug(playerControllerLog) << "************* Video play thread stopped."; emit videoStopped(); // m_videoPlayThread.reset(); checkAndResetState(); } // 线程管理槽函数 void PlayerController::setThreads() { if (!m_videoState) return; Threads threads; threads.read_tid = m_packetReadThread.get(); threads.video_decode_tid = m_decodeVideoThread.get(); threads.audio_decode_tid = m_decodeAudioThread.get(); threads.video_play_tid = m_videoPlayThread.get(); threads.audio_play_tid = m_audioPlayThread.get(); threads.subtitle_decode_tid = m_decodeSubtitleThread.get(); m_videoState->threads_setting(m_videoState->get_state(), threads); } void PlayerController::startSendData(bool send) { if (m_audioPlayThread) m_audioPlayThread->send_visual_open(send); } void PlayerController::videoSeek(double position, double increment) { if (!m_videoState) return; auto state = m_videoState->get_state(); if (!state) return; if (state->ic->start_time != AV_NOPTS_VALUE && position < state->ic->start_time / static_cast(AV_TIME_BASE)) { position = state->ic->start_time / static_cast(AV_TIME_BASE); } stream_seek(state, static_cast(position * AV_TIME_BASE), static_cast(increment * AV_TIME_BASE), 0); } // 核心私有实现 bool PlayerController::startPlay() { QElapsedTimer timer; timer.start(); if (m_currentFile.isEmpty()) { qCWarning(playerControllerLog) << "Filename is invalid. Please select a valid media file."; return false; } qCInfo(playerControllerLog) << "Starting playback:" << toNativePath(m_currentFile); // 创建数据包读取线程 if (!createReadThread()) { qCWarning(playerControllerLog) << "Packet read thread creation failed"; return false; } // 创建视频状态对象 if (!createVideoState(m_currentFile)) { qCWarning(playerControllerLog) << "Video state creation failed"; readPacketStopped(); return false; } // 检查状态有效性 assert(m_videoState); if (!m_videoState) { qCWarning(playerControllerLog) << "Video state initialization error"; return false; } m_packetReadThread->set_video_state(m_videoState->get_state()); const bool hasVideo = playingHasVideo(); const bool hasAudio = playingHasAudio(); const bool hasSubtitle = playingHasSubtitle(); // 创建视频相关线程 if (hasVideo) { if (!createDecodeVideoThread() || !createVideoPlayThread()) { qCWarning(playerControllerLog) << "Video processing setup failed"; return false; } } // 创建音频相关线程 if (hasAudio) { if (!createDecodeAudioThread() || !createAudioPlayThread()) { qCWarning(playerControllerLog) << "Audio processing setup failed"; return false; } } // 创建字幕线程 if (hasSubtitle && !createDecodeSubtitleThread()) { qCWarning(playerControllerLog) << "Subtitle processing setup failed"; return false; } // 开始播放 if (hasAudio) { startPlayThread(); // 异步启动(处理音频设备初始化) } else { playStarted(); // 同步启动(无音频流) } qCDebug(playerControllerLog) << "Playback initialized in " << timer.elapsed() << " ms"; return true; } bool PlayerController::waitStopPlay(const QString& file) { m_stopPlayWaitingThread = std::make_unique(this, file.toStdString()); m_stopPlayWaitingThread->setOnFinished([this]() { // 可根据需要添加额外处理 //m_stopPlayWaitingThread.reset(); }); m_stopPlayWaitingThread->start(); qCDebug(playerControllerLog) << "++++++++++ StopPlay waiting thread started"; return true; } void PlayerController::allThreadStart() { // 启动所有创建的线程 if (m_packetReadThread) { if (!m_videoState || !m_videoState->get_state()) { qCWarning(playerControllerLog) << "VideoState invalid, skip starting read thread"; } else { m_packetReadThread->start(); } qCDebug(playerControllerLog) << "++++++++++ Read packets thread started"; } if (m_decodeVideoThread) { m_decodeVideoThread->start(); qCDebug(playerControllerLog) << "++++++++++ Video decode thread started"; } if (m_decodeAudioThread) { m_decodeAudioThread->start(); qCDebug(playerControllerLog) << "++++++++++ Audio decode thread started"; } if (m_decodeSubtitleThread) { m_decodeSubtitleThread->start(); qCDebug(playerControllerLog) << "++++++++++ Subtitle decode thread started"; } if (m_videoPlayThread) { m_videoPlayThread->start(); qCDebug(playerControllerLog) << "++++++++++ Video play thread started"; } if (m_audioPlayThread) { m_audioPlayThread->start(); qCDebug(playerControllerLog) << "++++++++++ Audio play thread started"; } // 通知UI更新 emit setPlayControlWnd(true); emit updatePlayControlVolume(); emit updatePlayControlStatus(); } // 辅助函数 void PlayerController::videoSeekInc(double increment) { if (!m_videoState) return; auto state = m_videoState->get_state(); if (!state) return; double position = get_master_clock(state); if (std::isnan(position)) { position = static_cast(state->seek_pos) / AV_TIME_BASE; } position += increment; videoSeek(position, increment); } // 新增自愈机制辅助函数 void PlayerController::checkAndResetState() { // std::lock_guard lock(m_stopMutex); // if (!m_packetReadThread && !m_decodeVideoThread && !m_decodeAudioThread && !m_audioPlayThread // && !m_videoPlayThread && !m_decodeSubtitleThread) { // m_videoState.reset(); // m_currentFile.clear(); // m_state = PlayerState::Idle; // qCDebug(playerControllerLog) // << "[PlayerController] All threads stopped, state reset to Idle."; // emit playbackFinished(); // 新增:通知UI // } else { // qCDebug(playerControllerLog) << "[PlayerController] checkAndResetState: " // << "m_packetReadThread:" << (bool) m_packetReadThread // << "m_decodeVideoThread:" << (bool) m_decodeVideoThread // << "m_decodeAudioThread:" << (bool) m_decodeAudioThread // << "m_audioPlayThread:" << (bool) m_audioPlayThread // << "m_videoPlayThread:" << (bool) m_videoPlayThread // << "m_decodeSubtitleThread:" << (bool) m_decodeSubtitleThread; // } } // 线程创建方法 bool PlayerController::createVideoState(const QString& file) { const bool useHardware = false; // 待实现:来自UI设置 const bool loop = false; // 待实现:来自UI设置 if (m_videoState) return false; m_videoState = std::make_unique(useHardware, loop); const int ret = m_videoState->create_video_state(file.toUtf8().constData()); if (ret < 0) { m_videoState.reset(); qCWarning(playerControllerLog) << "Video state creation failed (error: " << ret << ")"; return false; } return true; } void PlayerController::deleteVideoState() { m_videoState.reset(); } bool PlayerController::createReadThread() { if (m_packetReadThread) return false; m_packetReadThread = std::make_unique(m_videoState ? m_videoState->get_state() : nullptr); m_packetReadThread->setOnFinished([this]() { readPacketStopped(); }); return true; } bool PlayerController::createDecodeVideoThread() { if (!m_videoState || m_decodeVideoThread) return false; auto state = m_videoState->get_state(); if (!state) return false; m_decodeVideoThread = std::make_unique(state); m_decodeVideoThread->setOnFinished([this]() { decodeVideoStopped(); }); auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO); // 初始化视频解码器 int ret = decoder_init(&state->viddec, codecContext, &state->videoq, state->continue_read_thread); if (ret < 0) { qCWarning(playerControllerLog) << "Video decoder initialization failed (error: " << ret << ")"; return false; } ret = decoder_start(&state->viddec, m_decodeVideoThread.get(), "video_decoder"); if (ret < 0) { qCWarning(playerControllerLog) << "Video decoder start failed (error: " << ret << ")"; return false; } state->queue_attachments_req = 1; return true; } bool PlayerController::createDecodeAudioThread() { if (!m_videoState || m_decodeAudioThread) return false; auto state = m_videoState->get_state(); if (!state) return false; m_decodeAudioThread = std::make_unique(state); m_decodeAudioThread->setOnFinished([this]() { decodeAudioStopped(); }); auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_AUDIO); // 初始化音频解码器 int ret = decoder_init(&state->auddec, codecContext, &state->audioq, state->continue_read_thread); if (ret < 0) { qCWarning(playerControllerLog) << "Audio decoder initialization failed (error: " << ret << ")"; return false; } ret = decoder_start(&state->auddec, m_decodeAudioThread.get(), "audio_decoder"); if (ret < 0) { qCWarning(playerControllerLog) << "Audio decoder start failed (error: " << ret << ")"; return false; } return true; } bool PlayerController::createDecodeSubtitleThread() { if (!m_videoState || m_decodeSubtitleThread) return false; auto state = m_videoState->get_state(); if (!state) return false; m_decodeSubtitleThread = std::make_unique(state); m_decodeSubtitleThread->setOnFinished([this]() { decodeSubtitleStopped(); }); auto codecContext = m_videoState->get_contex(AVMEDIA_TYPE_SUBTITLE); // 初始化字幕解码器 int ret = decoder_init(&state->subdec, codecContext, &state->subtitleq, state->continue_read_thread); if (ret < 0) { qCWarning(playerControllerLog) << "Subtitle decoder initialization failed (error: " << ret << ")"; return false; } ret = decoder_start(&state->subdec, m_decodeSubtitleThread.get(), "subtitle_decoder"); if (ret < 0) { qCWarning(playerControllerLog) << "Subtitle decoder start failed (error: " << ret << ")"; return false; } return true; } bool PlayerController::createVideoPlayThread() { if (!m_videoState || m_videoPlayThread) return false; auto state = m_videoState->get_state(); if (!state) return false; m_videoPlayThread = std::make_unique(state); m_videoPlayThread->setOnFinished([this]() { videoPlayStopped(); }); m_videoPlayThread->setOnFrameReady([this](AVFrame* frame) { this->onFrameReady(frame); }); m_videoPlayThread->setOnSubtitleReady([this](const QString& text) { // TODO: 实现 PlayerController::onSubtitleReady(const QString&) 处理字幕 // onSubtitleReady(text); }); // 初始化参数 auto videoContext = m_videoState->get_contex(AVMEDIA_TYPE_VIDEO); const bool useHardware = m_videoState->is_hardware_decode(); if (!m_videoPlayThread->init_resample_param(videoContext, useHardware)) { qCWarning(playerControllerLog) << "Video resample parameters initialization failed"; return false; } return true; } bool PlayerController::createAudioPlayThread() { if (!m_videoState || m_audioPlayThread) return false; auto state = m_videoState->get_state(); if (!state) return false; m_audioPlayThread = std::make_unique(state); m_audioPlayThread->setOnFinished([this]() { audioPlayStopped(); }); m_audioPlayThread->setOnUpdatePlayTime([this]() { // TODO: 实现 PlayerController::onUpdatePlayTime() 处理播放时间更新 // onUpdatePlayTime(); }); m_audioPlayThread->setOnDataVisualReady([this](const AudioData& data) { // TODO: 实现 PlayerController::onDataVisualReady(const AudioData&) 处理可视化数据 // onDataVisualReady(data); }); // 连接信号 // connect(m_audioPlayThread.get(), // &AudioPlayThread::update_play_time, // this, // &PlayerController::updatePlayTime); // connect(m_audioPlayThread.get(), // &AudioPlayThread::data_visual_ready, // this, // &PlayerController::audioData); // 音频设备初始化在独立线程中完成 return true; } bool PlayerController::startPlayThread() { if (m_beforePlayThread) return false; m_beforePlayThread = std::make_unique(m_audioPlayThread.get(), m_videoState.get()); m_beforePlayThread->setOnFinished([this]() { qCDebug(playerControllerLog) << "[StartPlayThread] finished, call playStarted()"; playStarted(); }); m_beforePlayThread->start(); qCDebug(playerControllerLog) << "++++++++++ StartPlay thread (audio init) started"; return true; } // 调试辅助函数 void PlayerController::printDecodeContext(const AVCodecContext* codecCtx, bool isVideo) const { if (!codecCtx) return; qCInfo(playerControllerLog) << (isVideo ? "Video" : "Audio") << " codec: " << codecCtx->codec->name; qCInfo(playerControllerLog) << " Type:" << codecCtx->codec_type << "ID:" << codecCtx->codec_id << "Tag:" << codecCtx->codec_tag; if (isVideo) { qCInfo(playerControllerLog) << " Dimensions: " << codecCtx->width << "x" << codecCtx->height; } else { qCInfo(playerControllerLog) << " Sample rate: " << codecCtx->sample_rate << " Hz, Channels: " << codecCtx->ch_layout.nb_channels << ", Format: " << codecCtx->sample_fmt; qCInfo(playerControllerLog) << " Frame size: " << codecCtx->frame_size << ", Block align: " << codecCtx->block_align; } } // 在合适位置实现 onFrameReady void PlayerController::onFrameReady(AVFrame* frame) { // 这里可以做帧处理、缓存、同步等操作 emit frameReady(frame); // 直接转发给 UI 层 }