blob: a411b237a0a6c4912b352f25dcd381c494d9762e [file] [log] [blame]
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +02001/*
2 * Copyright (c) 2020 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_INTERFACE_H_
12#define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_
13
14#include <memory>
15#include <string>
16#include <vector>
17
18#include "absl/types/optional.h"
19#include "api/frame_transformer_interface.h"
20#include "api/scoped_refptr.h"
21#include "api/transport/webrtc_key_value_config.h"
22#include "api/video/video_bitrate_allocation.h"
23#include "modules/rtp_rtcp/include/receive_statistics.h"
24#include "modules/rtp_rtcp/include/report_block_data.h"
25#include "modules/rtp_rtcp/include/rtp_packet_sender.h"
26#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
27#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
28#include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
29#include "modules/rtp_rtcp/source/video_fec_generator.h"
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +010030#include "system_wrappers/include/ntp_time.h"
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020031
32namespace webrtc {
33
34// Forward declarations.
35class FrameEncryptorInterface;
36class RateLimiter;
37class RemoteBitrateEstimator;
38class RtcEventLog;
39class RTPSender;
40class Transport;
41class VideoBitrateAllocationObserver;
42
43class RtpRtcpInterface : public RtcpFeedbackSenderInterface {
44 public:
45 struct Configuration {
46 Configuration() = default;
47 Configuration(Configuration&& rhs) = default;
48
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090049 Configuration(const Configuration&) = delete;
50 Configuration& operator=(const Configuration&) = delete;
51
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020052 // True for a audio version of the RTP/RTCP module object false will create
53 // a video version.
54 bool audio = false;
55 bool receiver_only = false;
56
57 // The clock to use to read time. If nullptr then system clock will be used.
58 Clock* clock = nullptr;
59
60 ReceiveStatisticsProvider* receive_statistics = nullptr;
61
62 // Transport object that will be called when packets are ready to be sent
63 // out on the network.
64 Transport* outgoing_transport = nullptr;
65
66 // Called when the receiver requests an intra frame.
67 RtcpIntraFrameObserver* intra_frame_callback = nullptr;
68
69 // Called when the receiver sends a loss notification.
70 RtcpLossNotificationObserver* rtcp_loss_notification_observer = nullptr;
71
72 // Called when we receive a changed estimate from the receiver of out
73 // stream.
74 RtcpBandwidthObserver* bandwidth_callback = nullptr;
75
76 NetworkStateEstimateObserver* network_state_estimate_observer = nullptr;
77 TransportFeedbackObserver* transport_feedback_callback = nullptr;
78 VideoBitrateAllocationObserver* bitrate_allocation_observer = nullptr;
79 RtcpRttStats* rtt_stats = nullptr;
80 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer = nullptr;
81 // Called on receipt of RTCP report block from remote side.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020082 // TODO(bugs.webrtc.org/10679): Consider whether we want to use
83 // only getters or only callbacks. If we decide on getters, the
84 // ReportBlockDataObserver should also be removed in favor of
85 // GetLatestReportBlockData().
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020086 RtcpCnameCallback* rtcp_cname_callback = nullptr;
87 ReportBlockDataObserver* report_block_data_observer = nullptr;
88
89 // Estimates the bandwidth available for a set of streams from the same
90 // client.
91 RemoteBitrateEstimator* remote_bitrate_estimator = nullptr;
92
93 // Spread any bursts of packets into smaller bursts to minimize packet loss.
94 RtpPacketSender* paced_sender = nullptr;
95
96 // Generates FEC packets.
97 // TODO(sprang): Wire up to RtpSenderEgress.
98 VideoFecGenerator* fec_generator = nullptr;
99
100 BitrateStatisticsObserver* send_bitrate_observer = nullptr;
101 SendSideDelayObserver* send_side_delay_observer = nullptr;
102 RtcEventLog* event_log = nullptr;
103 SendPacketObserver* send_packet_observer = nullptr;
104 RateLimiter* retransmission_rate_limiter = nullptr;
105 StreamDataCountersCallback* rtp_stats_callback = nullptr;
106
107 int rtcp_report_interval_ms = 0;
108
109 // Update network2 instead of pacer_exit field of video timing extension.
110 bool populate_network2_timestamp = false;
111
112 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer;
113
114 // E2EE Custom Video Frame Encryption
115 FrameEncryptorInterface* frame_encryptor = nullptr;
116 // Require all outgoing frames to be encrypted with a FrameEncryptor.
117 bool require_frame_encryption = false;
118
119 // Corresponds to extmap-allow-mixed in SDP negotiation.
120 bool extmap_allow_mixed = false;
121
122 // If true, the RTP sender will always annotate outgoing packets with
123 // MID and RID header extensions, if provided and negotiated.
124 // If false, the RTP sender will stop sending MID and RID header extensions,
125 // when it knows that the receiver is ready to demux based on SSRC. This is
126 // done by RTCP RR acking.
127 bool always_send_mid_and_rid = false;
128
Artem Titov913cfa72021-07-28 23:57:33 +0200129 // If set, field trials are read from `field_trials`, otherwise
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200130 // defaults to webrtc::FieldTrialBasedConfig.
131 const WebRtcKeyValueConfig* field_trials = nullptr;
132
133 // SSRCs for media and retransmission, respectively.
Artem Titov913cfa72021-07-28 23:57:33 +0200134 // FlexFec SSRC is fetched from `flexfec_sender`.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200135 uint32_t local_media_ssrc = 0;
136 absl::optional<uint32_t> rtx_send_ssrc;
137
138 bool need_rtp_packet_infos = false;
139
140 // If true, the RTP packet history will select RTX packets based on
141 // heuristics such as send time, retransmission count etc, in order to
142 // make padding potentially more useful.
143 // If false, the last packet will always be picked. This may reduce CPU
144 // overhead.
145 bool enable_rtx_padding_prioritization = true;
146
Niels Möllerbe810cb2020-12-02 14:25:03 +0100147 // Estimate RTT as non-sender as described in
148 // https://tools.ietf.org/html/rfc3611#section-4.4 and #section-4.5
149 bool non_sender_rtt_measurement = false;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200150 };
151
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100152 // Stats for RTCP sender reports (SR) for a specific SSRC.
153 // Refer to https://tools.ietf.org/html/rfc3550#section-6.4.1.
154 struct SenderReportStats {
Ivo Creusen2562cf02021-09-03 14:51:22 +0000155 // Arrival NTP timestamp for the last received RTCP SR.
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100156 NtpTime last_arrival_timestamp;
157 // Received (a.k.a., remote) NTP timestamp for the last received RTCP SR.
158 NtpTime last_remote_timestamp;
159 // Total number of RTP data packets transmitted by the sender since starting
160 // transmission up until the time this SR packet was generated. The count
161 // should be reset if the sender changes its SSRC identifier.
162 uint32_t packets_sent;
163 // Total number of payload octets (i.e., not including header or padding)
164 // transmitted in RTP data packets by the sender since starting transmission
165 // up until the time this SR packet was generated. The count should be reset
166 // if the sender changes its SSRC identifier.
167 uint64_t bytes_sent;
168 // Total number of RTCP SR blocks received.
169 // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-reportssent.
170 uint64_t reports_count;
171 };
Ivo Creusen2562cf02021-09-03 14:51:22 +0000172 // Stats about the non-sender SSRC, based on RTCP extended reports (XR).
173 // Refer to https://datatracker.ietf.org/doc/html/rfc3611#section-2.
174 struct NonSenderRttStats {
175 // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
176 absl::optional<TimeDelta> round_trip_time;
177 // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-totalroundtriptime
178 TimeDelta total_round_trip_time = TimeDelta::Zero();
179 // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptimemeasurements
180 int round_trip_time_measurements = 0;
181 };
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100182
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200183 // **************************************************************************
184 // Receiver functions
185 // **************************************************************************
186
187 virtual void IncomingRtcpPacket(const uint8_t* incoming_packet,
188 size_t incoming_packet_length) = 0;
189
190 virtual void SetRemoteSSRC(uint32_t ssrc) = 0;
191
Tommi08be9ba2021-06-15 23:01:57 +0200192 // Called when the local ssrc changes (post initialization) for receive
193 // streams to match with send. Called on the packet receive thread/tq.
194 virtual void SetLocalSsrc(uint32_t ssrc) = 0;
195
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200196 // **************************************************************************
197 // Sender
198 // **************************************************************************
199
200 // Sets the maximum size of an RTP packet, including RTP headers.
201 virtual void SetMaxRtpPacketSize(size_t size) = 0;
202
203 // Returns max RTP packet size. Takes into account RTP headers and
204 // FEC/ULP/RED overhead (when FEC is enabled).
205 virtual size_t MaxRtpPacketSize() const = 0;
206
207 virtual void RegisterSendPayloadFrequency(int payload_type,
208 int payload_frequency) = 0;
209
210 // Unregisters a send payload.
Artem Titov913cfa72021-07-28 23:57:33 +0200211 // `payload_type` - payload type of codec
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200212 // Returns -1 on failure else 0.
213 virtual int32_t DeRegisterSendPayload(int8_t payload_type) = 0;
214
215 virtual void SetExtmapAllowMixed(bool extmap_allow_mixed) = 0;
216
217 // Register extension by uri, triggers CHECK on falure.
218 virtual void RegisterRtpHeaderExtension(absl::string_view uri, int id) = 0;
219
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200220 virtual void DeregisterSendRtpHeaderExtension(absl::string_view uri) = 0;
221
222 // Returns true if RTP module is send media, and any of the extensions
223 // required for bandwidth estimation is registered.
224 virtual bool SupportsPadding() const = 0;
225 // Same as SupportsPadding(), but additionally requires that
226 // SetRtxSendStatus() has been called with the kRtxRedundantPayloads option
227 // enabled.
228 virtual bool SupportsRtxPayloadPadding() const = 0;
229
230 // Returns start timestamp.
231 virtual uint32_t StartTimestamp() const = 0;
232
233 // Sets start timestamp. Start timestamp is set to a random value if this
234 // function is never called.
235 virtual void SetStartTimestamp(uint32_t timestamp) = 0;
236
237 // Returns SequenceNumber.
238 virtual uint16_t SequenceNumber() const = 0;
239
240 // Sets SequenceNumber, default is a random number.
241 virtual void SetSequenceNumber(uint16_t seq) = 0;
242
243 virtual void SetRtpState(const RtpState& rtp_state) = 0;
244 virtual void SetRtxState(const RtpState& rtp_state) = 0;
245 virtual RtpState GetRtpState() const = 0;
246 virtual RtpState GetRtxState() const = 0;
247
Ivo Creusen8c40d512021-07-13 12:53:22 +0000248 // This can be used to enable/disable receive-side RTT.
249 virtual void SetNonSenderRttMeasurement(bool enabled) = 0;
250
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200251 // Returns SSRC.
252 virtual uint32_t SSRC() const = 0;
253
254 // Sets the value for sending in the RID (and Repaired) RTP header extension.
255 // RIDs are used to identify an RTP stream if SSRCs are not negotiated.
256 // If the RID and Repaired RID extensions are not registered, the RID will
257 // not be sent.
258 virtual void SetRid(const std::string& rid) = 0;
259
260 // Sets the value for sending in the MID RTP header extension.
261 // The MID RTP header extension should be registered for this to do anything.
262 // Once set, this value can not be changed or removed.
263 virtual void SetMid(const std::string& mid) = 0;
264
265 // Sets CSRC.
Artem Titov913cfa72021-07-28 23:57:33 +0200266 // `csrcs` - vector of CSRCs
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200267 virtual void SetCsrcs(const std::vector<uint32_t>& csrcs) = 0;
268
269 // Turns on/off sending RTX (RFC 4588). The modes can be set as a combination
270 // of values of the enumerator RtxMode.
271 virtual void SetRtxSendStatus(int modes) = 0;
272
273 // Returns status of sending RTX (RFC 4588). The returned value can be
274 // a combination of values of the enumerator RtxMode.
275 virtual int RtxSendStatus() const = 0;
276
277 // Returns the SSRC used for RTX if set, otherwise a nullopt.
278 virtual absl::optional<uint32_t> RtxSsrc() const = 0;
279
280 // Sets the payload type to use when sending RTX packets. Note that this
281 // doesn't enable RTX, only the payload type is set.
282 virtual void SetRtxSendPayloadType(int payload_type,
283 int associated_payload_type) = 0;
284
285 // Returns the FlexFEC SSRC, if there is one.
286 virtual absl::optional<uint32_t> FlexfecSsrc() const = 0;
287
288 // Sets sending status. Sends kRtcpByeCode when going from true to false.
289 // Returns -1 on failure else 0.
290 virtual int32_t SetSendingStatus(bool sending) = 0;
291
292 // Returns current sending status.
293 virtual bool Sending() const = 0;
294
295 // Starts/Stops media packets. On by default.
296 virtual void SetSendingMediaStatus(bool sending) = 0;
297
298 // Returns current media sending status.
299 virtual bool SendingMedia() const = 0;
300
301 // Returns whether audio is configured (i.e. Configuration::audio = true).
302 virtual bool IsAudioConfigured() const = 0;
303
304 // Indicate that the packets sent by this module should be counted towards the
305 // bitrate estimate since the stream participates in the bitrate allocation.
306 virtual void SetAsPartOfAllocation(bool part_of_allocation) = 0;
307
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200308 // Returns bitrate sent (post-pacing) per packet type.
309 virtual RtpSendRates GetSendRates() const = 0;
310
311 virtual RTPSender* RtpSender() = 0;
312 virtual const RTPSender* RtpSender() const = 0;
313
314 // Record that a frame is about to be sent. Returns true on success, and false
315 // if the module isn't ready to send.
316 virtual bool OnSendingRtpFrame(uint32_t timestamp,
317 int64_t capture_time_ms,
318 int payload_type,
319 bool force_sender_report) = 0;
320
321 // Try to send the provided packet. Returns true iff packet matches any of
322 // the SSRCs for this module (media/rtx/fec etc) and was forwarded to the
323 // transport.
324 virtual bool TrySendPacket(RtpPacketToSend* packet,
325 const PacedPacketInfo& pacing_info) = 0;
326
Erik Språng1d50cb62020-07-02 17:41:32 +0200327 // Update the FEC protection parameters to use for delta- and key-frames.
328 // Only used when deferred FEC is active.
329 virtual void SetFecProtectionParams(
330 const FecProtectionParams& delta_params,
331 const FecProtectionParams& key_params) = 0;
332
333 // If deferred FEC generation is enabled, this method should be called after
334 // calling TrySendPacket(). Any generated FEC packets will be removed and
335 // returned from the FEC generator.
336 virtual std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() = 0;
337
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200338 virtual void OnPacketsAcknowledged(
339 rtc::ArrayView<const uint16_t> sequence_numbers) = 0;
340
341 virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
342 size_t target_size_bytes) = 0;
343
344 virtual std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
345 rtc::ArrayView<const uint16_t> sequence_numbers) const = 0;
346
347 // Returns an expected per packet overhead representing the main RTP header,
348 // any CSRCs, and the registered header extensions that are expected on all
349 // packets (i.e. disregarding things like abs capture time which is only
350 // populated on a subset of packets, but counting MID/RID type extensions
351 // when we expect to send them).
352 virtual size_t ExpectedPerPacketOverhead() const = 0;
353
Erik Språngb6bbdeb2021-08-13 16:12:41 +0200354 // Access to packet state (e.g. sequence numbering) must only be access by
355 // one thread at a time. It may be only one thread, or a construction thread
356 // that calls SetRtpState() - handing over to a pacer thread that calls
357 // TrySendPacket() - and at teardown ownership is handed to a destruciton
358 // thread that calls GetRtpState().
359 // This method is used to signal that "ownership" of the rtp state is being
360 // transferred to another thread.
361 virtual void OnPacketSendingThreadSwitched() = 0;
362
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200363 // **************************************************************************
364 // RTCP
365 // **************************************************************************
366
367 // Returns RTCP status.
368 virtual RtcpMode RTCP() const = 0;
369
370 // Sets RTCP status i.e on(compound or non-compound)/off.
Artem Titov913cfa72021-07-28 23:57:33 +0200371 // `method` - RTCP method to use.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200372 virtual void SetRTCPStatus(RtcpMode method) = 0;
373
374 // Sets RTCP CName (i.e unique identifier).
375 // Returns -1 on failure else 0.
376 virtual int32_t SetCNAME(const char* cname) = 0;
377
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200378 // Returns remote NTP.
379 // Returns -1 on failure else 0.
380 virtual int32_t RemoteNTP(uint32_t* received_ntp_secs,
381 uint32_t* received_ntp_frac,
382 uint32_t* rtcp_arrival_time_secs,
383 uint32_t* rtcp_arrival_time_frac,
384 uint32_t* rtcp_timestamp) const = 0;
385
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200386 // Returns current RTT (round-trip time) estimate.
387 // Returns -1 on failure else 0.
388 virtual int32_t RTT(uint32_t remote_ssrc,
389 int64_t* rtt,
390 int64_t* avg_rtt,
391 int64_t* min_rtt,
392 int64_t* max_rtt) const = 0;
393
394 // Returns the estimated RTT, with fallback to a default value.
395 virtual int64_t ExpectedRetransmissionTimeMs() const = 0;
396
397 // Forces a send of a RTCP packet. Periodic SR and RR are triggered via the
398 // process function.
399 // Returns -1 on failure else 0.
400 virtual int32_t SendRTCP(RTCPPacketType rtcp_packet_type) = 0;
401
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200402 // Returns send statistics for the RTP and RTX stream.
403 virtual void GetSendStreamDataCounters(
404 StreamDataCounters* rtp_counters,
405 StreamDataCounters* rtx_counters) const = 0;
406
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200407 // A snapshot of Report Blocks with additional data of interest to statistics.
408 // Within this list, the sender-source SSRC pair is unique and per-pair the
409 // ReportBlockData represents the latest Report Block that was received for
410 // that pair.
411 virtual std::vector<ReportBlockData> GetLatestReportBlockData() const = 0;
Alessio Bazzicabc1c93d2021-03-12 17:45:26 +0100412 // Returns stats based on the received RTCP SRs.
413 virtual absl::optional<SenderReportStats> GetSenderReportStats() const = 0;
Ivo Creusen2562cf02021-09-03 14:51:22 +0000414 // Returns non-sender RTT stats, based on DLRR.
415 virtual absl::optional<NonSenderRttStats> GetNonSenderRttStats() const = 0;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200416
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200417 // (REMB) Receiver Estimated Max Bitrate.
418 // Schedules sending REMB on next and following sender/receiver reports.
419 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override = 0;
420 // Stops sending REMB on next and following sender/receiver reports.
421 void UnsetRemb() override = 0;
422
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200423 // (NACK)
424
425 // Sends a Negative acknowledgement packet.
426 // Returns -1 on failure else 0.
427 // TODO(philipel): Deprecate this and start using SendNack instead, mostly
428 // because we want a function that actually send NACK for the specified
429 // packets.
430 virtual int32_t SendNACK(const uint16_t* nack_list, uint16_t size) = 0;
431
432 // Sends NACK for the packets specified.
433 // Note: This assumes the caller keeps track of timing and doesn't rely on
434 // the RTP module to do this.
435 virtual void SendNack(const std::vector<uint16_t>& sequence_numbers) = 0;
436
437 // Store the sent packets, needed to answer to a Negative acknowledgment
438 // requests.
439 virtual void SetStorePacketsStatus(bool enable, uint16_t numberToStore) = 0;
440
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200441 virtual void SetVideoBitrateAllocation(
442 const VideoBitrateAllocation& bitrate) = 0;
443
444 // **************************************************************************
445 // Video
446 // **************************************************************************
447
448 // Requests new key frame.
449 // using PLI, https://tools.ietf.org/html/rfc4585#section-6.3.1.1
450 void SendPictureLossIndication() { SendRTCP(kRtcpPli); }
451 // using FIR, https://tools.ietf.org/html/rfc5104#section-4.3.1.2
452 void SendFullIntraRequest() { SendRTCP(kRtcpFir); }
453
454 // Sends a LossNotification RTCP message.
455 // Returns -1 on failure else 0.
456 virtual int32_t SendLossNotification(uint16_t last_decoded_seq_num,
457 uint16_t last_received_seq_num,
458 bool decodability_flag,
459 bool buffering_allowed) = 0;
460};
461
462} // namespace webrtc
463
464#endif // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_