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