Sebastian Jansson | b34556e | 2018-03-21 14:38:32 +0100 | [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 | |
| 13 | #include <stdint.h> |
| 14 | #include <memory> |
| 15 | |
| 16 | #include "api/optional.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | // The receive time calculator serves the purpose of combining packet time |
| 21 | // stamps with a safely incremental clock. This assumes that the packet time |
| 22 | // stamps are based on lower layer timestamps that have more accurate time |
| 23 | // increments since they are based on the exact receive time. They might |
| 24 | // however, have large jumps due to clock resets in the system. To compensate |
| 25 | // this they are combined with a safe clock source that is guaranteed to be |
| 26 | // consistent, but it will not be able to measure the exact time when a packet |
| 27 | // is received. |
| 28 | class ReceiveTimeCalculator { |
| 29 | public: |
| 30 | static std::unique_ptr<ReceiveTimeCalculator> CreateFromFieldTrial(); |
| 31 | // The min delta is used to correct for jumps backwards in time, to allow some |
| 32 | // packet reordering a small negative value is appropriate to use. The max |
| 33 | // delta difference is used as margin when detecting when packet time |
| 34 | // increases more than the safe clock. This should be larger than the largest |
| 35 | // expected sysmtem induced delay in the safe clock timestamp. |
| 36 | ReceiveTimeCalculator(int64_t min_delta_ms, int64_t max_delta_diff_ms); |
| 37 | int64_t ReconcileReceiveTimes(int64_t packet_time_us_, int64_t safe_time_us_); |
| 38 | |
| 39 | private: |
| 40 | const int64_t min_delta_us_; |
| 41 | const int64_t max_delta_diff_us_; |
| 42 | rtc::Optional<int64_t> receive_time_offset_us_; |
| 43 | int64_t last_packet_time_us_ = 0; |
| 44 | int64_t last_safe_time_us_ = 0; |
| 45 | }; |
| 46 | } // namespace webrtc |
| 47 | #endif // CALL_RECEIVE_TIME_CALCULATOR_H_ |