From d81b2cb140d5386bc820f19ca863a3d5599a4377 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 11 Jan 2016 13:15:46 +0800 Subject: [PATCH] support hls mount to vhost and reload --- trunk/conf/full.conf | 2 + trunk/src/app/srs_app_http_static.cpp | 84 +++++++++++++++++++-------- trunk/src/app/srs_app_http_static.hpp | 3 + 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 2b0790186..1670ff08e 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -956,6 +956,7 @@ vhost with-hls.srs.com { # [vhost], the vhost of stream. # [app], the app of stream. # [stream], the stream name of stream. + # recommend: [vhost]/[app]/[stream].m3u8 # default: [app]/[stream].m3u8 hls_m3u8_file [app]/[stream].m3u8; # the hls ts file name. @@ -974,6 +975,7 @@ vhost with-hls.srs.com { # [seq], the sequence number of ts. # @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path # @see https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#hls-config + # recommend: [vhost]/[app]/[stream]-[seq].ts # default: [app]/[stream]-[seq].ts hls_ts_file [app]/[stream]-[seq].ts; # whether use floor for the hls_ts_file path generation. diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index b96c26395..c4019d87e 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -206,10 +206,12 @@ int SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r SrsHttpStaticServer::SrsHttpStaticServer(SrsServer* svr) { server = svr; + _srs_config->subscribe(this); } SrsHttpStaticServer::~SrsHttpStaticServer() { + _srs_config->unsubscribe(this); } int SrsHttpStaticServer::initialize() @@ -227,36 +229,17 @@ int SrsHttpStaticServer::initialize() continue; } - std::string vhost = conf->arg0(); - if (!_srs_config->get_vhost_http_enabled(vhost)) { - continue; - } - - std::string mount = _srs_config->get_vhost_http_mount(vhost); - std::string dir = _srs_config->get_vhost_http_dir(vhost); - - // replace the vhost variable - mount = srs_string_replace(mount, "[vhost]", vhost); - - // remove the default vhost mount - mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/"); - - // the dir mount must always ends with "/" - if (mount != "/" && !srs_string_ends_with(mount, "/")) { - mount += "/"; - } - - // mount the http of vhost. - if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) { - srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); + string pmount; + string vhost = conf->arg0(); + if ((ret = mount_vhost(vhost, pmount)) != ERROR_SUCCESS) { return ret; } - if (mount == "/") { + if (pmount == "/") { default_root_exists = true; + std::string dir = _srs_config->get_vhost_http_dir(vhost); srs_warn("http: root mount to %s", dir.c_str()); } - srs_trace("http: vhost=%s mount to %s", vhost.c_str(), mount.c_str()); } if (!default_root_exists) { @@ -272,6 +255,59 @@ int SrsHttpStaticServer::initialize() return ret; } +int SrsHttpStaticServer::mount_vhost(string vhost, string& pmount) +{ + int ret = ERROR_SUCCESS; + + // when vhost disabled, ignore. + if (!_srs_config->get_vhost_enabled(vhost)) { + return ret; + } + + // when vhost http_static disabled, ignore. + if (!_srs_config->get_vhost_http_enabled(vhost)) { + return ret; + } + + std::string mount = _srs_config->get_vhost_http_mount(vhost); + std::string dir = _srs_config->get_vhost_http_dir(vhost); + + // replace the vhost variable + mount = srs_string_replace(mount, "[vhost]", vhost); + dir = srs_string_replace(dir, "[vhost]", vhost); + + // remove the default vhost mount + mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/"); + + // the dir mount must always ends with "/" + if (mount != "/" && !srs_string_ends_with(mount, "/")) { + mount += "/"; + } + + // mount the http of vhost. + if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) { + srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); + return ret; + } + srs_trace("http: vhost=%s mount to %s at %s", vhost.c_str(), mount.c_str(), dir.c_str()); + + pmount = mount; + + return ret; +} + +int SrsHttpStaticServer::on_reload_vhost_added(string vhost) +{ + int ret = ERROR_SUCCESS; + + string pmount; + if ((ret = mount_vhost(vhost, pmount)) != ERROR_SUCCESS) { + return ret; + } + + return ret; +} + int SrsHttpStaticServer::on_reload_vhost_http_updated() { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_http_static.hpp b/trunk/src/app/srs_app_http_static.hpp index c89c9f8be..9ecb6579c 100644 --- a/trunk/src/app/srs_app_http_static.hpp +++ b/trunk/src/app/srs_app_http_static.hpp @@ -65,8 +65,11 @@ public: virtual ~SrsHttpStaticServer(); public: virtual int initialize(); +private: + virtual int mount_vhost(std::string vhost, std::string& pmount); // interface ISrsReloadHandler. public: + virtual int on_reload_vhost_added(std::string vhost); virtual int on_reload_vhost_http_updated(); };