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