app.cc (3625B)
1 /* 2 * Copyright (c) 2015 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/app.h" 12 13 #include <cstdint> 14 #include <cstring> 15 16 #include "modules/rtp_rtcp/source/byte_io.h" 17 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h" 18 #include "rtc_base/checks.h" 19 #include "rtc_base/logging.h" 20 21 namespace webrtc { 22 namespace rtcp { 23 // Application-Defined packet (APP) (RFC 3550). 24 // 25 // 0 1 2 3 26 // 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 27 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 28 // |V=2|P| subtype | PT=APP=204 | length | 29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 30 // 0 | SSRC/CSRC | 31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 32 // 4 | name (ASCII) | 33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 34 // 8 | application-dependent data ... 35 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36 37 App::App() : sub_type_(0), name_(0) {} 38 39 App::~App() = default; 40 41 bool App::Parse(const CommonHeader& packet) { 42 RTC_DCHECK_EQ(packet.type(), kPacketType); 43 if (packet.payload_size_bytes() < kAppBaseLength) { 44 RTC_LOG(LS_WARNING) << "Packet is too small to be a valid APP packet"; 45 return false; 46 } 47 if (packet.payload_size_bytes() % 4 != 0) { 48 RTC_LOG(LS_WARNING) 49 << "Packet payload must be 32 bits aligned to make a valid APP packet"; 50 return false; 51 } 52 sub_type_ = packet.fmt(); 53 SetSenderSsrc(ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[0])); 54 name_ = ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[4]); 55 data_.SetData(packet.payload() + kAppBaseLength, 56 packet.payload_size_bytes() - kAppBaseLength); 57 return true; 58 } 59 60 void App::SetSubType(uint8_t subtype) { 61 RTC_DCHECK_LE(subtype, 0x1f); 62 sub_type_ = subtype; 63 } 64 65 void App::SetData(const uint8_t* data, size_t data_length) { 66 RTC_DCHECK(data); 67 RTC_DCHECK_EQ(data_length % 4, 0) << "Data must be 32 bits aligned."; 68 RTC_DCHECK_LE(data_length, kMaxDataSize) 69 << "App data size " << data_length << " exceed maximum of " 70 << kMaxDataSize << " bytes."; 71 data_.SetData(data, data_length); 72 } 73 74 size_t App::BlockLength() const { 75 return kHeaderLength + kAppBaseLength + data_.size(); 76 } 77 78 bool App::Create(uint8_t* packet, 79 size_t* index, 80 size_t max_length, 81 PacketReadyCallback callback) const { 82 while (*index + BlockLength() > max_length) { 83 if (!OnBufferFull(packet, index, callback)) 84 return false; 85 } 86 const size_t index_end = *index + BlockLength(); 87 CreateHeader(sub_type_, kPacketType, HeaderLength(), packet, index); 88 89 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], sender_ssrc()); 90 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 4], name_); 91 if (!data_.empty()) { 92 memcpy(&packet[*index + 8], data_.data(), data_.size()); 93 } 94 *index += (8 + data_.size()); 95 RTC_DCHECK_EQ(index_end, *index); 96 return true; 97 } 98 99 } // namespace rtcp 100 } // namespace webrtc