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