Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtcp_sender.cc
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
12
13 #include <assert.h>  // assert
14 #include <stdlib.h>  // rand
15 #include <string.h>  // memcpy
16
17 #include <algorithm>  // min
18
19 #include "webrtc/common_types.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
21 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
22 #include "webrtc/system_wrappers/interface/logging.h"
23 #include "webrtc/system_wrappers/interface/trace_event.h"
24
25 namespace webrtc {
26
27 using RTCPUtility::RTCPCnameInformation;
28
29 NACKStringBuilder::NACKStringBuilder() :
30     _stream(""), _count(0), _consecutive(false)
31 {
32     // Empty.
33 }
34
35 NACKStringBuilder::~NACKStringBuilder() {}
36
37 void NACKStringBuilder::PushNACK(uint16_t nack)
38 {
39     if (_count == 0)
40     {
41         _stream << nack;
42     } else if (nack == _prevNack + 1)
43     {
44         _consecutive = true;
45     } else
46     {
47         if (_consecutive)
48         {
49             _stream << "-" << _prevNack;
50             _consecutive = false;
51         }
52         _stream << "," << nack;
53     }
54     _count++;
55     _prevNack = nack;
56 }
57
58 std::string NACKStringBuilder::GetResult()
59 {
60     if (_consecutive)
61     {
62         _stream << "-" << _prevNack;
63         _consecutive = false;
64     }
65     return _stream.str();
66 }
67
68 RTCPSender::FeedbackState::FeedbackState()
69     : send_payload_type(0),
70       frequency_hz(0),
71       packets_sent(0),
72       media_bytes_sent(0),
73       send_bitrate(0),
74       last_rr_ntp_secs(0),
75       last_rr_ntp_frac(0),
76       remote_sr(0),
77       has_last_xr_rr(false) {}
78
79 RTCPSender::RTCPSender(const int32_t id,
80                        const bool audio,
81                        Clock* clock,
82                        ReceiveStatistics* receive_statistics) :
83     _id(id),
84     _audio(audio),
85     _clock(clock),
86     _method(kRtcpOff),
87     _criticalSectionTransport(CriticalSectionWrapper::CreateCriticalSection()),
88     _cbTransport(NULL),
89
90     _criticalSectionRTCPSender(CriticalSectionWrapper::CreateCriticalSection()),
91     _usingNack(false),
92     _sending(false),
93     _sendTMMBN(false),
94     _REMB(false),
95     _sendREMB(false),
96     _TMMBR(false),
97     _IJ(false),
98     _nextTimeToSendRTCP(0),
99     start_timestamp_(0),
100     last_rtp_timestamp_(0),
101     last_frame_capture_time_ms_(-1),
102     _SSRC(0),
103     _remoteSSRC(0),
104     _CNAME(),
105     receive_statistics_(receive_statistics),
106     internal_report_blocks_(),
107     external_report_blocks_(),
108     _csrcCNAMEs(),
109
110     _cameraDelayMS(0),
111
112     _lastSendReport(),
113     _lastRTCPTime(),
114
115     last_xr_rr_(),
116
117     _CSRCs(0),
118     _CSRC(),
119     _includeCSRCs(true),
120
121     _sequenceNumberFIR(0),
122
123     _lengthRembSSRC(0),
124     _sizeRembSSRC(0),
125     _rembSSRC(NULL),
126     _rembBitrate(0),
127
128     _tmmbrHelp(),
129     _tmmbr_Send(0),
130     _packetOH_Send(0),
131
132     _appSend(false),
133     _appSubType(0),
134     _appName(),
135     _appData(NULL),
136     _appLength(0),
137
138     xrSendReceiverReferenceTimeEnabled_(false),
139     _xrSendVoIPMetric(false),
140     _xrVoIPMetric()
141 {
142     memset(_CNAME, 0, sizeof(_CNAME));
143     memset(_lastSendReport, 0, sizeof(_lastSendReport));
144     memset(_lastRTCPTime, 0, sizeof(_lastRTCPTime));
145 }
146
147 RTCPSender::~RTCPSender() {
148   delete [] _rembSSRC;
149   delete [] _appData;
150
151   while (!internal_report_blocks_.empty()) {
152     delete internal_report_blocks_.begin()->second;
153     internal_report_blocks_.erase(internal_report_blocks_.begin());
154   }
155   while (!external_report_blocks_.empty()) {
156     std::map<uint32_t, RTCPReportBlock*>::iterator it =
157         external_report_blocks_.begin();
158     delete it->second;
159     external_report_blocks_.erase(it);
160   }
161   while (!_csrcCNAMEs.empty()) {
162     std::map<uint32_t, RTCPCnameInformation*>::iterator it =
163         _csrcCNAMEs.begin();
164     delete it->second;
165     _csrcCNAMEs.erase(it);
166   }
167   delete _criticalSectionTransport;
168   delete _criticalSectionRTCPSender;
169 }
170
171 int32_t
172 RTCPSender::RegisterSendTransport(Transport* outgoingTransport)
173 {
174     CriticalSectionScoped lock(_criticalSectionTransport);
175     _cbTransport = outgoingTransport;
176     return 0;
177 }
178
179 RTCPMethod
180 RTCPSender::Status() const
181 {
182     CriticalSectionScoped lock(_criticalSectionRTCPSender);
183     return _method;
184 }
185
186 int32_t
187 RTCPSender::SetRTCPStatus(const RTCPMethod method)
188 {
189     CriticalSectionScoped lock(_criticalSectionRTCPSender);
190     if(method != kRtcpOff)
191     {
192         if(_audio)
193         {
194             _nextTimeToSendRTCP = _clock->TimeInMilliseconds() +
195                 (RTCP_INTERVAL_AUDIO_MS/2);
196         } else
197         {
198             _nextTimeToSendRTCP = _clock->TimeInMilliseconds() +
199                 (RTCP_INTERVAL_VIDEO_MS/2);
200         }
201     }
202     _method = method;
203     return 0;
204 }
205
206 bool
207 RTCPSender::Sending() const
208 {
209     CriticalSectionScoped lock(_criticalSectionRTCPSender);
210     return _sending;
211 }
212
213 int32_t
214 RTCPSender::SetSendingStatus(const FeedbackState& feedback_state, bool sending)
215 {
216     bool sendRTCPBye = false;
217     {
218         CriticalSectionScoped lock(_criticalSectionRTCPSender);
219
220         if(_method != kRtcpOff)
221         {
222             if(sending == false && _sending == true)
223             {
224                 // Trigger RTCP bye
225                 sendRTCPBye = true;
226             }
227         }
228         _sending = sending;
229     }
230     if(sendRTCPBye)
231     {
232         return SendRTCP(feedback_state, kRtcpBye);
233     }
234     return 0;
235 }
236
237 bool
238 RTCPSender::REMB() const
239 {
240     CriticalSectionScoped lock(_criticalSectionRTCPSender);
241     return _REMB;
242 }
243
244 int32_t
245 RTCPSender::SetREMBStatus(const bool enable)
246 {
247     CriticalSectionScoped lock(_criticalSectionRTCPSender);
248     _REMB = enable;
249     return 0;
250 }
251
252 int32_t
253 RTCPSender::SetREMBData(const uint32_t bitrate,
254                         const uint8_t numberOfSSRC,
255                         const uint32_t* SSRC)
256 {
257     CriticalSectionScoped lock(_criticalSectionRTCPSender);
258     _rembBitrate = bitrate;
259
260     if(_sizeRembSSRC < numberOfSSRC)
261     {
262         delete [] _rembSSRC;
263         _rembSSRC = new uint32_t[numberOfSSRC];
264         _sizeRembSSRC = numberOfSSRC;
265     }
266
267     _lengthRembSSRC = numberOfSSRC;
268     for (int i = 0; i < numberOfSSRC; i++)
269     {
270         _rembSSRC[i] = SSRC[i];
271     }
272     _sendREMB = true;
273     // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
274     // throttled by the caller.
275     _nextTimeToSendRTCP = _clock->TimeInMilliseconds();
276     return 0;
277 }
278
279 bool
280 RTCPSender::TMMBR() const
281 {
282     CriticalSectionScoped lock(_criticalSectionRTCPSender);
283     return _TMMBR;
284 }
285
286 int32_t
287 RTCPSender::SetTMMBRStatus(const bool enable)
288 {
289     CriticalSectionScoped lock(_criticalSectionRTCPSender);
290     _TMMBR = enable;
291     return 0;
292 }
293
294 bool
295 RTCPSender::IJ() const
296 {
297     CriticalSectionScoped lock(_criticalSectionRTCPSender);
298     return _IJ;
299 }
300
301 int32_t
302 RTCPSender::SetIJStatus(const bool enable)
303 {
304     CriticalSectionScoped lock(_criticalSectionRTCPSender);
305     _IJ = enable;
306     return 0;
307 }
308
309 void RTCPSender::SetStartTimestamp(uint32_t start_timestamp) {
310   CriticalSectionScoped lock(_criticalSectionRTCPSender);
311   start_timestamp_ = start_timestamp;
312 }
313
314 void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
315                                 int64_t capture_time_ms) {
316   CriticalSectionScoped lock(_criticalSectionRTCPSender);
317   last_rtp_timestamp_ = rtp_timestamp;
318   if (capture_time_ms < 0) {
319     // We don't currently get a capture time from VoiceEngine.
320     last_frame_capture_time_ms_ = _clock->TimeInMilliseconds();
321   } else {
322     last_frame_capture_time_ms_ = capture_time_ms;
323   }
324 }
325
326 void
327 RTCPSender::SetSSRC( const uint32_t ssrc)
328 {
329     CriticalSectionScoped lock(_criticalSectionRTCPSender);
330
331     if(_SSRC != 0)
332     {
333         // not first SetSSRC, probably due to a collision
334         // schedule a new RTCP report
335         // make sure that we send a RTP packet
336         _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + 100;
337     }
338     _SSRC = ssrc;
339 }
340
341 void RTCPSender::SetRemoteSSRC(uint32_t ssrc)
342 {
343     CriticalSectionScoped lock(_criticalSectionRTCPSender);
344     _remoteSSRC = ssrc;
345 }
346
347 int32_t
348 RTCPSender::SetCameraDelay(const int32_t delayMS)
349 {
350     CriticalSectionScoped lock(_criticalSectionRTCPSender);
351     if(delayMS > 1000 || delayMS < -1000)
352     {
353         LOG(LS_WARNING) << "Delay can't be larger than 1 second: "
354                         << delayMS << " ms";
355         return -1;
356     }
357     _cameraDelayMS = delayMS;
358     return 0;
359 }
360
361 int32_t RTCPSender::SetCNAME(const char cName[RTCP_CNAME_SIZE]) {
362   if (!cName)
363     return -1;
364
365   CriticalSectionScoped lock(_criticalSectionRTCPSender);
366   _CNAME[RTCP_CNAME_SIZE - 1] = 0;
367   strncpy(_CNAME, cName, RTCP_CNAME_SIZE - 1);
368   return 0;
369 }
370
371 int32_t RTCPSender::AddMixedCNAME(const uint32_t SSRC,
372                                   const char cName[RTCP_CNAME_SIZE]) {
373   assert(cName);
374   CriticalSectionScoped lock(_criticalSectionRTCPSender);
375   if (_csrcCNAMEs.size() >= kRtpCsrcSize) {
376     return -1;
377   }
378   RTCPCnameInformation* ptr = new RTCPCnameInformation();
379   ptr->name[RTCP_CNAME_SIZE - 1] = 0;
380   strncpy(ptr->name, cName, RTCP_CNAME_SIZE - 1);
381   _csrcCNAMEs[SSRC] = ptr;
382   return 0;
383 }
384
385 int32_t RTCPSender::RemoveMixedCNAME(const uint32_t SSRC) {
386   CriticalSectionScoped lock(_criticalSectionRTCPSender);
387   std::map<uint32_t, RTCPCnameInformation*>::iterator it =
388       _csrcCNAMEs.find(SSRC);
389
390   if (it == _csrcCNAMEs.end()) {
391     return -1;
392   }
393   delete it->second;
394   _csrcCNAMEs.erase(it);
395   return 0;
396 }
397
398 bool
399 RTCPSender::TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP) const
400 {
401 /*
402     For audio we use a fix 5 sec interval
403
404     For video we use 1 sec interval fo a BW smaller than 360 kbit/s,
405         technicaly we break the max 5% RTCP BW for video below 10 kbit/s but
406         that should be extremely rare
407
408
409 From RFC 3550
410
411     MAX RTCP BW is 5% if the session BW
412         A send report is approximately 65 bytes inc CNAME
413         A receiver report is approximately 28 bytes
414
415     The RECOMMENDED value for the reduced minimum in seconds is 360
416       divided by the session bandwidth in kilobits/second.  This minimum
417       is smaller than 5 seconds for bandwidths greater than 72 kb/s.
418
419     If the participant has not yet sent an RTCP packet (the variable
420       initial is true), the constant Tmin is set to 2.5 seconds, else it
421       is set to 5 seconds.
422
423     The interval between RTCP packets is varied randomly over the
424       range [0.5,1.5] times the calculated interval to avoid unintended
425       synchronization of all participants
426
427     if we send
428     If the participant is a sender (we_sent true), the constant C is
429       set to the average RTCP packet size (avg_rtcp_size) divided by 25%
430       of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
431       number of senders.
432
433     if we receive only
434       If we_sent is not true, the constant C is set
435       to the average RTCP packet size divided by 75% of the RTCP
436       bandwidth.  The constant n is set to the number of receivers
437       (members - senders).  If the number of senders is greater than
438       25%, senders and receivers are treated together.
439
440     reconsideration NOT required for peer-to-peer
441       "timer reconsideration" is
442       employed.  This algorithm implements a simple back-off mechanism
443       which causes users to hold back RTCP packet transmission if the
444       group sizes are increasing.
445
446       n = number of members
447       C = avg_size/(rtcpBW/4)
448
449    3. The deterministic calculated interval Td is set to max(Tmin, n*C).
450
451    4. The calculated interval T is set to a number uniformly distributed
452       between 0.5 and 1.5 times the deterministic calculated interval.
453
454    5. The resulting value of T is divided by e-3/2=1.21828 to compensate
455       for the fact that the timer reconsideration algorithm converges to
456       a value of the RTCP bandwidth below the intended average
457 */
458
459     int64_t now = _clock->TimeInMilliseconds();
460
461     CriticalSectionScoped lock(_criticalSectionRTCPSender);
462
463     if(_method == kRtcpOff)
464     {
465         return false;
466     }
467
468     if(!_audio && sendKeyframeBeforeRTP)
469     {
470         // for video key-frames we want to send the RTCP before the large key-frame
471         // if we have a 100 ms margin
472         now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
473     }
474
475     if(now >= _nextTimeToSendRTCP)
476     {
477         return true;
478
479     } else if(now < 0x0000ffff && _nextTimeToSendRTCP > 0xffff0000) // 65 sec margin
480     {
481         // wrap
482         return true;
483     }
484     return false;
485 }
486
487 uint32_t
488 RTCPSender::LastSendReport( uint32_t& lastRTCPTime)
489 {
490     CriticalSectionScoped lock(_criticalSectionRTCPSender);
491
492     lastRTCPTime = _lastRTCPTime[0];
493     return _lastSendReport[0];
494 }
495
496 uint32_t
497 RTCPSender::SendTimeOfSendReport(const uint32_t sendReport)
498 {
499     CriticalSectionScoped lock(_criticalSectionRTCPSender);
500
501     // This is only saved when we are the sender
502     if((_lastSendReport[0] == 0) || (sendReport == 0))
503     {
504         return 0; // will be ignored
505     } else
506     {
507         for(int i = 0; i < RTCP_NUMBER_OF_SR; ++i)
508         {
509             if( _lastSendReport[i] == sendReport)
510             {
511                 return _lastRTCPTime[i];
512             }
513         }
514     }
515     return 0;
516 }
517
518 bool RTCPSender::SendTimeOfXrRrReport(uint32_t mid_ntp,
519                                       int64_t* time_ms) const {
520   CriticalSectionScoped lock(_criticalSectionRTCPSender);
521
522   if (last_xr_rr_.empty()) {
523     return false;
524   }
525   std::map<uint32_t, int64_t>::const_iterator it = last_xr_rr_.find(mid_ntp);
526   if (it == last_xr_rr_.end()) {
527     return false;
528   }
529   *time_ms = it->second;
530   return true;
531 }
532
533 void RTCPSender::GetPacketTypeCounter(
534     RtcpPacketTypeCounter* packet_counter) const {
535   CriticalSectionScoped lock(_criticalSectionRTCPSender);
536   *packet_counter = packet_type_counter_;
537 }
538
539 int32_t RTCPSender::AddExternalReportBlock(
540     uint32_t SSRC,
541     const RTCPReportBlock* reportBlock) {
542   CriticalSectionScoped lock(_criticalSectionRTCPSender);
543   return AddReportBlock(SSRC, &external_report_blocks_, reportBlock);
544 }
545
546 int32_t RTCPSender::AddReportBlock(
547     uint32_t SSRC,
548     std::map<uint32_t, RTCPReportBlock*>* report_blocks,
549     const RTCPReportBlock* reportBlock) {
550   assert(reportBlock);
551
552   if (report_blocks->size() >= RTCP_MAX_REPORT_BLOCKS) {
553     LOG(LS_WARNING) << "Too many report blocks.";
554     return -1;
555   }
556   std::map<uint32_t, RTCPReportBlock*>::iterator it =
557       report_blocks->find(SSRC);
558   if (it != report_blocks->end()) {
559     delete it->second;
560     report_blocks->erase(it);
561   }
562   RTCPReportBlock* copyReportBlock = new RTCPReportBlock();
563   memcpy(copyReportBlock, reportBlock, sizeof(RTCPReportBlock));
564   (*report_blocks)[SSRC] = copyReportBlock;
565   return 0;
566 }
567
568 int32_t RTCPSender::RemoveExternalReportBlock(uint32_t SSRC) {
569   CriticalSectionScoped lock(_criticalSectionRTCPSender);
570
571   std::map<uint32_t, RTCPReportBlock*>::iterator it =
572       external_report_blocks_.find(SSRC);
573
574   if (it == external_report_blocks_.end()) {
575     return -1;
576   }
577   delete it->second;
578   external_report_blocks_.erase(it);
579   return 0;
580 }
581
582 int32_t RTCPSender::BuildSR(const FeedbackState& feedback_state,
583                             uint8_t* rtcpbuffer,
584                             int& pos,
585                             uint32_t NTPsec,
586                             uint32_t NTPfrac)
587 {
588     // sanity
589     if(pos + 52 >= IP_PACKET_SIZE)
590     {
591         LOG(LS_WARNING) << "Failed to build Sender Report.";
592         return -2;
593     }
594     uint32_t RTPtime;
595
596     uint32_t posNumberOfReportBlocks = pos;
597     rtcpbuffer[pos++]=(uint8_t)0x80;
598
599     // Sender report
600     rtcpbuffer[pos++]=(uint8_t)200;
601
602     for(int i = (RTCP_NUMBER_OF_SR-2); i >= 0; i--)
603     {
604         // shift old
605         _lastSendReport[i+1] = _lastSendReport[i];
606         _lastRTCPTime[i+1] =_lastRTCPTime[i];
607     }
608
609     _lastRTCPTime[0] = Clock::NtpToMs(NTPsec, NTPfrac);
610     _lastSendReport[0] = (NTPsec << 16) + (NTPfrac >> 16);
611
612     // The timestamp of this RTCP packet should be estimated as the timestamp of
613     // the frame being captured at this moment. We are calculating that
614     // timestamp as the last frame's timestamp + the time since the last frame
615     // was captured.
616     RTPtime = start_timestamp_ + last_rtp_timestamp_ +
617               (_clock->TimeInMilliseconds() - last_frame_capture_time_ms_) *
618                   (feedback_state.frequency_hz / 1000);
619
620     // Add sender data
621     // Save  for our length field
622     pos++;
623     pos++;
624
625     // Add our own SSRC
626     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
627     pos += 4;
628     // NTP
629     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, NTPsec);
630     pos += 4;
631     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, NTPfrac);
632     pos += 4;
633     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, RTPtime);
634     pos += 4;
635
636     //sender's packet count
637     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos,
638                                       feedback_state.packets_sent);
639     pos += 4;
640
641     //sender's octet count
642     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos,
643                                       feedback_state.media_bytes_sent);
644     pos += 4;
645
646     uint8_t numberOfReportBlocks = 0;
647     int32_t retVal = WriteAllReportBlocksToBuffer(rtcpbuffer, pos,
648                                                   numberOfReportBlocks,
649                                                   NTPsec, NTPfrac);
650     if(retVal < 0)
651     {
652         //
653         return retVal ;
654     }
655     pos = retVal;
656     rtcpbuffer[posNumberOfReportBlocks] += numberOfReportBlocks;
657
658     uint16_t len = uint16_t((pos/4) -1);
659     RtpUtility::AssignUWord16ToBuffer(rtcpbuffer + 2, len);
660     return 0;
661 }
662
663
664 int32_t RTCPSender::BuildSDEC(uint8_t* rtcpbuffer, int& pos) {
665   size_t lengthCname = strlen(_CNAME);
666   assert(lengthCname < RTCP_CNAME_SIZE);
667
668   // sanity
669   if(pos + 12 + lengthCname  >= IP_PACKET_SIZE) {
670     LOG(LS_WARNING) << "Failed to build SDEC.";
671     return -2;
672   }
673   // SDEC Source Description
674
675   // We always need to add SDES CNAME
676   rtcpbuffer[pos++] = static_cast<uint8_t>(0x80 + 1 + _csrcCNAMEs.size());
677   rtcpbuffer[pos++] = static_cast<uint8_t>(202);
678
679   // handle SDES length later on
680   uint32_t SDESLengthPos = pos;
681   pos++;
682   pos++;
683
684   // Add our own SSRC
685   RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
686   pos += 4;
687
688   // CNAME = 1
689   rtcpbuffer[pos++] = static_cast<uint8_t>(1);
690
691   //
692   rtcpbuffer[pos++] = static_cast<uint8_t>(lengthCname);
693
694   uint16_t SDESLength = 10;
695
696   memcpy(&rtcpbuffer[pos], _CNAME, lengthCname);
697   pos += lengthCname;
698   SDESLength += (uint16_t)lengthCname;
699
700   uint16_t padding = 0;
701   // We must have a zero field even if we have an even multiple of 4 bytes
702   if ((pos % 4) == 0) {
703     padding++;
704     rtcpbuffer[pos++]=0;
705   }
706   while ((pos % 4) != 0) {
707     padding++;
708     rtcpbuffer[pos++]=0;
709   }
710   SDESLength += padding;
711
712   std::map<uint32_t, RTCPUtility::RTCPCnameInformation*>::iterator it =
713       _csrcCNAMEs.begin();
714
715   for(; it != _csrcCNAMEs.end(); it++) {
716     RTCPCnameInformation* cname = it->second;
717     uint32_t SSRC = it->first;
718
719     // Add SSRC
720     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, SSRC);
721     pos += 4;
722
723     // CNAME = 1
724     rtcpbuffer[pos++] = static_cast<uint8_t>(1);
725
726     size_t length = strlen(cname->name);
727     assert(length < RTCP_CNAME_SIZE);
728
729     rtcpbuffer[pos++]= static_cast<uint8_t>(length);
730     SDESLength += 6;
731
732     memcpy(&rtcpbuffer[pos],cname->name, length);
733
734     pos += length;
735     SDESLength += length;
736     uint16_t padding = 0;
737
738     // We must have a zero field even if we have an even multiple of 4 bytes
739     if((pos % 4) == 0){
740       padding++;
741       rtcpbuffer[pos++]=0;
742     }
743     while((pos % 4) != 0){
744       padding++;
745       rtcpbuffer[pos++] = 0;
746     }
747     SDESLength += padding;
748   }
749   // in 32-bit words minus one and we don't count the header
750   uint16_t buffer_length = (SDESLength / 4) - 1;
751   RtpUtility::AssignUWord16ToBuffer(rtcpbuffer + SDESLengthPos, buffer_length);
752   return 0;
753 }
754
755 int32_t
756 RTCPSender::BuildRR(uint8_t* rtcpbuffer,
757                     int& pos,
758                     const uint32_t NTPsec,
759                     const uint32_t NTPfrac)
760 {
761     // sanity one block
762     if(pos + 32 >= IP_PACKET_SIZE)
763     {
764         return -2;
765     }
766     uint32_t posNumberOfReportBlocks = pos;
767
768     rtcpbuffer[pos++]=(uint8_t)0x80;
769     rtcpbuffer[pos++]=(uint8_t)201;
770
771     // Save  for our length field
772     pos++;
773     pos++;
774
775     // Add our own SSRC
776     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
777     pos += 4;
778
779     uint8_t numberOfReportBlocks = 0;
780     int retVal = WriteAllReportBlocksToBuffer(rtcpbuffer, pos,
781                                               numberOfReportBlocks,
782                                               NTPsec, NTPfrac);
783     if(retVal < 0)
784     {
785         return pos;
786     }
787     pos = retVal;
788     rtcpbuffer[posNumberOfReportBlocks] += numberOfReportBlocks;
789
790     uint16_t len = uint16_t((pos)/4 -1);
791     RtpUtility::AssignUWord16ToBuffer(rtcpbuffer + 2, len);
792     return 0;
793 }
794
795 // From RFC 5450: Transmission Time Offsets in RTP Streams.
796 //        0                   1                   2                   3
797 //        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
798 //       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
799 //   hdr |V=2|P|    RC   |   PT=IJ=195   |             length            |
800 //       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
801 //       |                      inter-arrival jitter                     |
802 //       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
803 //       .                                                               .
804 //       .                                                               .
805 //       .                                                               .
806 //       |                      inter-arrival jitter                     |
807 //       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
808 //
809 //  If present, this RTCP packet must be placed after a receiver report
810 //  (inside a compound RTCP packet), and MUST have the same value for RC
811 //  (reception report count) as the receiver report.
812
813 int32_t
814 RTCPSender::BuildExtendedJitterReport(
815     uint8_t* rtcpbuffer,
816     int& pos,
817     const uint32_t jitterTransmissionTimeOffset)
818 {
819     if (external_report_blocks_.size() > 0)
820     {
821         // TODO(andresp): Remove external report blocks since they are not
822         // supported.
823         LOG(LS_ERROR) << "Handling of external report blocks not implemented.";
824         return 0;
825     }
826
827     // sanity
828     if(pos + 8 >= IP_PACKET_SIZE)
829     {
830         return -2;
831     }
832     // add picture loss indicator
833     uint8_t RC = 1;
834     rtcpbuffer[pos++]=(uint8_t)0x80 + RC;
835     rtcpbuffer[pos++]=(uint8_t)195;
836
837     // Used fixed length of 2
838     rtcpbuffer[pos++]=(uint8_t)0;
839     rtcpbuffer[pos++]=(uint8_t)(1);
840
841     // Add inter-arrival jitter
842     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos,
843                                       jitterTransmissionTimeOffset);
844     pos += 4;
845     return 0;
846 }
847
848 int32_t
849 RTCPSender::BuildPLI(uint8_t* rtcpbuffer, int& pos)
850 {
851     // sanity
852     if(pos + 12 >= IP_PACKET_SIZE)
853     {
854         return -2;
855     }
856     // add picture loss indicator
857     uint8_t FMT = 1;
858     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
859     rtcpbuffer[pos++]=(uint8_t)206;
860
861     //Used fixed length of 2
862     rtcpbuffer[pos++]=(uint8_t)0;
863     rtcpbuffer[pos++]=(uint8_t)(2);
864
865     // Add our own SSRC
866     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
867     pos += 4;
868
869     // Add the remote SSRC
870     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
871     pos += 4;
872     return 0;
873 }
874
875 int32_t RTCPSender::BuildFIR(uint8_t* rtcpbuffer,
876                              int& pos,
877                              bool repeat) {
878   // sanity
879   if(pos + 20 >= IP_PACKET_SIZE)  {
880     return -2;
881   }
882   if (!repeat) {
883     _sequenceNumberFIR++;   // do not increase if repetition
884   }
885
886   // add full intra request indicator
887   uint8_t FMT = 4;
888   rtcpbuffer[pos++] = (uint8_t)0x80 + FMT;
889   rtcpbuffer[pos++] = (uint8_t)206;
890
891   //Length of 4
892   rtcpbuffer[pos++] = (uint8_t)0;
893   rtcpbuffer[pos++] = (uint8_t)(4);
894
895   // Add our own SSRC
896   RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
897   pos += 4;
898
899   // RFC 5104     4.3.1.2.  Semantics
900   // SSRC of media source
901   rtcpbuffer[pos++] = (uint8_t)0;
902   rtcpbuffer[pos++] = (uint8_t)0;
903   rtcpbuffer[pos++] = (uint8_t)0;
904   rtcpbuffer[pos++] = (uint8_t)0;
905
906   // Additional Feedback Control Information (FCI)
907   RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
908   pos += 4;
909
910   rtcpbuffer[pos++] = (uint8_t)(_sequenceNumberFIR);
911   rtcpbuffer[pos++] = (uint8_t)0;
912   rtcpbuffer[pos++] = (uint8_t)0;
913   rtcpbuffer[pos++] = (uint8_t)0;
914   return 0;
915 }
916
917 /*
918     0                   1                   2                   3
919     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
920    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
921    |            First        |        Number           | PictureID |
922    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
923 */
924 int32_t
925 RTCPSender::BuildSLI(uint8_t* rtcpbuffer, int& pos, const uint8_t pictureID)
926 {
927     // sanity
928     if(pos + 16 >= IP_PACKET_SIZE)
929     {
930         return -2;
931     }
932     // add slice loss indicator
933     uint8_t FMT = 2;
934     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
935     rtcpbuffer[pos++]=(uint8_t)206;
936
937     //Used fixed length of 3
938     rtcpbuffer[pos++]=(uint8_t)0;
939     rtcpbuffer[pos++]=(uint8_t)(3);
940
941     // Add our own SSRC
942     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
943     pos += 4;
944
945     // Add the remote SSRC
946     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
947     pos += 4;
948
949     // Add first, number & picture ID 6 bits
950     // first  = 0, 13 - bits
951     // number = 0x1fff, 13 - bits only ones for now
952     uint32_t sliField = (0x1fff << 6)+ (0x3f & pictureID);
953     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, sliField);
954     pos += 4;
955     return 0;
956 }
957
958 /*
959     0                   1                   2                   3
960     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
961    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
962    |      PB       |0| Payload Type|    Native RPSI bit string     |
963    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
964    |   defined per codec          ...                | Padding (0) |
965    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
966 */
967 /*
968 *    Note: not generic made for VP8
969 */
970 int32_t
971 RTCPSender::BuildRPSI(uint8_t* rtcpbuffer,
972                      int& pos,
973                      const uint64_t pictureID,
974                      const uint8_t payloadType)
975 {
976     // sanity
977     if(pos + 24 >= IP_PACKET_SIZE)
978     {
979         return -2;
980     }
981     // add Reference Picture Selection Indication
982     uint8_t FMT = 3;
983     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
984     rtcpbuffer[pos++]=(uint8_t)206;
985
986     // calc length
987     uint32_t bitsRequired = 7;
988     uint8_t bytesRequired = 1;
989     while((pictureID>>bitsRequired) > 0)
990     {
991         bitsRequired += 7;
992         bytesRequired++;
993     }
994
995     uint8_t size = 3;
996     if(bytesRequired > 6)
997     {
998         size = 5;
999     } else if(bytesRequired > 2)
1000     {
1001         size = 4;
1002     }
1003     rtcpbuffer[pos++]=(uint8_t)0;
1004     rtcpbuffer[pos++]=size;
1005
1006     // Add our own SSRC
1007     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1008     pos += 4;
1009
1010     // Add the remote SSRC
1011     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
1012     pos += 4;
1013
1014     // calc padding length
1015     uint8_t paddingBytes = 4-((2+bytesRequired)%4);
1016     if(paddingBytes == 4)
1017     {
1018         paddingBytes = 0;
1019     }
1020     // add padding length in bits
1021     rtcpbuffer[pos] = paddingBytes*8; // padding can be 0, 8, 16 or 24
1022     pos++;
1023
1024     // add payload type
1025     rtcpbuffer[pos] = payloadType;
1026     pos++;
1027
1028     // add picture ID
1029     for(int i = bytesRequired-1; i > 0; i--)
1030     {
1031         rtcpbuffer[pos] = 0x80 | uint8_t(pictureID >> (i*7));
1032         pos++;
1033     }
1034     // add last byte of picture ID
1035     rtcpbuffer[pos] = uint8_t(pictureID & 0x7f);
1036     pos++;
1037
1038     // add padding
1039     for(int j = 0; j <paddingBytes; j++)
1040     {
1041         rtcpbuffer[pos] = 0;
1042         pos++;
1043     }
1044     return 0;
1045 }
1046
1047 int32_t
1048 RTCPSender::BuildREMB(uint8_t* rtcpbuffer, int& pos)
1049 {
1050     // sanity
1051     if(pos + 20 + 4 * _lengthRembSSRC >= IP_PACKET_SIZE)
1052     {
1053         return -2;
1054     }
1055     // add application layer feedback
1056     uint8_t FMT = 15;
1057     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
1058     rtcpbuffer[pos++]=(uint8_t)206;
1059
1060     rtcpbuffer[pos++]=(uint8_t)0;
1061     rtcpbuffer[pos++]=_lengthRembSSRC + 4;
1062
1063     // Add our own SSRC
1064     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1065     pos += 4;
1066
1067     // Remote SSRC must be 0
1068     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, 0);
1069     pos += 4;
1070
1071     rtcpbuffer[pos++]='R';
1072     rtcpbuffer[pos++]='E';
1073     rtcpbuffer[pos++]='M';
1074     rtcpbuffer[pos++]='B';
1075
1076     rtcpbuffer[pos++] = _lengthRembSSRC;
1077     // 6 bit Exp
1078     // 18 bit mantissa
1079     uint8_t brExp = 0;
1080     for(uint32_t i=0; i<64; i++)
1081     {
1082         if(_rembBitrate <= ((uint32_t)262143 << i))
1083         {
1084             brExp = i;
1085             break;
1086         }
1087     }
1088     const uint32_t brMantissa = (_rembBitrate >> brExp);
1089     rtcpbuffer[pos++]=(uint8_t)((brExp << 2) + ((brMantissa >> 16) & 0x03));
1090     rtcpbuffer[pos++]=(uint8_t)(brMantissa >> 8);
1091     rtcpbuffer[pos++]=(uint8_t)(brMantissa);
1092
1093     for (int i = 0; i < _lengthRembSSRC; i++)
1094     {
1095       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _rembSSRC[i]);
1096         pos += 4;
1097     }
1098     return 0;
1099 }
1100
1101 void
1102 RTCPSender::SetTargetBitrate(unsigned int target_bitrate)
1103 {
1104     CriticalSectionScoped lock(_criticalSectionRTCPSender);
1105     _tmmbr_Send = target_bitrate / 1000;
1106 }
1107
1108 int32_t RTCPSender::BuildTMMBR(ModuleRtpRtcpImpl* rtp_rtcp_module,
1109                                uint8_t* rtcpbuffer,
1110                                int& pos) {
1111     if (rtp_rtcp_module == NULL)
1112       return -1;
1113     // Before sending the TMMBR check the received TMMBN, only an owner is allowed to raise the bitrate
1114     // If the sender is an owner of the TMMBN -> send TMMBR
1115     // If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
1116
1117     // get current bounding set from RTCP receiver
1118     bool tmmbrOwner = false;
1119     // store in candidateSet, allocates one extra slot
1120     TMMBRSet* candidateSet = _tmmbrHelp.CandidateSet();
1121
1122     // holding _criticalSectionRTCPSender while calling RTCPreceiver which
1123     // will accuire _criticalSectionRTCPReceiver is a potental deadlock but
1124     // since RTCPreceiver is not doing the reverse we should be fine
1125     int32_t lengthOfBoundingSet =
1126         rtp_rtcp_module->BoundingSet(tmmbrOwner, candidateSet);
1127
1128     if(lengthOfBoundingSet > 0)
1129     {
1130         for (int32_t i = 0; i < lengthOfBoundingSet; i++)
1131         {
1132             if( candidateSet->Tmmbr(i) == _tmmbr_Send &&
1133                 candidateSet->PacketOH(i) == _packetOH_Send)
1134             {
1135                 // do not send the same tuple
1136                 return 0;
1137             }
1138         }
1139         if(!tmmbrOwner)
1140         {
1141             // use received bounding set as candidate set
1142             // add current tuple
1143             candidateSet->SetEntry(lengthOfBoundingSet,
1144                                    _tmmbr_Send,
1145                                    _packetOH_Send,
1146                                    _SSRC);
1147             int numCandidates = lengthOfBoundingSet+ 1;
1148
1149             // find bounding set
1150             TMMBRSet* boundingSet = NULL;
1151             int numBoundingSet = _tmmbrHelp.FindTMMBRBoundingSet(boundingSet);
1152             if(numBoundingSet > 0 || numBoundingSet <= numCandidates)
1153             {
1154                 tmmbrOwner = _tmmbrHelp.IsOwner(_SSRC, numBoundingSet);
1155             }
1156             if(!tmmbrOwner)
1157             {
1158                 // did not enter bounding set, no meaning to send this request
1159                 return 0;
1160             }
1161         }
1162     }
1163
1164     if(_tmmbr_Send)
1165     {
1166         // sanity
1167         if(pos + 20 >= IP_PACKET_SIZE)
1168         {
1169             return -2;
1170         }
1171         // add TMMBR indicator
1172         uint8_t FMT = 3;
1173         rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
1174         rtcpbuffer[pos++]=(uint8_t)205;
1175
1176         //Length of 4
1177         rtcpbuffer[pos++]=(uint8_t)0;
1178         rtcpbuffer[pos++]=(uint8_t)(4);
1179
1180         // Add our own SSRC
1181         RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1182         pos += 4;
1183
1184         // RFC 5104     4.2.1.2.  Semantics
1185
1186         // SSRC of media source
1187         rtcpbuffer[pos++]=(uint8_t)0;
1188         rtcpbuffer[pos++]=(uint8_t)0;
1189         rtcpbuffer[pos++]=(uint8_t)0;
1190         rtcpbuffer[pos++]=(uint8_t)0;
1191
1192         // Additional Feedback Control Information (FCI)
1193         RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
1194         pos += 4;
1195
1196         uint32_t bitRate = _tmmbr_Send*1000;
1197         uint32_t mmbrExp = 0;
1198         for(uint32_t i=0;i<64;i++)
1199         {
1200             if(bitRate <= ((uint32_t)131071 << i))
1201             {
1202                 mmbrExp = i;
1203                 break;
1204             }
1205         }
1206         uint32_t mmbrMantissa = (bitRate >> mmbrExp);
1207
1208         rtcpbuffer[pos++]=(uint8_t)((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03));
1209         rtcpbuffer[pos++]=(uint8_t)(mmbrMantissa >> 7);
1210         rtcpbuffer[pos++]=(uint8_t)((mmbrMantissa << 1) + ((_packetOH_Send >> 8)& 0x01));
1211         rtcpbuffer[pos++]=(uint8_t)(_packetOH_Send);
1212     }
1213     return 0;
1214 }
1215
1216 int32_t
1217 RTCPSender::BuildTMMBN(uint8_t* rtcpbuffer, int& pos)
1218 {
1219     TMMBRSet* boundingSet = _tmmbrHelp.BoundingSetToSend();
1220     if(boundingSet == NULL)
1221     {
1222         return -1;
1223     }
1224     // sanity
1225     if(pos + 12 + boundingSet->lengthOfSet()*8 >= IP_PACKET_SIZE)
1226     {
1227         LOG(LS_WARNING) << "Failed to build TMMBN.";
1228         return -2;
1229     }
1230     uint8_t FMT = 4;
1231     // add TMMBN indicator
1232     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
1233     rtcpbuffer[pos++]=(uint8_t)205;
1234
1235     //Add length later
1236     int posLength = pos;
1237     pos++;
1238     pos++;
1239
1240     // Add our own SSRC
1241     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1242     pos += 4;
1243
1244     // RFC 5104     4.2.2.2.  Semantics
1245
1246     // SSRC of media source
1247     rtcpbuffer[pos++]=(uint8_t)0;
1248     rtcpbuffer[pos++]=(uint8_t)0;
1249     rtcpbuffer[pos++]=(uint8_t)0;
1250     rtcpbuffer[pos++]=(uint8_t)0;
1251
1252     // Additional Feedback Control Information (FCI)
1253     int numBoundingSet = 0;
1254     for(uint32_t n=0; n< boundingSet->lengthOfSet(); n++)
1255     {
1256         if (boundingSet->Tmmbr(n) > 0)
1257         {
1258             uint32_t tmmbrSSRC = boundingSet->Ssrc(n);
1259             RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, tmmbrSSRC);
1260             pos += 4;
1261
1262             uint32_t bitRate = boundingSet->Tmmbr(n) * 1000;
1263             uint32_t mmbrExp = 0;
1264             for(int i=0; i<64; i++)
1265             {
1266                 if(bitRate <=  ((uint32_t)131071 << i))
1267                 {
1268                     mmbrExp = i;
1269                     break;
1270                 }
1271             }
1272             uint32_t mmbrMantissa = (bitRate >> mmbrExp);
1273             uint32_t measuredOH = boundingSet->PacketOH(n);
1274
1275             rtcpbuffer[pos++]=(uint8_t)((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03));
1276             rtcpbuffer[pos++]=(uint8_t)(mmbrMantissa >> 7);
1277             rtcpbuffer[pos++]=(uint8_t)((mmbrMantissa << 1) + ((measuredOH >> 8)& 0x01));
1278             rtcpbuffer[pos++]=(uint8_t)(measuredOH);
1279             numBoundingSet++;
1280         }
1281     }
1282     uint16_t length= (uint16_t)(2+2*numBoundingSet);
1283     rtcpbuffer[posLength++]=(uint8_t)(length>>8);
1284     rtcpbuffer[posLength]=(uint8_t)(length);
1285     return 0;
1286 }
1287
1288 int32_t
1289 RTCPSender::BuildAPP(uint8_t* rtcpbuffer, int& pos)
1290 {
1291     // sanity
1292     if(_appData == NULL)
1293     {
1294         LOG(LS_WARNING) << "Failed to build app specific.";
1295         return -1;
1296     }
1297     if(pos + 12 + _appLength >= IP_PACKET_SIZE)
1298     {
1299         LOG(LS_WARNING) << "Failed to build app specific.";
1300         return -2;
1301     }
1302     rtcpbuffer[pos++]=(uint8_t)0x80 + _appSubType;
1303
1304     // Add APP ID
1305     rtcpbuffer[pos++]=(uint8_t)204;
1306
1307     uint16_t length = (_appLength>>2) + 2; // include SSRC and name
1308     rtcpbuffer[pos++]=(uint8_t)(length>>8);
1309     rtcpbuffer[pos++]=(uint8_t)(length);
1310
1311     // Add our own SSRC
1312     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1313     pos += 4;
1314
1315     // Add our application name
1316     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _appName);
1317     pos += 4;
1318
1319     // Add the data
1320     memcpy(rtcpbuffer +pos, _appData,_appLength);
1321     pos += _appLength;
1322     return 0;
1323 }
1324
1325 int32_t
1326 RTCPSender::BuildNACK(uint8_t* rtcpbuffer,
1327                       int& pos,
1328                       const int32_t nackSize,
1329                       const uint16_t* nackList,
1330                       std::string* nackString)
1331 {
1332     // sanity
1333     if(pos + 16 >= IP_PACKET_SIZE)
1334     {
1335         LOG(LS_WARNING) << "Failed to build NACK.";
1336         return -2;
1337     }
1338
1339     // int size, uint16_t* nackList
1340     // add nack list
1341     uint8_t FMT = 1;
1342     rtcpbuffer[pos++]=(uint8_t)0x80 + FMT;
1343     rtcpbuffer[pos++]=(uint8_t)205;
1344
1345     rtcpbuffer[pos++]=(uint8_t) 0;
1346     int nackSizePos = pos;
1347     rtcpbuffer[pos++]=(uint8_t)(3); //setting it to one kNACK signal as default
1348
1349     // Add our own SSRC
1350     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1351     pos += 4;
1352
1353     // Add the remote SSRC
1354     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
1355     pos += 4;
1356
1357     // Build NACK bitmasks and write them to the RTCP message.
1358     // The nack list should be sorted and not contain duplicates if one
1359     // wants to build the smallest rtcp nack packet.
1360     int numOfNackFields = 0;
1361     int maxNackFields = std::min<int>(kRtcpMaxNackFields,
1362                                       (IP_PACKET_SIZE - pos) / 4);
1363     int i = 0;
1364     while (i < nackSize && numOfNackFields < maxNackFields) {
1365       uint16_t nack = nackList[i++];
1366       uint16_t bitmask = 0;
1367       while (i < nackSize) {
1368         int shift = static_cast<uint16_t>(nackList[i] - nack) - 1;
1369         if (shift >= 0 && shift <= 15) {
1370           bitmask |= (1 << shift);
1371           ++i;
1372         } else {
1373           break;
1374         }
1375       }
1376       // Write the sequence number and the bitmask to the packet.
1377       assert(pos + 4 < IP_PACKET_SIZE);
1378       RtpUtility::AssignUWord16ToBuffer(rtcpbuffer + pos, nack);
1379       pos += 2;
1380       RtpUtility::AssignUWord16ToBuffer(rtcpbuffer + pos, bitmask);
1381       pos += 2;
1382       numOfNackFields++;
1383     }
1384     rtcpbuffer[nackSizePos] = static_cast<uint8_t>(2 + numOfNackFields);
1385
1386     if (i != nackSize) {
1387       LOG(LS_WARNING) << "Nack list too large for one packet.";
1388     }
1389
1390     // Report stats.
1391     NACKStringBuilder stringBuilder;
1392     for (int idx = 0; idx < i; ++idx) {
1393       stringBuilder.PushNACK(nackList[idx]);
1394       nack_stats_.ReportRequest(nackList[idx]);
1395     }
1396     *nackString = stringBuilder.GetResult();
1397     packet_type_counter_.nack_requests = nack_stats_.requests();
1398     packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
1399     return 0;
1400 }
1401
1402 int32_t
1403 RTCPSender::BuildBYE(uint8_t* rtcpbuffer, int& pos)
1404 {
1405     // sanity
1406     if(pos + 8 >= IP_PACKET_SIZE)
1407     {
1408         return -2;
1409     }
1410     if(_includeCSRCs)
1411     {
1412         // Add a bye packet
1413         rtcpbuffer[pos++]=(uint8_t)0x80 + 1 + _CSRCs;  // number of SSRC+CSRCs
1414         rtcpbuffer[pos++]=(uint8_t)203;
1415
1416         // length
1417         rtcpbuffer[pos++]=(uint8_t)0;
1418         rtcpbuffer[pos++]=(uint8_t)(1 + _CSRCs);
1419
1420         // Add our own SSRC
1421         RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1422         pos += 4;
1423
1424         // add CSRCs
1425         for(int i = 0; i < _CSRCs; i++)
1426         {
1427           RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _CSRC[i]);
1428             pos += 4;
1429         }
1430     } else
1431     {
1432         // Add a bye packet
1433         rtcpbuffer[pos++]=(uint8_t)0x80 + 1;  // number of SSRC+CSRCs
1434         rtcpbuffer[pos++]=(uint8_t)203;
1435
1436         // length
1437         rtcpbuffer[pos++]=(uint8_t)0;
1438         rtcpbuffer[pos++]=(uint8_t)1;
1439
1440         // Add our own SSRC
1441         RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1442         pos += 4;
1443     }
1444     return 0;
1445 }
1446
1447 int32_t RTCPSender::BuildReceiverReferenceTime(uint8_t* buffer,
1448                                                int& pos,
1449                                                uint32_t ntp_sec,
1450                                                uint32_t ntp_frac) {
1451   const int kRrTimeBlockLength = 20;
1452   if (pos + kRrTimeBlockLength >= IP_PACKET_SIZE) {
1453     return -2;
1454   }
1455
1456   if (last_xr_rr_.size() >= RTCP_NUMBER_OF_SR) {
1457     last_xr_rr_.erase(last_xr_rr_.begin());
1458   }
1459   last_xr_rr_.insert(std::pair<uint32_t, int64_t>(
1460       RTCPUtility::MidNtp(ntp_sec, ntp_frac),
1461       Clock::NtpToMs(ntp_sec, ntp_frac)));
1462
1463   // Add XR header.
1464   buffer[pos++] = 0x80;
1465   buffer[pos++] = 207;
1466   buffer[pos++] = 0;  // XR packet length.
1467   buffer[pos++] = 4;  // XR packet length.
1468
1469   // Add our own SSRC.
1470   RtpUtility::AssignUWord32ToBuffer(buffer + pos, _SSRC);
1471   pos += 4;
1472
1473   //    0                   1                   2                   3
1474   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1475   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1476   //   |     BT=4      |   reserved    |       block length = 2        |
1477   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1478   //   |              NTP timestamp, most significant word             |
1479   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1480   //   |             NTP timestamp, least significant word             |
1481   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1482
1483   // Add Receiver Reference Time Report block.
1484   buffer[pos++] = 4;  // BT.
1485   buffer[pos++] = 0;  // Reserved.
1486   buffer[pos++] = 0;  // Block length.
1487   buffer[pos++] = 2;  // Block length.
1488
1489   // NTP timestamp.
1490   RtpUtility::AssignUWord32ToBuffer(buffer + pos, ntp_sec);
1491   pos += 4;
1492   RtpUtility::AssignUWord32ToBuffer(buffer + pos, ntp_frac);
1493   pos += 4;
1494
1495   return 0;
1496 }
1497
1498 int32_t RTCPSender::BuildDlrr(uint8_t* buffer,
1499                               int& pos,
1500                               const RtcpReceiveTimeInfo& info) {
1501   const int kDlrrBlockLength = 24;
1502   if (pos + kDlrrBlockLength >= IP_PACKET_SIZE) {
1503     return -2;
1504   }
1505
1506   // Add XR header.
1507   buffer[pos++] = 0x80;
1508   buffer[pos++] = 207;
1509   buffer[pos++] = 0;  // XR packet length.
1510   buffer[pos++] = 5;  // XR packet length.
1511
1512   // Add our own SSRC.
1513   RtpUtility::AssignUWord32ToBuffer(buffer + pos, _SSRC);
1514   pos += 4;
1515
1516   //   0                   1                   2                   3
1517   //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1518   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1519   //  |     BT=5      |   reserved    |         block length          |
1520   //  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
1521   //  |                 SSRC_1 (SSRC of first receiver)               | sub-
1522   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
1523   //  |                         last RR (LRR)                         |   1
1524   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1525   //  |                   delay since last RR (DLRR)                  |
1526   //  +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
1527   //  |                 SSRC_2 (SSRC of second receiver)              | sub-
1528   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
1529   //  :                               ...                             :   2
1530
1531   // Add DLRR sub block.
1532   buffer[pos++] = 5;  // BT.
1533   buffer[pos++] = 0;  // Reserved.
1534   buffer[pos++] = 0;  // Block length.
1535   buffer[pos++] = 3;  // Block length.
1536
1537   // NTP timestamp.
1538   RtpUtility::AssignUWord32ToBuffer(buffer + pos, info.sourceSSRC);
1539   pos += 4;
1540   RtpUtility::AssignUWord32ToBuffer(buffer + pos, info.lastRR);
1541   pos += 4;
1542   RtpUtility::AssignUWord32ToBuffer(buffer + pos, info.delaySinceLastRR);
1543   pos += 4;
1544
1545   return 0;
1546 }
1547
1548 int32_t
1549 RTCPSender::BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos)
1550 {
1551     // sanity
1552     if(pos + 44 >= IP_PACKET_SIZE)
1553     {
1554         return -2;
1555     }
1556
1557     // Add XR header
1558     rtcpbuffer[pos++]=(uint8_t)0x80;
1559     rtcpbuffer[pos++]=(uint8_t)207;
1560
1561     uint32_t XRLengthPos = pos;
1562
1563     // handle length later on
1564     pos++;
1565     pos++;
1566
1567     // Add our own SSRC
1568     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC);
1569     pos += 4;
1570
1571     // Add a VoIP metrics block
1572     rtcpbuffer[pos++]=7;
1573     rtcpbuffer[pos++]=0;
1574     rtcpbuffer[pos++]=0;
1575     rtcpbuffer[pos++]=8;
1576
1577     // Add the remote SSRC
1578     RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC);
1579     pos += 4;
1580
1581     rtcpbuffer[pos++] = _xrVoIPMetric.lossRate;
1582     rtcpbuffer[pos++] = _xrVoIPMetric.discardRate;
1583     rtcpbuffer[pos++] = _xrVoIPMetric.burstDensity;
1584     rtcpbuffer[pos++] = _xrVoIPMetric.gapDensity;
1585
1586     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.burstDuration >> 8);
1587     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.burstDuration);
1588     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.gapDuration >> 8);
1589     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.gapDuration);
1590
1591     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.roundTripDelay >> 8);
1592     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.roundTripDelay);
1593     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.endSystemDelay >> 8);
1594     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.endSystemDelay);
1595
1596     rtcpbuffer[pos++] = _xrVoIPMetric.signalLevel;
1597     rtcpbuffer[pos++] = _xrVoIPMetric.noiseLevel;
1598     rtcpbuffer[pos++] = _xrVoIPMetric.RERL;
1599     rtcpbuffer[pos++] = _xrVoIPMetric.Gmin;
1600
1601     rtcpbuffer[pos++] = _xrVoIPMetric.Rfactor;
1602     rtcpbuffer[pos++] = _xrVoIPMetric.extRfactor;
1603     rtcpbuffer[pos++] = _xrVoIPMetric.MOSLQ;
1604     rtcpbuffer[pos++] = _xrVoIPMetric.MOSCQ;
1605
1606     rtcpbuffer[pos++] = _xrVoIPMetric.RXconfig;
1607     rtcpbuffer[pos++] = 0; // reserved
1608     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBnominal >> 8);
1609     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBnominal);
1610
1611     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBmax >> 8);
1612     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBmax);
1613     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBabsMax >> 8);
1614     rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBabsMax);
1615
1616     rtcpbuffer[XRLengthPos]=(uint8_t)(0);
1617     rtcpbuffer[XRLengthPos+1]=(uint8_t)(10);
1618     return 0;
1619 }
1620
1621 int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
1622                              uint32_t packetTypeFlags,
1623                              int32_t nackSize,
1624                              const uint16_t* nackList,
1625                              bool repeat,
1626                              uint64_t pictureID) {
1627   {
1628     CriticalSectionScoped lock(_criticalSectionRTCPSender);
1629     if(_method == kRtcpOff)
1630     {
1631         LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
1632         return -1;
1633     }
1634   }
1635   uint8_t rtcp_buffer[IP_PACKET_SIZE];
1636   int rtcp_length = PrepareRTCP(feedback_state,
1637                                 packetTypeFlags,
1638                                 nackSize,
1639                                 nackList,
1640                                 repeat,
1641                                 pictureID,
1642                                 rtcp_buffer,
1643                                 IP_PACKET_SIZE);
1644   if (rtcp_length < 0) {
1645     return -1;
1646   }
1647   // Sanity don't send empty packets.
1648   if (rtcp_length == 0)
1649   {
1650       return -1;
1651   }
1652   return SendToNetwork(rtcp_buffer, static_cast<uint16_t>(rtcp_length));
1653 }
1654
1655 int RTCPSender::PrepareRTCP(const FeedbackState& feedback_state,
1656                             uint32_t packetTypeFlags,
1657                             int32_t nackSize,
1658                             const uint16_t* nackList,
1659                             bool repeat,
1660                             uint64_t pictureID,
1661                             uint8_t* rtcp_buffer,
1662                             int buffer_size) {
1663   uint32_t rtcpPacketTypeFlags = packetTypeFlags;
1664   // Collect the received information.
1665   uint32_t NTPsec = 0;
1666   uint32_t NTPfrac = 0;
1667   uint32_t jitterTransmissionOffset = 0;
1668   int position = 0;
1669
1670   CriticalSectionScoped lock(_criticalSectionRTCPSender);
1671
1672   if(_TMMBR )  // Attach TMMBR to send and receive reports.
1673   {
1674       rtcpPacketTypeFlags |= kRtcpTmmbr;
1675   }
1676   if(_appSend)
1677   {
1678       rtcpPacketTypeFlags |= kRtcpApp;
1679       _appSend = false;
1680   }
1681   if(_REMB && _sendREMB)
1682   {
1683       // Always attach REMB to SR if that is configured. Note that REMB is
1684       // only sent on one of the RTP modules in the REMB group.
1685       rtcpPacketTypeFlags |= kRtcpRemb;
1686   }
1687   if(_xrSendVoIPMetric)
1688   {
1689       rtcpPacketTypeFlags |= kRtcpXrVoipMetric;
1690       _xrSendVoIPMetric = false;
1691   }
1692   if(_sendTMMBN)  // Set when having received a TMMBR.
1693   {
1694       rtcpPacketTypeFlags |= kRtcpTmmbn;
1695       _sendTMMBN = false;
1696   }
1697   if (rtcpPacketTypeFlags & kRtcpReport)
1698   {
1699       if (xrSendReceiverReferenceTimeEnabled_ && !_sending)
1700       {
1701           rtcpPacketTypeFlags |= kRtcpXrReceiverReferenceTime;
1702       }
1703       if (feedback_state.has_last_xr_rr)
1704       {
1705           rtcpPacketTypeFlags |= kRtcpXrDlrrReportBlock;
1706       }
1707   }
1708   if(_method == kRtcpCompound)
1709   {
1710       if(_sending)
1711       {
1712           rtcpPacketTypeFlags |= kRtcpSr;
1713       } else
1714       {
1715           rtcpPacketTypeFlags |= kRtcpRr;
1716       }
1717   } else if(_method == kRtcpNonCompound)
1718   {
1719       if(rtcpPacketTypeFlags & kRtcpReport)
1720       {
1721           if(_sending)
1722           {
1723               rtcpPacketTypeFlags |= kRtcpSr;
1724           } else
1725           {
1726               rtcpPacketTypeFlags |= kRtcpRr;
1727           }
1728       }
1729   }
1730   if( rtcpPacketTypeFlags & kRtcpRr ||
1731       rtcpPacketTypeFlags & kRtcpSr)
1732   {
1733       // generate next time to send a RTCP report
1734       // seeded from RTP constructor
1735       int32_t random = rand() % 1000;
1736       int32_t timeToNext = RTCP_INTERVAL_AUDIO_MS;
1737
1738       if(_audio)
1739       {
1740           timeToNext = (RTCP_INTERVAL_AUDIO_MS/2) +
1741               (RTCP_INTERVAL_AUDIO_MS*random/1000);
1742       }else
1743       {
1744           uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
1745           if(_sending)
1746           {
1747             // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
1748             uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
1749             if (send_bitrate_kbit != 0)
1750               minIntervalMs = 360000 / send_bitrate_kbit;
1751           }
1752           if(minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
1753           {
1754               minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
1755           }
1756           timeToNext = (minIntervalMs/2) + (minIntervalMs*random/1000);
1757       }
1758       _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + timeToNext;
1759   }
1760
1761   // If the data does not fit in the packet we fill it as much as possible.
1762   int32_t buildVal = 0;
1763
1764   // We need to send our NTP even if we haven't received any reports.
1765   _clock->CurrentNtp(NTPsec, NTPfrac);
1766   if (ShouldSendReportBlocks(rtcpPacketTypeFlags)) {
1767     StatisticianMap statisticians =
1768         receive_statistics_->GetActiveStatisticians();
1769     if (!statisticians.empty()) {
1770       StatisticianMap::const_iterator it;
1771       int i;
1772       for (it = statisticians.begin(), i = 0; it != statisticians.end();
1773            ++it, ++i) {
1774         RTCPReportBlock report_block;
1775         if (PrepareReport(
1776                 feedback_state, it->second, &report_block, &NTPsec, &NTPfrac))
1777           AddReportBlock(it->first, &internal_report_blocks_, &report_block);
1778       }
1779       if (_IJ && !statisticians.empty()) {
1780         rtcpPacketTypeFlags |= kRtcpTransmissionTimeOffset;
1781       }
1782     }
1783   }
1784
1785   if(rtcpPacketTypeFlags & kRtcpSr)
1786   {
1787     buildVal = BuildSR(feedback_state, rtcp_buffer, position, NTPsec, NTPfrac);
1788       if (buildVal == -1) {
1789         return -1;
1790       } else if (buildVal == -2) {
1791         return position;
1792       }
1793       buildVal = BuildSDEC(rtcp_buffer, position);
1794       if (buildVal == -1) {
1795         return -1;
1796       } else if (buildVal == -2) {
1797         return position;
1798       }
1799   }else if(rtcpPacketTypeFlags & kRtcpRr)
1800   {
1801       buildVal = BuildRR(rtcp_buffer, position, NTPsec, NTPfrac);
1802       if (buildVal == -1) {
1803         return -1;
1804       } else if (buildVal == -2) {
1805         return position;
1806       }
1807       // only of set
1808       if(_CNAME[0] != 0)
1809       {
1810           buildVal = BuildSDEC(rtcp_buffer, position);
1811           if (buildVal == -1) {
1812             return -1;
1813           }
1814       }
1815   }
1816   if(rtcpPacketTypeFlags & kRtcpTransmissionTimeOffset)
1817   {
1818       // If present, this RTCP packet must be placed after a
1819       // receiver report.
1820       buildVal = BuildExtendedJitterReport(rtcp_buffer,
1821                                            position,
1822                                            jitterTransmissionOffset);
1823       if (buildVal == -1) {
1824         return -1;
1825       } else if (buildVal == -2) {
1826         return position;
1827       }
1828   }
1829   if(rtcpPacketTypeFlags & kRtcpPli)
1830   {
1831       buildVal = BuildPLI(rtcp_buffer, position);
1832       if (buildVal == -1) {
1833         return -1;
1834       } else if (buildVal == -2) {
1835         return position;
1836       }
1837       TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::PLI");
1838       ++packet_type_counter_.pli_packets;
1839       TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_PLICount", _SSRC,
1840                         packet_type_counter_.pli_packets);
1841   }
1842   if(rtcpPacketTypeFlags & kRtcpFir)
1843   {
1844       buildVal = BuildFIR(rtcp_buffer, position, repeat);
1845       if (buildVal == -1) {
1846         return -1;
1847       } else if (buildVal == -2) {
1848         return position;
1849       }
1850       TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::FIR");
1851       ++packet_type_counter_.fir_packets;
1852       TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_FIRCount", _SSRC,
1853                         packet_type_counter_.fir_packets);
1854   }
1855   if(rtcpPacketTypeFlags & kRtcpSli)
1856   {
1857       buildVal = BuildSLI(rtcp_buffer, position, (uint8_t)pictureID);
1858       if (buildVal == -1) {
1859         return -1;
1860       } else if (buildVal == -2) {
1861         return position;
1862       }
1863   }
1864   if(rtcpPacketTypeFlags & kRtcpRpsi)
1865   {
1866       const int8_t payloadType = feedback_state.send_payload_type;
1867       if (payloadType == -1) {
1868         return -1;
1869       }
1870       buildVal = BuildRPSI(rtcp_buffer, position, pictureID,
1871                            (uint8_t)payloadType);
1872       if (buildVal == -1) {
1873         return -1;
1874       } else if (buildVal == -2) {
1875         return position;
1876       }
1877   }
1878   if(rtcpPacketTypeFlags & kRtcpRemb)
1879   {
1880       buildVal = BuildREMB(rtcp_buffer, position);
1881       if (buildVal == -1) {
1882         return -1;
1883       } else if (buildVal == -2) {
1884         return position;
1885       }
1886       TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::REMB");
1887   }
1888   if(rtcpPacketTypeFlags & kRtcpBye)
1889   {
1890       buildVal = BuildBYE(rtcp_buffer, position);
1891       if (buildVal == -1) {
1892         return -1;
1893       } else if (buildVal == -2) {
1894         return position;
1895       }
1896   }
1897   if(rtcpPacketTypeFlags & kRtcpApp)
1898   {
1899       buildVal = BuildAPP(rtcp_buffer, position);
1900       if (buildVal == -1) {
1901         return -1;
1902       } else if (buildVal == -2) {
1903         return position;
1904       }
1905   }
1906   if(rtcpPacketTypeFlags & kRtcpTmmbr)
1907   {
1908       buildVal = BuildTMMBR(feedback_state.module, rtcp_buffer, position);
1909       if (buildVal == -1) {
1910         return -1;
1911       } else if (buildVal == -2) {
1912         return position;
1913       }
1914   }
1915   if(rtcpPacketTypeFlags & kRtcpTmmbn)
1916   {
1917       buildVal = BuildTMMBN(rtcp_buffer, position);
1918       if (buildVal == -1) {
1919         return -1;
1920       } else if (buildVal == -2) {
1921         return position;
1922       }
1923   }
1924   if(rtcpPacketTypeFlags & kRtcpNack)
1925   {
1926       std::string nackString;
1927       buildVal = BuildNACK(rtcp_buffer, position, nackSize, nackList,
1928                            &nackString);
1929       if (buildVal == -1) {
1930         return -1;
1931       } else if (buildVal == -2) {
1932         return position;
1933       }
1934       TRACE_EVENT_INSTANT1("webrtc_rtp", "RTCPSender::NACK",
1935                            "nacks", TRACE_STR_COPY(nackString.c_str()));
1936       ++packet_type_counter_.nack_packets;
1937       TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_NACKCount", _SSRC,
1938                         packet_type_counter_.nack_packets);
1939   }
1940   if(rtcpPacketTypeFlags & kRtcpXrVoipMetric)
1941   {
1942       buildVal = BuildVoIPMetric(rtcp_buffer, position);
1943       if (buildVal == -1) {
1944         return -1;
1945       } else if (buildVal == -2) {
1946         return position;
1947       }
1948   }
1949   if (rtcpPacketTypeFlags & kRtcpXrReceiverReferenceTime)
1950   {
1951       buildVal = BuildReceiverReferenceTime(rtcp_buffer,
1952                                             position,
1953                                             NTPsec,
1954                                             NTPfrac);
1955       if (buildVal == -1) {
1956         return -1;
1957       } else if (buildVal == -2) {
1958         return position;
1959       }
1960   }
1961   if (rtcpPacketTypeFlags & kRtcpXrDlrrReportBlock)
1962   {
1963       buildVal = BuildDlrr(rtcp_buffer, position, feedback_state.last_xr_rr);
1964       if (buildVal == -1) {
1965         return -1;
1966       } else if (buildVal == -2) {
1967         return position;
1968       }
1969   }
1970   return position;
1971 }
1972
1973 bool RTCPSender::ShouldSendReportBlocks(uint32_t rtcp_packet_type) const {
1974   return Status() == kRtcpCompound ||
1975       (rtcp_packet_type & kRtcpReport) ||
1976       (rtcp_packet_type & kRtcpSr) ||
1977       (rtcp_packet_type & kRtcpRr);
1978 }
1979
1980 bool RTCPSender::PrepareReport(const FeedbackState& feedback_state,
1981                                StreamStatistician* statistician,
1982                                RTCPReportBlock* report_block,
1983                                uint32_t* ntp_secs, uint32_t* ntp_frac) {
1984   // Do we have receive statistics to send?
1985   RtcpStatistics stats;
1986   if (!statistician->GetStatistics(&stats, true))
1987     return false;
1988   report_block->fractionLost = stats.fraction_lost;
1989   report_block->cumulativeLost = stats.cumulative_lost;
1990   report_block->extendedHighSeqNum =
1991       stats.extended_max_sequence_number;
1992   report_block->jitter = stats.jitter;
1993
1994   // get our NTP as late as possible to avoid a race
1995   _clock->CurrentNtp(*ntp_secs, *ntp_frac);
1996
1997   // Delay since last received report
1998   uint32_t delaySinceLastReceivedSR = 0;
1999   if ((feedback_state.last_rr_ntp_secs != 0) ||
2000       (feedback_state.last_rr_ntp_frac != 0)) {
2001     // get the 16 lowest bits of seconds and the 16 higest bits of fractions
2002     uint32_t now=*ntp_secs&0x0000FFFF;
2003     now <<=16;
2004     now += (*ntp_frac&0xffff0000)>>16;
2005
2006     uint32_t receiveTime = feedback_state.last_rr_ntp_secs&0x0000FFFF;
2007     receiveTime <<=16;
2008     receiveTime += (feedback_state.last_rr_ntp_frac&0xffff0000)>>16;
2009
2010     delaySinceLastReceivedSR = now-receiveTime;
2011   }
2012   report_block->delaySinceLastSR = delaySinceLastReceivedSR;
2013   report_block->lastSR = feedback_state.remote_sr;
2014   return true;
2015 }
2016
2017 int32_t
2018 RTCPSender::SendToNetwork(const uint8_t* dataBuffer,
2019                           const uint16_t length)
2020 {
2021     CriticalSectionScoped lock(_criticalSectionTransport);
2022     if(_cbTransport)
2023     {
2024         if(_cbTransport->SendRTCPPacket(_id, dataBuffer, length) > 0)
2025         {
2026             return 0;
2027         }
2028     }
2029     return -1;
2030 }
2031
2032 int32_t
2033 RTCPSender::SetCSRCStatus(const bool include)
2034 {
2035     CriticalSectionScoped lock(_criticalSectionRTCPSender);
2036     _includeCSRCs = include;
2037     return 0;
2038 }
2039
2040 int32_t
2041 RTCPSender::SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
2042                     const uint8_t arrLength)
2043 {
2044     assert(arrLength <= kRtpCsrcSize);
2045     CriticalSectionScoped lock(_criticalSectionRTCPSender);
2046
2047     for(int i = 0; i < arrLength;i++)
2048     {
2049         _CSRC[i] = arrOfCSRC[i];
2050     }
2051     _CSRCs = arrLength;
2052     return 0;
2053 }
2054
2055 int32_t
2056 RTCPSender::SetApplicationSpecificData(const uint8_t subType,
2057                                        const uint32_t name,
2058                                        const uint8_t* data,
2059                                        const uint16_t length)
2060 {
2061     if(length %4 != 0)
2062     {
2063         LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
2064         return -1;
2065     }
2066     CriticalSectionScoped lock(_criticalSectionRTCPSender);
2067
2068     if(_appData)
2069     {
2070         delete [] _appData;
2071     }
2072
2073     _appSend = true;
2074     _appSubType = subType;
2075     _appName = name;
2076     _appData = new uint8_t[length];
2077     _appLength = length;
2078     memcpy(_appData, data, length);
2079     return 0;
2080 }
2081
2082 int32_t
2083 RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric)
2084 {
2085     CriticalSectionScoped lock(_criticalSectionRTCPSender);
2086     memcpy(&_xrVoIPMetric, VoIPMetric, sizeof(RTCPVoIPMetric));
2087
2088     _xrSendVoIPMetric = true;
2089     return 0;
2090 }
2091
2092 void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
2093   CriticalSectionScoped lock(_criticalSectionRTCPSender);
2094   xrSendReceiverReferenceTimeEnabled_ = enable;
2095 }
2096
2097 bool RTCPSender::RtcpXrReceiverReferenceTime() const {
2098   CriticalSectionScoped lock(_criticalSectionRTCPSender);
2099   return xrSendReceiverReferenceTimeEnabled_;
2100 }
2101
2102 // called under critsect _criticalSectionRTCPSender
2103 int32_t RTCPSender::WriteAllReportBlocksToBuffer(
2104     uint8_t* rtcpbuffer,
2105     int pos,
2106     uint8_t& numberOfReportBlocks,
2107     const uint32_t NTPsec,
2108     const uint32_t NTPfrac) {
2109   numberOfReportBlocks = external_report_blocks_.size();
2110   numberOfReportBlocks += internal_report_blocks_.size();
2111   if ((pos + numberOfReportBlocks * 24) >= IP_PACKET_SIZE) {
2112     LOG(LS_WARNING) << "Can't fit all report blocks.";
2113     return -1;
2114   }
2115   pos = WriteReportBlocksToBuffer(rtcpbuffer, pos, internal_report_blocks_);
2116   while (!internal_report_blocks_.empty()) {
2117     delete internal_report_blocks_.begin()->second;
2118     internal_report_blocks_.erase(internal_report_blocks_.begin());
2119   }
2120   pos = WriteReportBlocksToBuffer(rtcpbuffer, pos, external_report_blocks_);
2121   return pos;
2122 }
2123
2124 int32_t RTCPSender::WriteReportBlocksToBuffer(
2125     uint8_t* rtcpbuffer,
2126     int32_t position,
2127     const std::map<uint32_t, RTCPReportBlock*>& report_blocks) {
2128   std::map<uint32_t, RTCPReportBlock*>::const_iterator it =
2129       report_blocks.begin();
2130   for (; it != report_blocks.end(); it++) {
2131     uint32_t remoteSSRC = it->first;
2132     RTCPReportBlock* reportBlock = it->second;
2133     if (reportBlock) {
2134       // Remote SSRC
2135       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + position, remoteSSRC);
2136       position += 4;
2137
2138       // fraction lost
2139       rtcpbuffer[position++] = reportBlock->fractionLost;
2140
2141       // cumulative loss
2142       RtpUtility::AssignUWord24ToBuffer(rtcpbuffer + position,
2143                                         reportBlock->cumulativeLost);
2144       position += 3;
2145
2146       // extended highest seq_no, contain the highest sequence number received
2147       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + position,
2148                                         reportBlock->extendedHighSeqNum);
2149       position += 4;
2150
2151       // Jitter
2152       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + position,
2153                                         reportBlock->jitter);
2154       position += 4;
2155
2156       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + position,
2157                                         reportBlock->lastSR);
2158       position += 4;
2159
2160       RtpUtility::AssignUWord32ToBuffer(rtcpbuffer + position,
2161                                         reportBlock->delaySinceLastSR);
2162       position += 4;
2163     }
2164   }
2165   return position;
2166 }
2167
2168 // no callbacks allowed inside this function
2169 int32_t
2170 RTCPSender::SetTMMBN(const TMMBRSet* boundingSet,
2171                      const uint32_t maxBitrateKbit)
2172 {
2173     CriticalSectionScoped lock(_criticalSectionRTCPSender);
2174
2175     if (0 == _tmmbrHelp.SetTMMBRBoundingSetToSend(boundingSet, maxBitrateKbit))
2176     {
2177         _sendTMMBN = true;
2178         return 0;
2179     }
2180     return -1;
2181 }
2182 }  // namespace webrtc