2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-01-01 13:37:28 +00:00
|
|
|
* Copyright (c) 2013-2019 Winlin
|
2017-03-25 09:21:39 +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.
|
|
|
|
*/
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-04-10 06:12:22 +00:00
|
|
|
#include <srs_core.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-03-23 04:42:05 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2015-09-24 04:15:12 +00:00
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_GPERF_MP
|
2017-03-25 09:21:39 +00:00
|
|
|
#include <gperftools/heap-profiler.h>
|
2014-03-06 10:12:19 +00:00
|
|
|
#endif
|
2014-04-15 06:01:57 +00:00
|
|
|
#ifdef SRS_AUTO_GPERF_CP
|
2017-03-25 09:21:39 +00:00
|
|
|
#include <gperftools/profiler.h>
|
2014-03-06 10:12:19 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-30 01:05:02 +00:00
|
|
|
#include <unistd.h>
|
2016-09-23 07:37:51 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-04-10 06:12:22 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_app_server.hpp>
|
|
|
|
#include <srs_app_config.hpp>
|
|
|
|
#include <srs_app_log.hpp>
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2014-12-04 13:35:13 +00:00
|
|
|
#include <srs_core_performance.hpp>
|
2015-09-24 04:15:12 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2016-12-08 09:26:04 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2014-04-10 06:12:22 +00:00
|
|
|
|
2014-04-12 12:37:16 +00:00
|
|
|
// pre-declare
|
2017-06-09 03:50:35 +00:00
|
|
|
srs_error_t run(SrsServer* svr);
|
2017-06-10 06:29:41 +00:00
|
|
|
srs_error_t run_master(SrsServer* svr);
|
2016-12-08 09:35:33 +00:00
|
|
|
void show_macro_features();
|
2016-12-08 09:55:11 +00:00
|
|
|
string srs_getenv(const char* name);
|
2014-04-12 12:37:16 +00:00
|
|
|
|
2016-12-08 09:26:04 +00:00
|
|
|
// @global log and context.
|
2014-04-10 06:12:22 +00:00
|
|
|
ISrsLog* _srs_log = new SrsFastLog();
|
2016-12-08 09:29:13 +00:00
|
|
|
ISrsThreadContext* _srs_context = new SrsThreadContext();
|
2016-12-08 09:26:04 +00:00
|
|
|
// @global config object for app module.
|
2014-04-10 06:12:22 +00:00
|
|
|
SrsConfig* _srs_config = new SrsConfig();
|
2016-12-08 09:26:04 +00:00
|
|
|
|
|
|
|
// @global version of srs, which can grep keyword "XCORE"
|
2016-01-05 08:49:27 +00:00
|
|
|
extern const char* _srs_version;
|
2014-04-10 06:12:22 +00:00
|
|
|
|
2014-09-26 03:05:45 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* main entrance.
|
2016-12-08 09:55:11 +00:00
|
|
|
*/
|
2017-06-11 06:03:19 +00:00
|
|
|
srs_error_t do_main(int argc, char** argv)
|
2016-12-08 09:35:33 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// TODO: support both little and big endian.
|
|
|
|
srs_assert(srs_is_little_endian());
|
2016-12-08 09:55:11 +00:00
|
|
|
|
|
|
|
// for gperf gmp or gcp,
|
2016-12-08 09:35:33 +00:00
|
|
|
// should never enable it when not enabled for performance issue.
|
|
|
|
#ifdef SRS_AUTO_GPERF_MP
|
|
|
|
HeapProfilerStart("gperf.srs.gmp");
|
|
|
|
#endif
|
|
|
|
#ifdef SRS_AUTO_GPERF_CP
|
|
|
|
ProfilerStart("gperf.srs.gcp");
|
|
|
|
#endif
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// directly compile error when these two macro defines.
|
|
|
|
#if defined(SRS_AUTO_GPERF_MC) && defined(SRS_AUTO_GPERF_MP)
|
2016-12-08 09:55:11 +00:00
|
|
|
#error ("option --with-gmc confict with --with-gmp, "
|
|
|
|
"@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
|
|
|
|
"Note that since the heap-checker uses the heap-profiling framework internally, "
|
|
|
|
"it is not possible to run both the heap-checker and heap profiler at the same time");
|
2016-12-08 09:35:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// never use gmp to check memory leak.
|
|
|
|
#ifdef SRS_AUTO_GPERF_MP
|
2016-12-08 09:55:11 +00:00
|
|
|
#warning "gmp is not used for memory leak, please use gmc instead."
|
2016-12-08 09:35:33 +00:00
|
|
|
#endif
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// never use srs log(srs_trace, srs_error, etc) before config parse the option,
|
|
|
|
// which will load the log config and apply it.
|
2017-06-11 06:03:19 +00:00
|
|
|
if ((err = _srs_config->parse_options(argc, argv)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "config parse options");
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// change the work dir and set cwd.
|
2017-06-11 06:03:19 +00:00
|
|
|
int r0 = 0;
|
2016-12-08 09:35:33 +00:00
|
|
|
string cwd = _srs_config->get_work_dir();
|
2017-06-11 06:03:19 +00:00
|
|
|
if (!cwd.empty() && cwd != "./" && (r0 = chdir(cwd.c_str())) == -1) {
|
|
|
|
return srs_error_new(-1, "chdir to %s, r0=%d", cwd.c_str(), r0);
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
2017-06-11 06:03:19 +00:00
|
|
|
if ((err = _srs_config->initialize_cwd()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "config cwd");
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// config parsed, initialize log.
|
2017-06-11 06:03:19 +00:00
|
|
|
if ((err = _srs_log->initialize()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "log initialize");
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// config already applied to log.
|
2019-04-30 00:43:12 +00:00
|
|
|
srs_trace(RTMP_SIG_SRS_SERVER);
|
2019-04-30 00:38:57 +00:00
|
|
|
srs_trace("license: " RTMP_SIG_SRS_LICENSE);
|
2017-05-06 06:05:22 +00:00
|
|
|
srs_trace("contributors: " SRS_AUTO_CONSTRIBUTORS);
|
2016-12-08 09:35:33 +00:00
|
|
|
srs_trace("build: %s, configure:%s, uname: %s", SRS_AUTO_BUILD_DATE, SRS_AUTO_USER_CONFIGURE, SRS_AUTO_UNAME);
|
2017-05-06 06:05:22 +00:00
|
|
|
srs_trace("configure detail: " SRS_AUTO_CONFIGURE);
|
2016-12-08 09:35:33 +00:00
|
|
|
#ifdef SRS_AUTO_EMBEDED_TOOL_CHAIN
|
2017-05-06 06:05:22 +00:00
|
|
|
srs_trace("crossbuild tool chain: " SRS_AUTO_EMBEDED_TOOL_CHAIN);
|
2016-12-08 09:35:33 +00:00
|
|
|
#endif
|
|
|
|
srs_trace("cwd=%s, work_dir=%s", _srs_config->cwd().c_str(), cwd.c_str());
|
|
|
|
|
2016-12-08 09:55:11 +00:00
|
|
|
// for memory check or detect.
|
|
|
|
if (true) {
|
|
|
|
stringstream ss;
|
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
#ifdef SRS_PERF_GLIBC_MEMORY_CHECK
|
2016-12-08 09:55:11 +00:00
|
|
|
// ensure glibc write error to stderr.
|
|
|
|
string lfsov = srs_getenv("LIBC_FATAL_STDERR_");
|
|
|
|
setenv("LIBC_FATAL_STDERR_", "1", 1);
|
|
|
|
string lfsnv = srs_getenv("LIBC_FATAL_STDERR_");
|
|
|
|
//
|
|
|
|
// ensure glibc to do alloc check.
|
|
|
|
string mcov = srs_getenv("MALLOC_CHECK_");
|
|
|
|
setenv("MALLOC_CHECK_", "1", 1);
|
|
|
|
string mcnv = srs_getenv("MALLOC_CHECK_");
|
|
|
|
ss << "glic mem-check env MALLOC_CHECK_ " << mcov << "=>" << mcnv << ", LIBC_FATAL_STDERR_ " << lfsov << "=>" << lfsnv << ".";
|
2016-12-08 09:35:33 +00:00
|
|
|
#endif
|
2016-12-08 09:55:11 +00:00
|
|
|
|
|
|
|
#ifdef SRS_AUTO_GPERF_MC
|
|
|
|
string hcov = srs_getenv("HEAPCHECK");
|
|
|
|
if (hcov.empty()) {
|
|
|
|
string cpath = _srs_config->config();
|
|
|
|
srs_warn("gmc HEAPCHECK is required, for example: env HEAPCHECK=normal ./objs/srs -c %s", cpath.c_str());
|
|
|
|
} else {
|
|
|
|
ss << "gmc env HEAPCHECK=" << hcov << ".";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
#ifdef SRS_AUTO_GPERF_MD
|
2016-12-08 09:55:11 +00:00
|
|
|
char* TCMALLOC_PAGE_FENCE = getenv("TCMALLOC_PAGE_FENCE");
|
|
|
|
if (!TCMALLOC_PAGE_FENCE || strcmp(TCMALLOC_PAGE_FENCE, "1")) {
|
|
|
|
srs_warn("gmd enabled without env TCMALLOC_PAGE_FENCE=1");
|
|
|
|
} else {
|
|
|
|
ss << "gmd env TCMALLOC_PAGE_FENCE=" << TCMALLOC_PAGE_FENCE << ".";
|
|
|
|
}
|
2016-12-08 09:35:33 +00:00
|
|
|
#endif
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2017-02-11 13:14:28 +00:00
|
|
|
string sss = ss.str();
|
|
|
|
if (!sss.empty()) {
|
|
|
|
srs_trace(sss.c_str());
|
|
|
|
}
|
2016-12-08 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// we check the config when the log initialized.
|
2017-06-11 06:03:19 +00:00
|
|
|
if ((err = _srs_config->check_config()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "check config");
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
2016-12-08 09:55:11 +00:00
|
|
|
|
2016-12-08 09:35:33 +00:00
|
|
|
// features
|
|
|
|
show_macro_features();
|
|
|
|
|
|
|
|
SrsServer* svr = new SrsServer();
|
|
|
|
SrsAutoFree(SrsServer, svr);
|
|
|
|
|
2017-06-11 06:03:19 +00:00
|
|
|
if ((err = run(svr)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "run");
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
srs_error_t err = do_main(argc, argv);
|
|
|
|
|
2017-06-09 03:50:35 +00:00
|
|
|
if (err != srs_success) {
|
|
|
|
srs_error("Failed, %s", srs_error_desc(err).c_str());
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 06:03:19 +00:00
|
|
|
int ret = srs_error_code(err);
|
2017-06-09 03:50:35 +00:00
|
|
|
srs_freep(err);
|
|
|
|
return ret;
|
2016-12-08 09:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* show the features by macro, the actual macro values.
|
|
|
|
*/
|
2014-09-26 03:05:45 +00:00
|
|
|
void show_macro_features()
|
2014-09-23 08:53:12 +00:00
|
|
|
{
|
2015-09-24 04:15:12 +00:00
|
|
|
if (true) {
|
|
|
|
stringstream ss;
|
|
|
|
|
|
|
|
ss << "features";
|
|
|
|
|
|
|
|
// rch(rtmp complex handshake)
|
2018-12-22 12:03:40 +00:00
|
|
|
ss << ", rch:" << srs_bool2switch(true);
|
2017-02-11 13:14:28 +00:00
|
|
|
ss << ", dash:" << "on";
|
2018-12-22 11:54:10 +00:00
|
|
|
ss << ", hls:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << ", hds:" << srs_bool2switch(SRS_AUTO_HDS_BOOL);
|
|
|
|
// hc(http callback)
|
2017-01-23 09:55:52 +00:00
|
|
|
ss << ", hc:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
// ha(http api)
|
2017-01-23 09:55:52 +00:00
|
|
|
ss << ", ha:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
// hs(http server)
|
2017-01-23 09:55:52 +00:00
|
|
|
ss << ", hs:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
// hp(http parser)
|
2017-01-23 09:55:52 +00:00
|
|
|
ss << ", hp:" << srs_bool2switch(true);
|
2018-12-22 11:50:57 +00:00
|
|
|
ss << ", dvr:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
// trans(transcode)
|
2018-12-22 11:48:55 +00:00
|
|
|
ss << ", trans:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
// inge(ingest)
|
2018-12-22 12:30:13 +00:00
|
|
|
ss << ", inge:" << srs_bool2switch(true);
|
2018-12-22 11:58:36 +00:00
|
|
|
ss << ", stat:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << ", nginx:" << srs_bool2switch(SRS_AUTO_NGINX_BOOL);
|
|
|
|
// ff(ffmpeg)
|
|
|
|
ss << ", ff:" << srs_bool2switch(SRS_AUTO_FFMPEG_TOOL_BOOL);
|
|
|
|
// sc(stream-caster)
|
2018-12-22 12:45:25 +00:00
|
|
|
ss << ", sc:" << srs_bool2switch(true);
|
2015-09-24 04:15:12 +00:00
|
|
|
srs_trace(ss.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
stringstream ss;
|
|
|
|
ss << "SRS on ";
|
|
|
|
#ifdef SRS_OSX
|
|
|
|
ss << "OSX";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#ifdef SRS_PI
|
|
|
|
ss << "RespberryPi";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#ifdef SRS_CUBIE
|
|
|
|
ss << "CubieBoard";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#ifdef SRS_ARM_UBUNTU12
|
|
|
|
ss << "ARM(build on ubuntu)";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#ifdef SRS_MIPS_UBUNTU12
|
|
|
|
ss << "MIPS(build on ubuntu)";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
#if defined(__amd64__)
|
|
|
|
ss << " amd64";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#if defined(__x86_64__)
|
|
|
|
ss << " x86_64";
|
2014-09-23 08:53:12 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#if defined(__i386__)
|
|
|
|
ss << " i386";
|
2014-11-06 06:23:08 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
#if defined(__arm__)
|
|
|
|
ss << "arm";
|
2015-02-15 13:28:31 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
#ifndef SRS_OSX
|
2015-10-27 09:57:58 +00:00
|
|
|
ss << ", glibc" << (int)__GLIBC__ << "." << (int)__GLIBC_MINOR__;
|
2015-09-24 04:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ss << ", conf:" << _srs_config->config() << ", limit:" << _srs_config->get_max_connections()
|
2016-12-08 09:35:33 +00:00
|
|
|
<< ", writev:" << sysconf(_SC_IOV_MAX) << ", encoding:" << (srs_is_little_endian()? "little-endian":"big-endian")
|
|
|
|
<< ", HZ:" << (int)sysconf(_SC_CLK_TCK);
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
srs_trace(ss.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
stringstream ss;
|
|
|
|
|
|
|
|
// mw(merged-write)
|
2019-04-09 23:53:09 +00:00
|
|
|
ss << "mw sleep:" << srsu2msi(SRS_PERF_MW_SLEEP) << "ms";
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
// mr(merged-read)
|
|
|
|
ss << ". mr ";
|
2014-12-04 13:35:13 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "enabled:on";
|
2014-12-04 13:35:13 +00:00
|
|
|
#else
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "enabled:off";
|
2014-12-04 13:35:13 +00:00
|
|
|
#endif
|
2019-04-09 23:53:09 +00:00
|
|
|
ss << ", default:" << SRS_PERF_MR_ENABLED << ", sleep:" << srsu2msi(SRS_PERF_MR_SLEEP) << "ms";
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
srs_trace(ss.str().c_str());
|
|
|
|
}
|
2015-03-04 08:52:43 +00:00
|
|
|
|
2015-09-24 04:15:12 +00:00
|
|
|
if (true) {
|
|
|
|
stringstream ss;
|
|
|
|
|
|
|
|
// gc(gop-cache)
|
|
|
|
ss << "gc:" << srs_bool2switch(SRS_PERF_GOP_CACHE);
|
|
|
|
// pq(play-queue)
|
2019-04-15 23:55:19 +00:00
|
|
|
ss << ", pq:" << srsu2msi(SRS_PERF_PLAY_QUEUE) << "ms";
|
2015-09-24 04:15:12 +00:00
|
|
|
// cscc(chunk stream cache cid)
|
|
|
|
ss << ", cscc:[0," << SRS_PERF_CHUNK_STREAM_CACHE << ")";
|
|
|
|
// csa(complex send algorithm)
|
|
|
|
ss << ", csa:";
|
2015-03-04 08:52:43 +00:00
|
|
|
#ifndef SRS_PERF_COMPLEX_SEND
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "off";
|
2015-03-04 08:52:43 +00:00
|
|
|
#else
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "on";
|
2015-03-04 08:52:43 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
// tn(TCP_NODELAY)
|
|
|
|
ss << ", tn:";
|
2015-03-04 08:52:43 +00:00
|
|
|
#ifdef SRS_PERF_TCP_NODELAY
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "on(may hurts performance)";
|
2015-03-04 08:52:43 +00:00
|
|
|
#else
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "off";
|
2015-03-04 08:52:43 +00:00
|
|
|
#endif
|
2016-12-08 09:35:33 +00:00
|
|
|
|
2015-09-24 04:15:12 +00:00
|
|
|
// ss(SO_SENDBUF)
|
|
|
|
ss << ", ss:";
|
2015-03-04 08:52:43 +00:00
|
|
|
#ifdef SRS_PERF_SO_SNDBUF_SIZE
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << SRS_PERF_SO_SNDBUF_SIZE;
|
2015-03-04 08:52:43 +00:00
|
|
|
#else
|
2015-09-24 04:15:12 +00:00
|
|
|
ss << "auto(guess by merged write)";
|
2015-03-04 08:52:43 +00:00
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
srs_trace(ss.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// others
|
2014-12-04 13:35:13 +00:00
|
|
|
int possible_mr_latency = 0;
|
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2019-04-09 23:53:09 +00:00
|
|
|
possible_mr_latency = srsu2msi(SRS_PERF_MR_SLEEP);
|
2014-12-04 13:35:13 +00:00
|
|
|
#endif
|
2019-04-22 00:12:17 +00:00
|
|
|
srs_trace("system default latency(ms): mw(0-%d) + mr(0-%d) + play-queue(0-%d)",
|
2019-04-15 23:55:19 +00:00
|
|
|
srsu2msi(SRS_PERF_MW_SLEEP), possible_mr_latency, srsu2msi(SRS_PERF_PLAY_QUEUE));
|
2015-06-08 01:47:45 +00:00
|
|
|
|
2015-06-13 07:45:25 +00:00
|
|
|
#ifdef SRS_AUTO_MEM_WATCH
|
2016-12-08 09:35:33 +00:00
|
|
|
#warning "srs memory watcher will hurts performance. user should kill by SIGTERM or init.d script."
|
2015-06-08 01:47:45 +00:00
|
|
|
srs_warn("srs memory watcher will hurts performance. user should kill by SIGTERM or init.d script.");
|
|
|
|
#endif
|
2015-09-24 04:15:12 +00:00
|
|
|
|
|
|
|
#if VERSION_MAJOR > VERSION_STABLE
|
2017-03-01 01:21:20 +00:00
|
|
|
#warning "Current branch is unstable."
|
2017-06-10 06:29:41 +00:00
|
|
|
srs_warn("Develop is unstable, please use branch: git checkout -b %s origin/%s", VERSION_STABLE_BRANCH, VERSION_STABLE_BRANCH);
|
2015-03-04 08:52:43 +00:00
|
|
|
#endif
|
2015-09-23 07:46:51 +00:00
|
|
|
|
2015-09-24 04:15:12 +00:00
|
|
|
#if defined(SRS_PERF_SO_SNDBUF_SIZE) && !defined(SRS_PERF_MW_SO_SNDBUF)
|
2016-12-08 09:35:33 +00:00
|
|
|
#error "SRS_PERF_SO_SNDBUF_SIZE depends on SRS_PERF_MW_SO_SNDBUF"
|
2015-09-23 07:46:51 +00:00
|
|
|
#endif
|
2014-09-23 08:53:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 09:55:11 +00:00
|
|
|
string srs_getenv(const char* name)
|
|
|
|
{
|
|
|
|
char* cv = ::getenv(name);
|
|
|
|
|
|
|
|
if (cv) {
|
|
|
|
return cv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:50:35 +00:00
|
|
|
srs_error_t run(SrsServer* svr)
|
2014-03-23 04:42:05 +00:00
|
|
|
{
|
2017-06-09 03:50:35 +00:00
|
|
|
srs_error_t err = srs_success;
|
2019-04-30 00:30:13 +00:00
|
|
|
|
|
|
|
// Initialize the whole system, set hooks to handle server level events.
|
2017-06-09 03:50:35 +00:00
|
|
|
if ((err = svr->initialize(NULL)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "server initialize");
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:46:20 +00:00
|
|
|
// If not daemon, directly run master.
|
|
|
|
if (!_srs_config->get_daemon()) {
|
2017-06-10 06:29:41 +00:00
|
|
|
if ((err = run_master(svr)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "run master");
|
2017-06-09 03:50:35 +00:00
|
|
|
}
|
|
|
|
return srs_success;
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 23:46:20 +00:00
|
|
|
srs_trace("start daemon mode...");
|
2014-03-23 04:42:05 +00:00
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
|
2014-04-16 07:04:38 +00:00
|
|
|
if(pid < 0) {
|
2017-06-09 03:50:35 +00:00
|
|
|
return srs_error_new(-1, "fork father process");
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-03-23 04:42:05 +00:00
|
|
|
// grandpa
|
2014-04-10 06:22:09 +00:00
|
|
|
if(pid > 0) {
|
2014-03-23 04:42:05 +00:00
|
|
|
int status = 0;
|
2017-06-09 03:50:35 +00:00
|
|
|
waitpid(pid, &status, 0);
|
2014-03-23 04:42:05 +00:00
|
|
|
srs_trace("grandpa process exit.");
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-03-23 04:42:05 +00:00
|
|
|
// father
|
|
|
|
pid = fork();
|
|
|
|
|
2014-04-16 07:04:38 +00:00
|
|
|
if(pid < 0) {
|
2017-06-09 03:50:35 +00:00
|
|
|
return srs_error_new(-1, "fork child process");
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-04-10 06:22:09 +00:00
|
|
|
if(pid > 0) {
|
2017-06-09 03:50:35 +00:00
|
|
|
srs_trace("father process exit");
|
2014-03-23 04:42:05 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-03-23 04:42:05 +00:00
|
|
|
// son
|
2019-05-06 23:46:20 +00:00
|
|
|
srs_trace("son(daemon) process running.");
|
2014-03-23 04:42:05 +00:00
|
|
|
|
2017-06-10 06:29:41 +00:00
|
|
|
if ((err = run_master(svr)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "daemon run master");
|
2017-06-09 03:50:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
return err;
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 06:29:41 +00:00
|
|
|
srs_error_t run_master(SrsServer* svr)
|
2014-03-23 04:42:05 +00:00
|
|
|
{
|
2017-06-10 06:29:41 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-03-23 04:42:05 +00:00
|
|
|
|
2017-06-10 06:29:41 +00:00
|
|
|
if ((err = svr->initialize_st()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "initialize st");
|
2014-04-30 03:26:32 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->initialize_signal()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "initialize signal");
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
2014-03-25 03:04:36 +00:00
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->acquire_pid_file()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "acquire pid file");
|
2014-04-12 12:35:40 +00:00
|
|
|
}
|
2014-04-03 10:32:51 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = svr->listen()) != srs_success) {
|
2017-06-10 07:20:48 +00:00
|
|
|
return srs_error_wrap(err, "listen");
|
2014-04-12 12:35:40 +00:00
|
|
|
}
|
2014-03-23 06:40:55 +00:00
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->register_signal()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "register signal");
|
2014-04-30 03:26:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->http_handle()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http handle");
|
2015-03-07 14:25:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->ingest()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "ingest");
|
2014-03-23 06:40:55 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 07:20:48 +00:00
|
|
|
if ((err = svr->cycle()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "main cycle");
|
2014-04-12 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 06:29:41 +00:00
|
|
|
return err;
|
2014-03-23 04:42:05 +00:00
|
|
|
}
|
2014-08-02 14:18:39 +00:00
|
|
|
|