blob: 0e2eb9e41a3e1d2c27652c5e10580f61999973a4 [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
danilchap822a16f2016-09-27 09:27:47 -070013#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/byte_io.h"
16#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
danilchap50c51362015-11-22 09:03:11 -080019
20namespace webrtc {
21namespace rtcp {
danilchap2f255d82016-10-17 02:07:54 -070022constexpr uint8_t Bye::kPacketType;
danilchap50c51362015-11-22 09:03:11 -080023// Bye packet (BYE) (RFC 3550).
24//
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| SC | PT=BYE=203 | length |
28// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29// | SSRC/CSRC |
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// : ... :
32// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
33// (opt) | length | reason for leaving ...
34// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35Bye::Bye() : sender_ssrc_(0) {}
36
eladalon8fa21c42017-06-16 07:07:47 -070037Bye::~Bye() = default;
38
danilchap54a20692016-04-05 08:48:11 -070039bool Bye::Parse(const CommonHeader& packet) {
danilchap2f255d82016-10-17 02:07:54 -070040 RTC_DCHECK_EQ(packet.type(), kPacketType);
danilchap50c51362015-11-22 09:03:11 -080041
danilchap54a20692016-04-05 08:48:11 -070042 const uint8_t src_count = packet.count();
danilchap50c51362015-11-22 09:03:11 -080043 // Validate packet.
danilchap54a20692016-04-05 08:48:11 -070044 if (packet.payload_size_bytes() < 4u * src_count) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010045 RTC_LOG(LS_WARNING)
danilchap50c51362015-11-22 09:03:11 -080046 << "Packet is too small to contain CSRCs it promise to have.";
47 return false;
48 }
danilchap54a20692016-04-05 08:48:11 -070049 const uint8_t* const payload = packet.payload();
50 bool has_reason = packet.payload_size_bytes() > 4u * src_count;
danilchap50c51362015-11-22 09:03:11 -080051 uint8_t reason_length = 0;
52 if (has_reason) {
53 reason_length = payload[4u * src_count];
danilchap54a20692016-04-05 08:48:11 -070054 if (packet.payload_size_bytes() - 4u * src_count < 1u + reason_length) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010055 RTC_LOG(LS_WARNING) << "Invalid reason length: " << reason_length;
danilchap50c51362015-11-22 09:03:11 -080056 return false;
57 }
58 }
59 // Once sure packet is valid, copy values.
60 if (src_count == 0) { // A count value of zero is valid, but useless.
61 sender_ssrc_ = 0;
62 csrcs_.clear();
63 } else {
64 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload);
65 csrcs_.resize(src_count - 1);
66 for (size_t i = 1; i < src_count; ++i)
67 csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]);
68 }
69
70 if (has_reason) {
71 reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]),
72 reason_length);
73 } else {
74 reason_.clear();
75 }
76
77 return true;
78}
79
80bool Bye::Create(uint8_t* packet,
81 size_t* index,
82 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010083 PacketReadyCallback callback) const {
danilchap50c51362015-11-22 09:03:11 -080084 while (*index + BlockLength() > max_length) {
85 if (!OnBufferFull(packet, index, callback))
86 return false;
87 }
88 const size_t index_end = *index + BlockLength();
89
90 CreateHeader(1 + csrcs_.size(), kPacketType, HeaderLength(), packet, index);
91 // Store srcs of the leaving clients.
92 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_);
93 *index += sizeof(uint32_t);
94 for (uint32_t csrc : csrcs_) {
95 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc);
96 *index += sizeof(uint32_t);
97 }
98 // Store the reason to leave.
99 if (!reason_.empty()) {
Danil Chapovalov6c170572017-09-15 16:48:14 +0200100 uint8_t reason_length = static_cast<uint8_t>(reason_.size());
danilchap50c51362015-11-22 09:03:11 -0800101 packet[(*index)++] = reason_length;
102 memcpy(&packet[*index], reason_.data(), reason_length);
103 *index += reason_length;
104 // Add padding bytes if needed.
105 size_t bytes_to_pad = index_end - *index;
kwibergaf476c72016-11-28 15:21:39 -0800106 RTC_DCHECK_LE(bytes_to_pad, 3);
danilchap50c51362015-11-22 09:03:11 -0800107 if (bytes_to_pad > 0) {
108 memset(&packet[*index], 0, bytes_to_pad);
109 *index += bytes_to_pad;
110 }
111 }
112 RTC_DCHECK_EQ(index_end, *index);
113 return true;
114}
115
danilchap822a16f2016-09-27 09:27:47 -0700116bool Bye::SetCsrcs(std::vector<uint32_t> csrcs) {
117 if (csrcs.size() > kMaxNumberOfCsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100118 RTC_LOG(LS_WARNING) << "Too many CSRCs for Bye packet.";
danilchap50c51362015-11-22 09:03:11 -0800119 return false;
120 }
danilchap822a16f2016-09-27 09:27:47 -0700121 csrcs_ = std::move(csrcs);
danilchap50c51362015-11-22 09:03:11 -0800122 return true;
123}
124
danilchap822a16f2016-09-27 09:27:47 -0700125void Bye::SetReason(std::string reason) {
danilchap50c51362015-11-22 09:03:11 -0800126 RTC_DCHECK_LE(reason.size(), 0xffu);
danilchap822a16f2016-09-27 09:27:47 -0700127 reason_ = std::move(reason);
danilchap50c51362015-11-22 09:03:11 -0800128}
129
130size_t Bye::BlockLength() const {
131 size_t src_count = (1 + csrcs_.size());
132 size_t reason_size_in_32bits = reason_.empty() ? 0 : (reason_.size() / 4 + 1);
133 return kHeaderLength + 4 * (src_count + reason_size_in_32bits);
134}
135
136} // namespace rtcp
137} // namespace webrtc