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,9 +229,44 @@ int SrsHttpStaticServer::initialize() @@ -227,9 +229,44 @@ int SrsHttpStaticServer::initialize()
227 continue; 229 continue;
228 } 230 }
229 231
230 - std::string vhost = conf->arg0(); 232 + string pmount;
  233 + string vhost = conf->arg0();
  234 + if ((ret = mount_vhost(vhost, pmount)) != ERROR_SUCCESS) {
  235 + return ret;
  236 + }
  237 +
  238 + if (pmount == "/") {
  239 + default_root_exists = true;
  240 + std::string dir = _srs_config->get_vhost_http_dir(vhost);
  241 + srs_warn("http: root mount to %s", dir.c_str());
  242 + }
  243 + }
  244 +
  245 + if (!default_root_exists) {
  246 + // add root
  247 + std::string dir = _srs_config->get_http_stream_dir();
  248 + if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) {
  249 + srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret);
  250 + return ret;
  251 + }
  252 + srs_trace("http: root mount to %s", dir.c_str());
  253 + }
  254 +
  255 + return ret;
  256 +}
  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.
231 if (!_srs_config->get_vhost_http_enabled(vhost)) { 268 if (!_srs_config->get_vhost_http_enabled(vhost)) {
232 - continue; 269 + return ret;
233 } 270 }
234 271
235 std::string mount = _srs_config->get_vhost_http_mount(vhost); 272 std::string mount = _srs_config->get_vhost_http_mount(vhost);
@@ -237,6 +274,7 @@ int SrsHttpStaticServer::initialize() @@ -237,6 +274,7 @@ int SrsHttpStaticServer::initialize()
237 274
238 // replace the vhost variable 275 // replace the vhost variable
239 mount = srs_string_replace(mount, "[vhost]", vhost); 276 mount = srs_string_replace(mount, "[vhost]", vhost);
  277 + dir = srs_string_replace(dir, "[vhost]", vhost);
240 278
241 // remove the default vhost mount 279 // remove the default vhost mount
242 mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/"); 280 mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
@@ -251,22 +289,20 @@ int SrsHttpStaticServer::initialize() @@ -251,22 +289,20 @@ int SrsHttpStaticServer::initialize()
251 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); 289 srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
252 return ret; 290 return ret;
253 } 291 }
  292 + srs_trace("http: vhost=%s mount to %s at %s", vhost.c_str(), mount.c_str(), dir.c_str());
254 293
255 - if (mount == "/") {  
256 - default_root_exists = true;  
257 - srs_warn("http: root mount to %s", dir.c_str());  
258 - }  
259 - srs_trace("http: vhost=%s mount to %s", vhost.c_str(), mount.c_str());  
260 - } 294 + pmount = mount;
261 295
262 - if (!default_root_exists) {  
263 - // add root  
264 - std::string dir = _srs_config->get_http_stream_dir();  
265 - if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) {  
266 - srs_error("http: mount root dir=%s failed. ret=%d", dir.c_str(), ret);  
267 return ret; 296 return ret;
268 - }  
269 - srs_trace("http: root mount to %s", dir.c_str()); 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;
270 } 306 }
271 307
272 return ret; 308 return ret;
@@ -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