stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 12 | #include "webrtc/call/bitrate_allocator.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
| 15 | #include <utility> |
| 16 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 22 | // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 23 | // bandwidth estimate allows it. |
| 24 | const int kTransmissionMaxBitrateMultiplier = 2; |
| 25 | const int kDefaultBitrateBps = 300000; |
| 26 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 27 | BitrateAllocator::BitrateAllocator() |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 28 | : bitrate_observer_configs_(), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 29 | enforce_min_bitrate_(true), |
| 30 | last_bitrate_bps_(kDefaultBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 31 | last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 32 | last_fraction_loss_(0), |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 33 | last_rtt_(0) {} |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 34 | |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 35 | uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, |
| 36 | uint8_t fraction_loss, |
| 37 | int64_t rtt) { |
tommi | 63cb434 | 2016-01-20 02:32:54 -0800 | [diff] [blame] | 38 | rtc::CritScope lock(&crit_sect_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 39 | last_bitrate_bps_ = bitrate; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 40 | last_non_zero_bitrate_bps_ = |
| 41 | bitrate > 0 ? bitrate : last_non_zero_bitrate_bps_; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 42 | last_fraction_loss_ = fraction_loss; |
| 43 | last_rtt_ = rtt; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 44 | |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 45 | uint32_t allocated_bitrate_bps = 0; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 46 | ObserverAllocation allocation = AllocateBitrates(bitrate); |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 47 | for (const auto& kv : allocation) { |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 48 | kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 49 | allocated_bitrate_bps += kv.second; |
| 50 | } |
| 51 | return allocated_bitrate_bps; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 52 | } |
| 53 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 54 | int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
| 55 | uint32_t min_bitrate_bps, |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 56 | uint32_t max_bitrate_bps, |
| 57 | bool enforce_min_bitrate) { |
tommi | 63cb434 | 2016-01-20 02:32:54 -0800 | [diff] [blame] | 58 | rtc::CritScope lock(&crit_sect_); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 59 | // TODO(mflodman): Enforce this per observer. |
| 60 | EnforceMinBitrate(enforce_min_bitrate); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 61 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 62 | auto it = FindObserverConfig(observer); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 63 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 64 | // Allow the max bitrate to be exceeded for FEC and retransmissions. |
| 65 | // TODO(holmer): We have to get rid of this hack as it makes it difficult to |
| 66 | // properly allocate bitrate. The allocator should instead distribute any |
| 67 | // extra bitrate after all streams have maxed out. |
| 68 | max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 69 | if (it != bitrate_observer_configs_.end()) { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 70 | // Update current configuration. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 71 | it->min_bitrate_bps = min_bitrate_bps; |
| 72 | it->max_bitrate_bps = max_bitrate_bps; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 73 | } else { |
| 74 | // Add new settings. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 75 | bitrate_observer_configs_.push_back(ObserverConfig( |
| 76 | observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate)); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 77 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 78 | |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 79 | int new_observer_bitrate_bps = 0; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 80 | if (last_bitrate_bps_ > 0) { // We have a bitrate to allocate. |
| 81 | ObserverAllocation allocation = AllocateBitrates(last_bitrate_bps_); |
| 82 | for (auto& kv : allocation) { |
| 83 | // Update all observers with the new allocation. |
| 84 | kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); |
| 85 | if (kv.first == observer) |
| 86 | new_observer_bitrate_bps = kv.second; |
| 87 | } |
| 88 | } else { |
| 89 | // Currently, an encoder is not allowed to produce frames. |
| 90 | // But we still have to return the initial config bitrate + let the |
| 91 | // observer know that it can not produce frames. |
| 92 | ObserverAllocation allocation = |
| 93 | AllocateBitrates(last_non_zero_bitrate_bps_); |
| 94 | observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
| 95 | new_observer_bitrate_bps = allocation[observer]; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 96 | } |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 97 | return new_observer_bitrate_bps; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 100 | void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
tommi | 63cb434 | 2016-01-20 02:32:54 -0800 | [diff] [blame] | 101 | rtc::CritScope lock(&crit_sect_); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 102 | auto it = FindObserverConfig(observer); |
| 103 | if (it != bitrate_observer_configs_.end()) { |
| 104 | bitrate_observer_configs_.erase(it); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 108 | void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 109 | enforce_min_bitrate_ = enforce_min_bitrate; |
| 110 | } |
| 111 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 112 | BitrateAllocator::ObserverConfigList::iterator |
| 113 | BitrateAllocator::FindObserverConfig( |
| 114 | const BitrateAllocatorObserver* observer) { |
| 115 | for (auto it = bitrate_observer_configs_.begin(); |
| 116 | it != bitrate_observer_configs_.end(); ++it) { |
| 117 | if (it->observer == observer) |
| 118 | return it; |
| 119 | } |
| 120 | return bitrate_observer_configs_.end(); |
| 121 | } |
| 122 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 123 | BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates( |
| 124 | uint32_t bitrate) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 125 | if (bitrate_observer_configs_.empty()) |
| 126 | return ObserverAllocation(); |
| 127 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 128 | if (bitrate == 0) |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 129 | return ZeroRateAllocation(); |
| 130 | |
| 131 | uint32_t sum_min_bitrates = 0; |
| 132 | for (const auto& observer_config : bitrate_observer_configs_) |
| 133 | sum_min_bitrates += observer_config.min_bitrate_bps; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 134 | if (bitrate <= sum_min_bitrates) |
| 135 | return LowRateAllocation(bitrate); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 136 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame^] | 137 | return NormalRateAllocation(bitrate, sum_min_bitrates); |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation( |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 141 | uint32_t bitrate, |
| 142 | uint32_t sum_min_bitrates) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 143 | uint32_t num_remaining_observers = |
| 144 | static_cast<uint32_t>(bitrate_observer_configs_.size()); |
| 145 | RTC_DCHECK_GT(num_remaining_observers, 0u); |
| 146 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 147 | uint32_t bitrate_per_observer = |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 148 | (bitrate - sum_min_bitrates) / num_remaining_observers; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 149 | // Use map to sort list based on max bitrate. |
| 150 | ObserverSortingMap list_max_bitrates; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 151 | for (const auto& config : bitrate_observer_configs_) { |
| 152 | list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>( |
| 153 | config.max_bitrate_bps, &config)); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 154 | } |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 155 | |
| 156 | ObserverAllocation allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 157 | ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); |
| 158 | while (max_it != list_max_bitrates.end()) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 159 | num_remaining_observers--; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 160 | uint32_t observer_allowance = |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 161 | max_it->second->min_bitrate_bps + bitrate_per_observer; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 162 | if (max_it->first < observer_allowance) { |
| 163 | // We have more than enough for this observer. |
| 164 | // Carry the remainder forward. |
| 165 | uint32_t remainder = observer_allowance - max_it->first; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 166 | if (num_remaining_observers != 0) |
| 167 | bitrate_per_observer += remainder / num_remaining_observers; |
| 168 | allocation[max_it->second->observer] = max_it->first; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 169 | } else { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 170 | allocation[max_it->second->observer] = observer_allowance; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 171 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 172 | list_max_bitrates.erase(max_it); |
| 173 | // Prepare next iteration. |
| 174 | max_it = list_max_bitrates.begin(); |
| 175 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 176 | return allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 177 | } |
| 178 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 179 | BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() { |
| 180 | ObserverAllocation allocation; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 181 | // Zero bitrate to all observers. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 182 | for (const auto& observer_config : bitrate_observer_configs_) |
| 183 | allocation[observer_config.observer] = 0; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 184 | return allocation; |
| 185 | } |
| 186 | |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 187 | BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation( |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 188 | uint32_t bitrate) { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 189 | ObserverAllocation allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 190 | if (enforce_min_bitrate_) { |
| 191 | // Min bitrate to all observers. |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 192 | for (const auto& observer_config : bitrate_observer_configs_) |
| 193 | allocation[observer_config.observer] = observer_config.min_bitrate_bps; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 194 | } else { |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 195 | // Allocate up to |min_bitrate_bps| to one observer at a time, until |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 196 | // |bitrate| is depleted. |
| 197 | uint32_t remainder = bitrate; |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 198 | for (const auto& observer_config : bitrate_observer_configs_) { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 199 | uint32_t allocated_bitrate = |
mflodman | 2ebe5b1 | 2016-05-13 01:43:51 -0700 | [diff] [blame] | 200 | std::min(remainder, observer_config.min_bitrate_bps); |
| 201 | allocation[observer_config.observer] = allocated_bitrate; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 202 | remainder -= allocated_bitrate; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 205 | return allocation; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 206 | } |
| 207 | } // namespace webrtc |