blob: fcb9012887e375761d3374cb6c883ab4705c8591 [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>
danilchapb8b6fbb2015-12-10 05:05:27 -080018#include <vector>
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000019
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000020#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000021#include "webrtc/base/thread_annotations.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000022#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
23#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Erik Språngbdc0b0d2015-06-22 15:21:24 +020026#include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000027#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
28#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
29#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
pbos2d566682015-09-28 09:59:31 -070030#include "webrtc/transport.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000031#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33namespace webrtc {
pwestin@webrtc.org741da942011-09-20 13:52:04 +000034
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035class ModuleRtpRtcpImpl;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000036class RTCPReceiver;
pwestin@webrtc.org741da942011-09-20 13:52:04 +000037
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000038class NACKStringBuilder {
39 public:
40 NACKStringBuilder();
41 ~NACKStringBuilder();
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000042
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000043 void PushNACK(uint16_t nack);
44 std::string GetResult();
edjee@google.com79b02892013-04-04 19:43:34 +000045
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000046 private:
Erik Språng242e22b2015-05-11 10:17:43 +020047 std::ostringstream stream_;
48 int count_;
49 uint16_t prevNack_;
50 bool consecutive_;
edjee@google.com79b02892013-04-04 19:43:34 +000051};
52
asapersson@webrtc.org9ffd8fe2015-01-21 08:22:50 +000053class RTCPSender {
danilchap162abd32015-12-10 02:39:40 -080054 public:
55 struct FeedbackState {
56 FeedbackState();
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000057
danilchap162abd32015-12-10 02:39:40 -080058 uint8_t send_payload_type;
59 uint32_t frequency_hz;
60 uint32_t packets_sent;
61 size_t media_bytes_sent;
62 uint32_t send_bitrate;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000063
danilchap162abd32015-12-10 02:39:40 -080064 uint32_t last_rr_ntp_secs;
65 uint32_t last_rr_ntp_frac;
66 uint32_t remote_sr;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000067
danilchap162abd32015-12-10 02:39:40 -080068 bool has_last_xr_rr;
69 RtcpReceiveTimeInfo last_xr_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000070
danilchap162abd32015-12-10 02:39:40 -080071 // Used when generating TMMBR.
72 ModuleRtpRtcpImpl* module;
73 };
Erik Språng61be2a42015-04-27 13:32:52 +020074
danilchap162abd32015-12-10 02:39:40 -080075 RTCPSender(bool audio,
76 Clock* clock,
77 ReceiveStatistics* receive_statistics,
78 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
79 Transport* outgoing_transport);
80 virtual ~RTCPSender();
niklase@google.com470e71d2011-07-07 08:21:25 +000081
danilchap162abd32015-12-10 02:39:40 -080082 RtcpMode Status() const;
83 void SetRTCPStatus(RtcpMode method);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
danilchap162abd32015-12-10 02:39:40 -080085 bool Sending() const;
86 int32_t SetSendingStatus(const FeedbackState& feedback_state,
87 bool enabled); // combine the functions
niklase@google.com470e71d2011-07-07 08:21:25 +000088
danilchap162abd32015-12-10 02:39:40 -080089 int32_t SetNackStatus(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
danilchap162abd32015-12-10 02:39:40 -080091 void SetStartTimestamp(uint32_t start_timestamp);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000092
danilchap162abd32015-12-10 02:39:40 -080093 void SetLastRtpTime(uint32_t rtp_timestamp, int64_t capture_time_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000094
danilchap162abd32015-12-10 02:39:40 -080095 void SetSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
danilchap162abd32015-12-10 02:39:40 -080097 void SetRemoteSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
danilchap162abd32015-12-10 02:39:40 -080099 int32_t SetCNAME(const char* cName);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
danilchap162abd32015-12-10 02:39:40 -0800101 int32_t AddMixedCNAME(uint32_t SSRC, const char* c_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
danilchap162abd32015-12-10 02:39:40 -0800103 int32_t RemoveMixedCNAME(uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
danilchap162abd32015-12-10 02:39:40 -0800105 int64_t SendTimeOfSendReport(uint32_t sendReport);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
danilchap162abd32015-12-10 02:39:40 -0800107 bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000108
danilchap162abd32015-12-10 02:39:40 -0800109 bool TimeToSendRTCPReport(bool sendKeyframeBeforeRTP = false) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
danilchap162abd32015-12-10 02:39:40 -0800111 int32_t SendRTCP(const FeedbackState& feedback_state,
112 RTCPPacketType packetType,
113 int32_t nackSize = 0,
114 const uint16_t* nackList = 0,
115 bool repeat = false,
116 uint64_t pictureID = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
danilchap162abd32015-12-10 02:39:40 -0800118 int32_t SendCompoundRTCP(const FeedbackState& feedback_state,
119 const std::set<RTCPPacketType>& packetTypes,
120 int32_t nackSize = 0,
121 const uint16_t* nackList = 0,
122 bool repeat = false,
123 uint64_t pictureID = 0);
Erik Språng242e22b2015-05-11 10:17:43 +0200124
danilchap162abd32015-12-10 02:39:40 -0800125 bool REMB() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
danilchap162abd32015-12-10 02:39:40 -0800127 void SetREMBStatus(bool enable);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000128
danilchap162abd32015-12-10 02:39:40 -0800129 void SetREMBData(uint32_t bitrate, const std::vector<uint32_t>& ssrcs);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000130
danilchap162abd32015-12-10 02:39:40 -0800131 bool TMMBR() const;
mflodman@webrtc.org84dc3d12011-12-22 10:26:13 +0000132
danilchap162abd32015-12-10 02:39:40 -0800133 void SetTMMBRStatus(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
danilchap162abd32015-12-10 02:39:40 -0800135 int32_t SetTMMBN(const TMMBRSet* boundingSet, uint32_t maxBitrateKbit);
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
danilchap162abd32015-12-10 02:39:40 -0800137 int32_t SetApplicationSpecificData(uint8_t subType,
138 uint32_t name,
139 const uint8_t* data,
140 uint16_t length);
141 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000142
danilchap162abd32015-12-10 02:39:40 -0800143 void SendRtcpXrReceiverReferenceTime(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
danilchap162abd32015-12-10 02:39:40 -0800145 bool RtcpXrReceiverReferenceTime() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
danilchap162abd32015-12-10 02:39:40 -0800147 void SetCsrcs(const std::vector<uint32_t>& csrcs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
danilchap162abd32015-12-10 02:39:40 -0800149 void SetTargetBitrate(unsigned int target_bitrate);
150 bool SendFeedbackPacket(const rtcp::TransportFeedback& packet);
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000151
danilchap162abd32015-12-10 02:39:40 -0800152 private:
153 class RtcpContext;
Erik Språng242e22b2015-05-11 10:17:43 +0200154
danilchap162abd32015-12-10 02:39:40 -0800155 // Determine which RTCP messages should be sent and setup flags.
156 void PrepareReport(const std::set<RTCPPacketType>& packetTypes,
157 const FeedbackState& feedback_state)
158 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
danilchap162abd32015-12-10 02:39:40 -0800160 int32_t AddReportBlock(const RTCPReportBlock& report_block)
161 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
danilchap162abd32015-12-10 02:39:40 -0800163 bool PrepareReportBlock(const FeedbackState& feedback_state,
164 uint32_t ssrc,
165 StreamStatistician* statistician,
166 RTCPReportBlock* report_block);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000167
danilchap162abd32015-12-10 02:39:40 -0800168 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSR(const RtcpContext& context)
169 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
170 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRR(const RtcpContext& context)
171 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
172 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSDES(const RtcpContext& context)
173 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
174 rtc::scoped_ptr<rtcp::RtcpPacket> BuildPLI(const RtcpContext& context)
175 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
176 rtc::scoped_ptr<rtcp::RtcpPacket> BuildREMB(const RtcpContext& context)
177 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
178 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBR(const RtcpContext& context)
179 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
180 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBN(const RtcpContext& context)
181 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
182 rtc::scoped_ptr<rtcp::RtcpPacket> BuildAPP(const RtcpContext& context)
183 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
184 rtc::scoped_ptr<rtcp::RtcpPacket> BuildVoIPMetric(const RtcpContext& context)
185 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
186 rtc::scoped_ptr<rtcp::RtcpPacket> BuildBYE(const RtcpContext& context)
187 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
188 rtc::scoped_ptr<rtcp::RtcpPacket> BuildFIR(const RtcpContext& context)
189 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
190 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSLI(const RtcpContext& context)
191 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
192 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRPSI(const RtcpContext& context)
193 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
194 rtc::scoped_ptr<rtcp::RtcpPacket> BuildNACK(const RtcpContext& context)
195 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
196 rtc::scoped_ptr<rtcp::RtcpPacket> BuildReceiverReferenceTime(
197 const RtcpContext& context)
198 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
199 rtc::scoped_ptr<rtcp::RtcpPacket> BuildDlrr(const RtcpContext& context)
200 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000201
danilchap162abd32015-12-10 02:39:40 -0800202 private:
203 const bool audio_;
204 Clock* const clock_;
205 RtcpMode method_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
danilchap162abd32015-12-10 02:39:40 -0800207 Transport* const transport_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
danilchap162abd32015-12-10 02:39:40 -0800209 rtc::scoped_ptr<CriticalSectionWrapper> critical_section_rtcp_sender_;
210 bool using_nack_ GUARDED_BY(critical_section_rtcp_sender_);
211 bool sending_ GUARDED_BY(critical_section_rtcp_sender_);
212 bool remb_enabled_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
danilchap162abd32015-12-10 02:39:40 -0800214 int64_t next_time_to_send_rtcp_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
danilchap162abd32015-12-10 02:39:40 -0800216 uint32_t start_timestamp_ GUARDED_BY(critical_section_rtcp_sender_);
217 uint32_t last_rtp_timestamp_ GUARDED_BY(critical_section_rtcp_sender_);
218 int64_t last_frame_capture_time_ms_ GUARDED_BY(critical_section_rtcp_sender_);
219 uint32_t ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
220 // SSRC that we receive on our RTP channel
221 uint32_t remote_ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
222 std::string cname_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
danilchap162abd32015-12-10 02:39:40 -0800224 ReceiveStatistics* receive_statistics_
225 GUARDED_BY(critical_section_rtcp_sender_);
226 std::map<uint32_t, rtcp::ReportBlock> report_blocks_
227 GUARDED_BY(critical_section_rtcp_sender_);
228 std::map<uint32_t, std::string> csrc_cnames_
229 GUARDED_BY(critical_section_rtcp_sender_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000230
danilchap162abd32015-12-10 02:39:40 -0800231 // Sent
232 uint32_t last_send_report_[RTCP_NUMBER_OF_SR] GUARDED_BY(
233 critical_section_rtcp_sender_); // allow packet loss and RTT above 1 sec
234 int64_t last_rtcp_time_[RTCP_NUMBER_OF_SR] GUARDED_BY(
235 critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
danilchap162abd32015-12-10 02:39:40 -0800237 // Sent XR receiver reference time report.
238 // <mid ntp (mid 32 bits of the 64 bits NTP timestamp), send time in ms>.
239 std::map<uint32_t, int64_t> last_xr_rr_
240 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000241
danilchap162abd32015-12-10 02:39:40 -0800242 // send CSRCs
243 std::vector<uint32_t> csrcs_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
danilchap162abd32015-12-10 02:39:40 -0800245 // Full intra request
246 uint8_t sequence_number_fir_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
danilchap162abd32015-12-10 02:39:40 -0800248 // REMB
249 uint32_t remb_bitrate_ GUARDED_BY(critical_section_rtcp_sender_);
250 std::vector<uint32_t> remb_ssrcs_ GUARDED_BY(critical_section_rtcp_sender_);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000251
danilchap162abd32015-12-10 02:39:40 -0800252 TMMBRHelp tmmbr_help_ GUARDED_BY(critical_section_rtcp_sender_);
253 uint32_t tmmbr_send_ GUARDED_BY(critical_section_rtcp_sender_);
254 uint32_t packet_oh_send_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
danilchap162abd32015-12-10 02:39:40 -0800256 // APP
257 uint8_t app_sub_type_ GUARDED_BY(critical_section_rtcp_sender_);
258 uint32_t app_name_ GUARDED_BY(critical_section_rtcp_sender_);
259 rtc::scoped_ptr<uint8_t[]> app_data_
260 GUARDED_BY(critical_section_rtcp_sender_);
261 uint16_t app_length_ GUARDED_BY(critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
danilchap162abd32015-12-10 02:39:40 -0800263 // True if sending of XR Receiver reference time report is enabled.
264 bool xr_send_receiver_reference_time_enabled_
265 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000266
danilchap162abd32015-12-10 02:39:40 -0800267 // XR VoIP metric
268 RTCPVoIPMetric xr_voip_metric_ GUARDED_BY(critical_section_rtcp_sender_);
edjee@google.com79b02892013-04-04 19:43:34 +0000269
danilchap162abd32015-12-10 02:39:40 -0800270 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
271 RtcpPacketTypeCounter packet_type_counter_
272 GUARDED_BY(critical_section_rtcp_sender_);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000273
danilchap162abd32015-12-10 02:39:40 -0800274 RTCPUtility::NackStats nack_stats_ GUARDED_BY(critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200275
danilchap162abd32015-12-10 02:39:40 -0800276 void SetFlag(RTCPPacketType type, bool is_volatile)
277 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
278 void SetFlags(const std::set<RTCPPacketType>& types, bool is_volatile)
279 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
280 bool IsFlagPresent(RTCPPacketType type) const
281 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
282 bool ConsumeFlag(RTCPPacketType type, bool forced = false)
283 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
284 bool AllVolatileFlagsConsumed() const
285 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
286 struct ReportFlag {
287 ReportFlag(RTCPPacketType type, bool is_volatile)
288 : type(type), is_volatile(is_volatile) {}
289 bool operator<(const ReportFlag& flag) const { return type < flag.type; }
290 bool operator==(const ReportFlag& flag) const { return type == flag.type; }
291 const RTCPPacketType type;
292 const bool is_volatile;
293 };
Erik Språng242e22b2015-05-11 10:17:43 +0200294
danilchap162abd32015-12-10 02:39:40 -0800295 std::set<ReportFlag> report_flags_ GUARDED_BY(critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200296
danilchap162abd32015-12-10 02:39:40 -0800297 typedef rtc::scoped_ptr<rtcp::RtcpPacket> (RTCPSender::*BuilderFunc)(
298 const RtcpContext&);
299 std::map<RTCPPacketType, BuilderFunc> builders_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000301} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
danilchap162abd32015-12-10 02:39:40 -0800303#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_