2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
16 #include "webrtc/common_types.h"
17 #include "webrtc/system_wrappers/interface/logging.h"
18 #include "webrtc/system_wrappers/interface/trace.h"
21 #include "webrtc/modules/rtp_rtcp/test/BWEStandAlone/MatlabPlot.h"
22 extern MatlabEngine eng; // Global variable defined elsewhere.
26 // Disable warning C4355: 'this' : used in base member initializer list.
27 #pragma warning(disable : 4355)
32 RtpRtcp::Configuration::Configuration()
37 receive_statistics(NullObjectReceiveStatistics()),
38 outgoing_transport(NULL),
40 intra_frame_callback(NULL),
41 bandwidth_callback(NULL),
43 audio_messages(NullObjectRtpAudioFeedback()),
44 remote_bitrate_estimator(NULL),
48 RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
49 if (configuration.clock) {
50 return new ModuleRtpRtcpImpl(configuration);
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;
62 ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
63 : rtp_sender_(configuration.id,
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()),
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),
90 key_frame_req_method_(kKeyFrameReqFirRtp),
91 remote_bitrate_(configuration.remote_bitrate_estimator),
95 rtt_stats_(configuration.rtt_stats),
96 critical_section_rtt_(CriticalSectionWrapper::CreateCriticalSection()),
98 send_video_codec_.codecType = kVideoCodecUnknown;
100 if (default_module_) {
101 default_module_->RegisterChildModule(this);
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);
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);
115 ModuleRtpRtcpImpl::~ModuleRtpRtcpImpl() {
116 // All child modules MUST be deleted before deleting the default.
117 assert(child_modules_.empty());
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);
126 eng.DeletePlot(plot1_);
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());
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));
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());
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);
162 // Returns the number of milliseconds until the module want a worker thread
164 int32_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
165 const int64_t now = clock_->TimeInMilliseconds();
166 return kRtpRtcpMaxIdleTimeProcess - (now - last_process_time_);
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;
174 if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
175 rtp_sender_.ProcessBitrate();
176 last_bitrate_process_time_ = now;
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) {
192 rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
193 max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
196 if (rtt_stats_ && max_rtt != 0)
197 rtt_stats_->OnRttUpdate(max_rtt);
200 // Verify receiver reports are delivered and the reported sequence number
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)) {
207 "Timeout: No increase in RTCP RR extended highest sequence number.";
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();
217 rtcp_sender_.SetTargetBitrate(target_bitrate);
221 // Report rtt from receiver.
224 if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
225 rtt_stats_->OnRttUpdate(rtt_ms);
230 // Get processed rtt.
232 last_rtt_process_time_ = now;
234 set_rtt_ms(rtt_stats_->LastProcessedRtt());
238 if (rtcp_sender_.TimeToSendRTCPReport()) {
239 RTCPSender::FeedbackState feedback_state(this);
240 rtcp_sender_.SendRTCP(feedback_state, kRtcpReport);
244 if (UpdateRTCPReceiveInformationTimers()) {
245 // A receiver has timed out
246 rtcp_receiver_.UpdateTMMBR();
251 int32_t ModuleRtpRtcpImpl::SetRTXSendStatus(int mode, bool set_ssrc,
253 rtp_sender_.SetRTXStatus(mode, set_ssrc, ssrc);
257 int32_t ModuleRtpRtcpImpl::RTXSendStatus(int* mode, uint32_t* ssrc,
258 int* payload_type) const {
259 rtp_sender_.RTXStatus(mode, ssrc, payload_type);
263 void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type) {
264 rtp_sender_.SetRtxPayloadType(payload_type);
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);
273 const bool valid_rtcpheader = rtcp_parser.IsValid();
274 if (!valid_rtcpheader) {
275 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
278 RTCPHelp::RTCPPacketInformation rtcp_packet_information;
279 int32_t ret_val = rtcp_receiver_.IncomingRTCPPacket(
280 rtcp_packet_information, &rtcp_parser);
282 rtcp_receiver_.TriggerCallbacksFromRTCPPacket(rtcp_packet_information);
287 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
288 const CodecInst& voice_codec) {
289 return rtp_sender_.RegisterPayload(
293 voice_codec.channels,
294 (voice_codec.rate < 0) ? 0 : voice_codec.rate);
297 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
298 const VideoCodec& video_codec) {
299 send_video_codec_ = video_codec;
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;
306 return rtp_sender_.RegisterPayload(video_codec.plName,
310 video_codec.maxBitrate);
313 int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(
314 const int8_t payload_type) {
315 return rtp_sender_.DeRegisterSendPayload(payload_type);
318 int8_t ModuleRtpRtcpImpl::SendPayloadType() const {
319 return rtp_sender_.SendPayloadType();
322 uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
323 return rtp_sender_.StartTimestamp();
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.
334 uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
335 return rtp_sender_.SequenceNumber();
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.
345 uint32_t ModuleRtpRtcpImpl::SSRC() const {
346 return rtp_sender_.SSRC();
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);
355 return 0; // TODO(pwestin): change to void.
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.
364 int32_t ModuleRtpRtcpImpl::CSRCs(
365 uint32_t arr_of_csrc[kRtpCsrcSize]) const {
366 return rtp_sender_.CSRCs(arr_of_csrc);
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());
376 std::list<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
377 while (it != child_modules_.end()) {
378 RtpRtcp* module = *it;
380 module->SetCSRCs(arr_of_csrc, arr_length);
385 rtcp_sender_.SetCSRCs(arr_of_csrc, arr_length);
386 rtp_sender_.SetCSRCs(arr_of_csrc, arr_length);
388 return 0; // TODO(pwestin): change to void.
391 uint32_t ModuleRtpRtcpImpl::PacketCountSent() const {
392 return rtp_sender_.Packets();
395 uint32_t ModuleRtpRtcpImpl::ByteCountSent() const {
396 return rtp_sender_.Bytes();
399 int ModuleRtpRtcpImpl::CurrentSendFrequencyHz() const {
400 return rtp_sender_.SendPayloadFrequency();
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";
411 collision_detected_ = false;
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);
417 // Make sure the RTCP sender has the same timestamp offset.
418 rtcp_sender_.SetStartTimestamp(rtp_sender_.StartTimestamp());
421 // Make sure that RTCP objects are aware of our SSRC (it could have changed
423 uint32_t SSRC = rtp_sender_.SSRC();
424 rtcp_sender_.SetSSRC(SSRC);
425 SetRtcpReceiverSsrcs(SSRC);
432 bool ModuleRtpRtcpImpl::Sending() const {
433 return rtcp_sender_.Sending();
436 int32_t ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
437 rtp_sender_.SetSendingMediaStatus(sending);
441 bool ModuleRtpRtcpImpl::SendingMedia() const {
442 if (!IsDefaultModule()) {
443 return rtp_sender_.SendingMedia();
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()) {
458 int32_t ModuleRtpRtcpImpl::SendOutgoingData(
459 FrameType frame_type,
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);
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);
475 return rtp_sender_.SendOutgoingData(frame_type,
483 &(rtp_video_hdr->codecHeader));
485 int32_t ret_val = -1;
486 CriticalSectionScoped lock(critical_section_module_ptrs_.get());
488 if (rtp_video_hdr == NULL) {
492 std::list<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
493 for (; idx < rtp_video_hdr->simulcastIdx; ++it) {
494 if (it == child_modules_.end()) {
497 if ((*it)->SendingMedia()) {
501 for (; it != child_modules_.end(); ++it) {
502 if ((*it)->SendingMedia()) {
507 if (it == child_modules_.end()) {
510 return (*it)->SendOutgoingData(frame_type,
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,
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,
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,
560 // No RTP sender is interested in sending this packet.
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);
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);
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);
589 if (IsDefaultModule()) {
590 // This API is only supported for child modules.
593 return rtp_sender_.GetSendSideDelay(avg_send_delay_ms, max_send_delay_ms);
596 uint16_t ModuleRtpRtcpImpl::MaxPayloadLength() const {
597 return rtp_sender_.MaxPayloadLength();
600 uint16_t ModuleRtpRtcpImpl::MaxDataPayloadLength() const {
602 uint16_t min_data_payload_length = IP_PACKET_SIZE - 28;
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;
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;
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;
626 return min_data_payload_length;
629 int32_t ModuleRtpRtcpImpl::SetTransportOverhead(
632 const uint8_t authentication_overhead) {
633 uint16_t packet_overhead = 0;
635 packet_overhead = 40;
637 packet_overhead = 20;
641 packet_overhead += 20;
644 packet_overhead += 8;
646 packet_overhead += authentication_overhead;
648 if (packet_overhead == packet_overhead_) {
649 // Ok same as before.
653 int16_t packet_over_head_diff = packet_overhead - packet_overhead_;
656 packet_overhead_ = packet_overhead;
659 rtp_sender_.MaxPayloadLength() - packet_over_head_diff;
660 return rtp_sender_.SetMaxPayloadLength(length, packet_overhead_);
663 int32_t ModuleRtpRtcpImpl::SetMaxTransferUnit(const uint16_t mtu) {
664 if (mtu > IP_PACKET_SIZE) {
665 LOG(LS_ERROR) << "Invalid mtu: " << mtu;
668 return rtp_sender_.SetMaxPayloadLength(mtu - packet_overhead_,
672 RTCPMethod ModuleRtpRtcpImpl::RTCP() const {
673 if (rtcp_sender_.Status() != kRtcpOff) {
674 return rtcp_receiver_.Status();
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);
687 // Only for internal test.
688 uint32_t ModuleRtpRtcpImpl::LastSendReport(
689 uint32_t& last_rtcptime) {
690 return rtcp_sender_.LastSendReport(last_rtcptime);
693 int32_t ModuleRtpRtcpImpl::SetCNAME(const char c_name[RTCP_CNAME_SIZE]) {
694 return rtcp_sender_.SetCNAME(c_name);
697 int32_t ModuleRtpRtcpImpl::CNAME(char c_name[RTCP_CNAME_SIZE]) {
698 return rtcp_sender_.CNAME(c_name);
701 int32_t ModuleRtpRtcpImpl::AddMixedCNAME(
703 const char c_name[RTCP_CNAME_SIZE]) {
704 return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
707 int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
708 return rtcp_sender_.RemoveMixedCNAME(ssrc);
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);
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,
725 rtcp_arrival_time_secs,
726 rtcp_arrival_time_frac,
730 // Get RoundTripTime.
731 int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
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());
744 // Reset RoundTripTime statistics.
745 int32_t ModuleRtpRtcpImpl::ResetRTT(const uint32_t remote_ssrc) {
746 return rtcp_receiver_.ResetRTT(remote_ssrc);
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.
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);
762 int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
763 const uint8_t sub_type,
766 const uint16_t length) {
767 return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
771 int32_t ModuleRtpRtcpImpl::SetRTCPVoIPMetrics(
772 const RTCPVoIPMetric* voip_metric) {
773 return rtcp_sender_.SetRTCPVoIPMetrics(voip_metric);
776 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
777 return rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
780 bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
781 return rtcp_sender_.RtcpXrReceiverReferenceTime();
784 int32_t ModuleRtpRtcpImpl::DataCountersRTP(
785 uint32_t* bytes_sent,
786 uint32_t* packets_sent) const {
788 *bytes_sent = rtp_sender_.Bytes();
791 *packets_sent = rtp_sender_.Packets();
796 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(RTCPSenderInfo* sender_info) {
797 return rtcp_receiver_.SenderInfoReceived(sender_info);
800 // Received RTCP report.
801 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
802 std::vector<RTCPReportBlock>* receive_blocks) const {
803 return rtcp_receiver_.StatisticsReceived(receive_blocks);
806 int32_t ModuleRtpRtcpImpl::AddRTCPReportBlock(
808 const RTCPReportBlock* report_block) {
809 return rtcp_sender_.AddExternalReportBlock(ssrc, report_block);
812 int32_t ModuleRtpRtcpImpl::RemoveRTCPReportBlock(
813 const uint32_t ssrc) {
814 return rtcp_sender_.RemoveExternalReportBlock(ssrc);
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);
824 // (REMB) Receiver Estimated Max Bitrate.
825 bool ModuleRtpRtcpImpl::REMB() const {
826 return rtcp_sender_.REMB();
829 int32_t ModuleRtpRtcpImpl::SetREMBStatus(const bool enable) {
830 return rtcp_sender_.SetREMBStatus(enable);
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);
839 // (IJ) Extended jitter report.
840 bool ModuleRtpRtcpImpl::IJ() const {
841 return rtcp_sender_.IJ();
844 int32_t ModuleRtpRtcpImpl::SetIJStatus(const bool enable) {
845 return rtcp_sender_.SetIJStatus(enable);
848 int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
849 const RTPExtensionType type,
851 return rtp_sender_.RegisterRtpHeaderExtension(type, id);
854 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
855 const RTPExtensionType type) {
856 return rtp_sender_.DeregisterRtpHeaderExtension(type);
859 // (TMMBR) Temporary Max Media Bit Rate.
860 bool ModuleRtpRtcpImpl::TMMBR() const {
861 return rtcp_sender_.TMMBR();
864 int32_t ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
865 return rtcp_sender_.SetTMMBRStatus(enable);
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);
874 // Returns the currently configured retransmission mode.
875 int ModuleRtpRtcpImpl::SelectiveRetransmissions() const {
876 return rtp_sender_.SelectiveRetransmissions();
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);
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();
891 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
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.
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;
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;
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.
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]) {
921 nackLength = size - start_id;
924 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
925 // numbers per RTCP packet.
926 if (nackLength > kRtcpMaxNackFields) {
927 nackLength = kRtcpMaxNackFields;
929 nack_last_seq_number_sent_ = nack_list[start_id + nackLength - 1];
931 RTCPSender::FeedbackState feedback_state(this);
932 return rtcp_sender_.SendRTCP(
933 feedback_state, kRtcpNack, nackLength, &nack_list[start_id]);
936 // Store the sent packets, needed to answer to a Negative acknowledgment
938 int32_t ModuleRtpRtcpImpl::SetStorePacketsStatus(
940 const uint16_t number_to_store) {
941 rtp_sender_.SetStorePacketsStatus(enable, number_to_store);
942 return 0; // TODO(pwestin): change to void.
945 bool ModuleRtpRtcpImpl::StorePackets() const {
946 return rtp_sender_.StorePackets();
949 void ModuleRtpRtcpImpl::RegisterSendChannelRtcpStatisticsCallback(
950 RtcpStatisticsCallback* callback) {
951 rtcp_receiver_.RegisterRtcpStatisticsCallback(callback);
954 RtcpStatisticsCallback* ModuleRtpRtcpImpl::
955 GetSendChannelRtcpStatisticsCallback() {
956 return rtcp_receiver_.GetRtcpStatisticsCallback();
959 // Send a TelephoneEvent tone using RFC 2833 (4733).
960 int32_t ModuleRtpRtcpImpl::SendTelephoneEventOutband(
962 const uint16_t time_ms,
963 const uint8_t level) {
964 return rtp_sender_.SendTelephoneEvent(key, time_ms, level);
967 bool ModuleRtpRtcpImpl::SendTelephoneEventActive(
968 int8_t& telephone_event) const {
969 return rtp_sender_.SendTelephoneEventActive(&telephone_event);
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);
979 int32_t ModuleRtpRtcpImpl::SetAudioLevel(
980 const uint8_t level_d_bov) {
981 return rtp_sender_.SetAudioLevel(level_d_bov);
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);
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);
996 RtpVideoCodecTypes ModuleRtpRtcpImpl::SendVideoCodec() const {
997 return rtp_sender_.VideoCodecType();
1000 void ModuleRtpRtcpImpl::SetTargetSendBitrate(
1001 const std::vector<uint32_t>& stream_bitrates) {
1002 if (IsDefaultModule()) {
1003 CriticalSectionScoped lock(critical_section_module_ptrs_.get());
1005 std::list<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
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]);
1015 if (stream_bitrates.size() > 1)
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]);
1024 if (stream_bitrates.size() > 1)
1026 rtp_sender_.SetTargetSendBitrate(stream_bitrates[0]);
1030 int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
1031 const KeyFrameRequestMethod method) {
1032 key_frame_req_method_ = method;
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);
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);
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;
1062 module->SetCameraDelay(delay_ms);
1068 return rtcp_sender_.SetCameraDelay(delay_ms);
1071 int32_t ModuleRtpRtcpImpl::SetGenericFECStatus(
1073 const uint8_t payload_type_red,
1074 const uint8_t payload_type_fec) {
1075 return rtp_sender_.SetGenericFECStatus(enable,
1080 int32_t ModuleRtpRtcpImpl::GenericFECStatus(
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;
1092 bool enabled = false;
1093 uint8_t dummy_ptype_red = 0;
1094 uint8_t dummy_ptype_fec = 0;
1095 if (module->GenericFECStatus(enabled,
1097 dummy_ptype_fec) == 0 && enabled) {
1098 child_enabled = true;
1105 int32_t ret_val = rtp_sender_.GenericFECStatus(&enable,
1108 if (child_enabled) {
1109 // Returns true if enabled for any child module.
1110 enable = child_enabled;
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());
1122 std::list<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
1123 while (it != child_modules_.end()) {
1124 RtpRtcp* module = *it;
1126 module->SetFecParameters(delta_params, key_params);
1132 return rtp_sender_.SetFecParameters(delta_params, key_params);
1135 void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
1136 // Inform about the incoming SSRC.
1137 rtcp_sender_.SetRemoteSSRC(ssrc);
1138 rtcp_receiver_.SetRemoteSSRC(ssrc);
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.
1149 if (kRtcpOff != rtcp_sender_.Status()) {
1150 // Send RTCP bye on the current SSRC.
1153 // Change local SSRC and inform all objects about the new SSRC.
1154 rtcp_sender_.SetSSRC(new_ssrc);
1155 SetRtcpReceiverSsrcs(new_ssrc);
1159 void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
1160 uint32_t* video_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());
1167 if (total_rate != NULL)
1169 if (video_rate != NULL)
1171 if (fec_rate != NULL)
1173 if (nack_rate != NULL)
1176 std::list<ModuleRtpRtcpImpl*>::const_iterator it =
1177 child_modules_.begin();
1178 while (it != child_modules_.end()) {
1179 RtpRtcp* module = *it;
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,
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;
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();
1212 void ModuleRtpRtcpImpl::RegisterVideoBitrateObserver(
1213 BitrateStatisticsObserver* observer) {
1214 assert(!IsDefaultModule());
1215 rtp_sender_.RegisterBitrateObserver(observer);
1218 BitrateStatisticsObserver* ModuleRtpRtcpImpl::GetVideoBitrateObserver() const {
1219 return rtp_sender_.GetBitrateObserver();
1222 void ModuleRtpRtcpImpl::OnRequestIntraFrame() {
1226 void ModuleRtpRtcpImpl::OnRequestSendReport() {
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);
1237 uint32_t ModuleRtpRtcpImpl::SendTimeOfSendReport(
1238 const uint32_t send_report) {
1239 return rtcp_sender_.SendTimeOfSendReport(send_report);
1242 bool ModuleRtpRtcpImpl::SendTimeOfXrRrReport(
1243 uint32_t mid_ntp, int64_t* time_ms) const {
1244 return rtcp_sender_.SendTimeOfXrRrReport(mid_ntp, time_ms);
1247 void ModuleRtpRtcpImpl::OnReceivedNACK(
1248 const std::list<uint16_t>& nack_sequence_numbers) {
1249 if (!rtp_sender_.StorePackets() ||
1250 nack_sequence_numbers.size() == 0) {
1253 // Use RTT from RtcpRttStats class if provided.
1254 uint16_t rtt = rtt_ms();
1256 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
1258 rtp_sender_.OnReceivedNACK(nack_sequence_numbers, rtt);
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;
1269 if (-1 == rtcp_receiver_.NTP(&ntp_secs,
1271 &rtcp_arrival_time_secs,
1272 &rtcp_arrival_time_frac,
1276 remote_sr = ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
1280 bool ModuleRtpRtcpImpl::LastReceivedXrReferenceTimeInfo(
1281 RtcpReceiveTimeInfo* info) const {
1282 return rtcp_receiver_.LastReceivedXrReferenceTimeInfo(info);
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();
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);
1297 int64_t ModuleRtpRtcpImpl::RtcpReportInterval() {
1299 return RTCP_INTERVAL_AUDIO_MS;
1301 return RTCP_INTERVAL_VIDEO_MS;
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);
1316 void ModuleRtpRtcpImpl::set_rtt_ms(uint32_t rtt_ms) {
1317 CriticalSectionScoped cs(critical_section_rtt_.get());
1321 uint32_t ModuleRtpRtcpImpl::rtt_ms() const {
1322 CriticalSectionScoped cs(critical_section_rtt_.get());
1326 void ModuleRtpRtcpImpl::RegisterSendChannelRtpStatisticsCallback(
1327 StreamDataCountersCallback* callback) {
1328 rtp_sender_.RegisterRtpStatisticsCallback(callback);
1331 StreamDataCountersCallback*
1332 ModuleRtpRtcpImpl::GetSendChannelRtpStatisticsCallback() const {
1333 return rtp_sender_.GetRtpStatisticsCallback();
1336 void ModuleRtpRtcpImpl::RegisterSendFrameCountObserver(
1337 FrameCountObserver* observer) {
1338 rtp_sender_.RegisterFrameCountObserver(observer);
1341 FrameCountObserver* ModuleRtpRtcpImpl::GetSendFrameCountObserver() const {
1342 return rtp_sender_.GetFrameCountObserver();
1345 bool ModuleRtpRtcpImpl::IsDefaultModule() const {
1346 CriticalSectionScoped cs(critical_section_module_ptrs_.get());
1347 return !child_modules_.empty();
1350 } // Namespace webrtc