OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 #include "webrtc/call/congestion_controller.h" | 11 #include "webrtc/call/congestion_controller.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
18 #include "webrtc/base/socket.h" | 18 #include "webrtc/base/socket.h" |
19 #include "webrtc/base/thread_annotations.h" | 19 #include "webrtc/base/thread_annotations.h" |
20 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 20 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
21 #include "webrtc/modules/pacing/paced_sender.h" | 21 #include "webrtc/modules/pacing/paced_sender.h" |
22 #include "webrtc/modules/pacing/packet_router.h" | 22 #include "webrtc/modules/pacing/packet_router.h" |
23 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 23 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
24 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s end_time.h" | 24 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s end_time.h" |
25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl e_stream.h" | 25 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl e_stream.h" |
26 #include "webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h" | 26 #include "webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h" |
27 #include "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h" | 27 #include "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h" |
28 #include "webrtc/modules/utility/include/process_thread.h" | 28 #include "webrtc/modules/utility/include/process_thread.h" |
29 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 29 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
30 #include "webrtc/video/call_stats.h" | |
31 #include "webrtc/video/payload_router.h" | 30 #include "webrtc/video/payload_router.h" |
32 | 31 |
33 namespace webrtc { | 32 namespace webrtc { |
34 namespace { | 33 namespace { |
35 | 34 |
36 static const uint32_t kTimeOffsetSwitchThreshold = 30; | 35 static const uint32_t kTimeOffsetSwitchThreshold = 30; |
37 | 36 |
38 class WrappingBitrateEstimator : public RemoteBitrateEstimator { | 37 class WrappingBitrateEstimator : public RemoteBitrateEstimator { |
39 public: | 38 public: |
40 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) | 39 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 uint32_t packets_since_absolute_send_time_; | 132 uint32_t packets_since_absolute_send_time_; |
134 int min_bitrate_bps_; | 133 int min_bitrate_bps_; |
135 | 134 |
136 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); | 135 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); |
137 }; | 136 }; |
138 | 137 |
139 } // namespace | 138 } // namespace |
140 | 139 |
141 CongestionController::CongestionController( | 140 CongestionController::CongestionController( |
142 Clock* clock, | 141 Clock* clock, |
143 ProcessThread* process_thread, | |
144 CallStats* call_stats, | |
145 BitrateObserver* bitrate_observer, | 142 BitrateObserver* bitrate_observer, |
146 RemoteBitrateObserver* remote_bitrate_observer) | 143 RemoteBitrateObserver* remote_bitrate_observer) |
147 : clock_(clock), | 144 : clock_(clock), |
148 packet_router_(new PacketRouter()), | 145 packet_router_(new PacketRouter()), |
149 pacer_(new PacedSender(clock_, | 146 pacer_(new PacedSender(clock_, |
150 packet_router_.get(), | 147 packet_router_.get(), |
151 BitrateController::kDefaultStartBitrateKbps, | 148 BitrateController::kDefaultStartBitrateKbps, |
152 PacedSender::kDefaultPaceMultiplier * | 149 PacedSender::kDefaultPaceMultiplier * |
153 BitrateController::kDefaultStartBitrateKbps, | 150 BitrateController::kDefaultStartBitrateKbps, |
154 0)), | 151 0)), |
155 remote_bitrate_estimator_( | 152 remote_bitrate_estimator_( |
156 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), | 153 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
157 remote_estimator_proxy_( | 154 remote_estimator_proxy_( |
158 new RemoteEstimatorProxy(clock_, packet_router_.get())), | 155 new RemoteEstimatorProxy(clock_, packet_router_.get())), |
159 process_thread_(process_thread), | |
160 call_stats_(call_stats), | |
161 pacer_thread_(ProcessThread::Create("PacerThread")), | 156 pacer_thread_(ProcessThread::Create("PacerThread")), |
162 // Constructed last as this object calls the provided callback on | 157 // Constructed last as this object calls the provided callback on |
163 // construction. | 158 // construction. |
164 bitrate_controller_( | 159 bitrate_controller_( |
165 BitrateController::CreateBitrateController(clock_, bitrate_observer)), | 160 BitrateController::CreateBitrateController(clock_, bitrate_observer)), |
166 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) { | 161 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) { |
167 call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get()); | |
168 | |
169 pacer_thread_->RegisterModule(pacer_.get()); | 162 pacer_thread_->RegisterModule(pacer_.get()); |
170 pacer_thread_->RegisterModule(remote_estimator_proxy_.get()); | 163 pacer_thread_->RegisterModule(remote_estimator_proxy_.get()); |
171 pacer_thread_->Start(); | 164 pacer_thread_->Start(); |
172 | |
173 process_thread_->RegisterModule(remote_bitrate_estimator_.get()); | |
174 process_thread_->RegisterModule(bitrate_controller_.get()); | |
175 } | 165 } |
176 | 166 |
177 CongestionController::~CongestionController() { | 167 CongestionController::~CongestionController() { |
178 pacer_thread_->Stop(); | 168 pacer_thread_->Stop(); |
179 pacer_thread_->DeRegisterModule(pacer_.get()); | 169 pacer_thread_->DeRegisterModule(pacer_.get()); |
180 pacer_thread_->DeRegisterModule(remote_estimator_proxy_.get()); | 170 pacer_thread_->DeRegisterModule(remote_estimator_proxy_.get()); |
181 process_thread_->DeRegisterModule(bitrate_controller_.get()); | |
182 process_thread_->DeRegisterModule(remote_bitrate_estimator_.get()); | |
183 call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get()); | |
184 if (transport_feedback_adapter_.get()) | |
185 call_stats_->DeregisterStatsObserver(transport_feedback_adapter_.get()); | |
186 } | 171 } |
187 | 172 |
188 | 173 |
189 void CongestionController::SetBweBitrates(int min_bitrate_bps, | 174 void CongestionController::SetBweBitrates(int min_bitrate_bps, |
190 int start_bitrate_bps, | 175 int start_bitrate_bps, |
191 int max_bitrate_bps) { | 176 int max_bitrate_bps) { |
177 RTC_DCHECK(config_thread_checker_.CalledOnValidThread()); | |
192 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, | 178 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, |
193 // and that we don't try to set the min bitrate to 0 from any applications. | 179 // and that we don't try to set the min bitrate to 0 from any applications. |
194 // The congestion controller should allow a min bitrate of 0. | 180 // The congestion controller should allow a min bitrate of 0. |
195 const int kMinBitrateBps = 10000; | 181 const int kMinBitrateBps = 10000; |
196 if (min_bitrate_bps < kMinBitrateBps) | 182 if (min_bitrate_bps < kMinBitrateBps) |
197 min_bitrate_bps = kMinBitrateBps; | 183 min_bitrate_bps = kMinBitrateBps; |
198 if (max_bitrate_bps > 0) | 184 if (max_bitrate_bps > 0) |
199 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); | 185 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); |
200 if (start_bitrate_bps > 0) { | 186 if (start_bitrate_bps > 0) { |
201 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); | 187 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); |
202 bitrate_controller_->SetStartBitrate(start_bitrate_bps); | 188 bitrate_controller_->SetStartBitrate(start_bitrate_bps); |
203 } | 189 } |
204 bitrate_controller_->SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps); | 190 bitrate_controller_->SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps); |
205 if (remote_bitrate_estimator_.get()) | 191 if (remote_bitrate_estimator_) |
206 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); | 192 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
207 if (transport_feedback_adapter_.get()) | 193 min_bitrate_bps_ = min_bitrate_bps; |
194 | |
195 rtc::CritScope lock(&crit_); | |
196 if (transport_feedback_adapter_) | |
208 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( | 197 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( |
209 min_bitrate_bps); | 198 min_bitrate_bps); |
210 min_bitrate_bps_ = min_bitrate_bps; | |
211 } | 199 } |
212 | 200 |
213 BitrateController* CongestionController::GetBitrateController() const { | 201 BitrateController* CongestionController::GetBitrateController() const { |
214 return bitrate_controller_.get(); | 202 return bitrate_controller_.get(); |
215 } | 203 } |
216 | 204 |
217 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( | 205 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( |
218 bool send_side_bwe) const { | 206 bool send_side_bwe) const { |
219 | |
220 if (send_side_bwe) | 207 if (send_side_bwe) |
221 return remote_estimator_proxy_.get(); | 208 return remote_estimator_proxy_.get(); |
222 else | 209 else |
223 return remote_bitrate_estimator_.get(); | 210 return remote_bitrate_estimator_.get(); |
224 } | 211 } |
225 | 212 |
226 TransportFeedbackObserver* | 213 TransportFeedbackObserver* |
the sun
2016/02/17 12:47:26
So, you may return a pointer to the TFA on one thr
stefan-webrtc
2016/02/17 13:32:48
Yes, it turned out to be fairly easy to make that
| |
227 CongestionController::GetTransportFeedbackObserver() { | 214 CongestionController::GetTransportFeedbackObserver() { |
215 RTC_DCHECK(config_thread_checker_.CalledOnValidThread()); | |
216 rtc::CritScope lock(&crit_); | |
228 if (transport_feedback_adapter_.get() == nullptr) { | 217 if (transport_feedback_adapter_.get() == nullptr) { |
pbos-webrtc
2016/02/17 12:17:12
!transport_feedback_adapter_
stefan-webrtc
2016/02/17 13:32:48
Done.
| |
229 transport_feedback_adapter_.reset(new TransportFeedbackAdapter( | 218 transport_feedback_adapter_.reset( |
230 bitrate_controller_.get(), clock_, process_thread_)); | 219 new TransportFeedbackAdapter(bitrate_controller_.get(), clock_)); |
231 transport_feedback_adapter_->SetBitrateEstimator( | 220 transport_feedback_adapter_->SetBitrateEstimator( |
232 new RemoteBitrateEstimatorAbsSendTime(transport_feedback_adapter_.get(), | 221 new RemoteBitrateEstimatorAbsSendTime(transport_feedback_adapter_.get(), |
233 clock_)); | 222 clock_)); |
234 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( | 223 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( |
235 min_bitrate_bps_); | 224 min_bitrate_bps_); |
236 call_stats_->RegisterStatsObserver(transport_feedback_adapter_.get()); | |
237 } | 225 } |
238 return transport_feedback_adapter_.get(); | 226 return transport_feedback_adapter_.get(); |
239 } | 227 } |
240 | 228 |
241 void CongestionController::UpdatePacerBitrate(int bitrate_kbps, | 229 void CongestionController::UpdatePacerBitrate(int bitrate_kbps, |
242 int max_bitrate_kbps, | 230 int max_bitrate_kbps, |
243 int min_bitrate_kbps) { | 231 int min_bitrate_kbps) { |
244 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps); | 232 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps); |
245 } | 233 } |
246 | 234 |
247 int64_t CongestionController::GetPacerQueuingDelayMs() const { | 235 int64_t CongestionController::GetPacerQueuingDelayMs() const { |
248 return pacer_->QueueInMs(); | 236 return pacer_->QueueInMs(); |
249 } | 237 } |
250 | 238 |
251 void CongestionController::SignalNetworkState(NetworkState state) { | 239 void CongestionController::SignalNetworkState(NetworkState state) { |
252 if (state == kNetworkUp) { | 240 if (state == kNetworkUp) { |
253 pacer_->Resume(); | 241 pacer_->Resume(); |
254 } else { | 242 } else { |
255 pacer_->Pause(); | 243 pacer_->Pause(); |
256 } | 244 } |
257 } | 245 } |
258 | 246 |
259 void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) { | 247 void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) { |
248 rtc::CritScope lock(&crit_); | |
260 if (transport_feedback_adapter_) { | 249 if (transport_feedback_adapter_) { |
261 transport_feedback_adapter_->OnSentPacket(sent_packet.packet_id, | 250 transport_feedback_adapter_->OnSentPacket(sent_packet.packet_id, |
262 sent_packet.send_time_ms); | 251 sent_packet.send_time_ms); |
263 } | 252 } |
264 } | 253 } |
254 | |
255 void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { | |
256 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); | |
257 | |
258 rtc::CritScope lock(&crit_); | |
259 if (transport_feedback_adapter_) | |
260 transport_feedback_adapter_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); | |
261 } | |
262 | |
263 int64_t CongestionController::TimeUntilNextProcess() { | |
264 int64_t time_until_process = | |
265 std::min(bitrate_controller_->TimeUntilNextProcess(), | |
266 remote_bitrate_estimator_->TimeUntilNextProcess()); | |
267 rtc::CritScope lock(&crit_); | |
268 if (transport_feedback_adapter_) { | |
269 time_until_process = | |
270 std::min(transport_feedback_adapter_->GetBitrateEstimator() | |
271 ->TimeUntilNextProcess(), | |
272 time_until_process); | |
273 } | |
274 return time_until_process; | |
275 } | |
276 | |
277 int32_t CongestionController::Process() { | |
278 bitrate_controller_->Process(); | |
279 remote_bitrate_estimator_->Process(); | |
280 rtc::CritScope lock(&crit_); | |
281 if (transport_feedback_adapter_) | |
282 transport_feedback_adapter_->GetBitrateEstimator()->Process(); | |
283 return 0; | |
284 } | |
285 | |
265 } // namespace webrtc | 286 } // namespace webrtc |
OLD | NEW |