blob: 9eb7e3a6d622f5e8123a1ff8c8c0b763ff2a28c8 [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"
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +020040#include "rtc_base/task_utils/pending_task_safety_flag.h"
41#include "rtc_base/task_utils/repeating_task.h"
Tommi3a5742c2020-05-20 09:32:51 +020042
43namespace webrtc {
44
45class Clock;
46struct PacedPacketInfo;
47struct RTPVideoHeader;
48
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020049class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
50 public Module,
Tommi3a5742c2020-05-20 09:32:51 +020051 public RTCPReceiver::ModuleRtpRtcp {
52 public:
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020053 explicit ModuleRtpRtcpImpl2(
54 const RtpRtcpInterface::Configuration& configuration);
Tommi3a5742c2020-05-20 09:32:51 +020055 ~ModuleRtpRtcpImpl2() override;
56
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020057 // This method is provided to easy with migrating away from the
58 // RtpRtcp::Create factory method. Since this is an internal implementation
59 // detail though, creating an instance of ModuleRtpRtcpImpl2 directly should
60 // be fine.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020061 static std::unique_ptr<ModuleRtpRtcpImpl2> Create(
62 const Configuration& configuration);
Tomas Gunnarssonfae05622020-06-03 08:54:39 +020063
Tommi3a5742c2020-05-20 09:32:51 +020064 // Returns the number of milliseconds until the module want a worker thread to
65 // call Process.
66 int64_t TimeUntilNextProcess() override;
67
68 // Process any pending tasks such as timeouts.
69 void Process() override;
70
71 // Receiver part.
72
73 // Called when we receive an RTCP packet.
74 void IncomingRtcpPacket(const uint8_t* incoming_packet,
75 size_t incoming_packet_length) override;
76
77 void SetRemoteSSRC(uint32_t ssrc) override;
78
79 // Sender part.
80 void RegisterSendPayloadFrequency(int payload_type,
81 int payload_frequency) override;
82
83 int32_t DeRegisterSendPayload(int8_t payload_type) override;
84
85 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
86
Tommi3a5742c2020-05-20 09:32:51 +020087 void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
88 int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
89 void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
90
91 bool SupportsPadding() const override;
92 bool SupportsRtxPayloadPadding() const override;
93
94 // Get start timestamp.
95 uint32_t StartTimestamp() const override;
96
97 // Configure start timestamp, default is a random number.
98 void SetStartTimestamp(uint32_t timestamp) override;
99
100 uint16_t SequenceNumber() const override;
101
102 // Set SequenceNumber, default is a random number.
103 void SetSequenceNumber(uint16_t seq) override;
104
105 void SetRtpState(const RtpState& rtp_state) override;
106 void SetRtxState(const RtpState& rtp_state) override;
107 RtpState GetRtpState() const override;
108 RtpState GetRtxState() const override;
109
110 uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
111
112 void SetRid(const std::string& rid) override;
113
114 void SetMid(const std::string& mid) override;
115
116 void SetCsrcs(const std::vector<uint32_t>& csrcs) override;
117
118 RTCPSender::FeedbackState GetFeedbackState();
119
120 void SetRtxSendStatus(int mode) override;
121 int RtxSendStatus() const override;
122 absl::optional<uint32_t> RtxSsrc() const override;
123
124 void SetRtxSendPayloadType(int payload_type,
125 int associated_payload_type) override;
126
127 absl::optional<uint32_t> FlexfecSsrc() const override;
128
129 // Sends kRtcpByeCode when going from true to false.
130 int32_t SetSendingStatus(bool sending) override;
131
132 bool Sending() const override;
133
134 // Drops or relays media packets.
135 void SetSendingMediaStatus(bool sending) override;
136
137 bool SendingMedia() const override;
138
139 bool IsAudioConfigured() const override;
140
141 void SetAsPartOfAllocation(bool part_of_allocation) override;
142
143 bool OnSendingRtpFrame(uint32_t timestamp,
144 int64_t capture_time_ms,
145 int payload_type,
146 bool force_sender_report) override;
147
148 bool TrySendPacket(RtpPacketToSend* packet,
149 const PacedPacketInfo& pacing_info) override;
150
Erik Språng1d50cb62020-07-02 17:41:32 +0200151 void SetFecProtectionParams(const FecProtectionParams& delta_params,
152 const FecProtectionParams& key_params) override;
153
154 std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
155
Tommi3a5742c2020-05-20 09:32:51 +0200156 void OnPacketsAcknowledged(
157 rtc::ArrayView<const uint16_t> sequence_numbers) override;
158
159 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
160 size_t target_size_bytes) override;
161
162 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
163 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
164
165 size_t ExpectedPerPacketOverhead() const override;
166
167 // RTCP part.
168
169 // Get RTCP status.
170 RtcpMode RTCP() const override;
171
172 // Configure RTCP status i.e on/off.
173 void SetRTCPStatus(RtcpMode method) override;
174
175 // Set RTCP CName.
176 int32_t SetCNAME(const char* c_name) override;
177
Tommi3a5742c2020-05-20 09:32:51 +0200178 // Get remote NTP.
179 int32_t RemoteNTP(uint32_t* received_ntp_secs,
180 uint32_t* received_ntp_frac,
181 uint32_t* rtcp_arrival_time_secs,
182 uint32_t* rtcp_arrival_time_frac,
183 uint32_t* rtcp_timestamp) const override;
184
Tommi3a5742c2020-05-20 09:32:51 +0200185 // Get RoundTripTime.
186 int32_t RTT(uint32_t remote_ssrc,
187 int64_t* rtt,
188 int64_t* avg_rtt,
189 int64_t* min_rtt,
190 int64_t* max_rtt) const override;
191
192 int64_t ExpectedRetransmissionTimeMs() const override;
193
194 // Force a send of an RTCP packet.
195 // Normal SR and RR are triggered via the process function.
196 int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
197
Tommi3a5742c2020-05-20 09:32:51 +0200198 void GetSendStreamDataCounters(
199 StreamDataCounters* rtp_counters,
200 StreamDataCounters* rtx_counters) const override;
201
202 // Get received RTCP report, report block.
203 int32_t RemoteRTCPStat(
204 std::vector<RTCPReportBlock>* receive_blocks) const override;
205 // A snapshot of the most recent Report Block with additional data of
206 // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
207 // Within this list, the ReportBlockData::RTCPReportBlock::source_ssrc(),
208 // which is the SSRC of the corresponding outbound RTP stream, is unique.
209 std::vector<ReportBlockData> GetLatestReportBlockData() const override;
210
211 // (REMB) Receiver Estimated Max Bitrate.
212 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
213 void UnsetRemb() override;
214
Tommi3a5742c2020-05-20 09:32:51 +0200215 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
216
217 size_t MaxRtpPacketSize() const override;
218
219 void SetMaxRtpPacketSize(size_t max_packet_size) override;
220
221 // (NACK) Negative acknowledgment part.
222
223 // Send a Negative acknowledgment packet.
224 // TODO(philipel): Deprecate SendNACK and use SendNack instead.
225 int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
226
227 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
228
229 // Store the sent packets, needed to answer to a negative acknowledgment
230 // requests.
231 void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
232
233 bool StorePackets() const override;
234
235 void SendCombinedRtcpPacket(
236 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
237
Tommi3a5742c2020-05-20 09:32:51 +0200238 // (XR) Receiver reference time report.
239 void SetRtcpXrRrtrStatus(bool enable) override;
240
241 bool RtcpXrRrtrStatus() const override;
242
243 // Video part.
244 int32_t SendLossNotification(uint16_t last_decoded_seq_num,
245 uint16_t last_received_seq_num,
246 bool decodability_flag,
247 bool buffering_allowed) override;
248
249 bool LastReceivedNTP(uint32_t* NTPsecs,
250 uint32_t* NTPfrac,
251 uint32_t* remote_sr) const;
252
Tommi3a5742c2020-05-20 09:32:51 +0200253 RtpSendRates GetSendRates() const override;
254
255 void OnReceivedNack(
256 const std::vector<uint16_t>& nack_sequence_numbers) override;
257 void OnReceivedRtcpReportBlocks(
258 const ReportBlockList& report_blocks) override;
259 void OnRequestSendReport() override;
260
261 void SetVideoBitrateAllocation(
262 const VideoBitrateAllocation& bitrate) override;
263
264 RTPSender* RtpSender() override;
265 const RTPSender* RtpSender() const override;
266
Tommi3a5742c2020-05-20 09:32:51 +0200267 private:
268 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
269 FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
270
Erik Språng1d50cb62020-07-02 17:41:32 +0200271 struct RtpSenderContext : public SequenceNumberAssigner {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200272 explicit RtpSenderContext(const RtpRtcpInterface::Configuration& config);
Erik Språng1d50cb62020-07-02 17:41:32 +0200273 void AssignSequenceNumber(RtpPacketToSend* packet) override;
Tommi3a5742c2020-05-20 09:32:51 +0200274 // Storage of packets, for retransmissions and padding, if applicable.
275 RtpPacketHistory packet_history;
276 // Handles final time timestamping/stats/etc and handover to Transport.
277 RtpSenderEgress packet_sender;
278 // If no paced sender configured, this class will be used to pass packets
279 // from |packet_generator_| to |packet_sender_|.
280 RtpSenderEgress::NonPacedPacketSender non_paced_sender;
281 // Handles creation of RTP packets to be sent.
282 RTPSender packet_generator;
283 };
284
285 void set_rtt_ms(int64_t rtt_ms);
286 int64_t rtt_ms() const;
287
288 bool TimeToSendFullNackList(int64_t now) const;
289
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200290 // Called on a timer, once a second, on the worker_queue_, to update the RTT,
291 // check if we need to send RTCP report, send TMMBR updates and fire events.
292 void PeriodicUpdate();
293
Tomas Gunnarsson473bbd82020-06-27 17:44:55 +0200294 TaskQueueBase* const worker_queue_;
Mirko Bonadei0abd5182020-10-06 19:39:59 +0000295 SequenceChecker process_thread_checker_;
Tommi3a5742c2020-05-20 09:32:51 +0200296
297 std::unique_ptr<RtpSenderContext> rtp_sender_;
298
299 RTCPSender rtcp_sender_;
300 RTCPReceiver rtcp_receiver_;
301
302 Clock* const clock_;
303
Tommi3a5742c2020-05-20 09:32:51 +0200304 int64_t last_rtt_process_time_;
305 int64_t next_process_time_;
306 uint16_t packet_overhead_;
307
308 // Send side
309 int64_t nack_last_time_sent_full_ms_;
310 uint16_t nack_last_seq_number_sent_;
311
312 RemoteBitrateEstimator* const remote_bitrate_;
313
314 RtcpRttStats* const rtt_stats_;
Tomas Gunnarssonba0ba712020-07-01 08:53:21 +0200315 RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
Tommi3a5742c2020-05-20 09:32:51 +0200316
317 // The processed RTT from RtcpRttStats.
Markus Handellf7303e62020-07-09 01:34:42 +0200318 mutable Mutex mutex_rtt_;
Tommi3a5742c2020-05-20 09:32:51 +0200319 int64_t rtt_ms_;
320};
321
322} // namespace webrtc
323
324#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_