winlin

support hls mount to vhost and reload

@@ -956,6 +956,7 @@ vhost with-hls.srs.com { @@ -956,6 +956,7 @@ vhost with-hls.srs.com {
956 # [vhost], the vhost of stream. 956 # [vhost], the vhost of stream.
957 # [app], the app of stream. 957 # [app], the app of stream.
958 # [stream], the stream name of stream. 958 # [stream], the stream name of stream.
  959 + # recommend: [vhost]/[app]/[stream].m3u8
959 # default: [app]/[stream].m3u8 960 # default: [app]/[stream].m3u8
960 hls_m3u8_file [app]/[stream].m3u8; 961 hls_m3u8_file [app]/[stream].m3u8;
961 # the hls ts file name. 962 # the hls ts file name.
@@ -974,6 +975,7 @@ vhost with-hls.srs.com { @@ -974,6 +975,7 @@ vhost with-hls.srs.com {
974 # [seq], the sequence number of ts. 975 # [seq], the sequence number of ts.
975 # @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path 976 # @see https://github.com/ossrs/srs/wiki/v2_CN_DVR#custom-path
976 # @see https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#hls-config 977 # @see https://github.com/ossrs/srs/wiki/v2_CN_DeliveryHLS#hls-config
  978 + # recommend: [vhost]/[app]/[stream]-[seq].ts
977 # default: [app]/[stream]-[seq].ts 979 # default: [app]/[stream]-[seq].ts
978 hls_ts_file [app]/[stream]-[seq].ts; 980 hls_ts_file [app]/[stream]-[seq].ts;
979 # whether use floor for the hls_ts_file path generation. 981 # whether use floor for the hls_ts_file path generation.
@@ -206,10 +206,12 @@ int SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r @@ -206,10 +206,12 @@ int SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
206 SrsHttpStaticServer::SrsHttpStaticServer(SrsServer* svr) 206 SrsHttpStaticServer::SrsHttpStaticServer(SrsServer* svr)
207 { 207 {
208 server = svr; 208 server = svr;
  209 + _srs_config->subscribe(this);
209 } 210 }
210 211
211 SrsHttpStaticServer::~SrsHttpStaticServer() 212 SrsHttpStaticServer::~SrsHttpStaticServer()
212 { 213 {
  214 + _srs_config->unsubscribe(this);
213 } 215 }
214 216
215 int SrsHttpStaticServer::initialize() 217 int SrsHttpStaticServer::initialize()
@@ -227,36 +229,17 @@ int SrsHttpStaticServer::initialize() @@ -227,36 +229,17 @@ int SrsHttpStaticServer::initialize()
227 continue; 229 continue;
228 } 230 }
229 231
230 - std::string vhost = conf->arg0();  
231 - if (!_srs_config->get_vhost_http_enabled(vhost)) {  
232 - continue;  
233 - }  
234 -  
235 - std::string mount = _srs_config->get_vhost_http_mount(vhost);  
236 - std::string dir = _srs_config->get_vhost_http_dir(vhost);  
237 -  
238 - // replace the vhost variable  
239 - mount = srs_string_replace(mount, "[vhost]", vhost);  
240 -  
241 - // remove the default vhost mount  
242 - mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");  
243 -  
244 - // the dir mount must always ends with "/"  
245 - if (mount != "/" && !srs_string_ends_with(mount, "/")) {  
246 - mount += "/";  
247 - }  
248 -  
249 - // mount the http of vhost.  
250 - if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {  
251 - srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); 232 + string pmount;
  233 + string vhost = conf->arg0();
  234 + if ((ret = mount_vhost(vhost, pmount)) != ERROR_SUCCESS) {
252 return ret; 235 return ret;
253 } 236 }
254 237
255 - if (mount == "/") { 238 + if (pmount == "/") {
256 default_root_exists = true; 239 default_root_exists = true;
  240 + std::string dir = _srs_config->get_vhost_http_dir(vhost);
257 srs_warn("http: root mount to %s", dir.c_str()); 241 srs_warn("http: root mount to %s", dir.c_str());
258 } 242 }
259 - srs_trace("http: vhost=%s mount to %s", vhost.c_str(), mount.c_str());  
260 } 243 }
261 244
262 if (!default_root_exists) { 245 if (!default_root_exists) {
@@ -272,6 +255,59 @@ int SrsHttpStaticServer::initialize() @@ -272,6 +255,59 @@ int SrsHttpStaticServer::initialize()
272 return ret; 255 return ret;
273 } 256 }
274 257
  258 +int SrsHttpStaticServer::mount_vhost(string vhost, string& pmount)
  259 +{
  260 + int ret = ERROR_SUCCESS;
  261 +
  262 + // when vhost disabled, ignore.
  263 + if (!_srs_config->get_vhost_enabled(vhost)) {
  264 + return ret;
  265 + }
  266 +
  267 + // when vhost http_static disabled, ignore.
  268 + if (!_srs_config->get_vhost_http_enabled(vhost)) {
  269 + return ret;
  270 + }
  271 +
  272 + std::string mount = _srs_config->get_vhost_http_mount(vhost);
  273 + std::string dir = _srs_config->get_vhost_http_dir(vhost);
  274 +
  275 + // replace the vhost variable
  276 + mount = srs_string_replace(mount, "[vhost]", vhost);
  277 + dir = srs_string_replace(dir, "[vhost]", vhost);
  278 +
  279 + // remove the default vhost mount
  280 + mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
  281 +
  282 + // the dir mount must always ends with "/"
  283 + if (mount != "/" && !srs_string_ends_with(mount, "/")) {
  284 + mount += "/";
  285 + }
  286 +
  287 + // mount the http of vhost.
  288 + if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) {
  289 + srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
  290 + return ret;
  291 + }
  292 + srs_trace("http: vhost=%s mount to %s at %s", vhost.c_str(), mount.c_str(), dir.c_str());
  293 +
  294 + pmount = mount;
  295 +
  296 + return ret;
  297 +}
  298 +
  299 +int SrsHttpStaticServer::on_reload_vhost_added(string vhost)
  300 +{
  301 + int ret = ERROR_SUCCESS;
  302 +
  303 + string pmount;
  304 + if ((ret = mount_vhost(vhost, pmount)) != ERROR_SUCCESS) {
  305 + return ret;
  306 + }
  307 +
  308 + return ret;
  309 +}
  310 +
275 int SrsHttpStaticServer::on_reload_vhost_http_updated() 311 int SrsHttpStaticServer::on_reload_vhost_http_updated()
276 { 312 {
277 int ret = ERROR_SUCCESS; 313 int ret = ERROR_SUCCESS;
@@ -65,8 +65,11 @@ public: @@ -65,8 +65,11 @@ public:
65 virtual ~SrsHttpStaticServer(); 65 virtual ~SrsHttpStaticServer();
66 public: 66 public:
67 virtual int initialize(); 67 virtual int initialize();
  68 +private:
  69 + virtual int mount_vhost(std::string vhost, std::string& pmount);
68 // interface ISrsReloadHandler. 70 // interface ISrsReloadHandler.
69 public: 71 public:
  72 + virtual int on_reload_vhost_added(std::string vhost);
70 virtual int on_reload_vhost_http_updated(); 73 virtual int on_reload_vhost_http_updated();
71 }; 74 };
72 75