winlin

implements the http api/stream framework

@@ -23,12 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,12 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_app_conn.hpp> 24 #include <srs_app_conn.hpp>
25 25
  26 +#include <arpa/inet.h>
  27 +
26 #include <srs_kernel_log.hpp> 28 #include <srs_kernel_log.hpp>
27 #include <srs_kernel_error.hpp> 29 #include <srs_kernel_error.hpp>
28 #include <srs_app_server.hpp> 30 #include <srs_app_server.hpp>
29 31
30 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) 32 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
31 { 33 {
  34 + ip = NULL;
32 server = srs_server; 35 server = srs_server;
33 stfd = client_stfd; 36 stfd = client_stfd;
34 connection_id = 0; 37 connection_id = 0;
@@ -36,6 +39,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) @@ -36,6 +39,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
36 39
37 SrsConnection::~SrsConnection() 40 SrsConnection::~SrsConnection()
38 { 41 {
  42 + srs_freepa(ip);
39 srs_close_stfd(stfd); 43 srs_close_stfd(stfd);
40 } 44 }
41 45
@@ -53,6 +57,41 @@ int SrsConnection::start() @@ -53,6 +57,41 @@ int SrsConnection::start()
53 return ret; 57 return ret;
54 } 58 }
55 59
  60 +int SrsConnection::get_peer_ip()
  61 +{
  62 + int ret = ERROR_SUCCESS;
  63 +
  64 + int fd = st_netfd_fileno(stfd);
  65 +
  66 + // discovery client information
  67 + sockaddr_in addr;
  68 + socklen_t addrlen = sizeof(addr);
  69 + if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
  70 + ret = ERROR_SOCKET_GET_PEER_NAME;
  71 + srs_error("discovery client information failed. ret=%d", ret);
  72 + return ret;
  73 + }
  74 + srs_verbose("get peer name success.");
  75 +
  76 + // ip v4 or v6
  77 + char buf[INET6_ADDRSTRLEN];
  78 + memset(buf, 0, sizeof(buf));
  79 +
  80 + if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
  81 + ret = ERROR_SOCKET_GET_PEER_IP;
  82 + srs_error("convert client information failed. ret=%d", ret);
  83 + return ret;
  84 + }
  85 + srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
  86 +
  87 + ip = new char[strlen(buf) + 1];
  88 + strcpy(ip, buf);
  89 +
  90 + srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
  91 +
  92 + return ret;
  93 +}
  94 +
