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