blob: 6553b89a6334feca0e408bd87185a7eb31397c90 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
danilchap822a16f2016-09-27 09:27:47 -070016#include <utility>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/rtp_rtcp/source/byte_io.h"
19#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
danilchap50c51362015-11-22 09:03:11 -080022
23namespace webrtc {
24namespace rtcp {
danilchap2f255d82016-10-17 02:07:54 -070025constexpr uint8_t Bye::kPacketType;
danilchap50c51362015-11-22 09:03:11 -080026// Bye packet (BYE) (RFC 3550).
27//
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| SC | PT=BYE=203 | length |
31// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32// | SSRC/CSRC |
33// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34// : ... :
35// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
36// (opt) | length | reason for leaving ...
37// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38Bye::Bye() : sender_ssrc_(0) {}
39
eladalon8fa21c42017-06-16 07:07:47 -070040Bye::~Bye() = default;
41
danilchap54a20692016-04-05 08:48:11 -070042bool Bye::Parse(const CommonHeader& packet) {
danilchap2f255d82016-10-17 02:07:54 -070043 RTC_DCHECK_EQ(packet.type(), kPacketType);
danilchap50c51362015-11-22 09:03:11 -080044
danilchap54a20692016-04-05 08:48:11 -070045 const uint8_t src_count = packet.count();
danilchap50c51362015-11-22 09:03:11 -080046 // Validate packet.
danilchap54a20692016-04-05 08:48:11 -070047 if (packet.payload_size_bytes() < 4u * src_count) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010048 RTC_LOG(LS_WARNING)
danilchap50c51362015-11-22 09:03:11 -080049 << "Packet is too small to contain CSRCs it promise to have.";
50 return false;
51 }
danilchap54a20692016-04-05 08:48:11 -070052 const uint8_t* const payload = packet.payload();
53 bool has_reason = packet.payload_size_bytes() > 4u * src_count;
danilchap50c51362015-11-22 09:03:11 -080054 uint8_t reason_length = 0;
55 if (has_reason) {
56 reason_length = payload[4u * src_count];
danilchap54a20692016-04-05 08:48:11 -070057 if (packet.payload_size_bytes() - 4u * src_count < 1u + reason_length) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG(LS_WARNING) << "Invalid reason length: " << reason_length;
danilchap50c51362015-11-22 09:03:11 -080059 return false;
60 }
61 }
62 // Once sure packet is valid, copy values.
63 if (src_count == 0) { // A count value of zero is valid, but useless.
64 sender_ssrc_ = 0;
65 csrcs_.clear();
66 } else {
67 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload);
68 csrcs_.resize(src_count - 1);
69 for (size_t i = 1; i < src_count; ++i)
70 csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]);
71 }
72
73 if (has_reason) {
74 reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]),
75 reason_length);
76 } else {
77 reason_.clear();
78 }
79
80 return true;
81}
82
83bool Bye::Create(uint8_t* packet,
84 size_t* index,
85 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010086 PacketReadyCallback callback) const {
danilchap50c51362015-11-22 09:03:11 -080087 while (*index + BlockLength() > max_length) {
88 if (!OnBufferFull(packet, index, callback))
89 return false;
90 }
91 const size_t index_end = *index + BlockLength();
92
93 CreateHeader(1 + csrcs_.size(), kPacketType, HeaderLength(), packet, index);
94 // Store srcs of the leaving clients.
95 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_);
96 *index += sizeof(uint32_t);
97 for (uint32_t csrc : csrcs_) {
98 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc);
99 *index += sizeof(uint32_t);
100 }
101 // Store the reason to leave.
102 if (!reason_.empty()) {
Danil Chapovalov6c170572017-09-15 16:48:14 +0200103 uint8_t reason_length = static_cast<uint8_t>(reason_.size());
danilchap50c51362015-11-22 09:03:11 -0800104 packet[(*index)++] = reason_length;
105 memcpy(&packet[*index], reason_.data(), reason_length);
106 *index += reason_length;
107 // Add padding bytes if needed.
108 size_t bytes_to_pad = index_end - *index;
kwibergaf476c72016-11-28 15:21:39 -0800109 RTC_DCHECK_LE(bytes_to_pad, 3);
danilchap50c51362015-11-22 09:03:11 -0800110 if (bytes_to_pad > 0) {
111 memset(&packet[*index], 0, bytes_to_pad);
112 *index += bytes_to_pad;
113 }
114 }
115 RTC_DCHECK_EQ(index_end, *index);
116 return true;
117}
118
danilchap822a16f2016-09-27 09:27:47 -0700119bool Bye::SetCsrcs(std::vector<uint32_t> csrcs) {
120 if (csrcs.size() > kMaxNumberOfCsrcs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_WARNING) << "Too many CSRCs for Bye packet.";
danilchap50c51362015-11-22 09:03:11 -0800122 return false;
123 }
danilchap822a16f2016-09-27 09:27:47 -0700124 csrcs_ = std::move(csrcs);
danilchap50c51362015-11-22 09:03:11 -0800125 return true;
126}
127
danilchap822a16f2016-09-27 09:27:47 -0700128void Bye::SetReason(std::string reason) {
danilchap50c51362015-11-22 09:03:11 -0800129 RTC_DCHECK_LE(reason.size(), 0xffu);
danilchap822a16f2016-09-27 09:27:47 -0700130 reason_ = std::move(reason);
danilchap50c51362015-11-22 09:03:11 -0800131}
132
133size_t Bye::BlockLength() const {
134 size_t src_count = (1 + csrcs_.size());
135 size_t reason_size_in_32bits = reason_.empty() ? 0 : (reason_.size() / 4 + 1);
136 return kHeaderLength + 4 * (src_count + reason_size_in_32bits);
137}
138
139} // namespace rtcp
140} // namespace webrtc