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