1 /* 2 * Copyright (c) 2012 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 <stddef.h> // size_t, ptrdiff_t 15 16 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" 17 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h" 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" 21 22 namespace webrtc { 23 24 const uint8_t kRtpMarkerBitMask = 0x80; 25 26 RtpData* NullObjectRtpData(); 27 RtpFeedback* NullObjectRtpFeedback(); 28 RtpAudioFeedback* NullObjectRtpAudioFeedback(); 29 ReceiveStatistics* NullObjectReceiveStatistics(); 30 31 namespace RtpUtility { 32 // January 1970, in NTP seconds. 33 const uint32_t NTP_JAN_1970 = 2208988800UL; 34 35 // Magic NTP fractional unit. 36 const double NTP_FRAC = 4.294967296E+9; 37 38 struct Payload 39 { 40 char name[RTP_PAYLOAD_NAME_SIZE]; 41 bool audio; 42 PayloadUnion typeSpecific; 43 }; 44 45 typedef std::map<int8_t, Payload*> PayloadTypeMap; 46 47 // Return the current RTP timestamp from the NTP timestamp 48 // returned by the specified clock. 49 uint32_t GetCurrentRTP(Clock* clock, uint32_t freq); 50 51 // Return the current RTP absolute timestamp. 52 uint32_t ConvertNTPTimeToRTP(uint32_t NTPsec, 53 uint32_t NTPfrac, 54 uint32_t freq); 55 56 uint32_t pow2(uint8_t exp); 57 58 // Returns true if |newTimestamp| is older than |existingTimestamp|. 59 // |wrapped| will be set to true if there has been a wraparound between the 60 // two timestamps. 61 bool OldTimestamp(uint32_t newTimestamp, 62 uint32_t existingTimestamp, 63 bool* wrapped); 64 65 bool StringCompare(const char* str1, 66 const char* str2, 67 const uint32_t length); 68 69 void AssignUWord32ToBuffer(uint8_t* dataBuffer, uint32_t value); 70 void AssignUWord24ToBuffer(uint8_t* dataBuffer, uint32_t value); 71 void AssignUWord16ToBuffer(uint8_t* dataBuffer, uint16_t value); 72 73 /** 74 * Converts a network-ordered two-byte input buffer to a host-ordered value. 75 * \param[in] dataBuffer Network-ordered two-byte buffer to convert. 76 * \return Host-ordered value. 77 */ 78 uint16_t BufferToUWord16(const uint8_t* dataBuffer); 79 80 /** 81 * Converts a network-ordered three-byte input buffer to a host-ordered value. 82 * \param[in] dataBuffer Network-ordered three-byte buffer to convert. 83 * \return Host-ordered value. 84 */ 85 uint32_t BufferToUWord24(const uint8_t* dataBuffer); 86 87 /** 88 * Converts a network-ordered four-byte input buffer to a host-ordered value. 89 * \param[in] dataBuffer Network-ordered four-byte buffer to convert. 90 * \return Host-ordered value. 91 */ 92 uint32_t BufferToUWord32(const uint8_t* dataBuffer); 93 94 class RtpHeaderParser { 95 public: 96 RtpHeaderParser(const uint8_t* rtpData, size_t rtpDataLength); 97 ~RtpHeaderParser(); 98 99 bool RTCP() const; 100 bool ParseRtcp(RTPHeader* header) const; 101 bool Parse(RTPHeader& parsedPacket, 102 RtpHeaderExtensionMap* ptrExtensionMap = NULL) const; 103 104 private: 105 void ParseOneByteExtensionHeader( 106 RTPHeader& parsedPacket, 107 const RtpHeaderExtensionMap* ptrExtensionMap, 108 const uint8_t* ptrRTPDataExtensionEnd, 109 const uint8_t* ptr) const; 110 111 uint8_t ParsePaddingBytes( 112 const uint8_t* ptrRTPDataExtensionEnd, 113 const uint8_t* ptr) const; 114 115 const uint8_t* const _ptrRTPDataBegin; 116 const uint8_t* const _ptrRTPDataEnd; 117 }; 118 } // namespace RtpUtility 119 } // namespace webrtc 120 121 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_ 122