|
|
@@ -10,21 +10,13 @@
|
|
|
#include <algorithm>
|
|
|
#include <cmath>
|
|
|
|
|
|
-#ifdef _WIN32
|
|
|
-#include <windows.h>
|
|
|
-#include <psapi.h>
|
|
|
-#else
|
|
|
-#include <sys/times.h>
|
|
|
-#include <unistd.h>
|
|
|
-#endif
|
|
|
-
|
|
|
namespace av {
|
|
|
namespace player {
|
|
|
PlayerCoreV2::PlayerCoreV2(const SyncConfigV2& syncConfig)
|
|
|
: m_state(PlayerState::Idle)
|
|
|
, m_eventCallback(nullptr)
|
|
|
, m_formatContext(nullptr)
|
|
|
- , m_openGLVideoRenderer(nullptr)
|
|
|
+ // , m_openGLVideoRenderer(nullptr) // 已移除
|
|
|
, m_volume(1.0)
|
|
|
, m_playbackSpeed(1.0)
|
|
|
, m_seekTarget(-1)
|
|
|
@@ -33,6 +25,7 @@ PlayerCoreV2::PlayerCoreV2(const SyncConfigV2& syncConfig)
|
|
|
, m_lastUpdateTime(0)
|
|
|
, m_threadsShouldStop(false)
|
|
|
, m_threadsRunning(false)
|
|
|
+ , m_paused(false)
|
|
|
, m_initialized(false)
|
|
|
, m_frameCount(0)
|
|
|
, m_lastFrameCount(0)
|
|
|
@@ -134,13 +127,7 @@ PlayerCoreV2::PlayerCoreV2(const SyncConfigV2& syncConfig)
|
|
|
// 初始化性能监控
|
|
|
m_lastStatsUpdate = std::chrono::steady_clock::now();
|
|
|
m_lastCpuMeasure = m_lastStatsUpdate;
|
|
|
-#ifdef _WIN32
|
|
|
- m_lastCpuTime = GetTickCount64();
|
|
|
-#else
|
|
|
- struct tms tm;
|
|
|
- m_lastCpuTime = times(&tm);
|
|
|
-#endif
|
|
|
-
|
|
|
+
|
|
|
m_initialized = true;
|
|
|
Logger::instance().info("PlayerCoreV2 initialized successfully");
|
|
|
|
|
|
@@ -210,20 +197,16 @@ ErrorCode PlayerCoreV2::openFile(const std::string& filename) {
|
|
|
|
|
|
setState(PlayerState::Stopped);
|
|
|
|
|
|
- // 如果已设置视频渲染器且有视频流,重新初始化渲染器
|
|
|
- if (m_mediaInfo.hasVideo && m_openGLVideoRenderer) {
|
|
|
+ // 如果有视频流,通知需要初始化渲染器
|
|
|
+ if (m_mediaInfo.hasVideo && m_eventCallback) {
|
|
|
AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
|
|
|
|
|
|
- bool rendererInitResult = m_openGLVideoRenderer->Open(
|
|
|
+ m_eventCallback->onVideoRendererInitRequired(
|
|
|
videoStream->codecpar->width,
|
|
|
videoStream->codecpar->height
|
|
|
);
|
|
|
|
|
|
- if (!rendererInitResult) {
|
|
|
- Logger::instance().warning("Failed to initialize OpenGL video renderer");
|
|
|
- } else {
|
|
|
- Logger::instance().info("OpenGL video renderer initialized successfully");
|
|
|
- }
|
|
|
+ Logger::instance().info("Video renderer initialization requested");
|
|
|
}
|
|
|
|
|
|
// 通知媒体信息变化
|
|
|
@@ -248,77 +231,114 @@ ErrorCode PlayerCoreV2::play() {
|
|
|
return ErrorCode::INVALID_STATE;
|
|
|
}
|
|
|
|
|
|
- // 启动同步器
|
|
|
- if (m_synchronizer && m_synchronizer->start() != ErrorCode::SUCCESS) {
|
|
|
- Logger::instance().error("Failed to start synchronizer");
|
|
|
- return ErrorCode::SYNC_ERROR;
|
|
|
+ // 启动或恢复同步器
|
|
|
+ if (m_synchronizer) {
|
|
|
+ ErrorCode syncResult = ErrorCode::SUCCESS;
|
|
|
+ if (m_state == PlayerState::Paused) {
|
|
|
+ // 从暂停状态恢复,调用resume方法
|
|
|
+ syncResult = m_synchronizer->resume();
|
|
|
+ } else {
|
|
|
+ // 从停止状态开始,调用start方法
|
|
|
+ syncResult = m_synchronizer->start();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (syncResult != ErrorCode::SUCCESS) {
|
|
|
+ Logger::instance().error("Failed to start/resume synchronizer");
|
|
|
+ return ErrorCode::SYNC_ERROR;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 记录播放开始时间
|
|
|
- m_playStartTime = std::chrono::steady_clock::now();
|
|
|
-
|
|
|
- // 如果是从停止状态开始播放,重置基准时间
|
|
|
+ // 根据当前状态设置播放开始时间
|
|
|
if (m_state == PlayerState::Stopped) {
|
|
|
+ // 从停止状态开始播放,重置所有时间相关变量
|
|
|
m_baseTime = 0;
|
|
|
m_frameCount = 0;
|
|
|
m_lastFrameCount = 0;
|
|
|
+ m_playStartTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
// 重置统计信息
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
m_stats = PlaybackStats();
|
|
|
m_stats.playbackSpeed = m_playbackSpeed;
|
|
|
+ } else if (m_state == PlayerState::Paused) {
|
|
|
+ // 从暂停状态恢复播放,重新设置播放开始时间
|
|
|
+ // m_baseTime已经在pause()方法中正确更新,这里只需要重新设置开始时间
|
|
|
+ m_playStartTime = std::chrono::steady_clock::now();
|
|
|
+
|
|
|
+ // 清除暂停标志并唤醒等待的线程,类似ffplay.c中的continue_read_thread机制
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_paused = false;
|
|
|
+ }
|
|
|
+ m_pauseCondition.notify_all();
|
|
|
}
|
|
|
|
|
|
// 启动音频输出设备
|
|
|
if (m_audioOutput && m_mediaInfo.hasAudio) {
|
|
|
if (m_state == PlayerState::Paused) {
|
|
|
- m_audioOutput->resume();
|
|
|
+ // 从暂停状态恢复音频
|
|
|
+ try {
|
|
|
+ m_audioOutput->resume();
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ Logger::instance().error("Failed to resume audio output: " + std::string(e.what()));
|
|
|
+ // 尝试重新初始化音频设备
|
|
|
+ try {
|
|
|
+ m_audioOutput->stop();
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
+ m_audioOutput->start();
|
|
|
+ } catch (const std::exception& e2) {
|
|
|
+ Logger::instance().error("Failed to restart audio output: " + std::string(e2.what()));
|
|
|
+ Logger::instance().warning("Continuing playback without audio");
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
Logger::instance().info("Starting audio output device...");
|
|
|
- m_audioOutput->start();
|
|
|
-
|
|
|
- // 检查音频设备是否成功启动
|
|
|
- if (!m_audioOutput->isPlaying()) {
|
|
|
- Logger::instance().error("Audio output device failed to start");
|
|
|
- // 不要因为音频失败而停止整个播放,继续播放视频
|
|
|
- Logger::instance().warning("Continuing playback without audio");
|
|
|
- } else {
|
|
|
- Logger::instance().info("Audio output device started successfully");
|
|
|
- }
|
|
|
+ try {
|
|
|
+ m_audioOutput->start();
|
|
|
+
|
|
|
+ // 检查音频设备是否成功启动
|
|
|
+ if (!m_audioOutput->isPlaying()) {
|
|
|
+ Logger::instance().error("Audio output device failed to start");
|
|
|
+ // 不要因为音频失败而停止整个播放,继续播放视频
|
|
|
+ Logger::instance().warning("Continuing playback without audio");
|
|
|
+ }
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ Logger::instance().error("Exception starting audio output: " + std::string(e.what()));
|
|
|
+ Logger::instance().warning("Continuing playback without audio");
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 设置音频参数
|
|
|
- m_audioOutput->setVolume(m_volume);
|
|
|
- m_audioOutput->setPlaybackSpeed(m_playbackSpeed);
|
|
|
-
|
|
|
- // 给音频输出设备更多时间初始化
|
|
|
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
- }
|
|
|
-
|
|
|
- // 启动线程
|
|
|
- m_threadsShouldStop = false;
|
|
|
-
|
|
|
- if (!startReadThread()) {
|
|
|
- Logger::instance().error("Failed to start read thread");
|
|
|
- return ErrorCode::THREAD_ERROR;
|
|
|
- }
|
|
|
-
|
|
|
- if (!startDecodeThreads()) {
|
|
|
- Logger::instance().error("Failed to start decode threads");
|
|
|
- return ErrorCode::THREAD_ERROR;
|
|
|
- }
|
|
|
-
|
|
|
- if (m_mediaInfo.hasVideo && m_videoStreamEnabled && !startVideoPlayThread()) {
|
|
|
- Logger::instance().error("Failed to start video play thread");
|
|
|
- return ErrorCode::THREAD_ERROR;
|
|
|
}
|
|
|
|
|
|
- if (m_mediaInfo.hasAudio && m_audioStreamEnabled && !startAudioPlayThread()) {
|
|
|
- Logger::instance().error("Failed to start audio play thread");
|
|
|
- return ErrorCode::THREAD_ERROR;
|
|
|
+ // 根据状态启动或恢复线程
|
|
|
+ if (m_state == PlayerState::Stopped) {
|
|
|
+ // 从停止状态开始,需要启动所有线程
|
|
|
+
|
|
|
+ // 启动播放线程
|
|
|
+ if (m_mediaInfo.hasVideo && !startVideoPlayThread()) {
|
|
|
+ Logger::instance().error("Failed to start video play thread");
|
|
|
+ return ErrorCode::THREAD_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_mediaInfo.hasAudio && !startAudioPlayThread()) {
|
|
|
+ Logger::instance().error("Failed to start audio play thread");
|
|
|
+ return ErrorCode::THREAD_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 启动解码线程
|
|
|
+ if (!startDecodeThreads()) {
|
|
|
+ Logger::instance().error("Failed to start decode threads");
|
|
|
+ return ErrorCode::THREAD_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 启动读取线程
|
|
|
+ if (!startReadThread()) {
|
|
|
+ Logger::instance().error("Failed to start read thread");
|
|
|
+ return ErrorCode::THREAD_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ m_threadsRunning = true;
|
|
|
}
|
|
|
-
|
|
|
- m_threadsRunning = true;
|
|
|
+ // 从暂停状态恢复时,线程已经在运行,只需要唤醒它们(已在上面完成)
|
|
|
setState(PlayerState::Playing);
|
|
|
|
|
|
Logger::instance().info("Playback started");
|
|
|
@@ -332,6 +352,17 @@ ErrorCode PlayerCoreV2::pause() {
|
|
|
Logger::instance().debug("Not playing, cannot pause");
|
|
|
return ErrorCode::INVALID_STATE;
|
|
|
}
|
|
|
+ // 设置暂停标志,让线程在内部循环中等待,而不是停止线程
|
|
|
+ // 类似ffplay.c中的paused标志机制
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> lock(m_pauseMutex);
|
|
|
+ Logger::instance().info("Setting m_paused to true");
|
|
|
+ m_paused = true;
|
|
|
+ Logger::instance().info("m_paused set to: " + std::to_string(m_paused.load()));
|
|
|
+ }
|
|
|
+ // 立即通知所有等待的线程检查暂停状态
|
|
|
+ Logger::instance().info("Notifying all threads about pause state change");
|
|
|
+ m_pauseCondition.notify_all();
|
|
|
|
|
|
// 暂停同步器
|
|
|
if (m_synchronizer && m_synchronizer->pause() != ErrorCode::SUCCESS) {
|
|
|
@@ -351,9 +382,9 @@ ErrorCode PlayerCoreV2::pause() {
|
|
|
if (m_audioOutput) {
|
|
|
m_audioOutput->pause();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
setState(PlayerState::Paused);
|
|
|
- Logger::instance().info("Playback paused");
|
|
|
+ Logger::instance().info("Playback paused (threads continue running)");
|
|
|
return ErrorCode::SUCCESS;
|
|
|
}
|
|
|
|
|
|
@@ -375,10 +406,17 @@ ErrorCode PlayerCoreV2::stop() {
|
|
|
m_audioOutput->stop();
|
|
|
}
|
|
|
|
|
|
- // 清空OpenGL视频渲染器
|
|
|
- if (m_openGLVideoRenderer) {
|
|
|
- m_openGLVideoRenderer->Close();
|
|
|
+ // 通知关闭视频渲染器
|
|
|
+ if (m_eventCallback) {
|
|
|
+ m_eventCallback->onVideoRendererCloseRequired();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除暂停标志并唤醒所有等待的线程
|
|
|
+ {
|
|
|
+ std::lock_guard<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_paused = false;
|
|
|
}
|
|
|
+ m_pauseCondition.notify_all();
|
|
|
|
|
|
// 停止所有线程
|
|
|
stopAllThreads();
|
|
|
@@ -511,22 +549,20 @@ int64_t PlayerCoreV2::getCurrentTime() const {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
-
|
|
|
- // // 回退到原来的计算方式(兼容性保证)
|
|
|
- // if (m_state == PlayerState::Paused) {
|
|
|
- // return m_baseTime;
|
|
|
- // }
|
|
|
+ // 回退到原来的计算方式(兼容性保证)
|
|
|
+ if (m_state == PlayerState::Paused) {
|
|
|
+ return m_baseTime;
|
|
|
+ }
|
|
|
|
|
|
- // if (m_playStartTime.time_since_epoch().count() == 0) {
|
|
|
- // return m_baseTime;
|
|
|
- // }
|
|
|
+ if (m_playStartTime.time_since_epoch().count() == 0) {
|
|
|
+ return m_baseTime;
|
|
|
+ }
|
|
|
|
|
|
- // auto currentTime = std::chrono::steady_clock::now();
|
|
|
- // auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
- // currentTime - m_playStartTime).count();
|
|
|
+ auto currentTime = std::chrono::steady_clock::now();
|
|
|
+ auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
+ currentTime - m_playStartTime).count();
|
|
|
|
|
|
- // return m_baseTime + static_cast<int64_t>(elapsed * m_playbackSpeed);
|
|
|
+ return m_baseTime + static_cast<int64_t>(elapsed * m_playbackSpeed);
|
|
|
}
|
|
|
|
|
|
double PlayerCoreV2::getPlaybackSpeed() const {
|
|
|
@@ -619,9 +655,9 @@ SyncConfigV2 PlayerCoreV2::getSyncConfig() const {
|
|
|
return SyncConfigV2();
|
|
|
}
|
|
|
|
|
|
-void PlayerCoreV2::setOpenGLVideoRenderer(OpenGLVideoWidget* renderer) {
|
|
|
- m_openGLVideoRenderer = renderer;
|
|
|
-}
|
|
|
+// void PlayerCoreV2::setOpenGLVideoRenderer(OpenGLVideoWidget* renderer) {
|
|
|
+// m_openGLVideoRenderer = renderer;
|
|
|
+// }
|
|
|
|
|
|
AVFrame* PlayerCoreV2::getNextVideoFrame() {
|
|
|
if (!m_videoFrameQueue || m_state != PlayerState::Playing) {
|
|
|
@@ -948,6 +984,12 @@ void PlayerCoreV2::resetDecoders() {
|
|
|
|
|
|
bool PlayerCoreV2::startReadThread() {
|
|
|
try {
|
|
|
+ // 确保之前的线程已经完全停止
|
|
|
+ if (m_readThread.joinable()) {
|
|
|
+ Logger::instance().warning("Read thread still running, waiting for it to stop...");
|
|
|
+ m_readThread.join();
|
|
|
+ }
|
|
|
+
|
|
|
m_readThread = std::thread(&PlayerCoreV2::readThreadFunc, this);
|
|
|
Logger::instance().info("Read thread started");
|
|
|
return true;
|
|
|
@@ -960,11 +1002,23 @@ bool PlayerCoreV2::startReadThread() {
|
|
|
bool PlayerCoreV2::startDecodeThreads() {
|
|
|
try {
|
|
|
if (m_mediaInfo.hasVideo) {
|
|
|
+ // 确保之前的视频解码线程已经完全停止
|
|
|
+ if (m_videoDecodeThread.joinable()) {
|
|
|
+ Logger::instance().warning("Video decode thread still running, waiting for it to stop...");
|
|
|
+ m_videoDecodeThread.join();
|
|
|
+ }
|
|
|
+
|
|
|
m_videoDecodeThread = std::thread(&PlayerCoreV2::videoDecodeThreadFunc, this);
|
|
|
Logger::instance().info("Video decode thread started");
|
|
|
}
|
|
|
|
|
|
if (m_mediaInfo.hasAudio) {
|
|
|
+ // 确保之前的音频解码线程已经完全停止
|
|
|
+ if (m_audioDecodeThread.joinable()) {
|
|
|
+ Logger::instance().warning("Audio decode thread still running, waiting for it to stop...");
|
|
|
+ m_audioDecodeThread.join();
|
|
|
+ }
|
|
|
+
|
|
|
m_audioDecodeThread = std::thread(&PlayerCoreV2::audioDecodeThreadFunc, this);
|
|
|
Logger::instance().info("Audio decode thread started");
|
|
|
}
|
|
|
@@ -978,6 +1032,12 @@ bool PlayerCoreV2::startDecodeThreads() {
|
|
|
|
|
|
bool PlayerCoreV2::startVideoPlayThread() {
|
|
|
try {
|
|
|
+ // 确保之前的视频播放线程已经完全停止
|
|
|
+ if (m_videoPlayThread.joinable()) {
|
|
|
+ Logger::instance().warning("Video play thread still running, waiting for it to stop...");
|
|
|
+ m_videoPlayThread.join();
|
|
|
+ }
|
|
|
+
|
|
|
m_videoPlayThread = std::thread(&PlayerCoreV2::videoPlayThreadFunc, this);
|
|
|
Logger::instance().info("Video play thread started");
|
|
|
return true;
|
|
|
@@ -989,6 +1049,12 @@ bool PlayerCoreV2::startVideoPlayThread() {
|
|
|
|
|
|
bool PlayerCoreV2::startAudioPlayThread() {
|
|
|
try {
|
|
|
+ // 确保之前的音频播放线程已经完全停止
|
|
|
+ if (m_audioPlayThread.joinable()) {
|
|
|
+ Logger::instance().warning("Audio play thread still running, waiting for it to stop...");
|
|
|
+ m_audioPlayThread.join();
|
|
|
+ }
|
|
|
+
|
|
|
m_audioPlayThread = std::thread(&PlayerCoreV2::audioPlayThreadFunc, this);
|
|
|
Logger::instance().info("Audio play thread started");
|
|
|
return true;
|
|
|
@@ -1128,23 +1194,10 @@ void PlayerCoreV2::updatePerformanceStats() {
|
|
|
}
|
|
|
|
|
|
double PlayerCoreV2::calculateCpuUsage() {
|
|
|
-#ifdef _WIN32
|
|
|
- FILETIME idleTime, kernelTime, userTime;
|
|
|
- if (GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
|
|
|
- // 简化的CPU使用率计算
|
|
|
- return 0.0; // 实际实现需要更复杂的逻辑
|
|
|
- }
|
|
|
-#endif
|
|
|
return 0.0;
|
|
|
}
|
|
|
|
|
|
double PlayerCoreV2::calculateMemoryUsage() {
|
|
|
-#ifdef _WIN32
|
|
|
- PROCESS_MEMORY_COUNTERS pmc;
|
|
|
- if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
|
|
|
- return pmc.WorkingSetSize / (1024.0 * 1024.0); // MB
|
|
|
- }
|
|
|
-#endif
|
|
|
return 0.0;
|
|
|
}
|
|
|
|
|
|
@@ -1153,25 +1206,26 @@ void PlayerCoreV2::stopAllThreads() {
|
|
|
|
|
|
m_threadsShouldStop = true;
|
|
|
|
|
|
- // 等待线程结束
|
|
|
- if (m_readThread.joinable()) {
|
|
|
- m_readThread.join();
|
|
|
- }
|
|
|
- if (m_videoDecodeThread.joinable()) {
|
|
|
- m_videoDecodeThread.join();
|
|
|
- }
|
|
|
- if (m_audioDecodeThread.joinable()) {
|
|
|
- m_audioDecodeThread.join();
|
|
|
- }
|
|
|
- if (m_videoPlayThread.joinable()) {
|
|
|
- m_videoPlayThread.join();
|
|
|
- }
|
|
|
- if (m_audioPlayThread.joinable()) {
|
|
|
- m_audioPlayThread.join();
|
|
|
- }
|
|
|
+ // 唤醒所有等待的队列,确保线程能够检查停止标志
|
|
|
+ flushBuffers();
|
|
|
+
|
|
|
+ // 等待线程结束,添加超时保护
|
|
|
+ auto joinWithTimeout = [](std::thread& t, const std::string& name) {
|
|
|
+ if (t.joinable()) {
|
|
|
+ Logger::instance().info("Waiting for " + name + " to stop...");
|
|
|
+ t.join();
|
|
|
+ Logger::instance().info(name + " stopped");
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ joinWithTimeout(m_readThread, "read thread");
|
|
|
+ joinWithTimeout(m_videoDecodeThread, "video decode thread");
|
|
|
+ joinWithTimeout(m_audioDecodeThread, "audio decode thread");
|
|
|
+ joinWithTimeout(m_videoPlayThread, "video play thread");
|
|
|
+ joinWithTimeout(m_audioPlayThread, "audio play thread");
|
|
|
|
|
|
m_threadsRunning = false;
|
|
|
- Logger::instance().info("All threads stopped");
|
|
|
+ Logger::instance().info("All threads stopped successfully");
|
|
|
}
|
|
|
|
|
|
void PlayerCoreV2::flushBuffers() {
|
|
|
@@ -1203,6 +1257,21 @@ void PlayerCoreV2::readThreadFunc() {
|
|
|
}
|
|
|
|
|
|
while (!m_threadsShouldStop) {
|
|
|
+ // 检查暂停状态,类似ffplay.c中的paused检查
|
|
|
+ bool pausedState = m_paused.load();
|
|
|
+ Logger::instance().debug("Video play thread loop - m_paused: " + std::to_string(pausedState));
|
|
|
+ if (pausedState) {
|
|
|
+ Logger::instance().info("Video play thread entering pause wait");
|
|
|
+ std::unique_lock<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_pauseCondition.wait(lock, [this] { return !m_paused || m_threadsShouldStop; });
|
|
|
+ if (m_threadsShouldStop) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Logger::instance().info("Video play thread exiting pause wait");
|
|
|
+ // 暂停状态结束后,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
// 检查是否需要seek
|
|
|
if (m_seeking) {
|
|
|
std::unique_lock<std::mutex> lock(m_seekMutex);
|
|
|
@@ -1350,6 +1419,21 @@ void PlayerCoreV2::videoDecodeThreadFunc() {
|
|
|
int packetCount = 0;
|
|
|
int frameCount = 0;
|
|
|
while (!m_threadsShouldStop) {
|
|
|
+ // 检查暂停状态,类似ffplay.c中的paused检查
|
|
|
+ bool pausedState = m_paused.load();
|
|
|
+ Logger::instance().debug("Video decode thread loop - m_paused: "
|
|
|
+ + std::to_string(pausedState));
|
|
|
+ if (pausedState) {
|
|
|
+ Logger::instance().info("Video decode thread entering pause wait");
|
|
|
+ std::unique_lock<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_pauseCondition.wait(lock, [this] { return !m_paused || m_threadsShouldStop; });
|
|
|
+ if (m_threadsShouldStop) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Logger::instance().info("Video decode thread exiting pause wait");
|
|
|
+ // 暂停状态结束后,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
if (!m_videoPacketQueue || !m_videoFrameQueue || !m_videoDecoder) {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
continue;
|
|
|
@@ -1455,6 +1539,22 @@ void PlayerCoreV2::audioDecodeThreadFunc() {
|
|
|
int packetCount = 0;
|
|
|
int frameCount = 0;
|
|
|
while (!m_threadsShouldStop) {
|
|
|
+ // 检查暂停状态,类似ffplay.c中的paused检查
|
|
|
+ bool pausedState = m_paused.load();
|
|
|
+ Logger::instance().debug("Audio decode thread loop - m_paused: "
|
|
|
+ + std::to_string(pausedState));
|
|
|
+ if (pausedState) {
|
|
|
+ Logger::instance().info("Audio decode thread entering pause wait");
|
|
|
+ std::unique_lock<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_pauseCondition.wait(lock, [this] { return !m_paused || m_threadsShouldStop; });
|
|
|
+ if (m_threadsShouldStop) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Logger::instance().info("Audio decode thread exiting pause wait");
|
|
|
+ // 暂停状态结束后,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
if (!m_audioPacketQueue || !m_audioFrameQueue || !m_audioDecoder) {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
continue;
|
|
|
@@ -1556,13 +1656,35 @@ void PlayerCoreV2::videoPlayThreadFunc() {
|
|
|
AVFrame* lastFrame = nullptr;
|
|
|
|
|
|
while (!m_threadsShouldStop) {
|
|
|
+ // 检查暂停状态,类似ffplay.c中的paused检查
|
|
|
+ bool pausedState = m_paused.load();
|
|
|
+ Logger::instance().debug("Video play thread loop - m_paused: "
|
|
|
+ + std::to_string(pausedState));
|
|
|
+ if (pausedState) {
|
|
|
+ Logger::instance().info("Video play thread entering pause wait");
|
|
|
+ std::unique_lock<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_pauseCondition.wait(lock, [this] { return !m_paused || m_threadsShouldStop; });
|
|
|
+ if (m_threadsShouldStop) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Logger::instance().info("Video play thread exiting pause wait");
|
|
|
+ // 暂停状态结束后,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
if (!m_videoFrameQueue || !m_synchronizer) {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- // 获取视频帧,使用超时避免无限阻塞
|
|
|
- AVFrame* frame = m_videoFrameQueue->pop(10); // 减少超时时间以提高seek响应速度
|
|
|
+ // 在pop之前再次检查暂停状态,避免在pop期间阻塞
|
|
|
+ if (m_paused.load()) {
|
|
|
+ Logger::instance().debug("Video play thread detected pause before pop");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取视频帧,使用更短的超时时间以提高暂停响应速度
|
|
|
+ AVFrame* frame = m_videoFrameQueue->pop(1); // 使用1ms超时以快速响应暂停
|
|
|
if (!frame) {
|
|
|
// 检查是否应该继续等待
|
|
|
if (m_threadsShouldStop) {
|
|
|
@@ -1572,6 +1694,15 @@ void PlayerCoreV2::videoPlayThreadFunc() {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // 获取帧后立即检查暂停状态,如果已暂停则释放帧并继续
|
|
|
+ bool pausedAfterPop = m_paused.load();
|
|
|
+ Logger::instance().debug("Video play thread after pop - m_paused: " + std::to_string(pausedAfterPop));
|
|
|
+ if (pausedAfterPop) {
|
|
|
+ Logger::instance().info("Video play thread releasing frame due to pause");
|
|
|
+ av_frame_free(&frame);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
Logger::instance().debug("Video play thread got frame, pts=" + std::to_string(frame->pts));
|
|
|
|
|
|
// 创建智能指针管理帧内存
|
|
|
@@ -1580,7 +1711,7 @@ void PlayerCoreV2::videoPlayThreadFunc() {
|
|
|
// 使用同步器计算视频帧显示时间
|
|
|
double pts = frame->pts * av_q2d(m_formatContext->streams[m_mediaInfo.videoStreamIndex]->time_base);
|
|
|
|
|
|
- // 更新视频时钟
|
|
|
+ // 只在非暂停状态下更新时钟和处理帧
|
|
|
m_synchronizer->setClock(av::utils::ClockType::VIDEO, pts, 0);
|
|
|
|
|
|
// 计算帧持续时间
|
|
|
@@ -1600,17 +1731,17 @@ void PlayerCoreV2::videoPlayThreadFunc() {
|
|
|
|
|
|
// 计算视频帧延迟并决定是否显示
|
|
|
FrameDecision decision = m_synchronizer->synchronizeVideo(pts, 0, last_duration);
|
|
|
-
|
|
|
+
|
|
|
if (decision.action == FrameAction::DISPLAY) {
|
|
|
// 如果需要延迟显示,等待指定时间
|
|
|
if (decision.delay > 0.0) {
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int64_t>(decision.delay * 1000000)));
|
|
|
}
|
|
|
|
|
|
- // 显示帧
|
|
|
- if (m_openGLVideoRenderer) {
|
|
|
- m_openGLVideoRenderer->Render(framePtr.get());
|
|
|
- Logger::instance().debug("Video frame displayed, pts=" + std::to_string(pts) + ", delay=" + std::to_string(decision.delay));
|
|
|
+ // 通知显示帧
|
|
|
+ if (m_eventCallback) {
|
|
|
+ m_eventCallback->onVideoFrameReady(framePtr.get());
|
|
|
+ Logger::instance().debug("Video frame ready for display, pts=" + std::to_string(pts) + ", delay=" + std::to_string(decision.delay));
|
|
|
}
|
|
|
|
|
|
// 更新上一帧指针
|
|
|
@@ -1636,13 +1767,35 @@ void PlayerCoreV2::audioPlayThreadFunc() {
|
|
|
|
|
|
int frameCount = 0;
|
|
|
while (!m_threadsShouldStop) {
|
|
|
+ // 检查暂停状态,类似ffplay.c中的paused检查
|
|
|
+ bool pausedState = m_paused.load();
|
|
|
+ Logger::instance().debug("Audio play thread loop - m_paused: "
|
|
|
+ + std::to_string(pausedState));
|
|
|
+ if (pausedState) {
|
|
|
+ Logger::instance().info("Audio play thread entering pause wait");
|
|
|
+ std::unique_lock<std::mutex> lock(m_pauseMutex);
|
|
|
+ m_pauseCondition.wait(lock, [this] { return !m_paused || m_threadsShouldStop; });
|
|
|
+ if (m_threadsShouldStop) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Logger::instance().info("Audio play thread exiting pause wait");
|
|
|
+ // 暂停状态结束后,继续下一次循环
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
if (!m_audioFrameQueue || !m_synchronizer || !m_audioOutput) {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- // 获取音频帧,使用较短的超时时间以提高seek响应速度
|
|
|
- AVFrame* frame = m_audioFrameQueue->pop(10); // 减少超时时间以提高seek响应速度
|
|
|
+ // 在pop之前再次检查暂停状态,避免在pop期间阻塞
|
|
|
+ if (m_paused.load()) {
|
|
|
+ Logger::instance().debug("Audio play thread detected pause before pop");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取音频帧,使用更短的超时时间以提高暂停响应速度
|
|
|
+ AVFrame* frame = m_audioFrameQueue->pop(1); // 使用1ms超时以快速响应暂停
|
|
|
if (!frame) {
|
|
|
// 检查是否应该继续等待
|
|
|
if (m_threadsShouldStop) {
|
|
|
@@ -1651,7 +1804,16 @@ void PlayerCoreV2::audioPlayThreadFunc() {
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ // 获取帧后立即检查暂停状态,如果已暂停则释放帧并继续
|
|
|
+ bool pausedAfterPop = m_paused.load();
|
|
|
+ Logger::instance().debug("Audio play thread after pop - m_paused: " + std::to_string(pausedAfterPop));
|
|
|
+ if (pausedAfterPop) {
|
|
|
+ Logger::instance().info("Audio play thread releasing frame due to pause");
|
|
|
+ av_frame_free(&frame);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
// 检查是否是EOF帧
|
|
|
if (frame->data[0] == nullptr && frame->nb_samples == 0) {
|
|
|
Logger::instance().info("Audio play thread received EOF frame, playback completed");
|
|
|
@@ -1675,7 +1837,7 @@ void PlayerCoreV2::audioPlayThreadFunc() {
|
|
|
double pts = frame->pts
|
|
|
* av_q2d(m_formatContext->streams[m_mediaInfo.audioStreamIndex]->time_base);
|
|
|
|
|
|
- // 更新音频时钟
|
|
|
+ // 只在非暂停状态下更新音频时钟
|
|
|
m_synchronizer->setClock(av::utils::ClockType::AUDIO, pts, 0);
|
|
|
|
|
|
// 同步音频样本数量
|
|
|
@@ -1688,7 +1850,9 @@ void PlayerCoreV2::audioPlayThreadFunc() {
|
|
|
Logger::instance().debug("Audio samples adjusted from " + std::to_string(originalSamples)
|
|
|
+ " to " + std::to_string(adjustedSamples) + " for sync");
|
|
|
}
|
|
|
-
|
|
|
+ if (m_paused) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
{
|
|
|
Logger::instance().debug("Writing audio frame to output device");
|
|
|
bool writeResult = m_audioOutput->writeFrame(framePtr);
|