niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 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, |
| 25 | kRtpH263Video = 1, |
| 26 | kRtpH2631998Video = 2, |
| 27 | kRtpMpeg4Video = 5, |
| 28 | kRtpFecVideo = 10, |
| 29 | kRtpVp8Video = 11 |
| 30 | }; |
| 31 | |
| 32 | const WebRtc_UWord8 kRtpMarkerBitMask = 0x80; |
| 33 | |
| 34 | namespace ModuleRTPUtility |
| 35 | { |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 36 | // January 1970, in NTP seconds. |
| 37 | const uint32_t NTP_JAN_1970 = 2208988800UL; |
| 38 | |
| 39 | // Magic NTP fractional unit. |
| 40 | const double NTP_FRAC = 4.294967296E+9; |
| 41 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | struct AudioPayload |
| 43 | { |
| 44 | WebRtc_UWord32 frequency; |
| 45 | WebRtc_UWord8 channels; |
| 46 | WebRtc_UWord8 bitsPerSample; |
| 47 | WebRtc_UWord32 rate; |
| 48 | }; |
| 49 | struct VideoPayload |
| 50 | { |
| 51 | RtpVideoCodecTypes videoCodecType; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 52 | WebRtc_UWord32 maxRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | }; |
| 54 | union PayloadUnion |
| 55 | { |
| 56 | AudioPayload Audio; |
| 57 | VideoPayload Video; |
| 58 | }; |
| 59 | struct Payload |
| 60 | { |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 61 | WebRtc_Word8 name[RTP_PAYLOAD_NAME_SIZE]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | bool audio; |
| 63 | PayloadUnion typeSpecific; |
| 64 | }; |
| 65 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 66 | // Return a clock that reads the time as reported by the operating |
| 67 | // system. The returned instances are guaranteed to read the same |
| 68 | // times; in particular, they return relative times relative to |
| 69 | // the same base. |
| 70 | RtpRtcpClock* GetSystemClock(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 72 | // Return the current RTP timestamp from the NTP timestamp |
| 73 | // returned by the specified clock. |
| 74 | WebRtc_UWord32 GetCurrentRTP(RtpRtcpClock* clock, WebRtc_UWord32 freq); |
| 75 | |
| 76 | // Return the current RTP absolute timestamp. |
| 77 | WebRtc_UWord32 ConvertNTPTimeToRTP(WebRtc_UWord32 NTPsec, |
| 78 | WebRtc_UWord32 NTPfrac, |
| 79 | WebRtc_UWord32 freq); |
| 80 | |
| 81 | // Return the time in milliseconds corresponding to the specified |
| 82 | // NTP timestamp. |
| 83 | WebRtc_UWord32 ConvertNTPTimeToMS(WebRtc_UWord32 NTPsec, |
| 84 | WebRtc_UWord32 NTPfrac); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
| 86 | WebRtc_UWord32 pow2(WebRtc_UWord8 exp); |
| 87 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | bool StringCompare(const WebRtc_Word8* str1 , const WebRtc_Word8* str2, const WebRtc_UWord32 length); |
| 89 | |
| 90 | void AssignUWord32ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value); |
| 91 | void AssignUWord24ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord32 value); |
| 92 | void AssignUWord16ToBuffer(WebRtc_UWord8* dataBuffer, WebRtc_UWord16 value); |
| 93 | |
| 94 | /** |
| 95 | * Converts a network-ordered two-byte input buffer to a host-ordered value. |
| 96 | * \param[in] dataBuffer Network-ordered two-byte buffer to convert. |
| 97 | * \return Host-ordered value. |
| 98 | */ |
| 99 | WebRtc_UWord16 BufferToUWord16(const WebRtc_UWord8* dataBuffer); |
| 100 | |
| 101 | /** |
| 102 | * Converts a network-ordered three-byte input buffer to a host-ordered value. |
| 103 | * \param[in] dataBuffer Network-ordered three-byte buffer to convert. |
| 104 | * \return Host-ordered value. |
| 105 | */ |
| 106 | WebRtc_UWord32 BufferToUWord24(const WebRtc_UWord8* dataBuffer); |
| 107 | |
| 108 | /** |
| 109 | * Converts a network-ordered four-byte input buffer to a host-ordered value. |
| 110 | * \param[in] dataBuffer Network-ordered four-byte buffer to convert. |
| 111 | * \return Host-ordered value. |
| 112 | */ |
| 113 | WebRtc_UWord32 BufferToUWord32(const WebRtc_UWord8* dataBuffer); |
| 114 | |
| 115 | class RTPHeaderParser |
| 116 | { |
| 117 | public: |
| 118 | RTPHeaderParser(const WebRtc_UWord8* rtpData, |
| 119 | const WebRtc_UWord32 rtpDataLength); |
| 120 | ~RTPHeaderParser(); |
| 121 | |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame^] | 122 | bool RTCP() const; |
| 123 | bool Parse(WebRtcRTPHeader& parsedPacket, |
| 124 | RtpHeaderExtensionMap* ptrExtensionMap = NULL) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
| 126 | private: |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame^] | 127 | void ParseOneByteExtensionHeader( |
| 128 | WebRtcRTPHeader& parsedPacket, |
| 129 | const RtpHeaderExtensionMap* ptrExtensionMap, |
| 130 | const WebRtc_UWord8* ptrRTPDataExtensionEnd, |
| 131 | const WebRtc_UWord8* ptr) const; |
| 132 | |
| 133 | WebRtc_UWord8 ParsePaddingBytes( |
| 134 | const WebRtc_UWord8* ptrRTPDataExtensionEnd, |
| 135 | const WebRtc_UWord8* ptr) const; |
| 136 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | const WebRtc_UWord8* const _ptrRTPDataBegin; |
| 138 | const WebRtc_UWord8* const _ptrRTPDataEnd; |
| 139 | }; |
| 140 | |
| 141 | enum FrameTypes |
| 142 | { |
| 143 | kIFrame, // key frame |
| 144 | kPFrame // Delta frame |
| 145 | }; |
| 146 | |
| 147 | struct RTPPayloadH263 |
| 148 | { |
| 149 | // H.263 and H.263+ |
| 150 | bool hasPictureStartCode; |
| 151 | bool insert2byteStartCode; |
| 152 | bool hasPbit; |
| 153 | WebRtc_UWord16 frameWidth; |
| 154 | WebRtc_UWord16 frameHeight; |
| 155 | |
| 156 | WebRtc_UWord8 endBits; // ignore last end bits |
| 157 | WebRtc_UWord8 startBits; // ignore first bits |
| 158 | |
| 159 | const WebRtc_UWord8* data; |
| 160 | WebRtc_UWord16 dataLength; |
| 161 | }; |
| 162 | |
| 163 | struct RTPPayloadMPEG4 |
| 164 | { |
| 165 | // MPEG4 |
| 166 | bool isFirstPacket; |
| 167 | const WebRtc_UWord8* data; |
| 168 | WebRtc_UWord16 dataLength; |
| 169 | }; |
| 170 | struct RTPPayloadVP8 |
| 171 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | bool nonReferenceFrame; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 173 | bool beginningOfPartition; |
| 174 | int partitionID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | bool hasPictureID; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 176 | bool hasTl0PicIdx; |
| 177 | bool hasTID; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 178 | bool hasKeyIdx; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 179 | int pictureID; |
| 180 | int tl0PicIdx; |
| 181 | int tID; |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 +0000 | [diff] [blame] | 182 | bool layerSync; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 183 | int keyIdx; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 184 | int frameWidth; |
| 185 | int frameHeight; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 187 | const WebRtc_UWord8* data; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | WebRtc_UWord16 dataLength; |
| 189 | }; |
| 190 | |
| 191 | union RTPPayloadUnion |
| 192 | { |
| 193 | RTPPayloadH263 H263; |
| 194 | RTPPayloadMPEG4 MPEG4; |
| 195 | RTPPayloadVP8 VP8; |
| 196 | }; |
| 197 | |
| 198 | struct RTPPayload |
| 199 | { |
| 200 | void SetType(RtpVideoCodecTypes videoType); |
| 201 | |
| 202 | RtpVideoCodecTypes type; |
| 203 | FrameTypes frameType; |
| 204 | RTPPayloadUnion info; |
| 205 | }; |
| 206 | |
| 207 | // RTP payload parser |
| 208 | class RTPPayloadParser |
| 209 | { |
| 210 | public: |
| 211 | RTPPayloadParser(const RtpVideoCodecTypes payloadType, |
| 212 | const WebRtc_UWord8* payloadData, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 213 | const WebRtc_UWord16 payloadDataLength, // Length w/o padding. |
| 214 | const WebRtc_Word32 id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | |
| 216 | ~RTPPayloadParser(); |
| 217 | |
| 218 | bool Parse(RTPPayload& parsedPacket) const; |
| 219 | |
| 220 | private: |
| 221 | bool ParseGeneric(RTPPayload& parsedPacket) const; |
| 222 | |
| 223 | bool ParseH263(RTPPayload& parsedPacket) const; |
| 224 | bool ParseH2631998(RTPPayload& parsedPacket) const; |
| 225 | |
| 226 | bool ParseMPEG4(RTPPayload& parsedPacket) const; |
| 227 | |
| 228 | bool ParseVP8(RTPPayload& parsedPacket) const; |
| 229 | |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 230 | int ParseVP8Extension(RTPPayloadVP8 *vp8, |
| 231 | const WebRtc_UWord8 *dataPtr, |
| 232 | int dataLength) const; |
| 233 | |
| 234 | int ParseVP8PictureID(RTPPayloadVP8 *vp8, |
| 235 | const WebRtc_UWord8 **dataPtr, |
| 236 | int *dataLength, |
| 237 | int *parsedBytes) const; |
| 238 | |
| 239 | int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8, |
| 240 | const WebRtc_UWord8 **dataPtr, |
| 241 | int *dataLength, |
| 242 | int *parsedBytes) const; |
| 243 | |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 244 | int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8, |
| 245 | const WebRtc_UWord8 **dataPtr, |
| 246 | int *dataLength, |
| 247 | int *parsedBytes) const; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 248 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 249 | int ParseVP8FrameSize(RTPPayload& parsedPacket, |
| 250 | const WebRtc_UWord8 *dataPtr, |
| 251 | int dataLength) const; |
| 252 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | // H.263 |
| 254 | bool H263PictureStartCode(const WebRtc_UWord8* data, |
| 255 | const bool skipFirst2bytes = false) const; |
| 256 | |
| 257 | void GetH263FrameSize(const WebRtc_UWord8* inputVideoBuffer, |
| 258 | WebRtc_UWord16& width, |
| 259 | WebRtc_UWord16& height) const; |
| 260 | |
| 261 | FrameTypes GetH263FrameType(const WebRtc_UWord8* inputVideoBuffer) const; |
| 262 | |
| 263 | private: |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 264 | WebRtc_Word32 _id; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | const WebRtc_UWord8* _dataPtr; |
| 266 | const WebRtc_UWord16 _dataLength; |
| 267 | const RtpVideoCodecTypes _videoType; |
| 268 | }; |
| 269 | } |
| 270 | } // namespace webrtc |
| 271 | |
| 272 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_ |