| 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 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 // Allow packets to be transmitted in up to 2 times max video bitrate if the | 21 // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 22 // bandwidth estimate allows it. | 22 // bandwidth estimate allows it. |
| 23 const int kTransmissionMaxBitrateMultiplier = 2; | 23 const int kTransmissionMaxBitrateMultiplier = 2; |
| 24 const int kDefaultBitrateBps = 300000; | 24 const int kDefaultBitrateBps = 300000; |
| 25 | 25 |
| 26 BitrateAllocator::BitrateAllocator() | 26 BitrateAllocator::BitrateAllocator() |
| 27 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 27 : bitrate_observers_(), |
| 28 bitrate_observers_(), | |
| 29 bitrate_observers_modified_(false), | 28 bitrate_observers_modified_(false), |
| 30 enforce_min_bitrate_(true), | 29 enforce_min_bitrate_(true), |
| 31 last_bitrate_bps_(kDefaultBitrateBps), | 30 last_bitrate_bps_(kDefaultBitrateBps), |
| 32 last_fraction_loss_(0), | 31 last_fraction_loss_(0), |
| 33 last_rtt_(0) {} | 32 last_rtt_(0) {} |
| 34 | 33 |
| 35 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, | 34 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, |
| 36 uint8_t fraction_loss, | 35 uint8_t fraction_loss, |
| 37 int64_t rtt) { | 36 int64_t rtt) { |
| 38 CriticalSectionScoped lock(crit_sect_.get()); | 37 rtc::CritScope lock(&crit_sect_); |
| 39 last_bitrate_bps_ = bitrate; | 38 last_bitrate_bps_ = bitrate; |
| 40 last_fraction_loss_ = fraction_loss; | 39 last_fraction_loss_ = fraction_loss; |
| 41 last_rtt_ = rtt; | 40 last_rtt_ = rtt; |
| 42 uint32_t allocated_bitrate_bps = 0; | 41 uint32_t allocated_bitrate_bps = 0; |
| 43 ObserverBitrateMap allocation = AllocateBitrates(); | 42 ObserverBitrateMap allocation = AllocateBitrates(); |
| 44 for (const auto& kv : allocation) { | 43 for (const auto& kv : allocation) { |
| 45 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | 44 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); |
| 46 allocated_bitrate_bps += kv.second; | 45 allocated_bitrate_bps += kv.second; |
| 47 } | 46 } |
| 48 return allocated_bitrate_bps; | 47 return allocated_bitrate_bps; |
| 49 } | 48 } |
| 50 | 49 |
| 51 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() { | 50 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() { |
| 52 if (bitrate_observers_.empty()) | 51 if (bitrate_observers_.empty()) |
| 53 return ObserverBitrateMap(); | 52 return ObserverBitrateMap(); |
| 54 | 53 |
| 55 uint32_t sum_min_bitrates = 0; | 54 uint32_t sum_min_bitrates = 0; |
| 56 for (const auto& observer : bitrate_observers_) | 55 for (const auto& observer : bitrate_observers_) |
| 57 sum_min_bitrates += observer.second.min_bitrate; | 56 sum_min_bitrates += observer.second.min_bitrate; |
| 58 if (last_bitrate_bps_ <= sum_min_bitrates) | 57 if (last_bitrate_bps_ <= sum_min_bitrates) |
| 59 return LowRateAllocation(last_bitrate_bps_); | 58 return LowRateAllocation(last_bitrate_bps_); |
| 60 else | 59 else |
| 61 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); | 60 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); |
| 62 } | 61 } |
| 63 | 62 |
| 64 int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer, | 63 int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer, |
| 65 uint32_t min_bitrate_bps, | 64 uint32_t min_bitrate_bps, |
| 66 uint32_t max_bitrate_bps) { | 65 uint32_t max_bitrate_bps) { |
| 67 CriticalSectionScoped lock(crit_sect_.get()); | 66 rtc::CritScope lock(&crit_sect_); |
| 68 | 67 |
| 69 BitrateObserverConfList::iterator it = | 68 BitrateObserverConfList::iterator it = |
| 70 FindObserverConfigurationPair(observer); | 69 FindObserverConfigurationPair(observer); |
| 71 | 70 |
| 72 // Allow the max bitrate to be exceeded for FEC and retransmissions. | 71 // 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 | 72 // 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 | 73 // properly allocate bitrate. The allocator should instead distribute any |
| 75 // extra bitrate after all streams have maxed out. | 74 // extra bitrate after all streams have maxed out. |
| 76 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; | 75 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; |
| 77 if (it != bitrate_observers_.end()) { | 76 if (it != bitrate_observers_.end()) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 int new_observer_bitrate_bps = 0; | 88 int new_observer_bitrate_bps = 0; |
| 90 for (auto& kv : allocation) { | 89 for (auto& kv : allocation) { |
| 91 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | 90 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); |
| 92 if (kv.first == observer) | 91 if (kv.first == observer) |
| 93 new_observer_bitrate_bps = kv.second; | 92 new_observer_bitrate_bps = kv.second; |
| 94 } | 93 } |
| 95 return new_observer_bitrate_bps; | 94 return new_observer_bitrate_bps; |
| 96 } | 95 } |
| 97 | 96 |
| 98 void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) { | 97 void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) { |
| 99 CriticalSectionScoped lock(crit_sect_.get()); | 98 rtc::CritScope lock(&crit_sect_); |
| 100 BitrateObserverConfList::iterator it = | 99 BitrateObserverConfList::iterator it = |
| 101 FindObserverConfigurationPair(observer); | 100 FindObserverConfigurationPair(observer); |
| 102 if (it != bitrate_observers_.end()) { | 101 if (it != bitrate_observers_.end()) { |
| 103 bitrate_observers_.erase(it); | 102 bitrate_observers_.erase(it); |
| 104 bitrate_observers_modified_ = true; | 103 bitrate_observers_modified_ = true; |
| 105 } | 104 } |
| 106 } | 105 } |
| 107 | 106 |
| 108 void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps, | 107 void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps, |
| 109 int* max_bitrate_sum_bps) const { | 108 int* max_bitrate_sum_bps) const { |
| 110 *min_bitrate_sum_bps = 0; | 109 *min_bitrate_sum_bps = 0; |
| 111 *max_bitrate_sum_bps = 0; | 110 *max_bitrate_sum_bps = 0; |
| 112 | 111 |
| 113 CriticalSectionScoped lock(crit_sect_.get()); | 112 rtc::CritScope lock(&crit_sect_); |
| 114 for (const auto& observer : bitrate_observers_) { | 113 for (const auto& observer : bitrate_observers_) { |
| 115 *min_bitrate_sum_bps += observer.second.min_bitrate; | 114 *min_bitrate_sum_bps += observer.second.min_bitrate; |
| 116 *max_bitrate_sum_bps += observer.second.max_bitrate; | 115 *max_bitrate_sum_bps += observer.second.max_bitrate; |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 | 118 |
| 120 BitrateAllocator::BitrateObserverConfList::iterator | 119 BitrateAllocator::BitrateObserverConfList::iterator |
| 121 BitrateAllocator::FindObserverConfigurationPair( | 120 BitrateAllocator::FindObserverConfigurationPair( |
| 122 const BitrateObserver* observer) { | 121 const BitrateObserver* observer) { |
| 123 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end(); | 122 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
| 124 ++it) { | 123 ++it) { |
| 125 if (it->first == observer) | 124 if (it->first == observer) |
| 126 return it; | 125 return it; |
| 127 } | 126 } |
| 128 return bitrate_observers_.end(); | 127 return bitrate_observers_.end(); |
| 129 } | 128 } |
| 130 | 129 |
| 131 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { | 130 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { |
| 132 CriticalSectionScoped lock(crit_sect_.get()); | 131 rtc::CritScope lock(&crit_sect_); |
| 133 enforce_min_bitrate_ = enforce_min_bitrate; | 132 enforce_min_bitrate_ = enforce_min_bitrate; |
| 134 } | 133 } |
| 135 | 134 |
| 136 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( | 135 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( |
| 137 uint32_t bitrate, | 136 uint32_t bitrate, |
| 138 uint32_t sum_min_bitrates) { | 137 uint32_t sum_min_bitrates) { |
| 139 uint32_t number_of_observers = | 138 uint32_t number_of_observers = |
| 140 static_cast<uint32_t>(bitrate_observers_.size()); | 139 static_cast<uint32_t>(bitrate_observers_.size()); |
| 141 uint32_t bitrate_per_observer = | 140 uint32_t bitrate_per_observer = |
| 142 (bitrate - sum_min_bitrates) / number_of_observers; | 141 (bitrate - sum_min_bitrates) / number_of_observers; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 for (const auto& observer : bitrate_observers_) { | 184 for (const auto& observer : bitrate_observers_) { |
| 186 uint32_t allocated_bitrate = | 185 uint32_t allocated_bitrate = |
| 187 std::min(remainder, observer.second.min_bitrate); | 186 std::min(remainder, observer.second.min_bitrate); |
| 188 allocation[observer.first] = allocated_bitrate; | 187 allocation[observer.first] = allocated_bitrate; |
| 189 remainder -= allocated_bitrate; | 188 remainder -= allocated_bitrate; |
| 190 } | 189 } |
| 191 } | 190 } |
| 192 return allocation; | 191 return allocation; |
| 193 } | 192 } |
| 194 } // namespace webrtc | 193 } // namespace webrtc |
| OLD | NEW |