blob: 6cdd149c42594ab614558d08341b0b73f9617423 [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"
Tommi3a5742c2020-05-20 09:32:51 +020037#include "rtc_base/gtest_prod_util.h"
Markus Handellf7303e62020-07-09 01:34:42 +020038#include "rtc_base/synchronization/mutex.h"
Tommi3a5742c2020-05-20 09:32:51 +020039#include "rtc_base/synchronization/sequence_checker.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"
Tommi3a5742c2020-05-20 09:32:51 +020043
44namespace webrtc {
45
46class Clock;
47struct PacedPacketInfo;
48struct RTPVideoHeader;
49
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020050class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
51 public Module,
Tommi3a5742c2020-05-20 09:32:51 +020052 public RTCPReceiver::ModuleRtpRtcp {
53 public:
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020054 explicit ModuleRtpRtcpImpl2(
55 const RtpRtcpInterface::Configuration& configuration);
Tommi3a5742c2020-05-20 09:32:51 +020056 ~ModuleRtpRtcpImpl2() override;
57
Tommi3a5742c2020-05-20 09:32:51 +020058 // Returns the number of milliseconds until the module want a worker thread to
59 // call Process.
60 int64_t TimeUntilNextProcess() override;
61
62 // Process any pending tasks such as timeouts.
63 void Process() override;
64
65 // Receiver part.
66
67 // Called when we receive an RTCP packet.
68 void IncomingRtcpPacket(const uint8_t* incoming_packet,
69 size_t incoming_packet_length) override;
70
71 void SetRemoteSSRC(uint32_t ssrc) override;
72
73 // Sender part.
74 void RegisterSendPayloadFrequency(int payload_type,
75 int payload_frequency) override;
76
77 int32_t DeRegisterSendPayload(int8_t payload_type) override;
78
79 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
80
Tommi3a5742c2020-05-20 09:32:51 +020081 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
82 int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
83 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
84
85 bool SupportsPadding() const override;
86 bool SupportsRtxPayloadPadding() const override;
87
88 // Get start timestamp.
89 uint32_t StartTimestamp() const override;
90
91 // Configure start timestamp, default is a random number.
92 void SetStartTimestamp(uint32_t timestamp) override;
93
94 uint16_t SequenceNumber() const override;
95
96 // Set SequenceNumber, default is a random number.
97 void SetSequenceNumber(uint16_t seq) override;
98
99 void SetRtpState(const RtpState& rtp_state) override;
100 void SetRtxState(const RtpState& rtp_state) override;
101 RtpState GetRtpState() const override;
102 RtpState GetRtxState() const override;
103
104 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
105
106 void SetRid(const std::string& rid) override;
107
108 void SetMid(const std::string& mid) override;
109
110 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
111
112 RTCPSender::FeedbackState GetFeedbackState();
113
114 void SetRtxSendStatus(int mode) override;
115 int RtxSendStatus() const override;
116 absl::optional<uint32_t> RtxSsrc() const override;
117
118 void SetRtxSendPayloadType(int payload_type,
119 int associated_payload_type) override;
120
121 absl::optional<uint32_t> FlexfecSsrc() const override;
122
123 // Sends kRtcpByeCode when going from true to false.
124 int32_t SetSendingStatus(bool sending) override;
125
126 bool Sending() const override;
127
128 // Drops or relays media packets.
129 void SetSendingMediaStatus(bool sending) override;
130
131 bool SendingMedia() const override;
132
133 bool IsAudioConfigured() const override;
134
135 void SetAsPartOfAllocation(bool part_of_allocation) override;
136
137 bool OnSendingRtpFrame(uint32_t timestamp,
138 int64_t capture_time_ms,
139 int payload_type,
140 bool force_sender_report) override;
141
142 bool TrySendPacket(RtpPacketToSend* packet,
143 const PacedPacketInfo& pacing_info) override;
144
Erik Språng1d50cb62020-07-02 17:41:32 +0200145 void SetFecProtectionParams(const FecProtectionParams& delta_params,
146 const FecProtectionParams& key_params) override;
147
148 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
149
Tommi3a5742c2020-05-20 09:32:51 +0200150 void OnPacketsAcknowledged(
151 rtc::ArrayView<const uint16_t> sequence_numbers) override;
152
153 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
154 size_t target_size_bytes) override;
155
156 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
157 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
158
159 size_t ExpectedPerPacketOverhead() const override;
160
161 // RTCP part.
162
163 // Get RTCP status.
164 RtcpMode RTCP() const override;
165
166 // Configure RTCP status i.e on/off.
167 void SetRTCPStatus(RtcpMode method) override;
168
169 // Set RTCP CName.
170 int32_t SetCNAME(const char* c_name) override;
171
Tommi3a5742c2020-05-20 09:32:51 +0200172 // Get remote NTP.
173 int32_t RemoteNTP(uint32_t* received_ntp_secs,
174 uint32_t* received_ntp_frac,
175 uint32_t* rtcp_arrival_time_secs,
176 uint32_t* rtcp_arrival_time_frac,
177 uint32_t* rtcp_timestamp) const override;
178
Tommi3a5742c2020-05-20 09:32:51 +0200179 // Get RoundTripTime.
180 int32_t RTT(uint32_t remote_ssrc,
181 int64_t* rtt,
182 int64_t* avg_rtt,
183 int64_t* min_rtt,
184 int64_t* max_rtt) const override;
185
186 int64_t ExpectedRetransmissionTimeMs() const override;
187
188 // Force a send of an RTCP packet.
189 // Normal SR and RR are triggered via the process function.
190 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
191
Tommi3a5742c2020-05-20 09:32:51 +0200192 void GetSendStreamDataCounters(
193 StreamDataCounters* rtp_counters,
194 StreamDataCounters* rtx_counters) const override;
195
196 // Get received RTCP report, report block.
197 int32_t RemoteRTCPStat(
198 std::vector<RTCPReportBlock>* receive_blocks) const override;
199 // A snapshot of the most recent Report Block with additional data of
200 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
201 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
202 // which is the SSRC of the corresponding outbound RTP stream, is unique.
203 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
204
205 // (REMB) Receiver Estimated Max Bitrate.
206 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
207 void UnsetRemb() override;
208
Tommi3a5742c2020-05-20 09:32:51 +0200209 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
210
211 size_t MaxRtpPacketSize() const override;
212
213 void SetMaxRtpPacketSize(size_t max_packet_size) override;
214
215 // (NACK) Negative acknowledgment part.
216
217 // Send a Negative acknowledgment packet.
218 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
219 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
220
221 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
222
223 // Store the sent packets, needed to answer to a negative acknowledgment
224 // requests.
225 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
226
Tommi3a5742c2020-05-20 09:32:51 +0200227 void SendCombinedRtcpPacket(
228 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
229
Tommi3a5742c2020-05-20 09:32:51 +0200230 // Video part.
231 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
232 uint16_t last_received_seq_num,
233 bool decodability_flag,
234 bool buffering_allowed) override;
235
236 bool LastReceivedNTP(uint32_t* NTPsecs,
237 uint32_t* NTPfrac,
238 uint32_t* remote_sr) const;
239
Tommi3a5742c2020-05-20 09:32:51 +0200240 RtpSendRates GetSendRates() const override;
241
242 void OnReceivedNack(
243 const std::vector<uint16_t>& nack_sequence_numbers) override;
244 void OnReceivedRtcpReportBlocks(
245 const ReportBlockList& report_blocks) override;
246 void OnRequestSendReport() override;
247
248 void SetVideoBitrateAllocation(
249 const VideoBitrateAllocation& bitrate) override;
250
251 RTPSender* RtpSender() override;
252 const RTPSender* RtpSender() const override;
253
Tommi3a5742c2020-05-20 09:32:51 +0200254 private:
255 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
256 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
257
Erik Språng1d50cb62020-07-02 17:41:32 +0200258 struct RtpSenderContext : public SequenceNumberAssigner {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200259 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Erik Språng1d50cb62020-07-02 17:41:32 +0200260 void AssignSequenceNumber(RtpPacketToSend* packet) override;
Tommi3a5742c2020-05-20 09:32:51 +0200261 // Storage of packets, for retransmissions and padding, if applicable.
262 RtpPacketHistory packet_history;
263 // Handles final time timestamping/stats/etc and handover to Transport.
264 RtpSenderEgress packet_sender;
265 // If no paced sender configured, this class will be used to pass packets
266 // from |packet_generator_| to |packet_sender_|.
267 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
268 // Handles creation of RTP packets to be sent.
269 RTPSender packet_generator;
270 };
271
272 void set_rtt_ms(int64_t rtt_ms);
273 int64_t rtt_ms() const;
274
275 bool TimeToSendFullNackList(int64_t now) const;
276
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200277 // Called on a timer, once a second, on the worker_queue_, to update the RTT,
278 // check if we need to send RTCP report, send TMMBR updates and fire events.
279 void PeriodicUpdate();
280
Niels Mölleraf6ea0c2020-11-20 12:21:21 +0100281 // Returns true if the module is configured to store packets.
282 bool StorePackets() const;
283
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +0200284 TaskQueueBase* const worker_queue_;
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100285 RTC_NO_UNIQUE_ADDRESS SequenceChecker process_thread_checker_;
Tommi3a5742c2020-05-20 09:32:51 +0200286
287 std::unique_ptr<RtpSenderContext> rtp_sender_;
288
289 RTCPSender rtcp_sender_;
290 RTCPReceiver rtcp_receiver_;
291
292 Clock* const clock_;
293
Tommi3a5742c2020-05-20 09:32:51 +0200294 int64_t last_rtt_process_time_;
295 int64_t next_process_time_;
296 uint16_t packet_overhead_;
297
298 // Send side
299 int64_t nack_last_time_sent_full_ms_;
300 uint16_t nack_last_seq_number_sent_;
301
302 RemoteBitrateEstimator* const remote_bitrate_;
303
304 RtcpRttStats* const rtt_stats_;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200305 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200306
307 // The processed RTT from RtcpRttStats.
Markus Handellf7303e62020-07-09 01:34:42 +0200308 mutable Mutex mutex_rtt_;
Niels Möllercd982132020-11-26 16:19:56 +0100309 int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
Tommi3a5742c2020-05-20 09:32:51 +0200310};
311
312} // namespace webrtc
313
314#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_