nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Sebastian Jansson | 91bb667 | 2018-02-21 13:02:51 +0100 | [diff] [blame] | 10 | #include <utility> |
nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 11 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "call/rtp_transport_controller_send.h" |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 13 | #include "modules/congestion_controller/include/send_side_congestion_controller.h" |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 14 | #include "modules/congestion_controller/rtp/include/send_side_congestion_controller.h" |
Sebastian Jansson | c33c0fc | 2018-02-22 11:10:18 +0100 | [diff] [blame] | 15 | #include "rtc_base/location.h" |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 16 | #include "rtc_base/logging.h" |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 17 | #include "rtc_base/ptr_util.h" |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 18 | #include "system_wrappers/include/field_trial.h" |
nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 21 | namespace { |
| 22 | const char kTaskQueueExperiment[] = "WebRTC-TaskQueueCongestionControl"; |
| 23 | using TaskQueueController = webrtc::webrtc_cc::SendSideCongestionController; |
| 24 | |
| 25 | bool TaskQueueExperimentEnabled() { |
| 26 | std::string trial = webrtc::field_trial::FindFullName(kTaskQueueExperiment); |
| 27 | return trial.find("Enable") == 0; |
| 28 | } |
| 29 | |
| 30 | std::unique_ptr<SendSideCongestionControllerInterface> CreateController( |
| 31 | Clock* clock, |
| 32 | webrtc::RtcEventLog* event_log, |
| 33 | PacedSender* pacer, |
| 34 | const BitrateConstraints& bitrate_config, |
| 35 | bool task_queue_controller) { |
| 36 | if (task_queue_controller) { |
Sebastian Jansson | 4e140da | 2018-04-13 13:25:19 +0200 | [diff] [blame^] | 37 | RTC_LOG(LS_INFO) << "Using TaskQueue based SSCC"; |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 38 | return rtc::MakeUnique<webrtc::webrtc_cc::SendSideCongestionController>( |
| 39 | clock, event_log, pacer, bitrate_config.start_bitrate_bps, |
| 40 | bitrate_config.min_bitrate_bps, bitrate_config.max_bitrate_bps); |
| 41 | } |
Sebastian Jansson | 4e140da | 2018-04-13 13:25:19 +0200 | [diff] [blame^] | 42 | RTC_LOG(LS_INFO) << "Using Legacy SSCC"; |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 43 | auto cc = rtc::MakeUnique<webrtc::SendSideCongestionController>( |
| 44 | clock, nullptr /* observer */, event_log, pacer); |
| 45 | cc->SignalNetworkState(kNetworkDown); |
| 46 | cc->SetBweBitrates(bitrate_config.min_bitrate_bps, |
| 47 | bitrate_config.start_bitrate_bps, |
| 48 | bitrate_config.max_bitrate_bps); |
| 49 | return std::move(cc); |
| 50 | } |
| 51 | } // namespace |
nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 52 | |
| 53 | RtpTransportControllerSend::RtpTransportControllerSend( |
| 54 | Clock* clock, |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 55 | webrtc::RtcEventLog* event_log, |
| 56 | const BitrateConstraints& bitrate_config) |
Sebastian Jansson | 19704ec | 2018-03-12 15:59:12 +0100 | [diff] [blame] | 57 | : clock_(clock), |
| 58 | pacer_(clock, &packet_router_, event_log), |
Sebastian Jansson | 317a522 | 2018-03-16 15:36:37 +0100 | [diff] [blame] | 59 | bitrate_configurator_(bitrate_config), |
| 60 | process_thread_(ProcessThread::Create("SendControllerThread")), |
| 61 | observer_(nullptr), |
Sebastian Jansson | 19bea51 | 2018-03-13 19:07:46 +0100 | [diff] [blame] | 62 | send_side_cc_(CreateController(clock, |
| 63 | event_log, |
| 64 | &pacer_, |
| 65 | bitrate_config, |
Sebastian Jansson | 317a522 | 2018-03-16 15:36:37 +0100 | [diff] [blame] | 66 | TaskQueueExperimentEnabled())) { |
Sebastian Jansson | 947120f | 2018-03-26 14:56:37 +0200 | [diff] [blame] | 67 | send_side_cc_ptr_ = send_side_cc_.get(); |
Sebastian Jansson | c33c0fc | 2018-02-22 11:10:18 +0100 | [diff] [blame] | 68 | process_thread_->RegisterModule(&pacer_, RTC_FROM_HERE); |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 69 | process_thread_->RegisterModule(send_side_cc_.get(), RTC_FROM_HERE); |
Sebastian Jansson | c33c0fc | 2018-02-22 11:10:18 +0100 | [diff] [blame] | 70 | process_thread_->Start(); |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 71 | } |
Sebastian Jansson | c33c0fc | 2018-02-22 11:10:18 +0100 | [diff] [blame] | 72 | |
| 73 | RtpTransportControllerSend::~RtpTransportControllerSend() { |
| 74 | process_thread_->Stop(); |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 75 | process_thread_->DeRegisterModule(send_side_cc_.get()); |
Sebastian Jansson | c33c0fc | 2018-02-22 11:10:18 +0100 | [diff] [blame] | 76 | process_thread_->DeRegisterModule(&pacer_); |
| 77 | } |
nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 78 | |
Sebastian Jansson | 19704ec | 2018-03-12 15:59:12 +0100 | [diff] [blame] | 79 | void RtpTransportControllerSend::OnNetworkChanged(uint32_t bitrate_bps, |
| 80 | uint8_t fraction_loss, |
| 81 | int64_t rtt_ms, |
| 82 | int64_t probing_interval_ms) { |
| 83 | // TODO(srte): Skip this step when old SendSideCongestionController is |
| 84 | // deprecated. |
| 85 | TargetTransferRate msg; |
| 86 | msg.at_time = Timestamp::ms(clock_->TimeInMilliseconds()); |
| 87 | msg.target_rate = DataRate::bps(bitrate_bps); |
| 88 | msg.network_estimate.at_time = msg.at_time; |
| 89 | msg.network_estimate.bwe_period = TimeDelta::ms(probing_interval_ms); |
| 90 | uint32_t bandwidth_bps; |
Sebastian Jansson | 947120f | 2018-03-26 14:56:37 +0200 | [diff] [blame] | 91 | if (send_side_cc_ptr_->AvailableBandwidth(&bandwidth_bps)) |
Sebastian Jansson | 19704ec | 2018-03-12 15:59:12 +0100 | [diff] [blame] | 92 | msg.network_estimate.bandwidth = DataRate::bps(bandwidth_bps); |
| 93 | msg.network_estimate.loss_rate_ratio = fraction_loss / 255.0; |
| 94 | msg.network_estimate.round_trip_time = TimeDelta::ms(rtt_ms); |
| 95 | rtc::CritScope cs(&observer_crit_); |
| 96 | // We wont register as observer until we have an observer. |
| 97 | RTC_DCHECK(observer_ != nullptr); |
| 98 | observer_->OnTargetTransferRate(msg); |
| 99 | } |
| 100 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 101 | PacketRouter* RtpTransportControllerSend::packet_router() { |
| 102 | return &packet_router_; |
| 103 | } |
| 104 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 105 | TransportFeedbackObserver* |
| 106 | RtpTransportControllerSend::transport_feedback_observer() { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 107 | return send_side_cc_.get(); |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | RtpPacketSender* RtpTransportControllerSend::packet_sender() { |
Stefan Holmer | 5c8942a | 2017-08-22 16:16:44 +0200 | [diff] [blame] | 111 | return &pacer_; |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 112 | } |
| 113 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 114 | const RtpKeepAliveConfig& RtpTransportControllerSend::keepalive_config() const { |
| 115 | return keepalive_; |
| 116 | } |
| 117 | |
Stefan Holmer | 5c8942a | 2017-08-22 16:16:44 +0200 | [diff] [blame] | 118 | void RtpTransportControllerSend::SetAllocatedSendBitrateLimits( |
| 119 | int min_send_bitrate_bps, |
philipel | 832b1c8 | 2018-02-28 17:04:18 +0100 | [diff] [blame] | 120 | int max_padding_bitrate_bps, |
philipel | db4fa4b | 2018-03-06 18:29:22 +0100 | [diff] [blame] | 121 | int max_total_bitrate_bps) { |
Sebastian Jansson | 68ee465 | 2018-03-13 11:40:34 +0100 | [diff] [blame] | 122 | send_side_cc_->SetAllocatedSendBitrateLimits( |
| 123 | min_send_bitrate_bps, max_padding_bitrate_bps, max_total_bitrate_bps); |
Stefan Holmer | 5c8942a | 2017-08-22 16:16:44 +0200 | [diff] [blame] | 124 | } |
| 125 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 126 | void RtpTransportControllerSend::SetKeepAliveConfig( |
| 127 | const RtpKeepAliveConfig& config) { |
| 128 | keepalive_ = config; |
| 129 | } |
Sebastian Jansson | 4c1ffb8 | 2018-02-15 16:51:58 +0100 | [diff] [blame] | 130 | void RtpTransportControllerSend::SetPacingFactor(float pacing_factor) { |
Sebastian Jansson | 68ee465 | 2018-03-13 11:40:34 +0100 | [diff] [blame] | 131 | send_side_cc_->SetPacingFactor(pacing_factor); |
Sebastian Jansson | 4c1ffb8 | 2018-02-15 16:51:58 +0100 | [diff] [blame] | 132 | } |
| 133 | void RtpTransportControllerSend::SetQueueTimeLimit(int limit_ms) { |
| 134 | pacer_.SetQueueTimeLimit(limit_ms); |
| 135 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 136 | CallStatsObserver* RtpTransportControllerSend::GetCallStatsObserver() { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 137 | return send_side_cc_.get(); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 138 | } |
| 139 | void RtpTransportControllerSend::RegisterPacketFeedbackObserver( |
| 140 | PacketFeedbackObserver* observer) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 141 | send_side_cc_->RegisterPacketFeedbackObserver(observer); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 142 | } |
| 143 | void RtpTransportControllerSend::DeRegisterPacketFeedbackObserver( |
| 144 | PacketFeedbackObserver* observer) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 145 | send_side_cc_->DeRegisterPacketFeedbackObserver(observer); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 146 | } |
Sebastian Jansson | 19704ec | 2018-03-12 15:59:12 +0100 | [diff] [blame] | 147 | |
| 148 | void RtpTransportControllerSend::RegisterTargetTransferRateObserver( |
| 149 | TargetTransferRateObserver* observer) { |
| 150 | { |
| 151 | rtc::CritScope cs(&observer_crit_); |
| 152 | RTC_DCHECK(observer_ == nullptr); |
| 153 | observer_ = observer; |
| 154 | } |
| 155 | send_side_cc_->RegisterNetworkObserver(this); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 156 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 157 | void RtpTransportControllerSend::OnNetworkRouteChanged( |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 158 | const std::string& transport_name, |
| 159 | const rtc::NetworkRoute& network_route) { |
Sebastian Jansson | 91bb667 | 2018-02-21 13:02:51 +0100 | [diff] [blame] | 160 | // Check if the network route is connected. |
| 161 | if (!network_route.connected) { |
| 162 | RTC_LOG(LS_INFO) << "Transport " << transport_name << " is disconnected"; |
| 163 | // TODO(honghaiz): Perhaps handle this in SignalChannelNetworkState and |
| 164 | // consider merging these two methods. |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Check whether the network route has changed on each transport. |
| 169 | auto result = |
| 170 | network_routes_.insert(std::make_pair(transport_name, network_route)); |
| 171 | auto kv = result.first; |
| 172 | bool inserted = result.second; |
| 173 | if (inserted) { |
| 174 | // No need to reset BWE if this is the first time the network connects. |
| 175 | return; |
| 176 | } |
| 177 | if (kv->second != network_route) { |
| 178 | kv->second = network_route; |
| 179 | BitrateConstraints bitrate_config = bitrate_configurator_.GetConfig(); |
| 180 | RTC_LOG(LS_INFO) << "Network route changed on transport " << transport_name |
| 181 | << ": new local network id " |
| 182 | << network_route.local_network_id |
| 183 | << " new remote network id " |
| 184 | << network_route.remote_network_id |
| 185 | << " Reset bitrates to min: " |
| 186 | << bitrate_config.min_bitrate_bps |
| 187 | << " bps, start: " << bitrate_config.start_bitrate_bps |
| 188 | << " bps, max: " << bitrate_config.max_bitrate_bps |
| 189 | << " bps."; |
| 190 | RTC_DCHECK_GT(bitrate_config.start_bitrate_bps, 0); |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 191 | send_side_cc_->OnNetworkRouteChanged( |
Sebastian Jansson | 91bb667 | 2018-02-21 13:02:51 +0100 | [diff] [blame] | 192 | network_route, bitrate_config.start_bitrate_bps, |
| 193 | bitrate_config.min_bitrate_bps, bitrate_config.max_bitrate_bps); |
| 194 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 195 | } |
| 196 | void RtpTransportControllerSend::OnNetworkAvailability(bool network_available) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 197 | send_side_cc_->SignalNetworkState(network_available ? kNetworkUp |
| 198 | : kNetworkDown); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 199 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 200 | RtcpBandwidthObserver* RtpTransportControllerSend::GetBandwidthObserver() { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 201 | return send_side_cc_->GetBandwidthObserver(); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 202 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 203 | int64_t RtpTransportControllerSend::GetPacerQueuingDelayMs() const { |
Sebastian Jansson | a06e919 | 2018-03-07 18:49:55 +0100 | [diff] [blame] | 204 | return pacer_.QueueInMs(); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 205 | } |
| 206 | int64_t RtpTransportControllerSend::GetFirstPacketTimeMs() const { |
Sebastian Jansson | a06e919 | 2018-03-07 18:49:55 +0100 | [diff] [blame] | 207 | return pacer_.FirstSentPacketTimeMs(); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 208 | } |
Sebastian Jansson | 12130bb | 2018-03-21 12:48:43 +0100 | [diff] [blame] | 209 | void RtpTransportControllerSend::SetPerPacketFeedbackAvailable(bool available) { |
| 210 | send_side_cc_->SetPerPacketFeedbackAvailable(available); |
| 211 | } |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 212 | void RtpTransportControllerSend::EnablePeriodicAlrProbing(bool enable) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 213 | send_side_cc_->EnablePeriodicAlrProbing(enable); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 214 | } |
| 215 | void RtpTransportControllerSend::OnSentPacket( |
| 216 | const rtc::SentPacket& sent_packet) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 217 | send_side_cc_->OnSentPacket(sent_packet); |
Sebastian Jansson | e4be6da | 2018-02-15 16:51:41 +0100 | [diff] [blame] | 218 | } |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 219 | |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 220 | void RtpTransportControllerSend::SetSdpBitrateParameters( |
| 221 | const BitrateConstraints& constraints) { |
| 222 | rtc::Optional<BitrateConstraints> updated = |
| 223 | bitrate_configurator_.UpdateWithSdpParameters(constraints); |
| 224 | if (updated.has_value()) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 225 | send_side_cc_->SetBweBitrates(updated->min_bitrate_bps, |
| 226 | updated->start_bitrate_bps, |
| 227 | updated->max_bitrate_bps); |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 228 | } else { |
| 229 | RTC_LOG(LS_VERBOSE) |
Sebastian Jansson | 8f83b42 | 2018-02-21 13:07:13 +0100 | [diff] [blame] | 230 | << "WebRTC.RtpTransportControllerSend.SetSdpBitrateParameters: " |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 231 | << "nothing to update"; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void RtpTransportControllerSend::SetClientBitratePreferences( |
| 236 | const BitrateConstraintsMask& preferences) { |
| 237 | rtc::Optional<BitrateConstraints> updated = |
| 238 | bitrate_configurator_.UpdateWithClientPreferences(preferences); |
| 239 | if (updated.has_value()) { |
Sebastian Jansson | 10211e9 | 2018-02-28 16:48:26 +0100 | [diff] [blame] | 240 | send_side_cc_->SetBweBitrates(updated->min_bitrate_bps, |
| 241 | updated->start_bitrate_bps, |
| 242 | updated->max_bitrate_bps); |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 243 | } else { |
| 244 | RTC_LOG(LS_VERBOSE) |
Sebastian Jansson | 8f83b42 | 2018-02-21 13:07:13 +0100 | [diff] [blame] | 245 | << "WebRTC.RtpTransportControllerSend.SetClientBitratePreferences: " |
Sebastian Jansson | 97f61ea | 2018-02-21 13:01:55 +0100 | [diff] [blame] | 246 | << "nothing to update"; |
| 247 | } |
| 248 | } |
nisse | cae45d0 | 2017-04-24 05:53:20 -0700 | [diff] [blame] | 249 | } // namespace webrtc |