2013-11-23 03:36:07 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-12-31 12:32:09 +00:00
|
|
|
Copyright (c) 2013-2015 winlin
|
2013-11-23 03:36:07 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#ifndef SRS_APP_SERVER_HPP
|
|
|
|
#define SRS_APP_SERVER_HPP
|
2013-11-23 03:36:07 +00:00
|
|
|
|
|
|
|
/*
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_server.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <vector>
|
2015-02-03 08:01:07 +00:00
|
|
|
#include <string>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_st.hpp>
|
|
|
|
#include <srs_app_reload.hpp>
|
2015-01-18 10:39:53 +00:00
|
|
|
#include <srs_app_source.hpp>
|
2015-02-03 08:01:07 +00:00
|
|
|
#include <srs_app_hls.hpp>
|
2015-02-17 08:28:28 +00:00
|
|
|
#include <srs_app_listener.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
|
|
|
class SrsServer;
|
|
|
|
class SrsConnection;
|
2015-03-04 05:09:35 +00:00
|
|
|
class SrsHttpServeMux;
|
2015-01-17 15:00:40 +00:00
|
|
|
class SrsHttpServer;
|
2014-04-07 01:27:30 +00:00
|
|
|
class SrsIngester;
|
2014-05-19 09:39:01 +00:00
|
|
|
class SrsHttpHeartbeat;
|
2014-06-19 07:28:05 +00:00
|
|
|
class SrsKbps;
|
2015-01-24 08:27:30 +00:00
|
|
|
class SrsConfDirective;
|
2015-02-17 13:10:06 +00:00
|
|
|
class ISrsTcpHandler;
|
2015-01-29 16:04:20 +00:00
|
|
|
class ISrsUdpHandler;
|
2015-02-17 08:28:28 +00:00
|
|
|
class SrsUdpListener;
|
|
|
|
class SrsTcpListener;
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-03-27 04:27:47 +00:00
|
|
|
// listener type for server to identify the connection,
|
|
|
|
// that is, use different type to process the connection.
|
2013-11-23 03:36:07 +00:00
|
|
|
enum SrsListenerType
|
|
|
|
{
|
2014-03-27 04:27:47 +00:00
|
|
|
// RTMP client,
|
2015-01-24 06:52:52 +00:00
|
|
|
SrsListenerRtmpStream = 0,
|
2014-03-27 04:27:47 +00:00
|
|
|
// HTTP api,
|
2015-01-24 06:52:52 +00:00
|
|
|
SrsListenerHttpApi = 1,
|
2014-03-27 04:27:47 +00:00
|
|
|
// HTTP stream, HDS/HLS/DASH
|
2015-01-24 06:52:52 +00:00
|
|
|
SrsListenerHttpStream = 2,
|
|
|
|
// UDP stream, MPEG-TS over udp.
|
|
|
|
SrsListenerMpegTsOverUdp = 3,
|
2015-02-15 13:28:31 +00:00
|
|
|
// TCP stream, RTSP stream.
|
|
|
|
SrsListenerRtsp = 4,
|
2013-11-23 03:36:07 +00:00
|
|
|
};
|
|
|
|
|
2015-01-24 06:52:52 +00:00
|
|
|
/**
|
|
|
|
* the common tcp listener, for RTMP/HTTP server.
|
|
|
|
*/
|
2015-02-17 08:28:28 +00:00
|
|
|
class SrsListener
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2015-02-17 08:28:28 +00:00
|
|
|
protected:
|
2014-04-12 14:16:39 +00:00
|
|
|
SrsListenerType _type;
|
2015-01-24 06:52:52 +00:00
|
|
|
protected:
|
2014-04-12 14:16:39 +00:00
|
|
|
int _port;
|
|
|
|
SrsServer* _server;
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2014-04-30 03:26:32 +00:00
|
|
|
SrsListener(SrsServer* server, SrsListenerType type);
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual ~SrsListener();
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2014-04-12 14:16:39 +00:00
|
|
|
virtual SrsListenerType type();
|
2015-02-17 08:28:28 +00:00
|
|
|
virtual int listen(int port) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tcp listener.
|
|
|
|
*/
|
|
|
|
class SrsStreamListener : virtual public SrsListener, virtual public ISrsTcpHandler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsTcpListener* listener;
|
|
|
|
public:
|
|
|
|
SrsStreamListener(SrsServer* server, SrsListenerType type);
|
|
|
|
virtual ~SrsStreamListener();
|
|
|
|
public:
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int listen(int port);
|
2015-02-17 08:28:28 +00:00
|
|
|
// ISrsTcpHandler
|
2013-12-14 06:06:32 +00:00
|
|
|
public:
|
2015-02-17 08:28:28 +00:00
|
|
|
virtual int on_tcp_client(st_netfd_t stfd);
|
2015-01-24 06:52:52 +00:00
|
|
|
};
|
|
|
|
|
2015-01-24 08:27:30 +00:00
|
|
|
#ifdef SRS_AUTO_STREAM_CASTER
|
2015-02-15 13:28:31 +00:00
|
|
|
/**
|
|
|
|
* the tcp listener, for rtsp server.
|
|
|
|
*/
|
2015-02-17 08:28:28 +00:00
|
|
|
class SrsRtspListener : virtual public SrsListener, virtual public ISrsTcpHandler
|
2015-02-15 13:28:31 +00:00
|
|
|
{
|
2015-02-17 08:28:28 +00:00
|
|
|
private:
|
|
|
|
SrsTcpListener* listener;
|
2015-02-17 13:10:06 +00:00
|
|
|
ISrsTcpHandler* caster;
|
2015-02-15 13:28:31 +00:00
|
|
|
public:
|
|
|
|
SrsRtspListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c);
|
|
|
|
virtual ~SrsRtspListener();
|
|
|
|
public:
|
2015-02-17 08:28:28 +00:00
|
|
|
virtual int listen(int port);
|
|
|
|
// ISrsTcpHandler
|
|
|
|
public:
|
|
|
|
virtual int on_tcp_client(st_netfd_t stfd);
|
2015-02-15 13:28:31 +00:00
|
|
|
};
|
|
|
|
|
2015-01-24 06:52:52 +00:00
|
|
|
/**
|
|
|
|
* the udp listener, for udp server.
|
|
|
|
*/
|
2015-02-17 08:28:28 +00:00
|
|
|
class SrsUdpCasterListener : public SrsListener
|
2015-01-24 06:52:52 +00:00
|
|
|
{
|
2015-01-24 07:36:11 +00:00
|
|
|
private:
|
2015-02-17 08:28:28 +00:00
|
|
|
SrsUdpListener* listener;
|
2015-01-29 16:04:20 +00:00
|
|
|
ISrsUdpHandler* caster;
|
2015-01-24 06:52:52 +00:00
|
|
|
public:
|
2015-02-17 08:28:28 +00:00
|
|
|
SrsUdpCasterListener(SrsServer* server, SrsListenerType type, SrsConfDirective* c);
|
|
|
|
virtual ~SrsUdpCasterListener();
|
2015-01-24 06:52:52 +00:00
|
|
|
public:
|
|
|
|
virtual int listen(int port);
|
2013-11-23 03:36:07 +00:00
|
|
|
};
|
2015-01-24 08:27:30 +00:00
|
|
|
#endif
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-04-30 03:26:32 +00:00
|
|
|
/**
|
|
|
|
* convert signal to io,
|
|
|
|
* @see: st-1.9/docs/notes.html
|
|
|
|
*/
|
|
|
|
class SrsSignalManager : public ISrsThreadHandler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/* Per-process pipe which is used as a signal queue. */
|
|
|
|
/* Up to PIPE_BUF/sizeof(int) signals can be queued up. */
|
|
|
|
int sig_pipe[2];
|
|
|
|
st_netfd_t signal_read_stfd;
|
|
|
|
private:
|
|
|
|
SrsServer* _server;
|
|
|
|
SrsThread* pthread;
|
|
|
|
public:
|
|
|
|
SrsSignalManager(SrsServer* server);
|
|
|
|
virtual ~SrsSignalManager();
|
|
|
|
public:
|
|
|
|
virtual int initialize();
|
|
|
|
virtual int start();
|
|
|
|
// interface ISrsThreadHandler.
|
|
|
|
public:
|
|
|
|
virtual int cycle();
|
|
|
|
private:
|
|
|
|
// global singleton instance
|
|
|
|
static SrsSignalManager* instance;
|
|
|
|
/* Signal catching function. */
|
|
|
|
/* Converts signal event to I/O event. */
|
|
|
|
static void sig_catcher(int signo);
|
|
|
|
};
|
|
|
|
|
2014-06-19 07:28:05 +00:00
|
|
|
/**
|
|
|
|
* SRS RTMP server, initialize and listen,
|
|
|
|
* start connection service thread, destroy client.
|
|
|
|
*/
|
2015-01-18 10:39:53 +00:00
|
|
|
class SrsServer : virtual public ISrsReloadHandler
|
2015-02-03 08:01:07 +00:00
|
|
|
, virtual public ISrsSourceHandler, virtual public ISrsHlsHandler
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2014-04-02 10:07:34 +00:00
|
|
|
private:
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_API
|
2015-03-04 05:09:35 +00:00
|
|
|
SrsHttpServeMux* http_api_mux;
|
2014-04-02 10:07:34 +00:00
|
|
|
#endif
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_SERVER
|
2015-01-17 15:00:40 +00:00
|
|
|
SrsHttpServer* http_stream_mux;
|
2014-04-02 10:07:34 +00:00
|
|
|
#endif
|
2014-05-19 09:39:01 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_PARSER
|
|
|
|
SrsHttpHeartbeat* http_heartbeat;
|
|
|
|
#endif
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_INGEST
|
2014-04-07 01:27:30 +00:00
|
|
|
SrsIngester* ingester;
|
|
|
|
#endif
|
2013-11-23 03:36:07 +00:00
|
|
|
private:
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* the pid file fd, lock the file write when server is running.
|
|
|
|
* @remark the init.d script should cleanup the pid file, when stop service,
|
|
|
|
* for the server never delete the file; when system startup, the pid in pid file
|
|
|
|
* maybe valid but the process is not SRS, the init.d script will never start server.
|
|
|
|
*/
|
2014-04-12 12:46:32 +00:00
|
|
|
int pid_fd;
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* all connections, connection manager
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
std::vector<SrsConnection*> conns;
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* all listners, listener manager.
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
std::vector<SrsListener*> listeners;
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* signal manager which convert gignal to io message.
|
|
|
|
*/
|
2014-04-30 03:26:32 +00:00
|
|
|
SrsSignalManager* signal_manager;
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* server total kbps.
|
|
|
|
*/
|
2014-06-19 07:28:05 +00:00
|
|
|
SrsKbps* kbps;
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* user send the signal, convert to variable.
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
bool signal_reload;
|
|
|
|
bool signal_gmc_stop;
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2014-03-18 03:32:58 +00:00
|
|
|
SrsServer();
|
|
|
|
virtual ~SrsServer();
|
2014-07-16 03:23:49 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* the destroy is for gmc to analysis the memory leak,
|
|
|
|
* if not destroy global/static data, the gmc will warning memory leak.
|
|
|
|
* in service, server never destroy, directly exit when restart.
|
|
|
|
*/
|
2014-05-02 06:08:48 +00:00
|
|
|
virtual void destroy();
|
2014-07-16 03:23:49 +00:00
|
|
|
// server startup workflow, @see run_master()
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int initialize();
|
2014-04-30 03:26:32 +00:00
|
|
|
virtual int initialize_signal();
|
2014-03-21 07:45:34 +00:00
|
|
|
virtual int acquire_pid_file();
|
2014-03-23 06:40:55 +00:00
|
|
|
virtual int initialize_st();
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int listen();
|
2014-04-30 03:26:32 +00:00
|
|
|
virtual int register_signal();
|
2014-04-07 05:13:57 +00:00
|
|
|
virtual int ingest();
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int cycle();
|
2014-07-16 03:23:49 +00:00
|
|
|
// server utility
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* callback for connection to remove itself.
|
|
|
|
* when connection thread cycle terminated, callback this to delete connection.
|
|
|
|
* @see SrsConnection.on_thread_stop().
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual void remove(SrsConnection* conn);
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* callback for signal manager got a signal.
|
|
|
|
* the signal manager convert signal to io message,
|
|
|
|
* whatever, we will got the signo like the orignal signal(int signo) handler.
|
|
|
|
* @remark, direclty exit for SIGTERM.
|
|
|
|
* @remark, do reload for SIGNAL_RELOAD.
|
|
|
|
* @remark, for SIGINT and SIGUSR2:
|
|
|
|
* no gmc, directly exit.
|
|
|
|
* for gmc, set the variable signal_gmc_stop, the cycle will return and cleanup for gmc.
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual void on_signal(int signo);
|
2013-11-23 03:36:07 +00:00
|
|
|
private:
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* the server thread main cycle,
|
|
|
|
* update the global static data, for instance, the current time,
|
|
|
|
* the cpu/mem/network statistic.
|
|
|
|
*/
|
2014-05-02 04:29:56 +00:00
|
|
|
virtual int do_cycle();
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* listen at specified protocol.
|
|
|
|
*/
|
2014-04-12 14:16:39 +00:00
|
|
|
virtual int listen_rtmp();
|
|
|
|
virtual int listen_http_api();
|
|
|
|
virtual int listen_http_stream();
|
2015-01-24 06:52:52 +00:00
|
|
|
virtual int listen_stream_caster();
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* close the listeners for specified type,
|
|
|
|
* remove the listen object from manager.
|
|
|
|
*/
|
2014-04-12 14:16:39 +00:00
|
|
|
virtual void close_listeners(SrsListenerType type);
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* resample the server kbps.
|
|
|
|
* if conn is NULL, resample all connections delta, then calc the total kbps.
|
|
|
|
* @param conn, the connection to do resample the kbps. NULL to resample all connections.
|
|
|
|
* @param do_resample, whether resample the server kbps. always false when sample a connection.
|
|
|
|
*/
|
2014-06-19 07:28:05 +00:00
|
|
|
virtual void resample_kbps(SrsConnection* conn, bool do_resample = true);
|
2014-04-30 03:26:32 +00:00
|
|
|
// internal only
|
|
|
|
public:
|
2014-07-16 03:23:49 +00:00
|
|
|
/**
|
|
|
|
* when listener got a fd, notice server to accept it.
|
|
|
|
* @param type, the client type, used to create concrete connection,
|
|
|
|
* for instance RTMP connection to serve client.
|
|
|
|
* @param client_stfd, the client fd in st boxed, the underlayer fd.
|
|
|
|
*/
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd);
|
2014-04-12 12:46:32 +00:00
|
|
|
// interface ISrsThreadHandler.
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2014-03-18 03:32:58 +00:00
|
|
|
virtual int on_reload_listen();
|
2014-04-12 12:46:32 +00:00
|
|
|
virtual int on_reload_pid();
|
2014-04-13 05:27:51 +00:00
|
|
|
virtual int on_reload_vhost_added(std::string vhost);
|
|
|
|
virtual int on_reload_vhost_removed(std::string vhost);
|
2014-04-13 05:08:10 +00:00
|
|
|
virtual int on_reload_http_api_enabled();
|
|
|
|
virtual int on_reload_http_api_disabled();
|
2014-04-13 05:27:51 +00:00
|
|
|
virtual int on_reload_http_stream_enabled();
|
|
|
|
virtual int on_reload_http_stream_disabled();
|
|
|
|
virtual int on_reload_http_stream_updated();
|
2015-01-18 10:39:53 +00:00
|
|
|
// interface ISrsSourceHandler
|
|
|
|
public:
|
|
|
|
virtual int on_publish(SrsSource* s, SrsRequest* r);
|
|
|
|
virtual void on_unpublish(SrsSource* s, SrsRequest* r);
|
2015-02-03 08:01:07 +00:00
|
|
|
// interface ISrsHlsHandler
|
|
|
|
public:
|
|
|
|
virtual int on_hls_publish(SrsRequest* r);
|
|
|
|
virtual int on_update_m3u8(SrsRequest* r, std::string m3u8);
|
|
|
|
virtual int on_update_ts(SrsRequest* r, std::string uri, std::string ts);
|
|
|
|
virtual int on_hls_unpublish(SrsRequest* r);
|
2013-11-23 03:36:07 +00:00
|
|
|
};
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
#endif
|