blob: 25c92b267a1c79121b57660878ce43d9d852adcb [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
Markus Handellc6b9ac72021-06-18 13:44:51 +020022#include "absl/types/optional.h"
Tommi3a5742c2020-05-20 09:32:51 +020023#include "api/transport/field_trial_based_config.h"
Markus Handellc6b9ac72021-06-18 13:44:51 +020024#include "api/units/timestamp.h"
Tommi3a5742c2020-05-20 09:32:51 +020025#include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
26#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
27#include "rtc_base/checks.h"
28#include "rtc_base/logging.h"
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +010029#include "system_wrappers/include/ntp_time.h"
Tommi3a5742c2020-05-20 09:32:51 +020030
31#ifdef _WIN32
32// Disable warning C4355: 'this' : used in base member initializer list.
33#pragma warning(disable : 4355)
34#endif
35
36namespace webrtc {
37namespace {
38const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5;
Tommi3a5742c2020-05-20 09:32:51 +020039const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020040
41constexpr TimeDelta kRttUpdateInterval = TimeDelta::Millis(1000);
Tommi3a5742c2020-05-20 09:32:51 +020042} // namespace
43
44ModuleRtpRtcpImpl2::RtpSenderContext::RtpSenderContext(
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020045 const RtpRtcpInterface::Configuration& config)
Tommi3a5742c2020-05-20 09:32:51 +020046 : packet_history(config.clock, config.enable_rtx_padding_prioritization),
47 packet_sender(config, &packet_history),
Erik Språng1d50cb62020-07-02 17:41:32 +020048 non_paced_sender(&packet_sender, this),
Tommi3a5742c2020-05-20 09:32:51 +020049 packet_generator(
50 config,
51 &packet_history,
52 config.paced_sender ? config.paced_sender : &non_paced_sender) {}
Erik Språng1d50cb62020-07-02 17:41:32 +020053void ModuleRtpRtcpImpl2::RtpSenderContext::AssignSequenceNumber(
54 RtpPacketToSend* packet) {
55 packet_generator.AssignSequenceNumber(packet);
56}
Tommi3a5742c2020-05-20 09:32:51 +020057
Tommi3a5742c2020-05-20 09:32:51 +020058ModuleRtpRtcpImpl2::ModuleRtpRtcpImpl2(const Configuration& configuration)
Niels Moller2accc7d2021-01-12 15:54:16 +000059 : worker_queue_(TaskQueueBase::Current()),
Markus Handell2e3edc12021-06-18 13:44:13 +020060 rtcp_sender_(
61 RTCPSender::Configuration::FromRtpRtcpConfiguration(configuration)),
Tommi3a5742c2020-05-20 09:32:51 +020062 rtcp_receiver_(configuration, this),
63 clock_(configuration.clock),
Tommi3a5742c2020-05-20 09:32:51 +020064 last_rtt_process_time_(clock_->TimeInMilliseconds()),
65 next_process_time_(clock_->TimeInMilliseconds() +
66 kRtpRtcpMaxIdleTimeProcessMs),
67 packet_overhead_(28), // IPV4 UDP.
68 nack_last_time_sent_full_ms_(0),
69 nack_last_seq_number_sent_(0),
70 remote_bitrate_(configuration.remote_bitrate_estimator),
71 rtt_stats_(configuration.rtt_stats),
72 rtt_ms_(0) {
Niels Moller2accc7d2021-01-12 15:54:16 +000073 RTC_DCHECK(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +020074 process_thread_checker_.Detach();
Tommi08be9ba2021-06-15 23:01:57 +020075 packet_sequence_checker_.Detach();
Tommi3a5742c2020-05-20 09:32:51 +020076 if (!configuration.receiver_only) {
77 rtp_sender_ = std::make_unique<RtpSenderContext>(configuration);
78 // Make sure rtcp sender use same timestamp offset as rtp sender.
79 rtcp_sender_.SetTimestampOffset(
80 rtp_sender_->packet_generator.TimestampOffset());
81 }
82
83 // Set default packet size limit.
84 // TODO(nisse): Kind-of duplicates
85 // webrtc::VideoSendStream::Config::Rtp::kDefaultMaxPacketSize.
86 const size_t kTcpOverIpv4HeaderSize = 40;
87 SetMaxRtpPacketSize(IP_PACKET_SIZE - kTcpOverIpv4HeaderSize);
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020088
89 if (rtt_stats_) {
90 rtt_update_task_ = RepeatingTaskHandle::DelayedStart(
91 worker_queue_, kRttUpdateInterval, [this]() {
92 PeriodicUpdate();
93 return kRttUpdateInterval;
94 });
95 }
Tommi3a5742c2020-05-20 09:32:51 +020096}
97
98ModuleRtpRtcpImpl2::~ModuleRtpRtcpImpl2() {
Niels Moller2accc7d2021-01-12 15:54:16 +000099 RTC_DCHECK_RUN_ON(worker_queue_);
100 rtt_update_task_.Stop();
101}
102
103// static
104std::unique_ptr<ModuleRtpRtcpImpl2> ModuleRtpRtcpImpl2::Create(
105 const Configuration& configuration) {
106 RTC_DCHECK(configuration.clock);
107 RTC_DCHECK(TaskQueueBase::Current());
108 return std::make_unique<ModuleRtpRtcpImpl2>(configuration);
Tomas Gunnarssonfae05622020-06-03 08:54:39 +0200109}
110
Tommi3a5742c2020-05-20 09:32:51 +0200111// Returns the number of milliseconds until the module want a worker thread
112// to call Process.
113int64_t ModuleRtpRtcpImpl2::TimeUntilNextProcess() {
114 RTC_DCHECK_RUN_ON(&process_thread_checker_);
115 return std::max<int64_t>(0,
116 next_process_time_ - clock_->TimeInMilliseconds());
117}
118
119// Process any pending tasks such as timeouts (non time critical events).
120void ModuleRtpRtcpImpl2::Process() {
121 RTC_DCHECK_RUN_ON(&process_thread_checker_);
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200122
123 const Timestamp now = clock_->CurrentTime();
124
Tommi3a5742c2020-05-20 09:32:51 +0200125 // TODO(bugs.webrtc.org/11581): Figure out why we need to call Process() 200
126 // times a second.
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200127 next_process_time_ = now.ms() + kRtpRtcpMaxIdleTimeProcessMs;
Tommi3a5742c2020-05-20 09:32:51 +0200128
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200129 // TODO(bugs.webrtc.org/11581): once we don't use Process() to trigger
130 // calls to SendRTCP(), the only remaining timer will require remote_bitrate_
131 // to be not null. In that case, we can disable the timer when it is null.
132 if (remote_bitrate_ && rtcp_sender_.Sending() && rtcp_sender_.TMMBR()) {
133 unsigned int target_bitrate = 0;
134 std::vector<unsigned int> ssrcs;
135 if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
136 if (!ssrcs.empty()) {
137 target_bitrate = target_bitrate / ssrcs.size();
Tommi3a5742c2020-05-20 09:32:51 +0200138 }
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200139 rtcp_sender_.SetTargetBitrate(target_bitrate);
Tommi3a5742c2020-05-20 09:32:51 +0200140 }
141 }
142
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200143 // TODO(bugs.webrtc.org/11581): Run this on a separate set of delayed tasks
144 // based off of next_time_to_send_rtcp_ in RTCPSender.
Tommi3a5742c2020-05-20 09:32:51 +0200145 if (rtcp_sender_.TimeToSendRTCPReport())
146 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
Tommi3a5742c2020-05-20 09:32:51 +0200147}
148
149void ModuleRtpRtcpImpl2::SetRtxSendStatus(int mode) {
150 rtp_sender_->packet_generator.SetRtxStatus(mode);
151}
152
153int ModuleRtpRtcpImpl2::RtxSendStatus() const {
154 return rtp_sender_ ? rtp_sender_->packet_generator.RtxStatus() : kRtxOff;
155}
156
157void ModuleRtpRtcpImpl2::SetRtxSendPayloadType(int payload_type,
158 int associated_payload_type) {
159 rtp_sender_->packet_generator.SetRtxPayloadType(payload_type,
160 associated_payload_type);
161}
162
163absl::optional<uint32_t> ModuleRtpRtcpImpl2::RtxSsrc() const {
164 return rtp_sender_ ? rtp_sender_->packet_generator.RtxSsrc() : absl::nullopt;
165}
166
167absl::optional<uint32_t> ModuleRtpRtcpImpl2::FlexfecSsrc() const {
168 if (rtp_sender_) {
169 return rtp_sender_->packet_generator.FlexfecSsrc();
170 }
171 return absl::nullopt;
172}
173
174void ModuleRtpRtcpImpl2::IncomingRtcpPacket(const uint8_t* rtcp_packet,
175 const size_t length) {
Tommi08be9ba2021-06-15 23:01:57 +0200176 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
Tommi3a5742c2020-05-20 09:32:51 +0200177 rtcp_receiver_.IncomingPacket(rtcp_packet, length);
178}
179
180void ModuleRtpRtcpImpl2::RegisterSendPayloadFrequency(int payload_type,
181 int payload_frequency) {
182 rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
183}
184
185int32_t ModuleRtpRtcpImpl2::DeRegisterSendPayload(const int8_t payload_type) {
186 return 0;
187}
188
189uint32_t ModuleRtpRtcpImpl2::StartTimestamp() const {
190 return rtp_sender_->packet_generator.TimestampOffset();
191}
192
193// Configure start timestamp, default is a random number.
194void ModuleRtpRtcpImpl2::SetStartTimestamp(const uint32_t timestamp) {
195 rtcp_sender_.SetTimestampOffset(timestamp);
196 rtp_sender_->packet_generator.SetTimestampOffset(timestamp);
197 rtp_sender_->packet_sender.SetTimestampOffset(timestamp);
198}
199
200uint16_t ModuleRtpRtcpImpl2::SequenceNumber() const {
201 return rtp_sender_->packet_generator.SequenceNumber();
202}
203
204// Set SequenceNumber, default is a random number.
205void ModuleRtpRtcpImpl2::SetSequenceNumber(const uint16_t seq_num) {
206 rtp_sender_->packet_generator.SetSequenceNumber(seq_num);
207}
208
209void ModuleRtpRtcpImpl2::SetRtpState(const RtpState& rtp_state) {
210 rtp_sender_->packet_generator.SetRtpState(rtp_state);
Tommi3a5742c2020-05-20 09:32:51 +0200211 rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
212}
213
214void ModuleRtpRtcpImpl2::SetRtxState(const RtpState& rtp_state) {
215 rtp_sender_->packet_generator.SetRtxRtpState(rtp_state);
216}
217
218RtpState ModuleRtpRtcpImpl2::GetRtpState() const {
219 RtpState state = rtp_sender_->packet_generator.GetRtpState();
Tommi3a5742c2020-05-20 09:32:51 +0200220 return state;
221}
222
223RtpState ModuleRtpRtcpImpl2::GetRtxState() const {
224 return rtp_sender_->packet_generator.GetRtxRtpState();
225}
226
Tommi08be9ba2021-06-15 23:01:57 +0200227uint32_t ModuleRtpRtcpImpl2::local_media_ssrc() const {
228 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
229 RTC_DCHECK_EQ(rtcp_receiver_.local_media_ssrc(), rtcp_sender_.SSRC());
230 return rtcp_receiver_.local_media_ssrc();
231}
232
Tommi3a5742c2020-05-20 09:32:51 +0200233void ModuleRtpRtcpImpl2::SetRid(const std::string& rid) {
234 if (rtp_sender_) {
235 rtp_sender_->packet_generator.SetRid(rid);
236 }
237}
238
239void ModuleRtpRtcpImpl2::SetMid(const std::string& mid) {
240 if (rtp_sender_) {
241 rtp_sender_->packet_generator.SetMid(mid);
242 }
243 // TODO(bugs.webrtc.org/4050): If we end up supporting the MID SDES item for
244 // RTCP, this will need to be passed down to the RTCPSender also.
245}
246
247void ModuleRtpRtcpImpl2::SetCsrcs(const std::vector<uint32_t>& csrcs) {
248 rtcp_sender_.SetCsrcs(csrcs);
249 rtp_sender_->packet_generator.SetCsrcs(csrcs);
250}
251
252// TODO(pbos): Handle media and RTX streams separately (separate RTCP
253// feedbacks).
254RTCPSender::FeedbackState ModuleRtpRtcpImpl2::GetFeedbackState() {
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200255 // TODO(bugs.webrtc.org/11581): Called by potentially multiple threads.
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200256 // Mostly "Send*" methods. Make sure it's only called on the
Tomas Gunnarssona1163742020-06-29 17:41:22 +0200257 // construction thread.
258
Tommi3a5742c2020-05-20 09:32:51 +0200259 RTCPSender::FeedbackState state;
260 // This is called also when receiver_only is true. Hence below
261 // checks that rtp_sender_ exists.
262 if (rtp_sender_) {
263 StreamDataCounters rtp_stats;
264 StreamDataCounters rtx_stats;
265 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
266 state.packets_sent =
267 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
268 state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
269 rtx_stats.transmitted.payload_bytes;
270 state.send_bitrate =
271 rtp_sender_->packet_sender.GetSendRates().Sum().bps<uint32_t>();
272 }
273 state.receiver = &rtcp_receiver_;
274
Alessio Bazzica79011ef2021-03-10 14:52:35 +0100275 uint32_t received_ntp_secs = 0;
276 uint32_t received_ntp_frac = 0;
277 state.remote_sr = 0;
278 if (rtcp_receiver_.NTP(&received_ntp_secs, &received_ntp_frac,
279 /*rtcp_arrival_time_secs=*/&state.last_rr_ntp_secs,
280 /*rtcp_arrival_time_frac=*/&state.last_rr_ntp_frac,
Alessio Bazzica048adc72021-03-10 15:05:55 +0100281 /*rtcp_timestamp=*/nullptr,
282 /*remote_sender_packet_count=*/nullptr,
283 /*remote_sender_octet_count=*/nullptr,
284 /*remote_sender_reports_count=*/nullptr)) {
Alessio Bazzica79011ef2021-03-10 14:52:35 +0100285 state.remote_sr = ((received_ntp_secs & 0x0000ffff) << 16) +
286 ((received_ntp_frac & 0xffff0000) >> 16);
287 }
Tommi3a5742c2020-05-20 09:32:51 +0200288
289 state.last_xr_rtis = rtcp_receiver_.ConsumeReceivedXrReferenceTimeInfo();
290
291 return state;
292}
293
294// TODO(nisse): This method shouldn't be called for a receive-only
295// stream. Delete rtp_sender_ check as soon as all applications are
296// updated.
297int32_t ModuleRtpRtcpImpl2::SetSendingStatus(const bool sending) {
298 if (rtcp_sender_.Sending() != sending) {
299 // Sends RTCP BYE when going from true to false
Tomas Gunnarssondbcf5d32021-04-23 20:31:08 +0200300 rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending);
Tommi3a5742c2020-05-20 09:32:51 +0200301 }
302 return 0;
303}
304
305bool ModuleRtpRtcpImpl2::Sending() const {
306 return rtcp_sender_.Sending();
307}
308
309// TODO(nisse): This method shouldn't be called for a receive-only
310// stream. Delete rtp_sender_ check as soon as all applications are
311// updated.
312void ModuleRtpRtcpImpl2::SetSendingMediaStatus(const bool sending) {
313 if (rtp_sender_) {
314 rtp_sender_->packet_generator.SetSendingMediaStatus(sending);
315 } else {
316 RTC_DCHECK(!sending);
317 }
318}
319
320bool ModuleRtpRtcpImpl2::SendingMedia() const {
321 return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false;
322}
323
324bool ModuleRtpRtcpImpl2::IsAudioConfigured() const {
325 return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured()
326 : false;
327}
328
329void ModuleRtpRtcpImpl2::SetAsPartOfAllocation(bool part_of_allocation) {
330 RTC_CHECK(rtp_sender_);
331 rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation(
332 part_of_allocation);
333}
334
335bool ModuleRtpRtcpImpl2::OnSendingRtpFrame(uint32_t timestamp,
336 int64_t capture_time_ms,
337 int payload_type,
338 bool force_sender_report) {
339 if (!Sending())
340 return false;
341
Markus Handellc6b9ac72021-06-18 13:44:51 +0200342 // TODO(bugs.webrtc.org/12873): Migrate this method and it's users to use
343 // optional Timestamps.
344 absl::optional<Timestamp> capture_time;
345 if (capture_time_ms > 0) {
346 capture_time = Timestamp::Millis(capture_time_ms);
347 }
348 absl::optional<int> payload_type_optional;
349 if (payload_type >= 0)
350 payload_type_optional = payload_type;
351 rtcp_sender_.SetLastRtpTime(timestamp, capture_time, payload_type_optional);
Tommi3a5742c2020-05-20 09:32:51 +0200352 // Make sure an RTCP report isn't queued behind a key frame.
353 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
354 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
355
356 return true;
357}
358
359bool ModuleRtpRtcpImpl2::TrySendPacket(RtpPacketToSend* packet,
360 const PacedPacketInfo& pacing_info) {
361 RTC_DCHECK(rtp_sender_);
362 // TODO(sprang): Consider if we can remove this check.
363 if (!rtp_sender_->packet_generator.SendingMedia()) {
364 return false;
365 }
366 rtp_sender_->packet_sender.SendPacket(packet, pacing_info);
367 return true;
368}
369
Erik Språng1d50cb62020-07-02 17:41:32 +0200370void ModuleRtpRtcpImpl2::SetFecProtectionParams(
371 const FecProtectionParams& delta_params,
372 const FecProtectionParams& key_params) {
373 RTC_DCHECK(rtp_sender_);
374 rtp_sender_->packet_sender.SetFecProtectionParameters(delta_params,
375 key_params);
376}
377
378std::vector<std::unique_ptr<RtpPacketToSend>>
379ModuleRtpRtcpImpl2::FetchFecPackets() {
380 RTC_DCHECK(rtp_sender_);
381 auto fec_packets = rtp_sender_->packet_sender.FetchFecPackets();
382 if (!fec_packets.empty()) {
383 // Don't assign sequence numbers for FlexFEC packets.
384 const bool generate_sequence_numbers =
385 !rtp_sender_->packet_sender.FlexFecSsrc().has_value();
386 if (generate_sequence_numbers) {
387 for (auto& fec_packet : fec_packets) {
388 rtp_sender_->packet_generator.AssignSequenceNumber(fec_packet.get());
389 }
390 }
391 }
392 return fec_packets;
393}
394
Tommi3a5742c2020-05-20 09:32:51 +0200395void ModuleRtpRtcpImpl2::OnPacketsAcknowledged(
396 rtc::ArrayView<const uint16_t> sequence_numbers) {
397 RTC_DCHECK(rtp_sender_);
398 rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers);
399}
400
401bool ModuleRtpRtcpImpl2::SupportsPadding() const {
402 RTC_DCHECK(rtp_sender_);
403 return rtp_sender_->packet_generator.SupportsPadding();
404}
405
406bool ModuleRtpRtcpImpl2::SupportsRtxPayloadPadding() const {
407 RTC_DCHECK(rtp_sender_);
408 return rtp_sender_->packet_generator.SupportsRtxPayloadPadding();
409}
410
411std::vector<std::unique_ptr<RtpPacketToSend>>
412ModuleRtpRtcpImpl2::GeneratePadding(size_t target_size_bytes) {
413 RTC_DCHECK(rtp_sender_);
414 return rtp_sender_->packet_generator.GeneratePadding(
415 target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent());
416}
417
418std::vector<RtpSequenceNumberMap::Info>
419ModuleRtpRtcpImpl2::GetSentRtpPacketInfos(
420 rtc::ArrayView<const uint16_t> sequence_numbers) const {
421 RTC_DCHECK(rtp_sender_);
422 return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers);
423}
424
425size_t ModuleRtpRtcpImpl2::ExpectedPerPacketOverhead() const {
426 if (!rtp_sender_) {
427 return 0;
428 }
429 return rtp_sender_->packet_generator.ExpectedPerPacketOverhead();
430}
431
432size_t ModuleRtpRtcpImpl2::MaxRtpPacketSize() const {
433 RTC_DCHECK(rtp_sender_);
434 return rtp_sender_->packet_generator.MaxRtpPacketSize();
435}
436
437void ModuleRtpRtcpImpl2::SetMaxRtpPacketSize(size_t rtp_packet_size) {
438 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
439 << "rtp packet size too large: " << rtp_packet_size;
440 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
441 << "rtp packet size too small: " << rtp_packet_size;
442
443 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
444 if (rtp_sender_) {
445 rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size);
446 }
447}
448
449RtcpMode ModuleRtpRtcpImpl2::RTCP() const {
450 return rtcp_sender_.Status();
451}
452
453// Configure RTCP status i.e on/off.
454void ModuleRtpRtcpImpl2::SetRTCPStatus(const RtcpMode method) {
455 rtcp_sender_.SetRTCPStatus(method);
456}
457
458int32_t ModuleRtpRtcpImpl2::SetCNAME(const char* c_name) {
459 return rtcp_sender_.SetCNAME(c_name);
460}
461
Tommi3a5742c2020-05-20 09:32:51 +0200462int32_t ModuleRtpRtcpImpl2::RemoteNTP(uint32_t* received_ntpsecs,
463 uint32_t* received_ntpfrac,
464 uint32_t* rtcp_arrival_time_secs,
465 uint32_t* rtcp_arrival_time_frac,
466 uint32_t* rtcp_timestamp) const {
467 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
468 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
Alessio Bazzica048adc72021-03-10 15:05:55 +0100469 rtcp_timestamp,
470 /*remote_sender_packet_count=*/nullptr,
471 /*remote_sender_octet_count=*/nullptr,
472 /*remote_sender_reports_count=*/nullptr)
Tommi3a5742c2020-05-20 09:32:51 +0200473 ? 0
474 : -1;
475}
476
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200477// TODO(tommi): Check if |avg_rtt_ms|, |min_rtt_ms|, |max_rtt_ms| params are
478// actually used in practice (some callers ask for it but don't use it). It
479// could be that only |rtt| is needed and if so, then the fast path could be to
480// just call rtt_ms() and rely on the calculation being done periodically.
Tommi3a5742c2020-05-20 09:32:51 +0200481int32_t ModuleRtpRtcpImpl2::RTT(const uint32_t remote_ssrc,
482 int64_t* rtt,
483 int64_t* avg_rtt,
484 int64_t* min_rtt,
485 int64_t* max_rtt) const {
486 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
487 if (rtt && *rtt == 0) {
488 // Try to get RTT from RtcpRttStats class.
489 *rtt = rtt_ms();
490 }
491 return ret;
492}
493
494int64_t ModuleRtpRtcpImpl2::ExpectedRetransmissionTimeMs() const {
495 int64_t expected_retransmission_time_ms = rtt_ms();
496 if (expected_retransmission_time_ms > 0) {
497 return expected_retransmission_time_ms;
498 }
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200499 // No rtt available (|kRttUpdateInterval| not yet passed?), so try to
Tommi3a5742c2020-05-20 09:32:51 +0200500 // poll avg_rtt_ms directly from rtcp receiver.
501 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
502 &expected_retransmission_time_ms, nullptr,
503 nullptr) == 0) {
504 return expected_retransmission_time_ms;
505 }
506 return kDefaultExpectedRetransmissionTimeMs;
507}
508
509// Force a send of an RTCP packet.
510// Normal SR and RR are triggered via the process function.
511int32_t ModuleRtpRtcpImpl2::SendRTCP(RTCPPacketType packet_type) {
512 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
513}
514
Tommi3a5742c2020-05-20 09:32:51 +0200515void ModuleRtpRtcpImpl2::GetSendStreamDataCounters(
516 StreamDataCounters* rtp_counters,
517 StreamDataCounters* rtx_counters) const {
518 rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters);
519}
520
521// Received RTCP report.
Tommi3a5742c2020-05-20 09:32:51 +0200522std::vector<ReportBlockData> ModuleRtpRtcpImpl2::GetLatestReportBlockData()
523 const {
524 return rtcp_receiver_.GetLatestReportBlockData();
525}
526
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100527absl::optional<RtpRtcpInterface::SenderReportStats>
528ModuleRtpRtcpImpl2::GetSenderReportStats() const {
529 SenderReportStats stats;
530 uint32_t remote_timestamp_secs;
531 uint32_t remote_timestamp_frac;
532 uint32_t arrival_timestamp_secs;
533 uint32_t arrival_timestamp_frac;
534 if (rtcp_receiver_.NTP(&remote_timestamp_secs, &remote_timestamp_frac,
535 &arrival_timestamp_secs, &arrival_timestamp_frac,
536 /*rtcp_timestamp=*/nullptr, &stats.packets_sent,
537 &stats.bytes_sent, &stats.reports_count)) {
538 stats.last_remote_timestamp.Set(remote_timestamp_secs,
539 remote_timestamp_frac);
540 stats.last_arrival_timestamp.Set(arrival_timestamp_secs,
541 arrival_timestamp_frac);
542 return stats;
543 }
544 return absl::nullopt;
545}
546
Tommi3a5742c2020-05-20 09:32:51 +0200547// (REMB) Receiver Estimated Max Bitrate.
548void ModuleRtpRtcpImpl2::SetRemb(int64_t bitrate_bps,
549 std::vector<uint32_t> ssrcs) {
550 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
551}
552
553void ModuleRtpRtcpImpl2::UnsetRemb() {
554 rtcp_sender_.UnsetRemb();
555}
556
557void ModuleRtpRtcpImpl2::SetExtmapAllowMixed(bool extmap_allow_mixed) {
558 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
559}
560
Tommi3a5742c2020-05-20 09:32:51 +0200561void ModuleRtpRtcpImpl2::RegisterRtpHeaderExtension(absl::string_view uri,
562 int id) {
563 bool registered =
564 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
565 RTC_CHECK(registered);
566}
567
568int32_t ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
569 const RTPExtensionType type) {
570 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
571}
572void ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension(
573 absl::string_view uri) {
574 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
575}
576
Tommi3a5742c2020-05-20 09:32:51 +0200577void ModuleRtpRtcpImpl2::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
578 rtcp_sender_.SetTmmbn(std::move(bounding_set));
579}
580
581// Send a Negative acknowledgment packet.
582int32_t ModuleRtpRtcpImpl2::SendNACK(const uint16_t* nack_list,
583 const uint16_t size) {
584 uint16_t nack_length = size;
585 uint16_t start_id = 0;
586 int64_t now_ms = clock_->TimeInMilliseconds();
587 if (TimeToSendFullNackList(now_ms)) {
588 nack_last_time_sent_full_ms_ = now_ms;
589 } else {
590 // Only send extended list.
591 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
592 // Last sequence number is the same, do not send list.
593 return 0;
594 }
595 // Send new sequence numbers.
596 for (int i = 0; i < size; ++i) {
597 if (nack_last_seq_number_sent_ == nack_list[i]) {
598 start_id = i + 1;
599 break;
600 }
601 }
602 nack_length = size - start_id;
603 }
604
605 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
606 // numbers per RTCP packet.
607 if (nack_length > kRtcpMaxNackFields) {
608 nack_length = kRtcpMaxNackFields;
609 }
610 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
611
612 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
613 &nack_list[start_id]);
614}
615
616void ModuleRtpRtcpImpl2::SendNack(
617 const std::vector<uint16_t>& sequence_numbers) {
618 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
619 sequence_numbers.data());
620}
621
622bool ModuleRtpRtcpImpl2::TimeToSendFullNackList(int64_t now) const {
623 // Use RTT from RtcpRttStats class if provided.
624 int64_t rtt = rtt_ms();
625 if (rtt == 0) {
626 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
627 }
628
629 const int64_t kStartUpRttMs = 100;
630 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
631 if (rtt == 0) {
632 wait_time = kStartUpRttMs;
633 }
634
635 // Send a full NACK list once within every |wait_time|.
636 return now - nack_last_time_sent_full_ms_ > wait_time;
637}
638
639// Store the sent packets, needed to answer to Negative acknowledgment requests.
640void ModuleRtpRtcpImpl2::SetStorePacketsStatus(const bool enable,
641 const uint16_t number_to_store) {
642 rtp_sender_->packet_history.SetStorePacketsStatus(
643 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
644 : RtpPacketHistory::StorageMode::kDisabled,
645 number_to_store);
646}
647
648bool ModuleRtpRtcpImpl2::StorePackets() const {
649 return rtp_sender_->packet_history.GetStorageMode() !=
650 RtpPacketHistory::StorageMode::kDisabled;
651}
652
653void ModuleRtpRtcpImpl2::SendCombinedRtcpPacket(
654 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
655 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
656}
657
658int32_t ModuleRtpRtcpImpl2::SendLossNotification(uint16_t last_decoded_seq_num,
659 uint16_t last_received_seq_num,
660 bool decodability_flag,
661 bool buffering_allowed) {
662 return rtcp_sender_.SendLossNotification(
663 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
664 decodability_flag, buffering_allowed);
665}
666
667void ModuleRtpRtcpImpl2::SetRemoteSSRC(const uint32_t ssrc) {
668 // Inform about the incoming SSRC.
669 rtcp_sender_.SetRemoteSSRC(ssrc);
670 rtcp_receiver_.SetRemoteSSRC(ssrc);
671}
672
Tommi08be9ba2021-06-15 23:01:57 +0200673void ModuleRtpRtcpImpl2::SetLocalSsrc(uint32_t local_ssrc) {
674 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
675 rtcp_receiver_.set_local_media_ssrc(local_ssrc);
676 rtcp_sender_.SetSsrc(local_ssrc);
677}
678
Tommi3a5742c2020-05-20 09:32:51 +0200679RtpSendRates ModuleRtpRtcpImpl2::GetSendRates() const {
Tommi1050fbc2021-06-03 17:58:28 +0200680 // Typically called on the `rtp_transport_queue_` owned by an
681 // RtpTransportControllerSendInterface instance.
Tommi3a5742c2020-05-20 09:32:51 +0200682 return rtp_sender_->packet_sender.GetSendRates();
683}
684
685void ModuleRtpRtcpImpl2::OnRequestSendReport() {
686 SendRTCP(kRtcpSr);
687}
688
689void ModuleRtpRtcpImpl2::OnReceivedNack(
690 const std::vector<uint16_t>& nack_sequence_numbers) {
691 if (!rtp_sender_)
692 return;
693
694 if (!StorePackets() || nack_sequence_numbers.empty()) {
695 return;
696 }
697 // Use RTT from RtcpRttStats class if provided.
698 int64_t rtt = rtt_ms();
699 if (rtt == 0) {
700 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
701 }
702 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
703}
704
705void ModuleRtpRtcpImpl2::OnReceivedRtcpReportBlocks(
706 const ReportBlockList& report_blocks) {
707 if (rtp_sender_) {
708 uint32_t ssrc = SSRC();
709 absl::optional<uint32_t> rtx_ssrc;
710 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
711 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
712 }
713
714 for (const RTCPReportBlock& report_block : report_blocks) {
715 if (ssrc == report_block.source_ssrc) {
716 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
717 report_block.extended_highest_sequence_number);
718 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
719 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
720 report_block.extended_highest_sequence_number);
721 }
722 }
723 }
724}
725
Tommi3a5742c2020-05-20 09:32:51 +0200726void ModuleRtpRtcpImpl2::set_rtt_ms(int64_t rtt_ms) {
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200727 RTC_DCHECK_RUN_ON(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200728 {
Markus Handellf7303e62020-07-09 01:34:42 +0200729 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200730 rtt_ms_ = rtt_ms;
731 }
732 if (rtp_sender_) {
733 rtp_sender_->packet_history.SetRtt(rtt_ms);
734 }
735}
736
737int64_t ModuleRtpRtcpImpl2::rtt_ms() const {
Markus Handellf7303e62020-07-09 01:34:42 +0200738 MutexLock lock(&mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200739 return rtt_ms_;
740}
741
742void ModuleRtpRtcpImpl2::SetVideoBitrateAllocation(
743 const VideoBitrateAllocation& bitrate) {
744 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
745}
746
747RTPSender* ModuleRtpRtcpImpl2::RtpSender() {
748 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
749}
750
751const RTPSender* ModuleRtpRtcpImpl2::RtpSender() const {
752 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
753}
754
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200755void ModuleRtpRtcpImpl2::PeriodicUpdate() {
756 RTC_DCHECK_RUN_ON(worker_queue_);
757
758 Timestamp check_since = clock_->CurrentTime() - kRttUpdateInterval;
759 absl::optional<TimeDelta> rtt =
760 rtcp_receiver_.OnPeriodicRttUpdate(check_since, rtcp_sender_.Sending());
761 if (rtt) {
762 rtt_stats_->OnRttUpdate(rtt->ms());
763 set_rtt_ms(rtt->ms());
764 }
765
766 // kTmmbrTimeoutIntervalMs is 25 seconds, so an order of seconds.
767 // Instead of this polling approach, consider having an optional timer in the
768 // RTCPReceiver class that is started/stopped based on the state of
769 // rtcp_sender_.TMMBR().
770 if (rtcp_sender_.TMMBR() && rtcp_receiver_.UpdateTmmbrTimers())
771 rtcp_receiver_.NotifyTmmbrUpdated();
772}
773
Tommi3a5742c2020-05-20 09:32:51 +0200774} // namespace webrtc