winlin

refine the forwarder

@@ -32,34 +32,34 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -32,34 +32,34 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 SrsForwarder::SrsForwarder() 32 SrsForwarder::SrsForwarder()
33 { 33 {
34 client = new SrsRtmpClient(); 34 client = new SrsRtmpClient();
35 - port = 1935;  
36 tid = NULL; 35 tid = NULL;
37 loop = false; 36 loop = false;
38 } 37 }
39 38
40 SrsForwarder::~SrsForwarder() 39 SrsForwarder::~SrsForwarder()
41 { 40 {
42 - srs_freep(client);  
43 -  
44 if (tid) { 41 if (tid) {
45 loop = false; 42 loop = false;
46 st_thread_interrupt(tid); 43 st_thread_interrupt(tid);
47 st_thread_join(tid, NULL); 44 st_thread_join(tid, NULL);
48 tid = NULL; 45 tid = NULL;
49 } 46 }
  47 +
  48 + srs_freep(client);
50 } 49 }
51 50
52 int SrsForwarder::on_publish(std::string vhost, std::string app, std::string stream, std::string forward_server) 51 int SrsForwarder::on_publish(std::string vhost, std::string app, std::string stream, std::string forward_server)
53 { 52 {
54 int ret = ERROR_SUCCESS; 53 int ret = ERROR_SUCCESS;
55 54
56 - tc_url = "rtmp://"; 55 + std::string tc_url = "rtmp://";
57 tc_url += vhost; 56 tc_url += vhost;
58 tc_url += "/"; 57 tc_url += "/";
59 tc_url += app; 58 tc_url += app;
60 59
61 - stream_name = stream;  
62 - server = forward_server; 60 + std::string stream_name = stream;
  61 + std::string server = forward_server;
  62 + int port = 1935;
63 63
64 size_t pos = forward_server.find(":"); 64 size_t pos = forward_server.find(":");
65 if (pos != std::string::npos) { 65 if (pos != std::string::npos) {
@@ -31,6 +31,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,6 +31,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 31
32 #include <string> 32 #include <string>
33 33
  34 +#include <st.h>
  35 +
34 class SrsSharedPtrMessage; 36 class SrsSharedPtrMessage;
35 class SrsOnMetaDataPacket; 37 class SrsOnMetaDataPacket;
36 class SrsRtmpClient; 38 class SrsRtmpClient;
@@ -45,9 +47,11 @@ private: @@ -45,9 +47,11 @@ private:
45 std::string stream_name; 47 std::string stream_name;
46 std::string server; 48 std::string server;
47 int port; 49 int port;
48 - SrsRtmpClient* client; 50 +private:
49 st_thread_t tid; 51 st_thread_t tid;
50 bool loop; 52 bool loop;
  53 +private:
  54 + SrsRtmpClient* client;
51 public: 55 public:
52 SrsForwarder(); 56 SrsForwarder();
53 virtual ~SrsForwarder(); 57 virtual ~SrsForwarder();
@@ -23,6 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,6 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_core_rtmp.hpp> 24 #include <srs_core_rtmp.hpp>
25 25
  26 +#include <sys/socket.h>
  27 +#include <netinet/in.h>
  28 +#include <arpa/inet.h>
  29 +#include <netdb.h>
  30 +
26 #include <srs_core_log.hpp> 31 #include <srs_core_log.hpp>
27 #include <srs_core_error.hpp> 32 #include <srs_core_error.hpp>
28 #include <srs_core_socket.hpp> 33 #include <srs_core_socket.hpp>
@@ -181,6 +186,34 @@ SrsRtmpClient::~SrsRtmpClient() @@ -181,6 +186,34 @@ SrsRtmpClient::~SrsRtmpClient()
181 } 186 }
182 } 187 }
183 188
  189 +int SrsRtmpClient::connect_to(std::string server, int port)
  190 +{
  191 + int ret = ERROR_SUCCESS;
  192 + return ret;
  193 +}
  194 +
  195 +std::string SrsRtmpClient::parse_server(std::string host){
  196 + if(inet_addr(host.c_str()) != INADDR_NONE){
  197 + return host;
  198 + }
  199 +
  200 + hostent* answer = gethostbyname(host.c_str());
  201 + if(answer == NULL){
  202 + srs_error("dns resolve host %s error.", host.c_str());
  203 + return "";
  204 + }
  205 +
  206 + char ipv4[16];
  207 + memset(ipv4, 0, sizeof(ipv4));
  208 + for(int i = 0; i < answer->h_length; i++){
  209 + inet_ntop(AF_INET, answer->h_addr_list[i], ipv4, sizeof(ipv4));
  210 + srs_info("dns resolve host %s to %s.", host.c_str(), ipv4);
  211 + break;
  212 + }
  213 +
  214 + return ipv4;
  215 +}
  216 +
184 SrsRtmp::SrsRtmp(st_netfd_t client_stfd) 217 SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
185 { 218 {
186 protocol = new SrsProtocol(client_stfd); 219 protocol = new SrsProtocol(client_stfd);
@@ -40,6 +40,8 @@ class SrsCommonMessage; @@ -40,6 +40,8 @@ class SrsCommonMessage;
40 class SrsCreateStreamPacket; 40 class SrsCreateStreamPacket;
41 class SrsFMLEStartPacket; 41 class SrsFMLEStartPacket;
42 class SrsPublishPacket; 42 class SrsPublishPacket;
  43 +class SrsSharedPtrMessage;
  44 +class SrsOnMetaDataPacket;
43 45
44 /** 46 /**
45 * the original request from client. 47 * the original request from client.
@@ -102,6 +104,9 @@ private: @@ -102,6 +104,9 @@ private:
102 public: 104 public:
103 SrsRtmpClient(); 105 SrsRtmpClient();
104 virtual ~SrsRtmpClient(); 106 virtual ~SrsRtmpClient();
  107 +private:
  108 + virtual int connect_to(std::string server, int port);
  109 + std::string parse_server(std::string host);
105 }; 110 };
106 111
107 /** 112 /**
@@ -618,7 +618,7 @@ int SrsSource::on_publish(std::string vhost, std::string app, std::string stream @@ -618,7 +618,7 @@ int SrsSource::on_publish(std::string vhost, std::string app, std::string stream
618 618
619 // create forwarders 619 // create forwarders
620 SrsConfDirective* conf = config->get_forward(vhost); 620 SrsConfDirective* conf = config->get_forward(vhost);
621 - for (int i = 0; conf && i < conf->args.size(); i++) { 621 + for (int i = 0; conf && i < (int)conf->args.size(); i++) {
622 std::string forward_server = conf->args.at(i); 622 std::string forward_server = conf->args.at(i);
623 623
624 SrsForwarder* forwarder = new SrsForwarder(); 624 SrsForwarder* forwarder = new SrsForwarder();