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 
13 #include <stdint.h>
14 
15 #include <memory>
16 
17 #include "api/units/time_delta.h"
18 #include "rtc_base/experiments/field_trial_parser.h"
19 
20 namespace webrtc {
21 
22 struct ReceiveTimeCalculatorConfig {
23   ReceiveTimeCalculatorConfig();
24   ReceiveTimeCalculatorConfig(const ReceiveTimeCalculatorConfig&);
25   ReceiveTimeCalculatorConfig& operator=(const ReceiveTimeCalculatorConfig&) =
26       default;
27   ~ReceiveTimeCalculatorConfig();
28   FieldTrialParameter<TimeDelta> max_packet_time_repair;
29   FieldTrialParameter<TimeDelta> stall_threshold;
30   FieldTrialParameter<TimeDelta> tolerance;
31   FieldTrialParameter<TimeDelta> max_stall;
32 };
33 
34 // The receive time calculator serves the purpose of combining packet time
35 // stamps with a safely incremental clock. This assumes that the packet time
36 // stamps are based on lower layer timestamps that have more accurate time
37 // increments since they are based on the exact receive time. They might
38 // however, have large jumps due to clock resets in the system. To compensate
39 // this they are combined with a safe clock source that is guaranteed to be
40 // consistent, but it will not be able to measure the exact time when a packet
41 // is received.
42 class ReceiveTimeCalculator {
43  public:
44   static std::unique_ptr<ReceiveTimeCalculator> CreateFromFieldTrial();
45   ReceiveTimeCalculator();
46   int64_t ReconcileReceiveTimes(int64_t packet_time_us_,
47                                 int64_t system_time_us_,
48                                 int64_t safe_time_us_);
49 
50  private:
51   int64_t last_corrected_time_us_ = -1;
52   int64_t last_packet_time_us_ = -1;
53   int64_t last_system_time_us_ = -1;
54   int64_t last_safe_time_us_ = -1;
55   int64_t total_system_time_passed_us_ = 0;
56   int64_t static_clock_offset_us_ = 0;
57   int64_t small_reset_during_stall_ = false;
58   ReceiveTimeCalculatorConfig config_;
59 };
60 }  // namespace webrtc
61 #endif  // CALL_RECEIVE_TIME_CALCULATOR_H_
62