[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/quic/quic_bandwidth.h" |
| 6 | |
| 7 | #include "base/logging.h" |
[email protected] | f002abb | 2013-06-28 02:30:21 | [diff] [blame] | 8 | #include "base/time/time.h" |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 9 | |
| 10 | namespace net { |
| 11 | |
| 12 | // Highest number that QuicBandwidth can hold. |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 13 | const int64 kQuicInfiniteBandwidth = GG_INT64_C(0x7fffffffffffffff); |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 14 | |
| 15 | // static |
| 16 | QuicBandwidth QuicBandwidth::Zero() { |
| 17 | return QuicBandwidth(0); |
| 18 | } |
| 19 | |
| 20 | // static |
| 21 | QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64 bits_per_second) { |
| 22 | return QuicBandwidth(bits_per_second); |
| 23 | } |
| 24 | |
| 25 | // static |
| 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); |
| 29 | } |
| 30 | |
| 31 | // static |
| 32 | QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64 bytes_per_second) { |
| 33 | DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8); |
| 34 | return QuicBandwidth(bytes_per_second * 8); |
| 35 | } |
| 36 | |
| 37 | // static |
| 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); |
| 41 | } |
| 42 | |
| 43 | // static |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 44 | QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes, |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 45 | QuicTime::Delta delta) { |
| 46 | DCHECK_LT(bytes, |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 47 | static_cast<uint64>(kQuicInfiniteBandwidth / |
| 48 | (8 * base::Time::kMicrosecondsPerSecond))); |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 49 | int64 bytes_per_second = (bytes * base::Time::kMicrosecondsPerSecond) / |
| 50 | delta.ToMicroseconds(); |
| 51 | return QuicBandwidth(bytes_per_second * 8); |
| 52 | } |
| 53 | |
| 54 | QuicBandwidth::QuicBandwidth(int64 bits_per_second) |
| 55 | : bits_per_second_(bits_per_second) { |
| 56 | DCHECK_GE(bits_per_second, 0); |
| 57 | } |
| 58 | |
| 59 | int64 QuicBandwidth::ToBitsPerSecond() const { |
| 60 | return bits_per_second_; |
| 61 | } |
| 62 | |
| 63 | int64 QuicBandwidth::ToKBitsPerSecond() const { |
| 64 | return bits_per_second_ / 1000; |
| 65 | } |
| 66 | |
| 67 | int64 QuicBandwidth::ToBytesPerSecond() const { |
| 68 | return bits_per_second_ / 8; |
| 69 | } |
| 70 | |
| 71 | int64 QuicBandwidth::ToKBytesPerSecond() const { |
| 72 | return bits_per_second_ / 8000; |
| 73 | } |
| 74 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 75 | QuicByteCount QuicBandwidth::ToBytesPerPeriod( |
| 76 | QuicTime::Delta time_period) const { |
| 77 | return ToBytesPerSecond() * time_period.ToMicroseconds() / |
| 78 | base::Time::kMicrosecondsPerSecond; |
| 79 | } |
| 80 | |
| 81 | int64 QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const { |
| 82 | return ToKBytesPerSecond() * time_period.ToMicroseconds() / |
| 83 | base::Time::kMicrosecondsPerSecond; |
| 84 | } |
| 85 | |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 86 | bool QuicBandwidth::IsZero() const { |
| 87 | return (bits_per_second_ == 0); |
| 88 | } |
| 89 | |
| 90 | QuicBandwidth QuicBandwidth::Add(const QuicBandwidth& delta) const { |
| 91 | return QuicBandwidth(bits_per_second_ + delta.bits_per_second_); |
| 92 | } |
| 93 | |
| 94 | QuicBandwidth QuicBandwidth::Subtract(const QuicBandwidth& delta) const { |
| 95 | return QuicBandwidth(bits_per_second_ - delta.bits_per_second_); |
| 96 | } |
| 97 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 98 | QuicBandwidth QuicBandwidth::Scale(float scale_factor) const { |
| 99 | return QuicBandwidth(bits_per_second_ * scale_factor); |
| 100 | } |
| 101 | |
[email protected] | 86a318d | 2013-01-23 21:16:04 | [diff] [blame] | 102 | } // namespace net |