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