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/rtcp_sender.h"
12 
13 #include <string.h>  // memcpy
14 
15 #include <utility>
16 
17 #include "common_types.h"  // NOLINT(build/include)
18 #include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
19 #include "logging/rtc_event_log/rtc_event_log.h"
20 #include "modules/rtp_rtcp/source/rtcp_packet/app.h"
21 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
22 #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
29 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
30 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
31 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
32 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
34 #include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
35 #include "modules/rtp_rtcp/source/time_util.h"
36 #include "modules/rtp_rtcp/source/tmmbr_help.h"
37 #include "rtc_base/checks.h"
38 #include "rtc_base/constructormagic.h"
39 #include "rtc_base/logging.h"
40 #include "rtc_base/ptr_util.h"
41 #include "rtc_base/trace_event.h"
42 
43 namespace webrtc {
44 
45 namespace {
46 const uint32_t kRtcpAnyExtendedReports =
47     kRtcpXrVoipMetric | kRtcpXrReceiverReferenceTime | kRtcpXrDlrrReportBlock |
48     kRtcpXrTargetBitrate;
49 }  // namespace
50 
NACKStringBuilder()51 NACKStringBuilder::NACKStringBuilder()
52     : stream_(""), count_(0), prevNack_(0), consecutive_(false) {}
53 
~NACKStringBuilder()54 NACKStringBuilder::~NACKStringBuilder() {}
55 
PushNACK(uint16_t nack)56 void NACKStringBuilder::PushNACK(uint16_t nack) {
57   if (count_ == 0) {
58     stream_ << nack;
59   } else if (nack == prevNack_ + 1) {
60     consecutive_ = true;
61   } else {
62     if (consecutive_) {
63       stream_ << "-" << prevNack_;
64       consecutive_ = false;
65     }
66     stream_ << "," << nack;
67   }
68   count_++;
69   prevNack_ = nack;
70 }
71 
GetResult()72 std::string NACKStringBuilder::GetResult() {
73   if (consecutive_) {
74     stream_ << "-" << prevNack_;
75     consecutive_ = false;
76   }
77   return stream_.str();
78 }
79 
FeedbackState()80 RTCPSender::FeedbackState::FeedbackState()
81     : packets_sent(0),
82       media_bytes_sent(0),
83       send_bitrate(0),
84       last_rr_ntp_secs(0),
85       last_rr_ntp_frac(0),
86       remote_sr(0),
87       has_last_xr_rr(false),
88       module(nullptr) {}
89 
90 class PacketContainer : public rtcp::CompoundPacket,
91                         public rtcp::RtcpPacket::PacketReadyCallback {
92  public:
PacketContainer(Transport * transport,RtcEventLog * event_log)93   PacketContainer(Transport* transport, RtcEventLog* event_log)
94       : transport_(transport), event_log_(event_log), bytes_sent_(0) {}
~PacketContainer()95   virtual ~PacketContainer() {
96     for (RtcpPacket* packet : appended_packets_)
97       delete packet;
98   }
99 
OnPacketReady(uint8_t * data,size_t length)100   void OnPacketReady(uint8_t* data, size_t length) override {
101     if (transport_->SendRtcp(data, length)) {
102       bytes_sent_ += length;
103       if (event_log_) {
104         event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(
105             rtc::ArrayView<const uint8_t>(data, length)));
106       }
107     }
108   }
109 
SendPackets(size_t max_payload_length)110   size_t SendPackets(size_t max_payload_length) {
111     RTC_DCHECK_LE(max_payload_length, IP_PACKET_SIZE);
112     uint8_t buffer[IP_PACKET_SIZE];
113     BuildExternalBuffer(buffer, max_payload_length, this);
114     return bytes_sent_;
115   }
116 
117  private:
118   Transport* transport_;
119   RtcEventLog* const event_log_;
120   size_t bytes_sent_;
121 
122   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
123 };
124 
125 class RTCPSender::RtcpContext {
126  public:
RtcpContext(const FeedbackState & feedback_state,int32_t nack_size,const uint16_t * nack_list,NtpTime now)127   RtcpContext(const FeedbackState& feedback_state,
128               int32_t nack_size,
129               const uint16_t* nack_list,
130               NtpTime now)
131       : feedback_state_(feedback_state),
132         nack_size_(nack_size),
133         nack_list_(nack_list),
134         now_(now) {}
135 
136   const FeedbackState& feedback_state_;
137   const int32_t nack_size_;
138   const uint16_t* nack_list_;
139   const NtpTime now_;
140 };
141 
RTCPSender(bool audio,Clock * clock,ReceiveStatisticsProvider * receive_statistics,RtcpPacketTypeCounterObserver * packet_type_counter_observer,RtcEventLog * event_log,Transport * outgoing_transport)142 RTCPSender::RTCPSender(
143     bool audio,
144     Clock* clock,
145     ReceiveStatisticsProvider* receive_statistics,
146     RtcpPacketTypeCounterObserver* packet_type_counter_observer,
147     RtcEventLog* event_log,
148     Transport* outgoing_transport)
149     : audio_(audio),
150       clock_(clock),
151       random_(clock_->TimeInMicroseconds()),
152       method_(RtcpMode::kOff),
153       event_log_(event_log),
154       transport_(outgoing_transport),
155       using_nack_(false),
156       sending_(false),
157       next_time_to_send_rtcp_(clock->TimeInMilliseconds()),
158       timestamp_offset_(0),
159       last_rtp_timestamp_(0),
160       last_frame_capture_time_ms_(-1),
161       ssrc_(0),
162       remote_ssrc_(0),
163       receive_statistics_(receive_statistics),
164 
165       sequence_number_fir_(0),
166 
167       remb_bitrate_(0),
168 
169       tmmbr_send_bps_(0),
170       packet_oh_send_(0),
171       max_packet_size_(IP_PACKET_SIZE - 28),  // IPv4 + UDP by default.
172 
173       app_sub_type_(0),
174       app_name_(0),
175       app_data_(nullptr),
176       app_length_(0),
177 
178       xr_send_receiver_reference_time_enabled_(false),
179       packet_type_counter_observer_(packet_type_counter_observer) {
180   memset(last_send_report_, 0, sizeof(last_send_report_));
181   memset(last_rtcp_time_, 0, sizeof(last_rtcp_time_));
182   memset(lastSRPacketCount_, 0, sizeof(lastSRPacketCount_));
183   memset(lastSROctetCount_, 0, sizeof(lastSROctetCount_));
184   RTC_DCHECK(transport_ != nullptr);
185 
186   builders_[kRtcpSr] = &RTCPSender::BuildSR;
187   builders_[kRtcpRr] = &RTCPSender::BuildRR;
188   builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
189   builders_[kRtcpPli] = &RTCPSender::BuildPLI;
190   builders_[kRtcpFir] = &RTCPSender::BuildFIR;
191   builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
192   builders_[kRtcpBye] = &RTCPSender::BuildBYE;
193   builders_[kRtcpApp] = &RTCPSender::BuildAPP;
194   builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
195   builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
196   builders_[kRtcpNack] = &RTCPSender::BuildNACK;
197   builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
198 }
199 
~RTCPSender()200 RTCPSender::~RTCPSender() {}
201 
Status() const202 RtcpMode RTCPSender::Status() const {
203   rtc::CritScope lock(&critical_section_rtcp_sender_);
204   return method_;
205 }
206 
SetRTCPStatus(RtcpMode new_method)207 void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
208   rtc::CritScope lock(&critical_section_rtcp_sender_);
209 
210   if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
211     // When switching on, reschedule the next packet
212     next_time_to_send_rtcp_ =
213       clock_->TimeInMilliseconds() + RTCP_INTERVAL_RAPID_SYNC_MS / 2;
214   }
215   method_ = new_method;
216 }
217 
Sending() const218 bool RTCPSender::Sending() const {
219   rtc::CritScope lock(&critical_section_rtcp_sender_);
220   return sending_;
221 }
222 
SetSendingStatus(const FeedbackState & feedback_state,bool sending)223 int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
224                                      bool sending) {
225   bool sendRTCPBye = false;
226   {
227     rtc::CritScope lock(&critical_section_rtcp_sender_);
228 
229     if (method_ != RtcpMode::kOff) {
230       if (sending == false && sending_ == true) {
231         // Trigger RTCP bye
232         sendRTCPBye = true;
233       }
234     }
235     sending_ = sending;
236   }
237   if (sendRTCPBye)
238     return SendRTCP(feedback_state, kRtcpBye);
239   return 0;
240 }
241 
SetRemb(uint32_t bitrate,const std::vector<uint32_t> & ssrcs)242 void RTCPSender::SetRemb(uint32_t bitrate, const std::vector<uint32_t>& ssrcs) {
243   rtc::CritScope lock(&critical_section_rtcp_sender_);
244   remb_bitrate_ = bitrate;
245   remb_ssrcs_ = ssrcs;
246 
247   SetFlag(kRtcpRemb, /*is_volatile=*/false);
248   // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
249   // throttled by the caller.
250   next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
251 }
252 
UnsetRemb()253 void RTCPSender::UnsetRemb() {
254   rtc::CritScope lock(&critical_section_rtcp_sender_);
255   // Stop sending REMB each report until it is reenabled and REMB data set.
256   ConsumeFlag(kRtcpRemb, /*forced=*/true);
257 }
258 
TMMBR() const259 bool RTCPSender::TMMBR() const {
260   rtc::CritScope lock(&critical_section_rtcp_sender_);
261   return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
262 }
263 
SetTMMBRStatus(bool enable)264 void RTCPSender::SetTMMBRStatus(bool enable) {
265   rtc::CritScope lock(&critical_section_rtcp_sender_);
266   if (enable) {
267     SetFlag(RTCPPacketType::kRtcpTmmbr, false);
268   } else {
269     ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
270   }
271 }
272 
SetMaxRtpPacketSize(size_t max_packet_size)273 void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
274   rtc::CritScope lock(&critical_section_rtcp_sender_);
275   max_packet_size_ = max_packet_size;
276 }
277 
SetTimestampOffset(uint32_t timestamp_offset)278 void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
279   rtc::CritScope lock(&critical_section_rtcp_sender_);
280   timestamp_offset_ = timestamp_offset;
281 }
282 
SetLastRtpTime(uint32_t rtp_timestamp,int64_t capture_time_ms)283 void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
284                                 int64_t capture_time_ms) {
285   rtc::CritScope lock(&critical_section_rtcp_sender_);
286   last_rtp_timestamp_ = rtp_timestamp;
287   if (capture_time_ms < 0) {
288     // We don't currently get a capture time from VoiceEngine.
289     last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
290   } else {
291     last_frame_capture_time_ms_ = capture_time_ms;
292   }
293 }
294 
SSRC() const295 uint32_t RTCPSender::SSRC() const {
296   rtc::CritScope lock(&critical_section_rtcp_sender_);
297   return ssrc_;
298 }
299 
SetSSRC(uint32_t ssrc)300 void RTCPSender::SetSSRC(uint32_t ssrc) {
301   rtc::CritScope lock(&critical_section_rtcp_sender_);
302 
303   if (ssrc_ != 0) {
304     // not first SetSSRC, probably due to a collision
305     // schedule a new RTCP report
306     // make sure that we send a RTP packet
307     next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
308   }
309   ssrc_ = ssrc;
310 }
311 
SetRemoteSSRC(uint32_t ssrc)312 void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
313   rtc::CritScope lock(&critical_section_rtcp_sender_);
314   remote_ssrc_ = ssrc;
315 }
316 
SetCNAME(const char * c_name)317 int32_t RTCPSender::SetCNAME(const char* c_name) {
318   if (!c_name)
319     return -1;
320 
321   RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
322   rtc::CritScope lock(&critical_section_rtcp_sender_);
323   cname_ = c_name;
324   return 0;
325 }
326 
AddMixedCNAME(uint32_t SSRC,const char * c_name)327 int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
328   RTC_DCHECK(c_name);
329   RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
330   rtc::CritScope lock(&critical_section_rtcp_sender_);
331   // One spot is reserved for ssrc_/cname_.
332   // TODO(danilchap): Add support for more than 30 contributes by sending
333   // several sdes packets.
334   if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
335     return -1;
336 
337   csrc_cnames_[SSRC] = c_name;
338   return 0;
339 }
340 
RemoveMixedCNAME(uint32_t SSRC)341 int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
342   rtc::CritScope lock(&critical_section_rtcp_sender_);
343   auto it = csrc_cnames_.find(SSRC);
344 
345   if (it == csrc_cnames_.end())
346     return -1;
347 
348   csrc_cnames_.erase(it);
349   return 0;
350 }
351 
TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const352 bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
353   /*
354       For audio we use a fix 5 sec interval
355 
356       For video we use 1 sec interval fo a BW smaller than 360 kbit/s,
357           technicaly we break the max 5% RTCP BW for video below 10 kbit/s but
358           that should be extremely rare
359 
360 
361   From RFC 3550
362 
363       MAX RTCP BW is 5% if the session BW
364           A send report is approximately 65 bytes inc CNAME
365           A receiver report is approximately 28 bytes
366 
367       The RECOMMENDED value for the reduced minimum in seconds is 360
368         divided by the session bandwidth in kilobits/second.  This minimum
369         is smaller than 5 seconds for bandwidths greater than 72 kb/s.
370 
371       If the participant has not yet sent an RTCP packet (the variable
372         initial is true), the constant Tmin is set to 2.5 seconds, else it
373         is set to 5 seconds.
374 
375       The interval between RTCP packets is varied randomly over the
376         range [0.5,1.5] times the calculated interval to avoid unintended
377         synchronization of all participants
378 
379       if we send
380       If the participant is a sender (we_sent true), the constant C is
381         set to the average RTCP packet size (avg_rtcp_size) divided by 25%
382         of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
383         number of senders.
384 
385       if we receive only
386         If we_sent is not true, the constant C is set
387         to the average RTCP packet size divided by 75% of the RTCP
388         bandwidth.  The constant n is set to the number of receivers
389         (members - senders).  If the number of senders is greater than
390         25%, senders and receivers are treated together.
391 
392       reconsideration NOT required for peer-to-peer
393         "timer reconsideration" is
394         employed.  This algorithm implements a simple back-off mechanism
395         which causes users to hold back RTCP packet transmission if the
396         group sizes are increasing.
397 
398         n = number of members
399         C = avg_size/(rtcpBW/4)
400 
401      3. The deterministic calculated interval Td is set to max(Tmin, n*C).
402 
403      4. The calculated interval T is set to a number uniformly distributed
404         between 0.5 and 1.5 times the deterministic calculated interval.
405 
406      5. The resulting value of T is divided by e-3/2=1.21828 to compensate
407         for the fact that the timer reconsideration algorithm converges to
408         a value of the RTCP bandwidth below the intended average
409   */
410 
411   int64_t now = clock_->TimeInMilliseconds();
412 
413   rtc::CritScope lock(&critical_section_rtcp_sender_);
414 
415   if (method_ == RtcpMode::kOff)
416     return false;
417 
418   if (!audio_ && sendKeyframeBeforeRTP) {
419     // for video key-frames we want to send the RTCP before the large key-frame
420     // if we have a 100 ms margin
421     now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
422   }
423 
424   if (now >= next_time_to_send_rtcp_) {
425     return true;
426   } else if (now < 0x0000ffff &&
427              next_time_to_send_rtcp_ > 0xffff0000) {  // 65 sec margin
428     // wrap
429     return true;
430   }
431   return false;
432 }
433 
434 bool
GetSendReportMetadata(const uint32_t sendReport,uint64_t * timeOfSend,uint32_t * packetCount,uint64_t * octetCount)435 RTCPSender::GetSendReportMetadata(const uint32_t sendReport,
436                                   uint64_t *timeOfSend,
437                                   uint32_t *packetCount,
438                                   uint64_t *octetCount)
439 {
440   rtc::CritScope lock(&critical_section_rtcp_sender_);
441 
442   // This is only saved when we are the sender
443   if ((last_send_report_[0] == 0) || (sendReport == 0)) {
444     return false;
445   } else {
446     for (int i = 0; i < RTCP_NUMBER_OF_SR; ++i) {
447       if (last_send_report_[i] == sendReport) {
448         *timeOfSend = last_rtcp_time_[i];
449         *packetCount = lastSRPacketCount_[i];
450         *octetCount = lastSROctetCount_[i];
451         return true;
452       }
453     }
454   }
455   return false;
456 }
457 
BuildSR(const RtcpContext & ctx)458 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
459   for (int i = (RTCP_NUMBER_OF_SR - 2); i >= 0; i--) {
460     // shift old
461     last_send_report_[i + 1] = last_send_report_[i];
462     last_rtcp_time_[i + 1] = last_rtcp_time_[i];
463     lastSRPacketCount_[i+1] = lastSRPacketCount_[i];
464     lastSROctetCount_[i+1] = lastSROctetCount_[i];
465   }
466 
467   last_rtcp_time_[0] = ctx.now_.ToMs();
468   last_send_report_[0] = (ctx.now_.seconds() << 16) + (ctx.now_.fractions() >> 16);
469   lastSRPacketCount_[0] = ctx.feedback_state_.packets_sent;
470   lastSROctetCount_[0] = ctx.feedback_state_.media_bytes_sent;
471 
472   // Timestamp shouldn't be estimated before first media frame.
473   RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
474   // The timestamp of this RTCP packet should be estimated as the timestamp of
475   // the frame being captured at this moment. We are calculating that
476   // timestamp as the last frame's timestamp + the time since the last frame
477   // was captured.
478   uint32_t rtp_rate =
479       (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) / 1000;
480   uint32_t rtp_timestamp =
481       timestamp_offset_ + last_rtp_timestamp_ +
482       (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) * rtp_rate;
483 
484   rtcp::SenderReport* report = new rtcp::SenderReport();
485   report->SetSenderSsrc(ssrc_);
486   report->SetNtp(ctx.now_);
487   report->SetRtpTimestamp(rtp_timestamp);
488   report->SetPacketCount(ctx.feedback_state_.packets_sent);
489   report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
490   report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
491 
492   return std::unique_ptr<rtcp::RtcpPacket>(report);
493 }
494 
BuildSDES(const RtcpContext & ctx)495 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
496     const RtcpContext& ctx) {
497   size_t length_cname = cname_.length();
498   RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
499 
500   rtcp::Sdes* sdes = new rtcp::Sdes();
501   sdes->AddCName(ssrc_, cname_);
502 
503   for (const auto& it : csrc_cnames_)
504     RTC_CHECK(sdes->AddCName(it.first, it.second));
505 
506   return std::unique_ptr<rtcp::RtcpPacket>(sdes);
507 }
508 
BuildRR(const RtcpContext & ctx)509 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
510   rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
511   report->SetSenderSsrc(ssrc_);
512   report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
513 
514   return std::unique_ptr<rtcp::RtcpPacket>(report);
515 }
516 
BuildPLI(const RtcpContext & ctx)517 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
518   rtcp::Pli* pli = new rtcp::Pli();
519   pli->SetSenderSsrc(ssrc_);
520   pli->SetMediaSsrc(remote_ssrc_);
521 
522   TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
523                        "RTCPSender::PLI");
524   ++packet_type_counter_.pli_packets;
525   TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_PLICount",
526                     ssrc_, packet_type_counter_.pli_packets);
527 
528   return std::unique_ptr<rtcp::RtcpPacket>(pli);
529 }
530 
BuildFIR(const RtcpContext & ctx)531 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
532   ++sequence_number_fir_;
533 
534   rtcp::Fir* fir = new rtcp::Fir();
535   fir->SetSenderSsrc(ssrc_);
536   fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
537 
538   TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
539                        "RTCPSender::FIR");
540   ++packet_type_counter_.fir_packets;
541   TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_FIRCount",
542                     ssrc_, packet_type_counter_.fir_packets);
543 
544   return std::unique_ptr<rtcp::RtcpPacket>(fir);
545 }
546 
BuildREMB(const RtcpContext & ctx)547 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
548     const RtcpContext& ctx) {
549   rtcp::Remb* remb = new rtcp::Remb();
550   remb->SetSenderSsrc(ssrc_);
551   remb->SetBitrateBps(remb_bitrate_);
552   remb->SetSsrcs(remb_ssrcs_);
553 
554   TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
555                        "RTCPSender::REMB");
556 
557   return std::unique_ptr<rtcp::RtcpPacket>(remb);
558 }
559 
SetTargetBitrate(unsigned int target_bitrate)560 void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
561   rtc::CritScope lock(&critical_section_rtcp_sender_);
562   tmmbr_send_bps_ = target_bitrate;
563 }
564 
BuildTMMBR(const RtcpContext & ctx)565 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
566     const RtcpContext& ctx) {
567   if (ctx.feedback_state_.module == nullptr)
568     return nullptr;
569   // Before sending the TMMBR check the received TMMBN, only an owner is
570   // allowed to raise the bitrate:
571   // * If the sender is an owner of the TMMBN -> send TMMBR
572   // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
573 
574   // get current bounding set from RTCP receiver
575   bool tmmbr_owner = false;
576 
577   // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
578   // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
579   // since RTCPreceiver is not doing the reverse we should be fine
580   std::vector<rtcp::TmmbItem> candidates =
581       ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
582 
583   if (!candidates.empty()) {
584     for (const auto& candidate : candidates) {
585       if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
586           candidate.packet_overhead() == packet_oh_send_) {
587         // Do not send the same tuple.
588         return nullptr;
589       }
590     }
591     if (!tmmbr_owner) {
592       // Use received bounding set as candidate set.
593       // Add current tuple.
594       candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
595 
596       // Find bounding set.
597       std::vector<rtcp::TmmbItem> bounding =
598           TMMBRHelp::FindBoundingSet(std::move(candidates));
599       tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
600       if (!tmmbr_owner) {
601         // Did not enter bounding set, no meaning to send this request.
602         return nullptr;
603       }
604     }
605   }
606 
607   if (!tmmbr_send_bps_)
608     return nullptr;
609 
610   rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
611   tmmbr->SetSenderSsrc(ssrc_);
612   rtcp::TmmbItem request;
613   request.set_ssrc(remote_ssrc_);
614   request.set_bitrate_bps(tmmbr_send_bps_);
615   request.set_packet_overhead(packet_oh_send_);
616   tmmbr->AddTmmbr(request);
617 
618   return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
619 }
620 
BuildTMMBN(const RtcpContext & ctx)621 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
622     const RtcpContext& ctx) {
623   rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
624   tmmbn->SetSenderSsrc(ssrc_);
625   for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
626     if (tmmbr.bitrate_bps() > 0) {
627       tmmbn->AddTmmbr(tmmbr);
628     }
629   }
630 
631   return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
632 }
633 
BuildAPP(const RtcpContext & ctx)634 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
635   rtcp::App* app = new rtcp::App();
636   app->SetSsrc(ssrc_);
637   app->SetSubType(app_sub_type_);
638   app->SetName(app_name_);
639   app->SetData(app_data_.get(), app_length_);
640 
641   return std::unique_ptr<rtcp::RtcpPacket>(app);
642 }
643 
BuildNACK(const RtcpContext & ctx)644 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
645     const RtcpContext& ctx) {
646   rtcp::Nack* nack = new rtcp::Nack();
647   nack->SetSenderSsrc(ssrc_);
648   nack->SetMediaSsrc(remote_ssrc_);
649   nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
650 
651   // Report stats.
652   NACKStringBuilder stringBuilder;
653   for (int idx = 0; idx < ctx.nack_size_; ++idx) {
654     stringBuilder.PushNACK(ctx.nack_list_[idx]);
655     nack_stats_.ReportRequest(ctx.nack_list_[idx]);
656   }
657   packet_type_counter_.nack_requests = nack_stats_.requests();
658   packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
659 
660   TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
661                        "RTCPSender::NACK", "nacks",
662                        TRACE_STR_COPY(stringBuilder.GetResult().c_str()));
663   ++packet_type_counter_.nack_packets;
664   TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_NACKCount",
665                     ssrc_, packet_type_counter_.nack_packets);
666 
667   return std::unique_ptr<rtcp::RtcpPacket>(nack);
668 }
669 
BuildBYE(const RtcpContext & ctx)670 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
671   rtcp::Bye* bye = new rtcp::Bye();
672   bye->SetSenderSsrc(ssrc_);
673   bye->SetCsrcs(csrcs_);
674 
675   return std::unique_ptr<rtcp::RtcpPacket>(bye);
676 }
677 
BuildExtendedReports(const RtcpContext & ctx)678 std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
679     const RtcpContext& ctx) {
680   std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
681   xr->SetSenderSsrc(ssrc_);
682 
683   if (!sending_ && xr_send_receiver_reference_time_enabled_) {
684     rtcp::Rrtr rrtr;
685     rrtr.SetNtp(ctx.now_);
686     xr->SetRrtr(rrtr);
687   }
688 
689   if (ctx.feedback_state_.has_last_xr_rr) {
690     xr->AddDlrrItem(ctx.feedback_state_.last_xr_rr);
691   }
692 
693   if (video_bitrate_allocation_) {
694     rtcp::TargetBitrate target_bitrate;
695 
696     for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
697       for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
698         if (video_bitrate_allocation_->HasBitrate(sl, tl)) {
699           target_bitrate.AddTargetBitrate(
700               sl, tl, video_bitrate_allocation_->GetBitrate(sl, tl) / 1000);
701         }
702       }
703     }
704 
705     xr->SetTargetBitrate(target_bitrate);
706     video_bitrate_allocation_.reset();
707   }
708 
709   if (xr_voip_metric_) {
710     rtcp::VoipMetric voip;
711     voip.SetMediaSsrc(remote_ssrc_);
712     voip.SetVoipMetric(*xr_voip_metric_);
713     xr_voip_metric_.reset();
714 
715     xr->SetVoipMetric(voip);
716   }
717 
718   return std::move(xr);
719 }
720 
SendRTCP(const FeedbackState & feedback_state,RTCPPacketType packetType,int32_t nack_size,const uint16_t * nack_list)721 int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
722                              RTCPPacketType packetType,
723                              int32_t nack_size,
724                              const uint16_t* nack_list) {
725   return SendCompoundRTCP(
726       feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
727       nack_size, nack_list);
728 }
729 
SendCompoundRTCP(const FeedbackState & feedback_state,const std::set<RTCPPacketType> & packet_types,int32_t nack_size,const uint16_t * nack_list)730 int32_t RTCPSender::SendCompoundRTCP(
731     const FeedbackState& feedback_state,
732     const std::set<RTCPPacketType>& packet_types,
733     int32_t nack_size,
734     const uint16_t* nack_list) {
735   PacketContainer container(transport_, event_log_);
736   size_t max_packet_size;
737 
738   {
739     rtc::CritScope lock(&critical_section_rtcp_sender_);
740     if (method_ == RtcpMode::kOff) {
741       RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
742       return -1;
743     }
744     // Add all flags as volatile. Non volatile entries will not be overwritten.
745     // All new volatile flags added will be consumed by the end of this call.
746     SetFlags(packet_types, true);
747 
748     // Prevent sending streams to send SR before any media has been sent.
749     const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
750     if (!can_calculate_rtp_timestamp) {
751       bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
752       bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
753       bool sender_report = consumed_report_flag || consumed_sr_flag;
754       if (sender_report && AllVolatileFlagsConsumed()) {
755         // This call was for Sender Report and nothing else.
756         return 0;
757       }
758       if (sending_ && method_ == RtcpMode::kCompound) {
759         // Not allowed to send any RTCP packet without sender report.
760         return -1;
761       }
762     }
763 
764     if (packet_type_counter_.first_packet_time_ms == -1)
765       packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
766 
767     // We need to send our NTP even if we haven't received any reports.
768     RtcpContext context(feedback_state, nack_size, nack_list,
769                         clock_->CurrentNtpTime());
770 
771     PrepareReport(feedback_state);
772 
773     std::unique_ptr<rtcp::RtcpPacket> packet_bye;
774 
775     auto it = report_flags_.begin();
776     while (it != report_flags_.end()) {
777       auto builder_it = builders_.find(it->type);
778       RTC_DCHECK(builder_it != builders_.end())
779           << "Could not find builder for packet type " << it->type;
780       if (it->is_volatile) {
781         report_flags_.erase(it++);
782       } else {
783         ++it;
784       }
785 
786       BuilderFunc func = builder_it->second;
787       std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
788       if (packet.get() == nullptr)
789         return -1;
790       // If there is a BYE, don't append now - save it and append it
791       // at the end later.
792       if (builder_it->first == kRtcpBye) {
793         packet_bye = std::move(packet);
794       } else {
795         container.Append(packet.release());
796       }
797     }
798 
799     // Append the BYE now at the end
800     if (packet_bye) {
801       container.Append(packet_bye.release());
802     }
803 
804     if (packet_type_counter_observer_ != nullptr) {
805       packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
806           remote_ssrc_, packet_type_counter_);
807     }
808 
809     RTC_DCHECK(AllVolatileFlagsConsumed());
810     max_packet_size = max_packet_size_;
811   }
812 
813   size_t bytes_sent = container.SendPackets(max_packet_size);
814   return bytes_sent == 0 ? -1 : 0;
815 }
816 
PrepareReport(const FeedbackState & feedback_state)817 void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
818   bool generate_report;
819   if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
820     // Report type already explicitly set, don't automatically populate.
821     generate_report = true;
822     RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
823   } else {
824     generate_report =
825         (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
826         method_ == RtcpMode::kCompound;
827     if (generate_report)
828       SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
829   }
830 
831   if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
832     SetFlag(kRtcpSdes, true);
833 
834   if (generate_report) {
835     if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
836         feedback_state.has_last_xr_rr || video_bitrate_allocation_) {
837       SetFlag(kRtcpAnyExtendedReports, true);
838     }
839 
840     // generate next time to send an RTCP report
841     uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
842 
843     if (!audio_) {
844       if (sending_) {
845         // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
846         uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
847         if (send_bitrate_kbit != 0)
848           minIntervalMs = 360000 / send_bitrate_kbit;
849       }
850       if (minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
851         minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
852     }
853     // The interval between RTCP packets is varied randomly over the
854     // range [1/2,3/2] times the calculated interval.
855     uint32_t timeToNext =
856         random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
857     next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
858 
859     // RtcpSender expected to be used for sending either just sender reports
860     // or just receiver reports.
861     RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
862   }
863 }
864 
CreateReportBlocks(const FeedbackState & feedback_state)865 std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
866     const FeedbackState& feedback_state) {
867   std::vector<rtcp::ReportBlock> result;
868   if (!receive_statistics_)
869     return result;
870 
871   // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
872   // compound rtcp packet when single rtcp module is used for multiple media
873   // streams.
874   result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
875 
876   if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
877                           (feedback_state.last_rr_ntp_frac != 0))) {
878     // Get our NTP as late as possible to avoid a race.
879     uint32_t now = CompactNtp(clock_->CurrentNtpTime());
880 
881     uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
882     receive_time <<= 16;
883     receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
884 
885     uint32_t delay_since_last_sr = now - receive_time;
886     // TODO(danilchap): Instead of setting same value on all report blocks,
887     // set only when media_ssrc match sender ssrc of the sender report
888     // remote times were taken from.
889     for (auto& report_block : result) {
890       report_block.SetLastSr(feedback_state.remote_sr);
891       report_block.SetDelayLastSr(delay_since_last_sr);
892     }
893   }
894   return result;
895 }
896 
SetCsrcs(const std::vector<uint32_t> & csrcs)897 void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
898   RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
899   rtc::CritScope lock(&critical_section_rtcp_sender_);
900   csrcs_ = csrcs;
901 }
902 
SetApplicationSpecificData(uint8_t subType,uint32_t name,const uint8_t * data,uint16_t length)903 int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
904                                                uint32_t name,
905                                                const uint8_t* data,
906                                                uint16_t length) {
907   if (length % 4 != 0) {
908     RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
909     return -1;
910   }
911   rtc::CritScope lock(&critical_section_rtcp_sender_);
912 
913   SetFlag(kRtcpApp, true);
914   app_sub_type_ = subType;
915   app_name_ = name;
916   app_data_.reset(new uint8_t[length]);
917   app_length_ = length;
918   memcpy(app_data_.get(), data, length);
919   return 0;
920 }
921 
922 // TODO(sprang): Remove support for VoIP metrics? (Not used in receiver.)
SetRTCPVoIPMetrics(const RTCPVoIPMetric * VoIPMetric)923 int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
924   rtc::CritScope lock(&critical_section_rtcp_sender_);
925   xr_voip_metric_.emplace(*VoIPMetric);
926 
927   SetFlag(kRtcpAnyExtendedReports, true);
928   return 0;
929 }
930 
SendRtcpXrReceiverReferenceTime(bool enable)931 void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
932   rtc::CritScope lock(&critical_section_rtcp_sender_);
933   xr_send_receiver_reference_time_enabled_ = enable;
934 }
935 
RtcpXrReceiverReferenceTime() const936 bool RTCPSender::RtcpXrReceiverReferenceTime() const {
937   rtc::CritScope lock(&critical_section_rtcp_sender_);
938   return xr_send_receiver_reference_time_enabled_;
939 }
940 
SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set)941 void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
942   rtc::CritScope lock(&critical_section_rtcp_sender_);
943   tmmbn_to_send_ = std::move(bounding_set);
944   SetFlag(kRtcpTmmbn, true);
945 }
946 
SetFlag(uint32_t type,bool is_volatile)947 void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
948   if (type & kRtcpAnyExtendedReports) {
949     report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
950   } else {
951     report_flags_.insert(ReportFlag(type, is_volatile));
952   }
953 }
954 
SetFlags(const std::set<RTCPPacketType> & types,bool is_volatile)955 void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
956                           bool is_volatile) {
957   for (RTCPPacketType type : types)
958     SetFlag(type, is_volatile);
959 }
960 
IsFlagPresent(uint32_t type) const961 bool RTCPSender::IsFlagPresent(uint32_t type) const {
962   return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
963 }
964 
ConsumeFlag(uint32_t type,bool forced)965 bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
966   auto it = report_flags_.find(ReportFlag(type, false));
967   if (it == report_flags_.end())
968     return false;
969   if (it->is_volatile || forced)
970     report_flags_.erase((it));
971   return true;
972 }
973 
AllVolatileFlagsConsumed() const974 bool RTCPSender::AllVolatileFlagsConsumed() const {
975   for (const ReportFlag& flag : report_flags_) {
976     if (flag.is_volatile)
977       return false;
978   }
979   return true;
980 }
981 
SetVideoBitrateAllocation(const BitrateAllocation & bitrate)982 void RTCPSender::SetVideoBitrateAllocation(const BitrateAllocation& bitrate) {
983   rtc::CritScope lock(&critical_section_rtcp_sender_);
984   video_bitrate_allocation_.emplace(bitrate);
985   SetFlag(kRtcpAnyExtendedReports, true);
986 }
987 
SendFeedbackPacket(const rtcp::TransportFeedback & packet)988 bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
989   class Sender : public rtcp::RtcpPacket::PacketReadyCallback {
990    public:
991     Sender(Transport* transport, RtcEventLog* event_log)
992         : transport_(transport), event_log_(event_log), send_failure_(false) {}
993 
994     void OnPacketReady(uint8_t* data, size_t length) override {
995       if (transport_->SendRtcp(data, length)) {
996         if (event_log_) {
997           event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(
998               rtc::ArrayView<const uint8_t>(data, length)));
999         }
1000       } else {
1001         send_failure_ = true;
1002       }
1003     }
1004 
1005     Transport* const transport_;
1006     RtcEventLog* const event_log_;
1007     bool send_failure_;
1008     // TODO(terelius): We would like to
1009     // RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sender);
1010     // but we can't because of an incorrect warning (C4822) in MVS 2013.
1011   } sender(transport_, event_log_);
1012 
1013   size_t max_packet_size;
1014   {
1015     rtc::CritScope lock(&critical_section_rtcp_sender_);
1016     if (method_ == RtcpMode::kOff)
1017       return false;
1018     max_packet_size = max_packet_size_;
1019   }
1020 
1021   RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1022   uint8_t buffer[IP_PACKET_SIZE];
1023   return packet.BuildExternalBuffer(buffer, max_packet_size, &sender) &&
1024          !sender.send_failure_;
1025 }
1026 
1027 }  // namespace webrtc
1028