[fix] fix sender report building and parsing

This commit is contained in:
dijunkun
2025-02-27 17:30:41 +08:00
parent b7a5066c6b
commit 962d856946
12 changed files with 108 additions and 65 deletions

View File

@@ -8,7 +8,7 @@
#define DEFAULT_RTCP_HEADER_SIZE 4
#define DEFAULT_SR_BLOCK_NUM 1
#define DEFAULT_SR_SIZE 52
#define DEFAULT_SR_SIZE 28
#define DEFAULT_RR_BLOCK_NUM 1
#define DEFAULT_RR_SIZE 32

View File

@@ -40,10 +40,10 @@ const uint8_t *SenderReport::Build() {
buffer_[pos++] = sender_info_.sender_ssrc >> 8 & 0xFF;
buffer_[pos++] = sender_info_.sender_ssrc & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 56 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 48 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 40 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 32 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 24 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 16 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw >> 8 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_msw & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_lsw >> 24 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_lsw >> 16 & 0xFF;
buffer_[pos++] = sender_info_.ntp_ts_lsw >> 8 & 0xFF;
@@ -71,36 +71,58 @@ const uint8_t *SenderReport::Build() {
return buffer_;
}
size_t SenderReport::Parse(const RtcpCommonHeader &packet) {
bool SenderReport::Parse(const RtcpCommonHeader &packet) {
reports_.clear();
size_t pos = packet.payload_size_bytes();
const uint8_t *payload = packet.payload();
const uint8_t *payload_end = packet.payload() + packet.payload_size_bytes();
size_t pos = 0;
sender_info_.sender_ssrc = (buffer_[pos] << 24) + (buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
sender_info_.sender_ssrc = (payload[pos] << 24) + (payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
sender_info_.ntp_ts_msw = (buffer_[pos] << 24) + (buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
if (pos > packet.payload_size_bytes()) {
return false;
}
sender_info_.ntp_ts_msw = (payload[pos] << 24) + (payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
sender_info_.ntp_ts_lsw = (buffer_[pos] << 24) + (buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
if (pos > packet.payload_size_bytes()) {
return false;
}
sender_info_.ntp_ts_lsw = (payload[pos] << 24) + (payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
sender_info_.rtp_ts = (buffer_[pos] << 24) + (buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
if (pos > packet.payload_size_bytes()) {
return false;
}
sender_info_.rtp_ts = (payload[pos] << 24) + (payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
sender_info_.sender_packet_count = (buffer_[pos] << 24) +
(buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
if (pos > packet.payload_size_bytes()) {
return false;
}
sender_info_.sender_packet_count = (payload[pos] << 24) +
(payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
sender_info_.sender_octet_count = (buffer_[pos] << 24) +
(buffer_[pos + 1] << 16) +
(buffer_[pos + 2] << 8) + buffer_[pos + 3];
if (pos > packet.payload_size_bytes()) {
return false;
}
sender_info_.sender_octet_count = (payload[pos] << 24) +
(payload[pos + 1] << 16) +
(payload[pos + 2] << 8) + payload[pos + 3];
pos += 4;
if (pos > packet.payload_size_bytes()) {
return false;
}
for (int i = 0; i < rtcp_common_header_.fmt(); i++) {
RtcpReportBlock report;
pos += report.Parse(buffer_ + pos);
pos += report.Parse(payload + pos);
reports_.emplace_back(std::move(report));
}
return pos;
return pos == packet.payload_size_bytes();
}

View File

@@ -44,6 +44,7 @@
#include <vector>
#include "api/ntp/ntp_time.h"
#include "rtcp_common_header.h"
#include "rtcp_report_block.h"
@@ -51,8 +52,8 @@ class SenderReport {
public:
typedef struct {
uint32_t sender_ssrc : 32;
uint64_t ntp_ts_msw : 64;
uint64_t ntp_ts_lsw : 64;
uint32_t ntp_ts_msw : 32;
uint32_t ntp_ts_lsw : 32;
uint32_t rtp_ts : 32;
uint32_t sender_packet_count : 32;
uint32_t sender_octet_count : 32;
@@ -65,8 +66,10 @@ class SenderReport {
public:
void SetSenderSsrc(uint32_t ssrc) { sender_info_.sender_ssrc = ssrc; }
void SetNtpTimestamp(uint64_t ntp_timestamp) {
sender_info_.ntp_ts_msw = ntp_timestamp >> 32;
sender_info_.ntp_ts_lsw = ntp_timestamp & 0xFFFFFFFF;
sender_info_.ntp_ts_msw =
ntp_timestamp / webrtc::NtpTime::kFractionsPerSecond;
sender_info_.ntp_ts_lsw =
ntp_timestamp % webrtc::NtpTime::kFractionsPerSecond;
}
void SetTimestamp(uint32_t timestamp) { sender_info_.rtp_ts = timestamp; }
void SetSenderPacketCount(uint32_t packet_count) {
@@ -79,8 +82,8 @@ class SenderReport {
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 NtpTimestamp() const {
return (sender_info_.ntp_ts_msw << 16) | sender_info_.ntp_ts_lsw >> 16;
}
uint32_t Timestamp() const { return sender_info_.rtp_ts; }
uint32_t SenderPacketCount() const {
@@ -90,7 +93,7 @@ class SenderReport {
public:
const uint8_t *Build();
size_t Parse(const RtcpCommonHeader &packet);
bool Parse(const RtcpCommonHeader &packet);
// Entire RTP buffer
const uint8_t *Buffer() const { return buffer_; }