Blame view

trunk/src/main/srs_main_server.cpp 8.8 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_core.hpp>
winlin authored
25 26

#include <stdlib.h>
winlin authored
27 28 29
#include <sys/types.h>
#include <sys/wait.h>
30
#ifdef SRS_AUTO_GPERF_MP
31
    #include <gperftools/heap-profiler.h>
32
#endif
33
#ifdef SRS_AUTO_GPERF_CP
34
    #include <gperftools/profiler.h>
35 36
#endif
37 38 39 40
#include <srs_kernel_error.hpp>
#include <srs_app_server.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
41
#include <srs_kernel_utility.hpp>
42
#include <srs_core_performance.hpp>
43
winlin authored
44 45 46 47 48 49 50
// pre-declare
int run();
int run_master();

// for the main objects(server, config, log, context),
// never subscribe handler in constructor,
// instead, subscribe handler in initialize method.
51 52 53 54 55 56 57
// kernel module.
ISrsLog* _srs_log = new SrsFastLog();
ISrsThreadContext* _srs_context = new SrsThreadContext();
// app module.
SrsConfig* _srs_config = new SrsConfig();
SrsServer* _srs_server = new SrsServer();
58 59 60 61
/**
* show the features by macro, the actual macro values.
*/
void show_macro_features()
62 63
{
#ifdef SRS_AUTO_SSL
64
    srs_trace("check feature rtmp handshake: on");
65
#else
66
    srs_warn("check feature rtmp handshake: off");
67 68 69
#endif

#ifdef SRS_AUTO_HLS
70
    srs_trace("check feature hls: on");
71
#else
72
    srs_warn("check feature hls: off");
73 74 75
#endif

#ifdef SRS_AUTO_HTTP_CALLBACK
76
    srs_trace("check feature http callback: on");
77
#else
78
    srs_warn("check feature http callback: off");
79 80 81
#endif

#ifdef SRS_AUTO_HTTP_API
82
    srs_trace("check feature http api: on");
83
#else
84
    srs_warn("check feature http api: off");
85 86 87
#endif

#ifdef SRS_AUTO_HTTP_SERVER
88
    srs_trace("check feature http server: on");
89
#else
90
    srs_warn("check feature http server: off");
91 92 93
#endif

#ifdef SRS_AUTO_HTTP_PARSER
94
    srs_trace("check feature http parser: on");
95
#else
96
    srs_warn("check feature http parser: off");
97 98 99
#endif

#ifdef SRS_AUTO_DVR
100
    srs_trace("check feature dvr: on");
101
#else
102
    srs_warn("check feature dvr: off");
103 104 105
#endif

#ifdef SRS_AUTO_TRANSCODE
106
    srs_trace("check feature transcode: on");
107
#else
108
    srs_warn("check feature transcode: off");
109 110 111
#endif

#ifdef SRS_AUTO_INGEST
112
    srs_trace("check feature ingest: on");
113
#else
114
    srs_warn("check feature ingest: off");
115 116 117
#endif

#ifdef SRS_AUTO_STAT
118
    srs_trace("check feature system stat: on");
119
#else
120
    srs_warn("check feature system stat: off");
121 122 123
#endif

#ifdef SRS_AUTO_NGINX
124
    srs_trace("check feature compile nginx: on");
125
#else
126
    srs_warn("check feature compile nginx: off");
127 128
#endif
129
#ifdef SRS_AUTO_FFMPEG_TOOL
130
    srs_trace("check feature compile ffmpeg: on");
131
#else
132 133
    srs_warn("check feature compile ffmpeg: off");
#endif
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

#ifdef SRS_PERF_MERGED_READ
    srs_trace("MR(merged-read): on, @see %s", RTMP_SIG_SRS_ISSUES(241));
#else
    srs_warn("MR(merged-read): off, @see %s", RTMP_SIG_SRS_ISSUES(241));
#endif

    srs_trace("MR(merged-read) default %d sleep %d", SRS_PERF_MR_ENABLED, SRS_PERF_MR_SLEEP);
    srs_trace("MW(merged-write) default sleep %d", SRS_PERF_MW_SLEEP);
    srs_trace("read chunk stream cache cid [0, %d)", SRS_PERF_CHUNK_STREAM_CACHE);
    srs_trace("default gop cache %d, play queue %ds", SRS_PERF_GOP_CACHE, SRS_PERF_PLAY_QUEUE);

    int possible_mr_latency = 0;
#ifdef SRS_PERF_MERGED_READ
    possible_mr_latency = SRS_PERF_MR_SLEEP;
#endif
    srs_trace("system default latency in ms: mw(0-%d) + mr(0-%d) + play-queue(0-%d)",
        SRS_PERF_MW_SLEEP, possible_mr_latency, SRS_PERF_PLAY_QUEUE*1000);
152 153 154 155 156
}

