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 <string.h>
14 
15 #include <limits>
16 #include <map>
17 #include <memory>
18 #include <utility>
19 #include <vector>
20 
21 #include "common_types.h"  // NOLINT(build/include)
22 #include "common_video/include/video_bitrate_allocator.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
29 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
30 #include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
31 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
32 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
34 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
35 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
36 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
37 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
38 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
39 #include "modules/rtp_rtcp/source/time_util.h"
40 #include "modules/rtp_rtcp/source/tmmbr_help.h"
41 #include "rtc_base/checks.h"
42 #include "rtc_base/logging.h"
43 #include "rtc_base/trace_event.h"
44 #include "system_wrappers/include/ntp_time.h"
45 
46 namespace webrtc {
47 namespace {
48 
49 using rtcp::CommonHeader;
50 using rtcp::ReportBlock;
51 
52 // The number of RTCP time intervals needed to trigger a timeout.
53 const int kRrTimeoutIntervals = 3;
54 
55 const int64_t kMaxWarningLogIntervalMs = 10000;
56 const int64_t kRtcpMinFrameLengthMs = 17;
57 
58 }  // namespace
59 
60 struct RTCPReceiver::PacketInformation {
61   uint32_t packet_type_flags = 0;  // RTCPPacketTypeFlags bit field.
62 
63   uint32_t remote_ssrc = 0;
64   std::vector<uint16_t> nack_sequence_numbers;
65   ReportBlockList report_blocks;
66   int64_t rtt_ms = 0;
67   uint32_t receiver_estimated_max_bitrate_bps = 0;
68   std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
69   rtc::Optional<BitrateAllocation> target_bitrate_allocation;
70 };
71 
72 // Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
73 struct RTCPReceiver::TmmbrInformation {
74   struct TimedTmmbrItem {
75     rtcp::TmmbItem tmmbr_item;
76     int64_t last_updated_ms;
77   };
78 
79   int64_t last_time_received_ms = 0;
80 
81   bool ready_for_delete = false;
82 
83   std::vector<rtcp::TmmbItem> tmmbn;
84   std::map<uint32_t, TimedTmmbrItem> tmmbr;
85 };
86 
87 struct RTCPReceiver::ReportBlockWithRtt {
88   RTCPReportBlock report_block;
89 
90   uint32_t lastReceivedRRNTPsecs = 0;
91   uint32_t lastReceivedRRNTPfrac = 0;
92 
93   int64_t last_rtt_ms = 0;
94   int64_t min_rtt_ms = 0;
95   int64_t max_rtt_ms = 0;
96   int64_t sum_rtt_ms = 0;
97   size_t num_rtts = 0;
98 };
99 
100 struct RTCPReceiver::LastFirStatus {
LastFirStatuswebrtc::RTCPReceiver::LastFirStatus101   LastFirStatus(int64_t now_ms, uint8_t sequence_number)
102       : request_ms(now_ms), sequence_number(sequence_number) {}
103   int64_t request_ms;
104   uint8_t sequence_number;
105 };
106 
RTCPReceiver(Clock * clock,bool receiver_only,RtcpPacketTypeCounterObserver * packet_type_counter_observer,RtcpBandwidthObserver * rtcp_bandwidth_observer,RtcpEventObserver * rtcp_event_observer,RtcpIntraFrameObserver * rtcp_intra_frame_observer,TransportFeedbackObserver * transport_feedback_observer,VideoBitrateAllocationObserver * bitrate_allocation_observer,ModuleRtpRtcp * owner)107 RTCPReceiver::RTCPReceiver(
108     Clock* clock,
109     bool receiver_only,
110     RtcpPacketTypeCounterObserver* packet_type_counter_observer,
111     RtcpBandwidthObserver* rtcp_bandwidth_observer,
112     RtcpEventObserver* rtcp_event_observer,
113     RtcpIntraFrameObserver* rtcp_intra_frame_observer,
114     TransportFeedbackObserver* transport_feedback_observer,
115     VideoBitrateAllocationObserver* bitrate_allocation_observer,
116     ModuleRtpRtcp* owner)
117     : clock_(clock),
118       receiver_only_(receiver_only),
119       rtp_rtcp_(owner),
120       rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
121       rtcp_event_observer_(rtcp_event_observer),
122       rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
123       transport_feedback_observer_(transport_feedback_observer),
124       bitrate_allocation_observer_(bitrate_allocation_observer),
125       main_ssrc_(0),
126       remote_ssrc_(0),
127       remote_sender_rtp_time_(0),
128       remote_sender_packet_count_(0),
129       remote_sender_octet_count_(0),
130       xr_rrtr_status_(false),
131       xr_rr_rtt_ms_(0),
132       oldest_tmmbr_info_ms_(0),
133       last_received_rb_ms_(0),
134       last_increased_sequence_number_ms_(0),
135       stats_callback_(nullptr),
136       packet_type_counter_observer_(packet_type_counter_observer),
137       num_skipped_packets_(0),
138       last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
139   RTC_DCHECK(owner);
140 }
141 
~RTCPReceiver()142 RTCPReceiver::~RTCPReceiver() {}
143 
IncomingPacket(const uint8_t * packet,size_t packet_size)144 void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
145   if (packet_size == 0) {
146     RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
147     return;
148   }
149 
150   PacketInformation packet_information;
151   if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
152     return;
153   TriggerCallbacksFromRtcpPacket(packet_information);
154 }
155 
LastReceivedReportBlockMs() const156 int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
157   rtc::CritScope lock(&rtcp_receiver_lock_);
158   return last_received_rb_ms_;
159 }
160 
SetRemoteSSRC(uint32_t ssrc)161 void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
162   rtc::CritScope lock(&rtcp_receiver_lock_);
163   // New SSRC reset old reports.
164   last_received_sr_ntp_.Reset();
165   remote_ssrc_ = ssrc;
166 }
167 
RemoteSSRC() const168 uint32_t RTCPReceiver::RemoteSSRC() const {
169   rtc::CritScope lock(&rtcp_receiver_lock_);
170   return remote_ssrc_;
171 }
172 
SetSsrcs(uint32_t main_ssrc,const std::set<uint32_t> & registered_ssrcs)173 void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
174                             const std::set<uint32_t>& registered_ssrcs) {
175   rtc::CritScope lock(&rtcp_receiver_lock_);
176   main_ssrc_ = main_ssrc;
177   registered_ssrcs_ = registered_ssrcs;
178 }
179 
RTT(uint32_t remote_ssrc,int64_t * last_rtt_ms,int64_t * avg_rtt_ms,int64_t * min_rtt_ms,int64_t * max_rtt_ms) const180 int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
181                           int64_t* last_rtt_ms,
182                           int64_t* avg_rtt_ms,
183                           int64_t* min_rtt_ms,
184                           int64_t* max_rtt_ms) const {
185   rtc::CritScope lock(&rtcp_receiver_lock_);
186 
187   auto it = received_report_blocks_.find(main_ssrc_);
188   if (it == received_report_blocks_.end())
189     return -1;
190 
191   auto it_info = it->second.find(remote_ssrc);
192   if (it_info == it->second.end())
193     return -1;
194 
195   const ReportBlockWithRtt* report_block = &it_info->second;
196 
197   if (report_block->num_rtts == 0)
198     return -1;
199 
200   if (last_rtt_ms)
201     *last_rtt_ms = report_block->last_rtt_ms;
202 
203   if (avg_rtt_ms)
204     *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
205 
206   if (min_rtt_ms)
207     *min_rtt_ms = report_block->min_rtt_ms;
208 
209   if (max_rtt_ms)
210     *max_rtt_ms = report_block->max_rtt_ms;
211 
212   return 0;
213 }
214 
SetRtcpXrRrtrStatus(bool enable)215 void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
216   rtc::CritScope lock(&rtcp_receiver_lock_);
217   xr_rrtr_status_ = enable;
218 }
219 
GetAndResetXrRrRtt(int64_t * rtt_ms)220 bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
221   RTC_DCHECK(rtt_ms);
222   rtc::CritScope lock(&rtcp_receiver_lock_);
223   if (xr_rr_rtt_ms_ == 0) {
224     return false;
225   }
226   *rtt_ms = xr_rr_rtt_ms_;
227   xr_rr_rtt_ms_ = 0;
228   return true;
229 }
230 
NTP(uint32_t * received_ntp_secs,uint32_t * received_ntp_frac,uint32_t * rtcp_arrival_time_secs,uint32_t * rtcp_arrival_time_frac,uint32_t * rtcp_timestamp) const231 bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
232                        uint32_t* received_ntp_frac,
233                        uint32_t* rtcp_arrival_time_secs,
234                        uint32_t* rtcp_arrival_time_frac,
235                        uint32_t* rtcp_timestamp) const {
236   rtc::CritScope lock(&rtcp_receiver_lock_);
237   if (!last_received_sr_ntp_.Valid())
238     return false;
239 
240   // NTP from incoming SenderReport.
241   if (received_ntp_secs)
242     *received_ntp_secs = remote_sender_ntp_time_.seconds();
243   if (received_ntp_frac)
244     *received_ntp_frac = remote_sender_ntp_time_.fractions();
245 
246   // Rtp time from incoming SenderReport.
247   if (rtcp_timestamp)
248     *rtcp_timestamp = remote_sender_rtp_time_;
249 
250   // Local NTP time when we received a RTCP packet with a send block.
251   if (rtcp_arrival_time_secs)
252     *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
253   if (rtcp_arrival_time_frac)
254     *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
255 
256   return true;
257 }
258 
RemoteRTCPSenderInfo(uint32_t * packet_count,uint32_t * octet_count,NtpTime * ntp_timestamp) const259 void RTCPReceiver::RemoteRTCPSenderInfo(uint32_t* packet_count,
260                                         uint32_t* octet_count,
261                                         NtpTime* ntp_timestamp) const {
262   rtc::CritScope lock(&rtcp_receiver_lock_);
263   *packet_count = remote_sender_packet_count_;
264   *octet_count = remote_sender_octet_count_;
265   *ntp_timestamp = remote_sender_ntp_time_;
266 }
267 
LastReceivedXrReferenceTimeInfo(rtcp::ReceiveTimeInfo * info) const268 bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
269     rtcp::ReceiveTimeInfo* info) const {
270   RTC_DCHECK(info);
271   rtc::CritScope lock(&rtcp_receiver_lock_);
272   if (!last_received_xr_ntp_.Valid())
273     return false;
274 
275   info->ssrc = remote_time_info_.ssrc;
276   info->last_rr = remote_time_info_.last_rr;
277 
278   // Get the delay since last received report (RFC 3611).
279   uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
280   uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
281 
282   info->delay_since_last_rr = now_ntp - receive_time_ntp;
283   return true;
284 }
285 
286 // We can get multiple receive reports when we receive the report from a CE.
StatisticsReceived(std::vector<RTCPReportBlock> * receive_blocks) const287 int32_t RTCPReceiver::StatisticsReceived(
288     std::vector<RTCPReportBlock>* receive_blocks) const {
289   RTC_DCHECK(receive_blocks);
290   rtc::CritScope lock(&rtcp_receiver_lock_);
291   for (const auto& reports_per_receiver : received_report_blocks_)
292     for (const auto& report : reports_per_receiver.second)
293       receive_blocks->push_back(report.second.report_block);
294   return 0;
295 }
296 
ParseCompoundPacket(const uint8_t * packet_begin,const uint8_t * packet_end,PacketInformation * packet_information)297 bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
298                                        const uint8_t* packet_end,
299                                        PacketInformation* packet_information) {
300   rtc::CritScope lock(&rtcp_receiver_lock_);
301 
302   CommonHeader rtcp_block;
303   for (const uint8_t* next_block = packet_begin; next_block != packet_end;
304        next_block = rtcp_block.NextPacket()) {
305     ptrdiff_t remaining_blocks_size = packet_end - next_block;
306     RTC_DCHECK_GT(remaining_blocks_size, 0);
307     if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
308       if (next_block == packet_begin) {
309         // Failed to parse 1st header, nothing was extracted from this packet.
310         RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
311         return false;
312       }
313       ++num_skipped_packets_;
314       break;
315     }
316 
317     if (packet_type_counter_.first_packet_time_ms == -1)
318       packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
319 
320     switch (rtcp_block.type()) {
321       case rtcp::SenderReport::kPacketType:
322         HandleSenderReport(rtcp_block, packet_information);
323         break;
324       case rtcp::ReceiverReport::kPacketType:
325         HandleReceiverReport(rtcp_block, packet_information);
326         break;
327       case rtcp::Sdes::kPacketType:
328         HandleSdes(rtcp_block, packet_information);
329         break;
330       case rtcp::ExtendedReports::kPacketType:
331         HandleXr(rtcp_block, packet_information);
332         break;
333       case rtcp::Bye::kPacketType:
334         HandleBye(rtcp_block);
335         break;
336       case rtcp::Rtpfb::kPacketType:
337         switch (rtcp_block.fmt()) {
338           case rtcp::Nack::kFeedbackMessageType:
339             HandleNack(rtcp_block, packet_information);
340             break;
341           case rtcp::Tmmbr::kFeedbackMessageType:
342             HandleTmmbr(rtcp_block, packet_information);
343             break;
344           case rtcp::Tmmbn::kFeedbackMessageType:
345             HandleTmmbn(rtcp_block, packet_information);
346             break;
347           case rtcp::RapidResyncRequest::kFeedbackMessageType:
348             HandleSrReq(rtcp_block, packet_information);
349             break;
350           case rtcp::TransportFeedback::kFeedbackMessageType:
351             HandleTransportFeedback(rtcp_block, packet_information);
352             break;
353           default:
354             ++num_skipped_packets_;
355             break;
356         }
357         break;
358       case rtcp::Psfb::kPacketType:
359         switch (rtcp_block.fmt()) {
360           case rtcp::Pli::kFeedbackMessageType:
361             HandlePli(rtcp_block, packet_information);
362             break;
363           case rtcp::Fir::kFeedbackMessageType:
364             HandleFir(rtcp_block, packet_information);
365             break;
366           case rtcp::Remb::kFeedbackMessageType:
367             HandlePsfbApp(rtcp_block, packet_information);
368             break;
369           default:
370             ++num_skipped_packets_;
371             break;
372         }
373         break;
374       default:
375         ++num_skipped_packets_;
376         break;
377     }
378   }
379 
380   if (packet_type_counter_observer_) {
381     packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
382         main_ssrc_, packet_type_counter_);
383   }
384 
385   int64_t now_ms = clock_->TimeInMilliseconds();
386   if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
387       num_skipped_packets_ > 0) {
388     last_skipped_packets_warning_ms_ = now_ms;
389     RTC_LOG(LS_WARNING)
390         << num_skipped_packets_
391         << " RTCP blocks were skipped due to being malformed or of "
392            "unrecognized/unsupported type, during the past "
393         << (kMaxWarningLogIntervalMs / 1000) << " second period.";
394   }
395 
396   return true;
397 }
398 
HandleSenderReport(const CommonHeader & rtcp_block,PacketInformation * packet_information)399 void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
400                                       PacketInformation* packet_information) {
401   rtcp::SenderReport sender_report;
402   if (!sender_report.Parse(rtcp_block)) {
403     ++num_skipped_packets_;
404     return;
405   }
406 
407   const uint32_t remote_ssrc = sender_report.sender_ssrc();
408 
409   packet_information->remote_ssrc = remote_ssrc;
410 
411   UpdateTmmbrRemoteIsAlive(remote_ssrc);
412 
413   TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
414                        "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
415 
416   // Have I received RTP packets from this party?
417   if (remote_ssrc_ == remote_ssrc) {
418     // Only signal that we have received a SR when we accept one.
419     packet_information->packet_type_flags |= kRtcpSr;
420 
421     remote_sender_ntp_time_ = sender_report.ntp();
422     remote_sender_rtp_time_ = sender_report.rtp_timestamp();
423     last_received_sr_ntp_ = clock_->CurrentNtpTime();
424     remote_sender_packet_count_ = sender_report.sender_packet_count();
425     remote_sender_octet_count_ = sender_report.sender_octet_count();
426   } else {
427     // We will only store the send report from one source, but
428     // we will store all the receive blocks.
429     packet_information->packet_type_flags |= kRtcpRr;
430   }
431 
432   for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
433     HandleReportBlock(report_block, packet_information, remote_ssrc);
434 }
435 
HandleReceiverReport(const CommonHeader & rtcp_block,PacketInformation * packet_information)436 void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
437                                         PacketInformation* packet_information) {
438   rtcp::ReceiverReport receiver_report;
439   if (!receiver_report.Parse(rtcp_block)) {
440     ++num_skipped_packets_;
441     return;
442   }
443 
444   const uint32_t remote_ssrc = receiver_report.sender_ssrc();
445 
446   packet_information->remote_ssrc = remote_ssrc;
447 
448   UpdateTmmbrRemoteIsAlive(remote_ssrc);
449 
450   TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
451                        "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
452 
453   packet_information->packet_type_flags |= kRtcpRr;
454 
455   for (const ReportBlock& report_block : receiver_report.report_blocks())
456     HandleReportBlock(report_block, packet_information, remote_ssrc);
457 }
458 
HandleReportBlock(const ReportBlock & report_block,PacketInformation * packet_information,uint32_t remote_ssrc)459 void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
460                                      PacketInformation* packet_information,
461                                      uint32_t remote_ssrc) {
462   // This will be called once per report block in the RTCP packet.
463   // We filter out all report blocks that are not for us.
464   // Each packet has max 31 RR blocks.
465   //
466   // We can calc RTT if we send a send report and get a report block back.
467 
468   // |report_block.source_ssrc()| is the SSRC identifier of the source to
469   // which the information in this reception report block pertains.
470 
471   // Filter out all report blocks that are not for us.
472   if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
473     return;
474 
475   last_received_rb_ms_ = clock_->TimeInMilliseconds();
476 
477   ReportBlockWithRtt* report_block_info =
478       &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
479   report_block_info->report_block.sender_ssrc = remote_ssrc;
480   report_block_info->report_block.source_ssrc = report_block.source_ssrc();
481   report_block_info->report_block.fraction_lost = report_block.fraction_lost();
482   report_block_info->report_block.packets_lost = report_block.cumulative_lost();
483   if (report_block.extended_high_seq_num() >
484       report_block_info->report_block.extended_highest_sequence_number) {
485     // We have successfully delivered new RTP packets to the remote side after
486     // the last RR was sent from the remote side.
487     last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
488   }
489   report_block_info->report_block.extended_highest_sequence_number =
490       report_block.extended_high_seq_num();
491   report_block_info->report_block.jitter = report_block.jitter();
492   report_block_info->report_block.delay_since_last_sender_report =
493       report_block.delay_since_last_sr();
494   report_block_info->report_block.last_sender_report_timestamp =
495       report_block.last_sr();
496 
497   int64_t rtt_ms = 0;
498   uint32_t send_time_ntp = report_block.last_sr();
499   // RFC3550, section 6.4.1, LSR field discription states:
500   // If no SR has been received yet, the field is set to zero.
501   // Receiver rtp_rtcp module is not expected to calculate rtt using
502   // Sender Reports even if it accidentally can.
503   if (!receiver_only_ && send_time_ntp != 0) {
504     uint32_t delay_ntp = report_block.delay_since_last_sr();
505     // Local NTP time.
506     uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
507 
508     // RTT in 1/(2^16) seconds.
509     uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
510     // Convert to 1/1000 seconds (milliseconds).
511     rtt_ms = CompactNtpRttToMs(rtt_ntp);
512     if (rtt_ms > report_block_info->max_rtt_ms)
513       report_block_info->max_rtt_ms = rtt_ms;
514 
515     if (report_block_info->num_rtts == 0 ||
516         rtt_ms < report_block_info->min_rtt_ms)
517       report_block_info->min_rtt_ms = rtt_ms;
518 
519     report_block_info->last_rtt_ms = rtt_ms;
520     report_block_info->sum_rtt_ms += rtt_ms;
521     ++report_block_info->num_rtts;
522   }
523 
524   TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
525                     report_block.source_ssrc(), rtt_ms);
526 
527   packet_information->rtt_ms = rtt_ms;
528   packet_information->report_blocks.push_back(report_block_info->report_block);
529 }
530 
FindOrCreateTmmbrInfo(uint32_t remote_ssrc)531 RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
532     uint32_t remote_ssrc) {
533   // Create or find receive information.
534   TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
535   // Update that this remote is alive.
536   tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
537   return tmmbr_info;
538 }
539 
UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc)540 void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
541   auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
542   if (tmmbr_it != tmmbr_infos_.end())
543     tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
544 }
545 
GetTmmbrInformation(uint32_t remote_ssrc)546 RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
547     uint32_t remote_ssrc) {
548   auto it = tmmbr_infos_.find(remote_ssrc);
549   if (it == tmmbr_infos_.end())
550     return nullptr;
551   return &it->second;
552 }
553 
RtcpRrTimeout(int64_t rtcp_interval_ms)554 bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
555   rtc::CritScope lock(&rtcp_receiver_lock_);
556   if (last_received_rb_ms_ == 0)
557     return false;
558 
559   int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
560   if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
561     // Reset the timer to only trigger one log.
562     last_received_rb_ms_ = 0;
563     if (rtcp_event_observer_) {
564       rtcp_event_observer_->OnRtcpTimeout();
565     }
566     return true;
567   }
568   return false;
569 }
570 
RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms)571 bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
572   rtc::CritScope lock(&rtcp_receiver_lock_);
573   if (last_increased_sequence_number_ms_ == 0)
574     return false;
575 
576   int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
577   if (clock_->TimeInMilliseconds() >
578       last_increased_sequence_number_ms_ + time_out_ms) {
579     // Reset the timer to only trigger one log.
580     last_increased_sequence_number_ms_ = 0;
581     if (rtcp_event_observer_) {
582       rtcp_event_observer_->OnRtcpTimeout();
583     }
584     return true;
585   }
586   return false;
587 }
588 
UpdateTmmbrTimers()589 bool RTCPReceiver::UpdateTmmbrTimers() {
590   rtc::CritScope lock(&rtcp_receiver_lock_);
591 
592   int64_t now_ms = clock_->TimeInMilliseconds();
593   // Use audio define since we don't know what interval the remote peer use.
594   int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
595 
596   if (oldest_tmmbr_info_ms_ >= timeout_ms)
597     return false;
598 
599   bool update_bounding_set = false;
600   oldest_tmmbr_info_ms_ = -1;
601   for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
602     TmmbrInformation* tmmbr_info = &tmmbr_it->second;
603     if (tmmbr_info->last_time_received_ms > 0) {
604       if (tmmbr_info->last_time_received_ms < timeout_ms) {
605         // No rtcp packet for the last 5 regular intervals, reset limitations.
606         tmmbr_info->tmmbr.clear();
607         // Prevent that we call this over and over again.
608         tmmbr_info->last_time_received_ms = 0;
609         // Send new TMMBN to all channels using the default codec.
610         update_bounding_set = true;
611       } else if (oldest_tmmbr_info_ms_ == -1 ||
612                  tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
613         oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
614       }
615       ++tmmbr_it;
616     } else if (tmmbr_info->ready_for_delete) {
617       // When we dont have a last_time_received_ms and the object is marked
618       // ready_for_delete it's removed from the map.
619       tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
620     } else {
621       ++tmmbr_it;
622     }
623   }
624   return update_bounding_set;
625 }
626 
BoundingSet(bool * tmmbr_owner)627 std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
628   rtc::CritScope lock(&rtcp_receiver_lock_);
629   TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
630   if (!tmmbr_info)
631     return std::vector<rtcp::TmmbItem>();
632 
633   *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
634   return tmmbr_info->tmmbn;
635 }
636 
HandleSdes(const CommonHeader & rtcp_block,PacketInformation * packet_information)637 void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
638                               PacketInformation* packet_information) {
639   rtcp::Sdes sdes;
640   if (!sdes.Parse(rtcp_block)) {
641     ++num_skipped_packets_;
642     return;
643   }
644 
645   for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
646     received_cnames_[chunk.ssrc] = chunk.cname;
647     {
648       rtc::CritScope lock(&feedbacks_lock_);
649       if (stats_callback_)
650         stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
651     }
652   }
653   packet_information->packet_type_flags |= kRtcpSdes;
654 }
655 
HandleNack(const CommonHeader & rtcp_block,PacketInformation * packet_information)656 void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
657                               PacketInformation* packet_information) {
658   rtcp::Nack nack;
659   if (!nack.Parse(rtcp_block)) {
660     ++num_skipped_packets_;
661     return;
662   }
663 
664   if (receiver_only_ || main_ssrc_ != nack.media_ssrc())  // Not to us.
665     return;
666 
667   packet_information->nack_sequence_numbers.insert(
668       packet_information->nack_sequence_numbers.end(),
669       nack.packet_ids().begin(), nack.packet_ids().end());
670   for (uint16_t packet_id : nack.packet_ids())
671     nack_stats_.ReportRequest(packet_id);
672 
673   if (!nack.packet_ids().empty()) {
674     packet_information->packet_type_flags |= kRtcpNack;
675     ++packet_type_counter_.nack_packets;
676     packet_type_counter_.nack_requests = nack_stats_.requests();
677     packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
678   }
679 }
680 
HandleBye(const CommonHeader & rtcp_block)681 void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
682   rtcp::Bye bye;
683   if (!bye.Parse(rtcp_block)) {
684     ++num_skipped_packets_;
685     return;
686   }
687 
688   if (rtcp_event_observer_) {
689     rtcp_event_observer_->OnRtcpBye();
690   }
691 
692   // Clear our lists.
693   for (auto& reports_per_receiver : received_report_blocks_)
694     reports_per_receiver.second.erase(bye.sender_ssrc());
695 
696   TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
697   if (tmmbr_info)
698     tmmbr_info->ready_for_delete = true;
699 
700   last_fir_.erase(bye.sender_ssrc());
701   received_cnames_.erase(bye.sender_ssrc());
702   xr_rr_rtt_ms_ = 0;
703 }
704 
HandleXr(const CommonHeader & rtcp_block,PacketInformation * packet_information)705 void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
706                             PacketInformation* packet_information) {
707   rtcp::ExtendedReports xr;
708   if (!xr.Parse(rtcp_block)) {
709     ++num_skipped_packets_;
710     return;
711   }
712 
713   if (xr.rrtr())
714     HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
715 
716   for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
717     HandleXrDlrrReportBlock(time_info);
718 
719   if (xr.target_bitrate()) {
720     HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
721                           packet_information);
722   }
723 }
724 
HandleXrReceiveReferenceTime(uint32_t sender_ssrc,const rtcp::Rrtr & rrtr)725 void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
726                                                 const rtcp::Rrtr& rrtr) {
727   remote_time_info_.ssrc = sender_ssrc;
728   remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
729   last_received_xr_ntp_ = clock_->CurrentNtpTime();
730 }
731 
HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo & rti)732 void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
733   if (registered_ssrcs_.count(rti.ssrc) == 0)  // Not to us.
734     return;
735 
736   // Caller should explicitly enable rtt calculation using extended reports.
737   if (!xr_rrtr_status_)
738     return;
739 
740   // The send_time and delay_rr fields are in units of 1/2^16 sec.
741   uint32_t send_time_ntp = rti.last_rr;
742   // RFC3611, section 4.5, LRR field discription states:
743   // If no such block has been received, the field is set to zero.
744   if (send_time_ntp == 0)
745     return;
746 
747   uint32_t delay_ntp = rti.delay_since_last_rr;
748   uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
749 
750   uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
751   xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
752 }
753 
HandleXrTargetBitrate(uint32_t ssrc,const rtcp::TargetBitrate & target_bitrate,PacketInformation * packet_information)754 void RTCPReceiver::HandleXrTargetBitrate(
755     uint32_t ssrc,
756     const rtcp::TargetBitrate& target_bitrate,
757     PacketInformation* packet_information) {
758   if (ssrc != remote_ssrc_) {
759     return;  // Not for us.
760   }
761 
762   BitrateAllocation bitrate_allocation;
763   for (const auto& item : target_bitrate.GetTargetBitrates()) {
764     if (item.spatial_layer >= kMaxSpatialLayers ||
765         item.temporal_layer >= kMaxTemporalStreams) {
766       RTC_LOG(LS_WARNING)
767           << "Invalid layer in XR target bitrate pack: spatial index "
768           << item.spatial_layer << ", temporal index " << item.temporal_layer
769           << ", dropping.";
770     } else {
771       bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
772                                     item.target_bitrate_kbps * 1000);
773     }
774   }
775   packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
776 }
777 
HandlePli(const CommonHeader & rtcp_block,PacketInformation * packet_information)778 void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
779                              PacketInformation* packet_information) {
780   rtcp::Pli pli;
781   if (!pli.Parse(rtcp_block)) {
782     ++num_skipped_packets_;
783     return;
784   }
785 
786   if (main_ssrc_ == pli.media_ssrc()) {
787     TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
788 
789     ++packet_type_counter_.pli_packets;
790     // Received a signal that we need to send a new key frame.
791     packet_information->packet_type_flags |= kRtcpPli;
792   }
793 }
794 
HandleTmmbr(const CommonHeader & rtcp_block,PacketInformation * packet_information)795 void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
796                                PacketInformation* packet_information) {
797   rtcp::Tmmbr tmmbr;
798   if (!tmmbr.Parse(rtcp_block)) {
799     ++num_skipped_packets_;
800     return;
801   }
802 
803   uint32_t sender_ssrc = tmmbr.sender_ssrc();
804   if (tmmbr.media_ssrc()) {
805     // media_ssrc() SHOULD be 0 if same as SenderSSRC.
806     // In relay mode this is a valid number.
807     sender_ssrc = tmmbr.media_ssrc();
808   }
809 
810   for (const rtcp::TmmbItem& request : tmmbr.requests()) {
811     if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
812       continue;
813 
814     TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
815     auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
816     entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
817                                        request.bitrate_bps(),
818                                        request.packet_overhead());
819     entry->last_updated_ms = clock_->TimeInMilliseconds();
820 
821     packet_information->packet_type_flags |= kRtcpTmmbr;
822     break;
823   }
824 }
825 
HandleTmmbn(const CommonHeader & rtcp_block,PacketInformation * packet_information)826 void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
827                                PacketInformation* packet_information) {
828   rtcp::Tmmbn tmmbn;
829   if (!tmmbn.Parse(rtcp_block)) {
830     ++num_skipped_packets_;
831     return;
832   }
833 
834   TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
835 
836   packet_information->packet_type_flags |= kRtcpTmmbn;
837 
838   tmmbr_info->tmmbn = tmmbn.items();
839 }
840 
HandleSrReq(const CommonHeader & rtcp_block,PacketInformation * packet_information)841 void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
842                                PacketInformation* packet_information) {
843   rtcp::RapidResyncRequest sr_req;
844   if (!sr_req.Parse(rtcp_block)) {
845     ++num_skipped_packets_;
846     return;
847   }
848 
849   packet_information->packet_type_flags |= kRtcpSrReq;
850 }
851 
HandlePsfbApp(const CommonHeader & rtcp_block,PacketInformation * packet_information)852 void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
853                                  PacketInformation* packet_information) {
854   rtcp::Remb remb;
855   if (remb.Parse(rtcp_block)) {
856     packet_information->packet_type_flags |= kRtcpRemb;
857     packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
858     return;
859   }
860 
861   ++num_skipped_packets_;
862 }
863 
HandleFir(const CommonHeader & rtcp_block,PacketInformation * packet_information)864 void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
865                              PacketInformation* packet_information) {
866   rtcp::Fir fir;
867   if (!fir.Parse(rtcp_block)) {
868     ++num_skipped_packets_;
869     return;
870   }
871 
872   for (const rtcp::Fir::Request& fir_request : fir.requests()) {
873     // Is it our sender that is requested to generate a new keyframe.
874     if (main_ssrc_ != fir_request.ssrc)
875       continue;
876 
877     ++packet_type_counter_.fir_packets;
878 
879     int64_t now_ms = clock_->TimeInMilliseconds();
880     auto inserted = last_fir_.insert(std::make_pair(
881         fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
882     if (!inserted.second) {  // There was already an entry.
883       LastFirStatus* last_fir = &inserted.first->second;
884 
885       // Check if we have reported this FIRSequenceNumber before.
886       if (fir_request.seq_nr == last_fir->sequence_number)
887         continue;
888 
889       // Sanity: don't go crazy with the callbacks.
890       if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
891         continue;
892 
893       last_fir->request_ms = now_ms;
894       last_fir->sequence_number = fir_request.seq_nr;
895     }
896     // Received signal that we need to send a new key frame.
897     packet_information->packet_type_flags |= kRtcpFir;
898   }
899 }
900 
HandleTransportFeedback(const CommonHeader & rtcp_block,PacketInformation * packet_information)901 void RTCPReceiver::HandleTransportFeedback(
902     const CommonHeader& rtcp_block,
903     PacketInformation* packet_information) {
904   std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
905       new rtcp::TransportFeedback());
906   if (!transport_feedback->Parse(rtcp_block)) {
907     ++num_skipped_packets_;
908     return;
909   }
910 
911   packet_information->packet_type_flags |= kRtcpTransportFeedback;
912   packet_information->transport_feedback = std::move(transport_feedback);
913 }
914 
NotifyTmmbrUpdated()915 void RTCPReceiver::NotifyTmmbrUpdated() {
916   // Find bounding set.
917   std::vector<rtcp::TmmbItem> bounding =
918       TMMBRHelp::FindBoundingSet(TmmbrReceived());
919 
920   if (!bounding.empty() && rtcp_bandwidth_observer_) {
921     // We have a new bandwidth estimate on this channel.
922     uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
923     if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
924       rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
925   }
926 
927   // Send tmmbn to inform remote clients about the new bandwidth.
928   rtp_rtcp_->SetTmmbn(std::move(bounding));
929 }
930 
RegisterRtcpStatisticsCallback(RtcpStatisticsCallback * callback)931 void RTCPReceiver::RegisterRtcpStatisticsCallback(
932     RtcpStatisticsCallback* callback) {
933   rtc::CritScope cs(&feedbacks_lock_);
934   stats_callback_ = callback;
935 }
936 
GetRtcpStatisticsCallback()937 RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
938   rtc::CritScope cs(&feedbacks_lock_);
939   return stats_callback_;
940 }
941 
942 // Holding no Critical section.
TriggerCallbacksFromRtcpPacket(const PacketInformation & packet_information)943 void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
944     const PacketInformation& packet_information) {
945   // Process TMMBR and REMB first to avoid multiple callbacks
946   // to OnNetworkChanged.
947   if (packet_information.packet_type_flags & kRtcpTmmbr) {
948     // Might trigger a OnReceivedBandwidthEstimateUpdate.
949     NotifyTmmbrUpdated();
950   }
951   uint32_t local_ssrc;
952   std::set<uint32_t> registered_ssrcs;
953   {
954     // We don't want to hold this critsect when triggering the callbacks below.
955     rtc::CritScope lock(&rtcp_receiver_lock_);
956     local_ssrc = main_ssrc_;
957     registered_ssrcs = registered_ssrcs_;
958   }
959   if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
960     rtp_rtcp_->OnRequestSendReport();
961   }
962   if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
963     if (!packet_information.nack_sequence_numbers.empty()) {
964       RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
965                           << packet_information.nack_sequence_numbers.size();
966       rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
967     }
968   }
969 
970   // We need feedback that we have received a report block(s) so that we
971   // can generate a new packet in a conference relay scenario, one received
972   // report can generate several RTCP packets, based on number relayed/mixed
973   // a send report block should go out to all receivers.
974   if (rtcp_intra_frame_observer_) {
975     RTC_DCHECK(!receiver_only_);
976     if ((packet_information.packet_type_flags & kRtcpPli) ||
977         (packet_information.packet_type_flags & kRtcpFir)) {
978       if (packet_information.packet_type_flags & kRtcpPli) {
979         RTC_LOG(LS_VERBOSE)
980             << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
981       } else {
982         RTC_LOG(LS_VERBOSE)
983             << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
984       }
985       rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
986     }
987   }
988   if (rtcp_bandwidth_observer_) {
989     RTC_DCHECK(!receiver_only_);
990     if (packet_information.packet_type_flags & kRtcpRemb) {
991       RTC_LOG(LS_VERBOSE)
992           << "Incoming REMB: "
993           << packet_information.receiver_estimated_max_bitrate_bps;
994       rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
995           packet_information.receiver_estimated_max_bitrate_bps);
996     }
997     if ((packet_information.packet_type_flags & kRtcpSr) ||
998         (packet_information.packet_type_flags & kRtcpRr)) {
999       int64_t now_ms = clock_->TimeInMilliseconds();
1000       rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
1001           packet_information.report_blocks, packet_information.rtt_ms, now_ms);
1002     }
1003   }
1004   if ((packet_information.packet_type_flags & kRtcpSr) ||
1005       (packet_information.packet_type_flags & kRtcpRr)) {
1006     rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
1007   }
1008 
1009   if (transport_feedback_observer_ &&
1010       (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1011     uint32_t media_source_ssrc =
1012         packet_information.transport_feedback->media_ssrc();
1013     if (media_source_ssrc == local_ssrc ||
1014         registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1015       transport_feedback_observer_->OnTransportFeedback(
1016           *packet_information.transport_feedback);
1017     }
1018   }
1019 
1020   if (bitrate_allocation_observer_ &&
1021       packet_information.target_bitrate_allocation) {
1022     bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1023         *packet_information.target_bitrate_allocation);
1024   }
1025 
1026   if (!receiver_only_) {
1027     rtc::CritScope cs(&feedbacks_lock_);
1028     if (stats_callback_) {
1029       for (const auto& report_block : packet_information.report_blocks) {
1030         RtcpStatistics stats;
1031         stats.packets_lost = report_block.packets_lost;
1032         stats.extended_highest_sequence_number =
1033             report_block.extended_highest_sequence_number;
1034         stats.fraction_lost = report_block.fraction_lost;
1035         stats.jitter = report_block.jitter;
1036 
1037         stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
1038       }
1039     }
1040   }
1041 }
1042 
CNAME(uint32_t remoteSSRC,char cName[RTCP_CNAME_SIZE]) const1043 int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
1044                             char cName[RTCP_CNAME_SIZE]) const {
1045   RTC_DCHECK(cName);
1046 
1047   rtc::CritScope lock(&rtcp_receiver_lock_);
1048   auto received_cname_it = received_cnames_.find(remoteSSRC);
1049   if (received_cname_it == received_cnames_.end())
1050     return -1;
1051 
1052   size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1053   cName[length] = 0;
1054   return 0;
1055 }
1056 
TmmbrReceived()1057 std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
1058   rtc::CritScope lock(&rtcp_receiver_lock_);
1059   std::vector<rtcp::TmmbItem> candidates;
1060 
1061   int64_t now_ms = clock_->TimeInMilliseconds();
1062   // Use audio define since we don't know what interval the remote peer use.
1063   int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
1064 
1065   for (auto& kv : tmmbr_infos_) {
1066     for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
1067       if (it->second.last_updated_ms < timeout_ms) {
1068         // Erase timeout entries.
1069         it = kv.second.tmmbr.erase(it);
1070       } else {
1071         candidates.push_back(it->second.tmmbr_item);
1072         ++it;
1073       }
1074     }
1075   }
1076   return candidates;
1077 }
1078 
1079 }  // namespace webrtc
1080