1 // Copyright 2014 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 // The purpose of this file is determine what bitrate to use for mirroring.
6 // Ideally this should be as much as possible, without causing any frames to
9 // The current algorithm is to measure how much bandwidth we've been using
10 // recently. We also keep track of how much data has been queued up for sending
11 // in a virtual "buffer" (this virtual buffer represents all the buffers between
12 // the sender and the receiver, including retransmissions and so forth.)
13 // If we estimate that our virtual buffer is mostly empty, we try to use
14 // more bandwidth than our recent usage, otherwise we use less.
16 #include "media/cast/sender/congestion_control.h"
18 #include "base/logging.h"
19 #include "media/cast/cast_config.h"
20 #include "media/cast/cast_defines.h"
25 // This means that we *try* to keep our buffer 90% empty.
26 // If it is less full, we increase the bandwidth, if it is more
27 // we decrease the bandwidth. Making this smaller makes the
28 // congestion control more aggressive.
29 static const double kTargetEmptyBufferFraction = 0.9;
31 // This is the size of our history in frames. Larger values makes the
32 // congestion control adapt slower.
33 static const size_t kHistorySize = 100;
35 CongestionControl::FrameStats::FrameStats() : frame_size(0) {
38 CongestionControl::CongestionControl(base::TickClock* clock,
39 uint32 max_bitrate_configured,
40 uint32 min_bitrate_configured,
41 size_t max_unacked_frames)
43 max_bitrate_configured_(max_bitrate_configured),
44 min_bitrate_configured_(min_bitrate_configured),
45 last_frame_stats_(static_cast<uint32>(-1)),
46 last_acked_frame_(static_cast<uint32>(-1)),
47 last_encoded_frame_(static_cast<uint32>(-1)),
48 history_size_(max_unacked_frames + kHistorySize),
49 acked_bits_in_history_(0) {
50 DCHECK_GE(max_bitrate_configured, min_bitrate_configured) << "Invalid config";
51 frame_stats_.resize(2);
52 base::TimeTicks now = clock->NowTicks();
53 frame_stats_[0].ack_time = now;
54 frame_stats_[0].sent_time = now;
55 frame_stats_[1].ack_time = now;
56 DCHECK(!frame_stats_[0].ack_time.is_null());
59 CongestionControl::~CongestionControl() {}
61 void CongestionControl::UpdateRtt(base::TimeDelta rtt) {
62 rtt_ = (7 * rtt_ + rtt) / 8;
65 // Calculate how much "dead air" there is between two frames.
66 base::TimeDelta CongestionControl::DeadTime(const FrameStats& a,
67 const FrameStats& b) {
68 if (b.sent_time > a.ack_time) {
69 return b.sent_time - a.ack_time;
71 return base::TimeDelta();
75 double CongestionControl::CalculateSafeBitrate() {
76 double transmit_time =
77 (GetFrameStats(last_acked_frame_)->ack_time -
78 frame_stats_.front().sent_time - dead_time_in_history_).InSecondsF();
80 if (acked_bits_in_history_ == 0 || transmit_time <= 0.0) {
81 return min_bitrate_configured_;
83 return acked_bits_in_history_ / std::max(transmit_time, 1E-3);
86 CongestionControl::FrameStats* CongestionControl::GetFrameStats(
88 int32 offset = static_cast<int32>(frame_id - last_frame_stats_);
89 DCHECK_LT(offset, static_cast<int32>(kHistorySize));
91 frame_stats_.resize(frame_stats_.size() + offset);
92 last_frame_stats_ += offset;
95 while (frame_stats_.size() > history_size_) {
96 DCHECK_GT(frame_stats_.size(), 1UL);
97 DCHECK(!frame_stats_[0].ack_time.is_null());
98 acked_bits_in_history_ -= frame_stats_[0].frame_size;
99 dead_time_in_history_ -= DeadTime(frame_stats_[0], frame_stats_[1]);
100 DCHECK_GE(acked_bits_in_history_, 0UL);
101 VLOG(2) << "DT: " << dead_time_in_history_.InSecondsF();
102 DCHECK_GE(dead_time_in_history_.InSecondsF(), 0.0);
103 frame_stats_.pop_front();
105 offset += frame_stats_.size() - 1;
106 if (offset < 0 || offset >= static_cast<int32>(frame_stats_.size())) {
109 return &frame_stats_[offset];
112 void CongestionControl::AckFrame(uint32 frame_id, base::TimeTicks when) {
113 FrameStats* frame_stats = GetFrameStats(last_acked_frame_);
114 while (IsNewerFrameId(frame_id, last_acked_frame_)) {
115 FrameStats* last_frame_stats = frame_stats;
117 frame_stats = GetFrameStats(last_acked_frame_);
119 frame_stats->ack_time = when;
120 acked_bits_in_history_ += frame_stats->frame_size;
121 dead_time_in_history_ += DeadTime(*last_frame_stats, *frame_stats);
125 void CongestionControl::SendFrameToTransport(uint32 frame_id,
127 base::TimeTicks when) {
128 last_encoded_frame_ = frame_id;
129 FrameStats* frame_stats = GetFrameStats(frame_id);
131 frame_stats->frame_size = frame_size;
132 frame_stats->sent_time = when;
135 base::TimeTicks CongestionControl::EstimatedAckTime(uint32 frame_id,
137 FrameStats* frame_stats = GetFrameStats(frame_id);
139 if (frame_stats->ack_time.is_null()) {
140 DCHECK(frame_stats->frame_size) << "frame_id: " << frame_id;
141 base::TimeTicks ret = EstimatedSendingTime(frame_id, bitrate);
142 ret += base::TimeDelta::FromSecondsD(frame_stats->frame_size / bitrate);
144 base::TimeTicks now = clock_->NowTicks();
146 // This is a little counter-intuitive, but it seems to work.
147 // Basically, when we estimate that the ACK should have already happened,
148 // we figure out how long ago it should have happened and guess that the
149 // ACK will happen half of that time in the future. This will cause some
150 // over-estimation when acks are late, which is actually what we want.
151 return now + (now - ret) / 2;
156 return frame_stats->ack_time;
160 base::TimeTicks CongestionControl::EstimatedSendingTime(uint32 frame_id,
162 FrameStats* frame_stats = GetFrameStats(frame_id);
164 base::TimeTicks ret = EstimatedAckTime(frame_id - 1, bitrate) - rtt_;
165 if (frame_stats->sent_time.is_null()) {
166 // Not sent yet, but we can't start sending it in the past.
167 return std::max(ret, clock_->NowTicks());
169 return std::max(ret, frame_stats->sent_time);
173 uint32 CongestionControl::GetBitrate(base::TimeTicks playout_time,
174 base::TimeDelta playout_delay) {
175 double safe_bitrate = CalculateSafeBitrate();
176 // Estimate when we might start sending the next frame.
177 base::TimeDelta time_to_catch_up =
179 EstimatedSendingTime(last_encoded_frame_ + 1, safe_bitrate);
181 double empty_buffer_fraction =
182 time_to_catch_up.InSecondsF() / playout_delay.InSecondsF();
183 empty_buffer_fraction = std::min(empty_buffer_fraction, 1.0);
184 empty_buffer_fraction = std::max(empty_buffer_fraction, 0.0);
186 uint32 bits_per_second = static_cast<uint32>(
187 safe_bitrate * empty_buffer_fraction / kTargetEmptyBufferFraction);
188 VLOG(3) << " FBR:" << (bits_per_second / 1E6)
189 << " EBF:" << empty_buffer_fraction
190 << " SBR:" << (safe_bitrate / 1E6);
191 bits_per_second = std::max(bits_per_second, min_bitrate_configured_);
192 bits_per_second = std::min(bits_per_second, max_bitrate_configured_);
193 return bits_per_second;