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