mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
SRT: Refine code, remove SrsSrtListenerType
This commit is contained in:
parent
9efb6de0b4
commit
077d93c7b6
2 changed files with 34 additions and 92 deletions
|
@ -18,44 +18,19 @@ using namespace std;
|
||||||
SrsSrtEventLoop* _srt_eventloop = NULL;
|
SrsSrtEventLoop* _srt_eventloop = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string srs_srt_listener_type2string(SrsSrtListenerType type)
|
SrsSrtAcceptor::SrsSrtAcceptor(SrsSrtServer* srt_server)
|
||||||
{
|
|
||||||
switch (type) {
|
|
||||||
case SrsSrtListenerMpegts:
|
|
||||||
return "SRT-MPEGTS";
|
|
||||||
default:
|
|
||||||
return "UNKONWN";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SrsSrtAcceptor::SrsSrtAcceptor(SrsSrtServer* srt_server, SrsSrtListenerType t)
|
|
||||||
{
|
{
|
||||||
port_ = 0;
|
port_ = 0;
|
||||||
srt_server_ = srt_server;
|
srt_server_ = srt_server;
|
||||||
type_ = t;
|
listener_ = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsSrtAcceptor::~SrsSrtAcceptor()
|
SrsSrtAcceptor::~SrsSrtAcceptor()
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
SrsSrtListenerType SrsSrtAcceptor::listen_type()
|
|
||||||
{
|
|
||||||
return type_;
|
|
||||||
}
|
|
||||||
|
|
||||||
SrsSrtMessageAcceptor::SrsSrtMessageAcceptor(SrsSrtServer* srt_server, SrsSrtListenerType listen_type)
|
|
||||||
: SrsSrtAcceptor(srt_server, listen_type)
|
|
||||||
{
|
|
||||||
listener_ = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
SrsSrtMessageAcceptor::~SrsSrtMessageAcceptor()
|
|
||||||
{
|
{
|
||||||
srs_freep(listener_);
|
srs_freep(listener_);
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_error_t SrsSrtMessageAcceptor::listen(std::string ip, int port)
|
srs_error_t SrsSrtAcceptor::listen(std::string ip, int port)
|
||||||
{
|
{
|
||||||
srs_error_t err = srs_success;
|
srs_error_t err = srs_success;
|
||||||
|
|
||||||
|
@ -80,13 +55,12 @@ srs_error_t SrsSrtMessageAcceptor::listen(std::string ip, int port)
|
||||||
return srs_error_wrap(err, "message srt acceptor");
|
return srs_error_wrap(err, "message srt acceptor");
|
||||||
}
|
}
|
||||||
|
|
||||||
string v = srs_srt_listener_type2string(type_);
|
srs_trace("srt listen at udp://%s:%d, fd=%d", ip_.c_str(), port_, listener_->fd());
|
||||||
srs_trace("%s listen at srt://%s:%d, fd=%d", v.c_str(), ip_.c_str(), port_, listener_->fd());
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_error_t SrsSrtMessageAcceptor::set_srt_opt()
|
srs_error_t SrsSrtAcceptor::set_srt_opt()
|
||||||
{
|
{
|
||||||
srs_error_t err = srs_success;
|
srs_error_t err = srs_success;
|
||||||
|
|
||||||
|
@ -141,16 +115,17 @@ srs_error_t SrsSrtMessageAcceptor::set_srt_opt()
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_error_t SrsSrtMessageAcceptor::on_srt_client(srs_srt_t srt_fd)
|
srs_error_t SrsSrtAcceptor::on_srt_client(srs_srt_t srt_fd)
|
||||||
{
|
{
|
||||||
|
srs_error_t err = srs_success;
|
||||||
|
|
||||||
// Notify srt server to accept srt client, and create new SrsSrtConn on it.
|
// Notify srt server to accept srt client, and create new SrsSrtConn on it.
|
||||||
srs_error_t err = srt_server_->accept_srt_client(type_, srt_fd);
|
if ((err = srt_server_->accept_srt_client(srt_fd)) != srs_success) {
|
||||||
if (err != srs_success) {
|
|
||||||
srs_warn("accept srt client failed, err is %s", srs_error_desc(err).c_str());
|
srs_warn("accept srt client failed, err is %s", srs_error_desc(err).c_str());
|
||||||
srs_freep(err);
|
srs_freep(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return srs_success;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsSrtServer::SrsSrtServer()
|
SrsSrtServer::SrsSrtServer()
|
||||||
|
@ -194,10 +169,10 @@ srs_error_t SrsSrtServer::listen_srt_mpegts()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close all listener for SRT if exists.
|
// Close all listener for SRT if exists.
|
||||||
close_listeners(SrsSrtListenerMpegts);
|
close_listeners();
|
||||||
|
|
||||||
// Start a listener for SRT, we might need multiple listeners in the future.
|
// Start a listener for SRT, we might need multiple listeners in the future.
|
||||||
SrsSrtAcceptor* acceptor = new SrsSrtMessageAcceptor(this, SrsSrtListenerMpegts);
|
SrsSrtAcceptor* acceptor = new SrsSrtAcceptor(this);
|
||||||
acceptors_.push_back(acceptor);
|
acceptors_.push_back(acceptor);
|
||||||
|
|
||||||
int port; string ip;
|
int port; string ip;
|
||||||
|
@ -210,29 +185,23 @@ srs_error_t SrsSrtServer::listen_srt_mpegts()
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SrsSrtServer::close_listeners(SrsSrtListenerType type)
|
void SrsSrtServer::close_listeners()
|
||||||
{
|
{
|
||||||
std::vector<SrsSrtAcceptor*>::iterator it;
|
std::vector<SrsSrtAcceptor*>::iterator it;
|
||||||
for (it = acceptors_.begin(); it != acceptors_.end();) {
|
for (it = acceptors_.begin(); it != acceptors_.end();) {
|
||||||
SrsSrtAcceptor* acceptor = *it;
|
SrsSrtAcceptor* acceptor = *it;
|
||||||
|
|
||||||
if (acceptor->listen_type() != type) {
|
|
||||||
++it;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
srs_freep(acceptor);
|
srs_freep(acceptor);
|
||||||
|
|
||||||
it = acceptors_.erase(it);
|
it = acceptors_.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_error_t SrsSrtServer::accept_srt_client(SrsSrtListenerType type, srs_srt_t srt_fd)
|
srs_error_t SrsSrtServer::accept_srt_client(srs_srt_t srt_fd)
|
||||||
{
|
{
|
||||||
srs_error_t err = srs_success;
|
srs_error_t err = srs_success;
|
||||||
|
|
||||||
ISrsStartableConneciton* conn = NULL;
|
ISrsStartableConneciton* conn = NULL;
|
||||||
|
if ((err = fd_to_resource(srt_fd, &conn)) != srs_success) {
|
||||||
if ((err = fd_to_resource(type, srt_fd, &conn)) != srs_success) {
|
|
||||||
//close fd on conn error, otherwise will lead to fd leak -gs
|
//close fd on conn error, otherwise will lead to fd leak -gs
|
||||||
// TODO: FIXME: Handle error.
|
// TODO: FIXME: Handle error.
|
||||||
srs_srt_close(srt_fd);
|
srs_srt_close(srt_fd);
|
||||||
|
@ -250,13 +219,12 @@ srs_error_t SrsSrtServer::accept_srt_client(SrsSrtListenerType type, srs_srt_t s
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_error_t SrsSrtServer::fd_to_resource(SrsSrtListenerType type, srs_srt_t srt_fd, ISrsStartableConneciton** pr)
|
srs_error_t SrsSrtServer::fd_to_resource(srs_srt_t srt_fd, ISrsStartableConneciton** pr)
|
||||||
{
|
{
|
||||||
srs_error_t err = srs_success;
|
srs_error_t err = srs_success;
|
||||||
|
|
||||||
string ip = "";
|
string ip = "";
|
||||||
int port = 0;
|
int port = 0;
|
||||||
|
|
||||||
if ((err = srs_srt_get_remote_ip_port(srt_fd, ip, port)) != srs_success) {
|
if ((err = srs_srt_get_remote_ip_port(srt_fd, ip, port)) != srs_success) {
|
||||||
return srs_error_wrap(err, "get srt ip port");
|
return srs_error_wrap(err, "get srt ip port");
|
||||||
}
|
}
|
||||||
|
@ -268,14 +236,8 @@ srs_error_t SrsSrtServer::fd_to_resource(SrsSrtListenerType type, srs_srt_t srt_
|
||||||
// The context id may change during creating the bellow objects.
|
// The context id may change during creating the bellow objects.
|
||||||
SrsContextRestore(_srs_context->get_id());
|
SrsContextRestore(_srs_context->get_id());
|
||||||
|
|
||||||
if (type == SrsSrtListenerMpegts) {
|
// Covert to SRT conection.
|
||||||
*pr = new SrsMpegtsSrtConn(this, srt_fd, ip, port);
|
*pr = new SrsMpegtsSrtConn(this, srt_fd, ip, port);
|
||||||
} else {
|
|
||||||
srs_warn("close for no service handler. srtfd=%d, ip=%s:%d", srt_fd, ip.c_str(), port);
|
|
||||||
// TODO: FIXME: Handle error.
|
|
||||||
srs_srt_close(srt_fd);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,38 +15,21 @@
|
||||||
|
|
||||||
class SrsSrtServer;
|
class SrsSrtServer;
|
||||||
|
|
||||||
enum SrsSrtListenerType
|
|
||||||
{
|
|
||||||
SrsSrtListenerMpegts = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
// A common srt acceptor, for SRT server.
|
// A common srt acceptor, for SRT server.
|
||||||
class SrsSrtAcceptor
|
class SrsSrtAcceptor : public ISrsSrtHandler
|
||||||
{
|
{
|
||||||
protected:
|
private:
|
||||||
SrsSrtListenerType type_;
|
|
||||||
protected:
|
|
||||||
std::string ip_;
|
std::string ip_;
|
||||||
int port_;
|
int port_;
|
||||||
SrsSrtServer* srt_server_;
|
SrsSrtServer* srt_server_;
|
||||||
public:
|
|
||||||
SrsSrtAcceptor(SrsSrtServer* srt_server, SrsSrtListenerType listen_type);
|
|
||||||
virtual ~SrsSrtAcceptor();
|
|
||||||
public:
|
|
||||||
virtual SrsSrtListenerType listen_type();
|
|
||||||
virtual srs_error_t listen(std::string ip, int port) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// A srt messge acceptor.
|
|
||||||
class SrsSrtMessageAcceptor : public SrsSrtAcceptor, public ISrsSrtHandler
|
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
SrsSrtListener* listener_;
|
SrsSrtListener* listener_;
|
||||||
public:
|
public:
|
||||||
SrsSrtMessageAcceptor(SrsSrtServer* srt_server, SrsSrtListenerType listen_type);
|
SrsSrtAcceptor(SrsSrtServer* srt_server);
|
||||||
virtual ~SrsSrtMessageAcceptor();
|
virtual ~SrsSrtAcceptor();
|
||||||
public:
|
public:
|
||||||
virtual srs_error_t listen(std::string i, int p);
|
virtual srs_error_t listen(std::string ip, int port);
|
||||||
|
private:
|
||||||
virtual srs_error_t set_srt_opt();
|
virtual srs_error_t set_srt_opt();
|
||||||
// Interface ISrsSrtHandler
|
// Interface ISrsSrtHandler
|
||||||
public:
|
public:
|
||||||
|
@ -69,18 +52,15 @@ public:
|
||||||
private:
|
private:
|
||||||
// listen at specified srt protocol.
|
// listen at specified srt protocol.
|
||||||
virtual srs_error_t listen_srt_mpegts();
|
virtual srs_error_t listen_srt_mpegts();
|
||||||
// Close the listeners for specified type,
|
// Close the listeners and remove the listen object from manager.
|
||||||
// Remove the listen object from manager.
|
virtual void close_listeners();
|
||||||
virtual void close_listeners(SrsSrtListenerType type);
|
|
||||||
// For internal only
|
// For internal only
|
||||||
public:
|
public:
|
||||||
// When listener got a fd, notice server to accept it.
|
// When listener got a fd, notice server to accept it.
|
||||||
// @param type, the client type, used to create concrete connection,
|
|
||||||
// for instance SRT connection to serve client.
|
|
||||||
// @param srt_fd, the client fd in srt boxed, the underlayer fd.
|
// @param srt_fd, the client fd in srt boxed, the underlayer fd.
|
||||||
virtual srs_error_t accept_srt_client(SrsSrtListenerType type, srs_srt_t srt_fd);
|
virtual srs_error_t accept_srt_client(srs_srt_t srt_fd);
|
||||||
private:
|
private:
|
||||||
virtual srs_error_t fd_to_resource(SrsSrtListenerType type, srs_srt_t srt_fd, ISrsStartableConneciton** pr);
|
virtual srs_error_t fd_to_resource(srs_srt_t srt_fd, ISrsStartableConneciton** pr);
|
||||||
// Interface ISrsResourceManager
|
// Interface ISrsResourceManager
|
||||||
public:
|
public:
|
||||||
// A callback for connection to remove itself.
|
// A callback for connection to remove itself.
|
||||||
|
|
Loading…
Reference in a new issue