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