You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PacketSendQueue.cpp 2.0KB

пре 5 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "PacketSendQueue.hpp"
  2. namespace SRT {
  3. PacketSendQueue::PacketSendQueue(uint32_t max_size, uint32_t latency,uint32_t flag)
  4. : _srt_flag(flag)
  5. , _pkt_cap(max_size)
  6. , _pkt_latency(latency) {}
  7. bool PacketSendQueue::drop(uint32_t num) {
  8. decltype(_pkt_cache.begin()) it;
  9. for (it = _pkt_cache.begin(); it != _pkt_cache.end(); ++it) {
  10. if ((*it)->packet_seq_number == num) {
  11. break;
  12. }
  13. }
  14. if (it != _pkt_cache.end()) {
  15. _pkt_cache.erase(_pkt_cache.begin(), it);
  16. }
  17. return true;
  18. }
  19. bool PacketSendQueue::inputPacket(DataPacket::Ptr pkt) {
  20. _pkt_cache.push_back(pkt);
  21. while (_pkt_cache.size() > _pkt_cap) {
  22. _pkt_cache.pop_front();
  23. }
  24. while (timeLatency() > _pkt_latency && TLPKTDrop()) {
  25. _pkt_cache.pop_front();
  26. }
  27. return true;
  28. }
  29. bool PacketSendQueue::TLPKTDrop(){
  30. return (_srt_flag&HSExtMessage::HS_EXT_MSG_TLPKTDROP) && (_srt_flag &HSExtMessage::HS_EXT_MSG_TSBPDSND);
  31. }
  32. std::list<DataPacket::Ptr> PacketSendQueue::findPacketBySeq(uint32_t start, uint32_t end) {
  33. std::list<DataPacket::Ptr> re;
  34. decltype(_pkt_cache.begin()) it;
  35. for (it = _pkt_cache.begin(); it != _pkt_cache.end(); ++it) {
  36. if ((*it)->packet_seq_number == start) {
  37. break;
  38. }
  39. }
  40. if (start == end) {
  41. if (it != _pkt_cache.end()) {
  42. re.push_back(*it);
  43. }
  44. return re;
  45. }
  46. for (; it != _pkt_cache.end(); ++it) {
  47. re.push_back(*it);
  48. if ((*it)->packet_seq_number == end) {
  49. break;
  50. }
  51. }
  52. return re;
  53. }
  54. uint32_t PacketSendQueue::timeLatency() {
  55. if (_pkt_cache.empty()) {
  56. return 0;
  57. }
  58. auto first = _pkt_cache.front()->timestamp;
  59. auto last = _pkt_cache.back()->timestamp;
  60. uint32_t dur;
  61. if (last > first) {
  62. dur = last - first;
  63. } else {
  64. dur = first - last;
  65. }
  66. if (dur > ((uint32_t)0x01 << 31)) {
  67. TraceL << "cycle timeLatency " << dur;
  68. dur = 0xffffffff - dur;
  69. }
  70. return dur;
  71. }
  72. } // namespace SRT