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