2021-06-25 15:43:47 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Rtcp/RtcpFCI.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NackList {
|
|
|
|
|
public:
|
|
|
|
|
NackList() = default;
|
|
|
|
|
~NackList() = default;
|
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
void pushBack(mediakit::RtpPacket::Ptr rtp);
|
|
|
|
|
void forEach(const mediakit::FCI_NACK &nack, const std::function<void(const mediakit::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();
|
|
|
|
|
int64_t getRtpStamp(uint16_t seq);
|
|
|
|
|
mediakit::RtpPacket::Ptr *getRtp(uint16_t seq);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::deque<uint16_t> _nack_cache_seq;
|
|
|
|
|
std::unordered_map<uint16_t, mediakit::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-02-02 20:34:50 +08:00
|
|
|
|
using onNack = std::function<void(const mediakit::FCI_NACK &nack)>;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
//最大保留的rtp丢包状态个数
|
2021-07-28 11:18:09 +08:00
|
|
|
|
static constexpr auto kNackMaxSize = 1024;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
//rtp丢包状态最长保留时间
|
|
|
|
|
static constexpr auto kNackMaxMS = 3 * 1000;
|
|
|
|
|
//nack最多请求重传10次
|
|
|
|
|
static constexpr auto kNackMaxCount = 10;
|
2021-07-28 11:18:09 +08:00
|
|
|
|
//nack重传频率,rtt的倍数
|
|
|
|
|
static constexpr auto kNackIntervalRatio = 2.0f;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
NackContext() = default;
|
|
|
|
|
~NackContext() = default;
|
|
|
|
|
|
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-02-02 20:34:50 +08:00
|
|
|
|
void doNack(const mediakit::FCI_NACK &nack, bool record_nack);
|
|
|
|
|
void recordNack(const mediakit::FCI_NACK &nack);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
void onRtx(uint16_t seq);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
|
|
|
|
private:
|
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;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
uint16_t _last_max_seq = 0;
|
2021-07-27 20:37:43 +08:00
|
|
|
|
|
|
|
|
|
struct NackStatus{
|
|
|
|
|
uint64_t first_stamp;
|
|
|
|
|
uint64_t update_stamp;
|
|
|
|
|
int nack_count = 0;
|
|
|
|
|
};
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::map<uint16_t/*seq*/, NackStatus > _nack_send_status;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_NACK_H
|