tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtcp_receiver.cc (43846B)


      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/rtcp_receiver.h"
     12 
     13 #include <algorithm>
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <cstring>
     17 #include <iterator>
     18 #include <limits>
     19 #include <map>
     20 #include <memory>
     21 #include <optional>
     22 #include <utility>
     23 #include <vector>
     24 
     25 #include "absl/algorithm/container.h"
     26 #include "absl/base/attributes.h"
     27 #include "api/array_view.h"
     28 #include "api/environment/environment.h"
     29 #include "api/field_trials_view.h"
     30 #include "api/sequence_checker.h"
     31 #include "api/transport/network_types.h"
     32 #include "api/units/data_rate.h"
     33 #include "api/units/time_delta.h"
     34 #include "api/units/timestamp.h"
     35 #include "api/video/video_bitrate_allocation.h"
     36 #include "api/video/video_bitrate_allocator.h"
     37 #include "api/video/video_codec_constants.h"
     38 #include "modules/rtp_rtcp/include/report_block_data.h"
     39 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     40 #include "modules/rtp_rtcp/source/ntp_time_util.h"
     41 #include "modules/rtp_rtcp/source/rtcp_packet/app.h"
     42 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
     43 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
     44 #include "modules/rtp_rtcp/source/rtcp_packet/congestion_control_feedback.h"
     45 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
     46 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
     47 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
     48 #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
     49 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
     50 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
     51 #include "modules/rtp_rtcp/source/rtcp_packet/psfb.h"
     52 #include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
     53 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
     54 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
     55 #include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
     56 #include "modules/rtp_rtcp/source/rtcp_packet/rtpfb.h"
     57 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
     58 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
     59 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
     60 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
     61 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
     62 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
     63 #include "modules/rtp_rtcp/source/tmmbr_help.h"
     64 #include "rtc_base/checks.h"
     65 #include "rtc_base/containers/flat_map.h"
     66 #include "rtc_base/logging.h"
     67 #include "rtc_base/synchronization/mutex.h"
     68 #include "rtc_base/trace_event.h"
     69 #include "system_wrappers/include/clock.h"
     70 #include "system_wrappers/include/ntp_time.h"
     71 
     72 namespace webrtc {
     73 namespace {
     74 
     75 using rtcp::CommonHeader;
     76 using rtcp::ReportBlock;
     77 
     78 // The number of RTCP time intervals needed to trigger a timeout.
     79 constexpr int kRrTimeoutIntervals = 3;
     80 
     81 constexpr TimeDelta kTmmbrTimeoutInterval = TimeDelta::Seconds(25);
     82 constexpr TimeDelta kMaxWarningLogInterval = TimeDelta::Seconds(10);
     83 constexpr TimeDelta kRtcpMinFrameLength = TimeDelta::Millis(17);
     84 
     85 // Maximum number of received RRTRs that will be stored.
     86 constexpr size_t kMaxNumberOfStoredRrtrs = 300;
     87 
     88 constexpr TimeDelta kDefaultVideoReportInterval = TimeDelta::Seconds(1);
     89 constexpr TimeDelta kDefaultAudioReportInterval = TimeDelta::Seconds(5);
     90 
     91 // Returns true if the `timestamp` has exceeded the |interval *
     92 // kRrTimeoutIntervals| period and was reset (set to PlusInfinity()). Returns
     93 // false if the timer was either already reset or if it has not expired.
     94 bool ResetTimestampIfExpired(const Timestamp now,
     95                             Timestamp& timestamp,
     96                             TimeDelta interval) {
     97  if (timestamp.IsInfinite() ||
     98      now <= timestamp + interval * kRrTimeoutIntervals) {
     99    return false;
    100  }
    101 
    102  timestamp = Timestamp::PlusInfinity();
    103  return true;
    104 }
    105 
    106 }  // namespace
    107 
    108 RTCPReceiver::RegisteredSsrcs::RegisteredSsrcs(
    109    const RtpRtcpInterface::Configuration& config) {
    110  packet_sequence_checker_.Detach();
    111  ssrcs_.push_back(config.local_media_ssrc);
    112  if (config.rtx_send_ssrc) {
    113    ssrcs_.push_back(*config.rtx_send_ssrc);
    114  }
    115  if (config.fec_generator) {
    116    std::optional<uint32_t> flexfec_ssrc = config.fec_generator->FecSsrc();
    117    if (flexfec_ssrc) {
    118      ssrcs_.push_back(*flexfec_ssrc);
    119    }
    120  }
    121  // Ensure that the RegisteredSsrcs can inline the SSRCs.
    122  RTC_DCHECK_LE(ssrcs_.size(), kMaxSimulcastStreams);
    123 }
    124 
    125 bool RTCPReceiver::RegisteredSsrcs::contains(uint32_t ssrc) const {
    126  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
    127  return absl::c_linear_search(ssrcs_, ssrc);
    128 }
    129 
    130 uint32_t RTCPReceiver::RegisteredSsrcs::media_ssrc() const {
    131  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
    132  return ssrcs_[kMediaSsrcIndex];
    133 }
    134 
    135 void RTCPReceiver::RegisteredSsrcs::set_media_ssrc(uint32_t ssrc) {
    136  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
    137  ssrcs_[kMediaSsrcIndex] = ssrc;
    138 }
    139 
    140 struct RTCPReceiver::PacketInformation {
    141  uint32_t packet_type_flags = 0;  // RTCPPacketTypeFlags bit field.
    142 
    143  uint32_t remote_ssrc = 0;
    144  std::vector<uint16_t> nack_sequence_numbers;
    145  std::vector<ReportBlockData> report_block_datas;
    146  std::optional<TimeDelta> rtt;
    147  uint32_t receiver_estimated_max_bitrate_bps = 0;
    148  std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
    149  std::optional<rtcp::CongestionControlFeedback> congestion_control_feedback;
    150  std::optional<VideoBitrateAllocation> target_bitrate_allocation;
    151  std::optional<NetworkStateEstimate> network_state_estimate;
    152  std::unique_ptr<rtcp::LossNotification> loss_notification;
    153 };
    154 
    155 RTCPReceiver::RTCPReceiver(const Environment& env,
    156                           const RtpRtcpInterface::Configuration& config,
    157                           ModuleRtpRtcp* owner)
    158    : env_(env),
    159      receiver_only_(config.receiver_only),
    160      enable_congestion_controller_feedback_(env_.field_trials().IsEnabled(
    161          "WebRTC-RFC8888CongestionControlFeedback")),
    162      rtp_rtcp_(owner),
    163      registered_ssrcs_(config),
    164      network_link_rtcp_observer_(config.network_link_rtcp_observer),
    165      rtcp_event_observer_(config.rtcp_event_observer),
    166      rtcp_intra_frame_observer_(config.intra_frame_callback),
    167      rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
    168      network_state_estimate_observer_(config.network_state_estimate_observer),
    169      bitrate_allocation_observer_(config.bitrate_allocation_observer),
    170      report_interval_(config.rtcp_report_interval_ms > 0
    171                           ? TimeDelta::Millis(config.rtcp_report_interval_ms)
    172                           : (config.audio ? kDefaultAudioReportInterval
    173                                           : kDefaultVideoReportInterval)),
    174      // TODO(bugs.webrtc.org/10774): Remove fallback.
    175      remote_ssrc_(0),
    176      xr_rrtr_status_(config.non_sender_rtt_measurement),
    177      oldest_tmmbr_info_(Timestamp::Zero()),
    178      cname_callback_(config.rtcp_cname_callback),
    179      report_block_data_observer_(config.report_block_data_observer),
    180      packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
    181      num_skipped_packets_(0),
    182      last_skipped_packets_warning_(env_.clock().CurrentTime()) {
    183  RTC_DCHECK(owner);
    184 }
    185 
    186 RTCPReceiver::~RTCPReceiver() {}
    187 
    188 void RTCPReceiver::IncomingPacket(ArrayView<const uint8_t> packet) {
    189  if (packet.empty()) {
    190    RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
    191    return;
    192  }
    193 
    194  PacketInformation packet_information;
    195  if (!ParseCompoundPacket(packet, &packet_information))
    196    return;
    197  TriggerCallbacksFromRtcpPacket(packet_information);
    198 }
    199 
    200 // This method is only used by test and legacy code, so we should be able to
    201 // remove it soon.
    202 int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
    203  MutexLock lock(&rtcp_receiver_lock_);
    204  return last_received_rb_.IsFinite() ? last_received_rb_.ms() : 0;
    205 }
    206 
    207 void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
    208  MutexLock lock(&rtcp_receiver_lock_);
    209  // New SSRC reset old reports.
    210  remote_sender_.last_arrival_ntp_timestamp.Reset();
    211  remote_ssrc_ = ssrc;
    212 }
    213 
    214 void RTCPReceiver::set_local_media_ssrc(uint32_t ssrc) {
    215  registered_ssrcs_.set_media_ssrc(ssrc);
    216 }
    217 
    218 uint32_t RTCPReceiver::local_media_ssrc() const {
    219  return registered_ssrcs_.media_ssrc();
    220 }
    221 
    222 uint32_t RTCPReceiver::RemoteSSRC() const {
    223  MutexLock lock(&rtcp_receiver_lock_);
    224  return remote_ssrc_;
    225 }
    226 
    227 void RTCPReceiver::RttStats::AddRtt(TimeDelta rtt) {
    228  last_rtt_ = rtt;
    229  sum_rtt_ += rtt;
    230  ++num_rtts_;
    231 }
    232 
    233 std::optional<TimeDelta> RTCPReceiver::AverageRtt() const {
    234  MutexLock lock(&rtcp_receiver_lock_);
    235  auto it = rtts_.find(remote_ssrc_);
    236  if (it == rtts_.end()) {
    237    return std::nullopt;
    238  }
    239  return it->second.average_rtt();
    240 }
    241 
    242 std::optional<TimeDelta> RTCPReceiver::LastRtt() const {
    243  MutexLock lock(&rtcp_receiver_lock_);
    244  auto it = rtts_.find(remote_ssrc_);
    245  if (it == rtts_.end()) {
    246    return std::nullopt;
    247  }
    248  return it->second.last_rtt();
    249 }
    250 
    251 RTCPReceiver::NonSenderRttStats RTCPReceiver::GetNonSenderRTT() const {
    252  MutexLock lock(&rtcp_receiver_lock_);
    253  auto it = non_sender_rtts_.find(remote_ssrc_);
    254  if (it == non_sender_rtts_.end()) {
    255    return {};
    256  }
    257  return it->second;
    258 }
    259 
    260 void RTCPReceiver::SetNonSenderRttMeasurement(bool enabled) {
    261  MutexLock lock(&rtcp_receiver_lock_);
    262  xr_rrtr_status_ = enabled;
    263 }
    264 
    265 std::optional<TimeDelta> RTCPReceiver::GetAndResetXrRrRtt() {
    266  MutexLock lock(&rtcp_receiver_lock_);
    267  std::optional<TimeDelta> rtt = xr_rr_rtt_;
    268  xr_rr_rtt_ = std::nullopt;
    269  return rtt;
    270 }
    271 
    272 // Called regularly (1/sec) on the worker thread to do rtt  calculations.
    273 std::optional<TimeDelta> RTCPReceiver::OnPeriodicRttUpdate(Timestamp newer_than,
    274                                                           bool sending) {
    275  // Running on the worker thread (same as construction thread).
    276  std::optional<TimeDelta> rtt;
    277 
    278  if (sending) {
    279    // Check if we've received a report block within the last kRttUpdateInterval
    280    // amount of time.
    281    MutexLock lock(&rtcp_receiver_lock_);
    282    if (last_received_rb_.IsInfinite() || last_received_rb_ > newer_than) {
    283      TimeDelta max_rtt = TimeDelta::MinusInfinity();
    284      for (const auto& rtt_stats : rtts_) {
    285        if (rtt_stats.second.last_rtt() > max_rtt) {
    286          max_rtt = rtt_stats.second.last_rtt();
    287        }
    288      }
    289      if (max_rtt.IsFinite()) {
    290        rtt = max_rtt;
    291      }
    292    }
    293 
    294    // Check for expired timers and if so, log and reset.
    295    Timestamp now = env_.clock().CurrentTime();
    296    if (RtcpRrTimeoutLocked(now)) {
    297      RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
    298    } else if (RtcpRrSequenceNumberTimeoutLocked(now)) {
    299      RTC_LOG_F(LS_WARNING) << "Timeout: No increase in RTCP RR extended "
    300                               "highest sequence number.";
    301    }
    302  } else {
    303    // Report rtt from receiver.
    304    rtt = GetAndResetXrRrRtt();
    305  }
    306 
    307  return rtt;
    308 }
    309 
    310 std::optional<RtpRtcpInterface::SenderReportStats>
    311 RTCPReceiver::GetSenderReportStats() const {
    312  MutexLock lock(&rtcp_receiver_lock_);
    313  if (!remote_sender_.last_arrival_ntp_timestamp.Valid()) {
    314    return std::nullopt;
    315  }
    316 
    317  return remote_sender_;
    318 }
    319 
    320 std::vector<rtcp::ReceiveTimeInfo>
    321 RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
    322  MutexLock lock(&rtcp_receiver_lock_);
    323 
    324  const size_t last_xr_rtis_size = std::min(
    325      received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
    326  std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
    327  last_xr_rtis.reserve(last_xr_rtis_size);
    328 
    329  const uint32_t now_ntp = CompactNtp(env_.clock().CurrentNtpTime());
    330 
    331  for (size_t i = 0; i < last_xr_rtis_size; ++i) {
    332    RrtrInformation& rrtr = received_rrtrs_.front();
    333    last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
    334                              now_ntp - rrtr.local_receive_mid_ntp_time);
    335    received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
    336    received_rrtrs_.pop_front();
    337  }
    338 
    339  return last_xr_rtis;
    340 }
    341 
    342 void RTCPReceiver::RemoteRTCPSenderInfo(uint32_t* packet_count,
    343                                        uint32_t* octet_count,
    344                                        int64_t* ntp_timestamp_ms,
    345                                        int64_t* remote_ntp_timestamp_ms) const {
    346  MutexLock lock(&rtcp_receiver_lock_);
    347  *packet_count = remote_sender_.packets_sent;
    348  *octet_count = remote_sender_.bytes_sent;
    349  *ntp_timestamp_ms = remote_sender_.last_arrival_ntp_timestamp.ToMs();
    350  *remote_ntp_timestamp_ms = remote_sender_.last_remote_ntp_timestamp.ToMs();
    351 }
    352 
    353 std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
    354  std::vector<ReportBlockData> result;
    355  MutexLock lock(&rtcp_receiver_lock_);
    356  for (const auto& report : received_report_blocks_) {
    357    result.push_back(report.second);
    358  }
    359  return result;
    360 }
    361 
    362 bool RTCPReceiver::ParseCompoundPacket(ArrayView<const uint8_t> packet,
    363                                       PacketInformation* packet_information) {
    364  MutexLock lock(&rtcp_receiver_lock_);
    365 
    366  CommonHeader rtcp_block;
    367  // If a sender report is received but no DLRR, we need to reset the
    368  // roundTripTime stat according to the standard, see
    369  // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
    370  struct RtcpReceivedBlock {
    371    bool sender_report = false;
    372    bool dlrr = false;
    373  };
    374  // For each remote SSRC we store if we've received a sender report or a DLRR
    375  // block.
    376  flat_map<uint32_t, RtcpReceivedBlock> received_blocks;
    377  bool valid = true;
    378  for (const uint8_t* next_block = packet.begin();
    379       valid && next_block != packet.end();
    380       next_block = rtcp_block.NextPacket()) {
    381    ptrdiff_t remaining_blocks_size = packet.end() - next_block;
    382    RTC_DCHECK_GT(remaining_blocks_size, 0);
    383    if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
    384      valid = false;
    385      break;
    386    }
    387 
    388    switch (rtcp_block.type()) {
    389      case rtcp::SenderReport::kPacketType:
    390        valid = HandleSenderReport(rtcp_block, packet_information);
    391        received_blocks[packet_information->remote_ssrc].sender_report = true;
    392        break;
    393      case rtcp::ReceiverReport::kPacketType:
    394        valid = HandleReceiverReport(rtcp_block, packet_information);
    395        break;
    396      case rtcp::Sdes::kPacketType:
    397        valid = HandleSdes(rtcp_block, packet_information);
    398        break;
    399      case rtcp::ExtendedReports::kPacketType: {
    400        bool contains_dlrr = false;
    401        uint32_t ssrc = 0;
    402        valid = HandleXr(rtcp_block, packet_information, contains_dlrr, ssrc);
    403        if (contains_dlrr) {
    404          received_blocks[ssrc].dlrr = true;
    405        }
    406        break;
    407      }
    408      case rtcp::Bye::kPacketType:
    409        valid = HandleBye(rtcp_block);
    410        break;
    411      case rtcp::App::kPacketType:
    412        valid = HandleApp(rtcp_block, packet_information);
    413        break;
    414      case rtcp::Rtpfb::kPacketType:
    415        switch (rtcp_block.fmt()) {
    416          case rtcp::Nack::kFeedbackMessageType:
    417            valid = HandleNack(rtcp_block, packet_information);
    418            break;
    419          case rtcp::Tmmbr::kFeedbackMessageType:
    420            valid = HandleTmmbr(rtcp_block, packet_information);
    421            break;
    422          case rtcp::Tmmbn::kFeedbackMessageType:
    423            valid = HandleTmmbn(rtcp_block, packet_information);
    424            break;
    425          case rtcp::RapidResyncRequest::kFeedbackMessageType:
    426            valid = HandleSrReq(rtcp_block, packet_information);
    427            break;
    428          case rtcp::TransportFeedback::kFeedbackMessageType:
    429            HandleTransportFeedback(rtcp_block, packet_information);
    430            break;
    431          case rtcp::CongestionControlFeedback::kFeedbackMessageType:
    432            if (enable_congestion_controller_feedback_) {
    433              valid = HandleCongestionControlFeedback(rtcp_block,
    434                                                      packet_information);
    435              break;
    436            }
    437            ABSL_FALLTHROUGH_INTENDED;
    438          default:
    439            ++num_skipped_packets_;
    440            break;
    441        }
    442        break;
    443      case rtcp::Psfb::kPacketType:
    444        switch (rtcp_block.fmt()) {
    445          case rtcp::Pli::kFeedbackMessageType:
    446            valid = HandlePli(rtcp_block, packet_information);
    447            break;
    448          case rtcp::Fir::kFeedbackMessageType:
    449            valid = HandleFir(rtcp_block, packet_information);
    450            break;
    451          case rtcp::Psfb::kAfbMessageType:
    452            HandlePsfbApp(rtcp_block, packet_information);
    453            break;
    454          default:
    455            ++num_skipped_packets_;
    456            break;
    457        }
    458        break;
    459      default:
    460        ++num_skipped_packets_;
    461        break;
    462    }
    463  }
    464 
    465  if (num_skipped_packets_ > 0) {
    466    const Timestamp now = env_.clock().CurrentTime();
    467    if (now - last_skipped_packets_warning_ >= kMaxWarningLogInterval) {
    468      last_skipped_packets_warning_ = now;
    469      RTC_LOG(LS_WARNING)
    470          << num_skipped_packets_
    471          << " RTCP blocks were skipped due to being malformed or of "
    472             "unrecognized/unsupported type, during the past "
    473          << kMaxWarningLogInterval << " period.";
    474    }
    475  }
    476 
    477  if (!valid) {
    478    ++num_skipped_packets_;
    479    return false;
    480  }
    481 
    482  for (const auto& rb : received_blocks) {
    483    if (rb.second.sender_report && !rb.second.dlrr) {
    484      auto rtt_stats = non_sender_rtts_.find(rb.first);
    485      if (rtt_stats != non_sender_rtts_.end()) {
    486        rtt_stats->second.Invalidate();
    487      }
    488    }
    489  }
    490 
    491  if (packet_type_counter_observer_) {
    492    packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
    493        local_media_ssrc(), packet_type_counter_);
    494  }
    495 
    496  return true;
    497 }
    498 
    499 bool RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
    500                                      PacketInformation* packet_information) {
    501  rtcp::SenderReport sender_report;
    502  if (!sender_report.Parse(rtcp_block)) {
    503    return false;
    504  }
    505 
    506  const uint32_t remote_ssrc = sender_report.sender_ssrc();
    507 
    508  packet_information->remote_ssrc = remote_ssrc;
    509 
    510  UpdateTmmbrRemoteIsAlive(remote_ssrc);
    511 
    512  // Have I received RTP packets from this party?
    513  if (remote_ssrc_ == remote_ssrc) {
    514    // Only signal that we have received a SR when we accept one.
    515    packet_information->packet_type_flags |= kRtcpSr;
    516 
    517    remote_sender_.last_remote_ntp_timestamp = sender_report.ntp();
    518    remote_sender_.last_remote_rtp_timestamp = sender_report.rtp_timestamp();
    519    remote_sender_.last_arrival_timestamp = env_.clock().CurrentTime();
    520    remote_sender_.last_arrival_ntp_timestamp = env_.clock().CurrentNtpTime();
    521    remote_sender_.packets_sent = sender_report.sender_packet_count();
    522    remote_sender_.bytes_sent = sender_report.sender_octet_count();
    523    remote_sender_.reports_count++;
    524  } else {
    525    // We will only store the send report from one source, but
    526    // we will store all the receive blocks.
    527    packet_information->packet_type_flags |= kRtcpRr;
    528  }
    529 
    530  for (const rtcp::ReportBlock& report_block : sender_report.report_blocks()) {
    531    HandleReportBlock(report_block, packet_information, remote_ssrc);
    532  }
    533 
    534  return true;
    535 }
    536 
    537 bool RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
    538                                        PacketInformation* packet_information) {
    539  rtcp::ReceiverReport receiver_report;
    540  if (!receiver_report.Parse(rtcp_block)) {
    541    return false;
    542  }
    543 
    544  const uint32_t remote_ssrc = receiver_report.sender_ssrc();
    545 
    546  packet_information->remote_ssrc = remote_ssrc;
    547 
    548  UpdateTmmbrRemoteIsAlive(remote_ssrc);
    549 
    550  packet_information->packet_type_flags |= kRtcpRr;
    551 
    552  for (const ReportBlock& report_block : receiver_report.report_blocks()) {
    553    HandleReportBlock(report_block, packet_information, remote_ssrc);
    554  }
    555 
    556  return true;
    557 }
    558 
    559 void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
    560                                     PacketInformation* packet_information,
    561                                     uint32_t remote_ssrc) {
    562  // This will be called once per report block in the RTCP packet.
    563  // We filter out all report blocks that are not for us.
    564  // Each packet has max 31 RR blocks.
    565  //
    566  // We can calc RTT if we send a send report and get a report block back.
    567 
    568  // `report_block.source_ssrc()` is the SSRC identifier of the source to
    569  // which the information in this reception report block pertains.
    570 
    571  // Filter out all report blocks that are not for us.
    572  if (!registered_ssrcs_.contains(report_block.source_ssrc()))
    573    return;
    574 
    575  Timestamp now = env_.clock().CurrentTime();
    576  last_received_rb_ = now;
    577 
    578  ReportBlockData* report_block_data =
    579      &received_report_blocks_[report_block.source_ssrc()];
    580  if (report_block.extended_high_seq_num() >
    581      report_block_data->extended_highest_sequence_number()) {
    582    // We have successfully delivered new RTP packets to the remote side after
    583    // the last RR was sent from the remote side.
    584    last_increased_sequence_number_ = last_received_rb_;
    585  }
    586  NtpTime now_ntp = env_.clock().ConvertTimestampToNtpTime(now);
    587  // Number of seconds since 1900 January 1 00:00 GMT (see
    588  // https://tools.ietf.org/html/rfc868).
    589  report_block_data->SetReportBlock(remote_ssrc, report_block,
    590                                    Clock::NtpToUtc(now_ntp), now);
    591 
    592  uint32_t send_time_ntp = report_block.last_sr();
    593  // RFC3550, section 6.4.1, LSR field discription states:
    594  // If no SR has been received yet, the field is set to zero.
    595  // Receiver rtp_rtcp module is not expected to calculate rtt using
    596  // Sender Reports even if it accidentally can.
    597  if (send_time_ntp != 0) {
    598    uint32_t delay_ntp = report_block.delay_since_last_sr();
    599    // Local NTP time.
    600    uint32_t receive_time_ntp = CompactNtp(now_ntp);
    601 
    602    // RTT in 1/(2^16) seconds.
    603    uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
    604    // Convert to 1/1000 seconds (milliseconds).
    605    TimeDelta rtt = CompactNtpRttToTimeDelta(rtt_ntp);
    606    report_block_data->AddRoundTripTimeSample(rtt);
    607    if (report_block.source_ssrc() == local_media_ssrc()) {
    608      rtts_[remote_ssrc].AddRtt(rtt);
    609    }
    610 
    611    packet_information->rtt = rtt;
    612  }
    613 
    614  packet_information->report_block_datas.push_back(*report_block_data);
    615 }
    616 
    617 RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
    618    uint32_t remote_ssrc) {
    619  // Create or find receive information.
    620  TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
    621  // Update that this remote is alive.
    622  tmmbr_info->last_time_received = env_.clock().CurrentTime();
    623  return tmmbr_info;
    624 }
    625 
    626 void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
    627  auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
    628  if (tmmbr_it != tmmbr_infos_.end())
    629    tmmbr_it->second.last_time_received = env_.clock().CurrentTime();
    630 }
    631 
    632 RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
    633    uint32_t remote_ssrc) {
    634  auto it = tmmbr_infos_.find(remote_ssrc);
    635  if (it == tmmbr_infos_.end())
    636    return nullptr;
    637  return &it->second;
    638 }
    639 
    640 // These two methods (RtcpRrTimeout and RtcpRrSequenceNumberTimeout) only exist
    641 // for tests and legacy code (rtp_rtcp_impl.cc). We should be able to to delete
    642 // the methods and require that access to the locked variables only happens on
    643 // the worker thread and thus no locking is needed.
    644 bool RTCPReceiver::RtcpRrTimeout() {
    645  MutexLock lock(&rtcp_receiver_lock_);
    646  return RtcpRrTimeoutLocked(env_.clock().CurrentTime());
    647 }
    648 
    649 bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
    650  MutexLock lock(&rtcp_receiver_lock_);
    651  return RtcpRrSequenceNumberTimeoutLocked(env_.clock().CurrentTime());
    652 }
    653 
    654 bool RTCPReceiver::UpdateTmmbrTimers() {
    655  MutexLock lock(&rtcp_receiver_lock_);
    656 
    657  Timestamp timeout = env_.clock().CurrentTime() - kTmmbrTimeoutInterval;
    658 
    659  if (oldest_tmmbr_info_ >= timeout)
    660    return false;
    661 
    662  bool update_bounding_set = false;
    663  oldest_tmmbr_info_ = Timestamp::MinusInfinity();
    664  for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
    665    TmmbrInformation* tmmbr_info = &tmmbr_it->second;
    666    if (tmmbr_info->last_time_received > Timestamp::Zero()) {
    667      if (tmmbr_info->last_time_received < timeout) {
    668        // No rtcp packet for the last 5 regular intervals, reset limitations.
    669        tmmbr_info->tmmbr.clear();
    670        // Prevent that we call this over and over again.
    671        tmmbr_info->last_time_received = Timestamp::Zero();
    672        // Send new TMMBN to all channels using the default codec.
    673        update_bounding_set = true;
    674      } else if (oldest_tmmbr_info_ == Timestamp::MinusInfinity() ||
    675                 tmmbr_info->last_time_received < oldest_tmmbr_info_) {
    676        oldest_tmmbr_info_ = tmmbr_info->last_time_received;
    677      }
    678      ++tmmbr_it;
    679    } else if (tmmbr_info->ready_for_delete) {
    680      // When we dont have a `last_time_received` and the object is marked
    681      // ready_for_delete it's removed from the map.
    682      tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
    683    } else {
    684      ++tmmbr_it;
    685    }
    686  }
    687  return update_bounding_set;
    688 }
    689 
    690 std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
    691  MutexLock lock(&rtcp_receiver_lock_);
    692  TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
    693  if (!tmmbr_info)
    694    return std::vector<rtcp::TmmbItem>();
    695 
    696  *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, local_media_ssrc());
    697  return tmmbr_info->tmmbn;
    698 }
    699 
    700 bool RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
    701                              PacketInformation* packet_information) {
    702  rtcp::Sdes sdes;
    703  if (!sdes.Parse(rtcp_block)) {
    704    return false;
    705  }
    706 
    707  for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
    708    if (cname_callback_)
    709      cname_callback_->OnCname(chunk.ssrc, chunk.cname);
    710  }
    711  packet_information->packet_type_flags |= kRtcpSdes;
    712 
    713  return true;
    714 }
    715 
    716 bool RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
    717                              PacketInformation* packet_information) {
    718  rtcp::Nack nack;
    719  if (!nack.Parse(rtcp_block)) {
    720    return false;
    721  }
    722 
    723  if (receiver_only_ || local_media_ssrc() != nack.media_ssrc())  // Not to us.
    724    return true;
    725 
    726  packet_information->nack_sequence_numbers.insert(
    727      packet_information->nack_sequence_numbers.end(),
    728      nack.packet_ids().begin(), nack.packet_ids().end());
    729  for (uint16_t packet_id : nack.packet_ids())
    730    nack_stats_.ReportRequest(packet_id);
    731 
    732  if (!nack.packet_ids().empty()) {
    733    packet_information->packet_type_flags |= kRtcpNack;
    734    ++packet_type_counter_.nack_packets;
    735    packet_type_counter_.nack_requests = nack_stats_.requests();
    736    packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
    737  }
    738 
    739  return true;
    740 }
    741 
    742 bool RTCPReceiver::HandleApp(const rtcp::CommonHeader& rtcp_block,
    743                             PacketInformation* packet_information) {
    744  rtcp::App app;
    745  if (!app.Parse(rtcp_block)) {
    746    return false;
    747  }
    748  if (app.name() == rtcp::RemoteEstimate::kName &&
    749      app.sub_type() == rtcp::RemoteEstimate::kSubType) {
    750    rtcp::RemoteEstimate estimate(std::move(app));
    751    if (estimate.ParseData()) {
    752      packet_information->network_state_estimate = estimate.estimate();
    753    }
    754    // RemoteEstimate is not a standard RTCP message. Failing to parse it
    755    // doesn't indicates RTCP packet is invalid. It may indicate sender happens
    756    // to use the same id for a different message. Thus don't return false.
    757  }
    758 
    759  return true;
    760 }
    761 
    762 bool RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
    763  rtcp::Bye bye;
    764  if (!bye.Parse(rtcp_block)) {
    765    return false;
    766  }
    767 
    768  if (rtcp_event_observer_) {
    769    rtcp_event_observer_->OnRtcpBye();
    770  }
    771 
    772  // Clear our lists.
    773  rtts_.erase(bye.sender_ssrc());
    774  EraseIf(received_report_blocks_, [&](const auto& elem) {
    775    return elem.second.sender_ssrc() == bye.sender_ssrc();
    776  });
    777 
    778  TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
    779  if (tmmbr_info)
    780    tmmbr_info->ready_for_delete = true;
    781 
    782  last_fir_.erase(bye.sender_ssrc());
    783  auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
    784  if (it != received_rrtrs_ssrc_it_.end()) {
    785    received_rrtrs_.erase(it->second);
    786    received_rrtrs_ssrc_it_.erase(it);
    787  }
    788  xr_rr_rtt_ = std::nullopt;
    789  return true;
    790 }
    791 
    792 bool RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
    793                            PacketInformation* packet_information,
    794                            bool& contains_dlrr,
    795                            uint32_t& ssrc) {
    796  rtcp::ExtendedReports xr;
    797  if (!xr.Parse(rtcp_block)) {
    798    return false;
    799  }
    800  ssrc = xr.sender_ssrc();
    801  contains_dlrr = !xr.dlrr().sub_blocks().empty();
    802 
    803  if (xr.rrtr())
    804    HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
    805 
    806  for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
    807    HandleXrDlrrReportBlock(xr.sender_ssrc(), time_info);
    808 
    809  if (xr.target_bitrate()) {
    810    HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
    811                          packet_information);
    812  }
    813  return true;
    814 }
    815 
    816 void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
    817                                                const rtcp::Rrtr& rrtr) {
    818  uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
    819  uint32_t local_receive_mid_ntp_time =
    820      CompactNtp(env_.clock().CurrentNtpTime());
    821 
    822  auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
    823  if (it != received_rrtrs_ssrc_it_.end()) {
    824    it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
    825    it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
    826  } else {
    827    if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
    828      received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
    829                                   local_receive_mid_ntp_time);
    830      received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
    831    } else {
    832      RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
    833                          << ", reached maximum number of stored RRTRs.";
    834    }
    835  }
    836 }
    837 
    838 void RTCPReceiver::HandleXrDlrrReportBlock(uint32_t sender_ssrc,
    839                                           const rtcp::ReceiveTimeInfo& rti) {
    840  if (!registered_ssrcs_.contains(rti.ssrc))  // Not to us.
    841    return;
    842 
    843  // Caller should explicitly enable rtt calculation using extended reports.
    844  if (!xr_rrtr_status_)
    845    return;
    846 
    847  // The send_time and delay_rr fields are in units of 1/2^16 sec.
    848  uint32_t send_time_ntp = rti.last_rr;
    849  // RFC3611, section 4.5, LRR field discription states:
    850  // If no such block has been received, the field is set to zero.
    851  if (send_time_ntp == 0) {
    852    auto rtt_stats = non_sender_rtts_.find(sender_ssrc);
    853    if (rtt_stats != non_sender_rtts_.end()) {
    854      rtt_stats->second.Invalidate();
    855    }
    856    return;
    857  }
    858 
    859  uint32_t delay_ntp = rti.delay_since_last_rr;
    860  uint32_t now_ntp = CompactNtp(env_.clock().CurrentNtpTime());
    861 
    862  uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
    863  TimeDelta rtt = CompactNtpRttToTimeDelta(rtt_ntp);
    864  xr_rr_rtt_ = rtt;
    865 
    866  non_sender_rtts_[sender_ssrc].Update(rtt);
    867 }
    868 
    869 void RTCPReceiver::HandleXrTargetBitrate(
    870    uint32_t ssrc,
    871    const rtcp::TargetBitrate& target_bitrate,
    872    PacketInformation* packet_information) {
    873  if (ssrc != remote_ssrc_) {
    874    return;  // Not for us.
    875  }
    876 
    877  VideoBitrateAllocation bitrate_allocation;
    878  for (const auto& item : target_bitrate.GetTargetBitrates()) {
    879    if (item.spatial_layer >= kMaxSpatialLayers ||
    880        item.temporal_layer >= kMaxTemporalStreams) {
    881      RTC_LOG(LS_WARNING)
    882          << "Invalid layer in XR target bitrate pack: spatial index "
    883          << item.spatial_layer << ", temporal index " << item.temporal_layer
    884          << ", dropping.";
    885    } else {
    886      bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
    887                                    item.target_bitrate_kbps * 1000);
    888    }
    889  }
    890  packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
    891 }
    892 
    893 bool RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
    894                             PacketInformation* packet_information) {
    895  rtcp::Pli pli;
    896  if (!pli.Parse(rtcp_block)) {
    897    return false;
    898  }
    899 
    900  if (local_media_ssrc() == pli.media_ssrc()) {
    901    ++packet_type_counter_.pli_packets;
    902    // Received a signal that we need to send a new key frame.
    903    packet_information->packet_type_flags |= kRtcpPli;
    904  }
    905  return true;
    906 }
    907 
    908 bool RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
    909                               PacketInformation* packet_information) {
    910  rtcp::Tmmbr tmmbr;
    911  if (!tmmbr.Parse(rtcp_block)) {
    912    return false;
    913  }
    914 
    915  uint32_t sender_ssrc = tmmbr.sender_ssrc();
    916  if (tmmbr.media_ssrc()) {
    917    // media_ssrc() SHOULD be 0 if same as SenderSSRC.
    918    // In relay mode this is a valid number.
    919    sender_ssrc = tmmbr.media_ssrc();
    920  }
    921 
    922  for (const rtcp::TmmbItem& request : tmmbr.requests()) {
    923    if (local_media_ssrc() != request.ssrc() || request.bitrate_bps() == 0)
    924      continue;
    925 
    926    TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
    927    auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
    928    entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
    929                                       request.packet_overhead());
    930    // FindOrCreateTmmbrInfo always sets `last_time_received` to
    931    // `clock_->CurrentTime()`.
    932    entry->last_updated = tmmbr_info->last_time_received;
    933 
    934    packet_information->packet_type_flags |= kRtcpTmmbr;
    935    break;
    936  }
    937  return true;
    938 }
    939 
    940 bool RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
    941                               PacketInformation* packet_information) {
    942  rtcp::Tmmbn tmmbn;
    943  if (!tmmbn.Parse(rtcp_block)) {
    944    return false;
    945  }
    946 
    947  TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
    948 
    949  packet_information->packet_type_flags |= kRtcpTmmbn;
    950 
    951  tmmbr_info->tmmbn = tmmbn.items();
    952  return true;
    953 }
    954 
    955 bool RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
    956                               PacketInformation* packet_information) {
    957  rtcp::RapidResyncRequest sr_req;
    958  if (!sr_req.Parse(rtcp_block)) {
    959    return false;
    960  }
    961 
    962  packet_information->packet_type_flags |= kRtcpSrReq;
    963  return true;
    964 }
    965 
    966 void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
    967                                 PacketInformation* packet_information) {
    968  {
    969    rtcp::Remb remb;
    970    if (remb.Parse(rtcp_block)) {
    971      packet_information->packet_type_flags |= kRtcpRemb;
    972      packet_information->receiver_estimated_max_bitrate_bps =
    973          remb.bitrate_bps();
    974      return;
    975    }
    976  }
    977 
    978  {
    979    auto loss_notification = std::make_unique<rtcp::LossNotification>();
    980    if (loss_notification->Parse(rtcp_block)) {
    981      packet_information->packet_type_flags |= kRtcpLossNotification;
    982      packet_information->loss_notification = std::move(loss_notification);
    983      return;
    984    }
    985  }
    986 
    987  RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
    988  ++num_skipped_packets_;
    989  // Application layer feedback message doesn't have a standard format.
    990  // Failing to parse one of known messages doesn't indicate an invalid RTCP.
    991 }
    992 
    993 bool RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
    994                             PacketInformation* packet_information) {
    995  rtcp::Fir fir;
    996  if (!fir.Parse(rtcp_block)) {
    997    return false;
    998  }
    999 
   1000  if (fir.requests().empty())
   1001    return true;
   1002 
   1003  const Timestamp now = env_.clock().CurrentTime();
   1004  for (const rtcp::Fir::Request& fir_request : fir.requests()) {
   1005    // Is it our sender that is requested to generate a new keyframe.
   1006    if (local_media_ssrc() != fir_request.ssrc)
   1007      continue;
   1008 
   1009    ++packet_type_counter_.fir_packets;
   1010 
   1011    auto [it, inserted] =
   1012        last_fir_.try_emplace(fir.sender_ssrc(), now, fir_request.seq_nr);
   1013    if (!inserted) {  // There was already an entry.
   1014      LastFirStatus* last_fir = &it->second;
   1015 
   1016      // Check if we have reported this FIRSequenceNumber before.
   1017      if (fir_request.seq_nr == last_fir->sequence_number)
   1018        continue;
   1019 
   1020      // Sanity: don't go crazy with the callbacks.
   1021      if (now - last_fir->request < kRtcpMinFrameLength)
   1022        continue;
   1023 
   1024      last_fir->request = now;
   1025      last_fir->sequence_number = fir_request.seq_nr;
   1026    }
   1027    // Received signal that we need to send a new key frame.
   1028    packet_information->packet_type_flags |= kRtcpFir;
   1029  }
   1030  return true;
   1031 }
   1032 
   1033 void RTCPReceiver::HandleTransportFeedback(
   1034    const CommonHeader& rtcp_block,
   1035    PacketInformation* packet_information) {
   1036  std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
   1037      new rtcp::TransportFeedback());
   1038  if (!transport_feedback->Parse(rtcp_block)) {
   1039    ++num_skipped_packets_;
   1040    // Application layer feedback message doesn't have a standard format.
   1041    // Failing to parse it as transport feedback messages doesn't indicate an
   1042    // invalid RTCP.
   1043    return;
   1044  }
   1045  uint32_t media_source_ssrc = transport_feedback->media_ssrc();
   1046  if (media_source_ssrc == local_media_ssrc() ||
   1047      registered_ssrcs_.contains(media_source_ssrc)) {
   1048    packet_information->packet_type_flags |= kRtcpTransportFeedback;
   1049    packet_information->transport_feedback = std::move(transport_feedback);
   1050  }
   1051 }
   1052 
   1053 bool RTCPReceiver::HandleCongestionControlFeedback(
   1054    const CommonHeader& rtcp_block,
   1055    PacketInformation* packet_information) {
   1056  rtcp::CongestionControlFeedback feedback;
   1057  if (!feedback.Parse(rtcp_block) || feedback.packets().empty()) {
   1058    return false;
   1059  }
   1060  uint32_t first_media_source_ssrc = feedback.packets()[0].ssrc;
   1061  if (first_media_source_ssrc == local_media_ssrc() ||
   1062      registered_ssrcs_.contains(first_media_source_ssrc)) {
   1063    packet_information->congestion_control_feedback.emplace(
   1064        std::move(feedback));
   1065  }
   1066  return true;
   1067 }
   1068 
   1069 void RTCPReceiver::NotifyTmmbrUpdated() {
   1070  // Find bounding set.
   1071  std::vector<rtcp::TmmbItem> bounding =
   1072      TMMBRHelp::FindBoundingSet(TmmbrReceived());
   1073 
   1074  if (!bounding.empty() && network_link_rtcp_observer_) {
   1075    // We have a new bandwidth estimate on this channel.
   1076    uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
   1077    if (bitrate_bps < std::numeric_limits<int64_t>::max()) {
   1078      network_link_rtcp_observer_->OnReceiverEstimatedMaxBitrate(
   1079          env_.clock().CurrentTime(), DataRate::BitsPerSec(bitrate_bps));
   1080    }
   1081  }
   1082 
   1083  // Send tmmbn to inform remote clients about the new bandwidth.
   1084  rtp_rtcp_->SetTmmbn(std::move(bounding));
   1085 }
   1086 
   1087 // Holding no Critical section.
   1088 void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
   1089    const PacketInformation& packet_information) {
   1090  // Process TMMBR and REMB first to avoid multiple callbacks
   1091  // to OnNetworkChanged.
   1092  if (packet_information.packet_type_flags & kRtcpTmmbr) {
   1093    // Might trigger a OnReceivedBandwidthEstimateUpdate.
   1094    NotifyTmmbrUpdated();
   1095  }
   1096 
   1097  if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
   1098    rtp_rtcp_->OnRequestSendReport();
   1099  }
   1100  if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
   1101    if (!packet_information.nack_sequence_numbers.empty()) {
   1102      RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
   1103                          << packet_information.nack_sequence_numbers.size();
   1104      rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
   1105    }
   1106  }
   1107 
   1108  // We need feedback that we have received a report block(s) so that we
   1109  // can generate a new packet in a conference relay scenario, one received
   1110  // report can generate several RTCP packets, based on number relayed/mixed
   1111  // a send report block should go out to all receivers.
   1112  if (rtcp_intra_frame_observer_) {
   1113    RTC_DCHECK(!receiver_only_);
   1114    if ((packet_information.packet_type_flags & kRtcpPli) ||
   1115        (packet_information.packet_type_flags & kRtcpFir)) {
   1116      if (packet_information.packet_type_flags & kRtcpPli) {
   1117        RTC_LOG(LS_VERBOSE)
   1118            << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
   1119      } else {
   1120        RTC_LOG(LS_VERBOSE)
   1121            << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
   1122      }
   1123      rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(
   1124          local_media_ssrc());
   1125    }
   1126  }
   1127  if (rtcp_loss_notification_observer_ &&
   1128      (packet_information.packet_type_flags & kRtcpLossNotification)) {
   1129    rtcp::LossNotification* loss_notification =
   1130        packet_information.loss_notification.get();
   1131    RTC_DCHECK(loss_notification);
   1132    if (loss_notification->media_ssrc() == local_media_ssrc()) {
   1133      rtcp_loss_notification_observer_->OnReceivedLossNotification(
   1134          loss_notification->media_ssrc(), loss_notification->last_decoded(),
   1135          loss_notification->last_received(),
   1136          loss_notification->decodability_flag());
   1137    }
   1138  }
   1139  // Network state estimate should be applied before other feedback since it may
   1140  // affect how other feedback is handled.
   1141  if (network_state_estimate_observer_ &&
   1142      packet_information.network_state_estimate) {
   1143    network_state_estimate_observer_->OnRemoteNetworkEstimate(
   1144        *packet_information.network_state_estimate);
   1145  }
   1146 
   1147  if (network_link_rtcp_observer_) {
   1148    Timestamp now = env_.clock().CurrentTime();
   1149    if (packet_information.packet_type_flags & kRtcpRemb) {
   1150      network_link_rtcp_observer_->OnReceiverEstimatedMaxBitrate(
   1151          now, DataRate::BitsPerSec(
   1152                   packet_information.receiver_estimated_max_bitrate_bps));
   1153    }
   1154    if (!packet_information.report_block_datas.empty()) {
   1155      network_link_rtcp_observer_->OnReport(
   1156          now, packet_information.report_block_datas);
   1157    }
   1158    if (packet_information.rtt.has_value()) {
   1159      network_link_rtcp_observer_->OnRttUpdate(now, *packet_information.rtt);
   1160    }
   1161    if (packet_information.transport_feedback != nullptr) {
   1162      network_link_rtcp_observer_->OnTransportFeedback(
   1163          now, *packet_information.transport_feedback);
   1164    }
   1165    if (packet_information.congestion_control_feedback) {
   1166      network_link_rtcp_observer_->OnCongestionControlFeedback(
   1167          now, *packet_information.congestion_control_feedback);
   1168    }
   1169  }
   1170 
   1171  if ((packet_information.packet_type_flags & kRtcpSr) ||
   1172      (packet_information.packet_type_flags & kRtcpRr)) {
   1173    rtp_rtcp_->OnReceivedRtcpReportBlocks(
   1174        packet_information.report_block_datas);
   1175  }
   1176 
   1177  if (bitrate_allocation_observer_ &&
   1178      packet_information.target_bitrate_allocation) {
   1179    bitrate_allocation_observer_->OnBitrateAllocationUpdated(
   1180        *packet_information.target_bitrate_allocation);
   1181  }
   1182 
   1183  if (!receiver_only_) {
   1184    if (report_block_data_observer_) {
   1185      for (const auto& report_block_data :
   1186           packet_information.report_block_datas) {
   1187        report_block_data_observer_->OnReportBlockDataUpdated(
   1188            report_block_data);
   1189      }
   1190    }
   1191  }
   1192 }
   1193 
   1194 std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
   1195  MutexLock lock(&rtcp_receiver_lock_);
   1196  std::vector<rtcp::TmmbItem> candidates;
   1197 
   1198  Timestamp now = env_.clock().CurrentTime();
   1199 
   1200  for (auto& kv : tmmbr_infos_) {
   1201    for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
   1202      if (now - it->second.last_updated > kTmmbrTimeoutInterval) {
   1203        // Erase timeout entries.
   1204        it = kv.second.tmmbr.erase(it);
   1205      } else {
   1206        candidates.push_back(it->second.tmmbr_item);
   1207        ++it;
   1208      }
   1209    }
   1210  }
   1211  return candidates;
   1212 }
   1213 
   1214 bool RTCPReceiver::RtcpRrTimeoutLocked(Timestamp now) {
   1215  bool result =
   1216      ResetTimestampIfExpired(now, last_received_rb_, report_interval_);
   1217  if (result && rtcp_event_observer_) {
   1218    rtcp_event_observer_->OnRtcpTimeout();
   1219  }
   1220  return result;
   1221 }
   1222 
   1223 bool RTCPReceiver::RtcpRrSequenceNumberTimeoutLocked(Timestamp now) {
   1224  bool result = ResetTimestampIfExpired(now, last_increased_sequence_number_,
   1225                                        report_interval_);
   1226  if (result && rtcp_event_observer_) {
   1227    rtcp_event_observer_->OnRtcpTimeout();
   1228  }
   1229  return result;
   1230 }
   1231 
   1232 }  // namespace webrtc