From d611bb6b45312e4c92acfea462e1f7206b446d9a Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 30 May 2015 10:48:02 +0800 Subject: [PATCH] for #209, server cycle to enable the hls to cleanup. do dispose --- trunk/conf/full.conf | 7 ++++- trunk/src/app/srs_app_config.cpp | 21 ++++++++++++++- trunk/src/app/srs_app_config.hpp | 4 +++ trunk/src/app/srs_app_hls.cpp | 13 +++++++++ trunk/src/app/srs_app_hls.hpp | 3 +++ trunk/src/app/srs_app_server.cpp | 10 ++++++- trunk/src/app/srs_app_source.cpp | 46 ++++++++++++++++++++++++++++++++ trunk/src/app/srs_app_source.hpp | 8 ++++++ 8 files changed, 109 insertions(+), 3 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 211a2d1d87..cab0cd054d 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -618,9 +618,14 @@ vhost with-hls.srs.com { # h264, vn # default: h264 hls_vcodec h264; - # whether cleanup the old ts files. + # whether cleanup the old expired ts files. # default: on hls_cleanup on; + # the timeout in seconds to dispose the hls, + # dispose is to remove all hls files, m3u8 and ts files. + # when timeout or server terminate, dispose hls. + # default: 300 + hls_dispose 300; # the max size to notify hls, # to read max bytes from ts of specified cdn network, # @remark only used when on_hls_notify is config. diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index cfd57f57b1..056f1efdd0 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1564,7 +1564,8 @@ int SrsConfig::check_config() string m = conf->at(j)->name.c_str(); if (m != "enabled" && m != "hls_entry_prefix" && m != "hls_path" && m != "hls_fragment" && m != "hls_window" && m != "hls_on_error" && m != "hls_storage" && m != "hls_mount" && m != "hls_td_ratio" && m != "hls_aof_ratio" && m != "hls_acodec" && m != "hls_vcodec" - && m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify" && m != "hls_wait_keyframe" + && m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify" + && m != "hls_wait_keyframe" && m != "hls_dispose" ) { ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("unsupported vhost hls directive %s, ret=%d", m.c_str(), ret); @@ -3561,6 +3562,24 @@ bool SrsConfig::get_hls_cleanup(string vhost) return SRS_CONF_PERFER_TRUE(conf->arg0()); } +int SrsConfig::get_hls_dispose(string vhost) +{ + SrsConfDirective* conf = get_hls(vhost); + + int DEFAULT = 300; + + if (!conf) { + return DEFAULT; + } + + conf = conf->get("hls_dispose"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + + return ::atoi(conf->arg0().c_str()); +} + bool SrsConfig::get_hls_wait_keyframe(string vhost) { SrsConfDirective* hls = get_hls(vhost); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 2534896837..eb03745405 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -984,6 +984,10 @@ class SrsConfig * whether cleanup the old ts files. */ virtual bool get_hls_cleanup(std::string vhost); + /** + * the timeout to dispose the hls. + */ + virtual int get_hls_dispose(std::string vhost); /** * whether reap the ts when got keyframe. */ diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index a11711b1a1..a7ad5a5eff 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -1109,6 +1109,19 @@ SrsHls::~SrsHls() srs_freep(pprint); } +void SrsHls::dispose() +{ +} + +int SrsHls::cycle() +{ + int ret = ERROR_SUCCESS; + + srs_info("hls cycle for source %d", source->source_id()); + + return ret; +} + int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_hls.hpp b/trunk/src/app/srs_app_hls.hpp index c47e2114ee..78e6604107 100644 --- a/trunk/src/app/srs_app_hls.hpp +++ b/trunk/src/app/srs_app_hls.hpp @@ -402,6 +402,9 @@ class SrsHls public: SrsHls(); virtual ~SrsHls(); +public: + virtual void dispose(); + virtual int cycle(); public: /** * initialize the hls by handler and source. diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 8e945a4950..f9918d56f6 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -562,9 +562,12 @@ void SrsServer::dispose() #ifdef SRS_AUTO_INGEST ingester->dispose(); - srs_trace("gracefully cleanup ingesters"); + srs_trace("gracefully dispose ingesters"); #endif + SrsSource::dispose_all(); + srs_trace("gracefully dispose sources"); + srs_trace("terminate server"); } @@ -962,6 +965,11 @@ int SrsServer::do_cycle() srs_trace("reload config success."); } + // notice the stream sources to cycle. + if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) { + return ret; + } + // update the cache time if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) { srs_info("update current time cache."); diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index bafa2d7a89..2aa5e157ec 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -771,6 +771,32 @@ SrsSource* SrsSource::fetch(std::string vhost, std::string app, std::string stre return source; } +void SrsSource::dispose_all() +{ + std::map::iterator it; + for (it = pool.begin(); it != pool.end(); ++it) { + SrsSource* source = it->second; + source->dispose(); + } + return; +} + +int SrsSource::cycle_all() +{ + int ret = ERROR_SUCCESS; + + // TODO: FIXME: support remove dead source for a long time. + std::map::iterator it; + for (it = pool.begin(); it != pool.end(); ++it) { + SrsSource* source = it->second; + if ((ret = source->cycle()) != ERROR_SUCCESS) { + return ret; + } + } + + return ret; +} + void SrsSource::destroy() { std::map::iterator it; @@ -909,6 +935,26 @@ SrsSource::~SrsSource() srs_freep(_req); } +void SrsSource::dispose() +{ +#ifdef SRS_AUTO_HLS + hls->dispose(); +#endif +} + +int SrsSource::cycle() +{ + int ret = ERROR_SUCCESS; + +#ifdef SRS_AUTO_HLS + if ((ret = hls->cycle()) != ERROR_SUCCESS) { + return ret; + } +#endif + + return ret; +} + int SrsSource::initialize(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 3c83e1163a..f7a75d4ac9 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -410,6 +410,11 @@ class SrsSource : public ISrsReloadHandler * get the exists source by stream info(vhost, app, stream), NULL when not exists. */ static SrsSource* fetch(std::string vhost, std::string app, std::string stream); + /** + * dispose and cycle all sources. + */ + static void dispose_all(); + static int cycle_all(); /** * when system exit, destroy the sources, * for gmc to analysis mem leaks. @@ -486,6 +491,9 @@ class SrsSource : public ISrsReloadHandler public: SrsSource(); virtual ~SrsSource(); +public: + virtual void dispose(); + virtual int cycle(); // initialize, get and setter. public: /**