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