Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/time_util.h" |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 16 | #include "rtc_base/time_utils.h" |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 17 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | // TODO(danilchap): Make generic, optimize and move to base. |
| 21 | inline int64_t DivideRoundToNearest(int64_t x, uint32_t y) { |
Danil Chapovalov | d4fdc27 | 2017-11-09 11:34:32 +0100 | [diff] [blame] | 22 | // Callers ensure x is positive and x + y / 2 doesn't overflow. |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 23 | return (x + y / 2) / y; |
| 24 | } |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 25 | |
Ilya Nikolaevskiy | 88c2c50 | 2018-10-26 16:00:08 +0200 | [diff] [blame] | 26 | int64_t NtpOffsetMsCalledOnce() { |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 27 | constexpr int64_t kNtpJan1970Sec = 2208988800; |
Ilya Nikolaevskiy | 88c2c50 | 2018-10-26 16:00:08 +0200 | [diff] [blame] | 28 | int64_t clock_time = rtc::TimeMillis(); |
| 29 | int64_t utc_time = rtc::TimeUTCMillis(); |
| 30 | return utc_time - clock_time + kNtpJan1970Sec * rtc::kNumMillisecsPerSec; |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 33 | } // namespace |
| 34 | |
Ilya Nikolaevskiy | 88c2c50 | 2018-10-26 16:00:08 +0200 | [diff] [blame] | 35 | int64_t NtpOffsetMs() { |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 36 | // Calculate the offset once. |
Ilya Nikolaevskiy | 88c2c50 | 2018-10-26 16:00:08 +0200 | [diff] [blame] | 37 | static int64_t ntp_offset_ms = NtpOffsetMsCalledOnce(); |
| 38 | return ntp_offset_ms; |
| 39 | } |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 40 | |
Ilya Nikolaevskiy | 88c2c50 | 2018-10-26 16:00:08 +0200 | [diff] [blame] | 41 | NtpTime TimeMicrosToNtp(int64_t time_us) { |
| 42 | // Since this doesn't return a wallclock time, but only NTP representation |
| 43 | // of rtc::TimeMillis() clock, the exact offset doesn't matter. |
| 44 | // To simplify conversions between NTP and RTP time, this offset is |
| 45 | // limited to milliseconds in resolution. |
| 46 | int64_t time_ntp_us = time_us + NtpOffsetMs() * 1000; |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 47 | RTC_DCHECK_GE(time_ntp_us, 0); // Time before year 1900 is unsupported. |
| 48 | |
| 49 | // TODO(danilchap): Convert both seconds and fraction together using int128 |
| 50 | // when that type is easily available. |
| 51 | // Currently conversion is done separetly for seconds and fraction of a second |
| 52 | // to avoid overflow. |
| 53 | |
| 54 | // Convert seconds to uint32 through uint64 for well-defined cast. |
| 55 | // Wrap around (will happen in 2036) is expected for ntp time. |
| 56 | uint32_t ntp_seconds = |
| 57 | static_cast<uint64_t>(time_ntp_us / rtc::kNumMicrosecsPerSec); |
| 58 | |
| 59 | // Scale fractions of the second to ntp resolution. |
| 60 | constexpr int64_t kNtpInSecond = 1LL << 32; |
| 61 | int64_t us_fractions = time_ntp_us % rtc::kNumMicrosecsPerSec; |
| 62 | uint32_t ntp_fractions = |
| 63 | us_fractions * kNtpInSecond / rtc::kNumMicrosecsPerSec; |
| 64 | return NtpTime(ntp_seconds, ntp_fractions); |
| 65 | } |
| 66 | |
Danil Chapovalov | d4fdc27 | 2017-11-09 11:34:32 +0100 | [diff] [blame] | 67 | uint32_t SaturatedUsToCompactNtp(int64_t us) { |
| 68 | constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF; |
Danil Chapovalov | d4fdc27 | 2017-11-09 11:34:32 +0100 | [diff] [blame] | 69 | constexpr int kCompactNtpInSecond = 0x10000; |
| 70 | if (us <= 0) |
| 71 | return 0; |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 72 | if (us >= kMaxCompactNtp * rtc::kNumMicrosecsPerSec / kCompactNtpInSecond) |
Danil Chapovalov | d4fdc27 | 2017-11-09 11:34:32 +0100 | [diff] [blame] | 73 | return kMaxCompactNtp; |
| 74 | // To convert to compact ntp need to divide by 1e6 to get seconds, |
| 75 | // then multiply by 0x10000 to get the final result. |
| 76 | // To avoid float operations, multiplication and division swapped. |
Danil Chapovalov | 2492984 | 2017-11-28 10:26:54 +0100 | [diff] [blame] | 77 | return DivideRoundToNearest(us * kCompactNtpInSecond, |
| 78 | rtc::kNumMicrosecsPerSec); |
Danil Chapovalov | d4fdc27 | 2017-11-09 11:34:32 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 81 | int64_t CompactNtpRttToMs(uint32_t compact_ntp_interval) { |
| 82 | // Interval to convert expected to be positive, e.g. rtt or delay. |
| 83 | // Because interval can be derived from non-monotonic ntp clock, |
| 84 | // it might become negative that is indistinguishable from very large values. |
| 85 | // Since very large rtt/delay are less likely than non-monotonic ntp clock, |
| 86 | // those values consider to be negative and convert to minimum value of 1ms. |
| 87 | if (compact_ntp_interval > 0x80000000) |
| 88 | return 1; |
| 89 | // Convert to 64bit value to avoid multiplication overflow. |
| 90 | int64_t value = static_cast<int64_t>(compact_ntp_interval); |
| 91 | // To convert to milliseconds need to divide by 2^16 to get seconds, |
| 92 | // then multiply by 1000 to get milliseconds. To avoid float operations, |
| 93 | // multiplication and division swapped. |
| 94 | int64_t ms = DivideRoundToNearest(value * 1000, 1 << 16); |
| 95 | // Rtt value 0 considered too good to be true and increases to 1. |
| 96 | return std::max<int64_t>(ms, 1); |
| 97 | } |
| 98 | } // namespace webrtc |