void check_macro_features()
{
    // for special features.
157 158 159 160
#ifndef SRS_PERF_MERGED_READ
    srs_warn("MR(merged-read) is disabled, hurts read performance. @see %s", RTMP_SIG_SRS_ISSUES(241));
#endif
161 162
    srs_trace("writev limits write %d iovs a time", sysconf(_SC_IOV_MAX));
163
#if VERSION_MAJOR > 1
164
    #warning "using develop SRS, please use release instead."
165
    srs_warn("SRS %s is develop branch, please use %s instead", RTMP_SIG_SRS_VERSION, RTMP_SIG_SRS_RELEASE);
166 167 168
#endif
}
169 170 171
/**
* main entrance.
*/
172
int main(int argc, char** argv) 
winlin authored
173
{
174
    int ret = ERROR_SUCCESS;
175
176 177 178
    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());
179 180
    // for gperf gmp or gcp, 
    // should never enable it when not enabled for performance issue.
181
#ifdef SRS_AUTO_GPERF_MP
182 183
    HeapProfilerStart("gperf.srs.gmp");
#endif
184
#ifdef SRS_AUTO_GPERF_CP
185 186 187
    ProfilerStart("gperf.srs.gcp");
#endif
188
    // directly compile error when these two macro defines.
189
#if defined(SRS_AUTO_GPERF_MC) && defined(SRS_AUTO_GPERF_MP)
190
    #error ("option --with-gmc confict with --with-gmp, "
191 192 193 194
        "@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");
#endif
winlin authored
195
    
196 197 198
    // never use srs log(srs_trace, srs_error, etc) before config parse the option,
    // which will load the log config and apply it.
    if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
winlin authored
199 200 201
        return ret;
    }
    
202 203
    // config parsed, initialize log.
    if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
204 205
        return ret;
    }
206 207 208 209 210

    // we check the config when the log initialized.
    if ((ret = _srs_config->check_config()) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
211
    
212
    srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
winlin authored
213
    srs_trace("license: "RTMP_SIG_SRS_LICENSE);
214 215
    srs_trace("primary: "RTMP_SIG_SRS_PRIMARY);
    srs_trace("authors: "RTMP_SIG_SRS_AUTHROS);
216
    srs_trace("contributors: "SRS_AUTO_CONSTRIBUTORS);
217 218
    srs_trace("uname: "SRS_AUTO_UNAME);
    srs_trace("build: %s, %s", SRS_AUTO_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
219 220
    srs_trace("configure: "SRS_AUTO_USER_CONFIGURE);
    srs_trace("features: "SRS_AUTO_CONFIGURE);
221
#ifdef SRS_AUTO_ARM_UBUNTU12
winlin authored
222
    srs_trace("arm tool chain: "SRS_AUTO_EMBEDED_TOOL_CHAIN);
223
#endif
224
    srs_trace("conf: %s, limit: %d", _srs_config->config().c_str(), _srs_config->get_max_connections());
225
    
226
    // features
227
    show_macro_features();
228
    check_macro_features();
229
    
winlin authored
230 231 232 233 234
    /**
    * we do nothing in the constructor of server,
    * and use initialize to create members, set hooks for instance the reload handler,
    * all initialize will done in this stage.
    */
235
    if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
236 237 238
        return ret;
    }
    
239
    return run();
winlin authored
240
}
winlin authored
241
242
int run()
winlin authored
243 244 245 246 247 248 249 250 251 252
{
    // if not deamon, directly run master.
    if (!_srs_config->get_deamon()) {
        return run_master();
    }
    
    srs_trace("start deamon mode...");
    
    int pid = fork();
    
253
    if(pid < 0) {
winlin authored
254 255 256 257 258
        srs_error("create process error. ret=-1"); //ret=0
        return -1;
    }

    // grandpa
259
    if(pid > 0) {
winlin authored
260
        int status = 0;
261
        if(waitpid(pid, &status, 0) == -1) {
winlin authored
262 263 264 265 266 267 268 269 270
            srs_error("wait child process error! ret=-1"); //ret=0
        }
        srs_trace("grandpa process exit.");
        exit(0);
    }

    // father
    pid = fork();
    
271 272
    if(pid < 0) {
        srs_error("create process error. ret=0");
winlin authored
273 274 275
        return -1;
    }
276
    if(pid > 0) {
277
        srs_trace("father process exit. ret=0");
winlin authored
278 279 280 281 282 283 284 285 286
        exit(0);
    }

    // son
    srs_trace("son(deamon) process running.");
    
    return run_master();
}
287
int run_master()
winlin authored
288 289 290
{
    int ret = ERROR_SUCCESS;
    
291 292 293
    if ((ret = _srs_server->initialize_signal()) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
294
    
295
    if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
winlin authored
296 297
        return ret;
    }
298
    
299 300 301
    if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
        return ret;
    }
302
    
303 304 305
    if ((ret = _srs_server->listen()) != ERROR_SUCCESS) {
        return ret;
    }
306
    
307 308 309 310
    if ((ret = _srs_server->register_signal()) != ERROR_SUCCESS) {
        return ret;
    }
    
311
    if ((ret = _srs_server->ingest()) != ERROR_SUCCESS) {
312 313 314
        return ret;
    }
    
315 316 317 318 319
    if ((ret = _srs_server->cycle()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return 0;
winlin authored
320
}
321