Line data Source code
1 : /*
2 : * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
12 : #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
13 :
14 : #include <map>
15 : #include <memory>
16 : #include <set>
17 : #include <sstream>
18 : #include <string>
19 : #include <vector>
20 :
21 : #include "webrtc/api/call/transport.h"
22 : #include "webrtc/base/constructormagic.h"
23 : #include "webrtc/base/criticalsection.h"
24 : #include "webrtc/base/optional.h"
25 : #include "webrtc/base/random.h"
26 : #include "webrtc/base/thread_annotations.h"
27 : #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
28 : #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
29 : #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
30 : #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
31 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
32 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
33 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/report_block.h"
34 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
35 : #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
36 : #include "webrtc/typedefs.h"
37 :
38 : namespace webrtc {
39 :
40 : class ModuleRtpRtcpImpl;
41 : class RtcEventLog;
42 :
43 : class NACKStringBuilder {
44 : public:
45 : NACKStringBuilder();
46 : ~NACKStringBuilder();
47 :
48 : void PushNACK(uint16_t nack);
49 : std::string GetResult();
50 :
51 : private:
52 : std::ostringstream stream_;
53 : int count_;
54 : uint16_t prevNack_;
55 : bool consecutive_;
56 : };
57 :
58 : class RTCPSender {
59 : public:
60 : struct FeedbackState {
61 : FeedbackState();
62 :
63 : uint8_t send_payload_type;
64 : uint32_t packets_sent;
65 : size_t media_bytes_sent;
66 : uint32_t send_bitrate;
67 :
68 : uint32_t last_rr_ntp_secs;
69 : uint32_t last_rr_ntp_frac;
70 : uint32_t remote_sr;
71 :
72 : bool has_last_xr_rr;
73 : rtcp::ReceiveTimeInfo last_xr_rr;
74 :
75 : // Used when generating TMMBR.
76 : ModuleRtpRtcpImpl* module;
77 : };
78 :
79 : RTCPSender(bool audio,
80 : Clock* clock,
81 : ReceiveStatistics* receive_statistics,
82 : RtcpPacketTypeCounterObserver* packet_type_counter_observer,
83 : RtcEventLog* event_log,
84 : Transport* outgoing_transport);
85 : virtual ~RTCPSender();
86 :
87 : RtcpMode Status() const;
88 : void SetRTCPStatus(RtcpMode method);
89 :
90 : bool Sending() const;
91 : int32_t SetSendingStatus(const FeedbackState& feedback_state,
92 : bool enabled); // combine the functions
93 :
94 : int32_t SetNackStatus(bool enable);
95 :
96 : void SetTimestampOffset(uint32_t timestamp_offset);
97 :
98 : void SetLastRtpTime(uint32_t rtp_timestamp, int64_t capture_time_ms);
99 :
100 : void SetSSRC(uint32_t ssrc);
101 :
102 : void SetRemoteSSRC(uint32_t ssrc);
103 :
104 : int32_t SetCNAME(const char* cName);
105 :
106 : int32_t AddMixedCNAME(uint32_t SSRC, const char* c_name);
107 :
108 : int32_t RemoveMixedCNAME(uint32_t SSRC);
109 :
110 : bool GetSendReportMetadata(const uint32_t sendReport,
111 : uint64_t *timeOfSend,
112 : uint32_t *packetCount,
113 : uint64_t *octetCount);
114 :
115 : bool TimeToSendRTCPReport(bool sendKeyframeBeforeRTP = false) const;
116 :
117 : int32_t SendRTCP(const FeedbackState& feedback_state,
118 : RTCPPacketType packetType,
119 : int32_t nackSize = 0,
120 : const uint16_t* nackList = 0,
121 : bool repeat = false,
122 : uint64_t pictureID = 0);
123 :
124 : int32_t SendCompoundRTCP(const FeedbackState& feedback_state,
125 : const std::set<RTCPPacketType>& packetTypes,
126 : int32_t nackSize = 0,
127 : const uint16_t* nackList = 0,
128 : bool repeat = false,
129 : uint64_t pictureID = 0);
130 :
131 : bool REMB() const;
132 :
133 : void SetREMBStatus(bool enable);
134 :
135 : void SetREMBData(uint32_t bitrate, const std::vector<uint32_t>& ssrcs);
136 :
137 : bool TMMBR() const;
138 :
139 : void SetTMMBRStatus(bool enable);
140 :
141 : void SetMaxRtpPacketSize(size_t max_packet_size);
142 :
143 : void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set);
144 :
145 : int32_t SetApplicationSpecificData(uint8_t subType,
146 : uint32_t name,
147 : const uint8_t* data,
148 : uint16_t length);
149 : int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
150 :
151 : void SendRtcpXrReceiverReferenceTime(bool enable);
152 :
153 : bool RtcpXrReceiverReferenceTime() const;
154 :
155 : void SetCsrcs(const std::vector<uint32_t>& csrcs);
156 :
157 : void SetTargetBitrate(unsigned int target_bitrate);
158 : void SetVideoBitrateAllocation(const BitrateAllocation& bitrate);
159 : bool SendFeedbackPacket(const rtcp::TransportFeedback& packet);
160 :
161 : private:
162 : class RtcpContext;
163 :
164 : // Determine which RTCP messages should be sent and setup flags.
165 : void PrepareReport(const FeedbackState& feedback_state)
166 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
167 :
168 : bool AddReportBlock(const FeedbackState& feedback_state,
169 : uint32_t ssrc,
170 : StreamStatistician* statistician)
171 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
172 :
173 : std::unique_ptr<rtcp::RtcpPacket> BuildSR(const RtcpContext& context)
174 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
175 : std::unique_ptr<rtcp::RtcpPacket> BuildRR(const RtcpContext& context)
176 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
177 : std::unique_ptr<rtcp::RtcpPacket> BuildSDES(const RtcpContext& context)
178 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
179 : std::unique_ptr<rtcp::RtcpPacket> BuildPLI(const RtcpContext& context)
180 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
181 : std::unique_ptr<rtcp::RtcpPacket> BuildREMB(const RtcpContext& context)
182 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
183 : std::unique_ptr<rtcp::RtcpPacket> BuildTMMBR(const RtcpContext& context)
184 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
185 : std::unique_ptr<rtcp::RtcpPacket> BuildTMMBN(const RtcpContext& context)
186 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
187 : std::unique_ptr<rtcp::RtcpPacket> BuildAPP(const RtcpContext& context)
188 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
189 : std::unique_ptr<rtcp::RtcpPacket> BuildExtendedReports(
190 : const RtcpContext& context)
191 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
192 : std::unique_ptr<rtcp::RtcpPacket> BuildBYE(const RtcpContext& context)
193 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
194 : std::unique_ptr<rtcp::RtcpPacket> BuildFIR(const RtcpContext& context)
195 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
196 : std::unique_ptr<rtcp::RtcpPacket> BuildSLI(const RtcpContext& context)
197 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
198 : std::unique_ptr<rtcp::RtcpPacket> BuildRPSI(const RtcpContext& context)
199 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
200 : std::unique_ptr<rtcp::RtcpPacket> BuildNACK(const RtcpContext& context)
201 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
202 :
203 : private:
204 : const bool audio_;
205 : Clock* const clock_;
206 : Random random_ GUARDED_BY(critical_section_rtcp_sender_);
207 : RtcpMode method_ GUARDED_BY(critical_section_rtcp_sender_);
208 :
209 : RtcEventLog* const event_log_;
210 : Transport* const transport_;
211 :
212 : rtc::CriticalSection critical_section_rtcp_sender_;
213 : bool using_nack_ GUARDED_BY(critical_section_rtcp_sender_);
214 : bool sending_ GUARDED_BY(critical_section_rtcp_sender_);
215 : bool remb_enabled_ GUARDED_BY(critical_section_rtcp_sender_);
216 :
217 : int64_t next_time_to_send_rtcp_ GUARDED_BY(critical_section_rtcp_sender_);
218 :
219 : uint32_t timestamp_offset_ GUARDED_BY(critical_section_rtcp_sender_);
220 : uint32_t last_rtp_timestamp_ GUARDED_BY(critical_section_rtcp_sender_);
221 : int64_t last_frame_capture_time_ms_ GUARDED_BY(critical_section_rtcp_sender_);
222 : uint32_t ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
223 : // SSRC that we receive on our RTP channel
224 : uint32_t remote_ssrc_ GUARDED_BY(critical_section_rtcp_sender_);
225 : std::string cname_ GUARDED_BY(critical_section_rtcp_sender_);
226 :
227 : ReceiveStatistics* receive_statistics_
228 : GUARDED_BY(critical_section_rtcp_sender_);
229 : std::map<uint32_t, rtcp::ReportBlock> report_blocks_
230 : GUARDED_BY(critical_section_rtcp_sender_);
231 : std::map<uint32_t, std::string> csrc_cnames_
232 : GUARDED_BY(critical_section_rtcp_sender_);
233 :
234 : // Sent
235 : uint32_t last_send_report_[RTCP_NUMBER_OF_SR] GUARDED_BY(
236 : critical_section_rtcp_sender_); // allow packet loss and RTT above 1 sec
237 : int64_t last_rtcp_time_[RTCP_NUMBER_OF_SR] GUARDED_BY(
238 : critical_section_rtcp_sender_);
239 : uint32_t lastSRPacketCount_[RTCP_NUMBER_OF_SR] GUARDED_BY(
240 : critical_section_rtcp_sender_);
241 : uint64_t lastSROctetCount_[RTCP_NUMBER_OF_SR] GUARDED_BY(
242 : critical_section_rtcp_sender_);
243 :
244 : // send CSRCs
245 : std::vector<uint32_t> csrcs_ GUARDED_BY(critical_section_rtcp_sender_);
246 :
247 : // Full intra request
248 : uint8_t sequence_number_fir_ GUARDED_BY(critical_section_rtcp_sender_);
249 :
250 : // REMB
251 : uint32_t remb_bitrate_ GUARDED_BY(critical_section_rtcp_sender_);
252 : std::vector<uint32_t> remb_ssrcs_ GUARDED_BY(critical_section_rtcp_sender_);
253 :
254 : std::vector<rtcp::TmmbItem> tmmbn_to_send_
255 : GUARDED_BY(critical_section_rtcp_sender_);
256 : uint32_t tmmbr_send_bps_ GUARDED_BY(critical_section_rtcp_sender_);
257 : uint32_t packet_oh_send_ GUARDED_BY(critical_section_rtcp_sender_);
258 : size_t max_packet_size_;
259 :
260 : // APP
261 : uint8_t app_sub_type_ GUARDED_BY(critical_section_rtcp_sender_);
262 : uint32_t app_name_ GUARDED_BY(critical_section_rtcp_sender_);
263 : std::unique_ptr<uint8_t[]> app_data_
264 : GUARDED_BY(critical_section_rtcp_sender_);
265 : uint16_t app_length_ GUARDED_BY(critical_section_rtcp_sender_);
266 :
267 : // True if sending of XR Receiver reference time report is enabled.
268 : bool xr_send_receiver_reference_time_enabled_
269 : GUARDED_BY(critical_section_rtcp_sender_);
270 :
271 : // XR VoIP metric
272 : rtc::Optional<RTCPVoIPMetric> xr_voip_metric_
273 : GUARDED_BY(critical_section_rtcp_sender_);
274 :
275 : RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
276 : RtcpPacketTypeCounter packet_type_counter_
277 : GUARDED_BY(critical_section_rtcp_sender_);
278 :
279 : RTCPUtility::NackStats nack_stats_ GUARDED_BY(critical_section_rtcp_sender_);
280 :
281 : rtc::Optional<BitrateAllocation> video_bitrate_allocation_
282 : GUARDED_BY(critical_section_rtcp_sender_);
283 :
284 : void SetFlag(uint32_t type, bool is_volatile)
285 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
286 : void SetFlags(const std::set<RTCPPacketType>& types, bool is_volatile)
287 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
288 : bool IsFlagPresent(uint32_t type) const
289 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
290 : bool ConsumeFlag(uint32_t type, bool forced = false)
291 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
292 : bool AllVolatileFlagsConsumed() const
293 : EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
294 : struct ReportFlag {
295 0 : ReportFlag(uint32_t type, bool is_volatile)
296 0 : : type(type), is_volatile(is_volatile) {}
297 0 : bool operator<(const ReportFlag& flag) const { return type < flag.type; }
298 : bool operator==(const ReportFlag& flag) const { return type == flag.type; }
299 : const uint32_t type;
300 : const bool is_volatile;
301 : };
302 :
303 : std::set<ReportFlag> report_flags_ GUARDED_BY(critical_section_rtcp_sender_);
304 :
305 : typedef std::unique_ptr<rtcp::RtcpPacket> (RTCPSender::*BuilderFunc)(
306 : const RtcpContext&);
307 : // Map from RTCPPacketType to builder.
308 : std::map<uint32_t, BuilderFunc> builders_;
309 :
310 : RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTCPSender);
311 : };
312 : } // namespace webrtc
313 :
314 : #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
|