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