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