winlin

for #209, server cycle to enable the hls to cleanup. do dispose

@@ -618,9 +618,14 @@ vhost with-hls.srs.com { @@ -618,9 +618,14 @@ vhost with-hls.srs.com {
618 # h264, vn 618 # h264, vn
619 # default: h264 619 # default: h264
620 hls_vcodec h264; 620 hls_vcodec h264;
621 - # whether cleanup the old ts files. 621 + # whether cleanup the old expired ts files.
622 # default: on 622 # default: on
623 hls_cleanup on; 623 hls_cleanup on;
  624 + # the timeout in seconds to dispose the hls,
  625 + # dispose is to remove all hls files, m3u8 and ts files.
  626 + # when timeout or server terminate, dispose hls.
  627 + # default: 300
  628 + hls_dispose 300;
624 # the max size to notify hls, 629 # the max size to notify hls,
625 # to read max bytes from ts of specified cdn network, 630 # to read max bytes from ts of specified cdn network,
626 # @remark only used when on_hls_notify is config. 631 # @remark only used when on_hls_notify is config.
@@ -1564,7 +1564,8 @@ int SrsConfig::check_config() @@ -1564,7 +1564,8 @@ int SrsConfig::check_config()
1564 string m = conf->at(j)->name.c_str(); 1564 string m = conf->at(j)->name.c_str();
1565 if (m != "enabled" && m != "hls_entry_prefix" && m != "hls_path" && m != "hls_fragment" && m != "hls_window" && m != "hls_on_error" 1565 if (m != "enabled" && m != "hls_entry_prefix" && m != "hls_path" && m != "hls_fragment" && m != "hls_window" && m != "hls_on_error"
1566 && m != "hls_storage" && m != "hls_mount" && m != "hls_td_ratio" && m != "hls_aof_ratio" && m != "hls_acodec" && m != "hls_vcodec" 1566 && m != "hls_storage" && m != "hls_mount" && m != "hls_td_ratio" && m != "hls_aof_ratio" && m != "hls_acodec" && m != "hls_vcodec"
1567 - && m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify" && m != "hls_wait_keyframe" 1567 + && m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify"
  1568 + && m != "hls_wait_keyframe" && m != "hls_dispose"
1568 ) { 1569 ) {
1569 ret = ERROR_SYSTEM_CONFIG_INVALID; 1570 ret = ERROR_SYSTEM_CONFIG_INVALID;
1570 srs_error("unsupported vhost hls directive %s, ret=%d", m.c_str(), ret); 1571 srs_error("unsupported vhost hls directive %s, ret=%d", m.c_str(), ret);
@@ -3561,6 +3562,24 @@ bool SrsConfig::get_hls_cleanup(string vhost) @@ -3561,6 +3562,24 @@ bool SrsConfig::get_hls_cleanup(string vhost)
3561 return SRS_CONF_PERFER_TRUE(conf->arg0()); 3562 return SRS_CONF_PERFER_TRUE(conf->arg0());
3562 } 3563 }
3563 3564
  3565 +int SrsConfig::get_hls_dispose(string vhost)
  3566 +{
  3567 + SrsConfDirective* conf = get_hls(vhost);
  3568 +
  3569 + int DEFAULT = 300;
  3570 +
  3571 + if (!conf) {
  3572 + return DEFAULT;
  3573 + }
  3574 +
  3575 + conf = conf->get("hls_dispose");
  3576 + if (!conf || conf->arg0().empty()) {
  3577 + return DEFAULT;
  3578 + }
  3579 +
  3580 + return ::atoi(conf->arg0().c_str());
  3581 +}
  3582 +
3564 bool SrsConfig::get_hls_wait_keyframe(string vhost) 3583 bool SrsConfig::get_hls_wait_keyframe(string vhost)
3565 { 3584 {
3566 SrsConfDirective* hls = get_hls(vhost); 3585 SrsConfDirective* hls = get_hls(vhost);
@@ -985,6 +985,10 @@ public: @@ -985,6 +985,10 @@ public:
985 */ 985 */
986 virtual bool get_hls_cleanup(std::string vhost); 986 virtual bool get_hls_cleanup(std::string vhost);
987 /** 987 /**
  988 + * the timeout to dispose the hls.
  989 + */
  990 + virtual int get_hls_dispose(std::string vhost);
  991 + /**
988 * whether reap the ts when got keyframe. 992 * whether reap the ts when got keyframe.
989 */ 993 */
990 virtual bool get_hls_wait_keyframe(std::string vhost); 994 virtual bool get_hls_wait_keyframe(std::string vhost);
@@ -1109,6 +1109,19 @@ SrsHls::~SrsHls() @@ -1109,6 +1109,19 @@ SrsHls::~SrsHls()
1109 srs_freep(pprint); 1109 srs_freep(pprint);
1110 } 1110 }
1111 1111
  1112 +void SrsHls::dispose()
  1113 +{
  1114 +}
  1115 +
  1116 +int SrsHls::cycle()
  1117 +{
  1118 + int ret = ERROR_SUCCESS;
  1119 +
  1120 + srs_info("hls cycle for source %d", source->source_id());
  1121 +
  1122 + return ret;
  1123 +}
  1124 +
1112 int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h) 1125 int SrsHls::initialize(SrsSource* s, ISrsHlsHandler* h)
1113 { 1126 {
1114 int ret = ERROR_SUCCESS; 1127 int ret = ERROR_SUCCESS;
@@ -403,6 +403,9 @@ public: @@ -403,6 +403,9 @@ public:
403 SrsHls(); 403 SrsHls();
404 virtual ~SrsHls(); 404 virtual ~SrsHls();
405 public: 405 public:
  406 + virtual void dispose();
  407 + virtual int cycle();
  408 +public:
406 /** 409 /**
407 * initialize the hls by handler and source. 410 * initialize the hls by handler and source.
408 */ 411 */
@@ -562,9 +562,12 @@ void SrsServer::dispose() @@ -562,9 +562,12 @@ void SrsServer::dispose()
562 562
563 #ifdef SRS_AUTO_INGEST 563 #ifdef SRS_AUTO_INGEST
564 ingester->dispose(); 564 ingester->dispose();
565 - srs_trace("gracefully cleanup ingesters"); 565 + srs_trace("gracefully dispose ingesters");
566 #endif 566 #endif
567 567
  568 + SrsSource::dispose_all();
  569 + srs_trace("gracefully dispose sources");
  570 +
568 srs_trace("terminate server"); 571 srs_trace("terminate server");
569 } 572 }
570 573
@@ -962,6 +965,11 @@ int SrsServer::do_cycle() @@ -962,6 +965,11 @@ int SrsServer::do_cycle()
962 srs_trace("reload config success."); 965 srs_trace("reload config success.");
963 } 966 }
964 967
  968 + // notice the stream sources to cycle.
  969 + if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) {
  970 + return ret;
  971 + }
  972 +
