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 WEBRTC_MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
12 #define WEBRTC_MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
13 
14 #include <stddef.h>
15 #include <list>
16 #include <vector>
17 
18 #include "webrtc/common_types.h"
19 #include "webrtc/modules/include/module_common_types.h"
20 #include "webrtc/system_wrappers/include/clock.h"
21 #include "webrtc/typedefs.h"
22 
23 #define RTCP_CNAME_SIZE 256    // RFC 3550 page 44, including null termination
24 #define IP_PACKET_SIZE 1500    // we assume ethernet
25 #define MAX_NUMBER_OF_PARALLEL_TELEPHONE_EVENTS 10
26 
27 namespace webrtc {
28 namespace rtcp {
29 class TransportFeedback;
30 }
31 
32 const int kVideoPayloadTypeFrequency = 90000;
33 // TODO(solenberg): RTP time stamp rate for RTCP is fixed at 8k, this is legacy
34 // and should be fixed.
35 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6458
36 const int kBogusRtpRateForAudioRtcp = 8000;
37 
38 // Minimum RTP header size in bytes.
39 const uint8_t kRtpHeaderSize = 12;
40 
41 struct AudioPayload {
42     uint32_t    frequency;
43     size_t      channels;
44     uint32_t    rate;
45 };
46 
47 struct VideoPayload {
48   RtpVideoCodecTypes videoCodecType;
49   // The H264 profile only matters if videoCodecType == kRtpVideoH264.
50   H264::Profile h264_profile;
51 };
52 
53 union PayloadUnion {
54     AudioPayload Audio;
55     VideoPayload Video;
56 };
57 
58 enum RTPAliveType { kRtpDead = 0, kRtpNoRtp = 1, kRtpAlive = 2 };
59 
60 enum ProtectionType {
61   kUnprotectedPacket,
62   kProtectedPacket
63 };
64 
65 enum StorageType {
66   kDontRetransmit,
67   kAllowRetransmission
68 };
69 
70 enum RTPExtensionType {
71   kRtpExtensionNone,
72   kRtpExtensionTransmissionTimeOffset,
73   kRtpExtensionAudioLevel,
74   kRtpExtensionAbsoluteSendTime,
75   kRtpExtensionVideoRotation,
76   kRtpExtensionTransportSequenceNumber,
77   kRtpExtensionPlayoutDelay,
78   kRtpExtensionRtpStreamId,
79   kRtpExtensionRepairedRtpStreamId,
80   kRtpExtensionMId,
81   kRtpExtensionCsrcAudioLevel,
82   kRtpExtensionNumberOfExtensions  // Must be the last entity in the enum.
83 };
84 
85 enum RTCPAppSubTypes { kAppSubtypeBwe = 0x00 };
86 
87 // TODO(sprang): Make this an enum class once rtcp_receiver has been cleaned up.
88 enum RTCPPacketType : uint32_t {
89   kRtcpReport = 0x0001,
90   kRtcpSr = 0x0002,
91   kRtcpRr = 0x0004,
92   kRtcpSdes = 0x0008,
93   kRtcpBye = 0x0010,
94   kRtcpPli = 0x0020,
95   kRtcpNack = 0x0040,
96   kRtcpFir = 0x0080,
97   kRtcpTmmbr = 0x0100,
98   kRtcpTmmbn = 0x0200,
99   kRtcpSrReq = 0x0400,
100   kRtcpXrVoipMetric = 0x0800,
101   kRtcpApp = 0x1000,
102   kRtcpSli = 0x4000,
103   kRtcpRpsi = 0x8000,
104   kRtcpRemb = 0x10000,
105   kRtcpTransmissionTimeOffset = 0x20000,
106   kRtcpXrReceiverReferenceTime = 0x40000,
107   kRtcpXrDlrrReportBlock = 0x80000,
108   kRtcpTransportFeedback = 0x100000,
109   kRtcpXrTargetBitrate = 0x200000
110 };
111 
112 enum KeyFrameRequestMethod { kKeyFrameReqPliRtcp, kKeyFrameReqFirRtcp };
113 
114 enum RtpRtcpPacketType { kPacketRtp = 0, kPacketKeepAlive = 1 };
115 
116 enum RetransmissionMode : uint8_t {
117   kRetransmitOff = 0x0,
118   kRetransmitFECPackets = 0x1,
119   kRetransmitBaseLayer = 0x2,
120   kRetransmitHigherLayers = 0x4,
121   kRetransmitAllPackets = 0xFF
122 };
123 
124 enum RtxMode {
125   kRtxOff                 = 0x0,
126   kRtxRetransmitted       = 0x1,  // Only send retransmissions over RTX.
127   kRtxRedundantPayloads   = 0x2   // Preventively send redundant payloads
128                                   // instead of padding.
129 };
130 
131 const size_t kRtxHeaderSize = 2;
132 
133 struct RTCPSenderInfo {
134     uint32_t NTPseconds;
135     uint32_t NTPfraction;
136     uint32_t RTPtimeStamp;
137     uint32_t sendPacketCount;
138     uint32_t sendOctetCount;
139 };
140 
141 struct RTCPReportBlock {
RTCPReportBlockRTCPReportBlock142   RTCPReportBlock()
143       : remoteSSRC(0), sourceSSRC(0), fractionLost(0), cumulativeLost(0),
144         extendedHighSeqNum(0), jitter(0), lastSR(0),
145         delaySinceLastSR(0) {}
146 
RTCPReportBlockRTCPReportBlock147   RTCPReportBlock(uint32_t remote_ssrc,
148                   uint32_t source_ssrc,
149                   uint8_t fraction_lost,
150                   uint32_t cumulative_lost,
151                   uint32_t extended_high_sequence_number,
152                   uint32_t jitter,
153                   uint32_t last_sender_report,
154                   uint32_t delay_since_last_sender_report)
155       : remoteSSRC(remote_ssrc),
156         sourceSSRC(source_ssrc),
157         fractionLost(fraction_lost),
158         cumulativeLost(cumulative_lost),
159         extendedHighSeqNum(extended_high_sequence_number),
160         jitter(jitter),
161         lastSR(last_sender_report),
162         delaySinceLastSR(delay_since_last_sender_report) {}
163 
164   // Fields as described by RFC 3550 6.4.2.
165   uint32_t remoteSSRC;  // SSRC of sender of this report.
166   uint32_t sourceSSRC;  // SSRC of the RTP packet sender.
167   uint8_t fractionLost;
168   uint32_t cumulativeLost;  // 24 bits valid.
169   uint32_t extendedHighSeqNum;
170   uint32_t jitter;
171   uint32_t lastSR;
172   uint32_t delaySinceLastSR;
173 };
174 
175 typedef std::list<RTCPReportBlock> ReportBlockList;
176 
177 struct RtpState {
RtpStateRtpState178   RtpState()
179       : sequence_number(0),
180         start_timestamp(0),
181         timestamp(0),
182         capture_time_ms(-1),
183         last_timestamp_time_ms(-1),
184         media_has_been_sent(false) {}
185   uint16_t sequence_number;
186   uint32_t start_timestamp;
187   uint32_t timestamp;
188   int64_t capture_time_ms;
189   int64_t last_timestamp_time_ms;
190   bool media_has_been_sent;
191 };
192 
193 class RtpData {
194  public:
~RtpData()195   virtual ~RtpData() {}
196 
197   virtual int32_t OnReceivedPayloadData(const uint8_t* payload_data,
198                                         size_t payload_size,
199                                         const WebRtcRTPHeader* rtp_header) = 0;
200 
201   virtual bool OnRecoveredPacket(const uint8_t* packet,
202                                  size_t packet_length) = 0;
203 };
204 
205 class RtpFeedback {
206  public:
~RtpFeedback()207   virtual ~RtpFeedback() {}
208 
209   // Receiving payload change or SSRC change. (return success!)
210   /*
211   *   channels    - number of channels in codec (1 = mono, 2 = stereo)
212   */
213   virtual int32_t OnInitializeDecoder(
214       int8_t payload_type,
215       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
216       int frequency,
217       size_t channels,
218       uint32_t rate) = 0;
219 
220   virtual void OnIncomingSSRCChanged(uint32_t ssrc) = 0;
221 
222   virtual void OnIncomingCSRCChanged(uint32_t csrc, bool added) = 0;
223 };
224 
225 class RtcpIntraFrameObserver {
226  public:
227   virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) = 0;
228 
229   virtual void OnReceivedSLI(uint32_t ssrc,
230                              uint8_t picture_id) = 0;
231 
232   virtual void OnReceivedRPSI(uint32_t ssrc,
233                               uint64_t picture_id) = 0;
234 
~RtcpIntraFrameObserver()235   virtual ~RtcpIntraFrameObserver() {}
236 };
237 
238 class RtcpBandwidthObserver {
239  public:
240   // REMB or TMMBR
241   virtual void OnReceivedEstimatedBitrate(uint32_t bitrate) = 0;
242 
243   virtual void OnReceivedRtcpReceiverReport(
244       const ReportBlockList& report_blocks,
245       int64_t rtt,
246       int64_t now_ms) = 0;
247 
~RtcpBandwidthObserver()248   virtual ~RtcpBandwidthObserver() {}
249 };
250 
251 struct PacketInfo {
PacketInfoPacketInfo252   PacketInfo(int64_t arrival_time_ms, uint16_t sequence_number)
253       : PacketInfo(-1,
254                    arrival_time_ms,
255                    -1,
256                    sequence_number,
257                    0,
258                    kNotAProbe) {}
259 
PacketInfoPacketInfo260   PacketInfo(int64_t arrival_time_ms,
261              int64_t send_time_ms,
262              uint16_t sequence_number,
263              size_t payload_size,
264              int probe_cluster_id)
265       : PacketInfo(-1,
266                    arrival_time_ms,
267                    send_time_ms,
268                    sequence_number,
269                    payload_size,
270                    probe_cluster_id) {}
271 
PacketInfoPacketInfo272   PacketInfo(int64_t creation_time_ms,
273              int64_t arrival_time_ms,
274              int64_t send_time_ms,
275              uint16_t sequence_number,
276              size_t payload_size,
277              int probe_cluster_id)
278       : creation_time_ms(creation_time_ms),
279         arrival_time_ms(arrival_time_ms),
280         send_time_ms(send_time_ms),
281         sequence_number(sequence_number),
282         payload_size(payload_size),
283         probe_cluster_id(probe_cluster_id) {}
284 
285   static constexpr int kNotAProbe = -1;
286 
287   // Time corresponding to when this object was created.
288   int64_t creation_time_ms;
289   // Time corresponding to when the packet was received. Timestamped with the
290   // receiver's clock.
291   int64_t arrival_time_ms;
292   // Time corresponding to when the packet was sent, timestamped with the
293   // sender's clock.
294   int64_t send_time_ms;
295   // Packet identifier, incremented with 1 for every packet generated by the
296   // sender.
297   uint16_t sequence_number;
298   // Size of the packet excluding RTP headers.
299   size_t payload_size;
300   // Which probing cluster this packets belongs to.
301   int probe_cluster_id;
302 };
303 
304 class TransportFeedbackObserver {
305  public:
TransportFeedbackObserver()306   TransportFeedbackObserver() {}
~TransportFeedbackObserver()307   virtual ~TransportFeedbackObserver() {}
308 
309   // Note: Transport-wide sequence number as sequence number. Arrival time
310   // must be set to 0.
311   virtual void AddPacket(uint16_t sequence_number,
312                          size_t length,
313                          int probe_cluster_id) = 0;
314 
315   virtual void OnTransportFeedback(const rtcp::TransportFeedback& feedback) = 0;
316 
317   virtual std::vector<PacketInfo> GetTransportFeedbackVector() const = 0;
318 };
319 
320 class RtcpRttStats {
321  public:
322   virtual void OnRttUpdate(int64_t rtt) = 0;
323 
324   virtual int64_t LastProcessedRtt() const = 0;
325 
~RtcpRttStats()326   virtual ~RtcpRttStats() {}
327 };
328 
329 // Null object version of RtpFeedback.
330 class NullRtpFeedback : public RtpFeedback {
331  public:
~NullRtpFeedback()332   virtual ~NullRtpFeedback() {}
333 
OnInitializeDecoder(int8_t payload_type,const char payloadName[RTP_PAYLOAD_NAME_SIZE],int frequency,size_t channels,uint32_t rate)334   int32_t OnInitializeDecoder(int8_t payload_type,
335                               const char payloadName[RTP_PAYLOAD_NAME_SIZE],
336                               int frequency,
337                               size_t channels,
338                               uint32_t rate) override {
339     return 0;
340   }
341 
OnIncomingSSRCChanged(uint32_t ssrc)342   void OnIncomingSSRCChanged(uint32_t ssrc) override {}
OnIncomingCSRCChanged(uint32_t csrc,bool added)343   void OnIncomingCSRCChanged(uint32_t csrc, bool added) override {}
344 };
345 
346 // Null object version of RtpData.
347 class NullRtpData : public RtpData {
348  public:
~NullRtpData()349   virtual ~NullRtpData() {}
350 
OnReceivedPayloadData(const uint8_t * payload_data,size_t payload_size,const WebRtcRTPHeader * rtp_header)351   int32_t OnReceivedPayloadData(const uint8_t* payload_data,
352                                 size_t payload_size,
353                                 const WebRtcRTPHeader* rtp_header) override {
354     return 0;
355   }
356 
OnRecoveredPacket(const uint8_t * packet,size_t packet_length)357   bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override {
358     return true;
359   }
360 };
361 
362 // Statistics about packet loss for a single directional connection. All values
363 // are totals since the connection initiated.
364 struct RtpPacketLossStats {
365   // The number of packets lost in events where no adjacent packets were also
366   // lost.
367   uint64_t single_packet_loss_count;
368   // The number of events in which more than one adjacent packet was lost.
369   uint64_t multiple_packet_loss_event_count;
370   // The number of packets lost in events where more than one adjacent packet
371   // was lost.
372   uint64_t multiple_packet_loss_packet_count;
373 };
374 
375 class RtpPacketSender {
376  public:
RtpPacketSender()377   RtpPacketSender() {}
~RtpPacketSender()378   virtual ~RtpPacketSender() {}
379 
380   enum Priority {
381     kHighPriority = 0,    // Pass through; will be sent immediately.
382     kNormalPriority = 2,  // Put in back of the line.
383     kLowPriority = 3,     // Put in back of the low priority line.
384   };
385   // Low priority packets are mixed with the normal priority packets
386   // while we are paused.
387 
388   // Returns true if we send the packet now, else it will add the packet
389   // information to the queue and call TimeToSendPacket when it's time to send.
390   virtual void InsertPacket(Priority priority,
391                             uint32_t ssrc,
392                             uint16_t sequence_number,
393                             int64_t capture_time_ms,
394                             size_t bytes,
395                             bool retransmission) = 0;
396 };
397 
398 class TransportSequenceNumberAllocator {
399  public:
TransportSequenceNumberAllocator()400   TransportSequenceNumberAllocator() {}
~TransportSequenceNumberAllocator()401   virtual ~TransportSequenceNumberAllocator() {}
402 
403   virtual uint16_t AllocateSequenceNumber() = 0;
404 };
405 
406 }  // namespace webrtc
407 #endif  // WEBRTC_MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
408