winlin

support reload http_stream

@@ -572,6 +572,11 @@ int SrsConfig::reload() @@ -572,6 +572,11 @@ int SrsConfig::reload()
572 if ((ret = reload_http_api(old_root)) != ERROR_SUCCESS) { 572 if ((ret = reload_http_api(old_root)) != ERROR_SUCCESS) {
573 return ret; 573 return ret;
574 } 574 }
  575 +
  576 + // merge config: http_stream
  577 + if ((ret = reload_http_stream(old_root)) != ERROR_SUCCESS) {
  578 + return ret;
  579 + }
575 580
576 // merge config: vhost 581 // merge config: vhost
577 if ((ret = reload_vhost(old_root)) != ERROR_SUCCESS) { 582 if ((ret = reload_vhost(old_root)) != ERROR_SUCCESS) {
@@ -646,6 +651,71 @@ int SrsConfig::reload_http_api(SrsConfDirective* old_root) @@ -646,6 +651,71 @@ int SrsConfig::reload_http_api(SrsConfDirective* old_root)
646 return ret; 651 return ret;
647 } 652 }
648 653
  654 +int SrsConfig::reload_http_stream(SrsConfDirective* old_root)
  655 +{
  656 + int ret = ERROR_SUCCESS;
  657 +
  658 + // merge config.
  659 + std::vector<ISrsReloadHandler*>::iterator it;
  660 +
  661 + // state graph
  662 + // old_http_stream new_http_stream
  663 + // DISABLED => ENABLED
  664 + // ENABLED => DISABLED
  665 + // ENABLED => ENABLED (modified)
  666 +
  667 + SrsConfDirective* new_http_stream = root->get("http_stream");
  668 + SrsConfDirective* old_http_stream = old_root->get("http_stream");
  669 +
  670 + // DISABLED => ENABLED
  671 + if (!get_http_stream_enabled(old_http_stream) && get_http_stream_enabled(new_http_stream)) {
  672 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  673 + ISrsReloadHandler* subscribe = *it;
  674 + if ((ret = subscribe->on_reload_http_stream_enabled()) != ERROR_SUCCESS) {
  675 + srs_error("notify subscribes http_stream disabled=>enabled failed. ret=%d", ret);
  676 + return ret;
  677 + }
  678 + }
  679 + srs_trace("reload disabled=>enabled http_stream success.");
  680 +
  681 + return ret;
  682 + }
  683 +
  684 + // ENABLED => DISABLED
  685 + if (get_http_stream_enabled(old_http_stream) && !get_http_stream_enabled(new_http_stream)) {
  686 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  687 + ISrsReloadHandler* subscribe = *it;
  688 + if ((ret = subscribe->on_reload_http_stream_disabled()) != ERROR_SUCCESS) {
  689 + srs_error("notify subscribes http_stream enabled=>disabled failed. ret=%d", ret);
  690 + return ret;
  691 + }
  692 + }
  693 + srs_trace("reload enabled=>disabled http_stream success.");
  694 +
  695 + return ret;
  696 + }
  697 +
  698 + // ENABLED => ENABLED (modified)
  699 + if (get_http_stream_enabled(old_http_stream) && get_http_stream_enabled(new_http_stream)
  700 + && !srs_directive_equals(old_http_stream, new_http_stream)
  701 + ) {
  702 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  703 + ISrsReloadHandler* subscribe = *it;
  704 + if ((ret = subscribe->on_reload_http_stream_updated()) != ERROR_SUCCESS) {
  705 + srs_error("notify subscribes http_stream enabled modified failed. ret=%d", ret);
  706 + return ret;
  707 + }
  708 + }
  709 + srs_trace("reload enabled modified http_stream success.");
  710 +
  711 + return ret;
  712 + }
  713 +
  714 + srs_trace("reload http_stream not changed success.");
  715 +
  716 + return ret;
  717 +}
  718 +
649 int SrsConfig::reload_vhost(SrsConfDirective* old_root) 719 int SrsConfig::reload_vhost(SrsConfDirective* old_root)
650 { 720 {
651 int ret = ERROR_SUCCESS; 721 int ret = ERROR_SUCCESS;
@@ -774,6 +844,17 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root) @@ -774,6 +844,17 @@ int SrsConfig::reload_vhost(SrsConfDirective* old_root)
774 } 844 }
775 srs_trace("vhost %s reload hls success.", vhost.c_str()); 845 srs_trace("vhost %s reload hls success.", vhost.c_str());
776 } 846 }
  847 + // http, only one per vhost.
  848 + if (!srs_directive_equals(new_vhost->get("http"), old_vhost->get("http"))) {
  849 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  850 + ISrsReloadHandler* subscribe = *it;
  851 + if ((ret = subscribe->on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
  852 + srs_error("vhost %s notify subscribes http failed. ret=%d", vhost.c_str(), ret);
  853 + return ret;
  854 + }
  855 + }
  856 + srs_trace("vhost %s reload http success.", vhost.c_str());
  857 + }
