xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_util.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 #include "modules/rtp_rtcp/source/rtp_util.h"
12 
13 #include <cstddef>
14 #include <cstdint>
15 
16 #include "api/array_view.h"
17 #include "modules/rtp_rtcp/source/byte_io.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 constexpr uint8_t kRtpVersion = 2;
24 constexpr size_t kMinRtpPacketLen = 12;
25 constexpr size_t kMinRtcpPacketLen = 4;
26 
HasCorrectRtpVersion(rtc::ArrayView<const uint8_t> packet)27 bool HasCorrectRtpVersion(rtc::ArrayView<const uint8_t> packet) {
28   return packet[0] >> 6 == kRtpVersion;
29 }
30 
31 // For additional details, see http://tools.ietf.org/html/rfc5761#section-4
PayloadTypeIsReservedForRtcp(uint8_t payload_type)32 bool PayloadTypeIsReservedForRtcp(uint8_t payload_type) {
33   return 64 <= payload_type && payload_type < 96;
34 }
35 
36 }  // namespace
37 
IsRtpPacket(rtc::ArrayView<const uint8_t> packet)38 bool IsRtpPacket(rtc::ArrayView<const uint8_t> packet) {
39   return packet.size() >= kMinRtpPacketLen && HasCorrectRtpVersion(packet) &&
40          !PayloadTypeIsReservedForRtcp(packet[1] & 0x7F);
41 }
42 
IsRtcpPacket(rtc::ArrayView<const uint8_t> packet)43 bool IsRtcpPacket(rtc::ArrayView<const uint8_t> packet) {
44   return packet.size() >= kMinRtcpPacketLen && HasCorrectRtpVersion(packet) &&
45          PayloadTypeIsReservedForRtcp(packet[1] & 0x7F);
46 }
47 
ParseRtpPayloadType(rtc::ArrayView<const uint8_t> rtp_packet)48 int ParseRtpPayloadType(rtc::ArrayView<const uint8_t> rtp_packet) {
49   RTC_DCHECK(IsRtpPacket(rtp_packet));
50   return rtp_packet[1] & 0x7F;
51 }
52 
ParseRtpSequenceNumber(rtc::ArrayView<const uint8_t> rtp_packet)53 uint16_t ParseRtpSequenceNumber(rtc::ArrayView<const uint8_t> rtp_packet) {
54   RTC_DCHECK(IsRtpPacket(rtp_packet));
55   return ByteReader<uint16_t>::ReadBigEndian(rtp_packet.data() + 2);
56 }
57 
ParseRtpSsrc(rtc::ArrayView<const uint8_t> rtp_packet)58 uint32_t ParseRtpSsrc(rtc::ArrayView<const uint8_t> rtp_packet) {
59   RTC_DCHECK(IsRtpPacket(rtp_packet));
60   return ByteReader<uint32_t>::ReadBigEndian(rtp_packet.data() + 8);
61 }
62 
63 }  // namespace webrtc
64