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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) | 48 BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer) |
49 : limit_observer_(limit_observer), | 49 : limit_observer_(limit_observer), |
50 bitrate_observer_configs_(), | 50 bitrate_observer_configs_(), |
51 last_bitrate_bps_(kDefaultBitrateBps), | 51 last_bitrate_bps_(kDefaultBitrateBps), |
52 last_non_zero_bitrate_bps_(kDefaultBitrateBps), | 52 last_non_zero_bitrate_bps_(kDefaultBitrateBps), |
53 last_fraction_loss_(0), | 53 last_fraction_loss_(0), |
54 last_rtt_(0), | 54 last_rtt_(0), |
55 num_pause_events_(0), | 55 num_pause_events_(0), |
56 clock_(Clock::GetRealTimeClock()), | 56 clock_(Clock::GetRealTimeClock()), |
57 last_bwe_log_time_(0) {} | 57 last_bwe_log_time_(0) { |
| 58 thread_checker_.DetachFromThread(); |
| 59 } |
58 | 60 |
59 BitrateAllocator::~BitrateAllocator() { | 61 BitrateAllocator::~BitrateAllocator() { |
60 RTC_LOGGED_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", | 62 RTC_LOGGED_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents", |
61 num_pause_events_); | 63 num_pause_events_); |
62 } | 64 } |
63 | 65 |
64 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, | 66 void BitrateAllocator::OnNetworkChanged(uint32_t target_bitrate_bps, |
65 uint8_t fraction_loss, | 67 uint8_t fraction_loss, |
66 int64_t rtt) { | 68 int64_t rtt) { |
67 rtc::CritScope lock(&crit_sect_); | 69 RTC_DCHECK_RUN_ON(&thread_checker_); |
68 last_bitrate_bps_ = target_bitrate_bps; | 70 last_bitrate_bps_ = target_bitrate_bps; |
69 last_non_zero_bitrate_bps_ = | 71 last_non_zero_bitrate_bps_ = |
70 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; | 72 target_bitrate_bps > 0 ? target_bitrate_bps : last_non_zero_bitrate_bps_; |
71 last_fraction_loss_ = fraction_loss; | 73 last_fraction_loss_ = fraction_loss; |
72 last_rtt_ = rtt; | 74 last_rtt_ = rtt; |
73 | 75 |
74 // Periodically log the incoming BWE. | 76 // Periodically log the incoming BWE. |
75 int64_t now = clock_->TimeInMilliseconds(); | 77 int64_t now = clock_->TimeInMilliseconds(); |
76 if (now > last_bwe_log_time_ + kBweLogIntervalMs) { | 78 if (now > last_bwe_log_time_ + kBweLogIntervalMs) { |
77 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; | 79 LOG(LS_INFO) << "Current BWE " << target_bitrate_bps; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); | 112 config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate); |
111 config.allocated_bitrate_bps = allocated_bitrate; | 113 config.allocated_bitrate_bps = allocated_bitrate; |
112 } | 114 } |
113 } | 115 } |
114 | 116 |
115 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, | 117 void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer, |
116 uint32_t min_bitrate_bps, | 118 uint32_t min_bitrate_bps, |
117 uint32_t max_bitrate_bps, | 119 uint32_t max_bitrate_bps, |
118 uint32_t pad_up_bitrate_bps, | 120 uint32_t pad_up_bitrate_bps, |
119 bool enforce_min_bitrate) { | 121 bool enforce_min_bitrate) { |
120 rtc::CritScope lock(&crit_sect_); | 122 RTC_DCHECK_RUN_ON(&thread_checker_); |
121 auto it = FindObserverConfig(observer); | 123 auto it = FindObserverConfig(observer); |
122 | 124 |
123 // Update settings if the observer already exists, create a new one otherwise. | 125 // Update settings if the observer already exists, create a new one otherwise. |
124 if (it != bitrate_observer_configs_.end()) { | 126 if (it != bitrate_observer_configs_.end()) { |
125 it->min_bitrate_bps = min_bitrate_bps; | 127 it->min_bitrate_bps = min_bitrate_bps; |
126 it->max_bitrate_bps = max_bitrate_bps; | 128 it->max_bitrate_bps = max_bitrate_bps; |
127 it->pad_up_bitrate_bps = pad_up_bitrate_bps; | 129 it->pad_up_bitrate_bps = pad_up_bitrate_bps; |
128 it->enforce_min_bitrate = enforce_min_bitrate; | 130 it->enforce_min_bitrate = enforce_min_bitrate; |
129 } else { | 131 } else { |
130 bitrate_observer_configs_.push_back( | 132 bitrate_observer_configs_.push_back( |
(...skipping 17 matching lines...) Expand all Loading... |
148 // Currently, an encoder is not allowed to produce frames. | 150 // Currently, an encoder is not allowed to produce frames. |
149 // But we still have to return the initial config bitrate + let the | 151 // But we still have to return the initial config bitrate + let the |
150 // observer know that it can not produce frames. | 152 // observer know that it can not produce frames. |
151 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); | 153 allocation = AllocateBitrates(last_non_zero_bitrate_bps_); |
152 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); | 154 observer->OnBitrateUpdated(0, last_fraction_loss_, last_rtt_); |
153 } | 155 } |
154 UpdateAllocationLimits(); | 156 UpdateAllocationLimits(); |
155 } | 157 } |
156 | 158 |
157 void BitrateAllocator::UpdateAllocationLimits() { | 159 void BitrateAllocator::UpdateAllocationLimits() { |
| 160 RTC_DCHECK_RUN_ON(&thread_checker_); |
158 uint32_t total_requested_padding_bitrate = 0; | 161 uint32_t total_requested_padding_bitrate = 0; |
159 uint32_t total_requested_min_bitrate = 0; | 162 uint32_t total_requested_min_bitrate = 0; |
160 | 163 |
161 { | |
162 rtc::CritScope lock(&crit_sect_); | |
163 for (const auto& config : bitrate_observer_configs_) { | 164 for (const auto& config : bitrate_observer_configs_) { |
164 if (config.enforce_min_bitrate) { | 165 if (config.enforce_min_bitrate) { |
165 total_requested_min_bitrate += config.min_bitrate_bps; | 166 total_requested_min_bitrate += config.min_bitrate_bps; |
166 } | 167 } |
167 total_requested_padding_bitrate += config.pad_up_bitrate_bps; | 168 total_requested_padding_bitrate += config.pad_up_bitrate_bps; |
168 } | 169 } |
169 } | |
170 | 170 |
171 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, | 171 limit_observer_->OnAllocationLimitsChanged(total_requested_min_bitrate, |
172 total_requested_padding_bitrate); | 172 total_requested_padding_bitrate); |
173 } | 173 } |
174 | 174 |
175 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { | 175 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) { |
176 { | 176 RTC_DCHECK_RUN_ON(&thread_checker_); |
177 rtc::CritScope lock(&crit_sect_); | 177 auto it = FindObserverConfig(observer); |
178 auto it = FindObserverConfig(observer); | 178 if (it != bitrate_observer_configs_.end()) { |
179 if (it != bitrate_observer_configs_.end()) { | 179 bitrate_observer_configs_.erase(it); |
180 bitrate_observer_configs_.erase(it); | |
181 } | 180 } |
182 } | 181 |
183 UpdateAllocationLimits(); | 182 UpdateAllocationLimits(); |
184 } | 183 } |
185 | 184 |
186 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { | 185 int BitrateAllocator::GetStartBitrate(BitrateAllocatorObserver* observer) { |
187 rtc::CritScope lock(&crit_sect_); | 186 RTC_DCHECK_RUN_ON(&thread_checker_); |
188 const auto& it = FindObserverConfig(observer); | 187 const auto& it = FindObserverConfig(observer); |
189 if (it == bitrate_observer_configs_.end()) { | 188 if (it == bitrate_observer_configs_.end()) { |
190 // This observer hasn't been added yet, just give it its fair share. | 189 // This observer hasn't been added yet, just give it its fair share. |
191 return last_non_zero_bitrate_bps_ / | 190 return last_non_zero_bitrate_bps_ / |
192 static_cast<int>((bitrate_observer_configs_.size() + 1)); | 191 static_cast<int>((bitrate_observer_configs_.size() + 1)); |
193 } else if (it->allocated_bitrate_bps == -1) { | 192 } else if (it->allocated_bitrate_bps == -1) { |
194 // This observer hasn't received an allocation yet, so do the same. | 193 // This observer hasn't received an allocation yet, so do the same. |
195 return last_non_zero_bitrate_bps_ / | 194 return last_non_zero_bitrate_bps_ / |
196 static_cast<int>(bitrate_observer_configs_.size()); | 195 static_cast<int>(bitrate_observer_configs_.size()); |
197 } else { | 196 } else { |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / | 399 uint32_t extra_bitrate_per_observer = (bitrate - sum_min_bitrates) / |
401 static_cast<uint32_t>(bitrate_observer_configs_.size()); | 400 static_cast<uint32_t>(bitrate_observer_configs_.size()); |
402 for (const auto& observer_config : bitrate_observer_configs_) { | 401 for (const auto& observer_config : bitrate_observer_configs_) { |
403 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < | 402 if (observer_config.min_bitrate_bps + extra_bitrate_per_observer < |
404 MinBitrateWithHysteresis(observer_config)) | 403 MinBitrateWithHysteresis(observer_config)) |
405 return false; | 404 return false; |
406 } | 405 } |
407 return true; | 406 return true; |
408 } | 407 } |
409 } // namespace webrtc | 408 } // namespace webrtc |
OLD | NEW |