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 RtpRtcpInterface::Configuration & config)41 ModuleRtpRtcpImpl::RtpSenderContext::RtpSenderContext(
42 const RtpRtcpInterface::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
DEPRECATED_Create(const Configuration & configuration)51 std::unique_ptr<RtpRtcp> RtpRtcp::DEPRECATED_Create(
52 const Configuration& configuration) {
53 RTC_DCHECK(configuration.clock);
54 RTC_LOG(LS_ERROR)
55 << "*********** USING WebRTC INTERNAL IMPLEMENTATION DETAILS ***********";
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 // TODO(bugs.webrtc.org/11581): Figure out why we need to call Process() 200
100 // times a second.
101 next_process_time_ = now + kRtpRtcpMaxIdleTimeProcessMs;
102
103 if (rtp_sender_) {
104 if (now >= last_bitrate_process_time_ + kRtpRtcpBitrateProcessTimeMs) {
105 rtp_sender_->packet_sender.ProcessBitrateAndNotifyObservers();
106 last_bitrate_process_time_ = now;
107 // TODO(bugs.webrtc.org/11581): Is this a bug? At the top of the function,
108 // next_process_time_ is incremented by 5ms, here we effectively do a
109 // std::min() of (now + 5ms, now + 10ms). Seems like this is a no-op?
110 next_process_time_ =
111 std::min(next_process_time_, now + kRtpRtcpBitrateProcessTimeMs);
112 }
113 }
114
115 // TODO(bugs.webrtc.org/11581): We update the RTT once a second, whereas other
116 // things that run in this method are updated much more frequently. Move the
117 // RTT checking over to the worker thread, which matches better with where the
118 // stats are maintained.
119 bool process_rtt = now >= last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs;
120 if (rtcp_sender_.Sending()) {
121 // Process RTT if we have received a report block and we haven't
122 // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
123 // Note that LastReceivedReportBlockMs() grabs a lock, so check
124 // |process_rtt| first.
125 if (process_rtt &&
126 rtcp_receiver_.LastReceivedReportBlockMs() > last_rtt_process_time_) {
127 std::vector<RTCPReportBlock> receive_blocks;
128 rtcp_receiver_.StatisticsReceived(&receive_blocks);
129 int64_t max_rtt = 0;
130 for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
131 it != receive_blocks.end(); ++it) {
132 int64_t rtt = 0;
133 rtcp_receiver_.RTT(it->sender_ssrc, &rtt, NULL, NULL, NULL);
134 max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
135 }
136 // Report the rtt.
137 if (rtt_stats_ && max_rtt != 0)
138 rtt_stats_->OnRttUpdate(max_rtt);
139 }
140
141 // Verify receiver reports are delivered and the reported sequence number
142 // is increasing.
143 // TODO(bugs.webrtc.org/11581): The timeout value needs to be checked every
144 // few seconds (see internals of RtcpRrTimeout). Here, we may be polling it
145 // a couple of hundred times a second, which isn't great since it grabs a
146 // lock. Note also that LastReceivedReportBlockMs() (called above) and
147 // RtcpRrTimeout() both grab the same lock and check the same timer, so
148 // it should be possible to consolidate that work somehow.
149 if (rtcp_receiver_.RtcpRrTimeout()) {
150 RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
151 } else if (rtcp_receiver_.RtcpRrSequenceNumberTimeout()) {
152 RTC_LOG_F(LS_WARNING) << "Timeout: No increase in RTCP RR extended "
153 "highest sequence number.";
154 }
155
156 if (remote_bitrate_ && rtcp_sender_.TMMBR()) {
157 unsigned int target_bitrate = 0;
158 std::vector<unsigned int> ssrcs;
159 if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
160 if (!ssrcs.empty()) {
161 target_bitrate = target_bitrate / ssrcs.size();
162 }
163 rtcp_sender_.SetTargetBitrate(target_bitrate);
164 }
165 }
166 } else {
167 // Report rtt from receiver.
168 if (process_rtt) {
169 int64_t rtt_ms;
170 if (rtt_stats_ && rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)) {
171 rtt_stats_->OnRttUpdate(rtt_ms);
172 }
173 }
174 }
175
176 // Get processed rtt.
177 if (process_rtt) {
178 last_rtt_process_time_ = now;
179 // TODO(bugs.webrtc.org/11581): Is this a bug? At the top of the function,
180 // next_process_time_ is incremented by 5ms, here we effectively do a
181 // std::min() of (now + 5ms, now + 1000ms). Seems like this is a no-op?
182 next_process_time_ = std::min(
183 next_process_time_, last_rtt_process_time_ + kRtpRtcpRttProcessTimeMs);
184 if (rtt_stats_) {
185 // Make sure we have a valid RTT before setting.
186 int64_t last_rtt = rtt_stats_->LastProcessedRtt();
187 if (last_rtt >= 0)
188 set_rtt_ms(last_rtt);
189 }
190 }
191
192 if (rtcp_sender_.TimeToSendRTCPReport())
193 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
194
195 if (TMMBR() && rtcp_receiver_.UpdateTmmbrTimers()) {
196 rtcp_receiver_.NotifyTmmbrUpdated();
197 }
198 }
199
SetRtxSendStatus(int mode)200 void ModuleRtpRtcpImpl::SetRtxSendStatus(int mode) {
201 rtp_sender_->packet_generator.SetRtxStatus(mode);
202 }
203
RtxSendStatus() const204 int ModuleRtpRtcpImpl::RtxSendStatus() const {
205 return rtp_sender_ ? rtp_sender_->packet_generator.RtxStatus() : kRtxOff;
206 }
207
SetRtxSendPayloadType(int payload_type,int associated_payload_type)208 void ModuleRtpRtcpImpl::SetRtxSendPayloadType(int payload_type,
209 int associated_payload_type) {
210 rtp_sender_->packet_generator.SetRtxPayloadType(payload_type,
211 associated_payload_type);
212 }
213
RtxSsrc() const214 absl::optional<uint32_t> ModuleRtpRtcpImpl::RtxSsrc() const {
215 return rtp_sender_ ? rtp_sender_->packet_generator.RtxSsrc() : absl::nullopt;
216 }
217
FlexfecSsrc() const218 absl::optional<uint32_t> ModuleRtpRtcpImpl::FlexfecSsrc() const {
219 if (rtp_sender_) {
220 return rtp_sender_->packet_generator.FlexfecSsrc();
221 }
222 return absl::nullopt;
223 }
224
IncomingRtcpPacket(const uint8_t * rtcp_packet,const size_t length)225 void ModuleRtpRtcpImpl::IncomingRtcpPacket(const uint8_t* rtcp_packet,
226 const size_t length) {
227 rtcp_receiver_.IncomingPacket(rtcp_packet, length);
228 }
229
RegisterSendPayloadFrequency(int payload_type,int payload_frequency)230 void ModuleRtpRtcpImpl::RegisterSendPayloadFrequency(int payload_type,
231 int payload_frequency) {
232 rtcp_sender_.SetRtpClockRate(payload_type, payload_frequency);
233 }
234
DeRegisterSendPayload(const int8_t payload_type)235 int32_t ModuleRtpRtcpImpl::DeRegisterSendPayload(const int8_t payload_type) {
236 return 0;
237 }
238
StartTimestamp() const239 uint32_t ModuleRtpRtcpImpl::StartTimestamp() const {
240 return rtp_sender_->packet_generator.TimestampOffset();
241 }
242
243 // Configure start timestamp, default is a random number.
SetStartTimestamp(const uint32_t timestamp)244 void ModuleRtpRtcpImpl::SetStartTimestamp(const uint32_t timestamp) {
245 rtcp_sender_.SetTimestampOffset(timestamp);
246 rtp_sender_->packet_generator.SetTimestampOffset(timestamp);
247 rtp_sender_->packet_sender.SetTimestampOffset(timestamp);
248 }
249
SequenceNumber() const250 uint16_t ModuleRtpRtcpImpl::SequenceNumber() const {
251 return rtp_sender_->packet_generator.SequenceNumber();
252 }
253
254 // Set SequenceNumber, default is a random number.
SetSequenceNumber(const uint16_t seq_num)255 void ModuleRtpRtcpImpl::SetSequenceNumber(const uint16_t seq_num) {
256 rtp_sender_->packet_generator.SetSequenceNumber(seq_num);
257 }
258
SetRtpState(const RtpState & rtp_state)259 void ModuleRtpRtcpImpl::SetRtpState(const RtpState& rtp_state) {
260 rtp_sender_->packet_generator.SetRtpState(rtp_state);
261 rtcp_sender_.SetTimestampOffset(rtp_state.start_timestamp);
262 }
263
SetRtxState(const RtpState & rtp_state)264 void ModuleRtpRtcpImpl::SetRtxState(const RtpState& rtp_state) {
265 rtp_sender_->packet_generator.SetRtxRtpState(rtp_state);
266 }
267
GetRtpState() const268 RtpState ModuleRtpRtcpImpl::GetRtpState() const {
269 RtpState state = rtp_sender_->packet_generator.GetRtpState();
270 return state;
271 }
272
GetRtxState() const273 RtpState ModuleRtpRtcpImpl::GetRtxState() const {
274 return rtp_sender_->packet_generator.GetRtxRtpState();
275 }
276
SetRid(const std::string & rid)277 void ModuleRtpRtcpImpl::SetRid(const std::string& rid) {
278 if (rtp_sender_) {
279 rtp_sender_->packet_generator.SetRid(rid);
280 }
281 }
282
SetMid(const std::string & mid)283 void ModuleRtpRtcpImpl::SetMid(const std::string& mid) {
284 if (rtp_sender_) {
285 rtp_sender_->packet_generator.SetMid(mid);
286 }
287 // TODO(bugs.webrtc.org/4050): If we end up supporting the MID SDES item for
288 // RTCP, this will need to be passed down to the RTCPSender also.
289 }
290
SetCsrcs(const std::vector<uint32_t> & csrcs)291 void ModuleRtpRtcpImpl::SetCsrcs(const std::vector<uint32_t>& csrcs) {
292 rtcp_sender_.SetCsrcs(csrcs);
293 rtp_sender_->packet_generator.SetCsrcs(csrcs);
294 }
295
296 // TODO(pbos): Handle media and RTX streams separately (separate RTCP
297 // feedbacks).
GetFeedbackState()298 RTCPSender::FeedbackState ModuleRtpRtcpImpl::GetFeedbackState() {
299 RTCPSender::FeedbackState state;
300 // This is called also when receiver_only is true. Hence below
301 // checks that rtp_sender_ exists.
302 if (rtp_sender_) {
303 StreamDataCounters rtp_stats;
304 StreamDataCounters rtx_stats;
305 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
306 state.packets_sent =
307 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
308 state.media_bytes_sent = rtp_stats.transmitted.payload_bytes +
309 rtx_stats.transmitted.payload_bytes;
310 state.send_bitrate =
311 rtp_sender_->packet_sender.GetSendRates().Sum().bps<uint32_t>();
312 }
313 state.receiver = &rtcp_receiver_;
314
315 LastReceivedNTP(&state.last_rr_ntp_secs, &state.last_rr_ntp_frac,
316 &state.remote_sr);
317
318 state.last_xr_rtis = rtcp_receiver_.ConsumeReceivedXrReferenceTimeInfo();
319
320 return state;
321 }
322
323 // TODO(nisse): This method shouldn't be called for a receive-only
324 // stream. Delete rtp_sender_ check as soon as all applications are
325 // updated.
SetSendingStatus(const bool sending)326 int32_t ModuleRtpRtcpImpl::SetSendingStatus(const bool sending) {
327 if (rtcp_sender_.Sending() != sending) {
328 // Sends RTCP BYE when going from true to false
329 if (rtcp_sender_.SetSendingStatus(GetFeedbackState(), sending) != 0) {
330 RTC_LOG(LS_WARNING) << "Failed to send RTCP BYE";
331 }
332 }
333 return 0;
334 }
335
Sending() const336 bool ModuleRtpRtcpImpl::Sending() const {
337 return rtcp_sender_.Sending();
338 }
339
340 // TODO(nisse): This method shouldn't be called for a receive-only
341 // stream. Delete rtp_sender_ check as soon as all applications are
342 // updated.
SetSendingMediaStatus(const bool sending)343 void ModuleRtpRtcpImpl::SetSendingMediaStatus(const bool sending) {
344 if (rtp_sender_) {
345 rtp_sender_->packet_generator.SetSendingMediaStatus(sending);
346 } else {
347 RTC_DCHECK(!sending);
348 }
349 }
350
SendingMedia() const351 bool ModuleRtpRtcpImpl::SendingMedia() const {
352 return rtp_sender_ ? rtp_sender_->packet_generator.SendingMedia() : false;
353 }
354
IsAudioConfigured() const355 bool ModuleRtpRtcpImpl::IsAudioConfigured() const {
356 return rtp_sender_ ? rtp_sender_->packet_generator.IsAudioConfigured()
357 : false;
358 }
359
SetAsPartOfAllocation(bool part_of_allocation)360 void ModuleRtpRtcpImpl::SetAsPartOfAllocation(bool part_of_allocation) {
361 RTC_CHECK(rtp_sender_);
362 rtp_sender_->packet_sender.ForceIncludeSendPacketsInAllocation(
363 part_of_allocation);
364 }
365
OnSendingRtpFrame(uint32_t timestamp,int64_t capture_time_ms,int payload_type,bool force_sender_report)366 bool ModuleRtpRtcpImpl::OnSendingRtpFrame(uint32_t timestamp,
367 int64_t capture_time_ms,
368 int payload_type,
369 bool force_sender_report) {
370 if (!Sending())
371 return false;
372
373 rtcp_sender_.SetLastRtpTime(timestamp, capture_time_ms, payload_type);
374 // Make sure an RTCP report isn't queued behind a key frame.
375 if (rtcp_sender_.TimeToSendRTCPReport(force_sender_report))
376 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpReport);
377
378 return true;
379 }
380
TrySendPacket(RtpPacketToSend * packet,const PacedPacketInfo & pacing_info)381 bool ModuleRtpRtcpImpl::TrySendPacket(RtpPacketToSend* packet,
382 const PacedPacketInfo& pacing_info) {
383 RTC_DCHECK(rtp_sender_);
384 // TODO(sprang): Consider if we can remove this check.
385 if (!rtp_sender_->packet_generator.SendingMedia()) {
386 return false;
387 }
388 rtp_sender_->packet_sender.SendPacket(packet, pacing_info);
389 return true;
390 }
391
SetFecProtectionParams(const FecProtectionParams &,const FecProtectionParams &)392 void ModuleRtpRtcpImpl::SetFecProtectionParams(const FecProtectionParams&,
393 const FecProtectionParams&) {
394 // Deferred FEC not supported in deprecated RTP module.
395 }
396
397 std::vector<std::unique_ptr<RtpPacketToSend>>
FetchFecPackets()398 ModuleRtpRtcpImpl::FetchFecPackets() {
399 // Deferred FEC not supported in deprecated RTP module.
400 return {};
401 }
402
OnPacketsAcknowledged(rtc::ArrayView<const uint16_t> sequence_numbers)403 void ModuleRtpRtcpImpl::OnPacketsAcknowledged(
404 rtc::ArrayView<const uint16_t> sequence_numbers) {
405 RTC_DCHECK(rtp_sender_);
406 rtp_sender_->packet_history.CullAcknowledgedPackets(sequence_numbers);
407 }
408
SupportsPadding() const409 bool ModuleRtpRtcpImpl::SupportsPadding() const {
410 RTC_DCHECK(rtp_sender_);
411 return rtp_sender_->packet_generator.SupportsPadding();
412 }
413
SupportsRtxPayloadPadding() const414 bool ModuleRtpRtcpImpl::SupportsRtxPayloadPadding() const {
415 RTC_DCHECK(rtp_sender_);
416 return rtp_sender_->packet_generator.SupportsRtxPayloadPadding();
417 }
418
419 std::vector<std::unique_ptr<RtpPacketToSend>>
GeneratePadding(size_t target_size_bytes)420 ModuleRtpRtcpImpl::GeneratePadding(size_t target_size_bytes) {
421 RTC_DCHECK(rtp_sender_);
422 return rtp_sender_->packet_generator.GeneratePadding(
423 target_size_bytes, rtp_sender_->packet_sender.MediaHasBeenSent());
424 }
425
426 std::vector<RtpSequenceNumberMap::Info>
GetSentRtpPacketInfos(rtc::ArrayView<const uint16_t> sequence_numbers) const427 ModuleRtpRtcpImpl::GetSentRtpPacketInfos(
428 rtc::ArrayView<const uint16_t> sequence_numbers) const {
429 RTC_DCHECK(rtp_sender_);
430 return rtp_sender_->packet_sender.GetSentRtpPacketInfos(sequence_numbers);
431 }
432
ExpectedPerPacketOverhead() const433 size_t ModuleRtpRtcpImpl::ExpectedPerPacketOverhead() const {
434 if (!rtp_sender_) {
435 return 0;
436 }
437 return rtp_sender_->packet_generator.ExpectedPerPacketOverhead();
438 }
439
MaxRtpPacketSize() const440 size_t ModuleRtpRtcpImpl::MaxRtpPacketSize() const {
441 RTC_DCHECK(rtp_sender_);
442 return rtp_sender_->packet_generator.MaxRtpPacketSize();
443 }
444
SetMaxRtpPacketSize(size_t rtp_packet_size)445 void ModuleRtpRtcpImpl::SetMaxRtpPacketSize(size_t rtp_packet_size) {
446 RTC_DCHECK_LE(rtp_packet_size, IP_PACKET_SIZE)
447 << "rtp packet size too large: " << rtp_packet_size;
448 RTC_DCHECK_GT(rtp_packet_size, packet_overhead_)
449 << "rtp packet size too small: " << rtp_packet_size;
450
451 rtcp_sender_.SetMaxRtpPacketSize(rtp_packet_size);
452 if (rtp_sender_) {
453 rtp_sender_->packet_generator.SetMaxRtpPacketSize(rtp_packet_size);
454 }
455 }
456
RTCP() const457 RtcpMode ModuleRtpRtcpImpl::RTCP() const {
458 return rtcp_sender_.Status();
459 }
460
461 // Configure RTCP status i.e on/off.
SetRTCPStatus(const RtcpMode method)462 void ModuleRtpRtcpImpl::SetRTCPStatus(const RtcpMode method) {
463 rtcp_sender_.SetRTCPStatus(method);
464 }
465
SetCNAME(const char * c_name)466 int32_t ModuleRtpRtcpImpl::SetCNAME(const char* c_name) {
467 return rtcp_sender_.SetCNAME(c_name);
468 }
469
AddMixedCNAME(uint32_t ssrc,const char * c_name)470 int32_t ModuleRtpRtcpImpl::AddMixedCNAME(uint32_t ssrc, const char* c_name) {
471 return rtcp_sender_.AddMixedCNAME(ssrc, c_name);
472 }
473
RemoveMixedCNAME(const uint32_t ssrc)474 int32_t ModuleRtpRtcpImpl::RemoveMixedCNAME(const uint32_t ssrc) {
475 return rtcp_sender_.RemoveMixedCNAME(ssrc);
476 }
477
RemoteCNAME(const uint32_t remote_ssrc,char c_name[RTCP_CNAME_SIZE]) const478 int32_t ModuleRtpRtcpImpl::RemoteCNAME(const uint32_t remote_ssrc,
479 char c_name[RTCP_CNAME_SIZE]) const {
480 return rtcp_receiver_.CNAME(remote_ssrc, c_name);
481 }
482
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) const483 int32_t ModuleRtpRtcpImpl::RemoteNTP(uint32_t* received_ntpsecs,
484 uint32_t* received_ntpfrac,
485 uint32_t* rtcp_arrival_time_secs,
486 uint32_t* rtcp_arrival_time_frac,
487 uint32_t* rtcp_timestamp) const {
488 return rtcp_receiver_.NTP(received_ntpsecs, received_ntpfrac,
489 rtcp_arrival_time_secs, rtcp_arrival_time_frac,
490 rtcp_timestamp)
491 ? 0
492 : -1;
493 }
494
495 // Get RoundTripTime.
RTT(const uint32_t remote_ssrc,int64_t * rtt,int64_t * avg_rtt,int64_t * min_rtt,int64_t * max_rtt) const496 int32_t ModuleRtpRtcpImpl::RTT(const uint32_t remote_ssrc,
497 int64_t* rtt,
498 int64_t* avg_rtt,
499 int64_t* min_rtt,
500 int64_t* max_rtt) const {
501 int32_t ret = rtcp_receiver_.RTT(remote_ssrc, rtt, avg_rtt, min_rtt, max_rtt);
502 if (rtt && *rtt == 0) {
503 // Try to get RTT from RtcpRttStats class.
504 *rtt = rtt_ms();
505 }
506 return ret;
507 }
508
ExpectedRetransmissionTimeMs() const509 int64_t ModuleRtpRtcpImpl::ExpectedRetransmissionTimeMs() const {
510 int64_t expected_retransmission_time_ms = rtt_ms();
511 if (expected_retransmission_time_ms > 0) {
512 return expected_retransmission_time_ms;
513 }
514 // No rtt available (|kRtpRtcpRttProcessTimeMs| not yet passed?), so try to
515 // poll avg_rtt_ms directly from rtcp receiver.
516 if (rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), nullptr,
517 &expected_retransmission_time_ms, nullptr,
518 nullptr) == 0) {
519 return expected_retransmission_time_ms;
520 }
521 return kDefaultExpectedRetransmissionTimeMs;
522 }
523
524 // Force a send of an RTCP packet.
525 // Normal SR and RR are triggered via the process function.
SendRTCP(RTCPPacketType packet_type)526 int32_t ModuleRtpRtcpImpl::SendRTCP(RTCPPacketType packet_type) {
527 return rtcp_sender_.SendRTCP(GetFeedbackState(), packet_type);
528 }
529
SetRTCPApplicationSpecificData(const uint8_t sub_type,const uint32_t name,const uint8_t * data,const uint16_t length)530 int32_t ModuleRtpRtcpImpl::SetRTCPApplicationSpecificData(
531 const uint8_t sub_type,
532 const uint32_t name,
533 const uint8_t* data,
534 const uint16_t length) {
535 RTC_NOTREACHED() << "Not implemented";
536 return -1;
537 }
538
SetRtcpXrRrtrStatus(bool enable)539 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
540 rtcp_receiver_.SetRtcpXrRrtrStatus(enable);
541 rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
542 }
543
RtcpXrRrtrStatus() const544 bool ModuleRtpRtcpImpl::RtcpXrRrtrStatus() const {
545 return rtcp_sender_.RtcpXrReceiverReferenceTime();
546 }
547
548 // TODO(asapersson): Replace this method with the one below.
DataCountersRTP(size_t * bytes_sent,uint32_t * packets_sent) const549 int32_t ModuleRtpRtcpImpl::DataCountersRTP(size_t* bytes_sent,
550 uint32_t* packets_sent) const {
551 StreamDataCounters rtp_stats;
552 StreamDataCounters rtx_stats;
553 rtp_sender_->packet_sender.GetDataCounters(&rtp_stats, &rtx_stats);
554
555 if (bytes_sent) {
556 // TODO(http://crbug.com/webrtc/10525): Bytes sent should only include
557 // payload bytes, not header and padding bytes.
558 *bytes_sent = rtp_stats.transmitted.payload_bytes +
559 rtp_stats.transmitted.padding_bytes +
560 rtp_stats.transmitted.header_bytes +
561 rtx_stats.transmitted.payload_bytes +
562 rtx_stats.transmitted.padding_bytes +
563 rtx_stats.transmitted.header_bytes;
564 }
565 if (packets_sent) {
566 *packets_sent =
567 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
568 }
569 return 0;
570 }
571
GetSendStreamDataCounters(StreamDataCounters * rtp_counters,StreamDataCounters * rtx_counters) const572 void ModuleRtpRtcpImpl::GetSendStreamDataCounters(
573 StreamDataCounters* rtp_counters,
574 StreamDataCounters* rtx_counters) const {
575 rtp_sender_->packet_sender.GetDataCounters(rtp_counters, rtx_counters);
576 }
577
578 // Received RTCP report.
RemoteRTCPStat(std::vector<RTCPReportBlock> * receive_blocks) const579 int32_t ModuleRtpRtcpImpl::RemoteRTCPStat(
580 std::vector<RTCPReportBlock>* receive_blocks) const {
581 return rtcp_receiver_.StatisticsReceived(receive_blocks);
582 }
583
GetLatestReportBlockData() const584 std::vector<ReportBlockData> ModuleRtpRtcpImpl::GetLatestReportBlockData()
585 const {
586 return rtcp_receiver_.GetLatestReportBlockData();
587 }
588
589 // (REMB) Receiver Estimated Max Bitrate.
SetRemb(int64_t bitrate_bps,std::vector<uint32_t> ssrcs)590 void ModuleRtpRtcpImpl::SetRemb(int64_t bitrate_bps,
591 std::vector<uint32_t> ssrcs) {
592 rtcp_sender_.SetRemb(bitrate_bps, std::move(ssrcs));
593 }
594
UnsetRemb()595 void ModuleRtpRtcpImpl::UnsetRemb() {
596 rtcp_sender_.UnsetRemb();
597 }
598
SetExtmapAllowMixed(bool extmap_allow_mixed)599 void ModuleRtpRtcpImpl::SetExtmapAllowMixed(bool extmap_allow_mixed) {
600 rtp_sender_->packet_generator.SetExtmapAllowMixed(extmap_allow_mixed);
601 }
602
RegisterSendRtpHeaderExtension(const RTPExtensionType type,const uint8_t id)603 int32_t ModuleRtpRtcpImpl::RegisterSendRtpHeaderExtension(
604 const RTPExtensionType type,
605 const uint8_t id) {
606 return rtp_sender_->packet_generator.RegisterRtpHeaderExtension(type, id);
607 }
608
RegisterRtpHeaderExtension(absl::string_view uri,int id)609 void ModuleRtpRtcpImpl::RegisterRtpHeaderExtension(absl::string_view uri,
610 int id) {
611 bool registered =
612 rtp_sender_->packet_generator.RegisterRtpHeaderExtension(uri, id);
613 RTC_CHECK(registered);
614 }
615
DeregisterSendRtpHeaderExtension(const RTPExtensionType type)616 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
617 const RTPExtensionType type) {
618 return rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(type);
619 }
DeregisterSendRtpHeaderExtension(absl::string_view uri)620 void ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
621 absl::string_view uri) {
622 rtp_sender_->packet_generator.DeregisterRtpHeaderExtension(uri);
623 }
624
625 // (TMMBR) Temporary Max Media Bit Rate.
TMMBR() const626 bool ModuleRtpRtcpImpl::TMMBR() const {
627 return rtcp_sender_.TMMBR();
628 }
629
SetTMMBRStatus(const bool enable)630 void ModuleRtpRtcpImpl::SetTMMBRStatus(const bool enable) {
631 rtcp_sender_.SetTMMBRStatus(enable);
632 }
633
SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set)634 void ModuleRtpRtcpImpl::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
635 rtcp_sender_.SetTmmbn(std::move(bounding_set));
636 }
637
638 // Send a Negative acknowledgment packet.
SendNACK(const uint16_t * nack_list,const uint16_t size)639 int32_t ModuleRtpRtcpImpl::SendNACK(const uint16_t* nack_list,
640 const uint16_t size) {
641 uint16_t nack_length = size;
642 uint16_t start_id = 0;
643 int64_t now_ms = clock_->TimeInMilliseconds();
644 if (TimeToSendFullNackList(now_ms)) {
645 nack_last_time_sent_full_ms_ = now_ms;
646 } else {
647 // Only send extended list.
648 if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
649 // Last sequence number is the same, do not send list.
650 return 0;
651 }
652 // Send new sequence numbers.
653 for (int i = 0; i < size; ++i) {
654 if (nack_last_seq_number_sent_ == nack_list[i]) {
655 start_id = i + 1;
656 break;
657 }
658 }
659 nack_length = size - start_id;
660 }
661
662 // Our RTCP NACK implementation is limited to kRtcpMaxNackFields sequence
663 // numbers per RTCP packet.
664 if (nack_length > kRtcpMaxNackFields) {
665 nack_length = kRtcpMaxNackFields;
666 }
667 nack_last_seq_number_sent_ = nack_list[start_id + nack_length - 1];
668
669 return rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, nack_length,
670 &nack_list[start_id]);
671 }
672
SendNack(const std::vector<uint16_t> & sequence_numbers)673 void ModuleRtpRtcpImpl::SendNack(
674 const std::vector<uint16_t>& sequence_numbers) {
675 rtcp_sender_.SendRTCP(GetFeedbackState(), kRtcpNack, sequence_numbers.size(),
676 sequence_numbers.data());
677 }
678
TimeToSendFullNackList(int64_t now) const679 bool ModuleRtpRtcpImpl::TimeToSendFullNackList(int64_t now) const {
680 // Use RTT from RtcpRttStats class if provided.
681 int64_t rtt = rtt_ms();
682 if (rtt == 0) {
683 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
684 }
685
686 const int64_t kStartUpRttMs = 100;
687 int64_t wait_time = 5 + ((rtt * 3) >> 1); // 5 + RTT * 1.5.
688 if (rtt == 0) {
689 wait_time = kStartUpRttMs;
690 }
691
692 // Send a full NACK list once within every |wait_time|.
693 return now - nack_last_time_sent_full_ms_ > wait_time;
694 }
695
696 // Store the sent packets, needed to answer to Negative acknowledgment requests.
SetStorePacketsStatus(const bool enable,const uint16_t number_to_store)697 void ModuleRtpRtcpImpl::SetStorePacketsStatus(const bool enable,
698 const uint16_t number_to_store) {
699 rtp_sender_->packet_history.SetStorePacketsStatus(
700 enable ? RtpPacketHistory::StorageMode::kStoreAndCull
701 : RtpPacketHistory::StorageMode::kDisabled,
702 number_to_store);
703 }
704
StorePackets() const705 bool ModuleRtpRtcpImpl::StorePackets() const {
706 return rtp_sender_->packet_history.GetStorageMode() !=
707 RtpPacketHistory::StorageMode::kDisabled;
708 }
709
SendCombinedRtcpPacket(std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets)710 void ModuleRtpRtcpImpl::SendCombinedRtcpPacket(
711 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
712 rtcp_sender_.SendCombinedRtcpPacket(std::move(rtcp_packets));
713 }
714
SendLossNotification(uint16_t last_decoded_seq_num,uint16_t last_received_seq_num,bool decodability_flag,bool buffering_allowed)715 int32_t ModuleRtpRtcpImpl::SendLossNotification(uint16_t last_decoded_seq_num,
716 uint16_t last_received_seq_num,
717 bool decodability_flag,
718 bool buffering_allowed) {
719 return rtcp_sender_.SendLossNotification(
720 GetFeedbackState(), last_decoded_seq_num, last_received_seq_num,
721 decodability_flag, buffering_allowed);
722 }
723
SetRemoteSSRC(const uint32_t ssrc)724 void ModuleRtpRtcpImpl::SetRemoteSSRC(const uint32_t ssrc) {
725 // Inform about the incoming SSRC.
726 rtcp_sender_.SetRemoteSSRC(ssrc);
727 rtcp_receiver_.SetRemoteSSRC(ssrc);
728 }
729
BitrateSent(uint32_t * total_rate,uint32_t * video_rate,uint32_t * fec_rate,uint32_t * nack_rate) const730 void ModuleRtpRtcpImpl::BitrateSent(uint32_t* total_rate,
731 uint32_t* video_rate,
732 uint32_t* fec_rate,
733 uint32_t* nack_rate) const {
734 RtpSendRates send_rates = rtp_sender_->packet_sender.GetSendRates();
735 *total_rate = send_rates.Sum().bps<uint32_t>();
736 if (video_rate)
737 *video_rate = 0;
738 if (fec_rate)
739 *fec_rate = 0;
740 *nack_rate = send_rates[RtpPacketMediaType::kRetransmission].bps<uint32_t>();
741 }
742
GetSendRates() const743 RtpSendRates ModuleRtpRtcpImpl::GetSendRates() const {
744 return rtp_sender_->packet_sender.GetSendRates();
745 }
746
OnRequestSendReport()747 void ModuleRtpRtcpImpl::OnRequestSendReport() {
748 SendRTCP(kRtcpSr);
749 }
750
OnReceivedNack(const std::vector<uint16_t> & nack_sequence_numbers)751 void ModuleRtpRtcpImpl::OnReceivedNack(
752 const std::vector<uint16_t>& nack_sequence_numbers) {
753 if (!rtp_sender_)
754 return;
755
756 if (!StorePackets() || nack_sequence_numbers.empty()) {
757 return;
758 }
759 // Use RTT from RtcpRttStats class if provided.
760 int64_t rtt = rtt_ms();
761 if (rtt == 0) {
762 rtcp_receiver_.RTT(rtcp_receiver_.RemoteSSRC(), NULL, &rtt, NULL, NULL);
763 }
764 rtp_sender_->packet_generator.OnReceivedNack(nack_sequence_numbers, rtt);
765 }
766
OnReceivedRtcpReportBlocks(const ReportBlockList & report_blocks)767 void ModuleRtpRtcpImpl::OnReceivedRtcpReportBlocks(
768 const ReportBlockList& report_blocks) {
769 if (rtp_sender_) {
770 uint32_t ssrc = SSRC();
771 absl::optional<uint32_t> rtx_ssrc;
772 if (rtp_sender_->packet_generator.RtxStatus() != kRtxOff) {
773 rtx_ssrc = rtp_sender_->packet_generator.RtxSsrc();
774 }
775
776 for (const RTCPReportBlock& report_block : report_blocks) {
777 if (ssrc == report_block.source_ssrc) {
778 rtp_sender_->packet_generator.OnReceivedAckOnSsrc(
779 report_block.extended_highest_sequence_number);
780 } else if (rtx_ssrc && *rtx_ssrc == report_block.source_ssrc) {
781 rtp_sender_->packet_generator.OnReceivedAckOnRtxSsrc(
782 report_block.extended_highest_sequence_number);
783 }
784 }
785 }
786 }
787
LastReceivedNTP(uint32_t * rtcp_arrival_time_secs,uint32_t * rtcp_arrival_time_frac,uint32_t * remote_sr) const788 bool ModuleRtpRtcpImpl::LastReceivedNTP(
789 uint32_t* rtcp_arrival_time_secs, // When we got the last report.
790 uint32_t* rtcp_arrival_time_frac,
791 uint32_t* remote_sr) const {
792 // Remote SR: NTP inside the last received (mid 16 bits from sec and frac).
793 uint32_t ntp_secs = 0;
794 uint32_t ntp_frac = 0;
795
796 if (!rtcp_receiver_.NTP(&ntp_secs, &ntp_frac, rtcp_arrival_time_secs,
797 rtcp_arrival_time_frac, NULL)) {
798 return false;
799 }
800 *remote_sr =
801 ((ntp_secs & 0x0000ffff) << 16) + ((ntp_frac & 0xffff0000) >> 16);
802 return true;
803 }
804
set_rtt_ms(int64_t rtt_ms)805 void ModuleRtpRtcpImpl::set_rtt_ms(int64_t rtt_ms) {
806 {
807 MutexLock lock(&mutex_rtt_);
808 rtt_ms_ = rtt_ms;
809 }
810 if (rtp_sender_) {
811 rtp_sender_->packet_history.SetRtt(rtt_ms);
812 }
813 }
814
rtt_ms() const815 int64_t ModuleRtpRtcpImpl::rtt_ms() const {
816 MutexLock lock(&mutex_rtt_);
817 return rtt_ms_;
818 }
819
SetVideoBitrateAllocation(const VideoBitrateAllocation & bitrate)820 void ModuleRtpRtcpImpl::SetVideoBitrateAllocation(
821 const VideoBitrateAllocation& bitrate) {
822 rtcp_sender_.SetVideoBitrateAllocation(bitrate);
823 }
824
RtpSender()825 RTPSender* ModuleRtpRtcpImpl::RtpSender() {
826 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
827 }
828
RtpSender() const829 const RTPSender* ModuleRtpRtcpImpl::RtpSender() const {
830 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
831 }
832
SendRate() const833 DataRate ModuleRtpRtcpImpl::SendRate() const {
834 RTC_DCHECK(rtp_sender_);
835 return rtp_sender_->packet_sender.GetSendRates().Sum();
836 }
837
NackOverheadRate() const838 DataRate ModuleRtpRtcpImpl::NackOverheadRate() const {
839 RTC_DCHECK(rtp_sender_);
840 return rtp_sender_->packet_sender
841 .GetSendRates()[RtpPacketMediaType::kRetransmission];
842 }
843
844 } // namespace webrtc
845