niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 16 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
tnakamura@webrtc.org | aa4d96a | 2013-07-16 19:25:04 +0000 | [diff] [blame^] | 22 | enum RtpVideoCodecTypes |
| 23 | { |
| 24 | kRtpGenericVideo = 0, |
| 25 | kRtpFecVideo = 10, |
| 26 | kRtpVp8Video = 11 |
| 27 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 29 | const uint8_t kRtpMarkerBitMask = 0x80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
| 31 | namespace ModuleRTPUtility |
| 32 | { |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 33 | // 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 | |
tnakamura@webrtc.org | aa4d96a | 2013-07-16 19:25:04 +0000 | [diff] [blame^] | 39 | struct AudioPayload |
| 40 | { |
| 41 | uint32_t frequency; |
| 42 | uint8_t channels; |
| 43 | uint32_t rate; |
| 44 | }; |
| 45 | struct VideoPayload |
| 46 | { |
| 47 | RtpVideoCodecTypes videoCodecType; |
| 48 | uint32_t maxRate; |
| 49 | }; |
| 50 | union PayloadUnion |
| 51 | { |
| 52 | AudioPayload Audio; |
| 53 | VideoPayload Video; |
| 54 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | struct Payload |
| 56 | { |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 57 | char name[RTP_PAYLOAD_NAME_SIZE]; |
| 58 | bool audio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | PayloadUnion typeSpecific; |
| 60 | }; |
| 61 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 62 | typedef std::map<int8_t, Payload*> PayloadTypeMap; |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 63 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 64 | // Return the current RTP timestamp from the NTP timestamp |
| 65 | // returned by the specified clock. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 66 | uint32_t GetCurrentRTP(Clock* clock, uint32_t freq); |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 67 | |
| 68 | // Return the current RTP absolute timestamp. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 69 | uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec, |
| 70 | uint32_t NTPfrac, |
| 71 | uint32_t freq); |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 72 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 73 | uint32_t pow2(uint8_t exp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 75 | // Returns a pointer to the payload data given a packet. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 76 | const uint8_t* GetPayloadData(const RTPHeader& rtp_header, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 77 | const uint8_t* packet); |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 78 | |
| 79 | // Returns payload length given a packet. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 80 | uint16_t GetPayloadDataLength(const RTPHeader& rtp_header, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 81 | const uint16_t packet_length); |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 82 | |
marpan@webrtc.org | 57353a3 | 2011-12-16 17:21:09 +0000 | [diff] [blame] | 83 | // 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.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 90 | bool StringCompare(const char* str1, |
| 91 | const char* str2, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 92 | const uint32_t length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 94 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
| 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.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 103 | uint16_t BufferToUWord16(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | |
| 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.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 110 | uint32_t BufferToUWord24(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
| 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.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 117 | uint32_t BufferToUWord32(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
| 119 | class RTPHeaderParser |
| 120 | { |
| 121 | public: |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 122 | RTPHeaderParser(const uint8_t* rtpData, |
| 123 | const uint32_t rtpDataLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | ~RTPHeaderParser(); |
| 125 | |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 126 | bool RTCP() const; |
solenberg@webrtc.org | a5fd2f1 | 2013-06-26 08:36:07 +0000 | [diff] [blame] | 127 | bool ParseRtcp(RTPHeader* header) const; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 128 | bool Parse(RTPHeader& parsedPacket, |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 129 | RtpHeaderExtensionMap* ptrExtensionMap = NULL) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
| 131 | private: |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 132 | void ParseOneByteExtensionHeader( |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 133 | RTPHeader& parsedPacket, |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 134 | const RtpHeaderExtensionMap* ptrExtensionMap, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 135 | const uint8_t* ptrRTPDataExtensionEnd, |
| 136 | const uint8_t* ptr) const; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 137 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 138 | uint8_t ParsePaddingBytes( |
| 139 | const uint8_t* ptrRTPDataExtensionEnd, |
| 140 | const uint8_t* ptr) const; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 141 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 142 | const uint8_t* const _ptrRTPDataBegin; |
| 143 | const uint8_t* const _ptrRTPDataEnd; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | enum FrameTypes |
| 147 | { |
| 148 | kIFrame, // key frame |
| 149 | kPFrame // Delta frame |
| 150 | }; |
| 151 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | struct RTPPayloadVP8 |
| 153 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | bool nonReferenceFrame; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 155 | bool beginningOfPartition; |
| 156 | int partitionID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | bool hasPictureID; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 158 | bool hasTl0PicIdx; |
| 159 | bool hasTID; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 160 | bool hasKeyIdx; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 161 | int pictureID; |
| 162 | int tl0PicIdx; |
| 163 | int tID; |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 +0000 | [diff] [blame] | 164 | bool layerSync; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 165 | int keyIdx; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 166 | int frameWidth; |
| 167 | int frameHeight; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 169 | const uint8_t* data; |
| 170 | uint16_t dataLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | union RTPPayloadUnion |
| 174 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | RTPPayloadVP8 VP8; |
| 176 | }; |
| 177 | |
| 178 | struct RTPPayload |
| 179 | { |
| 180 | void SetType(RtpVideoCodecTypes videoType); |
| 181 | |
| 182 | RtpVideoCodecTypes type; |
| 183 | FrameTypes frameType; |
| 184 | RTPPayloadUnion info; |
| 185 | }; |
| 186 | |
| 187 | // RTP payload parser |
| 188 | class RTPPayloadParser |
| 189 | { |
| 190 | public: |
| 191 | RTPPayloadParser(const RtpVideoCodecTypes payloadType, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 192 | const uint8_t* payloadData, |
| 193 | const uint16_t payloadDataLength, // Length w/o padding. |
| 194 | const int32_t id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | |
| 196 | ~RTPPayloadParser(); |
| 197 | |
| 198 | bool Parse(RTPPayload& parsedPacket) const; |
| 199 | |
| 200 | private: |
| 201 | bool ParseGeneric(RTPPayload& parsedPacket) const; |
| 202 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | bool ParseVP8(RTPPayload& parsedPacket) const; |
| 204 | |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 205 | int ParseVP8Extension(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 206 | const uint8_t *dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 207 | int dataLength) const; |
| 208 | |
| 209 | int ParseVP8PictureID(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 210 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 211 | int *dataLength, |
| 212 | int *parsedBytes) const; |
| 213 | |
| 214 | int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 215 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 216 | int *dataLength, |
| 217 | int *parsedBytes) const; |
| 218 | |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 219 | int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 220 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 221 | int *dataLength, |
| 222 | int *parsedBytes) const; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 223 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 224 | int ParseVP8FrameSize(RTPPayload& parsedPacket, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 225 | const uint8_t *dataPtr, |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 226 | int dataLength) const; |
| 227 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | private: |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 229 | int32_t _id; |
| 230 | const uint8_t* _dataPtr; |
| 231 | const uint16_t _dataLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | const RtpVideoCodecTypes _videoType; |
| 233 | }; |
henrike@webrtc.org | d5657c2 | 2012-02-08 23:41:49 +0000 | [diff] [blame] | 234 | |
| 235 | } // namespace ModuleRTPUtility |
| 236 | |
| 237 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 238 | |
| 239 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_ |