2022-06-03 13:25:32 +08:00
|
|
|
|
#include "PacketQueue.hpp"
|
|
|
|
|
|
|
|
|
|
namespace SRT {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
|
|
|
|
|
inline uint32_t genExpectedSeq(uint32_t seq){
|
|
|
|
|
return 0x7fffffff&seq;
|
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
PacketQueue::PacketQueue(uint32_t max_size, uint32_t init_seq, uint32_t lantency)
|
|
|
|
|
: _pkt_expected_seq(init_seq)
|
|
|
|
|
, _pkt_cap(max_size)
|
|
|
|
|
, _pkt_lantency(lantency) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PacketQueue::inputPacket(DataPacket::Ptr pkt) {
|
|
|
|
|
if (pkt->packet_seq_number < _pkt_expected_seq) {
|
|
|
|
|
// TOO later drop this packet
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pkt_map[pkt->packet_seq_number] = pkt;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::list<DataPacket::Ptr> PacketQueue::tryGetPacket() {
|
|
|
|
|
std::list<DataPacket::Ptr> re;
|
2022-06-04 20:51:49 +08:00
|
|
|
|
auto it = _pkt_map.find(_pkt_expected_seq);
|
|
|
|
|
while ( it != _pkt_map.end()) {
|
|
|
|
|
re.push_back(it->second);
|
|
|
|
|
_pkt_map.erase(it);
|
2022-06-04 22:09:56 +08:00
|
|
|
|
_pkt_expected_seq = genExpectedSeq(_pkt_expected_seq+1);
|
2022-06-04 20:51:49 +08:00
|
|
|
|
it = _pkt_map.find(_pkt_expected_seq);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (_pkt_map.size() > _pkt_cap) {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
// 防止回环
|
|
|
|
|
it = _pkt_map.find(_pkt_expected_seq);
|
|
|
|
|
if(it != _pkt_map.end()){
|
|
|
|
|
re.push_back(it->second);
|
|
|
|
|
_pkt_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
_pkt_expected_seq = genExpectedSeq(_pkt_expected_seq + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (timeLantency() > _pkt_lantency) {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
it = _pkt_map.find(_pkt_expected_seq);
|
|
|
|
|
if(it != _pkt_map.end()){
|
|
|
|
|
re.push_back(it->second);
|
|
|
|
|
_pkt_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
_pkt_expected_seq = genExpectedSeq(_pkt_expected_seq + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 20:51:49 +08:00
|
|
|
|
return re;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool PacketQueue::dropForRecv(uint32_t first,uint32_t last){
|
|
|
|
|
if(first >= last){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_pkt_expected_seq <= last){
|
2022-06-03 22:47:52 +08:00
|
|
|
|
for(uint32_t i =first;i<=last;++i){
|
|
|
|
|
if(_pkt_map.find(i) != _pkt_map.end()){
|
|
|
|
|
_pkt_map.erase(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-04 22:09:56 +08:00
|
|
|
|
_pkt_expected_seq =genExpectedSeq(last+1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-03 22:47:52 +08:00
|
|
|
|
|
|
|
|
|
bool PacketQueue::dropForSend(uint32_t num){
|
|
|
|
|
if(num <= _pkt_expected_seq){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-04 20:51:49 +08:00
|
|
|
|
decltype(_pkt_map.end()) it;
|
2022-06-03 22:47:52 +08:00
|
|
|
|
for(uint32_t i =_pkt_expected_seq;i< num;++i){
|
2022-06-04 20:51:49 +08:00
|
|
|
|
it = _pkt_map.find(i);
|
|
|
|
|
if(it != _pkt_map.end()){
|
|
|
|
|
_pkt_map.erase(it);
|
2022-06-03 22:47:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-04 22:09:56 +08:00
|
|
|
|
_pkt_expected_seq =genExpectedSeq(num);
|
2022-06-03 22:47:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataPacket::Ptr PacketQueue::findPacketBySeq(uint32_t seq){
|
|
|
|
|
auto it = _pkt_map.find(seq);
|
|
|
|
|
if(it != _pkt_map.end()){
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-06-04 20:51:49 +08:00
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
uint32_t PacketQueue::timeLantency() {
|
|
|
|
|
if (_pkt_map.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 22:09:56 +08:00
|
|
|
|
auto first = _pkt_map.begin()->second->timestamp;
|
|
|
|
|
auto last = _pkt_map.rbegin()->second->timestamp;
|
|
|
|
|
uint32_t dur;
|
|
|
|
|
if(last>first){
|
|
|
|
|
dur = last - first;
|
|
|
|
|
}else{
|
|
|
|
|
dur = first - last;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(dur > 0x80000000){
|
2022-06-04 22:42:28 +08:00
|
|
|
|
//WarnL<<"cycle dur "<<dur;
|
2022-06-04 22:09:56 +08:00
|
|
|
|
dur = 0xffffffff - dur;
|
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
|
2022-06-04 22:09:56 +08:00
|
|
|
|
return dur;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::list<PacketQueue::LostPair> PacketQueue::getLostSeq() {
|
|
|
|
|
std::list<PacketQueue::LostPair> re;
|
|
|
|
|
if(_pkt_map.empty()){
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(getExpectedSize() == getSize()){
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PacketQueue::LostPair lost;
|
|
|
|
|
lost.first = 0;
|
|
|
|
|
lost.second = 0;
|
|
|
|
|
|
|
|
|
|
uint32_t i = _pkt_expected_seq;
|
|
|
|
|
bool finish = true;
|
2022-06-04 22:09:56 +08:00
|
|
|
|
for(i = _pkt_expected_seq;i<=_pkt_map.rbegin()->first;){
|
2022-06-03 13:25:32 +08:00
|
|
|
|
if(_pkt_map.find(i) == _pkt_map.end()){
|
|
|
|
|
if(finish){
|
|
|
|
|
finish = false;
|
|
|
|
|
lost.first = i;
|
|
|
|
|
lost.second = i+1;
|
|
|
|
|
}else{
|
|
|
|
|
lost.second = i+1;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
if(!finish){
|
|
|
|
|
finish = true;
|
|
|
|
|
re.push_back(lost);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-04 22:09:56 +08:00
|
|
|
|
i = genExpectedSeq(i+1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t PacketQueue::getSize(){
|
|
|
|
|
return _pkt_map.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t PacketQueue::getExpectedSize() {
|
|
|
|
|
if(_pkt_map.empty()){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-06-04 22:42:28 +08:00
|
|
|
|
auto size = _pkt_map.rbegin()->first - _pkt_expected_seq+1;
|
|
|
|
|
if(size >= _pkt_cap){
|
|
|
|
|
// 回环
|
|
|
|
|
//WarnL<<"cycle size "<<size;
|
|
|
|
|
size = 0xffffffff - size;
|
|
|
|
|
}
|
|
|
|
|
return size;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t PacketQueue::getAvailableBufferSize(){
|
2022-06-04 22:42:28 +08:00
|
|
|
|
return _pkt_cap - getSize();
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t PacketQueue::getExpectedSeq(){
|
|
|
|
|
return _pkt_expected_seq;
|
|
|
|
|
}
|
2022-06-04 22:09:56 +08:00
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
} // namespace SRT
|