blob: ae7d9a0d9bd54e97a711ec1d423b48e1d1d14274 [file] [log] [blame]
Tommi3a5742c2020-05-20 09:32:51 +02001/*
2 * Copyright (c) 2012 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
11#include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
12
13#include <string.h>
14
15#include <algorithm>
16#include <cstdint>
17#include <memory>
18#include <set>
19#include <string>
20#include <utility>
21
22#include "api/transport/field_trial_based_config.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
24#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
25#include "rtc_base/checks.h"
26#include "rtc_base/logging.h"
27
28#ifdef _WIN32
29// Disable warning C4355: 'this' : used in base member initializer list.
30#pragma warning(disable : 4355)
31#endif
32
33namespace webrtc {
34namespace {
35const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5;
Tommi3a5742c2020-05-20 09:32:51 +020036const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020037
38constexpr TimeDelta kRttUpdateInterval = TimeDelta::Millis(1000);
Tommi3a5742c2020-05-20 09:32:51 +020039} // namespace
40
41ModuleRtpRtcpImpl2::RtpSenderContext::RtpSenderContext(
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020042 const RtpRtcpInterface::Configuration& config)
Tommi3a5742c2020-05-20 09:32:51 +020043 : packet_history(config.clock, config.enable_rtx_padding_prioritization),
44 packet_sender(config, &packet_history),
Erik Språng1d50cb62020-07-02 17:41:32 +020045 non_paced_sender(&packet_sender, this),
Tommi3a5742c2020-05-20 09:32:51 +020046 packet_generator(
47 config,
48 &packet_history,
49 config.paced_sender ? config.paced_sender : &non_paced_sender) {}
Erik Språng1d50cb62020-07-02 17:41:32 +020050void ModuleRtpRtcpImpl2::RtpSenderContext::AssignSequenceNumber(
51 RtpPacketToSend* packet) {
52 packet_generator.AssignSequenceNumber(packet);
53}
Tommi3a5742c2020-05-20 09:32:51 +020054
Tommi3a5742c2020-05-20 09:32:51 +020055ModuleRtpRtcpImpl2::ModuleRtpRtcpImpl2(const Configuration& configuration)
Niels Moller2accc7d2021-01-12 15:54:16 +000056 : worker_queue_(TaskQueueBase::Current()),
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +020057 rtcp_sender_(configuration),
Tommi3a5742c2020-05-20 09:32:51 +020058 rtcp_receiver_(configuration, this),
59 clock_(configuration.clock),
Tommi3a5742c2020-05-20 09:32:51 +020060 last_rtt_process_time_(clock_->TimeInMilliseconds()),
61 next_process_time_(clock_->TimeInMilliseconds() +
62 kRtpRtcpMaxIdleTimeProcessMs),
63 packet_overhead_(28), // IPV4 UDP.
64 nack_last_time_sent_full_ms_(0),
65 nack_last_seq_number_sent_(0),
66 remote_bitrate_(configuration.remote_bitrate_estimator),
67 rtt_stats_(configuration.rtt_stats),
68 rtt_ms_(0) {
Niels Moller2accc7d2021-01-12 15:54:16 +000069 RTC_DCHECK(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +020070 process_thread_checker_.Detach();
71 if (!configuration.receiver_only) {
72 rtp_sender_ = std::make_unique<RtpSenderContext>(configuration);
73 // Make sure rtcp sender use same timestamp offset as rtp sender.
74 rtcp_sender_.SetTimestampOffset(
75 rtp_sender_->packet_generator.TimestampOffset());
76 }
77
78 // Set default packet size limit.
79 // TODO(nisse): Kind-of duplicates
80 // webrtc::VideoSendStream::Config::Rtp::kDefaultMaxPacketSize.
81 const size_t kTcpOverIpv4HeaderSize = 40;
82 SetMaxRtpPacketSize(IP_PACKET_SIZE - kTcpOverIpv4HeaderSize);
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020083
84 if (rtt_stats_) {
85 rtt_update_task_ = RepeatingTaskHandle::DelayedStart(
86 worker_queue_, kRttUpdateInterval, [this]() {
87 PeriodicUpdate();
88 return kRttUpdateInterval;
89 });
90 }
Tommi3a5742c2020-05-20 09:32:51 +020091}
92
93ModuleRtpRtcpImpl2::~ModuleRtpRtcpImpl2() {
Niels Moller2accc7d2021-01-12 15:54:16 +000094 RTC_DCHECK_RUN_ON(worker_queue_);
95 rtt_update_task_.Stop();
96}
97
98// static
99std::unique_ptr<ModuleRtpRtcpImpl2> ModuleRtpRtcpImpl2::Create(
100 const Configuration& configuration) {
101 RTC_DCHECK(configuration.clock);
102 RTC_DCHECK(TaskQueueBase::Current());
103 return std::make_unique<ModuleRtpRtcpImpl2>(configuration);
Tomas Gunnarssonfae05622020-06-03 08:54:39 +0200104}
105
Tommi3a5742c2020-05-20 09:32:51 +0200106// Returns the number of milliseconds until the module want a worker thread
107// to call Process.
108int64_t ModuleRtpRtcpImpl2::TimeUntilNextProcess() {
109 RTC_DCHECK_RUN_ON(&process_thread_checker_);
110 return std::max<int64_t>(0,
111 next_process_time_ - clock_->TimeInMilliseconds());
112}
113
114// Process any pending tasks such as timeouts (non time critical events).
115void ModuleRtpRtcpImpl2::Process() {
116 RTC_DCHECK_RUN_ON(&process_thread_checker_);
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200117
118 const Timestamp now = clock_->CurrentTime();
119
Tommi3a5742c2020-05-20 09:32:51 +0200120 // TODO(bugs.webrtc.org/11581): Figure out why we need to call Process() 200
121 // times a second.
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200122 next_process_time_ = now.ms() + kRtpRtcpMaxIdleTimeProcessMs;
Tommi3a5742c2020-05-20 09:32:51 +0200123
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200124 // TODO(bugs.webrtc.org/11581): once we don't use Process() to trigger
125 // calls to SendRTCP(), the only remaining timer will require remote_bitrate_
126 // to be not null. In that case, we can disable the timer when it is null.
127 if (remote_bitrate_ && rtcp_sender_.Sending() && rtcp_sender_.TMMBR()) {
128 unsigned int target_bitrate = 0;
129 std::vector<unsigned int> ssrcs;
130 if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
131 if (!ssrcs.empty()) {
132 target_bitrate = target_bitrate / ssrcs.size();
Tommi3a5742c2020-05-20 09:32:51 +0200133 }
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200134 rtcp_sender_.SetTargetBitrate(target_bitrate);
Tommi3a5742c2020-05-20 09:32:51 +0200135 }
136 }
137
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200138 // TODO(bugs.webrtc.org/11581): Run this on a separate set of delayed tasks
139 // based off of next_time_to_send_rtcp_ in RTCPSender.
Tommi3a5742c2020-05-20 09:32:51 +0200140 if (rtcp_sender_.TimeToSendRTCPReport())
141 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
Tommi3a5742c2020-05-20 09:32:51 +0200142}
143
144void ModuleRtpRtcpImpl2::SetRtxSendStatus(int mode) {
145 rtp_sender_->packet_generator.SetRtxStatus(mode);
146}
147
148int ModuleRtpRtcpImpl2::RtxSendStatus() const {
149 return rtp_sender_ ? rtp_sender_->packet_generator.RtxStatus() : kRtxOff;
150}
151
152void ModuleRtpRtcpImpl2::SetRtxSendPayloadType(int payload_type,
153 int associated_payload_type) {
154 rtp_sender_->packet_generator.SetRtxPayloadType(payload_type,
155 associated_payload_type);
156}
157
158absl::optional<uint32_t> ModuleRtpRtcpImpl2::RtxSsrc() const {
159 return rtp_sender_ ? rtp_sender_->packet_generator.RtxSsrc() : absl::nullopt;
160}
161
162absl::optional<uint32_t> ModuleRtpRtcpImpl2::FlexfecSsrc() const {
163 if (rtp_sender_) {
164 return rtp_sender_->packet_generator.FlexfecSsrc();
165 }
166 return absl::nullopt;
167}
168
169void ModuleRtpRtcpImpl2::IncomingRtcpPacket(const uint8_t* rtcp_packet,
170 const size_t length) {
171 rtcp_receiver_.IncomingPacket(rtcp_packet, length);
172}
173
174void ModuleRtpRtcpImpl2::RegisterSendPayloadFrequency(int payload_type,
175 int payload_frequency) {
176 rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
177}
178
179int32_t ModuleRtpRtcpImpl2::DeRegisterSendPayload(const int8_t payload_type) {
180 return 0;
181}
182
183uint32_t ModuleRtpRtcpImpl2::StartTimestamp() const {
184 return rtp_sender_->packet_generator.TimestampOffset();
185}
186
187// Configure start timestamp, default is a random number.
188void ModuleRtpRtcpImpl2::SetStartTimestamp(const uint32_t timestamp) {
189 rtcp_sender_.SetTimestampOffset(timestamp);
190 rtp_sender_->packet_generator.SetTimestampOffset(timestamp);
191 rtp_sender_->packet_sender.SetTimestampOffset(timestamp);
192}
193
194uint16_t ModuleRtpRtcpImpl2::SequenceNumber() const {
195 return rtp_sender_->packet_generator.SequenceNumber();
196}
197
198// Set SequenceNumber, default is a random number.
199void ModuleRtpRtcpImpl2::SetSequenceNumber(const uint16_t seq_num) {
200 rtp_sender_->packet_generator.SetSequenceNumber(seq_num);
201}
202
203void ModuleRtpRtcpImpl2::SetRtpState(const RtpState& rtp_state) {
204 rtp_sender_->packet_generator.SetRtpState(rtp_state);
Tommi3a5742c2020-05-20 09:32:51 +0200205 rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
206}
207
208void ModuleRtpRtcpImpl2::SetRtxState(const RtpState& rtp_state) {
209 rtp_sender_->packet_generator.SetRtxRtpState(rtp_state);
210}
211
212RtpState ModuleRtpRtcpImpl2::GetRtpState() const {
213 RtpState state = rtp_sender_->packet_generator.GetRtpState();
Tommi3a5742c2020-05-20 09:32:51 +0200214 return state;
215}
216
217RtpState ModuleRtpRtcpImpl2::GetRtxState() const {
218 return rtp_sender_->packet_generator.GetRtxRtpState();
219}
220
221void ModuleRtpRtcpImpl2::SetRid(const std::string& rid) {
222 if (rtp_sender_) {
223 rtp_sender_->packet_generator.SetRid(rid);
224 }
225}
226
227void ModuleRtpRtcpImpl2::SetMid(const std::string& mid) {
228 if (rtp_sender_) {
229 rtp_sender_->packet_generator.SetMid(mid);
230 }
231 // TODO(bugs.webrtc.org/4050): If we end up supporting the MID SDES item for
232 // RTCP, this will need to be passed down to the RTCPSender also.
233}
234
235void ModuleRtpRtcpImpl2::SetCsrcs(const std::vector<uint32_t>& csrcs) {
236 rtcp_sender_.SetCsrcs(csrcs);
237 rtp_sender_->packet_generator.SetCsrcs(csrcs);
238}
239
240// TODO(pbos): Handle media and RTX streams separately (separate RTCP
241// feedbacks).
242RTCPSender::FeedbackState ModuleRtpRtcpImpl2::GetFeedbackState() {
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200243 // TODO(bugs.webrtc.org/11581): Called by potentially multiple threads.
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200244 // Mostly "Send*" methods. Make sure it's only called on the
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200245 // construction thread.
246
Tommi3a5742c2020-05-20 09:32:51 +0200247 RTCPSender::FeedbackState state;
248 // This is called also when receiver_only is true. Hence below
249 // checks that rtp_sender_ exists.
250 if (rtp_sender_) {
251 StreamDataCounters rtp_stats;
252 StreamDataCounters rtx_stats;
253 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
254 state.packets_sent =
255 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
256 state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
257 rtx_stats.transmitted.payload_bytes;
258 state.send_bitrate =
259 rtp_sender_->packet_sender.GetSendRates().Sum().bps<uint32_t>();
260 }
261 state.receiver = &rtcp_receiver_;
262
Alessio Bazzica79011ef2021-03-10 14:52:35 +0100263 uint32_t received_ntp_secs = 0;
264 uint32_t received_ntp_frac = 0;
265 state.remote_sr = 0;
266 if (rtcp_receiver_.NTP(&received_ntp_secs, &received_ntp_frac,
267 /*rtcp_arrival_time_secs=*/&state.last_rr_ntp_secs,
268 /*rtcp_arrival_time_frac=*/&state.last_rr_ntp_frac,
Alessio Bazzica048adc72021-03-10 15:05:55 +0100269 /*rtcp_timestamp=*/nullptr,
270 /*remote_sender_packet_count=*/nullptr,
271 /*remote_sender_octet_count=*/nullptr,
272 /*remote_sender_reports_count=*/nullptr)) {
Alessio Bazzica79011ef2021-03-10 14:52:35 +0100273 state.remote_sr = ((received_ntp_secs & 0x0000ffff) << 16) +
274 ((received_ntp_frac & 0xffff0000) >> 16);
275 }
Tommi3a5742c2020-05-20 09:32:51 +0200276
277 state.last_xr_rtis = rtcp_receiver_.ConsumeReceivedXrReferenceTimeInfo();
278
279 return state;
280}
281
282// TODO(nisse): This method shouldn't be called for a receive-only
283// stream. Delete rtp_sender_ check as soon as all applications are
284// updated.
285int32_t ModuleRtpRtcpImpl2::SetSendingStatus(const bool sending) {
286 if (rtcp_sender_.Sending() != sending) {
287 // Sends RTCP BYE when going from true to false
288 if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
289 RTC_LOG(LS_WARNING) << "Failed to send RTCP BYE";
290 }
291 }
292 return 0;
293}
294
295bool ModuleRtpRtcpImpl2::Sending() const {
296 return rtcp_sender_.Sending();
297}
298
299// TODO(nisse): This method shouldn't be called for a receive-only
300// stream. Delete rtp_sender_ check as soon as all applications are
301// updated.
302void ModuleRtpRtcpImpl2::SetSendingMediaStatus(const bool sending) {
303 if (rtp_sender_) {
304 rtp_sender_->packet_generator.SetSendingMediaStatus(sending);
305 } else {
306 RTC_DCHECK(!sending);
307 }
308}
309
310bool ModuleRtpRtcpImpl2::SendingMedia() const {
311 return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false;
312}
313
314bool ModuleRtpRtcpImpl2::IsAudioConfigured() const {
315 return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured()
316 : false;
317}
318
319void ModuleRtpRtcpImpl2::SetAsPartOfAllocation(bool part_of_allocation) {
320 RTC_CHECK(rtp_sender_);
321 rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation(
322 part_of_allocation);
323}
324
325bool ModuleRtpRtcpImpl2::OnSendingRtpFrame(uint32_t timestamp,
326 int64_t capture_time_ms,
327 int payload_type,
328 bool force_sender_report) {
329 if (!Sending())
330 return false;
331
332 rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type);
333 // Make sure an RTCP report isn't queued behind a key frame.
334 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
335 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
336
337 return true;
338}
339
340bool ModuleRtpRtcpImpl2::TrySendPacket(RtpPacketToSend* packet,
341 const PacedPacketInfo& pacing_info) {
342 RTC_DCHECK(rtp_sender_);
343 // TODO(sprang): Consider if we can remove this check.
344 if (!rtp_sender_->packet_generator.SendingMedia()) {
345 return false;
346 }
347 rtp_sender_->packet_sender.SendPacket(packet, pacing_info);
348 return true;
349}
350
Erik Språng1d50cb62020-07-02 17:41:32 +0200351void ModuleRtpRtcpImpl2::SetFecProtectionParams(
352 const FecProtectionParams& delta_params,
353 const FecProtectionParams& key_params) {
354 RTC_DCHECK(rtp_sender_);
355 rtp_sender_->packet_sender.SetFecProtectionParameters(delta_params,
356 key_params);
357}
358
359std::vector<std::unique_ptr<RtpPacketToSend>>
360ModuleRtpRtcpImpl2::FetchFecPackets() {
361 RTC_DCHECK(rtp_sender_);
362 auto fec_packets = rtp_sender_->packet_sender.FetchFecPackets();
363 if (!fec_packets.empty()) {
364 // Don't assign sequence numbers for FlexFEC packets.
365 const bool generate_sequence_numbers =
366 !rtp_sender_->packet_sender.FlexFecSsrc().has_value();
367 if (generate_sequence_numbers) {
368 for (auto& fec_packet : fec_packets) {
369 rtp_sender_->packet_generator.AssignSequenceNumber(fec_packet.get());
370 }
371 }
372 }
373 return fec_packets;
374}
375
Tommi3a5742c2020-05-20 09:32:51 +0200376void ModuleRtpRtcpImpl2::OnPacketsAcknowledged(
377 rtc::ArrayView<const uint16_t> sequence_numbers) {
378 RTC_DCHECK(rtp_sender_);
379 rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers);
380}
381
382bool ModuleRtpRtcpImpl2::SupportsPadding() const {
383 RTC_DCHECK(rtp_sender_);
384 return rtp_sender_->packet_generator.SupportsPadding();
385}
386
387bool ModuleRtpRtcpImpl2::SupportsRtxPayloadPadding() const {
388 RTC_DCHECK(rtp_sender_);
389 return rtp_sender_->packet_generator.SupportsRtxPayloadPadding();
390}
391
392std::vector<std::unique_ptr<RtpPacketToSend>>
393ModuleRtpRtcpImpl2::GeneratePadding(size_t target_size_bytes) {
394 RTC_DCHECK(rtp_sender_);
395 return rtp_sender_->packet_generator.GeneratePadding(
396 target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent());
397}
398
399std::vector<RtpSequenceNumberMap::Info>
400ModuleRtpRtcpImpl2::GetSentRtpPacketInfos(
401 rtc::ArrayView<const uint16_t> sequence_numbers) const {
402 RTC_DCHECK(rtp_sender_);
403 return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers);
404}
405
406size_t ModuleRtpRtcpImpl2::ExpectedPerPacketOverhead() const {
407 if (!rtp_sender_) {
408 return 0;
409 }
410 return rtp_sender_->packet_generator.ExpectedPerPacketOverhead();
411}
412
413size_t ModuleRtpRtcpImpl2::MaxRtpPacketSize() const {
414 RTC_DCHECK(rtp_sender_);
415 return rtp_sender_->packet_generator.MaxRtpPacketSize();
416}
417
418void ModuleRtpRtcpImpl2::SetMaxRtpPacketSize(size_t rtp_packet_size) {
419 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
420 << "rtp packet size too large: " << rtp_packet_size;
421 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
422 << "rtp packet size too small: " << rtp_packet_size;
423
424 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
425 if (rtp_sender_) {
426 rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size);
427 }
428}
429
430RtcpMode ModuleRtpRtcpImpl2::RTCP() const {
431 return rtcp_sender_.Status();
432}
433
434// Configure RTCP status i.e on/off.
435void ModuleRtpRtcpImpl2::SetRTCPStatus(const RtcpMode method) {
436 rtcp_sender_.SetRTCPStatus(method);
437}
438
439int32_t ModuleRtpRtcpImpl2::SetCNAME(const char* c_name) {
440 return rtcp_sender_.SetCNAME(c_name);
441}
442
Tommi3a5742c2020-05-20 09:32:51 +0200443int32_t ModuleRtpRtcpImpl2::RemoteNTP(uint32_t* received_ntpsecs,
444 uint32_t* received_ntpfrac,
445 uint32_t* rtcp_arrival_time_secs,
446 uint32_t* rtcp_arrival_time_frac,
447 uint32_t* rtcp_timestamp) const {
448 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
449 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
Alessio Bazzica048adc72021-03-10 15:05:55 +0100450 rtcp_timestamp,
451 /*remote_sender_packet_count=*/nullptr,
452 /*remote_sender_octet_count=*/nullptr,
453 /*remote_sender_reports_count=*/nullptr)
Tommi3a5742c2020-05-20 09:32:51 +0200454 ? 0
455 : -1;
456}
457
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200458// TODO(tommi): Check if |avg_rtt_ms|, |min_rtt_ms|, |max_rtt_ms| params are
459// actually used in practice (some callers ask for it but don't use it). It
460// could be that only |rtt| is needed and if so, then the fast path could be to
461// just call rtt_ms() and rely on the calculation being done periodically.
Tommi3a5742c2020-05-20 09:32:51 +0200462int32_t ModuleRtpRtcpImpl2::RTT(const uint32_t remote_ssrc,
463 int64_t* rtt,
464 int64_t* avg_rtt,
465 int64_t* min_rtt,
466 int64_t* max_rtt) const {
467 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
468 if (rtt && *rtt == 0) {
469 // Try to get RTT from RtcpRttStats class.
470 *rtt = rtt_ms();
471 }
472 return ret;
473}
474
475int64_t ModuleRtpRtcpImpl2::ExpectedRetransmissionTimeMs() const {
476 int64_t expected_retransmission_time_ms = rtt_ms();
477 if (expected_retransmission_time_ms > 0) {
478 return expected_retransmission_time_ms;
479 }
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200480 // No rtt available (|kRttUpdateInterval| not yet passed?), so try to
Tommi3a5742c2020-05-20 09:32:51 +0200481 // poll avg_rtt_ms directly from rtcp receiver.
482 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
483 &expected_retransmission_time_ms, nullptr,
484 nullptr) == 0) {
485 return expected_retransmission_time_ms;
486 }
487 return kDefaultExpectedRetransmissionTimeMs;
488}
489
490// Force a send of an RTCP packet.
491// Normal SR and RR are triggered via the process function.
492int32_t ModuleRtpRtcpImpl2::SendRTCP(RTCPPacketType packet_type) {
493 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
494}
495
Tommi3a5742c2020-05-20 09:32:51 +0200496void ModuleRtpRtcpImpl2::GetSendStreamDataCounters(
497 StreamDataCounters* rtp_counters,
498 StreamDataCounters* rtx_counters) const {
499 rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters);
500}
501
502// Received RTCP report.
503int32_t ModuleRtpRtcpImpl2::RemoteRTCPStat(
504 std::vector<RTCPReportBlock>* receive_blocks) const {
505 return rtcp_receiver_.StatisticsReceived(receive_blocks);
506}
507
508std::vector<ReportBlockData> ModuleRtpRtcpImpl2::GetLatestReportBlockData()
509 const {
510 return rtcp_receiver_.GetLatestReportBlockData();
511}
512
513// (REMB) Receiver Estimated Max Bitrate.
514void ModuleRtpRtcpImpl2::SetRemb(int64_t bitrate_bps,
515 std::vector<uint32_t> ssrcs) {
516 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
517}
518
519void ModuleRtpRtcpImpl2::UnsetRemb() {
520 rtcp_sender_.UnsetRemb();
521}
522
523void ModuleRtpRtcpImpl2::SetExtmapAllowMixed(bool extmap_allow_mixed) {
524 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
525}
526
Tommi3a5742c2020-05-20 09:32:51 +0200527void ModuleRtpRtcpImpl2::RegisterRtpHeaderExtension(absl::string_view uri,
528 int id) {
529 bool registered =
530 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
531 RTC_CHECK(registered);
532}
533
534int32_t ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
535 const RTPExtensionType type) {
536 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
537}
538void ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
539 absl::string_view uri) {
540 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
541}
542
Tommi3a5742c2020-05-20 09:32:51 +0200543void ModuleRtpRtcpImpl2::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
544 rtcp_sender_.SetTmmbn(std::move(bounding_set));
545}
546
547// Send a Negative acknowledgment packet.
548int32_t ModuleRtpRtcpImpl2::SendNACK(const uint16_t* nack_list,
549 const uint16_t size) {
550 uint16_t nack_length = size;
551 uint16_t start_id = 0;
552 int64_t now_ms = clock_->TimeInMilliseconds();
553 if (TimeToSendFullNackList(now_ms)) {
554 nack_last_time_sent_full_ms_ = now_ms;
555 } else {
556 // Only send extended list.
557 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
558 // Last sequence number is the same, do not send list.
559 return 0;
560 }
561 // Send new sequence numbers.
562 for (int i = 0; i < size; ++i) {
563 if (nack_last_seq_number_sent_ == nack_list[i]) {
564 start_id = i + 1;
565 break;
566 }
567 }
568 nack_length = size - start_id;
569 }
570
571 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
572 // numbers per RTCP packet.
573 if (nack_length > kRtcpMaxNackFields) {
574 nack_length = kRtcpMaxNackFields;
575 }
576 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
577
578 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
579 &nack_list[start_id]);
580}
581
582void ModuleRtpRtcpImpl2::SendNack(
583 const std::vector<uint16_t>& sequence_numbers) {
584 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
585 sequence_numbers.data());
586}
587
588bool ModuleRtpRtcpImpl2::TimeToSendFullNackList(int64_t now) const {
589 // Use RTT from RtcpRttStats class if provided.
590 int64_t rtt = rtt_ms();
591 if (rtt == 0) {
592 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
593 }
594
595 const int64_t kStartUpRttMs = 100;
596 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
597 if (rtt == 0) {
598 wait_time = kStartUpRttMs;
599 }
600
601 // Send a full NACK list once within every |wait_time|.
602 return now - nack_last_time_sent_full_ms_ > wait_time;
603}
604
605// Store the sent packets, needed to answer to Negative acknowledgment requests.
606void ModuleRtpRtcpImpl2::SetStorePacketsStatus(const bool enable,
607 const uint16_t number_to_store) {
608 rtp_sender_->packet_history.SetStorePacketsStatus(
609 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
610 : RtpPacketHistory::StorageMode::kDisabled,
611 number_to_store);
612}
613
614bool ModuleRtpRtcpImpl2::StorePackets() const {
615 return rtp_sender_->packet_history.GetStorageMode() !=
616 RtpPacketHistory::StorageMode::kDisabled;
617}
618
619void ModuleRtpRtcpImpl2::SendCombinedRtcpPacket(
620 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
621 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
622}
623
624int32_t ModuleRtpRtcpImpl2::SendLossNotification(uint16_t last_decoded_seq_num,
625 uint16_t last_received_seq_num,
626 bool decodability_flag,
627 bool buffering_allowed) {
628 return rtcp_sender_.SendLossNotification(
629 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
630 decodability_flag, buffering_allowed);
631}
632
633void ModuleRtpRtcpImpl2::SetRemoteSSRC(const uint32_t ssrc) {
634 // Inform about the incoming SSRC.
635 rtcp_sender_.SetRemoteSSRC(ssrc);
636 rtcp_receiver_.SetRemoteSSRC(ssrc);
637}
638
Tommi3a5742c2020-05-20 09:32:51 +0200639RtpSendRates ModuleRtpRtcpImpl2::GetSendRates() const {
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200640 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200641 return rtp_sender_->packet_sender.GetSendRates();
642}
643
644void ModuleRtpRtcpImpl2::OnRequestSendReport() {
645 SendRTCP(kRtcpSr);
646}
647
648void ModuleRtpRtcpImpl2::OnReceivedNack(
649 const std::vector<uint16_t>& nack_sequence_numbers) {
650 if (!rtp_sender_)
651 return;
652
653 if (!StorePackets() || nack_sequence_numbers.empty()) {
654 return;
655 }
656 // Use RTT from RtcpRttStats class if provided.
657 int64_t rtt = rtt_ms();
658 if (rtt == 0) {
659 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
660 }
661 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
662}
663
664void ModuleRtpRtcpImpl2::OnReceivedRtcpReportBlocks(
665 const ReportBlockList& report_blocks) {
666 if (rtp_sender_) {
667 uint32_t ssrc = SSRC();
668 absl::optional<uint32_t> rtx_ssrc;
669 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
670 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
671 }
672
673 for (const RTCPReportBlock& report_block : report_blocks) {
674 if (ssrc == report_block.source_ssrc) {
675 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
676 report_block.extended_highest_sequence_number);
677 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
678 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
679 report_block.extended_highest_sequence_number);
680 }
681 }
682 }
683}
684
Tommi3a5742c2020-05-20 09:32:51 +0200685void ModuleRtpRtcpImpl2::set_rtt_ms(int64_t rtt_ms) {
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200686 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200687 {
Markus Handellf7303e62020-07-09 01:34:42 +0200688 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200689 rtt_ms_ = rtt_ms;
690 }
691 if (rtp_sender_) {
692 rtp_sender_->packet_history.SetRtt(rtt_ms);
693 }
694}
695
696int64_t ModuleRtpRtcpImpl2::rtt_ms() const {
Markus Handellf7303e62020-07-09 01:34:42 +0200697 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200698 return rtt_ms_;
699}
700
701void ModuleRtpRtcpImpl2::SetVideoBitrateAllocation(
702 const VideoBitrateAllocation& bitrate) {
703 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
704}
705
706RTPSender* ModuleRtpRtcpImpl2::RtpSender() {
707 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
708}
709
710const RTPSender* ModuleRtpRtcpImpl2::RtpSender() const {
711 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
712}
713
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200714void ModuleRtpRtcpImpl2::PeriodicUpdate() {
715 RTC_DCHECK_RUN_ON(worker_queue_);
716
717 Timestamp check_since = clock_->CurrentTime() - kRttUpdateInterval;
718 absl::optional<TimeDelta> rtt =
719 rtcp_receiver_.OnPeriodicRttUpdate(check_since, rtcp_sender_.Sending());
720 if (rtt) {
721 rtt_stats_->OnRttUpdate(rtt->ms());
722 set_rtt_ms(rtt->ms());
723 }
724
725 // kTmmbrTimeoutIntervalMs is 25 seconds, so an order of seconds.
726 // Instead of this polling approach, consider having an optional timer in the
727 // RTCPReceiver class that is started/stopped based on the state of
728 // rtcp_sender_.TMMBR().
729 if (rtcp_sender_.TMMBR() && rtcp_receiver_.UpdateTmmbrTimers())
730 rtcp_receiver_.NotifyTmmbrUpdated();
731}
732
Tommi3a5742c2020-05-20 09:32:51 +0200733} // namespace webrtc