| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651 |
- #include "player_core_v2.h"
- #include "../base/media_common.h"
- #include "../base/logger.h"
- #include "../base/types.h"
- #include "../codec/codec_video_decoder.h"
- #include "../codec/codec_audio_decoder.h"
- #include "../utils/utils_synchronizer_v2.h"
- #include <chrono>
- #include <thread>
- #include <algorithm>
- #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_volume(1.0)
- , m_playbackSpeed(1.0)
- , m_seekTarget(-1)
- , m_seeking(false)
- , m_baseTime(0)
- , m_lastUpdateTime(0)
- , m_threadsShouldStop(false)
- , m_threadsRunning(false)
- , m_initialized(false)
- , m_frameCount(0)
- , m_lastFrameCount(0)
- , m_errorCount(0)
- , m_buffering(false)
- , m_bufferHealth(1.0)
- {
- Logger::instance().info("PlayerCoreV2 created");
- try {
- // 初始化FFmpeg
- if (!initializeFFmpeg()) {
- Logger::instance().error("Failed to initialize FFmpeg");
- setState(PlayerState::Error);
- return;
- }
-
- // 创建分离的视频和音频包队列 - 使用高容量队列以避免丢帧
- m_videoPacketQueue = av::utils::PacketQueueFactory::createHighCapacityQueue(2000); // 视频包队列,增大容量
- if (!m_videoPacketQueue) {
- Logger::instance().error("Failed to create video packet queue");
- setState(PlayerState::Error);
- return;
- }
- // 禁用丢包策略
- m_videoPacketQueue->setDropPolicy(false, true);
-
- m_audioPacketQueue = av::utils::PacketQueueFactory::createHighCapacityQueue(2000); // 音频包队列,增大容量
- if (!m_audioPacketQueue) {
- Logger::instance().error("Failed to create audio packet queue");
- setState(PlayerState::Error);
- return;
- }
- // 禁用丢包策略
- m_audioPacketQueue->setDropPolicy(false, true);
-
- m_videoFrameQueue = av::utils::FrameQueueFactory::createHighCapacityQueue(100); // 增加视频帧队列容量并禁用丢帧
- if (!m_videoFrameQueue) {
- Logger::instance().error("Failed to create video frame queue");
- setState(PlayerState::Error);
- return;
- }
- // 禁用丢帧策略
- m_videoFrameQueue->setDropPolicy(false, true);
-
- m_audioFrameQueue = av::utils::FrameQueueFactory::createHighCapacityQueue(400); // 增加音频帧队列容量并禁用丢帧
- if (!m_audioFrameQueue) {
- Logger::instance().error("Failed to create audio frame queue");
- setState(PlayerState::Error);
- return;
- }
- // 禁用丢帧策略
- m_audioFrameQueue->setDropPolicy(false, true);
-
- // 创建改进的同步器
- m_synchronizer = std::make_unique<SynchronizerV2>(syncConfig);
- if (!m_synchronizer) {
- Logger::instance().error("Failed to create synchronizer");
- setState(PlayerState::Error);
- return;
- }
- // 设置同步器回调
- m_synchronizer->setSyncErrorCallback([this](double error, const std::string& reason) {
- handleSyncError(error, reason);
- });
- m_synchronizer->setFrameDropCallback([this](av::utils::ClockType type, int64_t pts) {
- std::lock_guard<std::mutex> lock(m_mutex);
- m_stats.droppedFrames++;
- if (m_eventCallback) {
- m_eventCallback->onFrameDropped(m_stats.droppedFrames);
- }
- });
- // 创建解码器
- m_videoDecoder = std::make_unique<VideoDecoder>();
- if (!m_videoDecoder) {
- Logger::instance().error("Failed to create video decoder");
- setState(PlayerState::Error);
- return;
- }
-
- m_audioDecoder = std::make_unique<AudioDecoder>();
- if (!m_audioDecoder) {
- Logger::instance().error("Failed to create audio decoder");
- setState(PlayerState::Error);
- return;
- }
-
- // 创建音频输出设备
- m_audioOutput = std::make_unique<AudioOutput>();
- if (!m_audioOutput) {
- Logger::instance().error("Failed to create audio output");
- setState(PlayerState::Error);
- return;
- }
-
- // 初始化性能监控
- 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");
- } catch (const std::exception& e) {
- Logger::instance().error("Exception during PlayerCoreV2 initialization: " + std::string(e.what()));
- setState(PlayerState::Error);
- m_initialized = false;
- } catch (...) {
- Logger::instance().error("Unknown exception during PlayerCoreV2 initialization");
- setState(PlayerState::Error);
- m_initialized = false;
- }
- }
- PlayerCoreV2::~PlayerCoreV2() {
- Logger::instance().info("PlayerCoreV2 destroying...");
- stop();
- cleanup();
- Logger::instance().info("PlayerCoreV2 destroyed");
- }
- void PlayerCoreV2::setEventCallback(PlayerEventCallback* callback) {
- std::lock_guard<std::mutex> lock(m_mutex);
- m_eventCallback = callback;
- }
- ErrorCode PlayerCoreV2::openFile(const std::string& filename) {
- Logger::instance().info("Opening file: " + filename);
-
- if (!m_initialized) {
- Logger::instance().error("PlayerCoreV2 not initialized");
- return ErrorCode::NOT_INITIALIZED;
- }
-
- // 如果正在播放,先停止
- if (m_state != PlayerState::Idle) {
- stop();
- }
-
- setState(PlayerState::Opening);
-
- // 打开媒体文件
- if (!openMediaFile(filename)) {
- setState(PlayerState::Error);
- notifyError("Failed to open media file: " + filename);
- return ErrorCode::FILE_OPEN_FAILED;
- }
-
- // 设置媒体信息
- m_mediaInfo.filename = filename;
-
- // 设置解码器
- if (m_mediaInfo.hasVideo && !setupVideoDecoder()) {
- Logger::instance().error("Failed to setup video decoder");
- setState(PlayerState::Error);
- return ErrorCode::CODEC_OPEN_FAILED;
- }
-
- if (m_mediaInfo.hasAudio && !setupAudioDecoder()) {
- Logger::instance().error("Failed to setup audio decoder");
- setState(PlayerState::Error);
- return ErrorCode::CODEC_OPEN_FAILED;
- }
-
- // 注意:同步器的初始化已经在openMediaFile()中完成,这里不需要重复调用
- // 因为initialize()会重新选择主时钟,可能覆盖之前设置的流信息
-
- setState(PlayerState::Stopped);
-
- // 如果已设置视频渲染器且有视频流,重新初始化渲染器
- if (m_mediaInfo.hasVideo && m_openGLVideoRenderer) {
- AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
-
- bool rendererInitResult = m_openGLVideoRenderer->Open(
- 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");
- }
- }
-
- // 通知媒体信息变化
- if (m_eventCallback) {
- m_eventCallback->onMediaInfoChanged(m_mediaInfo);
- }
-
- Logger::instance().info("File opened successfully: " + filename);
- return ErrorCode::SUCCESS;
- }
- ErrorCode PlayerCoreV2::play() {
- Logger::instance().info("Starting playback");
-
- if (m_state == PlayerState::Playing) {
- Logger::instance().debug("Already playing");
- return ErrorCode::SUCCESS;
- }
-
- if (m_state != PlayerState::Stopped && m_state != PlayerState::Paused) {
- Logger::instance().error("Invalid state for play: " + std::to_string(static_cast<int>(m_state.load())));
- return ErrorCode::INVALID_STATE;
- }
-
- // 启动同步器
- if (m_synchronizer && m_synchronizer->start() != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to start 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;
-
- // 重置统计信息
- std::lock_guard<std::mutex> lock(m_mutex);
- m_stats = PlaybackStats();
- m_stats.playbackSpeed = m_playbackSpeed;
- }
-
- // 启动音频输出设备
- if (m_audioOutput && m_mediaInfo.hasAudio) {
- if (m_state == PlayerState::Paused) {
- m_audioOutput->resume();
- } 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");
- }
- }
-
- // 设置音频参数
- 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;
- }
-
- m_threadsRunning = true;
- setState(PlayerState::Playing);
-
- Logger::instance().info("Playback started");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PlayerCoreV2::pause() {
- Logger::instance().info("Pausing playback");
-
- if (m_state != PlayerState::Playing) {
- Logger::instance().debug("Not playing, cannot pause");
- return ErrorCode::INVALID_STATE;
- }
- // 暂停同步器
- if (m_synchronizer && m_synchronizer->pause() != ErrorCode::SUCCESS) {
- Logger::instance().warning("Failed to pause synchronizer");
- }
- // 记录暂停时的播放时间
- if (m_playStartTime.time_since_epoch().count() != 0) {
- auto currentTime = std::chrono::steady_clock::now();
- auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
- currentTime - m_playStartTime).count();
- m_baseTime += static_cast<int64_t>(elapsed * m_playbackSpeed);
- m_playStartTime = std::chrono::steady_clock::time_point{};
- }
-
- // 暂停音频输出
- if (m_audioOutput) {
- m_audioOutput->pause();
- }
-
- setState(PlayerState::Paused);
- Logger::instance().info("Playback paused");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PlayerCoreV2::stop() {
- Logger::instance().info("Stopping playback");
-
- if (m_state == PlayerState::Idle || m_state == PlayerState::Stopped) {
- Logger::instance().debug("Already stopped");
- return ErrorCode::SUCCESS;
- }
- // 停止同步器
- if (m_synchronizer) {
- m_synchronizer->stop();
- }
- // 停止音频输出
- if (m_audioOutput) {
- m_audioOutput->stop();
- }
-
- // 清空OpenGL视频渲染器
- if (m_openGLVideoRenderer) {
- m_openGLVideoRenderer->Close();
- }
-
- // 停止所有线程
- stopAllThreads();
-
- // 重置解码器
- resetDecoders();
-
- // 清空队列
- if (m_videoPacketQueue) m_videoPacketQueue->clear();
- if (m_audioPacketQueue) m_audioPacketQueue->clear();
- if (m_videoFrameQueue) m_videoFrameQueue->clear();
- if (m_audioFrameQueue) m_audioFrameQueue->clear();
-
- // 重置时间
- m_baseTime = 0;
- m_playStartTime = std::chrono::steady_clock::time_point{};
-
- setState(PlayerState::Stopped);
- Logger::instance().info("Playback stopped");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PlayerCoreV2::seek(int64_t timestamp) {
- Logger::instance().info("Seeking to: " + std::to_string(timestamp));
-
- if (m_state == PlayerState::Idle || m_state == PlayerState::Opening) {
- Logger::instance().error("Invalid state for seek");
- return ErrorCode::INVALID_STATE;
- }
-
- std::unique_lock<std::mutex> lock(m_seekMutex);
-
- // 设置seek目标
- m_seekTarget = timestamp;
- m_seeking = true;
-
- // 更新基准时间为跳转目标时间
- m_baseTime = timestamp;
- m_playStartTime = std::chrono::steady_clock::now();
- // 重置同步器
- if (m_synchronizer) {
- m_synchronizer->reset();
- }
- // 清空队列
- flushBuffers();
-
- setState(PlayerState::Seeking);
-
- // 通知seek条件
- m_seekCondition.notify_all();
-
- Logger::instance().info("Seek initiated");
- return ErrorCode::SUCCESS;
- }
- ErrorCode PlayerCoreV2::setPlaybackSpeed(double speed) {
- if (speed <= 0.0 || speed > 4.0) {
- Logger::instance().error("Invalid playback speed: " + std::to_string(speed));
- return ErrorCode::INVALID_PARAMS;
- }
-
- std::lock_guard<std::mutex> lock(m_mutex);
- m_playbackSpeed = speed;
- // 设置同步器的播放速度
- if (m_synchronizer) {
- m_synchronizer->setPlaybackSpeed(speed);
- }
- // 设置音频输出的播放速度
- if (m_audioOutput) {
- m_audioOutput->setPlaybackSpeed(speed);
- }
-
- // 更新统计信息
- m_stats.playbackSpeed = speed;
-
- Logger::instance().info("Playback speed set to: " + std::to_string(speed));
- return ErrorCode::SUCCESS;
- }
- MediaInfo PlayerCoreV2::getMediaInfo() const {
- std::lock_guard<std::mutex> lock(m_mutex);
- return m_mediaInfo;
- }
- PlaybackStats PlayerCoreV2::getStats() const {
- std::lock_guard<std::mutex> lock(m_mutex);
- PlaybackStats stats = m_stats;
-
- // 更新当前时间
- stats.currentTime = getCurrentTime();
-
- // 更新队列大小
- if (m_videoPacketQueue) stats.queuedPackets += m_videoPacketQueue->size();
- if (m_audioPacketQueue) stats.queuedPackets += m_audioPacketQueue->size();
- if (m_videoFrameQueue) stats.queuedVideoFrames = m_videoFrameQueue->size();
- if (m_audioFrameQueue) stats.queuedAudioFrames = m_audioFrameQueue->size();
- // 更新同步统计
- if (m_synchronizer) {
- auto syncStats = m_synchronizer->getStats();
- stats.syncError = syncStats.audioVideoSyncError;
- stats.avgSyncError = syncStats.avgSyncError;
- stats.maxSyncError = syncStats.maxSyncError;
- stats.droppedFrames = syncStats.droppedFrames;
- stats.duplicatedFrames = syncStats.duplicatedFrames;
- }
- return stats;
- }
- int64_t PlayerCoreV2::getCurrentTime() const {
- if (m_state == PlayerState::Idle || m_state == PlayerState::Stopped) {
- return 0;
- }
-
- if (m_state == PlayerState::Paused) {
- 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();
-
- return m_baseTime + static_cast<int64_t>(elapsed * m_playbackSpeed);
- }
- double PlayerCoreV2::getPlaybackSpeed() const {
- return m_playbackSpeed;
- }
- void PlayerCoreV2::setVolume(double volume) {
- volume = std::max(0.0, std::min(1.0, volume));
- m_volume = volume;
-
- // 同时设置音频输出设备的音量
- if (m_audioOutput) {
- m_audioOutput->setVolume(volume);
- }
-
- Logger::instance().debug("Volume set to: " + std::to_string(volume));
- }
- // 流控制接口实现
- void PlayerCoreV2::enableVideoStream(bool enable) {
- if (m_videoStreamEnabled == enable) {
- return; // 状态未变,不需要处理
- }
-
- m_videoStreamEnabled = enable;
- Logger::instance().info("Video stream " + std::string(enable ? "enabled" : "disabled"));
-
- // 如果播放器已经打开文件,需要更新同步器的流信息
- if (m_formatContext && m_synchronizer) {
- bool useAudio = m_mediaInfo.hasAudio && m_audioStreamEnabled;
- bool useVideo = m_mediaInfo.hasVideo && m_videoStreamEnabled;
- m_synchronizer->setStreamInfo(useAudio, useVideo);
- // 如果正在播放,需要重新启动播放
- if (m_state == PlayerState::Playing || m_state == PlayerState::Paused) {
- // 停止当前播放
- PlayerState oldState = m_state;
- stop();
- // 重新开始播放
- openFile(m_mediaInfo.filename);
- if (oldState == PlayerState::Playing) {
- play();
- }
- }
- }
- }
- void PlayerCoreV2::enableAudioStream(bool enable) {
- if (m_audioStreamEnabled == enable) {
- return; // 状态未变,不需要处理
- }
-
- m_audioStreamEnabled = enable;
- Logger::instance().info("Audio stream " + std::string(enable ? "enabled" : "disabled"));
-
- // 如果播放器已经打开文件,需要更新同步器的流信息
- if (m_formatContext && m_synchronizer) {
- bool useAudio = m_mediaInfo.hasAudio && m_audioStreamEnabled;
- bool useVideo = m_mediaInfo.hasVideo && m_videoStreamEnabled;
- m_synchronizer->setStreamInfo(useAudio, useVideo);
-
- // 如果正在播放,需要重新启动播放
- if (m_state == PlayerState::Playing) {
- // 暂停当前播放
- pause();
- // 重新开始播放
- play();
- }
- }
- }
- bool PlayerCoreV2::isVideoStreamEnabled() const {
- return m_videoStreamEnabled;
- }
- bool PlayerCoreV2::isAudioStreamEnabled() const {
- return m_audioStreamEnabled;
- }
- void PlayerCoreV2::setSyncConfig(const SyncConfigV2& config) {
- if (m_synchronizer) {
- m_synchronizer->setConfig(config);
- }
- }
- SyncConfigV2 PlayerCoreV2::getSyncConfig() const {
- if (m_synchronizer) {
- return m_synchronizer->getConfig();
- }
- return SyncConfigV2();
- }
- void PlayerCoreV2::setOpenGLVideoRenderer(OpenGLVideoWidget* renderer) {
- m_openGLVideoRenderer = renderer;
- }
- AVFrame* PlayerCoreV2::getNextVideoFrame() {
- if (!m_videoFrameQueue || m_state != PlayerState::Playing) {
- return nullptr;
- }
-
- return m_videoFrameQueue->pop();
- }
- AVFrame* PlayerCoreV2::getNextAudioFrame() {
- if (!m_audioFrameQueue || m_state != PlayerState::Playing) {
- return nullptr;
- }
-
- return m_audioFrameQueue->pop();
- }
- void PlayerCoreV2::releaseVideoFrame(AVFrame* frame) {
- if (frame) {
- av_frame_free(&frame);
- }
- }
- void PlayerCoreV2::releaseAudioFrame(AVFrame* frame) {
- if (frame) {
- av_frame_free(&frame);
- }
- }
- void PlayerCoreV2::update() {
- if (!m_initialized) {
- return;
- }
-
- // 更新同步状态
- updateSynchronization();
-
- // 更新统计信息
- auto now = std::chrono::steady_clock::now();
- if (std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastStatsUpdate).count() > 500) {
- updateStats();
- updatePerformanceStats();
- m_lastStatsUpdate = now;
-
- // 通知位置变化
- notifyPositionChanged();
- }
-
- // 检查错误恢复
- if (m_errorCount > 0) {
- auto timeSinceError = std::chrono::duration_cast<std::chrono::seconds>(now - m_lastErrorTime).count();
- if (timeSinceError > 5) { // 5秒后重置错误计数
- m_errorCount = 0;
- }
- }
- }
- std::string PlayerCoreV2::getDebugInfo() const {
- std::ostringstream oss;
-
- oss << "PlayerCoreV2 Debug Info:\n";
- oss << " State: " << static_cast<int>(m_state.load()) << "\n";
- oss << " Initialized: " << (m_initialized ? "Yes" : "No") << "\n";
- oss << " Threads Running: " << (m_threadsRunning ? "Yes" : "No") << "\n";
- oss << " Current Time: " << getCurrentTime() << " us\n";
- oss << " Playback Speed: " << m_playbackSpeed << "x\n";
- oss << " Volume: " << m_volume << "\n";
- oss << " Error Count: " << m_errorCount << "\n";
- if (m_synchronizer) {
- oss << "\n" << m_synchronizer->getDebugInfo();
- }
- return oss.str();
- }
- void PlayerCoreV2::dumpStats() const {
- PlaybackStats stats = getStats();
-
- Logger::instance().info("=== PlayerCoreV2 Statistics ===");
- Logger::instance().info("Current Time: " + std::to_string(stats.currentTime) + " us");
- Logger::instance().info("Total Frames: " + std::to_string(stats.totalFrames));
- Logger::instance().info("Dropped Frames: " + std::to_string(stats.droppedFrames));
- Logger::instance().info("Duplicated Frames: " + std::to_string(stats.duplicatedFrames));
- Logger::instance().info("Sync Error: " + std::to_string(stats.syncError * 1000) + " ms");
- Logger::instance().info("Avg Sync Error: " + std::to_string(stats.avgSyncError * 1000) + " ms");
- Logger::instance().info("Max Sync Error: " + std::to_string(stats.maxSyncError * 1000) + " ms");
- Logger::instance().info("CPU Usage: " + std::to_string(stats.cpuUsage) + "%");
- Logger::instance().info("Memory Usage: " + std::to_string(stats.memoryUsage) + " MB");
- Logger::instance().info("Queued Packets: " + std::to_string(stats.queuedPackets));
- Logger::instance().info("Queued Video Frames: " + std::to_string(stats.queuedVideoFrames));
- Logger::instance().info("Queued Audio Frames: " + std::to_string(stats.queuedAudioFrames));
- Logger::instance().info("===============================");
- }
- bool PlayerCoreV2::openMediaFile(const std::string& filename) {
- // 关闭之前的文件
- if (m_formatContext) {
- avformat_close_input(&m_formatContext);
- m_formatContext = nullptr;
- }
-
- // 分配格式上下文
- m_formatContext = avformat_alloc_context();
- if (!m_formatContext) {
- Logger::instance().error("Failed to allocate format context");
- return false;
- }
-
- // 打开输入文件
- if (avformat_open_input(&m_formatContext, filename.c_str(), nullptr, nullptr) < 0) {
- Logger::instance().error("Failed to open input file: " + filename);
- avformat_free_context(m_formatContext);
- m_formatContext = nullptr;
- return false;
- }
-
- // 查找流信息
- if (avformat_find_stream_info(m_formatContext, nullptr) < 0) {
- Logger::instance().error("Failed to find stream info");
- avformat_close_input(&m_formatContext);
- return false;
- }
-
- // 查找视频和音频流
- m_mediaInfo.videoStreamIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
- m_mediaInfo.audioStreamIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
-
- m_mediaInfo.hasVideo = (m_mediaInfo.videoStreamIndex >= 0);
- m_mediaInfo.hasAudio = (m_mediaInfo.audioStreamIndex >= 0);
-
- if (!m_mediaInfo.hasVideo && !m_mediaInfo.hasAudio) {
- Logger::instance().error("No video or audio streams found");
- avformat_close_input(&m_formatContext);
- return false;
- }
-
- // 获取媒体信息
- m_mediaInfo.duration = m_formatContext->duration;
- m_mediaInfo.bitrate = m_formatContext->bit_rate;
-
- if (m_mediaInfo.hasVideo) {
- AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
- m_mediaInfo.width = videoStream->codecpar->width;
- m_mediaInfo.height = videoStream->codecpar->height;
-
- // 计算帧率
- if (videoStream->avg_frame_rate.den != 0) {
- m_mediaInfo.fps = av_q2d(videoStream->avg_frame_rate);
- } else if (videoStream->r_frame_rate.den != 0) {
- m_mediaInfo.fps = av_q2d(videoStream->r_frame_rate);
- } else {
- m_mediaInfo.fps = 25.0; // 默认帧率
- }
-
- Logger::instance().info("Video stream found: " + std::to_string(m_mediaInfo.width) + "x" +
- std::to_string(m_mediaInfo.height) + " @ " + std::to_string(m_mediaInfo.fps) + " fps");
- }
-
- if (m_mediaInfo.hasAudio) {
- AVStream* audioStream = m_formatContext->streams[m_mediaInfo.audioStreamIndex];
- m_mediaInfo.sampleRate = audioStream->codecpar->sample_rate;
- m_mediaInfo.channels = audioStream->codecpar->ch_layout.nb_channels;
-
- Logger::instance().info("Audio stream found: " + std::to_string(m_mediaInfo.sampleRate) + " Hz, " +
- std::to_string(m_mediaInfo.channels) + " channels");
- }
- // 设置同步器的流信息
- if (m_synchronizer) {
- bool useAudio = m_mediaInfo.hasAudio && m_audioStreamEnabled;
- bool useVideo = m_mediaInfo.hasVideo && m_videoStreamEnabled;
- m_synchronizer->setStreamInfo(useAudio, useVideo);
- Logger::instance().info("Synchronizer stream info set: hasAudio=" + std::to_string(useAudio) +
- ", hasVideo=" + std::to_string(useVideo));
- // 在设置流信息后初始化同步器,确保主时钟选择基于正确的流信息
- if (m_synchronizer->initialize() != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to initialize synchronizer");
- return false;
- }
- Logger::instance().info("Synchronizer initialized with correct stream info");
- }
- Logger::instance().info("Media file opened successfully: " + filename);
- return true;
- }
- bool PlayerCoreV2::setupVideoDecoder() {
- if (!m_mediaInfo.hasVideo || !m_videoDecoder) {
- return false;
- }
-
- AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
-
- // 查找解码器
- const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id);
- if (!codec) {
- Logger::instance().error("Video codec not found");
- return false;
- }
-
- Logger::instance().info("Found video codec: " + std::string(codec->name));
- Logger::instance().info("Video stream info: width=" + std::to_string(videoStream->codecpar->width) +
- ", height=" + std::to_string(videoStream->codecpar->height) +
- ", format=" + std::to_string(videoStream->codecpar->format));
-
- // 创建视频解码器参数
- VideoDecoderParams videoParams;
- videoParams.codecName = codec->name;
- videoParams.width = videoStream->codecpar->width;
- videoParams.height = videoStream->codecpar->height;
- videoParams.pixelFormat = static_cast<AVPixelFormat>(videoStream->codecpar->format);
- videoParams.hardwareAccel = false; // 先禁用硬件加速来排除问题
- videoParams.lowLatency = false;
-
- Logger::instance().info("Video decoder params: codec=" + videoParams.codecName +
- ", size=" + std::to_string(videoParams.width) + "x" + std::to_string(videoParams.height) +
- ", format=" + std::to_string(static_cast<int>(videoParams.pixelFormat)));
-
- // 初始化视频解码器
- if (m_videoDecoder->initialize(videoParams) != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to initialize video decoder");
- return false;
- }
-
- // 设置流参数
- if (m_videoDecoder->setStreamParameters(videoStream->codecpar) != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to set video decoder stream parameters");
- return false;
- }
-
- // 打开视频解码器
- if (m_videoDecoder->open(videoParams) != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to open video decoder");
- return false;
- }
-
- Logger::instance().info("Video decoder setup successfully");
- return true;
- }
- bool PlayerCoreV2::setupAudioDecoder() {
- if (!m_mediaInfo.hasAudio || !m_audioDecoder) {
- return false;
- }
-
- AVStream* audioStream = m_formatContext->streams[m_mediaInfo.audioStreamIndex];
-
- // 查找解码器
- const AVCodec* codec = avcodec_find_decoder(audioStream->codecpar->codec_id);
- if (!codec) {
- Logger::instance().error("Audio codec not found");
- return false;
- }
-
- // 分配解码器上下文
- AVCodecContext* codecContext = avcodec_alloc_context3(codec);
- if (!codecContext) {
- Logger::instance().error("Failed to allocate audio codec context");
- return false;
- }
-
- // 复制流参数到解码器上下文
- if (avcodec_parameters_to_context(codecContext, audioStream->codecpar) < 0) {
- Logger::instance().error("Failed to copy audio codec parameters");
- avcodec_free_context(&codecContext);
- return false;
- }
-
- // 打开解码器
- if (avcodec_open2(codecContext, codec, nullptr) < 0) {
- Logger::instance().error("Failed to open audio codec");
- avcodec_free_context(&codecContext);
- return false;
- }
-
- // 创建音频解码器参数
- AudioDecoderParams audioParams;
- audioParams.codecName = codec->name;
- audioParams.sampleRate = codecContext->sample_rate;
- audioParams.channels = codecContext->ch_layout.nb_channels;
- audioParams.sampleFormat = codecContext->sample_fmt;
- audioParams.lowLatency = false;
- audioParams.enableResampling = true;
-
- // 初始化音频解码器
- if (m_audioDecoder->initialize(audioParams) != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to initialize audio decoder");
- avcodec_free_context(&codecContext);
- return false;
- }
-
- // 打开音频解码器
- if (m_audioDecoder->open(audioParams) != ErrorCode::SUCCESS) {
- Logger::instance().error("Failed to open audio decoder");
- avcodec_free_context(&codecContext);
- return false;
- }
-
- // 初始化音频输出设备
- if (m_audioOutput && !m_audioOutput->initialize(codecContext->sample_rate,
- codecContext->ch_layout.nb_channels,
- codecContext->sample_fmt)) {
- Logger::instance().error("Failed to initialize audio output");
- avcodec_free_context(&codecContext);
- return false;
- }
- // 释放解码器上下文
- avcodec_free_context(&codecContext);
-
- Logger::instance().info("Audio decoder setup successfully");
- return true;
- }
- void PlayerCoreV2::resetDecoders() {
- if (m_videoDecoder) {
- m_videoDecoder->reset();
- }
- if (m_audioDecoder) {
- m_audioDecoder->reset();
- }
- }
- bool PlayerCoreV2::startReadThread() {
- try {
- m_readThread = std::thread(&PlayerCoreV2::readThreadFunc, this);
- Logger::instance().info("Read thread started");
- return true;
- } catch (const std::exception& e) {
- Logger::instance().error("Failed to start read thread: " + std::string(e.what()));
- return false;
- }
- }
- bool PlayerCoreV2::startDecodeThreads() {
- try {
- if (m_mediaInfo.hasVideo) {
- m_videoDecodeThread = std::thread(&PlayerCoreV2::videoDecodeThreadFunc, this);
- Logger::instance().info("Video decode thread started");
- }
-
- if (m_mediaInfo.hasAudio) {
- m_audioDecodeThread = std::thread(&PlayerCoreV2::audioDecodeThreadFunc, this);
- Logger::instance().info("Audio decode thread started");
- }
-
- return true;
- } catch (const std::exception& e) {
- Logger::instance().error("Failed to start decode threads: " + std::string(e.what()));
- return false;
- }
- }
- bool PlayerCoreV2::startVideoPlayThread() {
- try {
- m_videoPlayThread = std::thread(&PlayerCoreV2::videoPlayThreadFunc, this);
- Logger::instance().info("Video play thread started");
- return true;
- } catch (const std::exception& e) {
- Logger::instance().error("Failed to start video play thread: " + std::string(e.what()));
- return false;
- }
- }
- bool PlayerCoreV2::startAudioPlayThread() {
- try {
- m_audioPlayThread = std::thread(&PlayerCoreV2::audioPlayThreadFunc, this);
- Logger::instance().info("Audio play thread started");
- return true;
- } catch (const std::exception& e) {
- Logger::instance().error("Failed to start audio play thread: " + std::string(e.what()));
- return false;
- }
- }
- bool PlayerCoreV2::initializeFFmpeg() {
- // FFmpeg初始化逻辑
- av_log_set_level(AV_LOG_WARNING);
- return true;
- }
- void PlayerCoreV2::cleanup() {
- stopAllThreads();
-
- if (m_formatContext) {
- avformat_close_input(&m_formatContext);
- m_formatContext = nullptr;
- }
- // if (m_synchronizer) {
- // m_synchronizer->close();
- // }
- }
- void PlayerCoreV2::setState(PlayerState newState) {
- PlayerState oldState = m_state.exchange(newState);
- if (oldState != newState) {
- notifyStateChanged(newState);
- }
- }
- void PlayerCoreV2::notifyStateChanged(PlayerState newState) {
- if (m_eventCallback) {
- m_eventCallback->onStateChanged(newState);
- }
- }
- void PlayerCoreV2::notifyError(const std::string& error) {
- Logger::instance().error(error);
- if (m_eventCallback) {
- m_eventCallback->onErrorOccurred(error);
- }
- }
- void PlayerCoreV2::notifyPositionChanged() {
- if (m_eventCallback) {
- m_eventCallback->onPositionChanged(getCurrentTime());
- }
- }
- void PlayerCoreV2::handleSyncError(double error, const std::string& reason) {
- Logger::instance().warning("Sync error: " + std::to_string(error * 1000) + "ms, reason: " + reason);
-
- if (m_eventCallback) {
- m_eventCallback->onSyncError(error, reason);
- }
-
- // 如果同步误差太大,尝试恢复
- if (error > 0.2) { // 200ms
- attemptRecovery();
- }
- }
- void PlayerCoreV2::attemptRecovery() {
- m_errorCount++;
- m_lastErrorTime = std::chrono::steady_clock::now();
-
- Logger::instance().warning("Attempting recovery, error count: " + std::to_string(m_errorCount.load()));
-
- if (m_errorCount > 5) {
- Logger::instance().error("Too many errors, stopping playback");
- handleError("Too many sync errors");
- return;
- }
- // // 重置同步器
- // if (m_synchronizer) {
- // m_synchronizer->reset();
- // }
- // 清空部分缓冲区
- if (m_videoFrameQueue) {
- m_videoFrameQueue->clear();
- }
- if (m_audioFrameQueue) {
- m_audioFrameQueue->clear();
- }
- }
- void PlayerCoreV2::handleError(const std::string& error) {
- setState(PlayerState::Error);
- notifyError(error);
- }
- void PlayerCoreV2::updateSynchronization() {
- if (!m_synchronizer || !m_threadsRunning) {
- return;
- }
- // 同步器会在内部自动更新
- // 更新同步器统计信息
- //m_synchronizer->updateStats();
- // 获取同步器统计信息并更新播放器统计
- std::lock_guard<std::mutex> lock(m_mutex);
- auto syncStats = m_synchronizer->getStats();
- m_stats.syncError = syncStats.audioVideoSyncError;
- m_stats.avgSyncError = syncStats.avgSyncError;
- m_stats.maxSyncError = syncStats.maxSyncError;
- m_stats.droppedFrames = syncStats.droppedFrames;
- m_stats.duplicatedFrames = syncStats.duplicatedFrames;
- }
- void PlayerCoreV2::updateStats() {
- std::lock_guard<std::mutex> lock(m_mutex);
-
- // 更新帧率统计
- int64_t currentFrameCount = m_frameCount;
- int64_t frameDiff = currentFrameCount - m_lastFrameCount;
- m_lastFrameCount = currentFrameCount;
-
- // 计算比特率等其他统计信息
- if (m_formatContext) {
- m_stats.bitrate = m_formatContext->bit_rate / 1000.0; // kbps
- }
- }
- void PlayerCoreV2::updatePerformanceStats() {
- std::lock_guard<std::mutex> lock(m_mutex);
-
- m_stats.cpuUsage = calculateCpuUsage();
- m_stats.memoryUsage = calculateMemoryUsage();
- }
- 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;
- }
- void PlayerCoreV2::stopAllThreads() {
- Logger::instance().info("Stopping all threads...");
-
- 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();
- }
-
- m_threadsRunning = false;
- Logger::instance().info("All threads stopped");
- }
- void PlayerCoreV2::flushBuffers() {
- if (m_videoPacketQueue) m_videoPacketQueue->clear();
- if (m_audioPacketQueue) m_audioPacketQueue->clear();
- if (m_videoFrameQueue) m_videoFrameQueue->clear();
- if (m_audioFrameQueue) m_audioFrameQueue->clear();
- }
- void PlayerCoreV2::readThreadFunc() {
- Logger::instance().info("Read thread started");
-
- AVPacket* packet = av_packet_alloc();
- if (!packet) {
- Logger::instance().error("Failed to allocate packet");
- return;
- }
-
- while (!m_threadsShouldStop) {
- // 检查是否需要seek
- if (m_seeking) {
- std::unique_lock<std::mutex> lock(m_seekMutex);
-
- // 执行seek操作
- int64_t seekTarget = m_seekTarget;
- int flags = AVSEEK_FLAG_BACKWARD;
-
- if (av_seek_frame(m_formatContext, -1, seekTarget, flags) < 0) {
- Logger::instance().error("Seek failed");
- } else {
- Logger::instance().info("Seek completed to: " + std::to_string(seekTarget));
-
- // 清空缓冲区
- flushBuffers();
-
- // 重置解码器
- resetDecoders();
- }
-
- m_seeking = false;
- setState(m_state == PlayerState::Seeking ? PlayerState::Playing : m_state.load());
- lock.unlock();
- m_seekCondition.notify_all();
- }
-
- // 检查队列是否接近容量上限 - 使用队列容量的90%作为警告阈值,但不丢弃包
- bool videoQueueNearFull = m_videoPacketQueue && m_videoPacketQueue->size() > 800;
- bool audioQueueNearFull = m_audioPacketQueue && m_audioPacketQueue->size() > 800;
- if (videoQueueNearFull || audioQueueNearFull) {
- // 队列接近满,暂停一段时间让解码线程处理
- Logger::instance().warning("Packet queue is getting large: Video=" +
- std::to_string(m_videoPacketQueue ? m_videoPacketQueue->size() : 0) +
- ", Audio=" + std::to_string(m_audioPacketQueue ? m_audioPacketQueue->size() : 0) +
- ". Slowing down read thread.");
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
- continue;
- }
-
- // 读取数据包
- int ret = av_read_frame(m_formatContext, packet);
- if (ret < 0) {
- if (ret == AVERROR_EOF) {
- Logger::instance().info("End of file reached, sending EOF packets to queues");
-
- // 向视频和音频队列分别发送EOF标记,让解码线程知道文件结束
- if (m_videoPacketQueue && m_mediaInfo.hasVideo) {
- AVPacket* eofPacket = av_packet_alloc();
- if (eofPacket) {
- eofPacket->data = nullptr;
- eofPacket->size = 0;
- eofPacket->stream_index = -1; // 特殊标记表示EOF
- m_videoPacketQueue->push(eofPacket);
- Logger::instance().info("EOF packet sent to video queue");
- }
- }
-
- if (m_audioPacketQueue && m_mediaInfo.hasAudio) {
- AVPacket* eofPacket = av_packet_alloc();
- if (eofPacket) {
- eofPacket->data = nullptr;
- eofPacket->size = 0;
- eofPacket->stream_index = -1; // 特殊标记表示EOF
- m_audioPacketQueue->push(eofPacket);
- Logger::instance().info("EOF packet sent to audio queue");
- }
- }
- break;
- } else {
- Logger::instance().error("Error reading frame: " + std::to_string(ret));
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- }
-
- // 根据流索引将数据包分发到对应队列
- if (packet->stream_index == m_mediaInfo.videoStreamIndex && m_videoStreamEnabled) {
- // 视频包放入视频队列 - 不考虑队列是否已满,确保不丢包
- if (m_videoPacketQueue) {
- AVPacket* packetCopy = av_packet_alloc();
- if (packetCopy && av_packet_ref(packetCopy, packet) == 0) {
- m_videoPacketQueue->push(packetCopy);
-
- // 记录队列大小,用于监控
- if (m_videoPacketQueue->size() % 100 == 0) {
- Logger::instance().debug("Video packet queue size: " + std::to_string(m_videoPacketQueue->size()));
- }
- } else {
- av_packet_free(&packetCopy);
- }
- }
- } else if (packet->stream_index == m_mediaInfo.audioStreamIndex && m_audioStreamEnabled) {
- // 音频包放入音频队列 - 不考虑队列是否已满,确保不丢包
- if (m_audioPacketQueue) {
- AVPacket* packetCopy = av_packet_alloc();
- if (packetCopy && av_packet_ref(packetCopy, packet) == 0) {
- m_audioPacketQueue->push(packetCopy);
-
- // 记录队列大小,用于监控
- if (m_audioPacketQueue->size() % 100 == 0) {
- Logger::instance().debug("Audio packet queue size: " + std::to_string(m_audioPacketQueue->size()));
- }
- } else {
- av_packet_free(&packetCopy);
- }
- }
- }
- av_packet_unref(packet);
- }
-
- av_packet_free(&packet);
- Logger::instance().info("Read thread finished");
- }
- void PlayerCoreV2::videoDecodeThreadFunc() {
- Logger::instance().info("Video decode thread started");
-
- int packetCount = 0;
- int frameCount = 0;
- while (!m_threadsShouldStop) {
- if (!m_videoPacketQueue || !m_videoFrameQueue || !m_videoDecoder) {
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- Logger::instance().info("Video decode thread read Packet111");
- // 从视频包队列获取包
- AVPacket* packet = nullptr;
- while (!m_threadsShouldStop && !packet) {
- Logger::instance().info("Video decode thread read Packet");
- packet = m_videoPacketQueue->pop(100);
- Logger::instance().infof("Video decode thread read Packet %d", packet);
- if (packet) {
- // 检查是否是EOF标记
- if (packet->stream_index == -1 && packet->data == nullptr) {
- Logger::instance().info("Video decode thread received EOF packet");
- av_packet_free(&packet);
-
- // 向视频帧队列发送EOF标记
- if (m_videoFrameQueue) {
- AVFrame* eofFrame = av_frame_alloc();
- if (eofFrame) {
- eofFrame->data[0] = nullptr;
- eofFrame->width = 0;
- eofFrame->height = 0;
- eofFrame->pts = AV_NOPTS_VALUE;
- m_videoFrameQueue->push(eofFrame);
- Logger::instance().info("EOF frame sent to video frame queue");
- }
- }
-
- // 视频解码线程结束
- Logger::instance().info("Video decode thread finishing due to EOF");
- return;
- }
- }
- if (!packet) {
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- }
- }
- if (!packet) {
- Logger::instance().debug("Video decode thread: no more packets available");
- continue;
- }
- packetCount++;
- Logger::instance().debug("Video decode thread got packet #" + std::to_string(packetCount) +
- ", size=" + std::to_string(packet->size) +
- ", pts=" + std::to_string(packet->pts));
-
- // 解码视频帧
- AVPacketPtr packetPtr(packet);
- std::vector<AVFramePtr> frames;
- ErrorCode decodeResult = m_videoDecoder->decode(packetPtr, frames);
-
- Logger::instance().debug("Video decode result: " + std::to_string(static_cast<int>(decodeResult)) +
- ", frames count: " + std::to_string(frames.size()));
-
- if (decodeResult == ErrorCode::SUCCESS) {
- for (auto& framePtr : frames) {
- if (framePtr && !m_threadsShouldStop) {
- frameCount++;
- Logger::instance().debug("Processing video frame #" + std::to_string(frameCount) +
- ", width=" + std::to_string(framePtr->width) +
- ", height=" + std::to_string(framePtr->height) +
- ", pts=" + std::to_string(framePtr->pts));
-
- // 设置帧的时间戳(保持原始PTS,在播放时再转换)
- if (framePtr->pts != AV_NOPTS_VALUE) {
- Logger::instance().debug("Frame PTS: " + std::to_string(framePtr->pts));
- }
-
- // 将帧放入队列 - 不丢弃任何帧
- // 释放智能指针的所有权,让队列管理帧的生命周期
- m_videoFrameQueue->push(framePtr.release());
- m_frameCount++;
- Logger::instance().debug("Video frame pushed to queue, queue size: " + std::to_string(m_videoFrameQueue->size()));
-
- // 如果队列大小超过警告阈值,记录警告但不丢弃
- if (m_videoFrameQueue->size() > 80) {
- Logger::instance().warning("Video frame queue is getting large: " + std::to_string(m_videoFrameQueue->size()) +
- " frames. Performance may be affected.");
- }
- }
- }
- } else {
- Logger::instance().warning("Video decode failed with error: " + std::to_string(static_cast<int>(decodeResult)));
- }
- }
- Logger::instance().info("Video decode thread finished, packets processed: "
- + std::to_string(packetCount)
- + ", frames decoded: " + std::to_string(frameCount));
- }
- void PlayerCoreV2::audioDecodeThreadFunc() {
- Logger::instance().info("Audio decode thread started");
-
- int packetCount = 0;
- int frameCount = 0;
- while (!m_threadsShouldStop) {
- if (!m_audioPacketQueue || !m_audioFrameQueue || !m_audioDecoder) {
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
-
- // 从音频包队列获取包
- AVPacket* packet = nullptr;
- while (!m_threadsShouldStop && !packet) {
- packet = m_audioPacketQueue->pop(100);
- if (packet) {
- // 检查是否是EOF标记
- if (packet->stream_index == -1 && packet->data == nullptr) {
- Logger::instance().info("Audio decode thread received EOF packet");
- av_packet_free(&packet);
-
- // 向音频帧队列发送EOF标记
- if (m_audioFrameQueue) {
- AVFrame* eofFrame = av_frame_alloc();
- if (eofFrame) {
- eofFrame->data[0] = nullptr;
- eofFrame->nb_samples = 0;
- eofFrame->pts = AV_NOPTS_VALUE;
- m_audioFrameQueue->push(eofFrame);
- Logger::instance().info("EOF frame sent to audio frame queue");
- }
- }
-
- // 音频解码线程结束
- Logger::instance().info("Audio decode thread finishing due to EOF");
- return;
- }
- }
- if (!packet) {
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- }
- }
-
- if (!packet) {
- Logger::instance().debug("Audio decode thread: no more packets available");
- continue;
- }
-
- packetCount++;
- Logger::instance().debug("Audio decode thread got packet #" + std::to_string(packetCount) +
- ", size=" + std::to_string(packet->size) +
- ", pts=" + std::to_string(packet->pts));
-
- // 解码音频帧
- AVPacketPtr packetPtr(packet);
- std::vector<AVFramePtr> frames;
- ErrorCode decodeResult = m_audioDecoder->decode(packetPtr, frames);
-
- Logger::instance().debug("Audio decode result: " + std::to_string(static_cast<int>(decodeResult)) +
- ", frames count: " + std::to_string(frames.size()));
-
- if (decodeResult == ErrorCode::SUCCESS) {
- for (auto& framePtr : frames) {
- if (framePtr && !m_threadsShouldStop) {
- frameCount++;
- Logger::instance().debug("Processing audio frame #" + std::to_string(frameCount) +
- ", nb_samples=" + std::to_string(framePtr->nb_samples) +
- ", pts=" + std::to_string(framePtr->pts));
-
- // 设置帧的时间戳(保持原始PTS,在播放时再转换)
- if (framePtr->pts != AV_NOPTS_VALUE) {
- Logger::instance().debug("Audio Frame PTS: " + std::to_string(framePtr->pts));
- }
-
- // 将帧放入队列 - 不丢弃任何帧
- // 释放智能指针的所有权,让队列管理帧的生命周期
- m_audioFrameQueue->push(framePtr.release());
- Logger::instance().debug("Audio frame pushed to queue, queue size: " + std::to_string(m_audioFrameQueue->size()));
-
- // 如果队列大小超过警告阈值,记录警告但不丢弃
- if (m_audioFrameQueue->size() > 300) {
- Logger::instance().warning("Audio frame queue is getting large: " + std::to_string(m_audioFrameQueue->size()) +
- " frames. Performance may be affected.");
- }
- }
- }
- } else {
- Logger::instance().warning("Audio decode failed with error: " + std::to_string(static_cast<int>(decodeResult)));
- }
-
- // packet已经被packetPtr管理,不需要手动释放
- }
-
- Logger::instance().info("Audio decode thread finished, packets processed: " + std::to_string(packetCount) +
- ", frames decoded: " + std::to_string(frameCount));
- }
- void PlayerCoreV2::videoPlayThreadFunc() {
- Logger::instance().info("Video play thread started");
- // 用于计算帧持续时间的变量
- AVFrame* lastFrame = nullptr;
- while (!m_threadsShouldStop) {
- if (!m_videoFrameQueue || !m_synchronizer) {
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- // 获取视频帧,使用超时避免无限阻塞
- AVFrame* frame = m_videoFrameQueue->pop(100); // 100ms超时
- if (!frame) {
- // 检查是否应该继续等待
- if (m_threadsShouldStop) {
- break;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- Logger::instance().debug("Video play thread got frame, pts=" + std::to_string(frame->pts));
- // 创建智能指针管理帧内存
- AVFramePtr framePtr(frame);
-
- // 使用同步器计算视频帧显示时间
- double pts = frame->pts * av_q2d(m_formatContext->streams[m_mediaInfo.videoStreamIndex]->time_base);
-
- // 更新视频时钟
- m_synchronizer->setClock(av::utils::ClockType::VIDEO, pts, 0);
-
- // 计算帧持续时间
- double last_duration = 0.0;
- if (lastFrame && lastFrame->pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) {
- // 计算两帧之间的时间差
- double last_pts = lastFrame->pts * av_q2d(m_formatContext->streams[m_mediaInfo.videoStreamIndex]->time_base);
- last_duration = pts - last_pts;
- if (last_duration < 0 || last_duration > 10.0) {
- // 如果持续时间不合理,使用帧率的倒数作为默认值
- last_duration = 1.0 / m_mediaInfo.fps;
- }
- } else {
- // 如果没有上一帧,使用帧率的倒数作为默认值
- last_duration = 1.0 / m_mediaInfo.fps;
- }
-
- // 计算视频帧延迟并决定是否显示
- 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 (lastFrame) {
- av_frame_free(&lastFrame);
- }
- lastFrame = av_frame_clone(frame);
- } else if (decision.action == FrameAction::DROP) {
- Logger::instance().debug("Video frame dropped due to sync, pts=" + std::to_string(pts) + ", error=" + std::to_string(decision.syncError));
- }
- }
- // 清理最后一帧
- if (lastFrame) {
- av_frame_free(&lastFrame);
- }
- Logger::instance().info("Video play thread finished");
- }
- void PlayerCoreV2::audioPlayThreadFunc() {
- Logger::instance().info("Audio play thread started");
- int frameCount = 0;
- while (!m_threadsShouldStop) {
- if (!m_audioFrameQueue || !m_synchronizer || !m_audioOutput) {
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- // 获取音频帧,使用较长的超时时间以确保能接收到EOF帧
- AVFrame* frame = m_audioFrameQueue->pop(100); // 100ms超时
- if (!frame) {
- // 检查是否应该继续等待
- if (m_threadsShouldStop) {
- break;
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- continue;
- }
- // 检查是否是EOF帧
- if (frame->data[0] == nullptr && frame->nb_samples == 0) {
- Logger::instance().info("Audio play thread received EOF frame, playback completed");
- av_frame_free(&frame);
- // 通知播放完成
- setState(PlayerState::Stopped);
- Logger::instance().info("Audio playback finished naturally");
- return;
- }
- frameCount++;
- Logger::instance().debug("Audio play thread got frame #" + std::to_string(frameCount)
- + ", pts=" + std::to_string(frame->pts)
- + ", nb_samples=" + std::to_string(frame->nb_samples));
- // 创建智能指针管理帧内存
- AVFramePtr framePtr(frame);
- // 计算音频PTS
- double pts = frame->pts
- * av_q2d(m_formatContext->streams[m_mediaInfo.audioStreamIndex]->time_base);
- // 更新音频时钟
- m_synchronizer->setClock(av::utils::ClockType::AUDIO, pts, 0);
- // 同步音频样本数量
- int originalSamples = frame->nb_samples;
- int adjustedSamples = m_synchronizer->synchronizeAudio((short*) frame->data[0],
- frame->nb_samples,
- pts);
- if (adjustedSamples != originalSamples) {
- Logger::instance().debug("Audio samples adjusted from " + std::to_string(originalSamples)
- + " to " + std::to_string(adjustedSamples) + " for sync");
- }
- {
- Logger::instance().debug("Writing audio frame to output device");
- bool writeResult = m_audioOutput->writeFrame(framePtr);
- Logger::instance().debug("Audio frame write result: " + std::to_string(writeResult));
- // 如果写入失败,等待一段时间避免快速循环
- if (!writeResult) {
- Logger::instance().warning("Audio frame write failed, waiting before next frame");
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- }
- // framePtr 会自动释放内存,无需手动调用 av_frame_free
- }
- Logger::instance().info("Audio play thread finished, total frames processed: "
- + std::to_string(frameCount));
- }
- } // namespace player
- } // namespace av
|