Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / sender / congestion_control.cc
blobbdf38dfd97e302a8e24dc00e04ef9e44b83a176a
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
7 // arrive late.
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 <deque>
20 #include "base/logging.h"
21 #include "media/cast/cast_config.h"
22 #include "media/cast/cast_defines.h"
24 namespace media {
25 namespace cast {
27 class AdaptiveCongestionControl : public CongestionControl {
28 public:
29 AdaptiveCongestionControl(base::TickClock* clock,
30 uint32 max_bitrate_configured,
31 uint32 min_bitrate_configured,
32 double max_frame_rate);
34 ~AdaptiveCongestionControl() final;
36 void UpdateRtt(base::TimeDelta rtt) final;
38 void UpdateTargetPlayoutDelay(base::TimeDelta delay) final;
40 // Called when an encoded frame is sent to the transport.
41 void SendFrameToTransport(uint32 frame_id,
42 size_t frame_size_in_bits,
43 base::TimeTicks when) final;
45 // Called when we receive an ACK for a frame.
46 void AckFrame(uint32 frame_id, base::TimeTicks when) final;
48 // Returns the bitrate we should use for the next frame.
49 uint32 GetBitrate(base::TimeTicks playout_time,
50 base::TimeDelta playout_delay) final;
52 private:
53 struct FrameStats {
54 FrameStats();
55 // Time this frame was first enqueued for transport.
56 base::TimeTicks enqueue_time;
57 // Time this frame was acked.
58 base::TimeTicks ack_time;
59 // Size of encoded frame in bits.
60 size_t frame_size_in_bits;
63 // Calculate how much "dead air" (idle time) there is between two frames.
64 static base::TimeDelta DeadTime(const FrameStats& a, const FrameStats& b);
65 // Get the FrameStats for a given |frame_id|. Never returns nullptr.
66 // Note: Older FrameStats will be removed automatically.
67 FrameStats* GetFrameStats(uint32 frame_id);
68 // Discard old FrameStats.
69 void PruneFrameStats();
70 // Calculate a safe bitrate. This is based on how much we've been
71 // sending in the past.
72 double CalculateSafeBitrate();
74 // Estimate when the transport will start sending the data for a given frame.
75 // |estimated_bitrate| is the current estimated transmit bitrate in bits per
76 // second.
77 base::TimeTicks EstimatedSendingTime(uint32 frame_id,
78 double estimated_bitrate);
80 base::TickClock* const clock_; // Not owned by this class.
81 const uint32 max_bitrate_configured_;
82 const uint32 min_bitrate_configured_;
83 const double max_frame_rate_;
84 std::deque<FrameStats> frame_stats_;
85 uint32 last_frame_stats_;
86 uint32 last_acked_frame_;
87 uint32 last_enqueued_frame_;
88 base::TimeDelta rtt_;
89 size_t history_size_;
90 size_t acked_bits_in_history_;
91 base::TimeDelta dead_time_in_history_;
93 DISALLOW_COPY_AND_ASSIGN(AdaptiveCongestionControl);
96 class FixedCongestionControl : public CongestionControl {
97 public:
98 FixedCongestionControl(uint32 bitrate) : bitrate_(bitrate) {}
99 ~FixedCongestionControl() final {}
101 void UpdateRtt(base::TimeDelta rtt) final {}
103 void UpdateTargetPlayoutDelay(base::TimeDelta delay) final {}
105 // Called when an encoded frame is sent to the transport.
106 void SendFrameToTransport(uint32 frame_id,
107 size_t frame_size_in_bits,
108 base::TimeTicks when) final {}
110 // Called when we receive an ACK for a frame.
111 void AckFrame(uint32 frame_id, base::TimeTicks when) final {}
113 // Returns the bitrate we should use for the next frame.
114 uint32 GetBitrate(base::TimeTicks playout_time,
115 base::TimeDelta playout_delay) final {
116 return bitrate_;
119 private:
120 uint32 bitrate_;
121 DISALLOW_COPY_AND_ASSIGN(FixedCongestionControl);
125 CongestionControl* NewAdaptiveCongestionControl(
126 base::TickClock* clock,
127 uint32 max_bitrate_configured,
128 uint32 min_bitrate_configured,
129 double max_frame_rate) {
130 return new AdaptiveCongestionControl(clock,
131 max_bitrate_configured,
132 min_bitrate_configured,
133 max_frame_rate);
136 CongestionControl* NewFixedCongestionControl(uint32 bitrate) {
137 return new FixedCongestionControl(bitrate);
140 // This means that we *try* to keep our buffer 90% empty.
141 // If it is less full, we increase the bandwidth, if it is more
142 // we decrease the bandwidth. Making this smaller makes the
143 // congestion control more aggressive.
144 static const double kTargetEmptyBufferFraction = 0.9;
146 // This is the size of our history in frames. Larger values makes the
147 // congestion control adapt slower.
148 static const size_t kHistorySize = 100;
150 AdaptiveCongestionControl::FrameStats::FrameStats() : frame_size_in_bits(0) {
153 AdaptiveCongestionControl::AdaptiveCongestionControl(
154 base::TickClock* clock,
155 uint32 max_bitrate_configured,
156 uint32 min_bitrate_configured,
157 double max_frame_rate)
158 : clock_(clock),
159 max_bitrate_configured_(max_bitrate_configured),
160 min_bitrate_configured_(min_bitrate_configured),
161 max_frame_rate_(max_frame_rate),
162 last_frame_stats_(static_cast<uint32>(-1)),
163 last_acked_frame_(static_cast<uint32>(-1)),
164 last_enqueued_frame_(static_cast<uint32>(-1)),
165 history_size_(kHistorySize),
166 acked_bits_in_history_(0) {
167 DCHECK_GE(max_bitrate_configured, min_bitrate_configured) << "Invalid config";
168 frame_stats_.resize(2);
169 base::TimeTicks now = clock->NowTicks();
170 frame_stats_[0].ack_time = now;
171 frame_stats_[0].enqueue_time = now;
172 frame_stats_[1].ack_time = now;
173 DCHECK(!frame_stats_[0].ack_time.is_null());
176 CongestionControl::~CongestionControl() {}
177 AdaptiveCongestionControl::~AdaptiveCongestionControl() {}
179 void AdaptiveCongestionControl::UpdateRtt(base::TimeDelta rtt) {
180 rtt_ = (7 * rtt_ + rtt) / 8;
183 void AdaptiveCongestionControl::UpdateTargetPlayoutDelay(
184 base::TimeDelta delay) {
185 const int max_unacked_frames =
186 std::min(kMaxUnackedFrames,
187 1 + static_cast<int>(delay * max_frame_rate_ /
188 base::TimeDelta::FromSeconds(1)));
189 DCHECK_GT(max_unacked_frames, 0);
190 history_size_ = max_unacked_frames + kHistorySize;
191 PruneFrameStats();
194 // Calculate how much "dead air" there is between two frames.
195 base::TimeDelta AdaptiveCongestionControl::DeadTime(const FrameStats& a,
196 const FrameStats& b) {
197 if (b.enqueue_time > a.ack_time) {
198 return b.enqueue_time - a.ack_time;
199 } else {
200 return base::TimeDelta();
204 double AdaptiveCongestionControl::CalculateSafeBitrate() {
205 double transmit_time =
206 (GetFrameStats(last_acked_frame_)->ack_time -
207 frame_stats_.front().enqueue_time - dead_time_in_history_).InSecondsF();
209 if (acked_bits_in_history_ == 0 || transmit_time <= 0.0) {
210 return min_bitrate_configured_;
212 return acked_bits_in_history_ / std::max(transmit_time, 1E-3);
215 AdaptiveCongestionControl::FrameStats*
216 AdaptiveCongestionControl::GetFrameStats(uint32 frame_id) {
217 int32 offset = static_cast<int32>(frame_id - last_frame_stats_);
218 DCHECK_LT(offset, static_cast<int32>(kHistorySize));
219 if (offset > 0) {
220 frame_stats_.resize(frame_stats_.size() + offset);
221 last_frame_stats_ += offset;
222 offset = 0;
224 PruneFrameStats();
225 offset += frame_stats_.size() - 1;
226 // TODO(miu): Change the following to DCHECK once crash fix is confirmed.
227 // http://crbug.com/517145
228 CHECK(offset >= 0 && offset < static_cast<int32>(frame_stats_.size()));
229 return &frame_stats_[offset];
232 void AdaptiveCongestionControl::PruneFrameStats() {
233 while (frame_stats_.size() > history_size_) {
234 DCHECK_GT(frame_stats_.size(), 1UL);
235 DCHECK(!frame_stats_[0].ack_time.is_null());
236 acked_bits_in_history_ -= frame_stats_[0].frame_size_in_bits;
237 dead_time_in_history_ -= DeadTime(frame_stats_[0], frame_stats_[1]);
238 DCHECK_GE(acked_bits_in_history_, 0UL);
239 VLOG(2) << "DT: " << dead_time_in_history_.InSecondsF();
240 DCHECK_GE(dead_time_in_history_.InSecondsF(), 0.0);
241 frame_stats_.pop_front();
245 void AdaptiveCongestionControl::AckFrame(uint32 frame_id,
246 base::TimeTicks when) {
247 FrameStats* frame_stats = GetFrameStats(last_acked_frame_);
248 while (IsNewerFrameId(frame_id, last_acked_frame_)) {
249 FrameStats* last_frame_stats = frame_stats;
250 frame_stats = GetFrameStats(last_acked_frame_ + 1);
251 if (frame_stats->enqueue_time.is_null()) {
252 // Can't ack a frame that hasn't been sent yet.
253 return;
255 last_acked_frame_++;
256 if (when < frame_stats->enqueue_time)
257 when = frame_stats->enqueue_time;
259 frame_stats->ack_time = when;
260 acked_bits_in_history_ += frame_stats->frame_size_in_bits;
261 dead_time_in_history_ += DeadTime(*last_frame_stats, *frame_stats);
265 void AdaptiveCongestionControl::SendFrameToTransport(uint32 frame_id,
266 size_t frame_size_in_bits,
267 base::TimeTicks when) {
268 last_enqueued_frame_ = frame_id;
269 FrameStats* frame_stats = GetFrameStats(frame_id);
270 frame_stats->enqueue_time = when;
271 frame_stats->frame_size_in_bits = frame_size_in_bits;
274 base::TimeTicks AdaptiveCongestionControl::EstimatedSendingTime(
275 uint32 frame_id,
276 double estimated_bitrate) {
277 const base::TimeTicks now = clock_->NowTicks();
279 // Starting with the time of the latest acknowledgement, extrapolate forward
280 // to determine an estimated sending time for |frame_id|.
282 // |estimated_sending_time| will contain the estimated sending time for each
283 // frame after the last ACK'ed frame. It is possible for multiple frames to
284 // be in-flight; and therefore it is common for the |estimated_sending_time|
285 // for those frames to be before |now|.
286 base::TimeTicks estimated_sending_time;
287 for (uint32 f = last_acked_frame_; IsNewerFrameId(frame_id, f); ++f) {
288 FrameStats* const stats = GetFrameStats(f);
290 // |estimated_ack_time| is the local time when the sender receives the ACK,
291 // and not the time when the ACK left the receiver.
292 base::TimeTicks estimated_ack_time = stats->ack_time;
294 // If |estimated_ack_time| is not null, then we already have the actual ACK
295 // time, so we'll just use it. Otherwise, we need to estimate when the ACK
296 // will arrive.
297 if (estimated_ack_time.is_null()) {
298 // Model: The |estimated_sending_time| is the time at which the first byte
299 // of the encoded frame is transmitted. Then, assume the transmission of
300 // the remaining bytes is paced such that the last byte has just left the
301 // sender at |frame_transmit_time| later. This last byte then takes
302 // ~RTT/2 amount of time to travel to the receiver. Finally, the ACK from
303 // the receiver is sent and this takes another ~RTT/2 amount of time to
304 // reach the sender.
305 const base::TimeDelta frame_transmit_time =
306 base::TimeDelta::FromSecondsD(stats->frame_size_in_bits /
307 estimated_bitrate);
308 estimated_ack_time =
309 std::max(estimated_sending_time, stats->enqueue_time) +
310 frame_transmit_time + rtt_;
312 if (estimated_ack_time < now) {
313 // The current frame has not yet been ACK'ed and the yet the computed
314 // |estimated_ack_time| is before |now|. This contradiction must be
315 // resolved.
317 // The solution below is a little counter-intuitive, but it seems to
318 // work. Basically, when we estimate that the ACK should have already
319 // happened, we figure out how long ago it should have happened and
320 // guess that the ACK will happen half of that time in the future. This
321 // will cause some over-estimation when acks are late, which is actually
322 // the desired behavior.
323 estimated_ack_time = now + (now - estimated_ack_time) / 2;
327 // Since we [in the common case] do not wait for an ACK before we start
328 // sending the next frame, estimate the next frame's sending time as the
329 // time just after the last byte of the current frame left the sender (see
330 // Model comment above).
331 estimated_sending_time =
332 std::max(estimated_sending_time, estimated_ack_time - rtt_);
335 FrameStats* const frame_stats = GetFrameStats(frame_id);
336 if (frame_stats->enqueue_time.is_null()) {
337 // The frame has not yet been enqueued for transport. Since it cannot be
338 // enqueued in the past, ensure the result is lower-bounded by |now|.
339 estimated_sending_time = std::max(estimated_sending_time, now);
340 } else {
341 // |frame_stats->enqueue_time| is the time the frame was enqueued for
342 // transport. The frame may not actually start being sent until a
343 // point-in-time after that, because the transport is waiting for prior
344 // frames to be acknowledged.
345 estimated_sending_time =
346 std::max(estimated_sending_time, frame_stats->enqueue_time);
349 return estimated_sending_time;
352 uint32 AdaptiveCongestionControl::GetBitrate(base::TimeTicks playout_time,
353 base::TimeDelta playout_delay) {
354 double safe_bitrate = CalculateSafeBitrate();
355 // Estimate when we might start sending the next frame.
356 base::TimeDelta time_to_catch_up =
357 playout_time -
358 EstimatedSendingTime(last_enqueued_frame_ + 1, safe_bitrate);
360 double empty_buffer_fraction =
361 time_to_catch_up.InSecondsF() / playout_delay.InSecondsF();
362 empty_buffer_fraction = std::min(empty_buffer_fraction, 1.0);
363 empty_buffer_fraction = std::max(empty_buffer_fraction, 0.0);
365 uint32 bits_per_second = static_cast<uint32>(
366 safe_bitrate * empty_buffer_fraction / kTargetEmptyBufferFraction);
367 VLOG(3) << " FBR:" << (bits_per_second / 1E6)
368 << " EBF:" << empty_buffer_fraction
369 << " SBR:" << (safe_bitrate / 1E6);
370 bits_per_second = std::max(bits_per_second, min_bitrate_configured_);
371 bits_per_second = std::min(bits_per_second, max_bitrate_configured_);
372 return bits_per_second;
375 } // namespace cast
376 } // namespace media