2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_receiver.h"
13 #include <assert.h> //assert
14 #include <string.h> //memset
18 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
19 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
20 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
21 #include "webrtc/system_wrappers/interface/logging.h"
22 #include "webrtc/system_wrappers/interface/trace_event.h"
25 using namespace RTCPUtility;
26 using namespace RTCPHelp;
28 // The number of RTCP time intervals needed to trigger a timeout.
29 const int kRrTimeoutIntervals = 3;
31 RTCPReceiver::RTCPReceiver(const int32_t id, Clock* clock,
32 ModuleRtpRtcpImpl* owner)
39 _criticalSectionFeedbacks(
40 CriticalSectionWrapper::CreateCriticalSection()),
41 _cbRtcpFeedback(NULL),
42 _cbRtcpBandwidthObserver(NULL),
43 _cbRtcpIntraFrameObserver(NULL),
44 _criticalSectionRTCPReceiver(
45 CriticalSectionWrapper::CreateCriticalSection()),
49 _lastReceivedSRNTPsecs(0),
50 _lastReceivedSRNTPfrac(0),
51 _lastReceivedXRNTPsecs(0),
52 _lastReceivedXRNTPfrac(0),
57 _lastIncreasedSequenceNumberMs(0),
58 stats_callback_(NULL) {
59 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo));
62 RTCPReceiver::~RTCPReceiver() {
63 delete _criticalSectionRTCPReceiver;
64 delete _criticalSectionFeedbacks;
66 while (!_receivedReportBlockMap.empty()) {
67 std::map<uint32_t, RTCPReportBlockInformation*>::iterator first =
68 _receivedReportBlockMap.begin();
70 _receivedReportBlockMap.erase(first);
72 while (!_receivedInfoMap.empty()) {
73 std::map<uint32_t, RTCPReceiveInformation*>::iterator first =
74 _receivedInfoMap.begin();
76 _receivedInfoMap.erase(first);
78 while (!_receivedCnameMap.empty()) {
79 std::map<uint32_t, RTCPCnameInformation*>::iterator first =
80 _receivedCnameMap.begin();
82 _receivedCnameMap.erase(first);
87 RTCPReceiver::ChangeUniqueId(const int32_t id)
93 RTCPReceiver::Status() const
95 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
100 RTCPReceiver::SetRTCPStatus(const RTCPMethod method)
102 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
108 RTCPReceiver::LastReceived()
110 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
111 return _lastReceived;
115 RTCPReceiver::LastReceivedReceiverReport() const {
116 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
117 int64_t last_received_rr = -1;
118 for (ReceivedInfoMap::const_iterator it = _receivedInfoMap.begin();
119 it != _receivedInfoMap.end(); ++it) {
120 if (it->second->lastTimeReceived > last_received_rr) {
121 last_received_rr = it->second->lastTimeReceived;
124 return last_received_rr;
128 RTCPReceiver::SetRemoteSSRC( const uint32_t ssrc)
130 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
132 // new SSRC reset old reports
133 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo));
134 _lastReceivedSRNTPsecs = 0;
135 _lastReceivedSRNTPfrac = 0;
141 uint32_t RTCPReceiver::RemoteSSRC() const {
142 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
146 void RTCPReceiver::RegisterRtcpObservers(
147 RtcpIntraFrameObserver* intra_frame_callback,
148 RtcpBandwidthObserver* bandwidth_callback,
149 RtcpFeedback* feedback_callback) {
150 CriticalSectionScoped lock(_criticalSectionFeedbacks);
151 _cbRtcpIntraFrameObserver = intra_frame_callback;
152 _cbRtcpBandwidthObserver = bandwidth_callback;
153 _cbRtcpFeedback = feedback_callback;
156 void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
157 const std::set<uint32_t>& registered_ssrcs) {
158 uint32_t old_ssrc = 0;
160 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
161 old_ssrc = main_ssrc_;
162 main_ssrc_ = main_ssrc;
163 registered_ssrcs_ = registered_ssrcs;
166 CriticalSectionScoped lock(_criticalSectionFeedbacks);
167 if (_cbRtcpIntraFrameObserver && old_ssrc != main_ssrc) {
168 _cbRtcpIntraFrameObserver->OnLocalSsrcChanged(old_ssrc, main_ssrc);
173 int32_t RTCPReceiver::ResetRTT(const uint32_t remoteSSRC) {
174 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
175 RTCPReportBlockInformation* reportBlock =
176 GetReportBlockInformation(remoteSSRC);
177 if (reportBlock == NULL) {
178 LOG(LS_WARNING) << "Failed to reset rtt for ssrc " << remoteSSRC;
181 reportBlock->RTT = 0;
182 reportBlock->avgRTT = 0;
183 reportBlock->minRTT = 0;
184 reportBlock->maxRTT = 0;
188 int32_t RTCPReceiver::RTT(uint32_t remoteSSRC,
192 uint16_t* maxRTT) const {
193 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
195 RTCPReportBlockInformation* reportBlock =
196 GetReportBlockInformation(remoteSSRC);
198 if (reportBlock == NULL) {
202 *RTT = reportBlock->RTT;
205 *avgRTT = reportBlock->avgRTT;
208 *minRTT = reportBlock->minRTT;
211 *maxRTT = reportBlock->maxRTT;
216 bool RTCPReceiver::GetAndResetXrRrRtt(uint16_t* rtt_ms) {
218 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
219 if (xr_rr_rtt_ms_ == 0) {
222 *rtt_ms = xr_rr_rtt_ms_;
227 // TODO(pbos): Make this fail when we haven't received NTP.
228 bool RTCPReceiver::NTP(uint32_t* ReceivedNTPsecs,
229 uint32_t* ReceivedNTPfrac,
230 uint32_t* RTCPArrivalTimeSecs,
231 uint32_t* RTCPArrivalTimeFrac,
232 uint32_t* rtcp_timestamp) const
234 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
237 *ReceivedNTPsecs = _remoteSenderInfo.NTPseconds; // NTP from incoming SendReport
241 *ReceivedNTPfrac = _remoteSenderInfo.NTPfraction;
243 if(RTCPArrivalTimeFrac)
245 *RTCPArrivalTimeFrac = _lastReceivedSRNTPfrac; // local NTP time when we received a RTCP packet with a send block
247 if(RTCPArrivalTimeSecs)
249 *RTCPArrivalTimeSecs = _lastReceivedSRNTPsecs;
251 if (rtcp_timestamp) {
252 *rtcp_timestamp = _remoteSenderInfo.RTPtimeStamp;
257 bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
258 RtcpReceiveTimeInfo* info) const {
260 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
261 if (_lastReceivedXRNTPsecs == 0 && _lastReceivedXRNTPfrac == 0) {
265 info->sourceSSRC = _remoteXRReceiveTimeInfo.sourceSSRC;
266 info->lastRR = _remoteXRReceiveTimeInfo.lastRR;
268 // Get the delay since last received report (RFC 3611).
269 uint32_t receive_time = RTCPUtility::MidNtp(_lastReceivedXRNTPsecs,
270 _lastReceivedXRNTPfrac);
272 uint32_t ntp_sec = 0;
273 uint32_t ntp_frac = 0;
274 _clock->CurrentNtp(ntp_sec, ntp_frac);
275 uint32_t now = RTCPUtility::MidNtp(ntp_sec, ntp_frac);
277 info->delaySinceLastRR = now - receive_time;
281 int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* senderInfo) const {
283 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
284 if (_lastReceivedSRNTPsecs == 0) {
287 memcpy(senderInfo, &(_remoteSenderInfo), sizeof(RTCPSenderInfo));
292 // we can get multiple receive reports when we receive the report from a CE
293 int32_t RTCPReceiver::StatisticsReceived(
294 std::vector<RTCPReportBlock>* receiveBlocks) const {
295 assert(receiveBlocks);
296 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
298 std::map<uint32_t, RTCPReportBlockInformation*>::const_iterator it =
299 _receivedReportBlockMap.begin();
301 while (it != _receivedReportBlockMap.end()) {
302 receiveBlocks->push_back(it->second->remoteReceiveBlock);
308 void RTCPReceiver::GetPacketTypeCounter(
309 RtcpPacketTypeCounter* packet_counter) const {
310 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
311 *packet_counter = packet_type_counter_;
315 RTCPReceiver::IncomingRTCPPacket(RTCPPacketInformation& rtcpPacketInformation,
316 RTCPUtility::RTCPParserV2* rtcpParser)
318 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
320 _lastReceived = _clock->TimeInMilliseconds();
322 RTCPUtility::RTCPPacketTypes pktType = rtcpParser->Begin();
323 while (pktType != RTCPUtility::kRtcpNotValidCode)
325 // Each "case" is responsible for iterate the parser to the
326 // next top level packet.
329 case RTCPUtility::kRtcpSrCode:
330 case RTCPUtility::kRtcpRrCode:
331 HandleSenderReceiverReport(*rtcpParser, rtcpPacketInformation);
333 case RTCPUtility::kRtcpSdesCode:
334 HandleSDES(*rtcpParser);
336 case RTCPUtility::kRtcpXrHeaderCode:
337 HandleXrHeader(*rtcpParser, rtcpPacketInformation);
339 case RTCPUtility::kRtcpXrReceiverReferenceTimeCode:
340 HandleXrReceiveReferenceTime(*rtcpParser, rtcpPacketInformation);
342 case RTCPUtility::kRtcpXrDlrrReportBlockCode:
343 HandleXrDlrrReportBlock(*rtcpParser, rtcpPacketInformation);
345 case RTCPUtility::kRtcpXrVoipMetricCode:
346 HandleXRVOIPMetric(*rtcpParser, rtcpPacketInformation);
348 case RTCPUtility::kRtcpByeCode:
349 HandleBYE(*rtcpParser);
351 case RTCPUtility::kRtcpRtpfbNackCode:
352 HandleNACK(*rtcpParser, rtcpPacketInformation);
354 case RTCPUtility::kRtcpRtpfbTmmbrCode:
355 HandleTMMBR(*rtcpParser, rtcpPacketInformation);
357 case RTCPUtility::kRtcpRtpfbTmmbnCode:
358 HandleTMMBN(*rtcpParser, rtcpPacketInformation);
360 case RTCPUtility::kRtcpRtpfbSrReqCode:
361 HandleSR_REQ(*rtcpParser, rtcpPacketInformation);
363 case RTCPUtility::kRtcpPsfbPliCode:
364 HandlePLI(*rtcpParser, rtcpPacketInformation);
366 case RTCPUtility::kRtcpPsfbSliCode:
367 HandleSLI(*rtcpParser, rtcpPacketInformation);
369 case RTCPUtility::kRtcpPsfbRpsiCode:
370 HandleRPSI(*rtcpParser, rtcpPacketInformation);
372 case RTCPUtility::kRtcpExtendedIjCode:
373 HandleIJ(*rtcpParser, rtcpPacketInformation);
375 case RTCPUtility::kRtcpPsfbFirCode:
376 HandleFIR(*rtcpParser, rtcpPacketInformation);
378 case RTCPUtility::kRtcpPsfbAppCode:
379 HandlePsfbApp(*rtcpParser, rtcpPacketInformation);
381 case RTCPUtility::kRtcpAppCode:
382 // generic application messages
383 HandleAPP(*rtcpParser, rtcpPacketInformation);
385 case RTCPUtility::kRtcpAppItemCode:
386 // generic application messages
387 HandleAPPItem(*rtcpParser, rtcpPacketInformation);
390 rtcpParser->Iterate();
393 pktType = rtcpParser->PacketType();
398 // no need for critsect we have _criticalSectionRTCPReceiver
400 RTCPReceiver::HandleSenderReceiverReport(RTCPUtility::RTCPParserV2& rtcpParser,
401 RTCPPacketInformation& rtcpPacketInformation)
403 RTCPUtility::RTCPPacketTypes rtcpPacketType = rtcpParser.PacketType();
404 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
406 assert((rtcpPacketType == RTCPUtility::kRtcpRrCode) || (rtcpPacketType == RTCPUtility::kRtcpSrCode));
409 // The synchronization source identifier for the originator of this SR packet
411 // rtcpPacket.RR.SenderSSRC
412 // The source of the packet sender, same as of SR? or is this a CE?
414 const uint32_t remoteSSRC = (rtcpPacketType == RTCPUtility::kRtcpRrCode) ? rtcpPacket.RR.SenderSSRC:rtcpPacket.SR.SenderSSRC;
415 const uint8_t numberOfReportBlocks = (rtcpPacketType == RTCPUtility::kRtcpRrCode) ? rtcpPacket.RR.NumberOfReportBlocks:rtcpPacket.SR.NumberOfReportBlocks;
417 rtcpPacketInformation.remoteSSRC = remoteSSRC;
419 RTCPReceiveInformation* ptrReceiveInfo = CreateReceiveInformation(remoteSSRC);
422 rtcpParser.Iterate();
426 if (rtcpPacketType == RTCPUtility::kRtcpSrCode)
428 TRACE_EVENT_INSTANT2("webrtc_rtp", "SR",
429 "remote_ssrc", remoteSSRC,
432 if (_remoteSSRC == remoteSSRC) // have I received RTP packets from this party
434 // only signal that we have received a SR when we accept one
435 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSr;
437 rtcpPacketInformation.ntp_secs = rtcpPacket.SR.NTPMostSignificant;
438 rtcpPacketInformation.ntp_frac = rtcpPacket.SR.NTPLeastSignificant;
439 rtcpPacketInformation.rtp_timestamp = rtcpPacket.SR.RTPTimestamp;
441 // We will only store the send report from one source, but
442 // we will store all the receive block
444 // Save the NTP time of this report
445 _remoteSenderInfo.NTPseconds = rtcpPacket.SR.NTPMostSignificant;
446 _remoteSenderInfo.NTPfraction = rtcpPacket.SR.NTPLeastSignificant;
447 _remoteSenderInfo.RTPtimeStamp = rtcpPacket.SR.RTPTimestamp;
448 _remoteSenderInfo.sendPacketCount = rtcpPacket.SR.SenderPacketCount;
449 _remoteSenderInfo.sendOctetCount = rtcpPacket.SR.SenderOctetCount;
451 _clock->CurrentNtp(_lastReceivedSRNTPsecs, _lastReceivedSRNTPfrac);
455 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr;
459 TRACE_EVENT_INSTANT2("webrtc_rtp", "RR",
460 "remote_ssrc", remoteSSRC,
463 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr;
465 UpdateReceiveInformation(*ptrReceiveInfo);
467 rtcpPacketType = rtcpParser.Iterate();
469 while (rtcpPacketType == RTCPUtility::kRtcpReportBlockItemCode)
471 HandleReportBlock(rtcpPacket, rtcpPacketInformation, remoteSSRC, numberOfReportBlocks);
472 rtcpPacketType = rtcpParser.Iterate();
476 // no need for critsect we have _criticalSectionRTCPReceiver
477 void RTCPReceiver::HandleReportBlock(
478 const RTCPUtility::RTCPPacket& rtcpPacket,
479 RTCPPacketInformation& rtcpPacketInformation,
480 const uint32_t remoteSSRC,
481 const uint8_t numberOfReportBlocks)
482 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver) {
483 // This will be called once per report block in the RTCP packet.
484 // We filter out all report blocks that are not for us.
485 // Each packet has max 31 RR blocks.
487 // We can calc RTT if we send a send report and get a report block back.
489 // |rtcpPacket.ReportBlockItem.SSRC| is the SSRC identifier of the source to
490 // which the information in this reception report block pertains.
492 // Filter out all report blocks that are not for us.
493 if (registered_ssrcs_.find(rtcpPacket.ReportBlockItem.SSRC) ==
494 registered_ssrcs_.end()) {
495 // This block is not for us ignore it.
499 // To avoid problem with acquiring _criticalSectionRTCPSender while holding
500 // _criticalSectionRTCPReceiver.
501 _criticalSectionRTCPReceiver->Leave();
502 uint32_t sendTimeMS =
503 _rtpRtcp.SendTimeOfSendReport(rtcpPacket.ReportBlockItem.LastSR);
504 _criticalSectionRTCPReceiver->Enter();
506 RTCPReportBlockInformation* reportBlock =
507 CreateReportBlockInformation(remoteSSRC);
508 if (reportBlock == NULL) {
509 LOG(LS_WARNING) << "Failed to CreateReportBlockInformation("
510 << remoteSSRC << ")";
514 _lastReceivedRrMs = _clock->TimeInMilliseconds();
515 const RTCPPacketReportBlockItem& rb = rtcpPacket.ReportBlockItem;
516 reportBlock->remoteReceiveBlock.remoteSSRC = remoteSSRC;
517 reportBlock->remoteReceiveBlock.sourceSSRC = rb.SSRC;
518 reportBlock->remoteReceiveBlock.fractionLost = rb.FractionLost;
519 reportBlock->remoteReceiveBlock.cumulativeLost =
520 rb.CumulativeNumOfPacketsLost;
521 if (rb.ExtendedHighestSequenceNumber >
522 reportBlock->remoteReceiveBlock.extendedHighSeqNum) {
523 // We have successfully delivered new RTP packets to the remote side after
524 // the last RR was sent from the remote side.
525 _lastIncreasedSequenceNumberMs = _lastReceivedRrMs;
527 reportBlock->remoteReceiveBlock.extendedHighSeqNum =
528 rb.ExtendedHighestSequenceNumber;
529 reportBlock->remoteReceiveBlock.jitter = rb.Jitter;
530 reportBlock->remoteReceiveBlock.delaySinceLastSR = rb.DelayLastSR;
531 reportBlock->remoteReceiveBlock.lastSR = rb.LastSR;
533 if (rtcpPacket.ReportBlockItem.Jitter > reportBlock->remoteMaxJitter) {
534 reportBlock->remoteMaxJitter = rtcpPacket.ReportBlockItem.Jitter;
537 uint32_t delaySinceLastSendReport =
538 rtcpPacket.ReportBlockItem.DelayLastSR;
540 // local NTP time when we received this
541 uint32_t lastReceivedRRNTPsecs = 0;
542 uint32_t lastReceivedRRNTPfrac = 0;
544 _clock->CurrentNtp(lastReceivedRRNTPsecs, lastReceivedRRNTPfrac);
546 // time when we received this in MS
547 uint32_t receiveTimeMS = Clock::NtpToMs(lastReceivedRRNTPsecs,
548 lastReceivedRRNTPfrac);
551 uint32_t d = (delaySinceLastSendReport & 0x0000ffff) * 1000;
553 d += ((delaySinceLastSendReport & 0xffff0000) >> 16) * 1000;
557 if (sendTimeMS > 0) {
558 RTT = receiveTimeMS - d - sendTimeMS;
562 if (RTT > reportBlock->maxRTT) {
564 reportBlock->maxRTT = (uint16_t) RTT;
566 if (reportBlock->minRTT == 0) {
568 reportBlock->minRTT = (uint16_t) RTT;
569 } else if (RTT < reportBlock->minRTT) {
571 reportBlock->minRTT = (uint16_t) RTT;
574 reportBlock->RTT = (uint16_t) RTT;
577 if (reportBlock->numAverageCalcs != 0) {
578 float ac = static_cast<float> (reportBlock->numAverageCalcs);
579 float newAverage = ((ac / (ac + 1)) * reportBlock->avgRTT)
580 + ((1 / (ac + 1)) * RTT);
581 reportBlock->avgRTT = static_cast<int> (newAverage + 0.5f);
584 reportBlock->avgRTT = (uint16_t) RTT;
586 reportBlock->numAverageCalcs++;
589 TRACE_COUNTER_ID1("webrtc_rtp", "RR_RTT", rb.SSRC, RTT);
591 rtcpPacketInformation.AddReportInfo(*reportBlock);
594 RTCPReportBlockInformation*
595 RTCPReceiver::CreateReportBlockInformation(uint32_t remoteSSRC) {
596 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
598 std::map<uint32_t, RTCPReportBlockInformation*>::iterator it =
599 _receivedReportBlockMap.find(remoteSSRC);
601 RTCPReportBlockInformation* ptrReportBlockInfo = NULL;
602 if (it != _receivedReportBlockMap.end()) {
603 ptrReportBlockInfo = it->second;
605 ptrReportBlockInfo = new RTCPReportBlockInformation;
606 _receivedReportBlockMap[remoteSSRC] = ptrReportBlockInfo;
608 return ptrReportBlockInfo;
611 RTCPReportBlockInformation*
612 RTCPReceiver::GetReportBlockInformation(uint32_t remoteSSRC) const {
613 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
615 std::map<uint32_t, RTCPReportBlockInformation*>::const_iterator it =
616 _receivedReportBlockMap.find(remoteSSRC);
618 if (it == _receivedReportBlockMap.end()) {
624 RTCPCnameInformation*
625 RTCPReceiver::CreateCnameInformation(uint32_t remoteSSRC) {
626 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
628 std::map<uint32_t, RTCPCnameInformation*>::iterator it =
629 _receivedCnameMap.find(remoteSSRC);
631 if (it != _receivedCnameMap.end()) {
634 RTCPCnameInformation* cnameInfo = new RTCPCnameInformation;
635 memset(cnameInfo->name, 0, RTCP_CNAME_SIZE);
636 _receivedCnameMap[remoteSSRC] = cnameInfo;
640 RTCPCnameInformation*
641 RTCPReceiver::GetCnameInformation(uint32_t remoteSSRC) const {
642 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
644 std::map<uint32_t, RTCPCnameInformation*>::const_iterator it =
645 _receivedCnameMap.find(remoteSSRC);
647 if (it == _receivedCnameMap.end()) {
653 RTCPReceiveInformation*
654 RTCPReceiver::CreateReceiveInformation(uint32_t remoteSSRC) {
655 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
657 std::map<uint32_t, RTCPReceiveInformation*>::iterator it =
658 _receivedInfoMap.find(remoteSSRC);
660 if (it != _receivedInfoMap.end()) {
663 RTCPReceiveInformation* receiveInfo = new RTCPReceiveInformation;
664 _receivedInfoMap[remoteSSRC] = receiveInfo;
668 RTCPReceiveInformation*
669 RTCPReceiver::GetReceiveInformation(uint32_t remoteSSRC) {
670 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
672 std::map<uint32_t, RTCPReceiveInformation*>::iterator it =
673 _receivedInfoMap.find(remoteSSRC);
674 if (it == _receivedInfoMap.end()) {
680 void RTCPReceiver::UpdateReceiveInformation(
681 RTCPReceiveInformation& receiveInformation) {
682 // Update that this remote is alive
683 receiveInformation.lastTimeReceived = _clock->TimeInMilliseconds();
686 bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
687 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
688 if (_lastReceivedRrMs == 0)
691 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
692 if (_clock->TimeInMilliseconds() > _lastReceivedRrMs + time_out_ms) {
693 // Reset the timer to only trigger one log.
694 _lastReceivedRrMs = 0;
700 bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
701 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
702 if (_lastIncreasedSequenceNumberMs == 0)
705 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
706 if (_clock->TimeInMilliseconds() > _lastIncreasedSequenceNumberMs +
708 // Reset the timer to only trigger one log.
709 _lastIncreasedSequenceNumberMs = 0;
715 bool RTCPReceiver::UpdateRTCPReceiveInformationTimers() {
716 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
718 bool updateBoundingSet = false;
719 int64_t timeNow = _clock->TimeInMilliseconds();
721 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
722 _receivedInfoMap.begin();
724 while (receiveInfoIt != _receivedInfoMap.end()) {
725 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
726 if (receiveInfo == NULL) {
727 return updateBoundingSet;
729 // time since last received rtcp packet
730 // when we dont have a lastTimeReceived and the object is marked
731 // readyForDelete it's removed from the map
732 if (receiveInfo->lastTimeReceived) {
733 /// use audio define since we don't know what interval the remote peer is
735 if ((timeNow - receiveInfo->lastTimeReceived) >
736 5 * RTCP_INTERVAL_AUDIO_MS) {
737 // no rtcp packet for the last five regular intervals, reset limitations
738 receiveInfo->TmmbrSet.clearSet();
739 // prevent that we call this over and over again
740 receiveInfo->lastTimeReceived = 0;
741 // send new TMMBN to all channels using the default codec
742 updateBoundingSet = true;
745 } else if (receiveInfo->readyForDelete) {
746 // store our current receiveInfoItem
747 std::map<uint32_t, RTCPReceiveInformation*>::iterator
748 receiveInfoItemToBeErased = receiveInfoIt;
750 delete receiveInfoItemToBeErased->second;
751 _receivedInfoMap.erase(receiveInfoItemToBeErased);
756 return updateBoundingSet;
759 int32_t RTCPReceiver::BoundingSet(bool &tmmbrOwner, TMMBRSet* boundingSetRec) {
760 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
762 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
763 _receivedInfoMap.find(_remoteSSRC);
765 if (receiveInfoIt == _receivedInfoMap.end()) {
768 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
769 if (receiveInfo == NULL) {
772 if (receiveInfo->TmmbnBoundingSet.lengthOfSet() > 0) {
773 boundingSetRec->VerifyAndAllocateSet(
774 receiveInfo->TmmbnBoundingSet.lengthOfSet() + 1);
775 for(uint32_t i=0; i< receiveInfo->TmmbnBoundingSet.lengthOfSet();
777 if(receiveInfo->TmmbnBoundingSet.Ssrc(i) == main_ssrc_) {
778 // owner of bounding set
781 boundingSetRec->SetEntry(i,
782 receiveInfo->TmmbnBoundingSet.Tmmbr(i),
783 receiveInfo->TmmbnBoundingSet.PacketOH(i),
784 receiveInfo->TmmbnBoundingSet.Ssrc(i));
787 return receiveInfo->TmmbnBoundingSet.lengthOfSet();
790 // no need for critsect we have _criticalSectionRTCPReceiver
792 RTCPReceiver::HandleSDES(RTCPUtility::RTCPParserV2& rtcpParser)
794 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
795 while (pktType == RTCPUtility::kRtcpSdesChunkCode)
797 HandleSDESChunk(rtcpParser);
798 pktType = rtcpParser.Iterate();
802 // no need for critsect we have _criticalSectionRTCPReceiver
803 void RTCPReceiver::HandleSDESChunk(RTCPUtility::RTCPParserV2& rtcpParser) {
804 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
805 RTCPCnameInformation* cnameInfo =
806 CreateCnameInformation(rtcpPacket.CName.SenderSSRC);
809 cnameInfo->name[RTCP_CNAME_SIZE - 1] = 0;
810 strncpy(cnameInfo->name, rtcpPacket.CName.CName, RTCP_CNAME_SIZE - 1);
813 // no need for critsect we have _criticalSectionRTCPReceiver
815 RTCPReceiver::HandleNACK(RTCPUtility::RTCPParserV2& rtcpParser,
816 RTCPPacketInformation& rtcpPacketInformation)
818 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
819 if (main_ssrc_ != rtcpPacket.NACK.MediaSSRC)
822 rtcpParser.Iterate();
825 rtcpPacketInformation.ResetNACKPacketIdArray();
827 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
828 while (pktType == RTCPUtility::kRtcpRtpfbNackItemCode)
830 HandleNACKItem(rtcpPacket, rtcpPacketInformation);
831 pktType = rtcpParser.Iterate();
834 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpNack) {
835 ++packet_type_counter_.nack_packets;
839 // no need for critsect we have _criticalSectionRTCPReceiver
841 RTCPReceiver::HandleNACKItem(const RTCPUtility::RTCPPacket& rtcpPacket,
842 RTCPPacketInformation& rtcpPacketInformation)
844 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID);
846 uint16_t bitMask = rtcpPacket.NACKItem.BitMask;
849 for(int i=1; i <= 16; ++i)
853 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID + i);
855 bitMask = bitMask >>1;
859 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpNack;
862 // no need for critsect we have _criticalSectionRTCPReceiver
863 void RTCPReceiver::HandleBYE(RTCPUtility::RTCPParserV2& rtcpParser) {
864 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
867 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
868 std::map<uint32_t, RTCPReportBlockInformation*>::iterator
869 reportBlockInfoIt = _receivedReportBlockMap.find(
870 rtcpPacket.BYE.SenderSSRC);
872 if (reportBlockInfoIt != _receivedReportBlockMap.end()) {
873 delete reportBlockInfoIt->second;
874 _receivedReportBlockMap.erase(reportBlockInfoIt);
876 // we can't delete it due to TMMBR
877 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
878 _receivedInfoMap.find(rtcpPacket.BYE.SenderSSRC);
880 if (receiveInfoIt != _receivedInfoMap.end()) {
881 receiveInfoIt->second->readyForDelete = true;
884 std::map<uint32_t, RTCPCnameInformation*>::iterator cnameInfoIt =
885 _receivedCnameMap.find(rtcpPacket.BYE.SenderSSRC);
887 if (cnameInfoIt != _receivedCnameMap.end()) {
888 delete cnameInfoIt->second;
889 _receivedCnameMap.erase(cnameInfoIt);
892 rtcpParser.Iterate();
895 void RTCPReceiver::HandleXrHeader(
896 RTCPUtility::RTCPParserV2& parser,
897 RTCPPacketInformation& rtcpPacketInformation) {
898 const RTCPUtility::RTCPPacket& packet = parser.Packet();
900 rtcpPacketInformation.xr_originator_ssrc = packet.XR.OriginatorSSRC;
905 void RTCPReceiver::HandleXrReceiveReferenceTime(
906 RTCPUtility::RTCPParserV2& parser,
907 RTCPPacketInformation& rtcpPacketInformation) {
908 const RTCPUtility::RTCPPacket& packet = parser.Packet();
910 _remoteXRReceiveTimeInfo.sourceSSRC =
911 rtcpPacketInformation.xr_originator_ssrc;
913 _remoteXRReceiveTimeInfo.lastRR = RTCPUtility::MidNtp(
914 packet.XRReceiverReferenceTimeItem.NTPMostSignificant,
915 packet.XRReceiverReferenceTimeItem.NTPLeastSignificant);
917 _clock->CurrentNtp(_lastReceivedXRNTPsecs, _lastReceivedXRNTPfrac);
919 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrReceiverReferenceTime;
924 void RTCPReceiver::HandleXrDlrrReportBlock(
925 RTCPUtility::RTCPParserV2& parser,
926 RTCPPacketInformation& rtcpPacketInformation) {
927 const RTCPUtility::RTCPPacket& packet = parser.Packet();
928 // Iterate through sub-block(s), if any.
929 RTCPUtility::RTCPPacketTypes packet_type = parser.Iterate();
931 while (packet_type == RTCPUtility::kRtcpXrDlrrReportBlockItemCode) {
932 HandleXrDlrrReportBlockItem(packet, rtcpPacketInformation);
933 packet_type = parser.Iterate();
937 void RTCPReceiver::HandleXrDlrrReportBlockItem(
938 const RTCPUtility::RTCPPacket& packet,
939 RTCPPacketInformation& rtcpPacketInformation)
940 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver) {
941 if (registered_ssrcs_.find(packet.XRDLRRReportBlockItem.SSRC) ==
942 registered_ssrcs_.end()) {
947 rtcpPacketInformation.xr_dlrr_item = true;
949 // To avoid problem with acquiring _criticalSectionRTCPSender while holding
950 // _criticalSectionRTCPReceiver.
951 _criticalSectionRTCPReceiver->Leave();
953 int64_t send_time_ms;
954 bool found = _rtpRtcp.SendTimeOfXrRrReport(
955 packet.XRDLRRReportBlockItem.LastRR, &send_time_ms);
957 _criticalSectionRTCPReceiver->Enter();
963 // The DelayLastRR field is in units of 1/65536 sec.
964 uint32_t delay_rr_ms =
965 (((packet.XRDLRRReportBlockItem.DelayLastRR & 0x0000ffff) * 1000) >> 16) +
966 (((packet.XRDLRRReportBlockItem.DelayLastRR & 0xffff0000) >> 16) * 1000);
968 int32_t rtt = _clock->CurrentNtpInMilliseconds() - delay_rr_ms - send_time_ms;
970 xr_rr_rtt_ms_ = static_cast<uint16_t>(std::max(rtt, 1));
972 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrDlrrReportBlock;
975 // no need for critsect we have _criticalSectionRTCPReceiver
977 RTCPReceiver::HandleXRVOIPMetric(RTCPUtility::RTCPParserV2& rtcpParser,
978 RTCPPacketInformation& rtcpPacketInformation)
980 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
982 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
984 if(rtcpPacket.XRVOIPMetricItem.SSRC == main_ssrc_)
986 // Store VoIP metrics block if it's about me
987 // from OriginatorSSRC do we filter it?
988 // rtcpPacket.XR.OriginatorSSRC;
990 RTCPVoIPMetric receivedVoIPMetrics;
991 receivedVoIPMetrics.burstDensity = rtcpPacket.XRVOIPMetricItem.burstDensity;
992 receivedVoIPMetrics.burstDuration = rtcpPacket.XRVOIPMetricItem.burstDuration;
993 receivedVoIPMetrics.discardRate = rtcpPacket.XRVOIPMetricItem.discardRate;
994 receivedVoIPMetrics.endSystemDelay = rtcpPacket.XRVOIPMetricItem.endSystemDelay;
995 receivedVoIPMetrics.extRfactor = rtcpPacket.XRVOIPMetricItem.extRfactor;
996 receivedVoIPMetrics.gapDensity = rtcpPacket.XRVOIPMetricItem.gapDensity;
997 receivedVoIPMetrics.gapDuration = rtcpPacket.XRVOIPMetricItem.gapDuration;
998 receivedVoIPMetrics.Gmin = rtcpPacket.XRVOIPMetricItem.Gmin;
999 receivedVoIPMetrics.JBabsMax = rtcpPacket.XRVOIPMetricItem.JBabsMax;
1000 receivedVoIPMetrics.JBmax = rtcpPacket.XRVOIPMetricItem.JBmax;
1001 receivedVoIPMetrics.JBnominal = rtcpPacket.XRVOIPMetricItem.JBnominal;
1002 receivedVoIPMetrics.lossRate = rtcpPacket.XRVOIPMetricItem.lossRate;
1003 receivedVoIPMetrics.MOSCQ = rtcpPacket.XRVOIPMetricItem.MOSCQ;
1004 receivedVoIPMetrics.MOSLQ = rtcpPacket.XRVOIPMetricItem.MOSLQ;
1005 receivedVoIPMetrics.noiseLevel = rtcpPacket.XRVOIPMetricItem.noiseLevel;
1006 receivedVoIPMetrics.RERL = rtcpPacket.XRVOIPMetricItem.RERL;
1007 receivedVoIPMetrics.Rfactor = rtcpPacket.XRVOIPMetricItem.Rfactor;
1008 receivedVoIPMetrics.roundTripDelay = rtcpPacket.XRVOIPMetricItem.roundTripDelay;
1009 receivedVoIPMetrics.RXconfig = rtcpPacket.XRVOIPMetricItem.RXconfig;
1010 receivedVoIPMetrics.signalLevel = rtcpPacket.XRVOIPMetricItem.signalLevel;
1012 rtcpPacketInformation.AddVoIPMetric(&receivedVoIPMetrics);
1014 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrVoipMetric; // received signal
1016 rtcpParser.Iterate();
1019 // no need for critsect we have _criticalSectionRTCPReceiver
1020 void RTCPReceiver::HandlePLI(RTCPUtility::RTCPParserV2& rtcpParser,
1021 RTCPPacketInformation& rtcpPacketInformation) {
1022 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1023 if (main_ssrc_ == rtcpPacket.PLI.MediaSSRC) {
1024 TRACE_EVENT_INSTANT0("webrtc_rtp", "PLI");
1026 ++packet_type_counter_.pli_packets;
1027 // Received a signal that we need to send a new key frame.
1028 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpPli;
1030 rtcpParser.Iterate();
1033 // no need for critsect we have _criticalSectionRTCPReceiver
1035 RTCPReceiver::HandleTMMBR(RTCPUtility::RTCPParserV2& rtcpParser,
1036 RTCPPacketInformation& rtcpPacketInformation)
1038 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1040 uint32_t senderSSRC = rtcpPacket.TMMBR.SenderSSRC;
1041 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation(senderSSRC);
1042 if (ptrReceiveInfo == NULL)
1044 // This remote SSRC must be saved before.
1045 rtcpParser.Iterate();
1048 if(rtcpPacket.TMMBR.MediaSSRC)
1050 // rtcpPacket.TMMBR.MediaSSRC SHOULD be 0 if same as SenderSSRC
1051 // in relay mode this is a valid number
1052 senderSSRC = rtcpPacket.TMMBR.MediaSSRC;
1055 // Use packet length to calc max number of TMMBR blocks
1056 // each TMMBR block is 8 bytes
1057 ptrdiff_t maxNumOfTMMBRBlocks = rtcpParser.LengthLeft() / 8;
1060 if(maxNumOfTMMBRBlocks > 200) // we can't have more than what's in one packet
1063 rtcpParser.Iterate();
1066 ptrReceiveInfo->VerifyAndAllocateTMMBRSet((uint32_t)maxNumOfTMMBRBlocks);
1068 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1069 while (pktType == RTCPUtility::kRtcpRtpfbTmmbrItemCode)
1071 HandleTMMBRItem(*ptrReceiveInfo, rtcpPacket, rtcpPacketInformation, senderSSRC);
1072 pktType = rtcpParser.Iterate();
1076 // no need for critsect we have _criticalSectionRTCPReceiver
1078 RTCPReceiver::HandleTMMBRItem(RTCPReceiveInformation& receiveInfo,
1079 const RTCPUtility::RTCPPacket& rtcpPacket,
1080 RTCPPacketInformation& rtcpPacketInformation,
1081 const uint32_t senderSSRC)
1083 if (main_ssrc_ == rtcpPacket.TMMBRItem.SSRC &&
1084 rtcpPacket.TMMBRItem.MaxTotalMediaBitRate > 0)
1086 receiveInfo.InsertTMMBRItem(senderSSRC, rtcpPacket.TMMBRItem,
1087 _clock->TimeInMilliseconds());
1088 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbr;
1092 // no need for critsect we have _criticalSectionRTCPReceiver
1094 RTCPReceiver::HandleTMMBN(RTCPUtility::RTCPParserV2& rtcpParser,
1095 RTCPPacketInformation& rtcpPacketInformation)
1097 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1098 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation(rtcpPacket.TMMBN.SenderSSRC);
1099 if (ptrReceiveInfo == NULL)
1101 // This remote SSRC must be saved before.
1102 rtcpParser.Iterate();
1105 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbn;
1106 // Use packet length to calc max number of TMMBN blocks
1107 // each TMMBN block is 8 bytes
1108 ptrdiff_t maxNumOfTMMBNBlocks = rtcpParser.LengthLeft() / 8;
1111 if(maxNumOfTMMBNBlocks > 200) // we cant have more than what's in one packet
1114 rtcpParser.Iterate();
1118 ptrReceiveInfo->VerifyAndAllocateBoundingSet((uint32_t)maxNumOfTMMBNBlocks);
1120 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1121 while (pktType == RTCPUtility::kRtcpRtpfbTmmbnItemCode)
1123 HandleTMMBNItem(*ptrReceiveInfo, rtcpPacket);
1124 pktType = rtcpParser.Iterate();
1128 // no need for critsect we have _criticalSectionRTCPReceiver
1130 RTCPReceiver::HandleSR_REQ(RTCPUtility::RTCPParserV2& rtcpParser,
1131 RTCPPacketInformation& rtcpPacketInformation)
1133 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSrReq;
1134 rtcpParser.Iterate();
1137 // no need for critsect we have _criticalSectionRTCPReceiver
1139 RTCPReceiver::HandleTMMBNItem(RTCPReceiveInformation& receiveInfo,
1140 const RTCPUtility::RTCPPacket& rtcpPacket)
1142 receiveInfo.TmmbnBoundingSet.AddEntry(
1143 rtcpPacket.TMMBNItem.MaxTotalMediaBitRate,
1144 rtcpPacket.TMMBNItem.MeasuredOverhead,
1145 rtcpPacket.TMMBNItem.SSRC);
1148 // no need for critsect we have _criticalSectionRTCPReceiver
1150 RTCPReceiver::HandleSLI(RTCPUtility::RTCPParserV2& rtcpParser,
1151 RTCPPacketInformation& rtcpPacketInformation)
1153 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1154 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1155 while (pktType == RTCPUtility::kRtcpPsfbSliItemCode)
1157 HandleSLIItem(rtcpPacket, rtcpPacketInformation);
1158 pktType = rtcpParser.Iterate();
1162 // no need for critsect we have _criticalSectionRTCPReceiver
1164 RTCPReceiver::HandleSLIItem(const RTCPUtility::RTCPPacket& rtcpPacket,
1165 RTCPPacketInformation& rtcpPacketInformation)
1167 // in theory there could be multiple slices lost
1168 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSli; // received signal that we need to refresh a slice
1169 rtcpPacketInformation.sliPictureId = rtcpPacket.SLIItem.PictureId;
1173 RTCPReceiver::HandleRPSI(RTCPUtility::RTCPParserV2& rtcpParser,
1174 RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
1176 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1177 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1178 if(pktType == RTCPUtility::kRtcpPsfbRpsiCode)
1180 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRpsi; // received signal that we have a confirmed reference picture
1181 if(rtcpPacket.RPSI.NumberOfValidBits%8 != 0)
1185 rtcpParser.Iterate();
1188 rtcpPacketInformation.rpsiPictureId = 0;
1190 // convert NativeBitString to rpsiPictureId
1191 uint8_t numberOfBytes = rtcpPacket.RPSI.NumberOfValidBits /8;
1192 for(uint8_t n = 0; n < (numberOfBytes-1); n++)
1194 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[n] & 0x7f);
1195 rtcpPacketInformation.rpsiPictureId <<= 7; // prepare next
1197 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[numberOfBytes-1] & 0x7f);
1201 // no need for critsect we have _criticalSectionRTCPReceiver
1202 void RTCPReceiver::HandlePsfbApp(RTCPUtility::RTCPParserV2& rtcpParser,
1203 RTCPPacketInformation& rtcpPacketInformation) {
1204 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1205 if (pktType == RTCPUtility::kRtcpPsfbRembCode) {
1206 pktType = rtcpParser.Iterate();
1207 if (pktType == RTCPUtility::kRtcpPsfbRembItemCode) {
1208 HandleREMBItem(rtcpParser, rtcpPacketInformation);
1209 rtcpParser.Iterate();
1214 // no need for critsect we have _criticalSectionRTCPReceiver
1216 RTCPReceiver::HandleIJ(RTCPUtility::RTCPParserV2& rtcpParser,
1217 RTCPPacketInformation& rtcpPacketInformation)
1219 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1221 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1222 while (pktType == RTCPUtility::kRtcpExtendedIjItemCode)
1224 HandleIJItem(rtcpPacket, rtcpPacketInformation);
1225 pktType = rtcpParser.Iterate();
1230 RTCPReceiver::HandleIJItem(const RTCPUtility::RTCPPacket& rtcpPacket,
1231 RTCPPacketInformation& rtcpPacketInformation)
1233 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTransmissionTimeOffset;
1234 rtcpPacketInformation.interArrivalJitter =
1235 rtcpPacket.ExtendedJitterReportItem.Jitter;
1238 void RTCPReceiver::HandleREMBItem(
1239 RTCPUtility::RTCPParserV2& rtcpParser,
1240 RTCPPacketInformation& rtcpPacketInformation) {
1241 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1242 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRemb;
1243 rtcpPacketInformation.receiverEstimatedMaxBitrate =
1244 rtcpPacket.REMBItem.BitRate;
1247 // no need for critsect we have _criticalSectionRTCPReceiver
1248 void RTCPReceiver::HandleFIR(RTCPUtility::RTCPParserV2& rtcpParser,
1249 RTCPPacketInformation& rtcpPacketInformation) {
1250 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1251 RTCPReceiveInformation* ptrReceiveInfo =
1252 GetReceiveInformation(rtcpPacket.FIR.SenderSSRC);
1254 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1255 while (pktType == RTCPUtility::kRtcpPsfbFirItemCode) {
1256 HandleFIRItem(ptrReceiveInfo, rtcpPacket, rtcpPacketInformation);
1257 pktType = rtcpParser.Iterate();
1261 // no need for critsect we have _criticalSectionRTCPReceiver
1262 void RTCPReceiver::HandleFIRItem(RTCPReceiveInformation* receiveInfo,
1263 const RTCPUtility::RTCPPacket& rtcpPacket,
1264 RTCPPacketInformation& rtcpPacketInformation) {
1265 // Is it our sender that is requested to generate a new keyframe
1266 if (main_ssrc_ != rtcpPacket.FIRItem.SSRC) {
1270 ++packet_type_counter_.fir_packets;
1272 // rtcpPacket.FIR.MediaSSRC SHOULD be 0 but we ignore to check it
1273 // we don't know who this originate from
1275 // check if we have reported this FIRSequenceNumber before
1276 if (rtcpPacket.FIRItem.CommandSequenceNumber !=
1277 receiveInfo->lastFIRSequenceNumber) {
1278 int64_t now = _clock->TimeInMilliseconds();
1279 // sanity; don't go crazy with the callbacks
1280 if ((now - receiveInfo->lastFIRRequest) > RTCP_MIN_FRAME_LENGTH_MS) {
1281 receiveInfo->lastFIRRequest = now;
1282 receiveInfo->lastFIRSequenceNumber =
1283 rtcpPacket.FIRItem.CommandSequenceNumber;
1284 // received signal that we need to send a new key frame
1285 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir;
1289 // received signal that we need to send a new key frame
1290 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir;
1295 RTCPReceiver::HandleAPP(RTCPUtility::RTCPParserV2& rtcpParser,
1296 RTCPPacketInformation& rtcpPacketInformation)
1298 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1300 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpApp;
1301 rtcpPacketInformation.applicationSubType = rtcpPacket.APP.SubType;
1302 rtcpPacketInformation.applicationName = rtcpPacket.APP.Name;
1304 rtcpParser.Iterate();
1308 RTCPReceiver::HandleAPPItem(RTCPUtility::RTCPParserV2& rtcpParser,
1309 RTCPPacketInformation& rtcpPacketInformation)
1311 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1313 rtcpPacketInformation.AddApplicationData(rtcpPacket.APP.Data, rtcpPacket.APP.Size);
1315 rtcpParser.Iterate();
1318 int32_t RTCPReceiver::UpdateTMMBR() {
1319 int32_t numBoundingSet = 0;
1320 uint32_t bitrate = 0;
1321 uint32_t accNumCandidates = 0;
1323 int32_t size = TMMBRReceived(0, 0, NULL);
1325 TMMBRSet* candidateSet = VerifyAndAllocateCandidateSet(size);
1326 // Get candidate set from receiver.
1327 accNumCandidates = TMMBRReceived(size, accNumCandidates, candidateSet);
1329 // Candidate set empty.
1330 VerifyAndAllocateCandidateSet(0); // resets candidate set
1332 // Find bounding set
1333 TMMBRSet* boundingSet = NULL;
1334 numBoundingSet = FindTMMBRBoundingSet(boundingSet);
1335 if (numBoundingSet == -1) {
1336 LOG(LS_WARNING) << "Failed to find TMMBR bounding set.";
1340 // Inform remote clients about the new bandwidth
1341 // inform the remote client
1342 _rtpRtcp.SetTMMBN(boundingSet);
1344 // might trigger a TMMBN
1345 if (numBoundingSet == 0) {
1346 // owner of max bitrate request has timed out
1347 // empty bounding set has been sent
1350 // Get net bitrate from bounding set depending on sent packet rate
1351 if (CalcMinBitRate(&bitrate)) {
1352 // we have a new bandwidth estimate on this channel
1353 CriticalSectionScoped lock(_criticalSectionFeedbacks);
1354 if (_cbRtcpBandwidthObserver) {
1355 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate(bitrate * 1000);
1361 void RTCPReceiver::RegisterRtcpStatisticsCallback(
1362 RtcpStatisticsCallback* callback) {
1363 CriticalSectionScoped cs(_criticalSectionFeedbacks);
1364 stats_callback_ = callback;
1367 RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
1368 CriticalSectionScoped cs(_criticalSectionFeedbacks);
1369 return stats_callback_;
1372 // Holding no Critical section
1373 void RTCPReceiver::TriggerCallbacksFromRTCPPacket(
1374 RTCPPacketInformation& rtcpPacketInformation) {
1375 // Process TMMBR and REMB first to avoid multiple callbacks
1376 // to OnNetworkChanged.
1377 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpTmmbr) {
1378 // Might trigger a OnReceivedBandwidthEstimateUpdate.
1381 unsigned int local_ssrc = 0;
1383 // We don't want to hold this critsect when triggering the callbacks below.
1384 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1385 local_ssrc = main_ssrc_;
1387 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSrReq) {
1388 _rtpRtcp.OnRequestSendReport();
1390 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpNack) {
1391 if (rtcpPacketInformation.nackSequenceNumbers.size() > 0) {
1392 LOG(LS_VERBOSE) << "Incoming NACK length: "
1393 << rtcpPacketInformation.nackSequenceNumbers.size();
1394 _rtpRtcp.OnReceivedNACK(rtcpPacketInformation.nackSequenceNumbers);
1398 CriticalSectionScoped lock(_criticalSectionFeedbacks);
1400 // We need feedback that we have received a report block(s) so that we
1401 // can generate a new packet in a conference relay scenario, one received
1402 // report can generate several RTCP packets, based on number relayed/mixed
1403 // a send report block should go out to all receivers.
1404 if (_cbRtcpIntraFrameObserver) {
1405 if ((rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) ||
1406 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpFir)) {
1407 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) {
1408 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
1409 << rtcpPacketInformation.remoteSSRC;
1411 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
1412 << rtcpPacketInformation.remoteSSRC;
1414 _cbRtcpIntraFrameObserver->OnReceivedIntraFrameRequest(local_ssrc);
1416 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSli) {
1417 _cbRtcpIntraFrameObserver->OnReceivedSLI(
1418 local_ssrc, rtcpPacketInformation.sliPictureId);
1420 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRpsi) {
1421 _cbRtcpIntraFrameObserver->OnReceivedRPSI(
1422 local_ssrc, rtcpPacketInformation.rpsiPictureId);
1425 if (_cbRtcpBandwidthObserver) {
1426 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRemb) {
1427 LOG(LS_VERBOSE) << "Incoming REMB: "
1428 << rtcpPacketInformation.receiverEstimatedMaxBitrate;
1429 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate(
1430 rtcpPacketInformation.receiverEstimatedMaxBitrate);
1432 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr ||
1433 rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRr) {
1434 int64_t now = _clock->TimeInMilliseconds();
1435 _cbRtcpBandwidthObserver->OnReceivedRtcpReceiverReport(
1436 rtcpPacketInformation.report_blocks,
1437 rtcpPacketInformation.rtt,
1441 if(_cbRtcpFeedback) {
1442 if(!(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr)) {
1443 _cbRtcpFeedback->OnReceiveReportReceived(_id,
1444 rtcpPacketInformation.remoteSSRC);
1446 if(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpXrVoipMetric) {
1447 _cbRtcpFeedback->OnXRVoIPMetricReceived(_id,
1448 rtcpPacketInformation.VoIPMetric);
1450 if(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpApp) {
1451 _cbRtcpFeedback->OnApplicationDataReceived(_id,
1452 rtcpPacketInformation.applicationSubType,
1453 rtcpPacketInformation.applicationName,
1454 rtcpPacketInformation.applicationLength,
1455 rtcpPacketInformation.applicationData);
1461 CriticalSectionScoped cs(_criticalSectionFeedbacks);
1462 if (stats_callback_) {
1463 for (ReportBlockList::const_iterator it =
1464 rtcpPacketInformation.report_blocks.begin();
1465 it != rtcpPacketInformation.report_blocks.end();
1467 RtcpStatistics stats;
1468 stats.cumulative_lost = it->cumulativeLost;
1469 stats.extended_max_sequence_number = it->extendedHighSeqNum;
1470 stats.fraction_lost = it->fractionLost;
1471 stats.jitter = it->jitter;
1473 stats_callback_->StatisticsUpdated(stats, it->sourceSSRC);
1479 int32_t RTCPReceiver::CNAME(const uint32_t remoteSSRC,
1480 char cName[RTCP_CNAME_SIZE]) const {
1483 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1484 RTCPCnameInformation* cnameInfo = GetCnameInformation(remoteSSRC);
1485 if (cnameInfo == NULL) {
1488 cName[RTCP_CNAME_SIZE - 1] = 0;
1489 strncpy(cName, cnameInfo->name, RTCP_CNAME_SIZE - 1);
1493 // no callbacks allowed inside this function
1494 int32_t RTCPReceiver::TMMBRReceived(const uint32_t size,
1495 const uint32_t accNumCandidates,
1496 TMMBRSet* candidateSet) const {
1497 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1499 std::map<uint32_t, RTCPReceiveInformation*>::const_iterator
1500 receiveInfoIt = _receivedInfoMap.begin();
1501 if (receiveInfoIt == _receivedInfoMap.end()) {
1504 uint32_t num = accNumCandidates;
1506 while( num < size && receiveInfoIt != _receivedInfoMap.end()) {
1507 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
1508 if (receiveInfo == NULL) {
1511 for (uint32_t i = 0;
1512 (num < size) && (i < receiveInfo->TmmbrSet.lengthOfSet()); i++) {
1513 if (receiveInfo->GetTMMBRSet(i, num, candidateSet,
1514 _clock->TimeInMilliseconds()) == 0) {
1521 while (receiveInfoIt != _receivedInfoMap.end()) {
1522 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
1523 if(receiveInfo == NULL) {
1526 num += receiveInfo->TmmbrSet.lengthOfSet();
1533 } // namespace webrtc