blob: b5b4f8bd98095e42c3bf075bd2cf2f0524f12c12 [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 Chapovalov693bf1e2019-09-18 18:51:34 +020016#include "rtc_base/numerics/divide_round.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/time_utils.h"
Danil Chapovalov24929842017-11-28 10:26:54 +010018
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010019namespace webrtc {
20namespace {
Danil Chapovalov24929842017-11-28 10:26:54 +010021
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020022int64_t NtpOffsetMsCalledOnce() {
Danil Chapovalov24929842017-11-28 10:26:54 +010023 constexpr int64_t kNtpJan1970Sec = 2208988800;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020024 int64_t clock_time = rtc::TimeMillis();
25 int64_t utc_time = rtc::TimeUTCMillis();
26 return utc_time - clock_time + kNtpJan1970Sec * rtc::kNumMillisecsPerSec;
Danil Chapovalov24929842017-11-28 10:26:54 +010027}
28
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010029} // namespace
30
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020031int64_t NtpOffsetMs() {
Danil Chapovalov24929842017-11-28 10:26:54 +010032 // Calculate the offset once.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020033 static int64_t ntp_offset_ms = NtpOffsetMsCalledOnce();
34 return ntp_offset_ms;
35}
Danil Chapovalov24929842017-11-28 10:26:54 +010036
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020037NtpTime 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;
Danil Chapovalov24929842017-11-28 10:26:54 +010043 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
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010063uint32_t SaturatedUsToCompactNtp(int64_t us) {
64 constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF;
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010065 constexpr int kCompactNtpInSecond = 0x10000;
66 if (us <= 0)
67 return 0;
Danil Chapovalov24929842017-11-28 10:26:54 +010068 if (us >= kMaxCompactNtp * rtc::kNumMicrosecsPerSec / kCompactNtpInSecond)
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010069 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.
Danil Chapovalov24929842017-11-28 10:26:54 +010073 return DivideRoundToNearest(us * kCompactNtpInSecond,
74 rtc::kNumMicrosecsPerSec);
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010075}
76
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010077int64_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