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