Line data Source code
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 :
11 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
12 :
13 : #include <utility>
14 :
15 : #include "webrtc/base/checks.h"
16 : #include "webrtc/base/logging.h"
17 : #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
18 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
19 :
20 : namespace webrtc {
21 : namespace rtcp {
22 : constexpr uint8_t Bye::kPacketType;
23 : // 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 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 0 : Bye::Bye() : sender_ssrc_(0) {}
36 :
37 0 : bool Bye::Parse(const CommonHeader& packet) {
38 0 : RTC_DCHECK_EQ(packet.type(), kPacketType);
39 :
40 0 : const uint8_t src_count = packet.count();
41 : // Validate packet.
42 0 : if (packet.payload_size_bytes() < 4u * src_count) {
43 0 : LOG(LS_WARNING)
44 0 : << "Packet is too small to contain CSRCs it promise to have.";
45 0 : return false;
46 : }
47 0 : const uint8_t* const payload = packet.payload();
48 0 : bool has_reason = packet.payload_size_bytes() > 4u * src_count;
49 0 : uint8_t reason_length = 0;
50 0 : if (has_reason) {
51 0 : reason_length = payload[4u * src_count];
52 0 : if (packet.payload_size_bytes() - 4u * src_count < 1u + reason_length) {
53 0 : LOG(LS_WARNING) << "Invalid reason length: " << reason_length;
54 0 : return false;
55 : }
56 : }
57 : // Once sure packet is valid, copy values.
58 0 : if (src_count == 0) { // A count value of zero is valid, but useless.
59 0 : sender_ssrc_ = 0;
60 0 : csrcs_.clear();
61 : } else {
62 0 : sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload);
63 0 : csrcs_.resize(src_count - 1);
64 0 : for (size_t i = 1; i < src_count; ++i)
65 0 : csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]);
66 : }
67 :
68 0 : if (has_reason) {
69 0 : reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]),
70 0 : reason_length);
71 : } else {
72 0 : reason_.clear();
73 : }
74 :
75 0 : return true;
76 : }
77 :
78 0 : bool Bye::Create(uint8_t* packet,
79 : size_t* index,
80 : size_t max_length,
81 : RtcpPacket::PacketReadyCallback* callback) const {
82 0 : while (*index + BlockLength() > max_length) {
83 0 : if (!OnBufferFull(packet, index, callback))
84 0 : return false;
85 : }
86 0 : const size_t index_end = *index + BlockLength();
87 :
88 0 : CreateHeader(1 + csrcs_.size(), kPacketType, HeaderLength(), packet, index);
89 : // Store srcs of the leaving clients.
90 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_);
91 0 : *index += sizeof(uint32_t);
92 0 : for (uint32_t csrc : csrcs_) {
93 0 : ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc);
94 0 : *index += sizeof(uint32_t);
95 : }
96 : // Store the reason to leave.
97 0 : if (!reason_.empty()) {
98 0 : uint8_t reason_length = reason_.size();
99 0 : packet[(*index)++] = reason_length;
100 0 : memcpy(&packet[*index], reason_.data(), reason_length);
101 0 : *index += reason_length;
102 : // Add padding bytes if needed.
103 0 : size_t bytes_to_pad = index_end - *index;
104 0 : RTC_DCHECK_LE(bytes_to_pad, 3);
105 0 : if (bytes_to_pad > 0) {
106 0 : memset(&packet[*index], 0, bytes_to_pad);
107 0 : *index += bytes_to_pad;
108 : }
109 : }
110 0 : RTC_DCHECK_EQ(index_end, *index);
111 0 : return true;
112 : }
113 :
114 0 : bool Bye::SetCsrcs(std::vector<uint32_t> csrcs) {
115 0 : if (csrcs.size() > kMaxNumberOfCsrcs) {
116 0 : LOG(LS_WARNING) << "Too many CSRCs for Bye packet.";
117 0 : return false;
118 : }
119 0 : csrcs_ = std::move(csrcs);
120 0 : return true;
121 : }
122 :
123 0 : void Bye::SetReason(std::string reason) {
124 0 : RTC_DCHECK_LE(reason.size(), 0xffu);
125 0 : reason_ = std::move(reason);
126 0 : }
127 :
128 0 : size_t Bye::BlockLength() const {
129 0 : size_t src_count = (1 + csrcs_.size());
130 0 : size_t reason_size_in_32bits = reason_.empty() ? 0 : (reason_.size() / 4 + 1);
131 0 : return kHeaderLength + 4 * (src_count + reason_size_in_32bits);
132 : }
133 :
134 : } // namespace rtcp
135 : } // namespace webrtc
|