mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
implements the http api/stream framework
This commit is contained in:
parent
ab3c6c92a0
commit
6913efe127
9 changed files with 107 additions and 47 deletions
|
@ -23,12 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_conn.hpp>
|
#include <srs_app_conn.hpp>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
#include <srs_kernel_error.hpp>
|
#include <srs_kernel_error.hpp>
|
||||||
#include <srs_app_server.hpp>
|
#include <srs_app_server.hpp>
|
||||||
|
|
||||||
SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
|
SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
{
|
{
|
||||||
|
ip = NULL;
|
||||||
server = srs_server;
|
server = srs_server;
|
||||||
stfd = client_stfd;
|
stfd = client_stfd;
|
||||||
connection_id = 0;
|
connection_id = 0;
|
||||||
|
@ -36,6 +39,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
|
|
||||||
SrsConnection::~SrsConnection()
|
SrsConnection::~SrsConnection()
|
||||||
{
|
{
|
||||||
|
srs_freepa(ip);
|
||||||
srs_close_stfd(stfd);
|
srs_close_stfd(stfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +57,41 @@ int SrsConnection::start()
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsConnection::get_peer_ip()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
int fd = st_netfd_fileno(stfd);
|
||||||
|
|
||||||
|
// discovery client information
|
||||||
|
sockaddr_in addr;
|
||||||
|
socklen_t addrlen = sizeof(addr);
|
||||||
|
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
|
||||||
|
ret = ERROR_SOCKET_GET_PEER_NAME;
|
||||||
|
srs_error("discovery client information failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
srs_verbose("get peer name success.");
|
||||||
|
|
||||||
|
// ip v4 or v6
|
||||||
|
char buf[INET6_ADDRSTRLEN];
|
||||||
|
memset(buf, 0, sizeof(buf));
|
||||||
|
|
||||||
|
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
|
||||||
|
ret = ERROR_SOCKET_GET_PEER_IP;
|
||||||
|
srs_error("convert client information failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
|
||||||
|
|
||||||
|
ip = new char[strlen(buf) + 1];
|
||||||
|
strcpy(ip, buf);
|
||||||
|
|
||||||
|
srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
void SrsConnection::cycle()
|
void SrsConnection::cycle()
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
|
@ -36,6 +36,7 @@ class SrsServer;
|
||||||
class SrsConnection
|
class SrsConnection
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
char* ip;
|
||||||
SrsServer* server;
|
SrsServer* server;
|
||||||
st_netfd_t stfd;
|
st_netfd_t stfd;
|
||||||
int connection_id;
|
int connection_id;
|
||||||
|
@ -46,6 +47,8 @@ public:
|
||||||
virtual int start();
|
virtual int start();
|
||||||
protected:
|
protected:
|
||||||
virtual int do_cycle() = 0;
|
virtual int do_cycle() = 0;
|
||||||
|
protected:
|
||||||
|
virtual int get_peer_ip();
|
||||||
private:
|
private:
|
||||||
virtual void cycle();
|
virtual void cycle();
|
||||||
static void* cycle_thread(void* arg);
|
static void* cycle_thread(void* arg);
|
||||||
|
|
|
@ -23,8 +23,27 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_http_api.hpp>
|
#include <srs_app_http_api.hpp>
|
||||||
|
|
||||||
SrsHttpApi::SrsHttpApi() {
|
#include <srs_kernel_log.hpp>
|
||||||
|
#include <srs_kernel_error.hpp>
|
||||||
|
|
||||||
|
SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
|
: SrsConnection(srs_server, client_stfd)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsHttpApi::~SrsHttpApi() {
|
SrsHttpApi::~SrsHttpApi()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsHttpApi::do_cycle()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
||||||
|
srs_error("get peer ip failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
srs_trace("api get peer ip success. ip=%s", ip);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
class SrsHttpApi
|
#include <srs_app_st.hpp>
|
||||||
|
#include <srs_app_conn.hpp>
|
||||||
|
|
||||||
|
class SrsHttpApi : public SrsConnection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SrsHttpApi();
|
SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd);
|
||||||
virtual ~SrsHttpApi();
|
virtual ~SrsHttpApi();
|
||||||
|
protected:
|
||||||
|
virtual int do_cycle();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -22,3 +22,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <srs_app_http_conn.hpp>
|
#include <srs_app_http_conn.hpp>
|
||||||
|
|
||||||
|
#include <srs_kernel_log.hpp>
|
||||||
|
#include <srs_kernel_error.hpp>
|
||||||
|
|
||||||
|
SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
|
: SrsConnection(srs_server, client_stfd)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsHttpConn::~SrsHttpConn()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsHttpConn::do_cycle()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
||||||
|
srs_error("get peer ip failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
srs_trace("http get peer ip success. ip=%s", ip);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
|
@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
class SrsHttpConn
|
#include <srs_app_st.hpp>
|
||||||
|
#include <srs_app_conn.hpp>
|
||||||
|
|
||||||
|
class SrsHttpConn : public SrsConnection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SrsHttpConn();
|
SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd);
|
||||||
virtual ~SrsHttpConn();
|
virtual ~SrsHttpConn();
|
||||||
|
protected:
|
||||||
|
virtual int do_cycle();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -23,7 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_rtmp_conn.hpp>
|
#include <srs_app_rtmp_conn.hpp>
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -46,7 +45,6 @@ using namespace std;
|
||||||
SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd)
|
SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
: SrsConnection(srs_server, client_stfd)
|
: SrsConnection(srs_server, client_stfd)
|
||||||
{
|
{
|
||||||
ip = NULL;
|
|
||||||
req = new SrsRequest();
|
req = new SrsRequest();
|
||||||
res = new SrsResponse();
|
res = new SrsResponse();
|
||||||
skt = new SrsSocket(client_stfd);
|
skt = new SrsSocket(client_stfd);
|
||||||
|
@ -64,7 +62,6 @@ SrsRtmpConn::~SrsRtmpConn()
|
||||||
{
|
{
|
||||||
_srs_config->unsubscribe(this);
|
_srs_config->unsubscribe(this);
|
||||||
|
|
||||||
srs_freepa(ip);
|
|
||||||
srs_freep(req);
|
srs_freep(req);
|
||||||
srs_freep(res);
|
srs_freep(res);
|
||||||
srs_freep(rtmp);
|
srs_freep(rtmp);
|
||||||
|
@ -85,7 +82,7 @@ int SrsRtmpConn::do_cycle()
|
||||||
srs_error("get peer ip failed. ret=%d", ret);
|
srs_error("get peer ip failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"",
|
srs_trace("rtmp get peer ip success. ip=%s, send_to=%"PRId64"us, recv_to=%"PRId64"us",
|
||||||
ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);
|
ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US);
|
||||||
|
|
||||||
rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
|
rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
|
||||||
|
@ -639,41 +636,6 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* ms
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsRtmpConn::get_peer_ip()
|
|
||||||
{
|
|
||||||
int ret = ERROR_SUCCESS;
|
|
||||||
|
|
||||||
int fd = st_netfd_fileno(stfd);
|
|
||||||
|
|
||||||
// discovery client information
|
|
||||||
sockaddr_in addr;
|
|
||||||
socklen_t addrlen = sizeof(addr);
|
|
||||||
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
|
|
||||||
ret = ERROR_SOCKET_GET_PEER_NAME;
|
|
||||||
srs_error("discovery client information failed. ret=%d", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
srs_verbose("get peer name success.");
|
|
||||||
|
|
||||||
// ip v4 or v6
|
|
||||||
char buf[INET6_ADDRSTRLEN];
|
|
||||||
memset(buf, 0, sizeof(buf));
|
|
||||||
|
|
||||||
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
|
|
||||||
ret = ERROR_SOCKET_GET_PEER_IP;
|
|
||||||
srs_error("convert client information failed. ret=%d", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
|
|
||||||
|
|
||||||
ip = new char[strlen(buf) + 1];
|
|
||||||
strcpy(ip, buf);
|
|
||||||
|
|
||||||
srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg)
|
int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
|
@ -53,7 +53,6 @@ class SrsBandwidth;
|
||||||
class SrsRtmpConn : public SrsConnection, public ISrsReloadHandler
|
class SrsRtmpConn : public SrsConnection, public ISrsReloadHandler
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
char* ip;
|
|
||||||
SrsRequest* req;
|
SrsRequest* req;
|
||||||
SrsResponse* res;
|
SrsResponse* res;
|
||||||
SrsSocket* skt;
|
SrsSocket* skt;
|
||||||
|
@ -81,7 +80,6 @@ private:
|
||||||
virtual int fmle_publish(SrsSource* source);
|
virtual int fmle_publish(SrsSource* source);
|
||||||
virtual int flash_publish(SrsSource* source);
|
virtual int flash_publish(SrsSource* source);
|
||||||
virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg);
|
virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg);
|
||||||
virtual int get_peer_ip();
|
|
||||||
virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
|
virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);
|
||||||
private:
|
private:
|
||||||
virtual int on_connect();
|
virtual int on_connect();
|
||||||
|
|
|
@ -38,6 +38,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <srs_app_rtmp_conn.hpp>
|
#include <srs_app_rtmp_conn.hpp>
|
||||||
#include <srs_app_config.hpp>
|
#include <srs_app_config.hpp>
|
||||||
#include <srs_kernel_utility.hpp>
|
#include <srs_kernel_utility.hpp>
|
||||||
|
#include <srs_app_http_api.hpp>
|
||||||
|
#include <srs_app_http_conn.hpp>
|
||||||
|
|
||||||
#define SERVER_LISTEN_BACKLOG 512
|
#define SERVER_LISTEN_BACKLOG 512
|
||||||
#define SRS_TIME_RESOLUTION_MS 500
|
#define SRS_TIME_RESOLUTION_MS 500
|
||||||
|
@ -436,7 +438,9 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
|
||||||
if (type == SrsListenerRtmpStream) {
|
if (type == SrsListenerRtmpStream) {
|
||||||
conn = new SrsRtmpConn(this, client_stfd);
|
conn = new SrsRtmpConn(this, client_stfd);
|
||||||
} else if (type == SrsListenerHttpApi) {
|
} else if (type == SrsListenerHttpApi) {
|
||||||
|
conn = new SrsHttpApi(this, client_stfd);
|
||||||
} else if (type == SrsListenerHttpStream) {
|
} else if (type == SrsListenerHttpStream) {
|
||||||
|
conn = new SrsHttpConn(this, client_stfd);
|
||||||
} else {
|
} else {
|
||||||
// TODO: FIXME: handler others
|
// TODO: FIXME: handler others
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue