blob: 4f35adcdfa98d88df214dbe438b9b57f28744e23 [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{
24 kRtpNoVideo = 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 a clock that reads the time as reported by the operating
65 // system. The returned instances are guaranteed to read the same
66 // times; in particular, they return relative times relative to
67 // the same base.
henrike@webrtc.orgf5da4da2012-02-15 23:54:59 +000068 // Note that even though the instances returned by this function
69 // read the same times a new object is created every time this
70 // API is called. The ownership of this object belongs to the
71 // caller.
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000072 RtpRtcpClock* GetSystemClock();
niklase@google.com470e71d2011-07-07 08:21:25 +000073
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000074 // Return the current RTP timestamp from the NTP timestamp
75 // returned by the specified clock.
76 WebRtc_UWord32 GetCurrentRTP(RtpRtcpClock* clock, WebRtc_UWord32 freq);
77
78 // Return the current RTP absolute timestamp.
79 WebRtc_UWord32 ConvertNTPTimeToRTP(WebRtc_UWord32 NTPsec,
80 WebRtc_UWord32 NTPfrac,
81 WebRtc_UWord32 freq);
82
83 // Return the time in milliseconds corresponding to the specified
84 // NTP timestamp.
85 WebRtc_UWord32 ConvertNTPTimeToMS(WebRtc_UWord32 NTPsec,
86 WebRtc_UWord32 NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88 WebRtc_UWord32 pow2(WebRtc_UWord8 exp);
89
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000090 // Returns a pointer to the payload data given a packet.
91 const WebRtc_UWord8* GetPayloadData(const WebRtcRTPHeader* rtp_header,
92 const WebRtc_UWord8* packet);
93
94 // Returns payload length given a packet.
95 WebRtc_UWord16 GetPayloadDataLength(const WebRtcRTPHeader* rtp_header,
96 const WebRtc_UWord16 packet_length);
97
marpan@webrtc.org57353a32011-12-16 17:21:09 +000098 // Returns true if |newTimestamp| is older than |existingTimestamp|.
99 // |wrapped| will be set to true if there has been a wraparound between the
100 // two timestamps.
101 bool OldTimestamp(uint32_t newTimestamp,
102 uint32_t existingTimestamp,
103 bool* wrapped);
104
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000105 bool StringCompare(const char* str1,
106 const char* str2,
marpan@webrtc.org57353a32011-12-16 17:21:09 +0000107 const WebRtc_UWord32 length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
109 void AssignUWord32ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
110 void AssignUWord24ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
111 void AssignUWord16ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord16 value);
112
113 /**
114 * Converts a network-ordered two-byte input buffer to a host-ordered value.
115 * \param[in] dataBuffer Network-ordered two-byte buffer to convert.
116 * \return Host-ordered value.
117 */
118 WebRtc_UWord16 BufferToUWord16(const WebRtc_UWord8* dataBuffer);
119
120 /**
121 * Converts a network-ordered three-byte input buffer to a host-ordered value.
122 * \param[in] dataBuffer Network-ordered three-byte buffer to convert.
123 * \return Host-ordered value.
124 */
125 WebRtc_UWord32 BufferToUWord24(const WebRtc_UWord8* dataBuffer);
126
127 /**
128 * Converts a network-ordered four-byte input buffer to a host-ordered value.
129 * \param[in] dataBuffer Network-ordered four-byte buffer to convert.
130 * \return Host-ordered value.
131 */
132 WebRtc_UWord32 BufferToUWord32(const WebRtc_UWord8* dataBuffer);
133
134 class RTPHeaderParser
135 {
136 public:
137 RTPHeaderParser(const WebRtc_UWord8* rtpData,
138 const WebRtc_UWord32 rtpDataLength);
139 ~RTPHeaderParser();
140
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141 bool RTCP() const;
142 bool Parse(WebRtcRTPHeader& parsedPacket,
143 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
145 private:
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000146 void ParseOneByteExtensionHeader(
147 WebRtcRTPHeader& parsedPacket,
148 const RtpHeaderExtensionMap* ptrExtensionMap,
149 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
150 const WebRtc_UWord8* ptr) const;
151
152 WebRtc_UWord8 ParsePaddingBytes(
153 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
154 const WebRtc_UWord8* ptr) const;
155
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 const WebRtc_UWord8* const _ptrRTPDataBegin;
157 const WebRtc_UWord8* const _ptrRTPDataEnd;
158 };
159
160 enum FrameTypes
161 {
162 kIFrame, // key frame
163 kPFrame // Delta frame
164 };
165
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 struct RTPPayloadVP8
167 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 bool nonReferenceFrame;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000169 bool beginningOfPartition;
170 int partitionID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 bool hasPictureID;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000172 bool hasTl0PicIdx;
173 bool hasTID;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000174 bool hasKeyIdx;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000175 int pictureID;
176 int tl0PicIdx;
177 int tID;
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +0000178 bool layerSync;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000179 int keyIdx;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000180 int frameWidth;
181 int frameHeight;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +0000183 const WebRtc_UWord8* data;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184 WebRtc_UWord16 dataLength;
185 };
186
187 union RTPPayloadUnion
188 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 RTPPayloadVP8 VP8;
190 };
191
192 struct RTPPayload
193 {
194 void SetType(RtpVideoCodecTypes videoType);
195
196 RtpVideoCodecTypes type;
197 FrameTypes frameType;
198 RTPPayloadUnion info;
199 };
200
201 // RTP payload parser
202 class RTPPayloadParser
203 {
204 public:
205 RTPPayloadParser(const RtpVideoCodecTypes payloadType,
206 const WebRtc_UWord8* payloadData,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000207 const WebRtc_UWord16 payloadDataLength, // Length w/o padding.
208 const WebRtc_Word32 id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
210 ~RTPPayloadParser();
211
212 bool Parse(RTPPayload& parsedPacket) const;
213
214 private:
215 bool ParseGeneric(RTPPayload& parsedPacket) const;
216
niklase@google.com470e71d2011-07-07 08:21:25 +0000217 bool ParseVP8(RTPPayload& parsedPacket) const;
218
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000219 int ParseVP8Extension(RTPPayloadVP8 *vp8,
220 const WebRtc_UWord8 *dataPtr,
221 int dataLength) const;
222
223 int ParseVP8PictureID(RTPPayloadVP8 *vp8,
224 const WebRtc_UWord8 **dataPtr,
225 int *dataLength,
226 int *parsedBytes) const;
227
228 int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
229 const WebRtc_UWord8 **dataPtr,
230 int *dataLength,
231 int *parsedBytes) const;
232
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000233 int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
234 const WebRtc_UWord8 **dataPtr,
235 int *dataLength,
236 int *parsedBytes) const;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000237
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000238 int ParseVP8FrameSize(RTPPayload& parsedPacket,
239 const WebRtc_UWord8 *dataPtr,
240 int dataLength) const;
241
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 private:
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000243 WebRtc_Word32 _id;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 const WebRtc_UWord8* _dataPtr;
245 const WebRtc_UWord16 _dataLength;
246 const RtpVideoCodecTypes _videoType;
247 };
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000248
249} // namespace ModuleRTPUtility
250
251} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
253#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_