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