blob: 23ac35f856b5389133dd0fb54d8f7e59713e3fb1 [file] [log] [blame]
danilchap50c51362015-11-22 09:03:11 -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/bye.h"
danilchap50c51362015-11-22 09:03:11 -080012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
14#include <cstdint>
danilchap822a16f2016-09-27 09:27:47 -070015#include <utility>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#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"
danilchap50c51362015-11-22 09:03:11 -080021
22namespace webrtc {
23namespace rtcp {
danilchap2f255d82016-10-17 02:07:54 -070024constexpr uint8_t Bye::kPacketType;
danilchap50c51362015-11-22 09:03:11 -080025// Bye packet (BYE) (RFC 3550).
26//
27// 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
28// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29// |V=2|P| SC | PT=BYE=203 | length |
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// | SSRC/CSRC |
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33// : ... :
34// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
35// (opt) | length | reason for leaving ...
36// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37Bye::Bye() : sender_ssrc_(0) {}
38
eladalon8fa21c42017-06-16 07:07:47 -070039Bye::~Bye() = default;
40
danilchap54a20692016-04-05 08:48:11 -070041bool Bye::Parse(const CommonHeader& packet) {
danilchap2f255d82016-10-17 02:07:54 -070042 RTC_DCHECK_EQ(packet.type(), kPacketType);
danilchap50c51362015-11-22 09:03:11 -080043
danilchap54a20692016-04-05 08:48:11 -070044 const uint8_t src_count = packet.count();
danilchap50c51362015-11-22 09:03:11 -080045 // Validate packet.
danilchap54a20692016-04-05 08:48:11 -070046 if (packet.payload_size_bytes() < 4u * src_count) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010047 RTC_LOG(LS_WARNING)
danilchap50c51362015-11-22 09:03:11 -080048 << "Packet is too small to contain CSRCs it promise to have.";
49 return false;
50 }
danilchap54a20692016-04-05 08:48:11 -070051 const uint8_t* const payload = packet.payload();
52 bool has_reason = packet.payload_size_bytes() > 4u * src_count;
danilchap50c51362015-11-22 09:03:11 -080053 uint8_t reason_length = 0;
54 if (has_reason) {
55 reason_length = payload[4u * src_count];
danilchap54a20692016-04-05 08:48:11 -070056 if (packet.payload_size_bytes() - 4u * src_count < 1u + reason_length) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG(LS_WARNING) << "Invalid reason length: " << reason_length;
danilchap50c51362015-11-22 09:03:11 -080058 return false;
59 }
60 }
61 // Once sure packet is valid, copy values.
62 if (src_count == 0) { // A count value of zero is valid, but useless.
63 sender_ssrc_ = 0;
64 csrcs_.clear();
65 } else {
66 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload);
67 csrcs_.resize(src_count - 1);
68 for (size_t i = 1; i < src_count; ++i)
69 csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]);
70 }
71
72 if (has_reason) {
73 reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]),
74 reason_length);
75 } else {
76 reason_.clear();
77 }
78
79 return true;
80}
81
82bool Bye::Create(uint8_t* packet,
83 size_t* index,
84 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010085 PacketReadyCallback callback) const {
danilchap50c51362015-11-22 09:03:11 -080086 while (*index + BlockLength() > max_length) {
87 if (!OnBufferFull(packet, index, callback))
88 return false;
89 }
90 const size_t index_end = *index + BlockLength();
91
92 CreateHeader(1 + csrcs_.size(), kPacketType, HeaderLength(), packet, index);
93 // Store srcs of the leaving clients.
94 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_);
95 *index += sizeof(uint32_t);
96 for (uint32_t csrc : csrcs_) {
97 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc);
98 *index += sizeof(uint32_t);
99 }
100 // Store the reason to leave.
101 if (!reason_.empty()) {
Danil Chapovalov6c170572017-09-15 16:48:14 +0200102 uint8_t reason_length = static_cast<uint8_t>(reason_.size());
danilchap50c51362015-11-22 09:03:11 -0800103 packet[(*index)++] = reason_length;
104 memcpy(&packet[*index], reason_.data(), reason_length);
105 *index += reason_length;
106 // Add padding bytes if needed.
107 size_t bytes_to_pad = index_end - *index;
kwibergaf476c72016-11-28 15:21:39 -0800108 RTC_DCHECK_LE(bytes_to_pad, 3);
danilchap50c51362015-11-22 09:03:11 -0800109 if (bytes_to_pad > 0) {
110 memset(&packet[*index], 0, bytes_to_pad);
111 *index += bytes_to_pad;
112 }
113 }
114 RTC_DCHECK_EQ(index_end, *index);
115 return true;
116}
117
danilchap822a16f2016-09-27 09:27:47 -0700118bool Bye::SetCsrcs(std::vector<uint32_t> csrcs) {
119 if (csrcs.size() > kMaxNumberOfCsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(LS_WARNING) << "Too many CSRCs for Bye packet.";
danilchap50c51362015-11-22 09:03:11 -0800121 return false;
122 }
danilchap822a16f2016-09-27 09:27:47 -0700123 csrcs_ = std::move(csrcs);
danilchap50c51362015-11-22 09:03:11 -0800124 return true;
125}
126
danilchap822a16f2016-09-27 09:27:47 -0700127void Bye::SetReason(std::string reason) {
danilchap50c51362015-11-22 09:03:11 -0800128 RTC_DCHECK_LE(reason.size(), 0xffu);
danilchap822a16f2016-09-27 09:27:47 -0700129 reason_ = std::move(reason);
danilchap50c51362015-11-22 09:03:11 -0800130}
131
132size_t Bye::BlockLength() const {
133 size_t src_count = (1 + csrcs_.size());
134 size_t reason_size_in_32bits = reason_.empty() ? 0 : (reason_.size() / 4 + 1);
135 return kHeaderLength + 4 * (src_count + reason_size_in_32bits);
136}
137
138} // namespace rtcp
139} // namespace webrtc