1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 06:54:22 +00:00
srs/trunk/src/app/srs_app_server.hpp

369 lines
12 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 Winlin
//
// SPDX-License-Identifier: MIT
//
2013-11-23 03:36:07 +00:00
#ifndef SRS_APP_SERVER_HPP
#define SRS_APP_SERVER_HPP
2013-11-23 03:36:07 +00:00
#include <srs_core.hpp>
#include <vector>
#include <string>
2013-11-23 03:36:07 +00:00
#include <srs_app_st.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_source.hpp>
#include <srs_app_hls.hpp>
#include <srs_app_listener.hpp>
#include <srs_app_conn.hpp>
#include <srs_service_st.hpp>
#include <srs_app_hourglass.hpp>
2021-02-15 12:01:49 +00:00
#include <srs_app_hybrid.hpp>
2013-11-23 03:36:07 +00:00
class SrsServer;
class SrsHttpServeMux;
class SrsHttpServer;
class SrsIngester;
2014-05-19 09:39:01 +00:00
class SrsHttpHeartbeat;
class SrsKbps;
class SrsConfDirective;
2015-02-17 13:10:06 +00:00
class ISrsTcpHandler;
2015-01-29 16:04:20 +00:00
class ISrsUdpHandler;
class SrsUdpListener;
class SrsTcpListener;
class SrsAppCasterFlv;
class SrsRtspCaster;
2020-09-19 02:30:05 +00:00
class SrsResourceManager;
class SrsLatestVersion;
2013-11-23 03:36:07 +00:00
2019-04-28 01:08:05 +00:00
// The listener type for server to identify the connection,
2014-03-27 04:27:47 +00:00
// that is, use different type to process the connection.
2017-03-25 09:21:39 +00:00
enum SrsListenerType
2013-11-23 03:36:07 +00:00
{
2014-03-27 04:27:47 +00:00
// RTMP client,
2017-01-16 07:47:26 +00:00
SrsListenerRtmpStream = 0,
2014-03-27 04:27:47 +00:00
// HTTP api,
2017-01-16 07:47:26 +00:00
SrsListenerHttpApi = 1,
2014-03-27 04:27:47 +00:00
// HTTP stream, HDS/HLS/DASH
2017-01-16 07:47:26 +00:00
SrsListenerHttpStream = 2,
// UDP stream, MPEG-TS over udp.
2017-01-16 07:47:26 +00:00
SrsListenerMpegTsOverUdp = 3,
// TCP stream, RTSP stream.
2017-01-16 07:47:26 +00:00
SrsListenerRtsp = 4,
// TCP stream, FLV stream over HTTP.
2017-01-16 07:47:26 +00:00
SrsListenerFlv = 5,
2020-11-06 07:05:01 +00:00
// HTTPS api,
SrsListenerHttpsApi = 8,
2020-11-06 09:22:23 +00:00
// HTTPS stream,
SrsListenerHttpsStream = 9,
2013-11-23 03:36:07 +00:00
};
2019-04-28 01:08:05 +00:00
// A common tcp listener, for RTMP/HTTP server.
class SrsListener
2013-11-23 03:36:07 +00:00
{
protected:
SrsListenerType type;
protected:
std::string ip;
int port;
SrsServer* server;
2013-11-23 03:36:07 +00:00
public:
SrsListener(SrsServer* svr, SrsListenerType t);
2014-03-18 03:32:58 +00:00
virtual ~SrsListener();
2013-11-23 03:36:07 +00:00
public:
virtual SrsListenerType listen_type();
virtual srs_error_t listen(std::string i, int p) = 0;
};
2019-04-28 01:08:05 +00:00
// A buffered TCP listener.
class SrsBufferListener : public SrsListener, public ISrsTcpHandler
{
private:
SrsTcpListener* listener;
public:
2015-09-22 00:57:31 +00:00
SrsBufferListener(SrsServer* server, SrsListenerType type);
virtual ~SrsBufferListener();
public:
virtual srs_error_t listen(std::string ip, int port);
2019-04-30 00:30:13 +00:00
// Interface ISrsTcpHandler
public:
virtual srs_error_t on_tcp_client(srs_netfd_t stfd);
};
2019-04-28 01:08:05 +00:00
// A TCP listener, for rtsp server.
class SrsRtspListener : public SrsListener, public ISrsTcpHandler
{
private:
SrsTcpListener* listener;
SrsRtspCaster* caster;
public:
SrsRtspListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c);
virtual ~SrsRtspListener();
public:
virtual srs_error_t listen(std::string i, int p);
2019-04-30 00:30:13 +00:00
// Interface ISrsTcpHandler
public:
virtual srs_error_t on_tcp_client(srs_netfd_t stfd);
};
2019-04-28 01:08:05 +00:00
// A TCP listener, for flv stream server.
class SrsHttpFlvListener : public SrsListener, public ISrsTcpHandler
{
private:
SrsTcpListener* listener;
SrsAppCasterFlv* caster;
public:
SrsHttpFlvListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c);
virtual ~SrsHttpFlvListener();
public:
virtual srs_error_t listen(std::string i, int p);
2019-04-30 00:30:13 +00:00
// Interface ISrsTcpHandler
public:
virtual srs_error_t on_tcp_client(srs_netfd_t stfd);
};
2019-04-28 01:08:05 +00:00
// A UDP listener, for udp server.
class SrsUdpStreamListener : public SrsListener
{
protected:
SrsUdpListener* listener;
2015-01-29 16:04:20 +00:00
ISrsUdpHandler* caster;
public:
SrsUdpStreamListener(SrsServer* svr, SrsListenerType t, ISrsUdpHandler* c);
virtual ~SrsUdpStreamListener();
public:
virtual srs_error_t listen(std::string i, int p);
};
2019-04-28 01:08:05 +00:00
// A UDP listener, for udp stream caster server.
class SrsUdpCasterListener : public SrsUdpStreamListener
{
public:
SrsUdpCasterListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c);
virtual ~SrsUdpCasterListener();
2013-11-23 03:36:07 +00:00
};
2019-04-28 01:08:05 +00:00
// Convert signal to io,
// @see: st-1.9/docs/notes.html
class SrsSignalManager : public ISrsCoroutineHandler
{
private:
2019-04-28 01:08:05 +00:00
// 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];
srs_netfd_t signal_read_stfd;
private:
SrsServer* server;
SrsCoroutine* trd;
public:
SrsSignalManager(SrsServer* s);
virtual ~SrsSignalManager();
public:
2017-06-10 07:20:48 +00:00
virtual srs_error_t initialize();
virtual srs_error_t start();
2019-04-30 00:30:13 +00:00
// Interface ISrsEndlessThreadHandler.
public:
virtual srs_error_t cycle();
private:
2019-04-28 01:08:05 +00:00
// Global singleton instance
static SrsSignalManager* instance;
2019-04-28 01:08:05 +00:00
// Signal catching function.
// Converts signal event to I/O event.
static void sig_catcher(int signo);
};
// Auto reload by inotify.
// @see https://github.com/ossrs/srs/issues/1635
class SrsInotifyWorker : public ISrsCoroutineHandler
{
private:
SrsServer* server;
SrsCoroutine* trd;
srs_netfd_t inotify_fd;
public:
SrsInotifyWorker(SrsServer* s);
virtual ~SrsInotifyWorker();
public:
virtual srs_error_t start();
// Interface ISrsEndlessThreadHandler.
public:
virtual srs_error_t cycle();
};
2019-04-28 01:08:05 +00:00
// A handler to the handle cycle in SRS RTMP server.
2015-03-31 10:06:55 +00:00
class ISrsServerCycle
{
public:
ISrsServerCycle();
virtual ~ISrsServerCycle();
2017-03-25 09:21:39 +00:00
public:
2019-04-28 01:08:05 +00:00
// Initialize the cycle handler.
2017-06-09 05:29:23 +00:00
virtual srs_error_t initialize() = 0;
2019-04-28 01:08:05 +00:00
// Do on_cycle while server doing cycle.
2017-06-11 01:40:07 +00:00
virtual srs_error_t on_cycle() = 0;
2019-04-28 01:08:05 +00:00
// Callback the handler when got client.
virtual srs_error_t on_accept_client(int max, int cur) = 0;
2020-08-31 07:09:33 +00:00
// Callback for logrotate.
virtual void on_logrotate() = 0;
2015-03-31 10:06:55 +00:00
};
2020-09-30 03:14:49 +00:00
// TODO: FIXME: Rename to SrsLiveServer.
2019-04-28 01:08:05 +00:00
// SRS RTMP server, initialize and listen, start connection service thread, destroy client.
2021-05-16 08:14:00 +00:00
class SrsServer : public ISrsReloadHandler, public ISrsLiveSourceHandler
, public ISrsResourceManager, public ISrsCoroutineHandler
, public ISrsHourGlass
2013-11-23 03:36:07 +00:00
{
2014-04-02 10:07:34 +00:00
private:
2020-09-30 03:14:49 +00:00
// TODO: FIXME: Extract an HttpApiServer.
SrsHttpServeMux* http_api_mux;
SrsHttpServer* http_server;
2014-05-19 09:39:01 +00:00
SrsHttpHeartbeat* http_heartbeat;
SrsIngester* ingester;
2020-09-19 02:30:05 +00:00
SrsResourceManager* conn_manager;
2021-02-09 04:44:56 +00:00
SrsCoroutine* trd_;
SrsHourGlass* timer_;
2013-11-23 03:36:07 +00:00
private:
2019-04-28 01:08:05 +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;
2019-04-28 01:08:05 +00:00
// All listners, listener manager.
2014-03-18 03:32:58 +00:00
std::vector<SrsListener*> listeners;
2019-04-28 01:08:05 +00:00
// Signal manager which convert gignal to io message.
SrsSignalManager* signal_manager;
// To query the latest available version of SRS.
SrsLatestVersion* latest_version_;
2019-04-28 01:08:05 +00:00
// Handle in server cycle.
2015-03-31 10:06:55 +00:00
ISrsServerCycle* handler;
2019-04-28 01:08:05 +00:00
// User send the signal, convert to variable.
2014-03-18 03:32:58 +00:00
bool signal_reload;
bool signal_persistence_config;
2014-03-18 03:32:58 +00:00
bool signal_gmc_stop;
bool signal_fast_quit;
bool signal_gracefully_quit;
2019-04-28 01:08:05 +00:00
// Parent pid for asprocess.
2016-09-23 07:00:50 +00:00
int ppid;
2013-11-23 03:36:07 +00:00
public:
2014-03-18 03:32:58 +00:00
SrsServer();
virtual ~SrsServer();
private:
2019-04-28 01:08:05 +00:00
// 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.
virtual void destroy();
2019-04-28 01:08:05 +00:00
// When SIGTERM, SRS should do cleanup, for example,
// to stop all ingesters, cleanup HLS and dvr.
virtual void dispose();
// Close listener to stop accepting new connections,
// then wait and quit when all connections finished.
virtual void gracefully_dispose();
2018-01-01 11:39:57 +00:00
// server startup workflow, @see run_master()
2013-11-23 03:36:07 +00:00
public:
2019-04-28 01:08:05 +00:00
// Initialize server with callback handler ch.
// @remark user must free the handler.
virtual srs_error_t initialize(ISrsServerCycle* ch);
2017-06-10 06:29:41 +00:00
virtual srs_error_t initialize_st();
2017-06-10 07:20:48 +00:00
virtual srs_error_t initialize_signal();
virtual srs_error_t acquire_pid_file();
virtual srs_error_t listen();
virtual srs_error_t register_signal();
virtual srs_error_t http_handle();
virtual srs_error_t ingest();
2021-02-09 04:44:56 +00:00
virtual srs_error_t start();
// interface ISrsCoroutineHandler
public:
2017-06-10 07:20:48 +00:00
virtual srs_error_t cycle();
2018-01-01 11:39:57 +00:00
// server utilities.
public:
2019-04-28 01:08:05 +00:00
// The 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.
// @param signo the signal number from user, where:
// SRS_SIGNAL_FAST_QUIT, the SIGTERM, do essential dispose then quit.
// SRS_SIGNAL_GRACEFULLY_QUIT, the SIGQUIT, do careful dispose then quit.
2019-04-28 01:08:05 +00:00
// SRS_SIGNAL_REOPEN_LOG, the SIGUSR1, reopen the log file.
// SRS_SIGNAL_RELOAD, the SIGHUP, reload the config.
// SRS_SIGNAL_PERSISTENCE_CONFIG, application level signal, persistence config to file.
// @remark, for SIGINT:
// no gmc, fast quit, do essential dispose then quit.
2019-04-28 01:08:05 +00:00
// for gmc, set the variable signal_gmc_stop, the cycle will return and cleanup for gmc.
// @remark, maybe the HTTP RAW API will trigger the on_signal() also.
2014-03-18 03:32:58 +00:00
virtual void on_signal(int signo);
2013-11-23 03:36:07 +00:00
private:
2019-04-28 01:08:05 +00:00
// The server thread main cycle,
// update the global static data, for instance, the current time,
// the cpu/mem/network statistic.
2017-06-10 07:20:48 +00:00
virtual srs_error_t do_cycle();
// interface ISrsHourGlass
private:
virtual srs_error_t setup_ticks();
virtual srs_error_t notify(int event, srs_utime_t interval, srs_utime_t tick);
private:
2019-04-28 01:08:05 +00:00
// listen at specified protocol.
2017-06-10 07:20:48 +00:00
virtual srs_error_t listen_rtmp();
virtual srs_error_t listen_http_api();
2020-11-06 07:05:01 +00:00
virtual srs_error_t listen_https_api();
2017-06-10 07:20:48 +00:00
virtual srs_error_t listen_http_stream();
2020-11-06 09:22:23 +00:00
virtual srs_error_t listen_https_stream();
2017-06-10 07:20:48 +00:00
virtual srs_error_t listen_stream_caster();
2019-04-28 01:08:05 +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);
2019-04-28 01:08:05 +00:00
// Resample the server kbs.
2015-03-08 04:55:40 +00:00
virtual void resample_kbps();
2019-04-28 01:08:05 +00:00
// For internal only
public:
2019-04-28 01:08:05 +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 stfd, the client fd in st boxed, the underlayer fd.
virtual srs_error_t accept_client(SrsListenerType type, srs_netfd_t stfd);
// TODO: FIXME: Fetch from hybrid server manager.
virtual SrsHttpServeMux* api_server();
2015-12-24 09:25:05 +00:00
private:
2020-11-04 12:57:07 +00:00
virtual srs_error_t fd_to_resource(SrsListenerType type, srs_netfd_t stfd, ISrsStartableConneciton** pr);
2020-09-19 02:30:05 +00:00
// Interface ISrsResourceManager
2015-09-23 09:21:57 +00:00
public:
2019-04-28 01:08:05 +00:00
// A callback for connection to remove itself.
// When connection thread cycle terminated, callback this to delete connection.
// @see SrsTcpConnection.on_thread_stop().
2020-09-19 02:30:05 +00:00
virtual void remove(ISrsResource* c);
2019-04-30 00:30:13 +00:00
// Interface ISrsReloadHandler.
2013-11-23 03:36:07 +00:00
public:
2017-09-22 08:14:30 +00:00
virtual srs_error_t on_reload_listen();
virtual srs_error_t on_reload_pid();
virtual srs_error_t on_reload_vhost_added(std::string vhost);
virtual srs_error_t on_reload_vhost_removed(std::string vhost);
virtual srs_error_t on_reload_http_api_enabled();
virtual srs_error_t on_reload_http_api_disabled();
virtual srs_error_t on_reload_http_stream_enabled();
virtual srs_error_t on_reload_http_stream_disabled();
virtual srs_error_t on_reload_http_stream_updated();
2021-05-16 08:14:00 +00:00
// Interface ISrsLiveSourceHandler
public:
2021-05-16 08:14:00 +00:00
virtual srs_error_t on_publish(SrsLiveSource* s, SrsRequest* r);
virtual void on_unpublish(SrsLiveSource* s, SrsRequest* r);
2013-11-23 03:36:07 +00:00
};
2021-02-15 12:01:49 +00:00
// The SRS server adapter, the master server.
class SrsServerAdapter : public ISrsHybridServer
{
private:
SrsServer* srs;
public:
SrsServerAdapter();
virtual ~SrsServerAdapter();
public:
virtual srs_error_t initialize();
virtual srs_error_t run();
virtual void stop();
public:
virtual SrsServer* instance();
};
2014-08-02 14:18:39 +00:00
#endif