目录
背景介绍:
TCP网络协议会维护拥塞窗口和发送窗口,在rtc中,也有类似的缓存机制,就是pacer模块,视频编码后的帧数据量都比较大,尤其是i帧 ,如果一股脑的往网络里直接发送会导致网络拥塞情况出现,如何根据拥塞估计反馈的码率完成优雅的平滑发送是pacer模块设计需要考虑的事情。
1.数据结构设计
class Pacer : public Module, public RtpPacketSender {
}
class PacedSender : public Pacer{
}
从上述代码看出来pacer类继承了Module模块,实现了相关定时处理接口,将由特定线程定时调用。pacersender是主要功能实现类,其重要成员如下:
kMaxQueueLengthMs //最大保存多少ms
static const float kDefaultPaceMultiplier; //一个因子系数?改变它可以降低缓冲延迟?
paused_ :代表是否发送
const Clock* const clock_; //时钟
const std::unique_ptr<AlrDetector> alr_detector_ //判断当前输出网络流量是否受限 ,是否打开alr状态
//就是一段时间内的媒体发送码率预算。 IntervalBudget根据时间流逝增加budget,报文发送后减少budget,每次发送报文前判断剩余budget是否足够,如果不足则取消本次发送。
const std::unique_ptr<IntervalBudget> media_budget_
const std::unique_ptr<IntervalBudget> padding_budget_ //码率不足需要补padding包
const std::unique_ptr<BitrateProber> prober_ //带宽维持器 当码率不足时 需要开启这个来维持一定的码率发送 用于带宽探测
uint32_t estimated_bitrate_bps_ //估算带宽
uint32_t min_send_bitrate_kbps_ //最小带宽
uint32_t max_padding_bitrate_kbps_ //最大带宽
uint32_t pacing_bitrate_kbps_ //pacing 分配带宽
int64_t time_last_update_us_ //上次更新时间
int64_t first_sent_packet_ms_ //第一次发送时间
const std::unique_ptr<PacketQueue> packets_ //包缓存优先级队列
uint64_t packet_counter_ //包计数?
float pacing_factor_ //平滑发送系数
int64_t queue_time_limit //最大缓存时间
bool account_for_audio_ //是否开启音频流量统计 ,音频的packet具备最高优先级
class PacketQueue {
public:
explicit PacketQueue(const Clock* clock);
virtual ~PacketQueue();
struct Packet {
Packet(RtpPacketSender::Priority priority,
uint32_t ssrc,
uint16_t seq_number,
int64_t capture_time_ms,
int64_t enqueue_time_ms,
size_t length_in_bytes,
bool retransmission,
uint64_t enqueue_order);
Packet(const Packet& other);
virtual ~Packet();
bool operator<(const Packet& other) const {
if (priority != other.priority)
return priority > other.priority;
if (retransmission != other.retransmission)
return other.retransmission;
return enqueue_order > other.enqueue_order;
}
RtpPacketSender::Priority priority;
uint32_t ssrc;
uint16_t sequence_number;
int64_t capture_time_ms; // Absolute time of frame capture.
int64_t enqueue_time_ms; // Absolute time of pacer queue entry.
int64_t sum_paused_ms;
size_t bytes;
bool retransmission;
uint64_t enqueue_order;
std::list<Packet>::iterator this_it;
std::multiset<int64_t>::iterator enqueue_time_it;
};
virtual void Push(const Packet& packet);
virtual const Packet& BeginPop();
virtual void CancelPop(const Packet& packet);
virtual void FinalizePop(const Packet& packet);
virtual bool Empty() const;
virtual size_t SizeInPackets() const;
virtual uint64_t SizeInBytes() const;
virtual int64_t OldestEnqueueTimeMs() const;
virtual void UpdateQueueTime(int64_t timestamp_ms);
virtual void SetPauseState(bool paused, int64_t timestamp_ms);
virtual int64_t AverageQueueTimeMs() const;
private:
// Try to add a packet to the set of ssrc/seqno identifiers currently in the
// queue. Return true if inserted, false if this is a duplicate.
bool AddToDupeSet(const Packet& packet);
void RemoveFromDupeSet(const Packet& packet);
// Used by priority queue to sort packets.
struct Comparator {
bool operator()(const Packet* first, const Packet* second) {
// Highest prio = 0.
if (first->priority != second->priority)
return first->priority > second->priority;
// Retransmissions go first.
if (second->retransmission != first->retransmission)
return second->retransmission;
// Older frames have higher prio.
if (first->capture_time_ms != second->capture_time_ms)
return first->capture_time_ms > second->capture_time_ms;
return first->enqueue_order > second->enqueue_order;
}
};
// List of packets, in the order the were enqueued. Since dequeueing may
// occur out of order, use list instead of vector.
std::list<Packet> packet_list_; //存储packet
// Priority queue of the packets, sorted according to Comparator.
// Use pointers into list, to avodi moving whole struct within heap.
std::priority_queue<Packet*, std::vector<Packet*>, Comparator> prio_queue_; //优先级队列,实际是存储的packet指针
// Total number of bytes in the queue.
uint64_t bytes_;
const Clock* const clock_;
int64_t queue_time_sum_;
int64_t time_last_updated_;
bool paused_;
};
2.对外提供的接口
virtual void CreateProbeCluster(int bitrate_bps); //创建一个码率探测的probecluster
//暂停发送
void Pause();