blob: 276f88a6b57bb6c04d40d14c95c6dbb357a44040 [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
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020046class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
47 public Module,
Tommi3a5742c2020-05-20 09:32:51 +020048 public RTCPReceiver::ModuleRtpRtcp {
49 public:
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020050 explicit ModuleRtpRtcpImpl2(
51 const RtpRtcpInterface::Configuration& configuration);
Tommi3a5742c2020-05-20 09:32:51 +020052 ~ModuleRtpRtcpImpl2() override;
53
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020054 // This method is provided to easy with migrating away from the
55 // RtpRtcp::Create factory method. Since this is an internal implementation
56 // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
57 // be fine.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020058 static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
59 const Configuration& configuration);
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020060
Tommi3a5742c2020-05-20 09:32:51 +020061 // Returns the number of milliseconds until the module want a worker thread to
62 // call Process.
63 int64_t TimeUntilNextProcess() override;
64
65 // Process any pending tasks such as timeouts.
66 void Process() override;
67
68 // Receiver part.
69
70 // Called when we receive an RTCP packet.
71 void IncomingRtcpPacket(const uint8_t* incoming_packet,
72 size_t incoming_packet_length) override;
73
74 void SetRemoteSSRC(uint32_t ssrc) override;
75
76 // Sender part.
77 void RegisterSendPayloadFrequency(int payload_type,
78 int payload_frequency) override;
79
80 int32_t DeRegisterSendPayload(int8_t payload_type) override;
81
82 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
83
Tommi3a5742c2020-05-20 09:32:51 +020084 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
85 int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
86 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
87
88 bool SupportsPadding() const override;
89 bool SupportsRtxPayloadPadding() const override;
90
91 // Get start timestamp.
92 uint32_t StartTimestamp() const override;
93
94 // Configure start timestamp, default is a random number.
95 void SetStartTimestamp(uint32_t timestamp) override;
96
97 uint16_t SequenceNumber() const override;
98
99 // Set SequenceNumber, default is a random number.
100 void SetSequenceNumber(uint16_t seq) override;
101
102 void SetRtpState(const RtpState& rtp_state) override;
103 void SetRtxState(const RtpState& rtp_state) override;
104 RtpState GetRtpState() const override;
105 RtpState GetRtxState() const override;
106
107 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
108
109 void SetRid(const std::string& rid) override;
110
111 void SetMid(const std::string& mid) override;
112
113 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
114
115 RTCPSender::FeedbackState GetFeedbackState();
116
117 void SetRtxSendStatus(int mode) override;
118 int RtxSendStatus() const override;
119 absl::optional<uint32_t> RtxSsrc() const override;
120
121 void SetRtxSendPayloadType(int payload_type,
122 int associated_payload_type) override;
123
124 absl::optional<uint32_t> FlexfecSsrc() const override;
125
126 // Sends kRtcpByeCode when going from true to false.
127 int32_t SetSendingStatus(bool sending) override;
128
129 bool Sending() const override;
130
131 // Drops or relays media packets.
132 void SetSendingMediaStatus(bool sending) override;
133
134 bool SendingMedia() const override;
135
136 bool IsAudioConfigured() const override;
137
138 void SetAsPartOfAllocation(bool part_of_allocation) override;
139
140 bool OnSendingRtpFrame(uint32_t timestamp,
141 int64_t capture_time_ms,
142 int payload_type,
143 bool force_sender_report) override;
144
145 bool TrySendPacket(RtpPacketToSend* packet,
146 const PacedPacketInfo& pacing_info) override;
147
148 void OnPacketsAcknowledged(
149 rtc::ArrayView<const uint16_t> sequence_numbers) override;
150
151 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
152 size_t target_size_bytes) override;
153
154 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
155 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
156
157 size_t ExpectedPerPacketOverhead() const override;
158
159 // RTCP part.
160
161 // Get RTCP status.
162 RtcpMode RTCP() const override;
163
164 // Configure RTCP status i.e on/off.
165 void SetRTCPStatus(RtcpMode method) override;
166
167 // Set RTCP CName.
168 int32_t SetCNAME(const char* c_name) override;
169
Tommi3a5742c2020-05-20 09:32:51 +0200170 // Get remote NTP.
171 int32_t RemoteNTP(uint32_t* received_ntp_secs,
172 uint32_t* received_ntp_frac,
173 uint32_t* rtcp_arrival_time_secs,
174 uint32_t* rtcp_arrival_time_frac,
175 uint32_t* rtcp_timestamp) const override;
176
Tommi3a5742c2020-05-20 09:32:51 +0200177 // Get RoundTripTime.
178 int32_t RTT(uint32_t remote_ssrc,
179 int64_t* rtt,
180 int64_t* avg_rtt,
181 int64_t* min_rtt,
182 int64_t* max_rtt) const override;
183
184 int64_t ExpectedRetransmissionTimeMs() const override;
185
186 // Force a send of an RTCP packet.
187 // Normal SR and RR are triggered via the process function.
188 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
189
Tommi3a5742c2020-05-20 09:32:51 +0200190 void GetSendStreamDataCounters(
191 StreamDataCounters* rtp_counters,
192 StreamDataCounters* rtx_counters) const override;
193
194 // Get received RTCP report, report block.
195 int32_t RemoteRTCPStat(
196 std::vector<RTCPReportBlock>* receive_blocks) const override;
197 // A snapshot of the most recent Report Block with additional data of
198 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
199 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
200 // which is the SSRC of the corresponding outbound RTP stream, is unique.
201 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
202
203 // (REMB) Receiver Estimated Max Bitrate.
204 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
205 void UnsetRemb() override;
206
Tommi3a5742c2020-05-20 09:32:51 +0200207 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
208
209 size_t MaxRtpPacketSize() const override;
210
211 void SetMaxRtpPacketSize(size_t max_packet_size) override;
212
213 // (NACK) Negative acknowledgment part.
214
215 // Send a Negative acknowledgment packet.
216 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
217 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
218
219 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
220
221 // Store the sent packets, needed to answer to a negative acknowledgment
222 // requests.
223 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
224
225 bool StorePackets() const override;
226
227 void SendCombinedRtcpPacket(
228 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
229
Tommi3a5742c2020-05-20 09:32:51 +0200230 // (XR) Receiver reference time report.
231 void SetRtcpXrRrtrStatus(bool enable) override;
232
233 bool RtcpXrRrtrStatus() const override;
234
235 // Video part.
236 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
237 uint16_t last_received_seq_num,
238 bool decodability_flag,
239 bool buffering_allowed) override;
240
241 bool LastReceivedNTP(uint32_t* NTPsecs,
242 uint32_t* NTPfrac,
243 uint32_t* remote_sr) const;
244
245 void BitrateSent(uint32_t* total_rate,
246 uint32_t* video_rate,
247 uint32_t* fec_rate,
248 uint32_t* nackRate) const override;
249
250 RtpSendRates GetSendRates() const override;
251
252 void OnReceivedNack(
253 const std::vector<uint16_t>& nack_sequence_numbers) override;
254 void OnReceivedRtcpReportBlocks(
255 const ReportBlockList& report_blocks) override;
256 void OnRequestSendReport() override;
257
258 void SetVideoBitrateAllocation(
259 const VideoBitrateAllocation& bitrate) override;
260
261 RTPSender* RtpSender() override;
262 const RTPSender* RtpSender() const override;
263
Tommi3a5742c2020-05-20 09:32:51 +0200264 private:
265 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
266 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
267
Erik Språng1b485322020-06-24 18:39:25 +0000268 struct RtpSenderContext {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200269 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Tommi3a5742c2020-05-20 09:32:51 +0200270 // Storage of packets, for retransmissions and padding, if applicable.
271 RtpPacketHistory packet_history;
272 // Handles final time timestamping/stats/etc and handover to Transport.
273 RtpSenderEgress packet_sender;
274 // If no paced sender configured, this class will be used to pass packets
275 // from |packet_generator_| to |packet_sender_|.
276 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
277 // Handles creation of RTP packets to be sent.
278 RTPSender packet_generator;
279 };
280
281 void set_rtt_ms(int64_t rtt_ms);
282 int64_t rtt_ms() const;
283
284 bool TimeToSendFullNackList(int64_t now) const;
285
286 SequenceChecker construction_thread_checker_;
287 SequenceChecker process_thread_checker_;
288
289 std::unique_ptr<RtpSenderContext> rtp_sender_;
290
291 RTCPSender rtcp_sender_;
292 RTCPReceiver rtcp_receiver_;
293
294 Clock* const clock_;
295
296 int64_t last_bitrate_process_time_;
297 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_