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