rtcp_packet.cc (3335B)
1 /* 2 * Copyright (c) 2014 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/rtcp_packet.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 16 #include "api/array_view.h" 17 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 18 #include "rtc_base/buffer.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 namespace rtcp { 23 24 Buffer RtcpPacket::Build() const { 25 Buffer packet(BlockLength()); 26 27 size_t length = 0; 28 bool created = Create(packet.data(), &length, packet.capacity(), nullptr); 29 RTC_DCHECK(created) << "Invalid packet is not supported."; 30 RTC_DCHECK_EQ(length, packet.size()) 31 << "BlockLength mispredicted size used by Create"; 32 33 return packet; 34 } 35 36 bool RtcpPacket::Build(size_t max_length, PacketReadyCallback callback) const { 37 RTC_CHECK_LE(max_length, IP_PACKET_SIZE); 38 uint8_t buffer[IP_PACKET_SIZE]; 39 size_t index = 0; 40 if (!Create(buffer, &index, max_length, callback)) 41 return false; 42 return OnBufferFull(buffer, &index, callback); 43 } 44 45 bool RtcpPacket::OnBufferFull(uint8_t* packet, 46 size_t* index, 47 PacketReadyCallback callback) const { 48 if (*index == 0) 49 return false; 50 RTC_DCHECK(callback) << "Fragmentation not supported."; 51 callback(ArrayView<const uint8_t>(packet, *index)); 52 *index = 0; 53 return true; 54 } 55 56 size_t RtcpPacket::HeaderLength() const { 57 size_t length_in_bytes = BlockLength(); 58 RTC_DCHECK_GT(length_in_bytes, 0); 59 RTC_DCHECK_EQ(length_in_bytes % 4, 0) 60 << "Padding must be handled by each subclass."; 61 // Length in 32-bit words without common header. 62 return (length_in_bytes - kHeaderLength) / 4; 63 } 64 65 // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. 66 // 67 // RTP header format. 68 // 0 1 2 3 69 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 70 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 71 // |V=2|P| RC/FMT | PT | length | 72 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 73 void RtcpPacket::CreateHeader( 74 size_t count_or_format, // Depends on packet type. 75 uint8_t packet_type, 76 size_t length, 77 uint8_t* buffer, 78 size_t* pos) { 79 CreateHeader(count_or_format, packet_type, length, /*padding=*/false, buffer, 80 pos); 81 } 82 83 void RtcpPacket::CreateHeader( 84 size_t count_or_format, // Depends on packet type. 85 uint8_t packet_type, 86 size_t length, 87 bool padding, 88 uint8_t* buffer, 89 size_t* pos) { 90 RTC_DCHECK_LE(length, 0xffffU); 91 RTC_DCHECK_LE(count_or_format, 0x1f); 92 constexpr uint8_t kVersionBits = 2 << 6; 93 uint8_t padding_bit = padding ? 1 << 5 : 0; 94 buffer[*pos + 0] = 95 kVersionBits | padding_bit | static_cast<uint8_t>(count_or_format); 96 buffer[*pos + 1] = packet_type; 97 buffer[*pos + 2] = (length >> 8) & 0xff; 98 buffer[*pos + 3] = length & 0xff; 99 *pos += kHeaderLength; 100 } 101 102 } // namespace rtcp 103 } // namespace webrtc