| /* |
| * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| * |
| * Use of this source code is governed by a BSD-style license |
| * that can be found in the LICENSE file in the root of the source |
| * tree. An additional intellectual property rights grant can be found |
| * in the file PATENTS. All contributing project authors may |
| * be found in the AUTHORS file in the root of the source tree. |
| */ |
| |
| #include "modules/congestion_controller/network_control/include/network_units.h" |
| #include <cmath> |
| |
| namespace webrtc { |
| TimeDelta TimeDelta::operator*(double scalar) const { |
| return TimeDelta::us(std::round(us() * scalar)); |
| } |
| |
| DataSize DataSize::operator*(double scalar) const { |
| return DataSize::bytes(std::round(bytes() * scalar)); |
| } |
| double TimeDelta::SecondsAsDouble() const { |
| if (IsPlusInfinity()) { |
| return std::numeric_limits<double>::infinity(); |
| } else if (IsMinusInfinity()) { |
| return -std::numeric_limits<double>::infinity(); |
| } else if (!IsInitialized()) { |
| return std::numeric_limits<double>::signaling_NaN(); |
| } else { |
| return us() * 1e-6; |
| } |
| } |
| |
| double Timestamp::SecondsAsDouble() const { |
| if (IsInfinite()) { |
| return std::numeric_limits<double>::infinity(); |
| } else if (!IsInitialized()) { |
| return std::numeric_limits<double>::signaling_NaN(); |
| } else { |
| return us() * 1e-6; |
| } |
| } |
| |
| DataRate DataRate::operator*(double scalar) const { |
| return DataRate::bytes_per_second(std::round(bytes_per_second() * scalar)); |
| } |
| |
| DataRate operator/(const DataSize& size, const TimeDelta& duration) { |
| RTC_DCHECK(size.bytes() < std::numeric_limits<int64_t>::max() / 1000000) |
| << "size is too large, size: " << size.bytes() << " is not less than " |
| << std::numeric_limits<int64_t>::max() / 1000000; |
| auto bytes_per_sec = size.bytes() * 1000000 / duration.us(); |
| return DataRate::bytes_per_second(bytes_per_sec); |
| } |
| |
| TimeDelta operator/(const DataSize& size, const DataRate& rate) { |
| RTC_DCHECK(size.bytes() < std::numeric_limits<int64_t>::max() / 1000000) |
| << "size is too large, size: " << size.bytes() << " is not less than " |
| << std::numeric_limits<int64_t>::max() / 1000000; |
| auto microseconds = size.bytes() * 1000000 / rate.bytes_per_second(); |
| return TimeDelta::us(microseconds); |
| } |
| |
| DataSize operator*(const DataRate& rate, const TimeDelta& duration) { |
| auto micro_bytes = rate.bytes_per_second() * duration.us(); |
| auto bytes = units_internal::DivideAndRound(micro_bytes, 1000000); |
| return DataSize::bytes(bytes); |
| } |
| |
| DataSize operator*(const TimeDelta& duration, const DataRate& rate) { |
| return rate * duration; |
| } |
| |
| } // namespace webrtc |