blob: fe0cfea11fdb4124b5f99c53a658e4530225bf63 [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 {
Danil Chapovalov24929842017-11-28 10:26:54 +010020
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010021uint32_t SaturatedUsToCompactNtp(int64_t us) {
22 constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF;
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010023 constexpr int kCompactNtpInSecond = 0x10000;
24 if (us <= 0)
25 return 0;
Danil Chapovalov24929842017-11-28 10:26:54 +010026 if (us >= kMaxCompactNtp * rtc::kNumMicrosecsPerSec / kCompactNtpInSecond)
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010027 return kMaxCompactNtp;
28 // To convert to compact ntp need to divide by 1e6 to get seconds,
29 // then multiply by 0x10000 to get the final result.
30 // To avoid float operations, multiplication and division swapped.
Danil Chapovalov24929842017-11-28 10:26:54 +010031 return DivideRoundToNearest(us * kCompactNtpInSecond,
32 rtc::kNumMicrosecsPerSec);
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010033}
34
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010035int64_t CompactNtpRttToMs(uint32_t compact_ntp_interval) {
36 // Interval to convert expected to be positive, e.g. rtt or delay.
37 // Because interval can be derived from non-monotonic ntp clock,
38 // it might become negative that is indistinguishable from very large values.
39 // Since very large rtt/delay are less likely than non-monotonic ntp clock,
40 // those values consider to be negative and convert to minimum value of 1ms.
41 if (compact_ntp_interval > 0x80000000)
42 return 1;
43 // Convert to 64bit value to avoid multiplication overflow.
44 int64_t value = static_cast<int64_t>(compact_ntp_interval);
45 // To convert to milliseconds need to divide by 2^16 to get seconds,
46 // then multiply by 1000 to get milliseconds. To avoid float operations,
47 // multiplication and division swapped.
48 int64_t ms = DivideRoundToNearest(value * 1000, 1 << 16);
49 // Rtt value 0 considered too good to be true and increases to 1.
50 return std::max<int64_t>(ms, 1);
51}
52} // namespace webrtc