965 // update the cache time 973 // update the cache time
966 if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) { 974 if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) {
967 srs_info("update current time cache."); 975 srs_info("update current time cache.");
@@ -771,6 +771,32 @@ SrsSource* SrsSource::fetch(std::string vhost, std::string app, std::string stre @@ -771,6 +771,32 @@ SrsSource* SrsSource::fetch(std::string vhost, std::string app, std::string stre
771 return source; 771 return source;
772 } 772 }
773 773
  774 +void SrsSource::dispose_all()
  775 +{
  776 + std::map<std::string, SrsSource*>::iterator it;
  777 + for (it = pool.begin(); it != pool.end(); ++it) {
  778 + SrsSource* source = it->second;
  779 + source->dispose();
  780 + }
  781 + return;
  782 +}
  783 +
  784 +int SrsSource::cycle_all()
  785 +{
  786 + int ret = ERROR_SUCCESS;
  787 +
  788 + // TODO: FIXME: support remove dead source for a long time.
  789 + std::map<std::string, SrsSource*>::iterator it;
  790 + for (it = pool.begin(); it != pool.end(); ++it) {
  791 + SrsSource* source = it->second;
  792 + if ((ret = source->cycle()) != ERROR_SUCCESS) {
  793 + return ret;
  794 + }
  795 + }
  796 +
  797 + return ret;
  798 +}
  799 +
774 void SrsSource::destroy() 800 void SrsSource::destroy()
775 { 801 {
776 std::map<std::string, SrsSource*>::iterator it; 802 std::map<std::string, SrsSource*>::iterator it;
@@ -909,6 +935,26 @@ SrsSource::~SrsSource() @@ -909,6 +935,26 @@ SrsSource::~SrsSource()
909 srs_freep(_req); 935 srs_freep(_req);
910 } 936 }
911 937
  938 +void SrsSource::dispose()
  939 +{
  940 +#ifdef SRS_AUTO_HLS
  941 + hls->dispose();
  942 +#endif
  943 +}
  944 +
  945 +int SrsSource::cycle()
  946 +{
  947 + int ret = ERROR_SUCCESS;
  948 +
  949 +#ifdef SRS_AUTO_HLS
  950 + if ((ret = hls->cycle()) != ERROR_SUCCESS) {
  951 + return ret;
  952 + }
  953 +#endif
  954 +
  955 + return ret;
  956 +}
  957 +
912 int SrsSource::initialize(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh) 958 int SrsSource::initialize(SrsRequest* r, ISrsSourceHandler* h, ISrsHlsHandler* hh)
913 { 959 {
914 int ret = ERROR_SUCCESS; 960 int ret = ERROR_SUCCESS;
@@ -411,6 +411,11 @@ public: @@ -411,6 +411,11 @@ public:
411 */ 411 */
412 static SrsSource* fetch(std::string vhost, std::string app, std::string stream); 412 static SrsSource* fetch(std::string vhost, std::string app, std::string stream);
413 /** 413 /**
  414 + * dispose and cycle all sources.
  415 + */
  416 + static void dispose_all();
  417 + static int cycle_all();
  418 + /**
414 * when system exit, destroy the sources, 419 * when system exit, destroy the sources,
415 * for gmc to analysis mem leaks. 420 * for gmc to analysis mem leaks.
416 */ 421 */
@@ -486,6 +491,9 @@ private: @@ -486,6 +491,9 @@ private:
486 public: 491 public:
487 SrsSource(); 492 SrsSource();
488 virtual ~SrsSource(); 493 virtual ~SrsSource();
  494 +public:
  495 + virtual void dispose();
  496 + virtual int cycle();
489 // initialize, get and setter. 497 // initialize, get and setter.
490 public: 498 public:
491 /** 499 /**