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 <assert.h> 14 #include <string.h> 15 16 #include <algorithm> 17 18 #include "webrtc/base/checks.h" 19 #include "webrtc/base/logging.h" 20 #include "webrtc/base/trace_event.h" 21 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" 22 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" 23 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h" 24 25 namespace webrtc { 26 using RTCPHelp::RTCPPacketInformation; 27 using RTCPHelp::RTCPReceiveInformation; 28 using RTCPHelp::RTCPReportBlockInformation; 29 using RTCPUtility::kBtVoipMetric; 30 using RTCPUtility::RTCPCnameInformation; 31 using RTCPUtility::RTCPPacketReportBlockItem; 32 using RTCPUtility::RTCPPacketTypes; 33 34 // The number of RTCP time intervals needed to trigger a timeout. 35 const int kRrTimeoutIntervals = 3; 36 37 const int64_t kMaxWarningLogIntervalMs = 10000; 38 39 RTCPReceiver::RTCPReceiver( 40 Clock* clock, 41 bool receiver_only, 42 RtcpPacketTypeCounterObserver* packet_type_counter_observer, 43 RtcpBandwidthObserver* rtcp_bandwidth_observer, 44 RtcpIntraFrameObserver* rtcp_intra_frame_observer, 45 TransportFeedbackObserver* transport_feedback_observer, 46 ModuleRtpRtcpImpl* owner) 47 : TMMBRHelp(), 48 _clock(clock), 49 receiver_only_(receiver_only), 50 _method(RtcpMode::kOff), 51 _lastReceived(0), 52 _rtpRtcp(*owner), 53 _criticalSectionFeedbacks( 54 CriticalSectionWrapper::CreateCriticalSection()), 55 _cbRtcpBandwidthObserver(rtcp_bandwidth_observer), 56 _cbRtcpIntraFrameObserver(rtcp_intra_frame_observer), 57 _cbTransportFeedbackObserver(transport_feedback_observer), 58 _criticalSectionRTCPReceiver( 59 CriticalSectionWrapper::CreateCriticalSection()), 60 main_ssrc_(0), 61 _remoteSSRC(0), 62 _remoteSenderInfo(), 63 _lastReceivedSRNTPsecs(0), 64 _lastReceivedSRNTPfrac(0), 65 _lastReceivedXRNTPsecs(0), 66 _lastReceivedXRNTPfrac(0), 67 xr_rr_rtt_ms_(0), 68 _receivedInfoMap(), 69 _packetTimeOutMS(0), 70 _lastReceivedRrMs(0), 71 _lastIncreasedSequenceNumberMs(0), 72 stats_callback_(NULL), 73 packet_type_counter_observer_(packet_type_counter_observer), 74 num_skipped_packets_(0), 75 last_skipped_packets_warning_(clock->TimeInMilliseconds()) { 76 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo)); 77 } 78 79 RTCPReceiver::~RTCPReceiver() { 80 delete _criticalSectionRTCPReceiver; 81 delete _criticalSectionFeedbacks; 82 83 ReportBlockMap::iterator it = _receivedReportBlockMap.begin(); 84 for (; it != _receivedReportBlockMap.end(); ++it) { 85 ReportBlockInfoMap* info_map = &(it->second); 86 while (!info_map->empty()) { 87 ReportBlockInfoMap::iterator it_info = info_map->begin(); 88 delete it_info->second; 89 info_map->erase(it_info); 90 } 91 } 92 while (!_receivedInfoMap.empty()) { 93 std::map<uint32_t, RTCPReceiveInformation*>::iterator first = 94 _receivedInfoMap.begin(); 95 delete first->second; 96 _receivedInfoMap.erase(first); 97 } 98 while (!_receivedCnameMap.empty()) { 99 std::map<uint32_t, RTCPCnameInformation*>::iterator first = 100 _receivedCnameMap.begin(); 101 delete first->second; 102 _receivedCnameMap.erase(first); 103 } 104 } 105 106 RtcpMode RTCPReceiver::Status() const { 107 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 108 return _method; 109 } 110 111 void RTCPReceiver::SetRTCPStatus(RtcpMode method) { 112 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 113 _method = method; 114 } 115 116 int64_t RTCPReceiver::LastReceived() { 117 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 118 return _lastReceived; 119 } 120 121 int64_t RTCPReceiver::LastReceivedReceiverReport() const { 122 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 123 int64_t last_received_rr = -1; 124 for (ReceivedInfoMap::const_iterator it = _receivedInfoMap.begin(); 125 it != _receivedInfoMap.end(); ++it) { 126 if (it->second->lastTimeReceived > last_received_rr) { 127 last_received_rr = it->second->lastTimeReceived; 128 } 129 } 130 return last_received_rr; 131 } 132 133 void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) { 134 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 135 136 // new SSRC reset old reports 137 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo)); 138 _lastReceivedSRNTPsecs = 0; 139 _lastReceivedSRNTPfrac = 0; 140 141 _remoteSSRC = ssrc; 142 } 143 144 uint32_t RTCPReceiver::RemoteSSRC() const { 145 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 146 return _remoteSSRC; 147 } 148 149 void RTCPReceiver::SetSsrcs(uint32_t main_ssrc, 150 const std::set<uint32_t>& registered_ssrcs) { 151 uint32_t old_ssrc = 0; 152 { 153 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 154 old_ssrc = main_ssrc_; 155 main_ssrc_ = main_ssrc; 156 registered_ssrcs_ = registered_ssrcs; 157 } 158 { 159 if (_cbRtcpIntraFrameObserver && old_ssrc != main_ssrc) { 160 _cbRtcpIntraFrameObserver->OnLocalSsrcChanged(old_ssrc, main_ssrc); 161 } 162 } 163 } 164 165 int32_t RTCPReceiver::RTT(uint32_t remoteSSRC, 166 int64_t* RTT, 167 int64_t* avgRTT, 168 int64_t* minRTT, 169 int64_t* maxRTT) const { 170 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 171 172 RTCPReportBlockInformation* reportBlock = 173 GetReportBlockInformation(remoteSSRC, main_ssrc_); 174 175 if (reportBlock == NULL) { 176 return -1; 177 } 178 if (RTT) { 179 *RTT = reportBlock->RTT; 180 } 181 if (avgRTT) { 182 *avgRTT = reportBlock->avgRTT; 183 } 184 if (minRTT) { 185 *minRTT = reportBlock->minRTT; 186 } 187 if (maxRTT) { 188 *maxRTT = reportBlock->maxRTT; 189 } 190 return 0; 191 } 192 193 bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) { 194 assert(rtt_ms); 195 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 196 if (xr_rr_rtt_ms_ == 0) { 197 return false; 198 } 199 *rtt_ms = xr_rr_rtt_ms_; 200 xr_rr_rtt_ms_ = 0; 201 return true; 202 } 203 204 // TODO(pbos): Make this fail when we haven't received NTP. 205 bool RTCPReceiver::NTP(uint32_t* ReceivedNTPsecs, 206 uint32_t* ReceivedNTPfrac, 207 uint32_t* RTCPArrivalTimeSecs, 208 uint32_t* RTCPArrivalTimeFrac, 209 uint32_t* rtcp_timestamp) const 210 { 211 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 212 if(ReceivedNTPsecs) 213 { 214 *ReceivedNTPsecs = _remoteSenderInfo.NTPseconds; // NTP from incoming SendReport 215 } 216 if(ReceivedNTPfrac) 217 { 218 *ReceivedNTPfrac = _remoteSenderInfo.NTPfraction; 219 } 220 if(RTCPArrivalTimeFrac) 221 { 222 *RTCPArrivalTimeFrac = _lastReceivedSRNTPfrac; // local NTP time when we received a RTCP packet with a send block 223 } 224 if(RTCPArrivalTimeSecs) 225 { 226 *RTCPArrivalTimeSecs = _lastReceivedSRNTPsecs; 227 } 228 if (rtcp_timestamp) { 229 *rtcp_timestamp = _remoteSenderInfo.RTPtimeStamp; 230 } 231 return true; 232 } 233 234 bool RTCPReceiver::LastReceivedXrReferenceTimeInfo( 235 RtcpReceiveTimeInfo* info) const { 236 assert(info); 237 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 238 if (_lastReceivedXRNTPsecs == 0 && _lastReceivedXRNTPfrac == 0) { 239 return false; 240 } 241 242 info->sourceSSRC = _remoteXRReceiveTimeInfo.sourceSSRC; 243 info->lastRR = _remoteXRReceiveTimeInfo.lastRR; 244 245 // Get the delay since last received report (RFC 3611). 246 uint32_t receive_time = RTCPUtility::MidNtp(_lastReceivedXRNTPsecs, 247 _lastReceivedXRNTPfrac); 248 249 uint32_t ntp_sec = 0; 250 uint32_t ntp_frac = 0; 251 _clock->CurrentNtp(ntp_sec, ntp_frac); 252 uint32_t now = RTCPUtility::MidNtp(ntp_sec, ntp_frac); 253 254 info->delaySinceLastRR = now - receive_time; 255 return true; 256 } 257 258 int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* senderInfo) const { 259 assert(senderInfo); 260 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 261 if (_lastReceivedSRNTPsecs == 0) { 262 return -1; 263 } 264 memcpy(senderInfo, &(_remoteSenderInfo), sizeof(RTCPSenderInfo)); 265 return 0; 266 } 267 268 // statistics 269 // we can get multiple receive reports when we receive the report from a CE 270 int32_t RTCPReceiver::StatisticsReceived( 271 std::vector<RTCPReportBlock>* receiveBlocks) const { 272 assert(receiveBlocks); 273 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 274 ReportBlockMap::const_iterator it = _receivedReportBlockMap.begin(); 275 for (; it != _receivedReportBlockMap.end(); ++it) { 276 const ReportBlockInfoMap* info_map = &(it->second); 277 ReportBlockInfoMap::const_iterator it_info = info_map->begin(); 278 for (; it_info != info_map->end(); ++it_info) { 279 receiveBlocks->push_back(it_info->second->remoteReceiveBlock); 280 } 281 } 282 return 0; 283 } 284 285 int32_t 286 RTCPReceiver::IncomingRTCPPacket(RTCPPacketInformation& rtcpPacketInformation, 287 RTCPUtility::RTCPParserV2* rtcpParser) 288 { 289 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 290 291 _lastReceived = _clock->TimeInMilliseconds(); 292 293 if (packet_type_counter_.first_packet_time_ms == -1) { 294 packet_type_counter_.first_packet_time_ms = _lastReceived; 295 } 296 297 RTCPUtility::RTCPPacketTypes pktType = rtcpParser->Begin(); 298 while (pktType != RTCPPacketTypes::kInvalid) { 299 // Each "case" is responsible for iterate the parser to the 300 // next top level packet. 301 switch (pktType) 302 { 303 case RTCPPacketTypes::kSr: 304 case RTCPPacketTypes::kRr: 305 HandleSenderReceiverReport(*rtcpParser, rtcpPacketInformation); 306 break; 307 case RTCPPacketTypes::kSdes: 308 HandleSDES(*rtcpParser, rtcpPacketInformation); 309 break; 310 case RTCPPacketTypes::kXrHeader: 311 HandleXrHeader(*rtcpParser, rtcpPacketInformation); 312 break; 313 case RTCPPacketTypes::kXrReceiverReferenceTime: 314 HandleXrReceiveReferenceTime(*rtcpParser, rtcpPacketInformation); 315 break; 316 case RTCPPacketTypes::kXrDlrrReportBlock: 317 HandleXrDlrrReportBlock(*rtcpParser, rtcpPacketInformation); 318 break; 319 case RTCPPacketTypes::kXrVoipMetric: 320 HandleXRVOIPMetric(*rtcpParser, rtcpPacketInformation); 321 break; 322 case RTCPPacketTypes::kBye: 323 HandleBYE(*rtcpParser); 324 break; 325 case RTCPPacketTypes::kRtpfbNack: 326 HandleNACK(*rtcpParser, rtcpPacketInformation); 327 break; 328 case RTCPPacketTypes::kRtpfbTmmbr: 329 HandleTMMBR(*rtcpParser, rtcpPacketInformation); 330 break; 331 case RTCPPacketTypes::kRtpfbTmmbn: 332 HandleTMMBN(*rtcpParser, rtcpPacketInformation); 333 break; 334 case RTCPPacketTypes::kRtpfbSrReq: 335 HandleSR_REQ(*rtcpParser, rtcpPacketInformation); 336 break; 337 case RTCPPacketTypes::kPsfbPli: 338 HandlePLI(*rtcpParser, rtcpPacketInformation); 339 break; 340 case RTCPPacketTypes::kPsfbSli: 341 HandleSLI(*rtcpParser, rtcpPacketInformation); 342 break; 343 case RTCPPacketTypes::kPsfbRpsi: 344 HandleRPSI(*rtcpParser, rtcpPacketInformation); 345 break; 346 case RTCPPacketTypes::kExtendedIj: 347 HandleIJ(*rtcpParser, rtcpPacketInformation); 348 break; 349 case RTCPPacketTypes::kPsfbFir: 350 HandleFIR(*rtcpParser, rtcpPacketInformation); 351 break; 352 case RTCPPacketTypes::kPsfbApp: 353 HandlePsfbApp(*rtcpParser, rtcpPacketInformation); 354 break; 355 case RTCPPacketTypes::kApp: 356 // generic application messages 357 HandleAPP(*rtcpParser, rtcpPacketInformation); 358 break; 359 case RTCPPacketTypes::kAppItem: 360 // generic application messages 361 HandleAPPItem(*rtcpParser, rtcpPacketInformation); 362 break; 363 case RTCPPacketTypes::kTransportFeedback: 364 HandleTransportFeedback(rtcpParser, &rtcpPacketInformation); 365 break; 366 default: 367 rtcpParser->Iterate(); 368 break; 369 } 370 pktType = rtcpParser->PacketType(); 371 } 372 373 if (packet_type_counter_observer_ != NULL) { 374 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated( 375 main_ssrc_, packet_type_counter_); 376 } 377 378 num_skipped_packets_ += rtcpParser->NumSkippedBlocks(); 379 380 int64_t now = _clock->TimeInMilliseconds(); 381 if (now - last_skipped_packets_warning_ >= kMaxWarningLogIntervalMs && 382 num_skipped_packets_ > 0) { 383 last_skipped_packets_warning_ = now; 384 LOG(LS_WARNING) 385 << num_skipped_packets_ 386 << " RTCP blocks were skipped due to being malformed or of " 387 "unrecognized/unsupported type, during the past " 388 << (kMaxWarningLogIntervalMs / 1000) << " second period."; 389 } 390 391 return 0; 392 } 393 394 void 395 RTCPReceiver::HandleSenderReceiverReport(RTCPUtility::RTCPParserV2& rtcpParser, 396 RTCPPacketInformation& rtcpPacketInformation) 397 { 398 RTCPUtility::RTCPPacketTypes rtcpPacketType = rtcpParser.PacketType(); 399 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 400 401 assert((rtcpPacketType == RTCPPacketTypes::kRr) || 402 (rtcpPacketType == RTCPPacketTypes::kSr)); 403 404 // SR.SenderSSRC 405 // The synchronization source identifier for the originator of this SR packet 406 407 // rtcpPacket.RR.SenderSSRC 408 // The source of the packet sender, same as of SR? or is this a CE? 409 410 const uint32_t remoteSSRC = (rtcpPacketType == RTCPPacketTypes::kRr) 411 ? rtcpPacket.RR.SenderSSRC 412 : rtcpPacket.SR.SenderSSRC; 413 414 rtcpPacketInformation.remoteSSRC = remoteSSRC; 415 416 RTCPReceiveInformation* ptrReceiveInfo = CreateReceiveInformation(remoteSSRC); 417 if (!ptrReceiveInfo) 418 { 419 rtcpParser.Iterate(); 420 return; 421 } 422 423 if (rtcpPacketType == RTCPPacketTypes::kSr) { 424 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR", 425 "remote_ssrc", remoteSSRC, "ssrc", main_ssrc_); 426 427 if (_remoteSSRC == remoteSSRC) // have I received RTP packets from this party 428 { 429 // only signal that we have received a SR when we accept one 430 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSr; 431 432 rtcpPacketInformation.ntp_secs = rtcpPacket.SR.NTPMostSignificant; 433 rtcpPacketInformation.ntp_frac = rtcpPacket.SR.NTPLeastSignificant; 434 rtcpPacketInformation.rtp_timestamp = rtcpPacket.SR.RTPTimestamp; 435 436 // We will only store the send report from one source, but 437 // we will store all the receive block 438 439 // Save the NTP time of this report 440 _remoteSenderInfo.NTPseconds = rtcpPacket.SR.NTPMostSignificant; 441 _remoteSenderInfo.NTPfraction = rtcpPacket.SR.NTPLeastSignificant; 442 _remoteSenderInfo.RTPtimeStamp = rtcpPacket.SR.RTPTimestamp; 443 _remoteSenderInfo.sendPacketCount = rtcpPacket.SR.SenderPacketCount; 444 _remoteSenderInfo.sendOctetCount = rtcpPacket.SR.SenderOctetCount; 445 446 _clock->CurrentNtp(_lastReceivedSRNTPsecs, _lastReceivedSRNTPfrac); 447 } 448 else 449 { 450 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr; 451 } 452 } else 453 { 454 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR", 455 "remote_ssrc", remoteSSRC, "ssrc", main_ssrc_); 456 457 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr; 458 } 459 UpdateReceiveInformation(*ptrReceiveInfo); 460 461 rtcpPacketType = rtcpParser.Iterate(); 462 463 while (rtcpPacketType == RTCPPacketTypes::kReportBlockItem) { 464 HandleReportBlock(rtcpPacket, rtcpPacketInformation, remoteSSRC); 465 rtcpPacketType = rtcpParser.Iterate(); 466 } 467 } 468 469 void RTCPReceiver::HandleReportBlock( 470 const RTCPUtility::RTCPPacket& rtcpPacket, 471 RTCPPacketInformation& rtcpPacketInformation, 472 uint32_t remoteSSRC) 473 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver) { 474 // This will be called once per report block in the RTCP packet. 475 // We filter out all report blocks that are not for us. 476 // Each packet has max 31 RR blocks. 477 // 478 // We can calc RTT if we send a send report and get a report block back. 479 480 // |rtcpPacket.ReportBlockItem.SSRC| is the SSRC identifier of the source to 481 // which the information in this reception report block pertains. 482 483 // Filter out all report blocks that are not for us. 484 if (registered_ssrcs_.find(rtcpPacket.ReportBlockItem.SSRC) == 485 registered_ssrcs_.end()) { 486 // This block is not for us ignore it. 487 return; 488 } 489 490 // To avoid problem with acquiring _criticalSectionRTCPSender while holding 491 // _criticalSectionRTCPReceiver. 492 _criticalSectionRTCPReceiver->Leave(); 493 int64_t sendTimeMS = 494 _rtpRtcp.SendTimeOfSendReport(rtcpPacket.ReportBlockItem.LastSR); 495 _criticalSectionRTCPReceiver->Enter(); 496 497 RTCPReportBlockInformation* reportBlock = 498 CreateOrGetReportBlockInformation(remoteSSRC, 499 rtcpPacket.ReportBlockItem.SSRC); 500 if (reportBlock == NULL) { 501 LOG(LS_WARNING) << "Failed to CreateReportBlockInformation(" 502 << remoteSSRC << ")"; 503 return; 504 } 505 506 _lastReceivedRrMs = _clock->TimeInMilliseconds(); 507 const RTCPPacketReportBlockItem& rb = rtcpPacket.ReportBlockItem; 508 reportBlock->remoteReceiveBlock.remoteSSRC = remoteSSRC; 509 reportBlock->remoteReceiveBlock.sourceSSRC = rb.SSRC; 510 reportBlock->remoteReceiveBlock.fractionLost = rb.FractionLost; 511 reportBlock->remoteReceiveBlock.cumulativeLost = 512 rb.CumulativeNumOfPacketsLost; 513 if (rb.ExtendedHighestSequenceNumber > 514 reportBlock->remoteReceiveBlock.extendedHighSeqNum) { 515 // We have successfully delivered new RTP packets to the remote side after 516 // the last RR was sent from the remote side. 517 _lastIncreasedSequenceNumberMs = _lastReceivedRrMs; 518 } 519 reportBlock->remoteReceiveBlock.extendedHighSeqNum = 520 rb.ExtendedHighestSequenceNumber; 521 reportBlock->remoteReceiveBlock.jitter = rb.Jitter; 522 reportBlock->remoteReceiveBlock.delaySinceLastSR = rb.DelayLastSR; 523 reportBlock->remoteReceiveBlock.lastSR = rb.LastSR; 524 525 if (rtcpPacket.ReportBlockItem.Jitter > reportBlock->remoteMaxJitter) { 526 reportBlock->remoteMaxJitter = rtcpPacket.ReportBlockItem.Jitter; 527 } 528 529 uint32_t delaySinceLastSendReport = 530 rtcpPacket.ReportBlockItem.DelayLastSR; 531 532 // local NTP time when we received this 533 uint32_t lastReceivedRRNTPsecs = 0; 534 uint32_t lastReceivedRRNTPfrac = 0; 535 536 _clock->CurrentNtp(lastReceivedRRNTPsecs, lastReceivedRRNTPfrac); 537 538 // time when we received this in MS 539 int64_t receiveTimeMS = Clock::NtpToMs(lastReceivedRRNTPsecs, 540 lastReceivedRRNTPfrac); 541 542 // Estimate RTT 543 uint32_t d = (delaySinceLastSendReport & 0x0000ffff) * 1000; 544 d /= 65536; 545 d += ((delaySinceLastSendReport & 0xffff0000) >> 16) * 1000; 546 547 int64_t RTT = 0; 548 549 if (sendTimeMS > 0) { 550 RTT = receiveTimeMS - d - sendTimeMS; 551 if (RTT <= 0) { 552 RTT = 1; 553 } 554 if (RTT > reportBlock->maxRTT) { 555 // store max RTT 556 reportBlock->maxRTT = RTT; 557 } 558 if (reportBlock->minRTT == 0) { 559 // first RTT 560 reportBlock->minRTT = RTT; 561 } else if (RTT < reportBlock->minRTT) { 562 // Store min RTT 563 reportBlock->minRTT = RTT; 564 } 565 // store last RTT 566 reportBlock->RTT = RTT; 567 568 // store average RTT 569 if (reportBlock->numAverageCalcs != 0) { 570 float ac = static_cast<float>(reportBlock->numAverageCalcs); 571 float newAverage = 572 ((ac / (ac + 1)) * reportBlock->avgRTT) + ((1 / (ac + 1)) * RTT); 573 reportBlock->avgRTT = static_cast<int64_t>(newAverage + 0.5f); 574 } else { 575 // first RTT 576 reportBlock->avgRTT = RTT; 577 } 578 reportBlock->numAverageCalcs++; 579 } 580 581 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT", rb.SSRC, 582 RTT); 583 584 rtcpPacketInformation.AddReportInfo(*reportBlock); 585 } 586 587 RTCPReportBlockInformation* RTCPReceiver::CreateOrGetReportBlockInformation( 588 uint32_t remote_ssrc, 589 uint32_t source_ssrc) { 590 RTCPReportBlockInformation* info = 591 GetReportBlockInformation(remote_ssrc, source_ssrc); 592 if (info == NULL) { 593 info = new RTCPReportBlockInformation; 594 _receivedReportBlockMap[source_ssrc][remote_ssrc] = info; 595 } 596 return info; 597 } 598 599 RTCPReportBlockInformation* RTCPReceiver::GetReportBlockInformation( 600 uint32_t remote_ssrc, 601 uint32_t source_ssrc) const { 602 ReportBlockMap::const_iterator it = _receivedReportBlockMap.find(source_ssrc); 603 if (it == _receivedReportBlockMap.end()) { 604 return NULL; 605 } 606 const ReportBlockInfoMap* info_map = &(it->second); 607 ReportBlockInfoMap::const_iterator it_info = info_map->find(remote_ssrc); 608 if (it_info == info_map->end()) { 609 return NULL; 610 } 611 return it_info->second; 612 } 613 614 RTCPCnameInformation* 615 RTCPReceiver::CreateCnameInformation(uint32_t remoteSSRC) { 616 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 617 618 std::map<uint32_t, RTCPCnameInformation*>::iterator it = 619 _receivedCnameMap.find(remoteSSRC); 620 621 if (it != _receivedCnameMap.end()) { 622 return it->second; 623 } 624 RTCPCnameInformation* cnameInfo = new RTCPCnameInformation; 625 memset(cnameInfo->name, 0, RTCP_CNAME_SIZE); 626 _receivedCnameMap[remoteSSRC] = cnameInfo; 627 return cnameInfo; 628 } 629 630 RTCPCnameInformation* 631 RTCPReceiver::GetCnameInformation(uint32_t remoteSSRC) const { 632 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 633 634 std::map<uint32_t, RTCPCnameInformation*>::const_iterator it = 635 _receivedCnameMap.find(remoteSSRC); 636 637 if (it == _receivedCnameMap.end()) { 638 return NULL; 639 } 640 return it->second; 641 } 642 643 RTCPReceiveInformation* 644 RTCPReceiver::CreateReceiveInformation(uint32_t remoteSSRC) { 645 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 646 647 std::map<uint32_t, RTCPReceiveInformation*>::iterator it = 648 _receivedInfoMap.find(remoteSSRC); 649 650 if (it != _receivedInfoMap.end()) { 651 return it->second; 652 } 653 RTCPReceiveInformation* receiveInfo = new RTCPReceiveInformation; 654 _receivedInfoMap[remoteSSRC] = receiveInfo; 655 return receiveInfo; 656 } 657 658 RTCPReceiveInformation* 659 RTCPReceiver::GetReceiveInformation(uint32_t remoteSSRC) { 660 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 661 662 std::map<uint32_t, RTCPReceiveInformation*>::iterator it = 663 _receivedInfoMap.find(remoteSSRC); 664 if (it == _receivedInfoMap.end()) { 665 return NULL; 666 } 667 return it->second; 668 } 669 670 void RTCPReceiver::UpdateReceiveInformation( 671 RTCPReceiveInformation& receiveInformation) { 672 // Update that this remote is alive 673 receiveInformation.lastTimeReceived = _clock->TimeInMilliseconds(); 674 } 675 676 bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) { 677 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 678 if (_lastReceivedRrMs == 0) 679 return false; 680 681 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms; 682 if (_clock->TimeInMilliseconds() > _lastReceivedRrMs + time_out_ms) { 683 // Reset the timer to only trigger one log. 684 _lastReceivedRrMs = 0; 685 return true; 686 } 687 return false; 688 } 689 690 bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) { 691 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 692 if (_lastIncreasedSequenceNumberMs == 0) 693 return false; 694 695 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms; 696 if (_clock->TimeInMilliseconds() > _lastIncreasedSequenceNumberMs + 697 time_out_ms) { 698 // Reset the timer to only trigger one log. 699 _lastIncreasedSequenceNumberMs = 0; 700 return true; 701 } 702 return false; 703 } 704 705 bool RTCPReceiver::UpdateRTCPReceiveInformationTimers() { 706 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 707 708 bool updateBoundingSet = false; 709 int64_t timeNow = _clock->TimeInMilliseconds(); 710 711 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt = 712 _receivedInfoMap.begin(); 713 714 while (receiveInfoIt != _receivedInfoMap.end()) { 715 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second; 716 if (receiveInfo == NULL) { 717 return updateBoundingSet; 718 } 719 // time since last received rtcp packet 720 // when we dont have a lastTimeReceived and the object is marked 721 // readyForDelete it's removed from the map 722 if (receiveInfo->lastTimeReceived) { 723 /// use audio define since we don't know what interval the remote peer is 724 // using 725 if ((timeNow - receiveInfo->lastTimeReceived) > 726 5 * RTCP_INTERVAL_AUDIO_MS) { 727 // no rtcp packet for the last five regular intervals, reset limitations 728 receiveInfo->TmmbrSet.clearSet(); 729 // prevent that we call this over and over again 730 receiveInfo->lastTimeReceived = 0; 731 // send new TMMBN to all channels using the default codec 732 updateBoundingSet = true; 733 } 734 receiveInfoIt++; 735 } else if (receiveInfo->readyForDelete) { 736 // store our current receiveInfoItem 737 std::map<uint32_t, RTCPReceiveInformation*>::iterator 738 receiveInfoItemToBeErased = receiveInfoIt; 739 receiveInfoIt++; 740 delete receiveInfoItemToBeErased->second; 741 _receivedInfoMap.erase(receiveInfoItemToBeErased); 742 } else { 743 receiveInfoIt++; 744 } 745 } 746 return updateBoundingSet; 747 } 748 749 int32_t RTCPReceiver::BoundingSet(bool* tmmbrOwner, TMMBRSet* boundingSetRec) { 750 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 751 752 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt = 753 _receivedInfoMap.find(_remoteSSRC); 754 755 if (receiveInfoIt == _receivedInfoMap.end()) { 756 return -1; 757 } 758 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second; 759 if (receiveInfo == NULL) { 760 return -1; 761 } 762 if (receiveInfo->TmmbnBoundingSet.lengthOfSet() > 0) { 763 boundingSetRec->VerifyAndAllocateSet( 764 receiveInfo->TmmbnBoundingSet.lengthOfSet() + 1); 765 for(uint32_t i=0; i< receiveInfo->TmmbnBoundingSet.lengthOfSet(); 766 i++) { 767 if(receiveInfo->TmmbnBoundingSet.Ssrc(i) == main_ssrc_) { 768 // owner of bounding set 769 *tmmbrOwner = true; 770 } 771 boundingSetRec->SetEntry(i, 772 receiveInfo->TmmbnBoundingSet.Tmmbr(i), 773 receiveInfo->TmmbnBoundingSet.PacketOH(i), 774 receiveInfo->TmmbnBoundingSet.Ssrc(i)); 775 } 776 } 777 return receiveInfo->TmmbnBoundingSet.lengthOfSet(); 778 } 779 780 void RTCPReceiver::HandleSDES(RTCPUtility::RTCPParserV2& rtcpParser, 781 RTCPPacketInformation& rtcpPacketInformation) { 782 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 783 while (pktType == RTCPPacketTypes::kSdesChunk) { 784 HandleSDESChunk(rtcpParser); 785 pktType = rtcpParser.Iterate(); 786 } 787 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSdes; 788 } 789 790 void RTCPReceiver::HandleSDESChunk(RTCPUtility::RTCPParserV2& rtcpParser) { 791 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 792 RTCPCnameInformation* cnameInfo = 793 CreateCnameInformation(rtcpPacket.CName.SenderSSRC); 794 assert(cnameInfo); 795 796 cnameInfo->name[RTCP_CNAME_SIZE - 1] = 0; 797 strncpy(cnameInfo->name, rtcpPacket.CName.CName, RTCP_CNAME_SIZE - 1); 798 { 799 CriticalSectionScoped lock(_criticalSectionFeedbacks); 800 if (stats_callback_ != NULL) { 801 stats_callback_->CNameChanged(rtcpPacket.CName.CName, 802 rtcpPacket.CName.SenderSSRC); 803 } 804 } 805 } 806 807 void RTCPReceiver::HandleNACK(RTCPUtility::RTCPParserV2& rtcpParser, 808 RTCPPacketInformation& rtcpPacketInformation) { 809 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 810 if (receiver_only_ || main_ssrc_ != rtcpPacket.NACK.MediaSSRC) { 811 // Not to us. 812 rtcpParser.Iterate(); 813 return; 814 } 815 rtcpPacketInformation.ResetNACKPacketIdArray(); 816 817 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 818 while (pktType == RTCPPacketTypes::kRtpfbNackItem) { 819 HandleNACKItem(rtcpPacket, rtcpPacketInformation); 820 pktType = rtcpParser.Iterate(); 821 } 822 823 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpNack) { 824 ++packet_type_counter_.nack_packets; 825 packet_type_counter_.nack_requests = nack_stats_.requests(); 826 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests(); 827 } 828 } 829 830 void 831 RTCPReceiver::HandleNACKItem(const RTCPUtility::RTCPPacket& rtcpPacket, 832 RTCPPacketInformation& rtcpPacketInformation) { 833 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID); 834 nack_stats_.ReportRequest(rtcpPacket.NACKItem.PacketID); 835 836 uint16_t bitMask = rtcpPacket.NACKItem.BitMask; 837 if (bitMask) { 838 for (int i=1; i <= 16; ++i) { 839 if (bitMask & 0x01) { 840 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID + i); 841 nack_stats_.ReportRequest(rtcpPacket.NACKItem.PacketID + i); 842 } 843 bitMask = bitMask >>1; 844 } 845 } 846 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpNack; 847 } 848 849 void RTCPReceiver::HandleBYE(RTCPUtility::RTCPParserV2& rtcpParser) { 850 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 851 852 // clear our lists 853 ReportBlockMap::iterator it = _receivedReportBlockMap.begin(); 854 for (; it != _receivedReportBlockMap.end(); ++it) { 855 ReportBlockInfoMap* info_map = &(it->second); 856 ReportBlockInfoMap::iterator it_info = info_map->find( 857 rtcpPacket.BYE.SenderSSRC); 858 if (it_info != info_map->end()) { 859 delete it_info->second; 860 info_map->erase(it_info); 861 } 862 } 863 864 // we can't delete it due to TMMBR 865 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt = 866 _receivedInfoMap.find(rtcpPacket.BYE.SenderSSRC); 867 868 if (receiveInfoIt != _receivedInfoMap.end()) { 869 receiveInfoIt->second->readyForDelete = true; 870 } 871 872 std::map<uint32_t, RTCPCnameInformation*>::iterator cnameInfoIt = 873 _receivedCnameMap.find(rtcpPacket.BYE.SenderSSRC); 874 875 if (cnameInfoIt != _receivedCnameMap.end()) { 876 delete cnameInfoIt->second; 877 _receivedCnameMap.erase(cnameInfoIt); 878 } 879 xr_rr_rtt_ms_ = 0; 880 rtcpParser.Iterate(); 881 } 882 883 void RTCPReceiver::HandleXrHeader( 884 RTCPUtility::RTCPParserV2& parser, 885 RTCPPacketInformation& rtcpPacketInformation) { 886 const RTCPUtility::RTCPPacket& packet = parser.Packet(); 887 888 rtcpPacketInformation.xr_originator_ssrc = packet.XR.OriginatorSSRC; 889 890 parser.Iterate(); 891 } 892 893 void RTCPReceiver::HandleXrReceiveReferenceTime( 894 RTCPUtility::RTCPParserV2& parser, 895 RTCPPacketInformation& rtcpPacketInformation) { 896 const RTCPUtility::RTCPPacket& packet = parser.Packet(); 897 898 _remoteXRReceiveTimeInfo.sourceSSRC = 899 rtcpPacketInformation.xr_originator_ssrc; 900 901 _remoteXRReceiveTimeInfo.lastRR = RTCPUtility::MidNtp( 902 packet.XRReceiverReferenceTimeItem.NTPMostSignificant, 903 packet.XRReceiverReferenceTimeItem.NTPLeastSignificant); 904 905 _clock->CurrentNtp(_lastReceivedXRNTPsecs, _lastReceivedXRNTPfrac); 906 907 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrReceiverReferenceTime; 908 909 parser.Iterate(); 910 } 911 912 void RTCPReceiver::HandleXrDlrrReportBlock( 913 RTCPUtility::RTCPParserV2& parser, 914 RTCPPacketInformation& rtcpPacketInformation) { 915 const RTCPUtility::RTCPPacket& packet = parser.Packet(); 916 // Iterate through sub-block(s), if any. 917 RTCPUtility::RTCPPacketTypes packet_type = parser.Iterate(); 918 919 while (packet_type == RTCPPacketTypes::kXrDlrrReportBlockItem) { 920 HandleXrDlrrReportBlockItem(packet, rtcpPacketInformation); 921 packet_type = parser.Iterate(); 922 } 923 } 924 925 void RTCPReceiver::HandleXrDlrrReportBlockItem( 926 const RTCPUtility::RTCPPacket& packet, 927 RTCPPacketInformation& rtcpPacketInformation) 928 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver) { 929 if (registered_ssrcs_.find(packet.XRDLRRReportBlockItem.SSRC) == 930 registered_ssrcs_.end()) { 931 // Not to us. 932 return; 933 } 934 935 rtcpPacketInformation.xr_dlrr_item = true; 936 937 // To avoid problem with acquiring _criticalSectionRTCPSender while holding 938 // _criticalSectionRTCPReceiver. 939 _criticalSectionRTCPReceiver->Leave(); 940 941 int64_t send_time_ms; 942 bool found = _rtpRtcp.SendTimeOfXrRrReport( 943 packet.XRDLRRReportBlockItem.LastRR, &send_time_ms); 944 945 _criticalSectionRTCPReceiver->Enter(); 946 947 if (!found) { 948 return; 949 } 950 951 // The DelayLastRR field is in units of 1/65536 sec. 952 uint32_t delay_rr_ms = 953 (((packet.XRDLRRReportBlockItem.DelayLastRR & 0x0000ffff) * 1000) >> 16) + 954 (((packet.XRDLRRReportBlockItem.DelayLastRR & 0xffff0000) >> 16) * 1000); 955 956 int64_t rtt = _clock->CurrentNtpInMilliseconds() - delay_rr_ms - send_time_ms; 957 958 xr_rr_rtt_ms_ = std::max<int64_t>(rtt, 1); 959 960 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrDlrrReportBlock; 961 } 962 963 void 964 RTCPReceiver::HandleXRVOIPMetric(RTCPUtility::RTCPParserV2& rtcpParser, 965 RTCPPacketInformation& rtcpPacketInformation) 966 { 967 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 968 969 if(rtcpPacket.XRVOIPMetricItem.SSRC == main_ssrc_) 970 { 971 // Store VoIP metrics block if it's about me 972 // from OriginatorSSRC do we filter it? 973 // rtcpPacket.XR.OriginatorSSRC; 974 975 RTCPVoIPMetric receivedVoIPMetrics; 976 receivedVoIPMetrics.burstDensity = rtcpPacket.XRVOIPMetricItem.burstDensity; 977 receivedVoIPMetrics.burstDuration = rtcpPacket.XRVOIPMetricItem.burstDuration; 978 receivedVoIPMetrics.discardRate = rtcpPacket.XRVOIPMetricItem.discardRate; 979 receivedVoIPMetrics.endSystemDelay = rtcpPacket.XRVOIPMetricItem.endSystemDelay; 980 receivedVoIPMetrics.extRfactor = rtcpPacket.XRVOIPMetricItem.extRfactor; 981 receivedVoIPMetrics.gapDensity = rtcpPacket.XRVOIPMetricItem.gapDensity; 982 receivedVoIPMetrics.gapDuration = rtcpPacket.XRVOIPMetricItem.gapDuration; 983 receivedVoIPMetrics.Gmin = rtcpPacket.XRVOIPMetricItem.Gmin; 984 receivedVoIPMetrics.JBabsMax = rtcpPacket.XRVOIPMetricItem.JBabsMax; 985 receivedVoIPMetrics.JBmax = rtcpPacket.XRVOIPMetricItem.JBmax; 986 receivedVoIPMetrics.JBnominal = rtcpPacket.XRVOIPMetricItem.JBnominal; 987 receivedVoIPMetrics.lossRate = rtcpPacket.XRVOIPMetricItem.lossRate; 988 receivedVoIPMetrics.MOSCQ = rtcpPacket.XRVOIPMetricItem.MOSCQ; 989 receivedVoIPMetrics.MOSLQ = rtcpPacket.XRVOIPMetricItem.MOSLQ; 990 receivedVoIPMetrics.noiseLevel = rtcpPacket.XRVOIPMetricItem.noiseLevel; 991 receivedVoIPMetrics.RERL = rtcpPacket.XRVOIPMetricItem.RERL; 992 receivedVoIPMetrics.Rfactor = rtcpPacket.XRVOIPMetricItem.Rfactor; 993 receivedVoIPMetrics.roundTripDelay = rtcpPacket.XRVOIPMetricItem.roundTripDelay; 994 receivedVoIPMetrics.RXconfig = rtcpPacket.XRVOIPMetricItem.RXconfig; 995 receivedVoIPMetrics.signalLevel = rtcpPacket.XRVOIPMetricItem.signalLevel; 996 997 rtcpPacketInformation.AddVoIPMetric(&receivedVoIPMetrics); 998 999 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrVoipMetric; // received signal 1000 } 1001 rtcpParser.Iterate(); 1002 } 1003 1004 void RTCPReceiver::HandlePLI(RTCPUtility::RTCPParserV2& rtcpParser, 1005 RTCPPacketInformation& rtcpPacketInformation) { 1006 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1007 if (main_ssrc_ == rtcpPacket.PLI.MediaSSRC) { 1008 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI"); 1009 1010 ++packet_type_counter_.pli_packets; 1011 // Received a signal that we need to send a new key frame. 1012 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpPli; 1013 } 1014 rtcpParser.Iterate(); 1015 } 1016 1017 void RTCPReceiver::HandleTMMBR(RTCPUtility::RTCPParserV2& rtcpParser, 1018 RTCPPacketInformation& rtcpPacketInformation) { 1019 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1020 1021 uint32_t senderSSRC = rtcpPacket.TMMBR.SenderSSRC; 1022 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation(senderSSRC); 1023 if (ptrReceiveInfo == NULL) { 1024 // This remote SSRC must be saved before. 1025 rtcpParser.Iterate(); 1026 return; 1027 } 1028 if (rtcpPacket.TMMBR.MediaSSRC) { 1029 // rtcpPacket.TMMBR.MediaSSRC SHOULD be 0 if same as SenderSSRC 1030 // in relay mode this is a valid number 1031 senderSSRC = rtcpPacket.TMMBR.MediaSSRC; 1032 } 1033 1034 // Use packet length to calc max number of TMMBR blocks 1035 // each TMMBR block is 8 bytes 1036 ptrdiff_t maxNumOfTMMBRBlocks = rtcpParser.LengthLeft() / 8; 1037 1038 // sanity, we can't have more than what's in one packet 1039 if (maxNumOfTMMBRBlocks > 200) { 1040 assert(false); 1041 rtcpParser.Iterate(); 1042 return; 1043 } 1044 ptrReceiveInfo->VerifyAndAllocateTMMBRSet((uint32_t)maxNumOfTMMBRBlocks); 1045 1046 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1047 while (pktType == RTCPPacketTypes::kRtpfbTmmbrItem) { 1048 HandleTMMBRItem(*ptrReceiveInfo, rtcpPacket, rtcpPacketInformation, senderSSRC); 1049 pktType = rtcpParser.Iterate(); 1050 } 1051 } 1052 1053 void RTCPReceiver::HandleTMMBRItem(RTCPReceiveInformation& receiveInfo, 1054 const RTCPUtility::RTCPPacket& rtcpPacket, 1055 RTCPPacketInformation& rtcpPacketInformation, 1056 uint32_t senderSSRC) { 1057 if (main_ssrc_ == rtcpPacket.TMMBRItem.SSRC && 1058 rtcpPacket.TMMBRItem.MaxTotalMediaBitRate > 0) { 1059 receiveInfo.InsertTMMBRItem(senderSSRC, rtcpPacket.TMMBRItem, 1060 _clock->TimeInMilliseconds()); 1061 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbr; 1062 } 1063 } 1064 1065 void RTCPReceiver::HandleTMMBN(RTCPUtility::RTCPParserV2& rtcpParser, 1066 RTCPPacketInformation& rtcpPacketInformation) { 1067 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1068 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation( 1069 rtcpPacket.TMMBN.SenderSSRC); 1070 if (ptrReceiveInfo == NULL) { 1071 // This remote SSRC must be saved before. 1072 rtcpParser.Iterate(); 1073 return; 1074 } 1075 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbn; 1076 // Use packet length to calc max number of TMMBN blocks 1077 // each TMMBN block is 8 bytes 1078 ptrdiff_t maxNumOfTMMBNBlocks = rtcpParser.LengthLeft() / 8; 1079 1080 // sanity, we cant have more than what's in one packet 1081 if (maxNumOfTMMBNBlocks > 200) { 1082 assert(false); 1083 rtcpParser.Iterate(); 1084 return; 1085 } 1086 1087 ptrReceiveInfo->VerifyAndAllocateBoundingSet((uint32_t)maxNumOfTMMBNBlocks); 1088 1089 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1090 while (pktType == RTCPPacketTypes::kRtpfbTmmbnItem) { 1091 HandleTMMBNItem(*ptrReceiveInfo, rtcpPacket); 1092 pktType = rtcpParser.Iterate(); 1093 } 1094 } 1095 1096 void RTCPReceiver::HandleSR_REQ(RTCPUtility::RTCPParserV2& rtcpParser, 1097 RTCPPacketInformation& rtcpPacketInformation) { 1098 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSrReq; 1099 rtcpParser.Iterate(); 1100 } 1101 1102 void RTCPReceiver::HandleTMMBNItem(RTCPReceiveInformation& receiveInfo, 1103 const RTCPUtility::RTCPPacket& rtcpPacket) { 1104 receiveInfo.TmmbnBoundingSet.AddEntry( 1105 rtcpPacket.TMMBNItem.MaxTotalMediaBitRate, 1106 rtcpPacket.TMMBNItem.MeasuredOverhead, 1107 rtcpPacket.TMMBNItem.SSRC); 1108 } 1109 1110 void RTCPReceiver::HandleSLI(RTCPUtility::RTCPParserV2& rtcpParser, 1111 RTCPPacketInformation& rtcpPacketInformation) { 1112 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1113 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1114 while (pktType == RTCPPacketTypes::kPsfbSliItem) { 1115 HandleSLIItem(rtcpPacket, rtcpPacketInformation); 1116 pktType = rtcpParser.Iterate(); 1117 } 1118 } 1119 1120 void RTCPReceiver::HandleSLIItem(const RTCPUtility::RTCPPacket& rtcpPacket, 1121 RTCPPacketInformation& rtcpPacketInformation) { 1122 // in theory there could be multiple slices lost 1123 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSli; // received signal that we need to refresh a slice 1124 rtcpPacketInformation.sliPictureId = rtcpPacket.SLIItem.PictureId; 1125 } 1126 1127 void 1128 RTCPReceiver::HandleRPSI(RTCPUtility::RTCPParserV2& rtcpParser, 1129 RTCPHelp::RTCPPacketInformation& rtcpPacketInformation) 1130 { 1131 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1132 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1133 if (pktType == RTCPPacketTypes::kPsfbRpsi) { 1134 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRpsi; // received signal that we have a confirmed reference picture 1135 if(rtcpPacket.RPSI.NumberOfValidBits%8 != 0) 1136 { 1137 // to us unknown 1138 // continue 1139 rtcpParser.Iterate(); 1140 return; 1141 } 1142 rtcpPacketInformation.rpsiPictureId = 0; 1143 1144 // convert NativeBitString to rpsiPictureId 1145 uint8_t numberOfBytes = rtcpPacket.RPSI.NumberOfValidBits /8; 1146 for(uint8_t n = 0; n < (numberOfBytes-1); n++) 1147 { 1148 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[n] & 0x7f); 1149 rtcpPacketInformation.rpsiPictureId <<= 7; // prepare next 1150 } 1151 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[numberOfBytes-1] & 0x7f); 1152 } 1153 } 1154 1155 void RTCPReceiver::HandlePsfbApp(RTCPUtility::RTCPParserV2& rtcpParser, 1156 RTCPPacketInformation& rtcpPacketInformation) { 1157 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1158 if (pktType == RTCPPacketTypes::kPsfbRemb) { 1159 pktType = rtcpParser.Iterate(); 1160 if (pktType == RTCPPacketTypes::kPsfbRembItem) { 1161 HandleREMBItem(rtcpParser, rtcpPacketInformation); 1162 rtcpParser.Iterate(); 1163 } 1164 } 1165 } 1166 1167 void RTCPReceiver::HandleIJ(RTCPUtility::RTCPParserV2& rtcpParser, 1168 RTCPPacketInformation& rtcpPacketInformation) { 1169 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1170 1171 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1172 while (pktType == RTCPPacketTypes::kExtendedIjItem) { 1173 HandleIJItem(rtcpPacket, rtcpPacketInformation); 1174 pktType = rtcpParser.Iterate(); 1175 } 1176 } 1177 1178 void RTCPReceiver::HandleIJItem(const RTCPUtility::RTCPPacket& rtcpPacket, 1179 RTCPPacketInformation& rtcpPacketInformation) { 1180 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTransmissionTimeOffset; 1181 rtcpPacketInformation.interArrivalJitter = 1182 rtcpPacket.ExtendedJitterReportItem.Jitter; 1183 } 1184 1185 void RTCPReceiver::HandleREMBItem( 1186 RTCPUtility::RTCPParserV2& rtcpParser, 1187 RTCPPacketInformation& rtcpPacketInformation) { 1188 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1189 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRemb; 1190 rtcpPacketInformation.receiverEstimatedMaxBitrate = 1191 rtcpPacket.REMBItem.BitRate; 1192 } 1193 1194 void RTCPReceiver::HandleFIR(RTCPUtility::RTCPParserV2& rtcpParser, 1195 RTCPPacketInformation& rtcpPacketInformation) { 1196 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1197 RTCPReceiveInformation* ptrReceiveInfo = 1198 GetReceiveInformation(rtcpPacket.FIR.SenderSSRC); 1199 1200 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate(); 1201 while (pktType == RTCPPacketTypes::kPsfbFirItem) { 1202 HandleFIRItem(ptrReceiveInfo, rtcpPacket, rtcpPacketInformation); 1203 pktType = rtcpParser.Iterate(); 1204 } 1205 } 1206 1207 void RTCPReceiver::HandleFIRItem(RTCPReceiveInformation* receiveInfo, 1208 const RTCPUtility::RTCPPacket& rtcpPacket, 1209 RTCPPacketInformation& rtcpPacketInformation) { 1210 // Is it our sender that is requested to generate a new keyframe 1211 if (main_ssrc_ != rtcpPacket.FIRItem.SSRC) { 1212 return; 1213 } 1214 1215 ++packet_type_counter_.fir_packets; 1216 1217 // rtcpPacket.FIR.MediaSSRC SHOULD be 0 but we ignore to check it 1218 // we don't know who this originate from 1219 if (receiveInfo) { 1220 // check if we have reported this FIRSequenceNumber before 1221 if (rtcpPacket.FIRItem.CommandSequenceNumber != 1222 receiveInfo->lastFIRSequenceNumber) { 1223 int64_t now = _clock->TimeInMilliseconds(); 1224 // sanity; don't go crazy with the callbacks 1225 if ((now - receiveInfo->lastFIRRequest) > RTCP_MIN_FRAME_LENGTH_MS) { 1226 receiveInfo->lastFIRRequest = now; 1227 receiveInfo->lastFIRSequenceNumber = 1228 rtcpPacket.FIRItem.CommandSequenceNumber; 1229 // received signal that we need to send a new key frame 1230 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir; 1231 } 1232 } 1233 } else { 1234 // received signal that we need to send a new key frame 1235 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir; 1236 } 1237 } 1238 1239 void RTCPReceiver::HandleAPP(RTCPUtility::RTCPParserV2& rtcpParser, 1240 RTCPPacketInformation& rtcpPacketInformation) { 1241 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1242 1243 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpApp; 1244 rtcpPacketInformation.applicationSubType = rtcpPacket.APP.SubType; 1245 rtcpPacketInformation.applicationName = rtcpPacket.APP.Name; 1246 1247 rtcpParser.Iterate(); 1248 } 1249 1250 void RTCPReceiver::HandleAPPItem(RTCPUtility::RTCPParserV2& rtcpParser, 1251 RTCPPacketInformation& rtcpPacketInformation) { 1252 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet(); 1253 1254 rtcpPacketInformation.AddApplicationData(rtcpPacket.APP.Data, rtcpPacket.APP.Size); 1255 1256 rtcpParser.Iterate(); 1257 } 1258 1259 void RTCPReceiver::HandleTransportFeedback( 1260 RTCPUtility::RTCPParserV2* rtcp_parser, 1261 RTCPHelp::RTCPPacketInformation* rtcp_packet_information) { 1262 rtcp::RtcpPacket* packet = rtcp_parser->ReleaseRtcpPacket(); 1263 RTC_DCHECK(packet != nullptr); 1264 rtcp_packet_information->rtcpPacketTypeFlags |= kRtcpTransportFeedback; 1265 rtcp_packet_information->transport_feedback_.reset( 1266 static_cast<rtcp::TransportFeedback*>(packet)); 1267 1268 rtcp_parser->Iterate(); 1269 } 1270 int32_t RTCPReceiver::UpdateTMMBR() { 1271 int32_t numBoundingSet = 0; 1272 uint32_t bitrate = 0; 1273 uint32_t accNumCandidates = 0; 1274 1275 int32_t size = TMMBRReceived(0, 0, NULL); 1276 if (size > 0) { 1277 TMMBRSet* candidateSet = VerifyAndAllocateCandidateSet(size); 1278 // Get candidate set from receiver. 1279 accNumCandidates = TMMBRReceived(size, accNumCandidates, candidateSet); 1280 } else { 1281 // Candidate set empty. 1282 VerifyAndAllocateCandidateSet(0); // resets candidate set 1283 } 1284 // Find bounding set 1285 TMMBRSet* boundingSet = NULL; 1286 numBoundingSet = FindTMMBRBoundingSet(boundingSet); 1287 if (numBoundingSet == -1) { 1288 LOG(LS_WARNING) << "Failed to find TMMBR bounding set."; 1289 return -1; 1290 } 1291 // Set bounding set 1292 // Inform remote clients about the new bandwidth 1293 // inform the remote client 1294 _rtpRtcp.SetTMMBN(boundingSet); 1295 1296 // might trigger a TMMBN 1297 if (numBoundingSet == 0) { 1298 // owner of max bitrate request has timed out 1299 // empty bounding set has been sent 1300 return 0; 1301 } 1302 // Get net bitrate from bounding set depending on sent packet rate 1303 if (CalcMinBitRate(&bitrate)) { 1304 // we have a new bandwidth estimate on this channel 1305 if (_cbRtcpBandwidthObserver) { 1306 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate(bitrate * 1000); 1307 } 1308 } 1309 return 0; 1310 } 1311 1312 void RTCPReceiver::RegisterRtcpStatisticsCallback( 1313 RtcpStatisticsCallback* callback) { 1314 CriticalSectionScoped cs(_criticalSectionFeedbacks); 1315 stats_callback_ = callback; 1316 } 1317 1318 RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() { 1319 CriticalSectionScoped cs(_criticalSectionFeedbacks); 1320 return stats_callback_; 1321 } 1322 1323 // Holding no Critical section 1324 void RTCPReceiver::TriggerCallbacksFromRTCPPacket( 1325 RTCPPacketInformation& rtcpPacketInformation) { 1326 // Process TMMBR and REMB first to avoid multiple callbacks 1327 // to OnNetworkChanged. 1328 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpTmmbr) { 1329 // Might trigger a OnReceivedBandwidthEstimateUpdate. 1330 UpdateTMMBR(); 1331 } 1332 uint32_t local_ssrc; 1333 std::set<uint32_t> registered_ssrcs; 1334 { 1335 // We don't want to hold this critsect when triggering the callbacks below. 1336 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 1337 local_ssrc = main_ssrc_; 1338 registered_ssrcs = registered_ssrcs_; 1339 } 1340 if (!receiver_only_ && 1341 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSrReq)) { 1342 _rtpRtcp.OnRequestSendReport(); 1343 } 1344 if (!receiver_only_ && 1345 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpNack)) { 1346 if (rtcpPacketInformation.nackSequenceNumbers.size() > 0) { 1347 LOG(LS_VERBOSE) << "Incoming NACK length: " 1348 << rtcpPacketInformation.nackSequenceNumbers.size(); 1349 _rtpRtcp.OnReceivedNACK(rtcpPacketInformation.nackSequenceNumbers); 1350 } 1351 } 1352 { 1353 // We need feedback that we have received a report block(s) so that we 1354 // can generate a new packet in a conference relay scenario, one received 1355 // report can generate several RTCP packets, based on number relayed/mixed 1356 // a send report block should go out to all receivers. 1357 if (_cbRtcpIntraFrameObserver) { 1358 RTC_DCHECK(!receiver_only_); 1359 if ((rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) || 1360 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpFir)) { 1361 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) { 1362 LOG(LS_VERBOSE) << "Incoming PLI from SSRC " 1363 << rtcpPacketInformation.remoteSSRC; 1364 } else { 1365 LOG(LS_VERBOSE) << "Incoming FIR from SSRC " 1366 << rtcpPacketInformation.remoteSSRC; 1367 } 1368 _cbRtcpIntraFrameObserver->OnReceivedIntraFrameRequest(local_ssrc); 1369 } 1370 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSli) { 1371 _cbRtcpIntraFrameObserver->OnReceivedSLI( 1372 local_ssrc, rtcpPacketInformation.sliPictureId); 1373 } 1374 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRpsi) { 1375 _cbRtcpIntraFrameObserver->OnReceivedRPSI( 1376 local_ssrc, rtcpPacketInformation.rpsiPictureId); 1377 } 1378 } 1379 if (_cbRtcpBandwidthObserver) { 1380 RTC_DCHECK(!receiver_only_); 1381 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRemb) { 1382 LOG(LS_VERBOSE) << "Incoming REMB: " 1383 << rtcpPacketInformation.receiverEstimatedMaxBitrate; 1384 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate( 1385 rtcpPacketInformation.receiverEstimatedMaxBitrate); 1386 } 1387 if ((rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr) || 1388 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRr)) { 1389 int64_t now = _clock->TimeInMilliseconds(); 1390 _cbRtcpBandwidthObserver->OnReceivedRtcpReceiverReport( 1391 rtcpPacketInformation.report_blocks, 1392 rtcpPacketInformation.rtt, 1393 now); 1394 } 1395 } 1396 if (_cbTransportFeedbackObserver && 1397 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpTransportFeedback)) { 1398 uint32_t media_source_ssrc = 1399 rtcpPacketInformation.transport_feedback_->GetMediaSourceSsrc(); 1400 if (media_source_ssrc == local_ssrc || 1401 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) { 1402 _cbTransportFeedbackObserver->OnTransportFeedback( 1403 *rtcpPacketInformation.transport_feedback_.get()); 1404 } 1405 } 1406 } 1407 1408 if (!receiver_only_) { 1409 CriticalSectionScoped cs(_criticalSectionFeedbacks); 1410 if (stats_callback_) { 1411 for (ReportBlockList::const_iterator it = 1412 rtcpPacketInformation.report_blocks.begin(); 1413 it != rtcpPacketInformation.report_blocks.end(); 1414 ++it) { 1415 RtcpStatistics stats; 1416 stats.cumulative_lost = it->cumulativeLost; 1417 stats.extended_max_sequence_number = it->extendedHighSeqNum; 1418 stats.fraction_lost = it->fractionLost; 1419 stats.jitter = it->jitter; 1420 1421 stats_callback_->StatisticsUpdated(stats, it->sourceSSRC); 1422 } 1423 } 1424 } 1425 } 1426 1427 int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC, 1428 char cName[RTCP_CNAME_SIZE]) const { 1429 assert(cName); 1430 1431 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 1432 RTCPCnameInformation* cnameInfo = GetCnameInformation(remoteSSRC); 1433 if (cnameInfo == NULL) { 1434 return -1; 1435 } 1436 cName[RTCP_CNAME_SIZE - 1] = 0; 1437 strncpy(cName, cnameInfo->name, RTCP_CNAME_SIZE - 1); 1438 return 0; 1439 } 1440 1441 // no callbacks allowed inside this function 1442 int32_t RTCPReceiver::TMMBRReceived(uint32_t size, 1443 uint32_t accNumCandidates, 1444 TMMBRSet* candidateSet) const { 1445 CriticalSectionScoped lock(_criticalSectionRTCPReceiver); 1446 1447 std::map<uint32_t, RTCPReceiveInformation*>::const_iterator 1448 receiveInfoIt = _receivedInfoMap.begin(); 1449 if (receiveInfoIt == _receivedInfoMap.end()) { 1450 return -1; 1451 } 1452 uint32_t num = accNumCandidates; 1453 if (candidateSet) { 1454 while( num < size && receiveInfoIt != _receivedInfoMap.end()) { 1455 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second; 1456 if (receiveInfo == NULL) { 1457 return 0; 1458 } 1459 for (uint32_t i = 0; 1460 (num < size) && (i < receiveInfo->TmmbrSet.lengthOfSet()); i++) { 1461 if (receiveInfo->GetTMMBRSet(i, num, candidateSet, 1462 _clock->TimeInMilliseconds()) == 0) { 1463 num++; 1464 } 1465 } 1466 receiveInfoIt++; 1467 } 1468 } else { 1469 while (receiveInfoIt != _receivedInfoMap.end()) { 1470 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second; 1471 if(receiveInfo == NULL) { 1472 return -1; 1473 } 1474 num += receiveInfo->TmmbrSet.lengthOfSet(); 1475 receiveInfoIt++; 1476 } 1477 } 1478 return num; 1479 } 1480 1481 } // namespace webrtc 1482