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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <stddef.h> // size_t, ptrdiff_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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" |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 17 | #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h" |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 18 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 24 | const uint8_t kRtpMarkerBitMask = 0x80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 26 | RtpData* NullObjectRtpData(); |
| 27 | RtpFeedback* NullObjectRtpFeedback(); |
| 28 | RtpAudioFeedback* NullObjectRtpAudioFeedback(); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 29 | ReceiveStatistics* NullObjectReceiveStatistics(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 30 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 Payload |
| 40 | { |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 41 | char name[RTP_PAYLOAD_NAME_SIZE]; |
| 42 | bool audio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | PayloadUnion typeSpecific; |
| 44 | }; |
| 45 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 46 | typedef std::map<int8_t, Payload*> PayloadTypeMap; |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 47 | |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 48 | // Return the current RTP timestamp from the NTP timestamp |
| 49 | // returned by the specified clock. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 50 | uint32_t GetCurrentRTP(Clock* clock, uint32_t freq); |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 51 | |
| 52 | // Return the current RTP absolute timestamp. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 53 | uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec, |
| 54 | uint32_t NTPfrac, |
| 55 | uint32_t freq); |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 56 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 57 | uint32_t pow2(uint8_t exp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 59 | // Returns a pointer to the payload data given a packet. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 60 | const uint8_t* GetPayloadData(const RTPHeader& rtp_header, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 61 | const uint8_t* packet); |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 62 | |
| 63 | // Returns payload length given a packet. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 64 | uint16_t GetPayloadDataLength(const RTPHeader& rtp_header, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 65 | const uint16_t packet_length); |
phoglund@webrtc.org | 07bf43c | 2012-12-18 15:40:53 +0000 | [diff] [blame] | 66 | |
marpan@webrtc.org | 57353a3 | 2011-12-16 17:21:09 +0000 | [diff] [blame] | 67 | // Returns true if |newTimestamp| is older than |existingTimestamp|. |
| 68 | // |wrapped| will be set to true if there has been a wraparound between the |
| 69 | // two timestamps. |
| 70 | bool OldTimestamp(uint32_t newTimestamp, |
| 71 | uint32_t existingTimestamp, |
| 72 | bool* wrapped); |
| 73 | |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 74 | bool StringCompare(const char* str1, |
| 75 | const char* str2, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 76 | const uint32_t length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 78 | void AssignUWord32ToBuffer(uint8_t* dataBuffer, uint32_t value); |
| 79 | void AssignUWord24ToBuffer(uint8_t* dataBuffer, uint32_t value); |
| 80 | void AssignUWord16ToBuffer(uint8_t* dataBuffer, uint16_t value); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Converts a network-ordered two-byte input buffer to a host-ordered value. |
| 84 | * \param[in] dataBuffer Network-ordered two-byte buffer to convert. |
| 85 | * \return Host-ordered value. |
| 86 | */ |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 87 | uint16_t BufferToUWord16(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Converts a network-ordered three-byte input buffer to a host-ordered value. |
| 91 | * \param[in] dataBuffer Network-ordered three-byte buffer to convert. |
| 92 | * \return Host-ordered value. |
| 93 | */ |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 94 | uint32_t BufferToUWord24(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Converts a network-ordered four-byte input buffer to a host-ordered value. |
| 98 | * \param[in] dataBuffer Network-ordered four-byte buffer to convert. |
| 99 | * \return Host-ordered value. |
| 100 | */ |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 101 | uint32_t BufferToUWord32(const uint8_t* dataBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | |
| 103 | class RTPHeaderParser |
| 104 | { |
| 105 | public: |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 106 | RTPHeaderParser(const uint8_t* rtpData, |
| 107 | const uint32_t rtpDataLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | ~RTPHeaderParser(); |
| 109 | |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 110 | bool RTCP() const; |
solenberg@webrtc.org | a5fd2f1 | 2013-06-26 08:36:07 +0000 | [diff] [blame] | 111 | bool ParseRtcp(RTPHeader* header) const; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 112 | bool Parse(RTPHeader& parsedPacket, |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 113 | RtpHeaderExtensionMap* ptrExtensionMap = NULL) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
| 115 | private: |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 116 | void ParseOneByteExtensionHeader( |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 117 | RTPHeader& parsedPacket, |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 118 | const RtpHeaderExtensionMap* ptrExtensionMap, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 119 | const uint8_t* ptrRTPDataExtensionEnd, |
| 120 | const uint8_t* ptr) const; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 121 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 122 | uint8_t ParsePaddingBytes( |
| 123 | const uint8_t* ptrRTPDataExtensionEnd, |
| 124 | const uint8_t* ptr) const; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 125 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 126 | const uint8_t* const _ptrRTPDataBegin; |
| 127 | const uint8_t* const _ptrRTPDataEnd; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | enum FrameTypes |
| 131 | { |
| 132 | kIFrame, // key frame |
| 133 | kPFrame // Delta frame |
| 134 | }; |
| 135 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | struct RTPPayloadVP8 |
| 137 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | bool nonReferenceFrame; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 139 | bool beginningOfPartition; |
| 140 | int partitionID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | bool hasPictureID; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 142 | bool hasTl0PicIdx; |
| 143 | bool hasTID; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 144 | bool hasKeyIdx; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 145 | int pictureID; |
| 146 | int tl0PicIdx; |
| 147 | int tID; |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 +0000 | [diff] [blame] | 148 | bool layerSync; |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 149 | int keyIdx; |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 150 | int frameWidth; |
| 151 | int frameHeight; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 153 | const uint8_t* data; |
| 154 | uint16_t dataLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | union RTPPayloadUnion |
| 158 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | RTPPayloadVP8 VP8; |
| 160 | }; |
| 161 | |
| 162 | struct RTPPayload |
| 163 | { |
| 164 | void SetType(RtpVideoCodecTypes videoType); |
| 165 | |
| 166 | RtpVideoCodecTypes type; |
| 167 | FrameTypes frameType; |
| 168 | RTPPayloadUnion info; |
| 169 | }; |
| 170 | |
| 171 | // RTP payload parser |
| 172 | class RTPPayloadParser |
| 173 | { |
| 174 | public: |
| 175 | RTPPayloadParser(const RtpVideoCodecTypes payloadType, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 176 | const uint8_t* payloadData, |
| 177 | const uint16_t payloadDataLength, // Length w/o padding. |
| 178 | const int32_t id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
| 180 | ~RTPPayloadParser(); |
| 181 | |
| 182 | bool Parse(RTPPayload& parsedPacket) const; |
| 183 | |
| 184 | private: |
| 185 | bool ParseGeneric(RTPPayload& parsedPacket) const; |
| 186 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | bool ParseVP8(RTPPayload& parsedPacket) const; |
| 188 | |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 189 | int ParseVP8Extension(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 190 | const uint8_t *dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 191 | int dataLength) const; |
| 192 | |
| 193 | int ParseVP8PictureID(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 194 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 195 | int *dataLength, |
| 196 | int *parsedBytes) const; |
| 197 | |
| 198 | int ParseVP8Tl0PicIdx(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 199 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 200 | int *dataLength, |
| 201 | int *parsedBytes) const; |
| 202 | |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 203 | int ParseVP8TIDAndKeyIdx(RTPPayloadVP8 *vp8, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 204 | const uint8_t **dataPtr, |
henrik.lundin@webrtc.org | 6f2c016 | 2011-11-24 12:52:40 +0000 | [diff] [blame] | 205 | int *dataLength, |
| 206 | int *parsedBytes) const; |
henrik.lundin@webrtc.org | 8571af7 | 2011-08-29 15:37:12 +0000 | [diff] [blame] | 207 | |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 208 | int ParseVP8FrameSize(RTPPayload& parsedPacket, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 209 | const uint8_t *dataPtr, |
pwestin@webrtc.org | 075e91f | 2011-11-02 23:14:58 +0000 | [diff] [blame] | 210 | int dataLength) const; |
| 211 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | private: |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 213 | int32_t _id; |
| 214 | const uint8_t* _dataPtr; |
| 215 | const uint16_t _dataLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | const RtpVideoCodecTypes _videoType; |
| 217 | }; |
henrike@webrtc.org | d5657c2 | 2012-02-08 23:41:49 +0000 | [diff] [blame] | 218 | |
| 219 | } // namespace ModuleRTPUtility |
| 220 | |
| 221 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | |
| 223 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_ |