重写rtp排序算法:#510

This commit is contained in:
xiongziliang 2020-10-01 21:33:07 +08:00
parent 6a8268f323
commit 4d39176877
3 changed files with 118 additions and 85 deletions

View File

@ -20,12 +20,9 @@
namespace mediakit {
RtpReceiver::RtpReceiver() {
GET_CONFIG(uint32_t, clearCount, Rtp::kClearCount);
GET_CONFIG(uint32_t, maxRtpCount, Rtp::kMaxRtpCount);
int index = 0;
for (auto &sortor : _rtp_sortor) {
sortor.setup(maxRtpCount, clearCount);
sortor.setOnSort([this, index](uint16_t seq, const RtpPacket::Ptr &packet) {
sortor.setOnSort([this, index](uint16_t seq, RtpPacket::Ptr &packet) {
onRtpSorted(packet, index);
});
++index;
@ -128,7 +125,7 @@ bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate,
//拷贝rtp负载
memcpy(payload_ptr + 4, rtp_raw_ptr, rtp_raw_len);
//排序rtp
sortRtp(rtp_ptr,track_index);
sortRtp(std::move(rtp_ptr), track_index);
return true;
}

View File

@ -21,23 +21,13 @@ using namespace toolkit;
namespace mediakit {
template<typename T, typename SEQ = uint16_t>
template<typename T, typename SEQ = uint16_t, uint32_t kMax = 256, uint32_t kMin = 10>
class PacketSortor {
public:
PacketSortor() = default;
~PacketSortor() = default;
/**
*
* @param max_sort_size
* @param clear_sort_size seq连续次数超过该值后
*/
void setup(uint32_t max_sort_size, uint32_t clear_sort_size) {
_max_sort_size = max_sort_size;
_clear_sort_size = clear_sort_size;
}
void setOnSort(function<void(SEQ seq, const T &packet)> cb){
void setOnSort(function<void(SEQ seq, T &packet)> cb) {
_cb = std::move(cb);
}
@ -45,11 +35,10 @@ public:
*
*/
void clear() {
_last_seq = 0;
_seq_ok_count = 0;
_sort_started = 0;
_seq_cycle_count = 0;
_rtp_sort_cache_map.clear();
_next_seq_out = 0;
_max_sort_size = kMin;
}
/**
@ -71,73 +60,62 @@ public:
* @param seq
* @param packet
*/
void sortPacket(SEQ seq, const T &packet){
if (seq != _last_seq + 1 && _last_seq != 0) {
//包乱序或丢包
_seq_ok_count = 0;
_sort_started = true;
if (_last_seq > seq && _last_seq - seq > 0xFF) {
//sequence回环清空所有排序缓存
while (_rtp_sort_cache_map.size()) {
popPacket();
}
void sortPacket(SEQ seq, T packet) {
if (seq < _next_seq_out && _next_seq_out - seq > kMax) {
//回环
++_seq_cycle_count;
}
} else {
//正确序列的包
_seq_ok_count++;
}
_last_seq = seq;
//开始排序缓存
if (_sort_started) {
_rtp_sort_cache_map.emplace(seq, packet);
if (_seq_ok_count >= _clear_sort_size) {
//网络环境改善,需要清空排序缓存
_seq_ok_count = 0;
_sort_started = false;
while (_rtp_sort_cache_map.size()) {
popPacket();
}
} else if (_rtp_sort_cache_map.size() >= _max_sort_size) {
//排序缓存溢出
popPacket();
}
} else {
//正确序列
onPacketSorted(seq, packet);
}
//放入排序缓存
_rtp_sort_cache_map.emplace(seq, std::move(packet));
//尝试输出排序后的包
tryPopPacket();
}
private:
void popPacket() {
auto it = _rtp_sort_cache_map.begin();
onPacketSorted(it->first, it->second);
_cb(it->first, it->second);
_next_seq_out = it->first + 1;
_rtp_sort_cache_map.erase(it);
}
void onPacketSorted(SEQ seq, const T &packet) {
_cb(seq, packet);
void tryPopPacket() {
bool flag = false;
while ((!_rtp_sort_cache_map.empty() && _rtp_sort_cache_map.begin()->first == _next_seq_out)) {
//找到下个包,直接输出
popPacket();
flag = true;
}
if (flag) {
setSortSize();
} else if (_rtp_sort_cache_map.size() > _max_sort_size) {
//排序缓存溢出,不再继续排序
popPacket();
setSortSize();
}
}
void setSortSize() {
_max_sort_size = 2 * _rtp_sort_cache_map.size();
if (_max_sort_size > kMax) {
_max_sort_size = kMax;
} else if (_max_sort_size < kMin) {
_max_sort_size = kMin;
}
}
private:
//是否开始seq排序
bool _sort_started = false;
//上次seq
SEQ _last_seq = 0;
//seq连续次数计数
uint32_t _seq_ok_count = 0;
//下次应该输出的SEQ
SEQ _next_seq_out = 0;
//seq回环次数计数
uint32_t _seq_cycle_count = 0;
//排序缓存长度
uint32_t _max_sort_size;
//seq连续次数超过该值后清空并关闭排序缓存
uint32_t _clear_sort_size;
uint32_t _max_sort_size = kMin;
//rtp排序缓存根据seq排序
map<SEQ, T> _rtp_sort_cache_map;
//回调
function<void(SEQ seq, const T &packet)> _cb;
function<void(SEQ seq, T &packet)> _cb;
};
class RtpReceiver {

58
tests/test_sortor.cpp Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <iostream>
#include <list>
#include "Rtsp/RtpReceiver.h"
using namespace mediakit;
//该测试程序由于检验rtp排序算法的正确性
int main(int argc, char *argv[]) {
srand((unsigned) time(NULL));
PacketSortor<uint16_t, uint16_t> sortor;
list<uint16_t> input_list, sorted_list;
sortor.setOnSort([&](uint16_t seq, const uint16_t &packet) {
sorted_list.push_back(seq);
});
for (int i = 0; i < 1000;) {
int count = 1 + rand() % 8;
for (int j = i + count; j >= i; --j) {
auto seq = j;
sortor.sortPacket(seq, seq);
input_list.push_back(seq);
}
i += (count + 1);
}
{
cout << "排序前:" << endl;
int i = 0;
for (auto &item : input_list) {
cout << item << " ";
if (++i % 10 == 0) {
cout << endl;
}
}
cout << endl;
}
{
cout << "排序后:" << endl;
int i = 0;
for (auto &item : sorted_list) {
cout << item << " ";
if (++i % 10 == 0) {
cout << endl;
}
}
cout << endl;
}
return 0;
}