Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/pacing/packet_router.h" |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 12 | |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <cstdint> |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 15 | #include <limits> |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame] | 16 | #include <utility> |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 17 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 20 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 21 | #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/time_utils.h" |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | constexpr int kRembSendIntervalMs = 200; |
| 31 | |
| 32 | } // namespace |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 33 | |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 34 | PacketRouter::PacketRouter() |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 35 | : last_send_module_(nullptr), |
| 36 | last_remb_time_ms_(rtc::TimeMillis()), |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 37 | last_send_bitrate_bps_(0), |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 38 | bitrate_bps_(0), |
| 39 | max_bitrate_bps_(std::numeric_limits<decltype(max_bitrate_bps_)>::max()), |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 40 | active_remb_module_(nullptr), |
erikvarga | bf5a2fc | 2017-06-16 05:02:05 -0700 | [diff] [blame] | 41 | transport_seq_(0) {} |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 42 | |
| 43 | PacketRouter::~PacketRouter() { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 44 | RTC_DCHECK(rtp_send_modules_.empty()); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 45 | RTC_DCHECK(rtcp_feedback_senders_.empty()); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 46 | RTC_DCHECK(sender_remb_candidates_.empty()); |
| 47 | RTC_DCHECK(receiver_remb_candidates_.empty()); |
| 48 | RTC_DCHECK(active_remb_module_ == nullptr); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 49 | } |
| 50 | |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 51 | void PacketRouter::AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate) { |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 52 | rtc::CritScope cs(&modules_crit_); |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 53 | RTC_DCHECK(std::find(rtp_send_modules_.begin(), rtp_send_modules_.end(), |
| 54 | rtp_module) == rtp_send_modules_.end()); |
stefan | 16b0221 | 2017-01-27 07:12:16 -0800 | [diff] [blame] | 55 | // Put modules which can use regular payload packets (over rtx) instead of |
| 56 | // padding first as it's less of a waste |
| 57 | if ((rtp_module->RtxSendStatus() & kRtxRedundantPayloads) > 0) { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 58 | rtp_send_modules_.push_front(rtp_module); |
stefan | 16b0221 | 2017-01-27 07:12:16 -0800 | [diff] [blame] | 59 | } else { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 60 | rtp_send_modules_.push_back(rtp_module); |
stefan | 16b0221 | 2017-01-27 07:12:16 -0800 | [diff] [blame] | 61 | } |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 62 | |
| 63 | if (remb_candidate) { |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 64 | AddRembModuleCandidate(rtp_module, /* media_sender = */ true); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 65 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 66 | } |
| 67 | |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 68 | void PacketRouter::RemoveSendRtpModule(RtpRtcp* rtp_module) { |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 69 | rtc::CritScope cs(&modules_crit_); |
Danil Chapovalov | db59de3 | 2019-06-26 11:06:41 +0200 | [diff] [blame] | 70 | rtp_module_cache_map_.clear(); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 71 | MaybeRemoveRembModuleCandidate(rtp_module, /* media_sender = */ true); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 72 | auto it = |
| 73 | std::find(rtp_send_modules_.begin(), rtp_send_modules_.end(), rtp_module); |
| 74 | RTC_DCHECK(it != rtp_send_modules_.end()); |
| 75 | rtp_send_modules_.erase(it); |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 76 | if (last_send_module_ == rtp_module) { |
| 77 | last_send_module_ = nullptr; |
| 78 | } |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 81 | void PacketRouter::AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender, |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 82 | bool remb_candidate) { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 83 | rtc::CritScope cs(&modules_crit_); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 84 | RTC_DCHECK(std::find(rtcp_feedback_senders_.begin(), |
| 85 | rtcp_feedback_senders_.end(), |
| 86 | rtcp_sender) == rtcp_feedback_senders_.end()); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 87 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 88 | rtcp_feedback_senders_.push_back(rtcp_sender); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 89 | |
| 90 | if (remb_candidate) { |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 91 | AddRembModuleCandidate(rtcp_sender, /* media_sender = */ false); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 92 | } |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 95 | void PacketRouter::RemoveReceiveRtpModule( |
| 96 | RtcpFeedbackSenderInterface* rtcp_sender) { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 97 | rtc::CritScope cs(&modules_crit_); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 98 | MaybeRemoveRembModuleCandidate(rtcp_sender, /* media_sender = */ false); |
| 99 | auto it = std::find(rtcp_feedback_senders_.begin(), |
| 100 | rtcp_feedback_senders_.end(), rtcp_sender); |
| 101 | RTC_DCHECK(it != rtcp_feedback_senders_.end()); |
| 102 | rtcp_feedback_senders_.erase(it); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Erik Språng | d287962 | 2019-05-10 08:29:01 -0700 | [diff] [blame] | 105 | RtpPacketSendResult PacketRouter::TimeToSendPacket( |
| 106 | uint32_t ssrc, |
| 107 | uint16_t sequence_number, |
| 108 | int64_t capture_timestamp, |
| 109 | bool retransmission, |
| 110 | const PacedPacketInfo& pacing_info) { |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 111 | rtc::CritScope cs(&modules_crit_); |
Danil Chapovalov | db59de3 | 2019-06-26 11:06:41 +0200 | [diff] [blame] | 112 | RtpRtcp* rtp_module = FindRtpModule(ssrc); |
| 113 | if (rtp_module == nullptr || !rtp_module->SendingMedia()) { |
| 114 | return RtpPacketSendResult::kPacketNotFound; |
| 115 | } |
| 116 | |
| 117 | if ((rtp_module->RtxSendStatus() & kRtxRedundantPayloads) && |
| 118 | rtp_module->HasBweExtensions()) { |
| 119 | // This is now the last module to send media, and has the desired |
| 120 | // properties needed for payload based padding. Cache it for later use. |
| 121 | last_send_module_ = rtp_module; |
| 122 | } |
| 123 | return rtp_module->TimeToSendPacket(ssrc, sequence_number, capture_timestamp, |
| 124 | retransmission, pacing_info); |
| 125 | } |
| 126 | |
| 127 | RtpRtcp* PacketRouter::FindRtpModule(uint32_t ssrc) { |
| 128 | auto it = rtp_module_cache_map_.find(ssrc); |
| 129 | if (it != rtp_module_cache_map_.end()) { |
| 130 | if (ssrc == it->second->SSRC() || ssrc == it->second->FlexfecSsrc()) { |
| 131 | return it->second; |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 132 | } |
Danil Chapovalov | db59de3 | 2019-06-26 11:06:41 +0200 | [diff] [blame] | 133 | // This entry is stale due to a changed ssrc - remove it. |
| 134 | rtp_module_cache_map_.erase(it); |
| 135 | } |
| 136 | // Slow path - find and cache matching module |
| 137 | for (RtpRtcp* rtp_module : rtp_send_modules_) { |
brandtr | 9dfff29 | 2016-11-14 05:14:50 -0800 | [diff] [blame] | 138 | if (ssrc == rtp_module->SSRC() || ssrc == rtp_module->FlexfecSsrc()) { |
Danil Chapovalov | db59de3 | 2019-06-26 11:06:41 +0200 | [diff] [blame] | 139 | rtp_module_cache_map_[ssrc] = rtp_module; |
| 140 | return rtp_module; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
Danil Chapovalov | db59de3 | 2019-06-26 11:06:41 +0200 | [diff] [blame] | 143 | return nullptr; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame] | 146 | void PacketRouter::SendPacket(std::unique_ptr<RtpPacketToSend> packet, |
| 147 | const PacedPacketInfo& cluster_info) { |
| 148 | rtc::CritScope cs(&modules_crit_); |
Erik Språng | f6468d2 | 2019-07-05 16:53:43 +0200 | [diff] [blame^] | 149 | // With the new pacer code path, transport sequence numbers are only set here, |
| 150 | // on the pacer thread. Therefore we don't need atomics/synchronization. |
| 151 | if (packet->IsExtensionReserved<TransportSequenceNumber>() && |
| 152 | packet->SetExtension<TransportSequenceNumber>(transport_seq_)) { |
| 153 | ++transport_seq_; |
| 154 | } |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame] | 155 | for (auto* rtp_module : rtp_send_modules_) { |
| 156 | if (rtp_module->TrySendPacket(packet.get(), cluster_info)) { |
| 157 | const bool can_send_padding = |
| 158 | (rtp_module->RtxSendStatus() & kRtxRedundantPayloads) && |
| 159 | rtp_module->HasBweExtensions(); |
| 160 | if (can_send_padding) { |
| 161 | // This is now the last module to send media, and has the desired |
| 162 | // properties needed for payload based padding. Cache it for later use. |
| 163 | last_send_module_ = rtp_module; |
| 164 | } |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | RTC_LOG(LS_WARNING) << "Failed to send packet, matching RTP module not found " |
| 170 | "or transport error. SSRC = " |
| 171 | << packet->Ssrc() << ", sequence number " |
| 172 | << packet->SequenceNumber(); |
| 173 | } |
| 174 | |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 175 | size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 176 | const PacedPacketInfo& pacing_info) { |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 177 | size_t total_bytes_sent = 0; |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 178 | rtc::CritScope cs(&modules_crit_); |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 179 | // First try on the last rtp module to have sent media. This increases the |
| 180 | // the chance that any payload based padding will be useful as it will be |
| 181 | // somewhat distributed over modules according the packet rate, even if it |
| 182 | // will be more skewed towards the highest bitrate stream. At the very least |
| 183 | // this prevents sending payload padding on a disabled stream where it's |
| 184 | // guaranteed not to be useful. |
| 185 | if (last_send_module_ != nullptr) { |
| 186 | RTC_DCHECK(std::find(rtp_send_modules_.begin(), rtp_send_modules_.end(), |
| 187 | last_send_module_) != rtp_send_modules_.end()); |
| 188 | RTC_DCHECK(last_send_module_->HasBweExtensions()); |
| 189 | total_bytes_sent += last_send_module_->TimeToSendPadding( |
| 190 | bytes_to_send - total_bytes_sent, pacing_info); |
| 191 | if (total_bytes_sent >= bytes_to_send) { |
| 192 | return total_bytes_sent; |
| 193 | } |
| 194 | } |
| 195 | |
stefan | 16b0221 | 2017-01-27 07:12:16 -0800 | [diff] [blame] | 196 | // Rtp modules are ordered by which stream can most benefit from padding. |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 197 | for (RtpRtcp* module : rtp_send_modules_) { |
stefan | 53b6cc3 | 2017-02-03 08:13:57 -0800 | [diff] [blame] | 198 | if (module->SendingMedia() && module->HasBweExtensions()) { |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 199 | size_t bytes_sent = module->TimeToSendPadding( |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 200 | bytes_to_send - total_bytes_sent, pacing_info); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 201 | total_bytes_sent += bytes_sent; |
| 202 | if (total_bytes_sent >= bytes_to_send) |
| 203 | break; |
| 204 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 205 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 206 | return total_bytes_sent; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 207 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 208 | |
Erik Språng | f6468d2 | 2019-07-05 16:53:43 +0200 | [diff] [blame^] | 209 | std::vector<std::unique_ptr<RtpPacketToSend>> PacketRouter::GeneratePadding( |
| 210 | size_t target_size_bytes) { |
Erik Språng | 478cb46 | 2019-06-26 15:49:27 +0200 | [diff] [blame] | 211 | rtc::CritScope cs(&modules_crit_); |
| 212 | // First try on the last rtp module to have sent media. This increases the |
| 213 | // the chance that any payload based padding will be useful as it will be |
| 214 | // somewhat distributed over modules according the packet rate, even if it |
| 215 | // will be more skewed towards the highest bitrate stream. At the very least |
| 216 | // this prevents sending payload padding on a disabled stream where it's |
| 217 | // guaranteed not to be useful. |
| 218 | if (last_send_module_ != nullptr) { |
| 219 | RTC_DCHECK(std::find(rtp_send_modules_.begin(), rtp_send_modules_.end(), |
| 220 | last_send_module_) != rtp_send_modules_.end()); |
| 221 | RTC_DCHECK(last_send_module_->HasBweExtensions()); |
Erik Språng | f6468d2 | 2019-07-05 16:53:43 +0200 | [diff] [blame^] | 222 | return last_send_module_->GeneratePadding(target_size_bytes); |
Erik Språng | 478cb46 | 2019-06-26 15:49:27 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Rtp modules are ordered by which stream can most benefit from padding. |
| 226 | for (RtpRtcp* rtp_module : rtp_send_modules_) { |
| 227 | if (rtp_module->SendingMedia() && rtp_module->HasBweExtensions()) { |
Erik Språng | f6468d2 | 2019-07-05 16:53:43 +0200 | [diff] [blame^] | 228 | return rtp_module->GeneratePadding(target_size_bytes); |
Erik Språng | 478cb46 | 2019-06-26 15:49:27 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
Erik Språng | f6468d2 | 2019-07-05 16:53:43 +0200 | [diff] [blame^] | 231 | |
| 232 | return {}; |
Erik Språng | 478cb46 | 2019-06-26 15:49:27 +0200 | [diff] [blame] | 233 | } |
| 234 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 235 | void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 236 | rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | uint16_t PacketRouter::AllocateSequenceNumber() { |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 240 | int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 241 | int desired_prev_seq; |
| 242 | int new_seq; |
| 243 | do { |
| 244 | desired_prev_seq = prev_seq; |
| 245 | new_seq = (desired_prev_seq + 1) & 0xFFFF; |
| 246 | // Note: CompareAndSwap returns the actual value of transport_seq at the |
| 247 | // time the CAS operation was executed. Thus, if prev_seq is returned, the |
| 248 | // operation was successful - otherwise we need to retry. Saving the |
| 249 | // return value saves us a load on retry. |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 250 | prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq, |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 251 | new_seq); |
| 252 | } while (prev_seq != desired_prev_seq); |
| 253 | |
| 254 | return new_seq; |
| 255 | } |
| 256 | |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 257 | void PacketRouter::OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, |
| 258 | uint32_t bitrate_bps) { |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 259 | // % threshold for if we should send a new REMB asap. |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 260 | const int64_t kSendThresholdPercent = 97; |
| 261 | // TODO(danilchap): Remove receive_bitrate_bps variable and the cast |
| 262 | // when OnReceiveBitrateChanged takes bitrate as int64_t. |
| 263 | int64_t receive_bitrate_bps = static_cast<int64_t>(bitrate_bps); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 264 | |
| 265 | int64_t now_ms = rtc::TimeMillis(); |
| 266 | { |
| 267 | rtc::CritScope lock(&remb_crit_); |
| 268 | |
| 269 | // If we already have an estimate, check if the new total estimate is below |
| 270 | // kSendThresholdPercent of the previous estimate. |
| 271 | if (last_send_bitrate_bps_ > 0) { |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 272 | int64_t new_remb_bitrate_bps = |
| 273 | last_send_bitrate_bps_ - bitrate_bps_ + receive_bitrate_bps; |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 274 | |
| 275 | if (new_remb_bitrate_bps < |
| 276 | kSendThresholdPercent * last_send_bitrate_bps_ / 100) { |
| 277 | // The new bitrate estimate is less than kSendThresholdPercent % of the |
| 278 | // last report. Send a REMB asap. |
| 279 | last_remb_time_ms_ = now_ms - kRembSendIntervalMs; |
| 280 | } |
| 281 | } |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 282 | bitrate_bps_ = receive_bitrate_bps; |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 283 | |
| 284 | if (now_ms - last_remb_time_ms_ < kRembSendIntervalMs) { |
| 285 | return; |
| 286 | } |
| 287 | // NOTE: Updated if we intend to send the data; we might not have |
| 288 | // a module to actually send it. |
| 289 | last_remb_time_ms_ = now_ms; |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 290 | last_send_bitrate_bps_ = receive_bitrate_bps; |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 291 | // Cap the value to send in remb with configured value. |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 292 | receive_bitrate_bps = std::min(receive_bitrate_bps, max_bitrate_bps_); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 293 | } |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 294 | SendRemb(receive_bitrate_bps, ssrcs); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 297 | void PacketRouter::SetMaxDesiredReceiveBitrate(int64_t bitrate_bps) { |
| 298 | RTC_DCHECK_GE(bitrate_bps, 0); |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 299 | { |
| 300 | rtc::CritScope lock(&remb_crit_); |
| 301 | max_bitrate_bps_ = bitrate_bps; |
| 302 | if (rtc::TimeMillis() - last_remb_time_ms_ < kRembSendIntervalMs && |
| 303 | last_send_bitrate_bps_ > 0 && |
| 304 | last_send_bitrate_bps_ <= max_bitrate_bps_) { |
| 305 | // Recent measured bitrate is already below the cap. |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | SendRemb(bitrate_bps, /*ssrcs=*/{}); |
| 310 | } |
| 311 | |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 312 | bool PacketRouter::SendRemb(int64_t bitrate_bps, |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 313 | const std::vector<uint32_t>& ssrcs) { |
| 314 | rtc::CritScope lock(&modules_crit_); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 315 | |
| 316 | if (!active_remb_module_) { |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 317 | return false; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 320 | // The Add* and Remove* methods above ensure that REMB is disabled on all |
| 321 | // other modules, because otherwise, they will send REMB with stale info. |
| 322 | active_remb_module_->SetRemb(bitrate_bps, ssrcs); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 323 | |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | |
| 327 | bool PacketRouter::SendTransportFeedback(rtcp::TransportFeedback* packet) { |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 328 | rtc::CritScope cs(&modules_crit_); |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 329 | // Prefer send modules. |
| 330 | for (auto* rtp_module : rtp_send_modules_) { |
| 331 | packet->SetSenderSsrc(rtp_module->SSRC()); |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 332 | if (rtp_module->SendFeedbackPacket(*packet)) { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 333 | return true; |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 334 | } |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 335 | } |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 336 | for (auto* rtcp_sender : rtcp_feedback_senders_) { |
| 337 | packet->SetSenderSsrc(rtcp_sender->SSRC()); |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 338 | if (rtcp_sender->SendFeedbackPacket(*packet)) { |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 339 | return true; |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 340 | } |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 341 | } |
sprang | 233bd87 | 2015-09-08 13:25:16 -0700 | [diff] [blame] | 342 | return false; |
| 343 | } |
| 344 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 345 | void PacketRouter::AddRembModuleCandidate( |
| 346 | RtcpFeedbackSenderInterface* candidate_module, |
| 347 | bool media_sender) { |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 348 | RTC_DCHECK(candidate_module); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 349 | std::vector<RtcpFeedbackSenderInterface*>& candidates = |
| 350 | media_sender ? sender_remb_candidates_ : receiver_remb_candidates_; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 351 | RTC_DCHECK(std::find(candidates.cbegin(), candidates.cend(), |
| 352 | candidate_module) == candidates.cend()); |
| 353 | candidates.push_back(candidate_module); |
| 354 | DetermineActiveRembModule(); |
| 355 | } |
| 356 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 357 | void PacketRouter::MaybeRemoveRembModuleCandidate( |
| 358 | RtcpFeedbackSenderInterface* candidate_module, |
| 359 | bool media_sender) { |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 360 | RTC_DCHECK(candidate_module); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 361 | std::vector<RtcpFeedbackSenderInterface*>& candidates = |
| 362 | media_sender ? sender_remb_candidates_ : receiver_remb_candidates_; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 363 | auto it = std::find(candidates.begin(), candidates.end(), candidate_module); |
| 364 | |
| 365 | if (it == candidates.end()) { |
| 366 | return; // Function called due to removal of non-REMB-candidate module. |
| 367 | } |
| 368 | |
| 369 | if (*it == active_remb_module_) { |
| 370 | UnsetActiveRembModule(); |
| 371 | } |
| 372 | candidates.erase(it); |
| 373 | DetermineActiveRembModule(); |
| 374 | } |
| 375 | |
| 376 | void PacketRouter::UnsetActiveRembModule() { |
| 377 | RTC_CHECK(active_remb_module_); |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 378 | active_remb_module_->UnsetRemb(); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 379 | active_remb_module_ = nullptr; |
| 380 | } |
| 381 | |
| 382 | void PacketRouter::DetermineActiveRembModule() { |
| 383 | // Sender modules take precedence over receiver modules, because SRs (sender |
| 384 | // reports) are sent more frequently than RR (receiver reports). |
| 385 | // When adding the first sender module, we should change the active REMB |
| 386 | // module to be that. Otherwise, we remain with the current active module. |
| 387 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 388 | RtcpFeedbackSenderInterface* new_active_remb_module; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 389 | |
| 390 | if (!sender_remb_candidates_.empty()) { |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 391 | new_active_remb_module = sender_remb_candidates_.front(); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 392 | } else if (!receiver_remb_candidates_.empty()) { |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 393 | new_active_remb_module = receiver_remb_candidates_.front(); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 394 | } else { |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 395 | new_active_remb_module = nullptr; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 398 | if (new_active_remb_module != active_remb_module_ && active_remb_module_) { |
| 399 | UnsetActiveRembModule(); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Danil Chapovalov | 51e21aa | 2017-10-10 17:46:26 +0200 | [diff] [blame] | 402 | active_remb_module_ = new_active_remb_module; |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 405 | } // namespace webrtc |