777 // transcode, many per vhost. 858 // transcode, many per vhost.
778 if ((ret = reload_transcode(new_vhost, old_vhost)) != ERROR_SUCCESS) { 859 if ((ret = reload_transcode(new_vhost, old_vhost)) != ERROR_SUCCESS) {
779 return ret; 860 return ret;
@@ -2232,6 +2313,11 @@ SrsConfDirective* SrsConfig::get_http_stream() @@ -2232,6 +2313,11 @@ SrsConfDirective* SrsConfig::get_http_stream()
2232 bool SrsConfig::get_http_stream_enabled() 2313 bool SrsConfig::get_http_stream_enabled()
2233 { 2314 {
2234 SrsConfDirective* conf = get_http_stream(); 2315 SrsConfDirective* conf = get_http_stream();
  2316 + return get_http_stream_enabled(conf);
  2317 +}
  2318 +
  2319 +bool SrsConfig::get_http_stream_enabled(SrsConfDirective* conf)
  2320 +{
2235 if (!conf) { 2321 if (!conf) {
2236 return false; 2322 return false;
2237 } 2323 }
@@ -125,6 +125,7 @@ public: @@ -125,6 +125,7 @@ public:
125 virtual int reload(); 125 virtual int reload();
126 private: 126 private:
127 virtual int reload_http_api(SrsConfDirective* old_root); 127 virtual int reload_http_api(SrsConfDirective* old_root);
  128 + virtual int reload_http_stream(SrsConfDirective* old_root);
128 virtual int reload_vhost(SrsConfDirective* old_root); 129 virtual int reload_vhost(SrsConfDirective* old_root);
129 virtual int reload_transcode(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost); 130 virtual int reload_transcode(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost);
130 virtual int reload_ingest(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost); 131 virtual int reload_ingest(SrsConfDirective* new_vhost, SrsConfDirective* old_vhost);
@@ -231,6 +232,7 @@ private: @@ -231,6 +232,7 @@ private:
231 virtual SrsConfDirective* get_http_stream(); 232 virtual SrsConfDirective* get_http_stream();
232 public: 233 public:
233 virtual bool get_http_stream_enabled(); 234 virtual bool get_http_stream_enabled();
  235 + virtual bool get_http_stream_enabled(SrsConfDirective* conf);
234 virtual int get_http_stream_listen(); 236 virtual int get_http_stream_listen();
235 virtual std::string get_http_stream_dir(); 237 virtual std::string get_http_stream_dir();
236 public: 238 public:
@@ -75,6 +75,26 @@ int ISrsReloadHandler::on_reload_http_api_disabled() @@ -75,6 +75,26 @@ int ISrsReloadHandler::on_reload_http_api_disabled()
75 return ERROR_SUCCESS; 75 return ERROR_SUCCESS;
76 } 76 }
77 77
  78 +int ISrsReloadHandler::on_reload_http_stream_enabled()
  79 +{
  80 + return ERROR_SUCCESS;
  81 +}
  82 +
  83 +int ISrsReloadHandler::on_reload_http_stream_disabled()
  84 +{
  85 + return ERROR_SUCCESS;
  86 +}
  87 +
  88 +int ISrsReloadHandler::on_reload_http_stream_updated()
  89 +{
  90 + return ERROR_SUCCESS;
  91 +}
  92 +
  93 +int ISrsReloadHandler::on_reload_vhost_http_updated()
  94 +{
  95 + return ERROR_SUCCESS;
  96 +}
  97 +
78 int ISrsReloadHandler::on_reload_vhost_added(string /*vhost*/) 98 int ISrsReloadHandler::on_reload_vhost_added(string /*vhost*/)
79 { 99 {
80 return ERROR_SUCCESS; 100 return ERROR_SUCCESS;
@@ -49,6 +49,10 @@ public: @@ -49,6 +49,10 @@ public:
49 virtual int on_reload_pithy_print(); 49 virtual int on_reload_pithy_print();
50 virtual int on_reload_http_api_enabled(); 50 virtual int on_reload_http_api_enabled();
51 virtual int on_reload_http_api_disabled(); 51 virtual int on_reload_http_api_disabled();
  52 + virtual int on_reload_http_stream_enabled();
  53 + virtual int on_reload_http_stream_disabled();
  54 + virtual int on_reload_http_stream_updated();
  55 + virtual int on_reload_vhost_http_updated();
52 virtual int on_reload_vhost_added(std::string vhost); 56 virtual int on_reload_vhost_added(std::string vhost);
53 virtual int on_reload_vhost_removed(std::string vhost); 57 virtual int on_reload_vhost_removed(std::string vhost);
54 virtual int on_reload_atc(std::string vhost); 58 virtual int on_reload_atc(std::string vhost);
@@ -618,16 +618,109 @@ int SrsServer::on_reload_pid() @@ -618,16 +618,109 @@ int SrsServer::on_reload_pid()
618 return acquire_pid_file(); 618 return acquire_pid_file();
619 } 619 }
620 620
  621 +int SrsServer::on_reload_vhost_added(std::string vhost)
  622 +{
  623 + int ret = ERROR_SUCCESS;
  624 +
  625 +#ifdef SRS_HTTP_SERVER
  626 + if (!_srs_config->get_vhost_http_enabled(vhost)) {
  627 + return ret;
  628 + }
  629 +
  630 + if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
  631 + return ret;
  632 + }
  633 +#endif
  634 +
  635 + return ret;
  636 +}
  637 +
  638 +int SrsServer::on_reload_vhost_removed(std::string vhost)
  639 +{
  640 + int ret = ERROR_SUCCESS;
  641 +
  642 +#ifdef SRS_HTTP_SERVER
  643 + if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
  644 + return ret;
  645 + }
  646 +#endif
  647 +
  648 + return ret;
  649 +}
  650 +
  651 +int SrsServer::on_reload_vhost_http_updated()
  652 +{
  653 + int ret = ERROR_SUCCESS;
  654 +
  655 +#ifdef SRS_HTTP_SERVER
  656 + srs_freep(http_stream_handler);
  657 + http_stream_handler = SrsHttpHandler::create_http_stream();
  658 +
  659 + if ((ret = http_stream_handler->initialize()) != ERROR_SUCCESS) {
  660 + return ret;
  661 + }
  662 +#endif
  663 +
  664 + return ret;
  665 +}
  666 +
