Chromium Code Reviews| Index: webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.cc |
| diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.cc b/webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d9d287832329838d032e6aa4b3655112315592a0 |
| --- /dev/null |
| +++ b/webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.cc |
| @@ -0,0 +1,135 @@ |
| +/* |
| + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h" |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/base/logging.h" |
| +#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
| +#include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| + |
| +using webrtc::RTCPUtility::RtcpCommonHeader; |
| + |
| +namespace webrtc { |
| +namespace rtcp { |
| + |
| +// Bye packet (BYE) (RFC 3550). |
| +// |
| +// 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 |
| +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| +// |V=2|P| SC | PT=BYE=203 | length | |
| +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| +// | SSRC/CSRC | |
| +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| +// : ... : |
| +// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| +// (opt) | length | reason for leaving ... |
| +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| +Bye::Bye() : RtcpPacket(), sender_ssrc_(0) {} |
|
sprang_webrtc
2015/11/12 16:28:31
Don't think you explicit RtcpPacket() call?
danilchap
2015/11/13 09:05:42
Wasn't sure what style is preferred, so left like
|
| + |
| +bool Bye::Parse(const RtcpCommonHeader& header, const uint8_t* payload) { |
| + RTC_DCHECK(header.packet_type == kPacketType); |
| + |
| + const uint8_t src_count = header.count_or_format; |
| + // Validate packet. |
| + if (src_count == 0) { |
| + LOG(LS_WARNING) << "Bye packet should have at least 1 src"; |
|
sprang_webrtc
2015/11/12 16:28:31
Bye packet must contain SSRC.
danilchap
2015/11/13 09:05:42
Done.
|
| + return false; |
| + } |
| + if (header.payload_size_bytes < 4u * src_count) { |
| + LOG(LS_WARNING) |
| + << "Packet is too small to contain CSRCs it promise to have"; |
| + return false; |
| + } |
| + bool has_reason = (header.payload_size_bytes > 4u * src_count); |
| + uint8_t reason_length; |
| + if (has_reason) { |
| + reason_length = payload[4u * src_count]; |
| + if (header.payload_size_bytes - 4u * src_count < 1u + reason_length) { |
| + LOG(LS_WARNING) << "Reason length is unreasonable"; |
|
sprang_webrtc
2015/11/12 16:28:31
"Invalid reason length: " << reason_length;
danilchap
2015/11/13 09:05:42
Done.
|
| + return false; |
| + } |
| + } else { |
| + reason_length = 0; |
| + } |
| + // Once sure packet is valid, copy values. |
| + sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload); |
| + csrcs_.resize(src_count - 1); |
| + for (size_t i = 1; i < src_count; ++i) |
| + csrcs_[i - 1] = ByteReader<uint32_t>::ReadBigEndian(&payload[4 * i]); |
| + |
| + if (has_reason) |
| + reason_.assign(reinterpret_cast<const char*>(&payload[4u * src_count + 1]), |
| + reason_length); |
| + else |
| + reason_.clear(); |
|
sprang_webrtc
2015/11/12 16:28:31
Use brackets:
if () {
...
} else {
...
}
danilchap
2015/11/13 09:05:42
Done.
|
| + |
| + return true; |
| +} |
| + |
| +bool Bye::Create(uint8_t* packet, |
| + size_t* index, |
| + size_t max_length, |
| + RtcpPacket::PacketReadyCallback* callback) const { |
| + while (*index + BlockLength() > max_length) { |
| + if (!OnBufferFull(packet, index, callback)) |
| + return false; |
| + } |
| + const size_t index_end = *index + BlockLength(); |
| + RTC_DCHECK_EQ(0u, BlockLength() % 4u); |
| + size_t size_in_32bits = (BlockLength() - kHeaderLength) / 4u; |
| + |
| + CreateHeader(1 + csrcs_.size(), kPacketType, size_in_32bits, packet, index); |
| + // Store srcs of the leaving clients. |
| + ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], sender_ssrc_); |
| + *index += sizeof(uint32_t); |
| + for (uint32_t csrc : csrcs_) { |
| + ByteWriter<uint32_t>::WriteBigEndian(&packet[*index], csrc); |
| + *index += sizeof(uint32_t); |
| + } |
| + // Store the reason to leave. |
| + if (!reason_.empty()) { |
| + uint8_t reason_length = reason_.size(); |
| + packet[(*index)++] = reason_length; |
| + memcpy(&packet[*index], reason_.data(), reason_length); |
| + *index += reason_length; |
| + // Add padding bytes if needed. |
| + size_t bytes_to_pad = index_end - *index; |
| + RTC_DCHECK_LE(bytes_to_pad, 3u); |
| + if (bytes_to_pad > 0) { |
| + memset(&packet[*index], 0, bytes_to_pad); |
| + *index += bytes_to_pad; |
| + } |
| + } |
| + RTC_DCHECK_EQ(index_end, *index); |
| + return true; |
| +} |
| + |
| +bool Bye::WithCsrc(uint32_t csrc) { |
| + if (csrcs_.size() >= kMaxNumberOfCsrcs) { |
| + LOG(LS_WARNING) << "Max CSRC size reached."; |
| + return false; |
| + } |
| + csrcs_.push_back(csrc); |
| + return true; |
| +} |
| + |
| +bool Bye::WithReason(const std::string& reason) { |
| + if (reason.size() > 0xff) { |
| + LOG(LS_WARNING) << "Reason length should fit in one byte," |
| + " i.e. be less than 256"; |
| + return false; |
| + } |
| + reason_ = reason; |
| + return true; |
| +} |
| + |
| +} // namespace rtcp |
| +} // namespace webrtc |