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. |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 30 | virtual void OnReceivedEstimatedBitrate(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, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 36 | int64_t rtt, |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 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 | |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 86 | BitrateControllerImpl::BitrateControllerImpl(Clock* clock, |
| 87 | bool enforce_min_bitrate) |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 88 | : clock_(clock), |
| 89 | last_bitrate_update_ms_(clock_->TimeInMilliseconds()), |
| 90 | critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 91 | bandwidth_estimation_(), |
| 92 | bitrate_observers_(), |
| 93 | enforce_min_bitrate_(enforce_min_bitrate), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 94 | reserved_bitrate_bps_(0), |
| 95 | last_bitrate_bps_(0), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 96 | last_fraction_loss_(0), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 97 | last_rtt_ms_(0), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 98 | last_enforce_min_bitrate_(!enforce_min_bitrate_), |
| 99 | bitrate_observers_modified_(false), |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 100 | last_reserved_bitrate_bps_(0), |
| 101 | remb_suppressor_(new RembSuppressor(clock)) { |
| 102 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 103 | |
| 104 | BitrateControllerImpl::~BitrateControllerImpl() { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 105 | BitrateObserverConfList::iterator it = bitrate_observers_.begin(); |
pwestin@webrtc.org | e9727cd | 2012-05-03 11:32:25 +0000 | [diff] [blame] | 106 | while (it != bitrate_observers_.end()) { |
| 107 | delete it->second; |
| 108 | bitrate_observers_.erase(it); |
| 109 | it = bitrate_observers_.begin(); |
| 110 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 111 | delete critsect_; |
| 112 | } |
| 113 | |
| 114 | RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() { |
| 115 | return new RtcpBandwidthObserverImpl(this); |
| 116 | } |
| 117 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 118 | BitrateControllerImpl::BitrateObserverConfList::iterator |
| 119 | BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver* |
| 120 | observer) { |
| 121 | BitrateObserverConfList::iterator it = bitrate_observers_.begin(); |
| 122 | for (; it != bitrate_observers_.end(); ++it) { |
| 123 | if (it->first == observer) { |
| 124 | return it; |
| 125 | } |
| 126 | } |
| 127 | return bitrate_observers_.end(); |
| 128 | } |
| 129 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 130 | void BitrateControllerImpl::SetBitrateObserver( |
| 131 | BitrateObserver* observer, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 132 | uint32_t start_bitrate, |
| 133 | uint32_t min_bitrate, |
| 134 | uint32_t max_bitrate) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 135 | CriticalSectionScoped cs(critsect_); |
| 136 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 137 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 138 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 139 | |
| 140 | if (it != bitrate_observers_.end()) { |
| 141 | // Update current configuration. |
| 142 | it->second->start_bitrate_ = start_bitrate; |
| 143 | it->second->min_bitrate_ = min_bitrate; |
| 144 | it->second->max_bitrate_ = max_bitrate; |
stefan@webrtc.org | 077593b | 2014-06-19 12:13:00 +0000 | [diff] [blame] | 145 | // Set the send-side bandwidth to the max of the sum of start bitrates and |
| 146 | // the current estimate, so that if the user wants to immediately use more |
| 147 | // bandwidth, that can be enforced. |
| 148 | uint32_t sum_start_bitrate = 0; |
| 149 | BitrateObserverConfList::iterator it; |
| 150 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 151 | ++it) { |
| 152 | sum_start_bitrate += it->second->start_bitrate_; |
| 153 | } |
| 154 | uint32_t current_estimate; |
| 155 | uint8_t loss; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 156 | int64_t rtt; |
stefan@webrtc.org | 077593b | 2014-06-19 12:13:00 +0000 | [diff] [blame] | 157 | bandwidth_estimation_.CurrentEstimate(¤t_estimate, &loss, &rtt); |
| 158 | bandwidth_estimation_.SetSendBitrate(std::max(sum_start_bitrate, |
| 159 | current_estimate)); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 160 | } else { |
| 161 | // Add new settings. |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 162 | bitrate_observers_.push_back(BitrateObserverConfiguration(observer, |
| 163 | new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate))); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 164 | bitrate_observers_modified_ = true; |
| 165 | |
| 166 | // TODO(andresp): This is a ugly way to set start bitrate. |
| 167 | // |
| 168 | // Only change start bitrate if we have exactly one observer. By definition |
| 169 | // you can only have one start bitrate, once we have our first estimate we |
| 170 | // will adapt from there. |
| 171 | if (bitrate_observers_.size() == 1) { |
| 172 | bandwidth_estimation_.SetSendBitrate(start_bitrate); |
| 173 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 174 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 175 | |
| 176 | UpdateMinMaxBitrate(); |
| 177 | } |
| 178 | |
| 179 | void BitrateControllerImpl::UpdateMinMaxBitrate() { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 180 | uint32_t sum_min_bitrate = 0; |
| 181 | uint32_t sum_max_bitrate = 0; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 182 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 183 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 184 | sum_min_bitrate += it->second->min_bitrate_; |
| 185 | sum_max_bitrate += it->second->max_bitrate_; |
| 186 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 187 | if (sum_max_bitrate == 0) { |
| 188 | // No max configured use 1Gbit/s. |
| 189 | sum_max_bitrate = 1000000000; |
| 190 | } |
| 191 | if (enforce_min_bitrate_ == false) { |
| 192 | // If not enforcing min bitrate, allow the bandwidth estimation to |
| 193 | // go as low as 10 kbps. |
| 194 | sum_min_bitrate = std::min(sum_min_bitrate, 10000u); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 195 | } |
| 196 | bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate, |
| 197 | sum_max_bitrate); |
| 198 | } |
| 199 | |
| 200 | void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) { |
| 201 | CriticalSectionScoped cs(critsect_); |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 202 | BitrateObserverConfList::iterator it = FindObserverConfigurationPair( |
| 203 | observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 204 | if (it != bitrate_observers_.end()) { |
| 205 | delete it->second; |
| 206 | bitrate_observers_.erase(it); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 207 | bitrate_observers_modified_ = true; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 +0000 | [diff] [blame] | 211 | void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) { |
| 212 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 213 | enforce_min_bitrate_ = enforce_min_bitrate; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 214 | UpdateMinMaxBitrate(); |
henrik.lundin@webrtc.org | 845862f | 2014-03-06 07:19:28 +0000 | [diff] [blame] | 215 | } |
| 216 | |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 217 | void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) { |
| 218 | CriticalSectionScoped cs(critsect_); |
| 219 | reserved_bitrate_bps_ = reserved_bitrate_bps; |
| 220 | MaybeTriggerOnNetworkChanged(); |
| 221 | } |
| 222 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 223 | void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 224 | CriticalSectionScoped cs(critsect_); |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 225 | if (remb_suppressor_->SuppresNewRemb(bitrate)) { |
| 226 | return; |
| 227 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 228 | bandwidth_estimation_.UpdateReceiverEstimate(bitrate); |
| 229 | MaybeTriggerOnNetworkChanged(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 230 | } |
| 231 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 232 | int64_t BitrateControllerImpl::TimeUntilNextProcess() { |
| 233 | const int64_t kBitrateControllerUpdateIntervalMs = 25; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 234 | CriticalSectionScoped cs(critsect_); |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 235 | int64_t time_since_update_ms = |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 236 | clock_->TimeInMilliseconds() - last_bitrate_update_ms_; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 237 | return std::max<int64_t>( |
| 238 | kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int32_t BitrateControllerImpl::Process() { |
| 242 | if (TimeUntilNextProcess() > 0) |
| 243 | return 0; |
| 244 | { |
| 245 | CriticalSectionScoped cs(critsect_); |
| 246 | bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds()); |
| 247 | MaybeTriggerOnNetworkChanged(); |
| 248 | } |
| 249 | last_bitrate_update_ms_ = clock_->TimeInMilliseconds(); |
| 250 | return 0; |
| 251 | } |
| 252 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 253 | void BitrateControllerImpl::OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 254 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 255 | int64_t rtt, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 256 | int number_of_packets, |
| 257 | int64_t now_ms) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 258 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 259 | bandwidth_estimation_.UpdateReceiverBlock( |
| 260 | fraction_loss, rtt, number_of_packets, now_ms); |
| 261 | MaybeTriggerOnNetworkChanged(); |
| 262 | } |
| 263 | |
| 264 | void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() { |
| 265 | uint32_t bitrate; |
| 266 | uint8_t fraction_loss; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 267 | int64_t rtt; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 268 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 269 | bitrate -= std::min(bitrate, reserved_bitrate_bps_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 270 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 271 | if (bitrate_observers_modified_ || |
| 272 | bitrate != last_bitrate_bps_ || |
| 273 | fraction_loss != last_fraction_loss_ || |
| 274 | rtt != last_rtt_ms_ || |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 275 | last_enforce_min_bitrate_ != enforce_min_bitrate_ || |
| 276 | last_reserved_bitrate_bps_ != reserved_bitrate_bps_) { |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 277 | last_bitrate_bps_ = bitrate; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 278 | last_fraction_loss_ = fraction_loss; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 279 | last_rtt_ms_ = rtt; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 280 | last_enforce_min_bitrate_ = enforce_min_bitrate_; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 281 | last_reserved_bitrate_bps_ = reserved_bitrate_bps_; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 282 | bitrate_observers_modified_ = false; |
| 283 | OnNetworkChanged(bitrate, fraction_loss, rtt); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 287 | void BitrateControllerImpl::OnNetworkChanged(uint32_t bitrate, |
| 288 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 289 | int64_t rtt) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 290 | // Sanity check. |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 291 | if (bitrate_observers_.empty()) |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 292 | return; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 293 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 294 | uint32_t sum_min_bitrates = 0; |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 +0000 | [diff] [blame] | 295 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 296 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 297 | sum_min_bitrates += it->second->min_bitrate_; |
| 298 | } |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 299 | if (bitrate <= sum_min_bitrates) |
| 300 | return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates); |
| 301 | else |
| 302 | return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates); |
| 303 | } |
| 304 | |
| 305 | void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate, |
| 306 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 307 | int64_t rtt, |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 308 | uint32_t sum_min_bitrates) { |
| 309 | uint32_t number_of_observers = bitrate_observers_.size(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 310 | uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) / |
| 311 | number_of_observers; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 312 | // Use map to sort list based on max bitrate. |
| 313 | ObserverSortingMap list_max_bitrates; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 314 | BitrateObserverConfList::iterator it; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 315 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) { |
| 316 | list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>( |
| 317 | it->second->max_bitrate_, |
| 318 | new ObserverConfiguration(it->first, it->second->min_bitrate_))); |
| 319 | } |
| 320 | ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); |
| 321 | while (max_it != list_max_bitrates.end()) { |
| 322 | number_of_observers--; |
| 323 | uint32_t observer_allowance = max_it->second->min_bitrate_ + |
| 324 | bitrate_per_observer; |
| 325 | if (max_it->first < observer_allowance) { |
| 326 | // We have more than enough for this observer. |
| 327 | // Carry the remainder forward. |
| 328 | uint32_t remainder = observer_allowance - max_it->first; |
| 329 | if (number_of_observers != 0) { |
| 330 | bitrate_per_observer += remainder / number_of_observers; |
| 331 | } |
| 332 | max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss, |
| 333 | rtt); |
| 334 | } else { |
| 335 | max_it->second->observer_->OnNetworkChanged(observer_allowance, |
| 336 | fraction_loss, rtt); |
| 337 | } |
| 338 | delete max_it->second; |
| 339 | list_max_bitrates.erase(max_it); |
| 340 | // Prepare next iteration. |
| 341 | max_it = list_max_bitrates.begin(); |
| 342 | } |
| 343 | } |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 344 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 345 | void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate, |
| 346 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 347 | int64_t rtt, |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 348 | uint32_t sum_min_bitrates) { |
| 349 | if (enforce_min_bitrate_) { |
| 350 | // Min bitrate to all observers. |
| 351 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 352 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 353 | ++it) { |
| 354 | it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt); |
| 355 | } |
| 356 | // Set sum of min to current send bitrate. |
| 357 | bandwidth_estimation_.SetSendBitrate(sum_min_bitrates); |
| 358 | } else { |
| 359 | // Allocate up to |min_bitrate_| to one observer at a time, until |
| 360 | // |bitrate| is depleted. |
| 361 | uint32_t remainder = bitrate; |
| 362 | BitrateControllerImpl::BitrateObserverConfList::iterator it; |
| 363 | for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 364 | ++it) { |
| 365 | uint32_t allocation = std::min(remainder, it->second->min_bitrate_); |
| 366 | it->first->OnNetworkChanged(allocation, fraction_loss, rtt); |
| 367 | remainder -= allocation; |
| 368 | } |
| 369 | // Set |bitrate| to current send bitrate. |
| 370 | bandwidth_estimation_.SetSendBitrate(bitrate); |
| 371 | } |
| 372 | } |
| 373 | |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 374 | bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const { |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 375 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 376 | uint32_t bitrate; |
| 377 | uint8_t fraction_loss; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 378 | int64_t rtt; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 379 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
| 380 | if (bitrate) { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 381 | *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | return false; |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 385 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 386 | |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 387 | void BitrateControllerImpl::SetBitrateSent(uint32_t bitrate_sent_bps) { |
| 388 | CriticalSectionScoped cs(critsect_); |
| 389 | remb_suppressor_->SetBitrateSent(bitrate_sent_bps); |
| 390 | } |
| 391 | |
| 392 | void BitrateControllerImpl::SetCodecMode(webrtc::VideoCodecMode mode) { |
| 393 | CriticalSectionScoped cs(critsect_); |
| 394 | remb_suppressor_->SetEnabled(mode == kScreensharing); |
| 395 | } |
| 396 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 397 | } // namespace webrtc |