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 | |
| 16 | #include "typedefs.h" |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 17 | #include "rtp_header_extension.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #include "rtp_rtcp_config.h" |
| 19 | #include "rtp_rtcp_defines.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | enum RtpVideoCodecTypes |
| 23 | { |
| 24 | kRtpNoVideo = 0, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | kRtpFecVideo = 10, |
| 26 | kRtpVp8Video = 11 |
| 27 | }; |
| 28 | |
| 29 | const WebRtc_UWord8 kRtpMarkerBitMask = 0x80; |
| 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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | struct AudioPayload |
| 40 | { |
| 41 | WebRtc_UWord32 frequency; |
| 42 | WebRtc_UWord8 channels; |
| 43 | WebRtc_UWord8 bitsPerSample; |
| 44 | WebRtc_UWord32 rate; |
tina.legrand@webrtc.org | df69775 | 2012-02-08 10:22:21 +0000 | [diff] [blame] | 45 | bool trueStereoCodec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | }; |
| 47 | struct VideoPayload |
| 48 | { |
| 49 | RtpVideoCodecTypes videoCodecType; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 50 | WebRtc_UWord32 maxRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | }; |
| 52 | union PayloadUnion |
| 53 | { |
| 54 | AudioPayload Audio; |
| 55 | VideoPayload Video; |
| 56 | }; |
| 57 | struct Payload |
| 58 | { |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 59 | char name[RTP_PAYLOAD_NAME_SIZE]; |
| 60 | bool audio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | PayloadUnion typeSpecific; |
| 62 | }; |
| 63 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 64 | // 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 70 | // 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | |
| 84 | WebRtc_UWord32 pow2(WebRtc_UWord8 exp); |
| 85 | |
marpan@webrtc.org | 57353a3 | 2011-12-16 17:21:09 +0000 | [diff] [blame] | 86 | // 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.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 93 | bool StringCompare(const char* str1, |
| 94 | const char* str2, |
marpan@webrtc.org | 57353a3 | 2011-12-16 17:21:09 +0000 | [diff] [blame] | 95 | const WebRtc_UWord32 length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
| 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.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 129 | bool RTCP() const; |
| 130 | bool Parse(WebRtcRTPHeader& parsedPacket, |
| 131 | RtpHeaderExtensionMap* ptrExtensionMap = NULL) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | |
| 133 | private: |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 134 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | struct RTPPayloadVP8 |
| 155 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | bool nonReferenceFrame; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 157 | bool beginningOfPartition; |
| 158 | int partitionID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | bool hasPictureID; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 160 | bool hasTl0PicIdx; |
| 161 | bool hasTID; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 162 | bool hasKeyIdx; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 163 | int pictureID; |
| 164 | int tl0PicIdx; |
| 165 | int tID; |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 +0000 | [diff] [blame] | 166 | bool layerSync; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 167 | int keyIdx; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 168 | int frameWidth; |
| 169 | int frameHeight; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 171 | const WebRtc_UWord8* data; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | WebRtc_UWord16 dataLength; |
| 173 | }; |
| 174 | |
| 175 | union RTPPayloadUnion |
| 176 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | 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.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 195 | const WebRtc_UWord16 payloadDataLength, // Length w/o padding. |
| 196 | const WebRtc_Word32 id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | |
| 198 | ~RTPPayloadParser(); |
| 199 | |
| 200 | bool Parse(RTPPayload& parsedPacket) const; |
| 201 | |
| 202 | private: |
| 203 | bool ParseGeneric(RTPPayload& parsedPacket) const; |
| 204 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | bool ParseVP8(RTPPayload& parsedPacket) const; |
| 206 | |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 207 | 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.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 221 | int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8, |
| 222 | const WebRtc_UWord8 **dataPtr, |
| 223 | int *dataLength, |
| 224 | int *parsedBytes) const; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 225 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 226 | int ParseVP8FrameSize(RTPPayload& parsedPacket, |
| 227 | const WebRtc_UWord8 *dataPtr, |
| 228 | int dataLength) const; |
| 229 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | private: |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 231 | WebRtc_Word32 _id; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | const WebRtc_UWord8* _dataPtr; |
| 233 | const WebRtc_UWord16 _dataLength; |
| 234 | const RtpVideoCodecTypes _videoType; |
| 235 | }; |
henrike@webrtc.org | d5657c2 | 2012-02-08 23:41:49 +0000 | [diff] [blame^] | 236 | |
| 237 | } // namespace ModuleRTPUtility |
| 238 | |
| 239 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | |
| 241 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_ |