优化on_publish hook响应太慢导致rtp推流无法秒开的问题

由限制缓存个数改成限制缓存时间长度(10秒)
This commit is contained in:
xiongziliang 2024-06-28 22:38:16 +08:00
parent 5470049076
commit 8390d72b78
2 changed files with 6 additions and 4 deletions

View File

@ -18,8 +18,8 @@ using namespace std;
using namespace toolkit; using namespace toolkit;
//在创建_muxer对象前(也就是推流鉴权成功前)需要先缓存frame这样可以防止丢包提高体验 //在创建_muxer对象前(也就是推流鉴权成功前)需要先缓存frame这样可以防止丢包提高体验
//但是同时需要控制缓冲长度,防止内存溢出。200帧数据大概有10秒数据应该足矣等待鉴权hook返回 //但是同时需要控制缓冲长度,防止内存溢出。最多缓存10秒数据应该足矣等待鉴权hook返回
static constexpr size_t kMaxCachedFrame = 200; static constexpr size_t kMaxCachedFrameMS = 10 * 1000;
namespace mediakit { namespace mediakit {
@ -112,6 +112,7 @@ bool RtpProcess::inputRtp(bool is_udp, const Socket::Ptr &sock, const char *data
_addr.reset(new sockaddr_storage(*((sockaddr_storage *)addr))); _addr.reset(new sockaddr_storage(*((sockaddr_storage *)addr)));
if (first) { if (first) {
emitOnPublish(); emitOnPublish();
_cache_ticker.resetTime();
} }
} }
@ -152,8 +153,8 @@ bool RtpProcess::inputFrame(const Frame::Ptr &frame) {
_last_frame_time.resetTime(); _last_frame_time.resetTime();
return _muxer->inputFrame(frame); return _muxer->inputFrame(frame);
} }
if (_cached_func.size() > kMaxCachedFrame) { if (_cache_ticker.elapsedTime() > kMaxCachedFrameMS) {
WarnL << "cached frame of track(" << frame->getCodecName() << ") is too much, now dropped, please check your on_publish hook url in config.ini file"; WarnL << "Cached frame of stream(" << _media_info.stream << ") is too much, your on_publish hook responded too late!";
return false; return false;
} }
auto frame_cached = Frame::getCacheAbleFrame(frame); auto frame_cached = Frame::getCacheAbleFrame(frame);

View File

@ -117,6 +117,7 @@ private:
toolkit::Timer::Ptr _timer; toolkit::Timer::Ptr _timer;
toolkit::Ticker _last_check_alive; toolkit::Ticker _last_check_alive;
std::recursive_mutex _func_mtx; std::recursive_mutex _func_mtx;
toolkit::Ticker _cache_ticker;
std::deque<std::function<void()> > _cached_func; std::deque<std::function<void()> > _cached_func;
}; };