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     // Round up to the nearest size that is a multiple of 4.
70     size_t Word32Align(size_t size);
71 
72     class RtpHeaderParser {
73     public:
74      RtpHeaderParser(const uint8_t* rtpData, size_t rtpDataLength);
75      ~RtpHeaderParser();
76 
77         bool RTCP() const;
78         bool ParseRtcp(RTPHeader* header) const;
79         bool Parse(RTPHeader& parsedPacket,
80                    RtpHeaderExtensionMap* ptrExtensionMap = NULL) const;
81 
82     private:
83         void ParseOneByteExtensionHeader(
84             RTPHeader& parsedPacket,
85             const RtpHeaderExtensionMap* ptrExtensionMap,
86             const uint8_t* ptrRTPDataExtensionEnd,
87             const uint8_t* ptr) const;
88 
89         uint8_t ParsePaddingBytes(
90             const uint8_t* ptrRTPDataExtensionEnd,
91             const uint8_t* ptr) const;
92 
93         const uint8_t* const _ptrRTPDataBegin;
94         const uint8_t* const _ptrRTPDataEnd;
95     };
96 }  // namespace RtpUtility
97 }  // namespace webrtc
98 
99 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_UTILITY_H_
100