blob: 255c0802a30774a66e94199ad267fb819de88015 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 <cstddef> // size_t, ptrdiff_t
15
16#include "typedefs.h"
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000017#include "rtp_header_extension.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018#include "rtp_rtcp_config.h"
19#include "rtp_rtcp_defines.h"
20
21namespace webrtc {
22enum RtpVideoCodecTypes
23{
pbos@webrtc.org8911ce42013-03-18 16:39:03 +000024 kRtpGenericVideo = 0,
niklase@google.com470e71d2011-07-07 08:21:25 +000025 kRtpFecVideo = 10,
26 kRtpVp8Video = 11
27};
28
29const WebRtc_UWord8 kRtpMarkerBitMask = 0x80;
30
31namespace ModuleRTPUtility
32{
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000033 // 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
niklase@google.com470e71d2011-07-07 08:21:25 +000039 struct AudioPayload
40 {
41 WebRtc_UWord32 frequency;
42 WebRtc_UWord8 channels;
niklase@google.com470e71d2011-07-07 08:21:25 +000043 WebRtc_UWord32 rate;
44 };
45 struct VideoPayload
46 {
47 RtpVideoCodecTypes videoCodecType;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +000048 WebRtc_UWord32 maxRate;
niklase@google.com470e71d2011-07-07 08:21:25 +000049 };
50 union PayloadUnion
51 {
52 AudioPayload Audio;
53 VideoPayload Video;
54 };
55 struct Payload
56 {
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000057 char name[RTP_PAYLOAD_NAME_SIZE];
58 bool audio;
niklase@google.com470e71d2011-07-07 08:21:25 +000059 PayloadUnion typeSpecific;
60 };
61
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000062 typedef std::map<WebRtc_Word8, Payload*> PayloadTypeMap;
63
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000064 // Return the current RTP timestamp from the NTP timestamp
65 // returned by the specified clock.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000066 WebRtc_UWord32 GetCurrentRTP(Clock* clock, WebRtc_UWord32 freq);
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000067
68 // Return the current RTP absolute timestamp.
69 WebRtc_UWord32 ConvertNTPTimeToRTP(WebRtc_UWord32 NTPsec,
70 WebRtc_UWord32 NTPfrac,
71 WebRtc_UWord32 freq);
72
73 // Return the time in milliseconds corresponding to the specified
74 // NTP timestamp.
75 WebRtc_UWord32 ConvertNTPTimeToMS(WebRtc_UWord32 NTPsec,
76 WebRtc_UWord32 NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
78 WebRtc_UWord32 pow2(WebRtc_UWord8 exp);
79
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000080 // Returns a pointer to the payload data given a packet.
81 const WebRtc_UWord8* GetPayloadData(const WebRtcRTPHeader* rtp_header,
82 const WebRtc_UWord8* packet);
83
84 // Returns payload length given a packet.
85 WebRtc_UWord16 GetPayloadDataLength(const WebRtcRTPHeader* rtp_header,
86 const WebRtc_UWord16 packet_length);
87
marpan@webrtc.org57353a32011-12-16 17:21:09 +000088 // Returns true if |newTimestamp| is older than |existingTimestamp|.
89 // |wrapped| will be set to true if there has been a wraparound between the
90 // two timestamps.
91 bool OldTimestamp(uint32_t newTimestamp,
92 uint32_t existingTimestamp,
93 bool* wrapped);
94
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000095 bool StringCompare(const char* str1,
96 const char* str2,
marpan@webrtc.org57353a32011-12-16 17:21:09 +000097 const WebRtc_UWord32 length);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
99 void AssignUWord32ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
100 void AssignUWord24ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
101 void AssignUWord16ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord16 value);
102
103 /**
104 * Converts a network-ordered two-byte input buffer to a host-ordered value.
105 * \param[in] dataBuffer Network-ordered two-byte buffer to convert.
106 * \return Host-ordered value.
107 */
108 WebRtc_UWord16 BufferToUWord16(const WebRtc_UWord8* dataBuffer);
109
110 /**
111 * Converts a network-ordered three-byte input buffer to a host-ordered value.
112 * \param[in] dataBuffer Network-ordered three-byte buffer to convert.
113 * \return Host-ordered value.
114 */
115 WebRtc_UWord32 BufferToUWord24(const WebRtc_UWord8* dataBuffer);
116
117 /**
118 * Converts a network-ordered four-byte input buffer to a host-ordered value.
119 * \param[in] dataBuffer Network-ordered four-byte buffer to convert.
120 * \return Host-ordered value.
121 */
122 WebRtc_UWord32 BufferToUWord32(const WebRtc_UWord8* dataBuffer);
123
124 class RTPHeaderParser
125 {
126 public:
127 RTPHeaderParser(const WebRtc_UWord8* rtpData,
128 const WebRtc_UWord32 rtpDataLength);
129 ~RTPHeaderParser();
130
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000131 bool RTCP() const;
132 bool Parse(WebRtcRTPHeader& parsedPacket,
133 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
135 private:
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000136 void ParseOneByteExtensionHeader(
137 WebRtcRTPHeader& parsedPacket,
138 const RtpHeaderExtensionMap* ptrExtensionMap,
139 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
140 const WebRtc_UWord8* ptr) const;
141
142 WebRtc_UWord8 ParsePaddingBytes(
143 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
144 const WebRtc_UWord8* ptr) const;
145
niklase@google.com470e71d2011-07-07 08:21:25 +0000146 const WebRtc_UWord8* const _ptrRTPDataBegin;
147 const WebRtc_UWord8* const _ptrRTPDataEnd;
148 };
149
150 enum FrameTypes
151 {
152 kIFrame, // key frame
153 kPFrame // Delta frame
154 };
155
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 struct RTPPayloadVP8
157 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 bool nonReferenceFrame;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000159 bool beginningOfPartition;
160 int partitionID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 bool hasPictureID;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000162 bool hasTl0PicIdx;
163 bool hasTID;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000164 bool hasKeyIdx;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000165 int pictureID;
166 int tl0PicIdx;
167 int tID;
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +0000168 bool layerSync;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000169 int keyIdx;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000170 int frameWidth;
171 int frameHeight;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +0000173 const WebRtc_UWord8* data;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 WebRtc_UWord16 dataLength;
175 };
176
177 union RTPPayloadUnion
178 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 RTPPayloadVP8 VP8;
180 };
181
182 struct RTPPayload
183 {
184 void SetType(RtpVideoCodecTypes videoType);
185
186 RtpVideoCodecTypes type;
187 FrameTypes frameType;
188 RTPPayloadUnion info;
189 };
190
191 // RTP payload parser
192 class RTPPayloadParser
193 {
194 public:
195 RTPPayloadParser(const RtpVideoCodecTypes payloadType,
196 const WebRtc_UWord8* payloadData,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000197 const WebRtc_UWord16 payloadDataLength, // Length w/o padding.
198 const WebRtc_Word32 id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
200 ~RTPPayloadParser();
201
202 bool Parse(RTPPayload& parsedPacket) const;
203
204 private:
205 bool ParseGeneric(RTPPayload& parsedPacket) const;
206
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 bool ParseVP8(RTPPayload& parsedPacket) const;
208
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000209 int ParseVP8Extension(RTPPayloadVP8 *vp8,
210 const WebRtc_UWord8 *dataPtr,
211 int dataLength) const;
212
213 int ParseVP8PictureID(RTPPayloadVP8 *vp8,
214 const WebRtc_UWord8 **dataPtr,
215 int *dataLength,
216 int *parsedBytes) const;
217
218 int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
219 const WebRtc_UWord8 **dataPtr,
220 int *dataLength,
221 int *parsedBytes) const;
222
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000223 int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
224 const WebRtc_UWord8 **dataPtr,
225 int *dataLength,
226 int *parsedBytes) const;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000227
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000228 int ParseVP8FrameSize(RTPPayload& parsedPacket,
229 const WebRtc_UWord8 *dataPtr,
230 int dataLength) const;
231
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 private:
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000233 WebRtc_Word32 _id;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 const WebRtc_UWord8* _dataPtr;
235 const WebRtc_UWord16 _dataLength;
236 const RtpVideoCodecTypes _videoType;
237 };
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000238
239} // namespace ModuleRTPUtility
240
241} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
243#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_