time_keeper.cpp 860 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "time_keeper.h"
  2. #include "defs.h"
  3. namespace ffmpeg {
  4. namespace {
  5. const long kMaxTimeBaseDiference = 10;
  6. }
  7. long TimeKeeper::adjust(long& decoderTimestamp) {
  8. const long now = std::chrono::duration_cast<std::chrono::microseconds>(
  9. std::chrono::system_clock::now().time_since_epoch())
  10. .count();
  11. if (startTime_ == 0) {
  12. startTime_ = now;
  13. }
  14. if (streamTimestamp_ == 0) {
  15. streamTimestamp_ = decoderTimestamp;
  16. }
  17. const auto runOut = startTime_ + decoderTimestamp - streamTimestamp_;
  18. if (std::labs((now - runOut) / AV_TIME_BASE) > kMaxTimeBaseDiference) {
  19. streamTimestamp_ = startTime_ - now + decoderTimestamp;
  20. }
  21. const auto sleepAdvised = runOut - now;
  22. decoderTimestamp += startTime_ - streamTimestamp_;
  23. return sleepAdvised > 0 ? sleepAdvised : 0;
  24. }
  25. } // namespace ffmpeg