blob: 849cc42c5e5e9fad4f29e921cd62ae00abc079e4 [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"
Artem Titovd15a5752021-02-10 14:31:24 +010024#include "api/sequence_checker.h"
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +020025#include "api/task_queue/task_queue_base.h"
Markus Handell885d5382021-06-21 18:57:36 +020026#include "api/units/time_delta.h"
Tommi3a5742c2020-05-20 09:32:51 +020027#include "api/video/video_bitrate_allocation.h"
28#include "modules/include/module_fec_types.h"
29#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Tommi3a5742c2020-05-20 09:32:51 +020030#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" // RTCPPacketType
31#include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
32#include "modules/rtp_rtcp/source/rtcp_receiver.h"
33#include "modules/rtp_rtcp/source/rtcp_sender.h"
34#include "modules/rtp_rtcp/source/rtp_packet_history.h"
35#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
36#include "modules/rtp_rtcp/source/rtp_sender.h"
37#include "modules/rtp_rtcp/source/rtp_sender_egress.h"
Tommi3a5742c2020-05-20 09:32:51 +020038#include "rtc_base/gtest_prod_util.h"
Markus Handellf7303e62020-07-09 01:34:42 +020039#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010040#include "rtc_base/system/no_unique_address.h"
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020041#include "rtc_base/task_utils/pending_task_safety_flag.h"
42#include "rtc_base/task_utils/repeating_task.h"
Markus Handell885d5382021-06-21 18:57:36 +020043#include "rtc_base/task_utils/to_queued_task.h"
44#include "rtc_base/thread_annotations.h"
Tommi3a5742c2020-05-20 09:32:51 +020045
46namespace webrtc {
47
48class Clock;
49struct PacedPacketInfo;
50struct RTPVideoHeader;
51
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020052class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
53 public Module,
Tommi3a5742c2020-05-20 09:32:51 +020054 public RTCPReceiver::ModuleRtpRtcp {
55 public:
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020056 explicit ModuleRtpRtcpImpl2(
57 const RtpRtcpInterface::Configuration& configuration);
Tommi3a5742c2020-05-20 09:32:51 +020058 ~ModuleRtpRtcpImpl2() override;
59
Niels Moller2accc7d2021-01-12 15:54:16 +000060 // This method is provided to easy with migrating away from the
61 // RtpRtcp::Create factory method. Since this is an internal implementation
62 // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
63 // be fine.
64 static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
65 const Configuration& configuration);
66
Tommi3a5742c2020-05-20 09:32:51 +020067 // Returns the number of milliseconds until the module want a worker thread to
68 // call Process.
69 int64_t TimeUntilNextProcess() override;
70
71 // Process any pending tasks such as timeouts.
72 void Process() override;
73
74 // Receiver part.
75
76 // Called when we receive an RTCP packet.
77 void IncomingRtcpPacket(const uint8_t* incoming_packet,
78 size_t incoming_packet_length) override;
79
80 void SetRemoteSSRC(uint32_t ssrc) override;
81
Tommi08be9ba2021-06-15 23:01:57 +020082 void SetLocalSsrc(uint32_t local_ssrc) override;
83
Tommi3a5742c2020-05-20 09:32:51 +020084 // Sender part.
85 void RegisterSendPayloadFrequency(int payload_type,
86 int payload_frequency) override;
87
88 int32_t DeRegisterSendPayload(int8_t payload_type) override;
89
90 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
91
Tommi3a5742c2020-05-20 09:32:51 +020092 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
93 int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
94 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
95
96 bool SupportsPadding() const override;
97 bool SupportsRtxPayloadPadding() const override;
98
99 // Get start timestamp.
100 uint32_t StartTimestamp() const override;
101
102 // Configure start timestamp, default is a random number.
103 void SetStartTimestamp(uint32_t timestamp) override;
104
105 uint16_t SequenceNumber() const override;
106
107 // Set SequenceNumber, default is a random number.
108 void SetSequenceNumber(uint16_t seq) override;
109
110 void SetRtpState(const RtpState& rtp_state) override;
111 void SetRtxState(const RtpState& rtp_state) override;
112 RtpState GetRtpState() const override;
113 RtpState GetRtxState() const override;
114
115 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
116
Tommi08be9ba2021-06-15 23:01:57 +0200117 // Semantically identical to `SSRC()` but must be called on the packet
118 // delivery thread/tq and returns the ssrc that maps to
119 // RtpRtcpInterface::Configuration::local_media_ssrc.
120 uint32_t local_media_ssrc() const;
121
Tommi3a5742c2020-05-20 09:32:51 +0200122 void SetRid(const std::string& rid) override;
123
124 void SetMid(const std::string& mid) override;
125
126 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
127
128 RTCPSender::FeedbackState GetFeedbackState();
129
130 void SetRtxSendStatus(int mode) override;
131 int RtxSendStatus() const override;
132 absl::optional<uint32_t> RtxSsrc() const override;
133
134 void SetRtxSendPayloadType(int payload_type,
135 int associated_payload_type) override;
136
137 absl::optional<uint32_t> FlexfecSsrc() const override;
138
139 // Sends kRtcpByeCode when going from true to false.
140 int32_t SetSendingStatus(bool sending) override;
141
142 bool Sending() const override;
143
144 // Drops or relays media packets.
145 void SetSendingMediaStatus(bool sending) override;
146
147 bool SendingMedia() const override;
148
149 bool IsAudioConfigured() const override;
150
151 void SetAsPartOfAllocation(bool part_of_allocation) override;
152
153 bool OnSendingRtpFrame(uint32_t timestamp,
154 int64_t capture_time_ms,
155 int payload_type,
156 bool force_sender_report) override;
157
158 bool TrySendPacket(RtpPacketToSend* packet,
159 const PacedPacketInfo& pacing_info) override;
160
Erik Språng1d50cb62020-07-02 17:41:32 +0200161 void SetFecProtectionParams(const FecProtectionParams& delta_params,
162 const FecProtectionParams& key_params) override;
163
164 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
165
Tommi3a5742c2020-05-20 09:32:51 +0200166 void OnPacketsAcknowledged(
167 rtc::ArrayView<const uint16_t> sequence_numbers) override;
168
169 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
170 size_t target_size_bytes) override;
171
172 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
173 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
174
175 size_t ExpectedPerPacketOverhead() const override;
176
177 // RTCP part.
178
179 // Get RTCP status.
180 RtcpMode RTCP() const override;
181
182 // Configure RTCP status i.e on/off.
183 void SetRTCPStatus(RtcpMode method) override;
184
185 // Set RTCP CName.
186 int32_t SetCNAME(const char* c_name) override;
187
Tommi3a5742c2020-05-20 09:32:51 +0200188 // Get remote NTP.
189 int32_t RemoteNTP(uint32_t* received_ntp_secs,
190 uint32_t* received_ntp_frac,
191 uint32_t* rtcp_arrival_time_secs,
192 uint32_t* rtcp_arrival_time_frac,
193 uint32_t* rtcp_timestamp) const override;
194
Tommi3a5742c2020-05-20 09:32:51 +0200195 // Get RoundTripTime.
196 int32_t RTT(uint32_t remote_ssrc,
197 int64_t* rtt,
198 int64_t* avg_rtt,
199 int64_t* min_rtt,
200 int64_t* max_rtt) const override;
201
202 int64_t ExpectedRetransmissionTimeMs() const override;
203
204 // Force a send of an RTCP packet.
Markus Handell885d5382021-06-21 18:57:36 +0200205 // Normal SR and RR are triggered via the task queue that's current when this
206 // object is created.
Tommi3a5742c2020-05-20 09:32:51 +0200207 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
208
Tommi3a5742c2020-05-20 09:32:51 +0200209 void GetSendStreamDataCounters(
210 StreamDataCounters* rtp_counters,
211 StreamDataCounters* rtx_counters) const override;
212
Tommi3a5742c2020-05-20 09:32:51 +0200213 // A snapshot of the most recent Report Block with additional data of
214 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
215 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
216 // which is the SSRC of the corresponding outbound RTP stream, is unique.
217 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100218 absl::optional<SenderReportStats> GetSenderReportStats() const override;
Tommi3a5742c2020-05-20 09:32:51 +0200219
220 // (REMB) Receiver Estimated Max Bitrate.
221 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
222 void UnsetRemb() override;
223
Tommi3a5742c2020-05-20 09:32:51 +0200224 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
225
226 size_t MaxRtpPacketSize() const override;
227
228 void SetMaxRtpPacketSize(size_t max_packet_size) override;
229
230 // (NACK) Negative acknowledgment part.
231
232 // Send a Negative acknowledgment packet.
233 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
234 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
235
236 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
237
238 // Store the sent packets, needed to answer to a negative acknowledgment
239 // requests.
240 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
241
Tommi3a5742c2020-05-20 09:32:51 +0200242 void SendCombinedRtcpPacket(
243 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
244
Tommi3a5742c2020-05-20 09:32:51 +0200245 // Video part.
246 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
247 uint16_t last_received_seq_num,
248 bool decodability_flag,
249 bool buffering_allowed) override;
250
Tommi3a5742c2020-05-20 09:32:51 +0200251 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ång1d50cb62020-07-02 17:41:32 +0200269 struct RtpSenderContext : public SequenceNumberAssigner {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200270 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Erik Språng1d50cb62020-07-02 17:41:32 +0200271 void AssignSequenceNumber(RtpPacketToSend* packet) override;
Tommi3a5742c2020-05-20 09:32:51 +0200272 // Storage of packets, for retransmissions and padding, if applicable.
273 RtpPacketHistory packet_history;
274 // Handles final time timestamping/stats/etc and handover to Transport.
275 RtpSenderEgress packet_sender;
276 // If no paced sender configured, this class will be used to pass packets
277 // from |packet_generator_| to |packet_sender_|.
278 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
279 // Handles creation of RTP packets to be sent.
280 RTPSender packet_generator;
281 };
282
283 void set_rtt_ms(int64_t rtt_ms);
284 int64_t rtt_ms() const;
285
286 bool TimeToSendFullNackList(int64_t now) const;
287
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200288 // Called on a timer, once a second, on the worker_queue_, to update the RTT,
289 // check if we need to send RTCP report, send TMMBR updates and fire events.
290 void PeriodicUpdate();
291
Niels Mölleraf6ea0c2020-11-20 12:21:21 +0100292 // Returns true if the module is configured to store packets.
293 bool StorePackets() const;
294
Markus Handell885d5382021-06-21 18:57:36 +0200295 // Used from RtcpSenderMediator to maybe send rtcp.
296 void MaybeSendRtcp() RTC_RUN_ON(worker_queue_);
297
298 // Called when |rtcp_sender_| informs of the next RTCP instant. The method may
299 // be called on various sequences, and is called under a RTCPSenderLock.
300 void ScheduleRtcpSendEvaluation(TimeDelta duration);
301
302 // Helper method combating too early delayed calls from task queues.
303 // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue
304 // is resolved.
305 void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time)
306 RTC_RUN_ON(worker_queue_);
307
308 // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by |duration|.
309 void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time,
310 TimeDelta duration);
311
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +0200312 TaskQueueBase* const worker_queue_;
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100313 RTC_NO_UNIQUE_ADDRESS SequenceChecker process_thread_checker_;
Tommi08be9ba2021-06-15 23:01:57 +0200314 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
Tommi3a5742c2020-05-20 09:32:51 +0200315
316 std::unique_ptr<RtpSenderContext> rtp_sender_;
Tommi3a5742c2020-05-20 09:32:51 +0200317 RTCPSender rtcp_sender_;
318 RTCPReceiver rtcp_receiver_;
319
320 Clock* const clock_;
321
Tommi3a5742c2020-05-20 09:32:51 +0200322 int64_t last_rtt_process_time_;
323 int64_t next_process_time_;
324 uint16_t packet_overhead_;
325
326 // Send side
327 int64_t nack_last_time_sent_full_ms_;
328 uint16_t nack_last_seq_number_sent_;
329
330 RemoteBitrateEstimator* const remote_bitrate_;
331
332 RtcpRttStats* const rtt_stats_;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200333 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200334
335 // The processed RTT from RtcpRttStats.
Markus Handellf7303e62020-07-09 01:34:42 +0200336 mutable Mutex mutex_rtt_;
Niels Möllercd982132020-11-26 16:19:56 +0100337 int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
Markus Handell885d5382021-06-21 18:57:36 +0200338
339 RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_;
Tommi3a5742c2020-05-20 09:32:51 +0200340};
341
342} // namespace webrtc
343
344#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_