1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "cast/streaming/packet_util.h" 6 7 #include "cast/streaming/rtcp_common.h" 8 #include "cast/streaming/rtp_defines.h" 9 10 namespace openscreen { 11 namespace cast { 12 InspectPacketForRouting(absl::Span<const uint8_t> packet)13std::pair<ApparentPacketType, Ssrc> InspectPacketForRouting( 14 absl::Span<const uint8_t> packet) { 15 // Check for RTP packets first, since they are more frequent. 16 if (packet.size() >= kRtpPacketMinValidSize && 17 packet[0] == kRtpRequiredFirstByte && 18 IsRtpPayloadType(packet[1] & kRtpPayloadTypeMask)) { 19 constexpr int kOffsetToSsrcField = 8; 20 return std::make_pair( 21 ApparentPacketType::RTP, 22 Ssrc{ReadBigEndian<uint32_t>(packet.data() + kOffsetToSsrcField)}); 23 } 24 25 // While RTCP packets are valid if they consist of just the RTCP Common 26 // Header, all the RTCP packet types processed by this implementation will 27 // also have a SSRC field immediately following the header. This is important 28 // for routing the packet to the correct parser instance. 29 constexpr int kRtcpPacketMinAcceptableSize = 30 kRtcpCommonHeaderSize + sizeof(uint32_t); 31 if (packet.size() >= kRtcpPacketMinAcceptableSize && 32 RtcpCommonHeader::Parse(packet).has_value()) { 33 return std::make_pair( 34 ApparentPacketType::RTCP, 35 Ssrc{ReadBigEndian<uint32_t>(packet.data() + kRtcpCommonHeaderSize)}); 36 } 37 38 return std::make_pair(ApparentPacketType::UNKNOWN, Ssrc{0}); 39 } 40 41 } // namespace cast 42 } // namespace openscreen 43