blob: ad83c9e0a51fff2e40fa5f5a5decbf76d8d046b0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000012
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000013#include <string.h>
sprang168794c2017-07-06 04:38:06 -070014#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000016#include <set>
Peter Boström9c017252016-02-26 16:26:20 +010017#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020018#include <utility>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000019
Niels Möller59ab1cf2019-02-06 22:48:11 +010020#include "absl/memory/memory.h"
Per Kjellandere11b7d22019-02-21 07:55:59 +010021#include "api/transport/field_trial_based_config.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
23#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/logging.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
niklase@google.com470e71d2011-07-07 08:21:25 +000027#ifdef _WIN32
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +000028// Disable warning C4355: 'this' : used in base member initializer list.
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +000029#pragma warning(disable : 4355)
niklase@google.com470e71d2011-07-07 08:21:25 +000030#endif
31
32namespace webrtc {
sprang168794c2017-07-06 04:38:06 -070033namespace {
34const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5;
35const int64_t kRtpRtcpRttProcessTimeMs = 1000;
36const int64_t kRtpRtcpBitrateProcessTimeMs = 10;
spranga8ae6f22017-09-04 07:23:56 -070037const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
Jiawei Ou8b5d9d82018-11-15 16:44:37 -080038constexpr int32_t kDefaultVideoReportInterval = 1000;
39constexpr int32_t kDefaultAudioReportInterval = 5000;
sprang168794c2017-07-06 04:38:06 -070040} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000041
danilchapd3f3c342017-07-25 04:20:12 -070042RtpRtcp::Configuration::Configuration() = default;
phoglund@webrtc.orga22a9bd2013-01-14 10:01:55 +000043
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +010044std::unique_ptr<RtpRtcp> RtpRtcp::Create(const Configuration& configuration) {
45 RTC_DCHECK(configuration.clock);
46 return absl::make_unique<ModuleRtpRtcpImpl>(configuration);
47}
48
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000049RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
50 if (configuration.clock) {
51 return new ModuleRtpRtcpImpl(configuration);
henrike@webrtc.orgf5da4da2012-02-15 23:54:59 +000052 } else {
pbos@webrtc.org180e5162014-07-11 15:36:26 +000053 // No clock implementation provided, use default clock.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000054 RtpRtcp::Configuration configuration_copy;
Yves Gerey665174f2018-06-19 15:03:05 +020055 memcpy(&configuration_copy, &configuration, sizeof(RtpRtcp::Configuration));
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000056 configuration_copy.clock = Clock::GetRealTimeClock();
pbos@webrtc.org180e5162014-07-11 15:36:26 +000057 return new ModuleRtpRtcpImpl(configuration_copy);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +000058 }
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000061ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
nisse14adba72017-03-20 03:52:39 -070062 : rtcp_sender_(configuration.audio,
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +000063 configuration.clock,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000064 configuration.receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -070065 configuration.rtcp_packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -080066 configuration.event_log,
Jiawei Ou3587b832018-01-31 22:08:26 -080067 configuration.outgoing_transport,
Jiawei Ou8b5d9d82018-11-15 16:44:37 -080068 configuration.rtcp_report_interval_ms > 0
69 ? configuration.rtcp_report_interval_ms
70 : (configuration.audio ? kDefaultAudioReportInterval
71 : kDefaultVideoReportInterval)),
Peter Boströmac547a62015-09-17 23:03:57 +020072 rtcp_receiver_(configuration.clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +020073 configuration.receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000074 configuration.rtcp_packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +000075 configuration.bandwidth_callback,
76 configuration.intra_frame_callback,
Elad Alon0a8562e2019-04-09 11:55:13 +020077 configuration.rtcp_loss_notification_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +020078 configuration.transport_feedback_callback,
spranga790d832016-12-02 07:29:44 -080079 configuration.bitrate_allocation_observer,
Jiawei Ou8b5d9d82018-11-15 16:44:37 -080080 configuration.rtcp_report_interval_ms > 0
81 ? configuration.rtcp_report_interval_ms
82 : (configuration.audio ? kDefaultAudioReportInterval
83 : kDefaultVideoReportInterval),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +000084 this),
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000085 clock_(configuration.clock),
sprang168794c2017-07-06 04:38:06 -070086 last_bitrate_process_time_(clock_->TimeInMilliseconds()),
87 last_rtt_process_time_(clock_->TimeInMilliseconds()),
88 next_process_time_(clock_->TimeInMilliseconds() +
89 kRtpRtcpMaxIdleTimeProcessMs),
asapersson35151f32016-05-02 23:44:01 -070090 packet_overhead_(28), // IPV4 UDP.
Danil Chapovalov9eb6ce12017-12-15 12:25:01 +010091 nack_last_time_sent_full_ms_(0),
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +000092 nack_last_seq_number_sent_(0),
Peter Boströme23e7372015-10-08 11:44:14 +020093 key_frame_req_method_(kKeyFrameReqPliRtcp),
mflodman@webrtc.org7c894b72012-11-26 12:40:15 +000094 remote_bitrate_(configuration.remote_bitrate_estimator),
Niels Möller5fe95102019-03-04 16:49:25 +010095 ack_observer_(configuration.ack_observer),
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000096 rtt_stats_(configuration.rtt_stats),
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000097 rtt_ms_(0) {
Per Kjellandere11b7d22019-02-21 07:55:59 +010098 FieldTrialBasedConfig default_trials;
nisse14adba72017-03-20 03:52:39 -070099 if (!configuration.receiver_only) {
100 rtp_sender_.reset(new RTPSender(
Erik Språng7b52f102018-02-07 14:37:37 +0100101 configuration.audio, configuration.clock,
102 configuration.outgoing_transport, configuration.paced_sender,
Niels Möller59ab1cf2019-02-06 22:48:11 +0100103 configuration.flexfec_sender
104 ? absl::make_optional(configuration.flexfec_sender->ssrc())
105 : absl::nullopt,
nisse14adba72017-03-20 03:52:39 -0700106 configuration.transport_sequence_number_allocator,
107 configuration.transport_feedback_callback,
108 configuration.send_bitrate_observer,
Erik Språng7b52f102018-02-07 14:37:37 +0100109 configuration.send_side_delay_observer, configuration.event_log,
nisse14adba72017-03-20 03:52:39 -0700110 configuration.send_packet_observer,
111 configuration.retransmission_rate_limiter,
Erik Språng7b52f102018-02-07 14:37:37 +0100112 configuration.overhead_observer,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700113 configuration.populate_network2_timestamp,
Johannes Kron9190b822018-10-29 11:22:05 +0100114 configuration.frame_encryptor, configuration.require_frame_encryption,
Per Kjellandere11b7d22019-02-21 07:55:59 +0100115 configuration.extmap_allow_mixed,
116 configuration.field_trials ? *configuration.field_trials
117 : default_trials));
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100118
nisse14adba72017-03-20 03:52:39 -0700119 // Make sure rtcp sender use same timestamp offset as rtp sender.
120 rtcp_sender_.SetTimestampOffset(rtp_sender_->TimestampOffset());
121 }
danilchap71fead22016-08-18 02:01:49 -0700122
123 // Set default packet size limit.
nisse284542b2017-01-10 08:58:32 -0800124 // TODO(nisse): Kind-of duplicates
125 // webrtc::VideoSendStream::Config::Rtp::kDefaultMaxPacketSize.
126 const size_t kTcpOverIpv4HeaderSize = 40;
127 SetMaxRtpPacketSize(IP_PACKET_SIZE - kTcpOverIpv4HeaderSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100130ModuleRtpRtcpImpl::~ModuleRtpRtcpImpl() = default;
131
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000132// Returns the number of milliseconds until the module want a worker thread
133// to call Process.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000134int64_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
sprang168794c2017-07-06 04:38:06 -0700135 return std::max<int64_t>(0,
136 next_process_time_ - clock_->TimeInMilliseconds());
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000139// Process any pending tasks such as timeouts (non time critical events).
pbosa26ac922016-02-25 04:50:01 -0800140void ModuleRtpRtcpImpl::Process() {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000141 const int64_t now = clock_->TimeInMilliseconds();
sprang168794c2017-07-06 04:38:06 -0700142 next_process_time_ = now + kRtpRtcpMaxIdleTimeProcessMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
nisse14adba72017-03-20 03:52:39 -0700144 if (rtp_sender_) {
nisse14adba72017-03-20 03:52:39 -0700145 if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
146 rtp_sender_->ProcessBitrate();
147 last_bitrate_process_time_ = now;
sprang168794c2017-07-06 04:38:06 -0700148 next_process_time_ =
149 std::min(next_process_time_, now + kRtpRtcpBitrateProcessTimeMs);
150 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000151 }
sprang168794c2017-07-06 04:38:06 -0700152
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000153 bool process_rtt = now >= last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs;
154 if (rtcp_sender_.Sending()) {
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200155 // Process RTT if we have received a report block and we haven't
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000156 // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200157 if (rtcp_receiver_.LastReceivedReportBlockMs() > last_rtt_process_time_ &&
158 process_rtt) {
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000159 std::vector<RTCPReportBlock> receive_blocks;
160 rtcp_receiver_.StatisticsReceived(&receive_blocks);
161 int64_t max_rtt = 0;
162 for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
163 it != receive_blocks.end(); ++it) {
164 int64_t rtt = 0;
srte3e69e5c2017-08-09 06:13:45 -0700165 rtcp_receiver_.RTT(it->sender_ssrc, &rtt, NULL, NULL, NULL);
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000166 max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
mflodman@webrtc.orgd7d46882012-02-14 12:49:59 +0000167 }
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000168 // Report the rtt.
169 if (rtt_stats_ && max_rtt != 0)
170 rtt_stats_->OnRttUpdate(max_rtt);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000171 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000172
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000173 // Verify receiver reports are delivered and the reported sequence number
174 // is increasing.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800175 if (rtcp_receiver_.RtcpRrTimeout()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100176 RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800177 } else if (rtcp_receiver_.RtcpRrSequenceNumberTimeout()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG_F(LS_WARNING) << "Timeout: No increase in RTCP RR extended "
179 "highest sequence number.";
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000180 }
181
182 if (remote_bitrate_ && rtcp_sender_.TMMBR()) {
183 unsigned int target_bitrate = 0;
184 std::vector<unsigned int> ssrcs;
185 if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
186 if (!ssrcs.empty()) {
187 target_bitrate = target_bitrate / ssrcs.size();
188 }
189 rtcp_sender_.SetTargetBitrate(target_bitrate);
190 }
191 }
192 } else {
193 // Report rtt from receiver.
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000194 if (process_rtt) {
Yves Gerey665174f2018-06-19 15:03:05 +0200195 int64_t rtt_ms;
196 if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
197 rtt_stats_->OnRttUpdate(rtt_ms);
198 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000199 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000200 }
201
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000202 // Get processed rtt.
203 if (process_rtt) {
204 last_rtt_process_time_ = now;
sprang168794c2017-07-06 04:38:06 -0700205 next_process_time_ = std::min(
206 next_process_time_, last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs);
sprange2d83d62016-02-19 09:03:26 -0800207 if (rtt_stats_) {
208 // Make sure we have a valid RTT before setting.
209 int64_t last_rtt = rtt_stats_->LastProcessedRtt();
210 if (last_rtt >= 0)
211 set_rtt_ms(last_rtt);
212 }
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000213 }
214
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200215 if (rtcp_sender_.TimeToSendRTCPReport())
216 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
mflodman@webrtc.org9dd0ebc2015-02-26 12:57:47 +0000217
danilchap9bf610e2017-02-20 06:03:01 -0800218 if (TMMBR() && rtcp_receiver_.UpdateTmmbrTimers()) {
219 rtcp_receiver_.NotifyTmmbrUpdated();
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000220 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000221}
222
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000223void ModuleRtpRtcpImpl::SetRtxSendStatus(int mode) {
nisse14adba72017-03-20 03:52:39 -0700224 rtp_sender_->SetRtxStatus(mode);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000225}
226
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000227int ModuleRtpRtcpImpl::RtxSendStatus() const {
nisse14adba72017-03-20 03:52:39 -0700228 return rtp_sender_ ? rtp_sender_->RtxStatus() : kRtxOff;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000229}
230
231void ModuleRtpRtcpImpl::SetRtxSsrc(uint32_t ssrc) {
nisse14adba72017-03-20 03:52:39 -0700232 rtp_sender_->SetRtxSsrc(ssrc);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000233}
234
Shao Changbine62202f2015-04-21 20:24:50 +0800235void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type,
236 int associated_payload_type) {
nisse14adba72017-03-20 03:52:39 -0700237 rtp_sender_->SetRtxPayloadType(payload_type, associated_payload_type);
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000238}
239
Danil Chapovalovd264df52018-06-14 12:59:38 +0200240absl::optional<uint32_t> ModuleRtpRtcpImpl::FlexfecSsrc() const {
brandtr7c7796b2017-07-03 06:02:53 -0700241 if (rtp_sender_)
242 return rtp_sender_->FlexfecSsrc();
Danil Chapovalovd264df52018-06-14 12:59:38 +0200243 return absl::nullopt;
brandtr9dfff292016-11-14 05:14:50 -0800244}
245
nisse479d3d72017-09-13 07:53:37 -0700246void ModuleRtpRtcpImpl::IncomingRtcpPacket(const uint8_t* rtcp_packet,
247 const size_t length) {
248 rtcp_receiver_.IncomingPacket(rtcp_packet, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000249}
250
Niels Möller5fe95102019-03-04 16:49:25 +0100251void ModuleRtpRtcpImpl::RegisterSendPayloadFrequency(int payload_type,
252 int payload_frequency) {
253 rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
Peter Boström8b79b072016-02-26 16:31:37 +0100254}
255
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +0000256int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(const int8_t payload_type) {
Niels Möller59ab1cf2019-02-06 22:48:11 +0100257 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000260uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
nisse14adba72017-03-20 03:52:39 -0700261 return rtp_sender_->TimestampOffset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000262}
263
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000264// Configure start timestamp, default is a random number.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000265void ModuleRtpRtcpImpl::SetStartTimestamp(const uint32_t timestamp) {
danilchap71fead22016-08-18 02:01:49 -0700266 rtcp_sender_.SetTimestampOffset(timestamp);
nisse14adba72017-03-20 03:52:39 -0700267 rtp_sender_->SetTimestampOffset(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000270uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
nisse14adba72017-03-20 03:52:39 -0700271 return rtp_sender_->SequenceNumber();
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000274// Set SequenceNumber, default is a random number.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000275void ModuleRtpRtcpImpl::SetSequenceNumber(const uint16_t seq_num) {
nisse14adba72017-03-20 03:52:39 -0700276 rtp_sender_->SetSequenceNumber(seq_num);
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
Per83d09102016-04-15 14:59:13 +0200279void ModuleRtpRtcpImpl::SetRtpState(const RtpState& rtp_state) {
nisse14adba72017-03-20 03:52:39 -0700280 rtp_sender_->SetRtpState(rtp_state);
danilchap71fead22016-08-18 02:01:49 -0700281 rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000282}
283
Per83d09102016-04-15 14:59:13 +0200284void ModuleRtpRtcpImpl::SetRtxState(const RtpState& rtp_state) {
nisse14adba72017-03-20 03:52:39 -0700285 rtp_sender_->SetRtxRtpState(rtp_state);
Per83d09102016-04-15 14:59:13 +0200286}
287
288RtpState ModuleRtpRtcpImpl::GetRtpState() const {
nisse14adba72017-03-20 03:52:39 -0700289 return rtp_sender_->GetRtpState();
Per83d09102016-04-15 14:59:13 +0200290}
291
292RtpState ModuleRtpRtcpImpl::GetRtxState() const {
nisse14adba72017-03-20 03:52:39 -0700293 return rtp_sender_->GetRtxRtpState();
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000294}
295
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000296uint32_t ModuleRtpRtcpImpl::SSRC() const {
nisse14adba72017-03-20 03:52:39 -0700297 return rtcp_sender_.SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +0000298}
299
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000300void ModuleRtpRtcpImpl::SetSSRC(const uint32_t ssrc) {
nisse14adba72017-03-20 03:52:39 -0700301 if (rtp_sender_) {
302 rtp_sender_->SetSSRC(ssrc);
303 }
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000304 rtcp_sender_.SetSSRC(ssrc);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000305 SetRtcpReceiverSsrcs(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
Amit Hilbuch77938e62018-12-21 09:23:38 -0800308void ModuleRtpRtcpImpl::SetRid(const std::string& rid) {
309 if (rtp_sender_) {
310 rtp_sender_->SetRid(rid);
311 }
312}
313
Steve Anton296a0ce2018-03-22 15:17:27 -0700314void ModuleRtpRtcpImpl::SetMid(const std::string& mid) {
315 if (rtp_sender_) {
316 rtp_sender_->SetMid(mid);
317 }
318 // TODO(bugs.webrtc.org/4050): If we end up supporting the MID SDES item for
319 // RTCP, this will need to be passed down to the RTCPSender also.
320}
321
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000322void ModuleRtpRtcpImpl::SetCsrcs(const std::vector<uint32_t>& csrcs) {
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000323 rtcp_sender_.SetCsrcs(csrcs);
nisse14adba72017-03-20 03:52:39 -0700324 rtp_sender_->SetCsrcs(csrcs);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000325}
326
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000327// TODO(pbos): Handle media and RTX streams separately (separate RTCP
328// feedbacks).
329RTCPSender::FeedbackState ModuleRtpRtcpImpl::GetFeedbackState() {
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000330 RTCPSender::FeedbackState state;
nisse14adba72017-03-20 03:52:39 -0700331 // This is called also when receiver_only is true. Hence below
332 // checks that rtp_sender_ exists.
333 if (rtp_sender_) {
334 StreamDataCounters rtp_stats;
335 StreamDataCounters rtx_stats;
336 rtp_sender_->GetDataCounters(&rtp_stats, &rtx_stats);
Yves Gerey665174f2018-06-19 15:03:05 +0200337 state.packets_sent =
338 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
nisse14adba72017-03-20 03:52:39 -0700339 state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
340 rtx_stats.transmitted.payload_bytes;
341 state.send_bitrate = rtp_sender_->BitrateSent();
342 }
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000343 state.module = this;
344
Yves Gerey665174f2018-06-19 15:03:05 +0200345 LastReceivedNTP(&state.last_rr_ntp_secs, &state.last_rr_ntp_frac,
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000346 &state.remote_sr);
347
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200348 state.last_xr_rtis = rtcp_receiver_.ConsumeReceivedXrReferenceTimeInfo();
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000349
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000350 return state;
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000351}
352
nisse14adba72017-03-20 03:52:39 -0700353// TODO(nisse): This method shouldn't be called for a receive-only
354// stream. Delete rtp_sender_ check as soon as all applications are
355// updated.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000356int32_t ModuleRtpRtcpImpl::SetSendingStatus(const bool sending) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000357 if (rtcp_sender_.Sending() != sending) {
358 // Sends RTCP BYE when going from true to false
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000359 if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG(LS_WARNING) << "Failed to send RTCP BYE";
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000361 }
nisse14adba72017-03-20 03:52:39 -0700362 if (sending && rtp_sender_) {
nisse7d59f6b2017-02-21 03:40:24 -0800363 // Update Rtcp receiver config, to track Rtx config changes from
364 // the SetRtxStatus and SetRtxSsrc methods.
nisse14adba72017-03-20 03:52:39 -0700365 SetRtcpReceiverSsrcs(rtp_sender_->SSRC());
nisse7d59f6b2017-02-21 03:40:24 -0800366 }
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000367 }
368 return 0;
369}
370
371bool ModuleRtpRtcpImpl::Sending() const {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000372 return rtcp_sender_.Sending();
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000373}
374
nisse14adba72017-03-20 03:52:39 -0700375// TODO(nisse): This method shouldn't be called for a receive-only
376// stream. Delete rtp_sender_ check as soon as all applications are
377// updated.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000378void ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
nisse14adba72017-03-20 03:52:39 -0700379 if (rtp_sender_) {
380 rtp_sender_->SetSendingMediaStatus(sending);
381 } else {
382 RTC_DCHECK(!sending);
383 }
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000384}
385
386bool ModuleRtpRtcpImpl::SendingMedia() const {
nisse14adba72017-03-20 03:52:39 -0700387 return rtp_sender_ ? rtp_sender_->SendingMedia() : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
Sebastian Jansson1bca65b2018-10-10 09:58:08 +0200390void ModuleRtpRtcpImpl::SetAsPartOfAllocation(bool part_of_allocation) {
391 RTC_CHECK(rtp_sender_);
392 rtp_sender_->SetAsPartOfAllocation(part_of_allocation);
393}
394
Niels Möller5fe95102019-03-04 16:49:25 +0100395bool ModuleRtpRtcpImpl::OnSendingRtpFrame(uint32_t timestamp,
396 int64_t capture_time_ms,
397 int payload_type,
398 bool force_sender_report) {
399 if (!Sending())
400 return false;
401
402 rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type);
403 // Make sure an RTCP report isn't queued behind a key frame.
404 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
405 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
406
407 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
Erik Språngd2879622019-05-10 08:29:01 -0700410RtpPacketSendResult ModuleRtpRtcpImpl::TimeToSendPacket(
411 uint32_t ssrc,
412 uint16_t sequence_number,
413 int64_t capture_time_ms,
414 bool retransmission,
415 const PacedPacketInfo& pacing_info) {
nisse14adba72017-03-20 03:52:39 -0700416 return rtp_sender_->TimeToSendPacket(ssrc, sequence_number, capture_time_ms,
Yves Gerey665174f2018-06-19 15:03:05 +0200417 retransmission, pacing_info);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000418}
419
philipelc7bf32a2017-02-17 03:59:43 -0800420size_t ModuleRtpRtcpImpl::TimeToSendPadding(
421 size_t bytes,
422 const PacedPacketInfo& pacing_info) {
nisse14adba72017-03-20 03:52:39 -0700423 return rtp_sender_->TimeToSendPadding(bytes, pacing_info);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000424}
425
nisse284542b2017-01-10 08:58:32 -0800426size_t ModuleRtpRtcpImpl::MaxRtpPacketSize() const {
nisse14adba72017-03-20 03:52:39 -0700427 return rtp_sender_->MaxRtpPacketSize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000428}
429
nisse284542b2017-01-10 08:58:32 -0800430void ModuleRtpRtcpImpl::SetMaxRtpPacketSize(size_t rtp_packet_size) {
431 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
432 << "rtp packet size too large: " << rtp_packet_size;
433 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
434 << "rtp packet size too small: " << rtp_packet_size;
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
nisse284542b2017-01-10 08:58:32 -0800436 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
nisse14adba72017-03-20 03:52:39 -0700437 if (rtp_sender_)
438 rtp_sender_->SetMaxRtpPacketSize(rtp_packet_size);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000439}
440
pbosda903ea2015-10-02 02:36:56 -0700441RtcpMode ModuleRtpRtcpImpl::RTCP() const {
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700442 return rtcp_sender_.Status();
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000443}
444
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000445// Configure RTCP status i.e on/off.
pbosda903ea2015-10-02 02:36:56 -0700446void ModuleRtpRtcpImpl::SetRTCPStatus(const RtcpMode method) {
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000447 rtcp_sender_.SetRTCPStatus(method);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000449
Peter Boström9ba52f82015-06-01 14:12:28 +0200450int32_t ModuleRtpRtcpImpl::SetCNAME(const char* c_name) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000451 return rtcp_sender_.SetCNAME(c_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
Erik Språng0ea42d32015-06-25 14:46:16 +0200454int32_t ModuleRtpRtcpImpl::AddMixedCNAME(uint32_t ssrc, const char* c_name) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000455 return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000458int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000459 return rtcp_sender_.RemoveMixedCNAME(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
Yves Gerey665174f2018-06-19 15:03:05 +0200462int32_t ModuleRtpRtcpImpl::RemoteCNAME(const uint32_t remote_ssrc,
463 char c_name[RTCP_CNAME_SIZE]) const {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000464 return rtcp_receiver_.CNAME(remote_ssrc, c_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
Yves Gerey665174f2018-06-19 15:03:05 +0200467int32_t ModuleRtpRtcpImpl::RemoteNTP(uint32_t* received_ntpsecs,
468 uint32_t* received_ntpfrac,
469 uint32_t* rtcp_arrival_time_secs,
470 uint32_t* rtcp_arrival_time_frac,
471 uint32_t* rtcp_timestamp) const {
472 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
473 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
pbos@webrtc.org376b4ea2014-07-15 15:51:33 +0000474 rtcp_timestamp)
475 ? 0
476 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477}
478
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000479// Get RoundTripTime.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000480int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000481 int64_t* rtt,
482 int64_t* avg_rtt,
483 int64_t* min_rtt,
484 int64_t* max_rtt) const {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000485 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
486 if (rtt && *rtt == 0) {
487 // Try to get RTT from RtcpRttStats class.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000488 *rtt = rtt_ms();
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000489 }
490 return ret;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491}
492
Niels Möller5fe95102019-03-04 16:49:25 +0100493int64_t ModuleRtpRtcpImpl::ExpectedRetransmissionTimeMs() const {
494 int64_t expected_retransmission_time_ms = rtt_ms();
495 if (expected_retransmission_time_ms > 0) {
496 return expected_retransmission_time_ms;
497 }
498 // No rtt available (|kRtpRtcpRttProcessTimeMs| not yet passed?), so try to
499 // poll avg_rtt_ms directly from rtcp receiver.
500 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
501 &expected_retransmission_time_ms, nullptr,
502 nullptr) == 0) {
503 return expected_retransmission_time_ms;
504 }
505 return kDefaultExpectedRetransmissionTimeMs;
506}
507
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000508// Force a send of an RTCP packet.
509// Normal SR and RR are triggered via the process function.
Erik Språng242e22b2015-05-11 10:17:43 +0200510int32_t ModuleRtpRtcpImpl::SendRTCP(RTCPPacketType packet_type) {
511 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
512}
513
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000514int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
515 const uint8_t sub_type,
516 const uint32_t name,
517 const uint8_t* data,
518 const uint16_t length) {
Yves Gerey665174f2018-06-19 15:03:05 +0200519 return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520}
521
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000522void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100523 rtcp_receiver_.SetRtcpXrRrtrStatus(enable);
524 rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000525}
526
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000527bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
528 return rtcp_sender_.RtcpXrReceiverReferenceTime();
529}
530
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000531// TODO(asapersson): Replace this method with the one below.
Yves Gerey665174f2018-06-19 15:03:05 +0200532int32_t ModuleRtpRtcpImpl::DataCountersRTP(size_t* bytes_sent,
533 uint32_t* packets_sent) const {
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000534 StreamDataCounters rtp_stats;
535 StreamDataCounters rtx_stats;
nisse14adba72017-03-20 03:52:39 -0700536 rtp_sender_->GetDataCounters(&rtp_stats, &rtx_stats);
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000537
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000538 if (bytes_sent) {
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200539 // TODO(http://crbug.com/webrtc/10525): Bytes sent should only include
540 // payload bytes, not header and padding bytes.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000541 *bytes_sent = rtp_stats.transmitted.payload_bytes +
542 rtp_stats.transmitted.padding_bytes +
543 rtp_stats.transmitted.header_bytes +
544 rtx_stats.transmitted.payload_bytes +
545 rtx_stats.transmitted.padding_bytes +
546 rtx_stats.transmitted.header_bytes;
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000547 }
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000548 if (packets_sent) {
Yves Gerey665174f2018-06-19 15:03:05 +0200549 *packets_sent =
550 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000551 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000552 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553}
554
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000555void ModuleRtpRtcpImpl::GetSendStreamDataCounters(
556 StreamDataCounters* rtp_counters,
557 StreamDataCounters* rtx_counters) const {
nisse14adba72017-03-20 03:52:39 -0700558 rtp_sender_->GetDataCounters(rtp_counters, rtx_counters);
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000559}
560
bcornell30409b42015-07-10 18:10:05 -0700561void ModuleRtpRtcpImpl::GetRtpPacketLossStats(
562 bool outgoing,
563 uint32_t ssrc,
564 struct RtpPacketLossStats* loss_stats) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200565 if (!loss_stats)
566 return;
bcornell30409b42015-07-10 18:10:05 -0700567 const PacketLossStats* stats_source = NULL;
568 if (outgoing) {
569 if (SSRC() == ssrc) {
570 stats_source = &send_loss_stats_;
571 }
572 } else {
573 if (rtcp_receiver_.RemoteSSRC() == ssrc) {
574 stats_source = &receive_loss_stats_;
575 }
576 }
577 if (stats_source) {
Yves Gerey665174f2018-06-19 15:03:05 +0200578 loss_stats->single_packet_loss_count = stats_source->GetSingleLossCount();
bcornell30409b42015-07-10 18:10:05 -0700579 loss_stats->multiple_packet_loss_event_count =
580 stats_source->GetMultipleLossEventCount();
581 loss_stats->multiple_packet_loss_packet_count =
582 stats_source->GetMultipleLossPacketCount();
583 }
584}
585
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000586// Received RTCP report.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000587int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000588 std::vector<RTCPReportBlock>* receive_blocks) const {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000589 return rtcp_receiver_.StatisticsReceived(receive_blocks);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
Henrik Boström6e436d12019-05-27 12:19:33 +0200592std::vector<ReportBlockData> ModuleRtpRtcpImpl::GetLatestReportBlockData()
593 const {
594 return rtcp_receiver_.GetLatestReportBlockData();
595}
596
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000597// (REMB) Receiver Estimated Max Bitrate.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100598void ModuleRtpRtcpImpl::SetRemb(int64_t bitrate_bps,
599 std::vector<uint32_t> ssrcs) {
600 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000601}
602
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200603void ModuleRtpRtcpImpl::UnsetRemb() {
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200604 rtcp_sender_.UnsetRemb();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000605}
606
Johannes Kron9190b822018-10-29 11:22:05 +0100607void ModuleRtpRtcpImpl::SetExtmapAllowMixed(bool extmap_allow_mixed) {
608 rtp_sender_->SetExtmapAllowMixed(extmap_allow_mixed);
609}
610
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000611int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000612 const RTPExtensionType type,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000613 const uint8_t id) {
nisse14adba72017-03-20 03:52:39 -0700614 return rtp_sender_->RegisterRtpHeaderExtension(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000615}
616
Danil Chapovalov585d1aa2018-09-14 18:29:32 +0200617bool ModuleRtpRtcpImpl::RegisterRtpHeaderExtension(const std::string& uri,
618 int id) {
619 return rtp_sender_->RegisterRtpHeaderExtension(uri, id);
620}
621
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000622int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000623 const RTPExtensionType type) {
nisse14adba72017-03-20 03:52:39 -0700624 return rtp_sender_->DeregisterRtpHeaderExtension(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000625}
626
stefan53b6cc32017-02-03 08:13:57 -0800627bool ModuleRtpRtcpImpl::HasBweExtensions() const {
nisse14adba72017-03-20 03:52:39 -0700628 return rtp_sender_->IsRtpHeaderExtensionRegistered(
stefan53b6cc32017-02-03 08:13:57 -0800629 kRtpExtensionTransportSequenceNumber) ||
nisse14adba72017-03-20 03:52:39 -0700630 rtp_sender_->IsRtpHeaderExtensionRegistered(
stefan53b6cc32017-02-03 08:13:57 -0800631 kRtpExtensionAbsoluteSendTime) ||
nisse14adba72017-03-20 03:52:39 -0700632 rtp_sender_->IsRtpHeaderExtensionRegistered(
stefan53b6cc32017-02-03 08:13:57 -0800633 kRtpExtensionTransmissionTimeOffset);
634}
635
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000636// (TMMBR) Temporary Max Media Bit Rate.
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000637bool ModuleRtpRtcpImpl::TMMBR() const {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000638 return rtcp_sender_.TMMBR();
niklase@google.com470e71d2011-07-07 08:21:25 +0000639}
640
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000641void ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
642 rtcp_sender_.SetTMMBRStatus(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
danilchap853ecb22016-08-22 08:26:15 -0700645void ModuleRtpRtcpImpl::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
646 rtcp_sender_.SetTmmbn(std::move(bounding_set));
niklase@google.com470e71d2011-07-07 08:21:25 +0000647}
648
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000649// Send a Negative acknowledgment packet.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000650int32_t ModuleRtpRtcpImpl::SendNACK(const uint16_t* nack_list,
651 const uint16_t size) {
bcornell30409b42015-07-10 18:10:05 -0700652 for (int i = 0; i < size; ++i) {
653 receive_loss_stats_.AddLostPacket(nack_list[i]);
654 }
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000655 uint16_t nack_length = size;
656 uint16_t start_id = 0;
Danil Chapovalov9eb6ce12017-12-15 12:25:01 +0100657 int64_t now_ms = clock_->TimeInMilliseconds();
658 if (TimeToSendFullNackList(now_ms)) {
659 nack_last_time_sent_full_ms_ = now_ms;
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000660 } else {
661 // Only send extended list.
662 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
663 // Last sequence number is the same, do not send list.
664 return 0;
665 }
666 // Send new sequence numbers.
667 for (int i = 0; i < size; ++i) {
668 if (nack_last_seq_number_sent_ == nack_list[i]) {
669 start_id = i + 1;
670 break;
671 }
672 }
673 nack_length = size - start_id;
674 }
675
676 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
677 // numbers per RTCP packet.
678 if (nack_length > kRtcpMaxNackFields) {
679 nack_length = kRtcpMaxNackFields;
680 }
681 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
682
philipel83f831a2016-03-12 03:30:23 -0800683 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
684 &nack_list[start_id]);
685}
686
687void ModuleRtpRtcpImpl::SendNack(
688 const std::vector<uint16_t>& sequence_numbers) {
689 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
690 sequence_numbers.data());
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000691}
692
693bool ModuleRtpRtcpImpl::TimeToSendFullNackList(int64_t now) const {
asapersson@webrtc.orge7b1e112013-12-16 14:40:36 +0000694 // Use RTT from RtcpRttStats class if provided.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000695 int64_t rtt = rtt_ms();
asapersson@webrtc.orge7b1e112013-12-16 14:40:36 +0000696 if (rtt == 0) {
697 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
698 }
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000699
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000700 const int64_t kStartUpRttMs = 100;
asapersson@webrtc.orge7b1e112013-12-16 14:40:36 +0000701 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000702 if (rtt == 0) {
703 wait_time = kStartUpRttMs;
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000704 }
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000705
asapersson@webrtc.orgba8138b2014-12-08 13:29:02 +0000706 // Send a full NACK list once within every |wait_time|.
Danil Chapovalov9eb6ce12017-12-15 12:25:01 +0100707 return now - nack_last_time_sent_full_ms_ > wait_time;
niklase@google.com470e71d2011-07-07 08:21:25 +0000708}
709
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +0000710// Store the sent packets, needed to answer to Negative acknowledgment requests.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000711void ModuleRtpRtcpImpl::SetStorePacketsStatus(const bool enable,
712 const uint16_t number_to_store) {
nisse14adba72017-03-20 03:52:39 -0700713 rtp_sender_->SetStorePacketsStatus(enable, number_to_store);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000714}
niklase@google.com470e71d2011-07-07 08:21:25 +0000715
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000716bool ModuleRtpRtcpImpl::StorePackets() const {
nisse14adba72017-03-20 03:52:39 -0700717 return rtp_sender_->StorePackets();
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +0000718}
719
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000720void ModuleRtpRtcpImpl::RegisterRtcpStatisticsCallback(
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000721 RtcpStatisticsCallback* callback) {
722 rtcp_receiver_.RegisterRtcpStatisticsCallback(callback);
723}
724
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000725RtcpStatisticsCallback* ModuleRtpRtcpImpl::GetRtcpStatisticsCallback() {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000726 return rtcp_receiver_.GetRtcpStatisticsCallback();
727}
728
Henrik Boström87e3f9d2019-05-27 10:44:24 +0200729void ModuleRtpRtcpImpl::SetReportBlockDataObserver(
730 ReportBlockDataObserver* observer) {
731 return rtcp_receiver_.SetReportBlockDataObserver(observer);
732}
733
sprang233bd872015-09-08 13:25:16 -0700734bool ModuleRtpRtcpImpl::SendFeedbackPacket(
735 const rtcp::TransportFeedback& packet) {
736 return rtcp_sender_.SendFeedbackPacket(packet);
737}
738
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000739int32_t ModuleRtpRtcpImpl::SetKeyFrameRequestMethod(
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000740 const KeyFrameRequestMethod method) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000741 key_frame_req_method_ = method;
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000742 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743}
744
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000745int32_t ModuleRtpRtcpImpl::RequestKeyFrame() {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000746 switch (key_frame_req_method_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 case kKeyFrameReqPliRtcp:
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000748 return SendRTCP(kRtcpPli);
pwestin@webrtc.org5e954812012-02-10 12:13:12 +0000749 case kKeyFrameReqFirRtcp:
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000750 return SendRTCP(kRtcpFir);
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000751 }
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000752 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
Elad Alon7d6a4c02019-02-25 13:00:51 +0100755int32_t ModuleRtpRtcpImpl::SendLossNotification(uint16_t last_decoded_seq_num,
756 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200757 bool decodability_flag,
758 bool buffering_allowed) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100759 return rtcp_sender_.SendLossNotification(
760 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200761 decodability_flag, buffering_allowed);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100762}
763
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000764void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000765 // Inform about the incoming SSRC.
766 rtcp_sender_.SetRemoteSSRC(ssrc);
767 rtcp_receiver_.SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000768}
769
Niels Möller5fe95102019-03-04 16:49:25 +0100770// TODO(nisse): Delete video_rate amd fec_rate arguments.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000771void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
772 uint32_t* video_rate,
773 uint32_t* fec_rate,
774 uint32_t* nack_rate) const {
nisse14adba72017-03-20 03:52:39 -0700775 *total_rate = rtp_sender_->BitrateSent();
Niels Möller5fe95102019-03-04 16:49:25 +0100776 if (video_rate)
777 *video_rate = 0;
778 if (fec_rate)
779 *fec_rate = 0;
nisse14adba72017-03-20 03:52:39 -0700780 *nack_rate = rtp_sender_->NackOverheadRate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000781}
782
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000783void ModuleRtpRtcpImpl::OnRequestSendReport() {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000784 SendRTCP(kRtcpSr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000785}
786
Danil Chapovalov2800d742016-08-26 18:48:46 +0200787void ModuleRtpRtcpImpl::OnReceivedNack(
788 const std::vector<uint16_t>& nack_sequence_numbers) {
nisse14adba72017-03-20 03:52:39 -0700789 if (!rtp_sender_)
790 return;
791
bcornell30409b42015-07-10 18:10:05 -0700792 for (uint16_t nack_sequence_number : nack_sequence_numbers) {
793 send_loss_stats_.AddLostPacket(nack_sequence_number);
794 }
Yves Gerey665174f2018-06-19 15:03:05 +0200795 if (!rtp_sender_->StorePackets() || nack_sequence_numbers.size() == 0) {
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000796 return;
797 }
asapersson@webrtc.orge7b1e112013-12-16 14:40:36 +0000798 // Use RTT from RtcpRttStats class if provided.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000799 int64_t rtt = rtt_ms();
asapersson@webrtc.orge7b1e112013-12-16 14:40:36 +0000800 if (rtt == 0) {
801 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
802 }
nisse14adba72017-03-20 03:52:39 -0700803 rtp_sender_->OnReceivedNack(nack_sequence_numbers, rtt);
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
isheriff6b4b5f32016-06-08 00:24:21 -0700806void ModuleRtpRtcpImpl::OnReceivedRtcpReportBlocks(
807 const ReportBlockList& report_blocks) {
Niels Möller5fe95102019-03-04 16:49:25 +0100808 if (ack_observer_) {
Niels Möller59ab1cf2019-02-06 22:48:11 +0100809 uint32_t ssrc = SSRC();
810
811 for (const RTCPReportBlock& report_block : report_blocks) {
812 if (ssrc == report_block.source_ssrc) {
Niels Möller5fe95102019-03-04 16:49:25 +0100813 ack_observer_->OnReceivedAck(
814 report_block.extended_highest_sequence_number);
Niels Möller59ab1cf2019-02-06 22:48:11 +0100815 }
816 }
817 }
isheriff6b4b5f32016-06-08 00:24:21 -0700818}
819
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000820bool ModuleRtpRtcpImpl::LastReceivedNTP(
821 uint32_t* rtcp_arrival_time_secs, // When we got the last report.
822 uint32_t* rtcp_arrival_time_frac,
823 uint32_t* remote_sr) const {
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000824 // Remote SR: NTP inside the last received (mid 16 bits from sec and frac).
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000825 uint32_t ntp_secs = 0;
826 uint32_t ntp_frac = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000827
Yves Gerey665174f2018-06-19 15:03:05 +0200828 if (!rtcp_receiver_.NTP(&ntp_secs, &ntp_frac, rtcp_arrival_time_secs,
829 rtcp_arrival_time_frac, NULL)) {
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000830 return false;
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000831 }
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000832 *remote_sr =
833 ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
834 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000835}
836
phoglund@webrtc.orgacfdd962013-01-16 10:27:33 +0000837// Called from RTCPsender.
danilchap2b616392016-08-18 06:17:42 -0700838std::vector<rtcp::TmmbItem> ModuleRtpRtcpImpl::BoundingSet(bool* tmmbr_owner) {
839 return rtcp_receiver_.BoundingSet(tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000840}
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000841
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000842void ModuleRtpRtcpImpl::SetRtcpReceiverSsrcs(uint32_t main_ssrc) {
843 std::set<uint32_t> ssrcs;
844 ssrcs.insert(main_ssrc);
nisse14adba72017-03-20 03:52:39 -0700845 if (RtxSendStatus() != kRtxOff)
846 ssrcs.insert(rtp_sender_->RtxSsrc());
Danil Chapovalovd264df52018-06-14 12:59:38 +0200847 absl::optional<uint32_t> flexfec_ssrc = FlexfecSsrc();
brandtr7c7796b2017-07-03 06:02:53 -0700848 if (flexfec_ssrc)
849 ssrcs.insert(*flexfec_ssrc);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000850 rtcp_receiver_.SetSsrcs(main_ssrc, ssrcs);
851}
852
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000853void ModuleRtpRtcpImpl::set_rtt_ms(int64_t rtt_ms) {
danilchap7c9426c2016-04-14 03:05:31 -0700854 rtc::CritScope cs(&critical_section_rtt_);
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000855 rtt_ms_ = rtt_ms;
Erik Språng8b101922018-01-18 11:58:05 -0800856 if (rtp_sender_)
857 rtp_sender_->SetRtt(rtt_ms);
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000858}
859
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000860int64_t ModuleRtpRtcpImpl::rtt_ms() const {
danilchap7c9426c2016-04-14 03:05:31 -0700861 rtc::CritScope cs(&critical_section_rtt_);
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000862 return rtt_ms_;
863}
864
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000865void ModuleRtpRtcpImpl::RegisterSendChannelRtpStatisticsCallback(
866 StreamDataCountersCallback* callback) {
nisse14adba72017-03-20 03:52:39 -0700867 rtp_sender_->RegisterRtpStatisticsCallback(callback);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000868}
869
870StreamDataCountersCallback*
Yves Gerey665174f2018-06-19 15:03:05 +0200871ModuleRtpRtcpImpl::GetSendChannelRtpStatisticsCallback() const {
nisse14adba72017-03-20 03:52:39 -0700872 return rtp_sender_->GetRtpStatisticsCallback();
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000873}
sprang5e38c962016-12-01 05:18:09 -0800874
875void ModuleRtpRtcpImpl::SetVideoBitrateAllocation(
Erik Språng566124a2018-04-23 12:32:22 +0200876 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800877 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
878}
Niels Möller5fe95102019-03-04 16:49:25 +0100879
880RTPSender* ModuleRtpRtcpImpl::RtpSender() {
881 return rtp_sender_.get();
882}
883
884const RTPSender* ModuleRtpRtcpImpl::RtpSender() const {
885 return rtp_sender_.get();
886}
887
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000888} // namespace webrtc