blob: 73baab1da7bc691742a8f9060a5bfbb5014099d8 [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.
504int32_t ModuleRtpRtcpImpl2::RemoteRTCPStat(
505 std::vector<RTCPReportBlock>* receive_blocks) const {
506 return rtcp_receiver_.StatisticsReceived(receive_blocks);
507}
508
509std::vector<ReportBlockData> ModuleRtpRtcpImpl2::GetLatestReportBlockData()
510 const {
511 return rtcp_receiver_.GetLatestReportBlockData();
512}
513
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100514absl::optional<RtpRtcpInterface::SenderReportStats>
515ModuleRtpRtcpImpl2::GetSenderReportStats() const {
516 SenderReportStats stats;
517 uint32_t remote_timestamp_secs;
518 uint32_t remote_timestamp_frac;
519 uint32_t arrival_timestamp_secs;
520 uint32_t arrival_timestamp_frac;
521 if (rtcp_receiver_.NTP(&remote_timestamp_secs, &remote_timestamp_frac,
522 &arrival_timestamp_secs, &arrival_timestamp_frac,
523 /*rtcp_timestamp=*/nullptr, &stats.packets_sent,
524 &stats.bytes_sent, &stats.reports_count)) {
525 stats.last_remote_timestamp.Set(remote_timestamp_secs,
526 remote_timestamp_frac);
527 stats.last_arrival_timestamp.Set(arrival_timestamp_secs,
528 arrival_timestamp_frac);
529 return stats;
530 }
531 return absl::nullopt;
532}
533
Tommi3a5742c2020-05-20 09:32:51 +0200534// (REMB) Receiver Estimated Max Bitrate.
535void ModuleRtpRtcpImpl2::SetRemb(int64_t bitrate_bps,
536 std::vector<uint32_t> ssrcs) {
537 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
538}
539
540void ModuleRtpRtcpImpl2::UnsetRemb() {
541 rtcp_sender_.UnsetRemb();
542}
543
544void ModuleRtpRtcpImpl2::SetExtmapAllowMixed(bool extmap_allow_mixed) {
545 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
546}
547
Tommi3a5742c2020-05-20 09:32:51 +0200548void ModuleRtpRtcpImpl2::RegisterRtpHeaderExtension(absl::string_view uri,
549 int id) {
550 bool registered =
551 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
552 RTC_CHECK(registered);
553}
554
555int32_t ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
556 const RTPExtensionType type) {
557 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
558}
559void ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
560 absl::string_view uri) {
561 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
562}
563
Tommi3a5742c2020-05-20 09:32:51 +0200564void ModuleRtpRtcpImpl2::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
565 rtcp_sender_.SetTmmbn(std::move(bounding_set));
566}
567
568// Send a Negative acknowledgment packet.
569int32_t ModuleRtpRtcpImpl2::SendNACK(const uint16_t* nack_list,
570 const uint16_t size) {
571 uint16_t nack_length = size;
572 uint16_t start_id = 0;
573 int64_t now_ms = clock_->TimeInMilliseconds();
574 if (TimeToSendFullNackList(now_ms)) {
575 nack_last_time_sent_full_ms_ = now_ms;
576 } else {
577 // Only send extended list.
578 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
579 // Last sequence number is the same, do not send list.
580 return 0;
581 }
582 // Send new sequence numbers.
583 for (int i = 0; i < size; ++i) {
584 if (nack_last_seq_number_sent_ == nack_list[i]) {
585 start_id = i + 1;
586 break;
587 }
588 }
589 nack_length = size - start_id;
590 }
591
592 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
593 // numbers per RTCP packet.
594 if (nack_length > kRtcpMaxNackFields) {
595 nack_length = kRtcpMaxNackFields;
596 }
597 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
598
599 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
600 &nack_list[start_id]);
601}
602
603void ModuleRtpRtcpImpl2::SendNack(
604 const std::vector<uint16_t>& sequence_numbers) {
605 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
606 sequence_numbers.data());
607}
608
609bool ModuleRtpRtcpImpl2::TimeToSendFullNackList(int64_t now) const {
610 // Use RTT from RtcpRttStats class if provided.
611 int64_t rtt = rtt_ms();
612 if (rtt == 0) {
613 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
614 }
615
616 const int64_t kStartUpRttMs = 100;
617 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
618 if (rtt == 0) {
619 wait_time = kStartUpRttMs;
620 }
621
622 // Send a full NACK list once within every |wait_time|.
623 return now - nack_last_time_sent_full_ms_ > wait_time;
624}
625
626// Store the sent packets, needed to answer to Negative acknowledgment requests.
627void ModuleRtpRtcpImpl2::SetStorePacketsStatus(const bool enable,
628 const uint16_t number_to_store) {
629 rtp_sender_->packet_history.SetStorePacketsStatus(
630 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
631 : RtpPacketHistory::StorageMode::kDisabled,
632 number_to_store);
633}
634
635bool ModuleRtpRtcpImpl2::StorePackets() const {
636 return rtp_sender_->packet_history.GetStorageMode() !=
637 RtpPacketHistory::StorageMode::kDisabled;
638}
639
640void ModuleRtpRtcpImpl2::SendCombinedRtcpPacket(
641 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
642 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
643}
644
645int32_t ModuleRtpRtcpImpl2::SendLossNotification(uint16_t last_decoded_seq_num,
646 uint16_t last_received_seq_num,
647 bool decodability_flag,
648 bool buffering_allowed) {
649 return rtcp_sender_.SendLossNotification(
650 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
651 decodability_flag, buffering_allowed);
652}
653
654void ModuleRtpRtcpImpl2::SetRemoteSSRC(const uint32_t ssrc) {
655 // Inform about the incoming SSRC.
656 rtcp_sender_.SetRemoteSSRC(ssrc);
657 rtcp_receiver_.SetRemoteSSRC(ssrc);
658}
659
Tommi3a5742c2020-05-20 09:32:51 +0200660RtpSendRates ModuleRtpRtcpImpl2::GetSendRates() const {
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200661 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200662 return rtp_sender_->packet_sender.GetSendRates();
663}
664
665void ModuleRtpRtcpImpl2::OnRequestSendReport() {
666 SendRTCP(kRtcpSr);
667}
668
669void ModuleRtpRtcpImpl2::OnReceivedNack(
670 const std::vector<uint16_t>& nack_sequence_numbers) {
671 if (!rtp_sender_)
672 return;
673
674 if (!StorePackets() || nack_sequence_numbers.empty()) {
675 return;
676 }
677 // Use RTT from RtcpRttStats class if provided.
678 int64_t rtt = rtt_ms();
679 if (rtt == 0) {
680 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
681 }
682 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
683}
684
685void ModuleRtpRtcpImpl2::OnReceivedRtcpReportBlocks(
686 const ReportBlockList& report_blocks) {
687 if (rtp_sender_) {
688 uint32_t ssrc = SSRC();
689 absl::optional<uint32_t> rtx_ssrc;
690 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
691 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
692 }
693
694 for (const RTCPReportBlock& report_block : report_blocks) {
695 if (ssrc == report_block.source_ssrc) {
696 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
697 report_block.extended_highest_sequence_number);
698 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
699 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
700 report_block.extended_highest_sequence_number);
701 }
702 }
703 }
704}
705
Tommi3a5742c2020-05-20 09:32:51 +0200706void ModuleRtpRtcpImpl2::set_rtt_ms(int64_t rtt_ms) {
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200707 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200708 {
Markus Handellf7303e62020-07-09 01:34:42 +0200709 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200710 rtt_ms_ = rtt_ms;
711 }
712 if (rtp_sender_) {
713 rtp_sender_->packet_history.SetRtt(rtt_ms);
714 }
715}
716
717int64_t ModuleRtpRtcpImpl2::rtt_ms() const {
Markus Handellf7303e62020-07-09 01:34:42 +0200718 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200719 return rtt_ms_;
720}
721
722void ModuleRtpRtcpImpl2::SetVideoBitrateAllocation(
723 const VideoBitrateAllocation& bitrate) {
724 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
725}
726
727RTPSender* ModuleRtpRtcpImpl2::RtpSender() {
728 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
729}
730
731const RTPSender* ModuleRtpRtcpImpl2::RtpSender() const {
732 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
733}
734
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200735void ModuleRtpRtcpImpl2::PeriodicUpdate() {
736 RTC_DCHECK_RUN_ON(worker_queue_);
737
738 Timestamp check_since = clock_->CurrentTime() - kRttUpdateInterval;
739 absl::optional<TimeDelta> rtt =
740 rtcp_receiver_.OnPeriodicRttUpdate(check_since, rtcp_sender_.Sending());
741 if (rtt) {
742 rtt_stats_->OnRttUpdate(rtt->ms());
743 set_rtt_ms(rtt->ms());
744 }
745
746 // kTmmbrTimeoutIntervalMs is 25 seconds, so an order of seconds.
747 // Instead of this polling approach, consider having an optional timer in the
748 // RTCPReceiver class that is started/stopped based on the state of
749 // rtcp_sender_.TMMBR().
750 if (rtcp_sender_.TMMBR() && rtcp_receiver_.UpdateTmmbrTimers())
751 rtcp_receiver_.NotifyTmmbrUpdated();
752}
753
Tommi3a5742c2020-05-20 09:32:51 +0200754} // namespace webrtc