player_core_v2.cpp 62 KB

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