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