av_clock.h 814 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #ifndef AVCLOCK_H
  2. #define AVCLOCK_H
  3. #include "ffmpeg_compat.h"
  4. class AVClock
  5. {
  6. public:
  7. AVClock()
  8. : m_pts(0.0)
  9. , m_drift(0.0)
  10. {}
  11. inline void reset()
  12. {
  13. m_pts = 0.0;
  14. m_drift = 0.0;
  15. }
  16. inline void setClock(double pts) { setCloctAt(pts); }
  17. inline double getClock() {
  18. // 使用高精度时间计算,减少累积误差
  19. double currentTime = av_gettime_relative() / 1000000.0;
  20. return m_drift + currentTime;
  21. }
  22. private:
  23. inline void setCloctAt(double pts)
  24. {
  25. // 使用高精度时间设置,减少设置时的精度损失
  26. double currentTime = av_gettime_relative() / 1000000.0;
  27. m_drift = pts - currentTime;
  28. m_pts = pts;
  29. }
  30. double m_pts;
  31. double m_drift;
  32. };
  33. #endif // AVCLOCK_H