56 void SrsConnection::cycle() 95 void SrsConnection::cycle()
57 { 96 {
58 int ret = ERROR_SUCCESS; 97 int ret = ERROR_SUCCESS;
@@ -36,6 +36,7 @@ class SrsServer; @@ -36,6 +36,7 @@ class SrsServer;
36 class SrsConnection 36 class SrsConnection
37 { 37 {
38 protected: 38 protected:
  39 + char* ip;
39 SrsServer* server; 40 SrsServer* server;
40 st_netfd_t stfd; 41 st_netfd_t stfd;
41 int connection_id; 42 int connection_id;
@@ -46,6 +47,8 @@ public: @@ -46,6 +47,8 @@ public:
46 virtual int start(); 47 virtual int start();
47 protected: 48 protected:
48 virtual int do_cycle() = 0; 49 virtual int do_cycle() = 0;
  50 +protected:
  51 + virtual int get_peer_ip();
49 private: 52 private:
50 virtual void cycle(); 53 virtual void cycle();
51 static void* cycle_thread(void* arg); 54 static void* cycle_thread(void* arg);
@@ -23,8 +23,27 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,8 +23,27 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_app_http_api.hpp> 24 #include <srs_app_http_api.hpp>
25 25
26 -SrsHttpApi::SrsHttpApi() { 26 +#include <srs_kernel_log.hpp>
  27 +#include <srs_kernel_error.hpp>
  28 +
  29 +SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd)
  30 + : SrsConnection(srs_server, client_stfd)
  31 +{
  32 +}
  33 +
  34 +SrsHttpApi::~SrsHttpApi()
  35 +{
27 } 36 }
28 37
29 -SrsHttpApi::~SrsHttpApi() { 38 +int SrsHttpApi::do_cycle()
  39 +{
  40 + int ret = ERROR_SUCCESS;
  41 +
  42 + if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
  43 + srs_error("get peer ip failed. ret=%d", ret);
  44 + return ret;
  45 + }
  46 + srs_trace("api get peer ip success. ip=%s", ip);
  47 +
  48 + return ret;
30 } 49 }
@@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 -class SrsHttpApi 33 +#include <srs_app_st.hpp>
  34 +#include <srs_app_conn.hpp>
  35 +
  36 +class SrsHttpApi : public SrsConnection
34 { 37 {
35 public: 38 public:
36 - SrsHttpApi(); 39 + SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd);
37 virtual ~SrsHttpApi(); 40 virtual ~SrsHttpApi();
  41 +protected:
  42 + virtual int do_cycle();
38 }; 43 };
39 44
40 #endif 45 #endif
@@ -22,3 +22,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -22,3 +22,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */ 22 */
23 23
24 #include <srs_app_http_conn.hpp> 24 #include <srs_app_http_conn.hpp>
  25 +
  26 +#include <srs_kernel_log.hpp>
  27 +#include <srs_kernel_error.hpp>
  28 +
  29 +SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd)
  30 + : SrsConnection(srs_server, client_stfd)
  31 +{
  32 +}
  33 +
  34 +SrsHttpConn::~SrsHttpConn()
  35 +{
  36 +}
  37 +
  38 +int SrsHttpConn::do_cycle()
  39 +{
  40 + int ret = ERROR_SUCCESS;
  41 +
  42 + if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
  43 + srs_error("get peer ip failed. ret=%d", ret);
  44 + return ret;
  45 + }
  46 + srs_trace("http get peer ip success. ip=%s", ip);
  47 +
  48 + return ret;
  49 +}
@@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 -class SrsHttpConn 33 +#include <srs_app_st.hpp>
  34 +#include <srs_app_conn.hpp>
  35 +
  36 +class SrsHttpConn : public SrsConnection
