1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16 #include <cstdint>
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <utility>
21
22 #include "api/transport/field_trial_based_config.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
24 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/logging.h"
27
28 #ifdef _WIN32
29 // Disable warning C4355: 'this' : used in base member initializer list.
30 #pragma warning(disable : 4355)
31 #endif
32
33 namespace webrtc {
34 namespace {
35 const int64_t kRtpRtcpMaxIdleTimeProcessMs = 5;
36 const int64_t kRtpRtcpRttProcessTimeMs = 1000;
37 const int64_t kRtpRtcpBitrateProcessTimeMs = 10;
38 const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
39 } // namespace
40
RtpSenderContext(const RtpRtcp::Configuration & config)41 ModuleRtpRtcpImpl::RtpSenderContext::RtpSenderContext(
42 const RtpRtcp::Configuration& config)
43 : packet_history(config.clock, config.enable_rtx_padding_prioritization),
44 packet_sender(config, &packet_history),
45 non_paced_sender(&packet_sender),
46 packet_generator(
47 config,
48 &packet_history,
49 config.paced_sender ? config.paced_sender : &non_paced_sender) {}
50
51 RtpRtcp::Configuration::Configuration() = default;
52 RtpRtcp::Configuration::Configuration(Configuration&& rhs) = default;
53
Create(const Configuration & configuration)54 std::unique_ptr<RtpRtcp> RtpRtcp::Create(const Configuration& configuration) {
55 RTC_DCHECK(configuration.clock);
56 return std::make_unique<ModuleRtpRtcpImpl>(configuration);
57 }
58
ModuleRtpRtcpImpl(const Configuration & configuration)59 ModuleRtpRtcpImpl::ModuleRtpRtcpImpl(const Configuration& configuration)
60 : rtcp_sender_(configuration),
61 rtcp_receiver_(configuration, this),
62 clock_(configuration.clock),
63 last_bitrate_process_time_(clock_->TimeInMilliseconds()),
64 last_rtt_process_time_(clock_->TimeInMilliseconds()),
65 next_process_time_(clock_->TimeInMilliseconds() +
66 kRtpRtcpMaxIdleTimeProcessMs),
67 packet_overhead_(28), // IPV4 UDP.
68 nack_last_time_sent_full_ms_(0),
69 nack_last_seq_number_sent_(0),
70 remote_bitrate_(configuration.remote_bitrate_estimator),
71 rtt_stats_(configuration.rtt_stats),
72 rtt_ms_(0) {
73 if (!configuration.receiver_only) {
74 rtp_sender_ = std::make_unique<RtpSenderContext>(configuration);
75 // Make sure rtcp sender use same timestamp offset as rtp sender.
76 rtcp_sender_.SetTimestampOffset(
77 rtp_sender_->packet_generator.TimestampOffset());
78 }
79
80 // Set default packet size limit.
81 // TODO(nisse): Kind-of duplicates
82 // webrtc::VideoSendStream::Config::Rtp::kDefaultMaxPacketSize.
83 const size_t kTcpOverIpv4HeaderSize = 40;
84 SetMaxRtpPacketSize(IP_PACKET_SIZE - kTcpOverIpv4HeaderSize);
85 }
86
87 ModuleRtpRtcpImpl::~ModuleRtpRtcpImpl() = default;
88
89 // Returns the number of milliseconds until the module want a worker thread
90 // to call Process.
TimeUntilNextProcess()91 int64_t ModuleRtpRtcpImpl::TimeUntilNextProcess() {
92 return std::max<int64_t>(0,
93 next_process_time_ - clock_->TimeInMilliseconds());
94 }
95
96 // Process any pending tasks such as timeouts (non time critical events).
Process()97 void ModuleRtpRtcpImpl::Process() {
98 const int64_t now = clock_->TimeInMilliseconds();
99 next_process_time_ = now + kRtpRtcpMaxIdleTimeProcessMs;
100
101 if (rtp_sender_) {
102 if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
103 rtp_sender_->packet_sender.ProcessBitrateAndNotifyObservers();
104 last_bitrate_process_time_ = now;
105 next_process_time_ =
106 std::min(next_process_time_, now + kRtpRtcpBitrateProcessTimeMs);
107 }
108 }
109
110 bool process_rtt = now >= last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs;
111 if (rtcp_sender_.Sending()) {
112 // Process RTT if we have received a report block and we haven't
113 // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
114 if (rtcp_receiver_.LastReceivedReportBlockMs() > last_rtt_process_time_ &&
115 process_rtt) {
116 std::vector<RTCPReportBlock> receive_blocks;
117 rtcp_receiver_.StatisticsReceived(&receive_blocks);
118 int64_t max_rtt = 0;
119 for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
120 it != receive_blocks.end(); ++it) {
121 int64_t rtt = 0;
122 rtcp_receiver_.RTT(it->sender_ssrc, &rtt, NULL, NULL, NULL);
123 max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
124 }
125 // Report the rtt.
126 if (rtt_stats_ && max_rtt != 0)
127 rtt_stats_->OnRttUpdate(max_rtt);
128 }
129
130 // Verify receiver reports are delivered and the reported sequence number
131 // is increasing.
132 if (rtcp_receiver_.RtcpRrTimeout()) {
133 RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
134 } else if (rtcp_receiver_.RtcpRrSequenceNumberTimeout()) {
135 RTC_LOG_F(LS_WARNING) << "Timeout: No increase in RTCP RR extended "
136 "highest sequence number.";
137 }
138
139 if (remote_bitrate_ && rtcp_sender_.TMMBR()) {
140 unsigned int target_bitrate = 0;
141 std::vector<unsigned int> ssrcs;
142 if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
143 if (!ssrcs.empty()) {
144 target_bitrate = target_bitrate / ssrcs.size();
145 }
146 rtcp_sender_.SetTargetBitrate(target_bitrate);
147 }
148 }
149 } else {
150 // Report rtt from receiver.
151 if (process_rtt) {
152 int64_t rtt_ms;
153 if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
154 rtt_stats_->OnRttUpdate(rtt_ms);
155 }
156 }
157 }
158
159 // Get processed rtt.
160 if (process_rtt) {
161 last_rtt_process_time_ = now;
162 next_process_time_ = std::min(
163 next_process_time_, last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs);
164 if (rtt_stats_) {
165 // Make sure we have a valid RTT before setting.
166 int64_t last_rtt = rtt_stats_->LastProcessedRtt();
167 if (last_rtt >= 0)
168 set_rtt_ms(last_rtt);
169 }
170 }
171
172 if (rtcp_sender_.TimeToSendRTCPReport())
173 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
174
175 if (TMMBR() && rtcp_receiver_.UpdateTmmbrTimers()) {
176 rtcp_receiver_.NotifyTmmbrUpdated();
177 }
178 }
179
SetRtxSendStatus(int mode)180 void ModuleRtpRtcpImpl::SetRtxSendStatus(int mode) {
181 rtp_sender_->packet_generator.SetRtxStatus(mode);
182 }
183
RtxSendStatus() const184 int ModuleRtpRtcpImpl::RtxSendStatus() const {
185 return rtp_sender_ ? rtp_sender_->packet_generator.RtxStatus() : kRtxOff;
186 }
187
SetRtxSendPayloadType(int payload_type,int associated_payload_type)188 void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type,
189 int associated_payload_type) {
190 rtp_sender_->packet_generator.SetRtxPayloadType(payload_type,
191 associated_payload_type);
192 }
193
RtxSsrc() const194 absl::optional<uint32_t> ModuleRtpRtcpImpl::RtxSsrc() const {
195 return rtp_sender_ ? rtp_sender_->packet_generator.RtxSsrc() : absl::nullopt;
196 }
197
FlexfecSsrc() const198 absl::optional<uint32_t> ModuleRtpRtcpImpl::FlexfecSsrc() const {
199 if (rtp_sender_) {
200 return rtp_sender_->packet_generator.FlexfecSsrc();
201 }
202 return absl::nullopt;
203 }
204
IncomingRtcpPacket(const uint8_t * rtcp_packet,const size_t length)205 void ModuleRtpRtcpImpl::IncomingRtcpPacket(const uint8_t* rtcp_packet,
206 const size_t length) {
207 rtcp_receiver_.IncomingPacket(rtcp_packet, length);
208 }
209
RegisterSendPayloadFrequency(int payload_type,int payload_frequency)210 void ModuleRtpRtcpImpl::RegisterSendPayloadFrequency(int payload_type,
211 int payload_frequency) {
212 rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
213 }
214
DeRegisterSendPayload(const int8_t payload_type)215 int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(const int8_t payload_type) {
216 return 0;
217 }
218
StartTimestamp() const219 uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
220 return rtp_sender_->packet_generator.TimestampOffset();
221 }
222
223 // Configure start timestamp, default is a random number.
SetStartTimestamp(const uint32_t timestamp)224 void ModuleRtpRtcpImpl::SetStartTimestamp(const uint32_t timestamp) {
225 rtcp_sender_.SetTimestampOffset(timestamp);
226 rtp_sender_->packet_generator.SetTimestampOffset(timestamp);
227 rtp_sender_->packet_sender.SetTimestampOffset(timestamp);
228 }
229
SequenceNumber() const230 uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
231 return rtp_sender_->packet_generator.SequenceNumber();
232 }
233
234 // Set SequenceNumber, default is a random number.
SetSequenceNumber(const uint16_t seq_num)235 void ModuleRtpRtcpImpl::SetSequenceNumber(const uint16_t seq_num) {
236 rtp_sender_->packet_generator.SetSequenceNumber(seq_num);
237 }
238
SetRtpState(const RtpState & rtp_state)239 void ModuleRtpRtcpImpl::SetRtpState(const RtpState& rtp_state) {
240 rtp_sender_->packet_generator.SetRtpState(rtp_state);
241 rtp_sender_->packet_sender.SetMediaHasBeenSent(rtp_state.media_has_been_sent);
242 rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
243 }
244
SetRtxState(const RtpState & rtp_state)245 void ModuleRtpRtcpImpl::SetRtxState(const RtpState& rtp_state) {
246 rtp_sender_->packet_generator.SetRtxRtpState(rtp_state);
247 }
248
GetRtpState() const249 RtpState ModuleRtpRtcpImpl::GetRtpState() const {
250 RtpState state = rtp_sender_->packet_generator.GetRtpState();
251 state.media_has_been_sent = rtp_sender_->packet_sender.MediaHasBeenSent();
252 return state;
253 }
254
GetRtxState() const255 RtpState ModuleRtpRtcpImpl::GetRtxState() const {
256 return rtp_sender_->packet_generator.GetRtxRtpState();
257 }
258
SetRid(const std::string & rid)259 void ModuleRtpRtcpImpl::SetRid(const std::string& rid) {
260 if (rtp_sender_) {
261 rtp_sender_->packet_generator.SetRid(rid);
262 }
263 }
264
SetMid(const std::string & mid)265 void ModuleRtpRtcpImpl::SetMid(const std::string& mid) {
266 if (rtp_sender_) {
267 rtp_sender_->packet_generator.SetMid(mid);
268 }
269 // TODO(bugs.webrtc.org/4050): If we end up supporting the MID SDES item for
270 // RTCP, this will need to be passed down to the RTCPSender also.
271 }
272
SetCsrcs(const std::vector<uint32_t> & csrcs)273 void ModuleRtpRtcpImpl::SetCsrcs(const std::vector<uint32_t>& csrcs) {
274 rtcp_sender_.SetCsrcs(csrcs);
275 rtp_sender_->packet_generator.SetCsrcs(csrcs);
276 }
277
278 // TODO(pbos): Handle media and RTX streams separately (separate RTCP
279 // feedbacks).
GetFeedbackState()280 RTCPSender::FeedbackState ModuleRtpRtcpImpl::GetFeedbackState() {
281 RTCPSender::FeedbackState state;
282 // This is called also when receiver_only is true. Hence below
283 // checks that rtp_sender_ exists.
284 if (rtp_sender_) {
285 StreamDataCounters rtp_stats;
286 StreamDataCounters rtx_stats;
287 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
288 state.packets_sent =
289 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
290 state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
291 rtx_stats.transmitted.payload_bytes;
292 state.send_bitrate =
293 rtp_sender_->packet_sender.SendBitrate().bps<uint32_t>();
294 }
295 state.module = this;
296
297 LastReceivedNTP(&state.last_rr_ntp_secs, &state.last_rr_ntp_frac,
298 &state.remote_sr);
299
300 state.last_xr_rtis = rtcp_receiver_.ConsumeReceivedXrReferenceTimeInfo();
301
302 return state;
303 }
304
305 // TODO(nisse): This method shouldn't be called for a receive-only
306 // stream. Delete rtp_sender_ check as soon as all applications are
307 // updated.
SetSendingStatus(const bool sending)308 int32_t ModuleRtpRtcpImpl::SetSendingStatus(const bool sending) {
309 if (rtcp_sender_.Sending() != sending) {
310 // Sends RTCP BYE when going from true to false
311 if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
312 RTC_LOG(LS_WARNING) << "Failed to send RTCP BYE";
313 }
314 }
315 return 0;
316 }
317
Sending() const318 bool ModuleRtpRtcpImpl::Sending() const {
319 return rtcp_sender_.Sending();
320 }
321
322 // TODO(nisse): This method shouldn't be called for a receive-only
323 // stream. Delete rtp_sender_ check as soon as all applications are
324 // updated.
SetSendingMediaStatus(const bool sending)325 void ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
326 if (rtp_sender_) {
327 rtp_sender_->packet_generator.SetSendingMediaStatus(sending);
328 } else {
329 RTC_DCHECK(!sending);
330 }
331 }
332
SendingMedia() const333 bool ModuleRtpRtcpImpl::SendingMedia() const {
334 return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false;
335 }
336
IsAudioConfigured() const337 bool ModuleRtpRtcpImpl::IsAudioConfigured() const {
338 return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured()
339 : false;
340 }
341
SetAsPartOfAllocation(bool part_of_allocation)342 void ModuleRtpRtcpImpl::SetAsPartOfAllocation(bool part_of_allocation) {
343 RTC_CHECK(rtp_sender_);
344 rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation(
345 part_of_allocation);
346 }
347
OnSendingRtpFrame(uint32_t timestamp,int64_t capture_time_ms,int payload_type,bool force_sender_report)348 bool ModuleRtpRtcpImpl::OnSendingRtpFrame(uint32_t timestamp,
349 int64_t capture_time_ms,
350 int payload_type,
351 bool force_sender_report) {
352 if (!Sending())
353 return false;
354
355 rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type);
356 // Make sure an RTCP report isn't queued behind a key frame.
357 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
358 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
359
360 return true;
361 }
362
TrySendPacket(RtpPacketToSend * packet,const PacedPacketInfo & pacing_info)363 bool ModuleRtpRtcpImpl::TrySendPacket(RtpPacketToSend* packet,
364 const PacedPacketInfo& pacing_info) {
365 RTC_DCHECK(rtp_sender_);
366 // TODO(sprang): Consider if we can remove this check.
367 if (!rtp_sender_->packet_generator.SendingMedia()) {
368 return false;
369 }
370 rtp_sender_->packet_sender.SendPacket(packet, pacing_info);
371 return true;
372 }
373
OnPacketsAcknowledged(rtc::ArrayView<const uint16_t> sequence_numbers)374 void ModuleRtpRtcpImpl::OnPacketsAcknowledged(
375 rtc::ArrayView<const uint16_t> sequence_numbers) {
376 RTC_DCHECK(rtp_sender_);
377 rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers);
378 }
379
SupportsPadding() const380 bool ModuleRtpRtcpImpl::SupportsPadding() const {
381 RTC_DCHECK(rtp_sender_);
382 return rtp_sender_->packet_generator.SupportsPadding();
383 }
384
SupportsRtxPayloadPadding() const385 bool ModuleRtpRtcpImpl::SupportsRtxPayloadPadding() const {
386 RTC_DCHECK(rtp_sender_);
387 return rtp_sender_->packet_generator.SupportsRtxPayloadPadding();
388 }
389
390 std::vector<std::unique_ptr<RtpPacketToSend>>
GeneratePadding(size_t target_size_bytes)391 ModuleRtpRtcpImpl::GeneratePadding(size_t target_size_bytes) {
392 RTC_DCHECK(rtp_sender_);
393 return rtp_sender_->packet_generator.GeneratePadding(
394 target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent());
395 }
396
397 std::vector<RtpSequenceNumberMap::Info>
GetSentRtpPacketInfos(rtc::ArrayView<const uint16_t> sequence_numbers) const398 ModuleRtpRtcpImpl::GetSentRtpPacketInfos(
399 rtc::ArrayView<const uint16_t> sequence_numbers) const {
400 RTC_DCHECK(rtp_sender_);
401 return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers);
402 }
403
MaxRtpPacketSize() const404 size_t ModuleRtpRtcpImpl::MaxRtpPacketSize() const {
405 RTC_DCHECK(rtp_sender_);
406 return rtp_sender_->packet_generator.MaxRtpPacketSize();
407 }
408
SetMaxRtpPacketSize(size_t rtp_packet_size)409 void ModuleRtpRtcpImpl::SetMaxRtpPacketSize(size_t rtp_packet_size) {
410 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
411 << "rtp packet size too large: " << rtp_packet_size;
412 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
413 << "rtp packet size too small: " << rtp_packet_size;
414
415 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
416 if (rtp_sender_) {
417 rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size);
418 }
419 }
420
RTCP() const421 RtcpMode ModuleRtpRtcpImpl::RTCP() const {
422 return rtcp_sender_.Status();
423 }
424
425 // Configure RTCP status i.e on/off.
SetRTCPStatus(const RtcpMode method)426 void ModuleRtpRtcpImpl::SetRTCPStatus(const RtcpMode method) {
427 rtcp_sender_.SetRTCPStatus(method);
428 }
429
SetCNAME(const char * c_name)430 int32_t ModuleRtpRtcpImpl::SetCNAME(const char* c_name) {
431 return rtcp_sender_.SetCNAME(c_name);
432 }
433
AddMixedCNAME(uint32_t ssrc,const char * c_name)434 int32_t ModuleRtpRtcpImpl::AddMixedCNAME(uint32_t ssrc, const char* c_name) {
435 return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
436 }
437
RemoveMixedCNAME(const uint32_t ssrc)438 int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
439 return rtcp_sender_.RemoveMixedCNAME(ssrc);
440 }
441
RemoteCNAME(const uint32_t remote_ssrc,char c_name[RTCP_CNAME_SIZE]) const442 int32_t ModuleRtpRtcpImpl::RemoteCNAME(const uint32_t remote_ssrc,
443 char c_name[RTCP_CNAME_SIZE]) const {
444 return rtcp_receiver_.CNAME(remote_ssrc, c_name);
445 }
446
RemoteNTP(uint32_t * received_ntpsecs,uint32_t * received_ntpfrac,uint32_t * rtcp_arrival_time_secs,uint32_t * rtcp_arrival_time_frac,uint32_t * rtcp_timestamp) const447 int32_t ModuleRtpRtcpImpl::RemoteNTP(uint32_t* received_ntpsecs,
448 uint32_t* received_ntpfrac,
449 uint32_t* rtcp_arrival_time_secs,
450 uint32_t* rtcp_arrival_time_frac,
451 uint32_t* rtcp_timestamp) const {
452 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
453 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
454 rtcp_timestamp)
455 ? 0
456 : -1;
457 }
458
459 // Get RoundTripTime.
RTT(const uint32_t remote_ssrc,int64_t * rtt,int64_t * avg_rtt,int64_t * min_rtt,int64_t * max_rtt) const460 int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
461 int64_t* rtt,
462 int64_t* avg_rtt,
463 int64_t* min_rtt,
464 int64_t* max_rtt) const {
465 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
466 if (rtt && *rtt == 0) {
467 // Try to get RTT from RtcpRttStats class.
468 *rtt = rtt_ms();
469 }
470 return ret;
471 }
472
ExpectedRetransmissionTimeMs() const473 int64_t ModuleRtpRtcpImpl::ExpectedRetransmissionTimeMs() const {
474 int64_t expected_retransmission_time_ms = rtt_ms();
475 if (expected_retransmission_time_ms > 0) {
476 return expected_retransmission_time_ms;
477 }
478 // No rtt available (|kRtpRtcpRttProcessTimeMs| not yet passed?), so try to
479 // poll avg_rtt_ms directly from rtcp receiver.
480 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
481 &expected_retransmission_time_ms, nullptr,
482 nullptr) == 0) {
483 return expected_retransmission_time_ms;
484 }
485 return kDefaultExpectedRetransmissionTimeMs;
486 }
487
488 // Force a send of an RTCP packet.
489 // Normal SR and RR are triggered via the process function.
SendRTCP(RTCPPacketType packet_type)490 int32_t ModuleRtpRtcpImpl::SendRTCP(RTCPPacketType packet_type) {
491 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
492 }
493
SetRTCPApplicationSpecificData(const uint8_t sub_type,const uint32_t name,const uint8_t * data,const uint16_t length)494 int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
495 const uint8_t sub_type,
496 const uint32_t name,
497 const uint8_t* data,
498 const uint16_t length) {
499 return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
500 }
501
SetRtcpXrRrtrStatus(bool enable)502 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
503 rtcp_receiver_.SetRtcpXrRrtrStatus(enable);
504 rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
505 }
506
RtcpXrRrtrStatus() const507 bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
508 return rtcp_sender_.RtcpXrReceiverReferenceTime();
509 }
510
511 // TODO(asapersson): Replace this method with the one below.
DataCountersRTP(size_t * bytes_sent,uint32_t * packets_sent) const512 int32_t ModuleRtpRtcpImpl::DataCountersRTP(size_t* bytes_sent,
513 uint32_t* packets_sent) const {
514 StreamDataCounters rtp_stats;
515 StreamDataCounters rtx_stats;
516 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
517
518 if (bytes_sent) {
519 // TODO(http://crbug.com/webrtc/10525): Bytes sent should only include
520 // payload bytes, not header and padding bytes.
521 *bytes_sent = rtp_stats.transmitted.payload_bytes +
522 rtp_stats.transmitted.padding_bytes +
523 rtp_stats.transmitted.header_bytes +
524 rtx_stats.transmitted.payload_bytes +
525 rtx_stats.transmitted.padding_bytes +
526 rtx_stats.transmitted.header_bytes;
527 }
528 if (packets_sent) {
529 *packets_sent =
530 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
531 }
532 return 0;
533 }
534
GetSendStreamDataCounters(StreamDataCounters * rtp_counters,StreamDataCounters * rtx_counters) const535 void ModuleRtpRtcpImpl::GetSendStreamDataCounters(
536 StreamDataCounters* rtp_counters,
537 StreamDataCounters* rtx_counters) const {
538 rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters);
539 }
540
541 // Received RTCP report.
RemoteRTCPStat(std::vector<RTCPReportBlock> * receive_blocks) const542 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
543 std::vector<RTCPReportBlock>* receive_blocks) const {
544 return rtcp_receiver_.StatisticsReceived(receive_blocks);
545 }
546
GetLatestReportBlockData() const547 std::vector<ReportBlockData> ModuleRtpRtcpImpl::GetLatestReportBlockData()
548 const {
549 return rtcp_receiver_.GetLatestReportBlockData();
550 }
551
552 // (REMB) Receiver Estimated Max Bitrate.
SetRemb(int64_t bitrate_bps,std::vector<uint32_t> ssrcs)553 void ModuleRtpRtcpImpl::SetRemb(int64_t bitrate_bps,
554 std::vector<uint32_t> ssrcs) {
555 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
556 }
557
UnsetRemb()558 void ModuleRtpRtcpImpl::UnsetRemb() {
559 rtcp_sender_.UnsetRemb();
560 }
561
SetExtmapAllowMixed(bool extmap_allow_mixed)562 void ModuleRtpRtcpImpl::SetExtmapAllowMixed(bool extmap_allow_mixed) {
563 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
564 }
565
RegisterSendRtpHeaderExtension(const RTPExtensionType type,const uint8_t id)566 int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
567 const RTPExtensionType type,
568 const uint8_t id) {
569 return rtp_sender_->packet_generator.RegisterRtpHeaderExtension(type, id);
570 }
571
RegisterRtpHeaderExtension(absl::string_view uri,int id)572 void ModuleRtpRtcpImpl::RegisterRtpHeaderExtension(absl::string_view uri,
573 int id) {
574 bool registered =
575 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
576 RTC_CHECK(registered);
577 }
578
DeregisterSendRtpHeaderExtension(const RTPExtensionType type)579 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
580 const RTPExtensionType type) {
581 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
582 }
DeregisterSendRtpHeaderExtension(absl::string_view uri)583 void ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
584 absl::string_view uri) {
585 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
586 }
587
588 // (TMMBR) Temporary Max Media Bit Rate.
TMMBR() const589 bool ModuleRtpRtcpImpl::TMMBR() const {
590 return rtcp_sender_.TMMBR();
591 }
592
SetTMMBRStatus(const bool enable)593 void ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
594 rtcp_sender_.SetTMMBRStatus(enable);
595 }
596
SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set)597 void ModuleRtpRtcpImpl::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
598 rtcp_sender_.SetTmmbn(std::move(bounding_set));
599 }
600
601 // Send a Negative acknowledgment packet.
SendNACK(const uint16_t * nack_list,const uint16_t size)602 int32_t ModuleRtpRtcpImpl::SendNACK(const uint16_t* nack_list,
603 const uint16_t size) {
604 uint16_t nack_length = size;
605 uint16_t start_id = 0;
606 int64_t now_ms = clock_->TimeInMilliseconds();
607 if (TimeToSendFullNackList(now_ms)) {
608 nack_last_time_sent_full_ms_ = now_ms;
609 } else {
610 // Only send extended list.
611 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
612 // Last sequence number is the same, do not send list.
613 return 0;
614 }
615 // Send new sequence numbers.
616 for (int i = 0; i < size; ++i) {
617 if (nack_last_seq_number_sent_ == nack_list[i]) {
618 start_id = i + 1;
619 break;
620 }
621 }
622 nack_length = size - start_id;
623 }
624
625 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
626 // numbers per RTCP packet.
627 if (nack_length > kRtcpMaxNackFields) {
628 nack_length = kRtcpMaxNackFields;
629 }
630 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
631
632 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
633 &nack_list[start_id]);
634 }
635
SendNack(const std::vector<uint16_t> & sequence_numbers)636 void ModuleRtpRtcpImpl::SendNack(
637 const std::vector<uint16_t>& sequence_numbers) {
638 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
639 sequence_numbers.data());
640 }
641
TimeToSendFullNackList(int64_t now) const642 bool ModuleRtpRtcpImpl::TimeToSendFullNackList(int64_t now) const {
643 // Use RTT from RtcpRttStats class if provided.
644 int64_t rtt = rtt_ms();
645 if (rtt == 0) {
646 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
647 }
648
649 const int64_t kStartUpRttMs = 100;
650 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
651 if (rtt == 0) {
652 wait_time = kStartUpRttMs;
653 }
654
655 // Send a full NACK list once within every |wait_time|.
656 return now - nack_last_time_sent_full_ms_ > wait_time;
657 }
658
659 // Store the sent packets, needed to answer to Negative acknowledgment requests.
SetStorePacketsStatus(const bool enable,const uint16_t number_to_store)660 void ModuleRtpRtcpImpl::SetStorePacketsStatus(const bool enable,
661 const uint16_t number_to_store) {
662 rtp_sender_->packet_history.SetStorePacketsStatus(
663 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
664 : RtpPacketHistory::StorageMode::kDisabled,
665 number_to_store);
666 }
667
StorePackets() const668 bool ModuleRtpRtcpImpl::StorePackets() const {
669 return rtp_sender_->packet_history.GetStorageMode() !=
670 RtpPacketHistory::StorageMode::kDisabled;
671 }
672
SendCombinedRtcpPacket(std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets)673 void ModuleRtpRtcpImpl::SendCombinedRtcpPacket(
674 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
675 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
676 }
677
SendLossNotification(uint16_t last_decoded_seq_num,uint16_t last_received_seq_num,bool decodability_flag,bool buffering_allowed)678 int32_t ModuleRtpRtcpImpl::SendLossNotification(uint16_t last_decoded_seq_num,
679 uint16_t last_received_seq_num,
680 bool decodability_flag,
681 bool buffering_allowed) {
682 return rtcp_sender_.SendLossNotification(
683 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
684 decodability_flag, buffering_allowed);
685 }
686
SetRemoteSSRC(const uint32_t ssrc)687 void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
688 // Inform about the incoming SSRC.
689 rtcp_sender_.SetRemoteSSRC(ssrc);
690 rtcp_receiver_.SetRemoteSSRC(ssrc);
691 }
692
693 // TODO(nisse): Delete video_rate amd fec_rate arguments.
BitrateSent(uint32_t * total_rate,uint32_t * video_rate,uint32_t * fec_rate,uint32_t * nack_rate) const694 void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
695 uint32_t* video_rate,
696 uint32_t* fec_rate,
697 uint32_t* nack_rate) const {
698 *total_rate = rtp_sender_->packet_sender.SendBitrate().bps<uint32_t>();
699 if (video_rate)
700 *video_rate = 0;
701 if (fec_rate)
702 *fec_rate = 0;
703 *nack_rate = rtp_sender_->packet_sender.NackOverheadRate().bps<uint32_t>();
704 }
705
OnRequestSendReport()706 void ModuleRtpRtcpImpl::OnRequestSendReport() {
707 SendRTCP(kRtcpSr);
708 }
709
OnReceivedNack(const std::vector<uint16_t> & nack_sequence_numbers)710 void ModuleRtpRtcpImpl::OnReceivedNack(
711 const std::vector<uint16_t>& nack_sequence_numbers) {
712 if (!rtp_sender_)
713 return;
714
715 if (!StorePackets() || nack_sequence_numbers.empty()) {
716 return;
717 }
718 // Use RTT from RtcpRttStats class if provided.
719 int64_t rtt = rtt_ms();
720 if (rtt == 0) {
721 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
722 }
723 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
724 }
725
OnReceivedRtcpReportBlocks(const ReportBlockList & report_blocks)726 void ModuleRtpRtcpImpl::OnReceivedRtcpReportBlocks(
727 const ReportBlockList& report_blocks) {
728 if (rtp_sender_) {
729 uint32_t ssrc = SSRC();
730 absl::optional<uint32_t> rtx_ssrc;
731 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
732 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
733 }
734
735 for (const RTCPReportBlock& report_block : report_blocks) {
736 if (ssrc == report_block.source_ssrc) {
737 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
738 report_block.extended_highest_sequence_number);
739 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
740 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
741 report_block.extended_highest_sequence_number);
742 }
743 }
744 }
745 }
746
LastReceivedNTP(uint32_t * rtcp_arrival_time_secs,uint32_t * rtcp_arrival_time_frac,uint32_t * remote_sr) const747 bool ModuleRtpRtcpImpl::LastReceivedNTP(
748 uint32_t* rtcp_arrival_time_secs, // When we got the last report.
749 uint32_t* rtcp_arrival_time_frac,
750 uint32_t* remote_sr) const {
751 // Remote SR: NTP inside the last received (mid 16 bits from sec and frac).
752 uint32_t ntp_secs = 0;
753 uint32_t ntp_frac = 0;
754
755 if (!rtcp_receiver_.NTP(&ntp_secs, &ntp_frac, rtcp_arrival_time_secs,
756 rtcp_arrival_time_frac, NULL)) {
757 return false;
758 }
759 *remote_sr =
760 ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
761 return true;
762 }
763
764 // Called from RTCPsender.
BoundingSet(bool * tmmbr_owner)765 std::vector<rtcp::TmmbItem> ModuleRtpRtcpImpl::BoundingSet(bool* tmmbr_owner) {
766 return rtcp_receiver_.BoundingSet(tmmbr_owner);
767 }
768
set_rtt_ms(int64_t rtt_ms)769 void ModuleRtpRtcpImpl::set_rtt_ms(int64_t rtt_ms) {
770 rtc::CritScope cs(&critical_section_rtt_);
771 rtt_ms_ = rtt_ms;
772 if (rtp_sender_) {
773 rtp_sender_->packet_history.SetRtt(rtt_ms);
774 }
775 }
776
rtt_ms() const777 int64_t ModuleRtpRtcpImpl::rtt_ms() const {
778 rtc::CritScope cs(&critical_section_rtt_);
779 return rtt_ms_;
780 }
781
SetVideoBitrateAllocation(const VideoBitrateAllocation & bitrate)782 void ModuleRtpRtcpImpl::SetVideoBitrateAllocation(
783 const VideoBitrateAllocation& bitrate) {
784 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
785 }
786
RtpSender()787 RTPSender* ModuleRtpRtcpImpl::RtpSender() {
788 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
789 }
790
RtpSender() const791 const RTPSender* ModuleRtpRtcpImpl::RtpSender() const {
792 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
793 }
794
SendRate() const795 DataRate ModuleRtpRtcpImpl::SendRate() const {
796 RTC_DCHECK(rtp_sender_);
797 return rtp_sender_->packet_sender.SendBitrate();
798 }
799
NackOverheadRate() const800 DataRate ModuleRtpRtcpImpl::NackOverheadRate() const {
801 RTC_DCHECK(rtp_sender_);
802 return rtp_sender_->packet_sender.NackOverheadRate();
803 }
804
805 } // namespace webrtc
806