blob: b6071d96ee1a26ccf1ddf879703b7c267b791e5e [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
Ali Tofighd14e8892022-05-13 11:42:16 +020022#include "absl/strings/string_view.h"
Tommi3a5742c2020-05-20 09:32:51 +020023#include "absl/types/optional.h"
24#include "api/rtp_headers.h"
Artem Titovd15a5752021-02-10 14:31:24 +010025#include "api/sequence_checker.h"
Artem Titovc374d112022-06-16 21:27:45 +020026#include "api/task_queue/pending_task_safety_flag.h"
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +020027#include "api/task_queue/task_queue_base.h"
Markus Handell885d5382021-06-21 18:57:36 +020028#include "api/units/time_delta.h"
Tommi3a5742c2020-05-20 09:32:51 +020029#include "api/video/video_bitrate_allocation.h"
30#include "modules/include/module_fec_types.h"
Tommi3a5742c2020-05-20 09:32:51 +020031#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" // RTCPPacketType
Erik Språngbfcfe032021-08-04 14:45:32 +020032#include "modules/rtp_rtcp/source/packet_sequencer.h"
Tommi3a5742c2020-05-20 09:32:51 +020033#include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
34#include "modules/rtp_rtcp/source/rtcp_receiver.h"
35#include "modules/rtp_rtcp/source/rtcp_sender.h"
36#include "modules/rtp_rtcp/source/rtp_packet_history.h"
37#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
38#include "modules/rtp_rtcp/source/rtp_sender.h"
39#include "modules/rtp_rtcp/source/rtp_sender_egress.h"
Tommi3a5742c2020-05-20 09:32:51 +020040#include "rtc_base/gtest_prod_util.h"
Markus Handellf7303e62020-07-09 01:34:42 +020041#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010042#include "rtc_base/system/no_unique_address.h"
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020043#include "rtc_base/task_utils/repeating_task.h"
Markus Handell885d5382021-06-21 18:57:36 +020044#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.
Harald Alvestrand1f206b82023-02-01 11:12:46 +000069 [[deprecated("Use ArrayView version")]] void IncomingRtcpPacket(
70 const uint8_t* incoming_packet,
71 size_t incoming_packet_length) override {
72 IncomingRtcpPacket(
73 rtc::MakeArrayView(incoming_packet, incoming_packet_length));
74 }
75 void IncomingRtcpPacket(
76 rtc::ArrayView<const uint8_t> incoming_packet) override;
Tommi3a5742c2020-05-20 09:32:51 +020077
78 void SetRemoteSSRC(uint32_t ssrc) override;
79
Tommi08be9ba2021-06-15 23:01:57 +020080 void SetLocalSsrc(uint32_t local_ssrc) override;
81
Tommi3a5742c2020-05-20 09:32:51 +020082 // Sender part.
83 void RegisterSendPayloadFrequency(int payload_type,
84 int payload_frequency) override;
85
86 int32_t DeRegisterSendPayload(int8_t payload_type) override;
87
88 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
89
Tommi3a5742c2020-05-20 09:32:51 +020090 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
Tommi3a5742c2020-05-20 09:32:51 +020091 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
92
93 bool SupportsPadding() const override;
94 bool SupportsRtxPayloadPadding() const override;
95
96 // Get start timestamp.
97 uint32_t StartTimestamp() const override;
98
99 // Configure start timestamp, default is a random number.
100 void SetStartTimestamp(uint32_t timestamp) override;
101
102 uint16_t SequenceNumber() const override;
103
104 // Set SequenceNumber, default is a random number.
105 void SetSequenceNumber(uint16_t seq) override;
106
107 void SetRtpState(const RtpState& rtp_state) override;
108 void SetRtxState(const RtpState& rtp_state) override;
109 RtpState GetRtpState() const override;
110 RtpState GetRtxState() const override;
111
Ivo Creusen8c40d512021-07-13 12:53:22 +0000112 void SetNonSenderRttMeasurement(bool enabled) override;
113
Tommi3a5742c2020-05-20 09:32:51 +0200114 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
115
Tommi08be9ba2021-06-15 23:01:57 +0200116 // Semantically identical to `SSRC()` but must be called on the packet
117 // delivery thread/tq and returns the ssrc that maps to
118 // RtpRtcpInterface::Configuration::local_media_ssrc.
119 uint32_t local_media_ssrc() const;
120
Ali Tofighd14e8892022-05-13 11:42:16 +0200121 void SetMid(absl::string_view mid) override;
Tommi3a5742c2020-05-20 09:32:51 +0200122
Tommi3a5742c2020-05-20 09:32:51 +0200123 RTCPSender::FeedbackState GetFeedbackState();
124
125 void SetRtxSendStatus(int mode) override;
126 int RtxSendStatus() const override;
127 absl::optional<uint32_t> RtxSsrc() const override;
128
129 void SetRtxSendPayloadType(int payload_type,
130 int associated_payload_type) override;
131
132 absl::optional<uint32_t> FlexfecSsrc() const override;
133
134 // Sends kRtcpByeCode when going from true to false.
135 int32_t SetSendingStatus(bool sending) override;
136
137 bool Sending() const override;
138
139 // Drops or relays media packets.
140 void SetSendingMediaStatus(bool sending) override;
141
142 bool SendingMedia() const override;
143
144 bool IsAudioConfigured() const override;
145
146 void SetAsPartOfAllocation(bool part_of_allocation) override;
147
148 bool OnSendingRtpFrame(uint32_t timestamp,
149 int64_t capture_time_ms,
150 int payload_type,
151 bool force_sender_report) override;
152
153 bool TrySendPacket(RtpPacketToSend* packet,
154 const PacedPacketInfo& pacing_info) override;
155
Erik Språng1d50cb62020-07-02 17:41:32 +0200156 void SetFecProtectionParams(const FecProtectionParams& delta_params,
157 const FecProtectionParams& key_params) override;
158
159 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
160
Erik Språng50459492022-09-08 16:53:06 +0200161 void OnAbortedRetransmissions(
162 rtc::ArrayView<const uint16_t> sequence_numbers) override;
163
Tommi3a5742c2020-05-20 09:32:51 +0200164 void OnPacketsAcknowledged(
165 rtc::ArrayView<const uint16_t> sequence_numbers) override;
166
167 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
168 size_t target_size_bytes) override;
169
170 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
171 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
172
173 size_t ExpectedPerPacketOverhead() const override;
174
Erik Språngb6bbdeb2021-08-13 16:12:41 +0200175 void OnPacketSendingThreadSwitched() override;
176
Tommi3a5742c2020-05-20 09:32:51 +0200177 // 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.
Ali Tofighd14e8892022-05-13 11:42:16 +0200186 int32_t SetCNAME(absl::string_view c_name) override;
Tommi3a5742c2020-05-20 09:32:51 +0200187
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;
Ivo Creusen2562cf02021-09-03 14:51:22 +0000219 absl::optional<NonSenderRttStats> GetNonSenderRttStats() const override;
Tommi3a5742c2020-05-20 09:32:51 +0200220
221 // (REMB) Receiver Estimated Max Bitrate.
222 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
223 void UnsetRemb() override;
224
Tommi3a5742c2020-05-20 09:32:51 +0200225 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
226
227 size_t MaxRtpPacketSize() const override;
228
229 void SetMaxRtpPacketSize(size_t max_packet_size) override;
230
231 // (NACK) Negative acknowledgment part.
232
233 // Send a Negative acknowledgment packet.
234 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
235 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
236
237 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
238
239 // Store the sent packets, needed to answer to a negative acknowledgment
240 // requests.
241 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
242
Tommi3a5742c2020-05-20 09:32:51 +0200243 void SendCombinedRtcpPacket(
244 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
245
Tommi3a5742c2020-05-20 09:32:51 +0200246 // Video part.
247 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
248 uint16_t last_received_seq_num,
249 bool decodability_flag,
250 bool buffering_allowed) override;
251
Tommi3a5742c2020-05-20 09:32:51 +0200252 RtpSendRates GetSendRates() const override;
253
254 void OnReceivedNack(
255 const std::vector<uint16_t>& nack_sequence_numbers) override;
256 void OnReceivedRtcpReportBlocks(
257 const ReportBlockList& report_blocks) override;
258 void OnRequestSendReport() override;
259
260 void SetVideoBitrateAllocation(
261 const VideoBitrateAllocation& bitrate) override;
262
263 RTPSender* RtpSender() override;
264 const RTPSender* RtpSender() const override;
265
Tommi3a5742c2020-05-20 09:32:51 +0200266 private:
267 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
268 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
269
Erik Språng54abf982021-08-13 17:25:44 +0200270 struct RtpSenderContext {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200271 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Tommi3a5742c2020-05-20 09:32:51 +0200272 // Storage of packets, for retransmissions and padding, if applicable.
273 RtpPacketHistory packet_history;
Erik Språngb6bbdeb2021-08-13 16:12:41 +0200274 SequenceChecker sequencing_checker;
Erik Språngbfcfe032021-08-04 14:45:32 +0200275 // Handles sequence number assignment and padding timestamp generation.
Erik Språngb6bbdeb2021-08-13 16:12:41 +0200276 PacketSequencer sequencer RTC_GUARDED_BY(sequencing_checker);
Tommi3a5742c2020-05-20 09:32:51 +0200277 // Handles final time timestamping/stats/etc and handover to Transport.
278 RtpSenderEgress packet_sender;
279 // If no paced sender configured, this class will be used to pass packets
Artem Titov913cfa72021-07-28 23:57:33 +0200280 // from `packet_generator_` to `packet_sender_`.
Tommi3a5742c2020-05-20 09:32:51 +0200281 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
282 // Handles creation of RTP packets to be sent.
283 RTPSender packet_generator;
284 };
285
286 void set_rtt_ms(int64_t rtt_ms);
287 int64_t rtt_ms() const;
288
289 bool TimeToSendFullNackList(int64_t now) const;
290
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200291 // Called on a timer, once a second, on the worker_queue_, to update the RTT,
292 // check if we need to send RTCP report, send TMMBR updates and fire events.
293 void PeriodicUpdate();
294
Niels Mölleraf6ea0c2020-11-20 12:21:21 +0100295 // Returns true if the module is configured to store packets.
296 bool StorePackets() const;
297
Markus Handell885d5382021-06-21 18:57:36 +0200298 // Used from RtcpSenderMediator to maybe send rtcp.
299 void MaybeSendRtcp() RTC_RUN_ON(worker_queue_);
300
Artem Titov913cfa72021-07-28 23:57:33 +0200301 // Called when `rtcp_sender_` informs of the next RTCP instant. The method may
Markus Handell885d5382021-06-21 18:57:36 +0200302 // be called on various sequences, and is called under a RTCPSenderLock.
303 void ScheduleRtcpSendEvaluation(TimeDelta duration);
304
305 // Helper method combating too early delayed calls from task queues.
306 // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue
307 // is resolved.
308 void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time)
309 RTC_RUN_ON(worker_queue_);
310
Artem Titov913cfa72021-07-28 23:57:33 +0200311 // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by `duration`.
Markus Handell885d5382021-06-21 18:57:36 +0200312 void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time,
313 TimeDelta duration);
314
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +0200315 TaskQueueBase* const worker_queue_;
Erik Språngb6bbdeb2021-08-13 16:12:41 +0200316 RTC_NO_UNIQUE_ADDRESS SequenceChecker rtcp_thread_checker_;
Tommi3a5742c2020-05-20 09:32:51 +0200317
318 std::unique_ptr<RtpSenderContext> rtp_sender_;
Tommi3a5742c2020-05-20 09:32:51 +0200319 RTCPSender rtcp_sender_;
320 RTCPReceiver rtcp_receiver_;
321
322 Clock* const clock_;
323
Tommi3a5742c2020-05-20 09:32:51 +0200324 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
Tommi3a5742c2020-05-20 09:32:51 +0200330 RtcpRttStats* const rtt_stats_;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200331 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200332
333 // The processed RTT from RtcpRttStats.
Markus Handellf7303e62020-07-09 01:34:42 +0200334 mutable Mutex mutex_rtt_;
Niels Möllercd982132020-11-26 16:19:56 +0100335 int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
Markus Handell885d5382021-06-21 18:57:36 +0200336
337 RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_;
Tommi3a5742c2020-05-20 09:32:51 +0200338};
339
340} // namespace webrtc
341
342#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_