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