621 int SrsServer::on_reload_http_api_enabled() 667 int SrsServer::on_reload_http_api_enabled()
622 { 668 {
623 - return listen_http_api(); 669 + int ret = ERROR_SUCCESS;
  670 +
  671 +#ifdef SRS_HTTP_API
  672 + ret = listen_http_api();
  673 +#endif
  674 +
  675 + return ret;
624 } 676 }
625 677
626 int SrsServer::on_reload_http_api_disabled() 678 int SrsServer::on_reload_http_api_disabled()
627 { 679 {
628 int ret = ERROR_SUCCESS; 680 int ret = ERROR_SUCCESS;
629 681
  682 +#ifdef SRS_HTTP_API
630 close_listeners(SrsListenerHttpApi); 683 close_listeners(SrsListenerHttpApi);
  684 +#endif
  685 +
  686 + return ret;
  687 +}
  688 +
  689 +int SrsServer::on_reload_http_stream_enabled()
  690 +{
  691 + int ret = ERROR_SUCCESS;
  692 +
  693 +#ifdef SRS_HTTP_SERVER
  694 + ret = listen_http_stream();
  695 +#endif
  696 +
  697 + return ret;
  698 +}
  699 +
  700 +int SrsServer::on_reload_http_stream_disabled()
  701 +{
  702 + int ret = ERROR_SUCCESS;
  703 +
  704 +#ifdef SRS_HTTP_SERVER
  705 + close_listeners(SrsListenerHttpStream);
  706 +#endif
  707 +
  708 + return ret;
  709 +}
  710 +
  711 +int SrsServer::on_reload_http_stream_updated()
  712 +{
  713 + int ret = ERROR_SUCCESS;
  714 +
  715 +#ifdef SRS_HTTP_SERVER
  716 + if ((ret = on_reload_http_stream_enabled()) != ERROR_SUCCESS) {
  717 + return ret;
  718 + }
  719 +
  720 + if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
  721 + return ret;
  722 + }
  723 +#endif
631 724
632 return ret; 725 return ret;
633 } 726 }
@@ -116,8 +116,14 @@ private: @@ -116,8 +116,14 @@ private:
116 public: 116 public:
117 virtual int on_reload_listen(); 117 virtual int on_reload_listen();
118 virtual int on_reload_pid(); 118 virtual int on_reload_pid();
  119 + virtual int on_reload_vhost_added(std::string vhost);
  120 + virtual int on_reload_vhost_removed(std::string vhost);
  121 + virtual int on_reload_vhost_http_updated();
119 virtual int on_reload_http_api_enabled(); 122 virtual int on_reload_http_api_enabled();
120 virtual int on_reload_http_api_disabled(); 123 virtual int on_reload_http_api_disabled();
  124 + virtual int on_reload_http_stream_enabled();
  125 + virtual int on_reload_http_stream_disabled();
  126 + virtual int on_reload_http_stream_updated();
121 }; 127 };
122 128
123 #endif 129 #endif