blob: 0440879e12a51d62a8060a32da49dcc589c9e3aa [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,
Tommi3a5742c2020-05-20 09:32:51 +020053 public RTCPReceiver::ModuleRtpRtcp {
54 public:
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020055 explicit ModuleRtpRtcpImpl2(
56 const RtpRtcpInterface::Configuration& configuration);
Tommi3a5742c2020-05-20 09:32:51 +020057 ~ModuleRtpRtcpImpl2() override;
58
Niels Moller2accc7d2021-01-12 15:54:16 +000059 // This method is provided to easy with migrating away from the
60 // RtpRtcp::Create factory method. Since this is an internal implementation
61 // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
62 // be fine.
63 static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
64 const Configuration& configuration);
65
Tommi3a5742c2020-05-20 09:32:51 +020066 // Receiver part.
67
68 // Called when we receive an RTCP packet.
69 void IncomingRtcpPacket(const uint8_t* incoming_packet,
70 size_t incoming_packet_length) override;
71
72 void SetRemoteSSRC(uint32_t ssrc) override;
73
Tommi08be9ba2021-06-15 23:01:57 +020074 void SetLocalSsrc(uint32_t local_ssrc) override;
75
Tommi3a5742c2020-05-20 09:32:51 +020076 // 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
Ivo Creusen8c40d512021-07-13 12:53:22 +0000107 void SetNonSenderRttMeasurement(bool enabled) override;
108
Tommi3a5742c2020-05-20 09:32:51 +0200109 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
110
Tommi08be9ba2021-06-15 23:01:57 +0200111 // Semantically identical to `SSRC()` but must be called on the packet
112 // delivery thread/tq and returns the ssrc that maps to
113 // RtpRtcpInterface::Configuration::local_media_ssrc.
114 uint32_t local_media_ssrc() const;
115
Tommi3a5742c2020-05-20 09:32:51 +0200116 void SetRid(const std::string& rid) override;
117
118 void SetMid(const std::string& mid) override;
119
120 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
121
122 RTCPSender::FeedbackState GetFeedbackState();
123
124 void SetRtxSendStatus(int mode) override;
125 int RtxSendStatus() const override;
126 absl::optional<uint32_t> RtxSsrc() const override;
127
128 void SetRtxSendPayloadType(int payload_type,
129 int associated_payload_type) override;
130
131 absl::optional<uint32_t> FlexfecSsrc() const override;
132
133 // Sends kRtcpByeCode when going from true to false.
134 int32_t SetSendingStatus(bool sending) override;
135
136 bool Sending() const override;
137
138 // Drops or relays media packets.
139 void SetSendingMediaStatus(bool sending) override;
140
141 bool SendingMedia() const override;
142
143 bool IsAudioConfigured() const override;
144
145 void SetAsPartOfAllocation(bool part_of_allocation) override;
146
147 bool OnSendingRtpFrame(uint32_t timestamp,
148 int64_t capture_time_ms,
149 int payload_type,
150 bool force_sender_report) override;
151
152 bool TrySendPacket(RtpPacketToSend* packet,
153 const PacedPacketInfo& pacing_info) override;
154
Erik Språng1d50cb62020-07-02 17:41:32 +0200155 void SetFecProtectionParams(const FecProtectionParams& delta_params,
156 const FecProtectionParams& key_params) override;
157
158 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
159
Tommi3a5742c2020-05-20 09:32:51 +0200160 void OnPacketsAcknowledged(
161 rtc::ArrayView<const uint16_t> sequence_numbers) override;
162
163 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
164 size_t target_size_bytes) override;
165
166 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
167 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
168
169 size_t ExpectedPerPacketOverhead() const override;
170
171 // RTCP part.
172
173 // Get RTCP status.
174 RtcpMode RTCP() const override;
175
176 // Configure RTCP status i.e on/off.
177 void SetRTCPStatus(RtcpMode method) override;
178
179 // Set RTCP CName.
180 int32_t SetCNAME(const char* c_name) override;
181
Tommi3a5742c2020-05-20 09:32:51 +0200182 // Get remote NTP.
183 int32_t RemoteNTP(uint32_t* received_ntp_secs,
184 uint32_t* received_ntp_frac,
185 uint32_t* rtcp_arrival_time_secs,
186 uint32_t* rtcp_arrival_time_frac,
187 uint32_t* rtcp_timestamp) const override;
188
Tommi3a5742c2020-05-20 09:32:51 +0200189 // Get RoundTripTime.
190 int32_t RTT(uint32_t remote_ssrc,
191 int64_t* rtt,
192 int64_t* avg_rtt,
193 int64_t* min_rtt,
194 int64_t* max_rtt) const override;
195
196 int64_t ExpectedRetransmissionTimeMs() const override;
197
198 // Force a send of an RTCP packet.
Markus Handell885d5382021-06-21 18:57:36 +0200199 // Normal SR and RR are triggered via the task queue that's current when this
200 // object is created.
Tommi3a5742c2020-05-20 09:32:51 +0200201 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
202
Tommi3a5742c2020-05-20 09:32:51 +0200203 void GetSendStreamDataCounters(
204 StreamDataCounters* rtp_counters,
205 StreamDataCounters* rtx_counters) const override;
206
Tommi3a5742c2020-05-20 09:32:51 +0200207 // A snapshot of the most recent Report Block with additional data of
208 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
209 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
210 // which is the SSRC of the corresponding outbound RTP stream, is unique.
211 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100212 absl::optional<SenderReportStats> GetSenderReportStats() const override;
Tommi3a5742c2020-05-20 09:32:51 +0200213
214 // (REMB) Receiver Estimated Max Bitrate.
215 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
216 void UnsetRemb() override;
217
Tommi3a5742c2020-05-20 09:32:51 +0200218 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
219
220 size_t MaxRtpPacketSize() const override;
221
222 void SetMaxRtpPacketSize(size_t max_packet_size) override;
223
224 // (NACK) Negative acknowledgment part.
225
226 // Send a Negative acknowledgment packet.
227 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
228 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
229
230 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
231
232 // Store the sent packets, needed to answer to a negative acknowledgment
233 // requests.
234 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
235
Tommi3a5742c2020-05-20 09:32:51 +0200236 void SendCombinedRtcpPacket(
237 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
238
Tommi3a5742c2020-05-20 09:32:51 +0200239 // Video part.
240 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
241 uint16_t last_received_seq_num,
242 bool decodability_flag,
243 bool buffering_allowed) override;
244
Tommi3a5742c2020-05-20 09:32:51 +0200245 RtpSendRates GetSendRates() const override;
246
247 void OnReceivedNack(
248 const std::vector<uint16_t>& nack_sequence_numbers) override;
249 void OnReceivedRtcpReportBlocks(
250 const ReportBlockList& report_blocks) override;
251 void OnRequestSendReport() override;
252
253 void SetVideoBitrateAllocation(
254 const VideoBitrateAllocation& bitrate) override;
255
256 RTPSender* RtpSender() override;
257 const RTPSender* RtpSender() const override;
258
Tommi3a5742c2020-05-20 09:32:51 +0200259 private:
260 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
261 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
262
Erik Språng1d50cb62020-07-02 17:41:32 +0200263 struct RtpSenderContext : public SequenceNumberAssigner {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200264 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Erik Språng1d50cb62020-07-02 17:41:32 +0200265 void AssignSequenceNumber(RtpPacketToSend* packet) override;
Tommi3a5742c2020-05-20 09:32:51 +0200266 // Storage of packets, for retransmissions and padding, if applicable.
267 RtpPacketHistory packet_history;
268 // Handles final time timestamping/stats/etc and handover to Transport.
269 RtpSenderEgress packet_sender;
270 // If no paced sender configured, this class will be used to pass packets
271 // from |packet_generator_| to |packet_sender_|.
272 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
273 // Handles creation of RTP packets to be sent.
274 RTPSender packet_generator;
275 };
276
277 void set_rtt_ms(int64_t rtt_ms);
278 int64_t rtt_ms() const;
279
280 bool TimeToSendFullNackList(int64_t now) const;
281
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200282 // Called on a timer, once a second, on the worker_queue_, to update the RTT,
283 // check if we need to send RTCP report, send TMMBR updates and fire events.
284 void PeriodicUpdate();
285
Niels Mölleraf6ea0c2020-11-20 12:21:21 +0100286 // Returns true if the module is configured to store packets.
287 bool StorePackets() const;
288
Markus Handell885d5382021-06-21 18:57:36 +0200289 // Used from RtcpSenderMediator to maybe send rtcp.
290 void MaybeSendRtcp() RTC_RUN_ON(worker_queue_);
291
292 // Called when |rtcp_sender_| informs of the next RTCP instant. The method may
293 // be called on various sequences, and is called under a RTCPSenderLock.
294 void ScheduleRtcpSendEvaluation(TimeDelta duration);
295
296 // Helper method combating too early delayed calls from task queues.
297 // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue
298 // is resolved.
299 void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time)
300 RTC_RUN_ON(worker_queue_);
301
302 // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by |duration|.
303 void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time,
304 TimeDelta duration);
305
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +0200306 TaskQueueBase* const worker_queue_;
Tommi08be9ba2021-06-15 23:01:57 +0200307 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
Tommi3a5742c2020-05-20 09:32:51 +0200308
309 std::unique_ptr<RtpSenderContext> rtp_sender_;
Tommi3a5742c2020-05-20 09:32:51 +0200310 RTCPSender rtcp_sender_;
311 RTCPReceiver rtcp_receiver_;
312
313 Clock* const clock_;
314
Tommi3a5742c2020-05-20 09:32:51 +0200315 uint16_t packet_overhead_;
316
317 // Send side
318 int64_t nack_last_time_sent_full_ms_;
319 uint16_t nack_last_seq_number_sent_;
320
321 RemoteBitrateEstimator* const remote_bitrate_;
322
323 RtcpRttStats* const rtt_stats_;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200324 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200325
326 // The processed RTT from RtcpRttStats.
Markus Handellf7303e62020-07-09 01:34:42 +0200327 mutable Mutex mutex_rtt_;
Niels Möllercd982132020-11-26 16:19:56 +0100328 int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
Markus Handell885d5382021-06-21 18:57:36 +0200329
330 RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_;
Tommi3a5742c2020-05-20 09:32:51 +0200331};
332
333} // namespace webrtc
334
335#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_