blob: 7bf1ec883cf700bfd2ca9a451215bcd082d0dee5 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
13
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000014#include <map>
Erik Språng242e22b2015-05-11 10:17:43 +020015#include <set>
edjee@google.com79b02892013-04-04 19:43:34 +000016#include <sstream>
17#include <string>
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000018
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000019#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000020#include "webrtc/base/thread_annotations.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000021#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
22#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
24#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Erik Språngbdc0b0d2015-06-22 15:21:24 +020025#include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000026#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
27#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
28#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
pbos2d566682015-09-28 09:59:31 -070029#include "webrtc/transport.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000030#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
32namespace webrtc {
pwestin@webrtc.org741da942011-09-20 13:52:04 +000033
wu@webrtc.org822fbd82013-08-15 23:38:54 +000034class ModuleRtpRtcpImpl;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000035class RTCPReceiver;
pwestin@webrtc.org741da942011-09-20 13:52:04 +000036
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000037class NACKStringBuilder {
38 public:
39 NACKStringBuilder();
40 ~NACKStringBuilder();
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000041
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000042 void PushNACK(uint16_t nack);
43 std::string GetResult();
edjee@google.com79b02892013-04-04 19:43:34 +000044
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000045 private:
Erik Språng242e22b2015-05-11 10:17:43 +020046 std::ostringstream stream_;
47 int count_;
48 uint16_t prevNack_;
49 bool consecutive_;
edjee@google.com79b02892013-04-04 19:43:34 +000050};
51
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000052class RTCPSender {
danilchap162abd32015-12-10 02:39:40 -080053 public:
54 struct FeedbackState {
55 FeedbackState();
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000056
danilchap162abd32015-12-10 02:39:40 -080057 uint8_t send_payload_type;
58 uint32_t frequency_hz;
59 uint32_t packets_sent;
60 size_t media_bytes_sent;
61 uint32_t send_bitrate;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000062
danilchap162abd32015-12-10 02:39:40 -080063 uint32_t last_rr_ntp_secs;
64 uint32_t last_rr_ntp_frac;
65 uint32_t remote_sr;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000066
danilchap162abd32015-12-10 02:39:40 -080067 bool has_last_xr_rr;
68 RtcpReceiveTimeInfo last_xr_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000069
danilchap162abd32015-12-10 02:39:40 -080070 // Used when generating TMMBR.
71 ModuleRtpRtcpImpl* module;
72 };
Erik Språng61be2a42015-04-27 13:32:52 +020073
danilchap162abd32015-12-10 02:39:40 -080074 RTCPSender(bool audio,
75 Clock* clock,
76 ReceiveStatistics* receive_statistics,
77 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
78 Transport* outgoing_transport);
79 virtual ~RTCPSender();
niklase@google.com470e71d2011-07-07 08:21:25 +000080
danilchap162abd32015-12-10 02:39:40 -080081 RtcpMode Status() const;
82 void SetRTCPStatus(RtcpMode method);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
danilchap162abd32015-12-10 02:39:40 -080084 bool Sending() const;
85 int32_t SetSendingStatus(const FeedbackState& feedback_state,
86 bool enabled); // combine the functions
niklase@google.com470e71d2011-07-07 08:21:25 +000087
danilchap162abd32015-12-10 02:39:40 -080088 int32_t SetNackStatus(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
danilchap162abd32015-12-10 02:39:40 -080090 void SetStartTimestamp(uint32_t start_timestamp);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000091
danilchap162abd32015-12-10 02:39:40 -080092 void SetLastRtpTime(uint32_t rtp_timestamp, int64_t capture_time_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000093
danilchap162abd32015-12-10 02:39:40 -080094 void SetSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000095
danilchap162abd32015-12-10 02:39:40 -080096 void SetRemoteSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
danilchap162abd32015-12-10 02:39:40 -080098 int32_t SetCNAME(const char* cName);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
danilchap162abd32015-12-10 02:39:40 -0800100 int32_t AddMixedCNAME(uint32_t SSRC, const char* c_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
danilchap162abd32015-12-10 02:39:40 -0800102 int32_t RemoveMixedCNAME(uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
danilchap162abd32015-12-10 02:39:40 -0800104 int64_t SendTimeOfSendReport(uint32_t sendReport);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
danilchap162abd32015-12-10 02:39:40 -0800106 bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000107
danilchap162abd32015-12-10 02:39:40 -0800108 bool TimeToSendRTCPReport(bool sendKeyframeBeforeRTP = false) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
danilchap162abd32015-12-10 02:39:40 -0800110 int32_t SendRTCP(const FeedbackState& feedback_state,
111 RTCPPacketType packetType,
112 int32_t nackSize = 0,
113 const uint16_t* nackList = 0,
114 bool repeat = false,
115 uint64_t pictureID = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
danilchap162abd32015-12-10 02:39:40 -0800117 int32_t SendCompoundRTCP(const FeedbackState& feedback_state,
118 const std::set<RTCPPacketType>& packetTypes,
119 int32_t nackSize = 0,
120 const uint16_t* nackList = 0,
121 bool repeat = false,
122 uint64_t pictureID = 0);
Erik Språng242e22b2015-05-11 10:17:43 +0200123
danilchap162abd32015-12-10 02:39:40 -0800124 bool REMB() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
danilchap162abd32015-12-10 02:39:40 -0800126 void SetREMBStatus(bool enable);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000127
danilchap162abd32015-12-10 02:39:40 -0800128 void SetREMBData(uint32_t bitrate, const std::vector<uint32_t>& ssrcs);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000129
danilchap162abd32015-12-10 02:39:40 -0800130 bool TMMBR() const;
mflodman@webrtc.org84dc3d12011-12-22 10:26:13 +0000131
danilchap162abd32015-12-10 02:39:40 -0800132 void SetTMMBRStatus(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
danilchap162abd32015-12-10 02:39:40 -0800134 int32_t SetTMMBN(const TMMBRSet* boundingSet, uint32_t maxBitrateKbit);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
danilchap162abd32015-12-10 02:39:40 -0800136 int32_t SetApplicationSpecificData(uint8_t subType,
137 uint32_t name,
138 const uint8_t* data,
139 uint16_t length);
140 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141
danilchap162abd32015-12-10 02:39:40 -0800142 void SendRtcpXrReceiverReferenceTime(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
danilchap162abd32015-12-10 02:39:40 -0800144 bool RtcpXrReceiverReferenceTime() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
danilchap162abd32015-12-10 02:39:40 -0800146 void SetCsrcs(const std::vector<uint32_t>& csrcs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
danilchap162abd32015-12-10 02:39:40 -0800148 void SetTargetBitrate(unsigned int target_bitrate);
149 bool SendFeedbackPacket(const rtcp::TransportFeedback& packet);
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000150
danilchap162abd32015-12-10 02:39:40 -0800151 private:
152 class RtcpContext;
Erik Språng242e22b2015-05-11 10:17:43 +0200153
danilchap162abd32015-12-10 02:39:40 -0800154 // Determine which RTCP messages should be sent and setup flags.
155 void PrepareReport(const std::set<RTCPPacketType>& packetTypes,
156 const FeedbackState& feedback_state)
157 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
danilchap162abd32015-12-10 02:39:40 -0800159 int32_t AddReportBlock(const RTCPReportBlock& report_block)
160 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
danilchap162abd32015-12-10 02:39:40 -0800162 bool PrepareReportBlock(const FeedbackState& feedback_state,
163 uint32_t ssrc,
164 StreamStatistician* statistician,
165 RTCPReportBlock* report_block);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000166
danilchap162abd32015-12-10 02:39:40 -0800167 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSR(const RtcpContext& context)
168 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
169 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRR(const RtcpContext& context)
170 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
171 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSDES(const RtcpContext& context)
172 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
173 rtc::scoped_ptr<rtcp::RtcpPacket> BuildPLI(const RtcpContext& context)
174 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
175 rtc::scoped_ptr<rtcp::RtcpPacket> BuildREMB(const RtcpContext& context)
176 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
177 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBR(const RtcpContext& context)
178 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
179 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBN(const RtcpContext& context)
180 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
181 rtc::scoped_ptr<rtcp::RtcpPacket> BuildAPP(const RtcpContext& context)
182 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
183 rtc::scoped_ptr<rtcp::RtcpPacket> BuildVoIPMetric(const RtcpContext& context)
184 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
185 rtc::scoped_ptr<rtcp::RtcpPacket> BuildBYE(const RtcpContext& context)
186 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
187 rtc::scoped_ptr<rtcp::RtcpPacket> BuildFIR(const RtcpContext& context)
188 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
189 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSLI(const RtcpContext& context)
190 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
191 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRPSI(const RtcpContext& context)
192 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
193 rtc::scoped_ptr<rtcp::RtcpPacket> BuildNACK(const RtcpContext& context)
194 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
195 rtc::scoped_ptr<rtcp::RtcpPacket> BuildReceiverReferenceTime(
196 const RtcpContext& context)
197 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
198 rtc::scoped_ptr<rtcp::RtcpPacket> BuildDlrr(const RtcpContext& context)
199 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000200
danilchap162abd32015-12-10 02:39:40 -0800201 private:
202 const bool audio_;
203 Clock* const clock_;
204 RtcpMode method_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
danilchap162abd32015-12-10 02:39:40 -0800206 Transport* const transport_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
danilchap162abd32015-12-10 02:39:40 -0800208 rtc::scoped_ptr<CriticalSectionWrapper> critical_section_rtcp_sender_;
209 bool using_nack_ GUARDED_BY(critical_section_rtcp_sender_);
210 bool sending_ GUARDED_BY(critical_section_rtcp_sender_);
211 bool remb_enabled_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
danilchap162abd32015-12-10 02:39:40 -0800213 int64_t next_time_to_send_rtcp_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
danilchap162abd32015-12-10 02:39:40 -0800215 uint32_t start_timestamp_ GUARDED_BY(critical_section_rtcp_sender_);
216 uint32_t last_rtp_timestamp_ GUARDED_BY(critical_section_rtcp_sender_);
217 int64_t last_frame_capture_time_ms_ GUARDED_BY(critical_section_rtcp_sender_);
218 uint32_t ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
219 // SSRC that we receive on our RTP channel
220 uint32_t remote_ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
221 std::string cname_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
danilchap162abd32015-12-10 02:39:40 -0800223 ReceiveStatistics* receive_statistics_
224 GUARDED_BY(critical_section_rtcp_sender_);
225 std::map<uint32_t, rtcp::ReportBlock> report_blocks_
226 GUARDED_BY(critical_section_rtcp_sender_);
227 std::map<uint32_t, std::string> csrc_cnames_
228 GUARDED_BY(critical_section_rtcp_sender_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000229
danilchap162abd32015-12-10 02:39:40 -0800230 // Sent
231 uint32_t last_send_report_[RTCP_NUMBER_OF_SR] GUARDED_BY(
232 critical_section_rtcp_sender_); // allow packet loss and RTT above 1 sec
233 int64_t last_rtcp_time_[RTCP_NUMBER_OF_SR] GUARDED_BY(
234 critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
danilchap162abd32015-12-10 02:39:40 -0800236 // Sent XR receiver reference time report.
237 // <mid ntp (mid 32 bits of the 64 bits NTP timestamp), send time in ms>.
238 std::map<uint32_t, int64_t> last_xr_rr_
239 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000240
danilchap162abd32015-12-10 02:39:40 -0800241 // send CSRCs
242 std::vector<uint32_t> csrcs_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000243
danilchap162abd32015-12-10 02:39:40 -0800244 // Full intra request
245 uint8_t sequence_number_fir_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
danilchap162abd32015-12-10 02:39:40 -0800247 // REMB
248 uint32_t remb_bitrate_ GUARDED_BY(critical_section_rtcp_sender_);
249 std::vector<uint32_t> remb_ssrcs_ GUARDED_BY(critical_section_rtcp_sender_);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000250
danilchap162abd32015-12-10 02:39:40 -0800251 TMMBRHelp tmmbr_help_ GUARDED_BY(critical_section_rtcp_sender_);
252 uint32_t tmmbr_send_ GUARDED_BY(critical_section_rtcp_sender_);
253 uint32_t packet_oh_send_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
danilchap162abd32015-12-10 02:39:40 -0800255 // APP
256 uint8_t app_sub_type_ GUARDED_BY(critical_section_rtcp_sender_);
257 uint32_t app_name_ GUARDED_BY(critical_section_rtcp_sender_);
258 rtc::scoped_ptr<uint8_t[]> app_data_
259 GUARDED_BY(critical_section_rtcp_sender_);
260 uint16_t app_length_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
danilchap162abd32015-12-10 02:39:40 -0800262 // True if sending of XR Receiver reference time report is enabled.
263 bool xr_send_receiver_reference_time_enabled_
264 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000265
danilchap162abd32015-12-10 02:39:40 -0800266 // XR VoIP metric
267 RTCPVoIPMetric xr_voip_metric_ GUARDED_BY(critical_section_rtcp_sender_);
edjee@google.com79b02892013-04-04 19:43:34 +0000268
danilchap162abd32015-12-10 02:39:40 -0800269 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
270 RtcpPacketTypeCounter packet_type_counter_
271 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000272
danilchap162abd32015-12-10 02:39:40 -0800273 RTCPUtility::NackStats nack_stats_ GUARDED_BY(critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200274
danilchap162abd32015-12-10 02:39:40 -0800275 void SetFlag(RTCPPacketType type, bool is_volatile)
276 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
277 void SetFlags(const std::set<RTCPPacketType>& types, bool is_volatile)
278 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
279 bool IsFlagPresent(RTCPPacketType type) const
280 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
281 bool ConsumeFlag(RTCPPacketType type, bool forced = false)
282 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
283 bool AllVolatileFlagsConsumed() const
284 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
285 struct ReportFlag {
286 ReportFlag(RTCPPacketType type, bool is_volatile)
287 : type(type), is_volatile(is_volatile) {}
288 bool operator<(const ReportFlag& flag) const { return type < flag.type; }
289 bool operator==(const ReportFlag& flag) const { return type == flag.type; }
290 const RTCPPacketType type;
291 const bool is_volatile;
292 };
Erik Språng242e22b2015-05-11 10:17:43 +0200293
danilchap162abd32015-12-10 02:39:40 -0800294 std::set<ReportFlag> report_flags_ GUARDED_BY(critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200295
danilchap162abd32015-12-10 02:39:40 -0800296 typedef rtc::scoped_ptr<rtcp::RtcpPacket> (RTCPSender::*BuilderFunc)(
297 const RtcpContext&);
298 std::map<RTCPPacketType, BuilderFunc> builders_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000300} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000301
danilchap162abd32015-12-10 02:39:40 -0800302#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_