danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtcp_packet/app.h" |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 13 | #include <string.h> |
| 14 | #include <cstdint> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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" |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace rtcp { |
danilchap | 2874ed5 | 2016-07-26 06:40:29 -0700 | [diff] [blame] | 23 | constexpr uint8_t App::kPacketType; |
| 24 | constexpr size_t App::kMaxDataSize; |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 25 | // Application-Defined packet (APP) (RFC 3550). |
| 26 | // |
| 27 | // 0 1 2 3 |
| 28 | // 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 |
| 29 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 | // |V=2|P| subtype | PT=APP=204 | length | |
| 31 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 32 | // 0 | SSRC/CSRC | |
| 33 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 | // 4 | name (ASCII) | |
| 35 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | // 8 | application-dependent data ... |
| 37 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 38 | |
| 39 | App::App() : sub_type_(0), ssrc_(0), name_(0) {} |
| 40 | |
| 41 | App::~App() = default; |
| 42 | |
danilchap | 2874ed5 | 2016-07-26 06:40:29 -0700 | [diff] [blame] | 43 | bool App::Parse(const CommonHeader& packet) { |
| 44 | RTC_DCHECK_EQ(packet.type(), kPacketType); |
| 45 | if (packet.payload_size_bytes() < kAppBaseLength) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 46 | RTC_LOG(LS_WARNING) << "Packet is too small to be a valid APP packet"; |
danilchap | 2874ed5 | 2016-07-26 06:40:29 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | if (packet.payload_size_bytes() % 4 != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 50 | RTC_LOG(LS_WARNING) |
danilchap | 2874ed5 | 2016-07-26 06:40:29 -0700 | [diff] [blame] | 51 | << "Packet payload must be 32 bits aligned to make a valid APP packet"; |
| 52 | return false; |
| 53 | } |
| 54 | sub_type_ = packet.fmt(); |
| 55 | ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[0]); |
| 56 | name_ = ByteReader<uint32_t>::ReadBigEndian(&packet.payload()[4]); |
| 57 | data_.SetData(packet.payload() + kAppBaseLength, |
| 58 | packet.payload_size_bytes() - kAppBaseLength); |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 62 | void App::SetSubType(uint8_t subtype) { |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 63 | RTC_DCHECK_LE(subtype, 0x1f); |
| 64 | sub_type_ = subtype; |
| 65 | } |
| 66 | |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 67 | void App::SetData(const uint8_t* data, size_t data_length) { |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 68 | RTC_DCHECK(data); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 69 | RTC_DCHECK_EQ(data_length % 4, 0) << "Data must be 32 bits aligned."; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | RTC_DCHECK_LE(data_length, kMaxDataSize) |
| 71 | << "App data size " << data_length << " exceed maximum of " |
| 72 | << kMaxDataSize << " bytes."; |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 73 | data_.SetData(data, data_length); |
| 74 | } |
| 75 | |
eladalon | 8fa21c4 | 2017-06-16 07:07:47 -0700 | [diff] [blame] | 76 | size_t App::BlockLength() const { |
| 77 | return kHeaderLength + kAppBaseLength + data_.size(); |
| 78 | } |
| 79 | |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 80 | bool App::Create(uint8_t* packet, |
| 81 | size_t* index, |
| 82 | size_t max_length, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 83 | PacketReadyCallback callback) const { |
danilchap | 0219c9b | 2015-11-18 05:56:53 -0800 | [diff] [blame] | 84 | while (*index + BlockLength() > max_length) { |
| 85 | if (!OnBufferFull(packet, index, callback)) |
| 86 | return false; |
| 87 | } |
| 88 | const size_t index_end = *index + BlockLength(); |
| 89 | CreateHeader(sub_type_, kPacketType, HeaderLength(), packet, index); |
| 90 | |
| 91 | ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], ssrc_); |
| 92 | ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 4], name_); |
| 93 | memcpy(&packet[*index + 8], data_.data(), data_.size()); |
| 94 | *index += (8 + data_.size()); |
| 95 | RTC_DCHECK_EQ(index_end, *index); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | } // namespace rtcp |
| 100 | } // namespace webrtc |