[fix] fix rtcp common header

This commit is contained in:
dijunkun
2025-02-26 17:30:24 +08:00
parent ee70280056
commit b7a5066c6b
29 changed files with 334 additions and 259 deletions

View File

@@ -21,8 +21,8 @@
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "byte_io.h"
#include "common_header.h"
#include "log.h"
#include "rtcp_common_header.h"
namespace webrtc {
namespace rtcp {
@@ -261,7 +261,7 @@ size_t CongestionControlFeedback::BlockLength() const {
return total_size;
}
bool CongestionControlFeedback::Parse(const CommonHeader& packet) {
bool CongestionControlFeedback::Parse(const RtcpCommonHeader& packet) {
const uint8_t* payload = packet.payload();
const uint8_t* payload_end = packet.payload() + packet.payload_size_bytes();

View File

@@ -17,7 +17,7 @@
#include "api/array_view.h"
#include "api/transport/ecn_marking.h"
#include "api/units/time_delta.h"
#include "common_header.h"
#include "rtcp_common_header.h"
#include "rtp_feedback.h"
namespace webrtc {
@@ -45,7 +45,7 @@ class CongestionControlFeedback : public RtpFeedback {
uint32_t report_timestamp_compact_ntp);
CongestionControlFeedback() = default;
bool Parse(const CommonHeader& packet);
bool Parse(const RtcpCommonHeader& packet);
rtc::ArrayView<const PacketInfo> packets() const { return packets_; }

View File

@@ -15,8 +15,8 @@
#include <utility>
#include "byte_io.h"
#include "common_header.h"
#include "log.h"
#include "rtcp_common_header.h"
namespace webrtc {
namespace rtcp {
@@ -48,7 +48,7 @@ Nack::Nack() = default;
Nack::Nack(const Nack& rhs) = default;
Nack::~Nack() = default;
bool Nack::Parse(const CommonHeader& packet) {
bool Nack::Parse(const RtcpCommonHeader& packet) {
if (packet.payload_size_bytes() < kCommonFeedbackLength + kNackItemLength) {
LOG_WARN("Payload length {} is too small for a Nack.",
packet.payload_size_bytes());

View File

@@ -13,7 +13,7 @@
#include <vector>
#include "common_header.h"
#include "rtcp_common_header.h"
#include "rtp_feedback.h"
namespace webrtc {
@@ -26,7 +26,7 @@ class Nack : public RtpFeedback {
~Nack() override;
// Parse assumes header is already parsed and validated.
bool Parse(const CommonHeader& packet);
bool Parse(const RtcpCommonHeader& packet);
void SetPacketIds(const uint16_t* nack_list, size_t length);
void SetPacketIds(std::vector<uint16_t> nack_list);

View File

@@ -1,83 +0,0 @@
/*
* Copyright (c) 2016 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 "common_header.h"
#include "byte_io.h"
#include "log.h"
// 0 1 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 0 |V=2|P| C/F |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 1 | Packet Type |
// ----------------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 2 | length |
// --------------------------------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// Common header for all RTCP packets, 4 octets.
bool CommonHeader::Parse(const uint8_t* buffer, size_t size_bytes) {
const uint8_t kVersion = 2;
if (size_bytes < kHeaderSizeBytes) {
LOG_WARN(
"Too little data ({} byte{}) remaining in buffer to parse RTCP header "
"(4 bytes).",
size_bytes, (size_bytes != 1 ? "s" : ""));
return false;
}
uint8_t version = buffer[0] >> 6;
if (version != kVersion) {
LOG_WARN("Invalid RTCP header: Version must be {} but was {}",
static_cast<int>(kVersion), static_cast<int>(version));
return false;
}
bool has_padding = (buffer[0] & 0x20) != 0;
count_or_format_ = buffer[0] & 0x1F;
packet_type_ = buffer[1];
payload_size_ = ByteReader<uint16_t>::ReadBigEndian(&buffer[2]) * 4;
payload_ = buffer + kHeaderSizeBytes;
padding_size_ = 0;
if (size_bytes < kHeaderSizeBytes + payload_size_) {
LOG_WARN(
"Buffer too small ({} bytes) to fit an RtcpPacket with a header and {} "
"bytes.",
size_bytes, payload_size_);
return false;
}
if (has_padding) {
if (payload_size_ == 0) {
LOG_WARN(
"Invalid RTCP header: Padding bit set but 0 payload size specified.");
return false;
}
padding_size_ = payload_[payload_size_ - 1];
if (padding_size_ == 0) {
LOG_WARN(
"Invalid RTCP header: Padding bit set but 0 padding size specified.");
return false;
}
if (padding_size_ > payload_size_) {
LOG_WARN(
"Invalid RTCP header: Too many padding bytes ({}) for a packet "
"payload size of {} bytes.",
padding_size_, payload_size_);
return false;
}
payload_size_ -= padding_size_;
}
return true;
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) 2016 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.
*/
#ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_COMMON_HEADER_H_
#define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_COMMON_HEADER_H_
#include <stddef.h>
#include <stdint.h>
class CommonHeader {
public:
static constexpr size_t kHeaderSizeBytes = 4;
CommonHeader() {}
CommonHeader(const CommonHeader&) = default;
CommonHeader& operator=(const CommonHeader&) = default;
bool Parse(const uint8_t* buffer, size_t size_bytes);
uint8_t type() const { return packet_type_; }
// Depending on packet type same header field can be used either as count or
// as feedback message type (fmt). Caller expected to know how it is used.
uint8_t fmt() const { return count_or_format_; }
uint8_t count() const { return count_or_format_; }
size_t payload_size_bytes() const { return payload_size_; }
const uint8_t* payload() const { return payload_; }
size_t packet_size() const {
return kHeaderSizeBytes + payload_size_ + padding_size_;
}
// Returns pointer to the next RTCP packet in compound packet.
const uint8_t* NextPacket() const {
return payload_ + payload_size_ + padding_size_;
}
private:
uint8_t packet_type_ = 0;
uint8_t count_or_format_ = 0;
uint8_t padding_size_ = 0;
uint32_t payload_size_ = 0;
const uint8_t* payload_ = nullptr;
};
#endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_COMMON_HEADER_H_

View File

@@ -19,7 +19,7 @@ void ReceiverReport::SetReportBlocks(
reports_ = std::move(rtcp_report_blocks);
}
const uint8_t *ReceiverReport::Create() {
const uint8_t *ReceiverReport::Build() {
size_t buffer_size =
DEFAULT_SR_SIZE + reports_.size() * RtcpReportBlock::kLength;
if (!buffer_ || buffer_size != size_) {
@@ -41,13 +41,13 @@ const uint8_t *ReceiverReport::Create() {
return buffer_;
}
size_t ReceiverReport::Parse(const uint8_t *buffer) {
size_t ReceiverReport::Parse(const RtcpCommonHeader &packet) {
reports_.clear();
size_t pos = rtcp_common_header_.Parse(buffer);
size_t pos = packet.payload_size_bytes();
for (int i = 0; i < rtcp_common_header_.CountOrFormat(); i++) {
for (int i = 0; i < rtcp_common_header_.fmt(); i++) {
RtcpReportBlock report;
pos += report.Parse(buffer + pos);
pos += report.Parse(buffer_ + pos);
reports_.emplace_back(std::move(report));
}

View File

@@ -48,8 +48,8 @@ class ReceiverReport {
void SetReportBlock(RtcpReportBlock &rtcp_report_block);
void SetReportBlocks(std::vector<RtcpReportBlock> &rtcp_report_blocks);
const uint8_t *Create();
size_t Parse(const uint8_t *buffer);
const uint8_t *Build();
size_t Parse(const RtcpCommonHeader &packet);
const uint8_t *Buffer() const { return buffer_; }
size_t Size() const { return size_; }

View File

@@ -1,51 +1,99 @@
/*
* Copyright (c) 2016 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 "rtcp_common_header.h"
#include "byte_io.h"
#include "log.h"
RtcpCommonHeader::RtcpCommonHeader()
: version_(0),
padding_(0),
count_or_format_(0),
payload_type_(PAYLOAD_TYPE::UNKNOWN),
length_(0) {}
// 0 1 1 2 3
// 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
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 0 |V=2|P| C/F |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 1 | Packet Type |
// ----------------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// 2 | length |
// --------------------------------+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// Common header for all RTCP packets, 4 octets.
RtcpCommonHeader::RtcpCommonHeader(const uint8_t* buffer, uint32_t size) {
if (size < 4) {
version_ = 2;
padding_ = 0;
count_or_format_ = 0;
payload_type_ = PAYLOAD_TYPE::UNKNOWN;
length_ = 0;
} else {
version_ = buffer[0] >> 6;
padding_ = buffer[0] >> 5 & 0x01;
count_or_format_ = buffer[0] & 0x1F;
payload_type_ = PAYLOAD_TYPE(buffer[1]);
length_ = (buffer[2] << 8) + buffer[3];
}
}
RtcpCommonHeader::~RtcpCommonHeader() {}
int RtcpCommonHeader::Create(uint8_t version, uint8_t padding,
int RtcpCommonHeader::Create(uint8_t version, uint8_t has_padding,
uint8_t count_or_format, uint8_t payload_type,
uint16_t length, uint8_t* buffer) {
if (!buffer) {
return 0;
}
buffer[0] = (version << 6) | (padding << 5) | (count_or_format << 4);
uint16_t payload_size = length - kHeaderSizeBytes;
buffer[0] = (version << 6) | (has_padding << 5) | (count_or_format << 4);
buffer[1] = payload_type;
buffer[2] = length >> 8 & 0xFF;
buffer[3] = length & 0xFF;
buffer[2] = payload_size >> 8 & 0xFF;
buffer[3] = payload_size & 0xFF;
return 4;
}
size_t RtcpCommonHeader::Parse(const uint8_t* buffer) {
version_ = buffer[0] >> 6;
padding_ = buffer[0] >> 5 & 0x01;
bool RtcpCommonHeader::Parse(const uint8_t* buffer, size_t size_bytes) {
const uint8_t kVersion = 2;
if (size_bytes < kHeaderSizeBytes) {
LOG_WARN(
"Too little data ({} byte{}) remaining in buffer to parse RTCP header "
"(4 bytes).",
size_bytes, (size_bytes != 1 ? "s" : ""));
return false;
}
uint8_t version = buffer[0] >> 6;
if (version != kVersion) {
LOG_WARN("Invalid RTCP header: Version must be {} but was {}",
static_cast<int>(kVersion), static_cast<int>(version));
return false;
}
bool has_padding = (buffer[0] & 0x20) != 0;
count_or_format_ = buffer[0] & 0x1F;
payload_type_ = PAYLOAD_TYPE(buffer[1]);
length_ = (buffer[2] << 8) + buffer[3];
return 4;
}
packet_type_ = buffer[1];
payload_size_ = buffer[2] << 8 | buffer[3];
payload_ = buffer + kHeaderSizeBytes;
padding_size_ = 0;
if (size_bytes < kHeaderSizeBytes + payload_size_) {
LOG_WARN(
"Buffer too small ({} bytes) to fit an RtcpPacket with a header and {} "
"bytes.",
size_bytes, payload_size_);
return false;
}
if (has_padding) {
if (payload_size_ == 0) {
LOG_WARN(
"Invalid RTCP header: Padding bit set but 0 payload size specified.");
return false;
}
padding_size_ = payload_[payload_size_ - 1];
if (padding_size_ == 0) {
LOG_WARN(
"Invalid RTCP header: Padding bit set but 0 padding size specified.");
return false;
}
if (padding_size_ > payload_size_) {
LOG_WARN(
"Invalid RTCP header: Too many padding bytes ({}) for a packet "
"payload size of {} bytes.",
padding_size_, payload_size_);
return false;
}
payload_size_ -= padding_size_;
}
return true;
}

View File

@@ -1,65 +1,50 @@
#ifndef _RTCP_HEADER_H_
#define _RTCP_HEADER_H_
/*
* @Author: DI JUNKUN
* @Date: 2025-02-26
* Copyright (c) 2025 by DI JUNKUN, All Rights Reserved.
*/
#include <cstddef>
#include <cstdint>
#ifndef _RTCP_COMMON_HEADER_H_
#define _RTCP_COMMON_HEADER_H_
// RTCP header
// 0 1 2 3
// 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| RC | PT=SR=200 | length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include <stddef.h>
#include <stdint.h>
#include "rtcp_typedef.h"
class RtcpCommonHeader {
public:
typedef enum {
UNKNOWN = 0,
SR = 200,
RR = 201,
SDES = 202,
BYE = 203,
APP = 204
} PAYLOAD_TYPE;
static constexpr size_t kHeaderSizeBytes = 4;
public:
RtcpCommonHeader();
RtcpCommonHeader(const uint8_t* buffer, uint32_t size);
~RtcpCommonHeader();
RtcpCommonHeader() {}
RtcpCommonHeader(const RtcpCommonHeader&) = default;
RtcpCommonHeader& operator=(const RtcpCommonHeader&) = default;
public:
void SetVerion(uint8_t version) { version_ = version; }
void SetPadding(uint8_t padding) { padding_ = padding; }
void SetCountOrFormat(uint8_t count_or_format) {
count_or_format_ = count_or_format;
}
void SetPayloadType(PAYLOAD_TYPE payload_type) {
payload_type_ = payload_type;
}
void SetLength(uint16_t length) { length_ = length; }
public:
uint8_t Verion() const { return version_; }
uint8_t Padding() const { return padding_; }
uint8_t CountOrFormat() const { return count_or_format_; }
PAYLOAD_TYPE PayloadType() const {
return PAYLOAD_TYPE((uint8_t)payload_type_);
}
uint16_t Length() const { return length_; }
int Create(uint8_t version, uint8_t padding, uint8_t count_or_format,
int Create(uint8_t version, uint8_t has_padding, uint8_t count_or_format,
uint8_t payload_type, uint16_t length, uint8_t* buffer);
bool Parse(const uint8_t* buffer, size_t size_bytes);
size_t Parse(const uint8_t* buffer);
uint8_t type() const { return packet_type_; }
// Depending on packet type same header field can be used either as count or
// as feedback message type (fmt). Caller expected to know how it is used.
uint8_t fmt() const { return count_or_format_; }
uint8_t count() const { return count_or_format_; }
size_t payload_size_bytes() const { return payload_size_; }
const uint8_t* payload() const { return payload_; }
size_t packet_size() const {
return kHeaderSizeBytes + payload_size_ + padding_size_;
}
// Returns pointer to the next RTCP packet in compound packet.
const uint8_t* NextPacket() const {
return payload_ + payload_size_ + padding_size_;
}
private:
uint8_t version_ : 2;
uint8_t padding_ : 1;
uint8_t count_or_format_ : 5;
PAYLOAD_TYPE payload_type_ : 8;
uint16_t length_ : 16;
uint8_t packet_type_ = 0;
uint8_t count_or_format_ = 0;
uint8_t padding_size_ = 0;
uint32_t payload_size_ = 0;
const uint8_t* payload_ = nullptr;
};
#endif

View File

@@ -20,7 +20,7 @@ void SenderReport::SetReportBlocks(
reports_ = std::move(rtcp_report_blocks);
}
const uint8_t *SenderReport::Create() {
const uint8_t *SenderReport::Build() {
size_t buffer_size =
DEFAULT_SR_SIZE + reports_.size() * RtcpReportBlock::kLength;
if (!buffer_ || buffer_size != size_) {
@@ -71,9 +71,9 @@ const uint8_t *SenderReport::Create() {
return buffer_;
}
size_t SenderReport::Parse() {
size_t SenderReport::Parse(const RtcpCommonHeader &packet) {
reports_.clear();
size_t pos = rtcp_common_header_.Parse(buffer_);
size_t pos = packet.payload_size_bytes();
sender_info_.sender_ssrc = (buffer_[pos] << 24) + (buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
@@ -96,7 +96,7 @@ size_t SenderReport::Parse() {
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
pos += 4;
for (int i = 0; i < rtcp_common_header_.CountOrFormat(); i++) {
for (int i = 0; i < rtcp_common_header_.fmt(); i++) {
RtcpReportBlock report;
pos += report.Parse(buffer_ + pos);
reports_.emplace_back(std::move(report));

View File

@@ -78,9 +78,19 @@ class SenderReport {
void SetReportBlock(RtcpReportBlock &rtcp_report_block);
void SetReportBlocks(std::vector<RtcpReportBlock> &rtcp_report_blocks);
uint32_t SenderSsrc() const { return sender_info_.sender_ssrc; }
uint64_t NtpTimestamp() const {
return (sender_info_.ntp_ts_msw << 32) | sender_info_.ntp_ts_lsw;
}
uint32_t Timestamp() const { return sender_info_.rtp_ts; }
uint32_t SenderPacketCount() const {
return sender_info_.sender_packet_count;
}
uint32_t SenderOctetCount() const { return sender_info_.sender_octet_count; }
public:
const uint8_t *Create();
size_t Parse();
const uint8_t *Build();
size_t Parse(const RtcpCommonHeader &packet);
// Entire RTP buffer
const uint8_t *Buffer() const { return buffer_; }

View File

@@ -10,7 +10,7 @@
#include <stddef.h>
#include <stdint.h>
#include "common_header.h"
#include "rtcp_common_header.h"
#include "rtcp_packet.h"
// RTPFB: Transport layer feedback message.