Home | History | Annotate | Download | only in source
      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/rtp_rtcp_impl.h"
     12 
     13 #include <assert.h>
     14 #include <string.h>
     15 
     16 #include "webrtc/common_types.h"
     17 #include "webrtc/system_wrappers/interface/logging.h"
     18 #include "webrtc/system_wrappers/interface/trace.h"
     19 
     20 #ifdef _WIN32
     21 // Disable warning C4355: 'this' : used in base member initializer list.
     22 #pragma warning(disable : 4355)
     23 #endif
     24 
     25 namespace webrtc {
     26 
     27 RtpRtcp::Configuration::Configuration()
     28     : id(-1),
     29       audio(false),
     30       clock(NULL),
     31       default_module(NULL),
     32       receive_statistics(NullObjectReceiveStatistics()),
     33       outgoing_transport(NULL),
     34       rtcp_feedback(NULL),
     35       intra_frame_callback(NULL),
     36       bandwidth_callback(NULL),
     37       rtt_stats(NULL),
     38       audio_messages(NullObjectRtpAudioFeedback()),
     39       remote_bitrate_estimator(NULL),
     40       paced_sender(NULL),
     41       send_bitrate_observer(NULL),
     42       send_frame_count_observer(NULL),
     43       send_side_delay_observer(NULL) {
     44 }
     45 
     46 RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
     47   if (configuration.clock) {
     48     return new ModuleRtpRtcpImpl(configuration);
     49   } else {
     50     // No clock implementation provided, use default clock.
     51     RtpRtcp::Configuration configuration_copy;
     52     memcpy(&configuration_copy, &configuration,
     53            sizeof(RtpRtcp::Configuration));
     54     configuration_copy.clock = Clock::GetRealTimeClock();
     55     return new ModuleRtpRtcpImpl(configuration_copy);
     56   }
     57 }
     58 
     59 ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
     60     : rtp_sender_(configuration.id,
     61                   configuration.audio,
     62                   configuration.clock,
     63                   configuration.outgoing_transport,
     64                   configuration.audio_messages,
     65                   configuration.paced_sender,
     66                   configuration.send_bitrate_observer,
     67                   configuration.send_frame_count_observer,
     68                   configuration.send_side_delay_observer),
     69       rtcp_sender_(configuration.id,
     70                    configuration.audio,
     71                    configuration.clock,
     72                    configuration.receive_statistics),
     73       rtcp_receiver_(configuration.id, configuration.clock, this),
     74       clock_(configuration.clock),
     75       id_(configuration.id),
     76       audio_(configuration.audio),
     77       collision_detected_(false),
     78       last_process_time_(configuration.clock->TimeInMilliseconds()),
     79       last_bitrate_process_time_(configuration.clock->TimeInMilliseconds()),
     80       last_rtt_process_time_(configuration.clock->TimeInMilliseconds()),
     81       packet_overhead_(28),  // IPV4 UDP.
     82       critical_section_module_ptrs_(
     83           CriticalSectionWrapper::CreateCriticalSection()),
     84       critical_section_module_ptrs_feedback_(
     85           CriticalSectionWrapper::CreateCriticalSection()),
     86       default_module_(
     87           static_cast<ModuleRtpRtcpImpl*>(configuration.default_module)),
     88       padding_index_(static_cast<size_t>(-1)),  // Start padding at first child.
     89       nack_method_(kNackOff),
     90       nack_last_time_sent_full_(0),
     91       nack_last_seq_number_sent_(0),
     92       simulcast_(false),
     93       key_frame_req_method_(kKeyFrameReqFirRtp),
     94       remote_bitrate_(configuration.remote_bitrate_estimator),
     95       rtt_stats_(configuration.rtt_stats),
     96       critical_section_rtt_(CriticalSectionWrapper::CreateCriticalSection()),
     97       rtt_ms_(0) {
     98   send_video_codec_.codecType = kVideoCodecUnknown;
     99 
    100   if (default_module_) {
    101     default_module_->RegisterChildModule(this);
    102   }
    103   // TODO(pwestin) move to constructors of each rtp/rtcp sender/receiver object.
    104   rtcp_receiver_.RegisterRtcpObservers(configuration.intra_frame_callback,
    105                                        configuration.bandwidth_callback,
    106                                        configuration.rtcp_feedback);
    107   rtcp_sender_.RegisterSendTransport(configuration.outgoing_transport);
    108 
    109   // Make sure that RTCP objects are aware of our SSRC.
    110   uint32_t SSRC = rtp_sender_.SSRC();
    111   rtcp_sender_.SetSSRC(SSRC);
    112   SetRtcpReceiverSsrcs(SSRC);
    113 }
    114 
    115 ModuleRtpRtcpImpl::~ModuleRtpRtcpImpl() {
    116   // All child modules MUST be deleted before deleting the default.
    117   assert(child_modules_.empty());
    118 
    119   // Deregister for the child modules.
    120   // Will go in to the default and remove it self.
    121   if (default_module_) {
    122     default_module_->DeRegisterChildModule(this);
    123   }
    124 }
    125 
    126 void ModuleRtpRtcpImpl::RegisterChildModule(RtpRtcp* module) {
    127   CriticalSectionScoped lock(
    128       critical_section_module_ptrs_.get());
    129   CriticalSectionScoped double_lock(
    130       critical_section_module_ptrs_feedback_.get());
    131 
    132   // We use two locks for protecting child_modules_, one
    133   // (critical_section_module_ptrs_feedback_) for incoming
    134   // messages (BitrateSent) and critical_section_module_ptrs_
    135   // for all outgoing messages sending packets etc.
    136   child_modules_.push_back(static_cast<ModuleRtpRtcpImpl*>(module));
    137 }
    138 
    139 void ModuleRtpRtcpImpl::DeRegisterChildModule(RtpRtcp* remove_module) {
    140   CriticalSectionScoped lock(
    141       critical_section_module_ptrs_.get());
    142   CriticalSectionScoped double_lock(
    143       critical_section_module_ptrs_feedback_.get());
    144 
    145   std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
    146   while (it != child_modules_.end()) {
    147     RtpRtcp* module = *it;
    148     if (module == remove_module) {
    149       child_modules_.erase(it);
    150       return;
    151     }
    152     it++;
    153   }
    154 }
    155 
    156 // Returns the number of milliseconds until the module want a worker thread
    157 // to call Process.
    158 int32_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
    159     const int64_t now = clock_->TimeInMilliseconds();
    160   return kRtpRtcpMaxIdleTimeProcess - (now - last_process_time_);
    161 }
    162 
    163 // Process any pending tasks such as timeouts (non time critical events).
    164 int32_t ModuleRtpRtcpImpl::Process() {
    165   const int64_t now = clock_->TimeInMilliseconds();
    166   last_process_time_ = now;
    167 
    168   if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
    169     rtp_sender_.ProcessBitrate();
    170     last_bitrate_process_time_ = now;
    171   }
    172 
    173   if (!IsDefaultModule()) {
    174     bool process_rtt = now >= last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs;
    175     if (rtcp_sender_.Sending()) {
    176       // Process RTT if we have received a receiver report and we haven't
    177       // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
    178       if (rtcp_receiver_.LastReceivedReceiverReport() >
    179           last_rtt_process_time_ && process_rtt) {
    180         std::vector<RTCPReportBlock> receive_blocks;
    181         rtcp_receiver_.StatisticsReceived(&receive_blocks);
    182         uint16_t max_rtt = 0;
    183         for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
    184              it != receive_blocks.end(); ++it) {
    185           uint16_t rtt = 0;
    186           rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
    187           max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
    188         }
    189         // Report the rtt.
    190         if (rtt_stats_ && max_rtt != 0)
    191           rtt_stats_->OnRttUpdate(max_rtt);
    192       }
    193 
    194       // Verify receiver reports are delivered and the reported sequence number
    195       // is increasing.
    196       int64_t rtcp_interval = RtcpReportInterval();
    197       if (rtcp_receiver_.RtcpRrTimeout(rtcp_interval)) {
    198         LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
    199       } else if (rtcp_receiver_.RtcpRrSequenceNumberTimeout(rtcp_interval)) {
    200         LOG_F(LS_WARNING) <<
    201             "Timeout: No increase in RTCP RR extended highest sequence number.";
    202       }
    203 
    204       if (remote_bitrate_ && rtcp_sender_.TMMBR()) {
    205         unsigned int target_bitrate = 0;
    206         std::vector<unsigned int> ssrcs;
    207         if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
    208           if (!ssrcs.empty()) {
    209             target_bitrate = target_bitrate / ssrcs.size();
    210           }
    211           rtcp_sender_.SetTargetBitrate(target_bitrate);
    212         }
    213       }
    214     } else {
    215       // Report rtt from receiver.
    216       if (process_rtt) {
    217          uint16_t rtt_ms;
    218          if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
    219            rtt_stats_->OnRttUpdate(rtt_ms);
    220          }
    221       }
    222     }
    223 
    224     // Get processed rtt.
    225     if (process_rtt) {
    226       last_rtt_process_time_ = now;
    227       if (rtt_stats_) {
    228         set_rtt_ms(rtt_stats_->LastProcessedRtt());
    229       }
    230     }
    231 
    232     if (rtcp_sender_.TimeToSendRTCPReport()) {
    233       rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
    234     }
    235   }
    236 
    237   if (UpdateRTCPReceiveInformationTimers()) {
    238     // A receiver has timed out
    239     rtcp_receiver_.UpdateTMMBR();
    240   }
    241   return 0;
    242 }
    243 
    244 void ModuleRtpRtcpImpl::SetRTXSendStatus(int mode) {
    245   rtp_sender_.SetRTXStatus(mode);
    246 }
    247 
    248 void ModuleRtpRtcpImpl::RTXSendStatus(int* mode,
    249                                       uint32_t* ssrc,
    250                                       int* payload_type) const {
    251   rtp_sender_.RTXStatus(mode, ssrc, payload_type);
    252 }
    253 
    254 void ModuleRtpRtcpImpl::SetRtxSsrc(uint32_t ssrc) {
    255   rtp_sender_.SetRtxSsrc(ssrc);
    256 }
    257 
    258 void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type) {
    259   rtp_sender_.SetRtxPayloadType(payload_type);
    260 }
    261 
    262 int32_t ModuleRtpRtcpImpl::IncomingRtcpPacket(
    263     const uint8_t* rtcp_packet,
    264     const uint16_t length) {
    265   // Allow receive of non-compound RTCP packets.
    266   RTCPUtility::RTCPParserV2 rtcp_parser(rtcp_packet, length, true);
    267 
    268   const bool valid_rtcpheader = rtcp_parser.IsValid();
    269   if (!valid_rtcpheader) {
    270     LOG(LS_WARNING) << "Incoming invalid RTCP packet";
    271     return -1;
    272   }
    273   RTCPHelp::RTCPPacketInformation rtcp_packet_information;
    274   int32_t ret_val = rtcp_receiver_.IncomingRTCPPacket(
    275       rtcp_packet_information, &rtcp_parser);
    276   if (ret_val == 0) {
    277     rtcp_receiver_.TriggerCallbacksFromRTCPPacket(rtcp_packet_information);
    278   }
    279   return ret_val;
    280 }
    281 
    282 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
    283     const CodecInst& voice_codec) {
    284   return rtp_sender_.RegisterPayload(
    285            voice_codec.plname,
    286            voice_codec.pltype,
    287            voice_codec.plfreq,
    288            voice_codec.channels,
    289            (voice_codec.rate < 0) ? 0 : voice_codec.rate);
    290 }
    291 
    292 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
    293     const VideoCodec& video_codec) {
    294   send_video_codec_ = video_codec;
    295   {
    296     // simulcast_ is accessed when accessing child_modules_, so this write needs
    297     // to be protected by the same lock.
    298     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    299     simulcast_ = video_codec.numberOfSimulcastStreams > 1;
    300   }
    301   return rtp_sender_.RegisterPayload(video_codec.plName,
    302                                      video_codec.plType,
    303                                      90000,
    304                                      0,
    305                                      video_codec.maxBitrate);
    306 }
    307 
    308 int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(
    309     const int8_t payload_type) {
    310   return rtp_sender_.DeRegisterSendPayload(payload_type);
    311 }
    312 
    313 int8_t ModuleRtpRtcpImpl::SendPayloadType() const {
    314   return rtp_sender_.SendPayloadType();
    315 }
    316 
    317 uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
    318   return rtp_sender_.StartTimestamp();
    319 }
    320 
    321 // Configure start timestamp, default is a random number.
    322 int32_t ModuleRtpRtcpImpl::SetStartTimestamp(
    323     const uint32_t timestamp) {
    324   rtcp_sender_.SetStartTimestamp(timestamp);
    325   rtp_sender_.SetStartTimestamp(timestamp, true);
    326   return 0;  // TODO(pwestin): change to void.
    327 }
    328 
    329 uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
    330   return rtp_sender_.SequenceNumber();
    331 }
    332 
    333 // Set SequenceNumber, default is a random number.
    334 int32_t ModuleRtpRtcpImpl::SetSequenceNumber(
    335     const uint16_t seq_num) {
    336   rtp_sender_.SetSequenceNumber(seq_num);
    337   return 0;  // TODO(pwestin): change to void.
    338 }
    339 
    340 void ModuleRtpRtcpImpl::SetRtpStateForSsrc(uint32_t ssrc,
    341                                            const RtpState& rtp_state) {
    342   if (rtp_sender_.SSRC() == ssrc) {
    343     rtp_sender_.SetRtpState(rtp_state);
    344     return;
    345   }
    346   if (rtp_sender_.RtxSsrc() == ssrc) {
    347     rtp_sender_.SetRtxRtpState(rtp_state);
    348     return;
    349   }
    350 
    351   CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    352   for (size_t i = 0; i < child_modules_.size(); ++i) {
    353     child_modules_[i]->SetRtpStateForSsrc(ssrc, rtp_state);
    354   }
    355 }
    356 
    357 bool ModuleRtpRtcpImpl::GetRtpStateForSsrc(uint32_t ssrc, RtpState* rtp_state) {
    358   if (rtp_sender_.SSRC() == ssrc) {
    359     *rtp_state = rtp_sender_.GetRtpState();
    360     return true;
    361   }
    362 
    363   if (rtp_sender_.RtxSsrc() == ssrc) {
    364     *rtp_state = rtp_sender_.GetRtxRtpState();
    365     return true;
    366   }
    367 
    368   CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    369   for (size_t i = 0; i < child_modules_.size(); ++i) {
    370     if (child_modules_[i]->GetRtpStateForSsrc(ssrc, rtp_state))
    371       return true;
    372   }
    373   return false;
    374 }
    375 
    376 uint32_t ModuleRtpRtcpImpl::SSRC() const {
    377   return rtp_sender_.SSRC();
    378 }
    379 
    380 // Configure SSRC, default is a random number.
    381 void ModuleRtpRtcpImpl::SetSSRC(const uint32_t ssrc) {
    382   rtp_sender_.SetSSRC(ssrc);
    383   rtcp_sender_.SetSSRC(ssrc);
    384   SetRtcpReceiverSsrcs(ssrc);
    385 }
    386 
    387 int32_t ModuleRtpRtcpImpl::SetCSRCStatus(const bool include) {
    388   rtcp_sender_.SetCSRCStatus(include);
    389   rtp_sender_.SetCSRCStatus(include);
    390   return 0;  // TODO(pwestin): change to void.
    391 }
    392 
    393 int32_t ModuleRtpRtcpImpl::CSRCs(
    394   uint32_t arr_of_csrc[kRtpCsrcSize]) const {
    395   return rtp_sender_.CSRCs(arr_of_csrc);
    396 }
    397 
    398 int32_t ModuleRtpRtcpImpl::SetCSRCs(
    399     const uint32_t arr_of_csrc[kRtpCsrcSize],
    400     const uint8_t arr_length) {
    401   if (IsDefaultModule()) {
    402     // For default we need to update all child modules too.
    403     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    404 
    405     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
    406     while (it != child_modules_.end()) {
    407       RtpRtcp* module = *it;
    408       if (module) {
    409         module->SetCSRCs(arr_of_csrc, arr_length);
    410       }
    411       it++;
    412     }
    413   } else {
    414     rtcp_sender_.SetCSRCs(arr_of_csrc, arr_length);
    415     rtp_sender_.SetCSRCs(arr_of_csrc, arr_length);
    416   }
    417   return 0;  // TODO(pwestin): change to void.
    418 }
    419 
    420 // TODO(pbos): Handle media and RTX streams separately (separate RTCP
    421 // feedbacks).
    422 RTCPSender::FeedbackState ModuleRtpRtcpImpl::GetFeedbackState() {
    423   StreamDataCounters rtp_stats;
    424   StreamDataCounters rtx_stats;
    425   rtp_sender_.GetDataCounters(&rtp_stats, &rtx_stats);
    426 
    427   RTCPSender::FeedbackState state;
    428   state.send_payload_type = SendPayloadType();
    429   state.frequency_hz = CurrentSendFrequencyHz();
    430   state.packets_sent = rtp_stats.packets + rtx_stats.packets;
    431   state.media_bytes_sent = rtp_stats.bytes + rtx_stats.bytes;
    432   state.module = this;
    433 
    434   LastReceivedNTP(&state.last_rr_ntp_secs,
    435                   &state.last_rr_ntp_frac,
    436                   &state.remote_sr);
    437 
    438   state.has_last_xr_rr = LastReceivedXrReferenceTimeInfo(&state.last_xr_rr);
    439 
    440   uint32_t tmp;
    441   BitrateSent(&state.send_bitrate, &tmp, &tmp, &tmp);
    442   return state;
    443 }
    444 
    445 int ModuleRtpRtcpImpl::CurrentSendFrequencyHz() const {
    446   return rtp_sender_.SendPayloadFrequency();
    447 }
    448 
    449 int32_t ModuleRtpRtcpImpl::SetSendingStatus(const bool sending) {
    450   if (rtcp_sender_.Sending() != sending) {
    451     // Sends RTCP BYE when going from true to false
    452     if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
    453       LOG(LS_WARNING) << "Failed to send RTCP BYE";
    454     }
    455 
    456     collision_detected_ = false;
    457 
    458     // Generate a new time_stamp if true and not configured via API
    459     // Generate a new SSRC for the next "call" if false
    460     rtp_sender_.SetSendingStatus(sending);
    461     if (sending) {
    462       // Make sure the RTCP sender has the same timestamp offset.
    463       rtcp_sender_.SetStartTimestamp(rtp_sender_.StartTimestamp());
    464     }
    465 
    466     // Make sure that RTCP objects are aware of our SSRC (it could have changed
    467     // Due to collision)
    468     uint32_t SSRC = rtp_sender_.SSRC();
    469     rtcp_sender_.SetSSRC(SSRC);
    470     SetRtcpReceiverSsrcs(SSRC);
    471 
    472     return 0;
    473   }
    474   return 0;
    475 }
    476 
    477 bool ModuleRtpRtcpImpl::Sending() const {
    478   return rtcp_sender_.Sending();
    479 }
    480 
    481 int32_t ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
    482   rtp_sender_.SetSendingMediaStatus(sending);
    483   return 0;
    484 }
    485 
    486 bool ModuleRtpRtcpImpl::SendingMedia() const {
    487   if (!IsDefaultModule()) {
    488     return rtp_sender_.SendingMedia();
    489   }
    490 
    491   CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    492   std::vector<ModuleRtpRtcpImpl*>::const_iterator it = child_modules_.begin();
    493   while (it != child_modules_.end()) {
    494     RTPSender& rtp_sender = (*it)->rtp_sender_;
    495     if (rtp_sender.SendingMedia()) {
    496       return true;
    497     }
    498     it++;
    499   }
    500   return false;
    501 }
    502 
    503 int32_t ModuleRtpRtcpImpl::SendOutgoingData(
    504     FrameType frame_type,
    505     int8_t payload_type,
    506     uint32_t time_stamp,
    507     int64_t capture_time_ms,
    508     const uint8_t* payload_data,
    509     uint32_t payload_size,
    510     const RTPFragmentationHeader* fragmentation,
    511     const RTPVideoHeader* rtp_video_hdr) {
    512   rtcp_sender_.SetLastRtpTime(time_stamp, capture_time_ms);
    513 
    514   if (!IsDefaultModule()) {
    515     // Don't send RTCP from default module.
    516     if (rtcp_sender_.TimeToSendRTCPReport(kVideoFrameKey == frame_type)) {
    517       rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
    518     }
    519     return rtp_sender_.SendOutgoingData(frame_type,
    520                                         payload_type,
    521                                         time_stamp,
    522                                         capture_time_ms,
    523                                         payload_data,
    524                                         payload_size,
    525                                         fragmentation,
    526                                         NULL,
    527                                         &(rtp_video_hdr->codecHeader));
    528   }
    529   int32_t ret_val = -1;
    530   CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    531   if (simulcast_) {
    532     if (rtp_video_hdr == NULL) {
    533       return -1;
    534     }
    535     int idx = 0;
    536     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
    537     for (; idx < rtp_video_hdr->simulcastIdx; ++it) {
    538       if (it == child_modules_.end()) {
    539         return -1;
    540       }
    541       if ((*it)->SendingMedia()) {
    542         ++idx;
    543       }
    544     }
    545     for (; it != child_modules_.end(); ++it) {
    546       if ((*it)->SendingMedia()) {
    547         break;
    548       }
    549       ++idx;
    550     }
    551     if (it == child_modules_.end()) {
    552       return -1;
    553     }
    554     return (*it)->SendOutgoingData(frame_type,
    555                                    payload_type,
    556                                    time_stamp,
    557                                    capture_time_ms,
    558                                    payload_data,
    559                                    payload_size,
    560                                    fragmentation,
    561                                    rtp_video_hdr);
    562   } else {
    563     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
    564     // Send to all "child" modules
    565     while (it != child_modules_.end()) {
    566       if ((*it)->SendingMedia()) {
    567         ret_val = (*it)->SendOutgoingData(frame_type,
    568                                           payload_type,
    569                                           time_stamp,
    570                                           capture_time_ms,
    571                                           payload_data,
    572                                           payload_size,
    573                                           fragmentation,
    574                                           rtp_video_hdr);
    575       }
    576       it++;
    577     }
    578   }
    579   return ret_val;
    580 }
    581 
    582 bool ModuleRtpRtcpImpl::TimeToSendPacket(uint32_t ssrc,
    583                                          uint16_t sequence_number,
    584                                          int64_t capture_time_ms,
    585                                          bool retransmission) {
    586   if (!IsDefaultModule()) {
    587     // Don't send from default module.
    588     if (SendingMedia() && ssrc == rtp_sender_.SSRC()) {
    589       return rtp_sender_.TimeToSendPacket(sequence_number, capture_time_ms,
    590                                           retransmission);
    591     }
    592   } else {
    593     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    594     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
    595     while (it != child_modules_.end()) {
    596       if ((*it)->SendingMedia() && ssrc == (*it)->rtp_sender_.SSRC()) {
    597         return (*it)->rtp_sender_.TimeToSendPacket(sequence_number,
    598                                                    capture_time_ms,
    599                                                    retransmission);
    600       }
    601       ++it;
    602     }
    603   }
    604   // No RTP sender is interested in sending this packet.
    605   return true;
    606 }
    607 
    608 int ModuleRtpRtcpImpl::TimeToSendPadding(int bytes) {
    609   if (!IsDefaultModule()) {
    610     // Don't send from default module.
    611     return rtp_sender_.TimeToSendPadding(bytes);
    612   } else {
    613     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    614     for (size_t i = 0; i < child_modules_.size(); ++i) {
    615       // Send padding on one of the modules sending media.
    616       if (child_modules_[i]->SendingMedia()) {
    617         return child_modules_[i]->rtp_sender_.TimeToSendPadding(bytes);
    618       }
    619     }
    620   }
    621   return 0;
    622 }
    623 
    624 bool ModuleRtpRtcpImpl::GetSendSideDelay(int* avg_send_delay_ms,
    625                                          int* max_send_delay_ms) const {
    626   assert(avg_send_delay_ms);
    627   assert(max_send_delay_ms);
    628 
    629   if (IsDefaultModule()) {
    630     // This API is only supported for child modules.
    631     return false;
    632   }
    633   return rtp_sender_.GetSendSideDelay(avg_send_delay_ms, max_send_delay_ms);
    634 }
    635 
    636 uint16_t ModuleRtpRtcpImpl::MaxPayloadLength() const {
    637   return rtp_sender_.MaxPayloadLength();
    638 }
    639 
    640 uint16_t ModuleRtpRtcpImpl::MaxDataPayloadLength() const {
    641   // Assuming IP/UDP.
    642   uint16_t min_data_payload_length = IP_PACKET_SIZE - 28;
    643 
    644   if (IsDefaultModule()) {
    645     // For default we need to update all child modules too.
    646     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
    647     std::vector<ModuleRtpRtcpImpl*>::const_iterator it = child_modules_.begin();
    648     while (it != child_modules_.end()) {
    649       RtpRtcp* module = *it;
    650       if (module) {
    651         uint16_t data_payload_length =
    652           module->MaxDataPayloadLength();
    653         if (data_payload_length < min_data_payload_length) {
    654           min_data_payload_length = data_payload_length;
    655         }
    656       }
    657       it++;
    658     }
    659   }
    660 
    661   uint16_t data_payload_length = rtp_sender_.MaxDataPayloadLength();
    662   if (data_payload_length < min_data_payload_length) {
    663     min_data_payload_length = data_payload_length;
    664   }
    665   return min_data_payload_length;
    666 }
    667 
    668 int32_t ModuleRtpRtcpImpl::SetTransportOverhead(
    669     const bool tcp,
    670     const bool ipv6,
    671     const uint8_t authentication_overhead) {
    672   uint16_t packet_overhead = 0;
    673   if (ipv6) {
    674     packet_overhead = 40;
    675   } else {
    676     packet_overhead = 20;
    677   }
    678   if (tcp) {
    679     // TCP.
    680     packet_overhead += 20;
    681   } else {
    682     // UDP.
    683     packet_overhead += 8;
    684   }
    685   packet_overhead += authentication_overhead;
    686 
    687   if (packet_overhead == packet_overhead_) {
    688     // Ok same as before.
    689     return 0;
    690   }
    691   // Calc diff.
    692   int16_t packet_over_head_diff = packet_overhead - packet_overhead_;
    693 
    694   // Store new.
    695   packet_overhead_ = packet_overhead;
    696 
    697   uint16_t length =
    698       rtp_sender_.MaxPayloadLength() - packet_over_head_diff;
    699   return rtp_sender_.SetMaxPayloadLength(length, packet_overhead_);
    700 }
    701 
    702 int32_t ModuleRtpRtcpImpl::SetMaxTransferUnit(const uint16_t mtu) {
    703   if (mtu > IP_PACKET_SIZE) {
    704     LOG(LS_ERROR) << "Invalid mtu: " << mtu;
    705     return -1;
    706   }
    707   return rtp_sender_.SetMaxPayloadLength(mtu - packet_overhead_,
    708                                          packet_overhead_);
    709 }
    710 
    711 RTCPMethod ModuleRtpRtcpImpl::RTCP() const {
    712   if (rtcp_sender_.Status() != kRtcpOff) {
    713     return rtcp_receiver_.Status();
    714   }
    715   return kRtcpOff;
    716 }
    717 
    718 // Configure RTCP status i.e on/off.
    719 int32_t ModuleRtpRtcpImpl::SetRTCPStatus(const RTCPMethod method) {
    720   if (rtcp_sender_.SetRTCPStatus(method) == 0) {
    721     return rtcp_receiver_.SetRTCPStatus(method);
    722   }
    723   return -1;
    724 }
    725 
    726 // Only for internal test.
    727 uint32_t ModuleRtpRtcpImpl::LastSendReport(
    728     uint32_t& last_rtcptime) {
    729   return rtcp_sender_.LastSendReport(last_rtcptime);
    730 }
    731 
    732 int32_t ModuleRtpRtcpImpl::SetCNAME(const char c_name[RTCP_CNAME_SIZE]) {
    733   return rtcp_sender_.SetCNAME(c_name);
    734 }
    735 
    736 int32_t ModuleRtpRtcpImpl::AddMixedCNAME(
    737   const uint32_t ssrc,
    738   const char c_name[RTCP_CNAME_SIZE]) {
    739   return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
    740 }
    741 
    742 int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
    743   return rtcp_sender_.RemoveMixedCNAME(ssrc);
    744 }
    745 
    746 int32_t ModuleRtpRtcpImpl::RemoteCNAME(
    747     const uint32_t remote_ssrc,
    748     char c_name[RTCP_CNAME_SIZE]) const {
    749   return rtcp_receiver_.CNAME(remote_ssrc, c_name);
    750 }
    751 
    752 int32_t ModuleRtpRtcpImpl::RemoteNTP(
    753     uint32_t* received_ntpsecs,
    754     uint32_t* received_ntpfrac,
    755     uint32_t* rtcp_arrival_time_secs,
    756     uint32_t* rtcp_arrival_time_frac,
    757     uint32_t* rtcp_timestamp) const {
    758   return rtcp_receiver_.NTP(received_ntpsecs,
    759                             received_ntpfrac,
    760                             rtcp_arrival_time_secs,
    761                             rtcp_arrival_time_frac,
    762                             rtcp_timestamp)
    763              ? 0
    764              : -1;
    765 }
    766 
    767 // Get RoundTripTime.
    768 int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
    769                                uint16_t* rtt,
    770                                uint16_t* avg_rtt,
    771                                uint16_t* min_rtt,
    772                                uint16_t* max_rtt) const {
    773   int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
    774   if (rtt && *rtt == 0) {
    775     // Try to get RTT from RtcpRttStats class.
    776     *rtt = static_cast<uint16_t>(rtt_ms());
    777   }
    778   return ret;
    779 }
    780 
    781 // Reset RoundTripTime statistics.
    782 int32_t ModuleRtpRtcpImpl::ResetRTT(const uint32_t remote_ssrc) {
    783   return rtcp_receiver_.ResetRTT(remote_ssrc);
    784 }
    785 
    786 // Reset RTP data counters for the sending side.
    787 int32_t ModuleRtpRtcpImpl::ResetSendDataCountersRTP() {
    788   rtp_sender_.ResetDataCounters();
    789   return 0;  // TODO(pwestin): change to void.
    790 }
    791 
    792 // Force a send of an RTCP packet.
    793 // Normal SR and RR are triggered via the process function.
    794 int32_t ModuleRtpRtcpImpl::SendRTCP(uint32_t rtcp_packet_type) {
    795   return rtcp_sender_.SendRTCP(GetFeedbackState(), rtcp_packet_type);
    796 }
    797 
    798 int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
    799     const uint8_t sub_type,
    800     const uint32_t name,
    801     const uint8_t* data,
    802     const uint16_t length) {
    803   return  rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
    804 }
    805 
    806 // (XR) VOIP metric.
    807 int32_t ModuleRtpRtcpImpl::SetRTCPVoIPMetrics(
    808   const RTCPVoIPMetric* voip_metric) {
    809   return  rtcp_sender_.SetRTCPVoIPMetrics(voip_metric);
    810 }
    811 
    812 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
    813   return rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
    814 }
    815 
    816 bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
    817   return rtcp_sender_.RtcpXrReceiverReferenceTime();
    818 }
    819 
    820 int32_t ModuleRtpRtcpImpl::DataCountersRTP(
    821     uint32_t* bytes_sent,
    822     uint32_t* packets_sent) const {
    823   StreamDataCounters rtp_stats;
    824   StreamDataCounters rtx_stats;
    825   rtp_sender_.GetDataCounters(&rtp_stats, &rtx_stats);
    826 
    827   if (bytes_sent) {
    828     *bytes_sent = rtp_stats.bytes + rtp_stats.padding_bytes +
    829                   rtp_stats.header_bytes + rtx_stats.bytes +
    830                   rtx_stats.padding_bytes + rtx_stats.header_bytes;
    831   }
    832   if (packets_sent) {
    833     *packets_sent = rtp_stats.packets + rtx_stats.packets;
    834   }
    835   return 0;
    836 }
    837 
    838 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(RTCPSenderInfo* sender_info) {
    839   return rtcp_receiver_.SenderInfoReceived(sender_info);
    840 }
    841 
    842 // Received RTCP report.
    843 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
    844     std::vector<RTCPReportBlock>* receive_blocks) const {
    845   return rtcp_receiver_.StatisticsReceived(receive_blocks);
    846 }
    847 
    848 int32_t ModuleRtpRtcpImpl::AddRTCPReportBlock(
    849     const uint32_t ssrc,
    850     const RTCPReportBlock* report_block) {
    851   return rtcp_sender_.AddExternalReportBlock(ssrc, report_block);
    852 }
    853 
    854 int32_t ModuleRtpRtcpImpl::RemoveRTCPReportBlock(
    855   const uint32_t ssrc) {
    856   return rtcp_sender_.RemoveExternalReportBlock(ssrc);
    857 }
    858 
    859 void ModuleRtpRtcpImpl::GetRtcpPacketTypeCounters(
    860     RtcpPacketTypeCounter* packets_sent,
    861     RtcpPacketTypeCounter* packets_received) const {
    862   rtcp_sender_.GetPacketTypeCounter(packets_sent);
    863   rtcp_receiver_.GetPacketTypeCounter(packets_received);
    864 }
    865 
    866 // (REMB) Receiver Estimated Max Bitrate.
    867 bool ModuleRtpRtcpImpl::REMB() const {
    868   return rtcp_sender_.REMB();
    869 }
    870 
    871 int32_t ModuleRtpRtcpImpl::SetREMBStatus(const bool enable) {
    872   return rtcp_sender_.SetREMBStatus(enable);
    873 }
    874 
    875 int32_t ModuleRtpRtcpImpl::SetREMBData(const uint32_t bitrate,
    876                                        const uint8_t number_of_ssrc,
    877                                        const uint32_t* ssrc) {
    878   return rtcp_sender_.SetREMBData(bitrate, number_of_ssrc, ssrc);
    879 }
    880 
    881 // (IJ) Extended jitter report.
    882 bool ModuleRtpRtcpImpl::IJ() const {
    883   return rtcp_sender_.IJ();
    884 }
    885 
    886 int32_t ModuleRtpRtcpImpl::SetIJStatus(const bool enable) {
    887   return rtcp_sender_.SetIJStatus(enable);
    888 }
    889 
    890 int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
    891     const RTPExtensionType type,
    892     const uint8_t id) {
    893   return rtp_sender_.RegisterRtpHeaderExtension(type, id);
    894 }
    895 
    896 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
    897     const RTPExtensionType type) {
    898   return rtp_sender_.DeregisterRtpHeaderExtension(type);
    899 }
    900 
    901 // (TMMBR) Temporary Max Media Bit Rate.
    902 bool ModuleRtpRtcpImpl::TMMBR() const {
    903   return rtcp_sender_.TMMBR();
    904 }
    905 
    906 int32_t ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
    907   return rtcp_sender_.SetTMMBRStatus(enable);
    908 }
    909 
    910 int32_t ModuleRtpRtcpImpl::SetTMMBN(const TMMBRSet* bounding_set) {
    911   uint32_t max_bitrate_kbit =
    912       rtp_sender_.MaxConfiguredBitrateVideo() / 1000;
    913   return rtcp_sender_.SetTMMBN(bounding_set, max_bitrate_kbit);
    914 }
    915 
    916 // Returns the currently configured retransmission mode.
    917 int ModuleRtpRtcpImpl::SelectiveRetransmissions() const {
    918   return rtp_sender_.SelectiveRetransmissions();
    919 }
    920 
    921 // Enable or disable a retransmission mode, which decides which packets will
    922 // be retransmitted if NACKed.
    923 int ModuleRtpRtcpImpl::SetSelectiveRetransmissions(uint8_t settings) {
    924   return rtp_sender_.SetSelectiveRetransmissions(settings);
    925 }
    926 
    927 // Send a Negative acknowledgment packet.
    928 int32_t ModuleRtpRtcpImpl::SendNACK(const uint16_t* nack_list,
    929                                     const uint16_t size) {
    930   // Use RTT from RtcpRttStats class if provided.
    931   uint16_t rtt = rtt_ms();
    932   if (rtt == 0) {
    933     rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
    934   }
    935 
    936   int64_t wait_time = 5 + ((rtt * 3) >> 1);  // 5 + RTT * 1.5.
    937   if (wait_time == 5) {
    938     wait_time = 100;  // During startup we don't have an RTT.
    939   }
    940   const int64_t now = clock_->TimeInMilliseconds();
    941   const int64_t time_limit = now - wait_time;
    942   uint16_t nackLength = size;
    943   uint16_t start_id = 0;
    944 
    945   if (nack_last_time_sent_full_ < time_limit) {
    946     // Send list. Set the timer to make sure we only send a full NACK list once
    947     // within every time_limit.
    948     nack_last_time_sent_full_ = now;
    949   } else {
    950     // Only send if extended list.
    951     if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
    952       // Last seq num is the same don't send list.
    953       return 0;
    954     } else {
    955       // Send NACKs only for new sequence numbers to avoid re-sending
    956       // NACKs for sequences we have already sent.
    957       for (int i = 0; i < size; ++i)  {
    958         if (nack_last_seq_number_sent_ == nack_list[i]) {
    959           start_id = i + 1;
    960           break;
    961         }
    962       }
    963       nackLength = size - start_id;
    964     }
    965   }
    966   // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
    967   // numbers per RTCP packet.
    968   if (nackLength > kRtcpMaxNackFields) {
    969     nackLength = kRtcpMaxNackFields;
    970   }
    971   nack_last_seq_number_sent_ = nack_list[start_id + nackLength - 1];
    972 
    973   return rtcp_sender_.SendRTCP(
    974       GetFeedbackState(), kRtcpNack, nackLength, &nack_list[start_id]);
    975 }
    976 
    977 // Store the sent packets, needed to answer to a Negative acknowledgment
    978 // requests.
    979 int32_t ModuleRtpRtcpImpl::SetStorePacketsStatus(
    980     const bool enable,
    981     const uint16_t number_to_store) {
    982   rtp_sender_.SetStorePacketsStatus(enable, number_to_store);
    983   return 0;  // TODO(pwestin): change to void.
    984 }
    985 
    986 bool ModuleRtpRtcpImpl::StorePackets() const {
    987   return rtp_sender_.StorePackets();
    988 }
    989 
    990 void ModuleRtpRtcpImpl::RegisterSendChannelRtcpStatisticsCallback(
    991     RtcpStatisticsCallback* callback) {
    992   rtcp_receiver_.RegisterRtcpStatisticsCallback(callback);
    993 }
    994 
    995 RtcpStatisticsCallback* ModuleRtpRtcpImpl::
    996         GetSendChannelRtcpStatisticsCallback() {
    997   return rtcp_receiver_.GetRtcpStatisticsCallback();
    998 }
    999 
   1000 // Send a TelephoneEvent tone using RFC 2833 (4733).
   1001 int32_t ModuleRtpRtcpImpl::SendTelephoneEventOutband(
   1002     const uint8_t key,
   1003     const uint16_t time_ms,
   1004     const uint8_t level) {
   1005   return rtp_sender_.SendTelephoneEvent(key, time_ms, level);
   1006 }
   1007 
   1008 bool ModuleRtpRtcpImpl::SendTelephoneEventActive(
   1009     int8_t& telephone_event) const {
   1010   return rtp_sender_.SendTelephoneEventActive(&telephone_event);
   1011 }
   1012 
   1013 // Set audio packet size, used to determine when it's time to send a DTMF
   1014 // packet in silence (CNG).
   1015 int32_t ModuleRtpRtcpImpl::SetAudioPacketSize(
   1016     const uint16_t packet_size_samples) {
   1017   return rtp_sender_.SetAudioPacketSize(packet_size_samples);
   1018 }
   1019 
   1020 int32_t ModuleRtpRtcpImpl::SetAudioLevel(
   1021     const uint8_t level_d_bov) {
   1022   return rtp_sender_.SetAudioLevel(level_d_bov);
   1023 }
   1024 
   1025 // Set payload type for Redundant Audio Data RFC 2198.
   1026 int32_t ModuleRtpRtcpImpl::SetSendREDPayloadType(
   1027     const int8_t payload_type) {
   1028   return rtp_sender_.SetRED(payload_type);
   1029 }
   1030 
   1031 // Get payload type for Redundant Audio Data RFC 2198.
   1032 int32_t ModuleRtpRtcpImpl::SendREDPayloadType(
   1033     int8_t& payload_type) const {
   1034   return rtp_sender_.RED(&payload_type);
   1035 }
   1036 
   1037 void ModuleRtpRtcpImpl::SetTargetSendBitrate(
   1038     const std::vector<uint32_t>& stream_bitrates) {
   1039   if (IsDefaultModule()) {
   1040     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
   1041     if (simulcast_) {
   1042       std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
   1043       for (size_t i = 0;
   1044            it != child_modules_.end() && i < stream_bitrates.size(); ++it) {
   1045         if ((*it)->SendingMedia()) {
   1046           RTPSender& rtp_sender = (*it)->rtp_sender_;
   1047           rtp_sender.SetTargetBitrate(stream_bitrates[i]);
   1048           ++i;
   1049         }
   1050       }
   1051     } else {
   1052       if (stream_bitrates.size() > 1)
   1053         return;
   1054       std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
   1055       for (; it != child_modules_.end(); ++it) {
   1056         RTPSender& rtp_sender = (*it)->rtp_sender_;
   1057         rtp_sender.SetTargetBitrate(stream_bitrates[0]);
   1058       }
   1059     }
   1060   } else {
   1061     if (stream_bitrates.size() > 1)
   1062       return;
   1063     rtp_sender_.SetTargetBitrate(stream_bitrates[0]);
   1064   }
   1065 }
   1066 
   1067 int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
   1068     const KeyFrameRequestMethod method) {
   1069   key_frame_req_method_ = method;
   1070   return 0;
   1071 }
   1072 
   1073 int32_t ModuleRtpRtcpImpl::RequestKeyFrame() {
   1074   switch (key_frame_req_method_) {
   1075     case kKeyFrameReqFirRtp:
   1076       return rtp_sender_.SendRTPIntraRequest();
   1077     case kKeyFrameReqPliRtcp:
   1078       return SendRTCP(kRtcpPli);
   1079     case kKeyFrameReqFirRtcp:
   1080       return SendRTCP(kRtcpFir);
   1081   }
   1082   return -1;
   1083 }
   1084 
   1085 int32_t ModuleRtpRtcpImpl::SendRTCPSliceLossIndication(
   1086     const uint8_t picture_id) {
   1087   return rtcp_sender_.SendRTCP(
   1088       GetFeedbackState(), kRtcpSli, 0, 0, false, picture_id);
   1089 }
   1090 
   1091 int32_t ModuleRtpRtcpImpl::SetCameraDelay(const int32_t delay_ms) {
   1092   if (IsDefaultModule()) {
   1093     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
   1094     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
   1095     while (it != child_modules_.end()) {
   1096       RtpRtcp* module = *it;
   1097       if (module) {
   1098         module->SetCameraDelay(delay_ms);
   1099       }
   1100       it++;
   1101     }
   1102     return 0;
   1103   }
   1104   return rtcp_sender_.SetCameraDelay(delay_ms);
   1105 }
   1106 
   1107 int32_t ModuleRtpRtcpImpl::SetGenericFECStatus(
   1108     const bool enable,
   1109     const uint8_t payload_type_red,
   1110     const uint8_t payload_type_fec) {
   1111   return rtp_sender_.SetGenericFECStatus(enable,
   1112                                          payload_type_red,
   1113                                          payload_type_fec);
   1114 }
   1115 
   1116 int32_t ModuleRtpRtcpImpl::GenericFECStatus(
   1117     bool& enable,
   1118     uint8_t& payload_type_red,
   1119     uint8_t& payload_type_fec) {
   1120   bool child_enabled = false;
   1121   if (IsDefaultModule()) {
   1122     // For default we need to check all child modules too.
   1123     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
   1124     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
   1125     while (it != child_modules_.end()) {
   1126       RtpRtcp* module = *it;
   1127       if (module)  {
   1128         bool enabled = false;
   1129         uint8_t dummy_ptype_red = 0;
   1130         uint8_t dummy_ptype_fec = 0;
   1131         if (module->GenericFECStatus(enabled,
   1132                                      dummy_ptype_red,
   1133                                      dummy_ptype_fec) == 0 && enabled) {
   1134           child_enabled = true;
   1135           break;
   1136         }
   1137       }
   1138       it++;
   1139     }
   1140   }
   1141   int32_t ret_val = rtp_sender_.GenericFECStatus(&enable,
   1142                                                  &payload_type_red,
   1143                                                  &payload_type_fec);
   1144   if (child_enabled) {
   1145     // Returns true if enabled for any child module.
   1146     enable = child_enabled;
   1147   }
   1148   return ret_val;
   1149 }
   1150 
   1151 int32_t ModuleRtpRtcpImpl::SetFecParameters(
   1152     const FecProtectionParams* delta_params,
   1153     const FecProtectionParams* key_params) {
   1154   if (IsDefaultModule())  {
   1155     // For default we need to update all child modules too.
   1156     CriticalSectionScoped lock(critical_section_module_ptrs_.get());
   1157 
   1158     std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
   1159     while (it != child_modules_.end()) {
   1160       RtpRtcp* module = *it;
   1161       if (module) {
   1162         module->SetFecParameters(delta_params, key_params);
   1163       }
   1164       it++;
   1165     }
   1166     return 0;
   1167   }
   1168   return rtp_sender_.SetFecParameters(delta_params, key_params);
   1169 }
   1170 
   1171 void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
   1172   // Inform about the incoming SSRC.
   1173   rtcp_sender_.SetRemoteSSRC(ssrc);
   1174   rtcp_receiver_.SetRemoteSSRC(ssrc);
   1175 
   1176   // Check for a SSRC collision.
   1177   if (rtp_sender_.SSRC() == ssrc && !collision_detected_) {
   1178     // If we detect a collision change the SSRC but only once.
   1179     collision_detected_ = true;
   1180     uint32_t new_ssrc = rtp_sender_.GenerateNewSSRC();
   1181     if (new_ssrc == 0) {
   1182       // Configured via API ignore.
   1183       return;
   1184     }
   1185     if (kRtcpOff != rtcp_sender_.Status()) {
   1186       // Send RTCP bye on the current SSRC.
   1187       SendRTCP(kRtcpBye);
   1188     }
   1189     // Change local SSRC and inform all objects about the new SSRC.
   1190     rtcp_sender_.SetSSRC(new_ssrc);
   1191     SetRtcpReceiverSsrcs(new_ssrc);
   1192   }
   1193 }
   1194 
   1195 void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
   1196                                     uint32_t* video_rate,
   1197                                     uint32_t* fec_rate,
   1198                                     uint32_t* nack_rate) const {
   1199   if (IsDefaultModule()) {
   1200     // For default we need to update the send bitrate.
   1201     CriticalSectionScoped lock(critical_section_module_ptrs_feedback_.get());
   1202 
   1203     if (total_rate != NULL)
   1204       *total_rate = 0;
   1205     if (video_rate != NULL)
   1206       *video_rate = 0;
   1207     if (fec_rate != NULL)
   1208       *fec_rate = 0;
   1209     if (nack_rate != NULL)
   1210       *nack_rate = 0;
   1211 
   1212     std::vector<ModuleRtpRtcpImpl*>::const_iterator it = child_modules_.begin();
   1213     while (it != child_modules_.end()) {
   1214       RtpRtcp* module = *it;
   1215       if (module) {
   1216         uint32_t child_total_rate = 0;
   1217         uint32_t child_video_rate = 0;
   1218         uint32_t child_fec_rate = 0;
   1219         uint32_t child_nack_rate = 0;
   1220         module->BitrateSent(&child_total_rate,
   1221                             &child_video_rate,
   1222                             &child_fec_rate,
   1223                             &child_nack_rate);
   1224         if (total_rate != NULL && child_total_rate > *total_rate)
   1225           *total_rate = child_total_rate;
   1226         if (video_rate != NULL && child_video_rate > *video_rate)
   1227           *video_rate = child_video_rate;
   1228         if (fec_rate != NULL && child_fec_rate > *fec_rate)
   1229           *fec_rate = child_fec_rate;
   1230         if (nack_rate != NULL && child_nack_rate > *nack_rate)
   1231           *nack_rate = child_nack_rate;
   1232       }
   1233       it++;
   1234     }
   1235     return;
   1236   }
   1237   if (total_rate != NULL)
   1238     *total_rate = rtp_sender_.BitrateSent();
   1239   if (video_rate != NULL)
   1240     *video_rate = rtp_sender_.VideoBitrateSent();
   1241   if (fec_rate != NULL)
   1242     *fec_rate = rtp_sender_.FecOverheadRate();
   1243   if (nack_rate != NULL)
   1244     *nack_rate = rtp_sender_.NackOverheadRate();
   1245 }
   1246 
   1247 void ModuleRtpRtcpImpl::OnRequestIntraFrame() {
   1248   RequestKeyFrame();
   1249 }
   1250 
   1251 void ModuleRtpRtcpImpl::OnRequestSendReport() {
   1252   SendRTCP(kRtcpSr);
   1253 }
   1254 
   1255 int32_t ModuleRtpRtcpImpl::SendRTCPReferencePictureSelection(
   1256     const uint64_t picture_id) {
   1257   return rtcp_sender_.SendRTCP(
   1258       GetFeedbackState(), kRtcpRpsi, 0, 0, false, picture_id);
   1259 }
   1260 
   1261 uint32_t ModuleRtpRtcpImpl::SendTimeOfSendReport(
   1262     const uint32_t send_report) {
   1263   return rtcp_sender_.SendTimeOfSendReport(send_report);
   1264 }
   1265 
   1266 bool ModuleRtpRtcpImpl::SendTimeOfXrRrReport(
   1267     uint32_t mid_ntp, int64_t* time_ms) const {
   1268   return rtcp_sender_.SendTimeOfXrRrReport(mid_ntp, time_ms);
   1269 }
   1270 
   1271 void ModuleRtpRtcpImpl::OnReceivedNACK(
   1272     const std::list<uint16_t>& nack_sequence_numbers) {
   1273   if (!rtp_sender_.StorePackets() ||
   1274       nack_sequence_numbers.size() == 0) {
   1275     return;
   1276   }
   1277   // Use RTT from RtcpRttStats class if provided.
   1278   uint16_t rtt = rtt_ms();
   1279   if (rtt == 0) {
   1280     rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
   1281   }
   1282   rtp_sender_.OnReceivedNACK(nack_sequence_numbers, rtt);
   1283 }
   1284 
   1285 bool ModuleRtpRtcpImpl::LastReceivedNTP(
   1286     uint32_t* rtcp_arrival_time_secs,  // When we got the last report.
   1287     uint32_t* rtcp_arrival_time_frac,
   1288     uint32_t* remote_sr) const {
   1289   // Remote SR: NTP inside the last received (mid 16 bits from sec and frac).
   1290   uint32_t ntp_secs = 0;
   1291   uint32_t ntp_frac = 0;
   1292 
   1293   if (!rtcp_receiver_.NTP(&ntp_secs,
   1294                           &ntp_frac,
   1295                           rtcp_arrival_time_secs,
   1296                           rtcp_arrival_time_frac,
   1297                           NULL)) {
   1298     return false;
   1299   }
   1300   *remote_sr =
   1301       ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
   1302   return true;
   1303 }
   1304 
   1305 bool ModuleRtpRtcpImpl::LastReceivedXrReferenceTimeInfo(
   1306     RtcpReceiveTimeInfo* info) const {
   1307   return rtcp_receiver_.LastReceivedXrReferenceTimeInfo(info);
   1308 }
   1309 
   1310 bool ModuleRtpRtcpImpl::UpdateRTCPReceiveInformationTimers() {
   1311   // If this returns true this channel has timed out.
   1312   // Periodically check if this is true and if so call UpdateTMMBR.
   1313   return rtcp_receiver_.UpdateRTCPReceiveInformationTimers();
   1314 }
   1315 
   1316 // Called from RTCPsender.
   1317 int32_t ModuleRtpRtcpImpl::BoundingSet(bool& tmmbr_owner,
   1318                                        TMMBRSet*& bounding_set) {
   1319   return rtcp_receiver_.BoundingSet(tmmbr_owner, bounding_set);
   1320 }
   1321 
   1322 int64_t ModuleRtpRtcpImpl::RtcpReportInterval() {
   1323   if (audio_)
   1324     return RTCP_INTERVAL_AUDIO_MS;
   1325   else
   1326     return RTCP_INTERVAL_VIDEO_MS;
   1327 }
   1328 
   1329 void ModuleRtpRtcpImpl::SetRtcpReceiverSsrcs(uint32_t main_ssrc) {
   1330   std::set<uint32_t> ssrcs;
   1331   ssrcs.insert(main_ssrc);
   1332   int rtx_mode = kRtxOff;
   1333   uint32_t rtx_ssrc = 0;
   1334   int rtx_payload_type = 0;
   1335   rtp_sender_.RTXStatus(&rtx_mode, &rtx_ssrc, &rtx_payload_type);
   1336   if (rtx_mode != kRtxOff)
   1337     ssrcs.insert(rtx_ssrc);
   1338   rtcp_receiver_.SetSsrcs(main_ssrc, ssrcs);
   1339 }
   1340 
   1341 void ModuleRtpRtcpImpl::set_rtt_ms(uint32_t rtt_ms) {
   1342   CriticalSectionScoped cs(critical_section_rtt_.get());
   1343   rtt_ms_ = rtt_ms;
   1344 }
   1345 
   1346 uint32_t ModuleRtpRtcpImpl::rtt_ms() const {
   1347   CriticalSectionScoped cs(critical_section_rtt_.get());
   1348   return rtt_ms_;
   1349 }
   1350 
   1351 void ModuleRtpRtcpImpl::RegisterSendChannelRtpStatisticsCallback(
   1352     StreamDataCountersCallback* callback) {
   1353   rtp_sender_.RegisterRtpStatisticsCallback(callback);
   1354 }
   1355 
   1356 StreamDataCountersCallback*
   1357     ModuleRtpRtcpImpl::GetSendChannelRtpStatisticsCallback() const {
   1358   return rtp_sender_.GetRtpStatisticsCallback();
   1359 }
   1360 
   1361 bool ModuleRtpRtcpImpl::IsDefaultModule() const {
   1362   CriticalSectionScoped cs(critical_section_module_ptrs_.get());
   1363   return !child_modules_.empty();
   1364 }
   1365 
   1366 }  // Namespace webrtc
   1367