Nack.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT-like license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLMEDIAKIT_NACK_H
  11. #define ZLMEDIAKIT_NACK_H
  12. #include <set>
  13. #include <map>
  14. #include <deque>
  15. #include <unordered_map>
  16. #include "Rtsp/Rtsp.h"
  17. #include "Rtcp/RtcpFCI.h"
  18. namespace mediakit {
  19. // RTC配置项目 [AUTO-TRANSLATED:19940011]
  20. // RTC configuration project
  21. namespace Rtc {
  22. // ~ nack发送端,rtp接收端 [AUTO-TRANSLATED:bb169205]
  23. // ~ nack sender, rtp receiver
  24. // 最大保留的rtp丢包状态个数 [AUTO-TRANSLATED:70eee442]
  25. // Maximum number of retained rtp packet loss states
  26. extern const std::string kNackMaxSize;
  27. // rtp丢包状态最长保留时间 [AUTO-TRANSLATED:f9306375]
  28. // Maximum retention time for rtp packet loss states
  29. extern const std::string kNackMaxMS;
  30. } // namespace Rtc
  31. class NackList {
  32. public:
  33. void pushBack(RtpPacket::Ptr rtp);
  34. void forEach(const FCI_NACK &nack, const std::function<void(const RtpPacket::Ptr &rtp)> &cb);
  35. private:
  36. void popFront();
  37. uint32_t getCacheMS();
  38. int64_t getNtpStamp(uint16_t seq);
  39. RtpPacket::Ptr *getRtp(uint16_t seq);
  40. private:
  41. uint32_t _cache_ms_check = 0;
  42. std::deque<uint16_t> _nack_cache_seq;
  43. std::unordered_map<uint16_t, RtpPacket::Ptr> _nack_cache_pkt;
  44. };
  45. class NackContext {
  46. public:
  47. using Ptr = std::shared_ptr<NackContext>;
  48. using onNack = std::function<void(const FCI_NACK &nack)>;
  49. NackContext();
  50. void received(uint16_t seq, bool is_rtx = false);
  51. void setOnNack(onNack cb);
  52. uint64_t reSendNack();
  53. private:
  54. void eraseFrontSeq();
  55. void doNack(const FCI_NACK &nack, bool record_nack);
  56. void recordNack(const FCI_NACK &nack);
  57. void clearNackStatus(uint16_t seq);
  58. void makeNack(uint16_t max, bool flush = false);
  59. private:
  60. bool _started = false;
  61. int _rtt = 50;
  62. onNack _cb;
  63. std::set<uint16_t> _seq;
  64. // 最新nack包中的rtp seq值 [AUTO-TRANSLATED:6984d95a]
  65. // RTP seq value in the latest nack packet
  66. uint16_t _nack_seq = 0;
  67. struct NackStatus {
  68. uint64_t first_stamp;
  69. uint64_t update_stamp;
  70. uint32_t nack_count = 0;
  71. };
  72. std::map<uint16_t /*seq*/, NackStatus> _nack_send_status;
  73. };
  74. } // namespace mediakit
  75. #endif //ZLMEDIAKIT_NACK_H