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