aboutsummaryrefslogtreecommitdiff
path: root/modules/congestion_controller/network_control/network_units.cc
blob: f48d453cd7fb41f05c122870671792ed573da8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 *  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));
}

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;
}

::std::ostream& operator<<(::std::ostream& os, const DataRate& value) {
  if (value.IsInfinite()) {
    return os << "inf bps";
  } else if (!value.IsInitialized()) {
    return os << "? bps";
  } else {
    return os << value.bps() << " bps";
  }
}
::std::ostream& operator<<(::std::ostream& os, const DataSize& value) {
  if (value.IsInfinite()) {
    return os << "inf bytes";
  } else if (!value.IsInitialized()) {
    return os << "? bytes";
  } else {
    return os << value.bytes() << " bytes";
  }
}
::std::ostream& operator<<(::std::ostream& os, const Timestamp& value) {
  if (value.IsInfinite()) {
    return os << "inf ms";
  } else if (!value.IsInitialized()) {
    return os << "? ms";
  } else {
    return os << value.ms() << " ms";
  }
}
::std::ostream& operator<<(::std::ostream& os, const TimeDelta& value) {
  if (value.IsPlusInfinity()) {
    return os << "+inf ms";
  } else if (value.IsMinusInfinity()) {
    return os << "-inf ms";
  } else if (!value.IsInitialized()) {
    return os << "? ms";
  } else {
    return os << value.ms() << " ms";
  }
}

}  // namespace webrtc