blob: 6ef97fa05a0088cc625c64cd7a8543858f468946 [file] [log] [blame]
danilchap0219c9b2015-11-18 05:56:53 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
danilchap0219c9b2015-11-18 05:56:53 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/source/byte_io.h"
14#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/logging.h"
danilchap0219c9b2015-11-18 05:56:53 -080017
18namespace webrtc {
19namespace rtcp {
danilchap2874ed52016-07-26 06:40:29 -070020constexpr uint8_t App::kPacketType;
21constexpr size_t App::kMaxDataSize;
danilchap0219c9b2015-11-18 05:56:53 -080022// Application-Defined packet (APP) (RFC 3550).
23//
24// 0 1 2 3
25// 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
26// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27// |V=2|P| subtype | PT=APP=204 | length |
28// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29// 0 | SSRC/CSRC |
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// 4 | name (ASCII) |
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// 8 | application-dependent data ...
34// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
eladalon8fa21c42017-06-16 07:07:47 -070035
36App::App() : sub_type_(0), ssrc_(0), name_(0) {}
37
38App::~App() = default;
39
danilchap2874ed52016-07-26 06:40:29 -070040bool App::Parse(const CommonHeader& packet) {
41 RTC_DCHECK_EQ(packet.type(), kPacketType);
42 if (packet.payload_size_bytes() < kAppBaseLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010043 RTC_LOG(LS_WARNING) << "Packet is too small to be a valid APP packet";
danilchap2874ed52016-07-26 06:40:29 -070044 return false;
45 }
46 if (packet.payload_size_bytes() % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010047 RTC_LOG(LS_WARNING)
danilchap2874ed52016-07-26 06:40:29 -070048 << "Packet payload must be 32 bits aligned to make a valid APP packet";
49 return false;
50 }
51 sub_type_ = packet.fmt();
52 ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[0]);
53 name_ = ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[4]);
54 data_.SetData(packet.payload() + kAppBaseLength,
55 packet.payload_size_bytes() - kAppBaseLength);
danilchap0219c9b2015-11-18 05:56:53 -080056 return true;
57}
58
danilchap822a16f2016-09-27 09:27:47 -070059void App::SetSubType(uint8_t subtype) {
danilchap0219c9b2015-11-18 05:56:53 -080060 RTC_DCHECK_LE(subtype, 0x1f);
61 sub_type_ = subtype;
62}
63
danilchap822a16f2016-09-27 09:27:47 -070064void App::SetData(const uint8_t* data, size_t data_length) {
danilchap0219c9b2015-11-18 05:56:53 -080065 RTC_DCHECK(data);
kwibergaf476c72016-11-28 15:21:39 -080066 RTC_DCHECK_EQ(data_length % 4, 0) << "Data must be 32 bits aligned.";
danilchap2874ed52016-07-26 06:40:29 -070067 RTC_DCHECK_LE(data_length, kMaxDataSize) << "App data size " << data_length
68 << " exceed maximum of "
69 << kMaxDataSize << " bytes.";
danilchap0219c9b2015-11-18 05:56:53 -080070 data_.SetData(data, data_length);
71}
72
eladalon8fa21c42017-06-16 07:07:47 -070073size_t App::BlockLength() const {
74 return kHeaderLength + kAppBaseLength + data_.size();
75}
76
danilchap0219c9b2015-11-18 05:56:53 -080077bool App::Create(uint8_t* packet,
78 size_t* index,
79 size_t max_length,
80 RtcpPacket::PacketReadyCallback* callback) const {
81 while (*index + BlockLength() > max_length) {
82 if (!OnBufferFull(packet, index, callback))
83 return false;
84 }
85 const size_t index_end = *index + BlockLength();
86 CreateHeader(sub_type_, kPacketType, HeaderLength(), packet, index);
87
88 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], ssrc_);
89 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 4], name_);
90 memcpy(&packet[*index + 8], data_.data(), data_.size());
91 *index += (8 + data_.size());
92 RTC_DCHECK_EQ(index_end, *index);
93 return true;
94}
95
96} // namespace rtcp
97} // namespace webrtc