player_core_v2.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. #include "player_core_v2.h"
  2. #include "../base/media_common.h"
  3. #include "../base/logger.h"
  4. #include "../base/types.h"
  5. #include "../codec/codec_video_decoder.h"
  6. #include "../codec/codec_audio_decoder.h"
  7. #include "../utils/utils_synchronizer_v2.h"
  8. #include <chrono>
  9. #include <thread>
  10. #include <algorithm>
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #include <psapi.h>
  14. #else
  15. #include <sys/times.h>
  16. #include <unistd.h>
  17. #endif
  18. namespace av {
  19. namespace player {
  20. PlayerCoreV2::PlayerCoreV2(const SyncConfigV2& syncConfig)
  21. : m_state(PlayerState::Idle)
  22. , m_eventCallback(nullptr)
  23. , m_formatContext(nullptr)
  24. , m_openGLVideoRenderer(nullptr)
  25. , m_volume(1.0)
  26. , m_playbackSpeed(1.0)
  27. , m_seekTarget(-1)
  28. , m_seeking(false)
  29. , m_baseTime(0)
  30. , m_lastUpdateTime(0)
  31. , m_threadsShouldStop(false)
  32. , m_threadsRunning(false)
  33. , m_initialized(false)
  34. , m_frameCount(0)
  35. , m_lastFrameCount(0)
  36. , m_errorCount(0)
  37. , m_buffering(false)
  38. , m_bufferHealth(1.0)
  39. {
  40. Logger::instance().info("PlayerCoreV2 created");
  41. try {
  42. // 初始化FFmpeg
  43. if (!initializeFFmpeg()) {
  44. Logger::instance().error("Failed to initialize FFmpeg");
  45. setState(PlayerState::Error);
  46. return;
  47. }
  48. // 创建分离的视频和音频包队列
  49. m_videoPacketQueue = av::utils::PacketQueueFactory::createStandardQueue(1000); // 视频包队列
  50. if (!m_videoPacketQueue) {
  51. Logger::instance().error("Failed to create video packet queue");
  52. setState(PlayerState::Error);
  53. return;
  54. }
  55. m_audioPacketQueue = av::utils::PacketQueueFactory::createStandardQueue(1000); // 音频包队列
  56. if (!m_audioPacketQueue) {
  57. Logger::instance().error("Failed to create audio packet queue");
  58. setState(PlayerState::Error);
  59. return;
  60. }
  61. m_videoFrameQueue = av::utils::FrameQueueFactory::createStandardQueue(50); // 增加视频帧队列
  62. if (!m_videoFrameQueue) {
  63. Logger::instance().error("Failed to create video frame queue");
  64. setState(PlayerState::Error);
  65. return;
  66. }
  67. m_audioFrameQueue = av::utils::FrameQueueFactory::createStandardQueue(200); // 增加音频帧队列
  68. if (!m_audioFrameQueue) {
  69. Logger::instance().error("Failed to create audio frame queue");
  70. setState(PlayerState::Error);
  71. return;
  72. }
  73. // 创建改进的同步器
  74. // m_synchronizer = std::make_unique<Synchronizer>(syncConfig);
  75. // if (!m_synchronizer) {
  76. // Logger::instance().error("Failed to create synchronizer");
  77. // setState(PlayerState::Error);
  78. // return;
  79. // }
  80. // 设置同步器回调
  81. // m_synchronizer->setSyncErrorCallback([this](double error, const std::string& reason) {
  82. // handleSyncError(error, reason);
  83. // });
  84. // m_synchronizer->setFrameDropCallback([this](av::utils::ClockType type, int64_t pts) {
  85. // std::lock_guard<std::mutex> lock(m_mutex);
  86. // m_stats.droppedFrames++;
  87. // if (m_eventCallback) {
  88. // m_eventCallback->onFrameDropped(m_stats.droppedFrames);
  89. // }
  90. // });
  91. // 创建解码器
  92. m_videoDecoder = std::make_unique<VideoDecoder>();
  93. if (!m_videoDecoder) {
  94. Logger::instance().error("Failed to create video decoder");
  95. setState(PlayerState::Error);
  96. return;
  97. }
  98. m_audioDecoder = std::make_unique<AudioDecoder>();
  99. if (!m_audioDecoder) {
  100. Logger::instance().error("Failed to create audio decoder");
  101. setState(PlayerState::Error);
  102. return;
  103. }
  104. // 创建音频输出设备
  105. m_audioOutput = std::make_unique<AudioOutput>();
  106. if (!m_audioOutput) {
  107. Logger::instance().error("Failed to create audio output");
  108. setState(PlayerState::Error);
  109. return;
  110. }
  111. // 初始化性能监控
  112. m_lastStatsUpdate = std::chrono::steady_clock::now();
  113. m_lastCpuMeasure = m_lastStatsUpdate;
  114. #ifdef _WIN32
  115. m_lastCpuTime = GetTickCount64();
  116. #else
  117. struct tms tm;
  118. m_lastCpuTime = times(&tm);
  119. #endif
  120. m_initialized = true;
  121. Logger::instance().info("PlayerCoreV2 initialized successfully");
  122. } catch (const std::exception& e) {
  123. Logger::instance().error("Exception during PlayerCoreV2 initialization: " + std::string(e.what()));
  124. setState(PlayerState::Error);
  125. m_initialized = false;
  126. } catch (...) {
  127. Logger::instance().error("Unknown exception during PlayerCoreV2 initialization");
  128. setState(PlayerState::Error);
  129. m_initialized = false;
  130. }
  131. }
  132. PlayerCoreV2::~PlayerCoreV2() {
  133. Logger::instance().info("PlayerCoreV2 destroying...");
  134. stop();
  135. cleanup();
  136. Logger::instance().info("PlayerCoreV2 destroyed");
  137. }
  138. void PlayerCoreV2::setEventCallback(PlayerEventCallback* callback) {
  139. std::lock_guard<std::mutex> lock(m_mutex);
  140. m_eventCallback = callback;
  141. }
  142. ErrorCode PlayerCoreV2::openFile(const std::string& filename) {
  143. Logger::instance().info("Opening file: " + filename);
  144. if (!m_initialized) {
  145. Logger::instance().error("PlayerCoreV2 not initialized");
  146. return ErrorCode::NOT_INITIALIZED;
  147. }
  148. // 如果正在播放,先停止
  149. if (m_state != PlayerState::Idle) {
  150. stop();
  151. }
  152. setState(PlayerState::Opening);
  153. // 打开媒体文件
  154. if (!openMediaFile(filename)) {
  155. setState(PlayerState::Error);
  156. notifyError("Failed to open media file: " + filename);
  157. return ErrorCode::FILE_OPEN_FAILED;
  158. }
  159. // 设置媒体信息
  160. m_mediaInfo.filename = filename;
  161. // 设置解码器
  162. if (m_mediaInfo.hasVideo && !setupVideoDecoder()) {
  163. Logger::instance().error("Failed to setup video decoder");
  164. setState(PlayerState::Error);
  165. return ErrorCode::CODEC_OPEN_FAILED;
  166. }
  167. if (m_mediaInfo.hasAudio && !setupAudioDecoder()) {
  168. Logger::instance().error("Failed to setup audio decoder");
  169. setState(PlayerState::Error);
  170. return ErrorCode::CODEC_OPEN_FAILED;
  171. }
  172. // 注意:同步器的初始化已经在openMediaFile()中完成,这里不需要重复调用
  173. // 因为initialize()会重新选择主时钟,可能覆盖之前设置的流信息
  174. setState(PlayerState::Stopped);
  175. // 如果已设置视频渲染器且有视频流,重新初始化渲染器
  176. if (m_mediaInfo.hasVideo && m_openGLVideoRenderer) {
  177. AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
  178. bool rendererInitResult = m_openGLVideoRenderer->Open(
  179. videoStream->codecpar->width,
  180. videoStream->codecpar->height
  181. );
  182. if (!rendererInitResult) {
  183. Logger::instance().warning("Failed to initialize OpenGL video renderer");
  184. } else {
  185. Logger::instance().info("OpenGL video renderer initialized successfully");
  186. }
  187. }
  188. // 通知媒体信息变化
  189. if (m_eventCallback) {
  190. m_eventCallback->onMediaInfoChanged(m_mediaInfo);
  191. }
  192. Logger::instance().info("File opened successfully: " + filename);
  193. return ErrorCode::SUCCESS;
  194. }
  195. ErrorCode PlayerCoreV2::play() {
  196. Logger::instance().info("Starting playback");
  197. if (m_state == PlayerState::Playing) {
  198. Logger::instance().debug("Already playing");
  199. return ErrorCode::SUCCESS;
  200. }
  201. if (m_state != PlayerState::Stopped && m_state != PlayerState::Paused) {
  202. Logger::instance().error("Invalid state for play: " + std::to_string(static_cast<int>(m_state.load())));
  203. return ErrorCode::INVALID_STATE;
  204. }
  205. // 启动同步器
  206. // if (m_synchronizer->start() != ErrorCode::SUCCESS) {
  207. // Logger::instance().error("Failed to start synchronizer");
  208. // return ErrorCode::SYNC_ERROR;
  209. // }
  210. // 记录播放开始时间
  211. m_playStartTime = std::chrono::steady_clock::now();
  212. // 如果是从停止状态开始播放,重置基准时间
  213. if (m_state == PlayerState::Stopped) {
  214. m_baseTime = 0;
  215. m_frameCount = 0;
  216. m_lastFrameCount = 0;
  217. // 重置统计信息
  218. std::lock_guard<std::mutex> lock(m_mutex);
  219. m_stats = PlaybackStats();
  220. m_stats.playbackSpeed = m_playbackSpeed;
  221. }
  222. // 启动音频输出设备
  223. if (m_audioOutput && m_mediaInfo.hasAudio) {
  224. if (m_state == PlayerState::Paused) {
  225. m_audioOutput->resume();
  226. } else {
  227. Logger::instance().info("Starting audio output device...");
  228. m_audioOutput->start();
  229. // 检查音频设备是否成功启动
  230. if (!m_audioOutput->isPlaying()) {
  231. Logger::instance().error("Audio output device failed to start");
  232. // 不要因为音频失败而停止整个播放,继续播放视频
  233. Logger::instance().warning("Continuing playback without audio");
  234. } else {
  235. Logger::instance().info("Audio output device started successfully");
  236. }
  237. }
  238. // 设置音频参数
  239. m_audioOutput->setVolume(m_volume);
  240. m_audioOutput->setPlaybackSpeed(m_playbackSpeed);
  241. // 给音频输出设备更多时间初始化
  242. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  243. }
  244. // 启动线程
  245. m_threadsShouldStop = false;
  246. if (!startReadThread()) {
  247. Logger::instance().error("Failed to start read thread");
  248. return ErrorCode::THREAD_ERROR;
  249. }
  250. if (!startDecodeThreads()) {
  251. Logger::instance().error("Failed to start decode threads");
  252. return ErrorCode::THREAD_ERROR;
  253. }
  254. if (m_mediaInfo.hasVideo && !startVideoPlayThread()) {
  255. Logger::instance().error("Failed to start video play thread");
  256. return ErrorCode::THREAD_ERROR;
  257. }
  258. if (m_mediaInfo.hasAudio && !startAudioPlayThread()) {
  259. Logger::instance().error("Failed to start audio play thread");
  260. return ErrorCode::THREAD_ERROR;
  261. }
  262. m_threadsRunning = true;
  263. setState(PlayerState::Playing);
  264. Logger::instance().info("Playback started");
  265. return ErrorCode::SUCCESS;
  266. }
  267. ErrorCode PlayerCoreV2::pause() {
  268. Logger::instance().info("Pausing playback");
  269. if (m_state != PlayerState::Playing) {
  270. Logger::instance().debug("Not playing, cannot pause");
  271. return ErrorCode::INVALID_STATE;
  272. }
  273. // // 暂停同步器
  274. // if (m_synchronizer->pause() != ErrorCode::SUCCESS) {
  275. // Logger::instance().warning("Failed to pause synchronizer");
  276. // }
  277. // 记录暂停时的播放时间
  278. if (m_playStartTime.time_since_epoch().count() != 0) {
  279. auto currentTime = std::chrono::steady_clock::now();
  280. auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
  281. currentTime - m_playStartTime).count();
  282. m_baseTime += static_cast<int64_t>(elapsed * m_playbackSpeed);
  283. m_playStartTime = std::chrono::steady_clock::time_point{};
  284. }
  285. // 暂停音频输出
  286. if (m_audioOutput) {
  287. m_audioOutput->pause();
  288. }
  289. setState(PlayerState::Paused);
  290. Logger::instance().info("Playback paused");
  291. return ErrorCode::SUCCESS;
  292. }
  293. ErrorCode PlayerCoreV2::stop() {
  294. Logger::instance().info("Stopping playback");
  295. if (m_state == PlayerState::Idle || m_state == PlayerState::Stopped) {
  296. Logger::instance().debug("Already stopped");
  297. return ErrorCode::SUCCESS;
  298. }
  299. // // 停止同步器
  300. // if (m_synchronizer) {
  301. // m_synchronizer->stop();
  302. // }
  303. // 停止音频输出
  304. if (m_audioOutput) {
  305. m_audioOutput->stop();
  306. }
  307. // 清空OpenGL视频渲染器
  308. if (m_openGLVideoRenderer) {
  309. m_openGLVideoRenderer->Close();
  310. }
  311. // 停止所有线程
  312. stopAllThreads();
  313. // 重置解码器
  314. resetDecoders();
  315. // 清空队列
  316. if (m_videoPacketQueue) m_videoPacketQueue->clear();
  317. if (m_audioPacketQueue) m_audioPacketQueue->clear();
  318. if (m_videoFrameQueue) m_videoFrameQueue->clear();
  319. if (m_audioFrameQueue) m_audioFrameQueue->clear();
  320. // 重置时间
  321. m_baseTime = 0;
  322. m_playStartTime = std::chrono::steady_clock::time_point{};
  323. setState(PlayerState::Stopped);
  324. Logger::instance().info("Playback stopped");
  325. return ErrorCode::SUCCESS;
  326. }
  327. ErrorCode PlayerCoreV2::seek(int64_t timestamp) {
  328. Logger::instance().info("Seeking to: " + std::to_string(timestamp));
  329. if (m_state == PlayerState::Idle || m_state == PlayerState::Opening) {
  330. Logger::instance().error("Invalid state for seek");
  331. return ErrorCode::INVALID_STATE;
  332. }
  333. std::unique_lock<std::mutex> lock(m_seekMutex);
  334. // 设置seek目标
  335. m_seekTarget = timestamp;
  336. m_seeking = true;
  337. // 更新基准时间为跳转目标时间
  338. m_baseTime = timestamp;
  339. m_playStartTime = std::chrono::steady_clock::now();
  340. // // 重置同步器
  341. // if (m_synchronizer) {
  342. // m_synchronizer->reset();
  343. // }
  344. // 清空队列
  345. flushBuffers();
  346. setState(PlayerState::Seeking);
  347. // 通知seek条件
  348. m_seekCondition.notify_all();
  349. Logger::instance().info("Seek initiated");
  350. return ErrorCode::SUCCESS;
  351. }
  352. ErrorCode PlayerCoreV2::setPlaybackSpeed(double speed) {
  353. if (speed <= 0.0 || speed > 4.0) {
  354. Logger::instance().error("Invalid playback speed: " + std::to_string(speed));
  355. return ErrorCode::INVALID_PARAMS;
  356. }
  357. std::lock_guard<std::mutex> lock(m_mutex);
  358. m_playbackSpeed = speed;
  359. // // 设置同步器的播放速度
  360. // if (m_synchronizer) {
  361. // m_synchronizer->setPlaybackSpeed(speed);
  362. // }
  363. // 设置音频输出的播放速度
  364. if (m_audioOutput) {
  365. m_audioOutput->setPlaybackSpeed(speed);
  366. }
  367. // 更新统计信息
  368. m_stats.playbackSpeed = speed;
  369. Logger::instance().info("Playback speed set to: " + std::to_string(speed));
  370. return ErrorCode::SUCCESS;
  371. }
  372. MediaInfo PlayerCoreV2::getMediaInfo() const {
  373. std::lock_guard<std::mutex> lock(m_mutex);
  374. return m_mediaInfo;
  375. }
  376. PlaybackStats PlayerCoreV2::getStats() const {
  377. std::lock_guard<std::mutex> lock(m_mutex);
  378. PlaybackStats stats = m_stats;
  379. // 更新当前时间
  380. stats.currentTime = getCurrentTime();
  381. // 更新队列大小
  382. if (m_videoPacketQueue) stats.queuedPackets += m_videoPacketQueue->size();
  383. if (m_audioPacketQueue) stats.queuedPackets += m_audioPacketQueue->size();
  384. if (m_videoFrameQueue) stats.queuedVideoFrames = m_videoFrameQueue->size();
  385. if (m_audioFrameQueue) stats.queuedAudioFrames = m_audioFrameQueue->size();
  386. // // 更新同步统计
  387. // if (m_synchronizer) {
  388. // auto syncStats = m_synchronizer->getStats();
  389. // stats.syncError = syncStats.syncError;
  390. // // stats.avgSyncError = syncStats.avgSyncError;
  391. // // stats.maxSyncError = syncStats.maxSyncError;
  392. // stats.droppedFrames = syncStats.droppedFrames;
  393. // stats.duplicatedFrames = syncStats.duplicatedFrames;
  394. // }
  395. return stats;
  396. }
  397. int64_t PlayerCoreV2::getCurrentTime() const {
  398. if (m_state == PlayerState::Idle || m_state == PlayerState::Stopped) {
  399. return 0;
  400. }
  401. if (m_state == PlayerState::Paused) {
  402. return m_baseTime;
  403. }
  404. if (m_playStartTime.time_since_epoch().count() == 0) {
  405. return m_baseTime;
  406. }
  407. auto currentTime = std::chrono::steady_clock::now();
  408. auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(
  409. currentTime - m_playStartTime).count();
  410. return m_baseTime + static_cast<int64_t>(elapsed * m_playbackSpeed);
  411. }
  412. double PlayerCoreV2::getPlaybackSpeed() const {
  413. return m_playbackSpeed;
  414. }
  415. void PlayerCoreV2::setVolume(double volume) {
  416. volume = std::max(0.0, std::min(1.0, volume));
  417. m_volume = volume;
  418. // 同时设置音频输出设备的音量
  419. if (m_audioOutput) {
  420. m_audioOutput->setVolume(volume);
  421. }
  422. Logger::instance().debug("Volume set to: " + std::to_string(volume));
  423. }
  424. // void PlayerCore::setSyncConfig(const SyncConfig& config) {
  425. // // if (m_synchronizer) {
  426. // // m_synchronizer->setConfig(config);
  427. // // }
  428. // }
  429. // SyncConfig PlayerCoreV2::getSyncConfig() const {
  430. // if (m_synchronizer) {
  431. // return m_synchronizer->getConfig();
  432. // }
  433. // return SyncConfig();
  434. // }
  435. void PlayerCoreV2::setOpenGLVideoRenderer(OpenGLVideoWidget* renderer) {
  436. m_openGLVideoRenderer = renderer;
  437. }
  438. AVFrame* PlayerCoreV2::getNextVideoFrame() {
  439. if (!m_videoFrameQueue || m_state != PlayerState::Playing) {
  440. return nullptr;
  441. }
  442. return m_videoFrameQueue->pop();
  443. }
  444. AVFrame* PlayerCoreV2::getNextAudioFrame() {
  445. if (!m_audioFrameQueue || m_state != PlayerState::Playing) {
  446. return nullptr;
  447. }
  448. return m_audioFrameQueue->pop();
  449. }
  450. void PlayerCoreV2::releaseVideoFrame(AVFrame* frame) {
  451. if (frame) {
  452. av_frame_free(&frame);
  453. }
  454. }
  455. void PlayerCoreV2::releaseAudioFrame(AVFrame* frame) {
  456. if (frame) {
  457. av_frame_free(&frame);
  458. }
  459. }
  460. void PlayerCoreV2::update() {
  461. if (!m_initialized) {
  462. return;
  463. }
  464. // 更新同步状态
  465. updateSynchronization();
  466. // 更新统计信息
  467. auto now = std::chrono::steady_clock::now();
  468. if (std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastStatsUpdate).count() > 500) {
  469. updateStats();
  470. updatePerformanceStats();
  471. m_lastStatsUpdate = now;
  472. // 通知位置变化
  473. notifyPositionChanged();
  474. }
  475. // 检查错误恢复
  476. if (m_errorCount > 0) {
  477. auto timeSinceError = std::chrono::duration_cast<std::chrono::seconds>(now - m_lastErrorTime).count();
  478. if (timeSinceError > 5) { // 5秒后重置错误计数
  479. m_errorCount = 0;
  480. }
  481. }
  482. }
  483. std::string PlayerCoreV2::getDebugInfo() const {
  484. std::ostringstream oss;
  485. oss << "PlayerCoreV2 Debug Info:\n";
  486. oss << " State: " << static_cast<int>(m_state.load()) << "\n";
  487. oss << " Initialized: " << (m_initialized ? "Yes" : "No") << "\n";
  488. oss << " Threads Running: " << (m_threadsRunning ? "Yes" : "No") << "\n";
  489. oss << " Current Time: " << getCurrentTime() << " us\n";
  490. oss << " Playback Speed: " << m_playbackSpeed << "x\n";
  491. oss << " Volume: " << m_volume << "\n";
  492. oss << " Error Count: " << m_errorCount << "\n";
  493. // if (m_synchronizer) {
  494. // oss << "\n" << m_synchronizer->getDebugInfo();
  495. // }
  496. return oss.str();
  497. }
  498. void PlayerCoreV2::dumpStats() const {
  499. PlaybackStats stats = getStats();
  500. Logger::instance().info("=== PlayerCoreV2 Statistics ===");
  501. Logger::instance().info("Current Time: " + std::to_string(stats.currentTime) + " us");
  502. Logger::instance().info("Total Frames: " + std::to_string(stats.totalFrames));
  503. Logger::instance().info("Dropped Frames: " + std::to_string(stats.droppedFrames));
  504. Logger::instance().info("Duplicated Frames: " + std::to_string(stats.duplicatedFrames));
  505. Logger::instance().info("Sync Error: " + std::to_string(stats.syncError * 1000) + " ms");
  506. Logger::instance().info("Avg Sync Error: " + std::to_string(stats.avgSyncError * 1000) + " ms");
  507. Logger::instance().info("Max Sync Error: " + std::to_string(stats.maxSyncError * 1000) + " ms");
  508. Logger::instance().info("CPU Usage: " + std::to_string(stats.cpuUsage) + "%");
  509. Logger::instance().info("Memory Usage: " + std::to_string(stats.memoryUsage) + " MB");
  510. Logger::instance().info("Queued Packets: " + std::to_string(stats.queuedPackets));
  511. Logger::instance().info("Queued Video Frames: " + std::to_string(stats.queuedVideoFrames));
  512. Logger::instance().info("Queued Audio Frames: " + std::to_string(stats.queuedAudioFrames));
  513. Logger::instance().info("===============================");
  514. }
  515. bool PlayerCoreV2::openMediaFile(const std::string& filename) {
  516. // 关闭之前的文件
  517. if (m_formatContext) {
  518. avformat_close_input(&m_formatContext);
  519. m_formatContext = nullptr;
  520. }
  521. // 分配格式上下文
  522. m_formatContext = avformat_alloc_context();
  523. if (!m_formatContext) {
  524. Logger::instance().error("Failed to allocate format context");
  525. return false;
  526. }
  527. // 打开输入文件
  528. if (avformat_open_input(&m_formatContext, filename.c_str(), nullptr, nullptr) < 0) {
  529. Logger::instance().error("Failed to open input file: " + filename);
  530. avformat_free_context(m_formatContext);
  531. m_formatContext = nullptr;
  532. return false;
  533. }
  534. // 查找流信息
  535. if (avformat_find_stream_info(m_formatContext, nullptr) < 0) {
  536. Logger::instance().error("Failed to find stream info");
  537. avformat_close_input(&m_formatContext);
  538. return false;
  539. }
  540. // 查找视频和音频流
  541. m_mediaInfo.videoStreamIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
  542. m_mediaInfo.audioStreamIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
  543. m_mediaInfo.hasVideo = (m_mediaInfo.videoStreamIndex >= 0);
  544. m_mediaInfo.hasAudio = (m_mediaInfo.audioStreamIndex >= 0);
  545. if (!m_mediaInfo.hasVideo && !m_mediaInfo.hasAudio) {
  546. Logger::instance().error("No video or audio streams found");
  547. avformat_close_input(&m_formatContext);
  548. return false;
  549. }
  550. // 获取媒体信息
  551. m_mediaInfo.duration = m_formatContext->duration;
  552. m_mediaInfo.bitrate = m_formatContext->bit_rate;
  553. if (m_mediaInfo.hasVideo) {
  554. AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
  555. m_mediaInfo.width = videoStream->codecpar->width;
  556. m_mediaInfo.height = videoStream->codecpar->height;
  557. // 计算帧率
  558. if (videoStream->avg_frame_rate.den != 0) {
  559. m_mediaInfo.fps = av_q2d(videoStream->avg_frame_rate);
  560. } else if (videoStream->r_frame_rate.den != 0) {
  561. m_mediaInfo.fps = av_q2d(videoStream->r_frame_rate);
  562. } else {
  563. m_mediaInfo.fps = 25.0; // 默认帧率
  564. }
  565. Logger::instance().info("Video stream found: " + std::to_string(m_mediaInfo.width) + "x" +
  566. std::to_string(m_mediaInfo.height) + " @ " + std::to_string(m_mediaInfo.fps) + " fps");
  567. }
  568. if (m_mediaInfo.hasAudio) {
  569. AVStream* audioStream = m_formatContext->streams[m_mediaInfo.audioStreamIndex];
  570. m_mediaInfo.sampleRate = audioStream->codecpar->sample_rate;
  571. m_mediaInfo.channels = audioStream->codecpar->ch_layout.nb_channels;
  572. Logger::instance().info("Audio stream found: " + std::to_string(m_mediaInfo.sampleRate) + " Hz, " +
  573. std::to_string(m_mediaInfo.channels) + " channels");
  574. }
  575. // // 设置同步器的流信息
  576. // if (m_synchronizer) {
  577. // m_synchronizer->setStreamInfo(m_mediaInfo.hasAudio, m_mediaInfo.hasVideo);
  578. // Logger::instance().info("Synchronizer stream info set: hasAudio=" + std::to_string(m_mediaInfo.hasAudio) +
  579. // ", hasVideo=" + std::to_string(m_mediaInfo.hasVideo));
  580. // // 在设置流信息后初始化同步器,确保主时钟选择基于正确的流信息
  581. // if (m_synchronizer->initialize() != ErrorCode::SUCCESS) {
  582. // Logger::instance().error("Failed to initialize synchronizer");
  583. // return false;
  584. // }
  585. // Logger::instance().info("Synchronizer initialized with correct stream info");
  586. // }
  587. Logger::instance().info("Media file opened successfully: " + filename);
  588. return true;
  589. }
  590. bool PlayerCoreV2::setupVideoDecoder() {
  591. if (!m_mediaInfo.hasVideo || !m_videoDecoder) {
  592. return false;
  593. }
  594. AVStream* videoStream = m_formatContext->streams[m_mediaInfo.videoStreamIndex];
  595. // 查找解码器
  596. const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id);
  597. if (!codec) {
  598. Logger::instance().error("Video codec not found");
  599. return false;
  600. }
  601. Logger::instance().info("Found video codec: " + std::string(codec->name));
  602. Logger::instance().info("Video stream info: width=" + std::to_string(videoStream->codecpar->width) +
  603. ", height=" + std::to_string(videoStream->codecpar->height) +
  604. ", format=" + std::to_string(videoStream->codecpar->format));
  605. // 创建视频解码器参数
  606. VideoDecoderParams videoParams;
  607. videoParams.codecName = codec->name;
  608. videoParams.width = videoStream->codecpar->width;
  609. videoParams.height = videoStream->codecpar->height;
  610. videoParams.pixelFormat = static_cast<AVPixelFormat>(videoStream->codecpar->format);
  611. videoParams.hardwareAccel = false; // 先禁用硬件加速来排除问题
  612. videoParams.lowLatency = false;
  613. Logger::instance().info("Video decoder params: codec=" + videoParams.codecName +
  614. ", size=" + std::to_string(videoParams.width) + "x" + std::to_string(videoParams.height) +
  615. ", format=" + std::to_string(static_cast<int>(videoParams.pixelFormat)));
  616. // 初始化视频解码器
  617. if (m_videoDecoder->initialize(videoParams) != ErrorCode::SUCCESS) {
  618. Logger::instance().error("Failed to initialize video decoder");
  619. return false;
  620. }
  621. // 设置流参数
  622. if (m_videoDecoder->setStreamParameters(videoStream->codecpar) != ErrorCode::SUCCESS) {
  623. Logger::instance().error("Failed to set video decoder stream parameters");
  624. return false;
  625. }
  626. // 打开视频解码器
  627. if (m_videoDecoder->open(videoParams) != ErrorCode::SUCCESS) {
  628. Logger::instance().error("Failed to open video decoder");
  629. return false;
  630. }
  631. Logger::instance().info("Video decoder setup successfully");
  632. return true;
  633. }
  634. bool PlayerCoreV2::setupAudioDecoder() {
  635. if (!m_mediaInfo.hasAudio || !m_audioDecoder) {
  636. return false;
  637. }
  638. AVStream* audioStream = m_formatContext->streams[m_mediaInfo.audioStreamIndex];
  639. // 查找解码器
  640. const AVCodec* codec = avcodec_find_decoder(audioStream->codecpar->codec_id);
  641. if (!codec) {
  642. Logger::instance().error("Audio codec not found");
  643. return false;
  644. }
  645. // 分配解码器上下文
  646. AVCodecContext* codecContext = avcodec_alloc_context3(codec);
  647. if (!codecContext) {
  648. Logger::instance().error("Failed to allocate audio codec context");
  649. return false;
  650. }
  651. // 复制流参数到解码器上下文
  652. if (avcodec_parameters_to_context(codecContext, audioStream->codecpar) < 0) {
  653. Logger::instance().error("Failed to copy audio codec parameters");
  654. avcodec_free_context(&codecContext);
  655. return false;
  656. }
  657. // 打开解码器
  658. if (avcodec_open2(codecContext, codec, nullptr) < 0) {
  659. Logger::instance().error("Failed to open audio codec");
  660. avcodec_free_context(&codecContext);
  661. return false;
  662. }
  663. // 创建音频解码器参数
  664. AudioDecoderParams audioParams;
  665. audioParams.codecName = codec->name;
  666. audioParams.sampleRate = codecContext->sample_rate;
  667. audioParams.channels = codecContext->ch_layout.nb_channels;
  668. audioParams.sampleFormat = codecContext->sample_fmt;
  669. audioParams.lowLatency = false;
  670. audioParams.enableResampling = true;
  671. // 初始化音频解码器
  672. if (m_audioDecoder->initialize(audioParams) != ErrorCode::SUCCESS) {
  673. Logger::instance().error("Failed to initialize audio decoder");
  674. avcodec_free_context(&codecContext);
  675. return false;
  676. }
  677. // 打开音频解码器
  678. if (m_audioDecoder->open(audioParams) != ErrorCode::SUCCESS) {
  679. Logger::instance().error("Failed to open audio decoder");
  680. avcodec_free_context(&codecContext);
  681. return false;
  682. }
  683. // 初始化音频输出设备
  684. if (m_audioOutput && !m_audioOutput->initialize(codecContext->sample_rate,
  685. codecContext->ch_layout.nb_channels,
  686. codecContext->sample_fmt)) {
  687. Logger::instance().error("Failed to initialize audio output");
  688. avcodec_free_context(&codecContext);
  689. return false;
  690. }
  691. // 释放解码器上下文
  692. avcodec_free_context(&codecContext);
  693. Logger::instance().info("Audio decoder setup successfully");
  694. return true;
  695. }
  696. void PlayerCoreV2::resetDecoders() {
  697. if (m_videoDecoder) {
  698. m_videoDecoder->reset();
  699. }
  700. if (m_audioDecoder) {
  701. m_audioDecoder->reset();
  702. }
  703. }
  704. bool PlayerCoreV2::startReadThread() {
  705. try {
  706. m_readThread = std::thread(&PlayerCoreV2::readThreadFunc, this);
  707. Logger::instance().info("Read thread started");
  708. return true;
  709. } catch (const std::exception& e) {
  710. Logger::instance().error("Failed to start read thread: " + std::string(e.what()));
  711. return false;
  712. }
  713. }
  714. bool PlayerCoreV2::startDecodeThreads() {
  715. try {
  716. if (m_mediaInfo.hasVideo) {
  717. m_videoDecodeThread = std::thread(&PlayerCoreV2::videoDecodeThreadFunc, this);
  718. Logger::instance().info("Video decode thread started");
  719. }
  720. if (m_mediaInfo.hasAudio) {
  721. m_audioDecodeThread = std::thread(&PlayerCoreV2::audioDecodeThreadFunc, this);
  722. Logger::instance().info("Audio decode thread started");
  723. }
  724. return true;
  725. } catch (const std::exception& e) {
  726. Logger::instance().error("Failed to start decode threads: " + std::string(e.what()));
  727. return false;
  728. }
  729. }
  730. bool PlayerCoreV2::startVideoPlayThread() {
  731. try {
  732. m_videoPlayThread = std::thread(&PlayerCoreV2::videoPlayThreadFunc, this);
  733. Logger::instance().info("Video play thread started");
  734. return true;
  735. } catch (const std::exception& e) {
  736. Logger::instance().error("Failed to start video play thread: " + std::string(e.what()));
  737. return false;
  738. }
  739. }
  740. bool PlayerCoreV2::startAudioPlayThread() {
  741. try {
  742. m_audioPlayThread = std::thread(&PlayerCoreV2::audioPlayThreadFunc, this);
  743. Logger::instance().info("Audio play thread started");
  744. return true;
  745. } catch (const std::exception& e) {
  746. Logger::instance().error("Failed to start audio play thread: " + std::string(e.what()));
  747. return false;
  748. }
  749. }
  750. bool PlayerCoreV2::initializeFFmpeg() {
  751. // FFmpeg初始化逻辑
  752. av_log_set_level(AV_LOG_WARNING);
  753. return true;
  754. }
  755. void PlayerCoreV2::cleanup() {
  756. stopAllThreads();
  757. if (m_formatContext) {
  758. avformat_close_input(&m_formatContext);
  759. m_formatContext = nullptr;
  760. }
  761. // if (m_synchronizer) {
  762. // m_synchronizer->close();
  763. // }
  764. }
  765. void PlayerCoreV2::setState(PlayerState newState) {
  766. PlayerState oldState = m_state.exchange(newState);
  767. if (oldState != newState) {
  768. notifyStateChanged(newState);
  769. }
  770. }
  771. void PlayerCoreV2::notifyStateChanged(PlayerState newState) {
  772. if (m_eventCallback) {
  773. m_eventCallback->onStateChanged(newState);
  774. }
  775. }
  776. void PlayerCoreV2::notifyError(const std::string& error) {
  777. Logger::instance().error(error);
  778. if (m_eventCallback) {
  779. m_eventCallback->onErrorOccurred(error);
  780. }
  781. }
  782. void PlayerCoreV2::notifyPositionChanged() {
  783. if (m_eventCallback) {
  784. m_eventCallback->onPositionChanged(getCurrentTime());
  785. }
  786. }
  787. void PlayerCoreV2::handleSyncError(double error, const std::string& reason) {
  788. Logger::instance().warning("Sync error: " + std::to_string(error * 1000) + "ms, reason: " + reason);
  789. if (m_eventCallback) {
  790. m_eventCallback->onSyncError(error, reason);
  791. }
  792. // 如果同步误差太大,尝试恢复
  793. if (error > 0.2) { // 200ms
  794. attemptRecovery();
  795. }
  796. }
  797. void PlayerCoreV2::attemptRecovery() {
  798. m_errorCount++;
  799. m_lastErrorTime = std::chrono::steady_clock::now();
  800. Logger::instance().warning("Attempting recovery, error count: " + std::to_string(m_errorCount.load()));
  801. if (m_errorCount > 5) {
  802. Logger::instance().error("Too many errors, stopping playback");
  803. handleError("Too many sync errors");
  804. return;
  805. }
  806. // // 重置同步器
  807. // if (m_synchronizer) {
  808. // m_synchronizer->reset();
  809. // }
  810. // 清空部分缓冲区
  811. if (m_videoFrameQueue) {
  812. m_videoFrameQueue->clear();
  813. }
  814. if (m_audioFrameQueue) {
  815. m_audioFrameQueue->clear();
  816. }
  817. }
  818. void PlayerCoreV2::handleError(const std::string& error) {
  819. setState(PlayerState::Error);
  820. notifyError(error);
  821. }
  822. void PlayerCoreV2::updateSynchronization() {
  823. // if (!m_synchronizer || !m_threadsRunning) {
  824. // return;
  825. // }
  826. // 同步器会在内部自动更新
  827. // 这里可以添加额外的同步逻辑
  828. }
  829. void PlayerCoreV2::updateStats() {
  830. std::lock_guard<std::mutex> lock(m_mutex);
  831. // 更新帧率统计
  832. int64_t currentFrameCount = m_frameCount;
  833. int64_t frameDiff = currentFrameCount - m_lastFrameCount;
  834. m_lastFrameCount = currentFrameCount;
  835. // 计算比特率等其他统计信息
  836. if (m_formatContext) {
  837. m_stats.bitrate = m_formatContext->bit_rate / 1000.0; // kbps
  838. }
  839. }
  840. void PlayerCoreV2::updatePerformanceStats() {
  841. std::lock_guard<std::mutex> lock(m_mutex);
  842. m_stats.cpuUsage = calculateCpuUsage();
  843. m_stats.memoryUsage = calculateMemoryUsage();
  844. }
  845. double PlayerCoreV2::calculateCpuUsage() {
  846. #ifdef _WIN32
  847. FILETIME idleTime, kernelTime, userTime;
  848. if (GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
  849. // 简化的CPU使用率计算
  850. return 0.0; // 实际实现需要更复杂的逻辑
  851. }
  852. #endif
  853. return 0.0;
  854. }
  855. double PlayerCoreV2::calculateMemoryUsage() {
  856. #ifdef _WIN32
  857. PROCESS_MEMORY_COUNTERS pmc;
  858. if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
  859. return pmc.WorkingSetSize / (1024.0 * 1024.0); // MB
  860. }
  861. #endif
  862. return 0.0;
  863. }
  864. void PlayerCoreV2::stopAllThreads() {
  865. Logger::instance().info("Stopping all threads...");
  866. m_threadsShouldStop = true;
  867. // 等待线程结束
  868. if (m_readThread.joinable()) {
  869. m_readThread.join();
  870. }
  871. if (m_videoDecodeThread.joinable()) {
  872. m_videoDecodeThread.join();
  873. }
  874. if (m_audioDecodeThread.joinable()) {
  875. m_audioDecodeThread.join();
  876. }
  877. if (m_videoPlayThread.joinable()) {
  878. m_videoPlayThread.join();
  879. }
  880. if (m_audioPlayThread.joinable()) {
  881. m_audioPlayThread.join();
  882. }
  883. m_threadsRunning = false;
  884. Logger::instance().info("All threads stopped");
  885. }
  886. void PlayerCoreV2::flushBuffers() {
  887. if (m_videoPacketQueue) m_videoPacketQueue->clear();
  888. if (m_audioPacketQueue) m_audioPacketQueue->clear();
  889. if (m_videoFrameQueue) m_videoFrameQueue->clear();
  890. if (m_audioFrameQueue) m_audioFrameQueue->clear();
  891. }
  892. void PlayerCoreV2::readThreadFunc() {
  893. Logger::instance().info("Read thread started");
  894. AVPacket* packet = av_packet_alloc();
  895. if (!packet) {
  896. Logger::instance().error("Failed to allocate packet");
  897. return;
  898. }
  899. while (!m_threadsShouldStop) {
  900. // 检查是否需要seek
  901. if (m_seeking) {
  902. std::unique_lock<std::mutex> lock(m_seekMutex);
  903. // 执行seek操作
  904. int64_t seekTarget = m_seekTarget;
  905. int flags = AVSEEK_FLAG_BACKWARD;
  906. if (av_seek_frame(m_formatContext, -1, seekTarget, flags) < 0) {
  907. Logger::instance().error("Seek failed");
  908. } else {
  909. Logger::instance().info("Seek completed to: " + std::to_string(seekTarget));
  910. // 清空缓冲区
  911. flushBuffers();
  912. // 重置解码器
  913. resetDecoders();
  914. }
  915. m_seeking = false;
  916. setState(m_state == PlayerState::Seeking ? PlayerState::Playing : m_state.load());
  917. lock.unlock();
  918. m_seekCondition.notify_all();
  919. }
  920. // 检查队列是否已满
  921. bool videoQueueFull = m_videoPacketQueue && m_videoPacketQueue->size() > 500;
  922. bool audioQueueFull = m_audioPacketQueue && m_audioPacketQueue->size() > 500;
  923. if (videoQueueFull && audioQueueFull) {
  924. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  925. continue;
  926. }
  927. // 读取数据包
  928. int ret = av_read_frame(m_formatContext, packet);
  929. if (ret < 0) {
  930. if (ret == AVERROR_EOF) {
  931. Logger::instance().info("End of file reached, sending EOF packets to queues");
  932. // 向视频和音频队列分别发送EOF标记,让解码线程知道文件结束
  933. if (m_videoPacketQueue && m_mediaInfo.hasVideo) {
  934. AVPacket* eofPacket = av_packet_alloc();
  935. if (eofPacket) {
  936. eofPacket->data = nullptr;
  937. eofPacket->size = 0;
  938. eofPacket->stream_index = -1; // 特殊标记表示EOF
  939. m_videoPacketQueue->push(eofPacket);
  940. Logger::instance().info("EOF packet sent to video queue");
  941. }
  942. }
  943. if (m_audioPacketQueue && m_mediaInfo.hasAudio) {
  944. AVPacket* eofPacket = av_packet_alloc();
  945. if (eofPacket) {
  946. eofPacket->data = nullptr;
  947. eofPacket->size = 0;
  948. eofPacket->stream_index = -1; // 特殊标记表示EOF
  949. m_audioPacketQueue->push(eofPacket);
  950. Logger::instance().info("EOF packet sent to audio queue");
  951. }
  952. }
  953. break;
  954. } else {
  955. Logger::instance().error("Error reading frame: " + std::to_string(ret));
  956. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  957. continue;
  958. }
  959. }
  960. // 根据流索引将数据包分发到对应队列
  961. if (packet->stream_index == m_mediaInfo.videoStreamIndex) {
  962. // 视频包放入视频队列
  963. if (m_videoPacketQueue && (!videoQueueFull || m_videoPacketQueue->size() < 500)) {
  964. AVPacket* packetCopy = av_packet_alloc();
  965. if (packetCopy && av_packet_ref(packetCopy, packet) == 0) {
  966. m_videoPacketQueue->push(packetCopy);
  967. } else {
  968. av_packet_free(&packetCopy);
  969. }
  970. }
  971. } else if (packet->stream_index == m_mediaInfo.audioStreamIndex) {
  972. // 音频包放入音频队列
  973. if (m_audioPacketQueue && (!audioQueueFull || m_audioPacketQueue->size() < 500)) {
  974. AVPacket* packetCopy = av_packet_alloc();
  975. if (packetCopy && av_packet_ref(packetCopy, packet) == 0) {
  976. m_audioPacketQueue->push(packetCopy);
  977. } else {
  978. av_packet_free(&packetCopy);
  979. }
  980. }
  981. }
  982. av_packet_unref(packet);
  983. }
  984. av_packet_free(&packet);
  985. Logger::instance().info("Read thread finished");
  986. }
  987. void PlayerCoreV2::videoDecodeThreadFunc() {
  988. Logger::instance().info("Video decode thread started");
  989. int packetCount = 0;
  990. int frameCount = 0;
  991. while (!m_threadsShouldStop) {
  992. if (!m_videoPacketQueue || !m_videoFrameQueue || !m_videoDecoder) {
  993. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  994. continue;
  995. }
  996. // 从视频包队列获取包
  997. AVPacket* packet = nullptr;
  998. while (!m_threadsShouldStop && !packet) {
  999. packet = m_videoPacketQueue->pop();
  1000. if (packet) {
  1001. // 检查是否是EOF标记
  1002. if (packet->stream_index == -1 && packet->data == nullptr) {
  1003. Logger::instance().info("Video decode thread received EOF packet");
  1004. av_packet_free(&packet);
  1005. // 向视频帧队列发送EOF标记
  1006. if (m_videoFrameQueue) {
  1007. AVFrame* eofFrame = av_frame_alloc();
  1008. if (eofFrame) {
  1009. eofFrame->data[0] = nullptr;
  1010. eofFrame->width = 0;
  1011. eofFrame->height = 0;
  1012. eofFrame->pts = AV_NOPTS_VALUE;
  1013. m_videoFrameQueue->push(eofFrame);
  1014. Logger::instance().info("EOF frame sent to video frame queue");
  1015. }
  1016. }
  1017. // 视频解码线程结束
  1018. Logger::instance().info("Video decode thread finishing due to EOF");
  1019. return;
  1020. }
  1021. }
  1022. if (!packet) {
  1023. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  1024. }
  1025. }
  1026. if (!packet) {
  1027. Logger::instance().debug("Video decode thread: no more packets available");
  1028. continue;
  1029. }
  1030. packetCount++;
  1031. Logger::instance().debug("Video decode thread got packet #" + std::to_string(packetCount) +
  1032. ", size=" + std::to_string(packet->size) +
  1033. ", pts=" + std::to_string(packet->pts));
  1034. // 解码视频帧
  1035. AVPacketPtr packetPtr(packet);
  1036. std::vector<AVFramePtr> frames;
  1037. ErrorCode decodeResult = m_videoDecoder->decode(packetPtr, frames);
  1038. Logger::instance().debug("Video decode result: " + std::to_string(static_cast<int>(decodeResult)) +
  1039. ", frames count: " + std::to_string(frames.size()));
  1040. if (decodeResult == ErrorCode::SUCCESS) {
  1041. for (auto& framePtr : frames) {
  1042. if (framePtr && !m_threadsShouldStop) {
  1043. frameCount++;
  1044. Logger::instance().debug("Processing video frame #" + std::to_string(frameCount) +
  1045. ", width=" + std::to_string(framePtr->width) +
  1046. ", height=" + std::to_string(framePtr->height) +
  1047. ", pts=" + std::to_string(framePtr->pts));
  1048. // 设置帧的时间戳(保持原始PTS,在播放时再转换)
  1049. if (framePtr->pts != AV_NOPTS_VALUE) {
  1050. Logger::instance().debug("Frame PTS: " + std::to_string(framePtr->pts));
  1051. }
  1052. // 将帧放入队列
  1053. if (m_videoFrameQueue->size() < 30) { // 限制队列大小
  1054. // 释放智能指针的所有权,让队列管理帧的生命周期
  1055. m_videoFrameQueue->push(framePtr.release());
  1056. m_frameCount++;
  1057. Logger::instance().debug("Video frame pushed to queue, queue size: " + std::to_string(m_videoFrameQueue->size()));
  1058. } else {
  1059. Logger::instance().warning("Video frame queue full, dropping frame");
  1060. }
  1061. }
  1062. }
  1063. } else {
  1064. Logger::instance().warning("Video decode failed with error: " + std::to_string(static_cast<int>(decodeResult)));
  1065. }
  1066. // packet已经被packetPtr管理,不需要手动释放
  1067. }
  1068. Logger::instance().info("Video decode thread finished, packets processed: " + std::to_string(packetCount) +
  1069. ", frames decoded: " + std::to_string(frameCount));
  1070. }
  1071. void PlayerCoreV2::audioDecodeThreadFunc() {
  1072. Logger::instance().info("Audio decode thread started");
  1073. int packetCount = 0;
  1074. int frameCount = 0;
  1075. while (!m_threadsShouldStop) {
  1076. if (!m_audioPacketQueue || !m_audioFrameQueue || !m_audioDecoder) {
  1077. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1078. continue;
  1079. }
  1080. // 从音频包队列获取包
  1081. AVPacket* packet = nullptr;
  1082. while (!m_threadsShouldStop && !packet) {
  1083. packet = m_audioPacketQueue->pop();
  1084. if (packet) {
  1085. // 检查是否是EOF标记
  1086. if (packet->stream_index == -1 && packet->data == nullptr) {
  1087. Logger::instance().info("Audio decode thread received EOF packet");
  1088. av_packet_free(&packet);
  1089. // 向音频帧队列发送EOF标记
  1090. if (m_audioFrameQueue) {
  1091. AVFrame* eofFrame = av_frame_alloc();
  1092. if (eofFrame) {
  1093. eofFrame->data[0] = nullptr;
  1094. eofFrame->nb_samples = 0;
  1095. eofFrame->pts = AV_NOPTS_VALUE;
  1096. m_audioFrameQueue->push(eofFrame);
  1097. Logger::instance().info("EOF frame sent to audio frame queue");
  1098. }
  1099. }
  1100. // 音频解码线程结束
  1101. Logger::instance().info("Audio decode thread finishing due to EOF");
  1102. return;
  1103. }
  1104. }
  1105. if (!packet) {
  1106. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  1107. }
  1108. }
  1109. if (!packet) {
  1110. Logger::instance().debug("Audio decode thread: no more packets available");
  1111. continue;
  1112. }
  1113. packetCount++;
  1114. Logger::instance().debug("Audio decode thread got packet #" + std::to_string(packetCount) +
  1115. ", size=" + std::to_string(packet->size) +
  1116. ", pts=" + std::to_string(packet->pts));
  1117. // 解码音频帧
  1118. AVPacketPtr packetPtr(packet);
  1119. std::vector<AVFramePtr> frames;
  1120. ErrorCode decodeResult = m_audioDecoder->decode(packetPtr, frames);
  1121. Logger::instance().debug("Audio decode result: " + std::to_string(static_cast<int>(decodeResult)) +
  1122. ", frames count: " + std::to_string(frames.size()));
  1123. if (decodeResult == ErrorCode::SUCCESS) {
  1124. for (auto& framePtr : frames) {
  1125. if (framePtr && !m_threadsShouldStop) {
  1126. frameCount++;
  1127. Logger::instance().debug("Processing audio frame #" + std::to_string(frameCount) +
  1128. ", nb_samples=" + std::to_string(framePtr->nb_samples) +
  1129. ", pts=" + std::to_string(framePtr->pts));
  1130. // 设置帧的时间戳(保持原始PTS,在播放时再转换)
  1131. if (framePtr->pts != AV_NOPTS_VALUE) {
  1132. Logger::instance().debug("Audio Frame PTS: " + std::to_string(framePtr->pts));
  1133. }
  1134. // 将帧放入队列
  1135. if (m_audioFrameQueue->size() < 100) { // 限制队列大小
  1136. // 释放智能指针的所有权,让队列管理帧的生命周期
  1137. m_audioFrameQueue->push(framePtr.release());
  1138. Logger::instance().debug("Audio frame pushed to queue, queue size: " + std::to_string(m_audioFrameQueue->size()));
  1139. } else {
  1140. Logger::instance().warning("Audio frame queue full, dropping frame");
  1141. }
  1142. }
  1143. }
  1144. } else {
  1145. Logger::instance().warning("Audio decode failed with error: " + std::to_string(static_cast<int>(decodeResult)));
  1146. }
  1147. // packet已经被packetPtr管理,不需要手动释放
  1148. }
  1149. Logger::instance().info("Audio decode thread finished, packets processed: " + std::to_string(packetCount) +
  1150. ", frames decoded: " + std::to_string(frameCount));
  1151. }
  1152. void PlayerCoreV2::videoPlayThreadFunc() {
  1153. Logger::instance().info("Video play thread started");
  1154. while (!m_threadsShouldStop) {
  1155. if (!m_videoFrameQueue /*|| !m_synchronizer*/) {
  1156. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1157. continue;
  1158. }
  1159. // 获取视频帧,使用超时避免无限阻塞
  1160. AVFrame* frame = m_videoFrameQueue->pop(100); // 100ms超时
  1161. if (!frame) {
  1162. // 检查是否应该继续等待
  1163. if (m_threadsShouldStop) {
  1164. break;
  1165. }
  1166. continue;
  1167. }
  1168. Logger::instance().debug("Video play thread got frame, pts=" + std::to_string(frame->pts));
  1169. // 尝试获取下一帧的PTS来计算精确的帧持续时间
  1170. double nextPts = AV_NOPTS_VALUE;
  1171. if (m_videoFrameQueue->size() > 0) {
  1172. AVFrame* nextFrame = m_videoFrameQueue->peekFrame();
  1173. if (nextFrame && nextFrame->pts != AV_NOPTS_VALUE) {
  1174. nextPts = nextFrame->pts;
  1175. }
  1176. }
  1177. // 创建智能指针管理帧内存
  1178. AVFramePtr framePtr(frame);
  1179. // 显示帧
  1180. if (m_openGLVideoRenderer) {
  1181. m_openGLVideoRenderer->Render(framePtr.get());
  1182. }
  1183. }
  1184. Logger::instance().info("Video play thread finished");
  1185. }
  1186. void PlayerCoreV2::audioPlayThreadFunc() {
  1187. Logger::instance().info("Audio play thread started");
  1188. int frameCount = 0;
  1189. while (!m_threadsShouldStop) {
  1190. if (!m_audioFrameQueue || /* !m_synchronizer ||*/ !m_audioOutput) {
  1191. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1192. continue;
  1193. }
  1194. // 获取音频帧,使用较长的超时时间以确保能接收到EOF帧
  1195. AVFrame* frame = m_audioFrameQueue->pop(100); // 100ms超时
  1196. if (!frame) {
  1197. // 检查是否应该继续等待
  1198. if (m_threadsShouldStop) {
  1199. break;
  1200. }
  1201. continue;
  1202. }
  1203. // 检查是否是EOF帧
  1204. if (frame->data[0] == nullptr && frame->nb_samples == 0) {
  1205. Logger::instance().info("Audio play thread received EOF frame, playback completed");
  1206. av_frame_free(&frame);
  1207. // 通知播放完成
  1208. setState(PlayerState::Stopped);
  1209. Logger::instance().info("Audio playback finished naturally");
  1210. return;
  1211. }
  1212. frameCount++;
  1213. Logger::instance().debug("Audio play thread got frame #" + std::to_string(frameCount) +
  1214. ", pts=" + std::to_string(frame->pts) +
  1215. ", nb_samples=" + std::to_string(frame->nb_samples));
  1216. // 创建智能指针管理帧内存
  1217. AVFramePtr framePtr(frame);
  1218. {
  1219. Logger::instance().debug("Writing audio frame to output device");
  1220. bool writeResult = m_audioOutput->writeFrame(framePtr);
  1221. Logger::instance().debug("Audio frame write result: " + std::to_string(writeResult));
  1222. // 如果写入失败,等待一段时间避免快速循环
  1223. if (!writeResult) {
  1224. Logger::instance().warning("Audio frame write failed, waiting before next frame");
  1225. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1226. }
  1227. }
  1228. // framePtr 会自动释放内存,无需手动调用 av_frame_free
  1229. }
  1230. Logger::instance().info("Audio play thread finished, total frames processed: " + std::to_string(frameCount));
  1231. }
  1232. } // namespace player
  1233. } // namespace av