receive_time_calculator.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef CALL_RECEIVE_TIME_CALCULATOR_H_
  11. #define CALL_RECEIVE_TIME_CALCULATOR_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include "api/units/time_delta.h"
  15. #include "rtc_base/experiments/field_trial_parser.h"
  16. namespace webrtc {
  17. struct ReceiveTimeCalculatorConfig {
  18. ReceiveTimeCalculatorConfig();
  19. ReceiveTimeCalculatorConfig(const ReceiveTimeCalculatorConfig&);
  20. ReceiveTimeCalculatorConfig& operator=(const ReceiveTimeCalculatorConfig&) =
  21. default;
  22. ~ReceiveTimeCalculatorConfig();
  23. FieldTrialParameter<TimeDelta> max_packet_time_repair;
  24. FieldTrialParameter<TimeDelta> stall_threshold;
  25. FieldTrialParameter<TimeDelta> tolerance;
  26. FieldTrialParameter<TimeDelta> max_stall;
  27. };
  28. // The receive time calculator serves the purpose of combining packet time
  29. // stamps with a safely incremental clock. This assumes that the packet time
  30. // stamps are based on lower layer timestamps that have more accurate time
  31. // increments since they are based on the exact receive time. They might
  32. // however, have large jumps due to clock resets in the system. To compensate
  33. // this they are combined with a safe clock source that is guaranteed to be
  34. // consistent, but it will not be able to measure the exact time when a packet
  35. // is received.
  36. class ReceiveTimeCalculator {
  37. public:
  38. static std::unique_ptr<ReceiveTimeCalculator> CreateFromFieldTrial();
  39. ReceiveTimeCalculator();
  40. int64_t ReconcileReceiveTimes(int64_t packet_time_us_,
  41. int64_t system_time_us_,
  42. int64_t safe_time_us_);
  43. private:
  44. int64_t last_corrected_time_us_ = -1;
  45. int64_t last_packet_time_us_ = -1;
  46. int64_t last_system_time_us_ = -1;
  47. int64_t last_safe_time_us_ = -1;
  48. int64_t total_system_time_passed_us_ = 0;
  49. int64_t static_clock_offset_us_ = 0;
  50. int64_t small_reset_during_stall_ = false;
  51. ReceiveTimeCalculatorConfig config_;
  52. };
  53. } // namespace webrtc
  54. #endif // CALL_RECEIVE_TIME_CALCULATOR_H_