• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_INCLUDE_RTP_RTCP_DEFINES_H_
12 #define MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
13 
14 #include <stddef.h>
15 
16 #include <list>
17 #include <memory>
18 #include <vector>
19 
20 #include "absl/algorithm/container.h"
21 #include "absl/strings/string_view.h"
22 #include "absl/types/optional.h"
23 #include "absl/types/variant.h"
24 #include "api/array_view.h"
25 #include "api/audio_codecs/audio_format.h"
26 #include "api/rtp_headers.h"
27 #include "api/transport/network_types.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
29 #include "system_wrappers/include/clock.h"
30 
31 #define RTCP_CNAME_SIZE 256  // RFC 3550 page 44, including null termination
32 #define IP_PACKET_SIZE 1500  // we assume ethernet
33 
34 namespace webrtc {
35 class RtpPacket;
36 class RtpPacketToSend;
37 namespace rtcp {
38 class TransportFeedback;
39 }
40 
41 const int kVideoPayloadTypeFrequency = 90000;
42 
43 // TODO(bugs.webrtc.org/6458): Remove this when all the depending projects are
44 // updated to correctly set rtp rate for RtcpSender.
45 const int kBogusRtpRateForAudioRtcp = 8000;
46 
47 // Minimum RTP header size in bytes.
48 const uint8_t kRtpHeaderSize = 12;
49 
50 bool IsLegalMidName(absl::string_view name);
51 bool IsLegalRsidName(absl::string_view name);
52 
53 // This enum must not have any gaps, i.e., all integers between
54 // kRtpExtensionNone and kRtpExtensionNumberOfExtensions must be valid enum
55 // entries.
56 enum RTPExtensionType : int {
57   kRtpExtensionNone,
58   kRtpExtensionTransmissionTimeOffset,
59   kRtpExtensionAudioLevel,
60   kRtpExtensionInbandComfortNoise,
61   kRtpExtensionAbsoluteSendTime,
62   kRtpExtensionAbsoluteCaptureTime,
63   kRtpExtensionVideoRotation,
64   kRtpExtensionTransportSequenceNumber,
65   kRtpExtensionTransportSequenceNumber02,
66   kRtpExtensionPlayoutDelay,
67   kRtpExtensionVideoContentType,
68   kRtpExtensionVideoTiming,
69   kRtpExtensionRtpStreamId,
70   kRtpExtensionRepairedRtpStreamId,
71   kRtpExtensionMid,
72   kRtpExtensionGenericFrameDescriptor00,
73   kRtpExtensionGenericFrameDescriptor = kRtpExtensionGenericFrameDescriptor00,
74   kRtpExtensionGenericFrameDescriptor02,
75   kRtpExtensionColorSpace,
76   kRtpExtensionNumberOfExtensions  // Must be the last entity in the enum.
77 };
78 
79 enum RTCPAppSubTypes { kAppSubtypeBwe = 0x00 };
80 
81 // TODO(sprang): Make this an enum class once rtcp_receiver has been cleaned up.
82 enum RTCPPacketType : uint32_t {
83   kRtcpReport = 0x0001,
84   kRtcpSr = 0x0002,
85   kRtcpRr = 0x0004,
86   kRtcpSdes = 0x0008,
87   kRtcpBye = 0x0010,
88   kRtcpPli = 0x0020,
89   kRtcpNack = 0x0040,
90   kRtcpFir = 0x0080,
91   kRtcpTmmbr = 0x0100,
92   kRtcpTmmbn = 0x0200,
93   kRtcpSrReq = 0x0400,
94   kRtcpLossNotification = 0x2000,
95   kRtcpRemb = 0x10000,
96   kRtcpTransmissionTimeOffset = 0x20000,
97   kRtcpXrReceiverReferenceTime = 0x40000,
98   kRtcpXrDlrrReportBlock = 0x80000,
99   kRtcpTransportFeedback = 0x100000,
100   kRtcpXrTargetBitrate = 0x200000
101 };
102 
103 enum RtxMode {
104   kRtxOff = 0x0,
105   kRtxRetransmitted = 0x1,     // Only send retransmissions over RTX.
106   kRtxRedundantPayloads = 0x2  // Preventively send redundant payloads
107                                // instead of padding.
108 };
109 
110 const size_t kRtxHeaderSize = 2;
111 
112 struct RTCPReportBlock {
RTCPReportBlockRTCPReportBlock113   RTCPReportBlock()
114       : sender_ssrc(0),
115         source_ssrc(0),
116         fraction_lost(0),
117         packets_lost(0),
118         extended_highest_sequence_number(0),
119         jitter(0),
120         last_sender_report_timestamp(0),
121         delay_since_last_sender_report(0) {}
122 
RTCPReportBlockRTCPReportBlock123   RTCPReportBlock(uint32_t sender_ssrc,
124                   uint32_t source_ssrc,
125                   uint8_t fraction_lost,
126                   int32_t packets_lost,
127                   uint32_t extended_highest_sequence_number,
128                   uint32_t jitter,
129                   uint32_t last_sender_report_timestamp,
130                   uint32_t delay_since_last_sender_report)
131       : sender_ssrc(sender_ssrc),
132         source_ssrc(source_ssrc),
133         fraction_lost(fraction_lost),
134         packets_lost(packets_lost),
135         extended_highest_sequence_number(extended_highest_sequence_number),
136         jitter(jitter),
137         last_sender_report_timestamp(last_sender_report_timestamp),
138         delay_since_last_sender_report(delay_since_last_sender_report) {}
139 
140   // Fields as described by RFC 3550 6.4.2.
141   uint32_t sender_ssrc;  // SSRC of sender of this report.
142   uint32_t source_ssrc;  // SSRC of the RTP packet sender.
143   uint8_t fraction_lost;
144   int32_t packets_lost;  // 24 bits valid.
145   uint32_t extended_highest_sequence_number;
146   uint32_t jitter;
147   uint32_t last_sender_report_timestamp;
148   uint32_t delay_since_last_sender_report;
149 };
150 
151 typedef std::list<RTCPReportBlock> ReportBlockList;
152 
153 struct RtpState {
RtpStateRtpState154   RtpState()
155       : sequence_number(0),
156         start_timestamp(0),
157         timestamp(0),
158         capture_time_ms(-1),
159         last_timestamp_time_ms(-1),
160         ssrc_has_acked(false) {}
161   uint16_t sequence_number;
162   uint32_t start_timestamp;
163   uint32_t timestamp;
164   int64_t capture_time_ms;
165   int64_t last_timestamp_time_ms;
166   bool ssrc_has_acked;
167 };
168 
169 // Callback interface for packets recovered by FlexFEC or ULPFEC. In
170 // the FlexFEC case, the implementation should be able to demultiplex
171 // the recovered RTP packets based on SSRC.
172 class RecoveredPacketReceiver {
173  public:
174   virtual void OnRecoveredPacket(const uint8_t* packet, size_t length) = 0;
175 
176  protected:
177   virtual ~RecoveredPacketReceiver() = default;
178 };
179 
180 class RtcpIntraFrameObserver {
181  public:
~RtcpIntraFrameObserver()182   virtual ~RtcpIntraFrameObserver() {}
183 
184   virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) = 0;
185 };
186 
187 // Observer for incoming LossNotification RTCP messages.
188 // See the documentation of LossNotification for details.
189 class RtcpLossNotificationObserver {
190  public:
191   virtual ~RtcpLossNotificationObserver() = default;
192 
193   virtual void OnReceivedLossNotification(uint32_t ssrc,
194                                           uint16_t seq_num_of_last_decodable,
195                                           uint16_t seq_num_of_last_received,
196                                           bool decodability_flag) = 0;
197 };
198 
199 class RtcpBandwidthObserver {
200  public:
201   // REMB or TMMBR
202   virtual void OnReceivedEstimatedBitrate(uint32_t bitrate) = 0;
203 
204   virtual void OnReceivedRtcpReceiverReport(
205       const ReportBlockList& report_blocks,
206       int64_t rtt,
207       int64_t now_ms) = 0;
208 
~RtcpBandwidthObserver()209   virtual ~RtcpBandwidthObserver() {}
210 };
211 
212 // NOTE! |kNumMediaTypes| must be kept in sync with RtpPacketMediaType!
213 static constexpr size_t kNumMediaTypes = 5;
214 enum class RtpPacketMediaType : size_t {
215   kAudio,                         // Audio media packets.
216   kVideo,                         // Video media packets.
217   kRetransmission,                // Retransmisions, sent as response to NACK.
218   kForwardErrorCorrection,        // FEC packets.
219   kPadding = kNumMediaTypes - 1,  // RTX or plain padding sent to maintain BWE.
220   // Again, don't forget to udate |kNumMediaTypes| if you add another value!
221 };
222 
223 struct RtpPacketSendInfo {
224  public:
225   RtpPacketSendInfo() = default;
226 
227   uint16_t transport_sequence_number = 0;
228   uint32_t ssrc = 0;
229   uint16_t rtp_sequence_number = 0;
230   size_t length = 0;
231   absl::optional<RtpPacketMediaType> packet_type;
232   PacedPacketInfo pacing_info;
233 };
234 
235 class NetworkStateEstimateObserver {
236  public:
237   virtual void OnRemoteNetworkEstimate(NetworkStateEstimate estimate) = 0;
238   virtual ~NetworkStateEstimateObserver() = default;
239 };
240 
241 class TransportFeedbackObserver {
242  public:
TransportFeedbackObserver()243   TransportFeedbackObserver() {}
~TransportFeedbackObserver()244   virtual ~TransportFeedbackObserver() {}
245 
246   virtual void OnAddPacket(const RtpPacketSendInfo& packet_info) = 0;
247   virtual void OnTransportFeedback(const rtcp::TransportFeedback& feedback) = 0;
248 };
249 
250 // Interface for PacketRouter to send rtcp feedback on behalf of
251 // congestion controller.
252 // TODO(bugs.webrtc.org/8239): Remove and use RtcpTransceiver directly
253 // when RtcpTransceiver always present in rtp transport.
254 class RtcpFeedbackSenderInterface {
255  public:
256   virtual ~RtcpFeedbackSenderInterface() = default;
257   virtual void SendCombinedRtcpPacket(
258       std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) = 0;
259   virtual void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) = 0;
260   virtual void UnsetRemb() = 0;
261 };
262 
263 class StreamFeedbackObserver {
264  public:
265   struct StreamPacketInfo {
266     uint32_t ssrc;
267     uint16_t rtp_sequence_number;
268     bool received;
269   };
270   virtual ~StreamFeedbackObserver() = default;
271 
272   virtual void OnPacketFeedbackVector(
273       std::vector<StreamPacketInfo> packet_feedback_vector) = 0;
274 };
275 
276 class StreamFeedbackProvider {
277  public:
278   virtual void RegisterStreamFeedbackObserver(
279       std::vector<uint32_t> ssrcs,
280       StreamFeedbackObserver* observer) = 0;
281   virtual void DeRegisterStreamFeedbackObserver(
282       StreamFeedbackObserver* observer) = 0;
283   virtual ~StreamFeedbackProvider() = default;
284 };
285 
286 class RtcpRttStats {
287  public:
288   virtual void OnRttUpdate(int64_t rtt) = 0;
289 
290   virtual int64_t LastProcessedRtt() const = 0;
291 
~RtcpRttStats()292   virtual ~RtcpRttStats() {}
293 };
294 
295 struct RtpPacketCounter {
RtpPacketCounterRtpPacketCounter296   RtpPacketCounter()
297       : header_bytes(0), payload_bytes(0), padding_bytes(0), packets(0) {}
298 
299   explicit RtpPacketCounter(const RtpPacket& packet);
300 
AddRtpPacketCounter301   void Add(const RtpPacketCounter& other) {
302     header_bytes += other.header_bytes;
303     payload_bytes += other.payload_bytes;
304     padding_bytes += other.padding_bytes;
305     packets += other.packets;
306   }
307 
SubtractRtpPacketCounter308   void Subtract(const RtpPacketCounter& other) {
309     RTC_DCHECK_GE(header_bytes, other.header_bytes);
310     header_bytes -= other.header_bytes;
311     RTC_DCHECK_GE(payload_bytes, other.payload_bytes);
312     payload_bytes -= other.payload_bytes;
313     RTC_DCHECK_GE(padding_bytes, other.padding_bytes);
314     padding_bytes -= other.padding_bytes;
315     RTC_DCHECK_GE(packets, other.packets);
316     packets -= other.packets;
317   }
318 
319   bool operator==(const RtpPacketCounter& other) const {
320     return header_bytes == other.header_bytes &&
321            payload_bytes == other.payload_bytes &&
322            padding_bytes == other.padding_bytes && packets == other.packets;
323   }
324 
325   // Not inlined, since use of RtpPacket would result in circular includes.
326   void AddPacket(const RtpPacket& packet);
327 
TotalBytesRtpPacketCounter328   size_t TotalBytes() const {
329     return header_bytes + payload_bytes + padding_bytes;
330   }
331 
332   size_t header_bytes;   // Number of bytes used by RTP headers.
333   size_t payload_bytes;  // Payload bytes, excluding RTP headers and padding.
334   size_t padding_bytes;  // Number of padding bytes.
335   uint32_t packets;      // Number of packets.
336 };
337 
338 // Data usage statistics for a (rtp) stream.
339 struct StreamDataCounters {
340   StreamDataCounters();
341 
AddStreamDataCounters342   void Add(const StreamDataCounters& other) {
343     transmitted.Add(other.transmitted);
344     retransmitted.Add(other.retransmitted);
345     fec.Add(other.fec);
346     if (other.first_packet_time_ms != -1 &&
347         (other.first_packet_time_ms < first_packet_time_ms ||
348          first_packet_time_ms == -1)) {
349       // Use oldest time.
350       first_packet_time_ms = other.first_packet_time_ms;
351     }
352   }
353 
SubtractStreamDataCounters354   void Subtract(const StreamDataCounters& other) {
355     transmitted.Subtract(other.transmitted);
356     retransmitted.Subtract(other.retransmitted);
357     fec.Subtract(other.fec);
358     if (other.first_packet_time_ms != -1 &&
359         (other.first_packet_time_ms > first_packet_time_ms ||
360          first_packet_time_ms == -1)) {
361       // Use youngest time.
362       first_packet_time_ms = other.first_packet_time_ms;
363     }
364   }
365 
TimeSinceFirstPacketInMsStreamDataCounters366   int64_t TimeSinceFirstPacketInMs(int64_t now_ms) const {
367     return (first_packet_time_ms == -1) ? -1 : (now_ms - first_packet_time_ms);
368   }
369 
370   // Returns the number of bytes corresponding to the actual media payload (i.e.
371   // RTP headers, padding, retransmissions and fec packets are excluded).
372   // Note this function does not have meaning for an RTX stream.
MediaPayloadBytesStreamDataCounters373   size_t MediaPayloadBytes() const {
374     return transmitted.payload_bytes - retransmitted.payload_bytes -
375            fec.payload_bytes;
376   }
377 
378   int64_t first_packet_time_ms;  // Time when first packet is sent/received.
379   // The timestamp at which the last packet was received, i.e. the time of the
380   // local clock when it was received - not the RTP timestamp of that packet.
381   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-lastpacketreceivedtimestamp
382   absl::optional<int64_t> last_packet_received_timestamp_ms;
383   RtpPacketCounter transmitted;    // Number of transmitted packets/bytes.
384   RtpPacketCounter retransmitted;  // Number of retransmitted packets/bytes.
385   RtpPacketCounter fec;            // Number of redundancy packets/bytes.
386 };
387 
388 class RtpSendRates {
389   template <std::size_t... Is>
make_zero_array(std::index_sequence<Is...>)390   constexpr std::array<DataRate, sizeof...(Is)> make_zero_array(
391       std::index_sequence<Is...>) {
392     return {{(static_cast<void>(Is), DataRate::Zero())...}};
393   }
394 
395  public:
RtpSendRates()396   RtpSendRates()
397       : send_rates_(
398             make_zero_array(std::make_index_sequence<kNumMediaTypes>())) {}
399   RtpSendRates(const RtpSendRates& rhs) = default;
400   RtpSendRates& operator=(const RtpSendRates&) = default;
401 
402   DataRate& operator[](RtpPacketMediaType type) {
403     return send_rates_[static_cast<size_t>(type)];
404   }
405   const DataRate& operator[](RtpPacketMediaType type) const {
406     return send_rates_[static_cast<size_t>(type)];
407   }
Sum()408   DataRate Sum() const {
409     return absl::c_accumulate(send_rates_, DataRate::Zero());
410   }
411 
412  private:
413   std::array<DataRate, kNumMediaTypes> send_rates_;
414 };
415 
416 // Callback, called whenever byte/packet counts have been updated.
417 class StreamDataCountersCallback {
418  public:
~StreamDataCountersCallback()419   virtual ~StreamDataCountersCallback() {}
420 
421   virtual void DataCountersUpdated(const StreamDataCounters& counters,
422                                    uint32_t ssrc) = 0;
423 };
424 
425 // Information exposed through the GetStats api.
426 struct RtpReceiveStats {
427   // |packets_lost| and |jitter| are defined by RFC 3550, and exposed in the
428   // RTCReceivedRtpStreamStats dictionary, see
429   // https://w3c.github.io/webrtc-stats/#receivedrtpstats-dict*
430   int32_t packets_lost = 0;
431   uint32_t jitter = 0;
432 
433   // Timestamp and counters exposed in RTCInboundRtpStreamStats, see
434   // https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict*
435   absl::optional<int64_t> last_packet_received_timestamp_ms;
436   RtpPacketCounter packet_counter;
437 };
438 
439 // Callback, used to notify an observer whenever new rates have been estimated.
440 class BitrateStatisticsObserver {
441  public:
~BitrateStatisticsObserver()442   virtual ~BitrateStatisticsObserver() {}
443 
444   virtual void Notify(uint32_t total_bitrate_bps,
445                       uint32_t retransmit_bitrate_bps,
446                       uint32_t ssrc) = 0;
447 };
448 
449 // Callback, used to notify an observer whenever the send-side delay is updated.
450 class SendSideDelayObserver {
451  public:
~SendSideDelayObserver()452   virtual ~SendSideDelayObserver() {}
453   virtual void SendSideDelayUpdated(int avg_delay_ms,
454                                     int max_delay_ms,
455                                     uint64_t total_delay_ms,
456                                     uint32_t ssrc) = 0;
457 };
458 
459 // Callback, used to notify an observer whenever a packet is sent to the
460 // transport.
461 // TODO(asapersson): This class will remove the need for SendSideDelayObserver.
462 // Remove SendSideDelayObserver once possible.
463 class SendPacketObserver {
464  public:
~SendPacketObserver()465   virtual ~SendPacketObserver() {}
466   virtual void OnSendPacket(uint16_t packet_id,
467                             int64_t capture_time_ms,
468                             uint32_t ssrc) = 0;
469 };
470 
471 // Interface for a class that can assign RTP sequence numbers for a packet
472 // to be sent.
473 class SequenceNumberAssigner {
474  public:
475   SequenceNumberAssigner() = default;
476   virtual ~SequenceNumberAssigner() = default;
477 
478   virtual void AssignSequenceNumber(RtpPacketToSend* packet) = 0;
479 };
480 }  // namespace webrtc
481 #endif  // MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
482