Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / quic / congestion_control / fix_rate_sender.cc
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/congestion_control/fix_rate_sender.h"
6
7 #include <math.h>
8
9 #include <algorithm>
10
11 #include "base/logging.h"
12 #include "net/quic/congestion_control/rtt_stats.h"
13 #include "net/quic/quic_protocol.h"
14
15 using std::max;
16
17 namespace {
18   const int kInitialBitrate = 100000;  // In bytes per second.
19   const uint64 kWindowSizeUs = 10000;  // 10 ms.
20 }
21
22 namespace net {
23
24 FixRateSender::FixRateSender(const RttStats* rtt_stats)
25     : rtt_stats_(rtt_stats),
26       bitrate_(QuicBandwidth::FromBytesPerSecond(kInitialBitrate)),
27       max_segment_size_(kDefaultMaxPacketSize),
28       fix_rate_leaky_bucket_(bitrate_),
29       paced_sender_(bitrate_, max_segment_size_),
30       latest_rtt_(QuicTime::Delta::Zero()) {
31   DVLOG(1) << "FixRateSender";
32 }
33
34 FixRateSender::~FixRateSender() {
35 }
36
37 void FixRateSender::SetFromConfig(const QuicConfig& config, bool is_server) {
38 }
39
40 void FixRateSender::OnIncomingQuicCongestionFeedbackFrame(
41     const QuicCongestionFeedbackFrame& feedback,
42     QuicTime feedback_receive_time) {
43   LOG_IF(DFATAL, feedback.type != kFixRate) <<
44       "Invalid incoming CongestionFeedbackType:" << feedback.type;
45   if (feedback.type == kFixRate) {
46     bitrate_ = feedback.fix_rate.bitrate;
47     fix_rate_leaky_bucket_.SetDrainingRate(feedback_receive_time, bitrate_);
48     paced_sender_.UpdateBandwidthEstimate(feedback_receive_time, bitrate_);
49   }
50   // Silently ignore invalid messages in release mode.
51 }
52
53 void FixRateSender::OnCongestionEvent(bool rtt_updated,
54                                       QuicByteCount bytes_in_flight,
55                                       const CongestionMap& acked_packets,
56                                       const CongestionMap& lost_packets) {
57 }
58
59 bool FixRateSender::OnPacketSent(
60     QuicTime sent_time,
61     QuicByteCount /*bytes_in_flight*/,
62     QuicPacketSequenceNumber /*sequence_number*/,
63     QuicByteCount bytes,
64     HasRetransmittableData /*has_retransmittable_data*/) {
65   fix_rate_leaky_bucket_.Add(sent_time, bytes);
66   paced_sender_.OnPacketSent(sent_time, bytes);
67
68   return true;
69 }
70
71 void FixRateSender::OnRetransmissionTimeout(bool packets_retransmitted) { }
72
73 QuicTime::Delta FixRateSender::TimeUntilSend(
74     QuicTime now,
75     QuicByteCount bytes_in_flight,
76     HasRetransmittableData /*has_retransmittable_data*/) {
77   if (CongestionWindow() > fix_rate_leaky_bucket_.BytesPending(now)) {
78     if (CongestionWindow() <= bytes_in_flight) {
79       // We need an ack before we send more.
80       return QuicTime::Delta::Infinite();
81     }
82     return paced_sender_.TimeUntilSend(now, QuicTime::Delta::Zero());
83   }
84   QuicTime::Delta time_remaining = fix_rate_leaky_bucket_.TimeRemaining(now);
85   if (time_remaining.IsZero()) {
86     // We need an ack before we send more.
87     return QuicTime::Delta::Infinite();
88   }
89   return paced_sender_.TimeUntilSend(now, time_remaining);
90 }
91
92 QuicByteCount FixRateSender::CongestionWindow() {
93   QuicByteCount window_size_bytes = bitrate_.ToBytesPerPeriod(
94       QuicTime::Delta::FromMicroseconds(kWindowSizeUs));
95   // Make sure window size is not less than a packet.
96   return max(kDefaultMaxPacketSize, window_size_bytes);
97 }
98
99 QuicBandwidth FixRateSender::BandwidthEstimate() const {
100   return bitrate_;
101 }
102
103
104 QuicTime::Delta FixRateSender::RetransmissionDelay() const {
105   // TODO(pwestin): Calculate and return retransmission delay.
106   // Use 2 * the latest RTT for now.
107   return latest_rtt_.Add(latest_rtt_);
108 }
109
110 QuicByteCount FixRateSender::GetCongestionWindow() const {
111   return 0;
112 }
113
114 }  // namespace net