Skip to content

Commit

Permalink
for #209, server cycle to enable the hls to cleanup. do dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed May 30, 2015
1 parent 567d84e commit d611bb6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
7 changes: 6 additions & 1 deletion trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
13 changes: 13 additions & 0 deletions trunk/src/app/srs_app_hls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_hls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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.");
Expand Down
46 changes: 46 additions & 0 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,32 @@ SrsSource* SrsSource::fetch(std::string vhost, std::string app, std::string stre
return source;
}

void SrsSource::dispose_all()
{
std::map<std::string, SrsSource*>::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<std::string, SrsSource*>::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<std::string, SrsSource*>::iterator it;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions trunk/src/app/srs_app_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -486,6 +491,9 @@ class SrsSource : public ISrsReloadHandler
public:
SrsSource();
virtual ~SrsSource();
public:
virtual void dispose();
virtual int cycle();
// initialize, get and setter.
public:
/**
Expand Down

0 comments on commit d611bb6

Please sign in to comment.