Fix rtcp header parse

This commit is contained in:
dijunkun
2023-09-13 10:49:18 +08:00
parent c1d31790d4
commit ea74495b5a
7 changed files with 107 additions and 31 deletions

View File

@@ -1,7 +1,27 @@
#include "rtcp_header.h" #include "rtcp_header.h"
RtcpHeader::RtcpHeader() RtcpHeader::RtcpHeader()
: version_(0), padding_(0), count_or_format_(0), length_(0) {} : version_(0),
padding_(0),
count_or_format_(0),
payload_type_(PAYLOAD_TYPE::UNKNOWN),
length_(0) {}
RtcpHeader::RtcpHeader(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];
}
}
RtcpHeader::~RtcpHeader() {} RtcpHeader::~RtcpHeader() {}

View File

@@ -3,6 +3,8 @@
#include <stdint.h> #include <stdint.h>
#include "log.h"
// RTCP header // RTCP header
// 0 1 2 3 // 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 // 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,6 +28,7 @@ class RtcpHeader {
public: public:
RtcpHeader(); RtcpHeader();
RtcpHeader(const uint8_t* buffer, uint32_t size);
~RtcpHeader(); ~RtcpHeader();
public: public:
@@ -34,14 +37,18 @@ class RtcpHeader {
void SetCountOrFormat(uint8_t count_or_format) { void SetCountOrFormat(uint8_t count_or_format) {
count_or_format_ = count_or_format; count_or_format_ = count_or_format;
} }
void SetPayloadType(uint8_t payload_type) { payload_type_ = payload_type; } void SetPayloadType(PAYLOAD_TYPE payload_type) {
payload_type_ = payload_type;
}
void SetLength(uint16_t length) { length_ = length; } void SetLength(uint16_t length) { length_ = length; }
public: public:
uint8_t Verion() const { return version_; } uint8_t Verion() const { return version_; }
uint8_t Padding() const { return padding_; } uint8_t Padding() const { return padding_; }
uint8_t CountOrFormat() const { return count_or_format_; } uint8_t CountOrFormat() const { return count_or_format_; }
PAYLOAD_TYPE PayloadType() const { return PAYLOAD_TYPE(payload_type_); } PAYLOAD_TYPE PayloadType() const {
return PAYLOAD_TYPE((uint8_t)payload_type_);
}
uint16_t Length() const { return length_; } uint16_t Length() const { return length_; }
int Encode(uint8_t version, uint8_t padding, uint8_t count_or_format, int Encode(uint8_t version, uint8_t padding, uint8_t count_or_format,
@@ -51,7 +58,7 @@ class RtcpHeader {
uint8_t version_ : 2; uint8_t version_ : 2;
uint8_t padding_ : 1; uint8_t padding_ : 1;
uint8_t count_or_format_ : 5; uint8_t count_or_format_ : 5;
uint8_t payload_type_ : 8; PAYLOAD_TYPE payload_type_ : 8;
uint16_t length_ : 16; uint16_t length_ : 16;
}; };

View File

@@ -9,10 +9,6 @@ RtpVideoReceiver::RtpVideoReceiver() {}
RtpVideoReceiver::~RtpVideoReceiver() {} RtpVideoReceiver::~RtpVideoReceiver() {}
void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) { void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
if (rtp_packet.PayloadType() == 200) {
LOG_ERROR("!!!!!!!!!!!!!!!!!!");
}
if (!rtp_video_receive_statistics_) { if (!rtp_video_receive_statistics_) {
rtp_video_receive_statistics_ = rtp_video_receive_statistics_ =
std::make_unique<RtpVideoReceiveStatistics>(); std::make_unique<RtpVideoReceiveStatistics>();

View File

@@ -43,7 +43,7 @@ int RtpVideoSender::SendRtpPacket(RtpPacket& rtp_packet) {
total_rtp_packets_sent_++; total_rtp_packets_sent_++;
total_rtp_payload_sent_ += rtp_packet.PayloadSize(); total_rtp_payload_sent_ += rtp_packet.PayloadSize();
if (CheckIsTimeSendRtcpPacket()) { if (CheckIsTimeSendSR()) {
RtcpSenderReport rtcp_sr; RtcpSenderReport rtcp_sr;
RtcpSenderReport::SENDER_INFO sender_info; RtcpSenderReport::SENDER_INFO sender_info;
@@ -90,10 +90,11 @@ int RtpVideoSender::SendRtcpSR(RtcpSenderReport& rtcp_sr) {
return 0; return 0;
} }
bool RtpVideoSender::CheckIsTimeSendRtcpPacket() { bool RtpVideoSender::CheckIsTimeSendSR() {
auto now_ts = uint32_t now_ts = static_cast<uint32_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count() * std::chrono::duration_cast<std::chrono::milliseconds>(
1000000; std::chrono::high_resolution_clock::now().time_since_epoch())
.count());
if (now_ts - last_send_rtcp_packet_ts_ >= RTCP_INTERVAL) { if (now_ts - last_send_rtcp_packet_ts_ >= RTCP_INTERVAL) {
last_send_rtcp_packet_ts_ = now_ts; last_send_rtcp_packet_ts_ = now_ts;

View File

@@ -26,7 +26,7 @@ class RtpVideoSender : public ThreadBase {
int SendRtpPacket(RtpPacket &rtp_packet); int SendRtpPacket(RtpPacket &rtp_packet);
int SendRtcpSR(RtcpSenderReport &rtcp_sr); int SendRtcpSR(RtcpSenderReport &rtcp_sr);
bool CheckIsTimeSendRtcpPacket(); bool CheckIsTimeSendSR();
private: private:
bool Process() override; bool Process() override;

View File

@@ -109,17 +109,19 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
} }
} }
}, },
[](juice_agent_t *agent, const char *data, size_t size, void *user_ptr) { [](juice_agent_t *agent, const char *buffer, size_t size,
void *user_ptr) {
if (user_ptr) { if (user_ptr) {
IceTransmission *ice_transmission_obj = IceTransmission *ice_transmission_obj =
static_cast<IceTransmission *>(user_ptr); static_cast<IceTransmission *>(user_ptr);
if (ice_transmission_obj) { if (ice_transmission_obj) {
RtpPacket packet((uint8_t *)data, size); if (ice_transmission_obj->CheckIsVideoPacket(buffer, size)) {
ice_transmission_obj->rtp_video_receiver_->InsertRtpPacket(packet); RtpPacket packet((uint8_t *)buffer, size);
// ice_transmission_obj->on_receive_ice_msg_cb_( ice_transmission_obj->rtp_video_receiver_->InsertRtpPacket(
// (const char *)packet.Payload(), packet.PayloadSize(), packet);
// ice_transmission_obj->remote_user_id_.data(), } else if (ice_transmission_obj->CheckIsRtcpPacket(buffer, size)) {
// ice_transmission_obj->remote_user_id_.size()); LOG_ERROR("Rtcp packet [{}]", (uint8_t)(buffer[1]));
}
} }
} }
}, },
@@ -219,16 +221,61 @@ int IceTransmission::SendData(const char *data, size_t size) {
if (rtp_video_sender_) { if (rtp_video_sender_) {
rtp_video_sender_->Enqueue(packets); rtp_video_sender_->Enqueue(packets);
} }
// for (auto &packet : packets) {
// ice_agent_->Send((const char *)packet.Buffer(), packet.Size());
// }
// std::vector<RtpPacket> packets =
// rtp_codec_->Encode((uint8_t *)(data), size);
// send_ringbuffer_.insert(send_ringbuffer_.end(), packets.begin(),
// packets.end());
} }
return 0; return 0;
}
uint8_t IceTransmission::CheckIsRtcpPacket(const char *buffer, size_t size) {
if (size < 4) {
return 0;
}
uint8_t pt = buffer[1];
switch (pt) {
case RtcpHeader::PAYLOAD_TYPE::SR: {
return pt;
}
case RtcpHeader::PAYLOAD_TYPE::RR: {
return pt;
}
case RtcpHeader::PAYLOAD_TYPE::SDES: {
return pt;
}
case RtcpHeader::PAYLOAD_TYPE::BYE: {
return pt;
}
case RtcpHeader::PAYLOAD_TYPE::APP: {
return pt;
}
default: {
return 0;
}
}
}
uint8_t IceTransmission::CheckIsVideoPacket(const char *buffer, size_t size) {
if (size < 4) {
return 0;
}
uint8_t pt = buffer[1] & 0x7F;
if (RtpPacket::PAYLOAD_TYPE::H264 == pt) {
return pt;
} else {
return 0;
}
}
uint8_t IceTransmission::CheckIsAudioPacket(const char *buffer, size_t size) {
if (size < 4) {
return 0;
}
uint8_t pt = buffer[1] & 0x7F;
if (RtpPacket::PAYLOAD_TYPE::OPUS == pt) {
return pt;
} else {
return 0;
}
} }

View File

@@ -52,6 +52,11 @@ class IceTransmission {
int SendAnswer(); int SendAnswer();
private:
uint8_t CheckIsRtcpPacket(const char *buffer, size_t size);
uint8_t CheckIsVideoPacket(const char *buffer, size_t size);
uint8_t CheckIsAudioPacket(const char *buffer, size_t size);
private: private:
std::unique_ptr<IceAgent> ice_agent_ = nullptr; std::unique_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<WsTransmission> ice_ws_transport_ = nullptr; std::shared_ptr<WsTransmission> ice_ws_transport_ = nullptr;