1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_bandwidth.h"
7 #include "base/logging.h"
8 #include "base/time/time.h"
12 // Highest number that QuicBandwidth can hold.
13 const int64 kQuicInfiniteBandwidth
= GG_INT64_C(0x7fffffffffffffff);
16 QuicBandwidth
QuicBandwidth::Zero() {
17 return QuicBandwidth(0);
21 QuicBandwidth
QuicBandwidth::FromBitsPerSecond(int64 bits_per_second
) {
22 return QuicBandwidth(bits_per_second
);
26 QuicBandwidth
QuicBandwidth::FromKBitsPerSecond(int64 k_bits_per_second
) {
27 DCHECK(k_bits_per_second
< kQuicInfiniteBandwidth
/ 1000);
28 return QuicBandwidth(k_bits_per_second
* 1000);
32 QuicBandwidth
QuicBandwidth::FromBytesPerSecond(int64 bytes_per_second
) {
33 DCHECK(bytes_per_second
< kQuicInfiniteBandwidth
/ 8);
34 return QuicBandwidth(bytes_per_second
* 8);
38 QuicBandwidth
QuicBandwidth::FromKBytesPerSecond(int64 k_bytes_per_second
) {
39 DCHECK(k_bytes_per_second
< kQuicInfiniteBandwidth
/ 8000);
40 return QuicBandwidth(k_bytes_per_second
* 8000);
44 QuicBandwidth
QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes
,
45 QuicTime::Delta delta
) {
47 static_cast<uint64
>(kQuicInfiniteBandwidth
/
48 (8 * base::Time::kMicrosecondsPerSecond
)));
49 int64 bytes_per_second
= (bytes
* base::Time::kMicrosecondsPerSecond
) /
50 delta
.ToMicroseconds();
51 return QuicBandwidth(bytes_per_second
* 8);
54 QuicBandwidth::QuicBandwidth(int64 bits_per_second
)
55 : bits_per_second_(bits_per_second
) {
56 DCHECK_GE(bits_per_second
, 0);
59 int64
QuicBandwidth::ToBitsPerSecond() const {
60 return bits_per_second_
;
63 int64
QuicBandwidth::ToKBitsPerSecond() const {
64 return bits_per_second_
/ 1000;
67 int64
QuicBandwidth::ToBytesPerSecond() const {
68 return bits_per_second_
/ 8;
71 int64
QuicBandwidth::ToKBytesPerSecond() const {
72 return bits_per_second_
/ 8000;
75 QuicByteCount
QuicBandwidth::ToBytesPerPeriod(
76 QuicTime::Delta time_period
) const {
77 return ToBytesPerSecond() * time_period
.ToMicroseconds() /
78 base::Time::kMicrosecondsPerSecond
;
81 int64
QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period
) const {
82 return ToKBytesPerSecond() * time_period
.ToMicroseconds() /
83 base::Time::kMicrosecondsPerSecond
;
86 bool QuicBandwidth::IsZero() const {
87 return (bits_per_second_
== 0);
90 QuicBandwidth
QuicBandwidth::Add(const QuicBandwidth
& delta
) const {
91 return QuicBandwidth(bits_per_second_
+ delta
.bits_per_second_
);
94 QuicBandwidth
QuicBandwidth::Subtract(const QuicBandwidth
& delta
) const {
95 return QuicBandwidth(bits_per_second_
- delta
.bits_per_second_
);
98 QuicBandwidth
QuicBandwidth::Scale(float scale_factor
) const {
99 return QuicBandwidth(static_cast<int64
>(bits_per_second_
* scale_factor
));
102 QuicTime::Delta
QuicBandwidth::TransferTime(QuicByteCount bytes
) const {
103 if (bits_per_second_
== 0) {
104 return QuicTime::Delta::Zero();
106 return QuicTime::Delta::FromMicroseconds(
107 bytes
* 8 * base::Time::kMicrosecondsPerSecond
/ bits_per_second_
);