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 // Disable warning C4355: 'this' : used in base member initializer list.
22 #pragma warning(disable : 4355)
27 RtpRtcp::Configuration::Configuration()
32 receive_statistics(NullObjectReceiveStatistics()),
33 outgoing_transport(NULL),
35 intra_frame_callback(NULL),
36 bandwidth_callback(NULL),
38 audio_messages(NullObjectRtpAudioFeedback()),
39 remote_bitrate_estimator(NULL),
43 RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
44 if (configuration.clock) {
45 return new ModuleRtpRtcpImpl(configuration);
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;
57 ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
58 : rtp_sender_(configuration.id,
61 configuration.outgoing_transport,
62 configuration.audio_messages,
63 configuration.paced_sender),
64 rtcp_sender_(configuration.id,
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()),
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),
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()),
93 send_video_codec_.codecType = kVideoCodecUnknown;
95 if (default_module_) {
96 default_module_->RegisterChildModule(this);
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);
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);
110 ModuleRtpRtcpImpl::~ModuleRtpRtcpImpl() {
111 // All child modules MUST be deleted before deleting the default.
112 assert(child_modules_.empty());
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);
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());
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));
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());
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);
151 // Returns the number of milliseconds until the module want a worker thread
153 int32_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
154 const int64_t now = clock_->TimeInMilliseconds();
155 return kRtpRtcpMaxIdleTimeProcess - (now - last_process_time_);
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;
163 if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
164 rtp_sender_.ProcessBitrate();
165 last_bitrate_process_time_ = now;
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) {
181 rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
182 max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
185 if (rtt_stats_ && max_rtt != 0)
186 rtt_stats_->OnRttUpdate(max_rtt);
189 // Verify receiver reports are delivered and the reported sequence number
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)) {
196 "Timeout: No increase in RTCP RR extended highest sequence number.";
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();
206 rtcp_sender_.SetTargetBitrate(target_bitrate);
210 // Report rtt from receiver.
213 if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
214 rtt_stats_->OnRttUpdate(rtt_ms);
219 // Get processed rtt.
221 last_rtt_process_time_ = now;
223 set_rtt_ms(rtt_stats_->LastProcessedRtt());
227 if (rtcp_sender_.TimeToSendRTCPReport()) {
228 RTCPSender::FeedbackState feedback_state(this);
229 rtcp_sender_.SendRTCP(feedback_state, kRtcpReport);
233 if (UpdateRTCPReceiveInformationTimers()) {
234 // A receiver has timed out
235 rtcp_receiver_.UpdateTMMBR();
240 void ModuleRtpRtcpImpl::SetRTXSendStatus(int mode) {
241 rtp_sender_.SetRTXStatus(mode);
244 void ModuleRtpRtcpImpl::RTXSendStatus(int* mode,
246 int* payload_type) const {
247 rtp_sender_.RTXStatus(mode, ssrc, payload_type);
250 void ModuleRtpRtcpImpl::SetRtxSsrc(uint32_t ssrc) {
251 rtp_sender_.SetRtxSsrc(ssrc);
254 void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type) {
255 rtp_sender_.SetRtxPayloadType(payload_type);
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);
264 const bool valid_rtcpheader = rtcp_parser.IsValid();
265 if (!valid_rtcpheader) {
266 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
269 RTCPHelp::RTCPPacketInformation rtcp_packet_information;
270 int32_t ret_val = rtcp_receiver_.IncomingRTCPPacket(
271 rtcp_packet_information, &rtcp_parser);
273 rtcp_receiver_.TriggerCallbacksFromRTCPPacket(rtcp_packet_information);
278 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
279 const CodecInst& voice_codec) {
280 return rtp_sender_.RegisterPayload(
284 voice_codec.channels,
285 (voice_codec.rate < 0) ? 0 : voice_codec.rate);
288 int32_t ModuleRtpRtcpImpl::RegisterSendPayload(
289 const VideoCodec& video_codec) {
290 send_video_codec_ = video_codec;
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;
297 return rtp_sender_.RegisterPayload(video_codec.plName,
301 video_codec.maxBitrate);
304 int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(
305 const int8_t payload_type) {
306 return rtp_sender_.DeRegisterSendPayload(payload_type);
309 int8_t ModuleRtpRtcpImpl::SendPayloadType() const {
310 return rtp_sender_.SendPayloadType();
313 uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
314 return rtp_sender_.StartTimestamp();
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.
325 uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
326 return rtp_sender_.SequenceNumber();
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.
336 uint32_t ModuleRtpRtcpImpl::SSRC() const {
337 return rtp_sender_.SSRC();
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);
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.
353 int32_t ModuleRtpRtcpImpl::CSRCs(
354 uint32_t arr_of_csrc[kRtpCsrcSize]) const {
355 return rtp_sender_.CSRCs(arr_of_csrc);
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());
365 std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
366 while (it != child_modules_.end()) {
367 RtpRtcp* module = *it;
369 module->SetCSRCs(arr_of_csrc, arr_length);
374 rtcp_sender_.SetCSRCs(arr_of_csrc, arr_length);
375 rtp_sender_.SetCSRCs(arr_of_csrc, arr_length);
377 return 0; // TODO(pwestin): change to void.
380 uint32_t ModuleRtpRtcpImpl::PacketCountSent() const {
381 return rtp_sender_.Packets();
384 uint32_t ModuleRtpRtcpImpl::ByteCountSent() const {
385 return rtp_sender_.Bytes();
388 int ModuleRtpRtcpImpl::CurrentSendFrequencyHz() const {
389 return rtp_sender_.SendPayloadFrequency();
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";
400 collision_detected_ = false;
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);
406 // Make sure the RTCP sender has the same timestamp offset.
407 rtcp_sender_.SetStartTimestamp(rtp_sender_.StartTimestamp());
410 // Make sure that RTCP objects are aware of our SSRC (it could have changed
412 uint32_t SSRC = rtp_sender_.SSRC();
413 rtcp_sender_.SetSSRC(SSRC);
414 SetRtcpReceiverSsrcs(SSRC);
421 bool ModuleRtpRtcpImpl::Sending() const {
422 return rtcp_sender_.Sending();
425 int32_t ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
426 rtp_sender_.SetSendingMediaStatus(sending);
430 bool ModuleRtpRtcpImpl::SendingMedia() const {
431 if (!IsDefaultModule()) {
432 return rtp_sender_.SendingMedia();
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()) {
447 int32_t ModuleRtpRtcpImpl::SendOutgoingData(
448 FrameType frame_type,
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);
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);
464 return rtp_sender_.SendOutgoingData(frame_type,
472 &(rtp_video_hdr->codecHeader));
474 int32_t ret_val = -1;
475 CriticalSectionScoped lock(critical_section_module_ptrs_.get());
477 if (rtp_video_hdr == NULL) {
481 std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
482 for (; idx < rtp_video_hdr->simulcastIdx; ++it) {
483 if (it == child_modules_.end()) {
486 if ((*it)->SendingMedia()) {
490 for (; it != child_modules_.end(); ++it) {
491 if ((*it)->SendingMedia()) {
496 if (it == child_modules_.end()) {
499 return (*it)->SendOutgoingData(frame_type,
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,
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,
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,
549 // No RTP sender is interested in sending this packet.
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);
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(
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);
580 if (IsDefaultModule()) {
581 // This API is only supported for child modules.
584 return rtp_sender_.GetSendSideDelay(avg_send_delay_ms, max_send_delay_ms);
587 uint16_t ModuleRtpRtcpImpl::MaxPayloadLength() const {
588 return rtp_sender_.MaxPayloadLength();
591 uint16_t ModuleRtpRtcpImpl::MaxDataPayloadLength() const {
593 uint16_t min_data_payload_length = IP_PACKET_SIZE - 28;
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;
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;
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;
616 return min_data_payload_length;
619 int32_t ModuleRtpRtcpImpl::SetTransportOverhead(
622 const uint8_t authentication_overhead) {
623 uint16_t packet_overhead = 0;
625 packet_overhead = 40;
627 packet_overhead = 20;
631 packet_overhead += 20;
634 packet_overhead += 8;
636 packet_overhead += authentication_overhead;
638 if (packet_overhead == packet_overhead_) {
639 // Ok same as before.
643 int16_t packet_over_head_diff = packet_overhead - packet_overhead_;
646 packet_overhead_ = packet_overhead;
649 rtp_sender_.MaxPayloadLength() - packet_over_head_diff;
650 return rtp_sender_.SetMaxPayloadLength(length, packet_overhead_);
653 int32_t ModuleRtpRtcpImpl::SetMaxTransferUnit(const uint16_t mtu) {
654 if (mtu > IP_PACKET_SIZE) {
655 LOG(LS_ERROR) << "Invalid mtu: " << mtu;
658 return rtp_sender_.SetMaxPayloadLength(mtu - packet_overhead_,
662 RTCPMethod ModuleRtpRtcpImpl::RTCP() const {
663 if (rtcp_sender_.Status() != kRtcpOff) {
664 return rtcp_receiver_.Status();
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);
677 // Only for internal test.
678 uint32_t ModuleRtpRtcpImpl::LastSendReport(
679 uint32_t& last_rtcptime) {
680 return rtcp_sender_.LastSendReport(last_rtcptime);
683 int32_t ModuleRtpRtcpImpl::SetCNAME(const char c_name[RTCP_CNAME_SIZE]) {
684 return rtcp_sender_.SetCNAME(c_name);
687 int32_t ModuleRtpRtcpImpl::CNAME(char c_name[RTCP_CNAME_SIZE]) {
688 return rtcp_sender_.CNAME(c_name);
691 int32_t ModuleRtpRtcpImpl::AddMixedCNAME(
693 const char c_name[RTCP_CNAME_SIZE]) {
694 return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
697 int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
698 return rtcp_sender_.RemoveMixedCNAME(ssrc);
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);
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,
715 rtcp_arrival_time_secs,
716 rtcp_arrival_time_frac,
720 // Get RoundTripTime.
721 int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
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());
734 // Reset RoundTripTime statistics.
735 int32_t ModuleRtpRtcpImpl::ResetRTT(const uint32_t remote_ssrc) {
736 return rtcp_receiver_.ResetRTT(remote_ssrc);
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.
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);
752 int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
753 const uint8_t sub_type,
756 const uint16_t length) {
757 return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
761 int32_t ModuleRtpRtcpImpl::SetRTCPVoIPMetrics(
762 const RTCPVoIPMetric* voip_metric) {
763 return rtcp_sender_.SetRTCPVoIPMetrics(voip_metric);
766 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
767 return rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
770 bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
771 return rtcp_sender_.RtcpXrReceiverReferenceTime();
774 int32_t ModuleRtpRtcpImpl::DataCountersRTP(
775 uint32_t* bytes_sent,
776 uint32_t* packets_sent) const {
778 *bytes_sent = rtp_sender_.Bytes();
781 *packets_sent = rtp_sender_.Packets();
786 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(RTCPSenderInfo* sender_info) {
787 return rtcp_receiver_.SenderInfoReceived(sender_info);
790 // Received RTCP report.
791 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
792 std::vector<RTCPReportBlock>* receive_blocks) const {
793 return rtcp_receiver_.StatisticsReceived(receive_blocks);
796 int32_t ModuleRtpRtcpImpl::AddRTCPReportBlock(
798 const RTCPReportBlock* report_block) {
799 return rtcp_sender_.AddExternalReportBlock(ssrc, report_block);
802 int32_t ModuleRtpRtcpImpl::RemoveRTCPReportBlock(
803 const uint32_t ssrc) {
804 return rtcp_sender_.RemoveExternalReportBlock(ssrc);
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);
814 // (REMB) Receiver Estimated Max Bitrate.
815 bool ModuleRtpRtcpImpl::REMB() const {
816 return rtcp_sender_.REMB();
819 int32_t ModuleRtpRtcpImpl::SetREMBStatus(const bool enable) {
820 return rtcp_sender_.SetREMBStatus(enable);
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);
829 // (IJ) Extended jitter report.
830 bool ModuleRtpRtcpImpl::IJ() const {
831 return rtcp_sender_.IJ();
834 int32_t ModuleRtpRtcpImpl::SetIJStatus(const bool enable) {
835 return rtcp_sender_.SetIJStatus(enable);
838 int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
839 const RTPExtensionType type,
841 return rtp_sender_.RegisterRtpHeaderExtension(type, id);
844 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
845 const RTPExtensionType type) {
846 return rtp_sender_.DeregisterRtpHeaderExtension(type);
849 // (TMMBR) Temporary Max Media Bit Rate.
850 bool ModuleRtpRtcpImpl::TMMBR() const {
851 return rtcp_sender_.TMMBR();
854 int32_t ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
855 return rtcp_sender_.SetTMMBRStatus(enable);
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);
864 // Returns the currently configured retransmission mode.
865 int ModuleRtpRtcpImpl::SelectiveRetransmissions() const {
866 return rtp_sender_.SelectiveRetransmissions();
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);
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();
881 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
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.
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;
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;
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.
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]) {
911 nackLength = size - start_id;
914 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
915 // numbers per RTCP packet.
916 if (nackLength > kRtcpMaxNackFields) {
917 nackLength = kRtcpMaxNackFields;
919 nack_last_seq_number_sent_ = nack_list[start_id + nackLength - 1];
921 RTCPSender::FeedbackState feedback_state(this);
922 return rtcp_sender_.SendRTCP(
923 feedback_state, kRtcpNack, nackLength, &nack_list[start_id]);
926 // Store the sent packets, needed to answer to a Negative acknowledgment
928 int32_t ModuleRtpRtcpImpl::SetStorePacketsStatus(
930 const uint16_t number_to_store) {
931 rtp_sender_.SetStorePacketsStatus(enable, number_to_store);
932 return 0; // TODO(pwestin): change to void.
935 bool ModuleRtpRtcpImpl::StorePackets() const {
936 return rtp_sender_.StorePackets();
939 void ModuleRtpRtcpImpl::RegisterSendChannelRtcpStatisticsCallback(
940 RtcpStatisticsCallback* callback) {
941 rtcp_receiver_.RegisterRtcpStatisticsCallback(callback);
944 RtcpStatisticsCallback* ModuleRtpRtcpImpl::
945 GetSendChannelRtcpStatisticsCallback() {
946 return rtcp_receiver_.GetRtcpStatisticsCallback();
949 // Send a TelephoneEvent tone using RFC 2833 (4733).
950 int32_t ModuleRtpRtcpImpl::SendTelephoneEventOutband(
952 const uint16_t time_ms,
953 const uint8_t level) {
954 return rtp_sender_.SendTelephoneEvent(key, time_ms, level);
957 bool ModuleRtpRtcpImpl::SendTelephoneEventActive(
958 int8_t& telephone_event) const {
959 return rtp_sender_.SendTelephoneEventActive(&telephone_event);
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);
969 int32_t ModuleRtpRtcpImpl::SetAudioLevel(
970 const uint8_t level_d_bov) {
971 return rtp_sender_.SetAudioLevel(level_d_bov);
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);
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);
986 RtpVideoCodecTypes ModuleRtpRtcpImpl::SendVideoCodec() const {
987 return rtp_sender_.VideoCodecType();
990 void ModuleRtpRtcpImpl::SetTargetSendBitrate(
991 const std::vector<uint32_t>& stream_bitrates) {
992 if (IsDefaultModule()) {
993 CriticalSectionScoped lock(critical_section_module_ptrs_.get());
995 std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
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]);
1005 if (stream_bitrates.size() > 1)
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]);
1014 if (stream_bitrates.size() > 1)
1016 rtp_sender_.SetTargetBitrate(stream_bitrates[0]);
1020 int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
1021 const KeyFrameRequestMethod method) {
1022 key_frame_req_method_ = method;
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);
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);
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;
1052 module->SetCameraDelay(delay_ms);
1058 return rtcp_sender_.SetCameraDelay(delay_ms);
1061 int32_t ModuleRtpRtcpImpl::SetGenericFECStatus(
1063 const uint8_t payload_type_red,
1064 const uint8_t payload_type_fec) {
1065 return rtp_sender_.SetGenericFECStatus(enable,
1070 int32_t ModuleRtpRtcpImpl::GenericFECStatus(
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;
1082 bool enabled = false;
1083 uint8_t dummy_ptype_red = 0;
1084 uint8_t dummy_ptype_fec = 0;
1085 if (module->GenericFECStatus(enabled,
1087 dummy_ptype_fec) == 0 && enabled) {
1088 child_enabled = true;
1095 int32_t ret_val = rtp_sender_.GenericFECStatus(&enable,
1098 if (child_enabled) {
1099 // Returns true if enabled for any child module.
1100 enable = child_enabled;
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());
1112 std::vector<ModuleRtpRtcpImpl*>::iterator it = child_modules_.begin();
1113 while (it != child_modules_.end()) {
1114 RtpRtcp* module = *it;
1116 module->SetFecParameters(delta_params, key_params);
1122 return rtp_sender_.SetFecParameters(delta_params, key_params);
1125 void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
1126 // Inform about the incoming SSRC.
1127 rtcp_sender_.SetRemoteSSRC(ssrc);
1128 rtcp_receiver_.SetRemoteSSRC(ssrc);
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.
1139 if (kRtcpOff != rtcp_sender_.Status()) {
1140 // Send RTCP bye on the current SSRC.
1143 // Change local SSRC and inform all objects about the new SSRC.
1144 rtcp_sender_.SetSSRC(new_ssrc);
1145 SetRtcpReceiverSsrcs(new_ssrc);
1149 void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
1150 uint32_t* video_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());
1157 if (total_rate != NULL)
1159 if (video_rate != NULL)
1161 if (fec_rate != NULL)
1163 if (nack_rate != NULL)
1166 std::vector<ModuleRtpRtcpImpl*>::const_iterator it = child_modules_.begin();
1167 while (it != child_modules_.end()) {
1168 RtpRtcp* module = *it;
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,
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;
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();
1201 void ModuleRtpRtcpImpl::RegisterVideoBitrateObserver(
1202 BitrateStatisticsObserver* observer) {
1203 assert(!IsDefaultModule());
1204 rtp_sender_.RegisterBitrateObserver(observer);
1207 BitrateStatisticsObserver* ModuleRtpRtcpImpl::GetVideoBitrateObserver() const {
1208 return rtp_sender_.GetBitrateObserver();
1211 void ModuleRtpRtcpImpl::OnRequestIntraFrame() {
1215 void ModuleRtpRtcpImpl::OnRequestSendReport() {
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);
1226 uint32_t ModuleRtpRtcpImpl::SendTimeOfSendReport(
1227 const uint32_t send_report) {
1228 return rtcp_sender_.SendTimeOfSendReport(send_report);
1231 bool ModuleRtpRtcpImpl::SendTimeOfXrRrReport(
1232 uint32_t mid_ntp, int64_t* time_ms) const {
1233 return rtcp_sender_.SendTimeOfXrRrReport(mid_ntp, time_ms);
1236 void ModuleRtpRtcpImpl::OnReceivedNACK(
1237 const std::list<uint16_t>& nack_sequence_numbers) {
1238 if (!rtp_sender_.StorePackets() ||
1239 nack_sequence_numbers.size() == 0) {
1242 // Use RTT from RtcpRttStats class if provided.
1243 uint16_t rtt = rtt_ms();
1245 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
1247 rtp_sender_.OnReceivedNACK(nack_sequence_numbers, rtt);
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;
1258 if (-1 == rtcp_receiver_.NTP(&ntp_secs,
1260 &rtcp_arrival_time_secs,
1261 &rtcp_arrival_time_frac,
1265 remote_sr = ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
1269 bool ModuleRtpRtcpImpl::LastReceivedXrReferenceTimeInfo(
1270 RtcpReceiveTimeInfo* info) const {
1271 return rtcp_receiver_.LastReceivedXrReferenceTimeInfo(info);
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();
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);
1286 int64_t ModuleRtpRtcpImpl::RtcpReportInterval() {
1288 return RTCP_INTERVAL_AUDIO_MS;
1290 return RTCP_INTERVAL_VIDEO_MS;
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);
1305 void ModuleRtpRtcpImpl::set_rtt_ms(uint32_t rtt_ms) {
1306 CriticalSectionScoped cs(critical_section_rtt_.get());
1310 uint32_t ModuleRtpRtcpImpl::rtt_ms() const {
1311 CriticalSectionScoped cs(critical_section_rtt_.get());
1315 void ModuleRtpRtcpImpl::RegisterSendChannelRtpStatisticsCallback(
1316 StreamDataCountersCallback* callback) {
1317 rtp_sender_.RegisterRtpStatisticsCallback(callback);
1320 StreamDataCountersCallback*
1321 ModuleRtpRtcpImpl::GetSendChannelRtpStatisticsCallback() const {
1322 return rtp_sender_.GetRtpStatisticsCallback();
1325 void ModuleRtpRtcpImpl::RegisterSendFrameCountObserver(
1326 FrameCountObserver* observer) {
1327 rtp_sender_.RegisterFrameCountObserver(observer);
1330 FrameCountObserver* ModuleRtpRtcpImpl::GetSendFrameCountObserver() const {
1331 return rtp_sender_.GetFrameCountObserver();
1334 bool ModuleRtpRtcpImpl::IsDefaultModule() const {
1335 CriticalSectionScoped cs(critical_section_module_ptrs_.get());
1336 return !child_modules_.empty();
1339 } // Namespace webrtc