Home | History | Annotate | Download | only in quic
      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"
      8 #include "base/time/time.h"
      9 
     10 namespace net {
     11 
     12 // Highest number that QuicBandwidth can hold.
     13 const int64 kQuicInfiniteBandwidth = GG_INT64_C(0x7fffffffffffffff);
     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
     44 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes,
     45                                                    QuicTime::Delta delta) {
     46   DCHECK_LT(bytes,
     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);
     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 
     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 
     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 
     98 QuicBandwidth QuicBandwidth::Scale(float scale_factor) const {
     99   return QuicBandwidth(bits_per_second_ * scale_factor);
    100 }
    101 
    102 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const {
    103   return QuicTime::Delta::FromMicroseconds(
    104       bytes * 8 * base::Time::kMicrosecondsPerSecond / bits_per_second_);
    105 }
    106 
    107 }  // namespace net
    108