blob: 6ac280a61db7e93e757a8cf7a5d2ce800ca858d5 [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
Danil Chapovalov24929842017-11-28 10:26:54 +010015#include "rtc_base/timeutils.h"
16
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010017namespace webrtc {
18namespace {
19// TODO(danilchap): Make generic, optimize and move to base.
20inline int64_t DivideRoundToNearest(int64_t x, uint32_t y) {
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010021 // Callers ensure x is positive and x + y / 2 doesn't overflow.
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010022 return (x + y / 2) / y;
23}
Danil Chapovalov24929842017-11-28 10:26:54 +010024
25int64_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 Chapovalovc1e55c72016-03-09 15:14:35 +010032} // namespace
33
Danil Chapovalov24929842017-11-28 10:26:54 +010034NtpTime 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 Chapovalovd4fdc272017-11-09 11:34:32 +010059uint32_t SaturatedUsToCompactNtp(int64_t us) {
60 constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF;
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010061 constexpr int kCompactNtpInSecond = 0x10000;
62 if (us <= 0)
63 return 0;
Danil Chapovalov24929842017-11-28 10:26:54 +010064 if (us >= kMaxCompactNtp * rtc::kNumMicrosecsPerSec / kCompactNtpInSecond)
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010065 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 Chapovalov24929842017-11-28 10:26:54 +010069 return DivideRoundToNearest(us * kCompactNtpInSecond,
70 rtc::kNumMicrosecsPerSec);
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010071}
72
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010073int64_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