blob: 732301f6fbea5d954308772855b661d20b509d24 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <stddef.h> // size_t, ptrdiff_t
niklase@google.com470e71d2011-07-07 08:21:25 +000015
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000016#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000017#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000018#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"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
pbos@webrtc.org2f446732013-04-08 11:08:41 +000024const uint8_t kRtpMarkerBitMask = 0x80;
niklase@google.com470e71d2011-07-07 08:21:25 +000025
wu@webrtc.org822fbd82013-08-15 23:38:54 +000026RtpData* NullObjectRtpData();
27RtpFeedback* NullObjectRtpFeedback();
28RtpAudioFeedback* NullObjectRtpAudioFeedback();
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000029ReceiveStatistics* NullObjectReceiveStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
niklase@google.com470e71d2011-07-07 08:21:25 +000031namespace 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 Payload
40 {
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000041 char name[RTP_PAYLOAD_NAME_SIZE];
42 bool audio;
niklase@google.com470e71d2011-07-07 08:21:25 +000043 PayloadUnion typeSpecific;
44 };
45
pbos@webrtc.org2f446732013-04-08 11:08:41 +000046 typedef std::map<int8_t, Payload*> PayloadTypeMap;
phoglund@webrtc.org07bf43c2012-12-18 15:40:53 +000047
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000048 // Return the current RTP timestamp from the NTP timestamp
49 // returned by the specified clock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000050 uint32_t GetCurrentRTP(Clock* clock, uint32_t freq);
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000051
52 // Return the current RTP absolute timestamp.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000053 uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec,
54 uint32_t NTPfrac,
55 uint32_t freq);
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000056
pbos@webrtc.org2f446732013-04-08 11:08:41 +000057 uint32_t pow2(uint8_t exp);
niklase@google.com470e71d2011-07-07 08:21:25 +000058
marpan@webrtc.org57353a32011-12-16 17:21:09 +000059 // 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
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000066 bool StringCompare(const char* str1,
67 const char* str2,
pbos@webrtc.org2f446732013-04-08 11:08:41 +000068 const uint32_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
pbos@webrtc.org2f446732013-04-08 11:08:41 +000070 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000073
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 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +000079 uint16_t BufferToUWord16(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
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 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +000086 uint32_t BufferToUWord24(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
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 */
pbos@webrtc.org2f446732013-04-08 11:08:41 +000093 uint32_t BufferToUWord32(const uint8_t* dataBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
95 class RTPHeaderParser
96 {
97 public:
pbos@webrtc.org2f446732013-04-08 11:08:41 +000098 RTPHeaderParser(const uint8_t* rtpData,
99 const uint32_t rtpDataLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 ~RTPHeaderParser();
101
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000102 bool RTCP() const;
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000103 bool ParseRtcp(RTPHeader* header) const;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000104 bool Parse(RTPHeader& parsedPacket,
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000105 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
107 private:
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000108 void ParseOneByteExtensionHeader(
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000109 RTPHeader& parsedPacket,
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000110 const RtpHeaderExtensionMap* ptrExtensionMap,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111 const uint8_t* ptrRTPDataExtensionEnd,
112 const uint8_t* ptr) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114 uint8_t ParsePaddingBytes(
115 const uint8_t* ptrRTPDataExtensionEnd,
116 const uint8_t* ptr) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000117
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000118 const uint8_t* const _ptrRTPDataBegin;
119 const uint8_t* const _ptrRTPDataEnd;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 };
121
122 enum FrameTypes
123 {
124 kIFrame, // key frame
125 kPFrame // Delta frame
126 };
127
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 struct RTPPayloadVP8
129 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 bool nonReferenceFrame;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000131 bool beginningOfPartition;
132 int partitionID;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 bool hasPictureID;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000134 bool hasTl0PicIdx;
135 bool hasTID;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000136 bool hasKeyIdx;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000137 int pictureID;
138 int tl0PicIdx;
139 int tID;
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:06 +0000140 bool layerSync;
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000141 int keyIdx;
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000142 int frameWidth;
143 int frameHeight;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145 const uint8_t* data;
146 uint16_t dataLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 };
148
149 union RTPPayloadUnion
150 {
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 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,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000168 const uint8_t* payloadData,
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000169 // Length w/o padding.
170 const uint16_t payloadDataLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
172 ~RTPPayloadParser();
173
174 bool Parse(RTPPayload& parsedPacket) const;
175
176 private:
177 bool ParseGeneric(RTPPayload& parsedPacket) const;
178
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 bool ParseVP8(RTPPayload& parsedPacket) const;
180
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000181 int ParseVP8Extension(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000182 const uint8_t *dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000183 int dataLength) const;
184
185 int ParseVP8PictureID(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000186 const uint8_t **dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000187 int *dataLength,
188 int *parsedBytes) const;
189
190 int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000191 const uint8_t **dataPtr,
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000192 int *dataLength,
193 int *parsedBytes) const;
194
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000195 int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000196 const uint8_t **dataPtr,
henrik.lundin@webrtc.org6f2c0162011-11-24 12:52:40 +0000197 int *dataLength,
198 int *parsedBytes) const;
henrik.lundin@webrtc.org8571af72011-08-29 15:37:12 +0000199
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000200 int ParseVP8FrameSize(RTPPayload& parsedPacket,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000201 const uint8_t *dataPtr,
pwestin@webrtc.org075e91f2011-11-02 23:14:58 +0000202 int dataLength) const;
203
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000205 const uint8_t* _dataPtr;
206 const uint16_t _dataLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 const RtpVideoCodecTypes _videoType;
208 };
henrike@webrtc.orgd5657c22012-02-08 23:41:49 +0000209
210} // namespace ModuleRTPUtility
211
212} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
214#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_