Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: webrtc/call/bitrate_allocator.cc

Issue 1952923005: Refactor before implementing per stream suspension. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/call/bitrate_allocator.h ('k') | webrtc/call/bitrate_allocator_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/base/checks.h"
17 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" 18 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
18 19
19 namespace webrtc { 20 namespace webrtc {
20 21
21 // Allow packets to be transmitted in up to 2 times max video bitrate if the 22 // Allow packets to be transmitted in up to 2 times max video bitrate if the
22 // bandwidth estimate allows it. 23 // bandwidth estimate allows it.
23 const int kTransmissionMaxBitrateMultiplier = 2; 24 const int kTransmissionMaxBitrateMultiplier = 2;
24 const int kDefaultBitrateBps = 300000; 25 const int kDefaultBitrateBps = 300000;
25 26
26 BitrateAllocator::BitrateAllocator() 27 BitrateAllocator::BitrateAllocator()
27 : bitrate_observers_(), 28 : bitrate_observer_configs_(),
28 bitrate_observers_modified_(false),
29 enforce_min_bitrate_(true), 29 enforce_min_bitrate_(true),
30 last_bitrate_bps_(kDefaultBitrateBps), 30 last_bitrate_bps_(kDefaultBitrateBps),
31 last_fraction_loss_(0), 31 last_fraction_loss_(0),
32 last_rtt_(0) {} 32 last_rtt_(0) {}
33 33
34 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, 34 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate,
35 uint8_t fraction_loss, 35 uint8_t fraction_loss,
36 int64_t rtt) { 36 int64_t rtt) {
37 rtc::CritScope lock(&crit_sect_); 37 rtc::CritScope lock(&crit_sect_);
38 last_bitrate_bps_ = bitrate; 38 last_bitrate_bps_ = bitrate;
39 last_fraction_loss_ = fraction_loss; 39 last_fraction_loss_ = fraction_loss;
40 last_rtt_ = rtt; 40 last_rtt_ = rtt;
41
41 uint32_t allocated_bitrate_bps = 0; 42 uint32_t allocated_bitrate_bps = 0;
42 ObserverBitrateMap allocation = AllocateBitrates(); 43 ObserverAllocation allocation = AllocateBitrates();
43 for (const auto& kv : allocation) { 44 for (const auto& kv : allocation) {
44 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); 45 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
45 allocated_bitrate_bps += kv.second; 46 allocated_bitrate_bps += kv.second;
46 } 47 }
47 return allocated_bitrate_bps; 48 return allocated_bitrate_bps;
48 } 49 }
49 50
50 BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() {
51 if (bitrate_observers_.empty())
52 return ObserverBitrateMap();
53
54 uint32_t sum_min_bitrates = 0;
55 for (const auto& observer : bitrate_observers_)
56 sum_min_bitrates += observer.second.min_bitrate;
57 if (last_bitrate_bps_ == 0)
58 return ZeroRateAllocation();
59 else if (last_bitrate_bps_ <= sum_min_bitrates)
60 return LowRateAllocation(last_bitrate_bps_);
61 else
62 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
63 }
64
65 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, 51 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
66 uint32_t min_bitrate_bps, 52 uint32_t min_bitrate_bps,
67 uint32_t max_bitrate_bps) { 53 uint32_t max_bitrate_bps,
54 bool enforce_min_bitrate) {
68 rtc::CritScope lock(&crit_sect_); 55 rtc::CritScope lock(&crit_sect_);
56 // TODO(mflodman): Enforce this per observer.
57 EnforceMinBitrate(enforce_min_bitrate);
69 58
70 BitrateObserverConfList::iterator it = 59 auto it = FindObserverConfig(observer);
71 FindObserverConfigurationPair(observer);
72 60
73 // Allow the max bitrate to be exceeded for FEC and retransmissions. 61 // Allow the max bitrate to be exceeded for FEC and retransmissions.
74 // TODO(holmer): We have to get rid of this hack as it makes it difficult to 62 // TODO(holmer): We have to get rid of this hack as it makes it difficult to
75 // properly allocate bitrate. The allocator should instead distribute any 63 // properly allocate bitrate. The allocator should instead distribute any
76 // extra bitrate after all streams have maxed out. 64 // extra bitrate after all streams have maxed out.
77 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; 65 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier;
78 if (it != bitrate_observers_.end()) { 66 if (it != bitrate_observer_configs_.end()) {
79 // Update current configuration. 67 // Update current configuration.
80 it->second.min_bitrate = min_bitrate_bps; 68 it->min_bitrate_bps = min_bitrate_bps;
81 it->second.max_bitrate = max_bitrate_bps; 69 it->max_bitrate_bps = max_bitrate_bps;
82 } else { 70 } else {
83 // Add new settings. 71 // Add new settings.
84 bitrate_observers_.push_back(BitrateObserverConfiguration( 72 bitrate_observer_configs_.push_back(ObserverConfig(
85 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps))); 73 observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate));
86 bitrate_observers_modified_ = true;
87 } 74 }
88 75
89 ObserverBitrateMap allocation = AllocateBitrates(); 76 ObserverAllocation allocation = AllocateBitrates();
90 int new_observer_bitrate_bps = 0; 77 int new_observer_bitrate_bps = 0;
91 for (auto& kv : allocation) { 78 for (auto& kv : allocation) {
92 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); 79 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
93 if (kv.first == observer) 80 if (kv.first == observer)
94 new_observer_bitrate_bps = kv.second; 81 new_observer_bitrate_bps = kv.second;
95 } 82 }
96 return new_observer_bitrate_bps; 83 return new_observer_bitrate_bps;
97 } 84 }
98 85
99 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { 86 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
100 rtc::CritScope lock(&crit_sect_); 87 rtc::CritScope lock(&crit_sect_);
101 BitrateObserverConfList::iterator it = 88 auto it = FindObserverConfig(observer);
102 FindObserverConfigurationPair(observer); 89 if (it != bitrate_observer_configs_.end()) {
103 if (it != bitrate_observers_.end()) { 90 bitrate_observer_configs_.erase(it);
104 bitrate_observers_.erase(it);
105 bitrate_observers_modified_ = true;
106 } 91 }
107 } 92 }
108 93
109 BitrateAllocator::BitrateObserverConfList::iterator
110 BitrateAllocator::FindObserverConfigurationPair(
111 const BitrateAllocatorObserver* observer) {
112 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end();
113 ++it) {
114 if (it->first == observer)
115 return it;
116 }
117 return bitrate_observers_.end();
118 }
119
120 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) { 94 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
121 rtc::CritScope lock(&crit_sect_);
122 enforce_min_bitrate_ = enforce_min_bitrate; 95 enforce_min_bitrate_ = enforce_min_bitrate;
123 } 96 }
124 97
125 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( 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(
126 uint32_t bitrate, 126 uint32_t bitrate,
127 uint32_t sum_min_bitrates) { 127 uint32_t sum_min_bitrates) {
128 uint32_t number_of_observers = 128 uint32_t num_remaining_observers =
129 static_cast<uint32_t>(bitrate_observers_.size()); 129 static_cast<uint32_t>(bitrate_observer_configs_.size());
130 RTC_DCHECK_GT(num_remaining_observers, 0u);
131
130 uint32_t bitrate_per_observer = 132 uint32_t bitrate_per_observer =
131 (bitrate - sum_min_bitrates) / number_of_observers; 133 (bitrate - sum_min_bitrates) / num_remaining_observers;
132 // Use map to sort list based on max bitrate. 134 // Use map to sort list based on max bitrate.
133 ObserverSortingMap list_max_bitrates; 135 ObserverSortingMap list_max_bitrates;
134 for (const auto& observer : bitrate_observers_) { 136 for (const auto& config : bitrate_observer_configs_) {
135 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>( 137 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
136 observer.second.max_bitrate, 138 config.max_bitrate_bps, &config));
137 ObserverConfiguration(observer.first, observer.second.min_bitrate)));
138 } 139 }
139 ObserverBitrateMap allocation; 140
141 ObserverAllocation allocation;
140 ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); 142 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
141 while (max_it != list_max_bitrates.end()) { 143 while (max_it != list_max_bitrates.end()) {
142 number_of_observers--; 144 num_remaining_observers--;
143 uint32_t observer_allowance = 145 uint32_t observer_allowance =
144 max_it->second.min_bitrate + bitrate_per_observer; 146 max_it->second->min_bitrate_bps + bitrate_per_observer;
145 if (max_it->first < observer_allowance) { 147 if (max_it->first < observer_allowance) {
146 // We have more than enough for this observer. 148 // We have more than enough for this observer.
147 // Carry the remainder forward. 149 // Carry the remainder forward.
148 uint32_t remainder = observer_allowance - max_it->first; 150 uint32_t remainder = observer_allowance - max_it->first;
149 if (number_of_observers != 0) { 151 if (num_remaining_observers != 0)
150 bitrate_per_observer += remainder / number_of_observers; 152 bitrate_per_observer += remainder / num_remaining_observers;
151 } 153 allocation[max_it->second->observer] = max_it->first;
152 allocation[max_it->second.observer] = max_it->first;
153 } else { 154 } else {
154 allocation[max_it->second.observer] = observer_allowance; 155 allocation[max_it->second->observer] = observer_allowance;
155 } 156 }
156 list_max_bitrates.erase(max_it); 157 list_max_bitrates.erase(max_it);
157 // Prepare next iteration. 158 // Prepare next iteration.
158 max_it = list_max_bitrates.begin(); 159 max_it = list_max_bitrates.begin();
159 } 160 }
160 return allocation; 161 return allocation;
161 } 162 }
162 163
163 BitrateAllocator::ObserverBitrateMap BitrateAllocator::ZeroRateAllocation() { 164 BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
164 ObserverBitrateMap allocation; 165 ObserverAllocation allocation;
165 // Zero bitrate to all observers. 166 // Zero bitrate to all observers.
166 for (const auto& observer : bitrate_observers_) 167 for (const auto& observer_config : bitrate_observer_configs_)
167 allocation[observer.first] = 0; 168 allocation[observer_config.observer] = 0;
168 return allocation; 169 return allocation;
169 } 170 }
170 171
171 BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation( 172 BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
172 uint32_t bitrate) { 173 uint32_t bitrate) {
173 ObserverBitrateMap allocation; 174 ObserverAllocation allocation;
174 if (enforce_min_bitrate_) { 175 if (enforce_min_bitrate_) {
175 // Min bitrate to all observers. 176 // Min bitrate to all observers.
176 for (const auto& observer : bitrate_observers_) 177 for (const auto& observer_config : bitrate_observer_configs_)
177 allocation[observer.first] = observer.second.min_bitrate; 178 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
178 } else { 179 } else {
179 // Allocate up to |min_bitrate| to one observer at a time, until 180 // Allocate up to |min_bitrate_bps| to one observer at a time, until
180 // |bitrate| is depleted. 181 // |bitrate| is depleted.
181 uint32_t remainder = bitrate; 182 uint32_t remainder = bitrate;
182 for (const auto& observer : bitrate_observers_) { 183 for (const auto& observer_config : bitrate_observer_configs_) {
183 uint32_t allocated_bitrate = 184 uint32_t allocated_bitrate =
184 std::min(remainder, observer.second.min_bitrate); 185 std::min(remainder, observer_config.min_bitrate_bps);
185 allocation[observer.first] = allocated_bitrate; 186 allocation[observer_config.observer] = allocated_bitrate;
186 remainder -= allocated_bitrate; 187 remainder -= allocated_bitrate;
187 } 188 }
188 } 189 }
189 return allocation; 190 return allocation;
190 } 191 }
191 } // namespace webrtc 192 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/call/bitrate_allocator.h ('k') | webrtc/call/bitrate_allocator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698