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