blob: 28670f98c9d05f41de7ab4545e8ffe3f774d1cbd [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
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000016#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
19#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
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
pbos@webrtc.org2f446732013-04-08 11:08:41 +000029const uint8_t kRtpMarkerBitMask = 0x80;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
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 {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000041 uint32_t frequency;
42 uint8_t channels;
43 uint32_t rate;
niklase@google.com470e71d2011-07-07 08:21:25 +000044 };
45 struct VideoPayload
46 {
47 RtpVideoCodecTypes videoCodecType;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000048 uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +000062 typedef std::map<int8_t, Payload*> PayloadTypeMap;
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000063
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000064 // Return the current RTP timestamp from the NTP timestamp
65 // returned by the specified clock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 uint32_t GetCurrentRTP(Clock* clock, uint32_t freq);
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000067
68 // Return the current RTP absolute timestamp.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec,
70 uint32_t NTPfrac,
71 uint32_t freq);
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000072
pbos@webrtc.org2f446732013-04-08 11:08:41 +000073 uint32_t pow2(uint8_t exp);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000075 // Returns a pointer to the payload data given a packet.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000076 const uint8_t* GetPayloadData(const RTPHeader& rtp_header,
pbos@webrtc.org2f446732013-04-08 11:08:41 +000077 const uint8_t* packet);
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000078
79 // Returns payload length given a packet.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000080 uint16_t GetPayloadDataLength(const RTPHeader& rtp_header,
pbos@webrtc.org2f446732013-04-08 11:08:41 +000081 const uint16_t packet_length);
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000082
marpan@webrtc.org57353a32011-12-16 17:21:09 +000083 // Returns true if |newTimestamp| is older than |existingTimestamp|.
84 // |wrapped| will be set to true if there has been a wraparound between the
85 // two timestamps.
86 bool OldTimestamp(uint32_t newTimestamp,
87 uint32_t existingTimestamp,
88 bool* wrapped);
89
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000090 bool StringCompare(const char* str1,
91 const char* str2,
pbos@webrtc.org2f446732013-04-08 11:08:41 +000092 const uint32_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +000093
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 void AssignUWord32ToBuffer(uint8_t* dataBuffer, uint32_t value);
95 void AssignUWord24ToBuffer(uint8_t* dataBuffer, uint32_t value);
96 void AssignUWord16ToBuffer(uint8_t* dataBuffer, uint16_t value);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
98 /**
99 * Converts a network-ordered two-byte input buffer to a host-ordered value.
100 * \param[in] dataBuffer Network-ordered two-byte buffer to convert.
101 * \return Host-ordered value.
102 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000103 uint16_t BufferToUWord16(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
105 /**
106 * Converts a network-ordered three-byte input buffer to a host-ordered value.
107 * \param[in] dataBuffer Network-ordered three-byte buffer to convert.
108 * \return Host-ordered value.
109 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110 uint32_t BufferToUWord24(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
112 /**
113 * Converts a network-ordered four-byte input buffer to a host-ordered value.
114 * \param[in] dataBuffer Network-ordered four-byte buffer to convert.
115 * \return Host-ordered value.
116 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000117 uint32_t BufferToUWord32(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
119 class RTPHeaderParser
120 {
121 public:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122 RTPHeaderParser(const uint8_t* rtpData,
123 const uint32_t rtpDataLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 ~RTPHeaderParser();
125
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000126 bool RTCP() const;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000127 bool Parse(RTPHeader& parsedPacket,
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000128 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
130 private:
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000131 void ParseOneByteExtensionHeader(
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000132 RTPHeader& parsedPacket,
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000133 const RtpHeaderExtensionMap* ptrExtensionMap,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000134 const uint8_t* ptrRTPDataExtensionEnd,
135 const uint8_t* ptr) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000136
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000137 uint8_t ParsePaddingBytes(
138 const uint8_t* ptrRTPDataExtensionEnd,
139 const uint8_t* ptr) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000140
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000141 const uint8_t* const _ptrRTPDataBegin;
142 const uint8_t* const _ptrRTPDataEnd;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 };
144
145 enum FrameTypes
146 {
147 kIFrame, // key frame
148 kPFrame // Delta frame
149 };
150
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 struct RTPPayloadVP8
152 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 bool nonReferenceFrame;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000154 bool beginningOfPartition;
155 int partitionID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 bool hasPictureID;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000157 bool hasTl0PicIdx;
158 bool hasTID;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000159 bool hasKeyIdx;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000160 int pictureID;
161 int tl0PicIdx;
162 int tID;
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +0000163 bool layerSync;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000164 int keyIdx;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000165 int frameWidth;
166 int frameHeight;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000168 const uint8_t* data;
169 uint16_t dataLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170 };
171
172 union RTPPayloadUnion
173 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 RTPPayloadVP8 VP8;
175 };
176
177 struct RTPPayload
178 {
179 void SetType(RtpVideoCodecTypes videoType);
180
181 RtpVideoCodecTypes type;
182 FrameTypes frameType;
183 RTPPayloadUnion info;
184 };
185
186 // RTP payload parser
187 class RTPPayloadParser
188 {
189 public:
190 RTPPayloadParser(const RtpVideoCodecTypes payloadType,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000191 const uint8_t* payloadData,
192 const uint16_t payloadDataLength, // Length w/o padding.
193 const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
195 ~RTPPayloadParser();
196
197 bool Parse(RTPPayload& parsedPacket) const;
198
199 private:
200 bool ParseGeneric(RTPPayload& parsedPacket) const;
201
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 bool ParseVP8(RTPPayload& parsedPacket) const;
203
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000204 int ParseVP8Extension(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000205 const uint8_t *dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000206 int dataLength) const;
207
208 int ParseVP8PictureID(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000209 const uint8_t **dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000210 int *dataLength,
211 int *parsedBytes) const;
212
213 int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214 const uint8_t **dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000215 int *dataLength,
216 int *parsedBytes) const;
217
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000218 int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000219 const uint8_t **dataPtr,
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000220 int *dataLength,
221 int *parsedBytes) const;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000222
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000223 int ParseVP8FrameSize(RTPPayload& parsedPacket,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224 const uint8_t *dataPtr,
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000225 int dataLength) const;
226
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000228 int32_t _id;
229 const uint8_t* _dataPtr;
230 const uint16_t _dataLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 const RtpVideoCodecTypes _videoType;
232 };
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000233
234} // namespace ModuleRTPUtility
235
236} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000237
238#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_