mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
[fix] fix rtcp common header
This commit is contained in:
@@ -22,8 +22,29 @@ class AudioChannelReceive {
|
||||
public:
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_audio_receiver_) {
|
||||
return rtp_audio_receiver_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t GetRemoteSsrc() {
|
||||
if (rtp_audio_receiver_) {
|
||||
return rtp_audio_receiver_->GetRemoteSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int OnReceiveRtpPacket(const char *data, size_t size);
|
||||
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time) {
|
||||
if (rtp_audio_receiver_) {
|
||||
rtp_audio_receiver_->OnSenderReport(now_time, ntp_time);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
|
||||
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
|
||||
|
||||
@@ -21,6 +21,14 @@ class AudioChannelSend {
|
||||
public:
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_audio_sender_) {
|
||||
return rtp_audio_sender_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendAudio(char *data, size_t size);
|
||||
|
||||
private:
|
||||
|
||||
@@ -21,8 +21,29 @@ class DataChannelReceive {
|
||||
public:
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_data_receiver_) {
|
||||
return rtp_data_receiver_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t GetRemoteSsrc() {
|
||||
if (rtp_data_receiver_) {
|
||||
return rtp_data_receiver_->GetRemoteSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int OnReceiveRtpPacket(const char *data, size_t size);
|
||||
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time) {
|
||||
if (rtp_data_receiver_) {
|
||||
rtp_data_receiver_->OnSenderReport(now_time, ntp_time);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
|
||||
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
|
||||
|
||||
@@ -21,6 +21,14 @@ class DataChannelSend {
|
||||
public:
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_data_sender_) {
|
||||
return rtp_data_sender_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
|
||||
private:
|
||||
|
||||
@@ -29,6 +29,10 @@ class RtpAudioReceiver {
|
||||
std::function<void(const char*, size_t)> on_receive_data) {
|
||||
on_receive_data_ = on_receive_data;
|
||||
}
|
||||
uint32_t GetSsrc() { return ssrc_; }
|
||||
uint32_t GetRemoteSsrc() { return remote_ssrc_; }
|
||||
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time) {}
|
||||
|
||||
private:
|
||||
bool CheckIsTimeSendRR();
|
||||
@@ -46,6 +50,12 @@ class RtpAudioReceiver {
|
||||
uint32_t total_rtp_packets_recv_ = 0;
|
||||
uint32_t last_send_rtcp_rr_packet_ts_ = 0;
|
||||
std::function<int(const char*, size_t)> data_send_func_ = nullptr;
|
||||
|
||||
uint32_t ssrc_ = 0;
|
||||
uint32_t remote_ssrc_ = 0;
|
||||
|
||||
uint32_t last_sr_ = 0;
|
||||
uint32_t last_delay_ = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -24,6 +24,10 @@ class RtpDataReceiver {
|
||||
on_receive_data_ = on_receive_data;
|
||||
}
|
||||
|
||||
uint32_t GetSsrc() { return ssrc_; }
|
||||
uint32_t GetRemoteSsrc() { return remote_ssrc_; }
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time) {}
|
||||
|
||||
private:
|
||||
bool CheckIsTimeSendRR();
|
||||
int SendRtcpRR(ReceiverReport& rtcp_rr);
|
||||
@@ -39,8 +43,13 @@ class RtpDataReceiver {
|
||||
uint32_t total_rtp_payload_recv_ = 0;
|
||||
uint32_t total_rtp_packets_recv_ = 0;
|
||||
|
||||
uint32_t ssrc_ = 0;
|
||||
uint32_t remote_ssrc_ = 0;
|
||||
uint32_t last_send_rtcp_rr_packet_ts_ = 0;
|
||||
std::function<int(const char*, size_t)> data_send_func_ = nullptr;
|
||||
|
||||
uint32_t last_sr_ = 0;
|
||||
uint32_t last_delay_ = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -545,3 +545,32 @@ void RtpVideoReceiver::SendLossNotification(uint16_t last_decoded_seq_num,
|
||||
uint16_t last_received_seq_num,
|
||||
bool decodability_flag,
|
||||
bool buffering_allowed) {}
|
||||
|
||||
inline uint32_t DivideRoundToNearest(int64_t dividend, int64_t divisor) {
|
||||
if (dividend < 0) {
|
||||
int64_t half_of_divisor = divisor / 2;
|
||||
int64_t quotient = dividend / divisor;
|
||||
int64_t remainder = dividend % divisor;
|
||||
if (-remainder > half_of_divisor) {
|
||||
--quotient;
|
||||
}
|
||||
return quotient;
|
||||
}
|
||||
|
||||
int64_t half_of_divisor = (divisor - 1) / 2;
|
||||
int64_t quotient = dividend / divisor;
|
||||
int64_t remainder = dividend % divisor;
|
||||
if (remainder > half_of_divisor) {
|
||||
++quotient;
|
||||
}
|
||||
return quotient;
|
||||
}
|
||||
|
||||
void RtpVideoReceiver::OnSenderReport(int64_t now_time, uint64_t ntp_time) {
|
||||
last_sr_ = ((uint32_t)(ntp_time / 0x100000000) << 16) |
|
||||
((uint32_t)(ntp_time % 0x100000000) >> 16);
|
||||
last_delay_ = DivideRoundToNearest(
|
||||
(clock_->CurrentTime().us() - now_time) * 0x10000, 1000000);
|
||||
|
||||
LOG_WARN("OnSenderReport [{}][{}]", last_sr_, last_delay_);
|
||||
}
|
||||
@@ -43,6 +43,9 @@ class RtpVideoReceiver : public ThreadBase,
|
||||
std::function<void(VideoFrame&)> on_receive_complete_frame) {
|
||||
on_receive_complete_frame_ = on_receive_complete_frame;
|
||||
}
|
||||
uint32_t GetSsrc() { return ssrc_; }
|
||||
uint32_t GetRemoteSsrc() { return remote_ssrc_; }
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time);
|
||||
|
||||
private:
|
||||
void ProcessAv1RtpPacket(RtpPacketAv1& rtp_packet_av1);
|
||||
@@ -114,15 +117,19 @@ class RtpVideoReceiver : public ThreadBase,
|
||||
int rtcp_tcc_interval_ms_ = 200;
|
||||
|
||||
private:
|
||||
uint32_t ssrc_ = 0;
|
||||
uint32_t remote_ssrc_ = 0;
|
||||
std::shared_ptr<webrtc::Clock> clock_;
|
||||
ReceiveSideCongestionController receive_side_congestion_controller_;
|
||||
RtcpFeedbackSenderInterface* active_remb_module_;
|
||||
uint32_t feedback_ssrc_ = 0;
|
||||
uint32_t remote_ssrc_ = 0;
|
||||
|
||||
std::unique_ptr<RtcpSender> rtcp_sender_;
|
||||
std::unique_ptr<NackRequester> nack_;
|
||||
|
||||
uint32_t last_sr_ = 0;
|
||||
uint32_t last_delay_ = 0;
|
||||
|
||||
private:
|
||||
FILE* file_rtp_recv_ = nullptr;
|
||||
};
|
||||
|
||||
@@ -118,18 +118,7 @@ int RtpVideoSender::SendRtpPacket(
|
||||
rtcp_sr.SetSenderPacketCount(total_rtp_packets_sent_);
|
||||
rtcp_sr.SetSenderOctetCount(total_rtp_payload_sent_);
|
||||
|
||||
RtcpReportBlock report;
|
||||
report.SetMediaSsrc(ssrc_);
|
||||
report.SetFractionLost(0);
|
||||
report.SetCumulativeLost(0);
|
||||
report.SetJitter(0);
|
||||
report.SetLastSr(0);
|
||||
report.SetExtHighestSeqNum(0);
|
||||
report.SetDelayLastSr(0);
|
||||
|
||||
rtcp_sr.SetReportBlock(report);
|
||||
rtcp_sr.Create();
|
||||
|
||||
rtcp_sr.Build();
|
||||
SendRtcpSR(rtcp_sr);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,28 @@ class VideoChannelReceive {
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_video_receiver_) {
|
||||
return rtp_video_receiver_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t GetRemoteSsrc() {
|
||||
if (rtp_video_receiver_) {
|
||||
return rtp_video_receiver_->GetRemoteSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int OnReceiveRtpPacket(const char *data, size_t size);
|
||||
|
||||
void OnSenderReport(int64_t now_time, uint64_t ntp_time) {
|
||||
if (rtp_video_receiver_) {
|
||||
rtp_video_receiver_->OnSenderReport(now_time, ntp_time);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
|
||||
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
|
||||
|
||||
@@ -32,6 +32,13 @@ class VideoChannelSend {
|
||||
void Initialize(rtp::PAYLOAD_TYPE payload_type);
|
||||
void Destroy();
|
||||
|
||||
uint32_t GetSsrc() {
|
||||
if (rtp_video_sender_) {
|
||||
return rtp_video_sender_->GetSsrc();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendVideo(std::shared_ptr<VideoFrameWrapper> encoded_frame);
|
||||
|
||||
void OnCongestionControlFeedback(
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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_; }
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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_
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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_; }
|
||||
|
||||
@@ -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;
|
||||
count_or_format_ = buffer[0] & 0x1F;
|
||||
payload_type_ = PAYLOAD_TYPE(buffer[1]);
|
||||
length_ = (buffer[2] << 8) + buffer[3];
|
||||
return 4;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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));
|
||||
|
||||
@@ -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_; }
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -209,7 +209,7 @@ void IceTransport::OnReceiveBuffer(NiceAgent *agent, guint stream_id,
|
||||
|
||||
bool IceTransport::ParseRtcpPacket(const uint8_t *buffer, size_t size,
|
||||
RtcpPacketInfo *rtcp_packet_info) {
|
||||
CommonHeader rtcp_block;
|
||||
RtcpCommonHeader rtcp_block;
|
||||
// If a sender report is received but no DLRR, we need to reset the
|
||||
// roundTripTime stat according to the standard, see
|
||||
// https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
|
||||
@@ -227,7 +227,7 @@ bool IceTransport::ParseRtcpPacket(const uint8_t *buffer, size_t size,
|
||||
switch (rtcp_block.type()) {
|
||||
case RtcpPacket::RtcpPayloadType::SR:
|
||||
LOG_INFO("Sender report");
|
||||
// valid = HandleSenderReport(rtcp_block, rtcp_packet_info);
|
||||
valid = HandleSenderReport(rtcp_block, rtcp_packet_info);
|
||||
// received_blocks[rtcp_packet_info->remote_ssrc].sender_report = true;
|
||||
break;
|
||||
case RtcpPacket::RtcpPayloadType::RR:
|
||||
@@ -301,8 +301,21 @@ bool IceTransport::ParseRtcpPacket(const uint8_t *buffer, size_t size,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IceTransport::HandleSenderReport(const RtcpCommonHeader &rtcp_block,
|
||||
RtcpPacketInfo *rtcp_packet_info) {
|
||||
SenderReport sender_report;
|
||||
if (!sender_report.Parse(rtcp_block)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ice_transport_controller_) {
|
||||
ice_transport_controller_->OnSenderReport(sender_report);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IceTransport::HandleCongestionControlFeedback(
|
||||
const CommonHeader &rtcp_block, RtcpPacketInfo *rtcp_packet_info) {
|
||||
const RtcpCommonHeader &rtcp_block, RtcpPacketInfo *rtcp_packet_info) {
|
||||
webrtc::rtcp::CongestionControlFeedback feedback;
|
||||
if (!feedback.Parse(rtcp_block) || feedback.packets().empty()) {
|
||||
return false;
|
||||
@@ -319,7 +332,7 @@ bool IceTransport::HandleCongestionControlFeedback(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IceTransport::HandleNack(const CommonHeader &rtcp_block,
|
||||
bool IceTransport::HandleNack(const RtcpCommonHeader &rtcp_block,
|
||||
RtcpPacketInfo *rtcp_packet_info) {
|
||||
webrtc::rtcp::Nack nack;
|
||||
if (!nack.Parse(rtcp_block)) {
|
||||
|
||||
@@ -130,10 +130,13 @@ class IceTransport {
|
||||
bool ParseRtcpPacket(const uint8_t *buffer, size_t size,
|
||||
RtcpPacketInfo *rtcp_packet_info);
|
||||
|
||||
bool HandleCongestionControlFeedback(const CommonHeader &rtcp_block,
|
||||
bool HandleSenderReport(const RtcpCommonHeader &rtcp_block,
|
||||
RtcpPacketInfo *rtcp_packet_info);
|
||||
|
||||
bool HandleNack(const CommonHeader &rtcp_block,
|
||||
bool HandleCongestionControlFeedback(const RtcpCommonHeader &rtcp_block,
|
||||
RtcpPacketInfo *rtcp_packet_info);
|
||||
|
||||
bool HandleNack(const RtcpCommonHeader &rtcp_block,
|
||||
RtcpPacketInfo *rtcp_packet_info);
|
||||
|
||||
private:
|
||||
|
||||
@@ -332,6 +332,24 @@ int IceTransportController::CreateAudioCodec() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IceTransportController::OnSenderReport(const SenderReport& sender_report) {
|
||||
if (video_channel_receive_ &&
|
||||
sender_report.SenderSsrc() == video_channel_receive_->GetRemoteSsrc()) {
|
||||
video_channel_receive_->OnSenderReport(clock_->CurrentTimeUs(),
|
||||
sender_report.NtpTimestamp());
|
||||
} else if (audio_channel_receive_ &&
|
||||
sender_report.SenderSsrc() ==
|
||||
audio_channel_receive_->GetRemoteSsrc()) {
|
||||
audio_channel_receive_->OnSenderReport(clock_->CurrentTimeUs(),
|
||||
sender_report.NtpTimestamp());
|
||||
} else if (data_channel_receive_ &&
|
||||
sender_report.SenderSsrc() ==
|
||||
data_channel_receive_->GetRemoteSsrc()) {
|
||||
data_channel_receive_->OnSenderReport(clock_->CurrentTimeUs(),
|
||||
sender_report.NtpTimestamp());
|
||||
}
|
||||
}
|
||||
|
||||
void IceTransportController::OnCongestionControlFeedback(
|
||||
const webrtc::rtcp::CongestionControlFeedback& feedback) {
|
||||
std::optional<webrtc::TransportPacketsFeedback> feedback_msg =
|
||||
@@ -376,7 +394,7 @@ void IceTransportController::PostUpdates(webrtc::NetworkControlUpdate update) {
|
||||
target_bitrate_ = update.target_rate.has_value()
|
||||
? update.target_rate->target_rate.bps()
|
||||
: 0;
|
||||
LOG_WARN("Target bitrate [{}]bps", target_bitrate_);
|
||||
// LOG_WARN("Target bitrate [{}]bps", target_bitrate_);
|
||||
video_encoder_->SetTargetBitrate(target_bitrate_);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ class IceTransportController
|
||||
void OnReceiveCompleteData(const char *data, size_t size);
|
||||
|
||||
public:
|
||||
void OnSenderReport(const SenderReport &sender_report);
|
||||
void OnCongestionControlFeedback(
|
||||
const webrtc::rtcp::CongestionControlFeedback &feedback);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user