blob: 3b9659a59906470d7c64a4d816066cbea410c6d8 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 "rtcp_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include <string.h> //memset
14#include <cassert> //assert
15
16#include "trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000017#include "trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018#include "critical_section_wrapper.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000019#include "rtcp_utility.h"
20#include "rtp_rtcp_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace
23{
24 const float FRAC = 4.294967296E9;
25}
26
27namespace webrtc {
28using namespace RTCPUtility;
29using namespace RTCPHelp;
30
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000031// The number of RTCP time intervals needed to trigger a timeout.
32const int kRrTimeoutIntervals = 3;
33
pbos@webrtc.org2f446732013-04-08 11:08:41 +000034RTCPReceiver::RTCPReceiver(const int32_t id, Clock* clock,
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +000035 ModuleRtpRtcpImpl* owner)
36 : TMMBRHelp(),
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000037 _id(id),
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000038 _clock(clock),
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000039 _method(kRtcpOff),
40 _lastReceived(0),
41 _rtpRtcp(*owner),
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +000042 _criticalSectionFeedbacks(
43 CriticalSectionWrapper::CreateCriticalSection()),
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000044 _cbRtcpFeedback(NULL),
45 _cbRtcpBandwidthObserver(NULL),
46 _cbRtcpIntraFrameObserver(NULL),
47 _criticalSectionRTCPReceiver(
48 CriticalSectionWrapper::CreateCriticalSection()),
49 _SSRC(0),
50 _remoteSSRC(0),
51 _remoteSenderInfo(),
52 _lastReceivedSRNTPsecs(0),
53 _lastReceivedSRNTPfrac(0),
54 _receivedInfoMap(),
55 _packetTimeOutMS(0),
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000056 _lastReceivedRrMs(0),
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +000057 _lastIncreasedSequenceNumberMs(0),
58 _rtt(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000059 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo));
60 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
61}
62
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000063RTCPReceiver::~RTCPReceiver() {
64 delete _criticalSectionRTCPReceiver;
65 delete _criticalSectionFeedbacks;
niklase@google.com470e71d2011-07-07 08:21:25 +000066
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000067 while (!_receivedReportBlockMap.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000068 std::map<uint32_t, RTCPReportBlockInformation*>::iterator first =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000069 _receivedReportBlockMap.begin();
70 delete first->second;
71 _receivedReportBlockMap.erase(first);
72 }
73 while (!_receivedInfoMap.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000074 std::map<uint32_t, RTCPReceiveInformation*>::iterator first =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000075 _receivedInfoMap.begin();
76 delete first->second;
77 _receivedInfoMap.erase(first);
78 }
79 while (!_receivedCnameMap.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000080 std::map<uint32_t, RTCPCnameInformation*>::iterator first =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000081 _receivedCnameMap.begin();
82 delete first->second;
83 _receivedCnameMap.erase(first);
84 }
85 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, _id,
86 "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
89void
pbos@webrtc.org2f446732013-04-08 11:08:41 +000090RTCPReceiver::ChangeUniqueId(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +000091{
92 _id = id;
93}
94
95RTCPMethod
96RTCPReceiver::Status() const
97{
98 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
99 return _method;
100}
101
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000102int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000103RTCPReceiver::SetRTCPStatus(const RTCPMethod method)
104{
105 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
106 _method = method;
107 return 0;
108}
109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110int64_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000111RTCPReceiver::LastReceived()
112{
113 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
114 return _lastReceived;
115}
116
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000117int64_t
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000118RTCPReceiver::LastReceivedReceiverReport() const {
119 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000120 int64_t last_received_rr = -1;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000121 for (ReceivedInfoMap::const_iterator it = _receivedInfoMap.begin();
122 it != _receivedInfoMap.end(); ++it) {
123 if (it->second->lastTimeReceived > last_received_rr) {
124 last_received_rr = it->second->lastTimeReceived;
125 }
126 }
127 return last_received_rr;
128}
129
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000130int32_t
131RTCPReceiver::SetRemoteSSRC( const uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000132{
133 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
134
135 // new SSRC reset old reports
136 memset(&_remoteSenderInfo, 0, sizeof(_remoteSenderInfo));
137 _lastReceivedSRNTPsecs = 0;
138 _lastReceivedSRNTPfrac = 0;
139
140 _remoteSSRC = ssrc;
141 return 0;
142}
143
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000144void RTCPReceiver::RegisterRtcpObservers(
145 RtcpIntraFrameObserver* intra_frame_callback,
146 RtcpBandwidthObserver* bandwidth_callback,
147 RtcpFeedback* feedback_callback) {
148 CriticalSectionScoped lock(_criticalSectionFeedbacks);
149 _cbRtcpIntraFrameObserver = intra_frame_callback;
150 _cbRtcpBandwidthObserver = bandwidth_callback;
151 _cbRtcpFeedback = feedback_callback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000155void RTCPReceiver::SetSSRC(const uint32_t ssrc) {
156 uint32_t old_ssrc = 0;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000157 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000159 old_ssrc = _SSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 _SSRC = ssrc;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000161 }
162 {
163 CriticalSectionScoped lock(_criticalSectionFeedbacks);
164 if (_cbRtcpIntraFrameObserver && old_ssrc != ssrc) {
165 _cbRtcpIntraFrameObserver->OnLocalSsrcChanged(old_ssrc, ssrc);
166 }
167 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTCPReceiver::ResetRTT(const uint32_t remoteSSRC) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000171 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
172 RTCPReportBlockInformation* reportBlock =
173 GetReportBlockInformation(remoteSSRC);
174 if (reportBlock == NULL) {
175 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
176 "\tfailed to GetReportBlockInformation(%u)", remoteSSRC);
177 return -1;
178 }
179 reportBlock->RTT = 0;
180 reportBlock->avgRTT = 0;
181 reportBlock->minRTT = 0;
182 reportBlock->maxRTT = 0;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000183 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000186int32_t RTCPReceiver::RTT(const uint32_t remoteSSRC,
187 uint16_t* RTT,
188 uint16_t* avgRTT,
189 uint16_t* minRTT,
190 uint16_t* maxRTT) const {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000191 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000193 RTCPReportBlockInformation* reportBlock =
194 GetReportBlockInformation(remoteSSRC);
195
196 if (reportBlock == NULL) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000197 return -1;
198 }
199 if (RTT) {
200 *RTT = reportBlock->RTT;
201 }
202 if (avgRTT) {
203 *avgRTT = reportBlock->avgRTT;
204 }
205 if (minRTT) {
206 *minRTT = reportBlock->minRTT;
207 }
208 if (maxRTT) {
209 *maxRTT = reportBlock->maxRTT;
210 }
211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000214uint16_t RTCPReceiver::RTT() const {
215 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
216 if (!_receivedReportBlockMap.empty()) {
217 return 0;
218 }
219 return _rtt;
220}
221
222int RTCPReceiver::SetRTT(uint16_t rtt) {
223 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
224 if (!_receivedReportBlockMap.empty()) {
225 return -1;
226 }
227 _rtt = rtt;
228 return 0;
229}
230
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000231int32_t
232RTCPReceiver::NTP(uint32_t *ReceivedNTPsecs,
233 uint32_t *ReceivedNTPfrac,
234 uint32_t *RTCPArrivalTimeSecs,
235 uint32_t *RTCPArrivalTimeFrac,
236 uint32_t *rtcp_timestamp) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000237{
238 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
239 if(ReceivedNTPsecs)
240 {
241 *ReceivedNTPsecs = _remoteSenderInfo.NTPseconds; // NTP from incoming SendReport
242 }
243 if(ReceivedNTPfrac)
244 {
245 *ReceivedNTPfrac = _remoteSenderInfo.NTPfraction;
246 }
247 if(RTCPArrivalTimeFrac)
248 {
249 *RTCPArrivalTimeFrac = _lastReceivedSRNTPfrac; // local NTP time when we received a RTCP packet with a send block
250 }
251 if(RTCPArrivalTimeSecs)
252 {
253 *RTCPArrivalTimeSecs = _lastReceivedSRNTPsecs;
254 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000255 if (rtcp_timestamp) {
256 *rtcp_timestamp = _remoteSenderInfo.RTPtimeStamp;
257 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000258 return 0;
259}
260
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000261int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000262RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* senderInfo) const
263{
264 if(senderInfo == NULL)
265 {
266 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__);
267 return -1;
268 }
269 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
270 if(_lastReceivedSRNTPsecs == 0)
271 {
272 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id, "%s No received SR", __FUNCTION__);
273 return -1;
274 }
275 memcpy(senderInfo, &(_remoteSenderInfo), sizeof(RTCPSenderInfo));
276 return 0;
277}
278
279// statistics
280// we can get multiple receive reports when we receive the report from a CE
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000281int32_t RTCPReceiver::StatisticsReceived(
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000282 std::vector<RTCPReportBlock>* receiveBlocks) const {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000283 assert(receiveBlocks);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000284 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
285
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000286 std::map<uint32_t, RTCPReportBlockInformation*>::const_iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000287 _receivedReportBlockMap.begin();
288
289 while (it != _receivedReportBlockMap.end()) {
290 receiveBlocks->push_back(it->second->remoteReceiveBlock);
291 it++;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000292 }
293 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294}
295
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000296int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000297RTCPReceiver::IncomingRTCPPacket(RTCPPacketInformation& rtcpPacketInformation,
298 RTCPUtility::RTCPParserV2* rtcpParser)
299{
300 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
301
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000302 _lastReceived = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
304 RTCPUtility::RTCPPacketTypes pktType = rtcpParser->Begin();
305 while (pktType != RTCPUtility::kRtcpNotValidCode)
306 {
307 // Each "case" is responsible for iterate the parser to the
308 // next top level packet.
309 switch (pktType)
310 {
311 case RTCPUtility::kRtcpSrCode:
312 case RTCPUtility::kRtcpRrCode:
313 HandleSenderReceiverReport(*rtcpParser, rtcpPacketInformation);
314 break;
315 case RTCPUtility::kRtcpSdesCode:
316 HandleSDES(*rtcpParser);
317 break;
318 case RTCPUtility::kRtcpXrVoipMetricCode:
319 HandleXRVOIPMetric(*rtcpParser, rtcpPacketInformation);
320 break;
321 case RTCPUtility::kRtcpByeCode:
322 HandleBYE(*rtcpParser);
323 break;
324 case RTCPUtility::kRtcpRtpfbNackCode:
325 HandleNACK(*rtcpParser, rtcpPacketInformation);
326 break;
327 case RTCPUtility::kRtcpRtpfbTmmbrCode:
328 HandleTMMBR(*rtcpParser, rtcpPacketInformation);
329 break;
330 case RTCPUtility::kRtcpRtpfbTmmbnCode:
hta@webrtc.org9d54cd12012-04-30 08:24:55 +0000331 HandleTMMBN(*rtcpParser, rtcpPacketInformation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 break;
333 case RTCPUtility::kRtcpRtpfbSrReqCode:
334 HandleSR_REQ(*rtcpParser, rtcpPacketInformation);
335 break;
336 case RTCPUtility::kRtcpPsfbPliCode:
337 HandlePLI(*rtcpParser, rtcpPacketInformation);
338 break;
339 case RTCPUtility::kRtcpPsfbSliCode:
340 HandleSLI(*rtcpParser, rtcpPacketInformation);
341 break;
342 case RTCPUtility::kRtcpPsfbRpsiCode:
343 HandleRPSI(*rtcpParser, rtcpPacketInformation);
344 break;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000345 case RTCPUtility::kRtcpExtendedIjCode:
346 HandleIJ(*rtcpParser, rtcpPacketInformation);
347 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348 case RTCPUtility::kRtcpPsfbFirCode:
349 HandleFIR(*rtcpParser, rtcpPacketInformation);
350 break;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000351 case RTCPUtility::kRtcpPsfbAppCode:
352 HandlePsfbApp(*rtcpParser, rtcpPacketInformation);
353 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354 case RTCPUtility::kRtcpAppCode:
355 // generic application messages
356 HandleAPP(*rtcpParser, rtcpPacketInformation);
357 break;
358 case RTCPUtility::kRtcpAppItemCode:
359 // generic application messages
360 HandleAPPItem(*rtcpParser, rtcpPacketInformation);
361 break;
362 default:
363 rtcpParser->Iterate();
364 break;
365 }
366 pktType = rtcpParser->PacketType();
367 }
368 return 0;
369}
370
371// no need for critsect we have _criticalSectionRTCPReceiver
372void
373RTCPReceiver::HandleSenderReceiverReport(RTCPUtility::RTCPParserV2& rtcpParser,
374 RTCPPacketInformation& rtcpPacketInformation)
375{
376 RTCPUtility::RTCPPacketTypes rtcpPacketType = rtcpParser.PacketType();
377 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
378
379 assert((rtcpPacketType == RTCPUtility::kRtcpRrCode) || (rtcpPacketType == RTCPUtility::kRtcpSrCode));
380
381 // SR.SenderSSRC
382 // The synchronization source identifier for the originator of this SR packet
383
384 // rtcpPacket.RR.SenderSSRC
385 // The source of the packet sender, same as of SR? or is this a CE?
386
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000387 const uint32_t remoteSSRC = (rtcpPacketType == RTCPUtility::kRtcpRrCode) ? rtcpPacket.RR.SenderSSRC:rtcpPacket.SR.SenderSSRC;
388 const uint8_t numberOfReportBlocks = (rtcpPacketType == RTCPUtility::kRtcpRrCode) ? rtcpPacket.RR.NumberOfReportBlocks:rtcpPacket.SR.NumberOfReportBlocks;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
390 rtcpPacketInformation.remoteSSRC = remoteSSRC;
391
392 RTCPReceiveInformation* ptrReceiveInfo = CreateReceiveInformation(remoteSSRC);
393 if (!ptrReceiveInfo)
394 {
395 rtcpParser.Iterate();
396 return;
397 }
398
399 if (rtcpPacketType == RTCPUtility::kRtcpSrCode)
400 {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000401 TRACE_EVENT_INSTANT2("webrtc_rtp", "SR",
402 "remote_ssrc", remoteSSRC,
403 "ssrc", _SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
405 if (_remoteSSRC == remoteSSRC) // have I received RTP packets from this party
406 {
407 // only signal that we have received a SR when we accept one
408 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSr;
409
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000410 rtcpPacketInformation.ntp_secs = rtcpPacket.SR.NTPMostSignificant;
411 rtcpPacketInformation.ntp_frac = rtcpPacket.SR.NTPLeastSignificant;
412 rtcpPacketInformation.rtp_timestamp = rtcpPacket.SR.RTPTimestamp;
413
niklase@google.com470e71d2011-07-07 08:21:25 +0000414 // We will only store the send report from one source, but
415 // we will store all the receive block
416
417 // Save the NTP time of this report
418 _remoteSenderInfo.NTPseconds = rtcpPacket.SR.NTPMostSignificant;
419 _remoteSenderInfo.NTPfraction = rtcpPacket.SR.NTPLeastSignificant;
420 _remoteSenderInfo.RTPtimeStamp = rtcpPacket.SR.RTPTimestamp;
421 _remoteSenderInfo.sendPacketCount = rtcpPacket.SR.SenderPacketCount;
422 _remoteSenderInfo.sendOctetCount = rtcpPacket.SR.SenderOctetCount;
423
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000424 _clock->CurrentNtp(_lastReceivedSRNTPsecs, _lastReceivedSRNTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000425 }
426 else
427 {
428 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr;
429 }
430 } else
431 {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000432 TRACE_EVENT_INSTANT2("webrtc_rtp", "RR",
433 "remote_ssrc", remoteSSRC,
434 "ssrc", _SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
436 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRr;
437 }
438 UpdateReceiveInformation(*ptrReceiveInfo);
439
440 rtcpPacketType = rtcpParser.Iterate();
441
442 while (rtcpPacketType == RTCPUtility::kRtcpReportBlockItemCode)
443 {
444 HandleReportBlock(rtcpPacket, rtcpPacketInformation, remoteSSRC, numberOfReportBlocks);
445 rtcpPacketType = rtcpParser.Iterate();
446 }
447}
448
449// no need for critsect we have _criticalSectionRTCPReceiver
450void
451RTCPReceiver::HandleReportBlock(const RTCPUtility::RTCPPacket& rtcpPacket,
452 RTCPPacketInformation& rtcpPacketInformation,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000453 const uint32_t remoteSSRC,
454 const uint8_t numberOfReportBlocks) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000455 // This will be called once per report block in the RTCP packet.
456 // We filter out all report blocks that are not for us.
457 // Each packet has max 31 RR blocks.
458 //
459 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000461 // |rtcpPacket.ReportBlockItem.SSRC| is the SSRC identifier of the source to
462 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000464 // Filter out all report blocks that are not for us.
465 if (rtcpPacket.ReportBlockItem.SSRC != _SSRC) {
466 // This block is not for us ignore it.
467 return;
468 }
469
470 // To avoid problem with acquiring _criticalSectionRTCPSender while holding
471 // _criticalSectionRTCPReceiver.
472 _criticalSectionRTCPReceiver->Leave();
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000473 uint32_t sendTimeMS =
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000474 _rtpRtcp.SendTimeOfSendReport(rtcpPacket.ReportBlockItem.LastSR);
475 _criticalSectionRTCPReceiver->Enter();
476
477 RTCPReportBlockInformation* reportBlock =
478 CreateReportBlockInformation(remoteSSRC);
479 if (reportBlock == NULL) {
480 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
481 "\tfailed to CreateReportBlockInformation(%u)", remoteSSRC);
482 return;
483 }
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000484
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000485 _lastReceivedRrMs = _clock->TimeInMilliseconds();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000486 const RTCPPacketReportBlockItem& rb = rtcpPacket.ReportBlockItem;
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000487 TRACE_COUNTER_ID1("webrtc_rtp", "RRFractionLost", rb.SSRC, rb.FractionLost);
488 TRACE_COUNTER_ID1("webrtc_rtp", "RRCumulativeNumOfPacketLost",
489 rb.SSRC, rb.CumulativeNumOfPacketsLost);
490 TRACE_COUNTER_ID1("webrtc_rtp", "RRJitter", rb.SSRC, rb.Jitter);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000491 reportBlock->remoteReceiveBlock.remoteSSRC = remoteSSRC;
492 reportBlock->remoteReceiveBlock.sourceSSRC = rb.SSRC;
493 reportBlock->remoteReceiveBlock.fractionLost = rb.FractionLost;
494 reportBlock->remoteReceiveBlock.cumulativeLost =
495 rb.CumulativeNumOfPacketsLost;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000496 if (rb.ExtendedHighestSequenceNumber >
497 reportBlock->remoteReceiveBlock.extendedHighSeqNum) {
498 // We have successfully delivered new RTP packets to the remote side after
499 // the last RR was sent from the remote side.
500 _lastIncreasedSequenceNumberMs = _lastReceivedRrMs;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000501 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000502 reportBlock->remoteReceiveBlock.extendedHighSeqNum =
503 rb.ExtendedHighestSequenceNumber;
504 reportBlock->remoteReceiveBlock.jitter = rb.Jitter;
505 reportBlock->remoteReceiveBlock.delaySinceLastSR = rb.DelayLastSR;
506 reportBlock->remoteReceiveBlock.lastSR = rb.LastSR;
507
508 if (rtcpPacket.ReportBlockItem.Jitter > reportBlock->remoteMaxJitter) {
509 reportBlock->remoteMaxJitter = rtcpPacket.ReportBlockItem.Jitter;
510 }
511
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000512 uint32_t delaySinceLastSendReport =
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000513 rtcpPacket.ReportBlockItem.DelayLastSR;
514
515 // local NTP time when we received this
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000516 uint32_t lastReceivedRRNTPsecs = 0;
517 uint32_t lastReceivedRRNTPfrac = 0;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000518
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000519 _clock->CurrentNtp(lastReceivedRRNTPsecs, lastReceivedRRNTPfrac);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000520
521 // time when we received this in MS
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000522 uint32_t receiveTimeMS = Clock::NtpToMs(lastReceivedRRNTPsecs,
523 lastReceivedRRNTPfrac);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000524
525 // Estimate RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000526 uint32_t d = (delaySinceLastSendReport & 0x0000ffff) * 1000;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000527 d /= 65536;
528 d += ((delaySinceLastSendReport & 0xffff0000) >> 16) * 1000;
529
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000530 int32_t RTT = 0;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000531
532 if (sendTimeMS > 0) {
533 RTT = receiveTimeMS - d - sendTimeMS;
534 if (RTT <= 0) {
535 RTT = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000536 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000537 if (RTT > reportBlock->maxRTT) {
538 // store max RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000539 reportBlock->maxRTT = (uint16_t) RTT;
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000541 if (reportBlock->minRTT == 0) {
542 // first RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000543 reportBlock->minRTT = (uint16_t) RTT;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000544 } else if (RTT < reportBlock->minRTT) {
545 // Store min RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000546 reportBlock->minRTT = (uint16_t) RTT;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000548 // store last RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000549 reportBlock->RTT = (uint16_t) RTT;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000551 // store average RTT
552 if (reportBlock->numAverageCalcs != 0) {
553 float ac = static_cast<float> (reportBlock->numAverageCalcs);
554 float newAverage = ((ac / (ac + 1)) * reportBlock->avgRTT)
555 + ((1 / (ac + 1)) * RTT);
556 reportBlock->avgRTT = static_cast<int> (newAverage + 0.5f);
557 } else {
558 // first RTT
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000559 reportBlock->avgRTT = (uint16_t) RTT;
niklase@google.com470e71d2011-07-07 08:21:25 +0000560 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000561 reportBlock->numAverageCalcs++;
562 }
563
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000564 TRACE_COUNTER_ID1("webrtc_rtp", "RR_RTT", rb.SSRC, RTT);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000565
566 // rtcpPacketInformation
567 rtcpPacketInformation.AddReportInfo(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000568 reportBlock->remoteReceiveBlock.fractionLost, (uint16_t) RTT,
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000569 reportBlock->remoteReceiveBlock.extendedHighSeqNum,
570 reportBlock->remoteReceiveBlock.jitter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000571}
572
573RTCPReportBlockInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000574RTCPReceiver::CreateReportBlockInformation(uint32_t remoteSSRC) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000575 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000577 std::map<uint32_t, RTCPReportBlockInformation*>::iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000578 _receivedReportBlockMap.find(remoteSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000580 RTCPReportBlockInformation* ptrReportBlockInfo = NULL;
581 if (it != _receivedReportBlockMap.end()) {
582 ptrReportBlockInfo = it->second;
583 } else {
584 ptrReportBlockInfo = new RTCPReportBlockInformation;
585 _receivedReportBlockMap[remoteSSRC] = ptrReportBlockInfo;
586 }
587 return ptrReportBlockInfo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588}
589
590RTCPReportBlockInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000591RTCPReceiver::GetReportBlockInformation(uint32_t remoteSSRC) const {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000592 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000594 std::map<uint32_t, RTCPReportBlockInformation*>::const_iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000595 _receivedReportBlockMap.find(remoteSSRC);
596
597 if (it == _receivedReportBlockMap.end()) {
598 return NULL;
599 }
600 return it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601}
602
603RTCPCnameInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000604RTCPReceiver::CreateCnameInformation(uint32_t remoteSSRC) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000605 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000606
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000607 std::map<uint32_t, RTCPCnameInformation*>::iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000608 _receivedCnameMap.find(remoteSSRC);
609
610 if (it != _receivedCnameMap.end()) {
611 return it->second;
612 }
613 RTCPCnameInformation* cnameInfo = new RTCPCnameInformation;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000614 memset(cnameInfo->name, 0, RTCP_CNAME_SIZE);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000615 _receivedCnameMap[remoteSSRC] = cnameInfo;
616 return cnameInfo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
619RTCPCnameInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000620RTCPReceiver::GetCnameInformation(uint32_t remoteSSRC) const {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000621 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000622
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000623 std::map<uint32_t, RTCPCnameInformation*>::const_iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000624 _receivedCnameMap.find(remoteSSRC);
625
626 if (it == _receivedCnameMap.end()) {
627 return NULL;
628 }
629 return it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000630}
631
632RTCPReceiveInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000633RTCPReceiver::CreateReceiveInformation(uint32_t remoteSSRC) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000634 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000635
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000636 std::map<uint32_t, RTCPReceiveInformation*>::iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000637 _receivedInfoMap.find(remoteSSRC);
638
639 if (it != _receivedInfoMap.end()) {
640 return it->second;
641 }
642 RTCPReceiveInformation* receiveInfo = new RTCPReceiveInformation;
643 _receivedInfoMap[remoteSSRC] = receiveInfo;
644 return receiveInfo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645}
646
647RTCPReceiveInformation*
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000648RTCPReceiver::GetReceiveInformation(uint32_t remoteSSRC) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000649 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000650
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000651 std::map<uint32_t, RTCPReceiveInformation*>::iterator it =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000652 _receivedInfoMap.find(remoteSSRC);
653 if (it == _receivedInfoMap.end()) {
654 return NULL;
655 }
656 return it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000659void RTCPReceiver::UpdateReceiveInformation(
660 RTCPReceiveInformation& receiveInformation) {
661 // Update that this remote is alive
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000662 receiveInformation.lastTimeReceived = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000663}
664
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000665bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
666 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
667 if (_lastReceivedRrMs == 0)
668 return false;
669
670 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000671 if (_clock->TimeInMilliseconds() > _lastReceivedRrMs + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000672 // Reset the timer to only trigger one log.
673 _lastReceivedRrMs = 0;
674 return true;
675 }
676 return false;
677}
678
679bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
680 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
681 if (_lastIncreasedSequenceNumberMs == 0)
682 return false;
683
684 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000685 if (_clock->TimeInMilliseconds() > _lastIncreasedSequenceNumberMs +
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +0000686 time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000687 // Reset the timer to only trigger one log.
688 _lastIncreasedSequenceNumberMs = 0;
689 return true;
690 }
691 return false;
692}
693
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000694bool RTCPReceiver::UpdateRTCPReceiveInformationTimers() {
695 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000697 bool updateBoundingSet = false;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000698 int64_t timeNow = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000700 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000701 _receivedInfoMap.begin();
niklase@google.com470e71d2011-07-07 08:21:25 +0000702
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000703 while (receiveInfoIt != _receivedInfoMap.end()) {
704 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
705 if (receiveInfo == NULL) {
706 return updateBoundingSet;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000708 // time since last received rtcp packet
709 // when we dont have a lastTimeReceived and the object is marked
710 // readyForDelete it's removed from the map
711 if (receiveInfo->lastTimeReceived) {
712 /// use audio define since we don't know what interval the remote peer is
713 // using
714 if ((timeNow - receiveInfo->lastTimeReceived) >
715 5 * RTCP_INTERVAL_AUDIO_MS) {
716 // no rtcp packet for the last five regular intervals, reset limitations
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000717 receiveInfo->TmmbrSet.clearSet();
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000718 // prevent that we call this over and over again
719 receiveInfo->lastTimeReceived = 0;
720 // send new TMMBN to all channels using the default codec
721 updateBoundingSet = true;
722 }
723 receiveInfoIt++;
724 } else if (receiveInfo->readyForDelete) {
725 // store our current receiveInfoItem
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000726 std::map<uint32_t, RTCPReceiveInformation*>::iterator
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000727 receiveInfoItemToBeErased = receiveInfoIt;
728 receiveInfoIt++;
729 delete receiveInfoItemToBeErased->second;
730 _receivedInfoMap.erase(receiveInfoItemToBeErased);
731 } else {
732 receiveInfoIt++;
733 }
734 }
735 return updateBoundingSet;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000738int32_t RTCPReceiver::BoundingSet(bool &tmmbrOwner, TMMBRSet* boundingSetRec) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000739 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000741 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000742 _receivedInfoMap.find(_remoteSSRC);
743
744 if (receiveInfoIt == _receivedInfoMap.end()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000745 return -1;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000746 }
747 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
748 if (receiveInfo == NULL) {
749 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
750 "%s failed to get RTCPReceiveInformation",
751 __FUNCTION__);
752 return -1;
753 }
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000754 if (receiveInfo->TmmbnBoundingSet.lengthOfSet() > 0) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000755 boundingSetRec->VerifyAndAllocateSet(
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000756 receiveInfo->TmmbnBoundingSet.lengthOfSet() + 1);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000757 for(uint32_t i=0; i< receiveInfo->TmmbnBoundingSet.lengthOfSet();
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000758 i++) {
759 if(receiveInfo->TmmbnBoundingSet.Ssrc(i) == _SSRC) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000760 // owner of bounding set
761 tmmbrOwner = true;
762 }
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000763 boundingSetRec->SetEntry(i,
764 receiveInfo->TmmbnBoundingSet.Tmmbr(i),
765 receiveInfo->TmmbnBoundingSet.PacketOH(i),
766 receiveInfo->TmmbnBoundingSet.Ssrc(i));
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000767 }
768 }
hta@webrtc.org54536bb2012-05-03 14:07:23 +0000769 return receiveInfo->TmmbnBoundingSet.lengthOfSet();
niklase@google.com470e71d2011-07-07 08:21:25 +0000770}
771
772// no need for critsect we have _criticalSectionRTCPReceiver
773void
774RTCPReceiver::HandleSDES(RTCPUtility::RTCPParserV2& rtcpParser)
775{
776 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
777 while (pktType == RTCPUtility::kRtcpSdesChunkCode)
778 {
779 HandleSDESChunk(rtcpParser);
780 pktType = rtcpParser.Iterate();
781 }
782}
783
784// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000785void RTCPReceiver::HandleSDESChunk(RTCPUtility::RTCPParserV2& rtcpParser) {
786 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
787 RTCPCnameInformation* cnameInfo =
788 CreateCnameInformation(rtcpPacket.CName.SenderSSRC);
789 assert(cnameInfo);
niklase@google.com470e71d2011-07-07 08:21:25 +0000790
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000791 cnameInfo->name[RTCP_CNAME_SIZE - 1] = 0;
792 strncpy(cnameInfo->name, rtcpPacket.CName.CName, RTCP_CNAME_SIZE - 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793}
794
795// no need for critsect we have _criticalSectionRTCPReceiver
796void
797RTCPReceiver::HandleNACK(RTCPUtility::RTCPParserV2& rtcpParser,
798 RTCPPacketInformation& rtcpPacketInformation)
799{
800 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 if (_SSRC != rtcpPacket.NACK.MediaSSRC)
802 {
803 // Not to us.
804 rtcpParser.Iterate();
805 return;
806 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 rtcpPacketInformation.ResetNACKPacketIdArray();
808
809 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
810 while (pktType == RTCPUtility::kRtcpRtpfbNackItemCode)
811 {
812 HandleNACKItem(rtcpPacket, rtcpPacketInformation);
813 pktType = rtcpParser.Iterate();
814 }
815}
816
817// no need for critsect we have _criticalSectionRTCPReceiver
818void
819RTCPReceiver::HandleNACKItem(const RTCPUtility::RTCPPacket& rtcpPacket,
820 RTCPPacketInformation& rtcpPacketInformation)
821{
822 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID);
823
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000824 uint16_t bitMask = rtcpPacket.NACKItem.BitMask;
niklase@google.com470e71d2011-07-07 08:21:25 +0000825 if(bitMask)
826 {
827 for(int i=1; i <= 16; ++i)
828 {
829 if(bitMask & 0x01)
830 {
831 rtcpPacketInformation.AddNACKPacket(rtcpPacket.NACKItem.PacketID + i);
832 }
833 bitMask = bitMask >>1;
834 }
835 }
836
837 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpNack;
838}
839
840// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000841void RTCPReceiver::HandleBYE(RTCPUtility::RTCPParserV2& rtcpParser) {
842 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
niklase@google.com470e71d2011-07-07 08:21:25 +0000843
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000844 // clear our lists
845 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000846 std::map<uint32_t, RTCPReportBlockInformation*>::iterator
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000847 reportBlockInfoIt = _receivedReportBlockMap.find(
848 rtcpPacket.BYE.SenderSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000849
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000850 if (reportBlockInfoIt != _receivedReportBlockMap.end()) {
851 delete reportBlockInfoIt->second;
852 _receivedReportBlockMap.erase(reportBlockInfoIt);
853 }
854 // we can't delete it due to TMMBR
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000855 std::map<uint32_t, RTCPReceiveInformation*>::iterator receiveInfoIt =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000856 _receivedInfoMap.find(rtcpPacket.BYE.SenderSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000858 if (receiveInfoIt != _receivedInfoMap.end()) {
859 receiveInfoIt->second->readyForDelete = true;
860 }
861
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000862 std::map<uint32_t, RTCPCnameInformation*>::iterator cnameInfoIt =
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000863 _receivedCnameMap.find(rtcpPacket.BYE.SenderSSRC);
864
865 if (cnameInfoIt != _receivedCnameMap.end()) {
866 delete cnameInfoIt->second;
867 _receivedCnameMap.erase(cnameInfoIt);
868 }
869 rtcpParser.Iterate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000870}
871
872// no need for critsect we have _criticalSectionRTCPReceiver
873void
874RTCPReceiver::HandleXRVOIPMetric(RTCPUtility::RTCPParserV2& rtcpParser,
875 RTCPPacketInformation& rtcpPacketInformation)
876{
877 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
878
879 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
880
881 if(rtcpPacket.XRVOIPMetricItem.SSRC == _SSRC)
882 {
883 // Store VoIP metrics block if it's about me
884 // from OriginatorSSRC do we filter it?
885 // rtcpPacket.XR.OriginatorSSRC;
886
887 RTCPVoIPMetric receivedVoIPMetrics;
888 receivedVoIPMetrics.burstDensity = rtcpPacket.XRVOIPMetricItem.burstDensity;
889 receivedVoIPMetrics.burstDuration = rtcpPacket.XRVOIPMetricItem.burstDuration;
890 receivedVoIPMetrics.discardRate = rtcpPacket.XRVOIPMetricItem.discardRate;
891 receivedVoIPMetrics.endSystemDelay = rtcpPacket.XRVOIPMetricItem.endSystemDelay;
892 receivedVoIPMetrics.extRfactor = rtcpPacket.XRVOIPMetricItem.extRfactor;
893 receivedVoIPMetrics.gapDensity = rtcpPacket.XRVOIPMetricItem.gapDensity;
894 receivedVoIPMetrics.gapDuration = rtcpPacket.XRVOIPMetricItem.gapDuration;
895 receivedVoIPMetrics.Gmin = rtcpPacket.XRVOIPMetricItem.Gmin;
896 receivedVoIPMetrics.JBabsMax = rtcpPacket.XRVOIPMetricItem.JBabsMax;
897 receivedVoIPMetrics.JBmax = rtcpPacket.XRVOIPMetricItem.JBmax;
898 receivedVoIPMetrics.JBnominal = rtcpPacket.XRVOIPMetricItem.JBnominal;
899 receivedVoIPMetrics.lossRate = rtcpPacket.XRVOIPMetricItem.lossRate;
900 receivedVoIPMetrics.MOSCQ = rtcpPacket.XRVOIPMetricItem.MOSCQ;
901 receivedVoIPMetrics.MOSLQ = rtcpPacket.XRVOIPMetricItem.MOSLQ;
902 receivedVoIPMetrics.noiseLevel = rtcpPacket.XRVOIPMetricItem.noiseLevel;
903 receivedVoIPMetrics.RERL = rtcpPacket.XRVOIPMetricItem.RERL;
904 receivedVoIPMetrics.Rfactor = rtcpPacket.XRVOIPMetricItem.Rfactor;
905 receivedVoIPMetrics.roundTripDelay = rtcpPacket.XRVOIPMetricItem.roundTripDelay;
906 receivedVoIPMetrics.RXconfig = rtcpPacket.XRVOIPMetricItem.RXconfig;
907 receivedVoIPMetrics.signalLevel = rtcpPacket.XRVOIPMetricItem.signalLevel;
908
909 rtcpPacketInformation.AddVoIPMetric(&receivedVoIPMetrics);
910
911 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpXrVoipMetric; // received signal
912 }
913 rtcpParser.Iterate();
914}
915
916// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000917void RTCPReceiver::HandlePLI(RTCPUtility::RTCPParserV2& rtcpParser,
918 RTCPPacketInformation& rtcpPacketInformation) {
919 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
920 if (_SSRC == rtcpPacket.PLI.MediaSSRC) {
921 // Received a signal that we need to send a new key frame.
922 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpPli;
923 }
924 rtcpParser.Iterate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000925}
926
927// no need for critsect we have _criticalSectionRTCPReceiver
928void
929RTCPReceiver::HandleTMMBR(RTCPUtility::RTCPParserV2& rtcpParser,
930 RTCPPacketInformation& rtcpPacketInformation)
931{
932 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
933
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000934 uint32_t senderSSRC = rtcpPacket.TMMBR.SenderSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation(senderSSRC);
936 if (ptrReceiveInfo == NULL)
937 {
938 // This remote SSRC must be saved before.
939 rtcpParser.Iterate();
940 return;
941 }
942 if(rtcpPacket.TMMBR.MediaSSRC)
943 {
944 // rtcpPacket.TMMBR.MediaSSRC SHOULD be 0 if same as SenderSSRC
945 // in relay mode this is a valid number
946 senderSSRC = rtcpPacket.TMMBR.MediaSSRC;
947 }
948
949 // Use packet length to calc max number of TMMBR blocks
950 // each TMMBR block is 8 bytes
951 ptrdiff_t maxNumOfTMMBRBlocks = rtcpParser.LengthLeft() / 8;
952
953 // sanity
954 if(maxNumOfTMMBRBlocks > 200) // we can't have more than what's in one packet
955 {
956 assert(false);
957 rtcpParser.Iterate();
958 return;
959 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000960 ptrReceiveInfo->VerifyAndAllocateTMMBRSet((uint32_t)maxNumOfTMMBRBlocks);
niklase@google.com470e71d2011-07-07 08:21:25 +0000961
962 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
963 while (pktType == RTCPUtility::kRtcpRtpfbTmmbrItemCode)
964 {
965 HandleTMMBRItem(*ptrReceiveInfo, rtcpPacket, rtcpPacketInformation, senderSSRC);
966 pktType = rtcpParser.Iterate();
967 }
968}
969
970// no need for critsect we have _criticalSectionRTCPReceiver
971void
972RTCPReceiver::HandleTMMBRItem(RTCPReceiveInformation& receiveInfo,
973 const RTCPUtility::RTCPPacket& rtcpPacket,
974 RTCPPacketInformation& rtcpPacketInformation,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000975 const uint32_t senderSSRC)
niklase@google.com470e71d2011-07-07 08:21:25 +0000976{
977 if (_SSRC == rtcpPacket.TMMBRItem.SSRC &&
978 rtcpPacket.TMMBRItem.MaxTotalMediaBitRate > 0)
979 {
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +0000980 receiveInfo.InsertTMMBRItem(senderSSRC, rtcpPacket.TMMBRItem,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000981 _clock->TimeInMilliseconds());
niklase@google.com470e71d2011-07-07 08:21:25 +0000982 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbr;
983 }
984}
985
986// no need for critsect we have _criticalSectionRTCPReceiver
987void
hta@webrtc.org9d54cd12012-04-30 08:24:55 +0000988RTCPReceiver::HandleTMMBN(RTCPUtility::RTCPParserV2& rtcpParser,
989 RTCPPacketInformation& rtcpPacketInformation)
niklase@google.com470e71d2011-07-07 08:21:25 +0000990{
991 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
992 RTCPReceiveInformation* ptrReceiveInfo = GetReceiveInformation(rtcpPacket.TMMBN.SenderSSRC);
993 if (ptrReceiveInfo == NULL)
994 {
995 // This remote SSRC must be saved before.
996 rtcpParser.Iterate();
997 return;
998 }
hta@webrtc.org9d54cd12012-04-30 08:24:55 +0000999 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +00001000 // Use packet length to calc max number of TMMBN blocks
1001 // each TMMBN block is 8 bytes
1002 ptrdiff_t maxNumOfTMMBNBlocks = rtcpParser.LengthLeft() / 8;
1003
1004 // sanity
1005 if(maxNumOfTMMBNBlocks > 200) // we cant have more than what's in one packet
1006 {
1007 assert(false);
1008 rtcpParser.Iterate();
1009 return;
1010 }
1011
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001012 ptrReceiveInfo->VerifyAndAllocateBoundingSet((uint32_t)maxNumOfTMMBNBlocks);
niklase@google.com470e71d2011-07-07 08:21:25 +00001013
1014 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1015 while (pktType == RTCPUtility::kRtcpRtpfbTmmbnItemCode)
1016 {
1017 HandleTMMBNItem(*ptrReceiveInfo, rtcpPacket);
1018 pktType = rtcpParser.Iterate();
1019 }
1020}
1021
1022// no need for critsect we have _criticalSectionRTCPReceiver
1023void
1024RTCPReceiver::HandleSR_REQ(RTCPUtility::RTCPParserV2& rtcpParser,
1025 RTCPPacketInformation& rtcpPacketInformation)
1026{
1027 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSrReq;
1028 rtcpParser.Iterate();
1029}
1030
1031// no need for critsect we have _criticalSectionRTCPReceiver
1032void
1033RTCPReceiver::HandleTMMBNItem(RTCPReceiveInformation& receiveInfo,
1034 const RTCPUtility::RTCPPacket& rtcpPacket)
1035{
hta@webrtc.org54536bb2012-05-03 14:07:23 +00001036 receiveInfo.TmmbnBoundingSet.AddEntry(
1037 rtcpPacket.TMMBNItem.MaxTotalMediaBitRate,
1038 rtcpPacket.TMMBNItem.MeasuredOverhead,
1039 rtcpPacket.TMMBNItem.SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00001040}
1041
1042// no need for critsect we have _criticalSectionRTCPReceiver
1043void
1044RTCPReceiver::HandleSLI(RTCPUtility::RTCPParserV2& rtcpParser,
1045 RTCPPacketInformation& rtcpPacketInformation)
1046{
1047 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1049 while (pktType == RTCPUtility::kRtcpPsfbSliItemCode)
1050 {
1051 HandleSLIItem(rtcpPacket, rtcpPacketInformation);
1052 pktType = rtcpParser.Iterate();
1053 }
1054}
1055
1056// no need for critsect we have _criticalSectionRTCPReceiver
1057void
1058RTCPReceiver::HandleSLIItem(const RTCPUtility::RTCPPacket& rtcpPacket,
1059 RTCPPacketInformation& rtcpPacketInformation)
1060{
1061 // in theory there could be multiple slices lost
1062 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpSli; // received signal that we need to refresh a slice
1063 rtcpPacketInformation.sliPictureId = rtcpPacket.SLIItem.PictureId;
1064}
1065
1066void
1067RTCPReceiver::HandleRPSI(RTCPUtility::RTCPParserV2& rtcpParser,
1068 RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
1069{
1070 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1072 if(pktType == RTCPUtility::kRtcpPsfbRpsiCode)
1073 {
1074 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRpsi; // received signal that we have a confirmed reference picture
1075 if(rtcpPacket.RPSI.NumberOfValidBits%8 != 0)
1076 {
1077 // to us unknown
1078 // continue
1079 rtcpParser.Iterate();
1080 return;
1081 }
1082 rtcpPacketInformation.rpsiPictureId = 0;
1083
1084 // convert NativeBitString to rpsiPictureId
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001085 uint8_t numberOfBytes = rtcpPacket.RPSI.NumberOfValidBits /8;
1086 for(uint8_t n = 0; n < (numberOfBytes-1); n++)
niklase@google.com470e71d2011-07-07 08:21:25 +00001087 {
1088 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[n] & 0x7f);
1089 rtcpPacketInformation.rpsiPictureId <<= 7; // prepare next
1090 }
1091 rtcpPacketInformation.rpsiPictureId += (rtcpPacket.RPSI.NativeBitString[numberOfBytes-1] & 0x7f);
1092 }
1093}
1094
1095// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001096void RTCPReceiver::HandlePsfbApp(RTCPUtility::RTCPParserV2& rtcpParser,
1097 RTCPPacketInformation& rtcpPacketInformation) {
1098 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1099 if (pktType == RTCPUtility::kRtcpPsfbRembCode) {
1100 pktType = rtcpParser.Iterate();
1101 if (pktType == RTCPUtility::kRtcpPsfbRembItemCode) {
1102 HandleREMBItem(rtcpParser, rtcpPacketInformation);
1103 rtcpParser.Iterate();
pwestin@webrtc.org741da942011-09-20 13:52:04 +00001104 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001105 }
pwestin@webrtc.org741da942011-09-20 13:52:04 +00001106}
1107
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001108// no need for critsect we have _criticalSectionRTCPReceiver
1109void
1110RTCPReceiver::HandleIJ(RTCPUtility::RTCPParserV2& rtcpParser,
1111 RTCPPacketInformation& rtcpPacketInformation)
1112{
1113 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1114
1115 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1116 while (pktType == RTCPUtility::kRtcpExtendedIjItemCode)
1117 {
1118 HandleIJItem(rtcpPacket, rtcpPacketInformation);
1119 pktType = rtcpParser.Iterate();
1120 }
1121}
1122
1123void
1124RTCPReceiver::HandleIJItem(const RTCPUtility::RTCPPacket& rtcpPacket,
1125 RTCPPacketInformation& rtcpPacketInformation)
1126{
1127 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpTransmissionTimeOffset;
1128 rtcpPacketInformation.interArrivalJitter =
1129 rtcpPacket.ExtendedJitterReportItem.Jitter;
1130}
1131
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001132void RTCPReceiver::HandleREMBItem(
1133 RTCPUtility::RTCPParserV2& rtcpParser,
1134 RTCPPacketInformation& rtcpPacketInformation) {
1135 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1136 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpRemb;
1137 rtcpPacketInformation.receiverEstimatedMaxBitrate =
1138 rtcpPacket.REMBItem.BitRate;
pwestin@webrtc.org741da942011-09-20 13:52:04 +00001139}
1140
1141// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +00001142void RTCPReceiver::HandleFIR(RTCPUtility::RTCPParserV2& rtcpParser,
1143 RTCPPacketInformation& rtcpPacketInformation) {
1144 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1145 RTCPReceiveInformation* ptrReceiveInfo =
1146 GetReceiveInformation(rtcpPacket.FIR.SenderSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00001147
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +00001148 RTCPUtility::RTCPPacketTypes pktType = rtcpParser.Iterate();
1149 while (pktType == RTCPUtility::kRtcpPsfbFirItemCode) {
1150 HandleFIRItem(ptrReceiveInfo, rtcpPacket, rtcpPacketInformation);
1151 pktType = rtcpParser.Iterate();
1152 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001153}
1154
1155// no need for critsect we have _criticalSectionRTCPReceiver
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +00001156void RTCPReceiver::HandleFIRItem(RTCPReceiveInformation* receiveInfo,
1157 const RTCPUtility::RTCPPacket& rtcpPacket,
1158 RTCPPacketInformation& rtcpPacketInformation) {
1159 // Is it our sender that is requested to generate a new keyframe
1160 if (_SSRC != rtcpPacket.FIRItem.SSRC) {
1161 return;
1162 }
1163 // rtcpPacket.FIR.MediaSSRC SHOULD be 0 but we ignore to check it
1164 // we don't know who this originate from
1165 if (receiveInfo) {
1166 // check if we have reported this FIRSequenceNumber before
1167 if (rtcpPacket.FIRItem.CommandSequenceNumber !=
1168 receiveInfo->lastFIRSequenceNumber) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001169 int64_t now = _clock->TimeInMilliseconds();
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +00001170 // sanity; don't go crazy with the callbacks
1171 if ((now - receiveInfo->lastFIRRequest) > RTCP_MIN_FRAME_LENGTH_MS) {
1172 receiveInfo->lastFIRRequest = now;
1173 receiveInfo->lastFIRSequenceNumber =
1174 rtcpPacket.FIRItem.CommandSequenceNumber;
1175 // received signal that we need to send a new key frame
1176 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir;
1177 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 }
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +00001179 } else {
1180 // received signal that we need to send a new key frame
1181 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpFir;
1182 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001183}
1184
1185void
1186RTCPReceiver::HandleAPP(RTCPUtility::RTCPParserV2& rtcpParser,
1187 RTCPPacketInformation& rtcpPacketInformation)
1188{
1189 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1190
1191 rtcpPacketInformation.rtcpPacketTypeFlags |= kRtcpApp;
1192 rtcpPacketInformation.applicationSubType = rtcpPacket.APP.SubType;
1193 rtcpPacketInformation.applicationName = rtcpPacket.APP.Name;
1194
1195 rtcpParser.Iterate();
1196}
1197
1198void
1199RTCPReceiver::HandleAPPItem(RTCPUtility::RTCPParserV2& rtcpParser,
1200 RTCPPacketInformation& rtcpPacketInformation)
1201{
1202 const RTCPUtility::RTCPPacket& rtcpPacket = rtcpParser.Packet();
1203
1204 rtcpPacketInformation.AddApplicationData(rtcpPacket.APP.Data, rtcpPacket.APP.Size);
1205
1206 rtcpParser.Iterate();
1207}
1208
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001209int32_t RTCPReceiver::UpdateTMMBR() {
1210 int32_t numBoundingSet = 0;
1211 uint32_t bitrate = 0;
1212 uint32_t accNumCandidates = 0;
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +00001213
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001214 int32_t size = TMMBRReceived(0, 0, NULL);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +00001215 if (size > 0) {
1216 TMMBRSet* candidateSet = VerifyAndAllocateCandidateSet(size);
1217 // Get candidate set from receiver.
1218 accNumCandidates = TMMBRReceived(size, accNumCandidates, candidateSet);
1219 } else {
1220 // Candidate set empty.
1221 VerifyAndAllocateCandidateSet(0); // resets candidate set
1222 }
1223 // Find bounding set
1224 TMMBRSet* boundingSet = NULL;
1225 numBoundingSet = FindTMMBRBoundingSet(boundingSet);
1226 if (numBoundingSet == -1) {
1227 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id,
1228 "Failed to find TMMBR bounding set.");
1229 return -1;
1230 }
1231 // Set bounding set
1232 // Inform remote clients about the new bandwidth
1233 // inform the remote client
1234 _rtpRtcp.SetTMMBN(boundingSet);
1235
1236 // might trigger a TMMBN
1237 if (numBoundingSet == 0) {
1238 // owner of max bitrate request has timed out
1239 // empty bounding set has been sent
1240 return 0;
1241 }
1242 // Get net bitrate from bounding set depending on sent packet rate
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001243 if (CalcMinBitRate(&bitrate)) {
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +00001244 // we have a new bandwidth estimate on this channel
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001245 CriticalSectionScoped lock(_criticalSectionFeedbacks);
1246 if (_cbRtcpBandwidthObserver) {
1247 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate(bitrate * 1000);
1248 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, _id,
1249 "Set TMMBR request:%d kbps", bitrate);
1250 }
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +00001251 }
1252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001253}
1254
1255// Holding no Critical section
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001256void RTCPReceiver::TriggerCallbacksFromRTCPPacket(
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001257 RTCPPacketInformation& rtcpPacketInformation) {
1258 // Process TMMBR and REMB first to avoid multiple callbacks
1259 // to OnNetworkChanged.
1260 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpTmmbr) {
1261 WEBRTC_TRACE(kTraceStateInfo, kTraceRtpRtcp, _id,
1262 "SIG [RTCP] Incoming TMMBR to id:%d", _id);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001263
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001264 // Might trigger a OnReceivedBandwidthEstimateUpdate.
1265 UpdateTMMBR();
1266 }
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001267 unsigned int local_ssrc = 0;
1268 {
1269 // We don't want to hold this critsect when triggering the callbacks below.
1270 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1271 local_ssrc = _SSRC;
1272 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001273 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSrReq) {
1274 _rtpRtcp.OnRequestSendReport();
1275 }
1276 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpNack) {
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +00001277 if (rtcpPacketInformation.nackSequenceNumbers.size() > 0) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001278 WEBRTC_TRACE(kTraceStateInfo, kTraceRtpRtcp, _id,
1279 "SIG [RTCP] Incoming NACK length:%d",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +00001280 rtcpPacketInformation.nackSequenceNumbers.size());
1281 _rtpRtcp.OnReceivedNACK(rtcpPacketInformation.nackSequenceNumbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001282 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001283 }
1284 {
1285 CriticalSectionScoped lock(_criticalSectionFeedbacks);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001286
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001287 // We need feedback that we have received a report block(s) so that we
1288 // can generate a new packet in a conference relay scenario, one received
1289 // report can generate several RTCP packets, based on number relayed/mixed
1290 // a send report block should go out to all receivers.
1291 if (_cbRtcpIntraFrameObserver) {
1292 if ((rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) ||
1293 (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpFir)) {
1294 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpPli) {
1295 WEBRTC_TRACE(kTraceStateInfo, kTraceRtpRtcp, _id,
1296 "SIG [RTCP] Incoming PLI from SSRC:0x%x",
1297 rtcpPacketInformation.remoteSSRC);
1298 } else {
1299 WEBRTC_TRACE(kTraceStateInfo, kTraceRtpRtcp, _id,
1300 "SIG [RTCP] Incoming FIR from SSRC:0x%x",
1301 rtcpPacketInformation.remoteSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00001302 }
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001303 _cbRtcpIntraFrameObserver->OnReceivedIntraFrameRequest(local_ssrc);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001304 }
1305 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSli) {
1306 _cbRtcpIntraFrameObserver->OnReceivedSLI(
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001307 local_ssrc, rtcpPacketInformation.sliPictureId);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001308 }
1309 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRpsi) {
1310 _cbRtcpIntraFrameObserver->OnReceivedRPSI(
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001311 local_ssrc, rtcpPacketInformation.rpsiPictureId);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001312 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001313 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001314 if (_cbRtcpBandwidthObserver) {
1315 if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRemb) {
1316 WEBRTC_TRACE(kTraceStateInfo, kTraceRtpRtcp, _id,
1317 "SIG [RTCP] Incoming REMB:%d",
1318 rtcpPacketInformation.receiverEstimatedMaxBitrate);
1319 _cbRtcpBandwidthObserver->OnReceivedEstimatedBitrate(
1320 rtcpPacketInformation.receiverEstimatedMaxBitrate);
1321 }
1322 if ((rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr ||
1323 rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpRr) &&
1324 rtcpPacketInformation.reportBlock) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001325 int64_t now = _clock->TimeInMilliseconds();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001326 _cbRtcpBandwidthObserver->OnReceivedRtcpReceiverReport(
1327 rtcpPacketInformation.remoteSSRC,
1328 rtcpPacketInformation.fractionLost,
1329 rtcpPacketInformation.roundTripTime,
1330 rtcpPacketInformation.lastReceivedExtendedHighSeqNum,
1331 now);
1332 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001333 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001334 if(_cbRtcpFeedback) {
1335 if(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr) {
1336 _cbRtcpFeedback->OnSendReportReceived(_id,
stefan@webrtc.org976a7e62012-09-21 13:20:21 +00001337 rtcpPacketInformation.remoteSSRC,
1338 rtcpPacketInformation.ntp_secs,
1339 rtcpPacketInformation.ntp_frac,
1340 rtcpPacketInformation.rtp_timestamp);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001341 } else {
1342 _cbRtcpFeedback->OnReceiveReportReceived(_id,
1343 rtcpPacketInformation.remoteSSRC);
1344 }
1345 if(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpXrVoipMetric) {
1346 _cbRtcpFeedback->OnXRVoIPMetricReceived(_id,
1347 rtcpPacketInformation.VoIPMetric);
1348 }
1349 if(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpApp) {
1350 _cbRtcpFeedback->OnApplicationDataReceived(_id,
1351 rtcpPacketInformation.applicationSubType,
1352 rtcpPacketInformation.applicationName,
1353 rtcpPacketInformation.applicationLength,
1354 rtcpPacketInformation.applicationData);
1355 }
1356 }
1357 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001358}
1359
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001360int32_t RTCPReceiver::CNAME(const uint32_t remoteSSRC,
1361 char cName[RTCP_CNAME_SIZE]) const {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001362 assert(cName);
1363
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001364 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1365 RTCPCnameInformation* cnameInfo = GetCnameInformation(remoteSSRC);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001366 if (cnameInfo == NULL) {
1367 return -1;
1368 }
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001369 cName[RTCP_CNAME_SIZE - 1] = 0;
1370 strncpy(cName, cnameInfo->name, RTCP_CNAME_SIZE - 1);
1371 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
1374// no callbacks allowed inside this function
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001375int32_t RTCPReceiver::TMMBRReceived(const uint32_t size,
1376 const uint32_t accNumCandidates,
1377 TMMBRSet* candidateSet) const {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001378 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +00001379
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001380 std::map<uint32_t, RTCPReceiveInformation*>::const_iterator
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001381 receiveInfoIt = _receivedInfoMap.begin();
1382 if (receiveInfoIt == _receivedInfoMap.end()) {
1383 return -1;
1384 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001385 uint32_t num = accNumCandidates;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001386 if (candidateSet) {
1387 while( num < size && receiveInfoIt != _receivedInfoMap.end()) {
1388 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
1389 if (receiveInfo == NULL) {
1390 return 0;
1391 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001392 for (uint32_t i = 0;
hta@webrtc.org54536bb2012-05-03 14:07:23 +00001393 (num < size) && (i < receiveInfo->TmmbrSet.lengthOfSet()); i++) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001394 if (receiveInfo->GetTMMBRSet(i, num, candidateSet,
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +00001395 _clock->TimeInMilliseconds()) == 0) {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001396 num++;
1397 }
1398 }
1399 receiveInfoIt++;
1400 }
1401 } else {
1402 while (receiveInfoIt != _receivedInfoMap.end()) {
1403 RTCPReceiveInformation* receiveInfo = receiveInfoIt->second;
1404 if(receiveInfo == NULL) {
1405 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
1406 "%s failed to get RTCPReceiveInformation",
1407 __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 return -1;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001409 }
hta@webrtc.org54536bb2012-05-03 14:07:23 +00001410 num += receiveInfo->TmmbrSet.lengthOfSet();
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001411 receiveInfoIt++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001412 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001413 }
1414 return num;
niklase@google.com470e71d2011-07-07 08:21:25 +00001415}
1416
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001417int32_t
1418RTCPReceiver::SetPacketTimeout(const uint32_t timeoutMS)
niklase@google.com470e71d2011-07-07 08:21:25 +00001419{
1420 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1421 _packetTimeOutMS = timeoutMS;
1422 return 0;
1423}
1424
1425void RTCPReceiver::PacketTimeout()
1426{
1427 if(_packetTimeOutMS == 0)
1428 {
1429 // not configured
1430 return;
1431 }
1432
1433 bool packetTimeOut = false;
1434 {
1435 CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
1436 if(_lastReceived == 0)
1437 {
1438 // not active
1439 return;
1440 }
1441
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001442 int64_t now = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +00001443 if(now - _lastReceived > _packetTimeOutMS)
1444 {
1445 packetTimeOut = true;
1446 _lastReceived = 0; // only one callback
1447 }
1448 }
1449 CriticalSectionScoped lock(_criticalSectionFeedbacks);
1450 if(packetTimeOut && _cbRtcpFeedback)
1451 {
1452 _cbRtcpFeedback->OnRTCPPacketTimeout(_id);
1453 }
1454}
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +00001455
niklase@google.com470e71d2011-07-07 08:21:25 +00001456} // namespace webrtc