blob: 3927fdfec703802066ca2c94f41bd527056f83ea [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;
43 WebRtc_UWord8 bitsPerSample;
44 WebRtc_UWord32 rate;
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +000045 bool trueStereoCodec;
niklase@google.com470e71d2011-07-07 08:21:25 +000046 };
47 struct VideoPayload
48 {
49 RtpVideoCodecTypes videoCodecType;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +000050 WebRtc_UWord32 maxRate;
niklase@google.com470e71d2011-07-07 08:21:25 +000051 };
52 union PayloadUnion
53 {
54 AudioPayload Audio;
55 VideoPayload Video;
56 };
57 struct Payload
58 {
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000059 char name[RTP_PAYLOAD_NAME_SIZE];
60 bool audio;
niklase@google.com470e71d2011-07-07 08:21:25 +000061 PayloadUnion typeSpecific;
62 };
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.
68 RtpRtcpClock* GetSystemClock();
niklase@google.com470e71d2011-07-07 08:21:25 +000069
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000070 // Return the current RTP timestamp from the NTP timestamp
71 // returned by the specified clock.
72 WebRtc_UWord32 GetCurrentRTP(RtpRtcpClock* clock, WebRtc_UWord32 freq);
73
74 // Return the current RTP absolute timestamp.
75 WebRtc_UWord32 ConvertNTPTimeToRTP(WebRtc_UWord32 NTPsec,
76 WebRtc_UWord32 NTPfrac,
77 WebRtc_UWord32 freq);
78
79 // Return the time in milliseconds corresponding to the specified
80 // NTP timestamp.
81 WebRtc_UWord32 ConvertNTPTimeToMS(WebRtc_UWord32 NTPsec,
82 WebRtc_UWord32 NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
84 WebRtc_UWord32 pow2(WebRtc_UWord8 exp);
85
marpan@webrtc.org57353a32011-12-16 17:21:09 +000086 // Returns true if |newTimestamp| is older than |existingTimestamp|.
87 // |wrapped| will be set to true if there has been a wraparound between the
88 // two timestamps.
89 bool OldTimestamp(uint32_t newTimestamp,
90 uint32_t existingTimestamp,
91 bool* wrapped);
92
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000093 bool StringCompare(const char* str1,
94 const char* str2,
marpan@webrtc.org57353a32011-12-16 17:21:09 +000095 const WebRtc_UWord32 length);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
97 void AssignUWord32ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
98 void AssignUWord24ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value);
99 void AssignUWord16ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord16 value);
100
101 /**
102 * Converts a network-ordered two-byte input buffer to a host-ordered value.
103 * \param[in] dataBuffer Network-ordered two-byte buffer to convert.
104 * \return Host-ordered value.
105 */
106 WebRtc_UWord16 BufferToUWord16(const WebRtc_UWord8* dataBuffer);
107
108 /**
109 * Converts a network-ordered three-byte input buffer to a host-ordered value.
110 * \param[in] dataBuffer Network-ordered three-byte buffer to convert.
111 * \return Host-ordered value.
112 */
113 WebRtc_UWord32 BufferToUWord24(const WebRtc_UWord8* dataBuffer);
114
115 /**
116 * Converts a network-ordered four-byte input buffer to a host-ordered value.
117 * \param[in] dataBuffer Network-ordered four-byte buffer to convert.
118 * \return Host-ordered value.
119 */
120 WebRtc_UWord32 BufferToUWord32(const WebRtc_UWord8* dataBuffer);
121
122 class RTPHeaderParser
123 {
124 public:
125 RTPHeaderParser(const WebRtc_UWord8* rtpData,
126 const WebRtc_UWord32 rtpDataLength);
127 ~RTPHeaderParser();
128
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000129 bool RTCP() const;
130 bool Parse(WebRtcRTPHeader& parsedPacket,
131 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
133 private:
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000134 void ParseOneByteExtensionHeader(
135 WebRtcRTPHeader& parsedPacket,
136 const RtpHeaderExtensionMap* ptrExtensionMap,
137 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
138 const WebRtc_UWord8* ptr) const;
139
140 WebRtc_UWord8 ParsePaddingBytes(
141 const WebRtc_UWord8* ptrRTPDataExtensionEnd,
142 const WebRtc_UWord8* ptr) const;
143
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 const WebRtc_UWord8* const _ptrRTPDataBegin;
145 const WebRtc_UWord8* const _ptrRTPDataEnd;
146 };
147
148 enum FrameTypes
149 {
150 kIFrame, // key frame
151 kPFrame // Delta frame
152 };
153
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 struct RTPPayloadVP8
155 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 bool nonReferenceFrame;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000157 bool beginningOfPartition;
158 int partitionID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159 bool hasPictureID;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000160 bool hasTl0PicIdx;
161 bool hasTID;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000162 bool hasKeyIdx;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000163 int pictureID;
164 int tl0PicIdx;
165 int tID;
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +0000166 bool layerSync;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000167 int keyIdx;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000168 int frameWidth;
169 int frameHeight;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000171 const WebRtc_UWord8* data;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 WebRtc_UWord16 dataLength;
173 };
174
175 union RTPPayloadUnion
176 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 RTPPayloadVP8 VP8;
178 };
179
180 struct RTPPayload
181 {
182 void SetType(RtpVideoCodecTypes videoType);
183
184 RtpVideoCodecTypes type;
185 FrameTypes frameType;
186 RTPPayloadUnion info;
187 };
188
189 // RTP payload parser
190 class RTPPayloadParser
191 {
192 public:
193 RTPPayloadParser(const RtpVideoCodecTypes payloadType,
194 const WebRtc_UWord8* payloadData,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000195 const WebRtc_UWord16 payloadDataLength, // Length w/o padding.
196 const WebRtc_Word32 id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
198 ~RTPPayloadParser();
199
200 bool Parse(RTPPayload& parsedPacket) const;
201
202 private:
203 bool ParseGeneric(RTPPayload& parsedPacket) const;
204
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 bool ParseVP8(RTPPayload& parsedPacket) const;
206
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000207 int ParseVP8Extension(RTPPayloadVP8 *vp8,
208 const WebRtc_UWord8 *dataPtr,
209 int dataLength) const;
210
211 int ParseVP8PictureID(RTPPayloadVP8 *vp8,
212 const WebRtc_UWord8 **dataPtr,
213 int *dataLength,
214 int *parsedBytes) const;
215
216 int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
217 const WebRtc_UWord8 **dataPtr,
218 int *dataLength,
219 int *parsedBytes) const;
220
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000221 int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
222 const WebRtc_UWord8 **dataPtr,
223 int *dataLength,
224 int *parsedBytes) const;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000225
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000226 int ParseVP8FrameSize(RTPPayload& parsedPacket,
227 const WebRtc_UWord8 *dataPtr,
228 int dataLength) const;
229
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 private:
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000231 WebRtc_Word32 _id;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 const WebRtc_UWord8* _dataPtr;
233 const WebRtc_UWord16 _dataLength;
234 const RtpVideoCodecTypes _videoType;
235 };
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000236
237} // namespace ModuleRTPUtility
238
239} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
241#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_