utils_synchronizer_v2.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. return clock->ptsDrift + time + (time - clock->lastUpdated) * (1.0 - clock->speed);
  194. }
  195. // 设置时钟
  196. void SynchronizerV2::setClock(ClockType type, double pts, int serial) {
  197. setClockAt(type, pts, serial, getCurrentTime());
  198. }
  199. // 在指定时间设置时钟
  200. void SynchronizerV2::setClockAt(ClockType type, double pts, int serial, double time) {
  201. std::lock_guard<std::mutex> lock(m_clockMutex);
  202. Clock* clock = nullptr;
  203. switch (type) {
  204. case ClockType::AUDIO:
  205. clock = &m_audioClock;
  206. break;
  207. case ClockType::VIDEO:
  208. clock = &m_videoClock;
  209. break;
  210. case ClockType::EXTERNAL:
  211. clock = &m_externalClock;
  212. break;
  213. }
  214. if (!clock) {
  215. return;
  216. }
  217. // 更新时钟
  218. clock->pts = pts;
  219. clock->lastUpdated = time;
  220. clock->ptsDrift = clock->pts - time;
  221. clock->serial = serial;
  222. // 更新统计信息
  223. //updateStats();
  224. }
  225. // 设置时钟速度
  226. void SynchronizerV2::setClockSpeed(ClockType type, double speed) {
  227. //std::lock_guard<std::mutex> lock(m_clockMutex);
  228. Clock* clock = nullptr;
  229. switch (type) {
  230. case ClockType::AUDIO:
  231. clock = &m_audioClock;
  232. break;
  233. case ClockType::VIDEO:
  234. clock = &m_videoClock;
  235. break;
  236. case ClockType::EXTERNAL:
  237. clock = &m_externalClock;
  238. break;
  239. }
  240. if (!clock) {
  241. return;
  242. }
  243. // 设置时钟速度前先更新当前pts
  244. double pts = getClock(type);
  245. clock->pts = pts;
  246. clock->lastUpdated = getCurrentTime();
  247. clock->ptsDrift = clock->pts - clock->lastUpdated;
  248. clock->speed = speed;
  249. }
  250. // 获取主时钟类型
  251. ClockType SynchronizerV2::getMasterClockType() const {
  252. // 根据同步策略和流信息确定主时钟类型
  253. switch (m_syncStrategy) {
  254. case SyncStrategy::AUDIO_MASTER:
  255. if (m_hasAudio) {
  256. return ClockType::AUDIO;
  257. }
  258. break;
  259. case SyncStrategy::VIDEO_MASTER:
  260. if (m_hasVideo) {
  261. return ClockType::VIDEO;
  262. }
  263. break;
  264. case SyncStrategy::EXTERNAL_MASTER:
  265. return ClockType::EXTERNAL;
  266. }
  267. // 如果首选的主时钟不可用,则按优先级选择
  268. if (m_hasAudio) {
  269. return ClockType::AUDIO;
  270. } else if (m_hasVideo) {
  271. return ClockType::VIDEO;
  272. } else {
  273. return ClockType::EXTERNAL;
  274. }
  275. }
  276. // 获取主时钟值
  277. double SynchronizerV2::getMasterClock() const {
  278. ClockType masterType = getMasterClockType();
  279. return getClock(masterType);
  280. }
  281. // 计算视频帧目标延迟
  282. double SynchronizerV2::computeTargetDelay(double frameDuration, double videoPts) const {
  283. double delay = 0.0;
  284. // 如果不是视频主时钟,则需要与主时钟同步
  285. if (getMasterClockType() != ClockType::VIDEO) {
  286. // 计算视频时钟与主时钟的差异
  287. double diff = videoPts - getMasterClock();
  288. // 如果差异太大(超过noSyncThreshold),则不进行同步
  289. if (std::fabs(diff) < m_config.noSyncThreshold) {
  290. // 如果视频落后于主时钟或超前太多,则调整延迟
  291. if (diff <= -m_config.syncThresholdMin) { // 视频落后,减少延迟
  292. delay = std::max(0.0, delay + diff);
  293. } else if (diff >= m_config.syncThresholdMax) { // 视频超前太多,增加延迟
  294. delay = delay + diff;
  295. } else if (diff >= m_config.syncThresholdMin) { // 视频略微超前,平滑调整
  296. delay = (delay + diff) * 2.0 / 3.0;
  297. }
  298. }
  299. }
  300. // 添加帧持续时间
  301. delay += frameDuration;
  302. return delay;
  303. }
  304. // 同步视频帧
  305. FrameDecision SynchronizerV2::synchronizeVideo(double pts, int serial, double duration) const {
  306. FrameDecision decision;
  307. // 如果没有视频流,直接显示
  308. if (!m_hasVideo) {
  309. decision.action = FrameAction::DISPLAY;
  310. decision.delay = 0.0;
  311. decision.reason = "No video stream";
  312. return decision;
  313. }
  314. // 如果暂停,直接显示
  315. if (m_paused) {
  316. decision.action = FrameAction::DISPLAY;
  317. decision.delay = 0.0;
  318. decision.reason = "Playback paused";
  319. return decision;
  320. }
  321. // 计算目标延迟
  322. double delay = computeTargetDelay(duration, pts);
  323. // 获取当前时间
  324. double time = getCurrentTime();
  325. // 计算实际延迟(相对于上一帧的显示时间)
  326. double actualDelay = time - m_frameTimer;
  327. // 计算同步误差
  328. double syncError = 0.0;
  329. if (getMasterClockType() != ClockType::VIDEO) {
  330. syncError = pts - getMasterClock();
  331. }
  332. // 更新帧计时器
  333. m_frameTimer += delay;
  334. // 如果实际延迟大于两倍目标延迟,说明系统可能卡顿,重置帧计时器
  335. if (actualDelay > 2.0 * delay) {
  336. m_frameTimer = time;
  337. }
  338. // 决定帧处理动作
  339. if (time < m_frameTimer) {
  340. // 当前时间小于下一帧显示时间,需要等待
  341. decision.action = FrameAction::DISPLAY;
  342. decision.delay = m_frameTimer - time; // 需要等待的时间
  343. decision.syncError = syncError;
  344. decision.reason = "Wait for next frame time";
  345. } else {
  346. // 当前时间已经超过下一帧显示时间
  347. // 如果延迟太大,可能需要丢帧
  348. if (time > m_frameTimer + m_config.frameDropThreshold && getMasterClockType() != ClockType::VIDEO) {
  349. decision.action = FrameAction::DROP;
  350. decision.delay = 0.0;
  351. decision.syncError = syncError;
  352. decision.reason = "Frame too late, dropping";
  353. // 通知帧丢弃
  354. notifyFrameDrop(ClockType::VIDEO, static_cast<int64_t>(pts * 1000));
  355. } else {
  356. // 正常显示
  357. decision.action = FrameAction::DISPLAY;
  358. decision.delay = 0.0; // 立即显示
  359. decision.syncError = syncError;
  360. decision.reason = "Display immediately";
  361. }
  362. }
  363. // 通知同步误差
  364. if (std::fabs(syncError) > m_config.syncThresholdMin) {
  365. notifySyncError(syncError, decision.reason);
  366. }
  367. return decision;
  368. }
  369. // 同步音频
  370. int SynchronizerV2::synchronizeAudio(short* samples, int samplesSize, double pts) {
  371. // 如果没有音频流或者音频是主时钟,则不需要同步
  372. if (!m_hasAudio || getMasterClockType() == ClockType::AUDIO || m_paused) {
  373. return samplesSize;
  374. }
  375. // 计算音频与主时钟的差异
  376. double diff = getClock(ClockType::AUDIO) - getMasterClock();
  377. // 如果差异太大,则不进行同步
  378. if (std::fabs(diff) > m_config.noSyncThreshold) {
  379. // 重置音频差异累积
  380. m_audioDiffCum = 0.0;
  381. m_audioDiffAvgCount = 0;
  382. return samplesSize;
  383. }
  384. // 更新音频差异平均值
  385. m_audioDiffCum = diff + m_audioDiffCum * (1.0 - m_config.audioDiffAvgCoef);
  386. if (m_audioDiffAvgCount < m_config.audioDiffAvgCount) {
  387. m_audioDiffAvgCount++;
  388. } else {
  389. double avgDiff = m_audioDiffCum / m_audioDiffAvgCount;
  390. // 根据差异调整音频样本数量
  391. if (std::fabs(avgDiff) >= m_config.audioDiffThreshold) {
  392. int wantedSamples = samplesSize;
  393. int minSamples = samplesSize * (1.0 - m_config.audioDiffThreshold);
  394. int maxSamples = samplesSize * (1.0 + m_config.audioDiffThreshold);
  395. // 计算调整后的样本数量
  396. wantedSamples += static_cast<int>(diff * samplesSize);
  397. wantedSamples = std::max(minSamples, std::min(wantedSamples, maxSamples));
  398. return wantedSamples;
  399. }
  400. }
  401. return samplesSize;
  402. }
  403. // 设置配置
  404. void SynchronizerV2::setConfig(const SyncConfigV2& config) {
  405. m_config = config;
  406. m_syncStrategy = config.strategy;
  407. }
  408. // 获取配置
  409. SyncConfigV2 SynchronizerV2::getConfig() const {
  410. return m_config;
  411. }
  412. // 设置同步策略
  413. void SynchronizerV2::setSyncStrategy(SyncStrategy strategy) {
  414. // 检查策略是否可用
  415. if (strategy == SyncStrategy::AUDIO_MASTER && !m_hasAudio) {
  416. if (m_hasVideo) {
  417. strategy = SyncStrategy::VIDEO_MASTER;
  418. } else {
  419. strategy = SyncStrategy::EXTERNAL_MASTER;
  420. }
  421. } else if (strategy == SyncStrategy::VIDEO_MASTER && !m_hasVideo) {
  422. if (m_hasAudio) {
  423. strategy = SyncStrategy::AUDIO_MASTER;
  424. } else {
  425. strategy = SyncStrategy::EXTERNAL_MASTER;
  426. }
  427. }
  428. m_syncStrategy = strategy;
  429. m_config.strategy = strategy;
  430. }
  431. // 获取同步策略
  432. SyncStrategy SynchronizerV2::getSyncStrategy() const {
  433. return m_syncStrategy;
  434. }
  435. // 设置播放速度
  436. void SynchronizerV2::setPlaybackSpeed(double speed) {
  437. // 限制速度范围
  438. speed = std::max(m_config.clockSpeedMin, std::min(speed, m_config.clockSpeedMax));
  439. // 如果速度没有变化,则不需要更新
  440. if (std::fabs(speed - m_playbackSpeed) < 0.01) {
  441. return;
  442. }
  443. // 更新播放速度
  444. m_playbackSpeed = speed;
  445. // 更新所有时钟的速度
  446. setClockSpeed(ClockType::AUDIO, speed);
  447. setClockSpeed(ClockType::VIDEO, speed);
  448. setClockSpeed(ClockType::EXTERNAL, speed);
  449. // 更新统计信息
  450. std::lock_guard<std::mutex> lock(m_statsMutex);
  451. m_stats.playbackSpeed = speed;
  452. }
  453. // 获取播放速度
  454. double SynchronizerV2::getPlaybackSpeed() const {
  455. return m_playbackSpeed;
  456. }
  457. // 获取统计信息
  458. SyncStatsV2 SynchronizerV2::getStats() const {
  459. std::lock_guard<std::mutex> lock(m_statsMutex);
  460. return m_stats;
  461. }
  462. // 设置同步错误回调
  463. void SynchronizerV2::setSyncErrorCallback(const SyncErrorCallback& callback) {
  464. std::lock_guard<std::mutex> lock(m_callbackMutex);
  465. m_syncErrorCallback = callback;
  466. }
  467. // 设置帧丢弃回调
  468. void SynchronizerV2::setFrameDropCallback(const FrameDropCallback& callback) {
  469. std::lock_guard<std::mutex> lock(m_callbackMutex);
  470. m_frameDropCallback = callback;
  471. }
  472. // 获取调试信息
  473. std::string SynchronizerV2::getDebugInfo() const {
  474. std::stringstream ss;
  475. ss.precision(4);
  476. // 获取当前状态
  477. bool initialized = m_initialized;
  478. bool running = m_running;
  479. bool paused = m_paused;
  480. // 获取时钟值
  481. double audioClock = getClock(ClockType::AUDIO);
  482. double videoClock = getClock(ClockType::VIDEO);
  483. double externalClock = getClock(ClockType::EXTERNAL);
  484. double masterClock = getMasterClock();
  485. // 获取同步策略
  486. SyncStrategy strategy = getSyncStrategy();
  487. std::string strategyStr;
  488. switch (strategy) {
  489. case SyncStrategy::AUDIO_MASTER:
  490. strategyStr = "AUDIO_MASTER";
  491. break;
  492. case SyncStrategy::VIDEO_MASTER:
  493. strategyStr = "VIDEO_MASTER";
  494. break;
  495. case SyncStrategy::EXTERNAL_MASTER:
  496. strategyStr = "EXTERNAL_MASTER";
  497. break;
  498. }
  499. // 获取主时钟类型
  500. ClockType masterType = getMasterClockType();
  501. std::string masterTypeStr;
  502. switch (masterType) {
  503. case ClockType::AUDIO:
  504. masterTypeStr = "AUDIO";
  505. break;
  506. case ClockType::VIDEO:
  507. masterTypeStr = "VIDEO";
  508. break;
  509. case ClockType::EXTERNAL:
  510. masterTypeStr = "EXTERNAL";
  511. break;
  512. }
  513. // 获取统计信息
  514. SyncStatsV2 stats = getStats();
  515. // 构建调试信息
  516. ss << "SynchronizerV2 Debug Info:\n";
  517. ss << " State: " << (initialized ? "Initialized" : "Not Initialized")
  518. << ", " << (running ? "Running" : "Stopped")
  519. << ", " << (paused ? "Paused" : "Playing") << "\n";
  520. ss << " Streams: " << (m_hasAudio ? "Audio" : "No Audio")
  521. << ", " << (m_hasVideo ? "Video" : "No Video") << "\n";
  522. ss << " Sync Strategy: " << strategyStr << "\n";
  523. ss << " Master Clock Type: " << masterTypeStr << "\n";
  524. ss << " Clocks (seconds):\n";
  525. ss << " Audio: " << audioClock << "\n";
  526. ss << " Video: " << videoClock << "\n";
  527. ss << " External: " << externalClock << "\n";
  528. ss << " Master: " << masterClock << "\n";
  529. ss << " Playback Speed: " << m_playbackSpeed << "\n";
  530. ss << " Stats:\n";
  531. ss << " Audio-Video Sync Error: " << stats.audioVideoSyncError << " seconds\n";
  532. ss << " Avg Sync Error: " << stats.avgSyncError << " seconds\n";
  533. ss << " Max Sync Error: " << stats.maxSyncError << " seconds\n";
  534. ss << " Dropped Frames: " << stats.droppedFrames << "\n";
  535. ss << " Duplicated Frames: " << stats.duplicatedFrames << "\n";
  536. return ss.str();
  537. }
  538. // 初始化时钟
  539. void SynchronizerV2::initClock(Clock& clock, int serial) {
  540. clock.pts = 0.0;
  541. clock.ptsDrift = 0.0;
  542. clock.lastUpdated = getCurrentTime();
  543. clock.speed = m_playbackSpeed;
  544. clock.serial = serial;
  545. clock.paused = false;
  546. clock.queueSerial = serial;
  547. }
  548. // 将时钟同步到从属时钟
  549. void SynchronizerV2::syncClockToSlave(Clock& c, const Clock& slave) {
  550. double clockDiff = getClock(ClockType::EXTERNAL) - getClock(ClockType::AUDIO);
  551. double correction = 0.0;
  552. // 根据差异调整时钟速度
  553. if (std::fabs(clockDiff) < m_config.noSyncThreshold) {
  554. if (clockDiff <= -m_config.syncThresholdMin) {
  555. // 外部时钟落后,加速
  556. correction = -m_config.clockSpeedStep;
  557. } else if (clockDiff >= m_config.syncThresholdMin) {
  558. // 外部时钟超前,减速
  559. correction = m_config.clockSpeedStep;
  560. }
  561. // 应用速度调整
  562. c.speed = m_playbackSpeed + correction;
  563. c.speed = std::max(m_config.clockSpeedMin, std::min(c.speed, m_config.clockSpeedMax));
  564. }
  565. }
  566. // 更新外部时钟
  567. void SynchronizerV2::updateExternalClock() {
  568. // 更新外部时钟,使其与主时钟同步
  569. if (getMasterClockType() != ClockType::EXTERNAL) {
  570. double masterClock = getMasterClock();
  571. setClock(ClockType::EXTERNAL, masterClock, m_externalSerial);
  572. }
  573. }
  574. // 检查外部时钟速度
  575. void SynchronizerV2::checkExternalClockSpeed() {
  576. if (m_hasAudio && getMasterClockType() == ClockType::EXTERNAL) {
  577. // 将外部时钟同步到音频时钟
  578. syncClockToSlave(m_externalClock, m_audioClock);
  579. } else if (m_hasVideo && getMasterClockType() == ClockType::EXTERNAL) {
  580. // 将外部时钟同步到视频时钟
  581. syncClockToSlave(m_externalClock, m_videoClock);
  582. }
  583. }
  584. // 获取当前时间(秒)
  585. double SynchronizerV2::getCurrentTime() const
  586. {
  587. return av_gettime_relative() / 1000000.0;
  588. }
  589. // 更新统计信息
  590. void SynchronizerV2::updateStats() {
  591. std::lock_guard<std::mutex> lock(m_statsMutex);
  592. // 更新时钟值
  593. m_stats.audioClock = getClock(ClockType::AUDIO);
  594. m_stats.videoClock = getClock(ClockType::VIDEO);
  595. m_stats.externalClock = getClock(ClockType::EXTERNAL);
  596. m_stats.masterClock = getMasterClock();
  597. // 计算音视频同步误差
  598. if (m_hasAudio && m_hasVideo) {
  599. m_stats.audioVideoSyncError = m_stats.audioClock - m_stats.videoClock;
  600. // 更新平均同步误差
  601. double absError = std::fabs(m_stats.audioVideoSyncError);
  602. m_stats.avgSyncError = (m_stats.avgSyncError * 0.9) + (absError * 0.1);
  603. // 更新最大同步误差
  604. if (absError > m_stats.maxSyncError) {
  605. m_stats.maxSyncError = absError;
  606. }
  607. }
  608. // 更新播放速度
  609. m_stats.playbackSpeed = m_playbackSpeed;
  610. }
  611. // 通知同步错误
  612. void SynchronizerV2::notifySyncError(double error, const std::string& reason) const {
  613. std::lock_guard<std::mutex> lock(m_callbackMutex);
  614. if (m_syncErrorCallback) {
  615. m_syncErrorCallback(error, reason);
  616. }
  617. }
  618. // 通知帧丢弃
  619. void SynchronizerV2::notifyFrameDrop(ClockType type, int64_t pts) const {
  620. std::lock_guard<std::mutex> lock(m_callbackMutex);
  621. if (m_frameDropCallback) {
  622. m_frameDropCallback(type, pts);
  623. }
  624. // 更新统计信息
  625. std::lock_guard<std::mutex> statsLock(m_statsMutex);
  626. m_stats.droppedFrames++;
  627. }
  628. } // namespace utils
  629. } // namespace av