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 MEDIA_BASE_RTP_UTILS_H_
12 #define MEDIA_BASE_RTP_UTILS_H_
13 
14 #include "absl/strings/string_view.h"
15 #include "api/array_view.h"
16 #include "rtc_base/byte_order.h"
17 #include "rtc_base/system/rtc_export.h"
18 
19 namespace rtc {
20 struct PacketTimeUpdateParams;
21 }  // namespace rtc
22 
23 namespace cricket {
24 
25 const size_t kMinRtpPacketLen = 12;
26 const size_t kMaxRtpPacketLen = 2048;
27 const size_t kMinRtcpPacketLen = 4;
28 
29 struct RtpHeader {
30   int payload_type;
31   int seq_num;
32   uint32_t timestamp;
33   uint32_t ssrc;
34 };
35 
36 enum RtcpTypes {
37   kRtcpTypeSR = 200,     // Sender report payload type.
38   kRtcpTypeRR = 201,     // Receiver report payload type.
39   kRtcpTypeSDES = 202,   // SDES payload type.
40   kRtcpTypeBye = 203,    // BYE payload type.
41   kRtcpTypeApp = 204,    // APP payload type.
42   kRtcpTypeRTPFB = 205,  // Transport layer Feedback message payload type.
43   kRtcpTypePSFB = 206,   // Payload-specific Feedback message payload type.
44 };
45 
46 enum class RtpPacketType {
47   kRtp,
48   kRtcp,
49   kUnknown,
50 };
51 
52 bool GetRtpPayloadType(const void* data, size_t len, int* value);
53 bool GetRtpSeqNum(const void* data, size_t len, int* value);
54 bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value);
55 bool GetRtpSsrc(const void* data, size_t len, uint32_t* value);
56 bool GetRtpHeaderLen(const void* data, size_t len, size_t* value);
57 bool GetRtcpType(const void* data, size_t len, int* value);
58 bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value);
59 bool GetRtpHeader(const void* data, size_t len, RtpHeader* header);
60 
61 bool SetRtpSsrc(void* data, size_t len, uint32_t value);
62 // Assumes version 2, no padding, no extensions, no csrcs.
63 bool SetRtpHeader(void* data, size_t len, const RtpHeader& header);
64 
65 bool IsRtpPacket(rtc::ArrayView<const char> packet);
66 
67 bool IsRtcpPacket(rtc::ArrayView<const char> packet);
68 // Checks the packet header to determine if it can be an RTP or RTCP packet.
69 RtpPacketType InferRtpPacketType(rtc::ArrayView<const char> packet);
70 // True if |payload type| is 0-127.
71 bool IsValidRtpPayloadType(int payload_type);
72 
73 // True if |size| is appropriate for the indicated packet type.
74 bool IsValidRtpPacketSize(RtpPacketType packet_type, size_t size);
75 
76 // Returns "RTCP", "RTP" or "Unknown" according to |packet_type|.
77 absl::string_view RtpPacketTypeToString(RtpPacketType packet_type);
78 
79 // Verifies that a packet has a valid RTP header.
80 bool RTC_EXPORT ValidateRtpHeader(const uint8_t* rtp,
81                                   size_t length,
82                                   size_t* header_length);
83 
84 // Helper method which updates the absolute send time extension if present.
85 bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
86                                    size_t length,
87                                    int extension_id,
88                                    uint64_t time_us);
89 
90 // Applies specified |options| to the packet. It updates the absolute send time
91 // extension header if it is present present then updates HMAC.
92 bool RTC_EXPORT
93 ApplyPacketOptions(uint8_t* data,
94                    size_t length,
95                    const rtc::PacketTimeUpdateParams& packet_time_params,
96                    uint64_t time_us);
97 
98 }  // namespace cricket
99 
100 #endif  // MEDIA_BASE_RTP_UTILS_H_
101