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

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: 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/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 : bitrate_observers_(), 27 : bitrate_observer_configs_(),
28 bitrate_observers_modified_(false),
29 enforce_min_bitrate_(true), 28 enforce_min_bitrate_(true),
30 last_bitrate_bps_(kDefaultBitrateBps), 29 last_bitrate_bps_(kDefaultBitrateBps),
31 last_fraction_loss_(0), 30 last_fraction_loss_(0),
32 last_rtt_(0) {} 31 last_rtt_(0) {}
33 32
34 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate, 33 uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate,
35 uint8_t fraction_loss, 34 uint8_t fraction_loss,
36 int64_t rtt) { 35 int64_t rtt) {
37 rtc::CritScope lock(&crit_sect_); 36 rtc::CritScope lock(&crit_sect_);
38 last_bitrate_bps_ = bitrate; 37 last_bitrate_bps_ = bitrate;
39 last_fraction_loss_ = fraction_loss; 38 last_fraction_loss_ = fraction_loss;
40 last_rtt_ = rtt; 39 last_rtt_ = rtt;
40
41 uint32_t allocated_bitrate_bps = 0; 41 uint32_t allocated_bitrate_bps = 0;
42 ObserverBitrateMap allocation = AllocateBitrates(); 42 ObserverAllocation allocation = AllocateBitrates();
43 for (const auto& kv : allocation) { 43 for (const auto& kv : allocation) {
44 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); 44 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
45 allocated_bitrate_bps += kv.second; 45 allocated_bitrate_bps += kv.second;
46 } 46 }
47 return allocated_bitrate_bps; 47 return allocated_bitrate_bps;
48 } 48 }
49 49
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, 50 int BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
66 uint32_t min_bitrate_bps, 51 uint32_t min_bitrate_bps,
67 uint32_t max_bitrate_bps) { 52 uint32_t max_bitrate_bps,
53 bool enforce_min_bitrate) {
68 rtc::CritScope lock(&crit_sect_); 54 rtc::CritScope lock(&crit_sect_);
55 // TODO(mflodman): Enforce this per observer.
56 EnforceMinBitrate(enforce_min_bitrate);
69 57
70 BitrateObserverConfList::iterator it = 58 ObserverConfigList::iterator it = FindObserverConfig(observer);
stefan-webrtc 2016/05/09 13:46:53 Optionally you can change this to "auto"
mflodman 2016/05/11 14:53:04 Done.
71 FindObserverConfigurationPair(observer);
72 59
73 // Allow the max bitrate to be exceeded for FEC and retransmissions. 60 // 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 61 // 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 62 // properly allocate bitrate. The allocator should instead distribute any
76 // extra bitrate after all streams have maxed out. 63 // extra bitrate after all streams have maxed out.
77 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier; 64 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier;
78 if (it != bitrate_observers_.end()) { 65 if (it != bitrate_observer_configs_.end()) {
79 // Update current configuration. 66 // Update current configuration.
80 it->second.min_bitrate = min_bitrate_bps; 67 it->min_bitrate_bps = min_bitrate_bps;
81 it->second.max_bitrate = max_bitrate_bps; 68 it->max_bitrate_bps = max_bitrate_bps;
82 } else { 69 } else {
83 // Add new settings. 70 // Add new settings.
84 bitrate_observers_.push_back(BitrateObserverConfiguration( 71 bitrate_observer_configs_.push_back(ObserverConfig(
85 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps))); 72 observer, min_bitrate_bps, max_bitrate_bps, enforce_min_bitrate));
86 bitrate_observers_modified_ = true;
87 } 73 }
88 74
89 ObserverBitrateMap allocation = AllocateBitrates(); 75 ObserverAllocation allocation = AllocateBitrates();
90 int new_observer_bitrate_bps = 0; 76 int new_observer_bitrate_bps = 0;
91 for (auto& kv : allocation) { 77 for (auto& kv : allocation) {
92 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_); 78 kv.first->OnBitrateUpdated(kv.second, last_fraction_loss_, last_rtt_);
93 if (kv.first == observer) 79 if (kv.first == observer)
94 new_observer_bitrate_bps = kv.second; 80 new_observer_bitrate_bps = kv.second;
95 } 81 }
96 return new_observer_bitrate_bps; 82 return new_observer_bitrate_bps;
97 } 83 }
98 84
99 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { 85 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
100 rtc::CritScope lock(&crit_sect_); 86 rtc::CritScope lock(&crit_sect_);
101 BitrateObserverConfList::iterator it = 87 ObserverConfigList::iterator it = FindObserverConfig(observer);
stefan-webrtc 2016/05/09 13:46:53 auto here too if you want.
mflodman 2016/05/11 14:53:04 Done.
102 FindObserverConfigurationPair(observer); 88 if (it != bitrate_observer_configs_.end()) {
103 if (it != bitrate_observers_.end()) { 89 bitrate_observer_configs_.erase(it);
104 bitrate_observers_.erase(it);
105 bitrate_observers_modified_ = true;
106 } 90 }
107 } 91 }
108 92
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) { 93 void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
121 rtc::CritScope lock(&crit_sect_);
122 enforce_min_bitrate_ = enforce_min_bitrate; 94 enforce_min_bitrate_ = enforce_min_bitrate;
123 } 95 }
124 96
125 BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation( 97 BitrateAllocator::ObserverConfigList::iterator
98 BitrateAllocator::FindObserverConfig(
99 const BitrateAllocatorObserver* observer) {
100 for (auto it = bitrate_observer_configs_.begin();
101 it != bitrate_observer_configs_.end(); ++it) {
stefan-webrtc 2016/05/09 13:46:53 for (auto it : bitrate_observer_configs_) { or au
mflodman 2016/05/11 14:53:04 Then I'll get an ObserverConfig reference instead
stefan-webrtc 2016/05/12 12:26:59 Ah, good point, letäs keep it as is.
102 if (it->observer == observer)
103 return it;
104 }
105 return bitrate_observer_configs_.end();
106 }
107
108 BitrateAllocator::ObserverAllocation BitrateAllocator::AllocateBitrates() {
109 if (bitrate_observer_configs_.empty())
110 return ObserverAllocation();
111
112 uint32_t sum_min_bitrates = 0;
113 for (const auto& observer_config : bitrate_observer_configs_)
114 sum_min_bitrates += observer_config.min_bitrate_bps;
115 if (last_bitrate_bps_ == 0)
116 return ZeroRateAllocation();
stefan-webrtc 2016/05/09 13:46:53 Move these two lines to line 111 since they don't
mflodman 2016/05/11 14:53:04 Done.
117 else if (last_bitrate_bps_ <= sum_min_bitrates)
118 return LowRateAllocation(last_bitrate_bps_);
119 else
120 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
121 }
122
123 BitrateAllocator::ObserverAllocation BitrateAllocator::NormalRateAllocation(
126 uint32_t bitrate, 124 uint32_t bitrate,
127 uint32_t sum_min_bitrates) { 125 uint32_t sum_min_bitrates) {
128 uint32_t number_of_observers = 126 uint32_t num_remaining_observers =
129 static_cast<uint32_t>(bitrate_observers_.size()); 127 static_cast<uint32_t>(bitrate_observer_configs_.size());
stefan-webrtc 2016/05/09 13:46:53 DCHECK that num_remaining_observers is greater tha
mflodman 2016/05/11 14:53:04 Done.
128
130 uint32_t bitrate_per_observer = 129 uint32_t bitrate_per_observer =
131 (bitrate - sum_min_bitrates) / number_of_observers; 130 (bitrate - sum_min_bitrates) / num_remaining_observers;
132 // Use map to sort list based on max bitrate. 131 // Use map to sort list based on max bitrate.
133 ObserverSortingMap list_max_bitrates; 132 ObserverSortingMap list_max_bitrates;
134 for (const auto& observer : bitrate_observers_) { 133 for (const auto& config : bitrate_observer_configs_) {
135 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>( 134 list_max_bitrates.insert(std::pair<uint32_t, const ObserverConfig*>(
136 observer.second.max_bitrate, 135 config.max_bitrate_bps, &config));
137 ObserverConfiguration(observer.first, observer.second.min_bitrate)));
138 } 136 }
139 ObserverBitrateMap allocation; 137
138 ObserverAllocation allocation;
140 ObserverSortingMap::iterator max_it = list_max_bitrates.begin(); 139 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
141 while (max_it != list_max_bitrates.end()) { 140 while (max_it != list_max_bitrates.end()) {
142 number_of_observers--; 141 num_remaining_observers--;
143 uint32_t observer_allowance = 142 uint32_t observer_allowance =
144 max_it->second.min_bitrate + bitrate_per_observer; 143 max_it->second->min_bitrate_bps + bitrate_per_observer;
145 if (max_it->first < observer_allowance) { 144 if (max_it->first < observer_allowance) {
146 // We have more than enough for this observer. 145 // We have more than enough for this observer.
147 // Carry the remainder forward. 146 // Carry the remainder forward.
148 uint32_t remainder = observer_allowance - max_it->first; 147 uint32_t remainder = observer_allowance - max_it->first;
149 if (number_of_observers != 0) { 148 if (num_remaining_observers != 0) {
stefan-webrtc 2016/05/09 13:46:53 Feel free to remove {}
mflodman 2016/05/11 14:53:04 Done.
150 bitrate_per_observer += remainder / number_of_observers; 149 bitrate_per_observer += remainder / num_remaining_observers;
151 } 150 }
152 allocation[max_it->second.observer] = max_it->first; 151 allocation[max_it->second->observer] = max_it->first;
153 } else { 152 } else {
154 allocation[max_it->second.observer] = observer_allowance; 153 allocation[max_it->second->observer] = observer_allowance;
155 } 154 }
156 list_max_bitrates.erase(max_it); 155 list_max_bitrates.erase(max_it);
157 // Prepare next iteration. 156 // Prepare next iteration.
158 max_it = list_max_bitrates.begin(); 157 max_it = list_max_bitrates.begin();
159 } 158 }
160 return allocation; 159 return allocation;
161 } 160 }
162 161
163 BitrateAllocator::ObserverBitrateMap BitrateAllocator::ZeroRateAllocation() { 162 BitrateAllocator::ObserverAllocation BitrateAllocator::ZeroRateAllocation() {
164 ObserverBitrateMap allocation; 163 ObserverAllocation allocation;
165 // Zero bitrate to all observers. 164 // Zero bitrate to all observers.
166 for (const auto& observer : bitrate_observers_) 165 for (const auto& observer_config : bitrate_observer_configs_)
167 allocation[observer.first] = 0; 166 allocation[observer_config.observer] = 0;
168 return allocation; 167 return allocation;
169 } 168 }
170 169
171 BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation( 170 BitrateAllocator::ObserverAllocation BitrateAllocator::LowRateAllocation(
172 uint32_t bitrate) { 171 uint32_t bitrate) {
173 ObserverBitrateMap allocation; 172 ObserverAllocation allocation;
174 if (enforce_min_bitrate_) { 173 if (enforce_min_bitrate_) {
175 // Min bitrate to all observers. 174 // Min bitrate to all observers.
176 for (const auto& observer : bitrate_observers_) 175 for (const auto& observer_config : bitrate_observer_configs_)
177 allocation[observer.first] = observer.second.min_bitrate; 176 allocation[observer_config.observer] = observer_config.min_bitrate_bps;
178 } else { 177 } else {
179 // Allocate up to |min_bitrate| to one observer at a time, until 178 // Allocate up to |min_bitrate_bps| to one observer at a time, until
180 // |bitrate| is depleted. 179 // |bitrate| is depleted.
181 uint32_t remainder = bitrate; 180 uint32_t remainder = bitrate;
182 for (const auto& observer : bitrate_observers_) { 181 for (const auto& observer_config : bitrate_observer_configs_) {
183 uint32_t allocated_bitrate = 182 uint32_t allocated_bitrate =
184 std::min(remainder, observer.second.min_bitrate); 183 std::min(remainder, observer_config.min_bitrate_bps);
185 allocation[observer.first] = allocated_bitrate; 184 allocation[observer_config.observer] = allocated_bitrate;
186 remainder -= allocated_bitrate; 185 remainder -= allocated_bitrate;
187 } 186 }
188 } 187 }
189 return allocation; 188 return allocation;
190 } 189 }
191 } // namespace webrtc 190 } // 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