_rtp_sort_cache_map -> _pkt_sort_cache_map, 不依赖于 RTP

This commit is contained in:
wxf 2021-07-17 19:58:29 +08:00
parent ac01dc97af
commit b891fc5654

View File

@ -37,7 +37,7 @@ public:
*/
void clear() {
_seq_cycle_count = 0;
_rtp_sort_cache_map.clear();
_pkt_sort_cache_map.clear();
_next_seq_out = 0;
_max_sort_size = kMin;
}
@ -46,7 +46,7 @@ public:
*
*/
size_t getJitterSize() const{
return _rtp_sort_cache_map.size();
return _pkt_sort_cache_map.size();
}
/**
@ -73,21 +73,21 @@ public:
}
//放入排序缓存
_rtp_sort_cache_map.emplace(seq, std::move(packet));
_pkt_sort_cache_map.emplace(seq, std::move(packet));
//尝试输出排序后的包
tryPopPacket();
}
void flush(){
//清空缓存
while (!_rtp_sort_cache_map.empty()) {
popIterator(_rtp_sort_cache_map.begin());
while (!_pkt_sort_cache_map.empty()) {
popIterator(_pkt_sort_cache_map.begin());
}
}
private:
void popPacket() {
auto it = _rtp_sort_cache_map.begin();
auto it = _pkt_sort_cache_map.begin();
if (it->first >= _next_seq_out) {
//过滤回跳包
popIterator(it);
@ -96,37 +96,37 @@ private:
if (_next_seq_out - it->first > (0xFFFF >> 1)) {
//产生回环了
if (_rtp_sort_cache_map.size() < 2 * kMin) {
if (_pkt_sort_cache_map.size() < 2 * kMin) {
//等足够多的数据后才处理回环, 因为后面还可能出现大的SEQ
return;
}
++_seq_cycle_count;
//找到大的SEQ并清空掉然后从小的SEQ重新开始排序
auto hit = _rtp_sort_cache_map.upper_bound((SEQ) (_next_seq_out - _rtp_sort_cache_map.size()));
while (hit != _rtp_sort_cache_map.end()) {
auto hit = _pkt_sort_cache_map.upper_bound((SEQ) (_next_seq_out - _pkt_sort_cache_map.size()));
while (hit != _pkt_sort_cache_map.end()) {
//回环前清空剩余的大的SEQ的数据
_cb(hit->first, hit->second);
hit = _rtp_sort_cache_map.erase(hit);
hit = _pkt_sort_cache_map.erase(hit);
}
//下一个回环的数据
popIterator(_rtp_sort_cache_map.begin());
popIterator(_pkt_sort_cache_map.begin());
return;
}
//删除回跳的数据包
_rtp_sort_cache_map.erase(it);
_pkt_sort_cache_map.erase(it);
}
void popIterator(typename map<SEQ, T>::iterator it) {
auto seq = it->first;
auto data = std::move(it->second);
_rtp_sort_cache_map.erase(it);
_pkt_sort_cache_map.erase(it);
_next_seq_out = seq + 1;
_cb(seq, data);
}
void tryPopPacket() {
int count = 0;
while ((!_rtp_sort_cache_map.empty() && _rtp_sort_cache_map.begin()->first == _next_seq_out)) {
while ((!_pkt_sort_cache_map.empty() && _pkt_sort_cache_map.begin()->first == _next_seq_out)) {
//找到下个包,直接输出
popPacket();
++count;
@ -134,7 +134,7 @@ private:
if (count) {
setSortSize();
} else if (_rtp_sort_cache_map.size() > _max_sort_size) {
} else if (_pkt_sort_cache_map.size() > _max_sort_size) {
//排序缓存溢出,不再继续排序
popPacket();
setSortSize();
@ -142,7 +142,7 @@ private:
}
void setSortSize() {
_max_sort_size = kMin + _rtp_sort_cache_map.size();
_max_sort_size = kMin + _pkt_sort_cache_map.size();
if (_max_sort_size > kMax) {
_max_sort_size = kMax;
}
@ -155,8 +155,8 @@ private:
size_t _seq_cycle_count = 0;
//排序缓存长度
size_t _max_sort_size = kMin;
//rtp排序缓存根据seq排序
map<SEQ, T> _rtp_sort_cache_map;
//pkt排序缓存根据seq排序
map<SEQ, T> _pkt_sort_cache_map;
//回调
function<void(SEQ seq, T &packet)> _cb;
};