2021-06-25 15:43:47 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2021-06-25 15:43:47 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2021-06-25 15:43:47 +08:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_NACK_H
|
|
|
|
|
#define ZLMEDIAKIT_NACK_H
|
|
|
|
|
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include <set>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <unordered_map>
|
2021-06-25 15:43:47 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Rtcp/RtcpFCI.h"
|
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
namespace mediakit {
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// RTC配置项目 [AUTO-TRANSLATED:19940011]
|
|
|
|
|
// RTC configuration project
|
2024-06-21 20:21:45 +08:00
|
|
|
|
namespace Rtc {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// ~ nack发送端,rtp接收端 [AUTO-TRANSLATED:bb169205]
|
|
|
|
|
// ~ nack sender, rtp receiver
|
|
|
|
|
// 最大保留的rtp丢包状态个数 [AUTO-TRANSLATED:70eee442]
|
|
|
|
|
// Maximum number of retained rtp packet loss states
|
2024-06-21 20:21:45 +08:00
|
|
|
|
extern const std::string kNackMaxSize;
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// rtp丢包状态最长保留时间 [AUTO-TRANSLATED:f9306375]
|
|
|
|
|
// Maximum retention time for rtp packet loss states
|
2024-06-21 20:21:45 +08:00
|
|
|
|
extern const std::string kNackMaxMS;
|
|
|
|
|
} // namespace Rtc
|
|
|
|
|
|
2021-06-25 15:43:47 +08:00
|
|
|
|
class NackList {
|
|
|
|
|
public:
|
2022-09-18 21:03:05 +08:00
|
|
|
|
void pushBack(RtpPacket::Ptr rtp);
|
|
|
|
|
void forEach(const FCI_NACK &nack, const std::function<void(const RtpPacket::Ptr &rtp)> &cb);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-02-27 02:07:25 +08:00
|
|
|
|
void popFront();
|
|
|
|
|
uint32_t getCacheMS();
|
2024-07-21 19:16:34 +08:00
|
|
|
|
int64_t getNtpStamp(uint16_t seq);
|
2022-09-18 21:03:05 +08:00
|
|
|
|
RtpPacket::Ptr *getRtp(uint16_t seq);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-02-27 21:52:12 +08:00
|
|
|
|
uint32_t _cache_ms_check = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::deque<uint16_t> _nack_cache_seq;
|
2022-09-18 21:03:05 +08:00
|
|
|
|
std::unordered_map<uint16_t, RtpPacket::Ptr> _nack_cache_pkt;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class NackContext {
|
|
|
|
|
public:
|
2021-07-27 20:37:43 +08:00
|
|
|
|
using Ptr = std::shared_ptr<NackContext>;
|
2022-09-18 21:03:05 +08:00
|
|
|
|
using onNack = std::function<void(const FCI_NACK &nack)>;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
2023-02-25 00:19:00 +08:00
|
|
|
|
NackContext();
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
2021-07-27 20:37:43 +08:00
|
|
|
|
void received(uint16_t seq, bool is_rtx = false);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
void setOnNack(onNack cb);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
uint64_t reSendNack();
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void eraseFrontSeq();
|
2022-09-18 21:03:05 +08:00
|
|
|
|
void doNack(const FCI_NACK &nack, bool record_nack);
|
|
|
|
|
void recordNack(const FCI_NACK &nack);
|
2023-02-25 00:19:00 +08:00
|
|
|
|
void clearNackStatus(uint16_t seq);
|
|
|
|
|
void makeNack(uint16_t max, bool flush = false);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2023-02-25 00:19:00 +08:00
|
|
|
|
bool _started = false;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
int _rtt = 50;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
onNack _cb;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::set<uint16_t> _seq;
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 最新nack包中的rtp seq值 [AUTO-TRANSLATED:6984d95a]
|
|
|
|
|
// RTP seq value in the latest nack packet
|
2023-02-25 00:19:00 +08:00
|
|
|
|
uint16_t _nack_seq = 0;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
struct NackStatus {
|
2021-07-27 20:37:43 +08:00
|
|
|
|
uint64_t first_stamp;
|
|
|
|
|
uint64_t update_stamp;
|
2024-07-21 19:16:34 +08:00
|
|
|
|
uint32_t nack_count = 0;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
};
|
2022-09-18 21:03:05 +08:00
|
|
|
|
std::map<uint16_t /*seq*/, NackStatus> _nack_send_status;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
} // namespace mediakit
|
|
|
|
|
|
2021-06-25 15:43:47 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_NACK_H
|