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_INTERFACE_RTP_RTCP_DEFINES_H_
12 #define WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_RTCP_DEFINES_H_
13 
14 #include <stddef.h>
15 #include <list>
16 
17 #include "webrtc/modules/interface/module_common_types.h"
18 #include "webrtc/system_wrappers/interface/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 
28 const int kVideoPayloadTypeFrequency = 90000;
29 
30 // Minimum RTP header size in bytes.
31 const uint8_t kRtpHeaderSize = 12;
32 
33 struct AudioPayload
34 {
35     uint32_t    frequency;
36     uint8_t     channels;
37     uint32_t    rate;
38 };
39 
40 struct VideoPayload
41 {
42     RtpVideoCodecTypes   videoCodecType;
43     uint32_t       maxRate;
44 };
45 
46 union PayloadUnion
47 {
48     AudioPayload Audio;
49     VideoPayload Video;
50 };
51 
52 enum RTCPMethod
53 {
54     kRtcpOff          = 0,
55     kRtcpCompound     = 1,
56     kRtcpNonCompound = 2
57 };
58 
59 enum RTPAliveType
60 {
61     kRtpDead   = 0,
62     kRtpNoRtp = 1,
63     kRtpAlive  = 2
64 };
65 
66 enum ProtectionType {
67   kUnprotectedPacket,
68   kProtectedPacket
69 };
70 
71 enum StorageType {
72   kDontStore,
73   kDontRetransmit,
74   kAllowRetransmission
75 };
76 
77 enum RTPExtensionType {
78   kRtpExtensionNone,
79   kRtpExtensionTransmissionTimeOffset,
80   kRtpExtensionAudioLevel,
81   kRtpExtensionAbsoluteSendTime,
82   kRtpExtensionVideoRotation,
83   kRtpExtensionTransportSequenceNumber,
84   kRtpExtensionRtpStreamId,
85 };
86 
87 enum RTCPAppSubTypes
88 {
89     kAppSubtypeBwe     = 0x00
90 };
91 
92 enum RTCPPacketType
93 {
94     kRtcpReport         = 0x0001,
95     kRtcpSr             = 0x0002,
96     kRtcpRr             = 0x0004,
97     kRtcpBye            = 0x0008,
98     kRtcpPli            = 0x0010,
99     kRtcpNack           = 0x0020,
100     kRtcpFir            = 0x0040,
101     kRtcpTmmbr          = 0x0080,
102     kRtcpTmmbn          = 0x0100,
103     kRtcpSrReq          = 0x0200,
104     kRtcpXrVoipMetric   = 0x0400,
105     kRtcpApp            = 0x0800,
106     kRtcpSli            = 0x4000,
107     kRtcpRpsi           = 0x8000,
108     kRtcpRemb           = 0x10000,
109     kRtcpTransmissionTimeOffset = 0x20000,
110     kRtcpXrReceiverReferenceTime = 0x40000,
111     kRtcpXrDlrrReportBlock = 0x80000
112 };
113 
114 enum KeyFrameRequestMethod
115 {
116     kKeyFrameReqFirRtp    = 1,
117     kKeyFrameReqPliRtcp   = 2,
118     kKeyFrameReqFirRtcp   = 3
119 };
120 
121 enum RtpRtcpPacketType
122 {
123     kPacketRtp        = 0,
124     kPacketKeepAlive = 1
125 };
126 
127 enum NACKMethod
128 {
129     kNackOff      = 0,
130     kNackRtcp     = 2
131 };
132 
133 enum RetransmissionMode {
134   kRetransmitOff          = 0x0,
135   kRetransmitFECPackets   = 0x1,
136   kRetransmitBaseLayer    = 0x2,
137   kRetransmitHigherLayers = 0x4,
138   kRetransmitAllPackets   = 0xFF
139 };
140 
141 enum RtxMode {
142   kRtxOff                 = 0x0,
143   kRtxRetransmitted       = 0x1,  // Only send retransmissions over RTX.
144   kRtxRedundantPayloads   = 0x2   // Preventively send redundant payloads
145                                   // instead of padding.
146 };
147 
148 const size_t kRtxHeaderSize = 2;
149 
150 struct RTCPSenderInfo
151 {
152     uint32_t NTPseconds;
153     uint32_t NTPfraction;
154     uint32_t RTPtimeStamp;
155     uint32_t sendPacketCount;
156     uint32_t sendOctetCount;
157 };
158 
159 struct RTCPReportBlock {
160   RTCPReportBlock()
161       : remoteSSRC(0), sourceSSRC(0), fractionLost(0), cumulativeLost(0),
162         extendedHighSeqNum(0), jitter(0), lastSR(0),
163         delaySinceLastSR(0) {}
164 
165   RTCPReportBlock(uint32_t remote_ssrc,
166                   uint32_t source_ssrc,
167                   uint8_t fraction_lost,
168                   uint32_t cumulative_lost,
169                   uint32_t extended_high_sequence_number,
170                   uint32_t jitter,
171                   uint32_t last_sender_report,
172                   uint32_t delay_since_last_sender_report)
173       : remoteSSRC(remote_ssrc),
174         sourceSSRC(source_ssrc),
175         fractionLost(fraction_lost),
176         cumulativeLost(cumulative_lost),
177         extendedHighSeqNum(extended_high_sequence_number),
178         jitter(jitter),
179         lastSR(last_sender_report),
180         delaySinceLastSR(delay_since_last_sender_report) {}
181 
182   // Fields as described by RFC 3550 6.4.2.
183   uint32_t remoteSSRC;  // SSRC of sender of this report.
184   uint32_t sourceSSRC;  // SSRC of the RTP packet sender.
185   uint8_t fractionLost;
186   uint32_t cumulativeLost;  // 24 bits valid.
187   uint32_t extendedHighSeqNum;
188   uint32_t jitter;
189   uint32_t lastSR;
190   uint32_t delaySinceLastSR;
191 };
192 
193 struct RtcpReceiveTimeInfo {
194   // Fields as described by RFC 3611 4.5.
195   uint32_t sourceSSRC;
196   uint32_t lastRR;
197   uint32_t delaySinceLastRR;
198 };
199 
200 typedef std::list<RTCPReportBlock> ReportBlockList;
201 
202 struct RtpState {
203   RtpState()
204       : sequence_number(0),
205         start_timestamp(0),
206         timestamp(0),
207         capture_time_ms(-1),
208         last_timestamp_time_ms(-1),
209         media_has_been_sent(false) {}
210   uint16_t sequence_number;
211   uint32_t start_timestamp;
212   uint32_t timestamp;
213   int64_t capture_time_ms;
214   int64_t last_timestamp_time_ms;
215   bool media_has_been_sent;
216 };
217 
218 class RtpData
219 {
220 public:
221     virtual ~RtpData() {}
222 
223     virtual int32_t OnReceivedPayloadData(
224         const uint8_t* payloadData,
225         const size_t payloadSize,
226         const WebRtcRTPHeader* rtpHeader) = 0;
227 
228     virtual bool OnRecoveredPacket(const uint8_t* packet,
229                                    size_t packet_length) = 0;
230 };
231 
232 class RtpFeedback
233 {
234 public:
235     virtual ~RtpFeedback() {}
236 
237     // Receiving payload change or SSRC change. (return success!)
238     /*
239     *   channels    - number of channels in codec (1 = mono, 2 = stereo)
240     */
241     virtual int32_t OnInitializeDecoder(
242         const int32_t id,
243         const int8_t payloadType,
244         const char payloadName[RTP_PAYLOAD_NAME_SIZE],
245         const int frequency,
246         const uint8_t channels,
247         const uint32_t rate) = 0;
248 
249     virtual void OnIncomingSSRCChanged( const int32_t id,
250                                         const uint32_t ssrc) = 0;
251 
252     virtual void OnIncomingCSRCChanged( const int32_t id,
253                                         const uint32_t CSRC,
254                                         const bool added) = 0;
255 
256     virtual void ResetStatistics(uint32_t ssrc) = 0;
257 };
258 
259 class RtpAudioFeedback {
260  public:
261 
262   virtual void OnPlayTelephoneEvent(const int32_t id,
263                                     const uint8_t event,
264                                     const uint16_t lengthMs,
265                                     const uint8_t volume) = 0;
266  protected:
267   virtual ~RtpAudioFeedback() {}
268 };
269 
270 class RtcpIntraFrameObserver {
271  public:
272   virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) = 0;
273 
274   virtual void OnReceivedSLI(uint32_t ssrc,
275                              uint8_t picture_id) = 0;
276 
277   virtual void OnReceivedRPSI(uint32_t ssrc,
278                               uint64_t picture_id) = 0;
279 
280   virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) = 0;
281 
282   virtual ~RtcpIntraFrameObserver() {}
283 };
284 
285 class RtcpBandwidthObserver {
286  public:
287   // REMB or TMMBR
288   virtual void OnReceivedEstimatedBitrate(uint32_t bitrate) = 0;
289 
290   virtual void OnReceivedRtcpReceiverReport(
291       const ReportBlockList& report_blocks,
292       int64_t rtt,
293       int64_t now_ms) = 0;
294 
295   virtual ~RtcpBandwidthObserver() {}
296 };
297 
298 class RtcpRttStats {
299  public:
300   virtual void OnRttUpdate(int64_t rtt) = 0;
301 
302   virtual int64_t LastProcessedRtt() const = 0;
303 
304   virtual ~RtcpRttStats() {};
305 };
306 
307 // Null object version of RtpFeedback.
308 class NullRtpFeedback : public RtpFeedback {
309  public:
310   virtual ~NullRtpFeedback() {}
311 
312   int32_t OnInitializeDecoder(const int32_t id,
313                               const int8_t payloadType,
314                               const char payloadName[RTP_PAYLOAD_NAME_SIZE],
315                               const int frequency,
316                               const uint8_t channels,
317                               const uint32_t rate) override {
318     return 0;
319   }
320 
321   void OnIncomingSSRCChanged(const int32_t id, const uint32_t ssrc) override {}
322 
323   void OnIncomingCSRCChanged(const int32_t id,
324                              const uint32_t CSRC,
325                              const bool added) override {}
326 
327   void ResetStatistics(uint32_t ssrc) override {}
328 };
329 
330 // Null object version of RtpData.
331 class NullRtpData : public RtpData {
332  public:
333   virtual ~NullRtpData() {}
334 
335   int32_t OnReceivedPayloadData(const uint8_t* payloadData,
336                                 const size_t payloadSize,
337                                 const WebRtcRTPHeader* rtpHeader) override {
338     return 0;
339   }
340 
341   bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override {
342     return true;
343   }
344 };
345 
346 // Null object version of RtpAudioFeedback.
347 class NullRtpAudioFeedback : public RtpAudioFeedback {
348  public:
349   virtual ~NullRtpAudioFeedback() {}
350 
351   void OnPlayTelephoneEvent(const int32_t id,
352                             const uint8_t event,
353                             const uint16_t lengthMs,
354                             const uint8_t volume) override {}
355 };
356 
357 }  // namespace webrtc
358 #endif // WEBRTC_MODULES_RTP_RTCP_INTERFACE_RTP_RTCP_DEFINES_H_
359