blob: e65329d06b78052b204053b2fd9da457160de03a [file] [log] [blame]
Danil Chapovalovc1e55c72016-03-09 15:14:35 +01001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/time_util.h"
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010012
13#include <algorithm>
14
Yves Gerey988cc082018-10-23 12:03:01 +020015#include "rtc_base/checks.h"
Danil Chapovalov24929842017-11-28 10:26:54 +010016#include "rtc_base/timeutils.h"
17
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010018namespace webrtc {
19namespace {
20// TODO(danilchap): Make generic, optimize and move to base.
21inline int64_t DivideRoundToNearest(int64_t x, uint32_t y) {
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010022 // Callers ensure x is positive and x + y / 2 doesn't overflow.
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010023 return (x + y / 2) / y;
24}
Danil Chapovalov24929842017-11-28 10:26:54 +010025
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020026int64_t NtpOffsetMsCalledOnce() {
Danil Chapovalov24929842017-11-28 10:26:54 +010027 constexpr int64_t kNtpJan1970Sec = 2208988800;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020028 int64_t clock_time = rtc::TimeMillis();
29 int64_t utc_time = rtc::TimeUTCMillis();
30 return utc_time - clock_time + kNtpJan1970Sec * rtc::kNumMillisecsPerSec;
Danil Chapovalov24929842017-11-28 10:26:54 +010031}
32
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010033} // namespace
34
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020035int64_t NtpOffsetMs() {
Danil Chapovalov24929842017-11-28 10:26:54 +010036 // Calculate the offset once.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020037 static int64_t ntp_offset_ms = NtpOffsetMsCalledOnce();
38 return ntp_offset_ms;
39}
Danil Chapovalov24929842017-11-28 10:26:54 +010040
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020041NtpTime 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 Chapovalov24929842017-11-28 10:26:54 +010047 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 Chapovalovd4fdc272017-11-09 11:34:32 +010067uint32_t SaturatedUsToCompactNtp(int64_t us) {
68 constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF;
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010069 constexpr int kCompactNtpInSecond = 0x10000;
70 if (us <= 0)
71 return 0;
Danil Chapovalov24929842017-11-28 10:26:54 +010072 if (us >= kMaxCompactNtp * rtc::kNumMicrosecsPerSec / kCompactNtpInSecond)
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010073 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 Chapovalov24929842017-11-28 10:26:54 +010077 return DivideRoundToNearest(us * kCompactNtpInSecond,
78 rtc::kNumMicrosecsPerSec);
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010079}
80
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010081int64_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