blob: 78ccf9907f9094e83f8f959682065856b22364b7 [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
289 if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
290 RTC_LOG(LS_WARNING) << "Failed to send RTCP BYE";
291 }
292 }
293 return 0;
294}
295
296bool ModuleRtpRtcpImpl2::Sending() const {
297 return rtcp_sender_.Sending();
298}
299
300// TODO(nisse): This method shouldn't be called for a receive-only
301// stream. Delete rtp_sender_ check as soon as all applications are
302// updated.
303void ModuleRtpRtcpImpl2::SetSendingMediaStatus(const bool sending) {
304 if (rtp_sender_) {
305 rtp_sender_->packet_generator.SetSendingMediaStatus(sending);
306 } else {
307 RTC_DCHECK(!sending);
308 }
309}
310
311bool ModuleRtpRtcpImpl2::SendingMedia() const {
312 return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false;
313}
314
315bool ModuleRtpRtcpImpl2::IsAudioConfigured() const {
316 return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured()
317 : false;
318}
319
320void ModuleRtpRtcpImpl2::SetAsPartOfAllocation(bool part_of_allocation) {
321 RTC_CHECK(rtp_sender_);
322 rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation(
323 part_of_allocation);
324}
325
326bool ModuleRtpRtcpImpl2::OnSendingRtpFrame(uint32_t timestamp,
327 int64_t capture_time_ms,
328 int payload_type,
329 bool force_sender_report) {
330 if (!Sending())
331 return false;
332
333 rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type);
334 // Make sure an RTCP report isn't queued behind a key frame.
335 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
336 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
337
338 return true;
339}
340
341bool ModuleRtpRtcpImpl2::TrySendPacket(RtpPacketToSend* packet,
342 const PacedPacketInfo& pacing_info) {
343 RTC_DCHECK(rtp_sender_);
344 // TODO(sprang): Consider if we can remove this check.
345 if (!rtp_sender_->packet_generator.SendingMedia()) {
346 return false;
347 }
348 rtp_sender_->packet_sender.SendPacket(packet, pacing_info);
349 return true;
350}
351
Erik Språng1d50cb62020-07-02 17:41:32 +0200352void ModuleRtpRtcpImpl2::SetFecProtectionParams(
353 const FecProtectionParams& delta_params,
354 const FecProtectionParams& key_params) {
355 RTC_DCHECK(rtp_sender_);
356 rtp_sender_->packet_sender.SetFecProtectionParameters(delta_params,
357 key_params);
358}
359
360std::vector<std::unique_ptr<RtpPacketToSend>>
361ModuleRtpRtcpImpl2::FetchFecPackets() {
362 RTC_DCHECK(rtp_sender_);
363 auto fec_packets = rtp_sender_->packet_sender.FetchFecPackets();
364 if (!fec_packets.empty()) {
365 // Don't assign sequence numbers for FlexFEC packets.
366 const bool generate_sequence_numbers =
367 !rtp_sender_->packet_sender.FlexFecSsrc().has_value();
368 if (generate_sequence_numbers) {
369 for (auto& fec_packet : fec_packets) {
370 rtp_sender_->packet_generator.AssignSequenceNumber(fec_packet.get());
371 }
372 }
373 }
374 return fec_packets;
375}
376
Tommi3a5742c2020-05-20 09:32:51 +0200377void ModuleRtpRtcpImpl2::OnPacketsAcknowledged(
378 rtc::ArrayView<const uint16_t> sequence_numbers) {
379 RTC_DCHECK(rtp_sender_);
380 rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers);
381}
382
383bool ModuleRtpRtcpImpl2::SupportsPadding() const {
384 RTC_DCHECK(rtp_sender_);
385 return rtp_sender_->packet_generator.SupportsPadding();
386}
387
388bool ModuleRtpRtcpImpl2::SupportsRtxPayloadPadding() const {
389 RTC_DCHECK(rtp_sender_);
390 return rtp_sender_->packet_generator.SupportsRtxPayloadPadding();
391}
392
393std::vector<std::unique_ptr<RtpPacketToSend>>
394ModuleRtpRtcpImpl2::GeneratePadding(size_t target_size_bytes) {
395 RTC_DCHECK(rtp_sender_);
396 return rtp_sender_->packet_generator.GeneratePadding(
397 target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent());
398}
399
400std::vector<RtpSequenceNumberMap::Info>
401ModuleRtpRtcpImpl2::GetSentRtpPacketInfos(
402 rtc::ArrayView<const uint16_t> sequence_numbers) const {
403 RTC_DCHECK(rtp_sender_);
404 return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers);
405}
406
407size_t ModuleRtpRtcpImpl2::ExpectedPerPacketOverhead() const {
408 if (!rtp_sender_) {
409 return 0;
410 }
411 return rtp_sender_->packet_generator.ExpectedPerPacketOverhead();
412}
413
414size_t ModuleRtpRtcpImpl2::MaxRtpPacketSize() const {
415 RTC_DCHECK(rtp_sender_);
416 return rtp_sender_->packet_generator.MaxRtpPacketSize();
417}
418
419void ModuleRtpRtcpImpl2::SetMaxRtpPacketSize(size_t rtp_packet_size) {
420 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
421 << "rtp packet size too large: " << rtp_packet_size;
422 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
423 << "rtp packet size too small: " << rtp_packet_size;
424
425 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
426 if (rtp_sender_) {
427 rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size);
428 }
429}
430
431RtcpMode ModuleRtpRtcpImpl2::RTCP() const {
432 return rtcp_sender_.Status();
433}
434
435// Configure RTCP status i.e on/off.
436void ModuleRtpRtcpImpl2::SetRTCPStatus(const RtcpMode method) {
437 rtcp_sender_.SetRTCPStatus(method);
438}
439
440int32_t ModuleRtpRtcpImpl2::SetCNAME(const char* c_name) {
441 return rtcp_sender_.SetCNAME(c_name);
442}
443
Tommi3a5742c2020-05-20 09:32:51 +0200444int32_t ModuleRtpRtcpImpl2::RemoteNTP(uint32_t* received_ntpsecs,
445 uint32_t* received_ntpfrac,
446 uint32_t* rtcp_arrival_time_secs,
447 uint32_t* rtcp_arrival_time_frac,
448 uint32_t* rtcp_timestamp) const {
449 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
450 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
Alessio Bazzica048adc72021-03-10 15:05:55 +0100451 rtcp_timestamp,
452 /*remote_sender_packet_count=*/nullptr,
453 /*remote_sender_octet_count=*/nullptr,
454 /*remote_sender_reports_count=*/nullptr)
Tommi3a5742c2020-05-20 09:32:51 +0200455 ? 0
456 : -1;
457}
458
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200459// TODO(tommi): Check if |avg_rtt_ms|, |min_rtt_ms|, |max_rtt_ms| params are
460// actually used in practice (some callers ask for it but don't use it). It
461// could be that only |rtt| is needed and if so, then the fast path could be to
462// just call rtt_ms() and rely on the calculation being done periodically.
Tommi3a5742c2020-05-20 09:32:51 +0200463int32_t ModuleRtpRtcpImpl2::RTT(const uint32_t remote_ssrc,
464 int64_t* rtt,
465 int64_t* avg_rtt,
466 int64_t* min_rtt,
467 int64_t* max_rtt) const {
468 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
469 if (rtt && *rtt == 0) {
470 // Try to get RTT from RtcpRttStats class.
471 *rtt = rtt_ms();
472 }
473 return ret;
474}
475
476int64_t ModuleRtpRtcpImpl2::ExpectedRetransmissionTimeMs() const {
477 int64_t expected_retransmission_time_ms = rtt_ms();
478 if (expected_retransmission_time_ms > 0) {
479 return expected_retransmission_time_ms;
480 }
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200481 // No rtt available (|kRttUpdateInterval| not yet passed?), so try to
Tommi3a5742c2020-05-20 09:32:51 +0200482 // poll avg_rtt_ms directly from rtcp receiver.
483 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
484 &expected_retransmission_time_ms, nullptr,
485 nullptr) == 0) {
486 return expected_retransmission_time_ms;
487 }
488 return kDefaultExpectedRetransmissionTimeMs;
489}
490
491// Force a send of an RTCP packet.
492// Normal SR and RR are triggered via the process function.
493int32_t ModuleRtpRtcpImpl2::SendRTCP(RTCPPacketType packet_type) {
494 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
495}
496
Tommi3a5742c2020-05-20 09:32:51 +0200497void ModuleRtpRtcpImpl2::GetSendStreamDataCounters(
498 StreamDataCounters* rtp_counters,
499 StreamDataCounters* rtx_counters) const {
500 rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters);
501}
502
503// Received RTCP report.
Tommi3a5742c2020-05-20 09:32:51 +0200504std::vector<ReportBlockData> ModuleRtpRtcpImpl2::GetLatestReportBlockData()
505 const {
506 return rtcp_receiver_.GetLatestReportBlockData();
507}
508
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100509absl::optional<RtpRtcpInterface::SenderReportStats>
510ModuleRtpRtcpImpl2::GetSenderReportStats() const {
511 SenderReportStats stats;
512 uint32_t remote_timestamp_secs;
513 uint32_t remote_timestamp_frac;
514 uint32_t arrival_timestamp_secs;
515 uint32_t arrival_timestamp_frac;
516 if (rtcp_receiver_.NTP(&remote_timestamp_secs, &remote_timestamp_frac,
517 &arrival_timestamp_secs, &arrival_timestamp_frac,
518 /*rtcp_timestamp=*/nullptr, &stats.packets_sent,
519 &stats.bytes_sent, &stats.reports_count)) {
520 stats.last_remote_timestamp.Set(remote_timestamp_secs,
521 remote_timestamp_frac);
522 stats.last_arrival_timestamp.Set(arrival_timestamp_secs,
523 arrival_timestamp_frac);
524 return stats;
525 }
526 return absl::nullopt;
527}
528
Tommi3a5742c2020-05-20 09:32:51 +0200529// (REMB) Receiver Estimated Max Bitrate.
530void ModuleRtpRtcpImpl2::SetRemb(int64_t bitrate_bps,
531 std::vector<uint32_t> ssrcs) {
532 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
533}
534
535void ModuleRtpRtcpImpl2::UnsetRemb() {
536 rtcp_sender_.UnsetRemb();
537}
538
539void ModuleRtpRtcpImpl2::SetExtmapAllowMixed(bool extmap_allow_mixed) {
540 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
541}
542
Tommi3a5742c2020-05-20 09:32:51 +0200543void ModuleRtpRtcpImpl2::RegisterRtpHeaderExtension(absl::string_view uri,
544 int id) {
545 bool registered =
546 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
547 RTC_CHECK(registered);
548}
549
550int32_t ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
551 const RTPExtensionType type) {
552 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
553}
554void ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
555 absl::string_view uri) {
556 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
557}
558
Tommi3a5742c2020-05-20 09:32:51 +0200559void ModuleRtpRtcpImpl2::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
560 rtcp_sender_.SetTmmbn(std::move(bounding_set));
561}
562
563// Send a Negative acknowledgment packet.
564int32_t ModuleRtpRtcpImpl2::SendNACK(const uint16_t* nack_list,
565 const uint16_t size) {
566 uint16_t nack_length = size;
567 uint16_t start_id = 0;
568 int64_t now_ms = clock_->TimeInMilliseconds();
569 if (TimeToSendFullNackList(now_ms)) {
570 nack_last_time_sent_full_ms_ = now_ms;
571 } else {
572 // Only send extended list.
573 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
574 // Last sequence number is the same, do not send list.
575 return 0;
576 }
577 // Send new sequence numbers.
578 for (int i = 0; i < size; ++i) {
579 if (nack_last_seq_number_sent_ == nack_list[i]) {
580 start_id = i + 1;
581 break;
582 }
583 }
584 nack_length = size - start_id;
585 }
586
587 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
588 // numbers per RTCP packet.
589 if (nack_length > kRtcpMaxNackFields) {
590 nack_length = kRtcpMaxNackFields;
591 }
592 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
593
594 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
595 &nack_list[start_id]);
596}
597
598void ModuleRtpRtcpImpl2::SendNack(
599 const std::vector<uint16_t>& sequence_numbers) {
600 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
601 sequence_numbers.data());
602}
603
604bool ModuleRtpRtcpImpl2::TimeToSendFullNackList(int64_t now) const {
605 // Use RTT from RtcpRttStats class if provided.
606 int64_t rtt = rtt_ms();
607 if (rtt == 0) {
608 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
609 }
610
611 const int64_t kStartUpRttMs = 100;
612 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
613 if (rtt == 0) {
614 wait_time = kStartUpRttMs;
615 }
616
617 // Send a full NACK list once within every |wait_time|.
618 return now - nack_last_time_sent_full_ms_ > wait_time;
619}
620
621// Store the sent packets, needed to answer to Negative acknowledgment requests.
622void ModuleRtpRtcpImpl2::SetStorePacketsStatus(const bool enable,
623 const uint16_t number_to_store) {
624 rtp_sender_->packet_history.SetStorePacketsStatus(
625 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
626 : RtpPacketHistory::StorageMode::kDisabled,
627 number_to_store);
628}
629
630bool ModuleRtpRtcpImpl2::StorePackets() const {
631 return rtp_sender_->packet_history.GetStorageMode() !=
632 RtpPacketHistory::StorageMode::kDisabled;
633}
634
635void ModuleRtpRtcpImpl2::SendCombinedRtcpPacket(
636 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
637 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
638}
639
640int32_t ModuleRtpRtcpImpl2::SendLossNotification(uint16_t last_decoded_seq_num,
641 uint16_t last_received_seq_num,
642 bool decodability_flag,
643 bool buffering_allowed) {
644 return rtcp_sender_.SendLossNotification(
645 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
646 decodability_flag, buffering_allowed);
647}
648
649void ModuleRtpRtcpImpl2::SetRemoteSSRC(const uint32_t ssrc) {
650 // Inform about the incoming SSRC.
651 rtcp_sender_.SetRemoteSSRC(ssrc);
652 rtcp_receiver_.SetRemoteSSRC(ssrc);
653}
654
Tommi3a5742c2020-05-20 09:32:51 +0200655RtpSendRates ModuleRtpRtcpImpl2::GetSendRates() const {
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200656 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200657 return rtp_sender_->packet_sender.GetSendRates();
658}
659
660void ModuleRtpRtcpImpl2::OnRequestSendReport() {
661 SendRTCP(kRtcpSr);
662}
663
664void ModuleRtpRtcpImpl2::OnReceivedNack(
665 const std::vector<uint16_t>& nack_sequence_numbers) {
666 if (!rtp_sender_)
667 return;
668
669 if (!StorePackets() || nack_sequence_numbers.empty()) {
670 return;
671 }
672 // Use RTT from RtcpRttStats class if provided.
673 int64_t rtt = rtt_ms();
674 if (rtt == 0) {
675 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
676 }
677 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
678}
679
680void ModuleRtpRtcpImpl2::OnReceivedRtcpReportBlocks(
681 const ReportBlockList& report_blocks) {
682 if (rtp_sender_) {
683 uint32_t ssrc = SSRC();
684 absl::optional<uint32_t> rtx_ssrc;
685 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
686 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
687 }
688
689 for (const RTCPReportBlock& report_block : report_blocks) {
690 if (ssrc == report_block.source_ssrc) {
691 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
692 report_block.extended_highest_sequence_number);
693 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
694 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
695 report_block.extended_highest_sequence_number);
696 }
697 }
698 }
699}
700
Tommi3a5742c2020-05-20 09:32:51 +0200701void ModuleRtpRtcpImpl2::set_rtt_ms(int64_t rtt_ms) {
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200702 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200703 {
Markus Handellf7303e62020-07-09 01:34:42 +0200704 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200705 rtt_ms_ = rtt_ms;
706 }
707 if (rtp_sender_) {
708 rtp_sender_->packet_history.SetRtt(rtt_ms);
709 }
710}
711
712int64_t ModuleRtpRtcpImpl2::rtt_ms() const {
Markus Handellf7303e62020-07-09 01:34:42 +0200713 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200714 return rtt_ms_;
715}
716
717void ModuleRtpRtcpImpl2::SetVideoBitrateAllocation(
718 const VideoBitrateAllocation& bitrate) {
719 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
720}
721
722RTPSender* ModuleRtpRtcpImpl2::RtpSender() {
723 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
724}
725
726const RTPSender* ModuleRtpRtcpImpl2::RtpSender() const {
727 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
728}
729
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200730void ModuleRtpRtcpImpl2::PeriodicUpdate() {
731 RTC_DCHECK_RUN_ON(worker_queue_);
732
733 Timestamp check_since = clock_->CurrentTime() - kRttUpdateInterval;
734 absl::optional<TimeDelta> rtt =
735 rtcp_receiver_.OnPeriodicRttUpdate(check_since, rtcp_sender_.Sending());
736 if (rtt) {
737 rtt_stats_->OnRttUpdate(rtt->ms());
738 set_rtt_ms(rtt->ms());
739 }
740
741 // kTmmbrTimeoutIntervalMs is 25 seconds, so an order of seconds.
742 // Instead of this polling approach, consider having an optional timer in the
743 // RTCPReceiver class that is started/stopped based on the state of
744 // rtcp_sender_.TMMBR().
745 if (rtcp_sender_.TMMBR() && rtcp_receiver_.UpdateTmmbrTimers())
746 rtcp_receiver_.NotifyTmmbrUpdated();
747}
748
Tommi3a5742c2020-05-20 09:32:51 +0200749} // namespace webrtc