blob: 87a810715638b4ee6a8f505d8004b3cbd1d8791e [file] [log] [blame]
Tommi3a5742c2020-05-20 09:32:51 +02001/*
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 MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
12#define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
13
14#include <stddef.h>
15#include <stdint.h>
16
17#include <memory>
18#include <set>
19#include <string>
20#include <vector>
21
22#include "absl/types/optional.h"
23#include "api/rtp_headers.h"
24#include "api/video/video_bitrate_allocation.h"
25#include "modules/include/module_fec_types.h"
26#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Tommi3a5742c2020-05-20 09:32:51 +020027#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" // RTCPPacketType
28#include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
29#include "modules/rtp_rtcp/source/rtcp_receiver.h"
30#include "modules/rtp_rtcp/source/rtcp_sender.h"
31#include "modules/rtp_rtcp/source/rtp_packet_history.h"
32#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020033#include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
Tommi3a5742c2020-05-20 09:32:51 +020034#include "modules/rtp_rtcp/source/rtp_sender.h"
35#include "modules/rtp_rtcp/source/rtp_sender_egress.h"
36#include "rtc_base/critical_section.h"
37#include "rtc_base/gtest_prod_util.h"
38#include "rtc_base/synchronization/sequence_checker.h"
39
40namespace webrtc {
41
42class Clock;
43struct PacedPacketInfo;
44struct RTPVideoHeader;
45
46class ModuleRtpRtcpImpl2 final : public RtpRtcp,
47 public RTCPReceiver::ModuleRtpRtcp {
48 public:
49 explicit ModuleRtpRtcpImpl2(const RtpRtcp::Configuration& configuration);
50 ~ModuleRtpRtcpImpl2() override;
51
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020052 // This method is provided to easy with migrating away from the
53 // RtpRtcp::Create factory method. Since this is an internal implementation
54 // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
55 // be fine.
56 static std::unique_ptr<RtpRtcp> Create(const Configuration& configuration);
57
58 // TODO(tommi): Make implementation private?
59
Tommi3a5742c2020-05-20 09:32:51 +020060 // Returns the number of milliseconds until the module want a worker thread to
61 // call Process.
62 int64_t TimeUntilNextProcess() override;
63
64 // Process any pending tasks such as timeouts.
65 void Process() override;
66
67 // Receiver part.
68
69 // Called when we receive an RTCP packet.
70 void IncomingRtcpPacket(const uint8_t* incoming_packet,
71 size_t incoming_packet_length) override;
72
73 void SetRemoteSSRC(uint32_t ssrc) override;
74
75 // Sender part.
76 void RegisterSendPayloadFrequency(int payload_type,
77 int payload_frequency) override;
78
79 int32_t DeRegisterSendPayload(int8_t payload_type) override;
80
81 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
82
83 // Register RTP header extension.
84 int32_t RegisterSendRtpHeaderExtension(RTPExtensionType type,
85 uint8_t id) override;
86 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
87 int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
88 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
89
90 bool SupportsPadding() const override;
91 bool SupportsRtxPayloadPadding() const override;
92
93 // Get start timestamp.
94 uint32_t StartTimestamp() const override;
95
96 // Configure start timestamp, default is a random number.
97 void SetStartTimestamp(uint32_t timestamp) override;
98
99 uint16_t SequenceNumber() const override;
100
101 // Set SequenceNumber, default is a random number.
102 void SetSequenceNumber(uint16_t seq) override;
103
104 void SetRtpState(const RtpState& rtp_state) override;
105 void SetRtxState(const RtpState& rtp_state) override;
106 RtpState GetRtpState() const override;
107 RtpState GetRtxState() const override;
108
109 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
110
111 void SetRid(const std::string& rid) override;
112
113 void SetMid(const std::string& mid) override;
114
115 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
116
117 RTCPSender::FeedbackState GetFeedbackState();
118
119 void SetRtxSendStatus(int mode) override;
120 int RtxSendStatus() const override;
121 absl::optional<uint32_t> RtxSsrc() const override;
122
123 void SetRtxSendPayloadType(int payload_type,
124 int associated_payload_type) override;
125
126 absl::optional<uint32_t> FlexfecSsrc() const override;
127
128 // Sends kRtcpByeCode when going from true to false.
129 int32_t SetSendingStatus(bool sending) override;
130
131 bool Sending() const override;
132
133 // Drops or relays media packets.
134 void SetSendingMediaStatus(bool sending) override;
135
136 bool SendingMedia() const override;
137
138 bool IsAudioConfigured() const override;
139
140 void SetAsPartOfAllocation(bool part_of_allocation) override;
141
142 bool OnSendingRtpFrame(uint32_t timestamp,
143 int64_t capture_time_ms,
144 int payload_type,
145 bool force_sender_report) override;
146
147 bool TrySendPacket(RtpPacketToSend* packet,
148 const PacedPacketInfo& pacing_info) override;
149
150 void OnPacketsAcknowledged(
151 rtc::ArrayView<const uint16_t> sequence_numbers) override;
152
153 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
154 size_t target_size_bytes) override;
155
156 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
157 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
158
159 size_t ExpectedPerPacketOverhead() const override;
160
161 // RTCP part.
162
163 // Get RTCP status.
164 RtcpMode RTCP() const override;
165
166 // Configure RTCP status i.e on/off.
167 void SetRTCPStatus(RtcpMode method) override;
168
169 // Set RTCP CName.
170 int32_t SetCNAME(const char* c_name) override;
171
172 // Get remote CName.
173 int32_t RemoteCNAME(uint32_t remote_ssrc,
174 char c_name[RTCP_CNAME_SIZE]) const override;
175
176 // Get remote NTP.
177 int32_t RemoteNTP(uint32_t* received_ntp_secs,
178 uint32_t* received_ntp_frac,
179 uint32_t* rtcp_arrival_time_secs,
180 uint32_t* rtcp_arrival_time_frac,
181 uint32_t* rtcp_timestamp) const override;
182
183 int32_t AddMixedCNAME(uint32_t ssrc, const char* c_name) override;
184
185 int32_t RemoveMixedCNAME(uint32_t ssrc) override;
186
187 // Get RoundTripTime.
188 int32_t RTT(uint32_t remote_ssrc,
189 int64_t* rtt,
190 int64_t* avg_rtt,
191 int64_t* min_rtt,
192 int64_t* max_rtt) const override;
193
194 int64_t ExpectedRetransmissionTimeMs() const override;
195
196 // Force a send of an RTCP packet.
197 // Normal SR and RR are triggered via the process function.
198 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
199
200 // Statistics of the amount of data sent and received.
201 int32_t DataCountersRTP(size_t* bytes_sent,
202 uint32_t* packets_sent) const override;
203
204 void GetSendStreamDataCounters(
205 StreamDataCounters* rtp_counters,
206 StreamDataCounters* rtx_counters) const override;
207
208 // Get received RTCP report, report block.
209 int32_t RemoteRTCPStat(
210 std::vector<RTCPReportBlock>* receive_blocks) const override;
211 // A snapshot of the most recent Report Block with additional data of
212 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
213 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
214 // which is the SSRC of the corresponding outbound RTP stream, is unique.
215 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
216
217 // (REMB) Receiver Estimated Max Bitrate.
218 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
219 void UnsetRemb() override;
220
221 // (TMMBR) Temporary Max Media Bit Rate.
222 bool TMMBR() const override;
223
224 void SetTMMBRStatus(bool enable) override;
225
226 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
227
228 size_t MaxRtpPacketSize() const override;
229
230 void SetMaxRtpPacketSize(size_t max_packet_size) override;
231
232 // (NACK) Negative acknowledgment part.
233
234 // Send a Negative acknowledgment packet.
235 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
236 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
237
238 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
239
240 // Store the sent packets, needed to answer to a negative acknowledgment
241 // requests.
242 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
243
244 bool StorePackets() const override;
245
246 void SendCombinedRtcpPacket(
247 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
248
249 // (APP) Application specific data.
250 int32_t SetRTCPApplicationSpecificData(uint8_t sub_type,
251 uint32_t name,
252 const uint8_t* data,
253 uint16_t length) override;
254
255 // (XR) Receiver reference time report.
256 void SetRtcpXrRrtrStatus(bool enable) override;
257
258 bool RtcpXrRrtrStatus() const override;
259
260 // Video part.
261 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
262 uint16_t last_received_seq_num,
263 bool decodability_flag,
264 bool buffering_allowed) override;
265
266 bool LastReceivedNTP(uint32_t* NTPsecs,
267 uint32_t* NTPfrac,
268 uint32_t* remote_sr) const;
269
270 void BitrateSent(uint32_t* total_rate,
271 uint32_t* video_rate,
272 uint32_t* fec_rate,
273 uint32_t* nackRate) const override;
274
275 RtpSendRates GetSendRates() const override;
276
277 void OnReceivedNack(
278 const std::vector<uint16_t>& nack_sequence_numbers) override;
279 void OnReceivedRtcpReportBlocks(
280 const ReportBlockList& report_blocks) override;
281 void OnRequestSendReport() override;
282
283 void SetVideoBitrateAllocation(
284 const VideoBitrateAllocation& bitrate) override;
285
286 RTPSender* RtpSender() override;
287 const RTPSender* RtpSender() const override;
288
289 protected:
290 bool UpdateRTCPReceiveInformationTimers();
291
292 RTPSender* rtp_sender() {
293 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
294 }
295 const RTPSender* rtp_sender() const {
296 return rtp_sender_ ? &rtp_sender_->packet_generator : nullptr;
297 }
298
299 RTCPSender* rtcp_sender() { return &rtcp_sender_; }
300 const RTCPSender* rtcp_sender() const { return &rtcp_sender_; }
301
302 RTCPReceiver* rtcp_receiver() { return &rtcp_receiver_; }
303 const RTCPReceiver* rtcp_receiver() const { return &rtcp_receiver_; }
304
305 Clock* clock() const { return clock_; }
306
307 // TODO(sprang): Remove when usage is gone.
308 DataRate SendRate() const;
309 DataRate NackOverheadRate() const;
310
311 private:
312 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
313 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
314
315 struct RtpSenderContext {
316 explicit RtpSenderContext(const RtpRtcp::Configuration& config);
317 // Storage of packets, for retransmissions and padding, if applicable.
318 RtpPacketHistory packet_history;
319 // Handles final time timestamping/stats/etc and handover to Transport.
320 RtpSenderEgress packet_sender;
321 // If no paced sender configured, this class will be used to pass packets
322 // from |packet_generator_| to |packet_sender_|.
323 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
324 // Handles creation of RTP packets to be sent.
325 RTPSender packet_generator;
326 };
327
328 void set_rtt_ms(int64_t rtt_ms);
329 int64_t rtt_ms() const;
330
331 bool TimeToSendFullNackList(int64_t now) const;
332
333 SequenceChecker construction_thread_checker_;
334 SequenceChecker process_thread_checker_;
335
336 std::unique_ptr<RtpSenderContext> rtp_sender_;
337
338 RTCPSender rtcp_sender_;
339 RTCPReceiver rtcp_receiver_;
340
341 Clock* const clock_;
342
343 int64_t last_bitrate_process_time_;
344 int64_t last_rtt_process_time_;
345 int64_t next_process_time_;
346 uint16_t packet_overhead_;
347
348 // Send side
349 int64_t nack_last_time_sent_full_ms_;
350 uint16_t nack_last_seq_number_sent_;
351
352 RemoteBitrateEstimator* const remote_bitrate_;
353
354 RtcpRttStats* const rtt_stats_;
355
356 // The processed RTT from RtcpRttStats.
357 rtc::CriticalSection critical_section_rtt_;
358 int64_t rtt_ms_;
359};
360
361} // namespace webrtc
362
363#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_