blob: 7492f1309e2295829feec143cdb1581e0b1536b5 [file] [log] [blame]
Stefan Holmere5904162015-03-26 11:11:06 +01001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/pacing/packet_router.h"
Stefan Holmere5904162015-03-26 11:11:06 +010012
danilchap47085372017-08-10 06:03:57 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
danilchap47085372017-08-10 06:03:57 -070015#include <limits>
Erik Språng58ee1872019-06-18 16:20:11 +020016#include <utility>
danilchap47085372017-08-10 06:03:57 -070017
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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 Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
Erik Språng58ee1872019-06-18 16:20:11 +020024#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/time_utils.h"
Stefan Holmere5904162015-03-26 11:11:06 +010026
27namespace webrtc {
danilchap47085372017-08-10 06:03:57 -070028namespace {
29
30constexpr int kRembSendIntervalMs = 200;
31
32} // namespace
Stefan Holmere5904162015-03-26 11:11:06 +010033
nisse05843312017-04-18 23:38:35 -070034PacketRouter::PacketRouter()
Erik Språng8b7ca4a2018-05-17 13:43:35 +020035 : last_send_module_(nullptr),
36 last_remb_time_ms_(rtc::TimeMillis()),
nisse05843312017-04-18 23:38:35 -070037 last_send_bitrate_bps_(0),
danilchap47085372017-08-10 06:03:57 -070038 bitrate_bps_(0),
39 max_bitrate_bps_(std::numeric_limits<decltype(max_bitrate_bps_)>::max()),
eladalon822ff2b2017-08-01 06:30:28 -070040 active_remb_module_(nullptr),
erikvargabf5a2fc2017-06-16 05:02:05 -070041 transport_seq_(0) {}
Stefan Holmere5904162015-03-26 11:11:06 +010042
43PacketRouter::~PacketRouter() {
nissefdbfdc92017-03-31 05:44:52 -070044 RTC_DCHECK(rtp_send_modules_.empty());
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010045 RTC_DCHECK(rtcp_feedback_senders_.empty());
eladalon822ff2b2017-08-01 06:30:28 -070046 RTC_DCHECK(sender_remb_candidates_.empty());
47 RTC_DCHECK(receiver_remb_candidates_.empty());
48 RTC_DCHECK(active_remb_module_ == nullptr);
Stefan Holmere5904162015-03-26 11:11:06 +010049}
50
eladalon822ff2b2017-08-01 06:30:28 -070051void PacketRouter::AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate) {
stefanbba9dec2016-02-01 04:39:55 -080052 rtc::CritScope cs(&modules_crit_);
nissefdbfdc92017-03-31 05:44:52 -070053 RTC_DCHECK(std::find(rtp_send_modules_.begin(), rtp_send_modules_.end(),
54 rtp_module) == rtp_send_modules_.end());
stefan16b02212017-01-27 07:12:16 -080055 // 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) {
nissefdbfdc92017-03-31 05:44:52 -070058 rtp_send_modules_.push_front(rtp_module);
stefan16b02212017-01-27 07:12:16 -080059 } else {
nissefdbfdc92017-03-31 05:44:52 -070060 rtp_send_modules_.push_back(rtp_module);
stefan16b02212017-01-27 07:12:16 -080061 }
eladalon822ff2b2017-08-01 06:30:28 -070062
63 if (remb_candidate) {
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010064 AddRembModuleCandidate(rtp_module, /* media_sender = */ true);
eladalon822ff2b2017-08-01 06:30:28 -070065 }
Stefan Holmere5904162015-03-26 11:11:06 +010066}
67
nissefdbfdc92017-03-31 05:44:52 -070068void PacketRouter::RemoveSendRtpModule(RtpRtcp* rtp_module) {
stefanbba9dec2016-02-01 04:39:55 -080069 rtc::CritScope cs(&modules_crit_);
Danil Chapovalovdb59de32019-06-26 11:06:41 +020070 rtp_module_cache_map_.clear();
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010071 MaybeRemoveRembModuleCandidate(rtp_module, /* media_sender = */ true);
eladalon822ff2b2017-08-01 06:30:28 -070072 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ång8b7ca4a2018-05-17 13:43:35 +020076 if (last_send_module_ == rtp_module) {
77 last_send_module_ = nullptr;
78 }
nissefdbfdc92017-03-31 05:44:52 -070079}
80
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010081void PacketRouter::AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender,
eladalon822ff2b2017-08-01 06:30:28 -070082 bool remb_candidate) {
nissefdbfdc92017-03-31 05:44:52 -070083 rtc::CritScope cs(&modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010084 RTC_DCHECK(std::find(rtcp_feedback_senders_.begin(),
85 rtcp_feedback_senders_.end(),
86 rtcp_sender) == rtcp_feedback_senders_.end());
eladalon822ff2b2017-08-01 06:30:28 -070087
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010088 rtcp_feedback_senders_.push_back(rtcp_sender);
eladalon822ff2b2017-08-01 06:30:28 -070089
90 if (remb_candidate) {
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010091 AddRembModuleCandidate(rtcp_sender, /* media_sender = */ false);
eladalon822ff2b2017-08-01 06:30:28 -070092 }
nissefdbfdc92017-03-31 05:44:52 -070093}
94
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010095void PacketRouter::RemoveReceiveRtpModule(
96 RtcpFeedbackSenderInterface* rtcp_sender) {
nissefdbfdc92017-03-31 05:44:52 -070097 rtc::CritScope cs(&modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010098 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 Holmere5904162015-03-26 11:11:06 +0100103}
104
Erik Språngd2879622019-05-10 08:29:01 -0700105RtpPacketSendResult PacketRouter::TimeToSendPacket(
106 uint32_t ssrc,
107 uint16_t sequence_number,
108 int64_t capture_timestamp,
109 bool retransmission,
110 const PacedPacketInfo& pacing_info) {
stefanbba9dec2016-02-01 04:39:55 -0800111 rtc::CritScope cs(&modules_crit_);
Danil Chapovalovdb59de32019-06-26 11:06:41 +0200112 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
127RtpRtcp* 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ång8b7ca4a2018-05-17 13:43:35 +0200132 }
Danil Chapovalovdb59de32019-06-26 11:06:41 +0200133 // 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_) {
brandtr9dfff292016-11-14 05:14:50 -0800138 if (ssrc == rtp_module->SSRC() || ssrc == rtp_module->FlexfecSsrc()) {
Danil Chapovalovdb59de32019-06-26 11:06:41 +0200139 rtp_module_cache_map_[ssrc] = rtp_module;
140 return rtp_module;
Stefan Holmere5904162015-03-26 11:11:06 +0100141 }
142 }
Danil Chapovalovdb59de32019-06-26 11:06:41 +0200143 return nullptr;
Stefan Holmere5904162015-03-26 11:11:06 +0100144}
145
Erik Språng58ee1872019-06-18 16:20:11 +0200146void PacketRouter::SendPacket(std::unique_ptr<RtpPacketToSend> packet,
147 const PacedPacketInfo& cluster_info) {
148 rtc::CritScope cs(&modules_crit_);
Erik Språngf6468d22019-07-05 16:53:43 +0200149 // 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ång58ee1872019-06-18 16:20:11 +0200155 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
philipela1ed0b32016-06-01 06:31:17 -0700175size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send,
philipelc7bf32a2017-02-17 03:59:43 -0800176 const PacedPacketInfo& pacing_info) {
sprang867fb522015-08-03 04:38:41 -0700177 size_t total_bytes_sent = 0;
stefanbba9dec2016-02-01 04:39:55 -0800178 rtc::CritScope cs(&modules_crit_);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200179 // 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
stefan16b02212017-01-27 07:12:16 -0800196 // Rtp modules are ordered by which stream can most benefit from padding.
nissefdbfdc92017-03-31 05:44:52 -0700197 for (RtpRtcp* module : rtp_send_modules_) {
stefan53b6cc32017-02-03 08:13:57 -0800198 if (module->SendingMedia() && module->HasBweExtensions()) {
philipela1ed0b32016-06-01 06:31:17 -0700199 size_t bytes_sent = module->TimeToSendPadding(
philipelc7bf32a2017-02-17 03:59:43 -0800200 bytes_to_send - total_bytes_sent, pacing_info);
sprang867fb522015-08-03 04:38:41 -0700201 total_bytes_sent += bytes_sent;
202 if (total_bytes_sent >= bytes_to_send)
203 break;
204 }
Stefan Holmere5904162015-03-26 11:11:06 +0100205 }
sprang867fb522015-08-03 04:38:41 -0700206 return total_bytes_sent;
Stefan Holmere5904162015-03-26 11:11:06 +0100207}
sprang867fb522015-08-03 04:38:41 -0700208
Erik Språngf6468d22019-07-05 16:53:43 +0200209std::vector<std::unique_ptr<RtpPacketToSend>> PacketRouter::GeneratePadding(
210 size_t target_size_bytes) {
Erik Språng478cb462019-06-26 15:49:27 +0200211 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ångf6468d22019-07-05 16:53:43 +0200222 return last_send_module_->GeneratePadding(target_size_bytes);
Erik Språng478cb462019-06-26 15:49:27 +0200223 }
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ångf6468d22019-07-05 16:53:43 +0200228 return rtp_module->GeneratePadding(target_size_bytes);
Erik Språng478cb462019-06-26 15:49:27 +0200229 }
230 }
Erik Språngf6468d22019-07-05 16:53:43 +0200231
232 return {};
Erik Språng478cb462019-06-26 15:49:27 +0200233}
234
sprang867fb522015-08-03 04:38:41 -0700235void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) {
pbos46ad5422015-12-07 14:29:14 -0800236 rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number);
sprang867fb522015-08-03 04:38:41 -0700237}
238
239uint16_t PacketRouter::AllocateSequenceNumber() {
pbos46ad5422015-12-07 14:29:14 -0800240 int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_);
sprang867fb522015-08-03 04:38:41 -0700241 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.
pbos46ad5422015-12-07 14:29:14 -0800250 prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq,
sprang867fb522015-08-03 04:38:41 -0700251 new_seq);
252 } while (prev_seq != desired_prev_seq);
253
254 return new_seq;
255}
256
nisse05843312017-04-18 23:38:35 -0700257void PacketRouter::OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
258 uint32_t bitrate_bps) {
nisse05843312017-04-18 23:38:35 -0700259 // % threshold for if we should send a new REMB asap.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100260 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);
nisse05843312017-04-18 23:38:35 -0700264
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 Chapovalov1de4b622017-12-13 13:35:10 +0100272 int64_t new_remb_bitrate_bps =
273 last_send_bitrate_bps_ - bitrate_bps_ + receive_bitrate_bps;
nisse05843312017-04-18 23:38:35 -0700274
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 Chapovalov1de4b622017-12-13 13:35:10 +0100282 bitrate_bps_ = receive_bitrate_bps;
nisse05843312017-04-18 23:38:35 -0700283
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 Chapovalov1de4b622017-12-13 13:35:10 +0100290 last_send_bitrate_bps_ = receive_bitrate_bps;
danilchap47085372017-08-10 06:03:57 -0700291 // Cap the value to send in remb with configured value.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100292 receive_bitrate_bps = std::min(receive_bitrate_bps, max_bitrate_bps_);
nisse05843312017-04-18 23:38:35 -0700293 }
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100294 SendRemb(receive_bitrate_bps, ssrcs);
nisse05843312017-04-18 23:38:35 -0700295}
296
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100297void PacketRouter::SetMaxDesiredReceiveBitrate(int64_t bitrate_bps) {
298 RTC_DCHECK_GE(bitrate_bps, 0);
danilchap47085372017-08-10 06:03:57 -0700299 {
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 Chapovalov1de4b622017-12-13 13:35:10 +0100312bool PacketRouter::SendRemb(int64_t bitrate_bps,
nisse05843312017-04-18 23:38:35 -0700313 const std::vector<uint32_t>& ssrcs) {
314 rtc::CritScope lock(&modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700315
316 if (!active_remb_module_) {
nisse05843312017-04-18 23:38:35 -0700317 return false;
eladalon822ff2b2017-08-01 06:30:28 -0700318 }
319
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200320 // 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);
eladalon822ff2b2017-08-01 06:30:28 -0700323
nisse05843312017-04-18 23:38:35 -0700324 return true;
325}
326
327bool PacketRouter::SendTransportFeedback(rtcp::TransportFeedback* packet) {
stefanbba9dec2016-02-01 04:39:55 -0800328 rtc::CritScope cs(&modules_crit_);
nissefdbfdc92017-03-31 05:44:52 -0700329 // Prefer send modules.
330 for (auto* rtp_module : rtp_send_modules_) {
331 packet->SetSenderSsrc(rtp_module->SSRC());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200332 if (rtp_module->SendFeedbackPacket(*packet)) {
nissefdbfdc92017-03-31 05:44:52 -0700333 return true;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200334 }
nissefdbfdc92017-03-31 05:44:52 -0700335 }
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100336 for (auto* rtcp_sender : rtcp_feedback_senders_) {
337 packet->SetSenderSsrc(rtcp_sender->SSRC());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200338 if (rtcp_sender->SendFeedbackPacket(*packet)) {
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100339 return true;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200340 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100341 }
sprang233bd872015-09-08 13:25:16 -0700342 return false;
343}
344
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100345void PacketRouter::AddRembModuleCandidate(
346 RtcpFeedbackSenderInterface* candidate_module,
347 bool media_sender) {
eladalon822ff2b2017-08-01 06:30:28 -0700348 RTC_DCHECK(candidate_module);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100349 std::vector<RtcpFeedbackSenderInterface*>& candidates =
350 media_sender ? sender_remb_candidates_ : receiver_remb_candidates_;
eladalon822ff2b2017-08-01 06:30:28 -0700351 RTC_DCHECK(std::find(candidates.cbegin(), candidates.cend(),
352 candidate_module) == candidates.cend());
353 candidates.push_back(candidate_module);
354 DetermineActiveRembModule();
355}
356
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100357void PacketRouter::MaybeRemoveRembModuleCandidate(
358 RtcpFeedbackSenderInterface* candidate_module,
359 bool media_sender) {
eladalon822ff2b2017-08-01 06:30:28 -0700360 RTC_DCHECK(candidate_module);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100361 std::vector<RtcpFeedbackSenderInterface*>& candidates =
362 media_sender ? sender_remb_candidates_ : receiver_remb_candidates_;
eladalon822ff2b2017-08-01 06:30:28 -0700363 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
376void PacketRouter::UnsetActiveRembModule() {
377 RTC_CHECK(active_remb_module_);
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200378 active_remb_module_->UnsetRemb();
eladalon822ff2b2017-08-01 06:30:28 -0700379 active_remb_module_ = nullptr;
380}
381
382void 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 Chapovaloveb0edd82017-12-14 16:02:31 +0100388 RtcpFeedbackSenderInterface* new_active_remb_module;
eladalon822ff2b2017-08-01 06:30:28 -0700389
390 if (!sender_remb_candidates_.empty()) {
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200391 new_active_remb_module = sender_remb_candidates_.front();
eladalon822ff2b2017-08-01 06:30:28 -0700392 } else if (!receiver_remb_candidates_.empty()) {
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200393 new_active_remb_module = receiver_remb_candidates_.front();
eladalon822ff2b2017-08-01 06:30:28 -0700394 } else {
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200395 new_active_remb_module = nullptr;
eladalon822ff2b2017-08-01 06:30:28 -0700396 }
397
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200398 if (new_active_remb_module != active_remb_module_ && active_remb_module_) {
399 UnsetActiveRembModule();
eladalon822ff2b2017-08-01 06:30:28 -0700400 }
401
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200402 active_remb_module_ = new_active_remb_module;
eladalon822ff2b2017-08-01 06:30:28 -0700403}
404
Stefan Holmere5904162015-03-26 11:11:06 +0100405} // namespace webrtc