• 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 WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_
13 
14 #include <stddef.h> // size_t, ptrdiff_t
15 
16 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
17 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
18 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
19 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
20 #include "webrtc/typedefs.h"
21 
22 namespace webrtc {
23 
24 const uint8_t kRtpMarkerBitMask = 0x80;
25 
26 RtpData* NullObjectRtpData();
27 RtpFeedback* NullObjectRtpFeedback();
28 RtpAudioFeedback* NullObjectRtpAudioFeedback();
29 ReceiveStatistics* NullObjectReceiveStatistics();
30 
31 namespace ModuleRTPUtility
32 {
33     // January 1970, in NTP seconds.
34     const uint32_t NTP_JAN_1970 = 2208988800UL;
35 
36     // Magic NTP fractional unit.
37     const double NTP_FRAC = 4.294967296E+9;
38 
39     struct Payload
40     {
41         char name[RTP_PAYLOAD_NAME_SIZE];
42         bool audio;
43         PayloadUnion typeSpecific;
44     };
45 
46     typedef std::map<int8_t, Payload*> PayloadTypeMap;
47 
48     // Return the current RTP timestamp from the NTP timestamp
49     // returned by the specified clock.
50     uint32_t GetCurrentRTP(Clock* clock, uint32_t freq);
51 
52     // Return the current RTP absolute timestamp.
53     uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec,
54                                  uint32_t NTPfrac,
55                                  uint32_t freq);
56 
57     uint32_t pow2(uint8_t exp);
58 
59     // Returns true if |newTimestamp| is older than |existingTimestamp|.
60     // |wrapped| will be set to true if there has been a wraparound between the
61     // two timestamps.
62     bool OldTimestamp(uint32_t newTimestamp,
63                       uint32_t existingTimestamp,
64                       bool* wrapped);
65 
66     bool StringCompare(const char* str1,
67                        const char* str2,
68                        const uint32_t length);
69 
70     void AssignUWord32ToBuffer(uint8_t* dataBuffer, uint32_t value);
71     void AssignUWord24ToBuffer(uint8_t* dataBuffer, uint32_t value);
72     void AssignUWord16ToBuffer(uint8_t* dataBuffer, uint16_t value);
73 
74     /**
75      * Converts a network-ordered two-byte input buffer to a host-ordered value.
76      * \param[in] dataBuffer Network-ordered two-byte buffer to convert.
77      * \return Host-ordered value.
78      */
79     uint16_t BufferToUWord16(const uint8_t* dataBuffer);
80 
81     /**
82      * Converts a network-ordered three-byte input buffer to a host-ordered value.
83      * \param[in] dataBuffer Network-ordered three-byte buffer to convert.
84      * \return Host-ordered value.
85      */
86     uint32_t BufferToUWord24(const uint8_t* dataBuffer);
87 
88     /**
89      * Converts a network-ordered four-byte input buffer to a host-ordered value.
90      * \param[in] dataBuffer Network-ordered four-byte buffer to convert.
91      * \return Host-ordered value.
92      */
93     uint32_t BufferToUWord32(const uint8_t* dataBuffer);
94 
95     class RTPHeaderParser
96     {
97     public:
98         RTPHeaderParser(const uint8_t* rtpData,
99                         const uint32_t rtpDataLength);
100         ~RTPHeaderParser();
101 
102         bool RTCP() const;
103         bool ParseRtcp(RTPHeader* header) const;
104         bool Parse(RTPHeader& parsedPacket,
105                    RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
106 
107     private:
108         void ParseOneByteExtensionHeader(
109             RTPHeader& parsedPacket,
110             const RtpHeaderExtensionMap* ptrExtensionMap,
111             const uint8_t* ptrRTPDataExtensionEnd,
112             const uint8_t* ptr) const;
113 
114         uint8_t ParsePaddingBytes(
115             const uint8_t* ptrRTPDataExtensionEnd,
116             const uint8_t* ptr) const;
117 
118         const uint8_t* const _ptrRTPDataBegin;
119         const uint8_t* const _ptrRTPDataEnd;
120     };
121 
122     enum FrameTypes
123     {
124         kIFrame,    // key frame
125         kPFrame         // Delta frame
126     };
127 
128     struct RTPPayloadVP8
129     {
130         bool                 nonReferenceFrame;
131         bool                 beginningOfPartition;
132         int                  partitionID;
133         bool                 hasPictureID;
134         bool                 hasTl0PicIdx;
135         bool                 hasTID;
136         bool                 hasKeyIdx;
137         int                  pictureID;
138         int                  tl0PicIdx;
139         int                  tID;
140         bool                 layerSync;
141         int                  keyIdx;
142         int                  frameWidth;
143         int                  frameHeight;
144 
145         const uint8_t*   data;
146         uint16_t         dataLength;
147     };
148 
149     union RTPPayloadUnion
150     {
151         RTPPayloadVP8   VP8;
152     };
153 
154     struct RTPPayload
155     {
156         void SetType(RtpVideoCodecTypes videoType);
157 
158         RtpVideoCodecTypes  type;
159         FrameTypes          frameType;
160         RTPPayloadUnion     info;
161     };
162 
163     // RTP payload parser
164     class RTPPayloadParser
165     {
166     public:
167         RTPPayloadParser(const RtpVideoCodecTypes payloadType,
168                          const uint8_t* payloadData,
169                          // Length w/o padding.
170                          const uint16_t payloadDataLength);
171 
172         ~RTPPayloadParser();
173 
174         bool Parse(RTPPayload& parsedPacket) const;
175 
176     private:
177         bool ParseGeneric(RTPPayload& parsedPacket) const;
178 
179         bool ParseVP8(RTPPayload& parsedPacket) const;
180 
181         int ParseVP8Extension(RTPPayloadVP8 *vp8,
182                               const uint8_t *dataPtr,
183                               int dataLength) const;
184 
185         int ParseVP8PictureID(RTPPayloadVP8 *vp8,
186                               const uint8_t **dataPtr,
187                               int *dataLength,
188                               int *parsedBytes) const;
189 
190         int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
191                               const uint8_t **dataPtr,
192                               int *dataLength,
193                               int *parsedBytes) const;
194 
195         int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
196                                  const uint8_t **dataPtr,
197                                  int *dataLength,
198                                  int *parsedBytes) const;
199 
200         int ParseVP8FrameSize(RTPPayload& parsedPacket,
201                               const uint8_t *dataPtr,
202                               int dataLength) const;
203 
204     private:
205         const uint8_t*        _dataPtr;
206         const uint16_t        _dataLength;
207         const RtpVideoCodecTypes    _videoType;
208     };
209 
210 }  // namespace ModuleRTPUtility
211 
212 }  // namespace webrtc
213 
214 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_
215