Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 1 | /* |
| 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 Bazzica | bc1c93d | 2021-03-12 17:45:26 +0100 | [diff] [blame] | 27 | #include "system_wrappers/include/ntp_time.h" |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 28 | |
| 29 | #ifdef _WIN32 |
| 30 | // Disable warning C4355: 'this' : used in base member initializer list. |
| 31 | #pragma warning(disable : 4355) |
| 32 | #endif |
| 33 | |
| 34 | namespace webrtc { |
| 35 | namespace { |
| 36 | const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 37 | const int64_t kDefaultExpectedRetransmissionTimeMs = 125; |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 38 | |
| 39 | constexpr TimeDelta kRttUpdateInterval = TimeDelta::Millis(1000); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | ModuleRtpRtcpImpl2::RtpSenderContext::RtpSenderContext( |
Tomas Gunnarsson | f25761d | 2020-06-03 22:55:33 +0200 | [diff] [blame] | 43 | const RtpRtcpInterface::Configuration& config) |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 44 | : packet_history(config.clock, config.enable_rtx_padding_prioritization), |
| 45 | packet_sender(config, &packet_history), |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 46 | non_paced_sender(&packet_sender, this), |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 47 | packet_generator( |
| 48 | config, |
| 49 | &packet_history, |
| 50 | config.paced_sender ? config.paced_sender : &non_paced_sender) {} |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 51 | void ModuleRtpRtcpImpl2::RtpSenderContext::AssignSequenceNumber( |
| 52 | RtpPacketToSend* packet) { |
| 53 | packet_generator.AssignSequenceNumber(packet); |
| 54 | } |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 55 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 56 | ModuleRtpRtcpImpl2::ModuleRtpRtcpImpl2(const Configuration& configuration) |
Niels Moller | 2accc7d | 2021-01-12 15:54:16 +0000 | [diff] [blame] | 57 | : worker_queue_(TaskQueueBase::Current()), |
Tomas Gunnarsson | 473bbd8 | 2020-06-27 17:44:55 +0200 | [diff] [blame] | 58 | rtcp_sender_(configuration), |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 59 | rtcp_receiver_(configuration, this), |
| 60 | clock_(configuration.clock), |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 61 | 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 Moller | 2accc7d | 2021-01-12 15:54:16 +0000 | [diff] [blame] | 70 | RTC_DCHECK(worker_queue_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 71 | 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 Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 84 | |
| 85 | if (rtt_stats_) { |
| 86 | rtt_update_task_ = RepeatingTaskHandle::DelayedStart( |
| 87 | worker_queue_, kRttUpdateInterval, [this]() { |
| 88 | PeriodicUpdate(); |
| 89 | return kRttUpdateInterval; |
| 90 | }); |
| 91 | } |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ModuleRtpRtcpImpl2::~ModuleRtpRtcpImpl2() { |
Niels Moller | 2accc7d | 2021-01-12 15:54:16 +0000 | [diff] [blame] | 95 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 96 | rtt_update_task_.Stop(); |
| 97 | } |
| 98 | |
| 99 | // static |
| 100 | std::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 Gunnarsson | fae0562 | 2020-06-03 08:54:39 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 107 | // Returns the number of milliseconds until the module want a worker thread |
| 108 | // to call Process. |
| 109 | int64_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). |
| 116 | void ModuleRtpRtcpImpl2::Process() { |
| 117 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 118 | |
| 119 | const Timestamp now = clock_->CurrentTime(); |
| 120 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 121 | // TODO(bugs.webrtc.org/11581): Figure out why we need to call Process() 200 |
| 122 | // times a second. |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 123 | next_process_time_ = now.ms() + kRtpRtcpMaxIdleTimeProcessMs; |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 124 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 125 | // 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(); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 134 | } |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 135 | rtcp_sender_.SetTargetBitrate(target_bitrate); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 139 | // 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. |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 141 | if (rtcp_sender_.TimeToSendRTCPReport()) |
| 142 | rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void ModuleRtpRtcpImpl2::SetRtxSendStatus(int mode) { |
| 146 | rtp_sender_->packet_generator.SetRtxStatus(mode); |
| 147 | } |
| 148 | |
| 149 | int ModuleRtpRtcpImpl2::RtxSendStatus() const { |
| 150 | return rtp_sender_ ? rtp_sender_->packet_generator.RtxStatus() : kRtxOff; |
| 151 | } |
| 152 | |
| 153 | void ModuleRtpRtcpImpl2::SetRtxSendPayloadType(int payload_type, |
| 154 | int associated_payload_type) { |
| 155 | rtp_sender_->packet_generator.SetRtxPayloadType(payload_type, |
| 156 | associated_payload_type); |
| 157 | } |
| 158 | |
| 159 | absl::optional<uint32_t> ModuleRtpRtcpImpl2::RtxSsrc() const { |
| 160 | return rtp_sender_ ? rtp_sender_->packet_generator.RtxSsrc() : absl::nullopt; |
| 161 | } |
| 162 | |
| 163 | absl::optional<uint32_t> ModuleRtpRtcpImpl2::FlexfecSsrc() const { |
| 164 | if (rtp_sender_) { |
| 165 | return rtp_sender_->packet_generator.FlexfecSsrc(); |
| 166 | } |
| 167 | return absl::nullopt; |
| 168 | } |
| 169 | |
| 170 | void ModuleRtpRtcpImpl2::IncomingRtcpPacket(const uint8_t* rtcp_packet, |
| 171 | const size_t length) { |
| 172 | rtcp_receiver_.IncomingPacket(rtcp_packet, length); |
| 173 | } |
| 174 | |
| 175 | void ModuleRtpRtcpImpl2::RegisterSendPayloadFrequency(int payload_type, |
| 176 | int payload_frequency) { |
| 177 | rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency); |
| 178 | } |
| 179 | |
| 180 | int32_t ModuleRtpRtcpImpl2::DeRegisterSendPayload(const int8_t payload_type) { |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | uint32_t ModuleRtpRtcpImpl2::StartTimestamp() const { |
| 185 | return rtp_sender_->packet_generator.TimestampOffset(); |
| 186 | } |
| 187 | |
| 188 | // Configure start timestamp, default is a random number. |
| 189 | void 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 | |
| 195 | uint16_t ModuleRtpRtcpImpl2::SequenceNumber() const { |
| 196 | return rtp_sender_->packet_generator.SequenceNumber(); |
| 197 | } |
| 198 | |
| 199 | // Set SequenceNumber, default is a random number. |
| 200 | void ModuleRtpRtcpImpl2::SetSequenceNumber(const uint16_t seq_num) { |
| 201 | rtp_sender_->packet_generator.SetSequenceNumber(seq_num); |
| 202 | } |
| 203 | |
| 204 | void ModuleRtpRtcpImpl2::SetRtpState(const RtpState& rtp_state) { |
| 205 | rtp_sender_->packet_generator.SetRtpState(rtp_state); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 206 | rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp); |
| 207 | } |
| 208 | |
| 209 | void ModuleRtpRtcpImpl2::SetRtxState(const RtpState& rtp_state) { |
| 210 | rtp_sender_->packet_generator.SetRtxRtpState(rtp_state); |
| 211 | } |
| 212 | |
| 213 | RtpState ModuleRtpRtcpImpl2::GetRtpState() const { |
| 214 | RtpState state = rtp_sender_->packet_generator.GetRtpState(); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 215 | return state; |
| 216 | } |
| 217 | |
| 218 | RtpState ModuleRtpRtcpImpl2::GetRtxState() const { |
| 219 | return rtp_sender_->packet_generator.GetRtxRtpState(); |
| 220 | } |
| 221 | |
| 222 | void ModuleRtpRtcpImpl2::SetRid(const std::string& rid) { |
| 223 | if (rtp_sender_) { |
| 224 | rtp_sender_->packet_generator.SetRid(rid); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void 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 | |
| 236 | void 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). |
| 243 | RTCPSender::FeedbackState ModuleRtpRtcpImpl2::GetFeedbackState() { |
Tomas Gunnarsson | a116374 | 2020-06-29 17:41:22 +0200 | [diff] [blame] | 244 | // TODO(bugs.webrtc.org/11581): Called by potentially multiple threads. |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 245 | // Mostly "Send*" methods. Make sure it's only called on the |
Tomas Gunnarsson | a116374 | 2020-06-29 17:41:22 +0200 | [diff] [blame] | 246 | // construction thread. |
| 247 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 248 | 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 Bazzica | 79011ef | 2021-03-10 14:52:35 +0100 | [diff] [blame] | 264 | 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 Bazzica | 048adc7 | 2021-03-10 15:05:55 +0100 | [diff] [blame] | 270 | /*rtcp_timestamp=*/nullptr, |
| 271 | /*remote_sender_packet_count=*/nullptr, |
| 272 | /*remote_sender_octet_count=*/nullptr, |
| 273 | /*remote_sender_reports_count=*/nullptr)) { |
Alessio Bazzica | 79011ef | 2021-03-10 14:52:35 +0100 | [diff] [blame] | 274 | state.remote_sr = ((received_ntp_secs & 0x0000ffff) << 16) + |
| 275 | ((received_ntp_frac & 0xffff0000) >> 16); |
| 276 | } |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 277 | |
| 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. |
| 286 | int32_t ModuleRtpRtcpImpl2::SetSendingStatus(const bool sending) { |
| 287 | if (rtcp_sender_.Sending() != sending) { |
| 288 | // Sends RTCP BYE when going from true to false |
Tomas Gunnarsson | dbcf5d3 | 2021-04-23 20:31:08 +0200 | [diff] [blame] | 289 | rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | bool ModuleRtpRtcpImpl2::Sending() const { |
| 295 | return rtcp_sender_.Sending(); |
| 296 | } |
| 297 | |
| 298 | // TODO(nisse): This method shouldn't be called for a receive-only |
| 299 | // stream. Delete rtp_sender_ check as soon as all applications are |
| 300 | // updated. |
| 301 | void ModuleRtpRtcpImpl2::SetSendingMediaStatus(const bool sending) { |
| 302 | if (rtp_sender_) { |
| 303 | rtp_sender_->packet_generator.SetSendingMediaStatus(sending); |
| 304 | } else { |
| 305 | RTC_DCHECK(!sending); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | bool ModuleRtpRtcpImpl2::SendingMedia() const { |
| 310 | return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false; |
| 311 | } |
| 312 | |
| 313 | bool ModuleRtpRtcpImpl2::IsAudioConfigured() const { |
| 314 | return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured() |
| 315 | : false; |
| 316 | } |
| 317 | |
| 318 | void ModuleRtpRtcpImpl2::SetAsPartOfAllocation(bool part_of_allocation) { |
| 319 | RTC_CHECK(rtp_sender_); |
| 320 | rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation( |
| 321 | part_of_allocation); |
| 322 | } |
| 323 | |
| 324 | bool ModuleRtpRtcpImpl2::OnSendingRtpFrame(uint32_t timestamp, |
| 325 | int64_t capture_time_ms, |
| 326 | int payload_type, |
| 327 | bool force_sender_report) { |
| 328 | if (!Sending()) |
| 329 | return false; |
| 330 | |
| 331 | rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type); |
| 332 | // Make sure an RTCP report isn't queued behind a key frame. |
| 333 | if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report)) |
| 334 | rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport); |
| 335 | |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | bool ModuleRtpRtcpImpl2::TrySendPacket(RtpPacketToSend* packet, |
| 340 | const PacedPacketInfo& pacing_info) { |
| 341 | RTC_DCHECK(rtp_sender_); |
| 342 | // TODO(sprang): Consider if we can remove this check. |
| 343 | if (!rtp_sender_->packet_generator.SendingMedia()) { |
| 344 | return false; |
| 345 | } |
| 346 | rtp_sender_->packet_sender.SendPacket(packet, pacing_info); |
| 347 | return true; |
| 348 | } |
| 349 | |
Erik Språng | 1d50cb6 | 2020-07-02 17:41:32 +0200 | [diff] [blame] | 350 | void ModuleRtpRtcpImpl2::SetFecProtectionParams( |
| 351 | const FecProtectionParams& delta_params, |
| 352 | const FecProtectionParams& key_params) { |
| 353 | RTC_DCHECK(rtp_sender_); |
| 354 | rtp_sender_->packet_sender.SetFecProtectionParameters(delta_params, |
| 355 | key_params); |
| 356 | } |
| 357 | |
| 358 | std::vector<std::unique_ptr<RtpPacketToSend>> |
| 359 | ModuleRtpRtcpImpl2::FetchFecPackets() { |
| 360 | RTC_DCHECK(rtp_sender_); |
| 361 | auto fec_packets = rtp_sender_->packet_sender.FetchFecPackets(); |
| 362 | if (!fec_packets.empty()) { |
| 363 | // Don't assign sequence numbers for FlexFEC packets. |
| 364 | const bool generate_sequence_numbers = |
| 365 | !rtp_sender_->packet_sender.FlexFecSsrc().has_value(); |
| 366 | if (generate_sequence_numbers) { |
| 367 | for (auto& fec_packet : fec_packets) { |
| 368 | rtp_sender_->packet_generator.AssignSequenceNumber(fec_packet.get()); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return fec_packets; |
| 373 | } |
| 374 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 375 | void ModuleRtpRtcpImpl2::OnPacketsAcknowledged( |
| 376 | rtc::ArrayView<const uint16_t> sequence_numbers) { |
| 377 | RTC_DCHECK(rtp_sender_); |
| 378 | rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers); |
| 379 | } |
| 380 | |
| 381 | bool ModuleRtpRtcpImpl2::SupportsPadding() const { |
| 382 | RTC_DCHECK(rtp_sender_); |
| 383 | return rtp_sender_->packet_generator.SupportsPadding(); |
| 384 | } |
| 385 | |
| 386 | bool ModuleRtpRtcpImpl2::SupportsRtxPayloadPadding() const { |
| 387 | RTC_DCHECK(rtp_sender_); |
| 388 | return rtp_sender_->packet_generator.SupportsRtxPayloadPadding(); |
| 389 | } |
| 390 | |
| 391 | std::vector<std::unique_ptr<RtpPacketToSend>> |
| 392 | ModuleRtpRtcpImpl2::GeneratePadding(size_t target_size_bytes) { |
| 393 | RTC_DCHECK(rtp_sender_); |
| 394 | return rtp_sender_->packet_generator.GeneratePadding( |
| 395 | target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent()); |
| 396 | } |
| 397 | |
| 398 | std::vector<RtpSequenceNumberMap::Info> |
| 399 | ModuleRtpRtcpImpl2::GetSentRtpPacketInfos( |
| 400 | rtc::ArrayView<const uint16_t> sequence_numbers) const { |
| 401 | RTC_DCHECK(rtp_sender_); |
| 402 | return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers); |
| 403 | } |
| 404 | |
| 405 | size_t ModuleRtpRtcpImpl2::ExpectedPerPacketOverhead() const { |
| 406 | if (!rtp_sender_) { |
| 407 | return 0; |
| 408 | } |
| 409 | return rtp_sender_->packet_generator.ExpectedPerPacketOverhead(); |
| 410 | } |
| 411 | |
| 412 | size_t ModuleRtpRtcpImpl2::MaxRtpPacketSize() const { |
| 413 | RTC_DCHECK(rtp_sender_); |
| 414 | return rtp_sender_->packet_generator.MaxRtpPacketSize(); |
| 415 | } |
| 416 | |
| 417 | void ModuleRtpRtcpImpl2::SetMaxRtpPacketSize(size_t rtp_packet_size) { |
| 418 | RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE) |
| 419 | << "rtp packet size too large: " << rtp_packet_size; |
| 420 | RTC_DCHECK_GT(rtp_packet_size, packet_overhead_) |
| 421 | << "rtp packet size too small: " << rtp_packet_size; |
| 422 | |
| 423 | rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size); |
| 424 | if (rtp_sender_) { |
| 425 | rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | RtcpMode ModuleRtpRtcpImpl2::RTCP() const { |
| 430 | return rtcp_sender_.Status(); |
| 431 | } |
| 432 | |
| 433 | // Configure RTCP status i.e on/off. |
| 434 | void ModuleRtpRtcpImpl2::SetRTCPStatus(const RtcpMode method) { |
| 435 | rtcp_sender_.SetRTCPStatus(method); |
| 436 | } |
| 437 | |
| 438 | int32_t ModuleRtpRtcpImpl2::SetCNAME(const char* c_name) { |
| 439 | return rtcp_sender_.SetCNAME(c_name); |
| 440 | } |
| 441 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 442 | int32_t ModuleRtpRtcpImpl2::RemoteNTP(uint32_t* received_ntpsecs, |
| 443 | uint32_t* received_ntpfrac, |
| 444 | uint32_t* rtcp_arrival_time_secs, |
| 445 | uint32_t* rtcp_arrival_time_frac, |
| 446 | uint32_t* rtcp_timestamp) const { |
| 447 | return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac, |
| 448 | rtcp_arrival_time_secs, rtcp_arrival_time_frac, |
Alessio Bazzica | 048adc7 | 2021-03-10 15:05:55 +0100 | [diff] [blame] | 449 | rtcp_timestamp, |
| 450 | /*remote_sender_packet_count=*/nullptr, |
| 451 | /*remote_sender_octet_count=*/nullptr, |
| 452 | /*remote_sender_reports_count=*/nullptr) |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 453 | ? 0 |
| 454 | : -1; |
| 455 | } |
| 456 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 457 | // TODO(tommi): Check if |avg_rtt_ms|, |min_rtt_ms|, |max_rtt_ms| params are |
| 458 | // actually used in practice (some callers ask for it but don't use it). It |
| 459 | // could be that only |rtt| is needed and if so, then the fast path could be to |
| 460 | // just call rtt_ms() and rely on the calculation being done periodically. |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 461 | int32_t ModuleRtpRtcpImpl2::RTT(const uint32_t remote_ssrc, |
| 462 | int64_t* rtt, |
| 463 | int64_t* avg_rtt, |
| 464 | int64_t* min_rtt, |
| 465 | int64_t* max_rtt) const { |
| 466 | int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt); |
| 467 | if (rtt && *rtt == 0) { |
| 468 | // Try to get RTT from RtcpRttStats class. |
| 469 | *rtt = rtt_ms(); |
| 470 | } |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | int64_t ModuleRtpRtcpImpl2::ExpectedRetransmissionTimeMs() const { |
| 475 | int64_t expected_retransmission_time_ms = rtt_ms(); |
| 476 | if (expected_retransmission_time_ms > 0) { |
| 477 | return expected_retransmission_time_ms; |
| 478 | } |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 479 | // No rtt available (|kRttUpdateInterval| not yet passed?), so try to |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 480 | // poll avg_rtt_ms directly from rtcp receiver. |
| 481 | if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr, |
| 482 | &expected_retransmission_time_ms, nullptr, |
| 483 | nullptr) == 0) { |
| 484 | return expected_retransmission_time_ms; |
| 485 | } |
| 486 | return kDefaultExpectedRetransmissionTimeMs; |
| 487 | } |
| 488 | |
| 489 | // Force a send of an RTCP packet. |
| 490 | // Normal SR and RR are triggered via the process function. |
| 491 | int32_t ModuleRtpRtcpImpl2::SendRTCP(RTCPPacketType packet_type) { |
| 492 | return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type); |
| 493 | } |
| 494 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 495 | void ModuleRtpRtcpImpl2::GetSendStreamDataCounters( |
| 496 | StreamDataCounters* rtp_counters, |
| 497 | StreamDataCounters* rtx_counters) const { |
| 498 | rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters); |
| 499 | } |
| 500 | |
| 501 | // Received RTCP report. |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 502 | std::vector<ReportBlockData> ModuleRtpRtcpImpl2::GetLatestReportBlockData() |
| 503 | const { |
| 504 | return rtcp_receiver_.GetLatestReportBlockData(); |
| 505 | } |
| 506 | |
Alessio Bazzica | bc1c93d | 2021-03-12 17:45:26 +0100 | [diff] [blame] | 507 | absl::optional<RtpRtcpInterface::SenderReportStats> |
| 508 | ModuleRtpRtcpImpl2::GetSenderReportStats() const { |
| 509 | SenderReportStats stats; |
| 510 | uint32_t remote_timestamp_secs; |
| 511 | uint32_t remote_timestamp_frac; |
| 512 | uint32_t arrival_timestamp_secs; |
| 513 | uint32_t arrival_timestamp_frac; |
| 514 | if (rtcp_receiver_.NTP(&remote_timestamp_secs, &remote_timestamp_frac, |
| 515 | &arrival_timestamp_secs, &arrival_timestamp_frac, |
| 516 | /*rtcp_timestamp=*/nullptr, &stats.packets_sent, |
| 517 | &stats.bytes_sent, &stats.reports_count)) { |
| 518 | stats.last_remote_timestamp.Set(remote_timestamp_secs, |
| 519 | remote_timestamp_frac); |
| 520 | stats.last_arrival_timestamp.Set(arrival_timestamp_secs, |
| 521 | arrival_timestamp_frac); |
| 522 | return stats; |
| 523 | } |
| 524 | return absl::nullopt; |
| 525 | } |
| 526 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 527 | // (REMB) Receiver Estimated Max Bitrate. |
| 528 | void ModuleRtpRtcpImpl2::SetRemb(int64_t bitrate_bps, |
| 529 | std::vector<uint32_t> ssrcs) { |
| 530 | rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs)); |
| 531 | } |
| 532 | |
| 533 | void ModuleRtpRtcpImpl2::UnsetRemb() { |
| 534 | rtcp_sender_.UnsetRemb(); |
| 535 | } |
| 536 | |
| 537 | void ModuleRtpRtcpImpl2::SetExtmapAllowMixed(bool extmap_allow_mixed) { |
| 538 | rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed); |
| 539 | } |
| 540 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 541 | void ModuleRtpRtcpImpl2::RegisterRtpHeaderExtension(absl::string_view uri, |
| 542 | int id) { |
| 543 | bool registered = |
| 544 | rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id); |
| 545 | RTC_CHECK(registered); |
| 546 | } |
| 547 | |
| 548 | int32_t ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension( |
| 549 | const RTPExtensionType type) { |
| 550 | return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type); |
| 551 | } |
| 552 | void ModuleRtpRtcpImpl2::DeregisterSendRtpHeaderExtension( |
| 553 | absl::string_view uri) { |
| 554 | rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri); |
| 555 | } |
| 556 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 557 | void ModuleRtpRtcpImpl2::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) { |
| 558 | rtcp_sender_.SetTmmbn(std::move(bounding_set)); |
| 559 | } |
| 560 | |
| 561 | // Send a Negative acknowledgment packet. |
| 562 | int32_t ModuleRtpRtcpImpl2::SendNACK(const uint16_t* nack_list, |
| 563 | const uint16_t size) { |
| 564 | uint16_t nack_length = size; |
| 565 | uint16_t start_id = 0; |
| 566 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 567 | if (TimeToSendFullNackList(now_ms)) { |
| 568 | nack_last_time_sent_full_ms_ = now_ms; |
| 569 | } else { |
| 570 | // Only send extended list. |
| 571 | if (nack_last_seq_number_sent_ == nack_list[size - 1]) { |
| 572 | // Last sequence number is the same, do not send list. |
| 573 | return 0; |
| 574 | } |
| 575 | // Send new sequence numbers. |
| 576 | for (int i = 0; i < size; ++i) { |
| 577 | if (nack_last_seq_number_sent_ == nack_list[i]) { |
| 578 | start_id = i + 1; |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | nack_length = size - start_id; |
| 583 | } |
| 584 | |
| 585 | // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence |
| 586 | // numbers per RTCP packet. |
| 587 | if (nack_length > kRtcpMaxNackFields) { |
| 588 | nack_length = kRtcpMaxNackFields; |
| 589 | } |
| 590 | nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1]; |
| 591 | |
| 592 | return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length, |
| 593 | &nack_list[start_id]); |
| 594 | } |
| 595 | |
| 596 | void ModuleRtpRtcpImpl2::SendNack( |
| 597 | const std::vector<uint16_t>& sequence_numbers) { |
| 598 | rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(), |
| 599 | sequence_numbers.data()); |
| 600 | } |
| 601 | |
| 602 | bool ModuleRtpRtcpImpl2::TimeToSendFullNackList(int64_t now) const { |
| 603 | // Use RTT from RtcpRttStats class if provided. |
| 604 | int64_t rtt = rtt_ms(); |
| 605 | if (rtt == 0) { |
| 606 | rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL); |
| 607 | } |
| 608 | |
| 609 | const int64_t kStartUpRttMs = 100; |
| 610 | int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5. |
| 611 | if (rtt == 0) { |
| 612 | wait_time = kStartUpRttMs; |
| 613 | } |
| 614 | |
| 615 | // Send a full NACK list once within every |wait_time|. |
| 616 | return now - nack_last_time_sent_full_ms_ > wait_time; |
| 617 | } |
| 618 | |
| 619 | // Store the sent packets, needed to answer to Negative acknowledgment requests. |
| 620 | void ModuleRtpRtcpImpl2::SetStorePacketsStatus(const bool enable, |
| 621 | const uint16_t number_to_store) { |
| 622 | rtp_sender_->packet_history.SetStorePacketsStatus( |
| 623 | enable ? RtpPacketHistory::StorageMode::kStoreAndCull |
| 624 | : RtpPacketHistory::StorageMode::kDisabled, |
| 625 | number_to_store); |
| 626 | } |
| 627 | |
| 628 | bool ModuleRtpRtcpImpl2::StorePackets() const { |
| 629 | return rtp_sender_->packet_history.GetStorageMode() != |
| 630 | RtpPacketHistory::StorageMode::kDisabled; |
| 631 | } |
| 632 | |
| 633 | void ModuleRtpRtcpImpl2::SendCombinedRtcpPacket( |
| 634 | std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) { |
| 635 | rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets)); |
| 636 | } |
| 637 | |
| 638 | int32_t ModuleRtpRtcpImpl2::SendLossNotification(uint16_t last_decoded_seq_num, |
| 639 | uint16_t last_received_seq_num, |
| 640 | bool decodability_flag, |
| 641 | bool buffering_allowed) { |
| 642 | return rtcp_sender_.SendLossNotification( |
| 643 | GetFeedbackState(), last_decoded_seq_num, last_received_seq_num, |
| 644 | decodability_flag, buffering_allowed); |
| 645 | } |
| 646 | |
| 647 | void ModuleRtpRtcpImpl2::SetRemoteSSRC(const uint32_t ssrc) { |
| 648 | // Inform about the incoming SSRC. |
| 649 | rtcp_sender_.SetRemoteSSRC(ssrc); |
| 650 | rtcp_receiver_.SetRemoteSSRC(ssrc); |
| 651 | } |
| 652 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 653 | RtpSendRates ModuleRtpRtcpImpl2::GetSendRates() const { |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 654 | // Typically called on the `rtp_transport_queue_` owned by an |
| 655 | // RtpTransportControllerSendInterface instance. |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 656 | return rtp_sender_->packet_sender.GetSendRates(); |
| 657 | } |
| 658 | |
| 659 | void ModuleRtpRtcpImpl2::OnRequestSendReport() { |
| 660 | SendRTCP(kRtcpSr); |
| 661 | } |
| 662 | |
| 663 | void ModuleRtpRtcpImpl2::OnReceivedNack( |
| 664 | const std::vector<uint16_t>& nack_sequence_numbers) { |
| 665 | if (!rtp_sender_) |
| 666 | return; |
| 667 | |
| 668 | if (!StorePackets() || nack_sequence_numbers.empty()) { |
| 669 | return; |
| 670 | } |
| 671 | // Use RTT from RtcpRttStats class if provided. |
| 672 | int64_t rtt = rtt_ms(); |
| 673 | if (rtt == 0) { |
| 674 | rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL); |
| 675 | } |
| 676 | rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt); |
| 677 | } |
| 678 | |
| 679 | void ModuleRtpRtcpImpl2::OnReceivedRtcpReportBlocks( |
| 680 | const ReportBlockList& report_blocks) { |
| 681 | if (rtp_sender_) { |
| 682 | uint32_t ssrc = SSRC(); |
| 683 | absl::optional<uint32_t> rtx_ssrc; |
| 684 | if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) { |
| 685 | rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc(); |
| 686 | } |
| 687 | |
| 688 | for (const RTCPReportBlock& report_block : report_blocks) { |
| 689 | if (ssrc == report_block.source_ssrc) { |
| 690 | rtp_sender_->packet_generator.OnReceivedAckOnSsrc( |
| 691 | report_block.extended_highest_sequence_number); |
| 692 | } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) { |
| 693 | rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc( |
| 694 | report_block.extended_highest_sequence_number); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 700 | void ModuleRtpRtcpImpl2::set_rtt_ms(int64_t rtt_ms) { |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 701 | RTC_DCHECK_RUN_ON(worker_queue_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 702 | { |
Markus Handell | f7303e6 | 2020-07-09 01:34:42 +0200 | [diff] [blame] | 703 | MutexLock lock(&mutex_rtt_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 704 | rtt_ms_ = rtt_ms; |
| 705 | } |
| 706 | if (rtp_sender_) { |
| 707 | rtp_sender_->packet_history.SetRtt(rtt_ms); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | int64_t ModuleRtpRtcpImpl2::rtt_ms() const { |
Markus Handell | f7303e6 | 2020-07-09 01:34:42 +0200 | [diff] [blame] | 712 | MutexLock lock(&mutex_rtt_); |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 713 | return rtt_ms_; |
| 714 | } |
| 715 | |
| 716 | void ModuleRtpRtcpImpl2::SetVideoBitrateAllocation( |
| 717 | const VideoBitrateAllocation& bitrate) { |
| 718 | rtcp_sender_.SetVideoBitrateAllocation(bitrate); |
| 719 | } |
| 720 | |
| 721 | RTPSender* ModuleRtpRtcpImpl2::RtpSender() { |
| 722 | return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr; |
| 723 | } |
| 724 | |
| 725 | const RTPSender* ModuleRtpRtcpImpl2::RtpSender() const { |
| 726 | return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr; |
| 727 | } |
| 728 | |
Tomas Gunnarsson | ba0ba71 | 2020-07-01 08:53:21 +0200 | [diff] [blame] | 729 | void ModuleRtpRtcpImpl2::PeriodicUpdate() { |
| 730 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 731 | |
| 732 | Timestamp check_since = clock_->CurrentTime() - kRttUpdateInterval; |
| 733 | absl::optional<TimeDelta> rtt = |
| 734 | rtcp_receiver_.OnPeriodicRttUpdate(check_since, rtcp_sender_.Sending()); |
| 735 | if (rtt) { |
| 736 | rtt_stats_->OnRttUpdate(rtt->ms()); |
| 737 | set_rtt_ms(rtt->ms()); |
| 738 | } |
| 739 | |
| 740 | // kTmmbrTimeoutIntervalMs is 25 seconds, so an order of seconds. |
| 741 | // Instead of this polling approach, consider having an optional timer in the |
| 742 | // RTCPReceiver class that is started/stopped based on the state of |
| 743 | // rtcp_sender_.TMMBR(). |
| 744 | if (rtcp_sender_.TMMBR() && rtcp_receiver_.UpdateTmmbrTimers()) |
| 745 | rtcp_receiver_.NotifyTmmbrUpdated(); |
| 746 | } |
| 747 | |
Tommi | 3a5742c | 2020-05-20 09:32:51 +0200 | [diff] [blame] | 748 | } // namespace webrtc |