File: time_keeper.cpp

package info (click to toggle)
pytorch-vision 0.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 15,188 kB
  • sloc: python: 49,008; cpp: 10,019; sh: 610; java: 550; xml: 79; objc: 56; makefile: 32
file content (35 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "time_keeper.h"
#include "defs.h"

namespace ffmpeg {

namespace {
const long kMaxTimeBaseDiference = 10;
}

long TimeKeeper::adjust(long& decoderTimestamp) {
  const long now = std::chrono::duration_cast<std::chrono::microseconds>(
                       std::chrono::system_clock::now().time_since_epoch())
                       .count();

  if (startTime_ == 0) {
    startTime_ = now;
  }
  if (streamTimestamp_ == 0) {
    streamTimestamp_ = decoderTimestamp;
  }

  const auto runOut = startTime_ + decoderTimestamp - streamTimestamp_;

  if (std::labs((now - runOut) / AV_TIME_BASE) > kMaxTimeBaseDiference) {
    streamTimestamp_ = startTime_ - now + decoderTimestamp;
  }

  const auto sleepAdvised = runOut - now;

  decoderTimestamp += startTime_ - streamTimestamp_;

  return sleepAdvised > 0 ? sleepAdvised : 0;
}

} // namespace ffmpeg