mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
[feat] update rtp packet history module
This commit is contained in:
@@ -1,23 +1,105 @@
|
||||
#include "rtp_packet_history.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "sequence_number_compare.h"
|
||||
|
||||
RtpPacketHistory::RtpPacketHistory() {}
|
||||
RtpPacketHistory::RtpPacketHistory(std::shared_ptr<webrtc::Clock> clock)
|
||||
: clock_(clock),
|
||||
rtt_(webrtc::TimeDelta::MinusInfinity()),
|
||||
number_to_store_(0),
|
||||
packets_inserted_(0) {}
|
||||
|
||||
RtpPacketHistory::~RtpPacketHistory() {}
|
||||
|
||||
void RtpPacketHistory::AddPacket(std::shared_ptr<RtpPacketToSend> rtp_packet,
|
||||
Timestamp send_time) {
|
||||
rtp_packet_history_.push_back(
|
||||
{rtp_packet, send_time, GetPacketIndex(rtp_packet->SequenceNumber())});
|
||||
void RtpPacketHistory::SetRtt(webrtc::TimeDelta rtt) {
|
||||
rtt_ = rtt;
|
||||
RemoveDeadPackets();
|
||||
}
|
||||
|
||||
void RtpPacketHistory::AddPacket(
|
||||
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet,
|
||||
webrtc::Timestamp send_time) {
|
||||
RemoveDeadPackets();
|
||||
const uint16_t rtp_seq_no = rtp_packet->SequenceNumber();
|
||||
int packet_index = GetPacketIndex(rtp_packet->SequenceNumber());
|
||||
if (packet_index >= 0 &&
|
||||
static_cast<size_t>(packet_index) < rtp_packet_history_.size() &&
|
||||
rtp_packet_history_[packet_index].rtp_packet != nullptr) {
|
||||
LOG_WARN("Duplicate packet inserted: {}", rtp_seq_no);
|
||||
// Remove previous packet to avoid inconsistent state.
|
||||
RemovePacket(packet_index);
|
||||
packet_index = GetPacketIndex(rtp_seq_no);
|
||||
}
|
||||
|
||||
// Packet to be inserted ahead of first packet, expand front.
|
||||
for (; packet_index < 0; ++packet_index) {
|
||||
rtp_packet_history_.emplace_front();
|
||||
}
|
||||
// Packet to be inserted behind last packet, expand back.
|
||||
while (static_cast<int>(rtp_packet_history_.size()) <= packet_index) {
|
||||
rtp_packet_history_.emplace_back();
|
||||
}
|
||||
|
||||
rtp_packet_history_[packet_index] = {rtp_packet, send_time,
|
||||
packets_inserted_++};
|
||||
}
|
||||
|
||||
void RtpPacketHistory::RemoveDeadPackets() {
|
||||
webrtc::Timestamp now = clock_->CurrentTime();
|
||||
webrtc::TimeDelta packet_duration =
|
||||
rtt_.IsFinite()
|
||||
? (std::max)(kMinPacketDurationRtt * rtt_, kMinPacketDuration)
|
||||
: kMinPacketDuration;
|
||||
while (!rtp_packet_history_.empty()) {
|
||||
if (rtp_packet_history_.size() >= kMaxCapacity) {
|
||||
// We have reached the absolute max capacity, remove one packet
|
||||
// unconditionally.
|
||||
RemovePacket(0);
|
||||
continue;
|
||||
}
|
||||
|
||||
const RtpPacketToSendInfo& stored_packet = rtp_packet_history_.front();
|
||||
|
||||
if (stored_packet.send_time + packet_duration > now) {
|
||||
// Don't cull packets too early to avoid failed retransmission requests.
|
||||
return;
|
||||
}
|
||||
|
||||
if (rtp_packet_history_.size() >= number_to_store_ ||
|
||||
stored_packet.send_time +
|
||||
(packet_duration * kPacketCullingDelayFactor) <=
|
||||
now) {
|
||||
// Too many packets in history, or this packet has timed out. Remove it
|
||||
// and continue.
|
||||
RemovePacket(0);
|
||||
} else {
|
||||
// No more packets can be removed right now.
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<webrtc::RtpPacketToSend> RtpPacketHistory::RemovePacket(
|
||||
int packet_index) {
|
||||
// Move the packet out from the StoredPacket container.
|
||||
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet =
|
||||
std::move(rtp_packet_history_[packet_index].rtp_packet);
|
||||
if (packet_index == 0) {
|
||||
while (!rtp_packet_history_.empty() &&
|
||||
rtp_packet_history_.front().rtp_packet == nullptr) {
|
||||
rtp_packet_history_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
return rtp_packet;
|
||||
}
|
||||
|
||||
int RtpPacketHistory::GetPacketIndex(uint16_t sequence_number) const {
|
||||
if (packet_history_.empty()) {
|
||||
if (rtp_packet_history_.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int first_seq = packet_history_.front().packet_->SequenceNumber();
|
||||
int first_seq = rtp_packet_history_.front().rtp_packet->SequenceNumber();
|
||||
if (first_seq == sequence_number) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,31 +8,58 @@
|
||||
#define _RTP_PACKET_HISTORY_H_
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
|
||||
#include "api/clock/clock.h"
|
||||
#include "rtp_packet_to_send.h"
|
||||
|
||||
class RtpPacketHistory {
|
||||
public:
|
||||
RtpPacketHistory();
|
||||
static constexpr size_t kMaxCapacity = 600;
|
||||
// Maximum number of entries in prioritized queue of padding packets.
|
||||
static constexpr size_t kMaxPaddingHistory = 63;
|
||||
// Don't remove packets within max(1 second, 3x RTT).
|
||||
static constexpr webrtc::TimeDelta kMinPacketDuration =
|
||||
webrtc::TimeDelta::Seconds(1);
|
||||
static constexpr int kMinPacketDurationRtt = 3;
|
||||
// With kStoreAndCull, always remove packets after 3x max(1000ms, 3x rtt).
|
||||
static constexpr int kPacketCullingDelayFactor = 3;
|
||||
|
||||
public:
|
||||
RtpPacketHistory(std::shared_ptr<webrtc::Clock> clock);
|
||||
~RtpPacketHistory();
|
||||
|
||||
void AddPacket(std::shared_ptr<RtpPacketToSend> rtp_packet,
|
||||
Timestamp send_time);
|
||||
public:
|
||||
void SetRtt(webrtc::TimeDelta rtt);
|
||||
void AddPacket(std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet,
|
||||
webrtc::Timestamp send_time);
|
||||
void RemoveDeadPackets();
|
||||
|
||||
private:
|
||||
std::shared_ptr<webrtc::RtpPacketToSend> RemovePacket(int packet_index);
|
||||
int GetPacketIndex(uint16_t sequence_number) const;
|
||||
|
||||
return packet_index;
|
||||
}
|
||||
private:
|
||||
struct RtpPacketToSendInfo {
|
||||
RtpPacketToSendInfo() = default;
|
||||
RtpPacketToSendInfo(std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet,
|
||||
webrtc::Timestamp send_time, uint64_t index)
|
||||
: rtp_packet(rtp_packet), send_time(send_time), index(index) {}
|
||||
RtpPacketToSendInfo(RtpPacketToSendInfo&&) = default;
|
||||
RtpPacketToSendInfo& operator=(RtpPacketToSendInfo&&) = default;
|
||||
~RtpPacketToSendInfo() = default;
|
||||
|
||||
private : struct RtpPacketToSendInfo {
|
||||
std::shared_ptr<RtpPacketToSend> rtp_packet;
|
||||
Timestamp send_time;
|
||||
uint64_t index;
|
||||
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet;
|
||||
webrtc::Timestamp send_time = webrtc::Timestamp::Zero();
|
||||
uint64_t index;
|
||||
};
|
||||
|
||||
private:
|
||||
std::shared_ptr<webrtc::Clock> clock_;
|
||||
std::deque<RtpPacketToSendInfo> rtp_packet_history_;
|
||||
uint64_t packets_inserted_;
|
||||
webrtc::TimeDelta rtt_;
|
||||
size_t number_to_store_;
|
||||
};
|
||||
|
||||
private:
|
||||
std::deque<std::shared_ptr<RtpPacketToSend>> rtp_packet_history_;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user