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