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