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 | |
| 21 | class RtcpBandwidthObserverImpl : public RtcpBandwidthObserver { |
| 22 | public: |
| 23 | explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner) |
| 24 | : owner_(owner) { |
| 25 | } |
| 26 | virtual ~RtcpBandwidthObserverImpl() { |
| 27 | } |
| 28 | // Received RTCP REMB or TMMBR. |
pbos@webrtc.org | 4fac8a4 | 2013-07-31 15:16:20 +0000 | [diff] [blame] | 29 | virtual void OnReceivedEstimatedBitrate(const uint32_t bitrate) OVERRIDE { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 30 | owner_->OnReceivedEstimatedBitrate(bitrate); |
| 31 | } |
| 32 | // Received RTCP receiver block. |
| 33 | virtual void OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 34 | const ReportBlockList& report_blocks, |
| 35 | uint16_t rtt, |
| 36 | int64_t now_ms) OVERRIDE { |
| 37 | if (report_blocks.empty()) |
| 38 | return; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 39 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 40 | int fraction_lost_aggregate = 0; |
| 41 | int total_number_of_packets = 0; |
| 42 | |
| 43 | // Compute the a weighted average of the fraction loss from all report |
| 44 | // blocks. |
| 45 | for (ReportBlockList::const_iterator it = report_blocks.begin(); |
| 46 | it != report_blocks.end(); ++it) { |
| 47 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 48 | ssrc_to_last_received_extended_high_seq_num_.find(it->sourceSSRC); |
| 49 | |
| 50 | int number_of_packets = 0; |
| 51 | if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) |
| 52 | number_of_packets = it->extendedHighSeqNum - |
| 53 | seq_num_it->second; |
| 54 | |
| 55 | fraction_lost_aggregate += number_of_packets * it->fractionLost; |
| 56 | total_number_of_packets += number_of_packets; |
| 57 | |
| 58 | // Update last received for this SSRC. |
| 59 | ssrc_to_last_received_extended_high_seq_num_[it->sourceSSRC] = |
| 60 | it->extendedHighSeqNum; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 61 | } |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 62 | if (total_number_of_packets == 0) |
| 63 | fraction_lost_aggregate = 0; |
| 64 | else |
| 65 | fraction_lost_aggregate = (fraction_lost_aggregate + |
| 66 | total_number_of_packets / 2) / total_number_of_packets; |
| 67 | if (fraction_lost_aggregate > 255) |
| 68 | return; |
| 69 | |
| 70 | owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt, |
| 71 | total_number_of_packets, now_ms); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 72 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 73 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 74 | private: |
| 75 | std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_; |
| 76 | BitrateControllerImpl* owner_; |
| 77 | }; |
| 78 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 79 | class LowRateStrategy { |
| 80 | public: |
| 81 | LowRateStrategy( |
| 82 | SendSideBandwidthEstimation* bandwidth_estimation, |
| 83 | BitrateControllerImpl::BitrateObserverConfList* bitrate_observers) |
| 84 | : bandwidth_estimation_(bandwidth_estimation), |
| 85 | bitrate_observers_(bitrate_observers) {} |
| 86 | |
| 87 | virtual ~LowRateStrategy() {} |
| 88 | |
| 89 | virtual void LowRateAllocation(uint32_t bitrate, |
| 90 | uint8_t fraction_loss, |
| 91 | uint32_t rtt, |
| 92 | uint32_t sum_min_bitrates) = 0; |
| 93 | |
| 94 | protected: |
| 95 | SendSideBandwidthEstimation* bandwidth_estimation_; |
| 96 | BitrateControllerImpl::BitrateObserverConfList* bitrate_observers_; |
| 97 | }; |
| 98 | |
| 99 | class EnforceMinRateStrategy : public LowRateStrategy { |
| 100 | public: |
| 101 | EnforceMinRateStrategy( |
| 102 | SendSideBandwidthEstimation* bandwidth_estimation, |
| 103 | BitrateControllerImpl::BitrateObserverConfList* bitrate_observers) |
| 104 | : LowRateStrategy(bandwidth_estimation, bitrate_observers) {} |
| 105 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 106 | void LowRateAllocation(uint32_t bitrate, |
| 107 | uint8_t fraction_loss, |
| 108 | uint32_t rtt, |
| 109 | uint32_t sum_min_bitrates) { |
| 110 | // Min bitrate to all observers. |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 111 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 112 | for (it = bitrate_observers_->begin(); it != bitrate_observers_->end(); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 113 | ++it) { |
| 114 | it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, |
| 115 | rtt); |
| 116 | } |
| 117 | // Set sum of min to current send bitrate. |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 118 | bandwidth_estimation_->SetSendBitrate(sum_min_bitrates); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 119 | } |
| 120 | }; |
| 121 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 122 | class NoEnforceMinRateStrategy : public LowRateStrategy { |
| 123 | public: |
| 124 | NoEnforceMinRateStrategy( |
| 125 | SendSideBandwidthEstimation* bandwidth_estimation, |
| 126 | BitrateControllerImpl::BitrateObserverConfList* bitrate_observers) |
| 127 | : LowRateStrategy(bandwidth_estimation, bitrate_observers) {} |
| 128 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 129 | void LowRateAllocation(uint32_t bitrate, |
| 130 | uint8_t fraction_loss, |
| 131 | uint32_t rtt, |
| 132 | uint32_t sum_min_bitrates) { |
| 133 | // Allocate up to |min_bitrate_| to one observer at a time, until |
| 134 | // |bitrate| is depleted. |
| 135 | uint32_t remainder = bitrate; |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 136 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 137 | for (it = bitrate_observers_->begin(); it != bitrate_observers_->end(); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 138 | ++it) { |
| 139 | uint32_t allocation = std::min(remainder, it->second->min_bitrate_); |
| 140 | it->first->OnNetworkChanged(allocation, fraction_loss, rtt); |
| 141 | remainder -= allocation; |
| 142 | } |
| 143 | // Set |bitrate| to current send bitrate. |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 144 | bandwidth_estimation_->SetSendBitrate(bitrate); |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | |
| 148 | BitrateController* BitrateController::CreateBitrateController( |
| 149 | bool enforce_min_bitrate) { |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 150 | return new BitrateControllerImpl(enforce_min_bitrate); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 153 | BitrateControllerImpl::BitrateControllerImpl(bool enforce_min_bitrate) |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 154 | : critsect_(CriticalSectionWrapper::CreateCriticalSection()) { |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 155 | if (enforce_min_bitrate) { |
| 156 | low_rate_strategy_.reset(new EnforceMinRateStrategy( |
| 157 | &bandwidth_estimation_, &bitrate_observers_)); |
| 158 | } else { |
| 159 | low_rate_strategy_.reset(new NoEnforceMinRateStrategy( |
| 160 | &bandwidth_estimation_, &bitrate_observers_)); |
| 161 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BitrateControllerImpl::~BitrateControllerImpl() { |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 165 | BitrateObserverConfList::iterator it = |
pwestin@webrtc.org | e9727cd | 2012-05-03 11:32:25 +0000 | [diff] [blame] | 166 | bitrate_observers_.begin(); |
| 167 | while (it != bitrate_observers_.end()) { |
| 168 | delete it->second; |
| 169 | bitrate_observers_.erase(it); |
| 170 | it = bitrate_observers_.begin(); |
| 171 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 172 | delete critsect_; |
| 173 | } |
| 174 | |
| 175 | RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() { |
| 176 | return new RtcpBandwidthObserverImpl(this); |
| 177 | } |
| 178 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 179 | BitrateControllerImpl::BitrateObserverConfList::iterator |
| 180 | BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver* |
| 181 | observer) { |
| 182 | BitrateObserverConfList::iterator it = bitrate_observers_.begin(); |
| 183 | for (; it != bitrate_observers_.end(); ++it) { |
| 184 | if (it->first == observer) { |
| 185 | return it; |
| 186 | } |
| 187 | } |
| 188 | return bitrate_observers_.end(); |
| 189 | } |
| 190 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 191 | void BitrateControllerImpl::SetBitrateObserver( |
| 192 | BitrateObserver* observer, |
| 193 | const uint32_t start_bitrate, |
| 194 | const uint32_t min_bitrate, |
| 195 | const uint32_t max_bitrate) { |
| 196 | CriticalSectionScoped cs(critsect_); |
| 197 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 198 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 199 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 200 | |
| 201 | if (it != bitrate_observers_.end()) { |
| 202 | // Update current configuration. |
| 203 | it->second->start_bitrate_ = start_bitrate; |
| 204 | it->second->min_bitrate_ = min_bitrate; |
| 205 | it->second->max_bitrate_ = max_bitrate; |
| 206 | } else { |
| 207 | // Add new settings. |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 208 | bitrate_observers_.push_back(BitrateObserverConfiguration(observer, |
| 209 | new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate))); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 210 | } |
| 211 | uint32_t sum_start_bitrate = 0; |
| 212 | uint32_t sum_min_bitrate = 0; |
| 213 | uint32_t sum_max_bitrate = 0; |
| 214 | |
| 215 | // Summarize all configurations. |
| 216 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 217 | sum_start_bitrate += it->second->start_bitrate_; |
| 218 | sum_min_bitrate += it->second->min_bitrate_; |
| 219 | sum_max_bitrate += it->second->max_bitrate_; |
| 220 | } |
| 221 | // Only change start bitrate if we have exactly one observer. By definition |
| 222 | // you can only have one start bitrate, once we have our first estimate we |
| 223 | // will adapt from there. |
| 224 | if (bitrate_observers_.size() == 1) { |
| 225 | bandwidth_estimation_.SetSendBitrate(sum_start_bitrate); |
| 226 | } |
| 227 | bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate, |
| 228 | sum_max_bitrate); |
| 229 | } |
| 230 | |
| 231 | void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) { |
| 232 | CriticalSectionScoped cs(critsect_); |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 233 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 234 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 235 | if (it != bitrate_observers_.end()) { |
| 236 | delete it->second; |
| 237 | bitrate_observers_.erase(it); |
| 238 | } |
| 239 | } |
| 240 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 241 | void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) { |
| 242 | CriticalSectionScoped cs(critsect_); |
| 243 | if (enforce_min_bitrate) { |
| 244 | low_rate_strategy_.reset(new EnforceMinRateStrategy( |
| 245 | &bandwidth_estimation_, &bitrate_observers_)); |
| 246 | } else { |
| 247 | low_rate_strategy_.reset(new NoEnforceMinRateStrategy( |
| 248 | &bandwidth_estimation_, &bitrate_observers_)); |
| 249 | } |
| 250 | } |
| 251 | |
henrik.lundin@webrtc.org | 845862f | 2014-03-06 07:19:28 +0000 | [diff] [blame^] | 252 | void BitrateControllerImpl::SetBweMinBitrate(uint32_t min_bitrate) { |
| 253 | CriticalSectionScoped cs(critsect_); |
| 254 | bandwidth_estimation_.SetMinBitrate(min_bitrate); |
| 255 | } |
| 256 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 257 | void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) { |
| 258 | uint32_t new_bitrate = 0; |
| 259 | uint8_t fraction_lost = 0; |
| 260 | uint16_t rtt = 0; |
| 261 | CriticalSectionScoped cs(critsect_); |
| 262 | if (bandwidth_estimation_.UpdateBandwidthEstimate(bitrate, |
| 263 | &new_bitrate, |
| 264 | &fraction_lost, |
| 265 | &rtt)) { |
| 266 | OnNetworkChanged(new_bitrate, fraction_lost, rtt); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void BitrateControllerImpl::OnReceivedRtcpReceiverReport( |
| 271 | const uint8_t fraction_loss, |
| 272 | const uint32_t rtt, |
| 273 | const int number_of_packets, |
| 274 | const uint32_t now_ms) { |
| 275 | uint32_t new_bitrate = 0; |
| 276 | uint8_t loss = fraction_loss; |
| 277 | CriticalSectionScoped cs(critsect_); |
| 278 | if (bandwidth_estimation_.UpdatePacketLoss(number_of_packets, rtt, now_ms, |
| 279 | &loss, &new_bitrate)) { |
| 280 | OnNetworkChanged(new_bitrate, loss, rtt); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // We have the lock here. |
| 285 | void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate, |
| 286 | const uint8_t fraction_loss, |
| 287 | const uint32_t rtt) { |
| 288 | // Sanity check. |
| 289 | uint32_t number_of_observers = bitrate_observers_.size(); |
| 290 | if (number_of_observers == 0) { |
| 291 | return; |
| 292 | } |
| 293 | uint32_t sum_min_bitrates = 0; |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +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 | sum_min_bitrates += it->second->min_bitrate_; |
| 297 | } |
| 298 | if (bitrate <= sum_min_bitrates) { |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 299 | return low_rate_strategy_->LowRateAllocation(bitrate, fraction_loss, rtt, |
| 300 | sum_min_bitrates); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 301 | } |
| 302 | uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) / |
| 303 | number_of_observers; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 304 | // Use map to sort list based on max bitrate. |
| 305 | ObserverSortingMap list_max_bitrates; |
| 306 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 307 | list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>( |
| 308 | it->second->max_bitrate_, |
| 309 | new ObserverConfiguration(it->first, it->second->min_bitrate_))); |
| 310 | } |
| 311 | ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); |
| 312 | while (max_it != list_max_bitrates.end()) { |
| 313 | number_of_observers--; |
| 314 | uint32_t observer_allowance = max_it->second->min_bitrate_ + |
| 315 | bitrate_per_observer; |
| 316 | if (max_it->first < observer_allowance) { |
| 317 | // We have more than enough for this observer. |
| 318 | // Carry the remainder forward. |
| 319 | uint32_t remainder = observer_allowance - max_it->first; |
| 320 | if (number_of_observers != 0) { |
| 321 | bitrate_per_observer += remainder / number_of_observers; |
| 322 | } |
| 323 | max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss, |
| 324 | rtt); |
| 325 | } else { |
| 326 | max_it->second->observer_->OnNetworkChanged(observer_allowance, |
| 327 | fraction_loss, rtt); |
| 328 | } |
| 329 | delete max_it->second; |
| 330 | list_max_bitrates.erase(max_it); |
| 331 | // Prepare next iteration. |
| 332 | max_it = list_max_bitrates.begin(); |
| 333 | } |
| 334 | } |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 335 | |
| 336 | bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const { |
| 337 | return bandwidth_estimation_.AvailableBandwidth(bandwidth); |
| 338 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 339 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 340 | } // namespace webrtc |