pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | * |
| 10 | */ |
| 11 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 12 | #include "webrtc/modules/bitrate_controller/bitrate_controller_impl.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 13 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 14 | #include <algorithm> |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 21 | class BitrateControllerImpl::RtcpBandwidthObserverImpl |
| 22 | : public RtcpBandwidthObserver { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 23 | public: |
| 24 | explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner) |
| 25 | : owner_(owner) { |
| 26 | } |
| 27 | virtual ~RtcpBandwidthObserverImpl() { |
| 28 | } |
| 29 | // Received RTCP REMB or TMMBR. |
pbos@webrtc.org | 4fac8a4 | 2013-07-31 15:16:20 +0000 | [diff] [blame] | 30 | virtual void OnReceivedEstimatedBitrate(const uint32_t bitrate) OVERRIDE { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 31 | owner_->OnReceivedEstimatedBitrate(bitrate); |
| 32 | } |
| 33 | // Received RTCP receiver block. |
| 34 | virtual void OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 35 | const ReportBlockList& report_blocks, |
| 36 | uint16_t rtt, |
| 37 | int64_t now_ms) OVERRIDE { |
| 38 | if (report_blocks.empty()) |
| 39 | return; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 40 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 41 | int fraction_lost_aggregate = 0; |
| 42 | int total_number_of_packets = 0; |
| 43 | |
| 44 | // Compute the a weighted average of the fraction loss from all report |
| 45 | // blocks. |
| 46 | for (ReportBlockList::const_iterator it = report_blocks.begin(); |
| 47 | it != report_blocks.end(); ++it) { |
| 48 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 49 | ssrc_to_last_received_extended_high_seq_num_.find(it->sourceSSRC); |
| 50 | |
| 51 | int number_of_packets = 0; |
| 52 | if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) |
| 53 | number_of_packets = it->extendedHighSeqNum - |
| 54 | seq_num_it->second; |
| 55 | |
| 56 | fraction_lost_aggregate += number_of_packets * it->fractionLost; |
| 57 | total_number_of_packets += number_of_packets; |
| 58 | |
| 59 | // Update last received for this SSRC. |
| 60 | ssrc_to_last_received_extended_high_seq_num_[it->sourceSSRC] = |
| 61 | it->extendedHighSeqNum; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 62 | } |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 63 | if (total_number_of_packets == 0) |
| 64 | fraction_lost_aggregate = 0; |
| 65 | else |
| 66 | fraction_lost_aggregate = (fraction_lost_aggregate + |
| 67 | total_number_of_packets / 2) / total_number_of_packets; |
| 68 | if (fraction_lost_aggregate > 255) |
| 69 | return; |
| 70 | |
| 71 | owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt, |
| 72 | total_number_of_packets, now_ms); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 73 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 74 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 75 | private: |
| 76 | std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_; |
| 77 | BitrateControllerImpl* owner_; |
| 78 | }; |
| 79 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 80 | BitrateController* BitrateController::CreateBitrateController( |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 81 | Clock* clock, |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 82 | bool enforce_min_bitrate) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 83 | return new BitrateControllerImpl(clock, enforce_min_bitrate); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 86 | BitrateControllerImpl::BitrateControllerImpl(Clock* clock, bool enforce_min_bitrate) |
| 87 | : clock_(clock), |
| 88 | last_bitrate_update_ms_(clock_->TimeInMilliseconds()), |
| 89 | critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 90 | bandwidth_estimation_(), |
| 91 | bitrate_observers_(), |
| 92 | enforce_min_bitrate_(enforce_min_bitrate), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 93 | reserved_bitrate_bps_(0), |
| 94 | last_bitrate_bps_(0), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 95 | last_fraction_loss_(0), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 96 | last_rtt_ms_(0), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 97 | last_enforce_min_bitrate_(!enforce_min_bitrate_), |
| 98 | bitrate_observers_modified_(false), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 99 | last_reserved_bitrate_bps_(0) {} |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 100 | |
| 101 | BitrateControllerImpl::~BitrateControllerImpl() { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 102 | BitrateObserverConfList::iterator it = bitrate_observers_.begin(); |
pwestin@webrtc.org | e9727cd | 2012-05-03 11:32:25 +0000 | [diff] [blame] | 103 | while (it != bitrate_observers_.end()) { |
| 104 | delete it->second; |
| 105 | bitrate_observers_.erase(it); |
| 106 | it = bitrate_observers_.begin(); |
| 107 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 108 | delete critsect_; |
| 109 | } |
| 110 | |
| 111 | RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() { |
| 112 | return new RtcpBandwidthObserverImpl(this); |
| 113 | } |
| 114 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 115 | BitrateControllerImpl::BitrateObserverConfList::iterator |
| 116 | BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver* |
| 117 | observer) { |
| 118 | BitrateObserverConfList::iterator it = bitrate_observers_.begin(); |
| 119 | for (; it != bitrate_observers_.end(); ++it) { |
| 120 | if (it->first == observer) { |
| 121 | return it; |
| 122 | } |
| 123 | } |
| 124 | return bitrate_observers_.end(); |
| 125 | } |
| 126 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 127 | void BitrateControllerImpl::SetBitrateObserver( |
| 128 | BitrateObserver* observer, |
| 129 | const uint32_t start_bitrate, |
| 130 | const uint32_t min_bitrate, |
| 131 | const uint32_t max_bitrate) { |
| 132 | CriticalSectionScoped cs(critsect_); |
| 133 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 134 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 135 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 136 | |
| 137 | if (it != bitrate_observers_.end()) { |
| 138 | // Update current configuration. |
| 139 | it->second->start_bitrate_ = start_bitrate; |
| 140 | it->second->min_bitrate_ = min_bitrate; |
| 141 | it->second->max_bitrate_ = max_bitrate; |
| 142 | } else { |
| 143 | // Add new settings. |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 144 | bitrate_observers_.push_back(BitrateObserverConfiguration(observer, |
| 145 | new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate))); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 146 | bitrate_observers_modified_ = true; |
| 147 | |
| 148 | // TODO(andresp): This is a ugly way to set start bitrate. |
| 149 | // |
| 150 | // Only change start bitrate if we have exactly one observer. By definition |
| 151 | // you can only have one start bitrate, once we have our first estimate we |
| 152 | // will adapt from there. |
| 153 | if (bitrate_observers_.size() == 1) { |
| 154 | bandwidth_estimation_.SetSendBitrate(start_bitrate); |
| 155 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 156 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 157 | |
| 158 | UpdateMinMaxBitrate(); |
| 159 | } |
| 160 | |
| 161 | void BitrateControllerImpl::UpdateMinMaxBitrate() { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 162 | uint32_t sum_start_bitrate = 0; |
| 163 | uint32_t sum_min_bitrate = 0; |
| 164 | uint32_t sum_max_bitrate = 0; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 165 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 166 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 167 | sum_start_bitrate += it->second->start_bitrate_; |
| 168 | sum_min_bitrate += it->second->min_bitrate_; |
| 169 | sum_max_bitrate += it->second->max_bitrate_; |
| 170 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 171 | if (sum_max_bitrate == 0) { |
| 172 | // No max configured use 1Gbit/s. |
| 173 | sum_max_bitrate = 1000000000; |
| 174 | } |
| 175 | if (enforce_min_bitrate_ == false) { |
| 176 | // If not enforcing min bitrate, allow the bandwidth estimation to |
| 177 | // go as low as 10 kbps. |
| 178 | sum_min_bitrate = std::min(sum_min_bitrate, 10000u); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 179 | } |
| 180 | bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate, |
| 181 | sum_max_bitrate); |
| 182 | } |
| 183 | |
| 184 | void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) { |
| 185 | CriticalSectionScoped cs(critsect_); |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 186 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 187 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 188 | if (it != bitrate_observers_.end()) { |
| 189 | delete it->second; |
| 190 | bitrate_observers_.erase(it); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 191 | bitrate_observers_modified_ = true; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 195 | void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) { |
| 196 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 197 | enforce_min_bitrate_ = enforce_min_bitrate; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 198 | UpdateMinMaxBitrate(); |
henrik.lundin@webrtc.org | 845862f | 2014-03-06 07:19:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 201 | void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) { |
| 202 | CriticalSectionScoped cs(critsect_); |
| 203 | reserved_bitrate_bps_ = reserved_bitrate_bps; |
| 204 | MaybeTriggerOnNetworkChanged(); |
| 205 | } |
| 206 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 207 | void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 208 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 209 | bandwidth_estimation_.UpdateReceiverEstimate(bitrate); |
| 210 | MaybeTriggerOnNetworkChanged(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 211 | } |
| 212 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 213 | int32_t BitrateControllerImpl::TimeUntilNextProcess() { |
| 214 | enum { kBitrateControllerUpdateIntervalMs = 25 }; |
| 215 | CriticalSectionScoped cs(critsect_); |
| 216 | int time_since_update_ms = |
| 217 | clock_->TimeInMilliseconds() - last_bitrate_update_ms_; |
| 218 | return std::max(0, kBitrateControllerUpdateIntervalMs - time_since_update_ms); |
| 219 | } |
| 220 | |
| 221 | int32_t BitrateControllerImpl::Process() { |
| 222 | if (TimeUntilNextProcess() > 0) |
| 223 | return 0; |
| 224 | { |
| 225 | CriticalSectionScoped cs(critsect_); |
| 226 | bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds()); |
| 227 | MaybeTriggerOnNetworkChanged(); |
| 228 | } |
| 229 | last_bitrate_update_ms_ = clock_->TimeInMilliseconds(); |
| 230 | return 0; |
| 231 | } |
| 232 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 233 | void BitrateControllerImpl::OnReceivedRtcpReceiverReport( |
| 234 | const uint8_t fraction_loss, |
| 235 | const uint32_t rtt, |
| 236 | const int number_of_packets, |
| 237 | const uint32_t now_ms) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 238 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 239 | bandwidth_estimation_.UpdateReceiverBlock( |
| 240 | fraction_loss, rtt, number_of_packets, now_ms); |
| 241 | MaybeTriggerOnNetworkChanged(); |
| 242 | } |
| 243 | |
| 244 | void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() { |
| 245 | uint32_t bitrate; |
| 246 | uint8_t fraction_loss; |
| 247 | uint32_t rtt; |
| 248 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 249 | bitrate -= std::min(bitrate, reserved_bitrate_bps_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 250 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 251 | if (bitrate_observers_modified_ || |
| 252 | bitrate != last_bitrate_bps_ || |
| 253 | fraction_loss != last_fraction_loss_ || |
| 254 | rtt != last_rtt_ms_ || |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 255 | last_enforce_min_bitrate_ != enforce_min_bitrate_ || |
| 256 | last_reserved_bitrate_bps_ != reserved_bitrate_bps_) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 257 | last_bitrate_bps_ = bitrate; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 258 | last_fraction_loss_ = fraction_loss; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 259 | last_rtt_ms_ = rtt; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 260 | last_enforce_min_bitrate_ = enforce_min_bitrate_; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 261 | last_reserved_bitrate_bps_ = reserved_bitrate_bps_; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 262 | bitrate_observers_modified_ = false; |
| 263 | OnNetworkChanged(bitrate, fraction_loss, rtt); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 267 | void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate, |
| 268 | const uint8_t fraction_loss, |
| 269 | const uint32_t rtt) { |
| 270 | // Sanity check. |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 271 | if (bitrate_observers_.empty()) |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 272 | return; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 273 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 274 | uint32_t sum_min_bitrates = 0; |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 275 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 276 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 277 | sum_min_bitrates += it->second->min_bitrate_; |
| 278 | } |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 279 | if (bitrate <= sum_min_bitrates) |
| 280 | return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates); |
| 281 | else |
| 282 | return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates); |
| 283 | } |
| 284 | |
| 285 | void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate, |
| 286 | uint8_t fraction_loss, |
| 287 | uint32_t rtt, |
| 288 | uint32_t sum_min_bitrates) { |
| 289 | uint32_t number_of_observers = bitrate_observers_.size(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 290 | uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) / |
| 291 | number_of_observers; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 292 | // Use map to sort list based on max bitrate. |
| 293 | ObserverSortingMap list_max_bitrates; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 294 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 295 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 296 | list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>( |
| 297 | it->second->max_bitrate_, |
| 298 | new ObserverConfiguration(it->first, it->second->min_bitrate_))); |
| 299 | } |
| 300 | ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); |
| 301 | while (max_it != list_max_bitrates.end()) { |
| 302 | number_of_observers--; |
| 303 | uint32_t observer_allowance = max_it->second->min_bitrate_ + |
| 304 | bitrate_per_observer; |
| 305 | if (max_it->first < observer_allowance) { |
| 306 | // We have more than enough for this observer. |
| 307 | // Carry the remainder forward. |
| 308 | uint32_t remainder = observer_allowance - max_it->first; |
| 309 | if (number_of_observers != 0) { |
| 310 | bitrate_per_observer += remainder / number_of_observers; |
| 311 | } |
| 312 | max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss, |
| 313 | rtt); |
| 314 | } else { |
| 315 | max_it->second->observer_->OnNetworkChanged(observer_allowance, |
| 316 | fraction_loss, rtt); |
| 317 | } |
| 318 | delete max_it->second; |
| 319 | list_max_bitrates.erase(max_it); |
| 320 | // Prepare next iteration. |
| 321 | max_it = list_max_bitrates.begin(); |
| 322 | } |
| 323 | } |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 324 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 325 | void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate, |
| 326 | uint8_t fraction_loss, |
| 327 | uint32_t rtt, |
| 328 | uint32_t sum_min_bitrates) { |
| 329 | if (enforce_min_bitrate_) { |
| 330 | // Min bitrate to all observers. |
| 331 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 332 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 333 | ++it) { |
| 334 | it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt); |
| 335 | } |
| 336 | // Set sum of min to current send bitrate. |
| 337 | bandwidth_estimation_.SetSendBitrate(sum_min_bitrates); |
| 338 | } else { |
| 339 | // Allocate up to |min_bitrate_| to one observer at a time, until |
| 340 | // |bitrate| is depleted. |
| 341 | uint32_t remainder = bitrate; |
| 342 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 343 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 344 | ++it) { |
| 345 | uint32_t allocation = std::min(remainder, it->second->min_bitrate_); |
| 346 | it->first->OnNetworkChanged(allocation, fraction_loss, rtt); |
| 347 | remainder -= allocation; |
| 348 | } |
| 349 | // Set |bitrate| to current send bitrate. |
| 350 | bandwidth_estimation_.SetSendBitrate(bitrate); |
| 351 | } |
| 352 | } |
| 353 | |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 354 | bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const { |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 355 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 356 | uint32_t bitrate; |
| 357 | uint8_t fraction_loss; |
| 358 | uint32_t rtt; |
| 359 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
| 360 | if (bitrate) { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 361 | *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | return false; |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 365 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 366 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 367 | } // namespace webrtc |