Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 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 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 17 | #include "api/field_trials_view.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 18 | #include "api/units/time_delta.h" |
| 19 | #include "rtc_base/experiments/field_trial_parser.h" |
Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
Christoffer Rodbro | 76ad154 | 2018-10-12 09:15:09 | [diff] [blame] | 23 | struct ReceiveTimeCalculatorConfig { |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 24 | explicit ReceiveTimeCalculatorConfig(const FieldTrialsView& field_trials); |
Christoffer Rodbro | 76ad154 | 2018-10-12 09:15:09 | [diff] [blame] | 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 | |
Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 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: |
Jonas Oreland | c7f691a | 2022-03-09 14:12:07 | [diff] [blame] | 45 | static std::unique_ptr<ReceiveTimeCalculator> CreateFromFieldTrial( |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 46 | const FieldTrialsView& field_trials); |
| 47 | explicit ReceiveTimeCalculator(const FieldTrialsView& field_trials); |
Christoffer Rodbro | 76ad154 | 2018-10-12 09:15:09 | [diff] [blame] | 48 | int64_t ReconcileReceiveTimes(int64_t packet_time_us_, |
| 49 | int64_t system_time_us_, |
| 50 | int64_t safe_time_us_); |
Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 51 | |
| 52 | private: |
Christoffer Rodbro | 76ad154 | 2018-10-12 09:15:09 | [diff] [blame] | 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_; |
Sebastian Jansson | b34556e | 2018-03-21 13:38:32 | [diff] [blame] | 61 | }; |
| 62 | } // namespace webrtc |
| 63 | #endif // CALL_RECEIVE_TIME_CALCULATOR_H_ |