| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The purpose of this file is determine what bitrate to use for mirroring. | 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 | 6 // Ideally this should be as much as possible, without causing any frames to |
| 7 // arrive late. | 7 // arrive late. |
| 8 | 8 |
| 9 // The current algorithm is to measure how much bandwidth we've been using | 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 | 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 | 11 // in a virtual "buffer" (this virtual buffer represents all the buffers between |
| 12 // the sender and the receiver, including retransmissions and so forth.) | 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 | 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. | 14 // more bandwidth than our recent usage, otherwise we use less. |
| 15 | 15 |
| 16 #include "media/cast/sender/congestion_control.h" | 16 #include "media/cast/sender/congestion_control.h" |
| 17 | 17 |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "media/cast/cast_config.h" | 19 #include "media/cast/cast_config.h" |
| 20 #include "media/cast/cast_defines.h" | 20 #include "media/cast/cast_defines.h" |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 namespace cast { | 23 namespace cast { |
| 24 | 24 |
| 25 class AdaptiveCongestionControl : public CongestionControl { | 25 class AdaptiveCongestionControl : public CongestionControl { |
| 26 public: | 26 public: |
| 27 AdaptiveCongestionControl(base::TickClock* clock, | 27 AdaptiveCongestionControl(base::TickClock* clock, |
| 28 uint32 max_bitrate_configured, | 28 uint32 max_bitrate_configured, |
| 29 uint32 min_bitrate_configured, | 29 uint32 min_bitrate_configured, |
| 30 size_t max_unacked_frames); | 30 double max_frame_rate); |
| 31 | 31 |
| 32 virtual ~AdaptiveCongestionControl() override; | 32 virtual ~AdaptiveCongestionControl() override; |
| 33 | 33 |
| 34 virtual void UpdateRtt(base::TimeDelta rtt) override; | 34 virtual void UpdateRtt(base::TimeDelta rtt) override; |
| 35 | 35 |
| 36 virtual void UpdateTargetPlayoutDelay(base::TimeDelta delay) OVERRIDE; |
| 37 |
| 36 // Called when an encoded frame is sent to the transport. | 38 // Called when an encoded frame is sent to the transport. |
| 37 virtual void SendFrameToTransport(uint32 frame_id, | 39 virtual void SendFrameToTransport(uint32 frame_id, |
| 38 size_t frame_size, | 40 size_t frame_size, |
| 39 base::TimeTicks when) override; | 41 base::TimeTicks when) override; |
| 40 | 42 |
| 41 // Called when we receive an ACK for a frame. | 43 // Called when we receive an ACK for a frame. |
| 42 virtual void AckFrame(uint32 frame_id, base::TimeTicks when) override; | 44 virtual void AckFrame(uint32 frame_id, base::TimeTicks when) override; |
| 43 | 45 |
| 44 // Returns the bitrate we should use for the next frame. | 46 // Returns the bitrate we should use for the next frame. |
| 45 virtual uint32 GetBitrate(base::TimeTicks playout_time, | 47 virtual uint32 GetBitrate(base::TimeTicks playout_time, |
| 46 base::TimeDelta playout_delay) override; | 48 base::TimeDelta playout_delay) override; |
| 47 | 49 |
| 48 private: | 50 private: |
| 49 struct FrameStats { | 51 struct FrameStats { |
| 50 FrameStats(); | 52 FrameStats(); |
| 51 // Time this frame was sent to the transport. | 53 // Time this frame was sent to the transport. |
| 52 base::TimeTicks sent_time; | 54 base::TimeTicks sent_time; |
| 53 // Time this frame was acked. | 55 // Time this frame was acked. |
| 54 base::TimeTicks ack_time; | 56 base::TimeTicks ack_time; |
| 55 // Size of encoded frame in bits. | 57 // Size of encoded frame in bits. |
| 56 size_t frame_size; | 58 size_t frame_size; |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 // Calculate how much "dead air" (idle time) there is between two frames. | 61 // Calculate how much "dead air" (idle time) there is between two frames. |
| 60 static base::TimeDelta DeadTime(const FrameStats& a, const FrameStats& b); | 62 static base::TimeDelta DeadTime(const FrameStats& a, const FrameStats& b); |
| 61 // Get the FrameStats for a given |frame_id|. | 63 // Get the FrameStats for a given |frame_id|. |
| 62 // Note: Older FrameStats will be removed automatically. | 64 // Note: Older FrameStats will be removed automatically. |
| 63 FrameStats* GetFrameStats(uint32 frame_id); | 65 FrameStats* GetFrameStats(uint32 frame_id); |
| 66 // Discard old FrameStats. |
| 67 void PruneFrameStats(); |
| 64 // Calculate a safe bitrate. This is based on how much we've been | 68 // Calculate a safe bitrate. This is based on how much we've been |
| 65 // sending in the past. | 69 // sending in the past. |
| 66 double CalculateSafeBitrate(); | 70 double CalculateSafeBitrate(); |
| 67 | 71 |
| 68 // For a given frame, calculate when it might be acked. | 72 // For a given frame, calculate when it might be acked. |
| 69 // (Or return the time it was acked, if it was.) | 73 // (Or return the time it was acked, if it was.) |
| 70 base::TimeTicks EstimatedAckTime(uint32 frame_id, double bitrate); | 74 base::TimeTicks EstimatedAckTime(uint32 frame_id, double bitrate); |
| 71 // Calculate when we start sending the data for a given frame. | 75 // Calculate when we start sending the data for a given frame. |
| 72 // This is done by calculating when we were done sending the previous | 76 // This is done by calculating when we were done sending the previous |
| 73 // frame, but obviously can't be less than |sent_time| (if known). | 77 // frame, but obviously can't be less than |sent_time| (if known). |
| 74 base::TimeTicks EstimatedSendingTime(uint32 frame_id, double bitrate); | 78 base::TimeTicks EstimatedSendingTime(uint32 frame_id, double bitrate); |
| 75 | 79 |
| 76 base::TickClock* const clock_; // Not owned by this class. | 80 base::TickClock* const clock_; // Not owned by this class. |
| 77 const uint32 max_bitrate_configured_; | 81 const uint32 max_bitrate_configured_; |
| 78 const uint32 min_bitrate_configured_; | 82 const uint32 min_bitrate_configured_; |
| 83 const double max_frame_rate_; |
| 79 std::deque<FrameStats> frame_stats_; | 84 std::deque<FrameStats> frame_stats_; |
| 80 uint32 last_frame_stats_; | 85 uint32 last_frame_stats_; |
| 81 uint32 last_acked_frame_; | 86 uint32 last_acked_frame_; |
| 82 uint32 last_encoded_frame_; | 87 uint32 last_encoded_frame_; |
| 83 base::TimeDelta rtt_; | 88 base::TimeDelta rtt_; |
| 84 size_t history_size_; | 89 size_t history_size_; |
| 85 size_t acked_bits_in_history_; | 90 size_t acked_bits_in_history_; |
| 86 base::TimeDelta dead_time_in_history_; | 91 base::TimeDelta dead_time_in_history_; |
| 87 | 92 |
| 88 DISALLOW_COPY_AND_ASSIGN(AdaptiveCongestionControl); | 93 DISALLOW_COPY_AND_ASSIGN(AdaptiveCongestionControl); |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 class FixedCongestionControl : public CongestionControl { | 96 class FixedCongestionControl : public CongestionControl { |
| 92 public: | 97 public: |
| 93 FixedCongestionControl(uint32 bitrate) : bitrate_(bitrate) {} | 98 FixedCongestionControl(uint32 bitrate) : bitrate_(bitrate) {} |
| 94 virtual ~FixedCongestionControl() override {} | 99 virtual ~FixedCongestionControl() override {} |
| 95 | 100 |
| 96 virtual void UpdateRtt(base::TimeDelta rtt) override { | 101 virtual void UpdateRtt(base::TimeDelta rtt) override { |
| 97 } | 102 } |
| 98 | 103 |
| 104 virtual void UpdateTargetPlayoutDelay(base::TimeDelta delay) OVERRIDE { |
| 105 } |
| 106 |
| 99 // Called when an encoded frame is sent to the transport. | 107 // Called when an encoded frame is sent to the transport. |
| 100 virtual void SendFrameToTransport(uint32 frame_id, | 108 virtual void SendFrameToTransport(uint32 frame_id, |
| 101 size_t frame_size, | 109 size_t frame_size, |
| 102 base::TimeTicks when) override { | 110 base::TimeTicks when) override { |
| 103 } | 111 } |
| 104 | 112 |
| 105 // Called when we receive an ACK for a frame. | 113 // Called when we receive an ACK for a frame. |
| 106 virtual void AckFrame(uint32 frame_id, base::TimeTicks when) override { | 114 virtual void AckFrame(uint32 frame_id, base::TimeTicks when) override { |
| 107 } | 115 } |
| 108 | 116 |
| 109 // Returns the bitrate we should use for the next frame. | 117 // Returns the bitrate we should use for the next frame. |
| 110 virtual uint32 GetBitrate(base::TimeTicks playout_time, | 118 virtual uint32 GetBitrate(base::TimeTicks playout_time, |
| 111 base::TimeDelta playout_delay) override { | 119 base::TimeDelta playout_delay) override { |
| 112 return bitrate_; | 120 return bitrate_; |
| 113 } | 121 } |
| 114 | 122 |
| 115 private: | 123 private: |
| 116 uint32 bitrate_; | 124 uint32 bitrate_; |
| 117 DISALLOW_COPY_AND_ASSIGN(FixedCongestionControl); | 125 DISALLOW_COPY_AND_ASSIGN(FixedCongestionControl); |
| 118 }; | 126 }; |
| 119 | 127 |
| 120 | 128 |
| 121 CongestionControl* NewAdaptiveCongestionControl( | 129 CongestionControl* NewAdaptiveCongestionControl( |
| 122 base::TickClock* clock, | 130 base::TickClock* clock, |
| 123 uint32 max_bitrate_configured, | 131 uint32 max_bitrate_configured, |
| 124 uint32 min_bitrate_configured, | 132 uint32 min_bitrate_configured, |
| 125 size_t max_unacked_frames) { | 133 double max_frame_rate) { |
| 126 return new AdaptiveCongestionControl(clock, | 134 return new AdaptiveCongestionControl(clock, |
| 127 max_bitrate_configured, | 135 max_bitrate_configured, |
| 128 min_bitrate_configured, | 136 min_bitrate_configured, |
| 129 max_unacked_frames); | 137 max_frame_rate); |
| 130 } | 138 } |
| 131 | 139 |
| 132 CongestionControl* NewFixedCongestionControl(uint32 bitrate) { | 140 CongestionControl* NewFixedCongestionControl(uint32 bitrate) { |
| 133 return new FixedCongestionControl(bitrate); | 141 return new FixedCongestionControl(bitrate); |
| 134 } | 142 } |
| 135 | 143 |
| 136 // This means that we *try* to keep our buffer 90% empty. | 144 // This means that we *try* to keep our buffer 90% empty. |
| 137 // If it is less full, we increase the bandwidth, if it is more | 145 // If it is less full, we increase the bandwidth, if it is more |
| 138 // we decrease the bandwidth. Making this smaller makes the | 146 // we decrease the bandwidth. Making this smaller makes the |
| 139 // congestion control more aggressive. | 147 // congestion control more aggressive. |
| 140 static const double kTargetEmptyBufferFraction = 0.9; | 148 static const double kTargetEmptyBufferFraction = 0.9; |
| 141 | 149 |
| 142 // This is the size of our history in frames. Larger values makes the | 150 // This is the size of our history in frames. Larger values makes the |
| 143 // congestion control adapt slower. | 151 // congestion control adapt slower. |
| 144 static const size_t kHistorySize = 100; | 152 static const size_t kHistorySize = 100; |
| 145 | 153 |
| 146 AdaptiveCongestionControl::FrameStats::FrameStats() : frame_size(0) { | 154 AdaptiveCongestionControl::FrameStats::FrameStats() : frame_size(0) { |
| 147 } | 155 } |
| 148 | 156 |
| 149 AdaptiveCongestionControl::AdaptiveCongestionControl( | 157 AdaptiveCongestionControl::AdaptiveCongestionControl( |
| 150 base::TickClock* clock, | 158 base::TickClock* clock, |
| 151 uint32 max_bitrate_configured, | 159 uint32 max_bitrate_configured, |
| 152 uint32 min_bitrate_configured, | 160 uint32 min_bitrate_configured, |
| 153 size_t max_unacked_frames) | 161 double max_frame_rate) |
| 154 : clock_(clock), | 162 : clock_(clock), |
| 155 max_bitrate_configured_(max_bitrate_configured), | 163 max_bitrate_configured_(max_bitrate_configured), |
| 156 min_bitrate_configured_(min_bitrate_configured), | 164 min_bitrate_configured_(min_bitrate_configured), |
| 165 max_frame_rate_(max_frame_rate), |
| 157 last_frame_stats_(static_cast<uint32>(-1)), | 166 last_frame_stats_(static_cast<uint32>(-1)), |
| 158 last_acked_frame_(static_cast<uint32>(-1)), | 167 last_acked_frame_(static_cast<uint32>(-1)), |
| 159 last_encoded_frame_(static_cast<uint32>(-1)), | 168 last_encoded_frame_(static_cast<uint32>(-1)), |
| 160 history_size_(max_unacked_frames + kHistorySize), | 169 history_size_(kHistorySize), |
| 161 acked_bits_in_history_(0) { | 170 acked_bits_in_history_(0) { |
| 162 DCHECK_GE(max_bitrate_configured, min_bitrate_configured) << "Invalid config"; | 171 DCHECK_GE(max_bitrate_configured, min_bitrate_configured) << "Invalid config"; |
| 163 frame_stats_.resize(2); | 172 frame_stats_.resize(2); |
| 164 base::TimeTicks now = clock->NowTicks(); | 173 base::TimeTicks now = clock->NowTicks(); |
| 165 frame_stats_[0].ack_time = now; | 174 frame_stats_[0].ack_time = now; |
| 166 frame_stats_[0].sent_time = now; | 175 frame_stats_[0].sent_time = now; |
| 167 frame_stats_[1].ack_time = now; | 176 frame_stats_[1].ack_time = now; |
| 168 DCHECK(!frame_stats_[0].ack_time.is_null()); | 177 DCHECK(!frame_stats_[0].ack_time.is_null()); |
| 169 } | 178 } |
| 170 | 179 |
| 171 CongestionControl::~CongestionControl() {} | 180 CongestionControl::~CongestionControl() {} |
| 172 AdaptiveCongestionControl::~AdaptiveCongestionControl() {} | 181 AdaptiveCongestionControl::~AdaptiveCongestionControl() {} |
| 173 | 182 |
| 174 void AdaptiveCongestionControl::UpdateRtt(base::TimeDelta rtt) { | 183 void AdaptiveCongestionControl::UpdateRtt(base::TimeDelta rtt) { |
| 175 rtt_ = (7 * rtt_ + rtt) / 8; | 184 rtt_ = (7 * rtt_ + rtt) / 8; |
| 176 } | 185 } |
| 177 | 186 |
| 187 void AdaptiveCongestionControl::UpdateTargetPlayoutDelay( |
| 188 base::TimeDelta delay) { |
| 189 const int max_unacked_frames = |
| 190 std::min(kMaxUnackedFrames, |
| 191 1 + static_cast<int>(delay * max_frame_rate_ / |
| 192 base::TimeDelta::FromSeconds(1))); |
| 193 DCHECK_GT(max_unacked_frames, 0); |
| 194 history_size_ = max_unacked_frames + kHistorySize; |
| 195 PruneFrameStats(); |
| 196 } |
| 197 |
| 178 // Calculate how much "dead air" there is between two frames. | 198 // Calculate how much "dead air" there is between two frames. |
| 179 base::TimeDelta AdaptiveCongestionControl::DeadTime(const FrameStats& a, | 199 base::TimeDelta AdaptiveCongestionControl::DeadTime(const FrameStats& a, |
| 180 const FrameStats& b) { | 200 const FrameStats& b) { |
| 181 if (b.sent_time > a.ack_time) { | 201 if (b.sent_time > a.ack_time) { |
| 182 return b.sent_time - a.ack_time; | 202 return b.sent_time - a.ack_time; |
| 183 } else { | 203 } else { |
| 184 return base::TimeDelta(); | 204 return base::TimeDelta(); |
| 185 } | 205 } |
| 186 } | 206 } |
| 187 | 207 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 198 | 218 |
| 199 AdaptiveCongestionControl::FrameStats* | 219 AdaptiveCongestionControl::FrameStats* |
| 200 AdaptiveCongestionControl::GetFrameStats(uint32 frame_id) { | 220 AdaptiveCongestionControl::GetFrameStats(uint32 frame_id) { |
| 201 int32 offset = static_cast<int32>(frame_id - last_frame_stats_); | 221 int32 offset = static_cast<int32>(frame_id - last_frame_stats_); |
| 202 DCHECK_LT(offset, static_cast<int32>(kHistorySize)); | 222 DCHECK_LT(offset, static_cast<int32>(kHistorySize)); |
| 203 if (offset > 0) { | 223 if (offset > 0) { |
| 204 frame_stats_.resize(frame_stats_.size() + offset); | 224 frame_stats_.resize(frame_stats_.size() + offset); |
| 205 last_frame_stats_ += offset; | 225 last_frame_stats_ += offset; |
| 206 offset = 0; | 226 offset = 0; |
| 207 } | 227 } |
| 208 while (frame_stats_.size() > history_size_) { | 228 PruneFrameStats(); |
| 229 offset += frame_stats_.size() - 1; |
| 230 if (offset < 0 || offset >= static_cast<int32>(frame_stats_.size())) { |
| 231 return NULL; |
| 232 } |
| 233 return &frame_stats_[offset]; |
| 234 } |
| 235 |
| 236 void AdaptiveCongestionControl::PruneFrameStats() { |
| 237 while (frame_stats_.size() > history_size_) { |
| 209 DCHECK_GT(frame_stats_.size(), 1UL); | 238 DCHECK_GT(frame_stats_.size(), 1UL); |
| 210 DCHECK(!frame_stats_[0].ack_time.is_null()); | 239 DCHECK(!frame_stats_[0].ack_time.is_null()); |
| 211 acked_bits_in_history_ -= frame_stats_[0].frame_size; | 240 acked_bits_in_history_ -= frame_stats_[0].frame_size; |
| 212 dead_time_in_history_ -= DeadTime(frame_stats_[0], frame_stats_[1]); | 241 dead_time_in_history_ -= DeadTime(frame_stats_[0], frame_stats_[1]); |
| 213 DCHECK_GE(acked_bits_in_history_, 0UL); | 242 DCHECK_GE(acked_bits_in_history_, 0UL); |
| 214 VLOG(2) << "DT: " << dead_time_in_history_.InSecondsF(); | 243 VLOG(2) << "DT: " << dead_time_in_history_.InSecondsF(); |
| 215 DCHECK_GE(dead_time_in_history_.InSecondsF(), 0.0); | 244 DCHECK_GE(dead_time_in_history_.InSecondsF(), 0.0); |
| 216 frame_stats_.pop_front(); | 245 frame_stats_.pop_front(); |
| 217 } | 246 } |
| 218 offset += frame_stats_.size() - 1; | |
| 219 if (offset < 0 || offset >= static_cast<int32>(frame_stats_.size())) { | |
| 220 return NULL; | |
| 221 } | |
| 222 return &frame_stats_[offset]; | |
| 223 } | 247 } |
| 224 | 248 |
| 225 void AdaptiveCongestionControl::AckFrame(uint32 frame_id, | 249 void AdaptiveCongestionControl::AckFrame(uint32 frame_id, |
| 226 base::TimeTicks when) { | 250 base::TimeTicks when) { |
| 227 FrameStats* frame_stats = GetFrameStats(last_acked_frame_); | 251 FrameStats* frame_stats = GetFrameStats(last_acked_frame_); |
| 228 while (IsNewerFrameId(frame_id, last_acked_frame_)) { | 252 while (IsNewerFrameId(frame_id, last_acked_frame_)) { |
| 229 FrameStats* last_frame_stats = frame_stats; | 253 FrameStats* last_frame_stats = frame_stats; |
| 230 frame_stats = GetFrameStats(last_acked_frame_ + 1); | 254 frame_stats = GetFrameStats(last_acked_frame_ + 1); |
| 231 DCHECK(frame_stats); | 255 DCHECK(frame_stats); |
| 232 if (frame_stats->sent_time.is_null()) { | 256 if (frame_stats->sent_time.is_null()) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 VLOG(3) << " FBR:" << (bits_per_second / 1E6) | 334 VLOG(3) << " FBR:" << (bits_per_second / 1E6) |
| 311 << " EBF:" << empty_buffer_fraction | 335 << " EBF:" << empty_buffer_fraction |
| 312 << " SBR:" << (safe_bitrate / 1E6); | 336 << " SBR:" << (safe_bitrate / 1E6); |
| 313 bits_per_second = std::max(bits_per_second, min_bitrate_configured_); | 337 bits_per_second = std::max(bits_per_second, min_bitrate_configured_); |
| 314 bits_per_second = std::min(bits_per_second, max_bitrate_configured_); | 338 bits_per_second = std::min(bits_per_second, max_bitrate_configured_); |
| 315 return bits_per_second; | 339 return bits_per_second; |
| 316 } | 340 } |
| 317 | 341 |
| 318 } // namespace cast | 342 } // namespace cast |
| 319 } // namespace media | 343 } // namespace media |
| OLD | NEW |