utils_synchronizer_v2.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #include "utils_synchronizer_v2.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <sstream>
  6. namespace av {
  7. namespace utils {
  8. // 构造函数
  9. SynchronizerV2::SynchronizerV2(const SyncConfigV2& config)
  10. : m_config(config)
  11. , m_syncStrategy(config.strategy)
  12. , m_playbackSpeed(1.0)
  13. {
  14. // 初始化时钟
  15. initClock(m_audioClock, 0);
  16. initClock(m_videoClock, 0);
  17. initClock(m_externalClock, 0);
  18. // 初始化帧计时器
  19. m_frameTimer = getCurrentTime();
  20. }
  21. // 析构函数
  22. SynchronizerV2::~SynchronizerV2() {
  23. close();
  24. }
  25. // 初始化
  26. ErrorCode SynchronizerV2::initialize() {
  27. if (m_initialized) {
  28. return ErrorCode::SUCCESS;
  29. }
  30. // 重置状态
  31. m_initialized = true;
  32. m_running = false;
  33. m_paused = false;
  34. // 重置时钟
  35. initClock(m_audioClock, 0);
  36. initClock(m_videoClock, 0);
  37. initClock(m_externalClock, 0);
  38. // 重置帧计时器
  39. m_frameTimer = getCurrentTime();
  40. // 重置统计信息
  41. std::lock_guard<std::mutex> lock(m_statsMutex);
  42. m_stats = SyncStatsV2();
  43. return ErrorCode::SUCCESS;
  44. }
  45. // 关闭
  46. void SynchronizerV2::close() {
  47. if (!m_initialized) {
  48. return;
  49. }
  50. stop();
  51. m_initialized = false;
  52. }
  53. // 启动
  54. ErrorCode SynchronizerV2::start() {
  55. if (!m_initialized) {
  56. return ErrorCode::NOT_INITIALIZED;
  57. }
  58. if (m_running) {
  59. return ErrorCode::SUCCESS;
  60. }
  61. // 重置帧计时器
  62. m_frameTimer = getCurrentTime();
  63. // 更新外部时钟
  64. updateExternalClock();
  65. m_running = true;
  66. m_paused = false;
  67. return ErrorCode::SUCCESS;
  68. }
  69. // 停止
  70. ErrorCode SynchronizerV2::stop() {
  71. if (!m_initialized || !m_running) {
  72. return ErrorCode::SUCCESS;
  73. }
  74. m_running = false;
  75. m_paused = false;
  76. return ErrorCode::SUCCESS;
  77. }
  78. // 暂停
  79. ErrorCode SynchronizerV2::pause() {
  80. if (!m_initialized || !m_running) {
  81. return ErrorCode::NOT_STARTED;
  82. }
  83. if (m_paused) {
  84. return ErrorCode::SUCCESS;
  85. }
  86. // 设置时钟暂停状态
  87. std::lock_guard<std::mutex> lock(m_clockMutex);
  88. m_audioClock.paused = true;
  89. m_videoClock.paused = true;
  90. m_externalClock.paused = true;
  91. m_paused = true;
  92. return ErrorCode::SUCCESS;
  93. }
  94. // 恢复
  95. ErrorCode SynchronizerV2::resume() {
  96. if (!m_initialized || !m_running) {
  97. return ErrorCode::NOT_STARTED;
  98. }
  99. if (!m_paused) {
  100. return ErrorCode::SUCCESS;
  101. }
  102. // 恢复时钟暂停状态
  103. std::lock_guard<std::mutex> lock(m_clockMutex);
  104. m_audioClock.paused = false;
  105. m_videoClock.paused = false;
  106. m_externalClock.paused = false;
  107. // 更新外部时钟
  108. updateExternalClock();
  109. m_paused = false;
  110. return ErrorCode::SUCCESS;
  111. }
  112. // 重置
  113. ErrorCode SynchronizerV2::reset() {
  114. if (!m_initialized) {
  115. return ErrorCode::NOT_INITIALIZED;
  116. }
  117. // 重置时钟
  118. std::lock_guard<std::mutex> lock(m_clockMutex);
  119. initClock(m_audioClock, m_audioSerial);
  120. initClock(m_videoClock, m_videoSerial);
  121. initClock(m_externalClock, m_externalSerial);
  122. // 重置帧计时器
  123. m_frameTimer = getCurrentTime();
  124. // 重置音频同步状态
  125. m_audioDiffCum = 0.0;
  126. m_audioDiffAvgCount = 0;
  127. // 重置统计信息
  128. std::lock_guard<std::mutex> statsLock(m_statsMutex);
  129. m_stats = SyncStatsV2();
  130. m_stats.playbackSpeed = m_playbackSpeed;
  131. return ErrorCode::SUCCESS;
  132. }
  133. // 设置流信息
  134. void SynchronizerV2::setStreamInfo(bool hasAudio, bool hasVideo) {
  135. m_hasAudio = hasAudio;
  136. m_hasVideo = hasVideo;
  137. // // 如果没有音频,但同步策略是音频主时钟,则切换到视频主时钟
  138. // if (!hasAudio && m_syncStrategy == SyncStrategy::AUDIO_MASTER) {
  139. // if (hasVideo) {
  140. // setSyncStrategy(SyncStrategy::VIDEO_MASTER);
  141. // } else {
  142. // setSyncStrategy(SyncStrategy::EXTERNAL_MASTER);
  143. // }
  144. // }
  145. // // 如果没有视频,但同步策略是视频主时钟,则切换到音频主时钟
  146. // if (!hasVideo && m_syncStrategy == SyncStrategy::VIDEO_MASTER) {
  147. // if (hasAudio) {
  148. // setSyncStrategy(SyncStrategy::AUDIO_MASTER);
  149. // } else {
  150. // setSyncStrategy(SyncStrategy::EXTERNAL_MASTER);
  151. // }
  152. // }
  153. // 根据可用流调整同步策略
  154. if (hasAudio && hasVideo) {
  155. // 两种流都有,使用音频优先
  156. setSyncStrategy(SyncStrategy::AUDIO_MASTER);
  157. } else if (hasVideo && !hasAudio) {
  158. // 只有视频,强制使用视频主时钟
  159. setSyncStrategy(SyncStrategy::VIDEO_MASTER);
  160. } else if (hasAudio && !hasVideo) {
  161. // 只有音频,强制使用音频主时钟
  162. setSyncStrategy(SyncStrategy::AUDIO_MASTER);
  163. } else {
  164. // 两种流都没有,使用外部时钟
  165. setSyncStrategy(SyncStrategy::EXTERNAL_MASTER);
  166. }
  167. }
  168. // 获取时钟值
  169. double SynchronizerV2::getClock(ClockType type) const {
  170. std::lock_guard<std::mutex> lock(m_clockMutex);
  171. const Clock* clock = nullptr;
  172. switch (type) {
  173. case ClockType::AUDIO:
  174. clock = &m_audioClock;
  175. break;
  176. case ClockType::VIDEO:
  177. clock = &m_videoClock;
  178. break;
  179. case ClockType::EXTERNAL:
  180. clock = &m_externalClock;
  181. break;
  182. }
  183. if (!clock) {
  184. return 0.0;
  185. }
  186. // 如果时钟暂停,直接返回pts
  187. if (clock->paused) {
  188. return clock->pts;
  189. }
  190. double time = getCurrentTime();
  191. // 计算当前时钟时间 = pts + (当前时间 - 上次更新时间) * 速度
  192. return clock->ptsDrift + (time - clock->lastUpdated) * clock->speed;
  193. }
  194. // 设置时钟
  195. void SynchronizerV2::setClock(ClockType type, double pts, int serial) {
  196. setClockAt(type, pts, serial, getCurrentTime());
  197. }
  198. // 在指定时间设置时钟
  199. void SynchronizerV2::setClockAt(ClockType type, double pts, int serial, double time) {
  200. std::lock_guard<std::mutex> lock(m_clockMutex);
  201. Clock* clock = nullptr;
  202. switch (type) {
  203. case ClockType::AUDIO:
  204. clock = &m_audioClock;
  205. break;
  206. case ClockType::VIDEO:
  207. clock = &m_videoClock;
  208. break;
  209. case ClockType::EXTERNAL:
  210. clock = &m_externalClock;
  211. break;
  212. }
  213. if (!clock) {
  214. return;
  215. }
  216. // 更新时钟
  217. clock->pts = pts;
  218. clock->lastUpdated = time;
  219. clock->ptsDrift = clock->pts - time;
  220. clock->serial = serial;
  221. // 更新统计信息
  222. //updateStats();
  223. }
  224. // 设置时钟速度
  225. void SynchronizerV2::setClockSpeed(ClockType type, double speed) {
  226. //std::lock_guard<std::mutex> lock(m_clockMutex);
  227. Clock* clock = nullptr;
  228. switch (type) {
  229. case ClockType::AUDIO:
  230. clock = &m_audioClock;
  231. break;
  232. case ClockType::VIDEO:
  233. clock = &m_videoClock;
  234. break;
  235. case ClockType::EXTERNAL:
  236. clock = &m_externalClock;
  237. break;
  238. }
  239. if (!clock) {
  240. return;
  241. }
  242. // 设置时钟速度前先更新当前pts
  243. double pts = getClock(type);
  244. clock->pts = pts;
  245. clock->lastUpdated = getCurrentTime();
  246. clock->ptsDrift = clock->pts - clock->lastUpdated;
  247. clock->speed = speed;
  248. }
  249. // 获取主时钟类型
  250. ClockType SynchronizerV2::getMasterClockType() const {
  251. // 根据同步策略和流信息确定主时钟类型
  252. switch (m_syncStrategy) {
  253. case SyncStrategy::AUDIO_MASTER:
  254. if (m_hasAudio) {
  255. return ClockType::AUDIO;
  256. }
  257. break;
  258. case SyncStrategy::VIDEO_MASTER:
  259. if (m_hasVideo) {
  260. return ClockType::VIDEO;
  261. }
  262. break;
  263. case SyncStrategy::EXTERNAL_MASTER:
  264. return ClockType::EXTERNAL;
  265. }
  266. // 如果首选的主时钟不可用,则按优先级选择
  267. if (m_hasAudio) {
  268. return ClockType::AUDIO;
  269. } else if (m_hasVideo) {
  270. return ClockType::VIDEO;
  271. } else {
  272. return ClockType::EXTERNAL;
  273. }
  274. }
  275. // 获取主时钟值
  276. double SynchronizerV2::getMasterClock() const {
  277. ClockType masterType = getMasterClockType();
  278. return getClock(masterType);
  279. }
  280. // 计算视频帧目标延迟
  281. double SynchronizerV2::computeTargetDelay(double frameDuration, double videoPts) const {
  282. double delay = 0.0;
  283. // 如果不是视频主时钟,则需要与主时钟同步
  284. if (getMasterClockType() != ClockType::VIDEO) {
  285. // 计算视频时钟与主时钟的差异
  286. double diff = videoPts - getMasterClock();
  287. // 如果差异太大(超过noSyncThreshold),则不进行同步
  288. if (std::fabs(diff) < m_config.noSyncThreshold) {
  289. // 如果视频落后于主时钟或超前太多,则调整延迟
  290. if (diff <= -m_config.syncThresholdMin) { // 视频落后,减少延迟
  291. delay = std::max(0.0, delay + diff);
  292. } else if (diff >= m_config.syncThresholdMax) { // 视频超前太多,增加延迟
  293. delay = delay + diff;
  294. } else if (diff >= m_config.syncThresholdMin) { // 视频略微超前,平滑调整
  295. delay = (delay + diff) * 2.0 / 3.0;
  296. }
  297. }
  298. }
  299. // 添加帧持续时间
  300. delay += frameDuration;
  301. return delay;
  302. }
  303. // 同步视频帧
  304. FrameDecision SynchronizerV2::synchronizeVideo(double pts, int serial, double duration) const {
  305. FrameDecision decision;
  306. // 如果没有视频流,直接显示
  307. if (!m_hasVideo) {
  308. decision.action = FrameAction::DISPLAY;
  309. decision.delay = 0.0;
  310. decision.reason = "No video stream";
  311. return decision;
  312. }
  313. // 如果暂停,直接显示
  314. if (m_paused) {
  315. decision.action = FrameAction::DISPLAY;
  316. decision.delay = 0.0;
  317. decision.reason = "Playback paused";
  318. return decision;
  319. }
  320. // 计算目标延迟
  321. double delay = computeTargetDelay(duration, pts);
  322. // 获取当前时间
  323. double time = getCurrentTime();
  324. // 计算实际延迟(相对于上一帧的显示时间)
  325. double actualDelay = time - m_frameTimer;
  326. // 计算同步误差
  327. double syncError = 0.0;
  328. if (getMasterClockType() != ClockType::VIDEO) {
  329. syncError = pts - getMasterClock();
  330. }
  331. // 更新帧计时器
  332. m_frameTimer += delay;
  333. // 如果实际延迟大于两倍目标延迟,说明系统可能卡顿,重置帧计时器
  334. if (actualDelay > 2.0 * delay) {
  335. m_frameTimer = time;
  336. }
  337. // 决定帧处理动作
  338. if (time < m_frameTimer) {
  339. // 当前时间小于下一帧显示时间,需要等待
  340. decision.action = FrameAction::DISPLAY;
  341. decision.delay = m_frameTimer - time; // 需要等待的时间
  342. decision.syncError = syncError;
  343. decision.reason = "Wait for next frame time";
  344. } else {
  345. // 当前时间已经超过下一帧显示时间
  346. // 如果延迟太大,可能需要丢帧
  347. if (time > m_frameTimer + m_config.frameDropThreshold && getMasterClockType() != ClockType::VIDEO) {
  348. decision.action = FrameAction::DROP;
  349. decision.delay = 0.0;
  350. decision.syncError = syncError;
  351. decision.reason = "Frame too late, dropping";
  352. // 通知帧丢弃
  353. notifyFrameDrop(ClockType::VIDEO, static_cast<int64_t>(pts * 1000));
  354. } else {
  355. // 正常显示
  356. decision.action = FrameAction::DISPLAY;
  357. decision.delay = 0.0; // 立即显示
  358. decision.syncError = syncError;
  359. decision.reason = "Display immediately";
  360. }
  361. }
  362. // 通知同步误差
  363. if (std::fabs(syncError) > m_config.syncThresholdMin) {
  364. notifySyncError(syncError, decision.reason);
  365. }
  366. return decision;
  367. }
  368. // 同步音频
  369. int SynchronizerV2::synchronizeAudio(short* samples, int samplesSize, double pts) {
  370. // 如果没有音频流或者音频是主时钟,则不需要同步
  371. if (!m_hasAudio || getMasterClockType() == ClockType::AUDIO || m_paused) {
  372. return samplesSize;
  373. }
  374. // 计算音频与主时钟的差异
  375. double diff = getClock(ClockType::AUDIO) - getMasterClock();
  376. // 如果差异太大,则不进行同步
  377. if (std::fabs(diff) > m_config.noSyncThreshold) {
  378. // 重置音频差异累积
  379. m_audioDiffCum = 0.0;
  380. m_audioDiffAvgCount = 0;
  381. return samplesSize;
  382. }
  383. // 更新音频差异平均值
  384. m_audioDiffCum = diff + m_audioDiffCum * (1.0 - m_config.audioDiffAvgCoef);
  385. if (m_audioDiffAvgCount < m_config.audioDiffAvgCount) {
  386. m_audioDiffAvgCount++;
  387. } else {
  388. double avgDiff = m_audioDiffCum / m_audioDiffAvgCount;
  389. // 根据差异调整音频样本数量
  390. if (std::fabs(avgDiff) >= m_config.audioDiffThreshold) {
  391. int wantedSamples = samplesSize;
  392. int minSamples = samplesSize * (1.0 - m_config.audioDiffThreshold);
  393. int maxSamples = samplesSize * (1.0 + m_config.audioDiffThreshold);
  394. // 计算调整后的样本数量
  395. wantedSamples += static_cast<int>(diff * samplesSize);
  396. wantedSamples = std::max(minSamples, std::min(wantedSamples, maxSamples));
  397. return wantedSamples;
  398. }
  399. }
  400. return samplesSize;
  401. }
  402. // 设置配置
  403. void SynchronizerV2::setConfig(const SyncConfigV2& config) {
  404. m_config = config;
  405. m_syncStrategy = config.strategy;
  406. }
  407. // 获取配置
  408. SyncConfigV2 SynchronizerV2::getConfig() const {
  409. return m_config;
  410. }
  411. // 设置同步策略
  412. void SynchronizerV2::setSyncStrategy(SyncStrategy strategy) {
  413. // 检查策略是否可用
  414. if (strategy == SyncStrategy::AUDIO_MASTER && !m_hasAudio) {
  415. if (m_hasVideo) {
  416. strategy = SyncStrategy::VIDEO_MASTER;
  417. } else {
  418. strategy = SyncStrategy::EXTERNAL_MASTER;
  419. }
  420. } else if (strategy == SyncStrategy::VIDEO_MASTER && !m_hasVideo) {
  421. if (m_hasAudio) {
  422. strategy = SyncStrategy::AUDIO_MASTER;
  423. } else {
  424. strategy = SyncStrategy::EXTERNAL_MASTER;
  425. }
  426. }
  427. m_syncStrategy = strategy;
  428. m_config.strategy = strategy;
  429. }
  430. // 获取同步策略
  431. SyncStrategy SynchronizerV2::getSyncStrategy() const {
  432. return m_syncStrategy;
  433. }
  434. // 设置播放速度
  435. void SynchronizerV2::setPlaybackSpeed(double speed) {
  436. // 限制速度范围
  437. speed = std::max(m_config.clockSpeedMin, std::min(speed, m_config.clockSpeedMax));
  438. // 如果速度没有变化,则不需要更新
  439. if (std::fabs(speed - m_playbackSpeed) < 0.01) {
  440. return;
  441. }
  442. // 更新播放速度
  443. m_playbackSpeed = speed;
  444. // 更新所有时钟的速度
  445. setClockSpeed(ClockType::AUDIO, speed);
  446. setClockSpeed(ClockType::VIDEO, speed);
  447. setClockSpeed(ClockType::EXTERNAL, speed);
  448. // 更新统计信息
  449. std::lock_guard<std::mutex> lock(m_statsMutex);
  450. m_stats.playbackSpeed = speed;
  451. }
  452. // 获取播放速度
  453. double SynchronizerV2::getPlaybackSpeed() const {
  454. return m_playbackSpeed;
  455. }
  456. // 获取统计信息
  457. SyncStatsV2 SynchronizerV2::getStats() const {
  458. std::lock_guard<std::mutex> lock(m_statsMutex);
  459. return m_stats;
  460. }
  461. // 设置同步错误回调
  462. void SynchronizerV2::setSyncErrorCallback(const SyncErrorCallback& callback) {
  463. std::lock_guard<std::mutex> lock(m_callbackMutex);
  464. m_syncErrorCallback = callback;
  465. }
  466. // 设置帧丢弃回调
  467. void SynchronizerV2::setFrameDropCallback(const FrameDropCallback& callback) {
  468. std::lock_guard<std::mutex> lock(m_callbackMutex);
  469. m_frameDropCallback = callback;
  470. }
  471. // 获取调试信息
  472. std::string SynchronizerV2::getDebugInfo() const {
  473. std::stringstream ss;
  474. // 获取当前状态
  475. bool initialized = m_initialized;
  476. bool running = m_running;
  477. bool paused = m_paused;
  478. // 获取时钟值
  479. double audioClock = getClock(ClockType::AUDIO);
  480. double videoClock = getClock(ClockType::VIDEO);
  481. double externalClock = getClock(ClockType::EXTERNAL);
  482. double masterClock = getMasterClock();
  483. // 获取同步策略
  484. SyncStrategy strategy = getSyncStrategy();
  485. std::string strategyStr;
  486. switch (strategy) {
  487. case SyncStrategy::AUDIO_MASTER:
  488. strategyStr = "AUDIO_MASTER";
  489. break;
  490. case SyncStrategy::VIDEO_MASTER:
  491. strategyStr = "VIDEO_MASTER";
  492. break;
  493. case SyncStrategy::EXTERNAL_MASTER:
  494. strategyStr = "EXTERNAL_MASTER";
  495. break;
  496. }
  497. // 获取主时钟类型
  498. ClockType masterType = getMasterClockType();
  499. std::string masterTypeStr;
  500. switch (masterType) {
  501. case ClockType::AUDIO:
  502. masterTypeStr = "AUDIO";
  503. break;
  504. case ClockType::VIDEO:
  505. masterTypeStr = "VIDEO";
  506. break;
  507. case ClockType::EXTERNAL:
  508. masterTypeStr = "EXTERNAL";
  509. break;
  510. }
  511. // 获取统计信息
  512. SyncStatsV2 stats = getStats();
  513. // 构建调试信息
  514. ss << "SynchronizerV2 Debug Info:\n";
  515. ss << " State: " << (initialized ? "Initialized" : "Not Initialized")
  516. << ", " << (running ? "Running" : "Stopped")
  517. << ", " << (paused ? "Paused" : "Playing") << "\n";
  518. ss << " Streams: " << (m_hasAudio ? "Audio" : "No Audio")
  519. << ", " << (m_hasVideo ? "Video" : "No Video") << "\n";
  520. ss << " Sync Strategy: " << strategyStr << "\n";
  521. ss << " Master Clock Type: " << masterTypeStr << "\n";
  522. ss << " Clocks (seconds):\n";
  523. ss << " Audio: " << audioClock << "\n";
  524. ss << " Video: " << videoClock << "\n";
  525. ss << " External: " << externalClock << "\n";
  526. ss << " Master: " << masterClock << "\n";
  527. ss << " Playback Speed: " << m_playbackSpeed << "\n";
  528. ss << " Stats:\n";
  529. ss << " Audio-Video Sync Error: " << stats.audioVideoSyncError << " seconds\n";
  530. ss << " Avg Sync Error: " << stats.avgSyncError << " seconds\n";
  531. ss << " Max Sync Error: " << stats.maxSyncError << " seconds\n";
  532. ss << " Dropped Frames: " << stats.droppedFrames << "\n";
  533. ss << " Duplicated Frames: " << stats.duplicatedFrames << "\n";
  534. return ss.str();
  535. }
  536. // 初始化时钟
  537. void SynchronizerV2::initClock(Clock& clock, int serial) {
  538. clock.pts = 0.0;
  539. clock.ptsDrift = 0.0;
  540. clock.lastUpdated = getCurrentTime();
  541. clock.speed = m_playbackSpeed;
  542. clock.serial = serial;
  543. clock.paused = false;
  544. clock.queueSerial = serial;
  545. }
  546. // 将时钟同步到从属时钟
  547. void SynchronizerV2::syncClockToSlave(Clock& c, const Clock& slave) {
  548. double clockDiff = getClock(ClockType::EXTERNAL) - getClock(ClockType::AUDIO);
  549. double correction = 0.0;
  550. // 根据差异调整时钟速度
  551. if (std::fabs(clockDiff) < m_config.noSyncThreshold) {
  552. if (clockDiff <= -m_config.syncThresholdMin) {
  553. // 外部时钟落后,加速
  554. correction = -m_config.clockSpeedStep;
  555. } else if (clockDiff >= m_config.syncThresholdMin) {
  556. // 外部时钟超前,减速
  557. correction = m_config.clockSpeedStep;
  558. }
  559. // 应用速度调整
  560. c.speed = m_playbackSpeed + correction;
  561. c.speed = std::max(m_config.clockSpeedMin, std::min(c.speed, m_config.clockSpeedMax));
  562. }
  563. }
  564. // 更新外部时钟
  565. void SynchronizerV2::updateExternalClock() {
  566. // 更新外部时钟,使其与主时钟同步
  567. if (getMasterClockType() != ClockType::EXTERNAL) {
  568. double masterClock = getMasterClock();
  569. setClock(ClockType::EXTERNAL, masterClock, m_externalSerial);
  570. }
  571. }
  572. // 检查外部时钟速度
  573. void SynchronizerV2::checkExternalClockSpeed() {
  574. if (m_hasAudio && getMasterClockType() == ClockType::EXTERNAL) {
  575. // 将外部时钟同步到音频时钟
  576. syncClockToSlave(m_externalClock, m_audioClock);
  577. } else if (m_hasVideo && getMasterClockType() == ClockType::EXTERNAL) {
  578. // 将外部时钟同步到视频时钟
  579. syncClockToSlave(m_externalClock, m_videoClock);
  580. }
  581. }
  582. // 获取当前时间(秒)
  583. double SynchronizerV2::getCurrentTime() const {
  584. // 使用av_gettime_relative获取相对时间(微秒),转换为秒
  585. return av_gettime_relative() / 1000000.0;
  586. }
  587. // 更新统计信息
  588. void SynchronizerV2::updateStats() {
  589. std::lock_guard<std::mutex> lock(m_statsMutex);
  590. // 更新时钟值
  591. m_stats.audioClock = getClock(ClockType::AUDIO);
  592. m_stats.videoClock = getClock(ClockType::VIDEO);
  593. m_stats.externalClock = getClock(ClockType::EXTERNAL);
  594. m_stats.masterClock = getMasterClock();
  595. // 计算音视频同步误差
  596. if (m_hasAudio && m_hasVideo) {
  597. m_stats.audioVideoSyncError = m_stats.audioClock - m_stats.videoClock;
  598. // 更新平均同步误差
  599. double absError = std::fabs(m_stats.audioVideoSyncError);
  600. m_stats.avgSyncError = (m_stats.avgSyncError * 0.9) + (absError * 0.1);
  601. // 更新最大同步误差
  602. if (absError > m_stats.maxSyncError) {
  603. m_stats.maxSyncError = absError;
  604. }
  605. }
  606. // 更新播放速度
  607. m_stats.playbackSpeed = m_playbackSpeed;
  608. }
  609. // 通知同步错误
  610. void SynchronizerV2::notifySyncError(double error, const std::string& reason) const {
  611. std::lock_guard<std::mutex> lock(m_callbackMutex);
  612. if (m_syncErrorCallback) {
  613. m_syncErrorCallback(error, reason);
  614. }
  615. }
  616. // 通知帧丢弃
  617. void SynchronizerV2::notifyFrameDrop(ClockType type, int64_t pts) const {
  618. std::lock_guard<std::mutex> lock(m_callbackMutex);
  619. if (m_frameDropCallback) {
  620. m_frameDropCallback(type, pts);
  621. }
  622. // 更新统计信息
  623. std::lock_guard<std::mutex> statsLock(m_statsMutex);
  624. m_stats.droppedFrames++;
  625. }
  626. } // namespace utils
  627. } // namespace av