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