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