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 */ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 for (const auto& kv : allocation) | 44 for (const auto& kv : allocation) |
45 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | 45 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); |
46 } | 46 } |
47 | 47 |
48 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() { | 48 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() { |
49 if (bitrate_observers_.empty()) | 49 if (bitrate_observers_.empty()) |
50 return ObserverBitrateMap(); | 50 return ObserverBitrateMap(); |
51 | 51 |
52 uint32_t sum_min_bitrates = 0; | 52 uint32_t sum_min_bitrates = 0; |
53 for (const auto& observer : bitrate_observers_) | 53 for (const auto& observer : bitrate_observers_) |
54 sum_min_bitrates += observer.second.min_bitrate_; | 54 sum_min_bitrates += observer.second.min_bitrate; |
55 if (last_bitrate_bps_ <= sum_min_bitrates) | 55 if (last_bitrate_bps_ <= sum_min_bitrates) |
56 return LowRateAllocation(last_bitrate_bps_); | 56 return LowRateAllocation(last_bitrate_bps_); |
57 else | 57 else |
58 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); | 58 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates); |
59 } | 59 } |
60 | 60 |
61 int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer, | 61 int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer, |
62 uint32_t start_bitrate_bps, | |
63 uint32_t min_bitrate_bps, | 62 uint32_t min_bitrate_bps, |
64 uint32_t max_bitrate_bps, | 63 uint32_t max_bitrate_bps) { |
65 int* new_observer_bitrate_bps) { | |
66 CriticalSectionScoped lock(crit_sect_.get()); | 64 CriticalSectionScoped lock(crit_sect_.get()); |
67 | 65 |
68 BitrateObserverConfList::iterator it = | 66 BitrateObserverConfList::iterator it = |
69 FindObserverConfigurationPair(observer); | 67 FindObserverConfigurationPair(observer); |
70 | 68 |
71 // Allow the max bitrate to be exceeded for FEC and retransmissions. | 69 // Allow the max bitrate to be exceeded for FEC and retransmissions. |
72 // TODO(holmer): We have to get rid of this hack as it makes it difficult to | 70 // TODO(holmer): We have to get rid of this hack as it makes it difficult to |
73 // properly allocate bitrate. The allocator should instead distribute any | 71 // properly allocate bitrate. The allocator should instead distribute any |
74 // extra bitrate after all streams have maxed out. | 72 // extra bitrate after all streams have maxed out. |
75 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; | 73 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; |
76 int new_bwe_candidate_bps = 0; | |
77 if (it != bitrate_observers_.end()) { | 74 if (it != bitrate_observers_.end()) { |
78 // Update current configuration. | 75 // Update current configuration. |
79 it->second.start_bitrate_ = start_bitrate_bps; | 76 it->second.min_bitrate = min_bitrate_bps; |
80 it->second.min_bitrate_ = min_bitrate_bps; | 77 it->second.max_bitrate = max_bitrate_bps; |
81 it->second.max_bitrate_ = max_bitrate_bps; | |
82 // Set the send-side bandwidth to the max of the sum of start bitrates and | |
83 // the current estimate, so that if the user wants to immediately use more | |
84 // bandwidth, that can be enforced. | |
85 for (const auto& observer : bitrate_observers_) | |
86 new_bwe_candidate_bps += observer.second.start_bitrate_; | |
87 } else { | 78 } else { |
88 // Add new settings. | 79 // Add new settings. |
89 bitrate_observers_.push_back(BitrateObserverConfiguration( | 80 bitrate_observers_.push_back(BitrateObserverConfiguration( |
90 observer, BitrateConfiguration(start_bitrate_bps, min_bitrate_bps, | 81 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps))); |
91 max_bitrate_bps))); | |
92 bitrate_observers_modified_ = true; | 82 bitrate_observers_modified_ = true; |
93 | |
94 // TODO(andresp): This is a ugly way to set start bitrate. | |
95 // | |
96 // Only change start bitrate if we have exactly one observer. By definition | |
97 // you can only have one start bitrate, once we have our first estimate we | |
98 // will adapt from there. | |
99 if (bitrate_observers_.size() == 1) | |
100 new_bwe_candidate_bps = start_bitrate_bps; | |
101 } | 83 } |
102 | 84 |
103 last_bitrate_bps_ = std::max<int>(new_bwe_candidate_bps, last_bitrate_bps_); | |
104 | |
105 ObserverBitrateMap allocation = AllocateBitrates(); | 85 ObserverBitrateMap allocation = AllocateBitrates(); |
106 *new_observer_bitrate_bps = 0; | 86 int new_observer_bitrate_bps = 0; |
107 for (auto& kv : allocation) { | 87 for (auto& kv : allocation) { |
108 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); | 88 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_); |
109 if (kv.first == observer) | 89 if (kv.first == observer) |
110 *new_observer_bitrate_bps = kv.second; | 90 new_observer_bitrate_bps = kv.second; |
111 } | 91 } |
112 return last_bitrate_bps_; | 92 return new_observer_bitrate_bps; |
113 } | 93 } |
114 | 94 |
115 void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) { | 95 void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) { |
116 CriticalSectionScoped lock(crit_sect_.get()); | 96 CriticalSectionScoped lock(crit_sect_.get()); |
117 BitrateObserverConfList::iterator it = | 97 BitrateObserverConfList::iterator it = |
118 FindObserverConfigurationPair(observer); | 98 FindObserverConfigurationPair(observer); |
119 if (it != bitrate_observers_.end()) { | 99 if (it != bitrate_observers_.end()) { |
120 bitrate_observers_.erase(it); | 100 bitrate_observers_.erase(it); |
121 bitrate_observers_modified_ = true; | 101 bitrate_observers_modified_ = true; |
122 } | 102 } |
123 } | 103 } |
124 | 104 |
125 void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps, | 105 void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps, |
126 int* max_bitrate_sum_bps) const { | 106 int* max_bitrate_sum_bps) const { |
127 *min_bitrate_sum_bps = 0; | 107 *min_bitrate_sum_bps = 0; |
128 *max_bitrate_sum_bps = 0; | 108 *max_bitrate_sum_bps = 0; |
129 | 109 |
130 CriticalSectionScoped lock(crit_sect_.get()); | 110 CriticalSectionScoped lock(crit_sect_.get()); |
131 for (const auto& observer : bitrate_observers_) { | 111 for (const auto& observer : bitrate_observers_) { |
132 *min_bitrate_sum_bps += observer.second.min_bitrate_; | 112 *min_bitrate_sum_bps += observer.second.min_bitrate; |
133 *max_bitrate_sum_bps += observer.second.max_bitrate_; | 113 *max_bitrate_sum_bps += observer.second.max_bitrate; |
134 } | 114 } |
135 } | 115 } |
136 | 116 |
137 BitrateAllocator::BitrateObserverConfList::iterator | 117 BitrateAllocator::BitrateObserverConfList::iterator |
138 BitrateAllocator::FindObserverConfigurationPair( | 118 BitrateAllocator::FindObserverConfigurationPair( |
139 const BitrateObserver* observer) { | 119 const BitrateObserver* observer) { |
140 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end(); | 120 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end(); |
141 ++it) { | 121 ++it) { |
142 if (it->first == observer) | 122 if (it->first == observer) |
143 return it; | 123 return it; |
144 } | 124 } |
145 return bitrate_observers_.end(); | 125 return bitrate_observers_.end(); |
146 } | 126 } |
147 | 127 |
148 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { | 128 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { |
149 CriticalSectionScoped lock(crit_sect_.get()); | 129 CriticalSectionScoped lock(crit_sect_.get()); |
150 enforce_min_bitrate_ = enforce_min_bitrate; | 130 enforce_min_bitrate_ = enforce_min_bitrate; |
151 } | 131 } |
152 | 132 |
153 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( | 133 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( |
154 uint32_t bitrate, | 134 uint32_t bitrate, |
155 uint32_t sum_min_bitrates) { | 135 uint32_t sum_min_bitrates) { |
156 uint32_t number_of_observers = bitrate_observers_.size(); | 136 uint32_t number_of_observers = bitrate_observers_.size(); |
157 uint32_t bitrate_per_observer = | 137 uint32_t bitrate_per_observer = |
158 (bitrate - sum_min_bitrates) / number_of_observers; | 138 (bitrate - sum_min_bitrates) / number_of_observers; |
159 // Use map to sort list based on max bitrate. | 139 // Use map to sort list based on max bitrate. |
160 ObserverSortingMap list_max_bitrates; | 140 ObserverSortingMap list_max_bitrates; |
161 for (const auto& observer : bitrate_observers_) { | 141 for (const auto& observer : bitrate_observers_) { |
162 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>( | 142 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>( |
163 observer.second.max_bitrate_, | 143 observer.second.max_bitrate, |
164 ObserverConfiguration(observer.first, observer.second.min_bitrate_))); | 144 ObserverConfiguration(observer.first, observer.second.min_bitrate))); |
165 } | 145 } |
166 ObserverBitrateMap allocation; | 146 ObserverBitrateMap allocation; |
167 ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); | 147 ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); |
168 while (max_it != list_max_bitrates.end()) { | 148 while (max_it != list_max_bitrates.end()) { |
169 number_of_observers--; | 149 number_of_observers--; |
170 uint32_t observer_allowance = | 150 uint32_t observer_allowance = |
171 max_it->second.min_bitrate_ + bitrate_per_observer; | 151 max_it->second.min_bitrate + bitrate_per_observer; |
172 if (max_it->first < observer_allowance) { | 152 if (max_it->first < observer_allowance) { |
173 // We have more than enough for this observer. | 153 // We have more than enough for this observer. |
174 // Carry the remainder forward. | 154 // Carry the remainder forward. |
175 uint32_t remainder = observer_allowance - max_it->first; | 155 uint32_t remainder = observer_allowance - max_it->first; |
176 if (number_of_observers != 0) { | 156 if (number_of_observers != 0) { |
177 bitrate_per_observer += remainder / number_of_observers; | 157 bitrate_per_observer += remainder / number_of_observers; |
178 } | 158 } |
179 allocation[max_it->second.observer_] = max_it->first; | 159 allocation[max_it->second.observer] = max_it->first; |
180 } else { | 160 } else { |
181 allocation[max_it->second.observer_] = observer_allowance; | 161 allocation[max_it->second.observer] = observer_allowance; |
182 } | 162 } |
183 list_max_bitrates.erase(max_it); | 163 list_max_bitrates.erase(max_it); |
184 // Prepare next iteration. | 164 // Prepare next iteration. |
185 max_it = list_max_bitrates.begin(); | 165 max_it = list_max_bitrates.begin(); |
186 } | 166 } |
187 return allocation; | 167 return allocation; |
188 } | 168 } |
189 | 169 |
190 BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation( | 170 BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation( |
191 uint32_t bitrate) { | 171 uint32_t bitrate) { |
192 ObserverBitrateMap allocation; | 172 ObserverBitrateMap allocation; |
193 if (enforce_min_bitrate_) { | 173 if (enforce_min_bitrate_) { |
194 // Min bitrate to all observers. | 174 // Min bitrate to all observers. |
195 for (const auto& observer : bitrate_observers_) | 175 for (const auto& observer : bitrate_observers_) |
196 allocation[observer.first] = observer.second.min_bitrate_; | 176 allocation[observer.first] = observer.second.min_bitrate; |
197 } else { | 177 } else { |
198 // Allocate up to |min_bitrate_| to one observer at a time, until | 178 // Allocate up to |min_bitrate| to one observer at a time, until |
199 // |bitrate| is depleted. | 179 // |bitrate| is depleted. |
200 uint32_t remainder = bitrate; | 180 uint32_t remainder = bitrate; |
201 for (const auto& observer : bitrate_observers_) { | 181 for (const auto& observer : bitrate_observers_) { |
202 uint32_t allocated_bitrate = | 182 uint32_t allocated_bitrate = |
203 std::min(remainder, observer.second.min_bitrate_); | 183 std::min(remainder, observer.second.min_bitrate); |
204 allocation[observer.first] = allocated_bitrate; | 184 allocation[observer.first] = allocated_bitrate; |
205 remainder -= allocated_bitrate; | 185 remainder -= allocated_bitrate; |
206 } | 186 } |
207 } | 187 } |
208 return allocation; | 188 return allocation; |
209 } | 189 } |
210 } // namespace webrtc | 190 } // namespace webrtc |
OLD | NEW |