34 { 37 {
35 public: 38 public:
36 - SrsHttpConn(); 39 + SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd);
37 virtual ~SrsHttpConn(); 40 virtual ~SrsHttpConn();
  41 +protected:
  42 + virtual int do_cycle();
38 }; 43 };
39 44
40 #endif 45 #endif
@@ -23,7 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,7 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_app_rtmp_conn.hpp> 24 #include <srs_app_rtmp_conn.hpp>
25 25
26 -#include <arpa/inet.h>  
27 #include <stdlib.h> 26 #include <stdlib.h>
28 27
29 using namespace std; 28 using namespace std;
@@ -46,7 +45,6 @@ using namespace std; @@ -46,7 +45,6 @@ using namespace std;
46 SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd) 45 SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd)
47 : SrsConnection(srs_server, client_stfd) 46 : SrsConnection(srs_server, client_stfd)
48 { 47 {
49 - ip = NULL;  
50 req = new SrsRequest(); 48 req = new SrsRequest();
51 res = new SrsResponse(); 49 res = new SrsResponse();
52 skt = new SrsSocket(client_stfd); 50 skt = new SrsSocket(client_stfd);
@@ -64,7 +62,6 @@ SrsRtmpConn::~SrsRtmpConn() @@ -64,7 +62,6 @@ SrsRtmpConn::~SrsRtmpConn()
64 { 62 {
65 _srs_config->unsubscribe(this); 63 _srs_config->unsubscribe(this);
66 64
67 - srs_freepa(ip);  
68 srs_freep(req); 65 srs_freep(req);
69 srs_freep(res); 66 srs_freep(res);
70 srs_freep(rtmp); 67 srs_freep(rtmp);
@@ -85,7 +82,7 @@ int SrsRtmpConn::do_cycle() @@ -85,7 +82,7 @@ int SrsRtmpConn::do_cycle()
85 srs_error("get peer ip failed. ret=%d", ret); 82 srs_error("get peer ip failed. ret=%d", ret);
86 return ret; 83 return ret;
87 } 84 }
88 - srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"", 85 + srs_trace("rtmp get peer ip success. ip=%s, send_to=%"PRId64"us, recv_to=%"PRId64"us",
89 ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US); 86 ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);
90 87
91 rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US); 88 rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
@@ -639,41 +636,6 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* ms @@ -639,41 +636,6 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* ms
639 return ret; 636 return ret;
640 } 637 }
641 638
642 -int SrsRtmpConn::get_peer_ip()  
643 -{  
644 - int ret = ERROR_SUCCESS;  
645 -  
646 - int fd = st_netfd_fileno(stfd);  
647 -  
648 - // discovery client information  
649 - sockaddr_in addr;  
650 - socklen_t addrlen = sizeof(addr);  
651 - if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {  
652 - ret = ERROR_SOCKET_GET_PEER_NAME;  
653 - srs_error("discovery client information failed. ret=%d", ret);  
654 - return ret;  
655 - }  
656 - srs_verbose("get peer name success.");  
657 -  
658 - // ip v4 or v6  
659 - char buf[INET6_ADDRSTRLEN];  
660 - memset(buf, 0, sizeof(buf));  
661 -  
662 - if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {  
663 - ret = ERROR_SOCKET_GET_PEER_IP;  
664 - srs_error("convert client information failed. ret=%d", ret);  
665 - return ret;  
666 - }  
667 - srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);  
668 -  
669 - ip = new char[strlen(buf) + 1];  
670 - strcpy(ip, buf);  
671 -  
672 - srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);  
673 -  
674 - return ret;  
675 -}  
676 -  
677 int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg) 639 int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg)
678 { 640 {
679 int ret = ERROR_SUCCESS; 641 int ret = ERROR_SUCCESS;
@@ -53,7 +53,6 @@ class SrsBandwidth; @@ -53,7 +53,6 @@ class SrsBandwidth;
53 class SrsRtmpConn : public SrsConnection, public ISrsReloadHandler 53 class SrsRtmpConn : public SrsConnection, public ISrsReloadHandler
54 { 54 {
55 private: 55 private:
56 - char* ip;  
57 SrsRequest* req; 56 SrsRequest* req;
58 SrsResponse* res; 57 SrsResponse* res;
59 SrsSocket* skt; 58 SrsSocket* skt;
@@ -81,7 +80,6 @@ private: @@ -81,7 +80,6 @@ private:
81 virtual int fmle_publish(SrsSource* source); 80 virtual int fmle_publish(SrsSource* source);
82 virtual int flash_publish(SrsSource* source); 81 virtual int flash_publish(SrsSource* source);
83 virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg); 82 virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg);
84 - virtual int get_peer_ip();  
85 virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); 83 virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
86 private: 84 private:
87 virtual int on_connect(); 85 virtual int on_connect();
@@ -38,6 +38,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -38,6 +38,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include <srs_app_rtmp_conn.hpp> 38 #include <srs_app_rtmp_conn.hpp>
39 #include <srs_app_config.hpp> 39 #include <srs_app_config.hpp>
40 #include <srs_kernel_utility.hpp> 40 #include <srs_kernel_utility.hpp>
  41 +#include <srs_app_http_api.hpp>
  42 +#include <srs_app_http_conn.hpp>
41 43
42 #define SERVER_LISTEN_BACKLOG 512 44 #define SERVER_LISTEN_BACKLOG 512
43 #define SRS_TIME_RESOLUTION_MS 500 45 #define SRS_TIME_RESOLUTION_MS 500
@@ -436,7 +438,9 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) @@ -436,7 +438,9 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
436 if (type == SrsListenerRtmpStream) { 438 if (type == SrsListenerRtmpStream) {
437 conn = new SrsRtmpConn(this, client_stfd); 439 conn = new SrsRtmpConn(this, client_stfd);
438 } else if (type == SrsListenerHttpApi) { 440 } else if (type == SrsListenerHttpApi) {
  441 + conn = new SrsHttpApi(this, client_stfd);
439 } else if (type == SrsListenerHttpStream) { 442 } else if (type == SrsListenerHttpStream) {
  443 + conn = new SrsHttpConn(this, client_stfd);
440 } else { 444 } else {
441 // TODO: FIXME: handler others 445 // TODO: FIXME: handler others
442 } 446 }