From 3a3265857ec24785636c53e8ac4c7f6094d50c74 Mon Sep 17 00:00:00 2001 From: wanglei Date: Wed, 9 Nov 2022 17:25:52 +0800 Subject: [PATCH 01/10] add gop_cache_max_frames --- trunk/conf/full.conf | 7 +++++++ trunk/conf/srs.conf | 4 ++++ trunk/src/app/srs_app_config.cpp | 31 +++++++++++++++++++++++++++-- trunk/src/app/srs_app_config.hpp | 4 ++++ trunk/src/app/srs_app_rtmp_conn.cpp | 1 + trunk/src/app/srs_app_source.cpp | 25 +++++++++++++++++++++++ trunk/src/app/srs_app_source.hpp | 7 +++++++ 7 files changed, 77 insertions(+), 2 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 8aaeb40137..2e975714a2 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -1096,6 +1096,13 @@ vhost play.srs.com { # Overwrite by env SRS_VHOST_PLAY_GOP_CACHE for all vhosts. # default: on gop_cache off; + + # to limit the max gop cache frames + # without this limit, if ingest stream always has no IDR frame, + # it may cause srs run out of memory + # default: 250 + gop_cache_max_frames 250; + # the max live queue length in seconds. # if the messages in the queue exceed the max length, # drop the old whole gop. diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index ec90ab07ff..756c4f1f66 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -36,4 +36,8 @@ vhost __defaultVhost__ { # @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp rtc_to_rtmp off; } + + play{ + gop_cache_max_frames 100; + } } diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index ed98dc8b3a..22a27c5bfa 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -517,7 +517,8 @@ srs_error_t srs_config_transform_vhost(SrsConfDirective* root) // SRS3+: // vhost { play { shadow; } } if (n == "time_jitter" || n == "mix_correct" || n == "atc" || n == "atc_auto" - || n == "mw_latency" || n == "gop_cache" || n == "queue_length" || n == "send_min_interval" + || n == "mw_latency" || n == "gop_cache" || n == "gop_cache_max_frames" + || n == "queue_length" || n == "send_min_interval" || n == "reduce_sequence_header") { it = dir->directives.erase(it); @@ -2548,7 +2549,7 @@ srs_error_t SrsConfig::check_normal_config() for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "time_jitter" && m != "mix_correct" && m != "atc" && m != "atc_auto" && m != "mw_latency" - && m != "gop_cache" && m != "queue_length" && m != "send_min_interval" && m != "reduce_sequence_header" + && m != "gop_cache" && m != "gop_cache_max_frames" && m != "queue_length" && m != "send_min_interval" && m != "reduce_sequence_header" && m != "mw_msgs") { return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.play.%s of %s", m.c_str(), vhost->arg0().c_str()); } @@ -4657,6 +4658,32 @@ bool SrsConfig::get_gop_cache(string vhost) return SRS_CONF_PERFER_TRUE(conf->arg0()); } + +int SrsConfig::get_gop_cache_max_frames(string vhost) +{ + SRS_OVERWRITE_BY_ENV_INT("srs.vhost.play.gop_cache_max_frames"); + + static int DEFAULT = 250; + + SrsConfDirective* conf = get_vhost(vhost); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("play"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("gop_cache_max_frames"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + + return ::atoi(conf->arg0().c_str()); +} + + bool SrsConfig::get_debug_srs_upnode(string vhost) { static bool DEFAULT = true; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index eeee24b563..d5944b7fe5 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -553,6 +553,10 @@ class SrsConfig // @return true when gop_cache is ok; otherwise, false. // @remark, default true. virtual bool get_gop_cache(std::string vhost); + // to set the max frames limit for gop cache + // @return gop cache frames limit + // @remark, default 250 (10s for 25fps) + virtual int get_gop_cache_max_frames(std::string vhost); // Whether debug_srs_upnode is enabled of vhost. // debug_srs_upnode is very important feature for tracable log, // but some server, for instance, flussonic donot support it. diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index cf8c54af8e..4c1d7cc7f7 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -560,6 +560,7 @@ srs_error_t SrsRtmpConn::stream_service_cycle() srs_trace("source url=%s, ip=%s, cache=%d, is_edge=%d, source_id=%s/%s", req->get_stream_url().c_str(), ip.c_str(), enabled_cache, info->edge, source->source_id().c_str(), source->pre_source_id().c_str()); source->set_cache(enabled_cache); + source->set_cache_max_frames(_srs_config->get_gop_cache_max_frames(req->vhost)); switch (info->type) { case SrsRtmpConnPlay: { diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 260f438383..25b9a4fe41 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -575,6 +575,7 @@ SrsGopCache::SrsGopCache() cached_video_count = 0; enable_gop_cache = true; audio_after_last_video_count = 0; + gop_cache_max_frames = 250; } SrsGopCache::~SrsGopCache() @@ -597,6 +598,17 @@ void SrsGopCache::set(bool v) } } + +void SrsGopCache::set_max_frames(int m) +{ + gop_cache_max_frames = m; + + if (cached_video_count > gop_cache_max_frames){ + srs_warn("too much frames in the gop cache"); + clear(); + } +} + bool SrsGopCache::enabled() { return enable_gop_cache; @@ -641,6 +653,11 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) return err; } + if (cached_video_count > gop_cache_max_frames){ + srs_warn("too much frames in the gop cache"); + clear(); + } + // clear gop cache when got key frame if (msg->is_video() && SrsFlvVideo::keyframe(msg->payload, msg->size)) { clear(); @@ -2086,6 +2103,8 @@ srs_error_t SrsLiveSource::on_reload_vhost_play(string vhost) string url = req->get_stream_url(); srs_trace("vhost %s gop_cache changed to %d, source url=%s", vhost.c_str(), v, url.c_str()); gop_cache->set(v); + + gop_cache->set_max_frames(_srs_config->get_gop_cache_max_frames(vhost)); } } @@ -2725,6 +2744,12 @@ void SrsLiveSource::set_cache(bool enabled) gop_cache->set(enabled); } +void SrsLiveSource::set_cache_max_frames(int max) +{ + gop_cache->set_max_frames(max); +} + + SrsRtmpJitterAlgorithm SrsLiveSource::jitter() { return jitter_algorithm; diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index f26f2945b9..473945cd01 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -228,6 +228,10 @@ class SrsGopCache // The client will wait for the next keyframe for h264, // and will be black-screen. bool enable_gop_cache; + // to limit the max gop cache frames + // without this limit, if ingest stream always has no IDR frame + // it will cause srs run out of memory + int gop_cache_max_frames; // The video frame count, avoid cache for pure audio stream. int cached_video_count; // when user disabled video when publishing, and gop cache enalbed, @@ -251,6 +255,8 @@ class SrsGopCache virtual void dispose(); // To enable or disable the gop cache. virtual void set(bool v); + // max gop frames + virtual void set_max_frames(int m); virtual bool enabled(); // only for h264 codec // 1. cache the gop when got h264 video packet. @@ -589,6 +595,7 @@ class SrsLiveSource : public ISrsReloadHandler virtual srs_error_t consumer_dumps(SrsLiveConsumer* consumer, bool ds = true, bool dm = true, bool dg = true); virtual void on_consumer_destroy(SrsLiveConsumer* consumer); virtual void set_cache(bool enabled); + virtual void set_cache_max_frames(int max); virtual SrsRtmpJitterAlgorithm jitter(); public: // For edge, when publish edge stream, check the state From dc11aa3830ee7ca843080820695e09d3c41d6a19 Mon Sep 17 00:00:00 2001 From: bluestn Date: Thu, 10 Nov 2022 23:30:21 +0800 Subject: [PATCH 02/10] change some variable names and code format. --- trunk/conf/srs.conf | 4 ++-- trunk/src/app/srs_app_source.cpp | 14 +++++++------- trunk/src/app/srs_app_source.hpp | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index 756c4f1f66..74e395e0d9 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -37,7 +37,7 @@ vhost __defaultVhost__ { rtc_to_rtmp off; } - play{ - gop_cache_max_frames 100; + play{ + gop_cache_max_frames 100; } } diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 25b9a4fe41..40d27cc42b 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -575,7 +575,7 @@ SrsGopCache::SrsGopCache() cached_video_count = 0; enable_gop_cache = true; audio_after_last_video_count = 0; - gop_cache_max_frames = 250; + gop_cache_max_frames_ = 250; } SrsGopCache::~SrsGopCache() @@ -599,11 +599,11 @@ void SrsGopCache::set(bool v) } -void SrsGopCache::set_max_frames(int m) +void SrsGopCache::set_max_frames(int v) { - gop_cache_max_frames = m; + gop_cache_max_frames_ = v; - if (cached_video_count > gop_cache_max_frames){ + if (cached_video_count > gop_cache_max_frames_){ srs_warn("too much frames in the gop cache"); clear(); } @@ -653,7 +653,7 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) return err; } - if (cached_video_count > gop_cache_max_frames){ + if (cached_video_count > gop_cache_max_frames_){ srs_warn("too much frames in the gop cache"); clear(); } @@ -2744,9 +2744,9 @@ void SrsLiveSource::set_cache(bool enabled) gop_cache->set(enabled); } -void SrsLiveSource::set_cache_max_frames(int max) +void SrsLiveSource::set_cache_max_frames(int v) { - gop_cache->set_max_frames(max); + gop_cache->set_max_frames(v); } diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 473945cd01..756055de33 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -231,7 +231,7 @@ class SrsGopCache // to limit the max gop cache frames // without this limit, if ingest stream always has no IDR frame // it will cause srs run out of memory - int gop_cache_max_frames; + int gop_cache_max_frames_; // The video frame count, avoid cache for pure audio stream. int cached_video_count; // when user disabled video when publishing, and gop cache enalbed, @@ -256,7 +256,7 @@ class SrsGopCache // To enable or disable the gop cache. virtual void set(bool v); // max gop frames - virtual void set_max_frames(int m); + virtual void set_max_frames(int v); virtual bool enabled(); // only for h264 codec // 1. cache the gop when got h264 video packet. @@ -595,7 +595,7 @@ class SrsLiveSource : public ISrsReloadHandler virtual srs_error_t consumer_dumps(SrsLiveConsumer* consumer, bool ds = true, bool dm = true, bool dg = true); virtual void on_consumer_destroy(SrsLiveConsumer* consumer); virtual void set_cache(bool enabled); - virtual void set_cache_max_frames(int max); + virtual void set_cache_max_frames(int v); virtual SrsRtmpJitterAlgorithm jitter(); public: // For edge, when publish edge stream, check the state From 8342e3d084879da39faac550644430fad73135ea Mon Sep 17 00:00:00 2001 From: wanglei Date: Fri, 11 Nov 2022 09:14:59 +0800 Subject: [PATCH 03/10] modify some gop_cache_max_frames comment & move gop_cache_max_frames limit to after key frame check. --- trunk/src/app/srs_app_config.hpp | 3 +-- trunk/src/app/srs_app_source.cpp | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index d5944b7fe5..f54b4674fe 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -553,9 +553,8 @@ class SrsConfig // @return true when gop_cache is ok; otherwise, false. // @remark, default true. virtual bool get_gop_cache(std::string vhost); - // to set the max frames limit for gop cache + // to set the max frames limit in gop cache // @return gop cache frames limit - // @remark, default 250 (10s for 25fps) virtual int get_gop_cache_max_frames(std::string vhost); // Whether debug_srs_upnode is enabled of vhost. // debug_srs_upnode is very important feature for tracable log, diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 40d27cc42b..0ea9fcb2f8 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -602,11 +602,6 @@ void SrsGopCache::set(bool v) void SrsGopCache::set_max_frames(int v) { gop_cache_max_frames_ = v; - - if (cached_video_count > gop_cache_max_frames_){ - srs_warn("too much frames in the gop cache"); - clear(); - } } bool SrsGopCache::enabled() @@ -653,11 +648,6 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) return err; } - if (cached_video_count > gop_cache_max_frames_){ - srs_warn("too much frames in the gop cache"); - clear(); - } - // clear gop cache when got key frame if (msg->is_video() && SrsFlvVideo::keyframe(msg->payload, msg->size)) { clear(); @@ -665,7 +655,16 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) // curent msg is video frame, so we set to 1. cached_video_count = 1; } - + + if (gop_cache.size() > gop_cache_max_frames_) { + srs_warn("too many frames in the gop cache, %d video frames & %d audio frames dropped, " + "audio_after_last_video_count: %d.", + cached_video_count, + (int)gop_cache.size() - cached_video_count, + audio_after_last_video_count); + clear(); + } + // cache the frame. gop_cache.push_back(msg->copy()); From 549e130a113450a15277a7e719e9e78f4160aeb3 Mon Sep 17 00:00:00 2001 From: wanglei Date: Fri, 11 Nov 2022 13:42:16 +0800 Subject: [PATCH 04/10] move gop_cache_max_frames check after keyframe check --- trunk/conf/srs.conf | 2 +- trunk/src/app/srs_app_source.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index 74e395e0d9..79ab702bc3 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -5,7 +5,7 @@ listen 1935; max_connections 1000; #srs_log_tank file; #srs_log_file ./objs/srs.log; -daemon on; +daemon off; http_api { enabled on; listen 1985; diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 0ea9fcb2f8..7824cd72fa 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -656,6 +656,10 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) cached_video_count = 1; } + + // cache the frame. + gop_cache.push_back(msg->copy()); + if (gop_cache.size() > gop_cache_max_frames_) { srs_warn("too many frames in the gop cache, %d video frames & %d audio frames dropped, " "audio_after_last_video_count: %d.", @@ -665,9 +669,6 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) clear(); } - // cache the frame. - gop_cache.push_back(msg->copy()); - return err; } From 44f1eb9648e8ec9528372d1f4eac73f4e0392657 Mon Sep 17 00:00:00 2001 From: wanglei Date: Tue, 22 Nov 2022 10:20:15 +0800 Subject: [PATCH 05/10] remove comment from set_max_frames --- trunk/src/app/srs_app_source.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 756055de33..4cc1c3e183 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -255,7 +255,6 @@ class SrsGopCache virtual void dispose(); // To enable or disable the gop cache. virtual void set(bool v); - // max gop frames virtual void set_max_frames(int v); virtual bool enabled(); // only for h264 codec From f457367baa2462a46a6911699e300e6418c4fc56 Mon Sep 17 00:00:00 2001 From: wanglei Date: Tue, 22 Nov 2022 10:21:23 +0800 Subject: [PATCH 06/10] change back srs.conf daemon to on --- trunk/conf/srs.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index 79ab702bc3..74e395e0d9 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -5,7 +5,7 @@ listen 1935; max_connections 1000; #srs_log_tank file; #srs_log_file ./objs/srs.log; -daemon off; +daemon on; http_api { enabled on; listen 1985; From af0199a3d45275d4ec0e06d2ad93a891e13b053f Mon Sep 17 00:00:00 2001 From: wanglei Date: Tue, 22 Nov 2022 10:34:42 +0800 Subject: [PATCH 07/10] rename set_cache_max_frames to set_gop_cache_max_frames --- trunk/src/app/srs_app_rtmp_conn.cpp | 8 +++++--- trunk/src/app/srs_app_source.cpp | 4 ++-- trunk/src/app/srs_app_source.hpp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 4c1d7cc7f7..a9c907c66a 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -557,10 +557,12 @@ srs_error_t SrsRtmpConn::stream_service_cycle() srs_assert(source != NULL); bool enabled_cache = _srs_config->get_gop_cache(req->vhost); - srs_trace("source url=%s, ip=%s, cache=%d, is_edge=%d, source_id=%s/%s", - req->get_stream_url().c_str(), ip.c_str(), enabled_cache, info->edge, source->source_id().c_str(), source->pre_source_id().c_str()); + int gcf = _srs_config->get_gop_cache_max_frames(req->vhost); + + srs_trace("source url=%s, ip=%s, cache=%d, gop_cache_max_frames=%d, is_edge=%d, source_id=%s/%s", + req->get_stream_url().c_str(), ip.c_str(), enabled_cache, gcf, info->edge, source->source_id().c_str(), source->pre_source_id().c_str()); source->set_cache(enabled_cache); - source->set_cache_max_frames(_srs_config->get_gop_cache_max_frames(req->vhost)); + source->set_gop_cache_max_frames(gcf); switch (info->type) { case SrsRtmpConnPlay: { diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 7824cd72fa..5aed5d6a70 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -660,7 +660,7 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) // cache the frame. gop_cache.push_back(msg->copy()); - if (gop_cache.size() > gop_cache_max_frames_) { + if (gop_cache.size() > (size_t)gop_cache_max_frames_) { srs_warn("too many frames in the gop cache, %d video frames & %d audio frames dropped, " "audio_after_last_video_count: %d.", cached_video_count, @@ -2744,7 +2744,7 @@ void SrsLiveSource::set_cache(bool enabled) gop_cache->set(enabled); } -void SrsLiveSource::set_cache_max_frames(int v) +void SrsLiveSource::set_gop_cache_max_frames(int v) { gop_cache->set_max_frames(v); } diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 4cc1c3e183..ec77b107b1 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -594,7 +594,7 @@ class SrsLiveSource : public ISrsReloadHandler virtual srs_error_t consumer_dumps(SrsLiveConsumer* consumer, bool ds = true, bool dm = true, bool dg = true); virtual void on_consumer_destroy(SrsLiveConsumer* consumer); virtual void set_cache(bool enabled); - virtual void set_cache_max_frames(int v); + virtual void set_gop_cache_max_frames(int v); virtual SrsRtmpJitterAlgorithm jitter(); public: // For edge, when publish edge stream, check the state From 05d244eb5fd435ff8a2c94d7f4d4da2345ada39b Mon Sep 17 00:00:00 2001 From: wanglei Date: Tue, 22 Nov 2022 10:45:29 +0800 Subject: [PATCH 08/10] rename set_max_frames to set_gop_cache_max_frames --- trunk/src/app/srs_app_source.cpp | 6 +++--- trunk/src/app/srs_app_source.hpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 5aed5d6a70..cceb6e73d0 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -599,7 +599,7 @@ void SrsGopCache::set(bool v) } -void SrsGopCache::set_max_frames(int v) +void SrsGopCache::set_gop_cache_max_frames(int v) { gop_cache_max_frames_ = v; } @@ -2104,7 +2104,7 @@ srs_error_t SrsLiveSource::on_reload_vhost_play(string vhost) srs_trace("vhost %s gop_cache changed to %d, source url=%s", vhost.c_str(), v, url.c_str()); gop_cache->set(v); - gop_cache->set_max_frames(_srs_config->get_gop_cache_max_frames(vhost)); + gop_cache->set_gop_cache_max_frames(_srs_config->get_gop_cache_max_frames(vhost)); } } @@ -2746,7 +2746,7 @@ void SrsLiveSource::set_cache(bool enabled) void SrsLiveSource::set_gop_cache_max_frames(int v) { - gop_cache->set_max_frames(v); + gop_cache->set_gop_cache_max_frames(v); } diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index ec77b107b1..9c8b7df6b4 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -255,7 +255,7 @@ class SrsGopCache virtual void dispose(); // To enable or disable the gop cache. virtual void set(bool v); - virtual void set_max_frames(int v); + virtual void set_gop_cache_max_frames(int v); virtual bool enabled(); // only for h264 codec // 1. cache the gop when got h264 video packet. From 6533c31618cd4a178cfa0f5679170fd28b80e82f Mon Sep 17 00:00:00 2001 From: wanglei Date: Tue, 22 Nov 2022 11:19:43 +0800 Subject: [PATCH 09/10] srs.conf tab -> space --- trunk/conf/srs.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index 74e395e0d9..d02e71143d 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -39,5 +39,5 @@ vhost __defaultVhost__ { play{ gop_cache_max_frames 100; - } + } } From 0bd96edad766c48a41eb9a795a13b8aa270f9289 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 22 Nov 2022 12:00:28 +0800 Subject: [PATCH 10/10] Live: Limit cached max frames by gop_cache_max_frames. v5.0.93 --- trunk/conf/full.conf | 5 ++--- trunk/conf/srs.conf | 2 +- trunk/doc/CHANGELOG.md | 1 + trunk/src/app/srs_app_config.hpp | 3 +-- trunk/src/app/srs_app_rtmp_conn.cpp | 10 +++++----- trunk/src/app/srs_app_source.cpp | 14 ++++---------- trunk/src/core/srs_core_version5.hpp | 2 +- 7 files changed, 15 insertions(+), 22 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 2e975714a2..b7ed2bef2f 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -1097,9 +1097,8 @@ vhost play.srs.com { # default: on gop_cache off; - # to limit the max gop cache frames - # without this limit, if ingest stream always has no IDR frame, - # it may cause srs run out of memory + # Limit the max frames in gop cache. It might cause OOM if video stream has no IDR frame, so we limit to N + # frames by default. # default: 250 gop_cache_max_frames 250; diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index d02e71143d..5194fc2620 100644 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -38,6 +38,6 @@ vhost __defaultVhost__ { } play{ - gop_cache_max_frames 100; + gop_cache_max_frames 250; } } diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 1f097bb371..3f57d733ed 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for SRS. ## SRS 5.0 Changelog +* v5.0, 2022-11-22, Merge [#3236](https://github.com/ossrs/srs/pull/3236): Live: Limit cached max frames by gop_cache_max_frames. v5.0.93 * v5.0, 2022-11-22, Asan: Check libasan and show tips. v5.0.92 * v5.0, 2022-11-21, Merge [#3264](https://github.com/ossrs/srs/pull/3264): Asan: Try to fix st_memory_leak for asan check. (#3264). v5.0.91 * v5.0, 2022-11-21, Asan: Fix global ip address leak check. v5.0.90 diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index f54b4674fe..0863c09a7e 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -553,8 +553,7 @@ class SrsConfig // @return true when gop_cache is ok; otherwise, false. // @remark, default true. virtual bool get_gop_cache(std::string vhost); - // to set the max frames limit in gop cache - // @return gop cache frames limit + // Get the limit max frames for gop cache. virtual int get_gop_cache_max_frames(std::string vhost); // Whether debug_srs_upnode is enabled of vhost. // debug_srs_upnode is very important feature for tracable log, diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index a9c907c66a..f8b14b6899 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -557,12 +557,12 @@ srs_error_t SrsRtmpConn::stream_service_cycle() srs_assert(source != NULL); bool enabled_cache = _srs_config->get_gop_cache(req->vhost); - int gcf = _srs_config->get_gop_cache_max_frames(req->vhost); - - srs_trace("source url=%s, ip=%s, cache=%d, gop_cache_max_frames=%d, is_edge=%d, source_id=%s/%s", - req->get_stream_url().c_str(), ip.c_str(), enabled_cache, gcf, info->edge, source->source_id().c_str(), source->pre_source_id().c_str()); + int gcmf = _srs_config->get_gop_cache_max_frames(req->vhost); + srs_trace("source url=%s, ip=%s, cache=%d/%d, is_edge=%d, source_id=%s/%s", + req->get_stream_url().c_str(), ip.c_str(), enabled_cache, gcmf, info->edge, source->source_id().c_str(), + source->pre_source_id().c_str()); source->set_cache(enabled_cache); - source->set_gop_cache_max_frames(gcf); + source->set_gop_cache_max_frames(gcmf); switch (info->type) { case SrsRtmpConnPlay: { diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index cceb6e73d0..ca64be1525 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -575,7 +575,7 @@ SrsGopCache::SrsGopCache() cached_video_count = 0; enable_gop_cache = true; audio_after_last_video_count = 0; - gop_cache_max_frames_ = 250; + gop_cache_max_frames_ = 0; } SrsGopCache::~SrsGopCache() @@ -598,7 +598,6 @@ void SrsGopCache::set(bool v) } } - void SrsGopCache::set_gop_cache_max_frames(int v) { gop_cache_max_frames_ = v; @@ -656,16 +655,13 @@ srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) cached_video_count = 1; } - // cache the frame. gop_cache.push_back(msg->copy()); + // Clear gop cache if exceed the max frames. if (gop_cache.size() > (size_t)gop_cache_max_frames_) { - srs_warn("too many frames in the gop cache, %d video frames & %d audio frames dropped, " - "audio_after_last_video_count: %d.", - cached_video_count, - (int)gop_cache.size() - cached_video_count, - audio_after_last_video_count); + srs_warn("Gop cache exceed max frames=%d, total=%d, videos=%d, aalvc=%d", + gop_cache_max_frames_, (int)gop_cache.size(), cached_video_count, audio_after_last_video_count); clear(); } @@ -2103,7 +2099,6 @@ srs_error_t SrsLiveSource::on_reload_vhost_play(string vhost) string url = req->get_stream_url(); srs_trace("vhost %s gop_cache changed to %d, source url=%s", vhost.c_str(), v, url.c_str()); gop_cache->set(v); - gop_cache->set_gop_cache_max_frames(_srs_config->get_gop_cache_max_frames(vhost)); } } @@ -2749,7 +2744,6 @@ void SrsLiveSource::set_gop_cache_max_frames(int v) gop_cache->set_gop_cache_max_frames(v); } - SrsRtmpJitterAlgorithm SrsLiveSource::jitter() { return jitter_algorithm; diff --git a/trunk/src/core/srs_core_version5.hpp b/trunk/src/core/srs_core_version5.hpp index 63d68fa5a6..bb5c81146f 100644 --- a/trunk/src/core/srs_core_version5.hpp +++ b/trunk/src/core/srs_core_version5.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 5 #define VERSION_MINOR 0 -#define VERSION_REVISION 92 +#define VERSION_REVISION 93 #endif