| OLD | NEW |
| (Empty) |
| 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 | |
| 12 #include "webrtc/modules/bitrate_controller/include/bitrate_allocator.h" | |
| 13 | |
| 14 #include <algorithm> | |
| 15 #include <utility> | |
| 16 | |
| 17 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 // Allow packets to be transmitted in up to 2 times max video bitrate if the | |
| 22 // bandwidth estimate allows it. | |
| 23 const int kTransmissionMaxBitrateMultiplier = 2; | |
| 24 const int kDefaultBitrateBps = 300000; | |
| 25 | |
| 26 BitrateAllocator::BitrateAllocator() | |
| 27 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 28 bitrate_observers_(), | |
| 29 bitrate_observers_modified_(false), | |
| 30 enforce_min_bitrate_(true), | |
| 31 last_bitrate_bps_(kDefaultBitrateBps), | |
| 32 last_fraction_loss_(0), | |
| 33 last_rtt_(0) {} | |
| 34 | |
| 35 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, | |
| 36 uint8_t fraction_loss, | |
| 37 int64_t rtt) { | |
| 38 CriticalSectionScoped lock(crit_sect_.get()); | |
| 39 last_bitrate_bps_ = bitrate; | |
| 40 last_fraction_loss_ = fraction_loss; | |
| 41 last_rtt_ = rtt; | |
| 42 uint32_t allocated_bitrate_bps = 0; | |
| 43 ObserverBitrateMap allocation = AllocateBitrates(); | |
| 44 for (const auto& kv : allocation) { | |
| 45 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | |
| 46 allocated_bitrate_bps += kv.second; | |
| 47 } | |
| 48 return allocated_bitrate_bps; | |
| 49 } | |
| 50 | |
| 51 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() { | |
| 52 if (bitrate_observers_.empty()) | |
| 53 return ObserverBitrateMap(); | |
| 54 | |
| 55 uint32_t sum_min_bitrates = 0; | |
| 56 for (const auto& observer : bitrate_observers_) | |
| 57 sum_min_bitrates += observer.second.min_bitrate; | |
| 58 if (last_bitrate_bps_ <= sum_min_bitrates) | |
| 59 return LowRateAllocation(last_bitrate_bps_); | |
| 60 else | |
| 61 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); | |
| 62 } | |
| 63 | |
| 64 int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer, | |
| 65 uint32_t min_bitrate_bps, | |
| 66 uint32_t max_bitrate_bps) { | |
| 67 CriticalSectionScoped lock(crit_sect_.get()); | |
| 68 | |
| 69 BitrateObserverConfList::iterator it = | |
| 70 FindObserverConfigurationPair(observer); | |
| 71 | |
| 72 // Allow the max bitrate to be exceeded for FEC and retransmissions. | |
| 73 // TODO(holmer): We have to get rid of this hack as it makes it difficult to | |
| 74 // properly allocate bitrate. The allocator should instead distribute any | |
| 75 // extra bitrate after all streams have maxed out. | |
| 76 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; | |
| 77 if (it != bitrate_observers_.end()) { | |
| 78 // Update current configuration. | |
| 79 it->second.min_bitrate = min_bitrate_bps; | |
| 80 it->second.max_bitrate = max_bitrate_bps; | |
| 81 } else { | |
| 82 // Add new settings. | |
| 83 bitrate_observers_.push_back(BitrateObserverConfiguration( | |
| 84 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps))); | |
| 85 bitrate_observers_modified_ = true; | |
| 86 } | |
| 87 | |
| 88 ObserverBitrateMap allocation = AllocateBitrates(); | |
| 89 int new_observer_bitrate_bps = 0; | |
| 90 for (auto& kv : allocation) { | |
| 91 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | |
| 92 if (kv.first == observer) | |
| 93 new_observer_bitrate_bps = kv.second; | |
| 94 } | |
| 95 return new_observer_bitrate_bps; | |
| 96 } | |
| 97 | |
| 98 void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) { | |
| 99 CriticalSectionScoped lock(crit_sect_.get()); | |
| 100 BitrateObserverConfList::iterator it = | |
| 101 FindObserverConfigurationPair(observer); | |
| 102 if (it != bitrate_observers_.end()) { | |
| 103 bitrate_observers_.erase(it); | |
| 104 bitrate_observers_modified_ = true; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps, | |
| 109 int* max_bitrate_sum_bps) const { | |
| 110 *min_bitrate_sum_bps = 0; | |
| 111 *max_bitrate_sum_bps = 0; | |
| 112 | |
| 113 CriticalSectionScoped lock(crit_sect_.get()); | |
| 114 for (const auto& observer : bitrate_observers_) { | |
| 115 *min_bitrate_sum_bps += observer.second.min_bitrate; | |
| 116 *max_bitrate_sum_bps += observer.second.max_bitrate; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 BitrateAllocator::BitrateObserverConfList::iterator | |
| 121 BitrateAllocator::FindObserverConfigurationPair( | |
| 122 const BitrateObserver* observer) { | |
| 123 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end(); | |
| 124 ++it) { | |
| 125 if (it->first == observer) | |
| 126 return it; | |
| 127 } | |
| 128 return bitrate_observers_.end(); | |
| 129 } | |
| 130 | |
| 131 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { | |
| 132 CriticalSectionScoped lock(crit_sect_.get()); | |
| 133 enforce_min_bitrate_ = enforce_min_bitrate; | |
| 134 } | |
| 135 | |
| 136 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( | |
| 137 uint32_t bitrate, | |
| 138 uint32_t sum_min_bitrates) { | |
| 139 uint32_t number_of_observers = bitrate_observers_.size(); | |
| 140 uint32_t bitrate_per_observer = | |
| 141 (bitrate - sum_min_bitrates) / number_of_observers; | |
| 142 // Use map to sort list based on max bitrate. | |
| 143 ObserverSortingMap list_max_bitrates; | |
| 144 for (const auto& observer : bitrate_observers_) { | |
| 145 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>( | |
| 146 observer.second.max_bitrate, | |
| 147 ObserverConfiguration(observer.first, observer.second.min_bitrate))); | |
| 148 } | |
| 149 ObserverBitrateMap allocation; | |
| 150 ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); | |
| 151 while (max_it != list_max_bitrates.end()) { | |
| 152 number_of_observers--; | |
| 153 uint32_t observer_allowance = | |
| 154 max_it->second.min_bitrate + bitrate_per_observer; | |
| 155 if (max_it->first < observer_allowance) { | |
| 156 // We have more than enough for this observer. | |
| 157 // Carry the remainder forward. | |
| 158 uint32_t remainder = observer_allowance - max_it->first; | |
| 159 if (number_of_observers != 0) { | |
| 160 bitrate_per_observer += remainder / number_of_observers; | |
| 161 } | |
| 162 allocation[max_it->second.observer] = max_it->first; | |
| 163 } else { | |
| 164 allocation[max_it->second.observer] = observer_allowance; | |
| 165 } | |
| 166 list_max_bitrates.erase(max_it); | |
| 167 // Prepare next iteration. | |
| 168 max_it = list_max_bitrates.begin(); | |
| 169 } | |
| 170 return allocation; | |
| 171 } | |
| 172 | |
| 173 BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation( | |
| 174 uint32_t bitrate) { | |
| 175 ObserverBitrateMap allocation; | |
| 176 if (enforce_min_bitrate_) { | |
| 177 // Min bitrate to all observers. | |
| 178 for (const auto& observer : bitrate_observers_) | |
| 179 allocation[observer.first] = observer.second.min_bitrate; | |
| 180 } else { | |
| 181 // Allocate up to |min_bitrate| to one observer at a time, until | |
| 182 // |bitrate| is depleted. | |
| 183 uint32_t remainder = bitrate; | |
| 184 for (const auto& observer : bitrate_observers_) { | |
| 185 uint32_t allocated_bitrate = | |
| 186 std::min(remainder, observer.second.min_bitrate); | |
| 187 allocation[observer.first] = allocated_bitrate; | |
| 188 remainder -= allocated_bitrate; | |
| 189 } | |
| 190 } | |
| 191 return allocation; | |
| 192 } | |
| 193 } // namespace webrtc | |
| OLD | NEW |