Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 * | 9 * |
| 10 */ | 10 */ |
| 11 | 11 |
| 12 #include "webrtc/call/bitrate_allocator.h" | 12 #include "webrtc/call/bitrate_allocator.h" |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 17 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 18 #include "webrtc/rtc_base/checks.h" | 18 #include "webrtc/rtc_base/checks.h" |
| 19 #include "webrtc/rtc_base/helpers.h" | |
| 19 #include "webrtc/rtc_base/logging.h" | 20 #include "webrtc/rtc_base/logging.h" |
| 20 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
| 21 #include "webrtc/system_wrappers/include/metrics.h" | 22 #include "webrtc/system_wrappers/include/metrics.h" |
| 22 | 23 |
| 23 namespace webrtc { | 24 namespace webrtc { |
| 24 | 25 |
| 25 // Allow packets to be transmitted in up to 2 times max video bitrate if the | 26 // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 26 // bandwidth estimate allows it. | 27 // bandwidth estimate allows it. |
| 27 const int kTransmissionMaxBitrateMultiplier = 2; | 28 const int kTransmissionMaxBitrateMultiplier = 2; |
| 28 const int kDefaultBitrateBps = 300000; | 29 const int kDefaultBitrateBps = 300000; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 49 : limit_observer_(limit_observer), | 50 : limit_observer_(limit_observer), |
| 50 bitrate_observer_configs_(), | 51 bitrate_observer_configs_(), |
| 51 last_bitrate_bps_(0), | 52 last_bitrate_bps_(0), |
| 52 last_non_zero_bitrate_bps_(kDefaultBitrateBps), | 53 last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
| 53 last_fraction_loss_(0), | 54 last_fraction_loss_(0), |
| 54 last_rtt_(0), | 55 last_rtt_(0), |
| 55 num_pause_events_(0), | 56 num_pause_events_(0), |
| 56 clock_(Clock::GetRealTimeClock()), | 57 clock_(Clock::GetRealTimeClock()), |
| 57 last_bwe_log_time_(0), | 58 last_bwe_log_time_(0), |
| 58 total_requested_padding_bitrate_(0), | 59 total_requested_padding_bitrate_(0), |
| 59 total_requested_min_bitrate_(0) { | 60 total_requested_min_bitrate_(0), |
| 61 bitrate_allocation_strategy_(nullptr) { | |
| 60 sequenced_checker_.Detach(); | 62 sequenced_checker_.Detach(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 BitrateAllocator::~BitrateAllocator() { | 65 BitrateAllocator::~BitrateAllocator() { |
| 64 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", | 66 RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", |
| 65 num_pause_events_); | 67 num_pause_events_); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, | 70 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
| 69 uint8_t fraction_loss, | 71 uint8_t fraction_loss, |
| 70 int64_t rtt, | 72 int64_t rtt, |
| 71 int64_t bwe_period_ms) { | 73 int64_t bwe_period_ms) { |
| 72 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 74 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 73 last_bitrate_bps_ = target_bitrate_bps; | 75 last_bitrate_bps_ = target_bitrate_bps; |
| 74 last_non_zero_bitrate_bps_ = | 76 last_non_zero_bitrate_bps_ = |
| 75 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; | 77 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
| 76 last_fraction_loss_ = fraction_loss; | 78 last_fraction_loss_ = fraction_loss; |
| 77 last_rtt_ = rtt; | 79 last_rtt_ = rtt; |
| 78 last_bwe_period_ms_ = bwe_period_ms; | 80 last_bwe_period_ms_ = bwe_period_ms; |
| 79 | 81 |
| 80 // Periodically log the incoming BWE. | 82 // Periodically log the incoming BWE. |
| 81 int64_t now = clock_->TimeInMilliseconds(); | 83 int64_t now = clock_->TimeInMilliseconds(); |
| 82 if (now > last_bwe_log_time_ + kBweLogIntervalMs) { | 84 if (now > last_bwe_log_time_ + kBweLogIntervalMs) { |
| 83 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; | 85 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; |
| 84 last_bwe_log_time_ = now; | 86 last_bwe_log_time_ = now; |
| 85 } | 87 } |
| 86 | 88 |
| 87 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); | 89 ObserverAllocation allocation = AllocateBitrates(target_bitrate_bps); |
| 88 | 90 |
| 89 for (auto& config : bitrate_observer_configs_) { | 91 for (const auto& track_config : bitrate_observer_configs_) { |
| 90 uint32_t allocated_bitrate = allocation[config.observer]; | 92 ObserverConfig* config = static_cast<ObserverConfig*>(track_config.get()); |
|
nisse-webrtc
2017/09/05 11:03:46
It not so nice to have these static casts everywhe
alexnarest
2017/09/05 13:37:45
TrackConfig is visible at BitrateAllocationStrateg
kwiberg-webrtc
2017/09/06 08:49:46
+1 to what nisse@ said about downcasting. It's rea
nisse-webrtc
2017/09/06 09:04:02
And we'd then need to change the signature of the
alexnarest
2017/09/08 17:09:23
I'm not fully agree with the tradeoff between perf
| |
| 91 uint32_t protection_bitrate = config.observer->OnBitrateUpdated( | 93 uint32_t allocated_bitrate = allocation[config->observer]; |
| 92 allocated_bitrate, last_fraction_loss_, last_rtt_, | 94 uint32_t protection_bitrate = config->observer->OnBitrateUpdated( |
| 93 last_bwe_period_ms_); | 95 allocated_bitrate, last_fraction_loss_, last_rtt_, last_bwe_period_ms_); |
| 94 | 96 |
| 95 if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) { | 97 if (allocated_bitrate == 0 && config->allocated_bitrate_bps > 0) { |
| 96 if (target_bitrate_bps > 0) | 98 if (target_bitrate_bps > 0) |
| 97 ++num_pause_events_; | 99 ++num_pause_events_; |
| 98 // The protection bitrate is an estimate based on the ratio between media | 100 // The protection bitrate is an estimate based on the ratio between media |
| 99 // and protection used before this observer was muted. | 101 // and protection used before this observer was muted. |
| 100 uint32_t predicted_protection_bps = | 102 uint32_t predicted_protection_bps = |
| 101 (1.0 - config.media_ratio) * config.min_bitrate_bps; | 103 (1.0 - config->media_ratio) * config->min_bitrate_bps; |
| 102 LOG(LS_INFO) << "Pausing observer " << config.observer | 104 LOG(LS_INFO) << "Pausing observer " << config->observer |
| 103 << " with configured min bitrate " << config.min_bitrate_bps | 105 << " with configured min bitrate " << config->min_bitrate_bps |
| 104 << " and current estimate of " << target_bitrate_bps | 106 << " and current estimate of " << target_bitrate_bps |
| 105 << " and protection bitrate " << predicted_protection_bps; | 107 << " and protection bitrate " << predicted_protection_bps; |
| 106 } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) { | 108 } else if (allocated_bitrate > 0 && config->allocated_bitrate_bps == 0) { |
| 107 if (target_bitrate_bps > 0) | 109 if (target_bitrate_bps > 0) |
| 108 ++num_pause_events_; | 110 ++num_pause_events_; |
| 109 LOG(LS_INFO) << "Resuming observer " << config.observer | 111 LOG(LS_INFO) << "Resuming observer " << config->observer |
| 110 << ", configured min bitrate " << config.min_bitrate_bps | 112 << ", configured min bitrate " << config->min_bitrate_bps |
| 111 << ", current allocation " << allocated_bitrate | 113 << ", current allocation " << allocated_bitrate |
| 112 << " and protection bitrate " << protection_bitrate; | 114 << " and protection bitrate " << protection_bitrate; |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Only update the media ratio if the observer got an allocation. | 117 // Only update the media ratio if the observer got an allocation. |
| 116 if (allocated_bitrate > 0) | 118 if (allocated_bitrate > 0) |
| 117 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); | 119 config->media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 118 config.allocated_bitrate_bps = allocated_bitrate; | 120 config->allocated_bitrate_bps = allocated_bitrate; |
| 119 } | 121 } |
| 120 UpdateAllocationLimits(); | 122 UpdateAllocationLimits(); |
| 121 } | 123 } |
| 122 | 124 |
| 123 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, | 125 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
| 124 uint32_t min_bitrate_bps, | 126 uint32_t min_bitrate_bps, |
| 125 uint32_t max_bitrate_bps, | 127 uint32_t max_bitrate_bps, |
| 126 uint32_t pad_up_bitrate_bps, | 128 uint32_t pad_up_bitrate_bps, |
| 127 bool enforce_min_bitrate, | 129 bool enforce_min_bitrate, |
| 128 std::string track_id) { | 130 std::string track_id) { |
| 129 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 131 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 130 auto it = FindObserverConfig(observer); | 132 auto it = FindObserverConfig(observer); |
| 131 | 133 |
| 132 // Update settings if the observer already exists, create a new one otherwise. | 134 // Update settings if the observer already exists, create a new one otherwise. |
| 133 if (it != bitrate_observer_configs_.end()) { | 135 if (it != bitrate_observer_configs_.end()) { |
| 134 it->min_bitrate_bps = min_bitrate_bps; | 136 ObserverConfig* observer_config = static_cast<ObserverConfig*>(it->get()); |
| 135 it->max_bitrate_bps = max_bitrate_bps; | 137 observer_config->min_bitrate_bps = min_bitrate_bps; |
| 136 it->pad_up_bitrate_bps = pad_up_bitrate_bps; | 138 observer_config->max_bitrate_bps = max_bitrate_bps; |
| 137 it->enforce_min_bitrate = enforce_min_bitrate; | 139 observer_config->pad_up_bitrate_bps = pad_up_bitrate_bps; |
| 140 observer_config->enforce_min_bitrate = enforce_min_bitrate; | |
| 141 observer_config->track_id = track_id; | |
| 138 } else { | 142 } else { |
| 139 bitrate_observer_configs_.push_back( | 143 bitrate_observer_configs_.emplace_back( |
| 140 ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps, | 144 new ObserverConfig(observer, min_bitrate_bps, max_bitrate_bps, |
| 141 pad_up_bitrate_bps, enforce_min_bitrate, track_id)); | 145 pad_up_bitrate_bps, enforce_min_bitrate, track_id)); |
| 142 } | 146 } |
| 143 | 147 |
| 144 ObserverAllocation allocation; | 148 ObserverAllocation allocation; |
| 145 if (last_bitrate_bps_ > 0) { | 149 if (last_bitrate_bps_ > 0) { |
| 146 // Calculate a new allocation and update all observers. | 150 // Calculate a new allocation and update all observers. |
| 147 allocation = AllocateBitrates(last_bitrate_bps_); | 151 allocation = AllocateBitrates(last_bitrate_bps_); |
| 148 for (auto& config : bitrate_observer_configs_) { | 152 for (auto& track_config : bitrate_observer_configs_) { |
| 149 uint32_t allocated_bitrate = allocation[config.observer]; | 153 ObserverConfig* config = static_cast<ObserverConfig*>(track_config.get()); |
| 150 uint32_t protection_bitrate = config.observer->OnBitrateUpdated( | 154 uint32_t allocated_bitrate = allocation[config->observer]; |
| 155 uint32_t protection_bitrate = config->observer->OnBitrateUpdated( | |
| 151 allocated_bitrate, last_fraction_loss_, last_rtt_, | 156 allocated_bitrate, last_fraction_loss_, last_rtt_, |
| 152 last_bwe_period_ms_); | 157 last_bwe_period_ms_); |
| 153 config.allocated_bitrate_bps = allocated_bitrate; | 158 config->allocated_bitrate_bps = allocated_bitrate; |
| 154 if (allocated_bitrate > 0) | 159 if (allocated_bitrate > 0) |
| 155 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); | 160 config->media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
| 156 } | 161 } |
| 157 } else { | 162 } else { |
| 158 // Currently, an encoder is not allowed to produce frames. | 163 // Currently, an encoder is not allowed to produce frames. |
| 159 // But we still have to return the initial config bitrate + let the | 164 // But we still have to return the initial config bitrate + let the |
| 160 // observer know that it can not produce frames. | 165 // observer know that it can not produce frames. |
| 161 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); | 166 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
| 162 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_, | 167 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_, |
| 163 last_bwe_period_ms_); | 168 last_bwe_period_ms_); |
| 164 } | 169 } |
| 165 UpdateAllocationLimits(); | 170 UpdateAllocationLimits(); |
| 166 } | 171 } |
| 167 | 172 |
| 168 void BitrateAllocator::UpdateAllocationLimits() { | 173 void BitrateAllocator::UpdateAllocationLimits() { |
| 169 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 174 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 170 uint32_t total_requested_padding_bitrate = 0; | 175 uint32_t total_requested_padding_bitrate = 0; |
| 171 uint32_t total_requested_min_bitrate = 0; | 176 uint32_t total_requested_min_bitrate = 0; |
| 172 | 177 |
| 173 for (const auto& config : bitrate_observer_configs_) { | 178 for (const auto& track_config : bitrate_observer_configs_) { |
| 174 uint32_t stream_padding = config.pad_up_bitrate_bps; | 179 ObserverConfig* config = static_cast<ObserverConfig*>(track_config.get()); |
| 175 if (config.enforce_min_bitrate) { | 180 uint32_t stream_padding = config->pad_up_bitrate_bps; |
| 176 total_requested_min_bitrate += config.min_bitrate_bps; | 181 if (config->enforce_min_bitrate) { |
| 177 } else if (config.allocated_bitrate_bps == 0) { | 182 total_requested_min_bitrate += config->min_bitrate_bps; |
| 183 } else if (config->allocated_bitrate_bps == 0) { | |
| 178 stream_padding = | 184 stream_padding = |
| 179 std::max(MinBitrateWithHysteresis(config), stream_padding); | 185 std::max(MinBitrateWithHysteresis(*config), stream_padding); |
| 180 } | 186 } |
| 181 total_requested_padding_bitrate += stream_padding; | 187 total_requested_padding_bitrate += stream_padding; |
| 182 } | 188 } |
| 183 | 189 |
| 184 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ && | 190 if (total_requested_padding_bitrate == total_requested_padding_bitrate_ && |
| 185 total_requested_min_bitrate == total_requested_min_bitrate_) { | 191 total_requested_min_bitrate == total_requested_min_bitrate_) { |
| 186 return; | 192 return; |
| 187 } | 193 } |
| 188 | 194 |
| 189 total_requested_min_bitrate_ = total_requested_min_bitrate; | 195 total_requested_min_bitrate_ = total_requested_min_bitrate; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 203 if (it != bitrate_observer_configs_.end()) { | 209 if (it != bitrate_observer_configs_.end()) { |
| 204 bitrate_observer_configs_.erase(it); | 210 bitrate_observer_configs_.erase(it); |
| 205 } | 211 } |
| 206 | 212 |
| 207 UpdateAllocationLimits(); | 213 UpdateAllocationLimits(); |
| 208 } | 214 } |
| 209 | 215 |
| 210 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { | 216 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { |
| 211 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 217 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 212 const auto& it = FindObserverConfig(observer); | 218 const auto& it = FindObserverConfig(observer); |
| 219 ObserverConfig* config = static_cast<ObserverConfig*>(it->get()); | |
| 213 if (it == bitrate_observer_configs_.end()) { | 220 if (it == bitrate_observer_configs_.end()) { |
| 214 // This observer hasn't been added yet, just give it its fair share. | 221 // This observer hasn't been added yet, just give it its fair share. |
| 215 return last_non_zero_bitrate_bps_ / | 222 return last_non_zero_bitrate_bps_ / |
| 216 static_cast<int>((bitrate_observer_configs_.size() + 1)); | 223 static_cast<int>((bitrate_observer_configs_.size() + 1)); |
| 217 } else if (it->allocated_bitrate_bps == -1) { | 224 } else if (config->allocated_bitrate_bps == -1) { |
| 218 // This observer hasn't received an allocation yet, so do the same. | 225 // This observer hasn't received an allocation yet, so do the same. |
| 219 return last_non_zero_bitrate_bps_ / | 226 return last_non_zero_bitrate_bps_ / |
| 220 static_cast<int>(bitrate_observer_configs_.size()); | 227 static_cast<int>(bitrate_observer_configs_.size()); |
| 221 } else { | 228 } else { |
| 222 // This observer already has an allocation. | 229 // This observer already has an allocation. |
| 223 return it->allocated_bitrate_bps; | 230 return config->allocated_bitrate_bps; |
| 224 } | 231 } |
| 225 } | 232 } |
| 226 | 233 |
| 227 BitrateAllocator::ObserverConfigs::iterator | 234 void BitrateAllocator::SetBitrateAllocationStrategy( |
| 235 rtc::scoped_refptr<rtc::BitrateAllocationStrategy> | |
| 236 bitrate_allocation_strategy) { | |
| 237 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | |
| 238 bitrate_allocation_strategy_ = bitrate_allocation_strategy; | |
| 239 } | |
| 240 | |
| 241 rtc::BitrateAllocationStrategy::TrackConfigs::iterator | |
| 228 BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) { | 242 BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) { |
| 229 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 243 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 230 for (auto it = bitrate_observer_configs_.begin(); | 244 for (auto it = bitrate_observer_configs_.begin(); |
| 231 it != bitrate_observer_configs_.end(); ++it) { | 245 it != bitrate_observer_configs_.end(); ++it) { |
| 232 if (it->observer == observer) | 246 ObserverConfig* config = static_cast<ObserverConfig*>(it->get()); |
| 247 if (config->observer == observer) | |
| 233 return it; | 248 return it; |
| 234 } | 249 } |
| 235 return bitrate_observer_configs_.end(); | 250 return bitrate_observer_configs_.end(); |
| 236 } | 251 } |
| 237 | 252 |
| 238 BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( | 253 BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( |
| 239 uint32_t bitrate) { | 254 uint32_t bitrate) { |
| 240 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 255 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 241 if (bitrate_observer_configs_.empty()) | 256 if (bitrate_observer_configs_.empty()) |
| 242 return ObserverAllocation(); | 257 return ObserverAllocation(); |
| 243 | 258 |
| 259 if (bitrate_allocation_strategy_ != nullptr) { | |
| 260 rtc::BitrateAllocationStrategy::TrackAllocations track_allocations = | |
| 261 bitrate_allocation_strategy_->AllocateBitrates( | |
| 262 bitrate, bitrate_observer_configs_); | |
|
nisse-webrtc
2017/09/05 11:03:46
You could consider using ArrayView as argument typ
alexnarest
2017/09/05 13:37:45
ArrayView does not implement the needed conversion
| |
| 263 // The strategy should return allocation for all tracks. | |
| 264 RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size()); | |
| 265 ObserverAllocation allocation; | |
| 266 auto track_allocations_it = track_allocations.begin(); | |
| 267 for (const auto& track_config : bitrate_observer_configs_) { | |
| 268 ObserverConfig* observer_config = | |
| 269 static_cast<ObserverConfig*>(track_config.get()); | |
| 270 allocation[observer_config->observer] = *track_allocations_it++; | |
|
stefan-webrtc
2017/09/05 10:42:47
Increment on a separate line for better readabilit
alexnarest
2017/09/05 13:37:45
git CL format does not like it on the separate lin
| |
| 271 } | |
| 272 return allocation; | |
| 273 } | |
| 274 | |
| 244 if (bitrate == 0) | 275 if (bitrate == 0) |
| 245 return ZeroRateAllocation(); | 276 return ZeroRateAllocation(); |
| 246 | 277 |
| 247 uint32_t sum_min_bitrates = 0; | 278 uint32_t sum_min_bitrates = 0; |
| 248 uint32_t sum_max_bitrates = 0; | 279 uint32_t sum_max_bitrates = 0; |
| 249 for (const auto& observer_config : bitrate_observer_configs_) { | 280 for (const auto& observer_config : bitrate_observer_configs_) { |
| 250 sum_min_bitrates += observer_config.min_bitrate_bps; | 281 sum_min_bitrates += observer_config->min_bitrate_bps; |
| 251 sum_max_bitrates += observer_config.max_bitrate_bps; | 282 sum_max_bitrates += observer_config->max_bitrate_bps; |
| 252 } | 283 } |
| 253 | 284 |
| 254 // Not enough for all observers to get an allocation, allocate according to: | 285 // Not enough for all observers to get an allocation, allocate according to: |
| 255 // enforced min bitrate -> allocated bitrate previous round -> restart paused | 286 // enforced min bitrate -> allocated bitrate previous round -> restart paused |
| 256 // streams. | 287 // streams. |
| 257 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates)) | 288 if (!EnoughBitrateForAllObservers(bitrate, sum_min_bitrates)) |
| 258 return LowRateAllocation(bitrate); | 289 return LowRateAllocation(bitrate); |
| 259 | 290 |
| 260 // All observers will get their min bitrate plus an even share of the rest. | 291 // All observers will get their min bitrate plus an even share of the rest. |
| 261 if (bitrate <= sum_max_bitrates) | 292 if (bitrate <= sum_max_bitrates) |
| 262 return NormalRateAllocation(bitrate, sum_min_bitrates); | 293 return NormalRateAllocation(bitrate, sum_min_bitrates); |
| 263 | 294 |
| 264 // All observers will get up to kTransmissionMaxBitrateMultiplier x max. | 295 // All observers will get up to kTransmissionMaxBitrateMultiplier x max. |
| 265 return MaxRateAllocation(bitrate, sum_max_bitrates); | 296 return MaxRateAllocation(bitrate, sum_max_bitrates); |
| 266 } | 297 } |
| 267 | 298 |
| 268 BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() { | 299 BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() { |
| 269 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 300 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 270 ObserverAllocation allocation; | 301 ObserverAllocation allocation; |
| 271 for (const auto& observer_config : bitrate_observer_configs_) | 302 for (const auto& track_config : bitrate_observer_configs_) { |
| 272 allocation[observer_config.observer] = 0; | 303 ObserverConfig* observer_config = |
| 304 static_cast<ObserverConfig*>(track_config.get()); | |
| 305 allocation[observer_config->observer] = 0; | |
| 306 } | |
| 273 return allocation; | 307 return allocation; |
| 274 } | 308 } |
| 275 | 309 |
| 276 BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation( | 310 BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation( |
| 277 uint32_t bitrate) { | 311 uint32_t bitrate) { |
| 278 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 312 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 279 ObserverAllocation allocation; | 313 ObserverAllocation allocation; |
| 280 // Start by allocating bitrate to observers enforcing a min bitrate, hence | 314 // Start by allocating bitrate to observers enforcing a min bitrate, hence |
| 281 // remaining_bitrate might turn negative. | 315 // remaining_bitrate might turn negative. |
| 282 int64_t remaining_bitrate = bitrate; | 316 int64_t remaining_bitrate = bitrate; |
| 283 for (const auto& observer_config : bitrate_observer_configs_) { | 317 for (const auto& track_config : bitrate_observer_configs_) { |
| 318 ObserverConfig* observer_config = | |
| 319 static_cast<ObserverConfig*>(track_config.get()); | |
| 284 int32_t allocated_bitrate = 0; | 320 int32_t allocated_bitrate = 0; |
| 285 if (observer_config.enforce_min_bitrate) | 321 if (observer_config->enforce_min_bitrate) |
| 286 allocated_bitrate = observer_config.min_bitrate_bps; | 322 allocated_bitrate = observer_config->min_bitrate_bps; |
| 287 | 323 |
| 288 allocation[observer_config.observer] = allocated_bitrate; | 324 allocation[observer_config->observer] = allocated_bitrate; |
| 289 remaining_bitrate -= allocated_bitrate; | 325 remaining_bitrate -= allocated_bitrate; |
| 290 } | 326 } |
| 291 | 327 |
| 292 // Allocate bitrate to all previously active streams. | 328 // Allocate bitrate to all previously active streams. |
| 293 if (remaining_bitrate > 0) { | 329 if (remaining_bitrate > 0) { |
| 294 for (const auto& observer_config : bitrate_observer_configs_) { | 330 for (const auto& track_config : bitrate_observer_configs_) { |
| 295 if (observer_config.enforce_min_bitrate || | 331 ObserverConfig* observer_config = |
| 296 LastAllocatedBitrate(observer_config) == 0) | 332 static_cast<ObserverConfig*>(track_config.get()); |
| 333 if (observer_config->enforce_min_bitrate || | |
| 334 LastAllocatedBitrate(*observer_config) == 0) | |
| 297 continue; | 335 continue; |
| 298 | 336 |
| 299 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config); | 337 uint32_t required_bitrate = MinBitrateWithHysteresis(*observer_config); |
| 300 if (remaining_bitrate >= required_bitrate) { | 338 if (remaining_bitrate >= required_bitrate) { |
| 301 allocation[observer_config.observer] = required_bitrate; | 339 allocation[observer_config->observer] = required_bitrate; |
| 302 remaining_bitrate -= required_bitrate; | 340 remaining_bitrate -= required_bitrate; |
| 303 } | 341 } |
| 304 } | 342 } |
| 305 } | 343 } |
| 306 | 344 |
| 307 // Allocate bitrate to previously paused streams. | 345 // Allocate bitrate to previously paused streams. |
| 308 if (remaining_bitrate > 0) { | 346 if (remaining_bitrate > 0) { |
| 309 for (const auto& observer_config : bitrate_observer_configs_) { | 347 for (const auto& track_config : bitrate_observer_configs_) { |
| 310 if (LastAllocatedBitrate(observer_config) != 0) | 348 ObserverConfig* observer_config = |
| 349 static_cast<ObserverConfig*>(track_config.get()); | |
| 350 if (LastAllocatedBitrate(*observer_config) != 0) | |
| 311 continue; | 351 continue; |
| 312 | 352 |
| 313 // Add a hysteresis to avoid toggling. | 353 // Add a hysteresis to avoid toggling. |
| 314 uint32_t required_bitrate = MinBitrateWithHysteresis(observer_config); | 354 uint32_t required_bitrate = MinBitrateWithHysteresis(*observer_config); |
| 315 if (remaining_bitrate >= required_bitrate) { | 355 if (remaining_bitrate >= required_bitrate) { |
| 316 allocation[observer_config.observer] = required_bitrate; | 356 allocation[observer_config->observer] = required_bitrate; |
| 317 remaining_bitrate -= required_bitrate; | 357 remaining_bitrate -= required_bitrate; |
| 318 } | 358 } |
| 319 } | 359 } |
| 320 } | 360 } |
| 321 | 361 |
| 322 // Split a possible remainder evenly on all streams with an allocation. | 362 // Split a possible remainder evenly on all streams with an allocation. |
| 323 if (remaining_bitrate > 0) | 363 if (remaining_bitrate > 0) |
| 324 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation); | 364 DistributeBitrateEvenly(remaining_bitrate, false, 1, &allocation); |
| 325 | 365 |
| 326 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size()); | 366 RTC_DCHECK_EQ(allocation.size(), bitrate_observer_configs_.size()); |
| 327 return allocation; | 367 return allocation; |
| 328 } | 368 } |
| 329 | 369 |
| 330 BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( | 370 BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( |
| 331 uint32_t bitrate, | 371 uint32_t bitrate, |
| 332 uint32_t sum_min_bitrates) { | 372 uint32_t sum_min_bitrates) { |
| 333 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 373 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 334 ObserverAllocation allocation; | 374 ObserverAllocation allocation; |
| 335 for (const auto& observer_config : bitrate_observer_configs_) | 375 for (const auto& track_config : bitrate_observer_configs_) { |
| 336 allocation[observer_config.observer] = observer_config.min_bitrate_bps; | 376 ObserverConfig* observer_config = |
| 377 static_cast<ObserverConfig*>(track_config.get()); | |
| 378 allocation[observer_config->observer] = observer_config->min_bitrate_bps; | |
| 379 } | |
| 337 | 380 |
| 338 bitrate -= sum_min_bitrates; | 381 bitrate -= sum_min_bitrates; |
| 339 if (bitrate > 0) | 382 if (bitrate > 0) |
| 340 DistributeBitrateEvenly(bitrate, true, 1, &allocation); | 383 DistributeBitrateEvenly(bitrate, true, 1, &allocation); |
| 341 | 384 |
| 342 return allocation; | 385 return allocation; |
| 343 } | 386 } |
| 344 | 387 |
| 345 BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation( | 388 BitrateAllocator::ObserverAllocation BitrateAllocator::MaxRateAllocation( |
| 346 uint32_t bitrate, | 389 uint32_t bitrate, |
| 347 uint32_t sum_max_bitrates) { | 390 uint32_t sum_max_bitrates) { |
| 348 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 391 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 349 ObserverAllocation allocation; | 392 ObserverAllocation allocation; |
| 350 | 393 |
| 351 for (const auto& observer_config : bitrate_observer_configs_) { | 394 for (const auto& track_config : bitrate_observer_configs_) { |
| 352 allocation[observer_config.observer] = observer_config.max_bitrate_bps; | 395 ObserverConfig* observer_config = |
| 353 bitrate -= observer_config.max_bitrate_bps; | 396 static_cast<ObserverConfig*>(track_config.get()); |
| 397 allocation[observer_config->observer] = observer_config->max_bitrate_bps; | |
| 398 bitrate -= observer_config->max_bitrate_bps; | |
| 354 } | 399 } |
| 355 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier, | 400 DistributeBitrateEvenly(bitrate, true, kTransmissionMaxBitrateMultiplier, |
| 356 &allocation); | 401 &allocation); |
| 357 return allocation; | 402 return allocation; |
| 358 } | 403 } |
| 359 | 404 |
| 360 uint32_t BitrateAllocator::LastAllocatedBitrate( | 405 uint32_t BitrateAllocator::LastAllocatedBitrate( |
| 361 const ObserverConfig& observer_config) { | 406 const ObserverConfig& observer_config) { |
| 362 // Return the configured minimum bitrate for newly added observers, to avoid | 407 // Return the configured minimum bitrate for newly added observers, to avoid |
| 363 // requiring an extra high bitrate for the observer to get an allocated | 408 // requiring an extra high bitrate for the observer to get an allocated |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 387 } | 432 } |
| 388 | 433 |
| 389 void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate, | 434 void BitrateAllocator::DistributeBitrateEvenly(uint32_t bitrate, |
| 390 bool include_zero_allocations, | 435 bool include_zero_allocations, |
| 391 int max_multiplier, | 436 int max_multiplier, |
| 392 ObserverAllocation* allocation) { | 437 ObserverAllocation* allocation) { |
| 393 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 438 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 394 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size()); | 439 RTC_DCHECK_EQ(allocation->size(), bitrate_observer_configs_.size()); |
| 395 | 440 |
| 396 ObserverSortingMap list_max_bitrates; | 441 ObserverSortingMap list_max_bitrates; |
| 397 for (const auto& observer_config : bitrate_observer_configs_) { | 442 for (const auto& track_config : bitrate_observer_configs_) { |
| 443 ObserverConfig* observer_config = | |
| 444 static_cast<ObserverConfig*>(track_config.get()); | |
| 398 if (include_zero_allocations || | 445 if (include_zero_allocations || |
| 399 allocation->at(observer_config.observer) != 0) { | 446 allocation->at(observer_config->observer) != 0) { |
| 400 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>( | 447 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfig*>( |
| 401 observer_config.max_bitrate_bps, &observer_config)); | 448 observer_config->max_bitrate_bps, observer_config)); |
| 402 } | 449 } |
| 403 } | 450 } |
| 404 auto it = list_max_bitrates.begin(); | 451 auto it = list_max_bitrates.begin(); |
| 405 while (it != list_max_bitrates.end()) { | 452 while (it != list_max_bitrates.end()) { |
| 406 RTC_DCHECK_GT(bitrate, 0); | 453 RTC_DCHECK_GT(bitrate, 0); |
| 407 uint32_t extra_allocation = | 454 uint32_t extra_allocation = |
| 408 bitrate / static_cast<uint32_t>(list_max_bitrates.size()); | 455 bitrate / static_cast<uint32_t>(list_max_bitrates.size()); |
| 409 uint32_t total_allocation = | 456 uint32_t total_allocation = |
| 410 extra_allocation + allocation->at(it->second->observer); | 457 extra_allocation + allocation->at(it->second->observer); |
| 411 bitrate -= extra_allocation; | 458 bitrate -= extra_allocation; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 423 | 470 |
| 424 bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate, | 471 bool BitrateAllocator::EnoughBitrateForAllObservers(uint32_t bitrate, |
| 425 uint32_t sum_min_bitrates) { | 472 uint32_t sum_min_bitrates) { |
| 426 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); | 473 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 427 if (bitrate < sum_min_bitrates) | 474 if (bitrate < sum_min_bitrates) |
| 428 return false; | 475 return false; |
| 429 | 476 |
| 430 uint32_t extra_bitrate_per_observer = | 477 uint32_t extra_bitrate_per_observer = |
| 431 (bitrate - sum_min_bitrates) / | 478 (bitrate - sum_min_bitrates) / |
| 432 static_cast<uint32_t>(bitrate_observer_configs_.size()); | 479 static_cast<uint32_t>(bitrate_observer_configs_.size()); |
| 433 for (const auto& observer_config : bitrate_observer_configs_) { | 480 for (const auto& track_config : bitrate_observer_configs_) { |
| 434 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < | 481 ObserverConfig* observer_config = |
| 435 MinBitrateWithHysteresis(observer_config)) { | 482 static_cast<ObserverConfig*>(track_config.get()); |
| 483 if (observer_config->min_bitrate_bps + extra_bitrate_per_observer < | |
| 484 MinBitrateWithHysteresis(*observer_config)) { | |
| 436 return false; | 485 return false; |
| 437 } | 486 } |
| 438 } | 487 } |
| 439 return true; | 488 return true; |
| 440 } | 489 } |
| 441 } // namespace webrtc | 490 } // namespace webrtc |
| OLD | NEW |