胡斌

add conf directive forward_append_stream_name,to support add ?vhost=xxxx to stre…

…am name when forward to cdn
1 -# the config for srs to forward 1 +# the cwrd_stream_name_appendnfig for srs to forward
2 # @see https://github.com/ossrs/srs/wiki/v1_CN_SampleForward 2 # @see https://github.com/ossrs/srs/wiki/v1_CN_SampleForward
3 # @see full.conf for detail config. 3 # @see full.conf for detail config.
4 4
@@ -16,4 +16,6 @@ vhost __defaultVhost__ { @@ -16,4 +16,6 @@ vhost __defaultVhost__ {
16 #if the forward server is same as this server , a origin,try use forward_peer, 16 #if the forward server is same as this server , a origin,try use forward_peer,
17 #the stream pushed from other forward peer will not forward any more in this server 17 #the stream pushed from other forward peer will not forward any more in this server
18 forward_peer 127.0.0.1:1936; 18 forward_peer 127.0.0.1:1936;
  19 +#append the string after start with ? to the stream name when forward to the server
  20 + forward_append_stream_name 127.0.0.1:19350?vhost=newrtmp.xuedianyun.com
19 } 21 }
@@ -1832,7 +1832,8 @@ int SrsConfig::check_config() @@ -1832,7 +1832,8 @@ int SrsConfig::check_config()
1832 && n != "dvr" && n != "ingest" && n != "hls" && n != "http_hooks" 1832 && n != "dvr" && n != "ingest" && n != "hls" && n != "http_hooks"
1833 && n != "gop_cache" && n != "queue_length" 1833 && n != "gop_cache" && n != "queue_length"
1834 && n != "refer" && n != "refer_publish" && n != "refer_play" 1834 && n != "refer" && n != "refer_publish" && n != "refer_play"
1835 - && n != "forward" && n != "forward_server_other" && n != "forward_in_turn" && n != "forward_peer" && n != "transcode" && n != "bandcheck" 1835 + && n != "forward" && n != "transcode" && n != "bandcheck"
  1836 + && n != "forward_server_other" && n != "forward_in_turn" && n != "forward_peer" && n != "forward_append_stream_name"
1836 && n != "time_jitter" && n != "mix_correct" 1837 && n != "time_jitter" && n != "mix_correct"
1837 && n != "atc" && n != "atc_auto" 1838 && n != "atc" && n != "atc_auto"
1838 && n != "debug_srs_upnode" 1839 && n != "debug_srs_upnode"
@@ -2816,6 +2817,17 @@ SrsConfDirective* SrsConfig::get_forward_in_turn(string vhost) @@ -2816,6 +2817,17 @@ SrsConfDirective* SrsConfig::get_forward_in_turn(string vhost)
2816 return conf->get("forward_in_turn"); 2817 return conf->get("forward_in_turn");
2817 } 2818 }
2818 2819
  2820 +SrsConfDirective* SrsConfig::get_forward_append_stream_name(string vhost)
  2821 +{
  2822 + SrsConfDirective* conf = get_vhost(vhost);
  2823 +
  2824 + if (!conf) {
  2825 + return NULL;
  2826 + }
  2827 +
  2828 + return conf->get("forward_append_stream_name");
  2829 +}
  2830 +
2819 SrsConfDirective* SrsConfig::get_forward_peer(string vhost) 2831 SrsConfDirective* SrsConfig::get_forward_peer(string vhost)
2820 { 2832 {
2821 SrsConfDirective* conf = get_vhost(vhost); 2833 SrsConfDirective* conf = get_vhost(vhost);
@@ -581,6 +581,10 @@ public: @@ -581,6 +581,10 @@ public:
581 * get the forward_peer directive of vhost. 581 * get the forward_peer directive of vhost.
582 */ 582 */
583 virtual SrsConfDirective* get_forward_peer(std::string vhost); 583 virtual SrsConfDirective* get_forward_peer(std::string vhost);
  584 + /**
  585 + * get the forward_append_stream_name directive of vhost.
  586 + */
  587 + virtual SrsConfDirective* get_forward_append_stream_name(std::string vhost);
584 // http_hooks section 588 // http_hooks section
585 private: 589 private:
586 /** 590 /**
@@ -138,6 +138,16 @@ int SrsForwarder::initialize(SrsRequest* req, string ep_forward) @@ -138,6 +138,16 @@ int SrsForwarder::initialize(SrsRequest* req, string ep_forward)
138 } 138 }
139 } 139 }
140 140
  141 + size_t ep_len = ep_forward.length();
  142 + conf = _srs_config->get_forward_append_stream_name(_req->vhost);
  143 + for (int i = 0; conf && i < (int)conf->args.size(); i++) {
  144 + std::string forward_append_stream_name = conf->args.at(i);
  145 + if(forward_append_stream_name.length() > ep_len && forward_append_stream_name.at(ep_len)=='?' && !strncmp(ep_forward.c_str(),forward_append_stream_name.c_str(),ep_len)){
  146 + _steam_name_appender = forward_append_stream_name.substr(ep_len, forward_append_stream_name.length());
  147 + break;
  148 + }
  149 + }
  150 +
141 return ret; 151 return ret;
142 } 152 }
143 153
@@ -300,7 +310,7 @@ int SrsForwarder::cycle() @@ -300,7 +310,7 @@ int SrsForwarder::cycle()
300 return ret; 310 return ret;
301 } 311 }
302 312
303 - if ((ret = client->publish(_req->stream, stream_id)) != ERROR_SUCCESS) { 313 + if ((ret = client->publish(_req->stream + _steam_name_appender, stream_id)) != ERROR_SUCCESS) {
304 srs_error("connect with server failed, stream_name=%s, stream_id=%d. ret=%d", 314 srs_error("connect with server failed, stream_name=%s, stream_id=%d. ret=%d",
305 _req->stream.c_str(), stream_id, ret); 315 _req->stream.c_str(), stream_id, ret);
306 return ret; 316 return ret;
@@ -55,6 +55,8 @@ private: @@ -55,6 +55,8 @@ private:
55 std::string _ep_forward; 55 std::string _ep_forward;
56 SrsRequest* _req; 56 SrsRequest* _req;
57 int stream_id; 57 int stream_id;
  58 + // the string append to stream name ,such as "?vhost=newrtmp.xuedianyun.com" for rtmp://video-center.alivecdn.com/live/test?vhost=newrtmp.xuedianyun.com
  59 + std::string _steam_name_appender;
58 private: 60 private:
59 st_netfd_t stfd; 61 st_netfd_t stfd;
60 SrsReusableThread2* pthread; 62 SrsReusableThread2* pthread;