2022-06-03 13:25:32 +08:00
|
|
|
|
#include "PacketQueue.hpp"
|
|
|
|
|
|
|
|
|
|
namespace SRT {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
static inline bool isSeqEdge(uint32_t seq, uint32_t cap) {
|
|
|
|
|
if (seq > (MAX_SEQ - cap)) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
static inline bool isTSCycle(uint32_t first, uint32_t second) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
uint32_t diff;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (first > second) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
diff = first - second;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
diff = second - first;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (diff > (MAX_TS >> 1)) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return true;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-04 22:09:56 +08:00
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
PacketQueue::PacketQueue(uint32_t max_size, uint32_t init_seq, uint32_t latency)
|
|
|
|
|
: _pkt_cap(max_size)
|
|
|
|
|
, _pkt_latency(latency)
|
|
|
|
|
, _pkt_expected_seq(init_seq) {}
|
|
|
|
|
|
|
|
|
|
void PacketQueue::tryInsertPkt(DataPacket::Ptr pkt) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
if (_pkt_expected_seq <= pkt->packet_seq_number) {
|
|
|
|
|
auto diff = pkt->packet_seq_number - _pkt_expected_seq;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (diff >= (MAX_SEQ >> 1)) {
|
|
|
|
|
TraceL << "drop packet too later for cycle "
|
|
|
|
|
<< "expected seq=" << _pkt_expected_seq << " pkt seq=" << pkt->packet_seq_number;
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
_pkt_map.emplace(pkt->packet_seq_number, pkt);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
auto diff = _pkt_expected_seq - pkt->packet_seq_number;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (diff >= (MAX_SEQ >> 1)) {
|
|
|
|
|
_pkt_map.emplace(pkt->packet_seq_number, pkt);
|
|
|
|
|
TraceL << " cycle packet "
|
|
|
|
|
<< "expected seq=" << _pkt_expected_seq << " pkt seq=" << pkt->packet_seq_number;
|
|
|
|
|
} else {
|
|
|
|
|
// TraceL << "drop packet too later "<< "expected seq=" << _pkt_expected_seq << " pkt seq=" <<
|
|
|
|
|
// pkt->packet_seq_number;
|
2022-06-05 11:07:42 +08:00
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
bool PacketQueue::inputPacket(DataPacket::Ptr pkt, std::list<DataPacket::Ptr> &out) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
tryInsertPkt(pkt);
|
2022-06-04 20:51:49 +08:00
|
|
|
|
auto it = _pkt_map.find(_pkt_expected_seq);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
while (it != _pkt_map.end()) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
out.push_back(it->second);
|
2022-06-04 20:51:49 +08:00
|
|
|
|
_pkt_map.erase(it);
|
2022-06-07 09:52:20 +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);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (it != _pkt_map.end()) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
out.push_back(it->second);
|
2022-06-04 22:09:56 +08:00
|
|
|
|
_pkt_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
_pkt_expected_seq = genExpectedSeq(_pkt_expected_seq + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
while (timeLatency() > _pkt_latency) {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
it = _pkt_map.find(_pkt_expected_seq);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (it != _pkt_map.end()) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
out.push_back(it->second);
|
2022-06-04 22:09:56 +08:00
|
|
|
|
_pkt_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
_pkt_expected_seq = genExpectedSeq(_pkt_expected_seq + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return true;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
2022-06-03 22:47:52 +08:00
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
bool PacketQueue::drop(uint32_t first, uint32_t last, std::list<DataPacket::Ptr> &out) {
|
|
|
|
|
uint32_t end = genExpectedSeq(last + 1);
|
2022-06-04 20:51:49 +08:00
|
|
|
|
decltype(_pkt_map.end()) it;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
for (uint32_t i = _pkt_expected_seq; i < end;) {
|
|
|
|
|
it = _pkt_map.find(i);
|
|
|
|
|
if (it != _pkt_map.end()) {
|
|
|
|
|
out.push_back(it->second);
|
|
|
|
|
_pkt_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
i = genExpectedSeq(i + 1);
|
2022-06-03 22:47:52 +08:00
|
|
|
|
}
|
2022-06-05 11:07:42 +08:00
|
|
|
|
_pkt_expected_seq = end;
|
2022-06-03 22:47:52 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
uint32_t PacketQueue::timeLatency() {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
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;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (last > first) {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
dur = last - first;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
2022-06-04 22:09:56 +08:00
|
|
|
|
dur = first - last;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (dur > 0x80000000) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
dur = MAX_TS - dur;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
WarnL << "cycle dur " << dur;
|
2022-06-04 22:09:56 +08:00
|
|
|
|
}
|
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;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_pkt_map.empty()) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return re;
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
if (getExpectedSize() == getSize()) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 11:07:42 +08:00
|
|
|
|
uint32_t end = 0;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
uint32_t first, last;
|
2022-06-05 11:07:42 +08:00
|
|
|
|
|
|
|
|
|
first = _pkt_map.begin()->second->packet_seq_number;
|
|
|
|
|
last = _pkt_map.rbegin()->second->packet_seq_number;
|
|
|
|
|
if ((last - first) > (MAX_SEQ >> 1)) {
|
|
|
|
|
TraceL << " cycle seq first " << first << " last " << last << " size " << _pkt_map.size();
|
|
|
|
|
end = first;
|
|
|
|
|
} else {
|
|
|
|
|
end = last;
|
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
PacketQueue::LostPair lost;
|
|
|
|
|
lost.first = 0;
|
|
|
|
|
lost.second = 0;
|
|
|
|
|
|
|
|
|
|
uint32_t i = _pkt_expected_seq;
|
|
|
|
|
bool finish = true;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
for (i = _pkt_expected_seq; i <= end;) {
|
|
|
|
|
if (_pkt_map.find(i) == _pkt_map.end()) {
|
|
|
|
|
if (finish) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
finish = false;
|
|
|
|
|
lost.first = i;
|
2022-06-09 19:30:03 +08:00
|
|
|
|
lost.second = genExpectedSeq(i + 1);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
2022-06-09 19:30:03 +08:00
|
|
|
|
lost.second = genExpectedSeq(i + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
} else {
|
|
|
|
|
if (!finish) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
finish = true;
|
|
|
|
|
re.push_back(lost);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
i = genExpectedSeq(i + 1);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
size_t PacketQueue::getSize() {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return _pkt_map.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t PacketQueue::getExpectedSize() {
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_pkt_map.empty()) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-06-05 11:07:42 +08:00
|
|
|
|
|
|
|
|
|
uint32_t max = _pkt_map.rbegin()->first;
|
|
|
|
|
uint32_t min = _pkt_map.begin()->first;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if ((max - min) >= (MAX_SEQ >> 1)) {
|
|
|
|
|
TraceL << "cycle "
|
|
|
|
|
<< "expected seq " << _pkt_expected_seq << " min " << min << " max " << max << " size "
|
|
|
|
|
<< _pkt_map.size();
|
|
|
|
|
return MAX_SEQ - _pkt_expected_seq + min + 1;
|
|
|
|
|
} else {
|
|
|
|
|
return max - _pkt_expected_seq + 1;
|
2022-06-04 22:42:28 +08:00
|
|
|
|
}
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
size_t PacketQueue::getAvailableBufferSize() {
|
|
|
|
|
auto size = getExpectedSize();
|
|
|
|
|
if (_pkt_cap > size) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return _pkt_cap - size;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_pkt_cap > _pkt_map.size()) {
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return _pkt_cap - _pkt_map.size();
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
WarnL << " cap " << _pkt_cap << " expected size " << size << " map size " << _pkt_map.size();
|
2022-06-05 11:07:42 +08:00
|
|
|
|
return _pkt_cap;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
uint32_t PacketQueue::getExpectedSeq() {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
return _pkt_expected_seq;
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
std::string PacketQueue::dump() {
|
|
|
|
|
_StrPrinter printer;
|
|
|
|
|
if (_pkt_map.empty()) {
|
|
|
|
|
printer << " expected seq :" << _pkt_expected_seq;
|
|
|
|
|
} else {
|
|
|
|
|
printer << " expected seq :" << _pkt_expected_seq << " size:" << _pkt_map.size()
|
|
|
|
|
<< " first:" << _pkt_map.begin()->second->packet_seq_number;
|
|
|
|
|
printer << " last:" << _pkt_map.rbegin()->second->packet_seq_number;
|
|
|
|
|
printer << " latency:" << timeLatency() / 1e3;
|
|
|
|
|
}
|
|
|
|
|
return std::move(printer);
|
2022-06-05 11:07:42 +08:00
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
} // namespace SRT
|